Best JavaScript code snippet using wpt
byte-stream-controller.ts
Source:byte-stream-controller.ts
...473 }474 const pullIntoDescriptor = controller._pendingPullIntos.peek();475 if (ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor)) {476 ReadableByteStreamControllerShiftPendingPullInto(controller);477 ReadableByteStreamControllerCommitPullIntoDescriptor(478 controller._controlledReadableByteStream,479 pullIntoDescriptor480 );481 }482 }483}484export function ReadableByteStreamControllerPullInto<T extends ArrayBufferView>(485 controller: ReadableByteStreamController,486 view: T,487 readIntoRequest: ReadIntoRequest<T>488): void {489 const stream = controller._controlledReadableByteStream;490 let elementSize = 1;491 if (view.constructor !== DataView) {492 elementSize = (view.constructor as ArrayBufferViewConstructor<T>).BYTES_PER_ELEMENT;493 }494 const ctor = view.constructor as ArrayBufferViewConstructor<T>;495 const buffer = TransferArrayBuffer(view.buffer);496 const pullIntoDescriptor: BYOBPullIntoDescriptor<T> = {497 buffer,498 byteOffset: view.byteOffset,499 byteLength: view.byteLength,500 bytesFilled: 0,501 elementSize,502 viewConstructor: ctor,503 readerType: 'byob'504 };505 if (controller._pendingPullIntos.length > 0) {506 controller._pendingPullIntos.push(pullIntoDescriptor);507 // No ReadableByteStreamControllerCallPullIfNeeded() call since:508 // - No change happens on desiredSize509 // - The source has already been notified of that there's at least 1 pending read(view)510 ReadableStreamAddReadIntoRequest(stream, readIntoRequest);511 return;512 }513 if (stream._state === 'closed') {514 const emptyView = new ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, 0);515 readIntoRequest._closeSteps(emptyView);516 return;517 }518 if (controller._queueTotalSize > 0) {519 if (ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor)) {520 const filledView = ReadableByteStreamControllerConvertPullIntoDescriptor<T>(pullIntoDescriptor);521 ReadableByteStreamControllerHandleQueueDrain(controller);522 readIntoRequest._chunkSteps(filledView);523 return;524 }525 if (controller._closeRequested) {526 const e = new TypeError('Insufficient bytes to fill elements in the given buffer');527 ReadableByteStreamControllerError(controller, e);528 readIntoRequest._errorSteps(e);529 return;530 }531 }532 controller._pendingPullIntos.push(pullIntoDescriptor);533 ReadableStreamAddReadIntoRequest<T>(stream, readIntoRequest);534 ReadableByteStreamControllerCallPullIfNeeded(controller);535}536function ReadableByteStreamControllerRespondInClosedState(controller: ReadableByteStreamController,537 firstDescriptor: PullIntoDescriptor) {538 firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);539 assert(firstDescriptor.bytesFilled === 0);540 const stream = controller._controlledReadableByteStream;541 if (ReadableStreamHasBYOBReader(stream)) {542 while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {543 const pullIntoDescriptor = ReadableByteStreamControllerShiftPendingPullInto(controller);544 ReadableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor);545 }546 }547}548function ReadableByteStreamControllerRespondInReadableState(controller: ReadableByteStreamController,549 bytesWritten: number,550 pullIntoDescriptor: PullIntoDescriptor) {551 if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {552 throw new RangeError('bytesWritten out of range');553 }554 ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);555 if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {556 // TODO: Figure out whether we should detach the buffer or not here.557 return;558 }559 ReadableByteStreamControllerShiftPendingPullInto(controller);560 const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;561 if (remainderSize > 0) {562 const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;563 const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);564 ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);565 }566 pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);567 pullIntoDescriptor.bytesFilled -= remainderSize;568 ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);569 ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);570}571function ReadableByteStreamControllerRespondInternal(controller: ReadableByteStreamController, bytesWritten: number) {572 const firstDescriptor = controller._pendingPullIntos.peek();573 const stream = controller._controlledReadableByteStream;574 if (stream._state === 'closed') {575 if (bytesWritten !== 0) {576 throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');577 }578 ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);579 } else {580 assert(stream._state === 'readable');581 ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);582 }...
readable_byte_stream_controller.ts
Source:readable_byte_stream_controller.ts
...221 }222 ReadableByteStreamControllerClearAlgorithms(controller);223 ReadableStreamClose(stream);224}225export function ReadableByteStreamControllerCommitPullIntoDescriptor(226 stream: ReadableStream,227 pullIntoDescriptor: PullIntoDescriptor228) {229 Assert(stream.state !== "errored");230 let done = false;231 if (stream.state === "closed") {232 Assert(pullIntoDescriptor.bytesFilled === 0);233 done = true;234 }235 const filledView = ReadableByteStreamControllerConvertPullIntoDescriptor(236 pullIntoDescriptor237 );238 if (pullIntoDescriptor.readerType === "default") {239 ReadableStreamFulfillReadRequest(stream, filledView, done);240 } else {241 Assert(pullIntoDescriptor.readerType === "byob");242 ReadableStreamFulfillReadIntoRequest(stream, filledView, done);243 }244}245export function ReadableByteStreamControllerConvertPullIntoDescriptor(246 pullIntoDescriptor: PullIntoDescriptor247) {248 const { bytesFilled, elementSize } = pullIntoDescriptor;249 Assert(bytesFilled <= pullIntoDescriptor.byteLength);250 Assert(bytesFilled % pullIntoDescriptor.elementSize === 0);251 return new pullIntoDescriptor.ctor(252 pullIntoDescriptor.buffer,253 pullIntoDescriptor.byteOffset,254 bytesFilled / elementSize255 );256}257export function ReadableByteStreamControllerEnqueue(258 controller: ReadableByteStreamController,259 chunk: ArrayBufferView260) {261 const stream = controller.controlledReadableByteStream;262 Assert(controller.closeRequested === false);263 Assert(stream.state === "readable");264 const { buffer } = chunk;265 const { byteOffset, byteLength } = chunk;266 const transferredBuffer = TransferArrayBuffer(buffer);267 if (ReadableStreamHasDefaultReader(stream)) {268 if (ReadableStreamGetNumReadRequests(stream) === 0) {269 ReadableByteStreamControllerEnqueueChunkToQueue(270 controller,271 transferredBuffer,272 byteOffset,273 byteLength274 );275 } else {276 Assert(controller.queue.length === 0, "l=0");277 const transferredView = new Uint8Array(278 transferredBuffer,279 byteOffset,280 byteLength281 );282 ReadableStreamFulfillReadRequest(stream, transferredView, false);283 }284 } else if (ReadableStreamHasBYOBReader(stream)) {285 ReadableByteStreamControllerEnqueueChunkToQueue(286 controller,287 transferredBuffer,288 byteOffset,289 byteLength290 );291 ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(292 controller293 );294 } else {295 Assert(296 IsReadableStreamLocked(stream) === false,297 "stream should not be locked"298 );299 ReadableByteStreamControllerEnqueueChunkToQueue(300 controller,301 transferredBuffer,302 byteOffset,303 byteLength304 );305 }306 ReadableByteStreamControllerCallPullIfNeeded(controller);307}308export function ReadableByteStreamControllerEnqueueChunkToQueue(309 controller: ReadableByteStreamController,310 buffer: ArrayBuffer,311 byteOffset: number,312 byteLength: number313) {314 controller.queue.push({315 buffer,316 byteOffset,317 byteLength318 });319 controller.queueTotalSize += byteLength;320}321export function ReadableByteStreamControllerError(322 controller: ReadableByteStreamController,323 e324) {325 const stream = controller.controlledReadableByteStream;326 if (stream.state !== "readable") {327 return;328 }329 ReadableByteStreamControllerClearPendingPullIntos(controller);330 ResetQueue(controller);331 ReadableByteStreamControllerClearAlgorithms(controller);332 ReadableStreamError(controller.controlledReadableByteStream, e);333}334export function ReadableByteStreamControllerFillHeadPullIntoDescriptor(335 controller: ReadableByteStreamController,336 size: number,337 pullIntoDescriptor: PullIntoDescriptor338) {339 Assert(340 controller.pendingPullIntos.length === 0 ||341 controller.pendingPullIntos[0] === pullIntoDescriptor342 );343 ReadableByteStreamControllerInvalidateBYOBRequest(controller);344 pullIntoDescriptor.bytesFilled += size;345}346export function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(347 controller: ReadableByteStreamController,348 pullIntoDescriptor: PullIntoDescriptor349): boolean {350 const { elementSize } = pullIntoDescriptor;351 const currentAlignedBytes =352 pullIntoDescriptor.bytesFilled -353 (pullIntoDescriptor.bytesFilled % elementSize);354 const maxBytesToCopy = Math.min(355 controller.queueTotalSize,356 pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled357 );358 const maxBytesFilled = pullIntoDescriptor.bytesFilled + maxBytesToCopy;359 const maxAlignedBytes = maxBytesFilled - (maxBytesFilled % elementSize);360 let totalBytesToCopyRemaining = maxBytesToCopy;361 let ready = false;362 if (maxAlignedBytes > currentAlignedBytes) {363 totalBytesToCopyRemaining =364 maxAlignedBytes - pullIntoDescriptor.bytesFilled;365 ready = true;366 }367 const { queue } = controller;368 while (totalBytesToCopyRemaining > 0) {369 const headOfQueue = queue[0];370 const bytesToCopy = Math.min(371 totalBytesToCopyRemaining,372 headOfQueue.byteLength373 );374 const destStart =375 pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;376 const srcView = new Uint8Array(377 headOfQueue.buffer,378 headOfQueue.byteOffset,379 headOfQueue.byteLength380 );381 const destView = new Uint8Array(382 pullIntoDescriptor.buffer,383 destStart,384 bytesToCopy385 );386 for (let i = 0; i < bytesToCopy; i++) {387 destView[i] = srcView[i];388 }389 if (headOfQueue.byteLength === bytesToCopy) {390 queue.shift();391 } else {392 headOfQueue.byteOffset += bytesToCopy;393 headOfQueue.byteLength -= bytesToCopy;394 }395 controller.queueTotalSize -= bytesToCopy;396 ReadableByteStreamControllerFillHeadPullIntoDescriptor(397 controller,398 bytesToCopy,399 pullIntoDescriptor400 );401 totalBytesToCopyRemaining -= bytesToCopy;402 }403 if (ready === false) {404 Assert(controller.queueTotalSize === 0);405 Assert(pullIntoDescriptor.bytesFilled > 0);406 Assert(pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize);407 }408 return ready;409}410export function ReadableByteStreamControllerGetDesiredSize(411 controller: ReadableByteStreamController412): number | null {413 const stream = controller.controlledReadableByteStream;414 const { state } = stream;415 if (state === "errored") {416 return null;417 }418 if (state === "closed") {419 return 0;420 }421 return controller.strategyHWM - controller.queueTotalSize;422}423export function ReadableByteStreamControllerHandleQueueDrain(424 controller: ReadableByteStreamController425) {426 Assert(controller.controlledReadableByteStream.state === "readable");427 if (controller.queueTotalSize === 0 && controller.closeRequested) {428 ReadableByteStreamControllerClearAlgorithms(controller);429 ReadableStreamClose(controller.controlledReadableByteStream);430 } else {431 ReadableByteStreamControllerCallPullIfNeeded(controller);432 }433}434export function ReadableByteStreamControllerInvalidateBYOBRequest(435 controller: ReadableByteStreamController436) {437 if (controller._byobRequest === void 0) {438 return;439 }440 controller._byobRequest.associatedReadableByteStreamController = void 0;441 controller._byobRequest._view = void 0;442 controller._byobRequest = void 0;443}444export function ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(445 controller: ReadableByteStreamController446) {447 Assert(controller.closeRequested === false);448 while (controller.pendingPullIntos.length > 0) {449 if (controller.queueTotalSize === 0) {450 return;451 }452 const pullIntoDescriptor = controller.pendingPullIntos[0];453 if (454 ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(455 controller,456 pullIntoDescriptor457 ) === true458 ) {459 ReadableByteStreamControllerShiftPendingPullInto(controller);460 ReadableByteStreamControllerCommitPullIntoDescriptor(461 controller.controlledReadableByteStream,462 pullIntoDescriptor463 );464 }465 }466}467const TypedArraySizeMap = {468 Int8Array: [1, Int8Array],469 Uint8Array: [1, Uint8Array],470 Uint8ClampedArray: [1, Uint8ClampedArray],471 Int16Array: [2, Int16Array],472 Uint16Array: [2, Uint16Array],473 Int32Array: [4, Int32Array],474 Uint32Array: [4, Uint32Array],475 Float32Array: [4, Float32Array],476 Float64Array: [8, Float64Array]477};478export function ReadableByteStreamControllerPullInto(479 controller: ReadableByteStreamController,480 view: ArrayBufferView,481 forAuthorCode?: boolean482): Promise<any> {483 const stream = controller.controlledReadableByteStream;484 let elementSize = 1;485 let ctor = DataView;486 const ctorName = view.constructor.name;487 if (TypedArraySizeMap[ctorName]) {488 [elementSize, ctor] = TypedArraySizeMap[ctorName];489 }490 const { byteOffset, byteLength } = view;491 const buffer = TransferArrayBuffer(view.buffer);492 const pullIntoDescriptor: PullIntoDescriptor = {493 buffer,494 byteOffset,495 byteLength,496 bytesFilled: 0,497 elementSize,498 ctor,499 readerType: "byob"500 };501 if (controller.pendingPullIntos.length > 0) {502 controller.pendingPullIntos.push(pullIntoDescriptor);503 return ReadableStreamAddReadIntoRequest(stream, forAuthorCode);504 }505 if (stream.state === "closed") {506 const emptyView = new ctor(507 pullIntoDescriptor.buffer,508 pullIntoDescriptor.byteOffset,509 0510 );511 return Promise.resolve(512 ReadableStreamCreateReadResult(emptyView, true, forAuthorCode)513 );514 }515 if (controller.queueTotalSize > 0) {516 if (517 ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(518 controller,519 pullIntoDescriptor520 )521 ) {522 const filedView = ReadableByteStreamControllerConvertPullIntoDescriptor(523 pullIntoDescriptor524 );525 ReadableByteStreamControllerHandleQueueDrain(controller);526 return Promise.resolve(527 ReadableStreamCreateReadResult(filedView, false, forAuthorCode)528 );529 }530 if (controller.closeRequested) {531 const e = new TypeError();532 ReadableByteStreamControllerError(controller, e);533 return Promise.reject(e);534 }535 }536 controller.pendingPullIntos.push(pullIntoDescriptor);537 const promise = ReadableStreamAddReadIntoRequest(stream, forAuthorCode);538 ReadableByteStreamControllerCallPullIfNeeded(controller);539 return promise;540}541export function ReadableByteStreamControllerRespond(542 controller: ReadableByteStreamController,543 bytesWritten: number544): void {545 if (IsFiniteNonNegativeNumber(bytesWritten) === false) {546 throw new RangeError();547 }548 Assert(controller.pendingPullIntos.length > 0);549 ReadableByteStreamControllerRespondInternal(controller, bytesWritten);550}551export function ReadableByteStreamControllerRespondInClosedState(552 controller: ReadableByteStreamController,553 firstDescriptor: PullIntoDescriptor554) {555 firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);556 Assert(firstDescriptor.bytesFilled === 0);557 const stream = controller.controlledReadableByteStream;558 if (ReadableStreamHasBYOBReader(stream)) {559 while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {560 const pullIntoDescriptor = ReadableByteStreamControllerShiftPendingPullInto(561 controller562 );563 ReadableByteStreamControllerCommitPullIntoDescriptor(564 stream,565 pullIntoDescriptor566 );567 }568 }569}570export function ReadableByteStreamControllerRespondInReadableState(571 controller: ReadableByteStreamController,572 bytesWritten: number,573 pullIntoDescriptor: PullIntoDescriptor574) {575 if (576 pullIntoDescriptor.bytesFilled + bytesWritten >577 pullIntoDescriptor.byteLength578 ) {579 throw new RangeError();580 }581 ReadableByteStreamControllerFillHeadPullIntoDescriptor(582 controller,583 bytesWritten,584 pullIntoDescriptor585 );586 if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {587 return;588 }589 ReadableByteStreamControllerShiftPendingPullInto(controller);590 const remainderSize =591 pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;592 if (remainderSize > 0) {593 const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;594 const remainder = CloneArrayBuffer(595 pullIntoDescriptor.buffer,596 end - remainderSize,597 remainderSize598 );599 ReadableByteStreamControllerEnqueueChunkToQueue(600 controller,601 remainder,602 0,603 remainder.byteLength604 );605 }606 pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);607 pullIntoDescriptor.bytesFilled =608 pullIntoDescriptor.bytesFilled - remainderSize;609 ReadableByteStreamControllerCommitPullIntoDescriptor(610 controller.controlledReadableByteStream,611 pullIntoDescriptor612 );613 ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);614}615function CloneArrayBuffer(616 srcBuffer: ArrayBuffer,617 srcByteOffset: number,618 srcLength: number619): ArrayBuffer {620 const ret = new ArrayBuffer(srcLength);621 const retView = new DataView(ret);622 const srcView = new DataView(srcBuffer, srcByteOffset, srcLength);623 for (let i = 0; i < srcLength; i++) {...
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
1import { ReadableByteStreamController } from 'streams/readable-byte-stream-controller';2import { ReadableStream } from 'streams/readable-stream';3import { ReadableStreamBYOBRequest } from 'streams/readable-stream-byob-request';4import { ReadableStreamDefaultController } from 'streams/readable-stream-default-controller';5import { ReadableStreamDefaultReader } from 'streams/readable-stream-default-reader';6import { ReadableStreamBYOBReader } from 'streams/readable-stream-byob-reader';7import { WritableStream } from 'streams/writable-stream';8import { WritableStreamDefaultController } from 'streams/writable-stream-default-controller';9import { WritableStreamDefaultWriter } from 'streams/writable-stream-default-writer';10import { TransformStream } from 'streams/transform-stream';11import { TransformStreamDefaultController } from 'streams/transform-stream-default-controller';12import { ByteLengthQueuingStrategy } from 'streams/queuing-strategies';13import { CountQueuingStrategy } from 'streams/queuing-strategies';14import { assert } from 'streams/test-utils/assert';15import { typeIsObject } from 'streams/test-utils/type-is-object';16import { DequeueValue, EnqueueValueWithSize, PeekQueueValue, ResetQueue } from 'streams/test-utils/readable-streams';17import { DequeueValue as DequeueValueFromWritableStream, EnqueueValueWithSize as EnqueueValueWithSizeIntoWritableStream, PeekQueueValue as PeekQueueValueFromWritableStream, ResetQueue as ResetQueueInWritableStream } from 'streams/test-utils/writable-streams';18import { CallOrNoop, PromiseInvokeOrNoop, PromiseInvokeOrPerformFallback, PromiseInvokeOrNoopWithThis, PromiseInvokeOrNoopWithFirstArgument, PromiseInvokeOrNoopWithFirstArgumentAndSecondArgument, PromiseInvokeOrNoopWithFirstArgumentAndThirdArgument, PromiseInvokeOrNoopWithFirstArgumentAndFourthArgument, PromiseInvokeOrNoopWithFirstArgumentAndRest, PromiseInvokeOrNoopWithFirstArgumentAndRestAndThis, PromiseInvokeOrNoopWithFirstArgumentAndRestAndThisAndFirstArgument, PromiseInvokeOrNoopWithFirstArgumentAndRestAndThisAndFirstArgumentAndSecondArgument, PromiseInvokeOrNoopWithFirstArgumentAndRestAndThisAndFirstArgumentAndSecondArgumentAndThirdArgument, PromiseInvokeOrNoopWithFirstArgumentAndRestAndThisAndFirstArgumentAndSecondArgumentAndThirdArgument
Using AI Code Generation
1var pullCount = 0;2var rs = new ReadableStream({3 pull(controller) {4 ++pullCount;5 controller.enqueue('a');6 controller.enqueue('b');7 controller.enqueue('c');8 }9});10var reader = rs.getReader();11var byobRequest = reader.read(new Uint8Array(3));12assert_equals(pullCount, 1, 'pull() should be called once');13var result = byobRequest.value;14assert_equals(result.byteLength, 3, 'result should have length 3');15assert_equals(result[0], 'a'.charCodeAt(0), 'result[0] should be "a"');16assert_equals(result[1], 'b'.charCodeAt(0), 'result[1] should be "b"');17assert_equals(result[2], 'c'.charCodeAt(0), 'result[2] should be "c"');18var pullCount = 0;19var rs = new ReadableStream({20 pull(controller) {21 ++pullCount;22 controller.enqueue('a');23 controller.enqueue('b');24 controller.enqueue('c');25 }26});27var reader = rs.getReader();28var byobRequest = reader.read(new Uint8Array(3));29assert_equals(pullCount, 1, 'pull() should be called once');30var result = byobRequest.value;31assert_equals(result.byteLength, 3, 'result should have length 3');32assert_equals(result[0], 'a'.charCodeAt(0), 'result[0] should be "a"');33assert_equals(result[1], 'b'.charCodeAt(0), 'result[1] should be "b"');34assert_equals(result[2], 'c'.charCodeAt(0), 'result[2] should be "c"');35var pullCount = 0;36var rs = new ReadableStream({37 pull(controller) {38 ++pullCount;39 controller.enqueue('a');40 controller.enqueue('b');41 controller.enqueue('c');42 }43});44var reader = rs.getReader();45var byobRequest = reader.read(new Uint8Array(3));46assert_equals(pullCount, 1, 'pull() should be called once');47var result = byobRequest.value;48assert_equals(result.byteLength, 3, 'result should have length 3');49assert_equals(result[
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(controller) {3 var buffer = new ArrayBuffer(4);4 var view = new Uint8Array(buffer);5 view[0] = 0x61;6 view[1] = 0x62;7 view[2] = 0x63;8 view[3] = 0x64;9 var descriptor = controller.byobRequest.desiredSize;10 var pullIntoDescriptor = ReadableByteStreamControllerCommitPullIntoDescriptor(controller, descriptor);11 pullIntoDescriptor.bytesFilled = 4;12 pullIntoDescriptor.buffer = buffer;13 controller.byobRequest.respondWithNewView(view);14 }15});16var reader = rs.getReader({mode: 'byob'});17var result = reader.read(new Uint8Array(4));18result.then(function(result) {19 assert_array_equals(result.value, [0x61, 0x62, 0x63, 0x64]);20 assert_false(result.done);21});22var rs = new ReadableStream({23 pull: function(controller) {24 var buffer = new ArrayBuffer(4);25 var view = new Uint8Array(buffer);26 view[0] = 0x61;27 view[1] = 0x62;28 view[2] = 0x63;29 view[3] = 0x64;30 controller.byobRequest.respondWithNewView(view);31 }32});33var reader = rs.getReader({mode: 'byob'});34var result = reader.read(new Uint8Array(4));35result.then(function(result) {36 assert_array_equals(result.value, [0x61, 0x62, 0x63, 0x64]);37 assert_false(result.done);38});39var rs = new ReadableStream({40 pull: function(controller) {41 var buffer = new ArrayBuffer(4);
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(controller) {3 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));4 }5});6var reader = rs.getReader({mode: 'byob'});7var view = new Uint8Array(3);8reader.read(view).then(function(result) {9 assert_array_equals(result.value, new Uint8Array([0x01, 0x02, 0x03]));10 assert_false(result.done);11 assert_equals(view[0], 0x01);12 assert_equals(view[1], 0x02);13 assert_equals(view[2], 0x03);14 return reader.read(view);15}).then(function(result) {16 assert_equals(result.value, undefined);17 assert_true(result.done);18});19var rs = new ReadableStream({20 pull: function(controller) {21 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));22 }23});24var reader = rs.getReader({mode: 'byob'});25var view = new Uint8Array(3);26reader.read(view).then(function(result) {27 assert_array_equals(result.value, new Uint8Array([0x01, 0x02, 0x03]));28 assert_false(result.done);29 assert_equals(view[0], 0x01);30 assert_equals(view[1], 0x02);31 assert_equals(view[2], 0x03);32 return reader.read(view);33}).then(function(result) {34 assert_equals(result.value, undefined);35 assert_true(result.done);36});37var rs = new ReadableStream({38 pull: function(controller) {39 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03
Using AI Code Generation
1var pullIntoDescriptor = {2 buffer: ArrayBuffer(12),3};4ReadableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor);5assert_equals(pullIntoDescriptor.bytesFilled, 0);6var pullIntoDescriptor = {7 buffer: ArrayBuffer(12),8};9ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(stream, pullIntoDescriptor);10assert_equals(pullIntoDescriptor.bytesFilled, 0);11var firstDescriptor = {12 buffer: ArrayBuffer(12),13};14var secondDescriptor = {15 buffer: ArrayBuffer(12),16};17ReadableByteStreamControllerRespondInClosedState(stream, firstDescriptor, secondDescriptor);18assert_equals(firstDescriptor.bytesFilled, 0);19assert_equals(secondDescriptor.bytesFilled, 0);20var firstDescriptor = {21 buffer: ArrayBuffer(12),
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!!