Skip to content

Commit e01de1c

Browse files
committed
stream: simplify eos immediate completion path
1 parent cacf709 commit e01de1c

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

lib/internal/streams/end-of-stream.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,6 @@ function eos(stream, options, callback) {
183183
} else if ((rState && stream.req && stream.aborted)) {
184184
immediateResult = getEosErrored(stream);
185185
}
186-
const returnImmediately = (result) => {
187-
const args = result === null ? [] : [result];
188-
if (options[kEosNodeSyncronousCallback]) {
189-
callback.call(stream, ...args);
190-
} else {
191-
if (AsyncContextFrame.current() || enabledHooksExist()) {
192-
// Avoid AsyncResource.bind() because it calls ObjectDefineProperties which
193-
// is a bottleneck here.
194-
callback = bindAsyncResource(callback, 'STREAM_END_OF_STREAM');
195-
}
196-
process.nextTick(() => callback.call(stream, ...args));
197-
}
198-
};
199186
let cleanup = () => {
200187
callback = nop;
201188
};
@@ -207,23 +194,27 @@ function eos(stream, options, callback) {
207194
stream.removeListener('error', nop);
208195
};
209196
}
210-
returnImmediately(immediateResult);
211-
return cleanup;
197+
} else if (options.signal?.aborted) {
198+
immediateResult = new AbortError(undefined, { cause: options.signal.reason });
212199
}
213-
214-
if (options.signal?.aborted) {
215-
returnImmediately(new AbortError(undefined, { cause: options.signal.reason }));
200+
if (immediateResult !== undefined && options[kEosNodeSyncronousCallback]) {
201+
ReflectApply(callback, stream, immediateResult === null ? [] : [immediateResult]);
216202
return cleanup;
217203
}
218204

219205
if (AsyncContextFrame.current() || enabledHooksExist()) {
220206
// Avoid AsyncResource.bind() because it calls ObjectDefineProperties which
221207
// is a bottleneck here.
222-
callback = once(bindAsyncResource(callback, 'STREAM_END_OF_STREAM'));
223-
} else {
224-
callback = once(callback);
208+
callback = bindAsyncResource(callback, 'STREAM_END_OF_STREAM');
225209
}
226210

211+
if (immediateResult !== undefined) {
212+
process.nextTick(() => ReflectApply(callback, stream, immediateResult === null ? [] : [immediateResult]));
213+
return cleanup;
214+
}
215+
216+
callback = once(callback);
217+
227218
const onlegacyfinish = () => {
228219
if (!stream.writable) {
229220
onfinish();

0 commit comments

Comments
 (0)