Best JavaScript code snippet using wpt
readable_stream.ts
Source:readable_stream.ts
1// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.2import {3 acquireReadableStreamDefaultReader,4 initializeReadableStream,5 isReadableStream,6 isReadableStreamLocked,7 isUnderlyingByteSource,8 isWritableStream,9 isWritableStreamLocked,10 makeSizeAlgorithmFromSizeFunction,11 setFunctionName,12 setPromiseIsHandledToTrue,13 readableStreamCancel,14 ReadableStreamGenericReader,15 readableStreamPipeTo,16 readableStreamTee,17 setUpReadableByteStreamControllerFromUnderlyingSource,18 setUpReadableStreamDefaultControllerFromUnderlyingSource,19 validateAndNormalizeHighWaterMark,20} from "./internals.ts";21import { ReadableByteStreamControllerImpl } from "./readable_byte_stream_controller.ts";22import { ReadableStreamAsyncIteratorPrototype } from "./readable_stream_async_iterator.ts";23import { ReadableStreamDefaultControllerImpl } from "./readable_stream_default_controller.ts";24import * as sym from "./symbols.ts";25import { customInspect } from "../console.ts";26import { AbortSignalImpl } from "../abort_signal.ts";27// eslint-disable-next-line @typescript-eslint/no-explicit-any28export class ReadableStreamImpl<R = any> implements ReadableStream<R> {29 [sym.disturbed]: boolean;30 [sym.readableStreamController]:31 | ReadableStreamDefaultControllerImpl<R>32 | ReadableByteStreamControllerImpl;33 [sym.reader]: ReadableStreamGenericReader<R> | undefined;34 [sym.state]: "readable" | "closed" | "errored";35 // eslint-disable-next-line @typescript-eslint/no-explicit-any36 [sym.storedError]: any;37 constructor(38 underlyingSource: UnderlyingByteSource | UnderlyingSource<R> = {},39 strategy:40 | {41 highWaterMark?: number;42 size?: undefined;43 }44 | QueuingStrategy<R> = {}45 ) {46 initializeReadableStream(this);47 const { size } = strategy;48 let { highWaterMark } = strategy;49 const { type } = underlyingSource;50 if (isUnderlyingByteSource(underlyingSource)) {51 if (size !== undefined) {52 throw new RangeError(53 `When underlying source is "bytes", strategy.size must be undefined.`54 );55 }56 highWaterMark = validateAndNormalizeHighWaterMark(highWaterMark ?? 0);57 setUpReadableByteStreamControllerFromUnderlyingSource(58 this,59 underlyingSource,60 highWaterMark61 );62 } else if (type === undefined) {63 const sizeAlgorithm = makeSizeAlgorithmFromSizeFunction(size);64 highWaterMark = validateAndNormalizeHighWaterMark(highWaterMark ?? 1);65 setUpReadableStreamDefaultControllerFromUnderlyingSource(66 this,67 underlyingSource,68 highWaterMark,69 sizeAlgorithm70 );71 } else {72 throw new RangeError(73 `Valid values for underlyingSource are "bytes" or undefined. Received: "${type}".`74 );75 }76 }77 get locked(): boolean {78 if (!isReadableStream(this)) {79 throw new TypeError("Invalid ReadableStream.");80 }81 return isReadableStreamLocked(this);82 }83 // eslint-disable-next-line @typescript-eslint/no-explicit-any84 cancel(reason?: any): Promise<void> {85 if (!isReadableStream(this)) {86 return Promise.reject(new TypeError("Invalid ReadableStream."));87 }88 if (isReadableStreamLocked(this)) {89 return Promise.reject(90 new TypeError("Cannot cancel a locked ReadableStream.")91 );92 }93 return readableStreamCancel(this, reason);94 }95 getIterator({96 preventCancel,97 }: { preventCancel?: boolean } = {}): AsyncIterableIterator<R> {98 if (!isReadableStream(this)) {99 throw new TypeError("Invalid ReadableStream.");100 }101 const reader = acquireReadableStreamDefaultReader(this);102 const iterator = Object.create(ReadableStreamAsyncIteratorPrototype);103 iterator[sym.asyncIteratorReader] = reader;104 iterator[sym.preventCancel] = Boolean(preventCancel);105 return iterator;106 }107 getReader({ mode }: { mode?: string } = {}): ReadableStreamDefaultReader<R> {108 if (!isReadableStream(this)) {109 throw new TypeError("Invalid ReadableStream.");110 }111 if (mode === undefined) {112 return acquireReadableStreamDefaultReader(this, true);113 }114 mode = String(mode);115 // 3.2.5.4.4 If mode is "byob", return ? AcquireReadableStreamBYOBReader(this, true).116 throw new RangeError(`Unsupported mode "${mode}"`);117 }118 pipeThrough<T>(119 {120 writable,121 readable,122 }: {123 writable: WritableStream<R>;124 readable: ReadableStream<T>;125 },126 { preventClose, preventAbort, preventCancel, signal }: PipeOptions = {}127 ): ReadableStream<T> {128 if (!isReadableStream(this)) {129 throw new TypeError("Invalid ReadableStream.");130 }131 if (!isWritableStream(writable)) {132 throw new TypeError("writable is not a valid WritableStream.");133 }134 if (!isReadableStream(readable)) {135 throw new TypeError("readable is not a valid ReadableStream.");136 }137 preventClose = Boolean(preventClose);138 preventAbort = Boolean(preventAbort);139 preventCancel = Boolean(preventCancel);140 if (signal && !(signal instanceof AbortSignalImpl)) {141 throw new TypeError("Invalid signal.");142 }143 if (isReadableStreamLocked(this)) {144 throw new TypeError("ReadableStream is locked.");145 }146 if (isWritableStreamLocked(writable)) {147 throw new TypeError("writable is locked.");148 }149 const promise = readableStreamPipeTo(150 this,151 writable,152 preventClose,153 preventAbort,154 preventCancel,155 signal156 );157 setPromiseIsHandledToTrue(promise);158 return readable;159 }160 pipeTo(161 dest: WritableStream<R>,162 { preventClose, preventAbort, preventCancel, signal }: PipeOptions = {}163 ): Promise<void> {164 if (!isReadableStream(this)) {165 return Promise.reject(new TypeError("Invalid ReadableStream."));166 }167 if (!isWritableStream(dest)) {168 return Promise.reject(169 new TypeError("dest is not a valid WritableStream.")170 );171 }172 preventClose = Boolean(preventClose);173 preventAbort = Boolean(preventAbort);174 preventCancel = Boolean(preventCancel);175 if (signal && !(signal instanceof AbortSignalImpl)) {176 return Promise.reject(new TypeError("Invalid signal."));177 }178 if (isReadableStreamLocked(this)) {179 return Promise.reject(new TypeError("ReadableStream is locked."));180 }181 if (isWritableStreamLocked(dest)) {182 return Promise.reject(new TypeError("dest is locked."));183 }184 return readableStreamPipeTo(185 this,186 dest,187 preventClose,188 preventAbort,189 preventCancel,190 signal191 );192 }193 tee(): [ReadableStreamImpl<R>, ReadableStreamImpl<R>] {194 if (!isReadableStream(this)) {195 throw new TypeError("Invalid ReadableStream.");196 }197 return readableStreamTee(this, false);198 }199 [customInspect](): string {200 return `${this.constructor.name} { locked: ${String(this.locked)} }`;201 }202 [Symbol.asyncIterator](203 options: {204 preventCancel?: boolean;205 } = {}206 ): AsyncIterableIterator<R> {207 return this.getIterator(options);208 }209}...
tee.ts
Source:tee.ts
1import { CreateReadableStream, IsReadableStream, ReadableStream, ReadableStreamCancel } from '../readable-stream';2import { AcquireReadableStreamDefaultReader, ReadableStreamDefaultReaderRead, ReadRequest } from './default-reader';3import assert from '../../stub/assert';4import { newPromise, promiseResolvedWith, queueMicrotask, uponRejection } from '../helpers/webidl';5import {6 ReadableStreamDefaultController,7 ReadableStreamDefaultControllerClose,8 ReadableStreamDefaultControllerEnqueue,9 ReadableStreamDefaultControllerError10} from './default-controller';11import { CreateArrayFromList } from '../abstract-ops/ecmascript';12export function ReadableStreamTee<R>(stream: ReadableStream<R>,13 cloneForBranch2: boolean): [ReadableStream<R>, ReadableStream<R>] {14 assert(IsReadableStream(stream));15 assert(typeof cloneForBranch2 === 'boolean');16 const reader = AcquireReadableStreamDefaultReader<R>(stream);17 let reading = false;18 let canceled1 = false;19 let canceled2 = false;20 let reason1: any;21 let reason2: any;22 let branch1: ReadableStream<R>;23 let branch2: ReadableStream<R>;24 let resolveCancelPromise: (reason: any) => void;25 const cancelPromise = newPromise<any>(resolve => {26 resolveCancelPromise = resolve;27 });28 function pullAlgorithm(): Promise<void> {29 if (reading) {30 return promiseResolvedWith(undefined);31 }32 reading = true;33 const readRequest: ReadRequest<R> = {34 _chunkSteps: value => {35 // This needs to be delayed a microtask because it takes at least a microtask to detect errors (using36 // reader._closedPromise below), and we want errors in stream to error both branches immediately. We cannot let37 // successful synchronously-available reads get ahead of asynchronously-available errors.38 queueMicrotask(() => {39 reading = false;40 const value1 = value;41 const value2 = value;42 // There is no way to access the cloning code right now in the reference implementation.43 // If we add one then we'll need an implementation for serializable objects.44 // if (!canceled2 && cloneForBranch2) {45 // value2 = StructuredDeserialize(StructuredSerialize(value2));46 // }47 if (!canceled1) {48 ReadableStreamDefaultControllerEnqueue(49 branch1._readableStreamController as ReadableStreamDefaultController<R>,50 value151 );52 }53 if (!canceled2) {54 ReadableStreamDefaultControllerEnqueue(55 branch2._readableStreamController as ReadableStreamDefaultController<R>,56 value257 );58 }59 });60 },61 _closeSteps: () => {62 reading = false;63 if (!canceled1) {64 ReadableStreamDefaultControllerClose(branch1._readableStreamController as ReadableStreamDefaultController<R>);65 }66 if (!canceled2) {67 ReadableStreamDefaultControllerClose(branch2._readableStreamController as ReadableStreamDefaultController<R>);68 }69 },70 _errorSteps: () => {71 reading = false;72 }73 };74 ReadableStreamDefaultReaderRead(reader, readRequest);75 return promiseResolvedWith(undefined);76 }77 function cancel1Algorithm(reason: any): Promise<void> {78 canceled1 = true;79 reason1 = reason;80 if (canceled2) {81 const compositeReason = CreateArrayFromList([reason1, reason2]);82 const cancelResult = ReadableStreamCancel(stream, compositeReason);83 resolveCancelPromise(cancelResult);84 }85 return cancelPromise;86 }87 function cancel2Algorithm(reason: any): Promise<void> {88 canceled2 = true;89 reason2 = reason;90 if (canceled1) {91 const compositeReason = CreateArrayFromList([reason1, reason2]);92 const cancelResult = ReadableStreamCancel(stream, compositeReason);93 resolveCancelPromise(cancelResult);94 }95 return cancelPromise;96 }97 function startAlgorithm() {98 // do nothing99 }100 branch1 = CreateReadableStream(startAlgorithm, pullAlgorithm, cancel1Algorithm);101 branch2 = CreateReadableStream(startAlgorithm, pullAlgorithm, cancel2Algorithm);102 uponRejection(reader._closedPromise, (r: any) => {103 ReadableStreamDefaultControllerError(branch1._readableStreamController as ReadableStreamDefaultController<R>, r);104 ReadableStreamDefaultControllerError(branch2._readableStreamController as ReadableStreamDefaultController<R>, r);105 });106 return [branch1, branch2];...
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.enqueue("c");6 }7});8var reader = rs.getReader();9var read1 = reader.read();10var read2 = reader.read();11var read3 = reader.read();12var read4 = reader.read();13var readAll = Promise.all([read1, read2, read3, read4]).then(function (values) {14 console.log(values);15});16test.js:12 Uncaught (in promise) TypeError: Cannot read property 'read' of undefined17I’m not sure if this is a bug or not, but it seems like it. I’m using the latest version of wpt (0.2.2) and I’m getting the following error:18 at ReadableStreamDefaultControllerCallPullIfNeeded (readable-stream.js:1028)19 at ReadableStreamDefaultControllerClose (readable-stream.js:1038)20 at ReadableStreamDefaultController.error (readable-stream.js:1047)
Using AI Code Generation
1const rs = new ReadableStream({2 start(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 controller.enqueue('c');6 }7});8const reader = rs.getReader();9const read1 = reader.read();10const read2 = reader.read();11const read3 = reader.read();12read1.then(result1 => {13 console.log(result1);14 read2.then(result2 => {15 console.log(result2);16 read3.then(result3 => {17 console.log(result3);18 });19 });20});21const rs = new ReadableStream({22 start(controller) {23 controller.enqueue(new Uint8Array([0, 1, 2]).buffer);24 controller.enqueue(new Uint8Array([3, 4, 5]).buffer);25 controller.enqueue(new Uint8Array([6, 7]).buffer);26 }27});28const reader = rs.getReader({ mode: 'byob' });29const view1 = new Uint8Array(3);30const readInto1 = reader.read(view1);31const view2 = new Uint8Array(3);32const readInto2 = reader.read(view2);33const view3 = new Uint8Array(3);34const readInto3 = reader.read(view3);35readInto1.then(result1 => {36 console.log(result1);37 readInto2.then(result2 => {38 console.log(result2);39 readInto3.then(result3 => {40 console.log(result3);41 });42 });43});44new ReadableStream([underlyingSource[, strategy]])
Using AI Code Generation
1const { ReadableStream } = require('stream/web');2const reader = new ReadableStream().getReader();3assert(reader instanceof ReadableStreamDefaultReader);4const { ReadableStream } = require('stream/web');5const reader = new ReadableStream({6}).getReader({ mode: 'byob' });7assert(reader instanceof ReadableStreamBYOBReader);8const { ReadableStream, ReadableStreamDefaultReader } = require('stream/web');9const rs = new ReadableStream({10 start(c) {11 c.enqueue('a');12 c.enqueue('b');13 c.enqueue('c');14 }15});16const reader = new ReadableStreamDefaultReader(rs);17const read = await reader.read();18const { ReadableStream, ReadableStreamBYOBReader } = require('stream/web');19const rs = new ReadableStream({20 pull(c) {21 c.enqueue(new Uint8Array([97, 98, 99]).buffer);22 }23});24const reader = new ReadableStreamBYOBReader(rs);25const ab = new ArrayBuffer(2);26const view = new Uint8Array(ab);27const read = await reader.read(view);28const { ReadableStream, ReadableStreamDefaultController } = require('stream/web');29const rs = new ReadableStream({30 start(c) {31 }32});33const reader = rs.getReader();34const { ReadableStream, ReadableStreamDefaultController } = require('stream/web');35const rs = new ReadableStream({36 start(c) {37 }38});39const reader = rs.getReader();40const { ReadableStream, ReadableStreamDefaultController } = require('stream/web
Using AI Code Generation
1test(() => {2 const stream = new ReadableStream();3 const reader = stream.getReader();4 assert_true(reader instanceof ReadableStreamDefaultReader);5}, 'ReadableStream.getReader() returns a ReadableStreamDefaultReader');6test(() => {7 const stream = new ReadableStream();8 const reader = stream.getReader({mode: 'byob'});9 assert_true(reader instanceof ReadableStreamBYOBReader);10}, 'ReadableStream.getReader({mode: "byob"}) returns a ReadableStreamBYOBReader');11test(() => {12 const stream = new ReadableStream();13 const reader = stream.getReader({mode: 'byob'});14 assert_true(reader instanceof ReadableStreamBYOBReader);15}, 'ReadableStream.getReader({mode: "byob"}) returns a ReadableStreamBYOBReader');16test(() => {17 const stream = new ReadableStream();18 const reader = stream.getReader({mode: 'byob'});19 assert_true(reader instanceof ReadableStreamBYOBReader);20}, 'ReadableStream.getReader({mode: "byob"}) returns a ReadableStreamBYOBReader');21test(() => {22 const stream = new ReadableStream();23 const reader = stream.getReader({mode: 'byob'});24 assert_true(reader instanceof ReadableStreamBYOBReader);25}, 'ReadableStream.getReader({mode: "byob"}) returns a ReadableStreamBYOBReader');26test(() => {27 const stream = new ReadableStream();28 const reader = stream.getReader({mode: 'byob'});29 assert_true(reader instanceof ReadableStreamBYOBReader);30}, 'ReadableStream.getReader({mode: "byob"}) returns a ReadableStreamBYOBReader');31test(() => {32 const stream = new ReadableStream();
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});8const reader = stream.getReader();9reader.read().then(result1 => {10 return reader.read();11}).then(result2 => {12 return reader.read();13}).then(result3 => {14 return reader.read();15}).then(result4 => {16});17const stream = new WritableStream({18 write(chunk, controller) {19 console.log(chunk);20 }21});22const writer = stream.getWriter();23writer.write('a');24writer.write('b');25writer.write('c');26writer.close();27const stream = new ReadableStream({28 pull(controller) {29 networkPullInto(controller.byobRequest.view).then(bytesRead => {30 controller.byobRequest.respond(bytesRead);31 });32 }33});34const reader = stream.getReader({mode: 'byob'});35const view = new Uint8Array(4);36reader.read(view).then(result => {37 const bytes = result.value;38 const done = result.done;39 console.log(bytes);40 console.log(done);41});42const stream = new ReadableStream({43 pull(controller) {44 networkPullInto(controller.byobRequest.view).then(bytesRead => {45 controller.byobRequest.respond(bytesRead);46 });47 }48});49const reader = stream.getReader({mode: '
Using AI Code Generation
1var test = async_test("AcquireReadableStreamDefaultReader method of wpt");2var readableStream = new ReadableStream({3 start: function(controller) {4 controller.enqueue("a");5 controller.enqueue("b");6 controller.enqueue("c");7 }8});9var reader = readableStream.getReader();10test.step(function() {11 assert_equals(reader.read().value, "a", "read() should return the first chunk");12 assert_equals(reader.read().value, "b", "read() should return the second chunk");13 assert_equals(reader.read().value, "c", "read() should return the last chunk");14 assert_equals(reader.read().done, true, "read() should return done");15 test.done();16});17var test = async_test("AcquireReadableStreamDefaultReader method of wpt");18var readableStream = new ReadableStream({19 start: function(controller) {20 controller.enqueue("a");21 controller.enqueue("b");22 controller.enqueue("c");23 }24});25var reader = readableStream.getReader();26test.step(function() {27 assert_equals(reader.read().value, "a", "read() should return the first chunk");28 assert_equals(reader.read().value, "b", "read() should return the second chunk");29 assert_equals(reader.read().value, "c", "read() should return the last chunk");30 assert_equals(reader.read().done, true, "read() should return done");31 test.done();32});33var test = async_test("Acquire
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.enqueue("c");6 }7});8var reader = rs.getReader();9var rs = new ReadableStream({10 start(controller) {11 controller.enqueue(new Uint8Array([0x01]));12 controller.enqueue(new Uint8Array([0x02]));13 controller.enqueue(new Uint8Array([0x03]));14 }15});16var reader = rs.getReader({
Using AI Code Generation
1var rs = new ReadableStream();2var reader = rs.getReader();3assert_equals(reader.read(), undefined);4assert_equals(reader.closed, undefined);5assert_equals(reader.read(), undefined);6var rs = new ReadableStream();7var reader = rs.getReader();8var promise = reader.read();9assert_equals(promise.constructor, Promise);10promise.then(function(result) {11 assert_equals(result.value, 'value');12 assert_equals(result.done, false);13});14var rs = new ReadableStream();15var reader = rs.getReader();16reader.releaseLock();17assert_equals(reader.read(), undefined);18assert_equals(reader.closed, undefined);19assert_equals(reader.read(), undefined);20var rs = new ReadableStream();21var reader = rs.getReader();22var promise = reader.cancel('reason');23assert_equals(promise.constructor, Promise);24promise.then(function(result) {25 assert_equals(result, undefined);26});27var rs = new ReadableStream({28 start: function(c) {29 c.close();30 }31});32var reader = rs.getReader();33var promise = reader.read();34assert_equals(promise.constructor, Promise);35promise.then(function(result) {36 assert_equals(result.value, undefined);37 assert_equals(result.done, true);38});39var rs = new ReadableStream({40 start: function(c) {41 c.enqueue('a');42 c.enqueue('b');43 c.enqueue('c');44 }45});46var reader = rs.getReader();47var promise = reader.read();48assert_equals(promise.constructor, Promise);49promise.then(function(result) {50 assert_equals(result.value, 'a');51 assert_equals(result.done, false);52 return reader.read();53}).then(function(result) {
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!!