Best JavaScript code snippet using wpt
readable_byte_stream_controller.ts
Source:readable_byte_stream_controller.ts
1// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.2import {3 BufferQueueItem,4 CancelAlgorithm,5 isDetachedBuffer,6 isReadableByteStreamController,7 PullAlgorithm,8 resetQueue,9 readableByteStreamControllerCallPullIfNeeded,10 readableByteStreamControllerClearAlgorithms,11 readableByteStreamControllerClose,12 readableByteStreamControllerEnqueue,13 readableByteStreamControllerError,14 readableByteStreamControllerGetDesiredSize,15 readableByteStreamControllerHandleQueueDrain,16 readableStreamAddReadRequest,17 readableStreamHasDefaultReader,18 readableStreamGetNumReadRequests,19 readableStreamCreateReadResult,20 setFunctionName,21} from "./internals.ts";22import { ReadableStreamImpl } from "./readable_stream.ts";23import * as sym from "./symbols.ts";24import { assert } from "../../util.ts";25import { customInspect } from "../console.ts";26export class ReadableByteStreamControllerImpl27 implements ReadableByteStreamController {28 [sym.autoAllocateChunkSize]: number | undefined;29 [sym.byobRequest]: undefined;30 [sym.cancelAlgorithm]: CancelAlgorithm;31 [sym.closeRequested]: boolean;32 [sym.controlledReadableByteStream]: ReadableStreamImpl<Uint8Array>;33 [sym.pullAgain]: boolean;34 [sym.pullAlgorithm]: PullAlgorithm;35 [sym.pulling]: boolean;36 [sym.queue]: BufferQueueItem[];37 [sym.queueTotalSize]: number;38 [sym.started]: boolean;39 [sym.strategyHWM]: number;40 private constructor() {41 throw new TypeError(42 "ReadableByteStreamController's constructor cannot be called."43 );44 }45 get byobRequest(): undefined {46 return undefined;47 }48 get desiredSize(): number | null {49 if (!isReadableByteStreamController(this)) {50 throw new TypeError("Invalid ReadableByteStreamController.");51 }52 return readableByteStreamControllerGetDesiredSize(this);53 }54 close(): void {55 if (!isReadableByteStreamController(this)) {56 throw new TypeError("Invalid ReadableByteStreamController.");57 }58 if (this[sym.closeRequested]) {59 throw new TypeError("Closed already requested.");60 }61 if (this[sym.controlledReadableByteStream][sym.state] !== "readable") {62 throw new TypeError(63 "ReadableByteStreamController's stream is not in a readable state."64 );65 }66 readableByteStreamControllerClose(this);67 }68 enqueue(chunk: ArrayBufferView): void {69 if (!isReadableByteStreamController(this)) {70 throw new TypeError("Invalid ReadableByteStreamController.");71 }72 if (this[sym.closeRequested]) {73 throw new TypeError("Closed already requested.");74 }75 if (this[sym.controlledReadableByteStream][sym.state] !== "readable") {76 throw new TypeError(77 "ReadableByteStreamController's stream is not in a readable state."78 );79 }80 if (!ArrayBuffer.isView(chunk)) {81 throw new TypeError(82 "You can only enqueue array buffer views when using a ReadableByteStreamController"83 );84 }85 if (isDetachedBuffer(chunk.buffer)) {86 throw new TypeError("Cannot enqueue a view onto a detached ArrayBuffer");87 }88 readableByteStreamControllerEnqueue(this, chunk);89 }90 // eslint-disable-next-line @typescript-eslint/no-explicit-any91 error(error?: any): void {92 if (!isReadableByteStreamController(this)) {93 throw new TypeError("Invalid ReadableByteStreamController.");94 }95 readableByteStreamControllerError(this, error);96 }97 // eslint-disable-next-line @typescript-eslint/no-explicit-any98 [sym.cancelSteps](reason: any): PromiseLike<void> {99 // 3.11.5.1.1 If this.[[pendingPullIntos]] is not empty,100 resetQueue(this);101 const result = this[sym.cancelAlgorithm](reason);102 readableByteStreamControllerClearAlgorithms(this);103 return result;104 }105 [sym.pullSteps](): Promise<ReadableStreamReadResult<Uint8Array>> {106 const stream = this[sym.controlledReadableByteStream];107 assert(readableStreamHasDefaultReader(stream));108 if (this[sym.queueTotalSize] > 0) {109 assert(readableStreamGetNumReadRequests(stream) === 0);110 const entry = this[sym.queue].shift();111 assert(entry);112 this[sym.queueTotalSize] -= entry.size;113 readableByteStreamControllerHandleQueueDrain(this);114 const view = new Uint8Array(entry.value, entry.offset, entry.size);115 return Promise.resolve(116 readableStreamCreateReadResult(117 view,118 false,119 stream[sym.reader]![sym.forAuthorCode]120 )121 );122 }123 // 3.11.5.2.5 If autoAllocateChunkSize is not undefined,124 const promise = readableStreamAddReadRequest(stream);125 readableByteStreamControllerCallPullIfNeeded(this);126 return promise;127 }128 [customInspect](): string {129 return `${this.constructor.name} { byobRequest: ${String(130 this.byobRequest131 )}, desiredSize: ${String(this.desiredSize)} }`;132 }133}134setFunctionName(135 ReadableByteStreamControllerImpl,136 "ReadableByteStreamController"...
ReadableByteStreamController.js
Source:ReadableByteStreamController.js
1/*2 * Copyright (C) 2016 Canon Inc.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)26function enqueue(chunk)27{28 "use strict";29 if (!@isReadableByteStreamController(this))30 throw @makeThisTypeError("ReadableByteStreamController", "enqueue");31 if (this.@closeRequested)32 @throwTypeError("ReadableByteStreamController is requested to close");33 if (this.@controlledReadableStream.@state !== @streamReadable)34 @throwTypeError("ReadableStream is not readable");35 if (!@isObject(chunk))36 @throwTypeError("Provided chunk is not an object");37 if (!@ArrayBuffer.@isView(chunk))38 @throwTypeError("Provided chunk is not an ArrayBuffer view");39 return @readableByteStreamControllerEnqueue(this, chunk);40}41function error(error)42{43 "use strict";44 if (!@isReadableByteStreamController(this))45 throw @makeThisTypeError("ReadableByteStreamController", "error");46 if (this.@controlledReadableStream.@state !== @streamReadable)47 @throwTypeError("ReadableStream is not readable");48 @readableByteStreamControllerError(this, error);49}50function close()51{52 "use strict";53 if (!@isReadableByteStreamController(this))54 throw @makeThisTypeError("ReadableByteStreamController", "close");55 if (this.@closeRequested)56 @throwTypeError("Close has already been requested");57 if (this.@controlledReadableStream.@state !== @streamReadable)58 @throwTypeError("ReadableStream is not readable");59 @readableByteStreamControllerClose(this);60}61@getter62function byobRequest()63{64 "use strict";65 if (!@isReadableByteStreamController(this))66 throw @makeGetterTypeError("ReadableByteStreamController", "byobRequest");67 if (this.@byobRequest === @undefined && this.@pendingPullIntos.length) {68 const firstDescriptor = this.@pendingPullIntos[0];69 const view = new @Uint8Array(firstDescriptor.buffer,70 firstDescriptor.byteOffset + firstDescriptor.bytesFilled,71 firstDescriptor.byteLength - firstDescriptor.bytesFilled);72 this.@byobRequest = new @ReadableStreamBYOBRequest(this, view);73 }74 return this.@byobRequest;75}76@getter77function desiredSize()78{79 "use strict";80 if (!@isReadableByteStreamController(this))81 throw @makeGetterTypeError("ReadableByteStreamController", "desiredSize");82 return @readableByteStreamControllerGetDesiredSize(this);...
Using AI Code Generation
1var rs = new ReadableStream({2 start(c) {3 var desiredSize = ReadableByteStreamControllerGetDesiredSize(c);4 assert_equals(desiredSize, 0, 'Desired size should be 0');5 c.enqueue(new Uint8Array(1));6 }7});8assert_equals(rs.desiredSize, 0, 'Desired size should be 0');9var reader = rs.getReader();10reader.read().then(function(result) {11 assert_false(result.done, 'Result should not be done');12 assert_equals(result.value.byteLength, 1, 'Result value should be 1 byte');13 assert_equals(rs.desiredSize, -1, 'Desired size should be -1');14 return reader.read();15}).then(function(result) {16 assert_true(result.done, 'Result should be done');17 assert_equals(rs.desiredSize, 0, 'Desired size should be 0');18 test.done();19}, test.unreached_func('Unexpected rejection'));20var rs = new ReadableStream({21 pull(c) {22 var view = c.byobRequest.view;23 view[0] = 1;24 c.byobRequest.respond(1);25 }26});27var reader = rs.getReader({ mode: 'byob' });28reader.read(new Uint8Array(1)).then(function(result) {29 assert_false(result.done, 'Result should not be done');30 assert_equals(result.value.byteLength, 1, 'Result value should be 1 byte');31 assert_equals(result.value[0], 1, 'Result value should be 1');32 return reader.read(new Uint8Array(1));33}).then(function(result) {34 assert_true(result.done, 'Result should be done');35 test.done();36}, test.unreached_func('Unexpected rejection'));37var rs = new ReadableStream({38 pull(c) {39 var view = c.byobRequest.view;40 view[0] = 1;41 c.byobRequest.respond(1);42 }43});44var reader = rs.getReader({ mode: 'byob' });45reader.read(new Uint8Array
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(c) {3 var desiredSize = ReadableByteStreamControllerGetDesiredSize(c);4 console.log(desiredSize);5 }6});7rs.getReader().read().then(function(v) { console.log(v); });
Using AI Code Generation
1test(function() {2 var rs = new ReadableStream({3 start: function(controller) {4 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),5 0, 'desiredSize should be 0');6 controller.enqueue(new ArrayBuffer(10));7 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),8 0, 'desiredSize should be 0');9 controller.close();10 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),11 0, 'desiredSize should be 0');12 }13 });14}, 'ReadableByteStreamControllerGetDesiredSize should return 0 if the stream is closed');15test(function() {16 var rs = new ReadableStream({17 start: function(controller) {18 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),19 0, 'desiredSize should be 0');20 controller.enqueue(new ArrayBuffer(10));21 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),22 0, 'desiredSize should be 0');23 controller.error();24 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),25 0, 'desiredSize should be 0');26 }27 });28}, 'ReadableByteStreamControllerGetDesiredSize should return 0 if the stream is errored');29test(function() {30 var rs = new ReadableStream({31 start: function(controller) {32 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),33 0, 'desiredSize should be 0');34 controller.enqueue(new ArrayBuffer(10));35 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),36 0, 'desiredSize should be 0');37 controller.close();38 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),39 0, 'desiredSize should be 0');40 controller.error();41 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),42 0, 'desiredSize should be 0');43 }44 });45}, 'ReadableByteStreamControllerGetDesiredSize should return 0 if the stream is closed and errored');
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);4 controller.enqueue(new Uint8Array(1));5 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);6 controller.enqueue(new Uint8Array(1));7 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);8 controller.enqueue(new Uint8Array(1));9 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);10 controller.enqueue(new Uint8Array(1));11 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);12 controller.enqueue(new Uint8Array(1));13 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);14 controller.enqueue(new Uint8Array(1));15 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);16 controller.enqueue(new Uint8Array(1));17 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);18 controller.enqueue(new Uint8Array(1));19 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);20 controller.enqueue(new Uint8Array(1));21 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);22 controller.enqueue(new Uint8Array(1));23 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);24 controller.enqueue(new Uint8Array(1));25 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller), 0);26 controller.close();27 }28});29rs.getReader();30var rs = new ReadableStream({31 start(controller) {32 assert_equals(ReadableByteStreamControllerGetDesiredSize(controller),
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue(new Uint8Array([0x00, 0x01]));4 }5});6var reader = rs.getReader();7reader.read().then(function(result) {8 var desiredSize = ReadableByteStreamControllerGetDesiredSize(reader._readableStreamController);9 assert_equals(desiredSize, 0, 'desiredSize should be 0');10 reader.releaseLock();11}).catch(unreached_rejection);12var rs = new ReadableStream({13 start(controller) {14 controller.enqueue('a');15 controller.enqueue('b');16 controller.enqueue('c');17 }18});19var reader = rs.getReader();20reader.read().then(function(result) {21 var desiredSize = ReadableStreamDefaultControllerGetDesiredSize(reader._readableStreamController);22 assert_equals(desiredSize, 2, 'desiredSize should be 2');23 reader.releaseLock();24}).catch(unreached_rejection);25var rs = new ReadableStream({26 start(controller) {27 var view = controller.byobRequest.view;28 view[0] = 0x00;29 view[1] = 0x01;30 controller.byobRequest.respond(2);31 }32});33var reader = rs.getReader({ mode: 'byob' });34var buffer = new ArrayBuffer(2);35reader.read(new Uint8Array(buffer)).then(function(result) {36 var desiredSize = ReadableStreamBYOBRequestGetDesiredSize(reader._readableStreamController._byobRequest);37 assert_equals(desiredSize, 0, 'desiredSize should be 0');38 reader.releaseLock();39}).catch(unreached_rejection);40var ws = new WritableStream({41 write(chunk) {42 return new Promise(resolve => setTimeout(resolve, 100));43 }44});45var writer = ws.getWriter();46var desiredSize = WritableStreamDefaultControllerGetDesiredSize(writer._writableStreamController);47assert_equals(desiredSize, 1, 'desiredSize should be 1');48writer.write('a').then(function() {
Using AI Code Generation
1promise_test(t => {2 let controller;3 const rs = new ReadableStream({4 start(c) {5 controller = c;6 }7 });8 return Promise.all([9 rs.getReader().read().then(result => assert_object_equals(result, { value: undefined, done: true })),10 rs.getReader().read().then(result => assert_object_equals(result, { value: undefined, done: true }))11 ]);12}, 'ReadableStream with byte source: getReader() twice');13promise_test(t => {14 let controller;15 const rs = new ReadableStream({16 start(c) {17 controller = c;18 }19 });20 const reader = rs.getReader();21 return reader.read().then(result => {22 assert_object_equals(result, { value: undefined, done: true });23 return reader.releaseLock();24 }).then(() => {25 return reader.closed;26 }).then(() => {27 assert_throws(new TypeError(), () => reader.read());28 });29}, 'ReadableStream with byte source: releaseLock()');30promise_test(t => {31 const rs = new ReadableStream({32 start(controller) {33 controller.enqueue(new Uint8Array([1, 2, 3]));34 controller.close();35 }36 });37 const reader = rs.getReader();38 return reader.read().then(result => {39 assert_object_equals(result, { value: new Uint8Array([1, 2, 3]), done: false });40 return reader.read();41 }).then(result => {42 assert_object_equals(result, { value: undefined, done: true });43 });44}, 'ReadableStream with byte source: read() after close()');45promise_test(t => {46 const rs = new ReadableStream({47 start(controller) {48 controller.enqueue(new Uint8Array([1, 2, 3]));49 controller.error();50 }51 });52 const reader = rs.getReader();53 return reader.read().then(result => {54 assert_object_equals(result, { value: new Uint8Array([1, 2, 3]), done: false });55 return reader.read();56 }).then(result => {57 assert_object_equals(result, { value:
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!!