Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 5c44562

Browse files
authored
chore: replace err-code with CodeError (#240)
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/interfaces@3.2.0 to dependencies - uses CodeError in place of err-code
1 parent b93f4c0 commit 5c44562

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,11 @@
144144
"@libp2p/interface-connection": "^3.0.2",
145145
"@libp2p/interface-metrics": "^4.0.0",
146146
"@libp2p/interface-transport": "^2.0.0",
147-
"@libp2p/interfaces": "^3.0.3",
147+
"@libp2p/interfaces": "^3.2.0",
148148
"@libp2p/logger": "^2.0.0",
149149
"@libp2p/utils": "^3.0.2",
150150
"@multiformats/mafmt": "^11.0.3",
151151
"@multiformats/multiaddr": "^11.0.0",
152-
"err-code": "^3.0.1",
153152
"stream-to-it": "^0.2.2"
154153
},
155154
"devDependencies": {

src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import net from 'net'
22
import * as mafmt from '@multiformats/mafmt'
3-
import errCode from 'err-code'
43
import { logger } from '@libp2p/logger'
54
import { toMultiaddrConnection } from './socket-to-conn.js'
65
import { TCPListener } from './listener.js'
76
import { multiaddrToNetConfig } from './utils.js'
8-
import { AbortError } from '@libp2p/interfaces/errors'
7+
import { AbortError, CodeError } from '@libp2p/interfaces/errors'
98
import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'
109
import { CreateListenerOptions, DialOptions, Listener, symbol, Transport } from '@libp2p/interface-transport'
1110
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
@@ -157,7 +156,7 @@ class TCP implements Transport {
157156
log('connection timeout %s', cOptsStr)
158157
this.metrics?.dialerEvents.increment({ timeout: true })
159158

160-
const err = errCode(new Error(`connection timeout after ${Date.now() - start}ms`), 'ERR_CONNECT_TIMEOUT')
159+
const err = new CodeError(`connection timeout after ${Date.now() - start}ms`, 'ERR_CONNECT_TIMEOUT')
161160
// Note: this will result in onError() being called
162161
rawSocket.emit('error', err)
163162
}

src/socket-to-conn.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import toIterable from 'stream-to-it'
44
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr'
55
import { CLOSE_TIMEOUT, SOCKET_TIMEOUT } from './constants.js'
66
import { multiaddrToNetConfig } from './utils.js'
7-
import errCode from 'err-code'
7+
import { CodeError } from '@libp2p/interfaces/errors'
88
import type { Socket } from 'net'
99
import type { Multiaddr } from '@multiformats/multiaddr'
1010
import type { MultiaddrConnection } from '@libp2p/interface-connection'
@@ -49,7 +49,7 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
4949
if (socket.remoteAddress == null || socket.remotePort == null) {
5050
// this can be undefined if the socket is destroyed (for example, if the client disconnected)
5151
// https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketremoteaddress
52-
throw errCode(new Error('Could not determine remote address or port'), 'ERR_NO_REMOTE_ADDRESS')
52+
throw new CodeError('Could not determine remote address or port', 'ERR_NO_REMOTE_ADDRESS')
5353
}
5454

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

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

142142
// will trigger 'error' and 'close' events that resolves promise
143-
socket.destroy(errCode(new Error('Socket close timeout'), 'ERR_SOCKET_CLOSE_TIMEOUT'))
143+
socket.destroy(new CodeError('Socket close timeout', 'ERR_SOCKET_CLOSE_TIMEOUT'))
144144
}
145145
}, closeTimeout).unref()
146146

0 commit comments

Comments
 (0)