Best JavaScript code snippet using wpt
WritableStream.js
Source:WritableStream.js
...318 stream[_pendingAbortRequest] = undefined;319 }320 WritableStreamDealWithRejection(stream, error);321 }322 function WritableStreamCloseQueuedOrInFlight(stream) {323 return stream[_closeRequest] !== undefined ||324 stream[_inFlightCloseRequest] !== undefined;325 }326 function WritableStreamHasOperationMarkedInFlight(stream) {327 return stream[_inFlightWriteRequest] !== undefined ||328 stream[_inFlightCloseRequest] !== undefined;329 }330 function WritableStreamMarkCloseRequestInFlight(stream) {331 // assert(stream[_inFlightCloseRequest] === undefined,332 // '_stream_.[[inFlightCloseRequest]] is *undefined*.');333 // assert(stream[_closeRequest] !== undefined,334 // '_stream_.[[closeRequest]] is not *undefined*.');335 stream[_inFlightCloseRequest] = stream[_closeRequest];336 stream[_closeRequest] = undefined;337 }338 function WritableStreamMarkFirstWriteRequestInFlight(stream) {339 // assert(stream[_inFlightWriteRequest] === undefined,340 // '_stream_.[[inFlightWriteRequest]] is *undefined*.');341 // assert(stream[_writeRequests].length !== 0,342 // '_stream_.[[writeRequests]] is not empty.');343 const writeRequest = stream[_writeRequests].shift();344 stream[_inFlightWriteRequest] = writeRequest;345 }346 function WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream) {347 // assert((stream[_stateAndFlags] & STATE_MASK) === ERRORED,348 // '_stream_.[[state]] is `"errored"`');349 if (stream[_closeRequest] !== undefined) {350 // assert(stream[_inFlightCloseRequest] === undefined,351 // '_stream_.[[inFlightCloseRequest]] is *undefined*');352 rejectPromise(stream[_closeRequest], stream[_storedError]);353 stream[_closeRequest] = undefined;354 }355 const writer = stream[_writer];356 if (writer !== undefined) {357 rejectPromise(writer[_closedPromise], stream[_storedError]);358 markPromiseAsHandled(writer[_closedPromise]);359 }360 }361 function WritableStreamUpdateBackpressure(stream, backpressure) {362 // assert((stream[_stateAndFlags] & STATE_MASK) === WRITABLE,363 // 'stream.[[state]] is "writable".');364 // assert(!WritableStreamCloseQueuedOrInFlight(stream),365 // 'WritableStreamCloseQueuedOrInFlight(_stream_) is *false*.');366 const writer = stream[_writer];367 if (writer !== undefined &&368 backpressure !== Boolean(stream[_stateAndFlags] & BACKPRESSURE_FLAG)) {369 if (backpressure) {370 writer[_readyPromise] = v8.createPromise();371 } else {372 // assert(!backpressure, '_backpressure_ is *false*.');373 resolvePromise(writer[_readyPromise], undefined);374 }375 }376 if (backpressure) {377 stream[_stateAndFlags] |= BACKPRESSURE_FLAG;378 } else {379 stream[_stateAndFlags] &= ~BACKPRESSURE_FLAG;380 }381 }382 // Functions to expose internals for ReadableStream.pipeTo. These are not383 // part of the standard.384 function isWritableStreamErrored(stream) {385 // assert(386 // IsWritableStream(stream), '! IsWritableStream(stream) is true.');387 return (stream[_stateAndFlags] & STATE_MASK) === ERRORED;388 }389 function isWritableStreamClosingOrClosed(stream) {390 // assert(391 // IsWritableStream(stream), '! IsWritableStream(stream) is true.');392 return WritableStreamCloseQueuedOrInFlight(stream) ||393 (stream[_stateAndFlags] & STATE_MASK) === CLOSED;394 }395 function getWritableStreamStoredError(stream) {396 // assert(397 // IsWritableStream(stream), '! IsWritableStream(stream) is true.');398 return stream[_storedError];399 }400 // Expose internals for TransformStream401 function isWritableStreamWritable(stream) {402 // assert(403 // IsWritableStream(stream), '! IsWritableStream(stream) is true.');404 return (stream[_stateAndFlags] & STATE_MASK) === WRITABLE;405 }406 function isWritableStreamErroring(stream) {407 // assert(408 // IsWritableStream(stream), '! IsWritableStream(stream) is true.');409 return (stream[_stateAndFlags] & STATE_MASK) === ERRORING;410 }411 function getWritableStreamController(stream) {412 // assert(413 // IsWritableStream(stream), '! IsWritableStream(stream) is true.');414 return stream[_writableStreamController];415 }416 class WritableStreamDefaultWriter {417 constructor(stream) {418 if (!IsWritableStream(stream)) {419 throw new TypeError(streamErrors.illegalConstructor);420 }421 if (IsWritableStreamLocked(stream)) {422 throw new TypeError(streamErrors.illegalConstructor);423 }424 this[_ownerWritableStream] = stream;425 stream[_writer] = this;426 const state = stream[_stateAndFlags] & STATE_MASK;427 switch (state) {428 case WRITABLE: {429 if (!WritableStreamCloseQueuedOrInFlight(stream) &&430 stream[_stateAndFlags] & BACKPRESSURE_FLAG) {431 this[_readyPromise] = v8.createPromise();432 } else {433 this[_readyPromise] = Promise_resolve(undefined);434 }435 this[_closedPromise] = v8.createPromise();436 break;437 }438 case ERRORING: {439 this[_readyPromise] = Promise_reject(stream[_storedError]);440 markPromiseAsHandled(this[_readyPromise]);441 this[_closedPromise] = v8.createPromise();442 break;443 }444 case CLOSED: {445 this[_readyPromise] = Promise_resolve(undefined);446 this[_closedPromise] = Promise_resolve(undefined);447 break;448 }449 default: {450 // assert(state === ERRORED, '_state_ is `"errored"`.');451 const storedError = stream[_storedError];452 this[_readyPromise] = Promise_reject(storedError);453 markPromiseAsHandled(this[_readyPromise]);454 this[_closedPromise] = Promise_reject(storedError);455 markPromiseAsHandled(this[_closedPromise]);456 break;457 }458 }459 }460 get closed() {461 if (!IsWritableStreamDefaultWriter(this)) {462 return Promise_reject(new TypeError(streamErrors.illegalInvocation));463 }464 return this[_closedPromise];465 }466 get desiredSize() {467 if (!IsWritableStreamDefaultWriter(this)) {468 throw new TypeError(streamErrors.illegalInvocation);469 }470 if (this[_ownerWritableStream] === undefined) {471 throw createWriterLockReleasedError(verbUsedToGetTheDesiredSize);472 }473 return WritableStreamDefaultWriterGetDesiredSize(this);474 }475 get ready() {476 if (!IsWritableStreamDefaultWriter(this)) {477 return Promise_reject(new TypeError(streamErrors.illegalInvocation));478 }479 return this[_readyPromise];480 }481 abort(reason) {482 if (!IsWritableStreamDefaultWriter(this)) {483 return Promise_reject(new TypeError(streamErrors.illegalInvocation));484 }485 if (this[_ownerWritableStream] === undefined) {486 return Promise_reject(createWriterLockReleasedError(verbAborted));487 }488 return WritableStreamDefaultWriterAbort(this, reason);489 }490 close() {491 if (!IsWritableStreamDefaultWriter(this)) {492 return Promise_reject(new TypeError(streamErrors.illegalInvocation));493 }494 const stream = this[_ownerWritableStream];495 if (stream === undefined) {496 return Promise_reject(createWriterLockReleasedError(verbClosed));497 }498 if (WritableStreamCloseQueuedOrInFlight(stream)) {499 return Promise_reject(new TypeError(errCloseCloseRequestedStream));500 }501 return WritableStreamDefaultWriterClose(this);502 }503 releaseLock() {504 if (!IsWritableStreamDefaultWriter(this)) {505 throw new TypeError(streamErrors.illegalInvocation);506 }507 const stream = this[_ownerWritableStream];508 if (stream === undefined) {509 return;510 }511 // assert(stream[_writer] !== undefined,512 // 'stream.[[writer]] is not undefined.');513 WritableStreamDefaultWriterRelease(this);514 }515 write(chunk) {516 if (!IsWritableStreamDefaultWriter(this)) {517 return Promise_reject(new TypeError(streamErrors.illegalInvocation));518 }519 if (this[_ownerWritableStream] === undefined) {520 return Promise_reject(createWriterLockReleasedError(verbWrittenTo));521 }522 return WritableStreamDefaultWriterWrite(this, chunk);523 }524 }525 // Writable Stream Writer Abstract Operations526 function IsWritableStreamDefaultWriter(x) {527 return hasOwnPropertyNoThrow(x, _ownerWritableStream);528 }529 function WritableStreamDefaultWriterAbort(writer, reason) {530 const stream = writer[_ownerWritableStream];531 // assert(stream !== undefined,532 // 'stream is not undefined.');533 return WritableStreamAbort(stream, reason);534 }535 function WritableStreamDefaultWriterClose(writer) {536 const stream = writer[_ownerWritableStream];537 // assert(stream !== undefined, 'stream is not undefined.');538 const state = stream[_stateAndFlags] & STATE_MASK;539 if (state === CLOSED || state === ERRORED) {540 return Promise_reject(541 createCannotActionOnStateStreamError('close', state));542 }543 // assert(state === WRITABLE || state === ERRORING,544 // '_state_ is `"writable"` or `"erroring"`.');545 // assert(!WritableStreamCloseQueuedOrInFlight(stream),546 // '! WritableStreamCloseQueuedOrInFlight(_stream_) is *false*.');547 const promise = v8.createPromise();548 stream[_closeRequest] = promise;549 if ((stream[_stateAndFlags] & BACKPRESSURE_FLAG) && state === WRITABLE) {550 resolvePromise(writer[_readyPromise], undefined);551 }552 WritableStreamDefaultControllerClose(stream[_writableStreamController]);553 return promise;554 }555 function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) {556 const stream = writer[_ownerWritableStream];557 // assert(stream !== undefined, 'stream is not undefined.');558 const state = stream[_stateAndFlags] & STATE_MASK;559 if (WritableStreamCloseQueuedOrInFlight(stream) || state === CLOSED) {560 return Promise_resolve(undefined);561 }562 if (state === ERRORED) {563 return Promise_reject(stream[_storedError]);564 }565 // assert(state === WRITABLE || state === ERRORING,566 // '_state_ is `"writable"` or `"erroring"`.');567 return WritableStreamDefaultWriterClose(writer);568 }569 function WritableStreamDefaultWriterEnsureClosedPromiseRejected(570 writer, error) {571 if (promiseState(writer[_closedPromise]) === v8.kPROMISE_PENDING) {572 rejectPromise(writer[_closedPromise], error);573 } else {574 writer[_closedPromise] = Promise_reject(error);575 }576 markPromiseAsHandled(writer[_closedPromise]);577 }578 function WritableStreamDefaultWriterEnsureReadyPromiseRejected(579 writer, error) {580 if (promiseState(writer[_readyPromise]) === v8.kPROMISE_PENDING) {581 rejectPromise(writer[_readyPromise], error);582 } else {583 writer[_readyPromise] = Promise_reject(error);584 }585 markPromiseAsHandled(writer[_readyPromise]);586 }587 function WritableStreamDefaultWriterGetDesiredSize(writer) {588 const stream = writer[_ownerWritableStream];589 const state = stream[_stateAndFlags] & STATE_MASK;590 if (state === ERRORED || state === ERRORING) {591 return null;592 }593 if (state === CLOSED) {594 return 0;595 }596 return WritableStreamDefaultControllerGetDesiredSize(597 stream[_writableStreamController]);598 }599 function WritableStreamDefaultWriterRelease(writer) {600 const stream = writer[_ownerWritableStream];601 // assert(stream !== undefined,602 // 'stream is not undefined.');603 // assert(stream[_writer] === writer,604 // 'stream.[[writer]] is writer.');605 const releasedError = new TypeError(errReleasedWriterClosedPromise);606 WritableStreamDefaultWriterEnsureReadyPromiseRejected(607 writer, releasedError);608 WritableStreamDefaultWriterEnsureClosedPromiseRejected(609 writer, releasedError);610 stream[_writer] = undefined;611 writer[_ownerWritableStream] = undefined;612 }613 function WritableStreamDefaultWriterWrite(writer, chunk) {614 const stream = writer[_ownerWritableStream];615 // assert(stream !== undefined, 'stream is not undefined.');616 const controller = stream[_writableStreamController];617 const chunkSize =618 WritableStreamDefaultControllerGetChunkSize(controller, chunk);619 if (stream !== writer[_ownerWritableStream]) {620 return Promise_reject(createWriterLockReleasedError(verbWrittenTo));621 }622 const state = stream[_stateAndFlags] & STATE_MASK;623 if (state === ERRORED) {624 return Promise_reject(stream[_storedError]);625 }626 if (WritableStreamCloseQueuedOrInFlight(stream)) {627 return Promise_reject(new TypeError(628 templateErrorCannotActionOnStateStream('write to', 'closing')));629 }630 if (state === CLOSED) {631 return Promise_reject(632 createCannotActionOnStateStreamError('write to', CLOSED));633 }634 if (state === ERRORING) {635 return Promise_reject(stream[_storedError]);636 }637 // assert(state === WRITABLE, '_state_ is `"writable"`');638 const promise = WritableStreamAddWriteRequest(stream);639 WritableStreamDefaultControllerWrite(controller, chunk, chunkSize);640 return promise;641 }642 // Functions to expose internals for ReadableStream.pipeTo. These do not643 // appear in the standard.644 function getWritableStreamDefaultWriterClosedPromise(writer) {645 // assert(646 // IsWritableStreamDefaultWriter(writer),647 // 'writer is a WritableStreamDefaultWriter.');648 return writer[_closedPromise];649 }650 function getWritableStreamDefaultWriterReadyPromise(writer) {651 // assert(652 // IsWritableStreamDefaultWriter(writer),653 // 'writer is a WritableStreamDefaultWriter.');654 return writer[_readyPromise];655 }656 class WritableStreamDefaultController {657 constructor(stream, underlyingSink, size, highWaterMark) {658 if (!IsWritableStream(stream)) {659 throw new TypeError(streamErrors.illegalConstructor);660 }661 if (stream[_writableStreamController] !== undefined) {662 throw new TypeError(streamErrors.illegalConstructor);663 }664 this[_controlledWritableStream] = stream;665 this[_underlyingSink] = underlyingSink;666 // These are just initialised to avoid triggering the assert() in667 // ResetQueue. They are overwritten by ResetQueue().668 this[_queue] = undefined;669 this[_queueTotalSize] = undefined;670 ResetQueue(this);671 this[_started] = false;672 const normalizedStrategy =673 ValidateAndNormalizeQueuingStrategy(size, highWaterMark);674 this[_strategySize] = normalizedStrategy.size;675 this[_strategyHWM] = normalizedStrategy.highWaterMark;676 const backpressure = WritableStreamDefaultControllerGetBackpressure(this);677 WritableStreamUpdateBackpressure(stream, backpressure);678 }679 error(e) {680 if (!IsWritableStreamDefaultController(this)) {681 throw new TypeError(streamErrors.illegalInvocation);682 }683 const state =684 this[_controlledWritableStream][_stateAndFlags] & STATE_MASK;685 if (state !== WRITABLE) {686 return;687 }688 WritableStreamDefaultControllerError(this, e);689 }690 }691 // Writable Stream Default Controller Internal Methods692 // TODO(ricea): Virtual dispatch via V8 Private Symbols seems to be difficult693 // or impossible, so use static dispatch for now. This will have to be fixed694 // when adding a byte controller.695 function WritableStreamDefaultControllerAbortSteps(controller, reason) {696 return PromiseCallOrNoop1(controller[_underlyingSink], 'abort', reason,697 'underlyingSink.abort');698 }699 function WritableStreamDefaultControllerErrorSteps(controller) {700 ResetQueue(controller);701 }702 function WritableStreamDefaultControllerStartSteps(controller) {703 const startResult = CallOrNoop1(controller[_underlyingSink], 'start',704 controller, 'underlyingSink.start');705 const stream = controller[_controlledWritableStream];706 const startPromise = Promise_resolve(startResult);707 thenPromise(708 startPromise,709 () => {710 // const state = stream[_stateAndFlags] & STATE_MASK;711 // assert(state === WRITABLE || state === ERRORING,712 // '_stream_.[[state]] is `"writable"` or `"erroring"`');713 controller[_started] = true;714 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);715 },716 r => {717 // const state = stream[_stateAndFlags] & STATE_MASK;718 // assert(state === WRITABLE || state === ERRORING,719 // '_stream_.[[state]] is `"writable"` or `"erroring"`');720 controller[_started] = true;721 WritableStreamDealWithRejection(stream, r);722 });723 }724 // Writable Stream Default Controller Abstract Operations725 function IsWritableStreamDefaultController(x) {726 return hasOwnPropertyNoThrow(x, _underlyingSink);727 }728 function WritableStreamDefaultControllerClose(controller) {729 EnqueueValueWithSize(controller, 'close', 0);730 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);731 }732 function WritableStreamDefaultControllerGetChunkSize(controller, chunk) {733 const strategySize = controller[_strategySize];734 if (strategySize === undefined) {735 return 1;736 }737 let value;738 try {739 value = Function_call(strategySize, undefined, chunk);740 } catch (e) {741 WritableStreamDefaultControllerErrorIfNeeded(controller, e);742 return 1;743 }744 return value;745 }746 function WritableStreamDefaultControllerGetDesiredSize(controller) {747 return controller[_strategyHWM] - controller[_queueTotalSize];748 }749 function WritableStreamDefaultControllerWrite(controller, chunk, chunkSize) {750 const writeRecord = {chunk};751 try {752 EnqueueValueWithSize(controller, writeRecord, chunkSize);753 } catch (e) {754 WritableStreamDefaultControllerErrorIfNeeded(controller, e);755 return;756 }757 const stream = controller[_controlledWritableStream];758 if (!WritableStreamCloseQueuedOrInFlight(stream) &&759 (stream[_stateAndFlags] & STATE_MASK) === WRITABLE) {760 const backpressure =761 WritableStreamDefaultControllerGetBackpressure(controller);762 WritableStreamUpdateBackpressure(stream, backpressure);763 }764 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);765 }766 function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {767 const stream = controller[_controlledWritableStream];768 if (!controller[_started]) {769 return;770 }771 if (stream[_inFlightWriteRequest] !== undefined) {772 return;773 }774 const state = stream[_stateAndFlags] & STATE_MASK;775 if (state === CLOSED || state === ERRORED) {776 return;777 }778 if (state === ERRORING) {779 WritableStreamFinishErroring(stream);780 return;781 }782 if (controller[_queue].length === 0) {783 return;784 }785 const writeRecord = PeekQueueValue(controller);786 if (writeRecord === 'close') {787 WritableStreamDefaultControllerProcessClose(controller);788 } else {789 WritableStreamDefaultControllerProcessWrite(790 controller, writeRecord.chunk);791 }792 }793 function WritableStreamDefaultControllerErrorIfNeeded(controller, error) {794 const state =795 controller[_controlledWritableStream][_stateAndFlags] & STATE_MASK;796 if (state === WRITABLE) {797 WritableStreamDefaultControllerError(controller, error);798 }799 }800 function WritableStreamDefaultControllerProcessClose(controller) {801 const stream = controller[_controlledWritableStream];802 WritableStreamMarkCloseRequestInFlight(stream);803 DequeueValue(controller);804 // assert(controller[_queue].length === 0,805 // 'controller.[[queue]] is empty.');806 const sinkClosePromise =807 PromiseCallOrNoop0(controller[_underlyingSink], 'close',808 'underlyingSink.close');809 thenPromise(810 sinkClosePromise, () => WritableStreamFinishInFlightClose(stream),811 reason => WritableStreamFinishInFlightCloseWithError(stream, reason));812 }813 function WritableStreamDefaultControllerProcessWrite(controller, chunk) {814 const stream = controller[_controlledWritableStream];815 WritableStreamMarkFirstWriteRequestInFlight(stream);816 const sinkWritePromise = PromiseCallOrNoop2(817 controller[_underlyingSink], 'write', chunk, controller,818 'underlyingSink.write');819 thenPromise(820 sinkWritePromise,821 () => {822 WritableStreamFinishInFlightWrite(stream);823 const state = stream[_stateAndFlags] & STATE_MASK;824 // assert(state === WRITABLE || state === ERRORING,825 // '_state_ is `"writable"` or `"erroring"`');826 DequeueValue(controller);827 if (!WritableStreamCloseQueuedOrInFlight(stream) &&828 state === WRITABLE) {829 const backpressure =830 WritableStreamDefaultControllerGetBackpressure(controller);831 WritableStreamUpdateBackpressure(stream, backpressure);832 }833 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);834 },835 reason => {836 WritableStreamFinishInFlightWriteWithError(stream, reason);837 });838 }839 function WritableStreamDefaultControllerGetBackpressure(controller) {840 const desiredSize =841 WritableStreamDefaultControllerGetDesiredSize(controller);...
pipe.ts
Source:pipe.ts
...136 shutdown();137 }138 });139 // Closing must be propagated backward140 if (WritableStreamCloseQueuedOrInFlight(dest) || dest._state === 'closed') {141 const destClosed = new TypeError('the destination writable stream closed before all data could be piped to it');142 if (!preventCancel) {143 shutdownWithAction(() => ReadableStreamCancel(source, destClosed), true, destClosed);144 } else {145 shutdown(true, destClosed);146 }147 }148 setPromiseIsHandledToTrue(pipeLoop());149 function waitForWritesToFinish(): Promise<void> {150 // Another write may have started while we were waiting on this currentWrite, so we have to be sure to wait151 // for that too.152 const oldCurrentWrite = currentWrite;153 return PerformPromiseThen(154 currentWrite,155 () => oldCurrentWrite !== currentWrite ? waitForWritesToFinish() : undefined156 );157 }158 function isOrBecomesErrored(stream: ReadableStream | WritableStream,159 promise: Promise<void>,160 action: (reason: any) => void) {161 if (stream._state === 'errored') {162 action(stream._storedError);163 } else {164 uponRejection(promise, action);165 }166 }167 function isOrBecomesClosed(stream: ReadableStream | WritableStream, promise: Promise<void>, action: () => void) {168 if (stream._state === 'closed') {169 action();170 } else {171 uponFulfillment(promise, action);172 }173 }174 function shutdownWithAction(action: () => Promise<unknown>, originalIsError?: boolean, originalError?: any) {175 if (shuttingDown) {176 return;177 }178 shuttingDown = true;179 if (dest._state === 'writable' && !WritableStreamCloseQueuedOrInFlight(dest)) {180 uponFulfillment(waitForWritesToFinish(), doTheRest);181 } else {182 doTheRest();183 }184 function doTheRest() {185 uponPromise(186 action(),187 () => finalize(originalIsError, originalError),188 newError => finalize(true, newError)189 );190 }191 }192 function shutdown(isError?: boolean, error?: any) {193 if (shuttingDown) {194 return;195 }196 shuttingDown = true;197 if (dest._state === 'writable' && !WritableStreamCloseQueuedOrInFlight(dest)) {198 uponFulfillment(waitForWritesToFinish(), () => finalize(isError, error));199 } else {200 finalize(isError, error);201 }202 }203 function finalize(isError?: boolean, error?: any) {204 WritableStreamDefaultWriterRelease(writer);205 ReadableStreamReaderGenericRelease(reader);206 if (signal !== undefined) {207 signal.removeEventListener('abort', abortAlgorithm);208 }209 if (isError) {210 reject(error);211 } else {...
Using AI Code Generation
1const { ReadableStream, WritableStream } = require('stream/web');2const rs = new ReadableStream({3 pull(controller) {4 controller.enqueue('a');5 controller.close();6 }7});8const ws = new WritableStream({9 write(chunk) {10 return new Promise(resolve => setTimeout(resolve, 1000));11 }12});13const writer = ws.getWriter();14const reader = rs.getReader();15const start = Date.now();16writer.write('a').then(() => {17 console.log(`the first write() finished after ${Date.now() - start}ms`);18});19writer.close();20reader.read().then(console.log);21reader.read().then(console.log);22reader.read().then(console.log);23writer.write('a').then(() => {24 console.log(`the second write() finished after ${Date.now() - start}ms`);25});26The example above shows how the second write() after the writer closes27partial interface WritableStreamDefaultWriter {28 [EnforceRange] readonly attribute long long? desiredSize;29};30<dfn method for="WritableStreamDefaultWriter">getDesiredSize(|this|)</dfn> can31function shouldApplyBackpressure(writer) {32 const desiredSize = writer.desiredSize;33 if (desiredSize === null) {34 return true;35 }36 assert(desiredSize !== undefined);37 return desiredSize <= 0;38}39partial interface WritableStreamDefaultWriter {40 Promise<void> close();41};42<dfn method for="WritableStreamDefaultWriter">close(|this|)</dfn> closes the
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, 100);11 });12 }13});14var writer = ws.getWriter();15var reader = rs.getReader();16var read = reader.read();17var write = writer.write("a");18Promise.all([read, write]).then(() => {19 console.log("done");20});21var ws = new WritableStream();22var writer = ws.getWriter();23writer.write("a");24writer.close();25var ws = new WritableStream({26 write() {27 return Promise.reject("oh no");28 }29});30var writer = ws.getWriter();31writer.write("a").catch(() => console.log("write rejected"));32writer.close().catch(() => console.log("close rejected"));33var ws = new WritableStream();34var writer = ws.getWriter();35writer.releaseLock();36writer.write("a").catch(() => console.log("write rejected"));37writer.close().catch(() => console.log("close rejected"));38var ws = new WritableStream();39var writer = ws.getWriter();40writer.write("a");41writer.write("b");42writer.write("c");43var ws = new WritableStream({44 write() {45 return Promise.reject("oh no");46 }47});48var writer = ws.getWriter();49writer.write("a").catch(() => console.log("write rejected"));50writer.write("b").catch(() => console.log("write rejected"));51writer.write("c").catch(() => console.log("write rejected"));52var ws = new WritableStream();53var writer = ws.getWriter();54writer.close();55writer.write("a").catch(() => console.log("write rejected"));56writer.write("b").catch(() => console.log("write rejected"));57writer.write("c").catch(() => console.log("write rejected"));
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue(new Uint8Array([0, 1, 2]));4 controller.close();5 }6});7var writer = rs.getWriter();8writer.closed.then(() => console.log("closed!"));9writer.releaseLock();10var rs = new ReadableStream({11 start(controller) {12 controller.enqueue(new Uint8Array([0, 1, 2]));13 controller.close();14 }15});16var writer = rs.getWriter();17writer.closed.then(() => console.log("closed!"));18writer.releaseLock();19var rs = new ReadableStream({20 start(controller) {21 controller.enqueue(new Uint8Array([0, 1, 2]));22 controller.close();23 }24});25var writer = rs.getWriter();26writer.closed.then(() => console.log("closed!"));27writer.releaseLock();28var rs = new ReadableStream({29 start(controller) {30 controller.enqueue(new Uint8Array([0, 1, 2]));31 controller.close();32 }33});34var writer = rs.getWriter();35writer.closed.then(() => console.log("closed!"));36writer.releaseLock();37var rs = new ReadableStream({38 start(controller) {39 controller.enqueue(new Uint8Array([0, 1, 2]));40 controller.close();41 }42});43var writer = rs.getWriter();44writer.closed.then(() => console.log("closed!"));45writer.releaseLock();46var rs = new ReadableStream({47 start(controller) {48 controller.enqueue(new Uint8Array([0, 1, 2]));49 controller.close();50 }51});52var writer = rs.getWriter();53writer.closed.then(() => console.log("closed!"));54writer.releaseLock();
Using AI Code Generation
1import { WritableStreamCloseQueuedOrInFlight } from "streams/writable-streams/close-queued-or-in-flight.js";2import { WritableStream } from "streams/writable-streams/writable-stream.js";3import { CreateWritableStream } from "streams/writable-streams/create-writable-stream.js";4import { WritableStreamDefaultWriter } from "streams/writable-streams/writable-stream-default-writer.js";5import { WritableStreamDefaultController } from "streams/writable-streams/writable-stream-default-controller.js";6import { WritableStreamCloseQueuedOrInFlight } from "streams/writable-streams/close-queued-or-in-flight.js";7import { WritableStreamAbortQueuedOrInFlight } from "streams/writable-streams/abort-queued-or-in-flight.js";8import { WritableStreamCloseQueuedOrInFlight } from "streams/writable-streams/close-queued-or-in-flight.js";9import { WritableStreamAbortQueuedOrInFlight } from "streams/writable-streams/abort-queued-or-in-flight.js";10import { WritableStreamCloseQueuedOrInFlight } from "streams/writable-streams/close-queued-or-in-flight.js";11import { WritableStreamAbortQueuedOrInFlight } from "streams/writable-streams/abort-queued-or-in-flight.js";12import { WritableStreamCloseQueuedOrInFlight } from "streams/writable-streams/close-queued-or-in-flight.js";13import { WritableStreamAbortQueuedOrInFlight } from "streams/writable-streams/abort-queued-or-in-flight.js";14import { WritableStreamCloseQueuedOrInFlight } from "streams/writable-streams/close-queued-or-in-flight.js";15import { WritableStreamAbortQueuedOrInFlight } from "streams/writable-streams/abort-queued-or-in-flight.js";16import { WritableStreamAbortQueuedOrInFlight } from "streams/writable-streams/abort-queued-or-in-flight.js";17import { WritableStream } from "streams/writable-streams/writable-stream.js";18import { CreateWritableStream } from "streams/writable-streams/create-writable-stream.js";19import { WritableStreamDefaultWriter } from "streams/writable-streams/writable-stream-default-writer.js";20import { WritableStreamDefaultController }
Using AI Code Generation
1const { WritableStream, CountQueuingStrategy } = require('stream/web');2const { WritableStreamCloseQueuedOrInFlight } = require('stream/web');3const ws = new WritableStream({4 write() {}5}, new CountQueuingStrategy({ highWaterMark: 1 }));6const writer = ws.getWriter();7writer.write('a');8console.log(WritableStreamCloseQueuedOrInFlight(ws));9writer.close();10console.log(WritableStreamCloseQueuedOrInFlight(ws));11writer.releaseLock();12const { WritableStream } = require('stream/web');13const { WritableStreamErrorQueuedOrInFlight } = require('stream/web');14const ws = new WritableStream({15 write() {}16});17const writer = ws.getWriter();18writer.write('a');19console.log(WritableStreamErrorQueuedOrInFlight(ws));20writer.abort();21console.log(WritableStreamErrorQueuedOrInFlight(ws));22writer.releaseLock();23const { WritableStream, CountQueuingStrategy } = require('stream/web');24const { WritableStreamGetDesiredSize } = require('stream/web');25const ws = new WritableStream({26 write() {}27}, new CountQueuingStrategy({ highWaterMark: 1 }));
Using AI Code Generation
1const { WritableStreamCloseQueuedOrInFlight } = require('wpt');2const stream = new WritableStream();3WritableStreamCloseQueuedOrInFlight(stream);4const stream = new WritableStream();5WritableStreamCloseQueuedOrInFlight(stream);6const stream = new WritableStream();7WritableStreamCloseQueuedOrInFlight(stream);8const stream = new WritableStream();9WritableStreamCloseQueuedOrInFlight(stream);10const stream = new WritableStream();11WritableStreamCloseQueuedOrInFlight(stream);12const stream = new WritableStream();13WritableStreamCloseQueuedOrInFlight(stream);14const stream = new WritableStream();15WritableStreamCloseQueuedOrInFlight(stream);16const stream = new WritableStream();17WritableStreamCloseQueuedOrInFlight(stream);18const stream = new WritableStream();19WritableStreamCloseQueuedOrInFlight(stream);20const stream = new WritableStream();21WritableStreamCloseQueuedOrInFlight(stream);22const stream = new WritableStream();23WritableStreamCloseQueuedOrInFlight(stream);24const stream = new WritableStream();25WritableStreamCloseQueuedOrInFlight(stream);
Using AI Code Generation
1var ws = new WritableStream({2 write(chunk, controller) {3 }4});5ws.write(1);6ws.close();7assert_equals(ws.state, 'closing');8assert_equals(ws.write(1), false);9assert_equals(ws.state, 'closing');10assert_equals(ws.write(2), false);11assert_equals(ws.state, 'closing');12assert_equals(ws.write(3), false);13assert_equals(ws.state, 'closing');14assert_equals(ws.write(4), false);15assert_equals(ws.state, 'closing');16assert_equals(ws.write(5), false);17assert_equals(ws.state, 'closing');18assert_equals(ws.write(6), false);19assert_equals(ws.state, 'closing');20assert_equals(ws.write(7), false);21assert_equals(ws.state, 'closing');22assert_equals(ws.write(8), false);23assert_equals(ws.state, 'closing');24assert_equals(ws.write(9), false);25assert_equals(ws.state, 'closing');26assert_equals(ws.write(10), false);27assert_equals(ws.state, 'closing');28assert_equals(ws.write(11), false);29assert_equals(ws.state, 'closing');30assert_equals(ws.write(12), false);31assert_equals(ws.state, 'closing');32assert_equals(ws.write(13), false);33assert_equals(ws.state, 'closing');34assert_equals(ws.write(14), false);35assert_equals(ws.state, 'closing');36assert_equals(ws.write(15), false);37assert_equals(ws.state, 'closing');38assert_equals(ws.write(16), false);39assert_equals(ws.state, 'closing');40assert_equals(ws.write(17), false);41assert_equals(ws.state, 'closing');42assert_equals(ws.write(18), false);43assert_equals(ws.state, 'closing');44assert_equals(ws.write(19), false);45assert_equals(ws.state, 'closing');46assert_equals(ws.write(20), false);47assert_equals(ws.state, 'closing');48assert_equals(ws.write(21), false);49assert_equals(ws.state, 'closing');50assert_equals(ws.write(22), false);51assert_equals(ws.state, 'closing');52assert_equals(ws.write(23), false);53assert_equals(ws.state, 'closing');54assert_equals(ws.write(24), false);55assert_equals(ws.state, 'closing');56assert_equals(ws.write(25), false);57assert_equals(ws.state, 'closing');58assert_equals(ws.write(26), false);59assert_equals(ws.state, 'closing');60assert_equals(ws.write(27), false);61assert_equals(ws.state, 'closing');
Using AI Code Generation
1const assert = require('assert');2const { WritableStream } = require('stream/web');3const { WritableStreamCloseQueuedOrInFlight } = require('stream/web');4const { WritableStreamAbortQueuedOrInFlight } = require('stream/web');5const { WritableStreamDefaultControllerClose } = require('stream/web');6const { WritableStreamDefaultControllerError } = require('stream/web');7const ws = new WritableStream({8 write() {}9});10assert.strictEqual(11 WritableStreamCloseQueuedOrInFlight(ws),12);13WritableStreamDefaultControllerClose(14 new WritableStreamDefaultController(ws)15);16assert.strictEqual(17 WritableStreamCloseQueuedOrInFlight(ws),18);19WritableStreamDefaultControllerError(20 new WritableStreamDefaultController(ws),21 new Error('Test')22);23assert.strictEqual(24 WritableStreamCloseQueuedOrInFlight(ws),25);26WritableStreamAbortQueuedOrInFlight(ws);27assert.strictEqual(28 WritableStreamCloseQueuedOrInFlight(ws),29);30WritableStreamDefaultControllerClose(31 new WritableStreamDefaultController(ws)32);33assert.strictEqual(34 WritableStreamCloseQueuedOrInFlight(ws),35);36WritableStreamDefaultControllerClose(37 new WritableStreamDefaultController(ws)38);39assert.strictEqual(40 WritableStreamCloseQueuedOrInFlight(ws),41);
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!!