Best JavaScript code snippet using wpt
ReadableByteStreamInternals.js
Source:ReadableByteStreamInternals.js
1/*2 * Copyright (C) 2016 Canon Inc. All rights reserved.3 *4 * Redistribution and use in source and binary forms, with or without5 * modification, are permitted provided that the following conditions6 * are met:7 * 1. Redistributions of source code must retain the above copyright8 * notice, this list of conditions and the following disclaimer.9 * 2. Redistributions in binary form must reproduce the above copyright10 * notice, this list of conditions and the following disclaimer in the11 * documentation and/or other materials provided with the distribution.12 *13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24 */25// @conditional=ENABLE(READABLE_STREAM_API) && ENABLE(READABLE_BYTE_STREAM_API)26// @internal27function privateInitializeReadableByteStreamController(stream, underlyingByteSource, highWaterMark)28{29 "use strict";30 if (!@isReadableStream(stream))31 @throwTypeError("ReadableByteStreamController needs a ReadableStream");32 // readableStreamController is initialized with null value.33 if (stream.@readableStreamController !== null)34 @throwTypeError("ReadableStream already has a controller");35 this.@controlledReadableStream = stream;36 this.@underlyingByteSource = underlyingByteSource;37 this.@pullAgain = false;38 this.@pulling = false;39 @readableByteStreamControllerClearPendingPullIntos(this);40 this.@queue = [];41 this.@totalQueuedBytes = 0;42 this.@started = false;43 this.@closeRequested = false;44 let hwm = @Number(highWaterMark);45 if (@isNaN(hwm) || hwm < 0)46 @throwRangeError("highWaterMark value is negative or not a number");47 this.@strategyHWM = hwm;48 let autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;49 if (autoAllocateChunkSize !== @undefined) {50 autoAllocateChunkSize = @Number(autoAllocateChunkSize);51 if (autoAllocateChunkSize <= 0 || autoAllocateChunkSize === @Number.POSITIVE_INFINITY || autoAllocateChunkSize === @Number.NEGATIVE_INFINITY)52 @throwRangeError("autoAllocateChunkSize value is negative or equal to positive or negative infinity");53 }54 this.@autoAllocateChunkSize = autoAllocateChunkSize;55 this.@pendingPullIntos = [];56 const controller = this;57 const startResult = @promiseInvokeOrNoopNoCatch(underlyingByteSource, "start", [this]).@then(() => {58 controller.@started = true;59 @assert(!controller.@pulling);60 @assert(!controller.@pullAgain);61 @readableByteStreamControllerCallPullIfNeeded(controller);62 }, (error) => {63 if (stream.@state === @streamReadable)64 @readableByteStreamControllerError(controller, error);65 });66 this.@cancel = @readableByteStreamControllerCancel;67 this.@pull = @readableByteStreamControllerPull;68 return this;69}70function privateInitializeReadableStreamBYOBRequest(controller, view)71{72 "use strict";73 this.@associatedReadableByteStreamController = controller;74 this.@view = view;75}76function isReadableByteStreamController(controller)77{78 "use strict";79 // Same test mechanism as in isReadableStreamDefaultController (ReadableStreamInternals.js).80 // See corresponding function for explanations.81 return @isObject(controller) && !!controller.@underlyingByteSource;82}83function isReadableStreamBYOBRequest(byobRequest)84{85 "use strict";86 // Same test mechanism as in isReadableStreamDefaultController (ReadableStreamInternals.js).87 // See corresponding function for explanations.88 return @isObject(byobRequest) && !!byobRequest.@associatedReadableByteStreamController;89}90function isReadableStreamBYOBReader(reader)91{92 "use strict";93 // FIXME: Since BYOBReader is not yet implemented, always return false.94 // To be implemented at the same time as BYOBReader (see isReadableStreamDefaultReader95 // to apply same model).96 return false;97}98function readableByteStreamControllerCancel(controller, reason)99{100 "use strict";101 if (controller.@pendingPullIntos.length > 0)102 controller.@pendingPullIntos[0].bytesFilled = 0;103 controller.@queue = [];104 controller.@totalQueuedBytes = 0;105 return @promiseInvokeOrNoop(controller.@underlyingByteSource, "cancel", [reason]);106}107function readableByteStreamControllerError(controller, e)108{109 "use strict";110 @assert(controller.@controlledReadableStream.@state === @streamReadable);111 @readableByteStreamControllerClearPendingPullIntos(controller);112 controller.@queue = [];113 @readableStreamError(controller.@controlledReadableStream, e);114}115function readableByteStreamControllerClose(controller)116{117 "use strict";118 @assert(!controller.@closeRequested);119 @assert(controller.@controlledReadableStream.@state === @streamReadable);120 if (controller.@totalQueuedBytes > 0) {121 controller.@closeRequested = true;122 return;123 }124 if (controller.@pendingPullIntos.length > 0) {125 if (controller.@pendingPullIntos[0].bytesFilled > 0) {126 const e = new @TypeError("Close requested while there remain pending bytes");127 @readableByteStreamControllerError(controller, e);128 throw e;129 }130 }131 @readableStreamClose(controller.@controlledReadableStream);132}133function readableByteStreamControllerClearPendingPullIntos(controller)134{135 "use strict";136 // FIXME: To be implemented in conjunction with ReadableStreamBYOBRequest.137}138function readableByteStreamControllerGetDesiredSize(controller)139{140 "use strict";141 return controller.@strategyHWM - controller.@totalQueuedBytes;142}143function readableStreamHasBYOBReader(stream)144{145 "use strict";146 return stream.@reader !== @undefined && @isReadableStreamBYOBReader(stream.@reader);147}148function readableStreamHasDefaultReader(stream)149{150 "use strict";151 return stream.@reader !== @undefined && @isReadableStreamDefaultReader(stream.@reader);152}153function readableByteStreamControllerHandleQueueDrain(controller) {154 "use strict";155 @assert(controller.@controlledReadableStream.@state === @streamReadable);156 if (!controller.@totalQueuedBytes && controller.@closeRequested)157 @readableStreamClose(controller.@controlledReadableStream);158 else159 @readableByteStreamControllerCallPullIfNeeded(controller);160}161function readableByteStreamControllerPull(controller)162{163 "use strict";164 const stream = controller.@controlledReadableStream;165 @assert(@readableStreamHasDefaultReader(stream));166 if (controller.@totalQueuedBytes > 0) {167 @assert(stream.@reader.@readRequests.length === 0);168 const entry = controller.@queue.@shift();169 controller.@totalQueuedBytes -= entry.byteLength;170 @readableByteStreamControllerHandleQueueDrain(controller);171 let view;172 try {173 view = new @Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);174 } catch (error) {175 return @Promise.@reject(error);176 }177 return @Promise.@resolve({value: view, done: false});178 }179 if (controller.@autoAllocateChunkSize !== @undefined) {180 let buffer;181 try {182 buffer = new @ArrayBuffer(controller.@autoAllocateChunkSize);183 } catch (error) {184 return @Promise.@reject(error);185 }186 const pullIntoDescriptor = {187 buffer,188 byteOffset: 0,189 byteLength: controller.@autoAllocateChunkSize,190 bytesFilled: 0,191 elementSize: 1,192 ctor: @Uint8Array,193 readerType: 'default'194 };195 controller.@pendingPullIntos.@push(pullIntoDescriptor);196 }197 const promise = @readableStreamAddReadRequest(stream);198 @readableByteStreamControllerCallPullIfNeeded(controller);199 return promise;200}201function readableByteStreamControllerShouldCallPull(controller)202{203 "use strict";204 const stream = controller.@controlledReadableStream;205 if (stream.@state !== @streamReadable)206 return false;207 if (controller.@closeRequested)208 return false;209 if (!controller.@started)210 return false;211 if (@readableStreamHasDefaultReader(stream) && stream.@reader.@readRequests.length > 0)212 return true;213 if (@readableStreamHasBYOBReader(stream) && stream.@reader.@readIntoRequests.length > 0)214 return true;215 if (@readableByteStreamControllerGetDesiredSize(controller) > 0)216 return true;217 return false;218}219function readableByteStreamControllerCallPullIfNeeded(controller)220{221 "use strict";222 if (!@readableByteStreamControllerShouldCallPull(controller))223 return;224 if (controller.@pulling) {225 controller.@pullAgain = true;226 return;227 }228 @assert(!controller.@pullAgain);229 controller.@pulling = true;230 @promiseInvokeOrNoop(controller.@underlyingByteSource, "pull", [controller]).@then(() => {231 controller.@pulling = false;232 if (controller.@pullAgain) {233 controller.@pullAgain = false;234 @readableByteStreamControllerCallPullIfNeeded(controller);235 }236 }, (error) => {237 if (controller.@controlledReadableStream.@state === @streamReadable)238 @readableByteStreamControllerError(controller, error);239 });240}241function transferBufferToCurrentRealm(buffer)242{243 "use strict";244 // FIXME: Determine what should be done here exactly (what is already existing in current245 // codebase and what has to be added). According to spec, Transfer operation should be246 // performed in order to transfer buffer to current realm. For the moment, simply return247 // received buffer.248 return buffer;249}250function readableByteStreamControllerEnqueue(controller, chunk)251{252 "use strict";253 const stream = controller.@controlledReadableStream;254 @assert(!controller.@closeRequested);255 @assert(stream.@state === @streamReadable);256 const buffer = chunk.buffer;257 const byteOffset = chunk.byteOffset;258 const byteLength = chunk.byteLength;259 const transferredBuffer = @transferBufferToCurrentRealm(buffer);260 if (@readableStreamHasDefaultReader(stream)) {261 if (!stream.@reader.@readRequests.length)262 @readableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);263 else {264 @assert(!controller.@queue.length);265 let transferredView = new @Uint8Array(transferredBuffer, byteOffset, byteLength);266 @readableStreamFulfillReadRequest(stream, transferredView, false);267 }268 return;269 }270 if (@readableStreamHasBYOBReader(stream)) {271 // FIXME: To be implemented once ReadableStreamBYOBReader has been implemented (for the moment,272 // test cannot be true).273 @throwTypeError("ReadableByteStreamController enqueue operation has no support for BYOB reader");274 return;275 }276 @assert(!@isReadableStreamLocked(stream));277 @readableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);278}279function readableByteStreamControllerEnqueueChunkToQueue(controller, buffer, byteOffset, byteLength)280{281 "use strict";282 controller.@queue.@push({283 buffer: buffer,284 byteOffset: byteOffset,285 byteLength: byteLength286 });287 controller.@totalQueuedBytes += byteLength;288}289function readableByteStreamControllerRespond(controller, bytesWritten)290{291 "use strict";292 bytesWritten = @Number(bytesWritten);293 if (@isNaN(bytesWritten) || bytesWritten === @Number.POSITIVE_INFINITY || bytesWritten < 0 )294 @throwRangeError("bytesWritten has an incorrect value");295 @assert(controller.@pendingPullIntos.length > 0);296 @readableByteStreamControllerRespondInternal(controller, bytesWritten);297}298function readableByteStreamControllerRespondInternal(controller, bytesWritten)299{300 "use strict";301 let firstDescriptor = controller.@pendingPullIntos[0];302 let stream = controller.@controlledReadableStream;303 if (stream.@state === @streamClosed) {304 if (bytesWritten !== 0)305 @throwTypeError("bytesWritten is different from 0 even though stream is closed");306 @readableByteStreamControllerRespondInClosedState(controller, firstDescriptor);307 } else {308 // FIXME: Also implement case of readable state (distinct patch to avoid adding too many different cases309 // in a single patch).310 @throwTypeError("Readable state is not yet supported");311 }312}313function readableByteStreamControllerRespondInClosedState(controller, firstDescriptor)314{315 "use strict";316 firstDescriptor.buffer = @transferBufferToCurrentRealm(firstDescriptor.buffer);317 @assert(firstDescriptor.bytesFilled === 0);318 // FIXME: Spec does not describe below test. However, only ReadableStreamBYOBReader has a readIntoRequests319 // property. This issue has been reported through WHATWG/streams GitHub320 // (https://github.com/whatwg/streams/issues/686), but no solution has been provided for the moment.321 // Therefore, below test is added as a temporary fix.322 if (!@isReadableStreamBYOBReader(controller.@reader))323 return;324 while (controller.@reader.@readIntoRequests.length > 0) {325 let pullIntoDescriptor = @readableByteStreamControllerShiftPendingPullInto(controller);326 @readableByteStreamControllerCommitPullIntoDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);327 }328}329function readableByteStreamControllerShiftPendingPullInto(controller)330{331 "use strict";332 let descriptor = controller.@pendingPullIntos.@shift();333 @readableByteStreamControllerInvalidateBYOBRequest(controller);334 return descriptor;335}336function readableByteStreamControllerInvalidateBYOBRequest(controller)337{338 "use strict";339 if (controller.@byobRequest === @undefined)340 return;341 controller.@byobRequest.@associatedReadableByteStreamController = @undefined;342 controller.@byobRequest.@view = @undefined;343 controller.@byobRequest = @undefined;344}345function readableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor)346{347 "use strict";348 @assert(stream.@state !== @streamErrored);349 let done = false;350 if (stream.@state === @streamClosed) {351 @assert(!pullIntoDescriptor.bytesFilled);352 done = true;353 }354 let filledView = @readableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor);355 if (pullIntoDescriptor.readerType === "default")356 @readableStreamFulfillReadRequest(stream, filledView, done);357 else {358 @assert(pullIntoDescriptor.readerType === "byob");359 @readableStreamFulfillReadIntoRequest(stream, filledView, done);360 }361}362function readableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor)363{364 "use strict";365 @assert(pullIntoDescriptor.bytesFilled <= pullIntoDescriptor.bytesLength);366 @assert(pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize === 0);367 return new pullIntoDescriptor.ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, pullIntoDescriptor.bytesFilled / pullIntoDescriptor.elementSize);368}369function readableStreamFulfillReadIntoRequest(stream, chunk, done)370{371 "use strict";372 stream.@reader.@readIntoRequests.@shift().@resolve.@call(@undefined, {value: chunk, done: done});...
Using AI Code Generation
1test(() => {2 const rs = new ReadableStream();3 const reader = rs.getReader();4 const controller = rs.[[readableStreamController]];5 assert_throws(new TypeError(), () => controller.respondInClosedState(0),6 'respondInClosedState should throw a TypeError exception');7}, 'ReadableByteStreamController RespondInClosedState should throw a TypeError exception');8test(() => {9 const rs = new ReadableStream();10 const reader = rs.getReader();11 const controller = rs.[[readableStreamController]];12 assert_throws(new TypeError(), () => controller.respondWithNewView(new Uint8Array(0)),13 'respondWithNewView should throw a TypeError exception');14}, 'ReadableByteStreamController RespondWithNewView should throw a TypeError exception');15test(() => {16 const rs = new ReadableStream({17 pull: c => c.close(),18 });19 const reader = rs.getReader({ mode: 'byob' });20 const readPromise = reader.read(new Uint8Array(1));21 return promise_rejects(t, new TypeError(), readPromise,22 'read() should reject with a TypeError exception');23}, 'ReadableStreamBYOBRequest Respond should throw a TypeError exception');24test(() => {25 const rs = new ReadableStream({26 pull: c => c.close(),27 });28 const reader = rs.getReader({ mode: 'byob' });29 const readPromise = reader.read(new Uint8Array(1));30 return promise_rejects(t, new TypeError(), readPromise,31 'read() should reject with a TypeError exception');32}, 'ReadableStreamBYOBRequest RespondWithNewView should throw a TypeError exception');33test(() => {34 const rs = new ReadableStream({35 pull: c => c.close(),36 });37 const reader = rs.getReader({ mode: 'byob' });
Using AI Code Generation
1'use strict';2const { ReadableStream } = require('stream/web');3const { ReadableByteStreamController } = require('stream/web');4const { ReadableByteStreamControllerRespondInClosedState } = require('stream/web');5const { ReadableStreamBYOBReader } = require('stream/web');6const { ReadableStreamBYOBRequest } = require('stream/web');7const { ReadableByteStreamControllerRespond } = require('stream/web');8const { ReadableStreamDefaultReader } = require('stream/web');9const { ReadableStreamDefaultControllerClose } = require('stream/web');10const { ReadableStreamDefaultControllerEnqueue } = require('stream/web');11const { ReadableStreamDefaultControllerError } = require('stream/web');12const { ReadableStreamDefaultControllerGetDesiredSize } = require('stream/web');13const { ReadableStreamDefaultControllerHasBackpressure } = require('stream/web');14const { ReadableStreamDefaultControllerCanCloseOrEnqueue } = require('stream/web');15const { ReadableStreamDefaultController } = require('stream/web');16const { ReadableStreamBYOBRequestRespond } = require('stream/web');17const { ReadableStreamBYOBRequestRespondWithNewView } = require('stream/web');18const { ReadableStreamBYOBRequestGetView } = require('stream/web');19const { ReadableStreamTee } = require('stream/web');20const { ReadableStreamCancel } = require('stream/web');21const { ReadableStreamClose } = require('stream/web');22const { ReadableStreamCreateReadResult } = require('stream/web');23const { ReadableStreamError } = require('stream/web');24const { ReadableStreamGetNumReadRequests } = require('stream/web');25const { ReadableStreamGetNumReadIntoRequests } = require('stream/web');26const { ReadableStreamHasDefaultReader } = require('stream/web');27const { ReadableStreamHasBYOBReader } = require('stream/web');28const { ReadableStreamGetBYOBReader } = require('stream/web');29const { ReadableStreamGetDefaultReader } = require('stream/web');30const { ReadableStreamFulfillReadRequest } = require('stream/web');31const { ReadableStreamFulfillReadIntoRequest } = require('stream/web');32const { ReadableStreamAddReadIntoRequest } = require('stream/web');33const { ReadableStreamAddReadRequest } = require('stream
Using AI Code Generation
1test(function() {2 var controller = new ReadableByteStreamController();3 assert_throws(new TypeError(), function() {4 controller.respondInClosedState(0);5 });6}, 'ReadableByteStreamControllerRespondInClosedState: Throws TypeError if this is not a ReadableByteStreamController');7test(function() {8 var controller = new ReadableByteStreamController();9 assert_throws(new TypeError(), function() {10 controller.respondInClosedState(0);11 });12}, 'ReadableByteStreamControllerRespondInClosedState: Throws TypeError if this is not a ReadableByteStreamController');13test(function() {14 var controller = new ReadableByteStreamController();15 assert_throws(new TypeError(), function() {16 controller.respondInClosedState(0);17 });18}, 'ReadableByteStreamControllerRespondInClosedState: Throws TypeError if this is not a ReadableByteStreamController');19test(function() {20 var controller = new ReadableByteStreamController();21 assert_throws(new TypeError(), function() {22 controller.respondInClosedState(0);23 });24}, 'ReadableByteStreamControllerRespondInClosedState: Throws TypeError if this is not a ReadableByteStreamController');25test(function() {26 var controller = new ReadableByteStreamController();27 assert_throws(new TypeError(), function() {28 controller.respondInClosedState(0);29 });30}, 'ReadableByteStreamControllerRespondInClosedState: Throws TypeError if this is not a ReadableByteStreamController');31test(function() {32 var controller = new ReadableByteStreamController();33 assert_throws(new TypeError(), function() {34 controller.respondInClosedState(0);35 });36}, 'ReadableByteStreamControllerRespondInClosedState: Throws TypeError if this is not a ReadableByteStreamController');
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(controller) {3 controller.close();4 }5});6var reader = rs.getReader();7var startPromise = reader.read().then(function(result) {8 assert_true(result.done, 'the stream should be closed');9});10var rs = new ReadableStream({11 pull: function(controller) {12 controller.error();13 }14});15var reader = rs.getReader();16var startPromise = reader.read().then(function(result) {17 assert_true(result.done, 'the stream should be errored');18});19var byobRequest;20var rs = new ReadableStream({21 pull: function(controller) {22 byobRequest = controller.byobRequest;23 assert_true(byobRequest !== undefined, 'byobRequest must be defined');24 var view = new Uint8Array(2);25 view[0] = 1;26 view[1] = 2;27 controller.respondWithNewView(view);28 }29});30var reader = rs.getReader();31var startPromise = reader.read().then(function(result) {32 assert_array_equals(result.value, [1, 2], 'the chunk read should be correct');33});34var byobRequest;35var rs = new ReadableStream({36 pull: function(controller) {37 byobRequest = controller.byobRequest;38 assert_true(byobRequest !== undefined, 'byobRequest must be defined');39 var view = new Uint8Array(2);40 view[0] = 1;41 view[1] = 2;42 byobRequest.respond(2);43 }44});45var reader = rs.getReader();46var startPromise = reader.read().then(function(result) {47 assert_array_equals(result.value, [1, 2], 'the chunk read should be correct');48});49var byobRequest;50var rs = new ReadableStream({51 pull: function(controller) {
Using AI Code Generation
1ReadableStreamBYOBReader.prototype.read = function() {2 return new Promise((resolve, reject) => {3 this._read().then(4 result => {5 if (result.done) {6 resolve(result);7 } else {8 this._readableByteStreamControllerRespondInClosedState(result.value.length);9 resolve(result);10 }11 },12 e => {13 reject(e);14 }15 );16 });17};18ReadableStreamBYOBReader.prototype._read = function() {19 if (IsReadableStreamBYOBReader(this) === false) {20 return Promise.reject(streamBrandCheckException('read'));21 }22 if (IsReadableStreamLocked(this._stream) === true) {23 return Promise.reject(new TypeError('Cannot read from a locked stream.'));24 }25 const promise = ReadableStreamAddReadIntoRequest(this._stream);26 ReadableByteStreamControllerCallPullIfNeeded(this._stream._readableStreamController);27 return promise;28};29function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {30 assert(controller._controlledReadableByteStream._state === 'closed');31 assert(firstDescriptor.bytesFilled === 0);32 const view = firstDescriptor.view;33 view.buffer.byteLength = 0;34 ReadableByteStreamControllerClearAlgorithms(controller);35 ReadableStreamClose(controller._controlledReadableByteStream);36}37function ReadableByteStreamControllerCallPullIfNeeded(controller) {38 const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);39 if (shouldPull === false) {40 return;41 }42 if (controller._pulling === true) {43 controller._pullAgain = true;44 return;45 }46 assert(controller._pullAgain === false);47 controller._pulling = true;48 PromiseInvokeOrNoop(controller._underlyingByteSource, 'pull', [controller])49 .then(50 () => {51 controller._pulling = false;52 if (controller._pullAgain === true) {53 controller._pullAgain = false;54 ReadableByteStreamControllerCallPullIfNeeded(controller);55 }56 },57 e => {58 ReadableByteStreamControllerError(controller, e);
Using AI Code Generation
1function test()2{3 var rs = new ReadableStream({4 start(c) {5 c.close();6 assert_throws(new TypeError(), () => c.respond(10),7 'ReadableByteStreamControllerRespondInClosedState should throw a TypeError');8 }9 });10 rs.getReader().read().then(r => {11 assert_equals(r.value, undefined, 'value');12 assert_true(r.done, 'done');13 }).catch(unreached_rejection('read() should not reject'));14}15test();16function test()17{18 var rs = new ReadableStream({19 start(c) {20 c.error();21 assert_throws(new TypeError(), () => c.respond(10),22 'ReadableByteStreamControllerRespondInErroredState should throw a TypeError');23 }24 });25 rs.getReader().read().then(r => {26 assert_equals(r.value, undefined, 'value');27 assert_true(r.done, 'done');28 }).catch(unreached_rejection('read() should not reject'));29}30test();31function test()32{33 var rs = new ReadableStream({34 start(c) {35 c.enqueue(new Uint8Array([1, 2, 3]));36 c.respond(0);37 assert_throws(new TypeError(), () => c.respond(10),38 'ReadableByteStreamControllerRespondInReadableState should throw a TypeError');39 }40 });41 rs.getReader().read().then(r => {42 assert_array_equals(r.value, [1, 2, 3], 'value');43 assert_false(r.done, 'done');44 }).catch(unreached_rejection('read() should not reject'));45}46test();47function test()48{49 var rs = new ReadableStream({50 start(c) {51 c.enqueue(new Uint8Array([1, 2, 3]));52 c.respond(0);53 assert_throws(new TypeError(), () => c.respondWithNewView(new Uint8Array([4, 5, 6])),
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!