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
1const { ReadableStream, CountQueuingStrategy } = require('stream/web');2const { ReadableByteStreamControllerEnqueue } = require('stream/web');3const { ReadableByteStreamControllerClose } = require('stream/web');4const { ReadableByteStreamControllerError } = require('stream/web');5const { ReadableByteStreamControllerGetDesiredSize } = require('stream/web');6const { ReadableByteStreamControllerGetTotalQueuedSize } = require('stream/web');7const rs = new ReadableStream({8 pull(controller) {9 console.log('pull()');10 controller.enqueue('a');11 controller.enqueue('b');12 controller.enqueue('c');13 controller.close();14 },15});16rs.getReader({ mode: 'byob' })17 .read(new Uint8Array(4))18 .then(({ value, done }) => {19 console.log(value);20 console.log(done);21 });22pull()23Uint8Array(4) [ 97, 98, 99, 0 ]24const { ReadableStream, CountQueuingStrategy } = require('stream/web');25const { ReadableByteStreamControllerEnqueue } = require('stream/web');26const { ReadableByteStreamControllerClose } = require('stream/web');27const { ReadableByteStreamControllerError } = require('stream/web');28const { ReadableByteStreamControllerGetDesiredSize } = require('stream/web');29const { ReadableByteStreamControllerGetTotalQueuedSize } = require('stream/web');30const rs = new ReadableStream({31 pull(controller) {32 console.log('pull()');33 controller.enqueue('a');34 controller.enqueue('b');35 controller.enqueue('c');36 controller.error(new Error("I'm a teapot"));37 },38});39rs.getReader({ mode: 'byob' })40 .read(new Uint8Array(4))41 .then(({ value, done }) => {42 console.log(value);43 console.log(done);44 })45 .catch(e => console.log(e));46pull()
Using AI Code Generation
1const {ReadableStream, CountQueuingStrategy} = require('stream/web');2const {ReadableByteStreamControllerEnqueue, ReadableByteStreamControllerClose, ReadableByteStreamControllerError} = require('stream/web');3const {ReadableStreamDefaultControllerEnqueue, ReadableStreamDefaultControllerClose, ReadableStreamDefaultControllerError} = require('stream/web');4const {ReadableStreamBYOBReaderRead, ReadableStreamBYOBReaderReleaseLock} = require('stream/web');5const {ReadableStreamDefaultReaderRead, ReadableStreamDefaultReaderReleaseLock} = require('stream/web');6const {ReadableStreamCancel} = require('stream/web');7const {ReadableStreamClose, ReadableStreamError} = require('stream/web');8const {ReadableStreamTee} = require('stream/web');9const {isReadableStream, isReadableStreamDisturbed, isReadableStreamLocked} = require('stream/web');10const {isReadableStreamDefaultReader, isReadableStreamBYOBReader} = require('stream/web');11const {isReadableStreamDefaultController, isReadableByteStreamController} = require('stream/web');12const {ReadableStreamDefaultController} = require('stream/web');13const {ReadableByteStreamController} = require('stream/web');
Using AI Code Generation
1var rs = new ReadableStream({2 start: function(controller) {3 controller.enqueue(new Uint8Array([0x01, 0x02]));4 controller.enqueue(new Uint8Array([0x03, 0x04]));5 controller.close();6 }7});8rs.getReader().read().then(function(result) {9 assert_array_equals(result.value, new Uint8Array([0x01, 0x02]), "The value should be [0x01, 0x02]");10 return rs.getReader().read();11}).then(function(result) {12 assert_array_equals(result.value, new Uint8Array([0x03, 0x04]), "The value should be [0x03, 0x04]");13 return rs.getReader().read();14}).then(function(result) {15 assert_true(result.done, "The stream should be done");16});17var rs = new ReadableStream({18 start: function(controller) {19 var buffer = new ArrayBuffer(4);20 var view = new Uint8Array(buffer);21 view[0] = 0x01;22 view[1] = 0x02;23 view[2] = 0x03;24 view[3] = 0x04;25 detachArrayBuffer(buffer);26 controller.enqueue(view);27 controller.close();28 }29});30rs.getReader().read().then(function(result) {31 assert_array_equals(result.value, new Uint8Array([0x01, 0x02, 0x03, 0x04]),32 "The value should be [0x01, 0x02, 0x03, 0x04]");33 return rs.getReader().read();34}).then(function(result) {35 assert_true(result.done, "The stream should be done");36});
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue(new Uint8Array([0x01]));4 }5});6rs.getReader().read().then(result => {7 console.log(result.value);8 console.log(result.done);9});10TypeError: undefined is not an object (evaluating 'this._underlyingSource.start')11I have tried this in Safari 11.1.2 and also in Safari Technology Preview Release 67 (Safari 12.0, WebKit 14606.1.4.1)12I have tried this in Safari 11.1.2 and also in Safari Technology Preview Release 67 (Safari 12.0, WebKit 14606.1.4.1)13I have tried this in Safari 11.1.2 and also in Safari Technology Preview Release 67 (Safari 12.0, WebKit 14606.1.4.1)14I have tried this in Safari 11.1.2 and also in Safari Technology Preview Release 67 (Safari 12.0, WebKit 14606.1.4.1)15I have tried this in Safari 11.1.2 and also in Safari Technology Preview Release 67 (Safari 12.0, WebKit 14606.1.4.1)16I have tried this in Safari 11.1.2 and also in Safari Technology Preview Release 67 (Safari 12.0, WebKit 14606.1.4.1)
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(controller) {3 var buffer = new ArrayBuffer(4);4 var view = new Uint8Array(buffer);5 view[0] = 0;6 view[1] = 1;7 view[2] = 2;8 view[3] = 3;9 controller.enqueue(buffer);10 }11});12rs.getReader().read().then(function(result) {13 var view = new Uint8Array(result.value);14 assert_equals(view[0], 0);15 assert_equals(view[1], 1);16 assert_equals(view[2], 2);17 assert_equals(view[3], 3);18 assert_true(result.done);19}).catch(unreached_rejection(t));20rs.getReader().read().then(function(result) {21 assert_equals(result.value, undefined);22 assert_true(result.done);23}).catch(unreached_rejection(t));24var rs = new ReadableStream({25 pull: function(controller) {26 var buffer = new ArrayBuffer(4);27 var view = new Uint8Array(buffer);28 view[0] = 0;29 view[1] = 1;30 view[2] = 2;31 view[3] = 3;32 controller.enqueue(buffer);33 }34});35var reader = rs.getReader({ mode: "byob" });36reader.read(new Uint8Array(4)).then(function(result) {37 var view = new Uint8Array(result.value);38 assert_equals(view[0], 0);39 assert_equals(view[1], 1);40 assert_equals(view[2], 2);41 assert_equals(view[3], 3);42 assert_true(result.done);43}).catch(unreached_rejection(t));44reader.read(new Uint8Array(4)).then(function(result) {45 assert_equals(result.value, undefined);46 assert_true(result.done);47}).catch(unreached_rejection(t));48var rs = new ReadableStream({
Using AI Code Generation
1var rs = new ReadableStream({2 pull: function(c) {3 c.enqueue(new Uint8Array([0, 1, 2, 3]));4 }5});6rs.getReader().read().then(function(result) {7 console.log(result.value);8});9var rs = new ReadableStream({10 pull: function(c) {11 var view = c.byobRequest.view;12 view[0] = 0;13 view[1] = 1;14 view[2] = 2;15 view[3] = 3;16 c.byobRequest.respond(4);17 }18});19rs.getReader({mode: "byob"}).read(new Uint8Array(4)).then(function(result) {20 console.log(result.value);21});22var rs = new ReadableStream({23 pull: function(c) {24 var firstView = c.byobRequest.view;25 var secondView = new Uint8Array(firstView.buffer, firstView.byteOffset + 1, 3);26 secondView[0] = 0;27 secondView[1] = 1;28 secondView[2] = 2;29 c.byobRequest.respondWithNewView(secondView);30 }31});32rs.getReader({mode: "byob"}).read(new Uint8Array(4)).then(function(result) {33 console.log(result.value);34});
Using AI Code Generation
1test(() => {2 const controller = new ReadableByteStreamController();3 const buffer = new ArrayBuffer(0);4 controller.enqueue(buffer);5}, 'ReadableByteStreamControllerEnqueue should not throw');6test(() => {7 const controller = new ReadableByteStreamController();8 const e = new Error('error');9 controller.error(e);10}, 'ReadableByteStreamControllerError should not throw');11test(() => {12 const stream = new ReadableStream({13 pull(controller) {14 const view = controller.byobRequest.view;15 view[0] = 0x01;16 controller.byobRequest.respond(1);17 }18 });19 const reader = stream.getReader({ mode: 'byob' });20 return reader.read(new Uint8Array(1)).then(result => {21 assert_array_equals(result.value, new Uint8Array([0x01]));22 assert_true(result.done);23 });24}, 'ReadableStreamBYOBRequestRespond should not throw');25test(() => {26 const stream = new ReadableStream({27 pull(controller) {28 const view = controller.byobRequest.view;29 view[0] = 0x01;30 controller.byobRequest.respondWithNewView(new Uint8Array(1));31 }32 });33 const reader = stream.getReader({ mode: 'byob' });34 return reader.read(new Uint8Array(1)).then(result => {35 assert_array_equals(result.value, new Uint8Array([0x01]));36 assert_true(result.done);37 });38}, 'ReadableStreamBYOBRequestRespondWithNewView should not throw');39test(() => {40 const stream = new ReadableStream({41 pull(controller) {42 assert_equals(stream._getNumReadIntoRequests(), 1);43 controller.close();44 }45 });46 const reader = stream.getReader({ mode: 'byob' });47 return reader.read(new Uint8Array(1)).then(result => {48 assert_true(result.done);49 assert_equals(stream._getNumReadIntoRequests(),
Using AI Code Generation
1var rs = new ReadableStream();2var controller = new ReadableByteStreamController(rs, {});3var chunk = new Uint8Array([1,2,3]);4ReadableByteStreamControllerEnqueue(controller, chunk);5ReadableByteStreamControllerClose(controller);6ReadableByteStreamControllerEnqueue(controller, chunk);7ReadableByteStreamControllerClose(controller);8ReadableByteStreamControllerEnqueue(controller, chunk);9ReadableByteStreamControllerError(controller, new Error());10ReadableByteStreamControllerEnqueue(controller, chunk);11ReadableByteStreamControllerError(controller, new Error());12ReadableByteStreamControllerEnqueue(controller, chunk);
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!!