-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathend-of-stream.js
More file actions
425 lines (379 loc) Β· 11.1 KB
/
end-of-stream.js
File metadata and controls
425 lines (379 loc) Β· 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Ported from https://github.com/mafintosh/end-of-stream with
// permission from the author, Mathias Buus (@mafintosh).
'use strict';
const {
Promise,
PromisePrototypeThen,
ReflectApply,
Symbol,
SymbolDispose,
} = primordials;
const {
AbortError,
codes: {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_PREMATURE_CLOSE,
},
} = require('internal/errors');
const {
kEmptyObject,
once,
} = require('internal/util');
const {
validateAbortSignal,
validateFunction,
validateObject,
validateBoolean,
} = require('internal/validators');
const {
isClosed,
isReadable,
isReadableNodeStream,
isReadableStream,
isReadableFinished,
isReadableErrored,
isWritable,
isWritableNodeStream,
isWritableStream,
isWritableFinished,
isWritableErrored,
isNodeStream,
willEmitClose: _willEmitClose,
kIsClosedPromise,
} = require('internal/streams/utils');
const { enabledHooksExist } = require('internal/async_hooks');
const AsyncContextFrame = require('internal/async_context_frame');
// Lazy load
let AsyncResource;
let addAbortListener;
function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
}
const nop = () => {};
function bindAsyncResource(fn, type) {
AsyncResource ??= require('async_hooks').AsyncResource;
const resource = new AsyncResource(type);
return function(...args) {
return resource.runInAsyncScope(fn, this, ...args);
};
}
const kEosImmediateClose = Symbol('kEosImmediateClose');
/**
* Returns the current stream error tracked by eos(), if any.
* @param {import('stream').Stream} stream
* @returns {Error | null}
*/
function getEosErrored(stream) {
const errored = isWritableErrored(stream) || isReadableErrored(stream);
return typeof errored !== 'boolean' && errored || null;
}
/**
* Returns the error eos() would report from an immediate close, including
* premature close detection for unfinished readable or writable sides.
* @param {import('stream').Stream} stream
* @param {boolean} readable
* @param {boolean | null} readableFinished
* @param {boolean} writable
* @param {boolean | null} writableFinished
* @returns {Error | typeof kEosImmediateClose}
*/
function getEosOnCloseError(stream, readable, readableFinished, writable, writableFinished) {
const errored = getEosErrored(stream);
if (errored) {
return errored;
}
if (readable && !readableFinished && isReadableNodeStream(stream, true)) {
if (!isReadableFinished(stream, false)) {
return new ERR_STREAM_PREMATURE_CLOSE();
}
}
if (writable && !writableFinished) {
if (!isWritableFinished(stream, false)) {
return new ERR_STREAM_PREMATURE_CLOSE();
}
}
return kEosImmediateClose;
}
function getEosInitialState(stream, options = kEmptyObject) {
const readable = options.readable ?? isReadableNodeStream(stream);
const writable = options.writable ?? isWritableNodeStream(stream);
// TODO (ronag): Improve soft detection to include core modules and
// common ecosystem modules that do properly emit 'close' but fail
// this generic check.
const willEmitClose = (
_willEmitClose(stream) &&
isReadableNodeStream(stream) === readable &&
isWritableNodeStream(stream) === writable
);
return {
readable,
writable,
willEmitClose,
writableFinished: isWritableFinished(stream, false),
readableFinished: isReadableFinished(stream, false),
closed: isClosed(stream),
};
}
/**
* Classifies whether eos() can synchronously determine the result for the
* current stream snapshot, or if it must defer to future events.
* @param {import('stream').Stream} stream
* @param {object} [options]
* @param {boolean} [options.readable]
* @param {boolean} [options.writable]
* @param {ReturnType<typeof getEosInitialState>} [state]
* @returns {Error | typeof kEosImmediateClose | null}
*/
function getEosImmediateResult(stream, options = kEmptyObject, state = getEosInitialState(stream, options)) {
const {
readable,
writable,
willEmitClose,
writableFinished,
readableFinished,
closed,
} = state;
const wState = stream._writableState;
const rState = stream._readableState;
if (closed) {
return getEosOnCloseError(
stream,
readable,
readableFinished,
writable,
writableFinished,
);
} else if (wState?.errorEmitted || rState?.errorEmitted) {
if (!willEmitClose) {
return getEosErrored(stream) ?? kEosImmediateClose;
}
} else if (
!readable &&
(!willEmitClose || isReadable(stream)) &&
(writableFinished || isWritable(stream) === false) &&
(wState == null || wState.pendingcb === undefined || wState.pendingcb === 0)
) {
return getEosErrored(stream) ?? kEosImmediateClose;
} else if (
!writable &&
(!willEmitClose || isWritable(stream)) &&
(readableFinished || isReadable(stream) === false)
) {
return getEosErrored(stream) ?? kEosImmediateClose;
} else if ((rState && stream.req && stream.aborted)) {
return getEosErrored(stream) ?? kEosImmediateClose;
}
return null;
}
function eos(stream, options, callback) {
if (arguments.length === 2) {
callback = options;
options = kEmptyObject;
} else if (options == null) {
options = kEmptyObject;
} else {
validateObject(options, 'options');
}
validateFunction(callback, 'callback');
validateAbortSignal(options.signal, 'options.signal');
if (AsyncContextFrame.current() || enabledHooksExist()) {
// Avoid AsyncResource.bind() because it calls ObjectDefineProperties which
// is a bottleneck here.
callback = once(bindAsyncResource(callback, 'STREAM_END_OF_STREAM'));
} else {
callback = once(callback);
}
if (isReadableStream(stream) || isWritableStream(stream)) {
return eosWeb(stream, options, callback);
}
if (!isNodeStream(stream)) {
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
}
const eosState = getEosInitialState(stream, options);
const wState = stream._writableState;
const onlegacyfinish = () => {
if (!stream.writable) {
onfinish();
}
};
const { readable, writable } = eosState;
let {
willEmitClose,
writableFinished,
readableFinished,
closed,
} = eosState;
const onfinish = () => {
writableFinished = true;
// Stream should not be destroyed here. If it is that
// means that user space is doing something differently and
// we cannot trust willEmitClose.
if (stream.destroyed) {
willEmitClose = false;
}
if (willEmitClose && (!stream.readable || readable)) {
return;
}
if (!readable || readableFinished) {
callback.call(stream);
}
};
const onend = () => {
readableFinished = true;
// Stream should not be destroyed here. If it is that
// means that user space is doing something differently and
// we cannot trust willEmitClose.
if (stream.destroyed) {
willEmitClose = false;
}
if (willEmitClose && (!stream.writable || writable)) {
return;
}
if (!writable || writableFinished) {
callback.call(stream);
}
};
const onerror = (err) => {
callback.call(stream, err);
};
const onclose = () => {
closed = true;
const error = getEosOnCloseError(stream, readable, readableFinished, writable, writableFinished);
if (error === kEosImmediateClose) {
callback.call(stream);
} else {
callback.call(stream, error);
}
};
const onrequest = () => {
stream.req.on('finish', onfinish);
};
if (isRequest(stream)) {
stream.on('complete', onfinish);
if (!willEmitClose) {
stream.on('abort', onclose);
}
if (stream.req) {
onrequest();
} else {
stream.on('request', onrequest);
}
} else if (writable && !wState) { // legacy streams
stream.on('end', onlegacyfinish);
stream.on('close', onlegacyfinish);
}
// Not all streams will emit 'close' after 'aborted'.
if (!willEmitClose && typeof stream.aborted === 'boolean') {
stream.on('aborted', onclose);
}
stream.on('end', onend);
stream.on('finish', onfinish);
if (options.error !== false) {
stream.on('error', onerror);
}
stream.on('close', onclose);
const immediateResult = getEosImmediateResult(stream, options, eosState);
if (immediateResult !== null) {
if (immediateResult === kEosImmediateClose) {
process.nextTick(() => callback.call(stream));
} else {
process.nextTick(() => callback.call(stream, immediateResult));
}
}
const cleanup = () => {
callback = nop;
stream.removeListener('aborted', onclose);
stream.removeListener('complete', onfinish);
stream.removeListener('abort', onclose);
stream.removeListener('request', onrequest);
if (stream.req) stream.req.removeListener('finish', onfinish);
stream.removeListener('end', onlegacyfinish);
stream.removeListener('close', onlegacyfinish);
stream.removeListener('finish', onfinish);
stream.removeListener('end', onend);
stream.removeListener('error', onerror);
stream.removeListener('close', onclose);
};
if (options.signal && !closed) {
const abort = () => {
// Keep it because cleanup removes it.
const endCallback = callback;
cleanup();
endCallback.call(
stream,
new AbortError(undefined, { cause: options.signal.reason }));
};
if (options.signal.aborted) {
process.nextTick(abort);
} else {
addAbortListener ??= require('internal/events/abort_listener').addAbortListener;
const disposable = addAbortListener(options.signal, abort);
const originalCallback = callback;
callback = once((...args) => {
disposable[SymbolDispose]();
ReflectApply(originalCallback, stream, args);
});
}
}
return cleanup;
}
function eosWeb(stream, options, callback) {
let isAborted = false;
let abort = nop;
if (options.signal) {
abort = () => {
isAborted = true;
callback.call(stream, new AbortError(undefined, { cause: options.signal.reason }));
};
if (options.signal.aborted) {
process.nextTick(abort);
} else {
addAbortListener ??= require('internal/events/abort_listener').addAbortListener;
const disposable = addAbortListener(options.signal, abort);
const originalCallback = callback;
callback = once((...args) => {
disposable[SymbolDispose]();
ReflectApply(originalCallback, stream, args);
});
}
}
const resolverFn = (...args) => {
if (!isAborted) {
process.nextTick(() => ReflectApply(callback, stream, args));
}
};
PromisePrototypeThen(
stream[kIsClosedPromise].promise,
resolverFn,
resolverFn,
);
return nop;
}
function finished(stream, opts) {
let autoCleanup = false;
if (opts === null) {
opts = kEmptyObject;
}
if (opts?.cleanup) {
validateBoolean(opts.cleanup, 'cleanup');
autoCleanup = opts.cleanup;
}
return new Promise((resolve, reject) => {
const cleanup = eos(stream, opts, (err) => {
if (autoCleanup) {
cleanup();
}
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
module.exports = {
eos,
finished,
getEosImmediateResult,
kEosImmediateClose,
};