@scelar/nodepod 1.0.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/LICENSE +43 -0
- package/README.md +240 -0
- package/dist/child_process-BJOMsZje.js +8233 -0
- package/dist/child_process-BJOMsZje.js.map +1 -0
- package/dist/child_process-Cj8vOcuc.cjs +7434 -0
- package/dist/child_process-Cj8vOcuc.cjs.map +1 -0
- package/dist/index-Cb1Cgdnd.js +35308 -0
- package/dist/index-Cb1Cgdnd.js.map +1 -0
- package/dist/index-DsMGS-xc.cjs +37195 -0
- package/dist/index-DsMGS-xc.cjs.map +1 -0
- package/dist/index.cjs +65 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +59 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +95 -0
- package/src/__tests__/smoke.test.ts +11 -0
- package/src/constants/cdn-urls.ts +18 -0
- package/src/constants/config.ts +236 -0
- package/src/cross-origin.ts +26 -0
- package/src/engine-factory.ts +176 -0
- package/src/engine-types.ts +56 -0
- package/src/helpers/byte-encoding.ts +39 -0
- package/src/helpers/digest.ts +9 -0
- package/src/helpers/event-loop.ts +96 -0
- package/src/helpers/wasm-cache.ts +133 -0
- package/src/iframe-sandbox.ts +141 -0
- package/src/index.ts +192 -0
- package/src/isolation-helpers.ts +148 -0
- package/src/memory-volume.ts +941 -0
- package/src/module-transformer.ts +368 -0
- package/src/packages/archive-extractor.ts +248 -0
- package/src/packages/browser-bundler.ts +284 -0
- package/src/packages/installer.ts +396 -0
- package/src/packages/registry-client.ts +131 -0
- package/src/packages/version-resolver.ts +411 -0
- package/src/polyfills/assert.ts +384 -0
- package/src/polyfills/async_hooks.ts +144 -0
- package/src/polyfills/buffer.ts +628 -0
- package/src/polyfills/child_process.ts +2288 -0
- package/src/polyfills/chokidar.ts +336 -0
- package/src/polyfills/cluster.ts +106 -0
- package/src/polyfills/console.ts +136 -0
- package/src/polyfills/constants.ts +123 -0
- package/src/polyfills/crypto.ts +885 -0
- package/src/polyfills/dgram.ts +87 -0
- package/src/polyfills/diagnostics_channel.ts +76 -0
- package/src/polyfills/dns.ts +134 -0
- package/src/polyfills/domain.ts +68 -0
- package/src/polyfills/esbuild.ts +854 -0
- package/src/polyfills/events.ts +276 -0
- package/src/polyfills/fs.ts +2888 -0
- package/src/polyfills/fsevents.ts +79 -0
- package/src/polyfills/http.ts +1449 -0
- package/src/polyfills/http2.ts +199 -0
- package/src/polyfills/https.ts +76 -0
- package/src/polyfills/inspector.ts +62 -0
- package/src/polyfills/lightningcss.ts +105 -0
- package/src/polyfills/module.ts +191 -0
- package/src/polyfills/net.ts +353 -0
- package/src/polyfills/os.ts +238 -0
- package/src/polyfills/path.ts +206 -0
- package/src/polyfills/perf_hooks.ts +102 -0
- package/src/polyfills/process.ts +690 -0
- package/src/polyfills/punycode.ts +159 -0
- package/src/polyfills/querystring.ts +93 -0
- package/src/polyfills/quic.ts +118 -0
- package/src/polyfills/readdirp.ts +229 -0
- package/src/polyfills/readline.ts +692 -0
- package/src/polyfills/repl.ts +134 -0
- package/src/polyfills/rollup.ts +119 -0
- package/src/polyfills/sea.ts +33 -0
- package/src/polyfills/sqlite.ts +78 -0
- package/src/polyfills/stream.ts +1620 -0
- package/src/polyfills/string_decoder.ts +25 -0
- package/src/polyfills/tailwindcss-oxide.ts +309 -0
- package/src/polyfills/test.ts +197 -0
- package/src/polyfills/timers.ts +32 -0
- package/src/polyfills/tls.ts +105 -0
- package/src/polyfills/trace_events.ts +50 -0
- package/src/polyfills/tty.ts +71 -0
- package/src/polyfills/url.ts +174 -0
- package/src/polyfills/util.ts +559 -0
- package/src/polyfills/v8.ts +126 -0
- package/src/polyfills/vm.ts +132 -0
- package/src/polyfills/volume-registry.ts +15 -0
- package/src/polyfills/wasi.ts +44 -0
- package/src/polyfills/worker_threads.ts +326 -0
- package/src/polyfills/ws.ts +595 -0
- package/src/polyfills/zlib.ts +881 -0
- package/src/request-proxy.ts +716 -0
- package/src/script-engine.ts +3375 -0
- package/src/sdk/nodepod-fs.ts +93 -0
- package/src/sdk/nodepod-process.ts +86 -0
- package/src/sdk/nodepod-terminal.ts +350 -0
- package/src/sdk/nodepod.ts +509 -0
- package/src/sdk/types.ts +70 -0
- package/src/shell/commands/bun.ts +121 -0
- package/src/shell/commands/directory.ts +297 -0
- package/src/shell/commands/file-ops.ts +525 -0
- package/src/shell/commands/git.ts +2142 -0
- package/src/shell/commands/node.ts +80 -0
- package/src/shell/commands/npm.ts +198 -0
- package/src/shell/commands/pm-types.ts +45 -0
- package/src/shell/commands/pnpm.ts +82 -0
- package/src/shell/commands/search.ts +264 -0
- package/src/shell/commands/shell-env.ts +352 -0
- package/src/shell/commands/text-processing.ts +1152 -0
- package/src/shell/commands/yarn.ts +84 -0
- package/src/shell/shell-builtins.ts +19 -0
- package/src/shell/shell-helpers.ts +250 -0
- package/src/shell/shell-interpreter.ts +514 -0
- package/src/shell/shell-parser.ts +429 -0
- package/src/shell/shell-types.ts +85 -0
- package/src/syntax-transforms.ts +561 -0
- package/src/threading/engine-worker.ts +64 -0
- package/src/threading/inline-worker.ts +372 -0
- package/src/threading/offload-types.ts +112 -0
- package/src/threading/offload-worker.ts +383 -0
- package/src/threading/offload.ts +271 -0
- package/src/threading/process-context.ts +92 -0
- package/src/threading/process-handle.ts +275 -0
- package/src/threading/process-manager.ts +956 -0
- package/src/threading/process-worker-entry.ts +854 -0
- package/src/threading/shared-vfs.ts +352 -0
- package/src/threading/sync-channel.ts +135 -0
- package/src/threading/task-queue.ts +177 -0
- package/src/threading/vfs-bridge.ts +231 -0
- package/src/threading/worker-pool.ts +233 -0
- package/src/threading/worker-protocol.ts +358 -0
- package/src/threading/worker-vfs.ts +218 -0
- package/src/types/externals.d.ts +38 -0
- package/src/types/fs-streams.ts +142 -0
- package/src/types/manifest.ts +17 -0
- package/src/worker-sandbox.ts +90 -0
|
@@ -0,0 +1,1620 @@
|
|
|
1
|
+
// Stream polyfill -- Readable, Writable, Duplex, Transform, PassThrough, pipeline, finished.
|
|
2
|
+
// Uses function constructors (not classes) so Readable.call(this) works for pre-ES6 inheritance.
|
|
3
|
+
|
|
4
|
+
import { EventEmitter } from "./events";
|
|
5
|
+
import { Buffer } from "./buffer";
|
|
6
|
+
|
|
7
|
+
function bufferByteLength(chunk: unknown): number {
|
|
8
|
+
if (chunk instanceof Uint8Array) return chunk.byteLength;
|
|
9
|
+
if (typeof chunk === "string") return Buffer.byteLength(chunk);
|
|
10
|
+
return 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Readable
|
|
14
|
+
|
|
15
|
+
export interface Readable extends EventEmitter {
|
|
16
|
+
readable: boolean;
|
|
17
|
+
readableEnded: boolean;
|
|
18
|
+
readableFlowing: boolean | null;
|
|
19
|
+
destroyed: boolean;
|
|
20
|
+
closed: boolean;
|
|
21
|
+
errored: Error | null;
|
|
22
|
+
readableObjectMode: boolean;
|
|
23
|
+
readableHighWaterMark: number;
|
|
24
|
+
readableDidRead: boolean;
|
|
25
|
+
readableAborted: boolean;
|
|
26
|
+
_readableState: any;
|
|
27
|
+
_read(size: number): void;
|
|
28
|
+
_destroy(err: Error | null, cb: (err?: Error | null) => void): void;
|
|
29
|
+
readonly readableLength: number;
|
|
30
|
+
readonly readableEncoding: BufferEncoding | null;
|
|
31
|
+
_rawBind(evt: string | symbol, fn: (...args: unknown[]) => void): this;
|
|
32
|
+
on(evt: string | symbol, fn: (...args: any[]) => void): this;
|
|
33
|
+
addListener(evt: string | symbol, fn: (...args: any[]) => void): this;
|
|
34
|
+
once(evt: string | symbol, fn: (...args: any[]) => void): this;
|
|
35
|
+
push(chunk: any): boolean;
|
|
36
|
+
unshift(chunk: any): void;
|
|
37
|
+
read(amount?: number): any;
|
|
38
|
+
resume(): this;
|
|
39
|
+
pause(): this;
|
|
40
|
+
isPaused(): boolean;
|
|
41
|
+
pipe(target: any): any;
|
|
42
|
+
unpipe(target?: any): this;
|
|
43
|
+
setEncoding(enc: string): this;
|
|
44
|
+
close(cb?: (err?: Error | null) => void): void;
|
|
45
|
+
destroy(fault?: Error): this;
|
|
46
|
+
wrap(oldStream: EventEmitter): this;
|
|
47
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<unknown>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ReadableConstructor {
|
|
51
|
+
new (opts?: any): Readable;
|
|
52
|
+
(this: any, opts?: any): void;
|
|
53
|
+
prototype: any;
|
|
54
|
+
toWeb(readable: Readable): ReadableStream<Uint8Array>;
|
|
55
|
+
fromWeb(webStream: ReadableStream, opts?: any): Readable;
|
|
56
|
+
from(source: Iterable<unknown> | AsyncIterable<unknown>, opts?: any): Readable;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const Readable = function Readable(this: any, opts?: any) {
|
|
60
|
+
if (!this) return;
|
|
61
|
+
EventEmitter.call(this);
|
|
62
|
+
|
|
63
|
+
this._queue = [];
|
|
64
|
+
this._terminated = false;
|
|
65
|
+
this._active = false;
|
|
66
|
+
this._endFired = false;
|
|
67
|
+
this._endEmitted = false;
|
|
68
|
+
this._objectMode = false;
|
|
69
|
+
this._reading = false;
|
|
70
|
+
this._highWaterMark = 16384;
|
|
71
|
+
this._autoDestroy = true;
|
|
72
|
+
this._encoding = null;
|
|
73
|
+
this._readableByteLength = 0;
|
|
74
|
+
this._draining = false;
|
|
75
|
+
|
|
76
|
+
this.readable = true;
|
|
77
|
+
this.readableEnded = false;
|
|
78
|
+
this.readableFlowing = null;
|
|
79
|
+
this.destroyed = false;
|
|
80
|
+
this.closed = false;
|
|
81
|
+
this.errored = null;
|
|
82
|
+
this.readableObjectMode = false;
|
|
83
|
+
this.readableHighWaterMark = 16384;
|
|
84
|
+
this.readableDidRead = false;
|
|
85
|
+
this.readableAborted = false;
|
|
86
|
+
|
|
87
|
+
// _readableState proxy so libs like readable-stream can inspect internals
|
|
88
|
+
const self = this;
|
|
89
|
+
this._readableState = {
|
|
90
|
+
get objectMode() { return self._objectMode; },
|
|
91
|
+
get highWaterMark() { return self._highWaterMark; },
|
|
92
|
+
get ended() { return self._terminated; },
|
|
93
|
+
get endEmitted() { return self._endEmitted; },
|
|
94
|
+
set endEmitted(v: boolean) { self._endEmitted = v; },
|
|
95
|
+
get flowing() { return self.readableFlowing; },
|
|
96
|
+
set flowing(v: boolean | null) { self.readableFlowing = v; },
|
|
97
|
+
get reading() { return self._reading; },
|
|
98
|
+
get length() { return self.readableLength; },
|
|
99
|
+
get destroyed() { return self.destroyed; },
|
|
100
|
+
get errored() { return self.errored; },
|
|
101
|
+
get closed() { return self.closed; },
|
|
102
|
+
pipes: [],
|
|
103
|
+
awaitDrainWriters: null,
|
|
104
|
+
multiAwaitDrain: false,
|
|
105
|
+
readableListening: false,
|
|
106
|
+
resumeScheduled: false,
|
|
107
|
+
paused: true,
|
|
108
|
+
emitClose: true,
|
|
109
|
+
get autoDestroy() { return self._autoDestroy; },
|
|
110
|
+
defaultEncoding: "utf8",
|
|
111
|
+
needReadable: false,
|
|
112
|
+
emittedReadable: false,
|
|
113
|
+
readingMore: false,
|
|
114
|
+
dataEmitted: false,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (opts) {
|
|
118
|
+
if (opts.objectMode) {
|
|
119
|
+
this._objectMode = true;
|
|
120
|
+
this.readableObjectMode = true;
|
|
121
|
+
if (opts.highWaterMark === undefined) {
|
|
122
|
+
this._highWaterMark = 16;
|
|
123
|
+
this.readableHighWaterMark = 16;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (opts.highWaterMark !== undefined) {
|
|
127
|
+
this._highWaterMark = opts.highWaterMark;
|
|
128
|
+
this.readableHighWaterMark = opts.highWaterMark;
|
|
129
|
+
}
|
|
130
|
+
if (opts.autoDestroy !== undefined) {
|
|
131
|
+
this._autoDestroy = opts.autoDestroy;
|
|
132
|
+
}
|
|
133
|
+
if (opts.read) {
|
|
134
|
+
this._read = opts.read.bind(this);
|
|
135
|
+
}
|
|
136
|
+
if (opts.destroy) {
|
|
137
|
+
this._destroy = opts.destroy.bind(this);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} as unknown as ReadableConstructor;
|
|
141
|
+
|
|
142
|
+
Object.setPrototypeOf(Readable.prototype, EventEmitter.prototype);
|
|
143
|
+
|
|
144
|
+
Readable.prototype._read = function _read(_size: number): void {};
|
|
145
|
+
|
|
146
|
+
Readable.prototype._destroy = function _destroy(
|
|
147
|
+
err: Error | null,
|
|
148
|
+
callback: (err?: Error | null) => void,
|
|
149
|
+
): void {
|
|
150
|
+
callback(err);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// skips auto-flow logic so internal listeners don't trigger resume()
|
|
154
|
+
Readable.prototype._rawBind = function _rawBind(
|
|
155
|
+
evt: string | symbol,
|
|
156
|
+
fn: (...args: unknown[]) => void,
|
|
157
|
+
): any {
|
|
158
|
+
EventEmitter.prototype.addListener.call(this, evt as string, fn);
|
|
159
|
+
return this;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
Readable.prototype.on = function on(
|
|
163
|
+
evt: string | symbol,
|
|
164
|
+
fn: (...args: unknown[]) => void,
|
|
165
|
+
): any {
|
|
166
|
+
this._rawBind(evt, fn);
|
|
167
|
+
if (evt === "data" && !this._active) {
|
|
168
|
+
this.resume();
|
|
169
|
+
}
|
|
170
|
+
if (evt === "readable") {
|
|
171
|
+
this.readableFlowing = false;
|
|
172
|
+
}
|
|
173
|
+
// fire late 'end' listeners async (matches Node.js behavior)
|
|
174
|
+
if (evt === "end" && this._endEmitted) {
|
|
175
|
+
queueMicrotask(() => fn());
|
|
176
|
+
} else if (evt === "end" && this._terminated && this._queue.length === 0 && !this._endFired) {
|
|
177
|
+
this._endFired = true;
|
|
178
|
+
queueMicrotask(() => {
|
|
179
|
+
this._endEmitted = true;
|
|
180
|
+
this.emit("end");
|
|
181
|
+
if (this._autoDestroy) {
|
|
182
|
+
this.destroy();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return this;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
Readable.prototype.addListener = function addListener(
|
|
190
|
+
evt: string | symbol,
|
|
191
|
+
fn: (...args: unknown[]) => void,
|
|
192
|
+
): any {
|
|
193
|
+
return this.on(evt, fn);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
Readable.prototype.once = function once(
|
|
197
|
+
evt: string | symbol,
|
|
198
|
+
fn: (...args: unknown[]) => void,
|
|
199
|
+
): any {
|
|
200
|
+
if (evt === "end" && this._endEmitted) {
|
|
201
|
+
queueMicrotask(() => fn());
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
if (evt === "end" && this._terminated && this._queue.length === 0 && !this._endFired) {
|
|
205
|
+
this._endFired = true;
|
|
206
|
+
EventEmitter.prototype.once.call(this, evt as string, fn);
|
|
207
|
+
queueMicrotask(() => {
|
|
208
|
+
this._endEmitted = true;
|
|
209
|
+
this.emit("end");
|
|
210
|
+
if (this._autoDestroy) {
|
|
211
|
+
this.destroy();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
if (evt === "data" && !this._active) {
|
|
217
|
+
EventEmitter.prototype.once.call(this, evt as string, fn);
|
|
218
|
+
this.resume();
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
return EventEmitter.prototype.once.call(this, evt as string, fn);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
Readable.prototype.push = function push(chunk: any): boolean {
|
|
225
|
+
if (chunk === null) {
|
|
226
|
+
this._terminated = true;
|
|
227
|
+
this.readableEnded = true;
|
|
228
|
+
this.readable = false;
|
|
229
|
+
if (this._active && this._queue.length === 0 && !this._endFired) {
|
|
230
|
+
this._endFired = true;
|
|
231
|
+
queueMicrotask(() => {
|
|
232
|
+
this._endEmitted = true;
|
|
233
|
+
this.emit("end");
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (this._objectMode) {
|
|
240
|
+
this._queue.push(chunk);
|
|
241
|
+
} else {
|
|
242
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
243
|
+
this._readableByteLength += bufferByteLength(bytes);
|
|
244
|
+
this._queue.push(bytes);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (this._active) {
|
|
248
|
+
this._drain();
|
|
249
|
+
}
|
|
250
|
+
return this._queue.length < this._highWaterMark;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
Readable.prototype.unshift = function unshift(chunk: any): void {
|
|
254
|
+
if (chunk === null) return;
|
|
255
|
+
if (this._objectMode) {
|
|
256
|
+
this._queue.unshift(chunk);
|
|
257
|
+
} else {
|
|
258
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
259
|
+
this._readableByteLength += bufferByteLength(bytes);
|
|
260
|
+
this._queue.unshift(bytes);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
Readable.prototype._drain = function _drain(): void {
|
|
265
|
+
if (this._draining) return;
|
|
266
|
+
this._draining = true;
|
|
267
|
+
while (this._queue.length > 0 && this._active) {
|
|
268
|
+
const item = this._queue.shift();
|
|
269
|
+
if (!this._objectMode) {
|
|
270
|
+
this._readableByteLength -= bufferByteLength(item);
|
|
271
|
+
}
|
|
272
|
+
this.readableDidRead = true;
|
|
273
|
+
if (this._encoding && item instanceof Uint8Array) {
|
|
274
|
+
this.emit("data", Buffer.from(item).toString(this._encoding));
|
|
275
|
+
} else {
|
|
276
|
+
this.emit("data", item);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
this._draining = false;
|
|
280
|
+
if (this._terminated && this._queue.length === 0 && !this._endFired) {
|
|
281
|
+
this._endFired = true;
|
|
282
|
+
queueMicrotask(() => {
|
|
283
|
+
this._endEmitted = true;
|
|
284
|
+
this.emit("end");
|
|
285
|
+
if (this._autoDestroy) {
|
|
286
|
+
this.destroy();
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
if (!this._terminated && this._queue.length === 0 && this._active && !this._reading) {
|
|
291
|
+
this._reading = true;
|
|
292
|
+
queueMicrotask(() => {
|
|
293
|
+
this._reading = false;
|
|
294
|
+
if (!this._terminated && this._active) {
|
|
295
|
+
this._read(this._highWaterMark);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
Readable.prototype.read = function read(amount?: number): any {
|
|
302
|
+
this.readableDidRead = true;
|
|
303
|
+
if (!this._reading && !this._terminated) {
|
|
304
|
+
this._reading = true;
|
|
305
|
+
this._read(amount ?? this._highWaterMark);
|
|
306
|
+
this._reading = false;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (this._queue.length === 0) return null;
|
|
310
|
+
|
|
311
|
+
if (this._objectMode) {
|
|
312
|
+
const item = this._queue.shift();
|
|
313
|
+
return item;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (amount === undefined || amount === 0) {
|
|
317
|
+
const combined = Buffer.concat(this._queue as Uint8Array[]);
|
|
318
|
+
this._readableByteLength = 0;
|
|
319
|
+
this._queue.length = 0;
|
|
320
|
+
return combined;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const pieces: Uint8Array[] = [];
|
|
324
|
+
let needed = amount;
|
|
325
|
+
while (needed > 0 && this._queue.length > 0) {
|
|
326
|
+
const front = this._queue[0] as Uint8Array;
|
|
327
|
+
if (front.length <= needed) {
|
|
328
|
+
pieces.push(this._queue.shift() as Uint8Array);
|
|
329
|
+
this._readableByteLength -= front.length;
|
|
330
|
+
needed -= front.length;
|
|
331
|
+
} else {
|
|
332
|
+
pieces.push(front.slice(0, needed));
|
|
333
|
+
this._queue[0] = front.slice(needed);
|
|
334
|
+
this._readableByteLength -= needed;
|
|
335
|
+
needed = 0;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return pieces.length > 0 ? Buffer.concat(pieces) : null;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
Readable.prototype.resume = function resume(): any {
|
|
342
|
+
this._active = true;
|
|
343
|
+
this.readableFlowing = true;
|
|
344
|
+
this._drain();
|
|
345
|
+
if (!this._terminated && this._queue.length === 0 && !this._reading) {
|
|
346
|
+
this._reading = true;
|
|
347
|
+
queueMicrotask(() => {
|
|
348
|
+
this._reading = false;
|
|
349
|
+
if (!this._terminated && this._active) {
|
|
350
|
+
this._read(this._highWaterMark);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
return this;
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
Readable.prototype.pause = function pause(): any {
|
|
358
|
+
this._active = false;
|
|
359
|
+
this.readableFlowing = false;
|
|
360
|
+
return this;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
Readable.prototype.isPaused = function isPaused(): boolean {
|
|
364
|
+
return !this._active;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
Readable.prototype.pipe = function pipe(target: any): any {
|
|
368
|
+
const self = this;
|
|
369
|
+
self.on("data", function onData(chunk: unknown) {
|
|
370
|
+
const needDrain = !target.write(chunk);
|
|
371
|
+
if (needDrain) {
|
|
372
|
+
self.pause();
|
|
373
|
+
target.once("drain", function onDrain() { self.resume(); });
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
self.on("end", function onEnd() {
|
|
377
|
+
target.end();
|
|
378
|
+
});
|
|
379
|
+
self.resume();
|
|
380
|
+
return target;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
Readable.prototype.unpipe = function unpipe(_target?: any): any {
|
|
384
|
+
this.removeAllListeners("data");
|
|
385
|
+
this.removeAllListeners("end");
|
|
386
|
+
return this;
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
Readable.prototype.setEncoding = function setEncoding(enc: string): any {
|
|
390
|
+
this._encoding = enc as BufferEncoding;
|
|
391
|
+
return this;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
Readable.prototype.close = function close(cb?: (err?: Error | null) => void): void {
|
|
395
|
+
this.destroy();
|
|
396
|
+
if (cb) cb(null);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
Readable.prototype.destroy = function destroy(fault?: Error): any {
|
|
400
|
+
if (this.destroyed) return this;
|
|
401
|
+
this.destroyed = true;
|
|
402
|
+
if (fault) this.errored = fault;
|
|
403
|
+
|
|
404
|
+
this._destroy(fault ?? null, (err: Error | null | undefined) => {
|
|
405
|
+
if (err && !fault) this.errored = err;
|
|
406
|
+
this._queue.length = 0;
|
|
407
|
+
this._readableByteLength = 0;
|
|
408
|
+
this._terminated = true;
|
|
409
|
+
this.readable = false;
|
|
410
|
+
if (err || fault) this.emit("error", err || fault);
|
|
411
|
+
this.closed = true;
|
|
412
|
+
this.emit("close");
|
|
413
|
+
});
|
|
414
|
+
return this;
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
Readable.prototype.wrap = function wrap(oldStream: any): any {
|
|
418
|
+
const self = this;
|
|
419
|
+
oldStream.on("data", function onData(chunk: unknown) {
|
|
420
|
+
self.push(chunk);
|
|
421
|
+
});
|
|
422
|
+
oldStream.on("end", function onEnd() {
|
|
423
|
+
self.push(null);
|
|
424
|
+
});
|
|
425
|
+
oldStream.on("error", function onError(err: unknown) {
|
|
426
|
+
self.destroy(err as Error);
|
|
427
|
+
});
|
|
428
|
+
return this;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
Readable.prototype[Symbol.asyncIterator] = function asyncIterator(): AsyncIterableIterator<unknown> {
|
|
432
|
+
const stream = this;
|
|
433
|
+
const buffer: unknown[] = [];
|
|
434
|
+
let done = false;
|
|
435
|
+
let error: Error | null = null;
|
|
436
|
+
let waiting: ((val: IteratorResult<unknown>) => void) | null = null;
|
|
437
|
+
let waitingReject: ((err: Error) => void) | null = null;
|
|
438
|
+
|
|
439
|
+
const onData = (chunk: unknown) => {
|
|
440
|
+
if (waiting) {
|
|
441
|
+
const resolve = waiting;
|
|
442
|
+
waiting = null;
|
|
443
|
+
waitingReject = null;
|
|
444
|
+
resolve({ value: chunk, done: false });
|
|
445
|
+
} else {
|
|
446
|
+
buffer.push(chunk);
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const onEnd = () => {
|
|
451
|
+
done = true;
|
|
452
|
+
if (waiting) {
|
|
453
|
+
const resolve = waiting;
|
|
454
|
+
waiting = null;
|
|
455
|
+
waitingReject = null;
|
|
456
|
+
resolve({ value: undefined, done: true });
|
|
457
|
+
}
|
|
458
|
+
cleanup();
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const onError = (...args: unknown[]) => {
|
|
462
|
+
error = args[0] as Error;
|
|
463
|
+
if (waitingReject) {
|
|
464
|
+
const reject = waitingReject;
|
|
465
|
+
waiting = null;
|
|
466
|
+
waitingReject = null;
|
|
467
|
+
reject(error);
|
|
468
|
+
}
|
|
469
|
+
cleanup();
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const cleanup = () => {
|
|
473
|
+
stream.removeListener("data", onData);
|
|
474
|
+
stream.removeListener("end", onEnd);
|
|
475
|
+
stream.removeListener("error", onError);
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
stream._rawBind("data", onData);
|
|
479
|
+
stream._rawBind("end", onEnd);
|
|
480
|
+
stream._rawBind("error", onError);
|
|
481
|
+
if (!stream._active) stream.resume();
|
|
482
|
+
|
|
483
|
+
return {
|
|
484
|
+
next(): Promise<IteratorResult<unknown>> {
|
|
485
|
+
if (buffer.length > 0) {
|
|
486
|
+
return Promise.resolve({ value: buffer.shift(), done: false });
|
|
487
|
+
}
|
|
488
|
+
if (error) return Promise.reject(error);
|
|
489
|
+
if (done) return Promise.resolve({ value: undefined, done: true });
|
|
490
|
+
|
|
491
|
+
return new Promise<IteratorResult<unknown>>((resolve, reject) => {
|
|
492
|
+
waiting = resolve;
|
|
493
|
+
waitingReject = reject;
|
|
494
|
+
});
|
|
495
|
+
},
|
|
496
|
+
return(): Promise<IteratorResult<unknown>> {
|
|
497
|
+
cleanup();
|
|
498
|
+
stream.destroy();
|
|
499
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
500
|
+
},
|
|
501
|
+
throw(err: Error): Promise<IteratorResult<unknown>> {
|
|
502
|
+
cleanup();
|
|
503
|
+
stream.destroy(err);
|
|
504
|
+
return Promise.reject(err);
|
|
505
|
+
},
|
|
506
|
+
[Symbol.asyncIterator]() {
|
|
507
|
+
return this;
|
|
508
|
+
},
|
|
509
|
+
};
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
Object.defineProperty(Readable.prototype, "readableLength", {
|
|
513
|
+
get: function (this: any) {
|
|
514
|
+
if (this._objectMode) return this._queue.length;
|
|
515
|
+
return this._readableByteLength;
|
|
516
|
+
},
|
|
517
|
+
configurable: true,
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
Object.defineProperty(Readable.prototype, "readableEncoding", {
|
|
521
|
+
get: function (this: any) {
|
|
522
|
+
return this._encoding;
|
|
523
|
+
},
|
|
524
|
+
configurable: true,
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
Readable.toWeb = function toWeb(readable: any): ReadableStream<Uint8Array> {
|
|
528
|
+
return new ReadableStream({
|
|
529
|
+
start(controller) {
|
|
530
|
+
readable.on("data", (chunk: unknown) => {
|
|
531
|
+
const buf =
|
|
532
|
+
chunk instanceof Uint8Array ? chunk : Buffer.from(String(chunk));
|
|
533
|
+
controller.enqueue(buf);
|
|
534
|
+
});
|
|
535
|
+
readable.on("end", () => {
|
|
536
|
+
controller.close();
|
|
537
|
+
});
|
|
538
|
+
readable.on("error", (err: unknown) => {
|
|
539
|
+
controller.error(err);
|
|
540
|
+
});
|
|
541
|
+
readable.resume();
|
|
542
|
+
},
|
|
543
|
+
cancel() {
|
|
544
|
+
readable.destroy();
|
|
545
|
+
},
|
|
546
|
+
});
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
Readable.fromWeb = function fromWeb(
|
|
550
|
+
webStream: ReadableStream,
|
|
551
|
+
opts?: { objectMode?: boolean; highWaterMark?: number },
|
|
552
|
+
): Readable {
|
|
553
|
+
const reader = webStream.getReader();
|
|
554
|
+
const stream = new Readable({
|
|
555
|
+
...opts,
|
|
556
|
+
read() {
|
|
557
|
+
reader.read().then(
|
|
558
|
+
({ value, done }: { value: any; done: boolean }) => {
|
|
559
|
+
if (done) {
|
|
560
|
+
stream.push(null);
|
|
561
|
+
} else {
|
|
562
|
+
stream.push(value);
|
|
563
|
+
}
|
|
564
|
+
},
|
|
565
|
+
(err: Error) => {
|
|
566
|
+
stream.destroy(err);
|
|
567
|
+
},
|
|
568
|
+
);
|
|
569
|
+
},
|
|
570
|
+
});
|
|
571
|
+
return stream;
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
Readable.from = function from(
|
|
575
|
+
source: Iterable<unknown> | AsyncIterable<unknown>,
|
|
576
|
+
_opts?: { objectMode?: boolean; highWaterMark?: number },
|
|
577
|
+
): Readable {
|
|
578
|
+
const stream = new Readable(_opts);
|
|
579
|
+
(async () => {
|
|
580
|
+
try {
|
|
581
|
+
for await (const item of source as AsyncIterable<unknown>) {
|
|
582
|
+
if (item !== null && item !== undefined) {
|
|
583
|
+
const data = typeof item === "string" ? Buffer.from(item) : item;
|
|
584
|
+
stream.push(data as Buffer);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
stream.push(null);
|
|
588
|
+
} catch (err) {
|
|
589
|
+
stream.destroy(err as Error);
|
|
590
|
+
}
|
|
591
|
+
})();
|
|
592
|
+
return stream;
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
// Writable
|
|
596
|
+
|
|
597
|
+
export interface Writable extends EventEmitter {
|
|
598
|
+
writable: boolean;
|
|
599
|
+
writableEnded: boolean;
|
|
600
|
+
writableFinished: boolean;
|
|
601
|
+
writableNeedDrain: boolean;
|
|
602
|
+
destroyed: boolean;
|
|
603
|
+
closed: boolean;
|
|
604
|
+
errored: Error | null;
|
|
605
|
+
writableObjectMode: boolean;
|
|
606
|
+
writableHighWaterMark: number;
|
|
607
|
+
writableCorked: number;
|
|
608
|
+
_writableState: any;
|
|
609
|
+
_write(chunk: any, encoding: string, callback: (err?: Error | null) => void): void;
|
|
610
|
+
_writev?(chunks: Array<{ chunk: any; encoding: string }>, callback: (err?: Error | null) => void): void;
|
|
611
|
+
_final(callback: (err?: Error | null) => void): void;
|
|
612
|
+
_destroy(err: Error | null, cb: (err?: Error | null) => void): void;
|
|
613
|
+
readonly writableLength: number;
|
|
614
|
+
write(chunk: any, encOrCb?: any, cb?: any): boolean;
|
|
615
|
+
end(chunkOrCb?: any, encOrCb?: any, cb?: any): this;
|
|
616
|
+
getBuffer(): Buffer;
|
|
617
|
+
getBufferAsString(enc?: BufferEncoding): string;
|
|
618
|
+
close(cb?: (err?: Error | null) => void): void;
|
|
619
|
+
destroy(fault?: Error): this;
|
|
620
|
+
cork(): void;
|
|
621
|
+
uncork(): void;
|
|
622
|
+
setDefaultEncoding(enc: string): this;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
interface WritableConstructor {
|
|
626
|
+
new (opts?: any): Writable;
|
|
627
|
+
(this: any, opts?: any): void;
|
|
628
|
+
prototype: any;
|
|
629
|
+
toWeb(writable: Writable): WritableStream<Uint8Array>;
|
|
630
|
+
fromWeb(webStream: WritableStream, opts?: any): Writable;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export const Writable = function Writable(this: any, opts?: any) {
|
|
634
|
+
if (!this) return;
|
|
635
|
+
EventEmitter.call(this);
|
|
636
|
+
|
|
637
|
+
this._parts = [];
|
|
638
|
+
this._closed = false;
|
|
639
|
+
this._objectMode = false;
|
|
640
|
+
this._highWaterMark = 16384;
|
|
641
|
+
this._autoDestroy = true;
|
|
642
|
+
this._corked = 0;
|
|
643
|
+
this._corkedWrites = [];
|
|
644
|
+
this._writableByteLength = 0;
|
|
645
|
+
|
|
646
|
+
this.writable = true;
|
|
647
|
+
this.writableEnded = false;
|
|
648
|
+
this.writableFinished = false;
|
|
649
|
+
this.writableNeedDrain = false;
|
|
650
|
+
this.destroyed = false;
|
|
651
|
+
this.closed = false;
|
|
652
|
+
this.errored = null;
|
|
653
|
+
this.writableObjectMode = false;
|
|
654
|
+
this.writableHighWaterMark = 16384;
|
|
655
|
+
this.writableCorked = 0;
|
|
656
|
+
|
|
657
|
+
const self = this;
|
|
658
|
+
this._writableState = {
|
|
659
|
+
get objectMode() { return self._objectMode; },
|
|
660
|
+
get highWaterMark() { return self._highWaterMark; },
|
|
661
|
+
get finished() { return self.writableFinished; },
|
|
662
|
+
set finished(v: boolean) { self.writableFinished = v; },
|
|
663
|
+
get ended() { return self.writableEnded; },
|
|
664
|
+
set ended(v: boolean) { self.writableEnded = v; },
|
|
665
|
+
get destroyed() { return self.destroyed; },
|
|
666
|
+
get errored() { return self.errored; },
|
|
667
|
+
get closed() { return self.closed; },
|
|
668
|
+
get corked() { return self._corked; },
|
|
669
|
+
get length() { return self.writableLength; },
|
|
670
|
+
get needDrain() { return self.writableNeedDrain; },
|
|
671
|
+
writing: false,
|
|
672
|
+
errorEmitted: false,
|
|
673
|
+
emitClose: true,
|
|
674
|
+
get autoDestroy() { return self._autoDestroy; },
|
|
675
|
+
defaultEncoding: "utf8",
|
|
676
|
+
finalCalled: false,
|
|
677
|
+
ending: false,
|
|
678
|
+
bufferedIndex: 0,
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
if (opts) {
|
|
682
|
+
if (opts.objectMode) {
|
|
683
|
+
this._objectMode = true;
|
|
684
|
+
this.writableObjectMode = true;
|
|
685
|
+
if (opts.highWaterMark === undefined) {
|
|
686
|
+
this._highWaterMark = 16;
|
|
687
|
+
this.writableHighWaterMark = 16;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
if (opts.highWaterMark !== undefined) {
|
|
691
|
+
this._highWaterMark = opts.highWaterMark;
|
|
692
|
+
this.writableHighWaterMark = opts.highWaterMark;
|
|
693
|
+
}
|
|
694
|
+
if (opts.autoDestroy !== undefined) {
|
|
695
|
+
this._autoDestroy = opts.autoDestroy;
|
|
696
|
+
}
|
|
697
|
+
if (opts.write) {
|
|
698
|
+
this._write = opts.write.bind(this);
|
|
699
|
+
}
|
|
700
|
+
if (opts.writev) {
|
|
701
|
+
this._writev = opts.writev.bind(this);
|
|
702
|
+
}
|
|
703
|
+
if (opts.final) {
|
|
704
|
+
this._final = opts.final.bind(this);
|
|
705
|
+
}
|
|
706
|
+
if (opts.destroy) {
|
|
707
|
+
this._destroy = opts.destroy.bind(this);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
} as unknown as WritableConstructor;
|
|
711
|
+
|
|
712
|
+
Object.setPrototypeOf(Writable.prototype, EventEmitter.prototype);
|
|
713
|
+
|
|
714
|
+
Writable.prototype._write = function _write(
|
|
715
|
+
_chunk: any,
|
|
716
|
+
_encoding: string,
|
|
717
|
+
callback: (err?: Error | null) => void,
|
|
718
|
+
): void {
|
|
719
|
+
callback(null);
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
Writable.prototype._final = function _final(
|
|
723
|
+
callback: (err?: Error | null) => void,
|
|
724
|
+
): void {
|
|
725
|
+
callback(null);
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
Writable.prototype._destroy = function _destroy(
|
|
729
|
+
err: Error | null,
|
|
730
|
+
callback: (err?: Error | null) => void,
|
|
731
|
+
): void {
|
|
732
|
+
callback(err);
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
Writable.prototype.write = function write(
|
|
736
|
+
chunk: any,
|
|
737
|
+
encOrCb?: string | ((err?: Error | null) => void),
|
|
738
|
+
cb?: (err?: Error | null) => void,
|
|
739
|
+
): boolean {
|
|
740
|
+
if (this._closed) {
|
|
741
|
+
const fault = new Error("write after end");
|
|
742
|
+
if (typeof encOrCb === "function") {
|
|
743
|
+
encOrCb(fault);
|
|
744
|
+
} else if (cb) {
|
|
745
|
+
cb(fault);
|
|
746
|
+
}
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
const encoding = typeof encOrCb === "string" ? encOrCb : "utf8";
|
|
751
|
+
const callback = typeof encOrCb === "function" ? encOrCb : cb;
|
|
752
|
+
|
|
753
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
754
|
+
this._parts.push(bytes);
|
|
755
|
+
this._writableByteLength += bufferByteLength(bytes);
|
|
756
|
+
|
|
757
|
+
if (this._corked > 0) {
|
|
758
|
+
this._corkedWrites.push({ chunk, encoding, callback });
|
|
759
|
+
return this._writableByteLength < this._highWaterMark;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
this._write(chunk, encoding, (err: Error | null | undefined) => {
|
|
763
|
+
this._writableByteLength -= bufferByteLength(bytes);
|
|
764
|
+
if (callback) callback(err);
|
|
765
|
+
if (this.writableNeedDrain && this._writableByteLength < this._highWaterMark) {
|
|
766
|
+
this.writableNeedDrain = false;
|
|
767
|
+
this.emit("drain");
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
const belowHWM = this._writableByteLength < this._highWaterMark;
|
|
772
|
+
if (!belowHWM) {
|
|
773
|
+
this.writableNeedDrain = true;
|
|
774
|
+
}
|
|
775
|
+
return belowHWM;
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
Writable.prototype.end = function end(
|
|
779
|
+
chunkOrCb?: any,
|
|
780
|
+
encOrCb?: any,
|
|
781
|
+
cb?: () => void,
|
|
782
|
+
): any {
|
|
783
|
+
if (typeof chunkOrCb === "function") {
|
|
784
|
+
cb = chunkOrCb;
|
|
785
|
+
} else if (chunkOrCb !== undefined) {
|
|
786
|
+
this.write(chunkOrCb);
|
|
787
|
+
}
|
|
788
|
+
if (typeof encOrCb === "function") cb = encOrCb;
|
|
789
|
+
|
|
790
|
+
this._closed = true;
|
|
791
|
+
this.writable = false;
|
|
792
|
+
this.writableEnded = true;
|
|
793
|
+
|
|
794
|
+
const self = this;
|
|
795
|
+
const doFinish = () => {
|
|
796
|
+
self.writableFinished = true;
|
|
797
|
+
self.emit("finish");
|
|
798
|
+
if (self._autoDestroy) {
|
|
799
|
+
self.closed = true;
|
|
800
|
+
self.emit("close");
|
|
801
|
+
}
|
|
802
|
+
if (cb) cb();
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
queueMicrotask(() => {
|
|
806
|
+
self._final((err: Error | null | undefined) => {
|
|
807
|
+
if (err) {
|
|
808
|
+
self.errored = err;
|
|
809
|
+
self.emit("error", err);
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
doFinish();
|
|
813
|
+
});
|
|
814
|
+
});
|
|
815
|
+
return this;
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
Writable.prototype.getBuffer = function getBuffer(): Buffer {
|
|
819
|
+
return Buffer.concat(this._parts);
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
Writable.prototype.getBufferAsString = function getBufferAsString(enc?: BufferEncoding): string {
|
|
823
|
+
return this.getBuffer().toString(enc || "utf8");
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
Writable.prototype.close = function close(cb?: (err?: Error | null) => void): void {
|
|
827
|
+
this.destroy();
|
|
828
|
+
if (cb) cb(null);
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
Writable.prototype.destroy = function destroy(fault?: Error): any {
|
|
832
|
+
if (this.destroyed) return this;
|
|
833
|
+
this.destroyed = true;
|
|
834
|
+
if (fault) this.errored = fault;
|
|
835
|
+
|
|
836
|
+
this._destroy(fault ?? null, (err: Error | null | undefined) => {
|
|
837
|
+
if (err && !fault) this.errored = err;
|
|
838
|
+
this._parts.length = 0;
|
|
839
|
+
this._writableByteLength = 0;
|
|
840
|
+
this._closed = true;
|
|
841
|
+
this.writable = false;
|
|
842
|
+
if (err || fault) this.emit("error", err || fault);
|
|
843
|
+
this.closed = true;
|
|
844
|
+
this.emit("close");
|
|
845
|
+
});
|
|
846
|
+
return this;
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
Writable.prototype.cork = function cork(): void {
|
|
850
|
+
this._corked++;
|
|
851
|
+
this.writableCorked = this._corked;
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
Writable.prototype.uncork = function uncork(): void {
|
|
855
|
+
if (this._corked > 0) {
|
|
856
|
+
this._corked--;
|
|
857
|
+
this.writableCorked = this._corked;
|
|
858
|
+
}
|
|
859
|
+
if (this._corked === 0 && this._corkedWrites.length > 0) {
|
|
860
|
+
const writes = this._corkedWrites.splice(0);
|
|
861
|
+
if (this._writev) {
|
|
862
|
+
this._writev(
|
|
863
|
+
writes.map((w: any) => ({ chunk: w.chunk, encoding: w.encoding })),
|
|
864
|
+
(err: Error | null | undefined) => {
|
|
865
|
+
for (const w of writes) {
|
|
866
|
+
if (w.callback) w.callback(err);
|
|
867
|
+
}
|
|
868
|
+
},
|
|
869
|
+
);
|
|
870
|
+
} else {
|
|
871
|
+
for (const w of writes) {
|
|
872
|
+
this._write(w.chunk, w.encoding, (err: Error | null | undefined) => {
|
|
873
|
+
if (w.callback) w.callback(err);
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(_enc: string): any {
|
|
881
|
+
return this;
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
Object.defineProperty(Writable.prototype, "writableLength", {
|
|
885
|
+
get: function (this: any) {
|
|
886
|
+
if (this._objectMode) return this._parts.length;
|
|
887
|
+
return this._writableByteLength;
|
|
888
|
+
},
|
|
889
|
+
configurable: true,
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
Writable.toWeb = function toWeb(writable: any): WritableStream<Uint8Array> {
|
|
893
|
+
return new WritableStream({
|
|
894
|
+
write(chunk) {
|
|
895
|
+
return new Promise<void>((resolve, reject) => {
|
|
896
|
+
const ok = writable.write(chunk, (err?: Error | null) => {
|
|
897
|
+
if (err) reject(err);
|
|
898
|
+
else resolve();
|
|
899
|
+
});
|
|
900
|
+
if (ok) resolve();
|
|
901
|
+
});
|
|
902
|
+
},
|
|
903
|
+
close() {
|
|
904
|
+
return new Promise<void>((resolve) => {
|
|
905
|
+
writable.end(() => resolve());
|
|
906
|
+
});
|
|
907
|
+
},
|
|
908
|
+
abort(reason) {
|
|
909
|
+
writable.destroy(
|
|
910
|
+
reason instanceof Error ? reason : new Error(String(reason)),
|
|
911
|
+
);
|
|
912
|
+
},
|
|
913
|
+
});
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
Writable.fromWeb = function fromWeb(
|
|
917
|
+
webStream: WritableStream,
|
|
918
|
+
opts?: { objectMode?: boolean; highWaterMark?: number },
|
|
919
|
+
): Writable {
|
|
920
|
+
const writer = webStream.getWriter();
|
|
921
|
+
return new Writable({
|
|
922
|
+
...opts,
|
|
923
|
+
write(chunk: any, _encoding: string, callback: (err?: Error | null) => void) {
|
|
924
|
+
writer.write(chunk).then(
|
|
925
|
+
() => callback(null),
|
|
926
|
+
(err: Error) => callback(err),
|
|
927
|
+
);
|
|
928
|
+
},
|
|
929
|
+
final(callback: (err?: Error | null) => void) {
|
|
930
|
+
writer.close().then(
|
|
931
|
+
() => callback(null),
|
|
932
|
+
(err: Error) => callback(err),
|
|
933
|
+
);
|
|
934
|
+
},
|
|
935
|
+
});
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
// Duplex
|
|
939
|
+
|
|
940
|
+
export interface Duplex extends Readable {
|
|
941
|
+
writable: boolean;
|
|
942
|
+
writableEnded: boolean;
|
|
943
|
+
writableFinished: boolean;
|
|
944
|
+
writableNeedDrain: boolean;
|
|
945
|
+
writableObjectMode: boolean;
|
|
946
|
+
writableHighWaterMark: number;
|
|
947
|
+
writableCorked: number;
|
|
948
|
+
allowHalfOpen: boolean;
|
|
949
|
+
_writableState: any;
|
|
950
|
+
_write(chunk: any, encoding: string, callback: (err?: Error | null) => void): void;
|
|
951
|
+
_writev?(chunks: Array<{ chunk: any; encoding: string }>, callback: (err?: Error | null) => void): void;
|
|
952
|
+
_final(callback: (err?: Error | null) => void): void;
|
|
953
|
+
readonly writableLength: number;
|
|
954
|
+
write(chunk: any, encOrCb?: any, cb?: any): boolean;
|
|
955
|
+
end(chunkOrCb?: any, encOrCb?: any, cb?: any): this;
|
|
956
|
+
cork(): void;
|
|
957
|
+
uncork(): void;
|
|
958
|
+
setDefaultEncoding(enc: string): this;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
interface DuplexConstructor {
|
|
962
|
+
new (opts?: any): Duplex;
|
|
963
|
+
(this: any, opts?: any): void;
|
|
964
|
+
prototype: any;
|
|
965
|
+
from(source: any, opts?: any): Duplex;
|
|
966
|
+
toWeb(duplex: any): any;
|
|
967
|
+
fromWeb(source: any, opts?: any): any;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
export const Duplex = function Duplex(this: any, opts?: any) {
|
|
971
|
+
if (!this) return;
|
|
972
|
+
|
|
973
|
+
Readable.call(this, {
|
|
974
|
+
objectMode: opts?.objectMode || opts?.readableObjectMode,
|
|
975
|
+
highWaterMark: opts?.readableHighWaterMark ?? opts?.highWaterMark,
|
|
976
|
+
autoDestroy: opts?.autoDestroy,
|
|
977
|
+
read: opts?.read,
|
|
978
|
+
destroy: opts?.destroy,
|
|
979
|
+
});
|
|
980
|
+
|
|
981
|
+
this._writeParts = [];
|
|
982
|
+
this._writeClosed = false;
|
|
983
|
+
this._writeObjectMode = false;
|
|
984
|
+
this._writeHighWaterMark = 16384;
|
|
985
|
+
this._writeAutoDestroy = true;
|
|
986
|
+
this._duplexCorked = 0;
|
|
987
|
+
this._duplexCorkedWrites = [];
|
|
988
|
+
this._writableByteLen = 0;
|
|
989
|
+
|
|
990
|
+
this.writable = true;
|
|
991
|
+
this.writableEnded = false;
|
|
992
|
+
this.writableFinished = false;
|
|
993
|
+
this.writableNeedDrain = false;
|
|
994
|
+
this.writableObjectMode = false;
|
|
995
|
+
this.writableHighWaterMark = 16384;
|
|
996
|
+
this.writableCorked = 0;
|
|
997
|
+
this.allowHalfOpen = true;
|
|
998
|
+
|
|
999
|
+
const self = this;
|
|
1000
|
+
this._writableState = {
|
|
1001
|
+
get objectMode() { return self._writeObjectMode; },
|
|
1002
|
+
get highWaterMark() { return self._writeHighWaterMark; },
|
|
1003
|
+
get finished() { return self.writableFinished; },
|
|
1004
|
+
set finished(v: boolean) { self.writableFinished = v; },
|
|
1005
|
+
get ended() { return self.writableEnded; },
|
|
1006
|
+
set ended(v: boolean) { self.writableEnded = v; },
|
|
1007
|
+
get destroyed() { return self.destroyed; },
|
|
1008
|
+
get errored() { return self.errored; },
|
|
1009
|
+
get closed() { return self.closed; },
|
|
1010
|
+
get corked() { return self._duplexCorked; },
|
|
1011
|
+
get length() { return self.writableLength; },
|
|
1012
|
+
get needDrain() { return self.writableNeedDrain; },
|
|
1013
|
+
writing: false,
|
|
1014
|
+
errorEmitted: false,
|
|
1015
|
+
emitClose: true,
|
|
1016
|
+
get autoDestroy() { return self._writeAutoDestroy; },
|
|
1017
|
+
defaultEncoding: "utf8",
|
|
1018
|
+
finalCalled: false,
|
|
1019
|
+
ending: false,
|
|
1020
|
+
bufferedIndex: 0,
|
|
1021
|
+
};
|
|
1022
|
+
|
|
1023
|
+
if (opts) {
|
|
1024
|
+
if (opts.objectMode || opts.writableObjectMode) {
|
|
1025
|
+
this._writeObjectMode = true;
|
|
1026
|
+
this.writableObjectMode = true;
|
|
1027
|
+
if ((opts.writableHighWaterMark ?? opts.highWaterMark) === undefined) {
|
|
1028
|
+
this._writeHighWaterMark = 16;
|
|
1029
|
+
this.writableHighWaterMark = 16;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
if (opts.writableHighWaterMark !== undefined) {
|
|
1033
|
+
this._writeHighWaterMark = opts.writableHighWaterMark;
|
|
1034
|
+
this.writableHighWaterMark = opts.writableHighWaterMark;
|
|
1035
|
+
} else if (opts.highWaterMark !== undefined) {
|
|
1036
|
+
this._writeHighWaterMark = opts.highWaterMark;
|
|
1037
|
+
this.writableHighWaterMark = opts.highWaterMark;
|
|
1038
|
+
}
|
|
1039
|
+
if (opts.autoDestroy !== undefined) {
|
|
1040
|
+
this._writeAutoDestroy = opts.autoDestroy;
|
|
1041
|
+
}
|
|
1042
|
+
if (opts.allowHalfOpen !== undefined) {
|
|
1043
|
+
this.allowHalfOpen = opts.allowHalfOpen;
|
|
1044
|
+
}
|
|
1045
|
+
if (opts.write) {
|
|
1046
|
+
this._write = opts.write.bind(this);
|
|
1047
|
+
}
|
|
1048
|
+
if (opts.writev) {
|
|
1049
|
+
this._writev = opts.writev.bind(this);
|
|
1050
|
+
}
|
|
1051
|
+
if (opts.final) {
|
|
1052
|
+
this._final = opts.final.bind(this);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
} as unknown as DuplexConstructor;
|
|
1056
|
+
|
|
1057
|
+
Object.setPrototypeOf(Duplex.prototype, Readable.prototype);
|
|
1058
|
+
|
|
1059
|
+
Duplex.prototype._write = function _write(
|
|
1060
|
+
_chunk: any,
|
|
1061
|
+
_encoding: string,
|
|
1062
|
+
callback: (err?: Error | null) => void,
|
|
1063
|
+
): void {
|
|
1064
|
+
callback(null);
|
|
1065
|
+
};
|
|
1066
|
+
|
|
1067
|
+
Duplex.prototype._final = function _final(
|
|
1068
|
+
callback: (err?: Error | null) => void,
|
|
1069
|
+
): void {
|
|
1070
|
+
callback(null);
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
Duplex.prototype.write = function write(
|
|
1074
|
+
chunk: any,
|
|
1075
|
+
encOrCb?: string | ((err?: Error | null) => void),
|
|
1076
|
+
cb?: (err?: Error | null) => void,
|
|
1077
|
+
): boolean {
|
|
1078
|
+
if (this._writeClosed) return false;
|
|
1079
|
+
const encoding = typeof encOrCb === "string" ? encOrCb : "utf8";
|
|
1080
|
+
const callback = typeof encOrCb === "function" ? encOrCb : cb;
|
|
1081
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
1082
|
+
this._writeParts.push(bytes);
|
|
1083
|
+
this._writableByteLen += bufferByteLength(bytes);
|
|
1084
|
+
|
|
1085
|
+
if (this._duplexCorked > 0) {
|
|
1086
|
+
this._duplexCorkedWrites.push({ chunk, encoding, callback });
|
|
1087
|
+
return this._writableByteLen < this._writeHighWaterMark;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
this._write(chunk, encoding, (err: Error | null | undefined) => {
|
|
1091
|
+
this._writableByteLen -= bufferByteLength(bytes);
|
|
1092
|
+
if (callback) callback(err);
|
|
1093
|
+
if (this.writableNeedDrain && this._writableByteLen < this._writeHighWaterMark) {
|
|
1094
|
+
this.writableNeedDrain = false;
|
|
1095
|
+
this.emit("drain");
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
|
|
1099
|
+
const belowHWM = this._writableByteLen < this._writeHighWaterMark;
|
|
1100
|
+
if (!belowHWM) {
|
|
1101
|
+
this.writableNeedDrain = true;
|
|
1102
|
+
}
|
|
1103
|
+
return belowHWM;
|
|
1104
|
+
};
|
|
1105
|
+
|
|
1106
|
+
Duplex.prototype.end = function end(
|
|
1107
|
+
chunkOrCb?: any,
|
|
1108
|
+
encOrCb?: any,
|
|
1109
|
+
cb?: () => void,
|
|
1110
|
+
): any {
|
|
1111
|
+
if (typeof chunkOrCb === "function") {
|
|
1112
|
+
cb = chunkOrCb;
|
|
1113
|
+
} else if (chunkOrCb !== undefined) {
|
|
1114
|
+
this.write(chunkOrCb);
|
|
1115
|
+
}
|
|
1116
|
+
if (typeof encOrCb === "function") cb = encOrCb;
|
|
1117
|
+
|
|
1118
|
+
this._writeClosed = true;
|
|
1119
|
+
this.writable = false;
|
|
1120
|
+
this.writableEnded = true;
|
|
1121
|
+
|
|
1122
|
+
const self = this;
|
|
1123
|
+
const doFinish = () => {
|
|
1124
|
+
self.writableFinished = true;
|
|
1125
|
+
self.emit("finish");
|
|
1126
|
+
if (cb) cb();
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1129
|
+
queueMicrotask(() => {
|
|
1130
|
+
self._final((err: Error | null | undefined) => {
|
|
1131
|
+
if (err) {
|
|
1132
|
+
self.errored = err;
|
|
1133
|
+
self.emit("error", err);
|
|
1134
|
+
return;
|
|
1135
|
+
}
|
|
1136
|
+
doFinish();
|
|
1137
|
+
});
|
|
1138
|
+
});
|
|
1139
|
+
return this;
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
Duplex.prototype.cork = function cork(): void {
|
|
1143
|
+
this._duplexCorked++;
|
|
1144
|
+
this.writableCorked = this._duplexCorked;
|
|
1145
|
+
};
|
|
1146
|
+
|
|
1147
|
+
Duplex.prototype.uncork = function uncork(): void {
|
|
1148
|
+
if (this._duplexCorked > 0) {
|
|
1149
|
+
this._duplexCorked--;
|
|
1150
|
+
this.writableCorked = this._duplexCorked;
|
|
1151
|
+
}
|
|
1152
|
+
if (this._duplexCorked === 0 && this._duplexCorkedWrites.length > 0) {
|
|
1153
|
+
const writes = this._duplexCorkedWrites.splice(0);
|
|
1154
|
+
if (this._writev) {
|
|
1155
|
+
this._writev(
|
|
1156
|
+
writes.map((w: any) => ({ chunk: w.chunk, encoding: w.encoding })),
|
|
1157
|
+
(err: Error | null | undefined) => {
|
|
1158
|
+
for (const w of writes) {
|
|
1159
|
+
if (w.callback) w.callback(err);
|
|
1160
|
+
}
|
|
1161
|
+
},
|
|
1162
|
+
);
|
|
1163
|
+
} else {
|
|
1164
|
+
for (const w of writes) {
|
|
1165
|
+
this._write(w.chunk, w.encoding, (err: Error | null | undefined) => {
|
|
1166
|
+
if (w.callback) w.callback(err);
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
Duplex.prototype.setDefaultEncoding = function setDefaultEncoding(_enc: string): any {
|
|
1174
|
+
return this;
|
|
1175
|
+
};
|
|
1176
|
+
|
|
1177
|
+
Object.defineProperty(Duplex.prototype, "writableLength", {
|
|
1178
|
+
get: function (this: any) {
|
|
1179
|
+
if (this._writeObjectMode) return this._writeParts.length;
|
|
1180
|
+
return this._writableByteLen;
|
|
1181
|
+
},
|
|
1182
|
+
configurable: true,
|
|
1183
|
+
});
|
|
1184
|
+
|
|
1185
|
+
Duplex.from = function from(
|
|
1186
|
+
source: any,
|
|
1187
|
+
_opts?: { objectMode?: boolean; highWaterMark?: number },
|
|
1188
|
+
): Duplex {
|
|
1189
|
+
const duplex = new Duplex();
|
|
1190
|
+
if (source instanceof ReadableStream) {
|
|
1191
|
+
const reader = source.getReader();
|
|
1192
|
+
(async () => {
|
|
1193
|
+
try {
|
|
1194
|
+
while (true) {
|
|
1195
|
+
const { value, done } = await reader.read();
|
|
1196
|
+
if (done) { duplex.push(null); break; }
|
|
1197
|
+
duplex.push(value);
|
|
1198
|
+
}
|
|
1199
|
+
} catch (err) {
|
|
1200
|
+
duplex.destroy(err as Error);
|
|
1201
|
+
}
|
|
1202
|
+
})();
|
|
1203
|
+
} else {
|
|
1204
|
+
(async () => {
|
|
1205
|
+
try {
|
|
1206
|
+
for await (const item of source as AsyncIterable<unknown>) {
|
|
1207
|
+
if (item !== null && item !== undefined) {
|
|
1208
|
+
const data = typeof item === "string" ? Buffer.from(item) : item;
|
|
1209
|
+
duplex.push(data as Buffer);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
duplex.push(null);
|
|
1213
|
+
} catch (err) {
|
|
1214
|
+
duplex.destroy(err as Error);
|
|
1215
|
+
}
|
|
1216
|
+
})();
|
|
1217
|
+
}
|
|
1218
|
+
return duplex;
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
Duplex.toWeb = function toWeb(duplex: any): any {
|
|
1222
|
+
if (duplex instanceof Duplex) {
|
|
1223
|
+
const readable = Readable.toWeb(duplex);
|
|
1224
|
+
const writable = Writable.toWeb(duplex as any);
|
|
1225
|
+
return { readable, writable };
|
|
1226
|
+
}
|
|
1227
|
+
return Readable.toWeb(duplex);
|
|
1228
|
+
};
|
|
1229
|
+
|
|
1230
|
+
Duplex.fromWeb = function fromWeb(
|
|
1231
|
+
source: any,
|
|
1232
|
+
_opts?: Record<string, unknown>,
|
|
1233
|
+
): any {
|
|
1234
|
+
if (source instanceof ReadableStream) {
|
|
1235
|
+
return Readable.fromWeb(source, _opts as { objectMode?: boolean; highWaterMark?: number });
|
|
1236
|
+
}
|
|
1237
|
+
const pair = source;
|
|
1238
|
+
const duplex = new Duplex();
|
|
1239
|
+
const reader = pair.readable.getReader();
|
|
1240
|
+
(async () => {
|
|
1241
|
+
try {
|
|
1242
|
+
while (true) {
|
|
1243
|
+
const { value, done } = await reader.read();
|
|
1244
|
+
if (done) { duplex.push(null); break; }
|
|
1245
|
+
duplex.push(value);
|
|
1246
|
+
}
|
|
1247
|
+
} catch (err) {
|
|
1248
|
+
duplex.destroy(err as Error);
|
|
1249
|
+
}
|
|
1250
|
+
})();
|
|
1251
|
+
return duplex;
|
|
1252
|
+
};
|
|
1253
|
+
|
|
1254
|
+
// PassThrough
|
|
1255
|
+
|
|
1256
|
+
export interface PassThrough extends Duplex {}
|
|
1257
|
+
|
|
1258
|
+
interface PassThroughConstructor {
|
|
1259
|
+
new (opts?: any): PassThrough;
|
|
1260
|
+
(this: any, opts?: any): void;
|
|
1261
|
+
prototype: any;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
export const PassThrough = function PassThrough(this: any, opts?: any) {
|
|
1265
|
+
if (!this) return;
|
|
1266
|
+
Duplex.call(this, opts);
|
|
1267
|
+
} as unknown as PassThroughConstructor;
|
|
1268
|
+
|
|
1269
|
+
Object.setPrototypeOf(PassThrough.prototype, Duplex.prototype);
|
|
1270
|
+
|
|
1271
|
+
PassThrough.prototype.write = function write(
|
|
1272
|
+
chunk: any,
|
|
1273
|
+
encOrCb?: string | ((err?: Error | null) => void),
|
|
1274
|
+
cb?: (err?: Error | null) => void,
|
|
1275
|
+
): boolean {
|
|
1276
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
1277
|
+
this.push(bytes);
|
|
1278
|
+
const callback = typeof encOrCb === "function" ? encOrCb : cb;
|
|
1279
|
+
if (callback) queueMicrotask(() => callback(null));
|
|
1280
|
+
return true;
|
|
1281
|
+
};
|
|
1282
|
+
|
|
1283
|
+
// Transform
|
|
1284
|
+
|
|
1285
|
+
export interface Transform extends Duplex {
|
|
1286
|
+
_transform(chunk: any, encoding: string, done: (err?: Error | null, output?: any) => void): void;
|
|
1287
|
+
_flush(done: (err?: Error | null, output?: any) => void): void;
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
interface TransformConstructor {
|
|
1291
|
+
new (opts?: any): Transform;
|
|
1292
|
+
(this: any, opts?: any): void;
|
|
1293
|
+
prototype: any;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
export const Transform = function Transform(this: any, opts?: any) {
|
|
1297
|
+
if (!this) return;
|
|
1298
|
+
Duplex.call(this, opts);
|
|
1299
|
+
this._flushed = false;
|
|
1300
|
+
|
|
1301
|
+
if (opts) {
|
|
1302
|
+
if (opts.transform) {
|
|
1303
|
+
this._transform = opts.transform.bind(this);
|
|
1304
|
+
}
|
|
1305
|
+
if (opts.flush) {
|
|
1306
|
+
this._flush = opts.flush.bind(this);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
} as unknown as TransformConstructor;
|
|
1310
|
+
|
|
1311
|
+
Object.setPrototypeOf(Transform.prototype, Duplex.prototype);
|
|
1312
|
+
|
|
1313
|
+
Transform.prototype._transform = function _transform(
|
|
1314
|
+
chunk: any,
|
|
1315
|
+
_encoding: string,
|
|
1316
|
+
done: (err?: Error | null, output?: any) => void,
|
|
1317
|
+
): void {
|
|
1318
|
+
done(null, chunk);
|
|
1319
|
+
};
|
|
1320
|
+
|
|
1321
|
+
Transform.prototype._flush = function _flush(
|
|
1322
|
+
done: (err?: Error | null, output?: any) => void,
|
|
1323
|
+
): void {
|
|
1324
|
+
done(null);
|
|
1325
|
+
};
|
|
1326
|
+
|
|
1327
|
+
// flush remaining transform data before terminating the readable side
|
|
1328
|
+
Transform.prototype._final = function _final(
|
|
1329
|
+
callback: (err?: Error | null) => void,
|
|
1330
|
+
): void {
|
|
1331
|
+
const self = this;
|
|
1332
|
+
const finish = () => {
|
|
1333
|
+
callback(null);
|
|
1334
|
+
self.push(null);
|
|
1335
|
+
};
|
|
1336
|
+
if (self._flushed) {
|
|
1337
|
+
finish();
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
self._flushed = true;
|
|
1341
|
+
self._flush((err: Error | null | undefined, output: any) => {
|
|
1342
|
+
if (err) {
|
|
1343
|
+
callback(err);
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
if (output) self.push(output);
|
|
1347
|
+
finish();
|
|
1348
|
+
});
|
|
1349
|
+
};
|
|
1350
|
+
|
|
1351
|
+
Transform.prototype.write = function write(
|
|
1352
|
+
chunk: any,
|
|
1353
|
+
encOrCb?: string | ((err?: Error | null) => void),
|
|
1354
|
+
cb?: (err?: Error | null) => void,
|
|
1355
|
+
): boolean {
|
|
1356
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
1357
|
+
const encoding = typeof encOrCb === "string" ? encOrCb : "utf8";
|
|
1358
|
+
const callback = typeof encOrCb === "function" ? encOrCb : cb;
|
|
1359
|
+
|
|
1360
|
+
this._transform(bytes, encoding, (err: Error | null | undefined, output: any) => {
|
|
1361
|
+
if (err) {
|
|
1362
|
+
if (callback) callback(err);
|
|
1363
|
+
return;
|
|
1364
|
+
}
|
|
1365
|
+
if (output) this.push(output);
|
|
1366
|
+
if (callback) callback(null);
|
|
1367
|
+
});
|
|
1368
|
+
return true;
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1371
|
+
Transform.prototype.end = function end(
|
|
1372
|
+
chunkOrCb?: any,
|
|
1373
|
+
encOrCb?: any,
|
|
1374
|
+
cb?: () => void,
|
|
1375
|
+
): any {
|
|
1376
|
+
// flush eagerly so data reaches the pipe destination before _final's microtask
|
|
1377
|
+
if (!this._flushed) {
|
|
1378
|
+
this._flushed = true;
|
|
1379
|
+
this._flush((_err: Error | null | undefined, output: any) => {
|
|
1380
|
+
if (output) this.push(output);
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
return Duplex.prototype.end.call(this, chunkOrCb, encOrCb, cb);
|
|
1384
|
+
};
|
|
1385
|
+
|
|
1386
|
+
// Stream (base class)
|
|
1387
|
+
|
|
1388
|
+
export interface Stream extends EventEmitter {
|
|
1389
|
+
pipe(dest: any): any;
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
interface StreamConstructor {
|
|
1393
|
+
new (): Stream;
|
|
1394
|
+
(this: any): void;
|
|
1395
|
+
prototype: any;
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
export const Stream = function Stream(this: any) {
|
|
1399
|
+
if (!this) return;
|
|
1400
|
+
EventEmitter.call(this);
|
|
1401
|
+
} as unknown as StreamConstructor;
|
|
1402
|
+
|
|
1403
|
+
Object.setPrototypeOf(Stream.prototype, EventEmitter.prototype);
|
|
1404
|
+
|
|
1405
|
+
Stream.prototype.pipe = function pipe(dest: any): any {
|
|
1406
|
+
return dest;
|
|
1407
|
+
};
|
|
1408
|
+
|
|
1409
|
+
export function addAbortSignal(
|
|
1410
|
+
signal: AbortSignal,
|
|
1411
|
+
stream: any,
|
|
1412
|
+
): any {
|
|
1413
|
+
if (signal.aborted) {
|
|
1414
|
+
stream.destroy(new Error("The operation was aborted"));
|
|
1415
|
+
} else {
|
|
1416
|
+
const onAbort = () => {
|
|
1417
|
+
stream.destroy(new Error("The operation was aborted"));
|
|
1418
|
+
};
|
|
1419
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1420
|
+
const origDestroy = stream.destroy.bind(stream);
|
|
1421
|
+
stream.destroy = (err?: Error) => {
|
|
1422
|
+
signal.removeEventListener("abort", onAbort);
|
|
1423
|
+
return origDestroy(err);
|
|
1424
|
+
};
|
|
1425
|
+
}
|
|
1426
|
+
return stream;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
// require('stream') compatibility -- attach all types as static properties
|
|
1430
|
+
const StreamAny = Stream as unknown as Record<string, unknown>;
|
|
1431
|
+
StreamAny.Readable = Readable;
|
|
1432
|
+
StreamAny.Writable = Writable;
|
|
1433
|
+
StreamAny.Duplex = Duplex;
|
|
1434
|
+
StreamAny.Transform = Transform;
|
|
1435
|
+
StreamAny.PassThrough = PassThrough;
|
|
1436
|
+
StreamAny.Stream = Stream;
|
|
1437
|
+
StreamAny.from = Readable.from;
|
|
1438
|
+
StreamAny.addAbortSignal = addAbortSignal;
|
|
1439
|
+
|
|
1440
|
+
export function pipeline(...args: unknown[]): unknown {
|
|
1441
|
+
const cb =
|
|
1442
|
+
typeof args[args.length - 1] === "function"
|
|
1443
|
+
? (args.pop() as (err?: Error | null) => void)
|
|
1444
|
+
: null;
|
|
1445
|
+
|
|
1446
|
+
const streams = args as any[];
|
|
1447
|
+
if (streams.length < 2) {
|
|
1448
|
+
if (cb)
|
|
1449
|
+
setTimeout(
|
|
1450
|
+
() => cb(new Error("pipeline requires at least 2 streams")),
|
|
1451
|
+
0,
|
|
1452
|
+
);
|
|
1453
|
+
return streams[0];
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
let errorOccurred = false;
|
|
1457
|
+
const onError = (...errArgs: unknown[]) => {
|
|
1458
|
+
const err = errArgs[0] as Error;
|
|
1459
|
+
if (errorOccurred) return;
|
|
1460
|
+
errorOccurred = true;
|
|
1461
|
+
for (const s of streams) {
|
|
1462
|
+
if (typeof s.destroy === "function") {
|
|
1463
|
+
s.destroy();
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
if (cb) cb(err);
|
|
1467
|
+
};
|
|
1468
|
+
|
|
1469
|
+
for (let i = 0; i < streams.length - 1; i++) {
|
|
1470
|
+
const src = streams[i];
|
|
1471
|
+
const dest = streams[i + 1];
|
|
1472
|
+
if (typeof src.pipe === "function") {
|
|
1473
|
+
src.pipe(dest);
|
|
1474
|
+
}
|
|
1475
|
+
src.on("error", onError);
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
const last = streams[streams.length - 1];
|
|
1479
|
+
last.on("error", onError);
|
|
1480
|
+
|
|
1481
|
+
if (cb && !errorOccurred) {
|
|
1482
|
+
const onFinish = () => {
|
|
1483
|
+
if (!errorOccurred) cb(null);
|
|
1484
|
+
};
|
|
1485
|
+
if (last instanceof Readable) {
|
|
1486
|
+
last.on("end", onFinish);
|
|
1487
|
+
} else {
|
|
1488
|
+
last.on("finish", onFinish);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
return last;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
export function finished(
|
|
1496
|
+
stream: unknown,
|
|
1497
|
+
optsOrCb?: { error?: boolean } | ((err?: Error) => void),
|
|
1498
|
+
cb?: (err?: Error) => void,
|
|
1499
|
+
): () => void {
|
|
1500
|
+
const done = typeof optsOrCb === "function" ? optsOrCb : cb;
|
|
1501
|
+
const s = stream as any;
|
|
1502
|
+
let called = false;
|
|
1503
|
+
|
|
1504
|
+
const onDone = (err?: Error) => {
|
|
1505
|
+
if (called) return;
|
|
1506
|
+
called = true;
|
|
1507
|
+
if (done) done(err);
|
|
1508
|
+
};
|
|
1509
|
+
|
|
1510
|
+
const cleanup = () => {
|
|
1511
|
+
s.removeListener("end", onEnd);
|
|
1512
|
+
s.removeListener("finish", onFinish);
|
|
1513
|
+
s.removeListener("error", onErr);
|
|
1514
|
+
s.removeListener("close", onClose);
|
|
1515
|
+
};
|
|
1516
|
+
|
|
1517
|
+
const onEnd = () => { cleanup(); onDone(); };
|
|
1518
|
+
const onFinish = () => { cleanup(); onDone(); };
|
|
1519
|
+
const onErr = (err: Error) => { cleanup(); onDone(err); };
|
|
1520
|
+
const onClose = () => { cleanup(); onDone(); };
|
|
1521
|
+
|
|
1522
|
+
s.on("end", onEnd);
|
|
1523
|
+
s.on("finish", onFinish);
|
|
1524
|
+
s.on("error", onErr);
|
|
1525
|
+
s.on("close", onClose);
|
|
1526
|
+
|
|
1527
|
+
if (s.readableEnded || s.writableFinished) {
|
|
1528
|
+
queueMicrotask(() => onDone());
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
return cleanup;
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
let _defaultHighWaterMark = 16384;
|
|
1535
|
+
let _defaultObjectHighWaterMark = 16;
|
|
1536
|
+
|
|
1537
|
+
export function getDefaultHighWaterMark(objectMode?: boolean): number {
|
|
1538
|
+
return objectMode ? _defaultObjectHighWaterMark : _defaultHighWaterMark;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
export function setDefaultHighWaterMark(objectMode: boolean, value: number): void {
|
|
1542
|
+
if (objectMode) {
|
|
1543
|
+
_defaultObjectHighWaterMark = value;
|
|
1544
|
+
} else {
|
|
1545
|
+
_defaultHighWaterMark = value;
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
export const promises = {
|
|
1550
|
+
pipeline: async (...streams: unknown[]): Promise<void> => {
|
|
1551
|
+
return new Promise<void>((resolve, reject) => {
|
|
1552
|
+
pipeline(...streams, (err?: Error | null) => {
|
|
1553
|
+
if (err) reject(err);
|
|
1554
|
+
else resolve();
|
|
1555
|
+
});
|
|
1556
|
+
});
|
|
1557
|
+
},
|
|
1558
|
+
finished: async (
|
|
1559
|
+
stream: unknown,
|
|
1560
|
+
opts?: { error?: boolean },
|
|
1561
|
+
): Promise<void> => {
|
|
1562
|
+
return new Promise<void>((resolve, reject) => {
|
|
1563
|
+
finished(stream, opts, (err) => {
|
|
1564
|
+
if (err) reject(err);
|
|
1565
|
+
else resolve();
|
|
1566
|
+
});
|
|
1567
|
+
});
|
|
1568
|
+
},
|
|
1569
|
+
};
|
|
1570
|
+
|
|
1571
|
+
StreamAny.pipeline = pipeline;
|
|
1572
|
+
StreamAny.finished = finished;
|
|
1573
|
+
StreamAny.promises = promises;
|
|
1574
|
+
StreamAny.getDefaultHighWaterMark = getDefaultHighWaterMark;
|
|
1575
|
+
StreamAny.setDefaultHighWaterMark = setDefaultHighWaterMark;
|
|
1576
|
+
|
|
1577
|
+
export function compose(..._streams: unknown[]): Duplex {
|
|
1578
|
+
return new PassThrough() as unknown as Duplex;
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
export function isReadable(stream: unknown): boolean {
|
|
1582
|
+
if (!stream || typeof stream !== "object") return false;
|
|
1583
|
+
const s = stream as any;
|
|
1584
|
+
if (typeof s.read !== "function") return false;
|
|
1585
|
+
if (s.destroyed) return false;
|
|
1586
|
+
if (s.readableEnded || s._terminated) return false;
|
|
1587
|
+
return true;
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
export function isWritable(stream: unknown): boolean {
|
|
1591
|
+
if (!stream || typeof stream !== "object") return false;
|
|
1592
|
+
const s = stream as any;
|
|
1593
|
+
if (typeof s.write !== "function") return false;
|
|
1594
|
+
if (s.destroyed) return false;
|
|
1595
|
+
if (s.writableEnded || s.writableFinished) return false;
|
|
1596
|
+
return true;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
export function isDisturbed(stream: unknown): boolean {
|
|
1600
|
+
if (!stream || typeof stream !== "object") return false;
|
|
1601
|
+
const s = stream as any;
|
|
1602
|
+
return !!s.readableDidRead || !!s._reading;
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
export function isErrored(stream: unknown): boolean {
|
|
1606
|
+
if (!stream || typeof stream !== "object") return false;
|
|
1607
|
+
const s = stream as any;
|
|
1608
|
+
return s.errored != null;
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
StreamAny.compose = compose;
|
|
1612
|
+
StreamAny.isReadable = isReadable;
|
|
1613
|
+
StreamAny.isWritable = isWritable;
|
|
1614
|
+
StreamAny.isDisturbed = isDisturbed;
|
|
1615
|
+
StreamAny.isErrored = isErrored;
|
|
1616
|
+
|
|
1617
|
+
(Readable as any).isDisturbed = isDisturbed;
|
|
1618
|
+
(Readable as any).isReadable = isReadable;
|
|
1619
|
+
|
|
1620
|
+
export default Stream;
|