Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
stream: add Transform.by utility function (updated)#38696
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d9575419005e46944f22884c84c9a1937c7ad353828d82c5b7a1ef775707445970ed3d6e67a8598aebc5eee19c526e96be185a6f48e73c51b73347f9789a5bbad0bfdf2c8b22001fe019545ee0f0fa8b6b504b15447a8952fe96e41115d52a8151fe3049db33ce77ac59791d06480563307e926382b9b558dc92db4b5441a985441e0c1f0ec5c862dadc6059967989e6ccafab9bccc23File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -73,6 +73,8 @@ const { | ||
| ERR_METHOD_NOT_IMPLEMENTED | ||
| } = require('internal/errors').codes; | ||
| const Duplex = require('internal/streams/duplex'); | ||
| const from = require('internal/streams/from'); | ||
| const { createDeferredPromise } = require('internal/util'); | ||
| ObjectSetPrototypeOf(Transform.prototype, Duplex.prototype); | ||
| ObjectSetPrototypeOf(Transform, Duplex); | ||
| @@ -244,3 +246,25 @@ Transform.prototype._read = function() { | ||
| callback(); | ||
| } | ||
| }; | ||
| Transform.by = function by(asyncGeneratorFn, options) { | ||
| let { promise, resolve } = createDeferredPromise(); | ||
safareli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const asyncGenerator = async function*() { | ||
| while (true) { | ||
| const { chunk, done, encoding, cb } = await promise; | ||
| process.nextTick(cb); | ||
safareli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (done) return; | ||
| yield { chunk, encoding }; | ||
| ({ promise, resolve } = createDeferredPromise()); | ||
| } | ||
| }(); | ||
| return from(Duplex, asyncGeneratorFn(asyncGenerator), { | ||
| objectMode: true, | ||
| autoDestroy: true, | ||
| ...options, | ||
| write: (chunk, encoding, cb) => { | ||
| resolve({ chunk, done: false, encoding, cb }); | ||
| }, | ||
| final: (cb) => resolve({ done: true, cb }) | ||
| }); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.