@@ -21,7 +21,6 @@ const { |
21 | 21 | SymbolAsyncDispose, |
22 | 22 | SymbolAsyncIterator, |
23 | 23 | SymbolDispose, |
24 | | - TypedArrayPrototypeGetByteLength, |
25 | 24 | } = primordials; |
26 | 25 |
|
27 | 26 | const { lazyDOMException } = require('internal/util'); |
@@ -59,6 +58,7 @@ const { |
59 | 58 | kMultiConsumerDefaultBudget, |
60 | 59 | kResolvedPromise, |
61 | 60 | convertChunks, |
| 61 | + createBatchEntry, |
62 | 62 | getWriterSignal, |
63 | 63 | getMinCursor, |
64 | 64 | hasProtocol, |
@@ -67,6 +67,7 @@ const { |
67 | 67 | wrapError, |
68 | 68 | toUint8Array, |
69 | 69 | validateBackpressure, |
| 70 | + validateBatchEntry, |
70 | 71 | } = require('internal/streams/iter/utils'); |
71 | 72 |
|
72 | 73 | const { |
@@ -216,7 +217,8 @@ class BroadcastImpl { |
216 | 217 |
|
217 | 218 | const bufferIndex = state.cursor - self.#bufferStart; |
218 | 219 | if (bufferIndex < self.#buffer.length) { |
219 | | - const chunk = self.#buffer.get(bufferIndex); |
| 220 | + const chunk = self.#readEntry(self.#buffer.get(bufferIndex)); |
| 221 | + if (chunk === null) return PromiseReject(self.#error); |
220 | 222 | const cursor = state.cursor; |
221 | 223 | state.cursor++; |
222 | 224 | if (cursor === self.#cachedMinCursor && |
@@ -306,10 +308,10 @@ class BroadcastImpl { |
306 | 308 |
|
307 | 309 | // Methods accessed by BroadcastWriter via symbol keys |
308 | 310 |
|
309 | | - [kWrite](chunk) { |
| 311 | + [kWrite](entry) { |
310 | 312 | if (this.#ended || this.#cancelled) return false; |
311 | 313 |
|
312 | | - const batchSize = this.#batchByteSize(chunk); |
| 314 | + const batchSize = entry.byteLength; |
313 | 315 |
|
314 | 316 | // Skip empty chunks -- zero-byte writes would accumulate infinitely |
315 | 317 | // without ever triggering backpressure under a byte-budget model. |
@@ -324,7 +326,7 @@ class BroadcastImpl { |
324 | 326 | while (this.#bufferedBytes >= this.#options.budget && |
325 | 327 | this.#buffer.length > 0) { |
326 | 328 | const evicted = this.#buffer.shift(); |
327 | | - this.#bufferedBytes -= this.#batchByteSize(evicted); |
| 329 | + this.#bufferedBytes -= evicted.byteLength; |
328 | 330 | this.#bufferStart++; |
329 | 331 | } |
330 | 332 | for (const consumer of this.#consumers) { |
@@ -340,7 +342,7 @@ class BroadcastImpl { |
340 | 342 | } |
341 | 343 | } |
342 | 344 |
|
343 | | - this.#buffer.push(chunk); |
| 345 | + this.#buffer.push(entry); |
344 | 346 | this.#bufferedBytes += batchSize; |
345 | 347 | this.#notifyConsumers(); |
346 | 348 | return true; |
@@ -354,7 +356,8 @@ class BroadcastImpl { |
354 | 356 | while (consumer.resolve) { |
355 | 357 | const bufferIndex = consumer.cursor - this.#bufferStart; |
356 | 358 | if (bufferIndex < this.#buffer.length) { |
357 | | - const chunk = this.#buffer.get(bufferIndex); |
| 359 | + const chunk = this.#readEntry(this.#buffer.get(bufferIndex)); |
| 360 | + if (chunk === null) return; |
358 | 361 | const cursor = consumer.cursor; |
359 | 362 | consumer.cursor++; |
360 | 363 | if (cursor === this.#cachedMinCursor && |
@@ -439,7 +442,7 @@ class BroadcastImpl { |
439 | 442 | if (trimCount > 0) { |
440 | 443 | for (let i = 0; i < trimCount; i++) { |
441 | 444 | const evicted = this.#buffer.get(i); |
442 | | - this.#bufferedBytes -= this.#batchByteSize(evicted); |
| 445 | + this.#bufferedBytes -= evicted.byteLength; |
443 | 446 | } |
444 | 447 | this.#buffer.trimFront(trimCount); |
445 | 448 | this.#bufferStart = this.#cachedMinCursor; |
@@ -451,12 +454,16 @@ class BroadcastImpl { |
451 | 454 | } |
452 | 455 | } |
453 | 456 |
|
454 | | - #batchByteSize(batch) { |
455 | | - let size = 0; |
456 | | - for (let i = 0; i < batch.length; i++) { |
457 | | - size += TypedArrayPrototypeGetByteLength(batch[i]); |
| 457 | + #readEntry(entry) { |
| 458 | + try { |
| 459 | + return validateBatchEntry(entry); |
| 460 | + } catch (error) { |
| 461 | + this.#writer.fail(error); |
| 462 | + if (this.#error === undefined) this[kAbort](error); |
| 463 | + this.#buffer.clear(); |
| 464 | + this.#bufferedBytes = 0; |
| 465 | + return null; |
458 | 466 | } |
459 | | - return size; |
460 | 467 | } |
461 | 468 |
|
462 | 469 | #notifyConsumers() { |
@@ -469,7 +476,8 @@ class BroadcastImpl { |
469 | 476 | if (consumer.resolve) { |
470 | 477 | const bufferIndex = consumer.cursor - this.#bufferStart; |
471 | 478 | if (bufferIndex < this.#buffer.length) { |
472 | | - const chunk = this.#buffer.get(bufferIndex); |
| 479 | + const chunk = this.#readEntry(this.#buffer.get(bufferIndex)); |
| 480 | + if (chunk === null) return; |
473 | 481 | const cursor = consumer.cursor; |
474 | 482 | consumer.cursor++; |
475 | 483 | if (cursor === this.#cachedMinCursor && |
@@ -592,8 +600,9 @@ class BroadcastWriter { |
592 | 600 | // Fast path: no signal, writer open, buffer has space |
593 | 601 | if (this.#canUseWriteFastPath(signal)) { |
594 | 602 | const converted = toUint8Array(chunk); |
595 | | - this.#broadcast[kWrite]([converted]); |
596 | | - this.#totalBytes += TypedArrayPrototypeGetByteLength(converted); |
| 603 | + const batch = createBatchEntry([converted]); |
| 604 | + this.#broadcast[kWrite](batch); |
| 605 | + this.#totalBytes += batch.byteLength; |
597 | 606 | return kResolvedPromise; |
598 | 607 | } |
599 | 608 | return this.#writevSlow([chunk], signal); |
@@ -605,10 +614,9 @@ class BroadcastWriter { |
605 | 614 | // Fast path: no signal, writer open, buffer has space |
606 | 615 | if (this.#canUseWriteFastPath(signal)) { |
607 | 616 | const converted = convertChunks(chunks); |
608 | | - this.#broadcast[kWrite](converted); |
609 | | - for (let i = 0; i < converted.length; i++) { |
610 | | - this.#totalBytes += TypedArrayPrototypeGetByteLength(converted[i]); |
611 | | - } |
| 617 | + const batch = createBatchEntry(converted); |
| 618 | + this.#broadcast[kWrite](batch); |
| 619 | + this.#totalBytes += batch.byteLength; |
612 | 620 | return kResolvedPromise; |
613 | 621 | } |
614 | 622 | return this.#writevSlow(chunks, signal); |
@@ -624,12 +632,10 @@ class BroadcastWriter { |
624 | 632 |
|
625 | 633 | signal?.throwIfAborted(); |
626 | 634 |
|
627 | | - const converted = convertChunks(chunks); |
| 635 | + const batch = createBatchEntry(convertChunks(chunks)); |
628 | 636 |
|
629 | | - if (this.#broadcast[kWrite](converted)) { |
630 | | - for (let i = 0; i < converted.length; i++) { |
631 | | - this.#totalBytes += TypedArrayPrototypeGetByteLength(converted[i]); |
632 | | - } |
| 637 | + if (this.#broadcast[kWrite](batch)) { |
| 638 | + this.#totalBytes += batch.byteLength; |
633 | 639 | return; |
634 | 640 | } |
635 | 641 |
|
@@ -641,20 +647,21 @@ class BroadcastWriter { |
641 | 647 | 'Backpressure violation: too many pending writes. ' + |
642 | 648 | 'Await each write() call to respect backpressure.'); |
643 | 649 | } |
644 | | - return this.#createPendingWrite(converted, signal); |
| 650 | + return this.#createPendingWrite(batch, signal); |
645 | 651 | } |
646 | 652 |
|
647 | 653 | // 'unbounded' policy |
648 | | - return this.#createPendingWrite(converted, signal); |
| 654 | + return this.#createPendingWrite(batch, signal); |
649 | 655 | } |
650 | 656 |
|
651 | 657 | writeSync(chunk) { |
652 | 658 | if (this.#state !== 'open') return false; |
653 | 659 | if (!this.#broadcast[kCanWrite]()) return false; |
654 | 660 | const converted = |
655 | 661 | toUint8Array(chunk); |
656 | | - if (this.#broadcast[kWrite]([converted])) { |
657 | | - this.#totalBytes += TypedArrayPrototypeGetByteLength(converted); |
| 662 | + const batch = createBatchEntry([converted]); |
| 663 | + if (this.#broadcast[kWrite](batch)) { |
| 664 | + this.#totalBytes += batch.byteLength; |
658 | 665 | return true; |
659 | 666 | } |
660 | 667 | return false; |
@@ -665,10 +672,9 @@ class BroadcastWriter { |
665 | 672 | if (this.#state !== 'open') return false; |
666 | 673 | if (!this.#broadcast[kCanWrite]()) return false; |
667 | 674 | const converted = convertChunks(chunks); |
668 | | - if (this.#broadcast[kWrite](converted)) { |
669 | | - for (let i = 0; i < converted.length; i++) { |
670 | | - this.#totalBytes += TypedArrayPrototypeGetByteLength(converted[i]); |
671 | | - } |
| 675 | + const batch = createBatchEntry(converted); |
| 676 | + if (this.#broadcast[kWrite](batch)) { |
| 677 | + this.#totalBytes += batch.byteLength; |
672 | 678 | return true; |
673 | 679 | } |
674 | 680 | return false; |
@@ -757,9 +763,9 @@ class BroadcastWriter { |
757 | 763 | * promise rejects. Signal listeners are cleaned up on normal resolution. |
758 | 764 | * @returns {Promise<void>} |
759 | 765 | */ |
760 | | - #createPendingWrite(chunk, signal) { |
| 766 | + #createPendingWrite(batch, signal) { |
761 | 767 | const { promise, resolve, reject } = PromiseWithResolvers(); |
762 | | - const entry = { __proto__: null, chunk, resolve, reject }; |
| 768 | + const entry = { __proto__: null, batch, resolve, reject }; |
763 | 769 | this.#pendingWrites.push(entry); |
764 | 770 | if (signal) { |
765 | 771 | wireBroadcastWriteSignal(entry, signal, resolve, reject, this); |
@@ -770,14 +776,17 @@ class BroadcastWriter { |
770 | 776 | #resolvePendingWrites() { |
771 | 777 | while (this.#pendingWrites.length > 0 && this.#broadcast[kCanWrite]()) { |
772 | 778 | const pending = this.#pendingWrites.shift(); |
773 | | - if (this.#broadcast[kWrite](pending.chunk)) { |
774 | | - for (let i = 0; i < pending.chunk.length; i++) { |
775 | | - this.#totalBytes += TypedArrayPrototypeGetByteLength(pending.chunk[i]); |
| 779 | + try { |
| 780 | + validateBatchEntry(pending.batch); |
| 781 | + if (this.#broadcast[kWrite](pending.batch)) { |
| 782 | + this.#totalBytes += pending.batch.byteLength; |
| 783 | + pending.resolve(); |
| 784 | + } else { |
| 785 | + this.#pendingWrites.unshift(pending); |
| 786 | + break; |
776 | 787 | } |
777 | | - pending.resolve(); |
778 | | - } else { |
779 | | - this.#pendingWrites.unshift(pending); |
780 | | - break; |
| 788 | + } catch (error) { |
| 789 | + pending.reject(error); |
781 | 790 | } |
782 | 791 | } |
783 | 792 | this.#finishEndIfReady(); |
@@ -811,18 +820,18 @@ function wireBroadcastWriteSignal(entry, signal, resolve, reject, self) { |
811 | 820 | const pendingWrites = getBroadcastPendingWrites(self); |
812 | 821 | const idx = pendingWrites.indexOf(entry); |
813 | 822 | if (idx !== -1) pendingWrites.removeAt(idx); |
814 | | - entry.chunk = null; |
| 823 | + entry.batch = null; |
815 | 824 | reject(signal.reason ?? lazyDOMException('Aborted', 'AbortError')); |
816 | 825 | if (idx !== -1) self[kPendingWriteRemoved](); |
817 | 826 | }; |
818 | 827 | entry.resolve = function() { |
819 | 828 | signal.removeEventListener('abort', onAbort); |
820 | | - entry.chunk = null; |
| 829 | + entry.batch = null; |
821 | 830 | resolve(); |
822 | 831 | }; |
823 | 832 | entry.reject = function(reason) { |
824 | 833 | signal.removeEventListener('abort', onAbort); |
825 | | - entry.chunk = null; |
| 834 | + entry.batch = null; |
826 | 835 | reject(reason); |
827 | 836 | }; |
828 | 837 | signal.addEventListener('abort', onAbort, { __proto__: null, once: true }); |
|