documonster 0.20.2 → 0.21.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.
Files changed (144) hide show
  1. package/README.md +1 -1
  2. package/README_zh.md +1 -1
  3. package/THIRD_PARTY_NOTICES.md +34 -2
  4. package/dist/esm/modules/archive/core/byte-queue.js +2 -1
  5. package/dist/esm/modules/archive/unzip/stream.browser.js +6 -0
  6. package/dist/esm/modules/csv/parse/async.js +2 -1
  7. package/dist/esm/modules/excel/bridge/csv-bridge.js +1 -1
  8. package/dist/esm/modules/excel/core/xlsx-io.browser.js +5 -9
  9. package/dist/esm/modules/excel/core/xlsx-io.js +1 -3
  10. package/dist/esm/modules/excel/core/xlsx-stream.js +3 -1
  11. package/dist/esm/modules/excel/stream/workbook-reader.browser.js +10 -10
  12. package/dist/esm/modules/excel/stream/workbook-reader.js +30 -6
  13. package/dist/esm/modules/stream/browser/collector.js +3 -5
  14. package/dist/esm/modules/stream/browser/compose.js +13 -228
  15. package/dist/esm/modules/stream/browser/duplex-pair.js +71 -0
  16. package/dist/esm/modules/stream/browser/factories.js +35 -217
  17. package/dist/esm/modules/stream/browser/pipeline.js +43 -478
  18. package/dist/esm/modules/stream/browser/utils.js +21 -247
  19. package/dist/esm/modules/stream/browser/web.js +25 -0
  20. package/dist/esm/modules/stream/buffered-stream.js +8 -6
  21. package/dist/esm/modules/stream/core/factories.js +166 -0
  22. package/dist/esm/modules/stream/core/pipeline-api.js +32 -0
  23. package/dist/esm/modules/stream/core/sink-adapter.js +94 -24
  24. package/dist/esm/modules/stream/core/stream-helpers.js +160 -0
  25. package/dist/esm/modules/stream/core/utils.js +0 -23
  26. package/dist/esm/modules/stream/core/web-conversions.js +31 -0
  27. package/dist/esm/modules/stream/core/web-pipeline.js +90 -0
  28. package/dist/esm/modules/stream/index.base.js +2 -2
  29. package/dist/esm/modules/stream/index.browser.js +13 -19
  30. package/dist/esm/modules/stream/index.js +6 -10
  31. package/dist/esm/modules/stream/kernel/classes.js +618 -0
  32. package/dist/esm/modules/stream/kernel/compose-impl.js +225 -0
  33. package/dist/esm/modules/stream/kernel/destroy.js +255 -0
  34. package/dist/esm/modules/stream/kernel/duplexify.js +355 -0
  35. package/dist/esm/modules/stream/kernel/end-of-stream.js +212 -0
  36. package/dist/esm/modules/stream/kernel/errors.js +215 -0
  37. package/dist/esm/modules/stream/kernel/from.js +202 -0
  38. package/dist/esm/modules/stream/kernel/legacy.js +35 -0
  39. package/dist/esm/modules/stream/kernel/operators.js +285 -0
  40. package/dist/esm/modules/stream/kernel/pipeline-impl.js +561 -0
  41. package/dist/esm/modules/stream/kernel/readable-impl.js +963 -0
  42. package/dist/esm/modules/stream/kernel/state.js +253 -0
  43. package/dist/esm/modules/stream/kernel/stream-utils.js +212 -0
  44. package/dist/esm/modules/stream/kernel/web-adapters.js +584 -0
  45. package/dist/esm/modules/stream/kernel/writable.js +651 -0
  46. package/dist/esm/modules/stream/node/collector.js +7 -1
  47. package/dist/esm/modules/stream/node/compose.js +21 -206
  48. package/dist/esm/modules/stream/node/factories.js +34 -238
  49. package/dist/esm/modules/stream/node/pipeline.js +40 -94
  50. package/dist/esm/modules/stream/node/utils.js +15 -239
  51. package/dist/esm/modules/stream/node/web.js +27 -0
  52. package/dist/esm/modules/stream/pull-stream.js +15 -9
  53. package/dist/esm/modules/word/writer/streaming-writer.js +3 -0
  54. package/dist/iife/THIRD_PARTY_NOTICES.md +34 -2
  55. package/dist/iife/documonster.archive.iife.min.js +4 -4
  56. package/dist/iife/documonster.csv.iife.min.js +19 -19
  57. package/dist/iife/documonster.draw.iife.min.js +1 -1
  58. package/dist/iife/documonster.excel.iife.min.js +50 -50
  59. package/dist/iife/documonster.formula.iife.min.js +1 -1
  60. package/dist/iife/documonster.markdown.iife.min.js +1 -1
  61. package/dist/iife/documonster.mermaid.iife.min.js +1 -1
  62. package/dist/iife/documonster.pdf.iife.min.js +1 -1
  63. package/dist/iife/documonster.stream.iife.min.js +2 -2
  64. package/dist/iife/documonster.word.iife.min.js +60 -60
  65. package/dist/iife/documonster.xml.iife.min.js +1 -1
  66. package/dist/types/modules/excel/core/xlsx-io.browser.d.ts +5 -9
  67. package/dist/types/modules/excel/core/xlsx-io.d.ts +1 -3
  68. package/dist/types/modules/excel/core/xlsx-stream.d.ts +3 -2
  69. package/dist/types/modules/excel/stream/workbook-reader.browser.d.ts +2 -1
  70. package/dist/types/modules/excel/stream/workbook-reader.d.ts +2 -1
  71. package/dist/types/modules/stream/browser/collector.d.ts +1 -1
  72. package/dist/types/modules/stream/browser/compose.d.ts +14 -15
  73. package/dist/types/modules/stream/browser/duplex-pair.d.ts +10 -0
  74. package/dist/types/modules/stream/browser/factories.d.ts +22 -79
  75. package/dist/types/modules/stream/browser/pipeline.d.ts +8 -40
  76. package/dist/types/modules/stream/browser/utils.d.ts +6 -47
  77. package/dist/types/modules/stream/browser/web.d.ts +18 -0
  78. package/dist/types/modules/stream/core/add-abort-signal.d.ts +2 -1
  79. package/dist/types/modules/stream/core/consumers.d.ts +1 -1
  80. package/dist/types/modules/stream/core/event-utils.d.ts +2 -1
  81. package/dist/types/modules/stream/core/factories.d.ts +129 -0
  82. package/dist/types/modules/stream/core/pipeline-api.d.ts +10 -0
  83. package/dist/types/modules/stream/core/sink-adapter.d.ts +18 -1
  84. package/dist/types/modules/stream/core/stream-helpers.d.ts +76 -0
  85. package/dist/types/modules/stream/core/utils.d.ts +0 -9
  86. package/dist/types/modules/stream/core/web-conversions.d.ts +68 -0
  87. package/dist/types/modules/stream/core/web-pipeline.d.ts +27 -0
  88. package/dist/types/modules/stream/index.base.d.ts +6 -4
  89. package/dist/types/modules/stream/index.browser.d.ts +13 -14
  90. package/dist/types/modules/stream/index.d.ts +7 -10
  91. package/dist/types/modules/stream/kernel/classes.d.ts +220 -0
  92. package/dist/types/modules/stream/kernel/compose-impl.d.ts +10 -0
  93. package/dist/types/modules/stream/kernel/destroy.d.ts +21 -0
  94. package/dist/types/modules/stream/kernel/duplexify.d.ts +31 -0
  95. package/dist/types/modules/stream/kernel/end-of-stream.d.ts +28 -0
  96. package/dist/types/modules/stream/kernel/errors.d.ts +42 -0
  97. package/dist/types/modules/stream/kernel/from.d.ts +17 -0
  98. package/dist/types/modules/stream/kernel/legacy.d.ts +20 -0
  99. package/dist/types/modules/stream/kernel/operators.d.ts +44 -0
  100. package/dist/types/modules/stream/kernel/pipeline-impl.d.ts +32 -0
  101. package/dist/types/modules/stream/kernel/readable-impl.d.ts +34 -0
  102. package/dist/types/modules/stream/kernel/state.d.ts +223 -0
  103. package/dist/types/modules/stream/kernel/stream-utils.d.ts +34 -0
  104. package/dist/types/modules/stream/kernel/web-adapters.d.ts +65 -0
  105. package/dist/types/modules/stream/kernel/writable.d.ts +107 -0
  106. package/dist/types/modules/stream/node/collector.d.ts +7 -2
  107. package/dist/types/modules/stream/node/compose.d.ts +31 -5
  108. package/dist/types/modules/stream/node/factories.d.ts +22 -85
  109. package/dist/types/modules/stream/node/pipeline.d.ts +4 -15
  110. package/dist/types/modules/stream/node/utils.d.ts +9 -49
  111. package/dist/types/modules/stream/node/web.d.ts +18 -0
  112. package/dist/types/modules/stream/pull-stream.d.ts +2 -2
  113. package/dist/types/modules/stream/types.d.ts +24 -6
  114. package/package.json +5 -3
  115. package/dist/esm/modules/stream/browser/_lazy.js +0 -30
  116. package/dist/esm/modules/stream/browser/chunk-buffer.js +0 -223
  117. package/dist/esm/modules/stream/browser/duplex.js +0 -979
  118. package/dist/esm/modules/stream/browser/helpers.js +0 -44
  119. package/dist/esm/modules/stream/browser/microtask-context.js +0 -60
  120. package/dist/esm/modules/stream/browser/passthrough.js +0 -16
  121. package/dist/esm/modules/stream/browser/pipe-manager.js +0 -161
  122. package/dist/esm/modules/stream/browser/readable.js +0 -2208
  123. package/dist/esm/modules/stream/browser/transform.js +0 -1241
  124. package/dist/esm/modules/stream/browser/writable.js +0 -1316
  125. package/dist/esm/modules/stream/core/end-args.js +0 -24
  126. package/dist/esm/modules/stream/node/writable.js +0 -125
  127. package/dist/esm/modules/stream/utils.base.js +0 -126
  128. package/dist/esm/modules/stream/utils.browser.js +0 -80
  129. package/dist/esm/modules/stream/utils.js +0 -81
  130. package/dist/types/modules/stream/browser/_lazy.d.ts +0 -20
  131. package/dist/types/modules/stream/browser/chunk-buffer.d.ts +0 -61
  132. package/dist/types/modules/stream/browser/duplex.d.ts +0 -299
  133. package/dist/types/modules/stream/browser/helpers.d.ts +0 -13
  134. package/dist/types/modules/stream/browser/microtask-context.d.ts +0 -44
  135. package/dist/types/modules/stream/browser/passthrough.d.ts +0 -11
  136. package/dist/types/modules/stream/browser/pipe-manager.d.ts +0 -38
  137. package/dist/types/modules/stream/browser/readable.d.ts +0 -379
  138. package/dist/types/modules/stream/browser/transform.d.ts +0 -314
  139. package/dist/types/modules/stream/browser/writable.d.ts +0 -238
  140. package/dist/types/modules/stream/core/end-args.d.ts +0 -20
  141. package/dist/types/modules/stream/node/writable.d.ts +0 -53
  142. package/dist/types/modules/stream/utils.base.d.ts +0 -63
  143. package/dist/types/modules/stream/utils.browser.d.ts +0 -23
  144. package/dist/types/modules/stream/utils.d.ts +0 -24
@@ -1,2208 +0,0 @@
1
- /**
2
- * Browser Stream - Readable
3
- */
4
- import { getDuplexFrom } from "./_lazy.js";
5
- import { ChunkBuffer } from "./chunk-buffer.js";
6
- import { deferTask, inDeferredContext } from "./microtask-context.js";
7
- import { PipeManager } from "./pipe-manager.js";
8
- import { stringToEncodedBytes } from "../core/binary-chunk.js";
9
- import { getDefaultHighWaterMark } from "../core/utils.js";
10
- import { createStreamDecoder, decodeBytesToString } from "../../../utils/binary.js";
11
- import { createAbortError, toError } from "../../../utils/errors.js";
12
- import { EventEmitter } from "../../../utils/event-emitter.js";
13
- /**
14
- * Shared toString implementation for Uint8Array chunks converted from strings.
15
- * Uses `this`-binding to avoid per-chunk closure allocation.
16
- * Supports all Node.js Buffer encodings (hex, base64, base64url, ascii, etc.).
17
- */
18
- function encodedBytesToString(enc) {
19
- return decodeBytesToString(this, enc ?? "utf-8");
20
- }
21
- /**
22
- * A wrapper around Web ReadableStream that provides Node.js-like API
23
- */
24
- export class Readable extends EventEmitter {
25
- /**
26
- * Allow duck-typed instanceof checks.
27
- * Node.js Duplex passes `instanceof Readable` via prototype chain.
28
- * Our browser Duplex composes a Readable, so we use Symbol.hasInstance
29
- * to check for key Readable-like methods/properties.
30
- */
31
- static [Symbol.hasInstance](instance) {
32
- if (instance == null || typeof instance !== "object") {
33
- return false;
34
- }
35
- // Fast path: actual Readable prototype
36
- if (Object.prototype.isPrototypeOf.call(Readable.prototype, instance)) {
37
- return true;
38
- }
39
- // Duck-type: must have key Readable methods and the stream brand
40
- const o = instance;
41
- return (o.__documonster_stream === true &&
42
- typeof o.read === "function" &&
43
- typeof o.pipe === "function" &&
44
- typeof o.on === "function" &&
45
- "readableFlowing" in instance);
46
- }
47
- constructor(options) {
48
- super();
49
- this._reader = null;
50
- this._reading = false;
51
- this._ended = false;
52
- this._endEmitted = false;
53
- this._destroyed = false;
54
- /** @internal Set by _callRead catch to signal afterDestroy to emit synchronously */
55
- this._internalDestroy = false;
56
- this._errored = null;
57
- this._closed = false;
58
- this._flowing = false;
59
- this._resumeScheduled = false;
60
- this._hasFlowed = false;
61
- this._encoding = null;
62
- this._decoder = null;
63
- this._didRead = false;
64
- // Whether this stream uses push() mode (true) or Web Stream mode (false)
65
- this._pushMode = false;
66
- // Whether this stream was created from an external Web Stream (true) or is controllable (false)
67
- this._webStreamMode = false;
68
- // True when a user-provided _read exists (via options or subclass override).
69
- // Used to gate _callRead invocations: the prototype _read throws
70
- // ERR_METHOD_NOT_IMPLEMENTED (Node.js parity), so we must not call it
71
- // speculatively during the flow loop for push-driven / Web-Stream-backed
72
- // streams that never intend to supply a pull-based _read.
73
- this._hasReadImpl = false;
74
- this._constructed = true;
75
- // Shared async-iterator state (Node.js parity: multiple iterators compete for chunks)
76
- this._asyncIterState = null;
77
- this._objectMode = options?.objectMode ?? false;
78
- this._highWaterMark = options?.highWaterMark ?? getDefaultHighWaterMark(this._objectMode);
79
- this._buf = new ChunkBuffer(this._objectMode);
80
- this._pipes = new PipeManager(this);
81
- this._autoDestroy = options?.autoDestroy ?? true;
82
- this._emitClose = options?.emitClose ?? true;
83
- // Store user-provided read function
84
- if (options?.read) {
85
- this._read = options.read.bind(this);
86
- this._hasReadImpl = true;
87
- this._pushMode = true; // User will call push()
88
- }
89
- else if (this._hasSubclassRead()) {
90
- // Subclass defines _read on its prototype — bind and mark as available.
91
- // This matches Node.js where subclass._read() is the canonical pull API.
92
- this._hasReadImpl = true;
93
- this._pushMode = true;
94
- }
95
- // Store user-provided destroy function
96
- if (options?.destroy) {
97
- this._destroy = options.destroy.bind(this);
98
- }
99
- // Store user-provided construct function
100
- if (options?.construct) {
101
- this._constructFunc = options.construct.bind(this);
102
- }
103
- if (options?.stream) {
104
- this._stream = options.stream;
105
- this._webStreamMode = true; // Created from external Web Stream
106
- }
107
- else {
108
- // Controllable stream - no need to eagerly create a Web ReadableStream.
109
- // The webStream getter will create one lazily when accessed.
110
- this._stream = null;
111
- }
112
- // M2: encoding constructor option — call setEncoding() if provided
113
- if (options?.encoding) {
114
- this.setEncoding(options.encoding);
115
- }
116
- // M1: signal constructor option — destroy stream when signal aborts
117
- if (options?.signal) {
118
- this._setupAbortSignal(options.signal);
119
- }
120
- // L2: _construct hook — if provided, delay _read until constructed
121
- this._maybeConstruct();
122
- }
123
- /**
124
- * Run _construct if provided (via options or subclass override).
125
- * Delays _read calls until the callback fires.
126
- */
127
- _maybeConstruct() {
128
- const hasConstructHook = this._constructFunc || this._hasSubclassConstruct();
129
- if (!hasConstructHook) {
130
- return;
131
- }
132
- this._constructed = false;
133
- // Call _construct on next microtask (matches Node.js which uses process.nextTick)
134
- deferTask(() => {
135
- const fn = this._constructFunc ?? this._construct.bind(this);
136
- fn(err => {
137
- if (err) {
138
- this.destroy(err);
139
- return;
140
- }
141
- this._constructed = true;
142
- // If _read was requested while not yet constructed, call it now
143
- if (this._hasReadImpl && this._flowing && !this._ended && !this._destroyed) {
144
- this._callRead(this._highWaterMark);
145
- }
146
- });
147
- });
148
- }
149
- /**
150
- * Check if a subclass defines _construct on its own prototype.
151
- * Node.js does NOT have _construct on any stream prototype — it only exists
152
- * when provided via constructor options or defined by a subclass.
153
- */
154
- _hasSubclassConstruct() {
155
- let proto = Object.getPrototypeOf(this);
156
- while (proto && proto !== Readable.prototype && proto !== Object.prototype) {
157
- if (Object.prototype.hasOwnProperty.call(proto, "_construct")) {
158
- return true;
159
- }
160
- proto = Object.getPrototypeOf(proto);
161
- }
162
- return false;
163
- }
164
- /**
165
- * Check if a subclass defines _read on its own prototype.
166
- * The base Readable.prototype._read throws ERR_METHOD_NOT_IMPLEMENTED,
167
- * so we must stop before it. Only return true if an intermediate prototype
168
- * (the user's subclass) owns _read.
169
- */
170
- _hasSubclassRead() {
171
- let proto = Object.getPrototypeOf(this);
172
- while (proto && proto !== Readable.prototype && proto !== Object.prototype) {
173
- if (Object.prototype.hasOwnProperty.call(proto, "_read")) {
174
- return true;
175
- }
176
- proto = Object.getPrototypeOf(proto);
177
- }
178
- return false;
179
- }
180
- /**
181
- * Wire up an AbortSignal to destroy this stream on abort.
182
- */
183
- _setupAbortSignal(signal) {
184
- if (signal.aborted) {
185
- this.destroy(createAbortError(signal.reason));
186
- return;
187
- }
188
- const onAbort = () => {
189
- cleanup();
190
- this.destroy(createAbortError(signal.reason));
191
- };
192
- const onDone = () => {
193
- cleanup();
194
- };
195
- const cleanup = () => {
196
- signal.removeEventListener("abort", onAbort);
197
- this.off("close", onDone);
198
- this.off("end", onDone);
199
- this.off("error", onDone);
200
- };
201
- signal.addEventListener("abort", onAbort, { once: true });
202
- this.on("close", onDone);
203
- this.on("end", onDone);
204
- this.on("error", onDone);
205
- }
206
- /**
207
- * Create a Readable from an iterable (static factory method)
208
- */
209
- static from(iterable, options) {
210
- // Node.js also supports creating from a Web ReadableStream.
211
- // Detect it explicitly (do not rely on Symbol.asyncIterator presence).
212
- if (iterable && typeof iterable.getReader === "function") {
213
- return Readable.fromWeb(iterable, options);
214
- }
215
- // Node.js: Blob → stream via its ReadableStream.
216
- // Must be checked before the generic Iterable path because Blob is not
217
- // iterable but has a .stream() method that returns a ReadableStream.
218
- if (typeof Blob !== "undefined" && iterable instanceof Blob) {
219
- // Boundary: Blob.stream() yields ReadableStream<Uint8Array>; the caller's
220
- // requested element type is honored at the fromWeb interop edge.
221
- return Readable.fromWeb(iterable.stream(), options);
222
- }
223
- // Validate argument type early (Node.js throws ERR_INVALID_ARG_TYPE).
224
- if (iterable == null || (typeof iterable !== "object" && typeof iterable !== "string")) {
225
- const err = new TypeError(`The "iterable" argument must be an instance of Iterable. Received type ${typeof iterable} (${String(iterable)})`);
226
- err.code = "ERR_INVALID_ARG_TYPE";
227
- throw err;
228
- }
229
- const hasIter = typeof iterable[Symbol.iterator] === "function";
230
- const hasAsyncIter = typeof iterable[Symbol.asyncIterator] ===
231
- "function";
232
- if (!hasIter && !hasAsyncIter && typeof iterable !== "string") {
233
- const name = iterable.constructor?.name ?? "Object";
234
- const err = new TypeError(`The "iterable" argument must be an instance of Iterable. Received an instance of ${name}`);
235
- err.code = "ERR_INVALID_ARG_TYPE";
236
- throw err;
237
- }
238
- const readable = new Readable({
239
- ...options,
240
- objectMode: options?.objectMode ?? true
241
- });
242
- const iter = iterable;
243
- // Node.js treats strings as a single chunk, not as Iterable<char>.
244
- // Match that behavior by wrapping strings in an array.
245
- const source = typeof iter === "string"
246
- ? toAsyncIterable([iter])
247
- : toAsyncIterable(iter);
248
- pumpAsyncIterableToReadable(readable, source);
249
- return readable;
250
- }
251
- /**
252
- * Check if a stream has been disturbed (read from).
253
- * Matches Node.js native: readableDidRead || readableAborted.
254
- * readableAborted = (destroyed || errored) && !endEmitted.
255
- */
256
- static isDisturbed(stream) {
257
- return (stream._didRead || ((stream._destroyed || stream._errored !== null) && !stream._endEmitted));
258
- }
259
- /**
260
- * Check if a stream is still readable (not destroyed, not ended).
261
- * Matches Node.js Readable.isReadable() (available since v21).
262
- * Returns null for non-stream inputs (matching Node.js behavior).
263
- */
264
- static isReadable(stream) {
265
- if (stream == null || typeof stream !== "object") {
266
- return null;
267
- }
268
- const s = stream;
269
- // Not a stream-like object — return null (matches Node.js)
270
- if (typeof s.read !== "function") {
271
- return null;
272
- }
273
- // If destroyed or errored, not readable
274
- if (s.destroyed || s._destroyed) {
275
- return false;
276
- }
277
- // If ended, not readable
278
- if (s.readableEnded || s._endEmitted) {
279
- return false;
280
- }
281
- // Must have readable property
282
- return s.readable !== false;
283
- }
284
- /**
285
- * Convert a Web ReadableStream to Node.js Readable
286
- */
287
- static fromWeb(webStream, options) {
288
- return new Readable({ ...options, stream: webStream });
289
- }
290
- /**
291
- * Convert a Node.js Readable to Web ReadableStream
292
- */
293
- static toWeb(nodeStream) {
294
- return nodeStream._webStream;
295
- }
296
- /**
297
- * Static wrap method - wraps an old-style stream into a Readable.
298
- * Matches Node.js Readable.wrap(stream, options).
299
- */
300
- static wrap(src, options) {
301
- return new Readable({
302
- objectMode: src.readableObjectMode ?? src.objectMode ?? true,
303
- ...options,
304
- destroy(err, callback) {
305
- if (typeof src.destroy === "function") {
306
- src.destroy(err ?? undefined);
307
- }
308
- callback(err);
309
- }
310
- }).wrap(src);
311
- }
312
- /**
313
- * Push data to the stream (when using controllable stream)
314
- */
315
- push(chunk, encoding) {
316
- if (this._destroyed) {
317
- return false;
318
- }
319
- // Mark as push mode when push() is called
320
- this._pushMode = true;
321
- // Handle string encoding (Node.js compatibility)
322
- // Node.js always converts strings to Buffer in binary mode, defaulting to utf8.
323
- // We override toString() so it behaves like Node.js Buffer.toString().
324
- if (chunk !== null && typeof chunk === "string" && !this._objectMode) {
325
- const encoded = stringToEncodedBytes(chunk, encoding || "utf8");
326
- encoded.toString = encodedBytesToString;
327
- // Boundary: in binary (non-object) mode chunks are materialized as bytes;
328
- // the encoded Uint8Array is the runtime chunk for element type T.
329
- chunk = encoded;
330
- }
331
- // Reject push() after EOF (matches Node.js ERR_STREAM_PUSH_AFTER_EOF)
332
- if (this._ended && chunk !== null) {
333
- const err = new Error("stream.push() after EOF");
334
- err.code = "ERR_STREAM_PUSH_AFTER_EOF";
335
- deferTask(() => this.emit("error", err));
336
- return false;
337
- }
338
- if (chunk === null) {
339
- // Prevent duplicate end handling
340
- if (this._ended) {
341
- return false;
342
- }
343
- this._ended = true;
344
- // Emit 'end' only after buffered data is fully drained.
345
- // This avoids premature 'end' when producers push null while paused.
346
- // Defer via deferTask to match Node.js process.nextTick behavior —
347
- // synchronous code after push(null) should not see 'end' yet.
348
- if (this._buf.length === 0) {
349
- deferTask(() => this._emitEndOnce());
350
- }
351
- // Note: Don't call destroy() here, let the stream be consumed naturally
352
- // The reader will return done:true when it finishes reading
353
- return false;
354
- }
355
- // Node.js: In binary mode, pushing a zero-length chunk (empty Buffer/Uint8Array
356
- // or string that encodes to empty) is a no-op — it is silently ignored.
357
- // In object mode, all values (including empty buffers) are valid chunks.
358
- if (!this._objectMode) {
359
- const sized = chunk;
360
- const len = typeof chunk === "string" ? chunk.length : (sized?.byteLength ?? sized?.length ?? 0);
361
- if (len === 0) {
362
- return true;
363
- }
364
- }
365
- if (this._flowing) {
366
- // IMPORTANT: If there is still buffered data waiting to be drained (from a
367
- // resume() microtask that hasn't fired yet), we must NOT emit this chunk
368
- // directly — doing so would deliver it out-of-order, ahead of the buffered
369
- // chunks. Buffer it instead and let the drain microtask deliver everything
370
- // in the correct sequence.
371
- if (this._buf.length > 0) {
372
- this._buf.push(chunk);
373
- return this._objectMode
374
- ? this._buf.length < this._highWaterMark
375
- : this._buf.byteSize < this._highWaterMark;
376
- }
377
- // In flowing mode with empty buffer, emit data directly
378
- this._didRead = true;
379
- this.emit("data", this._applyEncoding(chunk));
380
- // Check if stream was paused during emit (backpressure from consumer)
381
- if (!this._flowing) {
382
- return false;
383
- }
384
- // After emitting data, call _read again if available (Node.js behavior)
385
- if (this._hasReadImpl && !this._ended) {
386
- deferTask(() => {
387
- if (this._flowing && !this._ended && !this._destroyed) {
388
- this._callRead(this._highWaterMark);
389
- // Sync end check after _callRead — see resume() for rationale.
390
- if (this._ended && this._buf.length === 0) {
391
- this._emitEndOnce();
392
- }
393
- }
394
- });
395
- }
396
- // In flowing mode, return true (no backpressure since data is immediately consumed)
397
- return true;
398
- }
399
- else {
400
- // In paused mode, buffer for later
401
- const wasEmpty = this._buf.length === 0;
402
- this._buf.push(chunk);
403
- // Emit readable event when buffer goes from empty to having data
404
- if (wasEmpty) {
405
- deferTask(() => this.emit("readable"));
406
- }
407
- // Return false if buffer exceeds high water mark (backpressure signal)
408
- // Fast path for object mode - just count items
409
- if (this._objectMode) {
410
- return this._buf.length < this._highWaterMark;
411
- }
412
- // For binary mode, use tracked buffer size (O(1))
413
- return this._buf.byteSize < this._highWaterMark;
414
- }
415
- }
416
- _emitEndOnce() {
417
- if (this._endEmitted || this._destroyed) {
418
- return;
419
- }
420
- // Guard: if data was unshifted after push(null) scheduled us,
421
- // the buffer is non-empty — defer 'end' until data is consumed.
422
- if (this._buf.length > 0) {
423
- return;
424
- }
425
- // Flush the stream decoder's remainder (e.g. partial base64 group).
426
- // This mirrors Node.js StringDecoder.end() which is called before 'end'.
427
- const flushed = this._flushDecoder();
428
- if (flushed) {
429
- this.emit("data", flushed);
430
- }
431
- this._endEmitted = true;
432
- this.emit("end");
433
- // Match Node.js autoDestroy behavior: automatically destroy after end
434
- if (this._autoDestroy) {
435
- this.destroy();
436
- }
437
- }
438
- /**
439
- * Put a chunk back at the front of the buffer
440
- */
441
- unshift(chunk, encoding) {
442
- if (this._destroyed) {
443
- return;
444
- }
445
- // Node.js: unshift(null) signals EOF, just like push(null).
446
- // Note: unshift(undefined) does NOT signal EOF in Node.js — only null does.
447
- if (chunk === null) {
448
- if (!this._ended) {
449
- this._ended = true;
450
- if (this._buf.length === 0) {
451
- deferTask(() => this._emitEndOnce());
452
- }
453
- }
454
- return;
455
- }
456
- // Node.js emits ERR_STREAM_UNSHIFT_AFTER_END_EVENT when unshifting data
457
- // after the 'end' event has been emitted (readableEnded === true).
458
- // Note: unshift(data) right after push(null) but before end event is
459
- // silently ignored by Node.js — we match that by checking _endEmitted.
460
- if (this._endEmitted) {
461
- const err = new Error("stream.unshift() after end event");
462
- err.code = "ERR_STREAM_UNSHIFT_AFTER_END_EVENT";
463
- deferTask(() => this.emit("error", err));
464
- return;
465
- }
466
- // Handle string encoding (Node.js compatibility)
467
- // Node.js always converts strings to Buffer in binary mode, defaulting to utf8.
468
- if (typeof chunk === "string" && !this._objectMode) {
469
- const encoded = stringToEncodedBytes(chunk, encoding || "utf8");
470
- encoded.toString = encodedBytesToString;
471
- // Boundary: in binary (non-object) mode chunks are materialized as bytes;
472
- // the encoded Uint8Array is the runtime chunk for element type T.
473
- chunk = encoded;
474
- }
475
- const wasEmpty = this._buf.length === 0;
476
- this._buf.unshift(chunk);
477
- if (this._flowing) {
478
- // In flowing mode, schedule a drain microtask to deliver the unshifted data.
479
- // This is needed when unshift() is called after push(null) — the original
480
- // drain loop has already finished, so we must schedule a new one to emit
481
- // the data before 'end'.
482
- deferTask(() => {
483
- while (this._buf.length > 0 && this._flowing) {
484
- const c = this._buf.shift();
485
- this._didRead = true;
486
- this.emit("data", this._applyEncoding(c));
487
- }
488
- if (this._ended && this._buf.length === 0) {
489
- this._emitEndOnce();
490
- }
491
- });
492
- }
493
- else if (wasEmpty) {
494
- // Node.js emits 'readable' when data is unshifted into an empty buffer
495
- // while in paused mode, so that consumers know data is available.
496
- deferTask(() => this.emit("readable"));
497
- }
498
- }
499
- /**
500
- * Safely invoke _read(), catching synchronous exceptions.
501
- * Node.js catches sync throws in _read and emits an error event, then
502
- * destroys the stream. We mirror that behaviour here.
503
- */
504
- _callRead(size) {
505
- try {
506
- this._read(size);
507
- }
508
- catch (err) {
509
- const error = err instanceof Error ? err : new Error(String(err));
510
- // _callRead is always invoked from within a deferTask callback, so
511
- // we can destroy synchronously instead of scheduling another deferTask.
512
- // This ensures error/close fire before interleaved Promise.then
513
- // callbacks, matching Node.js process.nextTick nesting behavior.
514
- // Set _internalDestroy to signal afterDestroy to emit synchronously.
515
- this._internalDestroy = true;
516
- this.destroy(error);
517
- }
518
- }
519
- /**
520
- * Read data from the stream.
521
- *
522
- * Node.js behavior:
523
- * - read() / read(undefined) — in object mode returns one object;
524
- * in binary mode returns all buffered data as one chunk
525
- * - read(0) — returns null, may trigger internal _read()
526
- * - read(n) — returns exactly n bytes (binary) or null if not enough;
527
- * in object mode returns one object (size ignored)
528
- */
529
- read(size) {
530
- this._didRead = true;
531
- // Node.js normalization: parseInt then isNaN check.
532
- // NaN/null/undefined/Infinity/-Infinity/false/"" all become NaN after parseInt,
533
- // which means "read all available data" (same as read() with no args).
534
- // Negative integers return null. Zero returns null but may trigger _read.
535
- let n;
536
- if (size != null) {
537
- n = parseInt(String(size), 10);
538
- if (isNaN(n)) {
539
- n = undefined; // treat as read() — read all
540
- }
541
- else if (n <= 0) {
542
- // size === 0: return null but may trigger internal _read
543
- if (n === 0 && !this._ended && !this._destroyed && this._constructed) {
544
- const bufSize = this._objectMode ? this._buf.length : this._buf.byteSize;
545
- if (bufSize < this._highWaterMark || bufSize === 0) {
546
- this._callRead(this._highWaterMark);
547
- }
548
- }
549
- return null;
550
- }
551
- }
552
- if (this._buf.length === 0) {
553
- // Buffer is empty — trigger _read() to fill it (matches Node.js behavior
554
- // where read() on an empty buffer requests more data from the source).
555
- if (!this._ended && !this._destroyed && this._constructed) {
556
- this._callRead(this._highWaterMark);
557
- }
558
- return null;
559
- }
560
- // Object mode: always return a single object, ignore size
561
- if (this._objectMode) {
562
- const chunk = this._buf.shift();
563
- if (this._ended && this._buf.length === 0) {
564
- deferTask(() => this._emitEndOnce());
565
- }
566
- // Node.js triggers _read() to refill buffer after consuming an object
567
- if (this._hasReadImpl && !this._ended && !this._destroyed && this._constructed) {
568
- if (this._buf.length < this._highWaterMark) {
569
- deferTask(() => {
570
- if (!this._ended && !this._destroyed) {
571
- this._callRead(this._highWaterMark);
572
- }
573
- });
574
- }
575
- }
576
- return chunk;
577
- }
578
- // Binary mode
579
- let result;
580
- if (n == null) {
581
- // read() with no size: return all buffered data as one chunk.
582
- // Note: Node.js 26+ changed to return a single chunk, but our browser
583
- // implementation preserves the pre-26 behavior (consumeAll) for backward
584
- // compatibility and because it matches what most consumers expect.
585
- result = this._applyEncoding(this._buf.consumeAll());
586
- }
587
- else {
588
- // read(n): return exactly n bytes, or null if not enough
589
- if (this._buf.byteSize < n) {
590
- // Not enough data buffered
591
- if (this._ended) {
592
- // Stream ended — return whatever is available
593
- result = this._applyEncoding(this._buf.consumeAll());
594
- }
595
- else {
596
- // Trigger internal read to fill buffer
597
- // Node.js passes Math.max(n, highWaterMark) to _read so that
598
- // custom _read implementations know the consumer's requested size.
599
- if (this._hasReadImpl && this._constructed) {
600
- this._callRead(Math.max(n, this._highWaterMark));
601
- }
602
- return null;
603
- }
604
- }
605
- else {
606
- result = this._applyEncoding(this._buf.consumeBytes(n));
607
- }
608
- }
609
- // Trigger _read to refill buffer if below HWM
610
- if (this._hasReadImpl && !this._ended && !this._destroyed) {
611
- const bufSize = this._buf.byteSize;
612
- if (bufSize < this._highWaterMark) {
613
- deferTask(() => {
614
- if (!this._ended && !this._destroyed) {
615
- this._callRead(this._highWaterMark);
616
- }
617
- });
618
- }
619
- }
620
- if (this._ended && this._buf.length === 0) {
621
- deferTask(() => this._emitEndOnce());
622
- }
623
- // Node.js re-emits 'readable' when there is still data in the buffer
624
- // after a read(), so consumers know more data is available.
625
- if (!this._flowing && this._buf.length > 0) {
626
- deferTask(() => this.emit("readable"));
627
- }
628
- return result;
629
- }
630
- /**
631
- * Flush the stream decoder's internal remainder and clear it.
632
- * Returns the flushed string, or null if nothing to flush.
633
- * Called when the stream ends, both from the event path (_emitEndOnce)
634
- * and the async iterator path (_initAsyncIterState).
635
- */
636
- _flushDecoder() {
637
- if (!this._decoder) {
638
- return null;
639
- }
640
- const final = this._decoder.decode(new Uint8Array(0), { stream: false });
641
- this._decoder = null;
642
- return final || null;
643
- }
644
- /**
645
- * Set encoding for string output.
646
- * Creates a private TextDecoder instance for streaming decode to avoid
647
- * sharing mutable state across concurrent streams.
648
- */
649
- setEncoding(encoding) {
650
- this._encoding = encoding;
651
- // Always create a fresh decoder for this stream — the { stream: true }
652
- // option in _applyEncoding makes the decoder stateful (it retains
653
- // incomplete multi-byte sequences between calls). Sharing a cached
654
- // singleton across streams would corrupt multi-byte boundaries.
655
- this._decoder = createStreamDecoder(encoding);
656
- return this;
657
- }
658
- _applyEncoding(chunk) {
659
- if (this._encoding && chunk instanceof Uint8Array) {
660
- if (!this._decoder) {
661
- this._decoder = createStreamDecoder(this._encoding);
662
- }
663
- // Pass {stream: true} to handle multi-byte characters that may span
664
- // chunk boundaries (matches Node.js StringDecoder behavior).
665
- // Boundary: with an encoding set, byte chunks are decoded to strings,
666
- // which become the stream's element type T.
667
- return this._decoder.decode(chunk, { stream: true });
668
- }
669
- return chunk;
670
- }
671
- /**
672
- * Wrap an old-style stream
673
- */
674
- wrap(stream) {
675
- let paused = false;
676
- stream.on("data", (chunk) => {
677
- if (!this.push(chunk)) {
678
- stream.pause();
679
- paused = true;
680
- }
681
- });
682
- stream.on("end", () => this.push(null));
683
- stream.on("error", (err) => this.destroy(err));
684
- stream.on("close", () => this.destroy());
685
- // When the Readable's buffer drains, _read is called — resume the source.
686
- // This matches Node.js wrap() behavior where _read triggers source.resume().
687
- this._hasReadImpl = true;
688
- this._read = () => {
689
- if (paused) {
690
- paused = false;
691
- stream.resume();
692
- }
693
- };
694
- return this;
695
- }
696
- /**
697
- * Pause the stream
698
- */
699
- pause() {
700
- if (this._flowing) {
701
- this._flowing = false;
702
- this.emit("pause");
703
- }
704
- else {
705
- // Node.js: pause() on a fresh stream transitions readableFlowing
706
- // from null to false and sets isPaused() to true.
707
- this._hasFlowed = true;
708
- }
709
- return this;
710
- }
711
- /**
712
- * Resume the stream
713
- */
714
- resume() {
715
- if (!this._flowing) {
716
- this._flowing = true;
717
- this._hasFlowed = true;
718
- // Emit asynchronously to match Node.js process.nextTick timing
719
- // Use _resumeScheduled guard to prevent duplicate emissions when
720
- // resume() is called multiple times synchronously (e.g. on("data") + resume())
721
- if (!this._resumeScheduled) {
722
- this._resumeScheduled = true;
723
- deferTask(() => {
724
- this._resumeScheduled = false;
725
- this.emit("resume");
726
- });
727
- }
728
- }
729
- // Drain buffered data asynchronously (matches Node.js process.nextTick behavior).
730
- // Node.js does NOT drain synchronously on resume() — it defers to nextTick
731
- // so that multiple resume()/pipe() calls can register before data flows.
732
- if (this._buf.length > 0) {
733
- deferTask(() => {
734
- while (this._buf.length > 0 && this._flowing) {
735
- const chunk = this._buf.shift();
736
- this._didRead = true;
737
- this.emit("data", this._applyEncoding(chunk));
738
- }
739
- // After draining, check for end
740
- if (this._ended && this._buf.length === 0) {
741
- this._emitEndOnce();
742
- }
743
- // If we were paused due to backpressure while reading from a Web Stream,
744
- // resume reading once the buffered chunks have been drained.
745
- if (this._flowing &&
746
- this._buf.length === 0 &&
747
- this._webStreamMode &&
748
- !this._pushMode &&
749
- !this._ended &&
750
- !this._destroyed) {
751
- this._startReading();
752
- }
753
- // After draining buffered data, call _read() so the source can refill.
754
- // This is needed for wrap() where _read resumes the wrapped stream.
755
- if (this._hasReadImpl &&
756
- this._flowing &&
757
- !this._ended &&
758
- !this._destroyed &&
759
- this._constructed) {
760
- const bufSize = this._objectMode ? this._buf.length : this._buf.byteSize;
761
- if (bufSize < this._highWaterMark || bufSize === 0) {
762
- this._callRead(this._highWaterMark);
763
- }
764
- }
765
- });
766
- }
767
- else if (this._ended && this._buf.length === 0) {
768
- deferTask(() => this._emitEndOnce());
769
- }
770
- else if (this._webStreamMode && !this._pushMode) {
771
- // Start reading from underlying Web Stream when resuming.
772
- this._startReading();
773
- }
774
- else if (this._hasReadImpl) {
775
- // Call user-provided read function asynchronously
776
- // This allows multiple pipe() calls to register before data flows
777
- deferTask(() => {
778
- if (this._flowing && !this._ended && !this._destroyed && this._constructed) {
779
- this._callRead(this._highWaterMark);
780
- // After _callRead, check if push(null) was called synchronously.
781
- // If so, emit 'end' here rather than letting push(null)'s own
782
- // deferTask schedule it — otherwise a user Promise.then
783
- // queued before this microtask could observe readableEnded=false,
784
- // diverging from Node.js where process.nextTick drains greedily.
785
- if (this._ended && this._buf.length === 0) {
786
- this._emitEndOnce();
787
- }
788
- }
789
- });
790
- }
791
- return this;
792
- }
793
- /**
794
- * Override on() to automatically resume when 'data' listener is added
795
- * This matches Node.js behavior where adding a 'data' listener puts
796
- * the stream into flowing mode.
797
- */
798
- on(event, listener) {
799
- super.on(event, listener);
800
- // When a 'data' listener is added, switch to flowing mode
801
- if (event === "data") {
802
- this.resume();
803
- }
804
- else if (event === "readable") {
805
- // Node.js: adding a 'readable' listener always sets readableFlowing to
806
- // false, even if the stream is currently flowing. This pauses the stream
807
- // so consumers can use the pull-based readable interface.
808
- this._hasFlowed = true;
809
- this._flowing = false;
810
- if (this._buf.length > 0 || this._ended) {
811
- deferTask(() => this.emit("readable"));
812
- }
813
- }
814
- return this;
815
- }
816
- /**
817
- * Check if paused
818
- */
819
- isPaused() {
820
- // Node.js: isPaused() returns true only when readableFlowing is strictly false.
821
- // When readableFlowing is null (initial state), isPaused() returns false.
822
- return this.readableFlowing === false;
823
- }
824
- /**
825
- * Pipe to a writable stream, transform stream, or duplex stream.
826
- * Accepts any writable-like object (duck-typed, matching Node.js behavior).
827
- */
828
- pipe(destination, options) {
829
- return this._pipes.pipe(destination, options);
830
- }
831
- /**
832
- * Unpipe from destination
833
- */
834
- unpipe(destination) {
835
- this._pipes.unpipe(destination);
836
- return this;
837
- }
838
- /**
839
- * Destroy the stream
840
- */
841
- destroy(error) {
842
- if (this._destroyed) {
843
- return this;
844
- }
845
- this._destroyed = true;
846
- // Node.js: destroy() resets readable override so getter reflects true state
847
- this._readableOverride = undefined;
848
- // Note: Node.js does NOT clear the readable buffer synchronously on destroy.
849
- // readableLength retains its value even after destroy() returns.
850
- // Ensure we detach from destinations to avoid leaking listeners.
851
- this.unpipe();
852
- if (this._reader) {
853
- const reader = this._reader;
854
- this._reader = null;
855
- reader
856
- .cancel()
857
- .catch(() => { })
858
- .finally(() => {
859
- try {
860
- reader.releaseLock();
861
- }
862
- catch {
863
- // Ignore if a read is still pending
864
- }
865
- });
866
- }
867
- // If subclass overrides _destroy, call it and wait for callback before
868
- // emitting error/close (matches Node.js behavior).
869
- const afterDestroy = (finalError) => {
870
- // Node.js: _destroy's callback determines whether an error is emitted.
871
- // - cb(null) or cb() or cb(undefined): suppress the original error
872
- // - cb(new Error(...)): replace with the new error, emit it
873
- // Node.js checks `if (err)` on the callback argument, so null/undefined/no-arg
874
- // all suppress the error. Only a truthy error value triggers the error event.
875
- const err = finalError || null;
876
- if (err) {
877
- this._errored = err;
878
- }
879
- this._closed = true;
880
- const doEmit = () => {
881
- if (err) {
882
- this.emit("error", err);
883
- }
884
- if (this._emitClose) {
885
- this.emit("close");
886
- }
887
- };
888
- // When already inside a deferTask callback (e.g. autoDestroy after
889
- // 'end') and there is no async _destroy hook, emit synchronously.
890
- // This matches Node.js where nested process.nextTick runs before
891
- // interleaved Promise.then callbacks.
892
- // Only apply when the stream ended naturally (autoDestroy path) or
893
- // was destroyed internally (e.g. _callRead error) — user-initiated
894
- // destroy() from within a listener must still defer so that async
895
- // cleanup (e.g. iterator.return()) can complete.
896
- const isInternalPath = this._endEmitted || this._internalDestroy;
897
- this._internalDestroy = false;
898
- if (inDeferredContext() && !this._hasDestroyHook() && isInternalPath) {
899
- doEmit();
900
- }
901
- else {
902
- deferTask(doEmit);
903
- }
904
- };
905
- if (this._hasDestroyHook()) {
906
- try {
907
- this._destroy(error ?? null, afterDestroy);
908
- }
909
- catch (err) {
910
- afterDestroy(err instanceof Error ? err : new Error(String(err)));
911
- }
912
- }
913
- else {
914
- afterDestroy(error);
915
- }
916
- return this;
917
- }
918
- /**
919
- * Override in subclass to customise destroy behaviour.
920
- * Call `callback(err)` when cleanup is complete.
921
- */
922
- _destroy(error, callback) {
923
- callback(error);
924
- }
925
- /**
926
- * Reverse the effects of destroy() so the stream can potentially be reused.
927
- * Matches Node.js _undestroy() which resets destroyed and closed flags.
928
- */
929
- _undestroy() {
930
- this._destroyed = false;
931
- this._closed = false;
932
- this._errored = null;
933
- }
934
- /** Check if _destroy has been overridden by a subclass or constructor option. */
935
- _hasDestroyHook() {
936
- return (Object.prototype.hasOwnProperty.call(this, "_destroy") ||
937
- Object.getPrototypeOf(this)._destroy !== Readable.prototype._destroy);
938
- }
939
- /**
940
- * Get a Web ReadableStream view of this stream (internal).
941
- *
942
- * For external Web Streams (_webStreamMode), returns the original stream.
943
- * For controllable streams, creates a pull-based ReadableStream that reads
944
- * from the Node-style Readable on demand — matching Node.js `Readable.toWeb()`
945
- * which does NOT force the stream into flowing mode.
946
- * @internal
947
- */
948
- get _webStream() {
949
- if (this._stream) {
950
- return this._stream;
951
- }
952
- // Build a pull-based Web ReadableStream that only reads from the
953
- // Node-style Readable when the consumer requests data (via pull()).
954
- // This avoids forcing the stream into flowing mode immediately.
955
- // oxlint-disable-next-line no-this-alias -- needed for ReadableStream callback closures
956
- const readable = this;
957
- let ended = false;
958
- const ws = new ReadableStream({
959
- pull(controller) {
960
- return new Promise((resolve, reject) => {
961
- if (ended) {
962
- return resolve();
963
- }
964
- const tryRead = () => {
965
- // If the readable was destroyed or errored while we were waiting,
966
- // check before attempting read.
967
- if (readable._destroyed) {
968
- if (readable._errored) {
969
- ended = true;
970
- try {
971
- controller.error(readable._errored);
972
- }
973
- catch {
974
- /* controller may already be errored/closed */
975
- }
976
- return reject(readable._errored);
977
- }
978
- ended = true;
979
- try {
980
- controller.close();
981
- }
982
- catch {
983
- /* controller may already be closed */
984
- }
985
- return resolve();
986
- }
987
- const chunk = readable.read();
988
- if (chunk !== null) {
989
- try {
990
- controller.enqueue(chunk);
991
- }
992
- catch {
993
- /* controller may be closed */
994
- }
995
- return resolve();
996
- }
997
- // No data available yet — check if the stream has ended.
998
- if (readable._endEmitted) {
999
- ended = true;
1000
- try {
1001
- controller.close();
1002
- }
1003
- catch {
1004
- /* controller may already be closed */
1005
- }
1006
- return resolve();
1007
- }
1008
- // Wait for more data or end/error.
1009
- const onReadable = () => {
1010
- cleanup();
1011
- tryRead();
1012
- };
1013
- const onEnd = () => {
1014
- cleanup();
1015
- ended = true;
1016
- try {
1017
- controller.close();
1018
- }
1019
- catch {
1020
- /* controller may already be closed */
1021
- }
1022
- resolve();
1023
- };
1024
- const onError = (err) => {
1025
- cleanup();
1026
- ended = true;
1027
- try {
1028
- controller.error(err);
1029
- }
1030
- catch {
1031
- /* controller may already be errored/closed */
1032
- }
1033
- reject(err);
1034
- };
1035
- const onClose = () => {
1036
- cleanup();
1037
- if (!ended) {
1038
- ended = true;
1039
- try {
1040
- controller.close();
1041
- }
1042
- catch {
1043
- /* controller may already be closed */
1044
- }
1045
- }
1046
- resolve();
1047
- };
1048
- const cleanup = () => {
1049
- readable.off("readable", onReadable);
1050
- readable.off("end", onEnd);
1051
- readable.off("error", onError);
1052
- readable.off("close", onClose);
1053
- };
1054
- readable.once("readable", onReadable);
1055
- readable.once("end", onEnd);
1056
- readable.once("error", onError);
1057
- readable.once("close", onClose);
1058
- };
1059
- tryRead();
1060
- });
1061
- },
1062
- cancel() {
1063
- readable.destroy();
1064
- }
1065
- });
1066
- this._stream = ws;
1067
- return ws;
1068
- }
1069
- get readable() {
1070
- if (this._readableOverride !== undefined) {
1071
- return this._readableOverride;
1072
- }
1073
- // Node.js: readable stays true while buffer has data, even after push(null).
1074
- // It only becomes false after the 'end' event has been emitted (or on destroy).
1075
- return !this._destroyed && !this._endEmitted;
1076
- }
1077
- set readable(val) {
1078
- this._readableOverride = val;
1079
- }
1080
- get readableEnded() {
1081
- // Node.js: readableEnded only becomes true after the 'end' event has been emitted,
1082
- // not when push(null) is called.
1083
- return this._endEmitted;
1084
- }
1085
- get readableLength() {
1086
- return this._objectMode ? this._buf.length : this._buf.byteSize;
1087
- }
1088
- /** Whether the stream has been destroyed */
1089
- get destroyed() {
1090
- return this._destroyed;
1091
- }
1092
- set destroyed(val) {
1093
- this._destroyed = val;
1094
- }
1095
- /** The error that destroyed the stream, or null */
1096
- get errored() {
1097
- return this._errored;
1098
- }
1099
- /** Whether the stream has been closed */
1100
- get closed() {
1101
- return this._closed;
1102
- }
1103
- /** Whether the stream is in flowing mode */
1104
- get readableFlowing() {
1105
- if (this._flowing) {
1106
- return true;
1107
- }
1108
- // Distinguish between "never flowed" (null) and "paused after flowing" (false)
1109
- return this._hasFlowed ? false : null;
1110
- }
1111
- set readableFlowing(value) {
1112
- if (value === true) {
1113
- this._flowing = true;
1114
- this._hasFlowed = true;
1115
- }
1116
- else if (value === false) {
1117
- this._flowing = false;
1118
- this._hasFlowed = true;
1119
- }
1120
- else {
1121
- // null: reset to initial state
1122
- this._flowing = false;
1123
- this._hasFlowed = false;
1124
- }
1125
- }
1126
- /**
1127
- * Internal method to set readableFlowing state.
1128
- * Used by Transform/Duplex to control the internal Readable's flow state.
1129
- * @internal
1130
- */
1131
- _setReadableFlowing(value) {
1132
- this.readableFlowing = value;
1133
- }
1134
- /** Whether the stream was aborted (destroyed or errored before 'end' was emitted) */
1135
- get readableAborted() {
1136
- // Node.js: readable !== false && (destroyed || errored) && !endEmitted
1137
- // The 'readable !== false' check is for when user explicitly sets stream.readable = false.
1138
- // We check _readableOverride (not the computed getter which returns false after destroy).
1139
- if (this._readableOverride === false) {
1140
- return false;
1141
- }
1142
- return (this._destroyed || !!this._errored) && !this._endEmitted;
1143
- }
1144
- /** Whether read() has ever been called */
1145
- get readableDidRead() {
1146
- return this._didRead;
1147
- }
1148
- /** Current encoding or null */
1149
- get readableEncoding() {
1150
- return this._encoding;
1151
- }
1152
- /** Returns array of objects containing info about buffered data */
1153
- get readableObjectMode() {
1154
- return this._objectMode;
1155
- }
1156
- get readableHighWaterMark() {
1157
- return this._highWaterMark;
1158
- }
1159
- /**
1160
- * Get the internal buffer contents as an array (matches Node.js BufferList behavior)
1161
- */
1162
- get readableBuffer() {
1163
- return this._buf.toArray();
1164
- }
1165
- /**
1166
- * Release the internal reader lock, clearing _reader.
1167
- * Safe to call even when no reader is held.
1168
- */
1169
- _releaseReader() {
1170
- if (this._reader) {
1171
- const reader = this._reader;
1172
- this._reader = null;
1173
- try {
1174
- reader.releaseLock();
1175
- }
1176
- catch {
1177
- // Ignore if a read is still pending
1178
- }
1179
- }
1180
- }
1181
- async _startReading() {
1182
- if (this._reading || this._destroyed || !this._flowing) {
1183
- return;
1184
- }
1185
- this._reading = true;
1186
- try {
1187
- if (!this._reader) {
1188
- this._reader = this._stream.getReader();
1189
- }
1190
- while (this._flowing && !this._destroyed && !this._pushMode) {
1191
- const { done, value } = await this._reader.read();
1192
- // Check _pushMode again after async read - if push() was called, stop reading
1193
- if (this._pushMode) {
1194
- this._releaseReader();
1195
- break;
1196
- }
1197
- if (done) {
1198
- this._ended = true;
1199
- if (this._buf.length === 0) {
1200
- // Defer end to match Node.js nextTick-ish timing.
1201
- deferTask(() => this._emitEndOnce());
1202
- }
1203
- this._releaseReader();
1204
- break;
1205
- }
1206
- if (value !== undefined) {
1207
- // Always buffer Web Stream chunks (Node.js Readable buffers internally).
1208
- // If flowing, immediately drain the buffer into 'data' events.
1209
- const wasEmpty = this._buf.length === 0;
1210
- this._buf.push(value);
1211
- if (wasEmpty) {
1212
- deferTask(() => this.emit("readable"));
1213
- }
1214
- while (this._buf.length > 0 && this._flowing) {
1215
- const chunk = this._buf.shift();
1216
- this._didRead = true;
1217
- this.emit("data", this._applyEncoding(chunk));
1218
- }
1219
- // Backpressure: if paused, stop reading from the underlying Web Stream
1220
- // until resume() drains buffered chunks.
1221
- if (!this._flowing) {
1222
- break;
1223
- }
1224
- }
1225
- }
1226
- }
1227
- catch (err) {
1228
- this.emit("error", err);
1229
- this._releaseReader();
1230
- }
1231
- finally {
1232
- this._reading = false;
1233
- }
1234
- }
1235
- /**
1236
- * Async iterator support.
1237
- *
1238
- * Node.js parity: multiple calls to `[Symbol.asyncIterator]()` return
1239
- * independent iterators that *compete* for chunks from the same underlying
1240
- * stream. Each chunk is delivered to exactly one waiting iterator (FIFO).
1241
- * A single shared `data` listener is attached on the first call; subsequent
1242
- * calls reuse the same shared state.
1243
- */
1244
- async *[Symbol.asyncIterator]() {
1245
- // Lazily initialise the shared async-iterator state on first call.
1246
- if (!this._asyncIterState) {
1247
- this._asyncIterState = this._initAsyncIterState();
1248
- }
1249
- const state = this._asyncIterState;
1250
- state.activeCount++;
1251
- try {
1252
- while (true) {
1253
- if (state.streamError) {
1254
- throw state.streamError;
1255
- }
1256
- // Try to consume a chunk from the shared queue (competitive).
1257
- if (state.dataQueueIndex < state.dataQueue.length) {
1258
- const chunk = state.dataQueue[state.dataQueueIndex];
1259
- state.dataQueue[state.dataQueueIndex] = undefined; // release ref
1260
- state.dataQueueIndex++;
1261
- state.queuedSize -= this._chunkSizeForBackpressure(chunk);
1262
- // Compact the queue periodically.
1263
- if (state.dataQueueIndex >= 1024 && state.dataQueueIndex * 2 >= state.dataQueue.length) {
1264
- state.dataQueue.splice(0, state.dataQueueIndex);
1265
- state.dataQueueIndex = 0;
1266
- }
1267
- this._maybeResumeIterState(state);
1268
- yield chunk;
1269
- continue;
1270
- }
1271
- if (state.done) {
1272
- break;
1273
- }
1274
- // No data available — park this iterator until a chunk arrives.
1275
- const chunk = await new Promise((resolve, reject) => {
1276
- state.resolverQueue.push({ resolve, reject });
1277
- });
1278
- if (chunk === null) {
1279
- // Stream ended / closed while we were waiting.
1280
- break;
1281
- }
1282
- // Chunk was delivered directly by dataHandler (bypassed the queue),
1283
- // so queuedSize was never incremented — no need to decrement here.
1284
- yield chunk;
1285
- }
1286
- }
1287
- finally {
1288
- state.activeCount--;
1289
- // Node.js destroys the stream when ANY async iterator exits early
1290
- // (break/return/throw), even if other iterators are still active.
1291
- // This causes the remaining iterators' pending next() calls to resolve
1292
- // with done:true (via the 'close' handler).
1293
- if (state.activeCount === 0) {
1294
- this._teardownAsyncIterState();
1295
- }
1296
- if (!this._destroyed) {
1297
- this.destroy();
1298
- }
1299
- }
1300
- }
1301
- // ---------------------------------------------------------------------------
1302
- // Async iterator helpers (private)
1303
- // ---------------------------------------------------------------------------
1304
- _chunkSizeForBackpressure(chunk) {
1305
- if (this._objectMode) {
1306
- return 1;
1307
- }
1308
- if (chunk instanceof Uint8Array) {
1309
- return chunk.byteLength;
1310
- }
1311
- if (typeof chunk === "string") {
1312
- return chunk.length;
1313
- }
1314
- return 1;
1315
- }
1316
- _maybeResumeIterState(state) {
1317
- if (state.pausedByIterator &&
1318
- state.queuedSize < this._highWaterMark &&
1319
- !state.done &&
1320
- !this._destroyed) {
1321
- state.pausedByIterator = false;
1322
- this.resume();
1323
- }
1324
- }
1325
- /**
1326
- * Create and attach the shared async-iterator state.
1327
- * A single `data` listener routes chunks to the first waiting resolver
1328
- * (FIFO) or enqueues them. This matches Node.js semantics where multiple
1329
- * iterators compete for the same data stream.
1330
- */
1331
- _initAsyncIterState() {
1332
- const hwm = this._highWaterMark;
1333
- const noop = () => { };
1334
- const state = {
1335
- dataQueue: [],
1336
- dataQueueIndex: 0,
1337
- queuedSize: 0,
1338
- resolverQueue: [],
1339
- done: false,
1340
- streamError: null,
1341
- activeCount: 0,
1342
- listenersAttached: false,
1343
- pausedByIterator: false,
1344
- // Handlers are placeholder no-ops here and replaced below, where they
1345
- // can close over `state`.
1346
- dataHandler: noop,
1347
- doneHandler: noop,
1348
- errorHandler: noop
1349
- };
1350
- // -- data handler ---------------------------------------------------------
1351
- state.dataHandler = (chunk) => {
1352
- // Deliver directly to the first waiting resolver (FIFO competitive).
1353
- // Chunks delivered directly do not contribute to queuedSize because
1354
- // they never sit in the buffer — this avoids a transient inflation
1355
- // that could trigger an unnecessary pause().
1356
- if (state.resolverQueue.length > 0) {
1357
- const { resolve } = state.resolverQueue.shift();
1358
- resolve(chunk);
1359
- return;
1360
- }
1361
- state.dataQueue.push(chunk);
1362
- state.queuedSize += this._chunkSizeForBackpressure(chunk);
1363
- if (!state.pausedByIterator && state.queuedSize >= hwm) {
1364
- state.pausedByIterator = true;
1365
- this.pause();
1366
- }
1367
- };
1368
- // -- end / close handler --------------------------------------------------
1369
- state.doneHandler = () => {
1370
- state.done = true;
1371
- // Resolve all waiting iterators with null (signals completion).
1372
- for (const { resolve } of state.resolverQueue) {
1373
- resolve(null);
1374
- }
1375
- state.resolverQueue.length = 0;
1376
- };
1377
- // -- error handler --------------------------------------------------------
1378
- state.errorHandler = (err) => {
1379
- state.done = true;
1380
- state.streamError = err;
1381
- // Reject all waiting iterators.
1382
- for (const { reject } of state.resolverQueue) {
1383
- reject(err);
1384
- }
1385
- state.resolverQueue.length = 0;
1386
- };
1387
- // Drain any already-buffered data into the shared queue before attaching
1388
- // the `data` listener so that existing chunks are not lost.
1389
- while (this._buf.length > 0) {
1390
- const chunk = this._applyEncoding(this._buf.shift());
1391
- state.dataQueue.push(chunk);
1392
- state.queuedSize += this._chunkSizeForBackpressure(chunk);
1393
- }
1394
- if (this._ended) {
1395
- // Flush the stream decoder's remainder (e.g. partial base64 group)
1396
- // before marking the iterator as done. This mirrors the flush that
1397
- // _emitEndOnce performs for the event-based consumption path.
1398
- const flushed = this._flushDecoder();
1399
- if (flushed) {
1400
- // Boundary: a decoder flush yields a trailing string chunk, which is
1401
- // the element type T when an encoding is set on the stream.
1402
- const flushedChunk = flushed;
1403
- state.dataQueue.push(flushedChunk);
1404
- state.queuedSize += this._chunkSizeForBackpressure(flushedChunk);
1405
- }
1406
- state.done = true;
1407
- }
1408
- else {
1409
- this.on("data", state.dataHandler);
1410
- this.on("end", state.doneHandler);
1411
- this.on("error", state.errorHandler);
1412
- this.on("close", state.doneHandler);
1413
- state.listenersAttached = true;
1414
- // Drive the stream into flowing mode.
1415
- this.resume();
1416
- }
1417
- return state;
1418
- }
1419
- /** Remove shared listeners and discard state. */
1420
- _teardownAsyncIterState() {
1421
- const state = this._asyncIterState;
1422
- if (!state) {
1423
- return;
1424
- }
1425
- if (state.listenersAttached) {
1426
- this.off("data", state.dataHandler);
1427
- this.off("end", state.doneHandler);
1428
- this.off("error", state.errorHandler);
1429
- this.off("close", state.doneHandler);
1430
- }
1431
- this._asyncIterState = null;
1432
- }
1433
- /**
1434
- * Synchronous dispose — destroys the stream.
1435
- * Matches Node.js Symbol.dispose support (v20+, experimental).
1436
- */
1437
- [Symbol.dispose]() {
1438
- if (!this._destroyed) {
1439
- this.destroy();
1440
- }
1441
- }
1442
- /**
1443
- * Async dispose hook for `await using`.
1444
- * Destroys the stream and resolves after the 'close' event.
1445
- */
1446
- async [Symbol.asyncDispose]() {
1447
- const selfInitiated = !this._destroyed;
1448
- if (selfInitiated) {
1449
- this.destroy();
1450
- }
1451
- return new Promise((resolve, reject) => {
1452
- const settle = () => {
1453
- if (selfInitiated || this._endEmitted) {
1454
- resolve();
1455
- }
1456
- else {
1457
- reject(new Error("Premature close"));
1458
- }
1459
- };
1460
- if (this._closed) {
1461
- settle();
1462
- }
1463
- else {
1464
- this.once("close", settle);
1465
- }
1466
- });
1467
- }
1468
- /**
1469
- * Explicit iterator method (same as Symbol.asyncIterator)
1470
- */
1471
- iterator(options) {
1472
- const destroyOnReturn = options?.destroyOnReturn ?? true;
1473
- if (destroyOnReturn) {
1474
- return this[Symbol.asyncIterator]();
1475
- }
1476
- // Build a standalone async iterator that does NOT destroy the stream
1477
- // when the consumer breaks/returns early.
1478
- //
1479
- // Key difference from the default [Symbol.asyncIterator]():
1480
- // - We do NOT call resume() / flowing mode. Instead we use
1481
- // 'readable' events and explicit read() calls (pull-mode).
1482
- // This ensures the stream keeps buffered data when the consumer
1483
- // breaks early, so 'end' (and therefore autoDestroy) never fires
1484
- // prematurely — matching Node.js behavior.
1485
- // - On return() we just clean up listeners without destroying.
1486
- // oxlint-disable-next-line no-this-alias -- needed for closures in iterator object
1487
- const stream = this;
1488
- let resolveNext = null;
1489
- let rejectNext = null;
1490
- let done = false;
1491
- let streamError = null;
1492
- const tryRead = () => {
1493
- if (!resolveNext) {
1494
- return;
1495
- }
1496
- const chunk = stream.read();
1497
- if (chunk !== null) {
1498
- const r = resolveNext;
1499
- resolveNext = null;
1500
- rejectNext = null;
1501
- r({ done: false, value: chunk });
1502
- }
1503
- };
1504
- const readableHandler = () => {
1505
- tryRead();
1506
- };
1507
- const endHandler = () => {
1508
- done = true;
1509
- if (resolveNext) {
1510
- const r = resolveNext;
1511
- resolveNext = null;
1512
- rejectNext = null;
1513
- r({ done: true, value: undefined });
1514
- }
1515
- };
1516
- const errorHandler = (err) => {
1517
- done = true;
1518
- streamError = err;
1519
- if (rejectNext) {
1520
- const r = rejectNext;
1521
- resolveNext = null;
1522
- rejectNext = null;
1523
- r(err);
1524
- }
1525
- };
1526
- stream.on("readable", readableHandler);
1527
- stream.on("end", endHandler);
1528
- stream.on("error", errorHandler);
1529
- const cleanup = () => {
1530
- stream.off("readable", readableHandler);
1531
- stream.off("end", endHandler);
1532
- stream.off("error", errorHandler);
1533
- };
1534
- const iter = {
1535
- next: async () => {
1536
- if (streamError) {
1537
- throw toError(streamError);
1538
- }
1539
- // Try synchronous read first
1540
- const chunk = stream.read();
1541
- if (chunk !== null) {
1542
- return { done: false, value: chunk };
1543
- }
1544
- if (done) {
1545
- cleanup();
1546
- return { done: true, value: undefined };
1547
- }
1548
- // Wait for 'readable' or 'end'
1549
- return new Promise((resolve, reject) => {
1550
- resolveNext = resolve;
1551
- rejectNext = reject;
1552
- // One more try in case data arrived between our read() and listener setup
1553
- tryRead();
1554
- });
1555
- },
1556
- return: async (value) => {
1557
- // Do NOT destroy the stream — just clean up listeners
1558
- cleanup();
1559
- return { done: true, value };
1560
- },
1561
- throw: async (err) => {
1562
- // On throw, we DO destroy (matching Node.js behavior)
1563
- cleanup();
1564
- stream.destroy(err instanceof Error ? err : undefined);
1565
- return { done: true, value: undefined };
1566
- },
1567
- [Symbol.asyncIterator]() {
1568
- return this;
1569
- }
1570
- };
1571
- return iter;
1572
- }
1573
- // =============================================================================
1574
- // Functional / Higher-order Methods (Node.js Readable compatibility)
1575
- // =============================================================================
1576
- /**
1577
- * Map each chunk through a function, returning a new Readable.
1578
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1579
- */
1580
- map(fn, options) {
1581
- const signal = options?.signal;
1582
- _validateAbortSignal(signal);
1583
- const concurrency = options?.concurrency ?? 1;
1584
- const ac = new AbortController();
1585
- const innerSignal = ac.signal;
1586
- const result = new Readable({ objectMode: true });
1587
- pumpAsyncIterableToReadable(result, (async function* (src) {
1588
- try {
1589
- yield* _mapWithConcurrency(src, chunk => fn(chunk, { signal: innerSignal }), concurrency, signal);
1590
- }
1591
- finally {
1592
- ac.abort();
1593
- }
1594
- })(this));
1595
- result.once("close", () => {
1596
- if (!this.destroyed) {
1597
- this.destroy();
1598
- }
1599
- });
1600
- if (signal) {
1601
- const onAbort = () => {
1602
- result.destroy(createAbortError());
1603
- };
1604
- signal.addEventListener("abort", onAbort, { once: true });
1605
- }
1606
- return result;
1607
- }
1608
- /**
1609
- * Filter chunks by a predicate, returning a new Readable.
1610
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1611
- */
1612
- filter(fn, options) {
1613
- const signal = options?.signal;
1614
- _validateAbortSignal(signal);
1615
- const concurrency = options?.concurrency ?? 1;
1616
- const ac = new AbortController();
1617
- const innerSignal = ac.signal;
1618
- const result = new Readable({ objectMode: true });
1619
- const SKIP = Symbol("skip");
1620
- pumpAsyncIterableToReadable(result, (async function* (src) {
1621
- try {
1622
- if (concurrency <= 1) {
1623
- for await (const chunk of src) {
1624
- _throwIfAborted(signal);
1625
- const keep = await fn(chunk, { signal: innerSignal });
1626
- if (keep) {
1627
- yield chunk;
1628
- }
1629
- }
1630
- }
1631
- else {
1632
- for await (const item of _mapWithConcurrency(src, async (chunk) => {
1633
- const keep = await fn(chunk, { signal: innerSignal });
1634
- return keep ? chunk : SKIP;
1635
- }, concurrency, signal)) {
1636
- if (item !== SKIP) {
1637
- yield item;
1638
- }
1639
- }
1640
- }
1641
- }
1642
- finally {
1643
- ac.abort();
1644
- }
1645
- })(this));
1646
- result.once("close", () => {
1647
- if (!this.destroyed) {
1648
- this.destroy();
1649
- }
1650
- });
1651
- if (signal) {
1652
- const onAbort = () => {
1653
- result.destroy(createAbortError());
1654
- };
1655
- signal.addEventListener("abort", onAbort, { once: true });
1656
- }
1657
- return result;
1658
- }
1659
- /**
1660
- * Iterate all chunks. Returns a promise that resolves when the stream ends.
1661
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1662
- */
1663
- async forEach(fn, options) {
1664
- const signal = options?.signal;
1665
- _validateAbortSignal(signal);
1666
- const concurrency = options?.concurrency ?? 1;
1667
- const ac = new AbortController();
1668
- const innerSignal = ac.signal;
1669
- try {
1670
- if (concurrency <= 1) {
1671
- for await (const chunk of this) {
1672
- _throwIfAborted(signal);
1673
- await fn(chunk, { signal: innerSignal });
1674
- }
1675
- }
1676
- else {
1677
- // Consume all mapped results (side effects only) to drive concurrency
1678
- for await (const _item of _mapWithConcurrency(this, async (chunk) => {
1679
- await fn(chunk, { signal: innerSignal });
1680
- }, concurrency, signal)) {
1681
- // discard results; forEach is side-effect only
1682
- }
1683
- }
1684
- }
1685
- finally {
1686
- ac.abort();
1687
- }
1688
- return undefined;
1689
- }
1690
- /**
1691
- * Collect all chunks into an array.
1692
- */
1693
- async toArray(options) {
1694
- const signal = options?.signal;
1695
- _validateAbortSignal(signal);
1696
- const result = [];
1697
- for await (const chunk of this) {
1698
- _throwIfAborted(signal);
1699
- result.push(chunk);
1700
- }
1701
- return result;
1702
- }
1703
- /**
1704
- * Returns true if any chunk passes the predicate. Short-circuits on first match.
1705
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1706
- */
1707
- async some(fn, options) {
1708
- const signal = options?.signal;
1709
- _validateAbortSignal(signal);
1710
- const concurrency = options?.concurrency ?? 1;
1711
- const ac = new AbortController();
1712
- const innerSignal = ac.signal;
1713
- try {
1714
- if (concurrency <= 1) {
1715
- for await (const chunk of this) {
1716
- _throwIfAborted(signal);
1717
- const result = await fn(chunk, { signal: innerSignal });
1718
- if (result) {
1719
- this.destroy();
1720
- return true;
1721
- }
1722
- }
1723
- }
1724
- else {
1725
- for await (const result of _mapWithConcurrency(this, chunk => fn(chunk, { signal: innerSignal }), concurrency, signal)) {
1726
- if (result) {
1727
- this.destroy();
1728
- return true;
1729
- }
1730
- }
1731
- }
1732
- return false;
1733
- }
1734
- finally {
1735
- ac.abort();
1736
- }
1737
- }
1738
- /**
1739
- * Find the first chunk matching the predicate. Short-circuits on match.
1740
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1741
- */
1742
- async find(fn, options) {
1743
- const signal = options?.signal;
1744
- _validateAbortSignal(signal);
1745
- const concurrency = options?.concurrency ?? 1;
1746
- const ac = new AbortController();
1747
- const innerSignal = ac.signal;
1748
- try {
1749
- if (concurrency <= 1) {
1750
- for await (const chunk of this) {
1751
- _throwIfAborted(signal);
1752
- const result = await fn(chunk, { signal: innerSignal });
1753
- if (result) {
1754
- this.destroy();
1755
- return chunk;
1756
- }
1757
- }
1758
- }
1759
- else {
1760
- for await (const item of _mapWithConcurrency(this, async (chunk) => ({
1761
- chunk,
1762
- match: await fn(chunk, { signal: innerSignal })
1763
- }), concurrency, signal)) {
1764
- if (item.match) {
1765
- this.destroy();
1766
- return item.chunk;
1767
- }
1768
- }
1769
- }
1770
- return undefined;
1771
- }
1772
- finally {
1773
- ac.abort();
1774
- }
1775
- }
1776
- /**
1777
- * Returns true if all chunks pass the predicate. Short-circuits on first failure.
1778
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1779
- */
1780
- async every(fn, options) {
1781
- const signal = options?.signal;
1782
- _validateAbortSignal(signal);
1783
- const concurrency = options?.concurrency ?? 1;
1784
- const ac = new AbortController();
1785
- const innerSignal = ac.signal;
1786
- try {
1787
- if (concurrency <= 1) {
1788
- for await (const chunk of this) {
1789
- _throwIfAborted(signal);
1790
- const result = await fn(chunk, { signal: innerSignal });
1791
- if (!result) {
1792
- this.destroy();
1793
- return false;
1794
- }
1795
- }
1796
- }
1797
- else {
1798
- for await (const result of _mapWithConcurrency(this, chunk => fn(chunk, { signal: innerSignal }), concurrency, signal)) {
1799
- if (!result) {
1800
- this.destroy();
1801
- return false;
1802
- }
1803
- }
1804
- }
1805
- return true;
1806
- }
1807
- finally {
1808
- ac.abort();
1809
- }
1810
- }
1811
- /**
1812
- * Map each chunk to multiple outputs (flattening), returning a new Readable.
1813
- * Supports concurrent execution via `options.concurrency` (matching Node.js).
1814
- */
1815
- flatMap(fn, options) {
1816
- const signal = options?.signal;
1817
- _validateAbortSignal(signal);
1818
- const concurrency = options?.concurrency ?? 1;
1819
- const ac = new AbortController();
1820
- const innerSignal = ac.signal;
1821
- const result = new Readable({ objectMode: true });
1822
- pumpAsyncIterableToReadable(result, (async function* (src) {
1823
- try {
1824
- if (concurrency <= 1) {
1825
- for await (const chunk of src) {
1826
- _throwIfAborted(signal);
1827
- const mapped = await fn(chunk, { signal: innerSignal });
1828
- if (mapped &&
1829
- typeof mapped[Symbol.asyncIterator] ===
1830
- "function") {
1831
- for await (const item of mapped) {
1832
- yield item;
1833
- }
1834
- }
1835
- else if (mapped &&
1836
- typeof mapped[Symbol.iterator] === "function") {
1837
- for (const item of mapped) {
1838
- yield item;
1839
- }
1840
- }
1841
- }
1842
- }
1843
- else {
1844
- // With concurrency, map chunks to their flattened arrays in order,
1845
- // then yield each item from each array in sequence.
1846
- for await (const items of _mapWithConcurrency(src, async (chunk) => {
1847
- const mapped = await fn(chunk, { signal: innerSignal });
1848
- const collected = [];
1849
- if (mapped &&
1850
- typeof mapped[Symbol.asyncIterator] ===
1851
- "function") {
1852
- for await (const item of mapped) {
1853
- collected.push(item);
1854
- }
1855
- }
1856
- else if (mapped &&
1857
- typeof mapped[Symbol.iterator] === "function") {
1858
- for (const item of mapped) {
1859
- collected.push(item);
1860
- }
1861
- }
1862
- return collected;
1863
- }, concurrency, signal)) {
1864
- for (const item of items) {
1865
- yield item;
1866
- }
1867
- }
1868
- }
1869
- }
1870
- finally {
1871
- ac.abort();
1872
- }
1873
- })(this));
1874
- result.once("close", () => {
1875
- if (!this.destroyed) {
1876
- this.destroy();
1877
- }
1878
- });
1879
- if (signal) {
1880
- const onAbort = () => {
1881
- result.destroy(createAbortError());
1882
- };
1883
- signal.addEventListener("abort", onAbort, { once: true });
1884
- }
1885
- return result;
1886
- }
1887
- /**
1888
- * Skip the first `limit` chunks, returning a new Readable.
1889
- */
1890
- drop(limit, options) {
1891
- const signal = options?.signal;
1892
- _validateAbortSignal(signal);
1893
- _validateNonNegativeInteger(limit, "limit");
1894
- const result = new Readable({ objectMode: true });
1895
- pumpAsyncIterableToReadable(result, (async function* (src) {
1896
- let count = 0;
1897
- for await (const chunk of src) {
1898
- _throwIfAborted(signal);
1899
- if (count >= limit) {
1900
- yield chunk;
1901
- }
1902
- count++;
1903
- }
1904
- })(this));
1905
- // Propagate destruction from result back to source (matches Node.js)
1906
- result.once("close", () => {
1907
- if (!this.destroyed) {
1908
- this.destroy();
1909
- }
1910
- });
1911
- if (signal) {
1912
- const onAbort = () => {
1913
- result.destroy(createAbortError());
1914
- };
1915
- signal.addEventListener("abort", onAbort, { once: true });
1916
- }
1917
- return result;
1918
- }
1919
- /**
1920
- * Take only the first `limit` chunks, returning a new Readable.
1921
- */
1922
- take(limit, options) {
1923
- const signal = options?.signal;
1924
- _validateAbortSignal(signal);
1925
- _validateNonNegativeInteger(limit, "limit");
1926
- const result = new Readable({ objectMode: true });
1927
- pumpAsyncIterableToReadable(result, (async function* (src) {
1928
- let count = 0;
1929
- for await (const chunk of src) {
1930
- _throwIfAborted(signal);
1931
- if (count >= limit) {
1932
- break;
1933
- }
1934
- yield chunk;
1935
- count++;
1936
- }
1937
- })(this));
1938
- // Propagate destruction from result back to source (matches Node.js)
1939
- result.once("close", () => {
1940
- if (!this.destroyed) {
1941
- this.destroy();
1942
- }
1943
- });
1944
- if (signal) {
1945
- const onAbort = () => {
1946
- result.destroy(createAbortError());
1947
- };
1948
- signal.addEventListener("abort", onAbort, { once: true });
1949
- }
1950
- return result;
1951
- }
1952
- /**
1953
- * Reduce the stream to a single value.
1954
- */
1955
- async reduce(fn, initial, options) {
1956
- const signal = options?.signal;
1957
- _validateAbortSignal(signal);
1958
- const ac = new AbortController();
1959
- const innerSignal = ac.signal;
1960
- let accumulator;
1961
- const hasInitial = arguments.length >= 2;
1962
- let first = true;
1963
- try {
1964
- for await (const chunk of this) {
1965
- _throwIfAborted(signal);
1966
- if (first && !hasInitial) {
1967
- // Without an initial value, the first element seeds the accumulator;
1968
- // callers using this form expect U to coincide with the element type.
1969
- accumulator = chunk;
1970
- first = false;
1971
- continue;
1972
- }
1973
- if (first) {
1974
- accumulator = initial;
1975
- first = false;
1976
- }
1977
- accumulator = await fn(accumulator, chunk, { signal: innerSignal });
1978
- }
1979
- if (first && !hasInitial) {
1980
- throw new TypeError("Reduce of an empty stream requires an initial value");
1981
- }
1982
- if (first) {
1983
- return initial;
1984
- }
1985
- return accumulator;
1986
- }
1987
- finally {
1988
- ac.abort();
1989
- }
1990
- }
1991
- /**
1992
- * Returns a new readable containing [index, chunk] pairs.
1993
- * Matches Node.js Readable.prototype.asIndexedPairs() (experimental).
1994
- */
1995
- asIndexedPairs(options) {
1996
- let index = 0;
1997
- return this.map(data => [index++, data], options);
1998
- }
1999
- /**
2000
- * Compose this readable with a stream/transform, returning a Duplex.
2001
- * Matches Node.js behavior where compose() returns a Duplex.
2002
- */
2003
- compose(stream, _options) {
2004
- // If it's an async generator function, pipe through it
2005
- if (typeof stream === "function") {
2006
- const result = new Readable({ objectMode: true });
2007
- pumpAsyncIterableToReadable(result, stream(this));
2008
- // Propagate destruction from result back to source (matching map/filter/flatMap)
2009
- result.once("close", () => {
2010
- if (!this.destroyed) {
2011
- this.destroy();
2012
- }
2013
- });
2014
- const duplexFrom = getDuplexFrom();
2015
- if (!duplexFrom) {
2016
- // Should not happen at runtime: index.browser.ts registers Duplex.from
2017
- // at module load before any compose() call can run.
2018
- throw new Error("Duplex.from is not registered; cannot compose() in this environment");
2019
- }
2020
- // Boundary: Duplex.from erases element types; the composed readable emits
2021
- // U and consumes T.
2022
- return duplexFrom(result);
2023
- }
2024
- // If it's a transform/duplex with pipe support, pipe this into it and
2025
- // return its readable side wrapped as a Duplex.
2026
- const target = stream;
2027
- this.pipe(target);
2028
- // If the target is already a Duplex-like, return it directly.
2029
- if (target._readable && target._writable) {
2030
- return target;
2031
- }
2032
- // If the target has a _readable (Transform/Duplex), wrap it.
2033
- if (target._readable) {
2034
- const duplexFrom2 = getDuplexFrom();
2035
- if (!duplexFrom2) {
2036
- throw new Error("Duplex.from is not registered; cannot compose() in this environment");
2037
- }
2038
- return duplexFrom2(target._readable);
2039
- }
2040
- const duplexFrom3 = getDuplexFrom();
2041
- if (!duplexFrom3) {
2042
- throw new Error("Duplex.from is not registered; cannot compose() in this environment");
2043
- }
2044
- return duplexFrom3(target);
2045
- }
2046
- }
2047
- // Node.js: `Readable.prototype.addListener === Readable.prototype.on` (same function).
2048
- // Readable overrides `on` from EventEmitter, so we must re-alias `addListener`.
2049
- Readable.prototype.addListener = Readable.prototype.on;
2050
- // Node.js: `Readable.prototype._read` throws ERR_METHOD_NOT_IMPLEMENTED when called without
2051
- // a subclass override (Node.js 18+). We replicate that behavior here so that subclasses that
2052
- // forget to implement `_read()` see the same error event as on Node.js.
2053
- // Factory-created readables (e.g., from async iterables) rely on the internal `_read` check
2054
- // being falsy, so the instance field `_read?: ...` shadows this prototype method when set.
2055
- Readable.prototype._read = function _read(_size) {
2056
- const err = new Error("The _read() method is not implemented");
2057
- err.code = "ERR_METHOD_NOT_IMPLEMENTED";
2058
- throw err;
2059
- };
2060
- // =============================================================================
2061
- // Internal helpers – Functional method utilities
2062
- // =============================================================================
2063
- function _validateAbortSignal(signal) {
2064
- if (signal !== undefined && !(signal instanceof AbortSignal)) {
2065
- throw new TypeError("options.signal must be an AbortSignal");
2066
- }
2067
- }
2068
- function _throwIfAborted(signal) {
2069
- if (signal?.aborted) {
2070
- throw createAbortError(signal.reason);
2071
- }
2072
- }
2073
- function _validateNonNegativeInteger(value, name) {
2074
- if (typeof value !== "number" ||
2075
- Number.isNaN(value) ||
2076
- value < 0 ||
2077
- Math.floor(value) !== value) {
2078
- throw new RangeError(`The value of "${name}" must be a non-negative integer. Received ${value}`);
2079
- }
2080
- }
2081
- // =============================================================================
2082
- // Internal helpers – Concurrency support for higher-order methods
2083
- // =============================================================================
2084
- /**
2085
- * Run an async mapper over an async iterable with bounded concurrency,
2086
- * yielding results in input order (matching Node.js Readable.prototype.map).
2087
- *
2088
- * When concurrency === 1, falls back to a simple serial loop for efficiency.
2089
- */
2090
- const _EOF = Symbol("EOF");
2091
- async function* _mapWithConcurrency(source, fn, concurrency, signal) {
2092
- if (concurrency <= 1) {
2093
- for await (const chunk of source) {
2094
- _throwIfAborted(signal);
2095
- yield await fn(chunk);
2096
- }
2097
- return;
2098
- }
2099
- // Ordered concurrent execution using a sliding window of promises.
2100
- // Each slot holds a promise that resolves to the mapped value or _EOF sentinel.
2101
- const queue = [];
2102
- let error = null;
2103
- let sourceDone = false;
2104
- const iterator = source[Symbol.asyncIterator]();
2105
- // Pull one chunk from the source and start processing it.
2106
- async function pullOne() {
2107
- const next = await iterator.next();
2108
- if (next.done) {
2109
- sourceDone = true;
2110
- return _EOF;
2111
- }
2112
- _throwIfAborted(signal);
2113
- return fn(next.value);
2114
- }
2115
- function enqueue() {
2116
- queue.push(pullOne().catch(err => {
2117
- error = err;
2118
- throw err;
2119
- }));
2120
- }
2121
- try {
2122
- // Fill the initial window
2123
- while (queue.length < concurrency && !sourceDone && !error) {
2124
- _throwIfAborted(signal);
2125
- enqueue();
2126
- }
2127
- while (queue.length > 0) {
2128
- _throwIfAborted(signal);
2129
- const result = await queue.shift();
2130
- if (error) {
2131
- throw toError(error);
2132
- }
2133
- if (result === _EOF) {
2134
- // Source exhausted — drain any remaining real results
2135
- continue;
2136
- }
2137
- // Yield the result in order
2138
- yield result;
2139
- // Refill the window with one more
2140
- if (!sourceDone && !error) {
2141
- enqueue();
2142
- }
2143
- }
2144
- }
2145
- finally {
2146
- // Clean up: return the iterator if not done
2147
- if (!sourceDone) {
2148
- await iterator.return?.();
2149
- }
2150
- }
2151
- }
2152
- // =============================================================================
2153
- // Internal helpers
2154
- // =============================================================================
2155
- export function toAsyncIterable(iterable) {
2156
- if (iterable && typeof iterable[Symbol.asyncIterator] === "function") {
2157
- return iterable;
2158
- }
2159
- return (async function* () {
2160
- for (const item of iterable) {
2161
- yield item;
2162
- }
2163
- })();
2164
- }
2165
- export function pumpAsyncIterableToReadable(readable, iterable) {
2166
- const iterator = iterable[Symbol.asyncIterator]();
2167
- let reading = false;
2168
- let iteratorDone = false;
2169
- // Pull-based: advance the iterator one chunk per _read() call,
2170
- // matching Node.js Readable.from() behavior. These are private Readable
2171
- // internals wired here for factory-created readables.
2172
- const internals = readable;
2173
- internals._hasReadImpl = true;
2174
- internals._read = function () {
2175
- if (reading || iteratorDone) {
2176
- return;
2177
- }
2178
- reading = true;
2179
- (async () => {
2180
- try {
2181
- if (readable.destroyed) {
2182
- if (!iteratorDone) {
2183
- iteratorDone = true;
2184
- const p = iterator.return?.();
2185
- if (p && typeof p.then === "function") {
2186
- p.catch(() => { });
2187
- }
2188
- }
2189
- return;
2190
- }
2191
- const { value, done } = await iterator.next();
2192
- if (done) {
2193
- iteratorDone = true;
2194
- readable.push(null);
2195
- return;
2196
- }
2197
- readable.push(value);
2198
- }
2199
- catch (err) {
2200
- readable.destroy(err);
2201
- }
2202
- finally {
2203
- reading = false;
2204
- }
2205
- })();
2206
- };
2207
- internals._pushMode = true;
2208
- }