@gjsify/stream 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -2
- package/cjs-compat.cjs +7 -0
- package/lib/esm/consumers/index.js +37 -4
- package/lib/esm/index.js +1096 -4
- package/lib/esm/promises/index.js +28 -4
- package/lib/esm/web/index.js +34 -3
- package/lib/types/consumers/index.d.ts +13 -3
- package/lib/types/index.d.ts +182 -6
- package/lib/types/promises/index.d.ts +8 -3
- package/lib/types/web/index.d.ts +3 -3
- package/package.json +23 -45
- package/src/consumers/index.spec.ts +107 -0
- package/src/consumers/index.ts +39 -3
- package/src/edge-cases.spec.ts +593 -0
- package/src/index.spec.ts +2239 -7
- package/src/index.ts +1348 -5
- package/src/promises/index.spec.ts +140 -0
- package/src/promises/index.ts +31 -3
- package/src/test.mts +5 -2
- package/src/web/index.ts +32 -3
- package/tsconfig.json +21 -9
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/consumers/index.js +0 -6
- package/lib/cjs/index.js +0 -6
- package/lib/cjs/promises/index.js +0 -6
- package/lib/cjs/web/index.js +0 -6
- package/test.gjs.js +0 -34839
- package/test.gjs.mjs +0 -34693
- package/test.gjs.mjs.meta.json +0 -1
- package/test.node.js +0 -1234
- package/test.node.mjs +0 -315
- package/tsconfig.types.json +0 -8
- package/tsconfig.types.tsbuildinfo +0 -1
package/lib/esm/index.js
CHANGED
|
@@ -1,6 +1,1098 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { EventEmitter } from "@gjsify/events";
|
|
2
|
+
import { nextTick } from "@gjsify/utils";
|
|
3
|
+
let defaultHighWaterMark = 16384;
|
|
4
|
+
let defaultObjectHighWaterMark = 16;
|
|
5
|
+
function getDefaultHighWaterMark(objectMode) {
|
|
6
|
+
return objectMode ? defaultObjectHighWaterMark : defaultHighWaterMark;
|
|
7
|
+
}
|
|
8
|
+
function setDefaultHighWaterMark(objectMode, value) {
|
|
9
|
+
if (typeof value !== "number" || value < 0 || Number.isNaN(value)) {
|
|
10
|
+
throw new TypeError(`Invalid highWaterMark: ${value}`);
|
|
11
|
+
}
|
|
12
|
+
if (objectMode) {
|
|
13
|
+
defaultObjectHighWaterMark = value;
|
|
14
|
+
} else {
|
|
15
|
+
defaultHighWaterMark = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class Stream extends EventEmitter {
|
|
19
|
+
constructor(opts) {
|
|
20
|
+
super(opts);
|
|
21
|
+
}
|
|
22
|
+
pipe(destination, options) {
|
|
23
|
+
const source = this;
|
|
24
|
+
const doEnd = options?.end !== false;
|
|
25
|
+
const ondata = (chunk) => {
|
|
26
|
+
if (destination.writable) {
|
|
27
|
+
if (destination.write(chunk) === false && typeof source.pause === "function") {
|
|
28
|
+
source.pause();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
source.on("data", ondata);
|
|
33
|
+
const ondrain = () => {
|
|
34
|
+
if (typeof source.resume === "function") {
|
|
35
|
+
source.resume();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
destination.on("drain", ondrain);
|
|
39
|
+
const onend = () => {
|
|
40
|
+
if (doEnd) {
|
|
41
|
+
destination.end();
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
if (doEnd) {
|
|
45
|
+
source.on("end", onend);
|
|
46
|
+
}
|
|
47
|
+
const cleanup = () => {
|
|
48
|
+
source.removeListener("data", ondata);
|
|
49
|
+
destination.removeListener("drain", ondrain);
|
|
50
|
+
source.removeListener("end", onend);
|
|
51
|
+
};
|
|
52
|
+
source.on("close", cleanup);
|
|
53
|
+
destination.on("close", cleanup);
|
|
54
|
+
if (source instanceof Readable) {
|
|
55
|
+
source._pipeDests.push({ dest: destination, ondata, ondrain, onend, cleanup, doEnd });
|
|
56
|
+
}
|
|
57
|
+
destination.emit("pipe", source);
|
|
58
|
+
return destination;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
class Readable extends Stream {
|
|
62
|
+
readable = true;
|
|
63
|
+
readableFlowing = null;
|
|
64
|
+
readableLength = 0;
|
|
65
|
+
readableHighWaterMark;
|
|
66
|
+
readableEncoding;
|
|
67
|
+
readableObjectMode;
|
|
68
|
+
readableEnded = false;
|
|
69
|
+
readableAborted = false;
|
|
70
|
+
destroyed = false;
|
|
71
|
+
/** @internal Tracked pipe destinations for unpipe. */
|
|
72
|
+
_pipeDests = [];
|
|
73
|
+
_buffer = [];
|
|
74
|
+
_readableState = { ended: false, endEmitted: false, reading: false, constructed: true };
|
|
75
|
+
_readablePending = false;
|
|
76
|
+
_readImpl;
|
|
77
|
+
_destroyImpl;
|
|
78
|
+
_constructImpl;
|
|
79
|
+
constructor(opts) {
|
|
80
|
+
super(opts);
|
|
81
|
+
this.readableHighWaterMark = opts?.highWaterMark ?? getDefaultHighWaterMark(opts?.objectMode ?? false);
|
|
82
|
+
this.readableEncoding = opts?.encoding ?? null;
|
|
83
|
+
this.readableObjectMode = opts?.objectMode ?? false;
|
|
84
|
+
if (opts?.read) this._readImpl = opts.read;
|
|
85
|
+
if (opts?.destroy) this._destroyImpl = opts.destroy;
|
|
86
|
+
if (opts?.construct) this._constructImpl = opts.construct;
|
|
87
|
+
const hasConstruct = this._constructImpl || this._construct !== Readable.prototype._construct;
|
|
88
|
+
if (hasConstruct) {
|
|
89
|
+
this._readableState.constructed = false;
|
|
90
|
+
nextTick(() => {
|
|
91
|
+
this._construct((err) => {
|
|
92
|
+
this._readableState.constructed = true;
|
|
93
|
+
if (err) {
|
|
94
|
+
this.destroy(err);
|
|
95
|
+
} else {
|
|
96
|
+
if (this.readableFlowing === true) {
|
|
97
|
+
this._flow();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
_construct(callback) {
|
|
105
|
+
if (this._constructImpl) {
|
|
106
|
+
this._constructImpl.call(this, callback);
|
|
107
|
+
} else {
|
|
108
|
+
callback();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
_read(_size) {
|
|
112
|
+
if (this._readImpl) {
|
|
113
|
+
this._readImpl.call(this, _size);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
read(size) {
|
|
117
|
+
if (!this._readableState.constructed) return null;
|
|
118
|
+
if (this._buffer.length === 0) {
|
|
119
|
+
if (this._readableState.ended) return null;
|
|
120
|
+
this._readableState.reading = true;
|
|
121
|
+
this._read(size ?? this.readableHighWaterMark);
|
|
122
|
+
this._readableState.reading = false;
|
|
123
|
+
}
|
|
124
|
+
if (this._buffer.length === 0) return null;
|
|
125
|
+
if (size === 0) return null;
|
|
126
|
+
if (this.readableObjectMode) {
|
|
127
|
+
if (size === void 0) {
|
|
128
|
+
const chunk2 = this._buffer.shift();
|
|
129
|
+
this.readableLength -= 1;
|
|
130
|
+
if (this._readableState.ended && this._buffer.length === 0 && !this._readableState.endEmitted) {
|
|
131
|
+
this._emitEnd();
|
|
132
|
+
}
|
|
133
|
+
return chunk2;
|
|
134
|
+
}
|
|
135
|
+
if (size > this.readableLength) return null;
|
|
136
|
+
const chunk = this._buffer.shift();
|
|
137
|
+
this.readableLength -= 1;
|
|
138
|
+
return chunk;
|
|
139
|
+
}
|
|
140
|
+
if (size !== void 0 && size !== null) {
|
|
141
|
+
if (size > this.readableLength) return null;
|
|
142
|
+
return this._readBytes(size);
|
|
143
|
+
}
|
|
144
|
+
const result = this._buffer.splice(0);
|
|
145
|
+
this.readableLength = 0;
|
|
146
|
+
if (this._readableState.ended && this._buffer.length === 0 && !this._readableState.endEmitted) {
|
|
147
|
+
this._emitEnd();
|
|
148
|
+
}
|
|
149
|
+
if (result.length === 1) return result[0];
|
|
150
|
+
if (result.length === 0) return null;
|
|
151
|
+
if (typeof result[0] === "string") return result.join("");
|
|
152
|
+
const BufCtor = globalThis.Buffer;
|
|
153
|
+
return BufCtor?.concat ? BufCtor.concat(result) : result;
|
|
154
|
+
}
|
|
155
|
+
/** @internal Extract exactly `size` bytes from the internal buffer. */
|
|
156
|
+
_readBytes(size) {
|
|
157
|
+
let collected = 0;
|
|
158
|
+
const parts = [];
|
|
159
|
+
while (collected < size && this._buffer.length > 0) {
|
|
160
|
+
const chunk = this._buffer[0];
|
|
161
|
+
const chunkLen = chunk.length ?? 1;
|
|
162
|
+
if (collected + chunkLen <= size) {
|
|
163
|
+
parts.push(this._buffer.shift());
|
|
164
|
+
collected += chunkLen;
|
|
165
|
+
this.readableLength -= chunkLen;
|
|
166
|
+
} else {
|
|
167
|
+
const needed = size - collected;
|
|
168
|
+
const BufCtor2 = globalThis.Buffer;
|
|
169
|
+
if (BufCtor2 && BufCtor2.isBuffer(chunk)) {
|
|
170
|
+
parts.push(chunk.slice(0, needed));
|
|
171
|
+
this._buffer[0] = chunk.slice(needed);
|
|
172
|
+
} else if (typeof chunk === "string") {
|
|
173
|
+
parts.push(chunk.slice(0, needed));
|
|
174
|
+
this._buffer[0] = chunk.slice(needed);
|
|
175
|
+
} else {
|
|
176
|
+
parts.push(chunk.slice(0, needed));
|
|
177
|
+
this._buffer[0] = chunk.slice(needed);
|
|
178
|
+
}
|
|
179
|
+
this.readableLength -= needed;
|
|
180
|
+
collected += needed;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (parts.length === 1) return parts[0];
|
|
184
|
+
const BufCtor = globalThis.Buffer;
|
|
185
|
+
return BufCtor?.concat ? BufCtor.concat(parts) : parts;
|
|
186
|
+
}
|
|
187
|
+
push(chunk, encoding) {
|
|
188
|
+
if (chunk === null) {
|
|
189
|
+
this._readableState.ended = true;
|
|
190
|
+
this.readableEnded = true;
|
|
191
|
+
if (this._buffer.length === 0 && !this._readableState.endEmitted) {
|
|
192
|
+
nextTick(() => this._emitEnd());
|
|
193
|
+
}
|
|
194
|
+
this._scheduleReadable();
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
this._buffer.push(chunk);
|
|
198
|
+
this.readableLength += this.readableObjectMode ? 1 : chunk.length ?? 1;
|
|
199
|
+
if (this.readableFlowing && !this._flowing) {
|
|
200
|
+
nextTick(() => this._flow());
|
|
201
|
+
}
|
|
202
|
+
if (this.readableFlowing !== true) {
|
|
203
|
+
this._scheduleReadable();
|
|
204
|
+
}
|
|
205
|
+
return this.readableLength < this.readableHighWaterMark;
|
|
206
|
+
}
|
|
207
|
+
/** Emit 'end' followed by 'close' (matches Node.js autoDestroy behavior). */
|
|
208
|
+
_emitEnd() {
|
|
209
|
+
if (this._readableState.endEmitted) return;
|
|
210
|
+
this._readableState.endEmitted = true;
|
|
211
|
+
this.emit("end");
|
|
212
|
+
nextTick(() => this.emit("close"));
|
|
213
|
+
}
|
|
214
|
+
/** Schedule a single 'readable' event per microtask cycle (deduplicates multiple pushes). */
|
|
215
|
+
_scheduleReadable() {
|
|
216
|
+
if (this._readablePending || this.listenerCount("readable") === 0) return;
|
|
217
|
+
this._readablePending = true;
|
|
218
|
+
nextTick(() => {
|
|
219
|
+
this._readablePending = false;
|
|
220
|
+
if (!this.destroyed) this.emit("readable");
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
on(event, listener) {
|
|
224
|
+
super.on(event, listener);
|
|
225
|
+
if (event === "data" && this.readableFlowing !== false) {
|
|
226
|
+
this.resume();
|
|
227
|
+
}
|
|
228
|
+
if (event === "readable" && (this._buffer.length > 0 || this._readableState.ended)) {
|
|
229
|
+
this._scheduleReadable();
|
|
230
|
+
}
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
unshift(chunk) {
|
|
234
|
+
this._buffer.unshift(chunk);
|
|
235
|
+
this.readableLength += this.readableObjectMode ? 1 : chunk.length ?? 1;
|
|
236
|
+
}
|
|
237
|
+
setEncoding(encoding) {
|
|
238
|
+
this.readableEncoding = encoding;
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
pause() {
|
|
242
|
+
this.readableFlowing = false;
|
|
243
|
+
this.emit("pause");
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
resume() {
|
|
247
|
+
if (this.readableFlowing !== true) {
|
|
248
|
+
this.readableFlowing = true;
|
|
249
|
+
this.emit("resume");
|
|
250
|
+
if (this._readableState.constructed) {
|
|
251
|
+
this._flow();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
_flowing = false;
|
|
257
|
+
_flow() {
|
|
258
|
+
if (this.readableFlowing !== true || this._flowing || this.destroyed) return;
|
|
259
|
+
if (!this._readableState.constructed) return;
|
|
260
|
+
this._flowing = true;
|
|
261
|
+
try {
|
|
262
|
+
while (this._buffer.length > 0 && this.readableFlowing && !this.destroyed) {
|
|
263
|
+
let chunk = this._buffer.shift();
|
|
264
|
+
this.readableLength -= this.readableObjectMode ? 1 : chunk.length ?? 1;
|
|
265
|
+
if (this.readableEncoding && typeof chunk !== "string") {
|
|
266
|
+
const BufCtor = globalThis.Buffer;
|
|
267
|
+
if (BufCtor && BufCtor.isBuffer(chunk)) {
|
|
268
|
+
chunk = chunk.toString(this.readableEncoding);
|
|
269
|
+
} else if (chunk instanceof Uint8Array) {
|
|
270
|
+
chunk = new TextDecoder(this.readableEncoding).decode(chunk);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
this.emit("data", chunk);
|
|
274
|
+
}
|
|
275
|
+
if (this.destroyed) return;
|
|
276
|
+
if (this._readableState.ended && this._buffer.length === 0 && !this._readableState.endEmitted) {
|
|
277
|
+
nextTick(() => this._emitEnd());
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (!this._readableState.ended && !this._readableState.reading && !this.destroyed) {
|
|
281
|
+
this._readableState.reading = true;
|
|
282
|
+
this._read(this.readableHighWaterMark);
|
|
283
|
+
this._readableState.reading = false;
|
|
284
|
+
}
|
|
285
|
+
} finally {
|
|
286
|
+
this._flowing = false;
|
|
287
|
+
}
|
|
288
|
+
if (this._buffer.length > 0 && this.readableFlowing && !this.destroyed) {
|
|
289
|
+
nextTick(() => this._flow());
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
isPaused() {
|
|
293
|
+
return this.readableFlowing === false;
|
|
294
|
+
}
|
|
295
|
+
unpipe(destination) {
|
|
296
|
+
if (!destination) {
|
|
297
|
+
for (const state of this._pipeDests) {
|
|
298
|
+
state.cleanup();
|
|
299
|
+
state.dest.emit("unpipe", this);
|
|
300
|
+
}
|
|
301
|
+
this._pipeDests = [];
|
|
302
|
+
this.readableFlowing = false;
|
|
303
|
+
} else {
|
|
304
|
+
const idx = this._pipeDests.findIndex((s) => s.dest === destination);
|
|
305
|
+
if (idx !== -1) {
|
|
306
|
+
const state = this._pipeDests[idx];
|
|
307
|
+
state.cleanup();
|
|
308
|
+
this._pipeDests.splice(idx, 1);
|
|
309
|
+
destination.emit("unpipe", this);
|
|
310
|
+
if (this._pipeDests.length === 0) {
|
|
311
|
+
this.readableFlowing = false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
destroy(error) {
|
|
318
|
+
if (this.destroyed) return this;
|
|
319
|
+
this.destroyed = true;
|
|
320
|
+
this.readable = false;
|
|
321
|
+
this.readableAborted = !this.readableEnded;
|
|
322
|
+
const cb = (err) => {
|
|
323
|
+
if (err) nextTick(() => this.emit("error", err));
|
|
324
|
+
nextTick(() => this.emit("close"));
|
|
325
|
+
};
|
|
326
|
+
if (this._destroyImpl) {
|
|
327
|
+
this._destroyImpl.call(this, error ?? null, cb);
|
|
328
|
+
} else {
|
|
329
|
+
cb(error);
|
|
330
|
+
}
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
[Symbol.asyncIterator]() {
|
|
334
|
+
const readable = this;
|
|
335
|
+
const buffer = [];
|
|
336
|
+
let done = false;
|
|
337
|
+
let error = null;
|
|
338
|
+
let waitingResolve = null;
|
|
339
|
+
let waitingReject = null;
|
|
340
|
+
readable.on("data", (chunk) => {
|
|
341
|
+
if (waitingResolve) {
|
|
342
|
+
const resolve = waitingResolve;
|
|
343
|
+
waitingResolve = null;
|
|
344
|
+
waitingReject = null;
|
|
345
|
+
resolve({ value: chunk, done: false });
|
|
346
|
+
} else {
|
|
347
|
+
buffer.push(chunk);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
readable.on("end", () => {
|
|
351
|
+
done = true;
|
|
352
|
+
if (waitingResolve) {
|
|
353
|
+
const resolve = waitingResolve;
|
|
354
|
+
waitingResolve = null;
|
|
355
|
+
waitingReject = null;
|
|
356
|
+
resolve({ value: void 0, done: true });
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
readable.on("error", (err) => {
|
|
360
|
+
error = err;
|
|
361
|
+
done = true;
|
|
362
|
+
if (waitingReject) {
|
|
363
|
+
const reject = waitingReject;
|
|
364
|
+
waitingResolve = null;
|
|
365
|
+
waitingReject = null;
|
|
366
|
+
reject(err);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
return {
|
|
370
|
+
next() {
|
|
371
|
+
if (error) return Promise.reject(error);
|
|
372
|
+
if (buffer.length > 0) return Promise.resolve({ value: buffer.shift(), done: false });
|
|
373
|
+
if (done) return Promise.resolve({ value: void 0, done: true });
|
|
374
|
+
return new Promise((resolve, reject) => {
|
|
375
|
+
waitingResolve = resolve;
|
|
376
|
+
waitingReject = reject;
|
|
377
|
+
});
|
|
378
|
+
},
|
|
379
|
+
return() {
|
|
380
|
+
readable.destroy();
|
|
381
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
382
|
+
},
|
|
383
|
+
[Symbol.asyncIterator]() {
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
static from(iterable, opts) {
|
|
389
|
+
const readable = new Readable({
|
|
390
|
+
objectMode: true,
|
|
391
|
+
...opts,
|
|
392
|
+
read() {
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
if (typeof iterable === "string" || ArrayBuffer.isView(iterable)) {
|
|
396
|
+
readable.push(iterable);
|
|
397
|
+
readable.push(null);
|
|
398
|
+
return readable;
|
|
399
|
+
}
|
|
400
|
+
(async () => {
|
|
401
|
+
try {
|
|
402
|
+
for await (const chunk of iterable) {
|
|
403
|
+
if (!readable.push(chunk)) {
|
|
404
|
+
await new Promise((resolve) => readable.once("drain", resolve));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
readable.push(null);
|
|
408
|
+
} catch (err) {
|
|
409
|
+
readable.destroy(err);
|
|
410
|
+
}
|
|
411
|
+
})();
|
|
412
|
+
return readable;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
class Writable extends Stream {
|
|
416
|
+
writable = true;
|
|
417
|
+
writableHighWaterMark;
|
|
418
|
+
writableLength = 0;
|
|
419
|
+
writableObjectMode;
|
|
420
|
+
writableEnded = false;
|
|
421
|
+
writableFinished = false;
|
|
422
|
+
writableCorked = 0;
|
|
423
|
+
writableNeedDrain = false;
|
|
424
|
+
destroyed = false;
|
|
425
|
+
_writableState = { ended: false, finished: false, constructed: true, writing: false };
|
|
426
|
+
_corkedBuffer = [];
|
|
427
|
+
_writeBuffer = [];
|
|
428
|
+
_pendingConstruct = [];
|
|
429
|
+
_ending = false;
|
|
430
|
+
_endCallback;
|
|
431
|
+
_pendingEnd = null;
|
|
432
|
+
_writeImpl;
|
|
433
|
+
_writev;
|
|
434
|
+
_finalImpl;
|
|
435
|
+
_destroyImpl;
|
|
436
|
+
_constructImpl;
|
|
437
|
+
_decodeStrings;
|
|
438
|
+
_defaultEncoding = "utf8";
|
|
439
|
+
constructor(opts) {
|
|
440
|
+
super(opts);
|
|
441
|
+
this.writableHighWaterMark = opts?.highWaterMark ?? getDefaultHighWaterMark(opts?.objectMode ?? false);
|
|
442
|
+
this.writableObjectMode = opts?.objectMode ?? false;
|
|
443
|
+
this._decodeStrings = opts?.decodeStrings !== false;
|
|
444
|
+
if (opts?.write) this._writeImpl = opts.write;
|
|
445
|
+
if (opts?.writev) this._writev = opts.writev;
|
|
446
|
+
if (opts?.final) this._finalImpl = opts.final;
|
|
447
|
+
if (opts?.destroy) this._destroyImpl = opts.destroy;
|
|
448
|
+
if (opts?.construct) this._constructImpl = opts.construct;
|
|
449
|
+
const hasConstruct = this._constructImpl || this._construct !== Writable.prototype._construct;
|
|
450
|
+
if (hasConstruct) {
|
|
451
|
+
this._writableState.constructed = false;
|
|
452
|
+
nextTick(() => {
|
|
453
|
+
this._construct((err) => {
|
|
454
|
+
this._writableState.constructed = true;
|
|
455
|
+
if (err) {
|
|
456
|
+
this.destroy(err);
|
|
457
|
+
} else {
|
|
458
|
+
this._maybeFlush();
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
_construct(callback) {
|
|
465
|
+
if (this._constructImpl) {
|
|
466
|
+
this._constructImpl.call(this, callback);
|
|
467
|
+
} else {
|
|
468
|
+
callback();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
_write(chunk, encoding, callback) {
|
|
472
|
+
if (this._writeImpl) {
|
|
473
|
+
this._writeImpl.call(this, chunk, encoding, callback);
|
|
474
|
+
} else {
|
|
475
|
+
callback();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
_final(callback) {
|
|
479
|
+
if (this._finalImpl) {
|
|
480
|
+
this._finalImpl.call(this, callback);
|
|
481
|
+
} else {
|
|
482
|
+
callback();
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
_maybeFlush() {
|
|
486
|
+
const pending = this._pendingConstruct.splice(0);
|
|
487
|
+
if (pending.length > 0) {
|
|
488
|
+
const [first, ...rest] = pending;
|
|
489
|
+
this._writeBuffer.push(...rest);
|
|
490
|
+
this._doWrite(first.chunk, first.encoding, first.callback);
|
|
491
|
+
}
|
|
492
|
+
if (this._pendingEnd) {
|
|
493
|
+
const { chunk, encoding, callback } = this._pendingEnd;
|
|
494
|
+
this._pendingEnd = null;
|
|
495
|
+
this._doEnd(chunk, encoding, callback);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
_doWrite(chunk, encoding, callback) {
|
|
499
|
+
this._writableState.writing = true;
|
|
500
|
+
this._write(chunk, encoding, (err) => {
|
|
501
|
+
this._writableState.writing = false;
|
|
502
|
+
this.writableLength -= this.writableObjectMode ? 1 : chunk?.length ?? 1;
|
|
503
|
+
if (err) {
|
|
504
|
+
nextTick(() => {
|
|
505
|
+
callback(err);
|
|
506
|
+
this.emit("error", err);
|
|
507
|
+
this._drainWriteBuffer();
|
|
508
|
+
});
|
|
509
|
+
} else {
|
|
510
|
+
nextTick(() => {
|
|
511
|
+
callback();
|
|
512
|
+
if (this.writableNeedDrain && this.writableLength < this.writableHighWaterMark) {
|
|
513
|
+
this.writableNeedDrain = false;
|
|
514
|
+
this.emit("drain");
|
|
515
|
+
}
|
|
516
|
+
this._drainWriteBuffer();
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
_drainWriteBuffer() {
|
|
522
|
+
if (this._writeBuffer.length > 0) {
|
|
523
|
+
const next = this._writeBuffer.shift();
|
|
524
|
+
this._doWrite(next.chunk, next.encoding, next.callback);
|
|
525
|
+
} else {
|
|
526
|
+
this._maybeFinish();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
_maybeFinish() {
|
|
530
|
+
if (!this._ending || this._writableState.finished || this._writableState.writing || this._writeBuffer.length > 0) return;
|
|
531
|
+
this._ending = false;
|
|
532
|
+
this._final((err) => {
|
|
533
|
+
this.writableFinished = true;
|
|
534
|
+
this._writableState.finished = true;
|
|
535
|
+
nextTick(() => {
|
|
536
|
+
if (err) {
|
|
537
|
+
this.emit("error", err);
|
|
538
|
+
}
|
|
539
|
+
this.emit("finish");
|
|
540
|
+
nextTick(() => this.emit("close"));
|
|
541
|
+
if (this._endCallback) this._endCallback();
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
write(chunk, encoding, callback) {
|
|
546
|
+
if (typeof encoding === "function") {
|
|
547
|
+
callback = encoding;
|
|
548
|
+
encoding = void 0;
|
|
549
|
+
}
|
|
550
|
+
if (encoding === void 0) encoding = this._defaultEncoding;
|
|
551
|
+
callback = callback || (() => {
|
|
552
|
+
});
|
|
553
|
+
if (this._decodeStrings && !this.writableObjectMode && typeof chunk === "string") {
|
|
554
|
+
const BufCtor = globalThis.Buffer;
|
|
555
|
+
if (BufCtor) {
|
|
556
|
+
chunk = BufCtor.from(chunk, encoding);
|
|
557
|
+
encoding = "buffer";
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (typeof chunk !== "string" && !this.writableObjectMode) {
|
|
561
|
+
const BufCtor = globalThis.Buffer;
|
|
562
|
+
if (BufCtor && BufCtor.isBuffer(chunk) || chunk instanceof Uint8Array) {
|
|
563
|
+
encoding = "buffer";
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (this.writableEnded) {
|
|
567
|
+
const err = new Error("write after end");
|
|
568
|
+
nextTick(() => {
|
|
569
|
+
if (callback) callback(err);
|
|
570
|
+
this.emit("error", err);
|
|
571
|
+
});
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
this.writableLength += this.writableObjectMode ? 1 : chunk?.length ?? 1;
|
|
575
|
+
if (this.writableCorked > 0) {
|
|
576
|
+
this._corkedBuffer.push({ chunk, encoding, callback });
|
|
577
|
+
return this.writableLength < this.writableHighWaterMark;
|
|
578
|
+
}
|
|
579
|
+
if (!this._writableState.constructed) {
|
|
580
|
+
this._pendingConstruct.push({ chunk, encoding, callback });
|
|
581
|
+
return this.writableLength < this.writableHighWaterMark;
|
|
582
|
+
}
|
|
583
|
+
const belowHWM = this.writableLength < this.writableHighWaterMark;
|
|
584
|
+
if (!belowHWM) {
|
|
585
|
+
this.writableNeedDrain = true;
|
|
586
|
+
}
|
|
587
|
+
if (this._writableState.writing) {
|
|
588
|
+
this._writeBuffer.push({ chunk, encoding, callback });
|
|
589
|
+
} else {
|
|
590
|
+
this._doWrite(chunk, encoding, callback);
|
|
591
|
+
}
|
|
592
|
+
return belowHWM;
|
|
593
|
+
}
|
|
594
|
+
_doEnd(chunk, encoding, callback) {
|
|
595
|
+
if (chunk !== void 0 && chunk !== null) {
|
|
596
|
+
this.write(chunk, encoding);
|
|
597
|
+
}
|
|
598
|
+
this.writableEnded = true;
|
|
599
|
+
this._writableState.ended = true;
|
|
600
|
+
this._ending = true;
|
|
601
|
+
this._endCallback = callback;
|
|
602
|
+
this._maybeFinish();
|
|
603
|
+
}
|
|
604
|
+
end(chunk, encoding, callback) {
|
|
605
|
+
if (typeof chunk === "function") {
|
|
606
|
+
callback = chunk;
|
|
607
|
+
chunk = void 0;
|
|
608
|
+
}
|
|
609
|
+
if (typeof encoding === "function") {
|
|
610
|
+
callback = encoding;
|
|
611
|
+
encoding = void 0;
|
|
612
|
+
}
|
|
613
|
+
if (this.writableEnded) {
|
|
614
|
+
if (callback) nextTick(callback);
|
|
615
|
+
return this;
|
|
616
|
+
}
|
|
617
|
+
if (!this._writableState.constructed) {
|
|
618
|
+
this._pendingEnd = { chunk, encoding, callback };
|
|
619
|
+
return this;
|
|
620
|
+
}
|
|
621
|
+
this._doEnd(chunk, encoding, callback);
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
cork() {
|
|
625
|
+
this.writableCorked++;
|
|
626
|
+
}
|
|
627
|
+
uncork() {
|
|
628
|
+
if (this.writableCorked > 0) {
|
|
629
|
+
this.writableCorked--;
|
|
630
|
+
if (this.writableCorked === 0 && this._corkedBuffer.length > 0) {
|
|
631
|
+
this._flushCorkedBuffer();
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
_flushCorkedBuffer() {
|
|
636
|
+
if (this._writev && this._corkedBuffer.length > 1) {
|
|
637
|
+
const buffered = this._corkedBuffer.splice(0);
|
|
638
|
+
const chunks = buffered.map((b) => ({ chunk: b.chunk, encoding: b.encoding }));
|
|
639
|
+
this._writev.call(this, chunks, (err) => {
|
|
640
|
+
for (const b of buffered) {
|
|
641
|
+
this.writableLength -= this.writableObjectMode ? 1 : b.chunk?.length ?? 1;
|
|
642
|
+
}
|
|
643
|
+
if (err) {
|
|
644
|
+
for (const b of buffered) b.callback(err);
|
|
645
|
+
this.emit("error", err);
|
|
646
|
+
} else {
|
|
647
|
+
for (const b of buffered) b.callback();
|
|
648
|
+
if (this.writableNeedDrain && this.writableLength < this.writableHighWaterMark) {
|
|
649
|
+
this.writableNeedDrain = false;
|
|
650
|
+
this.emit("drain");
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
} else {
|
|
655
|
+
const buffered = this._corkedBuffer.splice(0);
|
|
656
|
+
if (buffered.length > 0) {
|
|
657
|
+
const [first, ...rest] = buffered;
|
|
658
|
+
this._writeBuffer.push(...rest);
|
|
659
|
+
this._doWrite(first.chunk, first.encoding, first.callback);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
setDefaultEncoding(encoding) {
|
|
664
|
+
this._defaultEncoding = encoding;
|
|
665
|
+
return this;
|
|
666
|
+
}
|
|
667
|
+
destroy(error) {
|
|
668
|
+
if (this.destroyed) return this;
|
|
669
|
+
this.destroyed = true;
|
|
670
|
+
this.writable = false;
|
|
671
|
+
const cb = (err) => {
|
|
672
|
+
if (err) nextTick(() => this.emit("error", err));
|
|
673
|
+
nextTick(() => this.emit("close"));
|
|
674
|
+
};
|
|
675
|
+
if (this._destroyImpl) {
|
|
676
|
+
this._destroyImpl.call(this, error ?? null, cb);
|
|
677
|
+
} else {
|
|
678
|
+
cb(error);
|
|
679
|
+
}
|
|
680
|
+
return this;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
class Duplex extends Readable {
|
|
684
|
+
writable = true;
|
|
685
|
+
writableHighWaterMark;
|
|
686
|
+
writableLength = 0;
|
|
687
|
+
writableObjectMode;
|
|
688
|
+
writableEnded = false;
|
|
689
|
+
writableFinished = false;
|
|
690
|
+
writableCorked = 0;
|
|
691
|
+
writableNeedDrain = false;
|
|
692
|
+
allowHalfOpen;
|
|
693
|
+
_decodeStrings;
|
|
694
|
+
_duplexCorkedBuffer = [];
|
|
695
|
+
_writeImpl;
|
|
696
|
+
_finalImpl;
|
|
697
|
+
_defaultEncoding = "utf8";
|
|
698
|
+
_pendingWrites = 0;
|
|
699
|
+
_pendingEndCb = null;
|
|
700
|
+
constructor(opts) {
|
|
701
|
+
super(opts);
|
|
702
|
+
this.writableHighWaterMark = opts?.writableHighWaterMark ?? opts?.highWaterMark ?? getDefaultHighWaterMark(opts?.writableObjectMode ?? opts?.objectMode ?? false);
|
|
703
|
+
this.writableObjectMode = opts?.writableObjectMode ?? opts?.objectMode ?? false;
|
|
704
|
+
this.allowHalfOpen = opts?.allowHalfOpen !== false;
|
|
705
|
+
this._decodeStrings = opts?.decodeStrings !== false;
|
|
706
|
+
if (opts?.write) this._writeImpl = opts.write;
|
|
707
|
+
if (opts?.final) this._finalImpl = opts.final;
|
|
708
|
+
if (!this.allowHalfOpen) {
|
|
709
|
+
this.once("end", () => {
|
|
710
|
+
if (!this.writableEnded) {
|
|
711
|
+
nextTick(() => this.end());
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
_write(chunk, encoding, callback) {
|
|
717
|
+
if (this._writeImpl) {
|
|
718
|
+
this._writeImpl.call(this, chunk, encoding, callback);
|
|
719
|
+
} else {
|
|
720
|
+
callback();
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
_final(callback) {
|
|
724
|
+
if (this._finalImpl) {
|
|
725
|
+
this._finalImpl.call(this, callback);
|
|
726
|
+
} else {
|
|
727
|
+
callback();
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
destroy(error) {
|
|
731
|
+
if (this.destroyed) return this;
|
|
732
|
+
this.writable = false;
|
|
733
|
+
return super.destroy(error);
|
|
734
|
+
}
|
|
735
|
+
write(chunk, encoding, callback) {
|
|
736
|
+
if (typeof encoding === "function") {
|
|
737
|
+
callback = encoding;
|
|
738
|
+
encoding = void 0;
|
|
739
|
+
}
|
|
740
|
+
if (encoding === void 0) encoding = this._defaultEncoding;
|
|
741
|
+
if (this._decodeStrings && !this.writableObjectMode && typeof chunk === "string") {
|
|
742
|
+
const BufCtor = globalThis.Buffer;
|
|
743
|
+
if (BufCtor) {
|
|
744
|
+
chunk = BufCtor.from(chunk, encoding);
|
|
745
|
+
encoding = "buffer";
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if (typeof chunk !== "string" && !this.writableObjectMode) {
|
|
749
|
+
const BufCtor = globalThis.Buffer;
|
|
750
|
+
if (BufCtor && BufCtor.isBuffer(chunk) || chunk instanceof Uint8Array) {
|
|
751
|
+
encoding = "buffer";
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
if (this.writableEnded) {
|
|
755
|
+
const err = new Error("write after end");
|
|
756
|
+
const cb2 = callback || (() => {
|
|
757
|
+
});
|
|
758
|
+
nextTick(() => {
|
|
759
|
+
cb2(err);
|
|
760
|
+
this.emit("error", err);
|
|
761
|
+
});
|
|
762
|
+
return false;
|
|
763
|
+
}
|
|
764
|
+
this.writableLength += this.writableObjectMode ? 1 : chunk?.length ?? 1;
|
|
765
|
+
if (this.writableCorked > 0) {
|
|
766
|
+
this._duplexCorkedBuffer.push({ chunk, encoding, callback: callback || (() => {
|
|
767
|
+
}) });
|
|
768
|
+
return this.writableLength < this.writableHighWaterMark;
|
|
769
|
+
}
|
|
770
|
+
const belowHWM = this.writableLength < this.writableHighWaterMark;
|
|
771
|
+
if (!belowHWM) {
|
|
772
|
+
this.writableNeedDrain = true;
|
|
773
|
+
}
|
|
774
|
+
const cb = callback || (() => {
|
|
775
|
+
});
|
|
776
|
+
this._pendingWrites++;
|
|
777
|
+
this._write(chunk, encoding, (err) => {
|
|
778
|
+
this._pendingWrites--;
|
|
779
|
+
this.writableLength -= this.writableObjectMode ? 1 : chunk?.length ?? 1;
|
|
780
|
+
if (err) {
|
|
781
|
+
nextTick(() => {
|
|
782
|
+
cb(err);
|
|
783
|
+
this.emit("error", err);
|
|
784
|
+
});
|
|
785
|
+
} else {
|
|
786
|
+
nextTick(() => {
|
|
787
|
+
cb();
|
|
788
|
+
if (this.writableNeedDrain && this.writableLength < this.writableHighWaterMark) {
|
|
789
|
+
this.writableNeedDrain = false;
|
|
790
|
+
this.emit("drain");
|
|
791
|
+
}
|
|
792
|
+
if (this._pendingWrites === 0 && this._pendingEndCb) {
|
|
793
|
+
const endCb = this._pendingEndCb;
|
|
794
|
+
this._pendingEndCb = null;
|
|
795
|
+
endCb();
|
|
796
|
+
}
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
return belowHWM;
|
|
801
|
+
}
|
|
802
|
+
end(chunk, encoding, callback) {
|
|
803
|
+
if (typeof chunk === "function") {
|
|
804
|
+
callback = chunk;
|
|
805
|
+
chunk = void 0;
|
|
806
|
+
}
|
|
807
|
+
if (typeof encoding === "function") {
|
|
808
|
+
callback = encoding;
|
|
809
|
+
encoding = void 0;
|
|
810
|
+
}
|
|
811
|
+
if (chunk !== void 0 && chunk !== null) {
|
|
812
|
+
this.write(chunk, encoding);
|
|
813
|
+
}
|
|
814
|
+
this.writableEnded = true;
|
|
815
|
+
const doFinal = () => {
|
|
816
|
+
this._final((err) => {
|
|
817
|
+
this.writableFinished = true;
|
|
818
|
+
nextTick(() => {
|
|
819
|
+
if (err) this.emit("error", err);
|
|
820
|
+
this.emit("finish");
|
|
821
|
+
nextTick(() => this.emit("close"));
|
|
822
|
+
if (callback) callback();
|
|
823
|
+
});
|
|
824
|
+
});
|
|
825
|
+
};
|
|
826
|
+
if (this._pendingWrites > 0) {
|
|
827
|
+
this._pendingEndCb = doFinal;
|
|
828
|
+
} else {
|
|
829
|
+
doFinal();
|
|
830
|
+
}
|
|
831
|
+
return this;
|
|
832
|
+
}
|
|
833
|
+
cork() {
|
|
834
|
+
this.writableCorked++;
|
|
835
|
+
}
|
|
836
|
+
uncork() {
|
|
837
|
+
if (this.writableCorked > 0) {
|
|
838
|
+
this.writableCorked--;
|
|
839
|
+
if (this.writableCorked === 0 && this._duplexCorkedBuffer.length > 0) {
|
|
840
|
+
const buffered = this._duplexCorkedBuffer.splice(0);
|
|
841
|
+
for (const { chunk, encoding, callback } of buffered) {
|
|
842
|
+
this._write(chunk, encoding, (err) => {
|
|
843
|
+
this.writableLength -= this.writableObjectMode ? 1 : chunk?.length ?? 1;
|
|
844
|
+
if (err) {
|
|
845
|
+
callback(err);
|
|
846
|
+
this.emit("error", err);
|
|
847
|
+
} else {
|
|
848
|
+
callback();
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
if (this.writableNeedDrain && this.writableLength < this.writableHighWaterMark) {
|
|
853
|
+
this.writableNeedDrain = false;
|
|
854
|
+
nextTick(() => this.emit("drain"));
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
setDefaultEncoding(encoding) {
|
|
860
|
+
this._defaultEncoding = encoding;
|
|
861
|
+
return this;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
class Transform extends Duplex {
|
|
865
|
+
_transformImpl;
|
|
866
|
+
_flushImpl;
|
|
867
|
+
constructor(opts) {
|
|
868
|
+
super({
|
|
869
|
+
...opts,
|
|
870
|
+
write: void 0
|
|
871
|
+
// Override write to use transform
|
|
872
|
+
});
|
|
873
|
+
if (opts?.transform) this._transformImpl = opts.transform;
|
|
874
|
+
if (opts?.flush) this._flushImpl = opts.flush;
|
|
875
|
+
}
|
|
876
|
+
_transform(chunk, encoding, callback) {
|
|
877
|
+
if (this._transformImpl) {
|
|
878
|
+
this._transformImpl.call(this, chunk, encoding, callback);
|
|
879
|
+
} else {
|
|
880
|
+
callback(null, chunk);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
_flush(callback) {
|
|
884
|
+
if (this._flushImpl) {
|
|
885
|
+
this._flushImpl.call(this, callback);
|
|
886
|
+
} else {
|
|
887
|
+
callback();
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
_write(chunk, encoding, callback) {
|
|
891
|
+
this._transform(chunk, encoding, (err, data) => {
|
|
892
|
+
if (err) {
|
|
893
|
+
callback(err);
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
if (data !== void 0 && data !== null) {
|
|
897
|
+
this.push(data);
|
|
898
|
+
}
|
|
899
|
+
callback();
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
_final(callback) {
|
|
903
|
+
this._flush((err, data) => {
|
|
904
|
+
if (err) {
|
|
905
|
+
callback(err);
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
if (data !== void 0 && data !== null) {
|
|
909
|
+
this.push(data);
|
|
910
|
+
}
|
|
911
|
+
this.push(null);
|
|
912
|
+
callback();
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
class PassThrough extends Transform {
|
|
917
|
+
constructor(opts) {
|
|
918
|
+
super({
|
|
919
|
+
...opts,
|
|
920
|
+
transform(chunk, _encoding, callback) {
|
|
921
|
+
callback(null, chunk);
|
|
922
|
+
}
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
function pipeline(...args) {
|
|
927
|
+
const callback = typeof args[args.length - 1] === "function" ? args.pop() : void 0;
|
|
928
|
+
const streams = args;
|
|
929
|
+
if (streams.length < 2) {
|
|
930
|
+
throw new Error("pipeline requires at least 2 streams");
|
|
931
|
+
}
|
|
932
|
+
let error = null;
|
|
933
|
+
function onError(err) {
|
|
934
|
+
if (!error) {
|
|
935
|
+
error = err;
|
|
936
|
+
for (const stream of streams) {
|
|
937
|
+
if (typeof stream.destroy === "function") {
|
|
938
|
+
stream.destroy();
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
if (callback) callback(err);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
let current = streams[0];
|
|
945
|
+
for (let i = 1; i < streams.length; i++) {
|
|
946
|
+
const next = streams[i];
|
|
947
|
+
current.pipe(next);
|
|
948
|
+
current.on("error", onError);
|
|
949
|
+
current = next;
|
|
950
|
+
}
|
|
951
|
+
const last = streams[streams.length - 1];
|
|
952
|
+
last.on("error", onError);
|
|
953
|
+
last.on("finish", () => {
|
|
954
|
+
if (callback && !error) callback(null);
|
|
955
|
+
});
|
|
956
|
+
return last;
|
|
957
|
+
}
|
|
958
|
+
function finished(stream, optsOrCb, callback) {
|
|
959
|
+
let cb;
|
|
960
|
+
let _opts = {};
|
|
961
|
+
if (typeof optsOrCb === "function") {
|
|
962
|
+
cb = optsOrCb;
|
|
963
|
+
} else {
|
|
964
|
+
_opts = optsOrCb || {};
|
|
965
|
+
cb = callback;
|
|
966
|
+
}
|
|
967
|
+
let called = false;
|
|
968
|
+
function done(err) {
|
|
969
|
+
if (!called) {
|
|
970
|
+
called = true;
|
|
971
|
+
cb(err);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
const onFinish = () => done();
|
|
975
|
+
const onEnd = () => done();
|
|
976
|
+
const onError = (err) => done(err);
|
|
977
|
+
const onClose = () => {
|
|
978
|
+
if (!stream.writableFinished && !stream.readableEnded) {
|
|
979
|
+
done(new Error("premature close"));
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
stream.on("finish", onFinish);
|
|
983
|
+
stream.on("end", onEnd);
|
|
984
|
+
stream.on("error", onError);
|
|
985
|
+
stream.on("close", onClose);
|
|
986
|
+
const isWritableStream = typeof stream.write === "function";
|
|
987
|
+
const isReadableStream = typeof stream.read === "function";
|
|
988
|
+
const writableFinished = stream.writableFinished === true;
|
|
989
|
+
const readableEnded = stream.readableEnded === true;
|
|
990
|
+
const destroyed = stream.destroyed === true;
|
|
991
|
+
if (destroyed) {
|
|
992
|
+
queueMicrotask(() => done(stream._err || null));
|
|
993
|
+
} else if (isWritableStream && !isReadableStream && writableFinished) {
|
|
994
|
+
queueMicrotask(() => done());
|
|
995
|
+
} else if (!isWritableStream && isReadableStream && readableEnded) {
|
|
996
|
+
queueMicrotask(() => done());
|
|
997
|
+
} else if (isWritableStream && isReadableStream && writableFinished && readableEnded) {
|
|
998
|
+
queueMicrotask(() => done());
|
|
999
|
+
}
|
|
1000
|
+
return function cleanup() {
|
|
1001
|
+
stream.removeListener("finish", onFinish);
|
|
1002
|
+
stream.removeListener("end", onEnd);
|
|
1003
|
+
stream.removeListener("error", onError);
|
|
1004
|
+
stream.removeListener("close", onClose);
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
function addAbortSignal(signal, stream) {
|
|
1008
|
+
if (!(signal instanceof AbortSignal)) {
|
|
1009
|
+
throw new TypeError("The first argument must be an AbortSignal");
|
|
1010
|
+
}
|
|
1011
|
+
if (!(stream instanceof Stream)) {
|
|
1012
|
+
throw new TypeError("The second argument must be a Stream");
|
|
1013
|
+
}
|
|
1014
|
+
if (signal.aborted) {
|
|
1015
|
+
stream.destroy(new Error("The operation was aborted"));
|
|
1016
|
+
} else {
|
|
1017
|
+
const onAbort = () => {
|
|
1018
|
+
stream.destroy(new Error("The operation was aborted"));
|
|
1019
|
+
};
|
|
1020
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1021
|
+
stream.once("close", () => {
|
|
1022
|
+
signal.removeEventListener("abort", onAbort);
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
return stream;
|
|
1026
|
+
}
|
|
1027
|
+
function isReadable(stream) {
|
|
1028
|
+
if (stream == null) return false;
|
|
1029
|
+
const s = stream;
|
|
1030
|
+
if (typeof s.readable !== "boolean") return false;
|
|
1031
|
+
if (typeof s.read !== "function") return false;
|
|
1032
|
+
if (s.destroyed === true) return false;
|
|
1033
|
+
if (s.readableEnded === true) return false;
|
|
1034
|
+
return s.readable === true;
|
|
1035
|
+
}
|
|
1036
|
+
function isWritable(stream) {
|
|
1037
|
+
if (stream == null) return false;
|
|
1038
|
+
const s = stream;
|
|
1039
|
+
if (typeof s.writable !== "boolean") return false;
|
|
1040
|
+
if (typeof s.write !== "function") return false;
|
|
1041
|
+
if (s.destroyed === true) return false;
|
|
1042
|
+
if (s.writableEnded === true) return false;
|
|
1043
|
+
return s.writable === true;
|
|
1044
|
+
}
|
|
1045
|
+
function isDestroyed(stream) {
|
|
1046
|
+
if (stream == null) return false;
|
|
1047
|
+
return stream.destroyed === true;
|
|
1048
|
+
}
|
|
1049
|
+
function isDisturbed(stream) {
|
|
1050
|
+
if (stream == null) return false;
|
|
1051
|
+
const s = stream;
|
|
1052
|
+
return s.readableDidRead === true || s.readableFlowing !== null && s.readableFlowing !== void 0;
|
|
1053
|
+
}
|
|
1054
|
+
function isErrored(stream) {
|
|
1055
|
+
if (stream == null) return false;
|
|
1056
|
+
const s = stream;
|
|
1057
|
+
if (s.destroyed === true && typeof s.readable === "boolean" && s.readable === false) return true;
|
|
1058
|
+
if (s.destroyed === true && typeof s.writable === "boolean" && s.writable === false) return true;
|
|
1059
|
+
return false;
|
|
1060
|
+
}
|
|
1061
|
+
const _default = Object.assign(Stream, {
|
|
1062
|
+
Stream,
|
|
1063
|
+
Readable,
|
|
1064
|
+
Writable,
|
|
1065
|
+
Duplex,
|
|
1066
|
+
Transform,
|
|
1067
|
+
PassThrough,
|
|
1068
|
+
pipeline,
|
|
1069
|
+
finished,
|
|
1070
|
+
addAbortSignal,
|
|
1071
|
+
isReadable,
|
|
1072
|
+
isWritable,
|
|
1073
|
+
isDestroyed,
|
|
1074
|
+
isDisturbed,
|
|
1075
|
+
isErrored,
|
|
1076
|
+
getDefaultHighWaterMark,
|
|
1077
|
+
setDefaultHighWaterMark
|
|
1078
|
+
});
|
|
1079
|
+
var index_default = _default;
|
|
4
1080
|
export {
|
|
5
|
-
|
|
1081
|
+
Duplex,
|
|
1082
|
+
PassThrough,
|
|
1083
|
+
Readable,
|
|
1084
|
+
Stream,
|
|
1085
|
+
Transform,
|
|
1086
|
+
Writable,
|
|
1087
|
+
addAbortSignal,
|
|
1088
|
+
index_default as default,
|
|
1089
|
+
finished,
|
|
1090
|
+
getDefaultHighWaterMark,
|
|
1091
|
+
isDestroyed,
|
|
1092
|
+
isDisturbed,
|
|
1093
|
+
isErrored,
|
|
1094
|
+
isReadable,
|
|
1095
|
+
isWritable,
|
|
1096
|
+
pipeline,
|
|
1097
|
+
setDefaultHighWaterMark
|
|
6
1098
|
};
|