Best JavaScript code snippet using wpt
ReadableByteStreamInternals.js
Source:ReadableByteStreamInternals.js
1/*2 * Copyright (C) 2016 Canon Inc. All rights reserved.3 *4 * Redistribution and use in source and binary forms, with or without5 * modification, are permitted provided that the following conditions6 * are met:7 * 1. Redistributions of source code must retain the above copyright8 * notice, this list of conditions and the following disclaimer.9 * 2. Redistributions in binary form must reproduce the above copyright10 * notice, this list of conditions and the following disclaimer in the11 * documentation and/or other materials provided with the distribution.12 *13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24 */25// @conditional=ENABLE(STREAMS_API)26// @internal27function 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;...
Using AI Code Generation
1test(function() {2 var rs = new ReadableStream({3 pull: function(controller) {4 controller.enqueue(new Uint8Array([1, 2, 3]));5 }6 });7 var reader = rs.getReader({ mode: 'byob' });8 return reader.read(new Uint8Array(3)).then(function(result) {9 assert_array_equals(result.value, [1, 2, 3]);10 assert_false(result.done);11 var byobRequest = reader.byobRequest;12 assert_not_equals(byobRequest, undefined);13 assert_equals(byobRequest.view.constructor, Uint8Array);14 assert_equals(byobRequest.view.byteOffset, 0);15 assert_equals(byobRequest.view.byteLength, 3);16 assert_equals(byobRequest.view.buffer.byteLength, 3);17 assert_equals(byobRequest.view[0], 1);18 assert_equals(byobRequest.view[1], 2);19 assert_equals(byobRequest.view[2], 3);20 reader.releaseLock();21 byobRequest = reader.byobRequest;22 assert_equals(byobRequest, undefined);23 return reader.read(new Uint8Array(3));24 }).then(function(result) {25 assert_array_equals(result.value, [1, 2, 3]);26 assert_false(result.done);27 var byobRequest = reader.byobRequest;28 assert_not_equals(byobRequest, undefined);29 assert_equals(byobRequest.view.constructor, Uint8Array);30 assert_equals(byobRequest.view.byteOffset, 0);31 assert_equals(byobRequest.view.byteLength, 3);32 assert_equals(byobRequest.view.buffer.byteLength, 3);33 assert_equals(byobRequest.view[0], 1);34 assert_equals(byobRequest.view[1], 2);35 assert_equals(byobRequest.view[2], 3);36 reader.releaseLock();37 byobRequest = reader.byobRequest;38 assert_equals(byobRequest, undefined);39 return reader.read(new Uint8Array(3));40 }).then(function(result) {41 assert_array_equals(result.value, [1, 2, 3]);42 assert_false(result.done);43 var byobRequest = reader.byobRequest;44 assert_not_equals(byobRequest, undefined);45 assert_equals(byobRequest.view.constructor, Uint8Array);46 assert_equals(byobRequest.view.byteOffset,
Using AI Code Generation
1test(() => {2 let controller = new ReadableStream({3 pull: (c) => {4 ReadableByteStreamControllerInvalidateBYOBRequest(c);5 }6 }).getReader({ mode: "byob" }).read(new Uint8Array(1));7}, "ReadableByteStreamControllerInvalidateBYOBRequest method of wpt");
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(c) {3 var buffer = new ArrayBuffer(16);4 var view = new Uint8Array(buffer);5 view[0] = 1;6 view[1] = 2;7 view[2] = 3;8 view[3] = 4;9 view[4] = 5;10 view[5] = 6;11 view[6] = 7;12 view[7] = 8;13 view[8] = 9;14 view[9] = 10;15 view[10] = 11;16 view[11] = 12;17 view[12] = 13;18 view[13] = 14;19 view[14] = 15;20 view[15] = 16;21 c.enqueue(view);22 }23});24var reader = rs.getReader({mode: "byob"});25var promise = reader.read(new Uint8Array(4));26promise.then(function(result) {27 console.log(result.value);28 console.log(result.done);29 reader.releaseLock();30});31Uint8Array(4) [ 1, 2, 3, 4 ]32ReadableByteStreamControllerInvalidateBYOBRequest(controller)33var controller = new ReadableStream().getReader().readRequest().controller;34ReadableByteStreamControllerInvalidateBYOBRequest(controller);35HTML | input type="file" stepUp() method
Using AI Code Generation
1var rs = new ReadableStream({2 pull(controller) {3 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));4 controller.close();5 }6});7var reader = rs.getReader({mode: 'byob'});8var view = new Uint8Array(1);9reader.read(view).then(function(result) {10false }11 reader.releaseLock();12 reader.read(view).then(function(result) {13 });14});15var rs = new ReadableStream({16 pull(controller) {17 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));18 controller.close();19 }20});21var reader = rs.getReader({mode: 'byob'});22var view = new Uint8Array(3);23reader.read(view).then(function(result) {24false }25 reader.releaseLock();26 reader.read(view).then(function(result) {27 });28});29var rs = new ReadableStream({30 pull(controller) {31 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));32 controller.close();33 }34});35var reader = rs.getReader({mode: 'byob'});36var view = new Uint8Array(3);37reader.read(view).then(function(result) {38false }39 reader.releaseLock();40 reader.read(view).then(function(result) {
Using AI Code Generation
1const { ReadableStream, CountQueuingStrategy } = new Stream();2const rs = new ReadableStream({3 pull: () => {}4}, {5});6const reader = rs.getReader({ mode: 'byob' });7const view = new Uint8Array(1024);8reader.read(view).then(result => {9 console.log(result);10 reader.read(view).then(result => {11 console.log(result);12 });13});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!