Best JavaScript code snippet using wpt
transfer.js
Source:transfer.js
1'use strict';2const {3 ObjectDefineProperties,4 PromiseResolve,5 ReflectConstruct,6} = primordials;7const {8 kState,9 setPromiseHandled,10} = require('internal/webstreams/util');11const {12 DOMException,13} = internalBinding('messaging');14const {15 ReadableStream,16 readableStreamDefaultControllerEnqueue,17 readableStreamDefaultControllerClose,18 readableStreamDefaultControllerError,19 readableStreamPipeTo,20} = require('internal/webstreams/readablestream');21const {22 WritableStream,23 writableStreamDefaultControllerErrorIfNeeded,24} = require('internal/webstreams/writablestream');25const {26 createDeferredPromise,27} = require('internal/util');28const assert = require('internal/assert');29const {30 makeTransferable,31 kClone,32 kDeserialize,33} = require('internal/worker/js_transferable');34// This class is a bit of a hack. The Node.js implementation of35// DOMException is not transferable/cloneable. This provides us36// with a variant that is. Unfortunately, it means playing around37// a bit with the message, name, and code properties and the38// prototype. We can revisit this if DOMException is ever made39// properly cloneable.40class CloneableDOMException extends DOMException {41 constructor(message, name) {42 super(message, name);43 this[kDeserialize]({44 message: this.message,45 name: this.name,46 code: this.code,47 });48 // eslint-disable-next-line no-constructor-return49 return makeTransferable(this);50 }51 [kClone]() {52 return {53 data: {54 message: this.message,55 name: this.name,56 code: this.code,57 },58 deserializeInfo:59 'internal/webstreams/transfer:InternalCloneableDOMException'60 };61 }62 [kDeserialize]({ message, name, code }) {63 ObjectDefineProperties(this, {64 message: {65 configurable: true,66 enumerable: true,67 get() { return message; },68 },69 name: {70 configurable: true,71 enumerable: true,72 get() { return name; },73 },74 code: {75 configurable: true,76 enumerable: true,77 get() { return code; },78 },79 });80 }81}82function InternalCloneableDOMException() {83 return makeTransferable(84 ReflectConstruct(85 CloneableDOMException,86 [],87 DOMException));88}89InternalCloneableDOMException[kDeserialize] = () => {};90class CrossRealmTransformReadableSource {91 constructor(port) {92 this[kState] = {93 port,94 controller: undefined,95 };96 port.onmessage = ({ data }) => {97 const {98 controller,99 } = this[kState];100 const {101 type,102 value,103 } = data;104 switch (type) {105 case 'chunk':106 readableStreamDefaultControllerEnqueue(107 controller,108 value);109 break;110 case 'close':111 readableStreamDefaultControllerClose(controller);112 port.close();113 break;114 case 'error':115 readableStreamDefaultControllerError(controller, value);116 port.close();117 break;118 }119 };120 port.onmessageerror = () => {121 const error = new CloneableDOMException(122 'Internal transferred ReadableStream error',123 'DataCloneError');124 port.postMessage({ type: 'error', value: error });125 readableStreamDefaultControllerError(126 this[kState].controller,127 error);128 port.close();129 };130 }131 start(controller) {132 this[kState].controller = controller;133 }134 async pull() {135 this[kState].port.postMessage({ type: 'pull' });136 }137 async cancel(reason) {138 try {139 this[kState].port.postMessage({ type: 'error', value: reason });140 } catch (error) {141 if (error instanceof DOMException) {142 // eslint-disable-next-line no-ex-assign143 error = new CloneableDOMException(error.message, error.name);144 }145 this[kState].port.postMessage({ type: 'error', value: error });146 throw error;147 } finally {148 this[kState].port.close();149 }150 }151}152class CrossRealmTransformWritableSink {153 constructor(port) {154 this[kState] = {155 port,156 controller: undefined,157 backpressurePromise: createDeferredPromise(),158 };159 port.onmessage = ({ data }) => {160 assert(typeof data === 'object');161 const {162 type,163 value164 } = { ...data };165 assert(typeof type === 'string');166 switch (type) {167 case 'pull':168 if (this[kState].backpressurePromise !== undefined)169 this[kState].backpressurePromise.resolve?.();170 this[kState].backpressurePromise = undefined;171 break;172 case 'error':173 writableStreamDefaultControllerErrorIfNeeded(174 this[kState].controller,175 value);176 if (this[kState].backpressurePromise !== undefined)177 this[kState].backpressurePromise.resolve?.();178 this[kState].backpressurePromise = undefined;179 break;180 }181 };182 port.onmessageerror = () => {183 const error = new CloneableDOMException(184 'Internal transferred ReadableStream error',185 'DataCloneError');186 port.postMessage({ type: 'error', value: error });187 writableStreamDefaultControllerErrorIfNeeded(188 this[kState].controller,189 error);190 port.close();191 };192 }193 start(controller) {194 this[kState].controller = controller;195 }196 async write(chunk) {197 if (this[kState].backpressurePromise === undefined) {198 this[kState].backpressurePromise = {199 promise: PromiseResolve(),200 resolve: undefined,201 reject: undefined,202 };203 }204 await this[kState].backpressurePromise.promise;205 this[kState].backpressurePromise = createDeferredPromise();206 try {207 this[kState].port.postMessage({ type: 'chunk', value: chunk });208 } catch (error) {209 if (error instanceof DOMException) {210 // eslint-disable-next-line no-ex-assign211 error = new CloneableDOMException(error.message, error.name);212 }213 this[kState].port.postMessage({ type: 'error', value: error });214 this[kState].port.close();215 throw error;216 }217 }218 close() {219 this[kState].port.postMessage({ type: 'close' });220 this[kState].port.close();221 }222 abort(reason) {223 try {224 this[kState].port.postMessage({ type: 'error', value: reason });225 } catch (error) {226 if (error instanceof DOMException) {227 // eslint-disable-next-line no-ex-assign228 error = new CloneableDOMException(error.message, error.name);229 }230 this[kState].port.postMessage({ type: 'error', value: error });231 throw error;232 } finally {233 this[kState].port.close();234 }235 }236}237function newCrossRealmReadableStream(writable, port) {238 const readable =239 new ReadableStream(240 new CrossRealmTransformReadableSource(port));241 const promise =242 readableStreamPipeTo(readable, writable, false, false, false);243 setPromiseHandled(promise);244 return {245 readable,246 promise,247 };248}249function newCrossRealmWritableSink(readable, port) {250 const writable =251 new WritableStream(252 new CrossRealmTransformWritableSink(port));253 const promise = readableStreamPipeTo(readable, writable, false, false, false);254 setPromiseHandled(promise);255 return {256 writable,257 promise,258 };259}260module.exports = {261 newCrossRealmReadableStream,262 newCrossRealmWritableSink,263 CrossRealmTransformWritableSink,264 CrossRealmTransformReadableSource,265 CloneableDOMException,266 InternalCloneableDOMException,...
Using AI Code Generation
1import { WritableStreamDefaultControllerErrorIfNeeded } from 'streams/writable-streams/default-controller.mjs';2import { WritableStream } from 'streams/writable-streams/writable-stream.mjs';3import { WritableStreamDefaultWriter } from 'streams/writable-streams/writable-stream-default-writer.mjs';4import { WritableStreamDefaultController } from 'streams/writable-streams/writable-stream-default-controller.mjs';5const ws = new WritableStream({6 start(c) {7 }8});9const writer = new WritableStreamDefaultWriter(ws);10const controller = writer.desiredSize;11WritableStreamDefaultControllerErrorIfNeeded(controller, new RangeError('Hello'));12writer.close();13import { WritableStreamDefaultControllerError } from 'streams/writable-streams/writable-stream-default-controller.mjs';14export function WritableStreamDefaultControllerErrorIfNeeded(controller, e) {15 if (controller._started) {16 WritableStreamDefaultControllerError(controller, e);17 }18}19import { WritableStreamDefaultControllerErrorIfNeeded } from 'streams/writable-streams/default-controller.mjs';20import { WritableStream } from 'streams/writable-streams/writable-stream.mjs';21import { WritableStreamDefaultWriter } from 'streams/writable-streams/writable-stream-default-writer.mjs';22import { WritableStreamDefaultController } from 'streams/writable-streams/writable-stream-default-controller.mjs';23export function WritableStreamDefaultControllerError(controller, e) {24 const stream = controller._controlledWritableStream;25 WritableStreamDefaultControllerClearAlgorithms(controller);26 WritableStreamStartErroring(stream, e);27}28import { WritableStreamDefaultControllerErrorIfNeeded } from 'streams/writable-streams/default-controller.mjs';29import { WritableStream } from 'streams/writable-streams/writable-stream.mjs';30import { WritableStreamDefaultWriter } from 'streams/writable-streams/writable-stream-default-writer.mjs';31import { WritableStreamDefaultController } from 'streams/writable-streams/writable-stream-default-controller.mjs';32export function WritableStreamStartErroring(stream, reason) {33 const state = stream._state;34 if (state === 'writable') {
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.close();5 }6});7var ws = new WritableStream({8 write(chunk) {9 return new Promise(resolve => {10 setTimeout(resolve, 200);11 });12 }13});14var writer = ws.getWriter();15writer.write("a");16writer.close();17var reader = rs.getReader();18reader.read().then(function(result) {19 assert_equals(result.value, "a", "read result");20 assert_false(result.done, "done");21 reader.releaseLock();22 writer.releaseLock();23 return delay(0);24}).then(function() {25 return delay(200);26}).then(function() {27 assert_equals(ws.state, "closed", "ws state");28 t.done();29}).catch(t.step_func(function(e) {30 assert_unreached(e);31}));32var rs = new ReadableStream({33 start(controller) {34 controller.enqueue("a");35 controller.close();36 }37});38var ws = new WritableStream({39 write(chunk) {40 return new Promise(resolve => {41 setTimeout(resolve, 200);42 });43 }44});45var writer = ws.getWriter();46writer.write("a");47writer.close();48var reader = rs.getReader();49reader.read().then(function(result) {50 assert_equals(result.value, "a", "read result");51 assert_false(result.done, "done");52 reader.releaseLock();53 writer.releaseLock();54 return delay(0);55}).then(function() {56 return delay(200);57}).then(function() {58 assert_equals(ws.state, "closed", "ws state");59 t.done();60}).catch(t.step_func(function(e) {61 assert_unreached(e);62}));63var rs = new ReadableStream({64 start(controller) {65 controller.enqueue("a");66 controller.close();67 }68});69var ws = new WritableStream({70 write(chunk) {71 return new Promise(resolve => {72 setTimeout(resolve, 200);73 });74 }75});76var writer = ws.getWriter();77writer.write("a");
Using AI Code Generation
1'use strict';2const stream = require('stream');3const assert = require('assert');4const readable = new stream.Readable({5 read() {}6});7readable.push('a');8readable.push('b');9readable.push('c');10readable.push(null);11const writable = new stream.Writable({12 write(chunk, encoding, callback) {13 callback(new Error('error'));14 }15});16readable.pipe(writable).on('error', common.mustCall((err) => {17 assert.strictEqual(err.message, 'error');18}));19'use strict';20const stream = require('stream');21const assert = require('assert');22const readable = new stream.Readable({23 read() {}24});25readable.push('a');26readable.push('b');27readable.push('c');28readable.push(null);29const writable = new stream.Writable({30 write(chunk, encoding, callback) {31 callback(new Error('error'));32 }33});34readable.pipe(writable).on('error', common.mustCall((err) => {35 assert.strictEqual(err.message, 'error');36}));37'use strict';38const stream = require('stream');39const assert = require('assert');40const readable = new stream.Readable({41 read() {}42});43readable.push('a');44readable.push('b');45readable.push('c');46readable.push(null);47const writable = new stream.Writable({48 write(chunk, encoding, callback) {49 callback(new Error('error'));50 }51});52readable.pipe(writable).on('error', common.mustCall((err) => {53 assert.strictEqual(err.message, 'error');54}));55'use strict';56const stream = require('stream');57const assert = require('assert');58const readable = new stream.Readable({59 read() {}60});61readable.push('a');62readable.push('b');63readable.push('c');64readable.push(null);65const writable = new stream.Writable({66 write(chunk, encoding, callback) {67 callback(new Error('error'));68 }69});70readable.pipe(writable).on('error', common.mustCall((err) => {71 assert.strictEqual(err.message, 'error');72}));
Using AI Code Generation
1 > LayoutTests/imported/w3c/web-platform-tests/streams/readable-streams/writable-streams/general.js:392> +test(() => {3> + const ws = new WritableStream();4> + const writer = ws.getWriter();5> + writer.releaseLock();6> + assert_throws(new TypeError(), () => writer.releaseLock());7> +}, 'releaseLock() on a released writer should throw a TypeError');8> +test(() => {9> + const ws = new WritableStream();10> + const writer = ws.getWriter();11> + writer.releaseLock();12> + assert_throws(new TypeError(), () => writer.write());13> +}, 'write() on a released writer should throw a TypeError');14> +test(() => {15> + const ws = new WritableStream();16> + const writer = ws.getWriter();17> + writer.releaseLock();18> + assert_throws(new TypeError(), () => writer.close());19> +}, 'close() on a released writer should throw a TypeError');20> +test(() => {21> + const ws = new WritableStream();22> + const writer = ws.getWriter();23> + writer.releaseLock();24> + assert_throws(new TypeError(), () => writer.abort());25> +}, 'abort() on a released writer should throw a TypeError');26> +test(() => {27> + const ws = new WritableStream();28> + const writer = ws.getWriter();29> + writer.releaseLock();30> + assert_throws(new TypeError(), () => writer.closed);31> +}, 'closed getter on a released writer should throw a TypeError');32> +test(() => {33> + const ws = new WritableStream();34> + const writer = ws.getWriter();35> + writer.releaseLock();36> + assert_throws(new TypeError(), () => writer.desiredSize);37> +}, 'desiredSize getter on a released writer should throw a TypeError');38> +test(() => {39> + const ws = new WritableStream();40> + const writer = ws.getWriter();41> + writer.releaseLock();
Using AI Code Generation
1var WritableStream = require('stream').Writable;2var ws = new WritableStream({3 write: function(chunk, encoding, callback) {4 callback(new Error('error'));5 }6});
Using AI Code Generation
1import { WritableStreamDefaultControllerErrorIfNeeded } from2'./writable-stream.js';3export async function writeError(controller, error) {4 WritableStreamDefaultControllerErrorIfNeeded(controller, error);5}6export function WritableStreamDefaultControllerErrorIfNeeded(controller,7error) {8 if (controller._controlledWritableStream._state === 'writable') {9 WritableStreamDefaultControllerError(controller, error);10 }11}12export function WritableStreamDefaultControllerError(controller, error) {13 const stream = controller._controlledWritableStream;14 if (stream._state === 'writable') {15 WritableStreamStartErroring(stream, error);16 }17}18export function WritableStreamStartErroring(stream, reason) {19 stream._state = 'erroring';20 stream._storedError = reason;21 const writer = stream._writer;22 if (writer !== undefined) {23 WritableStreamDefaultWriterEnsureReadyPromiseRejectedWith(writer, reason,24 true);25 }26 if (!stream._pendingAbortRequest) {27 WritableStreamFinishErroring(stream);28 }29}30export function WritableStreamFinishErroring(stream) {31 stream._state = 'errored';32 stream._writableStreamController[ErrorSteps]();33 const storedError = stream._storedError;34 stream._writableStreamController[ErrorSteps] = () => {35 throw storedError;36 };37 const writer = stream._writer;38 if (writer !== undefined) {39 defaultWriterClosedPromiseReject(writer, storedError);40 writer._readyPromise = Promise.reject(storedError);41 }42}43export function defaultWriterClosedPromiseReject(writer, reason) {44 if (writer._closedPromiseState === 'pending') {45 defaultWriterClosedPromiseInitializeAsRejected(writer, reason);46 }47}48export function defaultWriterClosedPromiseInitializeAsRejected(writer, reason) {
Using AI Code Generation
1import { WritableStreamDefaultControllerErrorIfNeeded } from './aborting.js';2const controller = {3 _pendingAbortRequest: {4 _reject: () => {}5 }6};7WritableStreamDefaultControllerErrorIfNeeded(controller, new Error('test'));8import { WritableStreamDefaultControllerErrorIfNeeded } from './aborting.js';9const controller = {10 _pendingAbortRequest: {11 _reject: () => {}12 }13};14WritableStreamDefaultControllerErrorIfNeeded(controller, new Error('test'));15import { WritableStreamDefaultControllerErrorIfNeeded } from './aborting.js';16const controller = {17 _pendingAbortRequest: {18 _reject: () => {}19 }20};21WritableStreamDefaultControllerErrorIfNeeded(controller, new Error('test'));22import { WritableStreamDefaultControllerErrorIfNeeded } from './aborting.js';23const controller = {24 _pendingAbortRequest: {25 _reject: () => {}26 }27};28WritableStreamDefaultControllerErrorIfNeeded(controller, new Error('test'));29The controller object is the object you pass to the WritableStreamDefaultControllerErrorIfNeeded() function. So you need to create a controller object that has the
Using AI Code Generation
1> + var controller = new WritableStreamDefaultController(stream); 2> + const error = new Error('error'); 3>> + var controller = new WritableStreamDefaultController(stream);4>> + const error = new Error('error');5>> + var controller = new WritableStreamDefaultController(stream);6>> + const error = new Error('error');
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!!