How to use ReadableByteStreamControllerPullInto method in wpt

Best JavaScript code snippet using wpt

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(STREAMS_API)26// @internal27function privateInitializeReadableStreamBYOBReader(stream)28{29 "use strict";30 if (!@isReadableStream(stream))31 @throwTypeError("ReadableStreamBYOBReader needs a ReadableStream");32 if (!@isReadableByteStreamController(stream.@readableStreamController))33 @throwTypeError("ReadableStreamBYOBReader needs a ReadableByteStreamController");34 if (@isReadableStreamLocked(stream))35 @throwTypeError("ReadableStream is locked");36 @readableStreamReaderGenericInitialize(this, stream);37 this.@readIntoRequests = [];38 return this;39}40function privateInitializeReadableByteStreamController(stream, underlyingByteSource, highWaterMark)41{42 "use strict";43 if (!@isReadableStream(stream))44 @throwTypeError("ReadableByteStreamController needs a ReadableStream");45 // readableStreamController is initialized with null value.46 if (stream.@readableStreamController !== null)47 @throwTypeError("ReadableStream already has a controller");48 this.@controlledReadableStream = stream;49 this.@underlyingByteSource = underlyingByteSource;50 this.@pullAgain = false;51 this.@pulling = false;52 @readableByteStreamControllerClearPendingPullIntos(this);53 this.@queue = @newQueue();54 this.@started = false;55 this.@closeRequested = false;56 let hwm = @toNumber(highWaterMark);57 if (@isNaN(hwm) || hwm < 0)58 @throwRangeError("highWaterMark value is negative or not a number");59 this.@strategyHWM = hwm;60 let autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;61 if (autoAllocateChunkSize !== @undefined) {62 autoAllocateChunkSize = @toNumber(autoAllocateChunkSize);63 if (autoAllocateChunkSize <= 0 || autoAllocateChunkSize === @Number.POSITIVE_INFINITY || autoAllocateChunkSize === @Number.NEGATIVE_INFINITY)64 @throwRangeError("autoAllocateChunkSize value is negative or equal to positive or negative infinity");65 }66 this.@autoAllocateChunkSize = autoAllocateChunkSize;67 this.@pendingPullIntos = [];68 const controller = this;69 const startResult = @promiseInvokeOrNoopNoCatch(underlyingByteSource, "start", [this]).@then(() => {70 controller.@started = true;71 @assert(!controller.@pulling);72 @assert(!controller.@pullAgain);73 @readableByteStreamControllerCallPullIfNeeded(controller);74 }, (error) => {75 if (stream.@state === @streamReadable)76 @readableByteStreamControllerError(controller, error);77 });78 this.@cancel = @readableByteStreamControllerCancel;79 this.@pull = @readableByteStreamControllerPull;80 return this;81}82function privateInitializeReadableStreamBYOBRequest(controller, view)83{84 "use strict";85 this.@associatedReadableByteStreamController = controller;86 this.@view = view;87}88function isReadableByteStreamController(controller)89{90 "use strict";91 // Same test mechanism as in isReadableStreamDefaultController (ReadableStreamInternals.js).92 // See corresponding function for explanations.93 return @isObject(controller) && !!controller.@underlyingByteSource;94}95function isReadableStreamBYOBRequest(byobRequest)96{97 "use strict";98 // Same test mechanism as in isReadableStreamDefaultController (ReadableStreamInternals.js).99 // See corresponding function for explanations.100 return @isObject(byobRequest) && !!byobRequest.@associatedReadableByteStreamController;101}102function isReadableStreamBYOBReader(reader)103{104 "use strict";105 // Spec tells to return true only if reader has a readIntoRequests internal slot.106 // However, since it is a private slot, it cannot be checked using hasOwnProperty().107 // Since readIntoRequests is initialized with an empty array, the following test is ok.108 return @isObject(reader) && !!reader.@readIntoRequests;109}110function readableByteStreamControllerCancel(controller, reason)111{112 "use strict";113 if (controller.@pendingPullIntos.length > 0)114 controller.@pendingPullIntos[0].bytesFilled = 0;115 controller.@queue = @newQueue();116 return @promiseInvokeOrNoop(controller.@underlyingByteSource, "cancel", [reason]);117}118function readableByteStreamControllerError(controller, e)119{120 "use strict";121 @assert(controller.@controlledReadableStream.@state === @streamReadable);122 @readableByteStreamControllerClearPendingPullIntos(controller);123 controller.@queue = @newQueue();124 @readableStreamError(controller.@controlledReadableStream, e);125}126function readableByteStreamControllerClose(controller)127{128 "use strict";129 @assert(!controller.@closeRequested);130 @assert(controller.@controlledReadableStream.@state === @streamReadable);131 if (controller.@queue.size > 0) {132 controller.@closeRequested = true;133 return;134 }135 if (controller.@pendingPullIntos.length > 0) {136 if (controller.@pendingPullIntos[0].bytesFilled > 0) {137 const e = new @TypeError("Close requested while there remain pending bytes");138 @readableByteStreamControllerError(controller, e);139 throw e;140 }141 }142 @readableStreamClose(controller.@controlledReadableStream);143}144function readableByteStreamControllerClearPendingPullIntos(controller)145{146 "use strict";147 @readableByteStreamControllerInvalidateBYOBRequest(controller);148 controller.@pendingPullIntos = [];149}150function readableByteStreamControllerGetDesiredSize(controller)151{152 "use strict";153 const stream = controller.@controlledReadableStream;154 if (stream.@state === @streamErrored)155 return null;156 if (stream.@state === @streamClosed)157 return 0;158 return controller.@strategyHWM - controller.@queue.size;159}160function readableStreamHasBYOBReader(stream)161{162 "use strict";163 return stream.@reader !== @undefined && @isReadableStreamBYOBReader(stream.@reader);164}165function readableStreamHasDefaultReader(stream)166{167 "use strict";168 return stream.@reader !== @undefined && @isReadableStreamDefaultReader(stream.@reader);169}170function readableByteStreamControllerHandleQueueDrain(controller) {171 "use strict";172 @assert(controller.@controlledReadableStream.@state === @streamReadable);173 if (!controller.@queue.size && controller.@closeRequested)174 @readableStreamClose(controller.@controlledReadableStream);175 else176 @readableByteStreamControllerCallPullIfNeeded(controller);177}178function readableByteStreamControllerPull(controller)179{180 "use strict";181 const stream = controller.@controlledReadableStream;182 @assert(@readableStreamHasDefaultReader(stream));183 if (controller.@queue.size > 0) {184 @assert(stream.@reader.@readRequests.length === 0);185 const entry = controller.@queue.content.@shift();186 controller.@queue.size -= entry.byteLength;187 @readableByteStreamControllerHandleQueueDrain(controller);188 let view;189 try {190 view = new @Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);191 } catch (error) {192 return @Promise.@reject(error);193 }194 return @Promise.@resolve({value: view, done: false});195 }196 if (controller.@autoAllocateChunkSize !== @undefined) {197 let buffer;198 try {199 buffer = new @ArrayBuffer(controller.@autoAllocateChunkSize);200 } catch (error) {201 return @Promise.@reject(error);202 }203 const pullIntoDescriptor = {204 buffer,205 byteOffset: 0,206 byteLength: controller.@autoAllocateChunkSize,207 bytesFilled: 0,208 elementSize: 1,209 ctor: @Uint8Array,210 readerType: 'default'211 };212 controller.@pendingPullIntos.@push(pullIntoDescriptor);213 }214 const promise = @readableStreamAddReadRequest(stream);215 @readableByteStreamControllerCallPullIfNeeded(controller);216 return promise;217}218function readableByteStreamControllerShouldCallPull(controller)219{220 "use strict";221 const stream = controller.@controlledReadableStream;222 if (stream.@state !== @streamReadable)223 return false;224 if (controller.@closeRequested)225 return false;226 if (!controller.@started)227 return false;228 if (@readableStreamHasDefaultReader(stream) && stream.@reader.@readRequests.length > 0)229 return true;230 if (@readableStreamHasBYOBReader(stream) && stream.@reader.@readIntoRequests.length > 0)231 return true;232 if (@readableByteStreamControllerGetDesiredSize(controller) > 0)233 return true;234 return false;235}236function readableByteStreamControllerCallPullIfNeeded(controller)237{238 "use strict";239 if (!@readableByteStreamControllerShouldCallPull(controller))240 return;241 if (controller.@pulling) {242 controller.@pullAgain = true;243 return;244 }245 @assert(!controller.@pullAgain);246 controller.@pulling = true;247 @promiseInvokeOrNoop(controller.@underlyingByteSource, "pull", [controller]).@then(() => {248 controller.@pulling = false;249 if (controller.@pullAgain) {250 controller.@pullAgain = false;251 @readableByteStreamControllerCallPullIfNeeded(controller);252 }253 }, (error) => {254 if (controller.@controlledReadableStream.@state === @streamReadable)255 @readableByteStreamControllerError(controller, error);256 });257}258function transferBufferToCurrentRealm(buffer)259{260 "use strict";261 // FIXME: Determine what should be done here exactly (what is already existing in current262 // codebase and what has to be added). According to spec, Transfer operation should be263 // performed in order to transfer buffer to current realm. For the moment, simply return264 // received buffer.265 return buffer;266}267function readableByteStreamControllerEnqueue(controller, chunk)268{269 "use strict";270 const stream = controller.@controlledReadableStream;271 @assert(!controller.@closeRequested);272 @assert(stream.@state === @streamReadable);273 const buffer = chunk.buffer;274 const byteOffset = chunk.byteOffset;275 const byteLength = chunk.byteLength;276 const transferredBuffer = @transferBufferToCurrentRealm(buffer);277 if (@readableStreamHasDefaultReader(stream)) {278 if (!stream.@reader.@readRequests.length)279 @readableByteStreamControllerEnqueueChunk(controller, transferredBuffer, byteOffset, byteLength);280 else {281 @assert(!controller.@queue.content.length);282 let transferredView = new @Uint8Array(transferredBuffer, byteOffset, byteLength);283 @readableStreamFulfillReadRequest(stream, transferredView, false);284 }285 return;286 }287 if (@readableStreamHasBYOBReader(stream)) {288 @readableByteStreamControllerEnqueueChunk(controller, transferredBuffer, byteOffset, byteLength);289 @readableByteStreamControllerProcessPullDescriptors(controller);290 return;291 }292 @assert(!@isReadableStreamLocked(stream));293 @readableByteStreamControllerEnqueueChunk(controller, transferredBuffer, byteOffset, byteLength);294}295// Spec name: readableByteStreamControllerEnqueueChunkToQueue.296function readableByteStreamControllerEnqueueChunk(controller, buffer, byteOffset, byteLength)297{298 "use strict";299 controller.@queue.content.@push({300 buffer: buffer,301 byteOffset: byteOffset,302 byteLength: byteLength303 });304 controller.@queue.size += byteLength;305}306function readableByteStreamControllerRespondWithNewView(controller, view)307{308 "use strict";309 @assert(controller.@pendingPullIntos.length > 0);310 let firstDescriptor = controller.@pendingPullIntos[0];311 if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset)312 @throwRangeError("Invalid value for view.byteOffset");313 if (firstDescriptor.byteLength !== view.byteLength)314 @throwRangeError("Invalid value for view.byteLength");315 firstDescriptor.buffer = view.buffer;316 @readableByteStreamControllerRespondInternal(controller, view.byteLength);317}318function readableByteStreamControllerRespond(controller, bytesWritten)319{320 "use strict";321 bytesWritten = @toNumber(bytesWritten);322 if (@isNaN(bytesWritten) || bytesWritten === @Number.POSITIVE_INFINITY || bytesWritten < 0 )323 @throwRangeError("bytesWritten has an incorrect value");324 @assert(controller.@pendingPullIntos.length > 0);325 @readableByteStreamControllerRespondInternal(controller, bytesWritten);326}327function readableByteStreamControllerRespondInternal(controller, bytesWritten)328{329 "use strict";330 let firstDescriptor = controller.@pendingPullIntos[0];331 let stream = controller.@controlledReadableStream;332 if (stream.@state === @streamClosed) {333 if (bytesWritten !== 0)334 @throwTypeError("bytesWritten is different from 0 even though stream is closed");335 @readableByteStreamControllerRespondInClosedState(controller, firstDescriptor);336 } else {337 @assert(stream.@state === @streamReadable);338 @readableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);339 }340}341function readableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor)342{343 "use strict";344 if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength)345 @throwRangeError("bytesWritten value is too great");346 @assert(controller.@pendingPullIntos.length === 0 || controller.@pendingPullIntos[0] === pullIntoDescriptor);347 @readableByteStreamControllerInvalidateBYOBRequest(controller);348 pullIntoDescriptor.bytesFilled += bytesWritten;349 if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize)350 return;351 @readableByteStreamControllerShiftPendingDescriptor(controller);352 const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;353 if (remainderSize > 0) {354 const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;355 const remainder = @cloneArrayBuffer(pullIntoDescriptor.buffer, end - remainderSize, remainderSize);356 @readableByteStreamControllerEnqueueChunk(controller, remainder, 0, remainder.byteLength);357 }358 pullIntoDescriptor.buffer = @transferBufferToCurrentRealm(pullIntoDescriptor.buffer);359 pullIntoDescriptor.bytesFilled -= remainderSize;360 @readableByteStreamControllerCommitDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);361 @readableByteStreamControllerProcessPullDescriptors(controller);362}363function readableByteStreamControllerRespondInClosedState(controller, firstDescriptor)364{365 "use strict";366 firstDescriptor.buffer = @transferBufferToCurrentRealm(firstDescriptor.buffer);367 @assert(firstDescriptor.bytesFilled === 0);368 if (@readableStreamHasBYOBReader(controller.@controlledReadableStream)) {369 while (controller.@controlledReadableStream.@reader.@readIntoRequests.length > 0) {370 let pullIntoDescriptor = @readableByteStreamControllerShiftPendingDescriptor(controller);371 @readableByteStreamControllerCommitDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);372 }373 }374}375// Spec name: readableByteStreamControllerProcessPullIntoDescriptorsUsingQueue (shortened for readability).376function readableByteStreamControllerProcessPullDescriptors(controller)377{378 "use strict";379 @assert(!controller.@closeRequested);380 while (controller.@pendingPullIntos.length > 0) {381 if (controller.@queue.size === 0)382 return;383 let pullIntoDescriptor = controller.@pendingPullIntos[0];384 if (@readableByteStreamControllerFillDescriptorFromQueue(controller, pullIntoDescriptor)) {385 @readableByteStreamControllerShiftPendingDescriptor(controller);386 @readableByteStreamControllerCommitDescriptor(controller.@controlledReadableStream, pullIntoDescriptor);387 }388 }389}390// Spec name: readableByteStreamControllerFillPullIntoDescriptorFromQueue (shortened for readability).391function readableByteStreamControllerFillDescriptorFromQueue(controller, pullIntoDescriptor)392{393 "use strict";394 const currentAlignedBytes = pullIntoDescriptor.bytesFilled - (pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize);395 const maxBytesToCopy = controller.@queue.size < pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled ?396 controller.@queue.size : pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled;397 const maxBytesFilled = pullIntoDescriptor.bytesFilled + maxBytesToCopy;398 const maxAlignedBytes = maxBytesFilled - (maxBytesFilled % pullIntoDescriptor.elementSize);399 let totalBytesToCopyRemaining = maxBytesToCopy;400 let ready = false;401 if (maxAlignedBytes > currentAlignedBytes) {402 totalBytesToCopyRemaining = maxAlignedBytes - pullIntoDescriptor.bytesFilled;403 ready = true;404 }405 while (totalBytesToCopyRemaining > 0) {406 let headOfQueue = controller.@queue.content[0];407 const bytesToCopy = totalBytesToCopyRemaining < headOfQueue.byteLength ? totalBytesToCopyRemaining : headOfQueue.byteLength;408 // Copy appropriate part of pullIntoDescriptor.buffer to headOfQueue.buffer.409 // Remark: this implementation is not completely aligned on the definition of CopyDataBlockBytes410 // operation of ECMAScript (the case of Shared Data Block is not considered here, but it doesn't seem to be an issue).411 const destStart = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;412 // FIXME: As indicated in comments of bug 172717, access to set is not safe. However, using prototype.@set.@call does413 // not work (@set is undefined). A safe way to do that is needed.414 new @Uint8Array(pullIntoDescriptor.buffer).set(new @Uint8Array(headOfQueue.buffer, headOfQueue.byteOffset, bytesToCopy), destStart);415 if (headOfQueue.byteLength === bytesToCopy)416 controller.@queue.content.@shift();417 else {418 headOfQueue.byteOffset += bytesToCopy;419 headOfQueue.byteLength -= bytesToCopy;420 }421 controller.@queue.size -= bytesToCopy;422 @assert(controller.@pendingPullIntos.length === 0 || controller.@pendingPullIntos[0] === pullIntoDescriptor);423 @readableByteStreamControllerInvalidateBYOBRequest(controller);424 pullIntoDescriptor.bytesFilled += bytesToCopy;425 totalBytesToCopyRemaining -= bytesToCopy;426 }427 if (!ready) {428 @assert(controller.@queue.size === 0);429 @assert(pullIntoDescriptor.bytesFilled > 0);430 @assert(pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize);431 }432 return ready;433}434// Spec name: readableByteStreamControllerShiftPendingPullInto (renamed for consistency).435function readableByteStreamControllerShiftPendingDescriptor(controller)436{437 "use strict";438 let descriptor = controller.@pendingPullIntos.@shift();439 @readableByteStreamControllerInvalidateBYOBRequest(controller);440 return descriptor;441}442function readableByteStreamControllerInvalidateBYOBRequest(controller)443{444 "use strict";445 if (controller.@byobRequest === @undefined)446 return;447 controller.@byobRequest.@associatedReadableByteStreamController = @undefined;448 controller.@byobRequest.@view = @undefined;449 controller.@byobRequest = @undefined;450}451// Spec name: readableByteStreamControllerCommitPullIntoDescriptor (shortened for readability).452function readableByteStreamControllerCommitDescriptor(stream, pullIntoDescriptor)453{454 "use strict";455 @assert(stream.@state !== @streamErrored);456 let done = false;457 if (stream.@state === @streamClosed) {458 @assert(!pullIntoDescriptor.bytesFilled);459 done = true;460 }461 let filledView = @readableByteStreamControllerConvertDescriptor(pullIntoDescriptor);462 if (pullIntoDescriptor.readerType === "default")463 @readableStreamFulfillReadRequest(stream, filledView, done);464 else {465 @assert(pullIntoDescriptor.readerType === "byob");466 @readableStreamFulfillReadIntoRequest(stream, filledView, done);467 }468}469// Spec name: readableByteStreamControllerConvertPullIntoDescriptor (shortened for readability).470function readableByteStreamControllerConvertDescriptor(pullIntoDescriptor)471{472 "use strict";473 @assert(pullIntoDescriptor.bytesFilled <= pullIntoDescriptor.byteLength);474 @assert(pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize === 0);475 return new pullIntoDescriptor.ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, pullIntoDescriptor.bytesFilled / pullIntoDescriptor.elementSize);476}477function readableStreamFulfillReadIntoRequest(stream, chunk, done)478{479 "use strict";480 stream.@reader.@readIntoRequests.@shift().@resolve.@call(@undefined, {value: chunk, done: done});481}482function readableStreamBYOBReaderRead(reader, view)483{484 "use strict";485 const stream = reader.@ownerReadableStream;486 @assert(!!stream);487 stream.@disturbed = true;488 if (stream.@state === @streamErrored)489 return @Promise.@reject(stream.@storedError);490 return @readableByteStreamControllerPullInto(stream.@readableStreamController, view);491}492function readableByteStreamControllerPullInto(controller, view)493{494 "use strict";495 const stream = controller.@controlledReadableStream;496 let elementSize = 1;497 // Spec describes that in the case where view is a TypedArray, elementSize498 // should be set to the size of an element (e.g. 2 for UInt16Array). For499 // DataView, BYTES_PER_ELEMENT is undefined, contrary to the same property500 // for TypedArrays.501 // FIXME: Getting BYTES_PER_ELEMENT like this is not safe (property is read-only502 // but can be modified if the prototype is redefined). A safe way of getting503 // it would be to determine which type of ArrayBufferView view is an instance504 // of based on typed arrays private variables. However, this is not possible due505 // to bug 167697, which prevents access to typed arrays through their private506 // names unless public name has already been met before.507 if (view.BYTES_PER_ELEMENT !== @undefined)508 elementSize = view.BYTES_PER_ELEMENT;509 // FIXME: Getting constructor like this is not safe. A safe way of getting510 // it would be to determine which type of ArrayBufferView view is an instance511 // of, and to assign appropriate constructor based on this (e.g. ctor =512 // @Uint8Array). However, this is not possible due to bug 167697, which513 // prevents access to typed arrays through their private names unless public514 // name has already been met before.515 const ctor = view.constructor;516 const pullIntoDescriptor = {517 buffer: view.buffer,518 byteOffset: view.byteOffset,519 byteLength: view.byteLength,520 bytesFilled: 0,521 elementSize,522 ctor,523 readerType: 'byob'524 };525 if (controller.@pendingPullIntos.length) {526 pullIntoDescriptor.buffer = @transferBufferToCurrentRealm(pullIntoDescriptor.buffer);527 controller.@pendingPullIntos.@push(pullIntoDescriptor);528 return @readableStreamAddReadIntoRequest(stream);529 }530 if (stream.@state === @streamClosed) {531 const emptyView = new ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, 0);532 return @Promise.@resolve({ value: emptyView, done: true });533 }534 if (controller.@queue.size > 0) {535 if (@readableByteStreamControllerFillDescriptorFromQueue(controller, pullIntoDescriptor)) {536 const filledView = @readableByteStreamControllerConvertDescriptor(pullIntoDescriptor);537 @readableByteStreamControllerHandleQueueDrain(controller);538 return @Promise.@resolve({ value: filledView, done: false });539 }540 if (controller.@closeRequested) {541 const e = new @TypeError("Closing stream has been requested");542 @readableByteStreamControllerError(controller, e);543 return @Promise.@reject(e);544 }545 }546 pullIntoDescriptor.buffer = @transferBufferToCurrentRealm(pullIntoDescriptor.buffer);547 controller.@pendingPullIntos.@push(pullIntoDescriptor);548 const promise = @readableStreamAddReadIntoRequest(stream);549 @readableByteStreamControllerCallPullIfNeeded(controller);550 return promise;551}552function readableStreamAddReadIntoRequest(stream)553{554 "use strict";555 @assert(@isReadableStreamBYOBReader(stream.@reader));556 @assert(stream.@state === @streamReadable || stream.@state === @streamClosed);557 const readRequest = @newPromiseCapability(@Promise);558 stream.@reader.@readIntoRequests.@push(readRequest);559 return readRequest.@promise;...

Full Screen

Full Screen

byob-reader.ts

Source:byob-reader.ts Github

copy

Full Screen

...205 stream._disturbed = true;206 if (stream._state === 'errored') {207 readIntoRequest._errorSteps(stream._storedError);208 } else {209 ReadableByteStreamControllerPullInto(210 stream._readableStreamController as ReadableByteStreamController,211 view,212 readIntoRequest213 );214 }215}216// Helper functions for the ReadableStreamBYOBReader.217function byobReaderBrandCheckException(name: string): TypeError {218 return new TypeError(219 `ReadableStreamBYOBReader.prototype.${name} can only be used on a ReadableStreamBYOBReader`);...

Full Screen

Full Screen

readable_stream_byob_reader.ts

Source:readable_stream_byob_reader.ts Github

copy

Full Screen

...97 if (stream.state === "errored") {98 return Promise.reject(stream.storedError);99 }100 Assert(stream.state === "readable");101 return ReadableByteStreamControllerPullInto(102 stream.readableStreamController as ReadableByteStreamController,103 view,104 forAuthorCode105 );...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pull: function(controller) {3 var buffer = new ArrayBuffer(4);4 var result = controller.pullInto(new Uint8Array(buffer));5 console.log(result);6 }7});8var reader = rs.getReader();9var read = reader.read();10read.then(function(result) {11 console.log(result);12 read = reader.read();13 read.then(function(result) {14 console.log(result);15 });16});17{ value: undefined, done: false }18{ value: undefined, done: false }19function ReadableByteStreamControllerPullInto(controller, view) {20 var stream = controller._controlledReadableByteStream;21 assert(stream._state === 'readable');22 controller._pendingPullIntos.push({23 });24 ReadableByteStreamControllerCallPullIfNeeded(controller);25}

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pullInto: function(controller, buffer, offset) {3 var view = new Uint8Array(buffer, offset, 4);4 view[0] = 0;5 view[1] = 1;6 view[2] = 2;7 view[3] = 3;8 return controller.byobRequest.respond(4);9 }10});11var reader = rs.getReader({ mode: 'byob' });12var view = new Uint8Array(4);13reader.read(view).then(function(result) {14 console.log(result.value);15});16var rs = new ReadableStream({17 pullInto: function(controller, buffer, offset) {18 var view = new Uint8Array(buffer, offset, 4);19 view[0] = 0;20 view[1] = 1;21 view[2] = 2;22 view[3] = 3;23 return controller.byobRequest.respond(4);24 }25});26var reader = rs.getReader({ mode: 'byob' });27var view = new Uint8Array(4);28reader.read(view).then(function(result) {29 console.log(result.value);30});31var rs = new ReadableStream({32 pullInto: function(controller, buffer, offset) {33 var view = new Uint8Array(buffer, offset, 4);34 view[0] = 0;35 view[1] = 1;36 view[2] = 2;37 view[3] = 3;38 return controller.byobRequest.respond(4);39 }40});41var reader = rs.getReader({ mode: 'byob' });42var view = new Uint8Array(4);43reader.read(view).then(function(result) {44 console.log(result.value);45});

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 pull(controller) {3 var buffer = new Uint8Array(16);4 controller.byobRequest.view.set(buffer);5 controller.byobRequest.respond(16);6 }7});8var reader = rs.getReader({mode: 'byob'});9var result = reader.read(new Uint8Array(16));10result.then(function(result) {11 console.log(result.value);12 console.log(re

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 start(controller) {3 var buffer = new ArrayBuffer(16);4 var view = new DataView(buffer);5 for (var i = 0; i < 8; i++) {6 view.setUint8(i, i);7 }8 controller.enqueue(view);9 }10});11var reader = rs.getReader();12var view = new Uint8Array(16);13var result = reader.read(view).then(function(result) {14 assert_array_equals(view, new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0]));15 assert_false(result.done);16 assert_equals(result.value.byteLength, 8);17});18var rs = new ReadableStream({19 start(controller) {20 controller.close();21 }22});23var reader = rs.getReader();24var result = reader.read().then(function(result) {25 assert_true(result.done);26 assert_equals(result.value, undefined);27});28var rs = new ReadableStream({29 start(controller) {30 controller.enqueue('a');31 controller.enqueue('b');32 controller.enqueue('c');33 }34});35var reader = rs.getReader();36var result = reader.read().then(function(result) {37 assert_false(result.done);38 assert_equals(result.value, 'a');39 return reader.read();40}).then(function(result) {41 assert_false(result.done);42 assert_equals(result.value, 'b');43 return reader.read();44}).then(function(result) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var test = async_test("ReadableByteStreamControllerPullInto");2var byobRequest;3var controller;4var stream = new ReadableStream({5 pullInto: function(c) {6 controller = c;7 byobRequest = c.byobRequest;8 test.step(function() {9 assert_equals(byobRequest.view.byteLength, 16, "byobRequest.view.byteLength");10 assert_equals(byobRequest.view.buffer.byteLength, 16, "byobRequest.view.buffer.byteLength");11 assert_equals(byobRequest.view.constructor, Uint8Array, "byobRequest.view.constructor");12 assert_equals(byobRequest.view.buffer.constructor, ArrayBuffer, "byobRequest.view.buffer.constructor");13 });14 test.done();15 }16});17var reader = stream.getReader({mode: "byob"});18var view = new Uint8Array(16);19reader.read(view).then(20 function(result) {21 test.step(function() {22 assert_equals(result.value, undefined, "result.value");23 assert_equals(result.done, true, "result.done");24 });25 test.done();26 },27 function(e) {28 test.step(function() {29 assert_unreached("read() should not reject");30 });31 test.done();32 }33);34var test = async_test("ReadableByteStreamControllerEnqueue");35var byobRequest;36var controller;37var stream = new ReadableStream({38 pullInto: function(c) {39 controller = c;40 byobRequest = c.byobRequest;41 test.step(function() {42 assert_equals(byobRequest.view.byteLength, 16, "byobRequest.view.byteLength");43 assert_equals(byobRequest.view.buffer.byteLength, 16, "byobRequest.view.buffer.byteLength");44 assert_equals(byobRequest.view.constructor, Uint8Array, "byobRequest.view.constructor");45 assert_equals(byobRequest.view.buffer.constructor, ArrayBuffer, "byobRequest.view.buffer.constructor");46 });47 test.done();48 }49});50var reader = stream.getReader({mode: "byob"});51var view = new Uint8Array(16);52controller.enqueue(view);

Full Screen

Using AI Code Generation

copy

Full Screen

1'use strict';2const rs = new ReadableStream({3 pull(controller) {4 const view = controller.byobRequest.view;5 view[0] = 0x61;6 view[1] = 0x62;7 view[2] = 0x63;8 view[3] = 0x64;9 controller.byobRequest.respond(4);10 },11 cancel() {12 console.log('rs.cancel() called');13 }14});15const reader = rs.getReader({ mode: 'byob' });16const read = async () => {17 const { value, done } = await reader.read(new Uint8Array(4));18 if (done) {19 console.log('done');20 return;21 }22 console.log(value);23 read();24};25read();26'use strict';27const rs = new ReadableStream({28 pull(controller) {29 const view = controller.byobRequest.view;30 view[0] = 0x61;31 view[1] = 0x62;32 view[2] = 0x63;33 view[3] = 0x64;34 controller.byobRequest.respond(4);35 },36 cancel() {37 console.log('rs.cancel() called');38 }39});40const reader = rs.getReader({ mode: 'byob' });41const read = async () => {42 const { value, done } = await reader.read(new Uint8Array(4));43 if (done) {44 console.log('done');45 return;46 }47 console.log(value);48 read();49};50read();

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