@@ -6,7 +6,7 @@ const { promiseResolvedWith, promiseRejectedWith, newPromise, resolvePromise, re
66 require ( '../helpers/webidl.js' ) ;
77const { CanTransferArrayBuffer, CopyDataBlockBytes, CreateArrayFromList, IsDetachedBuffer, TransferArrayBuffer } =
88 require ( './ecmascript.js' ) ;
9- const { CloneAsUint8Array, IsNonNegativeNumber } = require ( './miscellaneous.js' ) ;
9+ const { CloneAsUint8Array, IsNonNegativeNumber, StructuredTransferOrClone } = require ( './miscellaneous.js' ) ;
1010const { EnqueueValueWithSize, ResetQueue } = require ( './queue-with-sizes.js' ) ;
1111const { AcquireWritableStreamDefaultWriter, IsWritableStreamLocked, WritableStreamAbort,
1212 WritableStreamDefaultWriterCloseWithErrorPropagation, WritableStreamDefaultWriterRelease,
@@ -89,7 +89,7 @@ function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, hi
8989
9090 const controller = ReadableStreamDefaultController . new ( globalThis ) ;
9191 SetUpReadableStreamDefaultController (
92- stream , controller , startAlgorithm , pullAlgorithm , cancelAlgorithm , highWaterMark , sizeAlgorithm
92+ stream , controller , startAlgorithm , pullAlgorithm , cancelAlgorithm , highWaterMark , sizeAlgorithm , false
9393 ) ;
9494
9595 return stream ;
@@ -340,7 +340,7 @@ function ReadableStreamTee(stream, cloneForBranch2) {
340340 if ( ReadableByteStreamController . isImpl ( stream . _controller ) ) {
341341 return ReadableByteStreamTee ( stream ) ;
342342 }
343- return ReadableStreamDefaultTee ( stream , cloneForBranch2 ) ;
343+ return ReadableStreamDefaultTee ( stream , stream . _controller . isTransferring ? true : cloneForBranch2 ) ;
344344}
345345
346346function ReadableStreamDefaultTee ( stream , cloneForBranch2 ) {
@@ -392,10 +392,10 @@ function ReadableStreamDefaultTee(stream, cloneForBranch2) {
392392 // }
393393
394394 if ( canceled1 === false ) {
395- ReadableStreamDefaultControllerEnqueue ( branch1 . _controller , chunk1 ) ;
395+ ReadableStreamDefaultControllerEnqueue ( branch1 . _controller , chunk1 , undefined ) ;
396396 }
397397 if ( canceled2 === false ) {
398- ReadableStreamDefaultControllerEnqueue ( branch2 . _controller , chunk2 ) ;
398+ ReadableStreamDefaultControllerEnqueue ( branch2 . _controller , chunk2 , undefined ) ;
399399 }
400400
401401 reading = false ;
@@ -1074,14 +1074,22 @@ function ReadableStreamDefaultControllerClose(controller) {
10741074 }
10751075}
10761076
1077- function ReadableStreamDefaultControllerEnqueue ( controller , chunk ) {
1077+ function ReadableStreamDefaultControllerEnqueue ( controller , chunk , transfer ) {
10781078 if ( ReadableStreamDefaultControllerCanCloseOrEnqueue ( controller ) === false ) {
10791079 return ;
10801080 }
10811081
10821082 const stream = controller . _stream ;
10831083
10841084 if ( IsReadableStreamLocked ( stream ) === true && ReadableStreamGetNumReadRequests ( stream ) > 0 ) {
1085+ if ( controller . isTransferring ) {
1086+ try {
1087+ chunk = StructuredTransferOrClone ( chunk , transfer ) ;
1088+ } catch ( chunkCloneError ) {
1089+ ReadableStreamDefaultControllerError ( controller , chunkCloneError ) ;
1090+ throw chunkCloneError ;
1091+ }
1092+ }
10851093 ReadableStreamFulfillReadRequest ( stream , chunk , false ) ;
10861094 } else {
10871095 let chunkSize ;
@@ -1093,7 +1101,7 @@ function ReadableStreamDefaultControllerEnqueue(controller, chunk) {
10931101 }
10941102
10951103 try {
1096- EnqueueValueWithSize ( controller , chunk , chunkSize ) ;
1104+ EnqueueValueWithSize ( controller , chunk , chunkSize , transfer ) ;
10971105 } catch ( enqueueE ) {
10981106 ReadableStreamDefaultControllerError ( controller , enqueueE ) ;
10991107 throw enqueueE ;
@@ -1148,7 +1156,7 @@ function ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) {
11481156}
11491157
11501158function SetUpReadableStreamDefaultController (
1151- stream , controller , startAlgorithm , pullAlgorithm , cancelAlgorithm , highWaterMark , sizeAlgorithm ) {
1159+ stream , controller , startAlgorithm , pullAlgorithm , cancelAlgorithm , highWaterMark , sizeAlgorithm , isTransferring ) {
11521160 assert ( stream . _controller === undefined ) ;
11531161
11541162 controller . _stream = stream ;
@@ -1169,6 +1177,8 @@ function SetUpReadableStreamDefaultController(
11691177 controller . _pullAlgorithm = pullAlgorithm ;
11701178 controller . _cancelAlgorithm = cancelAlgorithm ;
11711179
1180+ controller . isTransferring = isTransferring ;
1181+
11721182 stream . _controller = controller ;
11731183
11741184 const startResult = startAlgorithm ( ) ;
@@ -1195,7 +1205,7 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(
11951205 let startAlgorithm = ( ) => undefined ;
11961206 let pullAlgorithm = ( ) => promiseResolvedWith ( undefined ) ;
11971207 let cancelAlgorithm = ( ) => promiseResolvedWith ( undefined ) ;
1198-
1208+ const isTransferring = underlyingSourceDict . type === 'transfer' ;
11991209 if ( 'start' in underlyingSourceDict ) {
12001210 startAlgorithm = ( ) => underlyingSourceDict . start . call ( underlyingSource , controller ) ;
12011211 }
@@ -1207,8 +1217,8 @@ function SetUpReadableStreamDefaultControllerFromUnderlyingSource(
12071217 }
12081218
12091219 SetUpReadableStreamDefaultController (
1210- stream , controller , startAlgorithm , pullAlgorithm , cancelAlgorithm , highWaterMark , sizeAlgorithm
1211- ) ;
1220+ stream , controller , startAlgorithm , pullAlgorithm , cancelAlgorithm , highWaterMark , sizeAlgorithm ,
1221+ isTransferring ) ;
12121222}
12131223
12141224// Byte stream controllers
0 commit comments