11// Flags: --expose-internals --no-warnings
22'use strict' ;
33
4- // Tests for the internal StreamBase pipe optimization infrastructure
5- // described in nodejs/performance#134
6- //
7- // Note(mertcanaltin): Full fast-path testing requires real StreamBase implementations
8- // (like HTTP/2 streams or TCP sockets), not JSStream mocks.
9- // These tests verify the marker attachment and fallback behavior.
4+ // Tests for internal StreamBase pipe optimization (nodejs/performance#134)
105
116const common = require ( '../common' ) ;
12-
137const assert = require ( 'assert' ) ;
14-
15- const {
16- internalBinding,
17- } = require ( 'internal/test/binding' ) ;
8+ const { internalBinding } = require ( 'internal/test/binding' ) ;
189
1910const {
2011 newWritableStreamFromStreamBase,
2112 newReadableStreamFromStreamBase,
2213} = require ( 'internal/webstreams/adapters' ) ;
2314
24- const {
25- kStreamBase,
26- } = require ( 'internal/webstreams/util' ) ;
27-
28- const {
29- JSStream,
30- } = internalBinding ( 'js_stream' ) ;
15+ const { kStreamBase } = require ( 'internal/webstreams/util' ) ;
16+ const { JSStream } = internalBinding ( 'js_stream' ) ;
3117
3218// kStreamBase marker is attached to ReadableStream
3319{
3420 const stream = new JSStream ( ) ;
3521 const readable = newReadableStreamFromStreamBase ( stream ) ;
36-
3722 assert . strictEqual ( readable [ kStreamBase ] , stream ) ;
38-
39- // Cleanup
4023 stream . emitEOF ( ) ;
4124}
4225
@@ -47,10 +30,7 @@ const {
4730 stream . onshutdown = ( req ) => req . oncomplete ( ) ;
4831
4932 const writable = newWritableStreamFromStreamBase ( stream ) ;
50-
5133 assert . strictEqual ( writable [ kStreamBase ] , stream ) ;
52-
53- // Cleanup
5434 writable . close ( ) ;
5535}
5636
@@ -65,18 +45,15 @@ const {
6545 } ,
6646 } ) ;
6747
68- const ws = new WritableStream ( {
69- write ( ) { } ,
70- } ) ;
48+ const ws = new WritableStream ( { write ( ) { } } ) ;
7149
7250 assert . strictEqual ( rs [ kStreamBase ] , undefined ) ;
7351 assert . strictEqual ( ws [ kStreamBase ] , undefined ) ;
7452
75- // Pipe should still work (standard path)
7653 rs . pipeTo ( ws ) . then ( common . mustCall ( ) ) ;
7754}
7855
79- // Mixed streams (one internal, one JS) use standard path
56+ // Mixed streams use standard JS path
8057{
8158 const stream = new JSStream ( ) ;
8259 stream . onshutdown = ( req ) => req . oncomplete ( ) ;
@@ -85,17 +62,13 @@ const {
8562 const { WritableStream } = require ( 'stream/web' ) ;
8663 const chunks = [ ] ;
8764 const ws = new WritableStream ( {
88- write ( chunk ) {
89- chunks . push ( chunk ) ;
90- } ,
65+ write ( chunk ) { chunks . push ( chunk ) ; } ,
9166 } ) ;
9267
93- // Readable has kStreamBase, ws does not - should use standard path
9468 assert . ok ( readable [ kStreamBase ] ) ;
9569 assert . strictEqual ( ws [ kStreamBase ] , undefined ) ;
9670
9771 const pipePromise = readable . pipeTo ( ws ) ;
98-
9972 stream . readBuffer ( Buffer . from ( 'hello' ) ) ;
10073 stream . emitEOF ( ) ;
10174
@@ -104,12 +77,47 @@ const {
10477 } ) ) ;
10578}
10679
107- // Verify kStreamBase is the correct symbol from util
80+ // Verify kStreamBase symbol identity
10881{
109- const {
110- kStreamBase : kStreamBase2 ,
111- } = require ( 'internal/webstreams/util' ) ;
112-
113- // Should be the same symbol
82+ const { kStreamBase : kStreamBase2 } = require ( 'internal/webstreams/util' ) ;
11483 assert . strictEqual ( kStreamBase , kStreamBase2 ) ;
11584}
85+
86+ // FileHandle.readableWebStream() uses async reads, not StreamBase
87+ {
88+ const fs = require ( 'fs/promises' ) ;
89+ const path = require ( 'path' ) ;
90+ const os = require ( 'os' ) ;
91+
92+ async function testFileStreamPipe ( ) {
93+ const tmpDir = os . tmpdir ( ) ;
94+ const testFile = path . join ( tmpDir , `test-webstream-pipe-${ process . pid } .txt` ) ;
95+ const testData = 'Hello, WebStreams pipe!' ;
96+
97+ await fs . writeFile ( testFile , testData ) ;
98+
99+ try {
100+ const fileHandle = await fs . open ( testFile , 'r' ) ;
101+ const readable = fileHandle . readableWebStream ( ) ;
102+
103+ assert . strictEqual ( readable [ kStreamBase ] , undefined ) ;
104+
105+ const chunks = [ ] ;
106+ const writable = new ( require ( 'stream/web' ) . WritableStream ) ( {
107+ write ( chunk ) { chunks . push ( chunk ) ; } ,
108+ } ) ;
109+
110+ await readable . pipeTo ( writable ) ;
111+ await fileHandle . close ( ) ;
112+
113+ const result = Buffer . concat ( chunks . map ( ( c ) =>
114+ ( c instanceof Uint8Array ? Buffer . from ( c ) : Buffer . from ( c ) ) ,
115+ ) ) . toString ( ) ;
116+ assert . strictEqual ( result , testData ) ;
117+ } finally {
118+ await fs . unlink ( testFile ) . catch ( ( ) => { } ) ;
119+ }
120+ }
121+
122+ testFileStreamPipe ( ) . then ( common . mustCall ( ) ) ;
123+ }
0 commit comments