-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
stream-whatwg: add whatwg streams #22352
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -988,6 +988,29 @@ Readable.prototype[Symbol.asyncIterator] = function() { | |
| return new ReadableAsyncIterator(this); | ||
| }; | ||
|
|
||
| Readable.prototype.acquireStandardStream = function() { | ||
| emitExperimentalWarning('Readable.acquireStandardStream'); | ||
| return new ReadableStream({ | ||
| start: (controller) => { | ||
| this.pause(); | ||
| this.on('data', (chunk) => { | ||
| controller.enqueue(chunk); | ||
| this.pause(); | ||
| }); | ||
| this.once('end', () => controller.close()); | ||
| this.once('error', (e) => controller.error(e)); | ||
| }, | ||
|
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. @mcollina... is this the best way of doing this? Should this use
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. IMHO we should use the |
||
| pull: () => { | ||
| this.resume(); | ||
| }, | ||
| cancel: () => { | ||
| this.destroy(); | ||
| }, | ||
| }, { | ||
| highWaterMark: this.readableHighWaterMark, | ||
| }); | ||
| }; | ||
|
|
||
| Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { | ||
| // making it explicit this property is not enumerable | ||
| // because otherwise some prototype manipulation in | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,7 @@ const { | |
| } = require('internal/errors').codes; | ||
| const Duplex = require('_stream_duplex'); | ||
| const util = require('util'); | ||
| const { emitExperimentalWarning } = require('internal/util'); | ||
| util.inherits(Transform, Duplex); | ||
|
|
||
|
|
||
|
|
@@ -202,6 +203,32 @@ Transform.prototype._destroy = function(err, cb) { | |
| }); | ||
| }; | ||
|
|
||
| Transform.prototype.acquireStandardStream = function() { | ||
| emitExperimentalWarning('Transform.acquireStandardStream'); | ||
| return new TransformStream({ | ||
| start: (controller) => { | ||
| this.on('data', (chunk) => { | ||
| controller.enqueue(chunk); | ||
| }); | ||
| this.once('end', () => controller.close()); | ||
| this.once('error', (e) => controller.error(e)); | ||
|
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. listening to the error event with
Member
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. whatwg streams only error once |
||
| }, | ||
| transform: (chunk) => { | ||
| return new Promise((resolve) => { | ||
| const underHighWaterMark = this.write(chunk); | ||
| if (!underHighWaterMark) { | ||
| this.once('drain', resolve); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }, | ||
| flush: () => { | ||
| this.end(); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| function done(stream, er, data) { | ||
| if (er) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -591,6 +591,33 @@ Writable.prototype.end = function(chunk, encoding, cb) { | |
| return this; | ||
| }; | ||
|
|
||
| Writable.prototype.acquireStandardStream = function() { | ||
|
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. How are errors propagated here? There's nothing listening to the error event. |
||
| internalUtil.emitExperimentalWarning('Writable.acquireStandardStream'); | ||
| return new WritableStream({ | ||
| start: (controller) => { | ||
| this.once('error', (e) => controller.error(e)); | ||
| }, | ||
| write: (chunk) => { | ||
| return new Promise((resolve) => { | ||
| const underHighWaterMark = this.write(chunk); | ||
| if (!underHighWaterMark) { | ||
| this.once('drain', resolve); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }, | ||
| close: (controller) => { | ||
| this.end(); | ||
| }, | ||
| abort: (reason) => { | ||
| this.destroy(reason); | ||
| }, | ||
| }, { | ||
| highWaterMark: this.writableHighWaterMark, | ||
| }); | ||
| }; | ||
|
|
||
| Object.defineProperty(Writable.prototype, 'writableLength', { | ||
| // making it explicit this property is not enumerable | ||
| // because otherwise some prototype manipulation in | ||
|
|
||
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.
Not sure about calling these "...StandardStream"... Don't really have a better suggestion right now tho
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.
This will definitely need more documentation about the new objects
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.
Another thing to keep in mind is that the
readable-streamsmodule will need to be able to implement support for this at some point. While that lags, these methods will not be implemented and will not be supported in an extremely large number of streams out there in the wild. It might be better to consider exposing these differently.This should allow it to work with any object that implements the right methods and events.
Uh oh!
There was an error while loading. Please reload this page.
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.
i don't really understand this. a polyfill of whatwg streams shouldn't have a concept of node streams.
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.
The
readable-streamsmodule provides an independently usable implementation of Node.js streams. This is adding new methods to the base class ofrequire('stream').Readableandrequire('stream').Writablethat will not be present in the implementation provided by thereadable-streamsmodule, nor will it be possible to implement those methods for every browser currently supported byreadable-streams. Streams created byreadable-streamstoday can be used within Node.js just as if they were the built-in streams.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.
The point is this can be done in a way where this isn't even a problem by not extending the prototype.
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.
sure it can, but I'm trying to design a good API, not a easy-for-some-npm-module-to-not-worry-about API.
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.
"good" is entirely subjective here. Others will likely have very different opinions of what is "good".
The "readable-streams" module is not just "some-npm-module", it's the most downloaded module in the npm ecosystem and is part of the Node.js project managed by the Streams WG. Given that, I'm very concerned about implementation decisions that directly impact it.
Yes, we're able to add features to Node.js core before they propagate down to the module, but we must do so carefully and deliberately, weighing each choice on the impact it may have.
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm hopeful we can come up with something that isn't importing separate functions. I want to have as little friction as possible for people wanting to use whatwg streams, as there are quite a lot of locations where the interop needs to happen. I'm willing to continue bikeshedding this for as long as we need to for something that works for everyone, as it would seem we both have constraints here.
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.
There are some relevant comments here that are being buried a bit after rebasing... just linking them here so that they don't get lost in the conversation: 1ca4477#r211124305 and 1ca4477#r211131273