Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return empty address list during listen operation #2904

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
10 changes: 5 additions & 5 deletions packages/transport-websockets/src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,20 @@ export class WebSocketListener extends TypedEventEmitter<ListenerEvents> impleme
}

getAddrs (): Multiaddr[] {
if (this.listeningMultiaddr == null) {
throw new Error('Listener is not ready yet')
}

const address = this.server.address()

if (address == null) {
throw new Error('Listener is not ready yet')
return []
}

if (typeof address === 'string') {
throw new Error('Wrong address type received - expected AddressInfo, got string - are you trying to listen on a unix socket?')
}

if (this.listeningMultiaddr == null) {
throw new Error('Listener is not ready yet')
}

const options = this.listeningMultiaddr.toOptions()
const multiaddrs: Multiaddr[] = []

Expand Down
16 changes: 16 additions & 0 deletions packages/transport-websockets/test/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ describe('listen', () => {
void listener.listen(ma)
})

it('should return an empty address list when `getAddrs` called before listening has finished', async () => {
listener = ws.createListener({ upgrader })

void listener.listen(ma)

// call getAddrs before sockets have opened
expect(listener.getAddrs()).to.be.empty()
})

it('should throw when `.getAddrs` called before `.listen`', async () => {
listener = ws.createListener({ upgrader })

// call getAddrs before sockets have opened
expect(() => listener.getAddrs()).to.throw(/not ready/)
})

it('should error on starting two listeners on same address', async () => {
listener = ws.createListener({ upgrader })
const dumbServer = http.createServer()
Expand Down
Loading