Skip to content

Commit

Permalink
refactor: replace err-code with CodeError
Browse files Browse the repository at this point in the history
Replaces [err-code](https://github.com/IndigoUnited/js-err-code/blob/master/index.js) with [CodeError](libp2p/js-libp2p-interfaces#314)

Related: [js-libp2p#1269](libp2p/js-libp2p#1269)

Changes

- removes err-code from dependencies
- adds @libp2p/[email protected] to dependencies
- uses CodeError in place of err-code
  • Loading branch information
tabcat committed Jan 9, 2023
1 parent b93f4c0 commit 6390ba9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-metrics": "^4.0.0",
"@libp2p/interface-transport": "^2.0.0",
"@libp2p/interfaces": "^3.0.3",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/utils": "^3.0.2",
"@multiformats/mafmt": "^11.0.3",
"@multiformats/multiaddr": "^11.0.0",
"err-code": "^3.0.1",
"stream-to-it": "^0.2.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import net from 'net'
import * as mafmt from '@multiformats/mafmt'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { logger } from '@libp2p/logger'
import { toMultiaddrConnection } from './socket-to-conn.js'
import { TCPListener } from './listener.js'
Expand Down Expand Up @@ -157,7 +157,7 @@ class TCP implements Transport {
log('connection timeout %s', cOptsStr)
this.metrics?.dialerEvents.increment({ timeout: true })

const err = errCode(new Error(`connection timeout after ${Date.now() - start}ms`), 'ERR_CONNECT_TIMEOUT')
const err = new CodeError(`connection timeout after ${Date.now() - start}ms`, 'ERR_CONNECT_TIMEOUT')
// Note: this will result in onError() being called
rawSocket.emit('error', err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/socket-to-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import toIterable from 'stream-to-it'
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr'
import { CLOSE_TIMEOUT, SOCKET_TIMEOUT } from './constants.js'
import { multiaddrToNetConfig } from './utils.js'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import type { Socket } from 'net'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { MultiaddrConnection } from '@libp2p/interface-connection'
Expand Down Expand Up @@ -49,7 +49,7 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
if (socket.remoteAddress == null || socket.remotePort == null) {
// this can be undefined if the socket is destroyed (for example, if the client disconnected)
// https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketremoteaddress
throw errCode(new Error('Could not determine remote address or port'), 'ERR_NO_REMOTE_ADDRESS')
throw new CodeError('Could not determine remote address or port', 'ERR_NO_REMOTE_ADDRESS')
}

remoteAddr = toMultiaddr(socket.remoteAddress, socket.remotePort)
Expand All @@ -68,7 +68,7 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
// only destroy with an error if the remote has not sent the FIN message
let err: Error | undefined
if (socket.readable) {
err = errCode(new Error('Socket read timeout'), 'ERR_SOCKET_READ_TIMEOUT')
err = new CodeError('Socket read timeout', 'ERR_SOCKET_READ_TIMEOUT')
}

// if the socket times out due to inactivity we must manually close the connection
Expand Down Expand Up @@ -140,7 +140,7 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
log('%s socket close timeout after %dms, destroying it manually', lOptsStr, Date.now() - start)

// will trigger 'error' and 'close' events that resolves promise
socket.destroy(errCode(new Error('Socket close timeout'), 'ERR_SOCKET_CLOSE_TIMEOUT'))
socket.destroy(new CodeError('Socket close timeout', 'ERR_SOCKET_CLOSE_TIMEOUT'))
}
}, closeTimeout).unref()

Expand Down

0 comments on commit 6390ba9

Please sign in to comment.