1// Flags: --expose-internals --no-warnings 2'use strict'; 3 4const common = require('../common'); 5 6const assert = require('assert'); 7 8const { 9 internalBinding, 10} = require('internal/test/binding'); 11 12const { 13 newWritableStreamFromStreamBase, 14 newReadableStreamFromStreamBase, 15} = require('internal/webstreams/adapters'); 16 17const { 18 JSStream 19} = internalBinding('js_stream'); 20 21{ 22 const stream = new JSStream(); 23 stream.onwrite = common.mustCall((req, buf) => { 24 assert.deepStrictEqual(buf[0], Buffer.from('hello')); 25 req.oncomplete(); 26 }); 27 28 const writable = newWritableStreamFromStreamBase(stream); 29 30 const writer = writable.getWriter(); 31 32 writer.write(Buffer.from('hello')).then(common.mustCall()); 33} 34 35{ 36 const buf = Buffer.from('hello'); 37 const check = new Uint8Array(buf); 38 39 const stream = new JSStream(); 40 41 const readable = newReadableStreamFromStreamBase(stream); 42 43 const reader = readable.getReader(); 44 45 reader.read().then(common.mustCall(({ done, value }) => { 46 assert(!done); 47 assert.deepStrictEqual(new Uint8Array(value), check); 48 49 reader.read().then(common.mustCall(({ done, value }) => { 50 assert(done); 51 assert.strictEqual(value, undefined); 52 })); 53 54 })); 55 56 stream.readBuffer(buf); 57 stream.emitEOF(); 58} 59 60{ 61 const stream = new JSStream(); 62 stream.onshutdown = common.mustCall((req) => { 63 req.oncomplete(); 64 }); 65 const readable = newReadableStreamFromStreamBase(stream); 66 readable.cancel().then(common.mustCall()); 67} 68