Skip to content
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

stream: use finished for async iteration #39282

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const maybeOverridePrepareStackTrace = (globalThis, error, trace) => {
};

const aggregateTwoErrors = hideStackFrames((innerError, outerError) => {
if (innerError && outerError) {
if (innerError && outerError && innerError !== outerError) {
if (ArrayIsArray(outerError.errors)) {
// If `outerError` is already an `AggregateError`.
ArrayPrototypePush(outerError.errors, innerError);
Expand Down
75 changes: 28 additions & 47 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const { Buffer } = require('buffer');
const {
addAbortSignalNoValidate,
} = require('internal/streams/add-abort-signal');
const eos = require('internal/streams/end-of-stream');

let debug = require('internal/util/debuglog').debuglog('stream', (fn) => {
debug = fn;
Expand All @@ -57,12 +58,14 @@ const {
} = require('internal/streams/state');

const {
ERR_INVALID_ARG_TYPE,
ERR_METHOD_NOT_IMPLEMENTED,
ERR_STREAM_PREMATURE_CLOSE,
ERR_STREAM_PUSH_AFTER_EOF,
ERR_STREAM_UNSHIFT_AFTER_END_EVENT,
} = require('internal/errors').codes;
aggregateTwoErrors,
codes: {
ERR_INVALID_ARG_TYPE,
ERR_METHOD_NOT_IMPLEMENTED,
ERR_STREAM_PUSH_AFTER_EOF,
ERR_STREAM_UNSHIFT_AFTER_END_EVENT,
}
} = require('internal/errors');
const { validateObject } = require('internal/validators');

const kPaused = Symbol('kPaused');
Expand Down Expand Up @@ -1090,12 +1093,6 @@ function streamToAsyncIterator(stream, options) {
async function* createAsyncIterator(stream, options) {
let callback = nop;

const opts = {
destroyOnReturn: true,
destroyOnError: true,
...options,
};

function next(resolve) {
if (this === stream) {
callback();
Expand All @@ -1105,54 +1102,38 @@ async function* createAsyncIterator(stream, options) {
}
}

const state = stream._readableState;
stream.on('readable', next);

let error;
eos(stream, { writable: false }, (err) => {
error = err ? aggregateTwoErrors(error, err) : null;
callback();
ronag marked this conversation as resolved.
Show resolved Hide resolved
callback = nop;
});

let error = state.errored;
let errorEmitted = state.errorEmitted;
let endEmitted = state.endEmitted;
let closeEmitted = state.closeEmitted;

stream
.on('readable', next)
.on('error', function(err) {
error = err;
errorEmitted = true;
next.call(this);
})
.on('end', function() {
endEmitted = true;
next.call(this);
})
.on('close', function() {
closeEmitted = true;
next.call(this);
});

let errorThrown = false;
try {
while (true) {
const chunk = stream.destroyed ? null : stream.read();
if (chunk !== null) {
yield chunk;
} else if (errorEmitted) {
} else if (error) {
throw error;
} else if (endEmitted) {
break;
} else if (closeEmitted) {
throw new ERR_STREAM_PREMATURE_CLOSE();
} else if (error === null) {
return;
} else {
await new Promise(next);
}
}
} catch (err) {
if (opts.destroyOnError) {
destroyImpl.destroyer(stream, err);
}
errorThrown = true;
throw err;
error = aggregateTwoErrors(error, err);
throw error;
} finally {
if (!errorThrown && opts.destroyOnReturn) {
if (state.autoDestroy || !endEmitted) {
if (error) {
if (options?.destroyOnError !== false) {
destroyImpl.destroyer(stream, error);
}
} else if (options?.destroyOnReturn !== false) {
if (error === undefined || stream._readableState.autoDestroy) {
destroyImpl.destroyer(stream, null);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async function tests() {
const iterator = readable[Symbol.asyncIterator]();

const err = new Error('kaboom');
readable.destroy(new Error('kaboom'));
readable.destroy(err);
await assert.rejects(iterator.next.bind(iterator), err);
}

Expand Down