Skip to content

Commit

Permalink
stream: fix fromAsyncGen
Browse files Browse the repository at this point in the history
Fixes: #40497
  • Loading branch information
ronag committed Oct 18, 2021
1 parent c0a7020 commit 5c8aa57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/internal/streams/duplexify.js
Expand Up @@ -210,11 +210,13 @@ function fromAsyncGen(fn) {
const value = fn(async function*() {
while (true) {
const { chunk, done, cb } = await promise;
promise = null;
resolve = null;
process.nextTick(cb);
if (done) return;
if (signal.aborted) throw new AbortError();
yield chunk;
({ promise, resolve } = createDeferredPromise());
yield chunk;
}
}(), { signal });

Expand Down
33 changes: 32 additions & 1 deletion test/parallel/test-stream-duplex-from.js
Expand Up @@ -2,7 +2,7 @@

const common = require('../common');
const assert = require('assert');
const { Duplex, Readable, Writable } = require('stream');
const { Duplex, Readable, Writable, pipeline } = require('stream');

{
const d = Duplex.from({
Expand Down Expand Up @@ -118,3 +118,34 @@ const { Duplex, Readable, Writable } = require('stream');
assert.strictEqual(d.readable, false);
}));
}

{
// https://github.com/nodejs/node/issues/40497
pipeline(
['abc\ndef\nghi'],
Duplex.from(async function * (source) {
let rest = ''
for await (const chunk of source) {
console.error(0)
const lines = (rest + chunk.toString()).split('\n')
rest = lines.pop()
for (const line of lines) {
console.error(1, line)
yield line
console.error(2)
}
console.error(3)
}
console.error(4)
yield rest
}),
async function * (source) {
let ret = ''
for await (const x of source) {
ret += x
}
assert.strictEqual(ret, 'abcdefghi')
},
common.mustCall(() => {}),
)
}

0 comments on commit 5c8aa57

Please sign in to comment.