Skip to content

Conversation

@rekmarks
Copy link
Member

@rekmarks rekmarks commented Nov 6, 2024

Ref: #218

Adds a new class StreamMultiplexer to @ocap/streams. A multiplexer is not a stream itself, but rather a wrapper around a duplex stream. The multiplexer provides methods for creating "channels" over the underlying stream, which are themselves duplex streams and may have a different message type and validation logic.

The multiplexer is constructed in an idle state, and must be explicitly "started" via the start() or drainAll() methods. All channels must be added before the multiplexer is started.

Starting the multiplexer will synchronize the underlying duplex stream, if it is synchronizable. Therefore, in order to prevent message loss, callers should not synchronize the underlying duplex stream before passing it to the multiplexer. For the same reason, the multiplexer will throw if any channels are added after it has started. We could enable "dynamic" channel creation by synchronization the channels themselves, but only if there's a clear need.

See tests for example usage.

Also tidy up various asynchronous operations in base stream classes.
These were causing promises returned from the stream multiplexer to
fulfill erratically.
Get rid of the `makeChannels()` contrivance in favor of exposing
a singular `addChannel()`. Also expose `start()` function in order to
enable either "manually" draining channels or using the `drainAll()`
method.
Also make `synchronize()` method of duplex streams optional.
Also fix issues in tests following rebase.
@rekmarks rekmarks requested a review from a team as a code owner November 6, 2024 17:28
@rekmarks rekmarks force-pushed the rekm/3-stream-multiplexer branch from b0b0d32 to bac8db2 Compare November 7, 2024 05:15
Copy link
Contributor

@sirtimid sirtimid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! 👌

Comment on lines +172 to +173
// eslint-disable-next-line @typescript-eslint/await-thenable
await null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't we have a rule to allow this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it didn't get merged, and I think it's fine to disable it when necessary? If someone feels strongly about it (cc: @FUDCo) I'm down to just disable it in our lint config.

@rekmarks rekmarks merged commit f50650a into main Nov 7, 2024
@rekmarks rekmarks deleted the rekm/3-stream-multiplexer branch November 7, 2024 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants