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

Commit c5872e2

Browse files
committed
chore: replace err-code with CodeError
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 c5872e2

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
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-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import net from 'net'
22
import * as mafmt from '@multiformats/mafmt'
3-
import errCode from 'err-code'
3+
import { CodeError } from '@libp2p/interfaces/errors'
44
import { logger } from '@libp2p/logger'
55
import { toMultiaddrConnection } from './socket-to-conn.js'
66
import { TCPListener } from './listener.js'
@@ -157,7 +157,7 @@ class TCP implements Transport {
157157
log('connection timeout %s', cOptsStr)
158158
this.metrics?.dialerEvents.increment({ timeout: true })
159159

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

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)