Skip to content

feat: allow async stream handlers #3212

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions packages/interface-compliance-tests/src/mocks/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectio
mss.handle(muxedStream, registrar.getProtocols(), {
log
})
.then(({ stream, protocol }) => {
.then(async ({ stream, protocol }) => {
log('%s: incoming stream opened on %s', direction, protocol)
muxedStream.protocol = protocol
muxedStream.sink = stream.sink
Expand All @@ -152,7 +152,7 @@ export function mockConnection (maConn: MultiaddrConnection, opts: MockConnectio
connection.streams.push(muxedStream)
const { handler } = registrar.getHandler(protocol)

handler({ connection, stream: muxedStream })
await handler({ connection, stream: muxedStream })
}).catch(err => {
log.error(err)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/interface/src/stream-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface StreamHandler {
/**
* A callback function that accepts the incoming stream data
*/
(data: IncomingStreamData): void
(data: IncomingStreamData): void | Promise<void>
}

export interface StreamHandlerOptions extends AbortOptions {
Expand Down
14 changes: 4 additions & 10 deletions packages/libp2p/src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,11 @@ export class Upgrader implements UpgraderInterface {

this.components.metrics?.trackProtocolStream(muxedStream, connection)

this._onStream({ connection, stream: muxedStream, protocol })
await this._onStream({ connection, stream: muxedStream, protocol })
})
.catch(async err => {
connection.log.error('error handling incoming stream id %s - %e', muxedStream.id, err)

if (muxedStream.timeline.close == null) {
await muxedStream.close({
signal
})
.catch(err => muxedStream.abort(err))
}
muxedStream.abort(err)
})
}
})
Expand Down Expand Up @@ -651,15 +645,15 @@ export class Upgrader implements UpgraderInterface {
/**
* Routes incoming streams to the correct handler
*/
_onStream (opts: OnStreamOptions): void {
async _onStream (opts: OnStreamOptions): Promise<void> {
const { connection, stream, protocol } = opts
const { handler, options } = this.components.registrar.getHandler(protocol)

if (connection.limits != null && options.runOnLimitedConnection !== true) {
throw new LimitedConnectionError('Cannot open protocol stream on limited connection')
}

handler({ connection, stream })
await handler({ connection, stream })
}

/**
Expand Down