@@ -331,12 +331,54 @@ assert.throws(() => new Blob({}), {
331331 const b = new Blob ( Array ( 10 ) . fill ( 'hello' ) ) ;
332332 const stream = b . stream ( ) ;
333333 const reader = stream . getReader ( ) ;
334- assert . strictEqual ( stream [ kState ] . controller . desiredSize , 1 ) ;
334+ assert . strictEqual ( stream [ kState ] . controller . desiredSize , 0 ) ;
335335 const { value, done } = await reader . read ( ) ;
336336 assert . strictEqual ( value . byteLength , 5 ) ;
337337 assert ( ! done ) ;
338338 setTimeout ( ( ) => {
339- assert . strictEqual ( stream [ kState ] . controller . desiredSize , 0 ) ;
339+ // The blob stream is now a byte stream hence after the first read,
340+ // it should pull in the next 'hello' which is 5 bytes hence -5.
341+ assert . strictEqual ( stream [ kState ] . controller . desiredSize , - 5 ) ;
342+ } , 0 ) ;
343+ } ) ( ) . then ( common . mustCall ( ) ) ;
344+
345+ ( async ( ) => {
346+ const blob = new Blob ( [ 'hello' , 'world' ] ) ;
347+ const stream = blob . stream ( ) ;
348+ const reader = stream . getReader ( { mode : 'byob' } ) ;
349+ const decoder = new TextDecoder ( ) ;
350+ const chunks = [ ] ;
351+ while ( true ) {
352+ const { value, done } = await reader . read ( new Uint8Array ( 100 ) ) ;
353+ if ( done ) break ;
354+ chunks . push ( decoder . decode ( value , { stream : true } ) ) ;
355+ }
356+ assert . strictEqual ( chunks . join ( '' ) , 'helloworld' ) ;
357+ } ) ( ) . then ( common . mustCall ( ) ) ;
358+
359+ ( async ( ) => {
360+ const b = new Blob ( Array ( 10 ) . fill ( 'hello' ) ) ;
361+ const stream = b . stream ( ) ;
362+ const reader = stream . getReader ( { mode : 'byob' } ) ;
363+ assert . strictEqual ( stream [ kState ] . controller . desiredSize , 0 ) ;
364+ const { value, done } = await reader . read ( new Uint8Array ( 100 ) ) ;
365+ assert . strictEqual ( value . byteLength , 5 ) ;
366+ assert ( ! done ) ;
367+ setTimeout ( ( ) => {
368+ assert . strictEqual ( stream [ kState ] . controller . desiredSize , - 5 ) ;
369+ } , 0 ) ;
370+ } ) ( ) . then ( common . mustCall ( ) ) ;
371+
372+ ( async ( ) => {
373+ const b = new Blob ( Array ( 10 ) . fill ( 'hello' ) ) ;
374+ const stream = b . stream ( ) ;
375+ const reader = stream . getReader ( { mode : 'byob' } ) ;
376+ assert . strictEqual ( stream [ kState ] . controller . desiredSize , 0 ) ;
377+ const { value, done } = await reader . read ( new Uint8Array ( 2 ) ) ;
378+ assert . strictEqual ( value . byteLength , 2 ) ;
379+ assert ( ! done ) ;
380+ setTimeout ( ( ) => {
381+ assert . strictEqual ( stream [ kState ] . controller . desiredSize , - 3 ) ;
340382 } , 0 ) ;
341383} ) ( ) . then ( common . mustCall ( ) ) ;
342384
0 commit comments