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(STREAMS_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 @readableByteStreamControllerEnqueueChunk(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 @readableByteStreamControllerEnqueueChunk(controller, transferredBuffer, byteOffset, byteLength);278}279// Spec name: readableByteStreamControllerEnqueueChunkToQueue.280function readableByteStreamControllerEnqueueChunk(controller, buffer, byteOffset, byteLength)281{282 "use strict";283 controller.@queue.@push({284 buffer: buffer,285 byteOffset: byteOffset,286 byteLength: byteLength287 });288 controller.@totalQueuedBytes += byteLength;289}290function readableByteStreamControllerRespondWithNewView(controller, view)291{292 "use strict";293 @assert(controller.@pendingPullIntos.length > 0);294 let firstDescriptor = controller.@pendingPullIntos[0];295 if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset)296 @throwRangeError("Invalid value for view.byteOffset");297 if (firstDescriptor.byteLength !== view.byteLength)298 @throwRangeError("Invalid value for view.byteLength");299 firstDescriptor.buffer = view.buffer;300 @readableByteStreamControllerRespondInternal(controller, view.byteLength);301}302function readableByteStreamControllerRespond(controller, bytesWritten)303{304 "use strict";305 bytesWritten = @Number(bytesWritten);306 if (@isNaN(bytesWritten) || bytesWritten === @Number.POSITIVE_INFINITY || bytesWritten < 0 )307 @throwRangeError("bytesWritten has an incorrect value");308 @assert(controller.@pendingPullIntos.length > 0);309 @readableByteStreamControllerRespondInternal(controller, bytesWritten);310}311function readableByteStreamControllerRespondInternal(controller, bytesWritten)312{313 "use strict";314 let firstDescriptor = controller.@pendingPullIntos[0];315 let stream = controller.@controlledReadableStream;316 if (stream.@state === @streamClosed) {317 if (bytesWritten !== 0)318 @throwTypeError("bytesWritten is different from 0 even though stream is closed");319 @readableByteStreamControllerRespondInClosedState(controller, firstDescriptor);320 } else {321 @assert(stream.@state === @streamReadable);322 @readableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);323 }324}325function readableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor)326{327 "use strict";328 if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength)329 @throwRangeError("bytesWritten value is too great");330 @assert(controller.@pendingPullIntos.length === 0 || controller.@pendingPullIntos[0] === pullIntoDescriptor);331 @readableByteStreamControllerInvalidateBYOBRequest(controller);332 pullIntoDescriptor.bytesFilled += bytesWritten;333 if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize)334 return;335 @readableByteStreamControllerShiftPendingDescriptor(controller);336 const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;337 if (remainderSize > 0) {338 const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;339 const remainder = @cloneArrayBuffer(pullIntoDescriptor.buffer, end - remainderSize, remainderSize);340 @readableByteStreamControllerEnqueueChunk(controller, remainder, 0, remainder.byteLength);341 }342 pullIntoDescriptor.buffer = @transferBufferToCurrentRealm(pullIntoDescriptor.buffer);343 pullIntoDescriptor.bytesFilled -= remainderSize;344 @readableByteStreamControllerCommitDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);345 @readableByteStreamControllerProcessPullDescriptors(controller);346}347function readableByteStreamControllerRespondInClosedState(controller, firstDescriptor)348{349 "use strict";350 firstDescriptor.buffer = @transferBufferToCurrentRealm(firstDescriptor.buffer);351 @assert(firstDescriptor.bytesFilled === 0);352 // FIXME: Spec does not describe below test. However, only ReadableStreamBYOBReader has a readIntoRequests353 // property. This issue has been reported through WHATWG/streams GitHub354 // (https://github.com/whatwg/streams/issues/686), but no solution has been provided for the moment.355 // Therefore, below test is added as a temporary fix.356 if (!@isReadableStreamBYOBReader(controller.@reader))357 return;358 while (controller.@reader.@readIntoRequests.length > 0) {359 let pullIntoDescriptor = @readableByteStreamControllerShiftPendingDescriptor(controller);360 @readableByteStreamControllerCommitDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);361 }362}363// Spec name: readableByteStreamControllerProcessPullIntoDescriptorsUsingQueue (shortened for readability).364function readableByteStreamControllerProcessPullDescriptors(controller)365{366 "use strict";367 @assert(!controller.@closeRequested);368 while (controller.@pendingPullIntos.length > 0) {369 if (controller.@totalQueuedBytes === 0)370 return;371 let pullIntoDescriptor = controller.@pendingPullIntos[0];372 if (@readableByteStreamControllerFillDescriptorFromQueue(controller, pullIntoDescriptor)) {373 @readableByteStreamControllerShiftPendingDescriptor(controller);374 @readableByteStreamControllerCommitDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);375 }376 }377}378// Spec name: readableByteStreamControllerFillPullIntoDescriptorFromQueue (shortened for readability).379function readableByteStreamControllerFillDescriptorFromQueue(controller, pullIntoDescriptor)380{381 "use strict";382 const currentAlignedBytes = pullIntoDescriptor.bytesFilled - (pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize);383 const maxBytesToCopy = controller.@totalQueuedBytes < pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled ?384 controller.@totalQueuedBytes : pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled;385 const maxBytesFilled = pullIntoDescriptor.bytesFilled + maxBytesToCopy;386 const maxAlignedBytes = maxBytesFilled - (maxBytesFilled % pullIntoDescriptor.elementSize);387 let totalBytesToCopyRemaining = maxBytesToCopy;388 let ready = false;389 if (maxAlignedBytes > currentAlignedBytes) {390 totalBytesToCopyRemaining = maxAlignedBytes - pullIntoDescriptor.bytesFilled;391 ready = true;392 }393 while (totalBytesToCopyRemaining > 0) {394 let headOfQueue = controller.@queue[0];395 const bytesToCopy = totalBytesToCopyRemaining < headOfQueue.byteLength ? totalBytesToCopyRemaining : headOfQueue.byteLength;396 // Copy appropriate part of pullIntoDescriptor.buffer to headOfQueue.buffer.397 // Remark: this implementation is not completely aligned on the definition of CopyDataBlockBytes398 // operation of ECMAScript (the case of Shared Data Block is not considered here, but it doesn't seem to be an issue).399 let fromIndex = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;400 let count = bytesToCopy;401 let toIndex = headOfQueue.byteOffset;402 while (count > 0) {403 headOfQueue.buffer[toIndex] = pullIntoDescriptor.buffer[fromIndex];404 toIndex++;405 fromIndex++;406 count--;407 }408 if (headOfQueue.byteLength === bytesToCopy)409 controller.@queue.@shift();410 else {411 headOfQueue.byteOffset += bytesToCopy;412 headOfQueue.byteLength -= bytesToCopy;413 }414 controller.@totalQueuedBytes -= bytesToCopy;415 @assert(controller.@pendingPullIntos.length === 0 || controller.@pendingPullIntos[0] === pullIntoDescriptor);416 @readableByteStreamControllerInvalidateBYOBRequest(controller);417 pullIntoDescriptor.bytesFilled += bytesToCopy;418 totalBytesToCopyRemaining -= bytesToCopy;419 }420 if (!ready) {421 @assert(controller.@totalQueuedBytes === 0);422 @assert(pullIntoDescriptor.bytesFilled > 0);423 @assert(pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize);424 }425 return ready;426}427// Spec name: readableByteStreamControllerShiftPendingPullInto (renamed for consistency).428function readableByteStreamControllerShiftPendingDescriptor(controller)429{430 "use strict";431 let descriptor = controller.@pendingPullIntos.@shift();432 @readableByteStreamControllerInvalidateBYOBRequest(controller);433 return descriptor;434}435function readableByteStreamControllerInvalidateBYOBRequest(controller)436{437 "use strict";438 if (controller.@byobRequest === @undefined)439 return;440 controller.@byobRequest.@associatedReadableByteStreamController = @undefined;441 controller.@byobRequest.@view = @undefined;442 controller.@byobRequest = @undefined;443}444// Spec name: readableByteStreamControllerCommitPullIntoDescriptor (shortened for readability).445function readableByteStreamControllerCommitDescriptor(stream, pullIntoDescriptor)446{447 "use strict";448 @assert(stream.@state !== @streamErrored);449 let done = false;450 if (stream.@state === @streamClosed) {451 @assert(!pullIntoDescriptor.bytesFilled);452 done = true;453 }454 let filledView = @readableByteStreamControllerConvertDescriptor(pullIntoDescriptor);455 if (pullIntoDescriptor.readerType === "default")456 @readableStreamFulfillReadRequest(stream, filledView, done);457 else {458 @assert(pullIntoDescriptor.readerType === "byob");459 @readableStreamFulfillReadIntoRequest(stream, filledView, done);460 }461}462// Spec name: readableByteStreamControllerConvertPullIntoDescriptor (shortened for readability).463function readableByteStreamControllerConvertDescriptor(pullIntoDescriptor)464{465 "use strict";466 @assert(pullIntoDescriptor.bytesFilled <= pullIntoDescriptor.byteLength);467 @assert(pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize === 0);468 return new pullIntoDescriptor.ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, pullIntoDescriptor.bytesFilled / pullIntoDescriptor.elementSize);469}470function readableStreamFulfillReadIntoRequest(stream, chunk, done)471{472 "use strict";473 stream.@reader.@readIntoRequests.@shift().@resolve.@call(@undefined, {value: chunk, done: done});...
Using AI Code Generation
1test(() => {2 const rs = new ReadableStream({3 pull(controller) {4 controller.close();5 }6 });7 const reader = rs.getReader({ mode: 'byob' });8 const view = new Uint8Array(1);9 return reader.read(view).then(result => {10 assert_true(result.done, 'done');11 assert_equals(result.value, undefined, 'value');12 });13}, 'ReadableByteStreamControllerRespondInReadableState should not throw when called in the readable state');14test(() => {15 const rs = new ReadableStream({16 pull(controller) {17 controller.close();18 }19 });20 const reader = rs.getReader({ mode: 'byob' });21 const view = new Uint8Array(1);22 return reader.read(view).then(result => {23 assert_true(result.done, 'done');24 assert_equals(result.value, undefined, 'value');25 });26}, 'ReadableByteStreamControllerRespondInReadableState should not throw when called in the readable state');27test(() => {28 const rs = new ReadableStream({29 pull(controller) {30 controller.close();31 }32 });33 const reader = rs.getReader({ mode: 'byob' });34 const view = new Uint8Array(1);35 return reader.read(view).then(result => {36 assert_true(result.done, 'done');37 assert_equals(result.value, undefined, 'value');38 });39}, 'ReadableByteStreamControllerRespondInReadableState should not throw when called in the readable state');40test(() => {41 const rs = new ReadableStream({42 pull(controller) {43 controller.close();44 }45 });46 const reader = rs.getReader({ mode: 'byob' });47 const view = new Uint8Array(1);48 return reader.read(view).then(result => {49 assert_true(result.done, 'done');50 assert_equals(result.value, undefined, 'value');51 });52}, 'ReadableByteStreamController
Using AI Code Generation
1test(function() {2 var controller;3 var rs = new ReadableStream({4 start: function(c) {5 controller = c;6 }7 });8 var reader = rs.getReader();9 var readPromise = reader.read();10 assert_true(readPromise instanceof Promise);11 controller.close();12 assert_equals(controller.desiredSize, 0);13 return readPromise.then(function(result) {14 assert_true(result.done);15 assert_equals(result.value, undefined);16 });17}, 'ReadableByteStreamControllerRespondInReadableState should not throw when called in readable state');18test(function() {19 var controller;20 var rs = new ReadableStream({21 start: function(c) {22 controller = c;23 }24 });25 var reader = rs.getReader();26 var readPromise = reader.read();27 assert_true(readPromise instanceof Promise);28 controller.close();29 assert_equals(controller.desiredSize, 0);30 return readPromise.then(function(result) {31 assert_true(result.done);32 assert_equals(result.value, undefined);33 });34}, 'ReadableByteStreamControllerRespondWithNewViewInReadableState should not throw when called in readable state');35test(function() {36 var controller;37 var rs = new ReadableStream({38 start: function(c) {39 controller = c;40 }41 });42 var reader = rs.getReader();43 var readPromise = reader.read();44 assert_true(readPromise instanceof Promise);45 controller.close();46 assert_equals(controller.desiredSize, 0);47 return readPromise.then(function(result) {48 assert_true(result.done);49 assert_equals(result.value, undefined);50 });51}, 'ReadableByteStreamControllerRespondInClosedState should not throw when called in closed state');52test(function() {53 var controller;54 var rs = new ReadableStream({55 start: function(c) {56 controller = c;57 }58 });59 var reader = rs.getReader();60 var readPromise = reader.read();61 assert_true(readPromise instanceof Promise);62 controller.close();63 assert_equals(controller.desiredSize,
Using AI Code Generation
1test(() => {2 const rs = new ReadableStream({3 pull(controller) {4 controller.enqueue(new Uint8Array([0]));5 }6 });7 const reader = rs.getReader({ mode: 'byob' });8 const view = new Uint8Array(1);9 return reader.read(view).then(() => {10 const controller = rs.getReader({ mode: 'byob' }).readableStreamController;11 assert_throws(new TypeError(), () => controller.respond(0));12 });13}, 'ReadableByteStreamControllerRespondInReadableState throws TypeError');14test(() => {15 const rs = new ReadableStream({16 pull(controller) {17 controller.enqueue(new Uint8Array([0]));18 }19 });20 const reader = rs.getReader({ mode: 'byob' });21 const view = new Uint8Array(1);22 return reader.read(view).then(() => {23 const controller = rs.getReader({ mode: 'byob' }).readableStreamController;24 assert_throws(new TypeError(), () => controller.respond(0, new Uint8Array(1)));25 });26}, 'ReadableByteStreamControllerRespondInReadableState throws TypeError');27test(() => {28 const rs = new ReadableStream({29 pull(controller) {30 controller.enqueue(new Uint8Array([0]));31 }32 });33 const reader = rs.getReader({ mode: 'byob' });34 const view = new Uint8Array(1);35 return reader.read(view).then(() => {36 const controller = rs.getReader({ mode: 'byob' }).readableStreamController;37 assert_throws(new TypeError(), () => controller.respondWithNewView(new Uint8Array(1)));38 });39}, 'ReadableByteStreamControllerRespondInReadableState throws TypeError');40test(() => {41 const rs = new ReadableStream({42 pull(controller) {43 controller.enqueue(new Uint8Array([0]));44 }45 });
Using AI Code Generation
1test(() => {2 const controller = new ReadableByteStreamController(new ReadableStream());3 assert_throws_js(TypeError, () => controller.respond(0));4}, 'ReadableByteStreamControllerRespondInReadableState throws a TypeError if called with a controller that is not in the readable state');5test(() => {6 const controller = new ReadableByteStreamController(new ReadableStream());7 assert_throws_js(TypeError, () => controller.respondWithNewView(new Uint8Array()));8}, 'ReadableByteStreamControllerRespondWithNewViewInReadableState throws a TypeError if called with a controller that is not in the readable state');9test(() => {10 const controller = new ReadableByteStreamController(new ReadableStream());11 controller.close();12 assert_throws_js(TypeError, () => controller.respond(0));13}, 'ReadableByteStreamControllerRespondInClosedState throws a TypeError if called with a controller that is not in the readable state');14test(() => {15 const controller = new ReadableByteStreamController(new ReadableStream());16 controller.close();17 assert_throws_js(TypeError, () => controller.respondWithNewView(new Uint8Array()));18}, 'ReadableByteStreamControllerRespondWithNewViewInClosedState throws a TypeError if called with a controller that is not in the readable state');19test(() => {20 const controller = new ReadableByteStreamController(new ReadableStream());21 controller.error();22 assert_throws_js(TypeError, () => controller.respond(0));23}, 'ReadableByteStreamControllerRespondInErroredState throws a TypeError if called with a controller that is not in the readable state');24test(() => {25 const controller = new ReadableByteStreamController(new ReadableStream());26 controller.error();27 assert_throws_js(TypeError, () => controller.respondWithNewView(new Uint8Array()));28},
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue('a');4 controller.close();5 }6});7var [branch1, branch2] = rs.tee();8var reader1 = branch1.getReader();9var reader2 = branch2.getReader();10promise_test(() => {11 return reader1.read().then(result => {12 assert_object_equals(result, { value: 'a', done: false }, 'chunk read');13 });14}, 'read from branch1');15promise_test(() => {16 return reader2.read().then(result => {17 assert_object_equals(result, { value: 'a', done: false }, 'chunk read');18 });19}, 'read from branch2');20var rs = new ReadableStream({21 start(controller) {22 controller.close();23 }24});25var [branch1, branch2] = rs.tee();26var reader1 = branch1.getReader();27var reader2 = branch2.getReader();28promise_test(() => {29 return reader1.read().then(result => {30 assert_object_equals(result, { value: undefined, done: true }, 'closed');31 });32}, 'read from branch1');33promise_test(() => {34 return reader2.read().then(result => {35 assert_object_equals(result, { value: undefined, done: true }, 'closed');36 });37}, 'read from branch2');38var rs = new ReadableStream({39 start(controller) {40 controller.error('boo!');41 }42});43var [branch1, branch2] = rs.tee();44var reader1 = branch1.getReader();45var reader2 = branch2.getReader();46promise_test(() => {47 return promise_rejects_exactly(reader1, new TypeError(), 'read() should reject with a TypeError',48 () => reader1.read());49}, 'read from branch1');50promise_test(() => {51 return promise_rejects_exactly(reader2, new TypeError(), 'read() should reject with a TypeError',52 () => reader2.read());53}, 'read from branch2');
Using AI Code Generation
1function testReadableByteStreamControllerRespondInReadableState() {2 var byobRequest = new ReadableStream({3 }).getReader({4 }).read(new Uint8Array(1));5 var controller = new ReadableStream({6 }).getReader({7 }).read(new Uint8Array(1))._byobRequest._associatedReadableByteStreamController;8 assert_throws_js(TypeError, function() {9 controller.respond(1);10 }, 'ReadableByteStreamControllerRespondInReadableState throws a TypeError exception');11 assert_throws_js(TypeError, function() {12 controller.respond(1, new Uint8Array(1));13 }, 'ReadableByteStreamControllerRespondInReadableState throws a TypeError exception');14 assert_throws_js(TypeError, function() {15 controller.respond(0, new Uint8Array(2));16 }, 'ReadableByteStreamControllerRespondInReadableState throws a TypeError exception');17 assert_throws_js(TypeError, function() {18 controller.respond(0);19 }, 'ReadableByteStreamControllerRespondInReadableState throws a TypeError exception');20}21function testReadableByteStreamControllerRespondWithNewViewInReadableState() {22 var byobRequest = new ReadableStream({23 }).getReader({24 }).read(new Uint8Array(1));25 var controller = new ReadableStream({26 }).getReader({27 }).read(new Uint8Array(1))._byobRequest._associatedReadableByteStreamController;28 assert_throws_js(TypeError, function() {29 controller.respondWithNewView(new Uint8Array(1));30 }, 'ReadableByteStreamControllerRespondWithNewViewInReadableState throws a TypeError exception');31 assert_throws_js(TypeError, function() {32 controller.respondWithNewView(new Uint8Array(2));33 }, 'ReadableByteStreamControllerRespondWithNewViewInReadableState throws a TypeError exception');34}
Using AI Code Generation
1function test()2{3 var rs = new ReadableStream({4 start(controller) {5 controller.enqueue(new Uint8Array([1]));6 controller.close();7 }8 });9 rs.getReader().read().then(result => {10 console.log(result.value);11 console.log(result.done);12 });13}14test();
Using AI Code Generation
1var rs = new ReadableStream({2 start: function(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 }6});7var reader = rs.getReader();8var readPromise = reader.read();9readPromise.then(function(result1) {10 assert_object_equals(result1, {value: 'a', done: false}, 'first read result');11 var readPromise2 = reader.read();12 readPromise2.then(function(result2) {13 assert_object_equals(result2, {value: 'b', done: false}, 'second read result');14 var readPromise3 = reader.read();15 readPromise3.then(function(result3) {16 assert_object_equals(result3, {value: undefined, done: true}, 'third read result');17 }, function() {18 assert_unreached('third read should not reject');19 });20 }, function() {21 assert_unreached('second read should not reject');22 });23}, function() {24 assert_unreached('first read should not reject');25});26var controller = rs._controller;27controller.respond(1);28controller.respond(1);29test(function() {30 assert_throws(new TypeError(), function() {31 controller.respond(1);32 }, 'responding to a readable stream should throw a TypeError');33}, 'responding to a readable stream should throw a TypeError');34test(function() {35 assert_throws(new TypeError(), function() {36 controller.respond(-1);37 }, 'responding to a readable stream with a negative value should throw a TypeError');38}, 'responding to a readable stream with a negative value should throw a TypeError');39test(function() {40 assert_throws(new TypeError(), function() {41 controller.respond(0);42 }, 'responding to a readable stream with a zero value should throw a TypeError');43}, 'responding to a readable stream with a zero value should throw a TypeError');44test(function() {45 assert_throws(new TypeError(), function() {46 controller.respond(2);47 }, 'responding to a readable stream with a value greater than the chunk size should throw a TypeError');48}, 'responding to a readable stream with a value greater than the chunk size should throw a TypeError');49test(function() {50 assert_throws(new TypeError(), function() {51 controller.respond(1.5);52 }, 'responding to a readable stream with a non-integer value
Using AI Code Generation
1var ReadableStream = testharness.ReadableStream;2var ReadableByteStreamController = testharness.ReadableByteStreamController;3var ReadableStreamDefaultController = testharness.ReadableStreamDefaultController;4var WritableStream = testharness.WritableStream;5var WritableStreamDefaultController = testharness.WritableStreamDefaultController;6var ByteLengthQueuingStrategy = testharness.ByteLengthQueuingStrategy;7var CountQueuingStrategy = testharness.CountQueuingStrategy;8var TransformStream = testharness.TransformStream;9var TransformStreamDefaultController = testharness.TransformStreamDefaultController;10var ReadableStreamBYOBRequest = testharness.ReadableStreamBYOBRequest;11var ReadableStreamDefaultReader = testharness.ReadableStreamDefaultReader;12var ReadableStreamBYOBReader = testharness.ReadableStreamBYOBReader;13var WritableStreamDefaultWriter = testharness.WritableStreamDefaultWriter;14var TransformStreamDefaultWriter = testharness.TransformStreamDefaultWriter;15var ReadableByteStreamController = testharness.ReadableByteStreamController;16var ReadableStream = testharness.ReadableStream;17var ReadableByteStreamController = testharness.ReadableByteStreamController;18var ReadableStreamDefaultController = testharness.ReadableStreamDefaultController;19var WritableStream = testharness.WritableStream;
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!!