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

fix: avoid Multiaddr.getPath() performance issue #264

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"@libp2p/logger": "^2.0.0",
"@libp2p/utils": "^3.0.2",
"@multiformats/mafmt": "^12.0.0",
"@multiformats/multiaddr": "^12.0.0",
"@multiformats/multiaddr": "^12.1.2",
"stream-to-it": "^0.2.2"
},
"devDependencies": {
Expand Down
10 changes: 6 additions & 4 deletions src/socket-to-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
options.localAddr = options.remoteAddr
}

let lOptsStr: string
let remoteAddr: Multiaddr

if (options.remoteAddr != null) {
remoteAddr = options.remoteAddr
const lOpts = multiaddrToNetConfig(remoteAddr)
lOptsStr = lOpts.path ?? `${lOpts.host ?? ''}:${lOpts.port ?? ''}`
} else {
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 new CodeError('Could not determine remote address or port', 'ERR_NO_REMOTE_ADDRESS')
}

remoteAddr = toMultiaddr(socket.remoteAddress, socket.remotePort)
const { remoteAddress, remotePort } = socket
remoteAddr = toMultiaddr(remoteAddress, remotePort)
lOptsStr = `${remoteAddress ?? ''}:${remotePort ?? ''}`
}

const lOpts = multiaddrToNetConfig(remoteAddr)
const lOptsStr = lOpts.path ?? `${lOpts.host ?? ''}:${lOpts.port ?? ''}`
const { sink, source } = toIterable.duplex(socket)

// by default there is no timeout
Expand Down