|
| 1 | +// Flags: --expose-internals --no-warnings |
| 2 | +'use strict'; |
| 3 | + |
| 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. |
| 10 | + |
| 11 | +const common = require('../common'); |
| 12 | + |
| 13 | +const assert = require('assert'); |
| 14 | + |
| 15 | +const { |
| 16 | + internalBinding, |
| 17 | +} = require('internal/test/binding'); |
| 18 | + |
| 19 | +const { |
| 20 | + newWritableStreamFromStreamBase, |
| 21 | + newReadableStreamFromStreamBase, |
| 22 | +} = require('internal/webstreams/adapters'); |
| 23 | + |
| 24 | +const { |
| 25 | + kStreamBase, |
| 26 | +} = require('internal/webstreams/util'); |
| 27 | + |
| 28 | +const { |
| 29 | + JSStream, |
| 30 | +} = internalBinding('js_stream'); |
| 31 | + |
| 32 | +// Test 1: kStreamBase marker is attached to ReadableStream |
| 33 | +{ |
| 34 | + const stream = new JSStream(); |
| 35 | + const readable = newReadableStreamFromStreamBase(stream); |
| 36 | + |
| 37 | + assert.strictEqual(readable[kStreamBase], stream); |
| 38 | + |
| 39 | + // Cleanup |
| 40 | + stream.emitEOF(); |
| 41 | +} |
| 42 | + |
| 43 | +// Test 2: kStreamBase marker is attached to WritableStream |
| 44 | +{ |
| 45 | + const stream = new JSStream(); |
| 46 | + stream.onwrite = common.mustNotCall(); |
| 47 | + stream.onshutdown = (req) => req.oncomplete(); |
| 48 | + |
| 49 | + const writable = newWritableStreamFromStreamBase(stream); |
| 50 | + |
| 51 | + assert.strictEqual(writable[kStreamBase], stream); |
| 52 | + |
| 53 | + // Cleanup |
| 54 | + writable.close(); |
| 55 | +} |
| 56 | + |
| 57 | +// Test 3: Regular JS streams don't have kStreamBase |
| 58 | +{ |
| 59 | + const { ReadableStream, WritableStream } = require('stream/web'); |
| 60 | + |
| 61 | + const rs = new ReadableStream({ |
| 62 | + pull(controller) { |
| 63 | + controller.enqueue('chunk'); |
| 64 | + controller.close(); |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const ws = new WritableStream({ |
| 69 | + write() {}, |
| 70 | + }); |
| 71 | + |
| 72 | + assert.strictEqual(rs[kStreamBase], undefined); |
| 73 | + assert.strictEqual(ws[kStreamBase], undefined); |
| 74 | + |
| 75 | + // Pipe should still work (standard path) |
| 76 | + rs.pipeTo(ws).then(common.mustCall()); |
| 77 | +} |
| 78 | + |
| 79 | +// Test 4: Mixed streams (one internal, one JS) use standard path |
| 80 | +{ |
| 81 | + const stream = new JSStream(); |
| 82 | + stream.onshutdown = (req) => req.oncomplete(); |
| 83 | + const readable = newReadableStreamFromStreamBase(stream); |
| 84 | + |
| 85 | + const { WritableStream } = require('stream/web'); |
| 86 | + const chunks = []; |
| 87 | + const ws = new WritableStream({ |
| 88 | + write(chunk) { |
| 89 | + chunks.push(chunk); |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + // Readable has kStreamBase, ws does not - should use standard path |
| 94 | + assert.ok(readable[kStreamBase]); |
| 95 | + assert.strictEqual(ws[kStreamBase], undefined); |
| 96 | + |
| 97 | + const pipePromise = readable.pipeTo(ws); |
| 98 | + |
| 99 | + stream.readBuffer(Buffer.from('hello')); |
| 100 | + stream.emitEOF(); |
| 101 | + |
| 102 | + pipePromise.then(common.mustCall(() => { |
| 103 | + assert.strictEqual(chunks.length, 1); |
| 104 | + })); |
| 105 | +} |
| 106 | + |
| 107 | +// Test 5: Verify kStreamBase is the correct symbol from util |
| 108 | +{ |
| 109 | + const { |
| 110 | + kStreamBase: kStreamBase2, |
| 111 | + } = require('internal/webstreams/util'); |
| 112 | + |
| 113 | + // Should be the same symbol |
| 114 | + assert.strictEqual(kStreamBase, kStreamBase2); |
| 115 | +} |
0 commit comments