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"...
readable_stream_default_controller.ts
Source:readable_stream_default_controller.ts
1// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.2import {3 CancelAlgorithm,4 dequeueValue,5 isReadableStreamDefaultController,6 Pair,7 PullAlgorithm,8 readableStreamAddReadRequest,9 readableStreamClose,10 readableStreamCreateReadResult,11 readableStreamDefaultControllerCallPullIfNeeded,12 readableStreamDefaultControllerCanCloseOrEnqueue,13 readableStreamDefaultControllerClearAlgorithms,14 readableStreamDefaultControllerClose,15 readableStreamDefaultControllerEnqueue,16 readableStreamDefaultControllerError,17 readableStreamDefaultControllerGetDesiredSize,18 resetQueue,19 SizeAlgorithm,20 setFunctionName,21} from "./internals.ts";22import { ReadableStreamImpl } from "./readable_stream.ts";23import * as sym from "./symbols.ts";24import { customInspect } from "../console.ts";25// eslint-disable-next-line @typescript-eslint/no-explicit-any26export class ReadableStreamDefaultControllerImpl<R = any>27 implements ReadableStreamDefaultController<R> {28 [sym.cancelAlgorithm]: CancelAlgorithm;29 [sym.closeRequested]: boolean;30 [sym.controlledReadableStream]: ReadableStreamImpl<R>;31 [sym.pullAgain]: boolean;32 [sym.pullAlgorithm]: PullAlgorithm;33 [sym.pulling]: boolean;34 [sym.queue]: Array<Pair<R>>;35 [sym.queueTotalSize]: number;36 [sym.started]: boolean;37 [sym.strategyHWM]: number;38 [sym.strategySizeAlgorithm]: SizeAlgorithm<R>;39 private constructor() {40 throw new TypeError(41 "ReadableStreamDefaultController's constructor cannot be called."42 );43 }44 get desiredSize(): number | null {45 if (!isReadableStreamDefaultController(this)) {46 throw new TypeError("Invalid ReadableStreamDefaultController.");47 }48 return readableStreamDefaultControllerGetDesiredSize(this);49 }50 close(): void {51 if (!isReadableStreamDefaultController(this)) {52 throw new TypeError("Invalid ReadableStreamDefaultController.");53 }54 if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) {55 throw new TypeError(56 "ReadableStreamDefaultController cannot close or enqueue."57 );58 }59 readableStreamDefaultControllerClose(this);60 }61 enqueue(chunk: R): void {62 if (!isReadableStreamDefaultController(this)) {63 throw new TypeError("Invalid ReadableStreamDefaultController.");64 }65 if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) {66 throw new TypeError("ReadableSteamController cannot enqueue.");67 }68 return readableStreamDefaultControllerEnqueue(this, chunk);69 }70 // eslint-disable-next-line @typescript-eslint/no-explicit-any71 error(error?: any): void {72 if (!isReadableStreamDefaultController(this)) {73 throw new TypeError("Invalid ReadableStreamDefaultController.");74 }75 readableStreamDefaultControllerError(this, error);76 }77 // eslint-disable-next-line @typescript-eslint/no-explicit-any78 [sym.cancelSteps](reason?: any): PromiseLike<void> {79 resetQueue(this);80 const result = this[sym.cancelAlgorithm](reason);81 readableStreamDefaultControllerClearAlgorithms(this);82 return result;83 }84 [sym.pullSteps](): Promise<ReadableStreamReadResult<R>> {85 const stream = this[sym.controlledReadableStream];86 if (this[sym.queue].length) {87 const chunk = dequeueValue<R>(this);88 if (this[sym.closeRequested] && this[sym.queue].length === 0) {89 readableStreamDefaultControllerClearAlgorithms(this);90 readableStreamClose(stream);91 } else {92 readableStreamDefaultControllerCallPullIfNeeded(this);93 }94 return Promise.resolve(95 readableStreamCreateReadResult(96 chunk,97 false,98 stream[sym.reader]![sym.forAuthorCode]99 )100 );101 }102 const pendingPromise = readableStreamAddReadRequest(stream);103 readableStreamDefaultControllerCallPullIfNeeded(this);104 return pendingPromise;105 }106 [customInspect](): string {107 return `${this.constructor.name} { desiredSize: ${String(108 this.desiredSize109 )} }`;110 }111}112setFunctionName(113 ReadableStreamDefaultControllerImpl,114 "ReadableStreamDefaultController"...
Using AI Code Generation
1const rs = new ReadableStream({2 start(controller) {3 controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));4 controller.close();5 }6});7const reader = rs.getReader();8const readRequest = {9 chunkSteps() {},10 closeSteps() {},11 errorSteps() {}12};13ReadableStreamAddReadRequest(reader, readRequest);14assert_equals(reader._readRequests.length, 1);15assert_equals(reader._readRequests[0], readRequest);16done();
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.close();6 }7});8var reader = rs.getReader();9var read = reader.read();10read.then(result => {11 console.log(result.value);12 console.log(result.done);13});14read.then(result => {15 console.log(result.value);16 console.log(result.done);17});18var rs = new ReadableStream({19 start(controller) {20 controller.close();21 }22});23var reader = rs.getReader();24var read = reader.read();25read.then(result => {26 console.log(result.value);27 console.log(result.done);28});29var rs = new ReadableStream({30 start(controller) {31 controller.enqueue("a");32 controller.enqueue("b");33 }34});35var reader = rs.getReader();36var read = reader.read();37read.then(result => {38 console.log(result.value);39 console.log(result.done);40});41read = reader.read();42read.then(result => {43 console.log(result.value);44 console.log(result.done);45});46var rs = new ReadableStream({47 start(controller) {48 controller.error("boo!");49 }50});51var reader = rs.getReader();52var read = reader.read();53read.catch(e => {54 console.error(e);55});56var rs = new ReadableStream({57 start(controller) {58 controller.enqueue("a");59 controller.enqueue("b");60 }61});62var reader = rs.getReader();63var read = reader.read();64read.then(result => {65 console.log(result.value);66 console.log(result.done);67});68read = reader.read();69read.then(result => {70 console.log(result.value);71 console.log(result.done);72});
Using AI Code Generation
1const stream = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.enqueue("c");6 }7});8let reader = stream.getReader();9reader.read().then(result1 => {10 console.log(result1.value);11 reader.read().then(result2 => {12 console.log(result2.value);13 reader.read().then(result3 => {14 console.log(result3.value);15 reader.read().then(result4 => {16 console.log(result4.done);17 });18 });19 });20});21const stream = new ReadableStream({22 start(controller) {23 controller.close();24 }25});26const reader = stream.getReader();27reader.read().then(result => {28 console.log(result.done);29});30const stream = new ReadableStream({31 start(controller) {32 controller.enqueue("a");33 controller.enqueue("b");34 controller.enqueue("c");35 }36});37const reader = stream.getReader();38reader.read().then(result1 => {39 console.log(result1.value);40 reader.read().then(result2 => {41 console.log(result2.value);42 reader.read().then(result3 => {43 console.log(result3.value);44 reader.read().then(result4 => {45 console.log(result4.done);46 });47 });48 });49});50const stream = new ReadableStream({51 start(controller) {52 controller.error("kaboom");53 }54});55const reader = stream.getReader();56reader.read().catch(e => {57 console.log(e);58});59const stream = new ReadableStream({60 start(controller) {61 console.log(controller.desiredSize);62 controller.enqueue("a");63 console.log(controller.desired
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.enqueue("c");6 controller.close();7 }8});9var reader = rs.getReader();10var readRequest = new ReadableStreamAddReadRequest(rs, reader);11assert_equals(readRequest.chunk, "a");12assert_equals(readRequest.done, false);13readRequest = new ReadableStreamAddReadRequest(rs, reader);14assert_equals(readRequest.chunk, "b");15assert_equals(readRequest.done, false);16readRequest = new ReadableStreamAddReadRequest(rs, reader);17assert_equals(readRequest.chunk, "c");18assert_equals(readRequest.done, false);19readRequest = new ReadableStreamAddReadRequest(rs, reader);20assert_equals(readRequest.chunk, undefined);21assert_equals(readRequest.done, true);22done();23var rs = new ReadableStream({24 start(controller) {25 }26});27var controller = rs.getReader().readableStreamController;28ReadableStreamDefaultControllerGetDesiredSizeTest(controller);
Using AI Code Generation
1var rs = new ReadableStream();2var controller = rs.getReader().releaseLock().closed;3var pullCount = 0;4var pull = function() {5 pullCount++;6 if (pullCount == 1) {7 controller.enqueue('a');8 controller.enqueue('b');9 controller.enqueue('c');10 } else if (pullCount == 2) {11 assert_throws(new TypeError(), function() {12 controller.enqueue('d');13 }, 'enqueue() should throw a TypeError if the stream is closed');14 } else if (pullCount == 3) {15 assert_throws(new TypeError(), function() {16 controller.close();17 }, 'close() should throw a TypeError if the stream is closed');18 } else if (pullCount == 4) {19 assert_throws(new TypeError(), function() {20 controller.error();21 }, 'error() should throw a TypeError if the stream is closed');22 } else {23 assert_unreached('The pull() method should not be called more than 4 times');24 }25};26var cancelCount = 0;27var cancel = function() {28 cancelCount++;29 assert_equals(cancelCount, 1, 'cancel() should only be called once');30};31rs = new ReadableStream({32 start: function(c) {33 controller = c;34 },35});36rs.getReader().closed37 .then(function() {38 assert_equals(pullCount, 1, 'pull() should be called once');39 assert_equals(cancelCount, 1, 'cancel() should be called once');40 })41 .then($DONE, $DONE);
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!!