How to use ReadableByteStreamControllerShiftPendingPullInto method in wpt

Best JavaScript code snippet using wpt

byte-stream-controller.ts

Source:byte-stream-controller.ts Github

copy

Full Screen

...472 return;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 }583 ReadableByteStreamControllerCallPullIfNeeded(controller);584}585function ReadableByteStreamControllerShiftPendingPullInto(controller: ReadableByteStreamController): PullIntoDescriptor {586 const descriptor = controller._pendingPullIntos.shift()!;587 ReadableByteStreamControllerInvalidateBYOBRequest(controller);588 return descriptor;589}590function ReadableByteStreamControllerShouldCallPull(controller: ReadableByteStreamController): boolean {591 const stream = controller._controlledReadableByteStream;592 if (stream._state !== 'readable') {593 return false;594 }595 if (controller._closeRequested) {596 return false;597 }598 if (!controller._started) {599 return false;...

Full Screen

Full Screen

readable_byte_stream_controller.ts

Source:readable_byte_stream_controller.ts Github

copy

Full Screen

...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++) {624 retView[i] = srcView[i];625 }626 return ret;627}628export function ReadableByteStreamControllerRespondInternal(629 controller: ReadableByteStreamController,630 bytesWritten: number631) {632 const firstDescriptor = controller.pendingPullIntos[0];633 const stream = controller.controlledReadableByteStream;634 if (stream.state === "closed") {635 if (bytesWritten !== 0) {636 throw new TypeError();637 }638 ReadableByteStreamControllerRespondInClosedState(639 controller,640 firstDescriptor641 );642 } else {643 Assert(stream.state === "readable");644 ReadableByteStreamControllerRespondInReadableState(645 controller,646 bytesWritten,647 firstDescriptor648 );649 }650 ReadableByteStreamControllerCallPullIfNeeded(controller);651}652export function ReadableByteStreamControllerRespondWithNewView(653 controller: ReadableByteStreamController,654 view655) {656 Assert(controller.pendingPullIntos.length > 0);657 const firstDescriptor = controller.pendingPullIntos[0];658 if (659 firstDescriptor.byteOffset + firstDescriptor.bytesFilled !==660 view.ByteOffset661 ) {662 throw new RangeError();663 }664 if (firstDescriptor.byteLength !== view.ByteLength) {665 throw new RangeError();666 }667 firstDescriptor.buffer = view.ViewedArrayBuffer;668 ReadableByteStreamControllerRespondInternal(controller, view.ByteLength);669}670export function ReadableByteStreamControllerShiftPendingPullInto(671 controller: ReadableByteStreamController672): PullIntoDescriptor {673 const descriptor = controller.pendingPullIntos.shift();674 ReadableByteStreamControllerInvalidateBYOBRequest(controller);675 return descriptor;676}677export function ReadableByteStreamControllerShouldCallPull(678 controller: ReadableByteStreamController679) {680 const stream = controller.controlledReadableByteStream;681 if (stream.state !== "readable") {682 return false;683 }684 if (controller.closeRequested === true) {...

Full Screen

Full Screen

ReadableByteStreamInternals.js

Source:ReadableByteStreamInternals.js Github

copy

Full Screen

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});...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pull: function(c) {3 var byobRequest = c.byobRequest;4 var view = byobRequest.view;5 view[0] = 1;6 view[1] = 2;7 view[2] = 3;8 c.byobRequest.respond(3);9 }10});11var reader = rs.getReader({mode: 'byob'});12var readPromise = reader.read(new Uint8Array(3));13readPromise.then(function(result) {14 assert_array_equals(new Uint8Array([1, 2, 3]), result.value);15 assert_false(result.done);16}).catch(unreached_rejection);17assert_promise_rejects(18 new TypeError(),19 reader.read(new Uint8Array(3)),20 'read() must reject with a TypeError when the stream has been errored');21done();

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pull: function(c) {3 var desc = Object.getOwnPropertyDescriptor(c, 'byobRequest');4 assert_false(desc.enumerable);5 assert_false(desc.configurable);6 assert_true(desc.writable);7 assert_equals(desc.value, null);8 c.enqueue(new Uint8Array([0, 1, 2, 3]));9 }10});11var reader = rs.getReader({mode: 'byob'});12var view = new Uint8Array(4);13var readPromise = reader.read(view).then(function(result) {14 assert_equals(result.value.byteLength, 4);15 assert_equals(result.value[0], 0);16 assert_equals(result.value[1], 1);17 assert_equals(result.value[2], 2);18 assert_equals(result.value[3], 3);19 assert_true(result.done);20}).then(function() {21 var desc = Object.getOwnPropertyDescriptor(c, 'byobRequest');22 assert_false(desc.enumerable);23 assert_false(desc.configurable);24 assert_true(desc.writable);25 assert_equals(desc.value, null);26});27promise_test(function() {28 return readPromise;29}, 'ReadableByteStreamControllerShiftPendingPullInto method of wpt');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ReadableByteStreamController } = require('stream/web');2const controller = new ReadableByteStreamController({3 pull: () => {},4 cancel: () => {},5});6controller.shiftPendingPullInto();7const { ReadableStream, ReadableStreamBYOBReader } = require('stream/web');8const rs = new ReadableStream({9 pull: () => {},10 cancel: () => {},11});12const reader = new ReadableStreamBYOBReader(rs);13reader.read(new Uint8Array(1));14const { ReadableStream, ReadableStreamBYOBReader } = require('stream/web');15const rs = new ReadableStream({16 pull: () => {},17 cancel: () => {},18});19const reader = new ReadableStreamBYOBReader(rs);20const byobRequest = reader.read(new Uint8Array(1));21byobRequest.respond(1);22const { ReadableStream, ReadableStreamBYOBReader } = require('stream/web');23const rs = new ReadableStream({24 pull: () => {},25 cancel: () => {},26});27const reader = new ReadableStreamBYOBReader(rs);28const byobRequest = reader.read(new Uint8Array(1));29byobRequest.respondWithNewView(new Uint8Array(1));30const { ReadableStream, ReadableStreamDefaultController } = require('stream/web');31const rs = new ReadableStream({32 pull: () => {},33 cancel: () => {}34});35const controller = new ReadableStreamDefaultController(rs);36controller.close();37const { ReadableStream, ReadableStreamDefaultController } = require('stream/web');

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pull: function(c) {3 c.enqueue(new Uint8Array([0, 1, 2, 3]));4 c.enqueue(new Uint8Array([4, 5, 6, 7]));5 c.enqueue(new Uint8Array([8, 9, 10, 11]));6 }7});8var reader = rs.getReader();9reader.read().then(function(r) {10 assert_array_equals(r.value, [0, 1, 2, 3], 'first read() result');11 assert_false(r.done, 'first read() done');12 return reader.read();13}).then(function(r) {14 assert_array_equals(r.value, [4, 5, 6, 7], 'second read() result');15 assert_false(r.done, 'second read() done');16 return reader.read();17}).then(function(r) {18 assert_array_equals(r.value, [8, 9, 10, 11], 'third read() result');19 assert_false(r.done, 'third read() done');20});

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful