-
-
Notifications
You must be signed in to change notification settings - Fork 32k
stream: simpler Readable async iterator #34035
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 all commits
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const Readable = require('stream').Readable; | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e5], | ||
sync: ['yes', 'no'], | ||
}); | ||
|
||
async function main({ n, sync }) { | ||
sync = sync === 'yes'; | ||
|
||
const s = new Readable({ | ||
objectMode: true, | ||
read() { | ||
if (sync) { | ||
this.push(1); | ||
} else { | ||
process.nextTick(() => { | ||
this.push(1); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
bench.start(); | ||
|
||
let x = 0; | ||
for await (const chunk of s) { | ||
x += chunk; | ||
if (x > n) { | ||
break; | ||
} | ||
} | ||
|
||
// Side effect to ensure V8 does not optimize away the | ||
// loop as a noop. | ||
console.log(x); | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bench.end(n); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ const { | |
NumberIsNaN, | ||
ObjectDefineProperties, | ||
ObjectSetPrototypeOf, | ||
Promise, | ||
Set, | ||
SymbolAsyncIterator, | ||
Symbol | ||
|
@@ -59,11 +60,11 @@ const kPaused = Symbol('kPaused'); | |
|
||
// Lazy loaded to improve the startup performance. | ||
let StringDecoder; | ||
let createReadableStreamAsyncIterator; | ||
let from; | ||
|
||
ObjectSetPrototypeOf(Readable.prototype, Stream.prototype); | ||
ObjectSetPrototypeOf(Readable, Stream); | ||
function nop() {} | ||
|
||
const { errorOrDestroy } = destroyImpl; | ||
|
||
|
@@ -1088,13 +1089,68 @@ Readable.prototype.wrap = function(stream) { | |
}; | ||
|
||
Readable.prototype[SymbolAsyncIterator] = function() { | ||
if (createReadableStreamAsyncIterator === undefined) { | ||
createReadableStreamAsyncIterator = | ||
require('internal/streams/async_iterator'); | ||
let stream = this; | ||
|
||
if (typeof stream.read !== 'function') { | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// v1 stream | ||
const src = stream; | ||
stream = new Readable({ | ||
objectMode: true, | ||
destroy(err, callback) { | ||
destroyImpl.destroyer(src, err); | ||
callback(); | ||
} | ||
}).wrap(src); | ||
} | ||
return createReadableStreamAsyncIterator(this); | ||
|
||
const iter = createAsyncIterator(stream); | ||
iter.stream = stream; | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return iter; | ||
}; | ||
|
||
async function* createAsyncIterator(stream) { | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let callback = nop; | ||
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. I think this implementation is mostly correct in terms of error/readable timings. Just timings when reviewing of readable/error:
|
||
|
||
function next(resolve) { | ||
if (this === stream) { | ||
callback(); | ||
callback = nop; | ||
} else { | ||
callback = resolve; | ||
} | ||
} | ||
|
||
stream | ||
.on('readable', next) | ||
.on('error', next) | ||
.on('end', next) | ||
.on('close', next); | ||
|
||
try { | ||
const state = stream._readableState; | ||
while (true) { | ||
const chunk = stream.read(); | ||
if (chunk !== null) { | ||
yield chunk; | ||
} else if (state.errored) { | ||
throw state.errored; | ||
} else if (state.ended) { | ||
break; | ||
} else if (state.closed) { | ||
// TODO(ronag): ERR_PREMATURE_CLOSE? | ||
break; | ||
} else { | ||
await new Promise(next); | ||
} | ||
} | ||
} catch (err) { | ||
destroyImpl.destroyer(stream, err); | ||
throw err; | ||
} finally { | ||
destroyImpl.destroyer(stream, null); | ||
} | ||
} | ||
|
||
// Making it explicit these properties are not enumerable | ||
// because otherwise some prototype manipulation in | ||
// userland will fail. | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ async function testMutualDestroy() { | |
break; | ||
} | ||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
break; | ||
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. Note this, I'm confused how this could have passed before. Was it a bug? |
||
} | ||
|
||
assert.deepStrictEqual(iteratedLines, expectedLines); | ||
|
Uh oh!
There was an error while loading. Please reload this page.