11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_cilet pass = 0; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci{ 91cb0ef41Sopenharmony_ci // ReadableStream with byte source: respondWithNewView() throws if the 101cb0ef41Sopenharmony_ci // supplied view's buffer has a different length (in the closed state) 111cb0ef41Sopenharmony_ci const stream = new ReadableStream({ 121cb0ef41Sopenharmony_ci pull: common.mustCall(async (c) => { 131cb0ef41Sopenharmony_ci const view = new Uint8Array(new ArrayBuffer(10), 0, 0); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci c.close(); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci assert.throws(() => c.byobRequest.respondWithNewView(view), { 181cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 191cb0ef41Sopenharmony_ci name: 'RangeError', 201cb0ef41Sopenharmony_ci }); 211cb0ef41Sopenharmony_ci pass++; 221cb0ef41Sopenharmony_ci }), 231cb0ef41Sopenharmony_ci type: 'bytes', 241cb0ef41Sopenharmony_ci }); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci const reader = stream.getReader({ mode: 'byob' }); 271cb0ef41Sopenharmony_ci reader.read(new Uint8Array([4, 5, 6])); 281cb0ef41Sopenharmony_ci} 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci{ 311cb0ef41Sopenharmony_ci // ReadableStream with byte source: respondWithNewView() throws if the 321cb0ef41Sopenharmony_ci // supplied view's buffer has been detached (in the closed state) 331cb0ef41Sopenharmony_ci const stream = new ReadableStream({ 341cb0ef41Sopenharmony_ci pull: common.mustCall((c) => { 351cb0ef41Sopenharmony_ci c.close(); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci // Detach it by reading into it 381cb0ef41Sopenharmony_ci const view = new Uint8Array([1, 2, 3]); 391cb0ef41Sopenharmony_ci reader.read(view); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci assert.throws(() => c.byobRequest.respondWithNewView(view), { 421cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE', 431cb0ef41Sopenharmony_ci name: 'TypeError', 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci pass++; 461cb0ef41Sopenharmony_ci }), 471cb0ef41Sopenharmony_ci type: 'bytes', 481cb0ef41Sopenharmony_ci }); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci const reader = stream.getReader({ mode: 'byob' }); 511cb0ef41Sopenharmony_ci reader.read(new Uint8Array([4, 5, 6])); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci{ 551cb0ef41Sopenharmony_ci const stream = new ReadableStream({ 561cb0ef41Sopenharmony_ci start(c) { 571cb0ef41Sopenharmony_ci c.enqueue(new Uint8Array([1, 2, 3])); 581cb0ef41Sopenharmony_ci }, 591cb0ef41Sopenharmony_ci type: 'bytes', 601cb0ef41Sopenharmony_ci }); 611cb0ef41Sopenharmony_ci const reader = stream.getReader({ mode: 'byob' }); 621cb0ef41Sopenharmony_ci const view = new Uint8Array(); 631cb0ef41Sopenharmony_ci assert 641cb0ef41Sopenharmony_ci .rejects(reader.read(view), { 651cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE', 661cb0ef41Sopenharmony_ci name: 'TypeError', 671cb0ef41Sopenharmony_ci }) 681cb0ef41Sopenharmony_ci .then(common.mustCall()); 691cb0ef41Sopenharmony_ci} 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci{ 721cb0ef41Sopenharmony_ci const stream = new ReadableStream({ 731cb0ef41Sopenharmony_ci start(c) { 741cb0ef41Sopenharmony_ci c.enqueue(new Uint8Array([1, 2, 3])); 751cb0ef41Sopenharmony_ci }, 761cb0ef41Sopenharmony_ci type: 'bytes', 771cb0ef41Sopenharmony_ci }); 781cb0ef41Sopenharmony_ci const reader = stream.getReader({ mode: 'byob' }); 791cb0ef41Sopenharmony_ci const view = new Uint8Array(new ArrayBuffer(10), 0, 0); 801cb0ef41Sopenharmony_ci assert 811cb0ef41Sopenharmony_ci .rejects(reader.read(view), { 821cb0ef41Sopenharmony_ci code: 'ERR_INVALID_STATE', 831cb0ef41Sopenharmony_ci name: 'TypeError', 841cb0ef41Sopenharmony_ci }) 851cb0ef41Sopenharmony_ci .then(common.mustCall()); 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciprocess.on('exit', () => assert.strictEqual(pass, 2)); 89