How to use ReadableByteStreamControllerCallPullIfNeeded method in wpt

Best JavaScript code snippet using wpt

readable_byte_stream_controller.ts

Source:readable_byte_stream_controller.ts Github

copy

Full Screen

...151 };152 this.pendingPullIntos.push(pullIntoDescriptor);153 }154 const promise = ReadableStreamAddReadRequest(stream, forAuthorCode);155 ReadableByteStreamControllerCallPullIfNeeded(this);156 return promise;157 }158}159export function IsReadableByteStreamController(160 x161): x is ReadableByteStreamController {162 return (163 typeof x === "object" && x.hasOwnProperty("controlledReadableByteStream")164 );165}166export function ReadableByteStreamControllerCallPullIfNeeded(167 controller: ReadableByteStreamController168) {169 const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);170 if (!shouldPull) {171 return;172 }173 if (controller.pulling) {174 controller.pullAgain = true;175 return;176 }177 Assert(!controller.pullAgain);178 controller.pulling = true;179 controller180 .pullAlgorithm()181 .then(() => {182 controller.pulling = false;183 if (controller.pullAgain) {184 controller.pullAgain = false;185 ReadableByteStreamControllerCallPullIfNeeded(controller);186 }187 })188 .catch(r => {189 ReadableByteStreamControllerError(controller, r);190 });191}192export function ReadableByteStreamControllerClearAlgorithms(193 controller: ReadableByteStreamController194) {195 controller.pullAlgorithm = void 0;196 controller.cancelAlgorithm = void 0;197}198export function ReadableByteStreamControllerClearPendingPullIntos(199 controller: ReadableByteStreamController200) {201 ReadableByteStreamControllerInvalidateBYOBRequest(controller);202 controller.pendingPullIntos = [];203}204export function ReadableByteStreamControllerClose(205 controller: ReadableByteStreamController206) {207 const stream = controller.controlledReadableByteStream;208 Assert(controller.closeRequested === false);209 Assert(stream.state === "readable");210 if (controller.queueTotalSize > 0) {211 controller.closeRequested = true;212 return;213 }214 if (controller.pendingPullIntos.length > 0) {215 const firstPengingPullInfo = controller.pendingPullIntos[0];216 if (firstPengingPullInfo.bytesFilled > 0) {217 const e = new TypeError();218 ReadableByteStreamControllerError(controller, e);219 throw e;220 }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++) {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) {685 return false;686 }687 if (controller.started === false) {688 return false;689 }690 if (691 ReadableStreamHasDefaultReader(stream) &&692 ReadableStreamGetNumReadRequests(stream) > 0693 ) {694 return true;695 }696 if (697 ReadableStreamHasBYOBReader(stream) &&698 ReadableStreamGetNumReadIntoRequests(stream) > 0699 ) {700 return true;701 }702 const desiredSize = ReadableByteStreamControllerGetDesiredSize(controller);703 Assert(desiredSize !== null);704 if (desiredSize > 0) {705 return true;706 }707 return false;708}709export function SetUpReadableByteStreamController(710 stream: ReadableStream,711 controller: ReadableByteStreamController,712 startAlgorithm: StartAlgorithm,713 pullAlgorithm: PullAlgorithm,714 cancelAlgorithm: CancelAlgorithm,715 highWaterMark: number,716 autoAllocateChunkSize: number717) {718 Assert(stream.readableStreamController === void 0);719 if (autoAllocateChunkSize !== void 0) {720 Assert(Number.isInteger(autoAllocateChunkSize));721 Assert(autoAllocateChunkSize > 0);722 }723 controller.controlledReadableByteStream = stream;724 controller.pullAgain = false;725 controller.pulling = false;726 ReadableByteStreamControllerClearPendingPullIntos(controller);727 ResetQueue(controller);728 controller.closeRequested = false;729 controller.started = false;730 controller.strategyHWM = ValidateAndNormalizeHighWaterMark(highWaterMark);731 controller.pullAlgorithm = pullAlgorithm;732 controller.cancelAlgorithm = cancelAlgorithm;733 controller.autoAllocateChunkSize = autoAllocateChunkSize;734 controller.pendingPullIntos = [];735 stream.readableStreamController = controller;736 Promise.resolve(startAlgorithm())737 .then(() => {738 controller.started = true;739 Assert(!controller.pulling);740 Assert(!controller.pullAgain);741 ReadableByteStreamControllerCallPullIfNeeded(controller);742 })743 .catch(r => {744 ReadableByteStreamControllerError(controller, r);745 });746}747export function SetUpReadableByteStreamControllerFromUnderlyingSource<T>(748 stream: ReadableStream,749 underlyingByteSource: UnderlyingSource,750 highWaterMark: number751) {752 Assert(underlyingByteSource !== void 0);753 const controller = Object.create(ReadableByteStreamController.prototype);754 const startAlgorithm = () =>755 InvokeOrNoop(underlyingByteSource, "start", controller);...

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

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1function ReadableByteStreamControllerCallPullIfNeeded(controller) {2 const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);3 if (!shouldPull) {4 return;5 }6 if (controller._pulling) {7 controller._pullAgain = true;8 return;9 }10 assert(!controller._controller._closeRequested);11 assert(!controller._controller._started);12 controller._pulling = true;13 const pullPromise = PromiseInvokeOrNoop(controller._underlyingByteSource, 'pull', [controller]);14 pullPromise.then(15 () => {16 controller._pulling = false;17 if (controller._pullAgain) {18 controller._pullAgain = false;19 ReadableByteStreamControllerCallPullIfNeeded(controller);20 }21 },22 e => {23 ReadableByteStreamControllerError(controller, e);24 }25 ).catch(rethrowAssertionErrorRejection);26}27function ReadableByteStreamControllerShouldCallPull(controller) {28 const stream = controller._controlledReadableByteStream;29 if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller._controller)) {30 return false;31 }32 if (controller._closeRequested) {33 return false;34 }35 const desiredSize = ReadableByteStreamControllerGetDesiredSize(controller);36 assert(desiredSize !== null);37 if (desiredSize > 0) {38 return true;39 }40 return false;41}42function ReadableByteStreamControllerGetDesiredSize(controller) {43 const state = controller._controlledReadableByteStream._state;44 if (state === 'errored') {45 return null;46 }47 if (state === 'closed') {48 return 0;49 }50 return controller._strategyHWM - controller._queueTotalSize;51}52function ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) {53 const state = controller._controlledReadableStream._state;54 if (!controller._closeRequested && state === 'readable') {55 return true;56 }57 return false;58}59function ReadableStreamDefaultControllerEnqueue(controller, chunk) {60 const stream = controller._controlledReadableStream;61 assert(ReadableStreamDefaultControllerCanCloseOrEnqueue(controller));62 const chunkSize = 1;63 assert(typeof chunkSize === 'number');64 if (IsReadableStreamLocked(stream) && ReadableStreamGetNumReadRequests(stream) > 0) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue(new Uint8Array([1, 2, 3]));4 controller.close();5 }6});7var reader = rs.getReader();8var readPromise = reader.read();9readPromise.then(function(result) {10 assert_array_equals(result.value, new Uint8Array([1, 2, 3]), 'read() should fulfill with the chunk enqueued in start()');11 assert_true(result.done, 'read() should fulfill with done=true');12}, function(e) {13 assert_unreached('read() should fulfill, not reject');14});15var pullCount = 0;16var rs = new ReadableStream({17 pull(controller) {18 ++pullCount;19 controller.enqueue(new Uint8Array([4, 5, 6]));20 controller.close();21 }22});23var reader = rs.getReader();24var readPromise = reader.read();25readPromise.then(function(result) {26 assert_array_equals(result.value, new Uint8Array([4, 5, 6]), 'read() should fulfill with the chunk enqueued in the first pull()');27 assert_true(result.done, 'read() should fulfill with done=true');28}, function(e) {29 assert_unreached('read() should fulfill, not reject');30});31assert_equals(pullCount, 1, 'pull() should have been called once');32var pullCount = 0;33var rs = new ReadableStream({34 pull(controller) {35 ++pullCount;36 controller.enqueue(new Uint8Array([7, 8, 9]));37 }38});39var reader = rs.getReader();40var readPromise = reader.read();41readPromise.then(function(result) {42 assert_array_equals(result.value, new Uint8Array([7, 8, 9]), 'read() should fulfill with the chunk enqueued in the first pull()');43 assert_false(result.done, 'read() should fulfill with done=false');44}, function(e) {45 assert_unreached('read() should fulfill, not reject');46});47readPromise = reader.read();48readPromise.then(function(result) {49 assert_equals(result.value, undefined, 'read() should fulfill with undefined');50 assert_true(result.done, 'read() should fulfill with done=true');51}, function(e) {52 assert_unreached('read() should fulfill,

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pull: function(controller) {3 controller.enqueue(new Uint8Array([0]));4 controller.close();5 }6});7var reader = rs.getReader();8var readPromise = reader.read();9readPromise.then(function(result) {10 assert_equals(result.value.length, 1, 'chunk size should be 1');11 assert_false(result.done, 'done should be false');12 return readPromise;13}).then(function(result) {14 assert_equals(result.value, undefined, 'chunk should be undefined');15 assert_true(result.done, 'done should be true');16}).then($DONE, $DONE);17var rs = new ReadableStream({18 pull: function(controller) {19 controller.enqueue(new Uint8Array([0]));20 controller.close();21 }22});23var reader = rs.getReader({ mode: 'byob' });24var view = new Uint8Array(1);25var readPromise = reader.read(view);26readPromise.then(function(result) {27 assert_equals(result.value, view, 'result.value should be view');28 assert_false(result.done, 'done should be false');29 return readPromise;30}).then(function(result) {31 assert_equals(result.value, undefined, 'chunk should be undefined');32 assert_true(result.done, 'done should be true');33}).then($DONE, $DONE);34var rs = new ReadableStream({35 pull: function(controller) {36 var view = controller.byobRequest.view;37 view[0] = 0;38 controller.byobRequest.respond(1);39 }40});41var reader = rs.getReader({ mode: 'byob' });42var view = new Uint8Array(1);43var readPromise = reader.read(view);44readPromise.then(function(result) {45 assert_equals(result.value, view, 'result.value should be view');46 assert_false(result.done, 'done should be

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ReadableStream, CountQueuingStrategy } = require('stream/web');2const rs = new ReadableStream({3 start(controller) {4 },5 pull(controller) {6 },7 cancel(reason) {8 }9}, new CountQueuingStrategy({ highWaterMark: 0 }));10rs.cancel();

Full Screen

Using AI Code Generation

copy

Full Screen

1import { ReadableByteStreamControllerCallPullIfNeeded } from2'./readable-streams/underlying-byte-source.js';3import { ReadableStream } from './readable-streams/readable-stream.js';4import { ReadableStreamBYOBReader } from5'./readable-streams/readable-stream-byob-reader.js';6import { ReadableStreamDefaultControllerClose,7ReadableStreamDefaultControllerError } from8'./readable-streams/readable-stream-default-controller.js';9import { ReadableStreamDefaultReader } from10'./readable-streams/readable-stream-default-reader.js';11import { ReadableStreamBYOBRequest } from12'./readable-streams/readable-stream-byob-request.js';13import { ReadableStreamDefaultController } from14'./readable-streams/readable-stream-default-controller.js';15const stream = new ReadableStream({16 start(controller) {17 ReadableByteStreamControllerCallPullIfNeeded(controller);18 },19 pull(controller) {20 const chunk = new Uint8Array([0x01, 0x02, 0x03]);21 ReadableStreamDefaultControllerEnqueue(controller, chunk);22 },23});24const reader = stream.getReader({ mode: 'byob' });25const view = new Uint8Array(3);26reader.read(view).then(({ value, done }) => {27 console.log(value);28 console.log(done);29});30const stream2 = new ReadableStream({31 start(controller) {32 ReadableByteStreamControllerCallPullIfNeeded(controller);33 },34 pull(controller) {35 const chunk = new Uint8Array([0x01, 0x02, 0x03]);36 ReadableStreamDefaultControllerEnqueue(controller, chunk);37 },38});39const reader2 = stream2.getReader({ mode: 'byob' });

Full Screen

Using AI Code Generation

copy

Full Screen

1 var rs = new ReadableStream({2 pull: function(controller) {3 var view = new Uint8Array(1);4 view[0] = 1;5 controller.enqueue(view);6 }7 });8 rs.getReader().read().then(function(result) {9 assert_equals(result.value[0], 1);10 });11 function ReadableByteStreamControllerCallPullIfNeeded(controller) {12 var shouldPull = ReadableByteStreamControllerShouldCallPull(controller);13 if (!shouldPull) {14 return;15 }16 if (controller._pulling) {17 controller._pullAgain = true;18 return;19 }20 controller._pulling = true;21 PromiseInvokeOrNoop(controller._underlyingByteSource, 'pull',22 [controller]).then(function() {23 controller._pulling = false;24 if (controller._pullAgain) {25 controller._pullAgain = false;26 ReadableByteStreamControllerCallPullIfNeeded(controller);27 }28 }).catch(function(e) {29 ReadableByteStreamControllerError(controller, e);30 });31 }32 function ReadableByteStreamControllerShouldCallPull(controller) {33 var stream = controller._controlledReadableByteStream;34 if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {35 return false;36 }37 if (controller._closeRequested) {38 return false;39 }40 if (!controller._started) {41 return false;42 }43 if (

Full Screen

Using AI Code Generation

copy

Full Screen

1const rs = new ReadableStream({2 pull(c) {3 assert_true(c.byobRequest === null);4 assert_true(c.desiredSize > 0);5 assert_true(c.pullAgain);6 assert_false(c.pullAgain);7 c.enqueue(new Uint8Array([1, 2, 3]));8 }9});10const reader = rs.getReader({ mode: 'byob' });11assert_false(reader._readableStreamController.pullAgain);12ReadableByteStreamControllerCallPullIfNeeded(13 reader._readableStreamController);14assert_false(reader._readableStreamController.pullAgain);15assert_false(reader._readableStreamController.pullAgain);16ReadableByteStreamControllerCallPullIfNeeded(17 reader._readableStreamController);18assert_false(reader._readableStreamController.pullAgain);19ReadableByteStreamControllerCallPullIfNeeded(20 reader._readableStreamController);

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