1'use strict'; 2 3const common = require('../common'); 4const assert = require('node:assert'); 5 6let pass = 0; 7 8{ 9 // ReadableStream with byte source: respondWithNewView() throws if the 10 // supplied view's buffer has a different length (in the closed state) 11 const stream = new ReadableStream({ 12 pull: common.mustCall(async (c) => { 13 const view = new Uint8Array(new ArrayBuffer(10), 0, 0); 14 15 c.close(); 16 17 assert.throws(() => c.byobRequest.respondWithNewView(view), { 18 code: 'ERR_INVALID_ARG_VALUE', 19 name: 'RangeError', 20 }); 21 pass++; 22 }), 23 type: 'bytes', 24 }); 25 26 const reader = stream.getReader({ mode: 'byob' }); 27 reader.read(new Uint8Array([4, 5, 6])); 28} 29 30{ 31 // ReadableStream with byte source: respondWithNewView() throws if the 32 // supplied view's buffer has been detached (in the closed state) 33 const stream = new ReadableStream({ 34 pull: common.mustCall((c) => { 35 c.close(); 36 37 // Detach it by reading into it 38 const view = new Uint8Array([1, 2, 3]); 39 reader.read(view); 40 41 assert.throws(() => c.byobRequest.respondWithNewView(view), { 42 code: 'ERR_INVALID_STATE', 43 name: 'TypeError', 44 }); 45 pass++; 46 }), 47 type: 'bytes', 48 }); 49 50 const reader = stream.getReader({ mode: 'byob' }); 51 reader.read(new Uint8Array([4, 5, 6])); 52} 53 54{ 55 const stream = new ReadableStream({ 56 start(c) { 57 c.enqueue(new Uint8Array([1, 2, 3])); 58 }, 59 type: 'bytes', 60 }); 61 const reader = stream.getReader({ mode: 'byob' }); 62 const view = new Uint8Array(); 63 assert 64 .rejects(reader.read(view), { 65 code: 'ERR_INVALID_STATE', 66 name: 'TypeError', 67 }) 68 .then(common.mustCall()); 69} 70 71{ 72 const stream = new ReadableStream({ 73 start(c) { 74 c.enqueue(new Uint8Array([1, 2, 3])); 75 }, 76 type: 'bytes', 77 }); 78 const reader = stream.getReader({ mode: 'byob' }); 79 const view = new Uint8Array(new ArrayBuffer(10), 0, 0); 80 assert 81 .rejects(reader.read(view), { 82 code: 'ERR_INVALID_STATE', 83 name: 'TypeError', 84 }) 85 .then(common.mustCall()); 86} 87 88process.on('exit', () => assert.strictEqual(pass, 2)); 89