@@ -12,7 +12,6 @@ const {
1212 ArrayPrototypePush,
1313 ArrayPrototypeSlice,
1414 MathMax,
15- Promise,
1615 PromisePrototypeThen,
1716 PromiseResolve,
1817 PromiseWithResolvers,
@@ -29,6 +28,7 @@ const { isError, lazyDOMException } = require('internal/util');
2928const {
3029 codes : {
3130 ERR_INVALID_ARG_TYPE ,
31+ ERR_INVALID_RETURN_VALUE ,
3232 ERR_INVALID_STATE ,
3333 } ,
3434} = require ( 'internal/errors' ) ;
@@ -81,16 +81,17 @@ function isPushStreamOptions(value) {
8181
8282function parsePushArgs ( args ) {
8383 if ( args . length === 0 ) {
84- return { transforms : [ ] , options : undefined } ;
84+ return { __proto__ : null , transforms : [ ] , options : undefined } ;
8585 }
8686 const last = args [ args . length - 1 ] ;
8787 if ( isPushStreamOptions ( last ) ) {
8888 return {
89+ __proto__ : null ,
8990 transforms : ArrayPrototypeSlice ( args , 0 , - 1 ) ,
9091 options : last ,
9192 } ;
9293 }
93- return { transforms : args , options : undefined } ;
94+ return { __proto__ : null , transforms : args , options : undefined } ;
9495}
9596
9697// =============================================================================
@@ -140,7 +141,7 @@ class BroadcastImpl {
140141 if ( transforms . length > 0 ) {
141142 if ( options ?. signal ) {
142143 return pullWithTransforms (
143- rawConsumer , ...transforms , { signal : options . signal } ) ;
144+ rawConsumer , ...transforms , { __proto__ : null , signal : options . signal } ) ;
144145 }
145146 return pullWithTransforms ( rawConsumer , ...transforms ) ;
146147 }
@@ -149,6 +150,7 @@ class BroadcastImpl {
149150
150151 #createRawConsumer( ) {
151152 const state = {
153+ __proto__ : null ,
152154 cursor : this . #bufferStart + this . #buffer. length ,
153155 resolve : null ,
154156 reject : null ,
@@ -399,9 +401,9 @@ class BroadcastWriter {
399401 const desired = this . desiredSize ;
400402 if ( desired === null ) return null ;
401403 if ( desired > 0 ) return PromiseResolve ( true ) ;
402- return new Promise ( ( resolve , reject ) => {
403- ArrayPrototypePush ( this . #pendingDrains, { resolve, reject } ) ;
404- } ) ;
404+ const { promise , resolve, reject } = PromiseWithResolvers ( ) ;
405+ ArrayPrototypePush ( this . #pendingDrains, { __proto__ : null , resolve, reject } ) ;
406+ return promise ;
405407 }
406408
407409 get desiredSize ( ) {
@@ -442,10 +444,8 @@ class BroadcastWriter {
442444 async #writevSlow( chunks , options ) {
443445 const signal = options ?. signal ;
444446
445- // Check for pre-aborted signal
446- if ( signal ?. aborted ) {
447- throw signal . reason ?? lazyDOMException ( 'Aborted' , 'AbortError' ) ;
448- }
447+ // Check for pre-aborted
448+ signal ?. throwIfAborted ( ) ;
449449
450450 if ( this . #closed || this . #aborted) {
451451 throw new ERR_INVALID_STATE ( 'Writer is closed' ) ;
@@ -558,35 +558,37 @@ class BroadcastWriter {
558558 * Create a pending write promise, optionally racing against a signal.
559559 * If the signal fires, the entry is removed from pendingWrites and the
560560 * promise rejects. Signal listeners are cleaned up on normal resolution.
561+ * @returns {Promise<void> }
561562 */
562563 #createPendingWrite( chunk , signal ) {
563- return new Promise ( ( resolve , reject ) => {
564- const entry = { chunk, resolve, reject } ;
565- this . #pendingWrites. push ( entry ) ;
566-
567- if ( ! signal ) return ;
568-
569- const onAbort = ( ) => {
570- // Remove from queue so it doesn't occupy a slot
571- const idx = this . #pendingWrites. indexOf ( entry ) ;
572- if ( idx !== - 1 ) this . #pendingWrites. removeAt ( idx ) ;
573- reject ( signal . reason ?? lazyDOMException ( 'Aborted' , 'AbortError' ) ) ;
574- } ;
575-
576- // Wrap resolve/reject to clean up signal listener
577- const origResolve = entry . resolve ;
578- const origReject = entry . reject ;
579- entry . resolve = function ( ) {
580- signal . removeEventListener ( 'abort' , onAbort ) ;
581- origResolve ( ) ;
582- } ;
583- entry . reject = function ( reason ) {
584- signal . removeEventListener ( 'abort' , onAbort ) ;
585- origReject ( reason ) ;
586- } ;
587-
588- signal . addEventListener ( 'abort' , onAbort , { once : true } ) ;
589- } ) ;
564+ const { promise, resolve, reject } = PromiseWithResolvers ( ) ;
565+ const entry = { __proto__ : null , chunk, resolve, reject } ;
566+ this . #pendingWrites. push ( entry ) ;
567+
568+ if ( ! signal ) return promise ;
569+
570+ const onAbort = ( ) => {
571+ // Remove from queue so it doesn't occupy a slot
572+ const idx = this . #pendingWrites. indexOf ( entry ) ;
573+ if ( idx !== - 1 ) this . #pendingWrites. removeAt ( idx ) ;
574+ reject ( signal . reason ?? lazyDOMException ( 'Aborted' , 'AbortError' ) ) ;
575+ } ;
576+
577+ // Wrap resolve/reject to clean up signal listener
578+ const origResolve = entry . resolve ;
579+ const origReject = entry . reject ;
580+ entry . resolve = function ( ) {
581+ signal . removeEventListener ( 'abort' , onAbort ) ;
582+ origResolve ( ) ;
583+ } ;
584+ entry . reject = function ( reason ) {
585+ signal . removeEventListener ( 'abort' , onAbort ) ;
586+ origReject ( reason ) ;
587+ } ;
588+
589+ signal . addEventListener ( 'abort' , onAbort , { __proto__ : null , once : true } ) ;
590+
591+ return promise ;
590592 }
591593
592594 #resolvePendingWrites( ) {
@@ -639,6 +641,7 @@ class BroadcastWriter {
639641 */
640642function broadcast ( options ) {
641643 const opts = {
644+ __proto__ : null ,
642645 highWaterMark : MathMax ( 1 , options ?. highWaterMark ?? 16 ) ,
643646 backpressure : options ?. backpressure ?? 'strict' ,
644647 signal : options ?. signal ,
@@ -654,11 +657,11 @@ function broadcast(options) {
654657 } else {
655658 opts . signal . addEventListener ( 'abort' , ( ) => {
656659 broadcastImpl . cancel ( ) ;
657- } , { once : true } ) ;
660+ } , { __proto__ : null , once : true } ) ;
658661 }
659662 }
660663
661- return { writer, broadcast : broadcastImpl } ;
664+ return { __proto__ : null , writer, broadcast : broadcastImpl } ;
662665}
663666
664667function isBroadcastable ( value ) {
@@ -674,7 +677,11 @@ const Broadcast = {
674677 from ( input , options ) {
675678 if ( isBroadcastable ( input ) ) {
676679 const bc = input [ broadcastProtocol ] ( options ) ;
677- return { writer : { } , broadcast : bc } ;
680+ if ( bc === null || typeof bc !== 'object' ) {
681+ throw new ERR_INVALID_RETURN_VALUE (
682+ 'an object' , '[Symbol.for(\'Stream.broadcastProtocol\')]' , bc ) ;
683+ }
684+ return { __proto__ : null , writer : { __proto__ : null } , broadcast : bc } ;
678685 }
679686
680687 const result = broadcast ( options ) ;
@@ -685,10 +692,7 @@ const Broadcast = {
685692 try {
686693 if ( isAsyncIterable ( input ) ) {
687694 for await ( const chunks of input ) {
688- if ( signal ?. aborted ) {
689- throw signal . reason ??
690- lazyDOMException ( 'Aborted' , 'AbortError' ) ;
691- }
695+ signal ?. throwIfAborted ( ) ;
692696 if ( ArrayIsArray ( chunks ) ) {
693697 if ( ! w . writevSync ( chunks ) ) {
694698 await w . writev ( chunks , signal ? { signal } : undefined ) ;
@@ -699,10 +703,7 @@ const Broadcast = {
699703 }
700704 } else if ( isSyncIterable ( input ) ) {
701705 for ( const chunks of input ) {
702- if ( signal ?. aborted ) {
703- throw signal . reason ??
704- lazyDOMException ( 'Aborted' , 'AbortError' ) ;
705- }
706+ signal ?. throwIfAborted ( ) ;
706707 if ( ArrayIsArray ( chunks ) ) {
707708 if ( ! w . writevSync ( chunks ) ) {
708709 await w . writev ( chunks , signal ? { signal } : undefined ) ;
0 commit comments