-
Notifications
You must be signed in to change notification settings - Fork 537
fix: handle terminal middleware without mutating chains #3534
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
base: main
Are you sure you want to change the base?
Changes from all commits
871939d
fca0203
28e2e95
0a21f94
a7dab76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,12 @@ export interface StreamHandler { | |
|
|
||
| /** | ||
| * Stream middleware allows accessing stream data outside of the stream handler | ||
| * | ||
| * Return false to stop the middleware chain without aborting the stream. | ||
| * Throw or reject to abort the stream. | ||
| */ | ||
| export interface StreamMiddleware { | ||
| (stream: Stream, connection: Connection, next: (stream: Stream, connection: Connection) => void): void | Promise<void> | ||
| (stream: Stream, connection: Connection, next: (stream: Stream, connection: Connection) => void): void | false | Promise<void | false> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be false - kind of want false to be an explicit option to cancel the middleware... or could make it true & void do the same thing which feels a bit off
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking about cases where the function returns some boolean logic. would have to coerce to void or false to stop or not stop the middleware while also fitting the type constraint. |
||
| } | ||
|
|
||
| export interface StreamHandlerOptions extends AbortOptions { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,7 +250,11 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement | |
| throw new LimitedConnectionError('Cannot open protocol stream on limited connection') | ||
| } | ||
|
|
||
| const middleware = this.components.registrar.getMiddleware(muxedStream.protocol) | ||
| // Copy registered middleware before appending the handler wrapper below; | ||
| // the registered middleware array is reused across streams. | ||
| const middleware = [ | ||
| ...this.components.registrar.getMiddleware(muxedStream.protocol) | ||
| ] | ||
|
|
||
| middleware.push(async (stream, connection, next) => { | ||
| await handler(stream, connection) | ||
|
|
@@ -268,22 +272,14 @@ export class Connection extends TypedEventEmitter<MessageStreamEvents> implement | |
| const mw = middleware[i] | ||
| stream.log.trace('running middleware', i, mw) | ||
|
|
||
| // eslint-disable-next-line no-loop-func | ||
| await new Promise<void>((resolve, reject) => { | ||
| try { | ||
| const result = mw(stream, connection, (s, c) => { | ||
| stream = s | ||
| connection = c | ||
| resolve() | ||
| }) | ||
|
|
||
| if (result instanceof Promise) { | ||
| result.catch(reject) | ||
| } | ||
| } catch (err) { | ||
| reject(err) | ||
| } | ||
| }) | ||
| const result = await runMiddleware(mw, stream, connection) | ||
| stream = result.stream | ||
| connection = result.connection | ||
|
|
||
| if (result.stop) { | ||
| stream.log.trace('middleware stopped chain', i, mw) | ||
| break | ||
| } | ||
|
|
||
| stream.log.trace('ran middleware', i, mw) | ||
| } | ||
|
|
@@ -353,6 +349,40 @@ function findOutgoingStreamLimit (protocol: string, registrar: Registrar, option | |
| return options.maxOutboundStreams ?? DEFAULT_MAX_OUTBOUND_STREAMS | ||
| } | ||
|
|
||
| interface RunMiddlewareResult { | ||
| stream: Stream | ||
| connection: ConnectionInterface | ||
| stop: boolean | ||
| } | ||
|
|
||
| function runMiddleware (mw: StreamMiddleware, stream: Stream, connection: ConnectionInterface): Promise<RunMiddlewareResult> { | ||
| return new Promise<RunMiddlewareResult>((resolve, reject) => { | ||
| const continueChain = (s: Stream, c: ConnectionInterface): void => { | ||
| resolve({ stream: s, connection: c, stop: false }) | ||
| } | ||
|
|
||
| const stopChain = (): void => { | ||
| resolve({ stream, connection, stop: true }) | ||
| } | ||
|
|
||
| try { | ||
| const result = mw(stream, connection, continueChain) | ||
|
|
||
| if (result === false) { | ||
| stopChain() | ||
| } else if (result != null) { | ||
| result.then(result => { | ||
| if (result === false) { | ||
| stopChain() | ||
| } | ||
| }).catch(reject) | ||
| } | ||
|
Comment on lines
+371
to
+379
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just await |
||
| } catch (err) { | ||
| reject(err) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function countStreams (protocol: string, direction: 'inbound' | 'outbound', connection: Connection): number { | ||
| let streamCount = 0 | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could said returning true or void lets the middleware continue.