@@ -4,65 +4,63 @@ export async function fromReadableStream(
44  stream : ReadableStream < Uint8Array > , 
55  base64 ?: boolean , 
66) : Promise < string >  { 
7-   const  reader  =  stream . getReader ( ) ; 
87  const  chunks : Uint8Array [ ]  =  [ ] ; 
98  let  totalLength  =  0 ; 
109
11-   try  { 
12-     while  ( true )  { 
13-       const  {  done,  value }  =  await  reader . read ( ) ; 
14-       if  ( done )  break ; 
15-       chunks . push ( value ) ; 
16-       totalLength  +=  value . length ; 
17-     } 
18- 
19-     if  ( chunks . length  ===  0 )  { 
20-       return  "" ; 
21-     } 
10+   for  await  ( const  chunk  of  stream )  { 
11+     chunks . push ( chunk ) ; 
12+     totalLength  +=  chunk . length ; 
13+   } 
2214
23-      if  ( chunks . length  ===  1 )  { 
24-        return  Buffer . from ( chunks [ 0 ] ) . toString ( base64  ?  "base64"  :  "utf8" ) ; 
25-      } 
15+   if  ( chunks . length  ===  0 )  { 
16+     return  "" ; 
17+   } 
2618
27-     // Pre-allocate buffer with exact size to avoid reallocation 
28-     const  buffer  =  Buffer . allocUnsafe ( totalLength ) ; 
29-     let  offset  =  0 ; 
30-     for  ( const  chunk  of  chunks )  { 
31-       buffer . set ( chunk ,  offset ) ; 
32-       offset  +=  chunk . length ; 
33-     } 
19+   if  ( chunks . length  ===  1 )  { 
20+     return  Buffer . from ( chunks [ 0 ] ) . toString ( base64  ? "base64"  : "utf8" ) ; 
21+   } 
3422
35-     return  buffer . toString ( base64  ? "base64"  : "utf8" ) ; 
36-   }  finally  { 
37-     reader . releaseLock ( ) ; 
23+   // Pre-allocate buffer with exact size to avoid reallocation 
24+   const  buffer  =  Buffer . alloc ( totalLength ) ; 
25+   let  offset  =  0 ; 
26+   for  ( const  chunk  of  chunks )  { 
27+     buffer . set ( chunk ,  offset ) ; 
28+     offset  +=  chunk . length ; 
3829  } 
30+ 
31+   return  buffer . toString ( base64  ? "base64"  : "utf8" ) ; 
3932} 
4033
4134export  function  toReadableStream ( 
4235  value : string , 
4336  isBase64 ?: boolean , 
4437) : ReadableStream  { 
45-   const  buffer  =  Buffer . from ( value ,  isBase64  ? "base64"  : "utf8" ) ; 
46- 
47-   return  new  ReadableStream ( { 
48-     start ( controller )  { 
49-       controller . enqueue ( buffer ) ; 
50-       controller . close ( ) ; 
38+   return  new  ReadableStream ( 
39+     { 
40+       start ( controller )  { 
41+         // Defer the Buffer.from conversion to when the stream is actually read. 
42+         controller . enqueue ( Buffer . from ( value ,  isBase64  ? "base64"  : "utf8" ) ) ; 
43+         controller . close ( ) ; 
44+       } , 
5145    } , 
52-   } ) ; 
46+     {  highWaterMark : 0  } , 
47+   ) ; 
5348} 
5449
5550let  maybeSomethingBuffer : Buffer  |  undefined ; 
5651
5752export  function  emptyReadableStream ( ) : ReadableStream  { 
5853  if  ( process . env . OPEN_NEXT_FORCE_NON_EMPTY_RESPONSE  ===  "true" )  { 
59-     return  new  ReadableStream ( { 
60-       start ( controller )  { 
61-         maybeSomethingBuffer  ??=  Buffer . from ( "SOMETHING" ) ; 
62-         controller . enqueue ( maybeSomethingBuffer ) ; 
63-         controller . close ( ) ; 
54+     return  new  ReadableStream ( 
55+       { 
56+         pull ( controller )  { 
57+           maybeSomethingBuffer  ??=  Buffer . from ( "SOMETHING" ) ; 
58+           controller . enqueue ( maybeSomethingBuffer ) ; 
59+           controller . close ( ) ; 
60+         } , 
6461      } , 
65-     } ) ; 
62+       {  highWaterMark : 0  } , 
63+     ) ; 
6664  } 
6765  return  new  ReadableStream ( { 
6866    start ( controller )  { 
0 commit comments