Best JavaScript code snippet using wpt
writable-stream.ts
Source:writable-stream.ts
...401 defaultWriterReadyPromiseInitialize(this);402 } else {403 defaultWriterReadyPromiseInitializeAsResolved(this);404 }405 defaultWriterClosedPromiseInitialize(this);406 } else if (state === 'erroring') {407 defaultWriterReadyPromiseInitializeAsRejected(this, stream._storedError);408 defaultWriterClosedPromiseInitialize(this);409 } else if (state === 'closed') {410 defaultWriterReadyPromiseInitializeAsResolved(this);411 defaultWriterClosedPromiseInitializeAsResolved(this);412 } else {413 assert(state === 'errored');414 const storedError = stream._storedError;415 defaultWriterReadyPromiseInitializeAsRejected(this, storedError);416 defaultWriterClosedPromiseInitializeAsRejected(this, storedError);417 }418 }419 get closed(): Promise<void> {420 if (IsWritableStreamDefaultWriter(this) === false) {421 return Promise.reject(defaultWriterBrandCheckException('closed'));422 }423 return this._closedPromise;424 }425 get desiredSize(): number | null {426 if (IsWritableStreamDefaultWriter(this) === false) {427 throw defaultWriterBrandCheckException('desiredSize');428 }429 if (this._ownerWritableStream === undefined) {430 throw defaultWriterLockException('desiredSize');431 }432 return WritableStreamDefaultWriterGetDesiredSize(this);433 }434 get ready(): Promise<void> {435 if (IsWritableStreamDefaultWriter(this) === false) {436 return Promise.reject(defaultWriterBrandCheckException('ready'));437 }438 return this._readyPromise;439 }440 abort(reason: any): Promise<void> {441 if (IsWritableStreamDefaultWriter(this) === false) {442 return Promise.reject(defaultWriterBrandCheckException('abort'));443 }444 if (this._ownerWritableStream === undefined) {445 return Promise.reject(defaultWriterLockException('abort'));446 }447 return WritableStreamDefaultWriterAbort(this, reason);448 }449 close(): Promise<void> {450 if (IsWritableStreamDefaultWriter(this) === false) {451 return Promise.reject(defaultWriterBrandCheckException('close'));452 }453 const stream = this._ownerWritableStream;454 if (stream === undefined) {455 return Promise.reject(defaultWriterLockException('close'));456 }457 if (WritableStreamCloseQueuedOrInFlight(stream) === true) {458 return Promise.reject(new TypeError('cannot close an already-closing stream'));459 }460 return WritableStreamDefaultWriterClose(this);461 }462 releaseLock(): void {463 if (IsWritableStreamDefaultWriter(this) === false) {464 throw defaultWriterBrandCheckException('releaseLock');465 }466 const stream = this._ownerWritableStream;467 if (stream === undefined) {468 return;469 }470 assert(stream._writer !== undefined);471 WritableStreamDefaultWriterRelease(this);472 }473 write(chunk: W): Promise<void> {474 if (IsWritableStreamDefaultWriter(this) === false) {475 return Promise.reject(defaultWriterBrandCheckException('write'));476 }477 if (this._ownerWritableStream === undefined) {478 return Promise.reject(defaultWriterLockException('write to'));479 }480 return WritableStreamDefaultWriterWrite(this, chunk);481 }482}483// Abstract operations for the WritableStreamDefaultWriter.484function IsWritableStreamDefaultWriter<W>(x: any): x is WritableStreamDefaultWriter<W> {485 if (!typeIsObject(x)) {486 return false;487 }488 if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {489 return false;490 }491 return true;492}493// A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.494function WritableStreamDefaultWriterAbort(writer: WritableStreamDefaultWriter<any>, reason: any) {495 const stream = writer._ownerWritableStream;496 assert(stream !== undefined);497 return WritableStreamAbort(stream, reason);498}499function WritableStreamDefaultWriterClose(writer: WritableStreamDefaultWriter<any>): Promise<void> {500 const stream = writer._ownerWritableStream;501 assert(stream !== undefined);502 const state = stream._state;503 if (state === 'closed' || state === 'errored') {504 return Promise.reject(new TypeError(505 `The stream (in ${state} state) is not in the writable state and cannot be closed`));506 }507 assert(state === 'writable' || state === 'erroring');508 assert(WritableStreamCloseQueuedOrInFlight(stream) === false);509 const promise = new Promise<void>((resolve, reject) => {510 const closeRequest: CloseRequest = {511 _resolve: resolve,512 _reject: reject513 };514 stream._closeRequest = closeRequest;515 });516 if (stream._backpressure === true && state === 'writable') {517 defaultWriterReadyPromiseResolve(writer);518 }519 WritableStreamDefaultControllerClose(stream._writableStreamController);520 return promise;521}522function WritableStreamDefaultWriterCloseWithErrorPropagation(writer: WritableStreamDefaultWriter<any>): Promise<void> {523 const stream = writer._ownerWritableStream;524 assert(stream !== undefined);525 const state = stream._state;526 if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {527 return Promise.resolve();528 }529 if (state === 'errored') {530 return Promise.reject(stream._storedError);531 }532 assert(state === 'writable' || state === 'erroring');533 return WritableStreamDefaultWriterClose(writer);534}535function WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer: WritableStreamDefaultWriter<any>, error: any) {536 if (writer._closedPromiseState === 'pending') {537 defaultWriterClosedPromiseReject(writer, error);538 } else {539 defaultWriterClosedPromiseResetToRejected(writer, error);540 }541}542function WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer: WritableStreamDefaultWriter<any>, error: any) {543 if (writer._readyPromiseState === 'pending') {544 defaultWriterReadyPromiseReject(writer, error);545 } else {546 defaultWriterReadyPromiseResetToRejected(writer, error);547 }548}549function WritableStreamDefaultWriterGetDesiredSize(writer: WritableStreamDefaultWriter<any>): number | null {550 const stream = writer._ownerWritableStream;551 const state = stream._state;552 if (state === 'errored' || state === 'erroring') {553 return null;554 }555 if (state === 'closed') {556 return 0;557 }558 return WritableStreamDefaultControllerGetDesiredSize(stream._writableStreamController);559}560function WritableStreamDefaultWriterRelease(writer: WritableStreamDefaultWriter<any>) {561 const stream = writer._ownerWritableStream;562 assert(stream !== undefined);563 assert(stream._writer === writer);564 const releasedError = new TypeError(565 'Writer was released and can no longer be used to monitor the stream\'s closedness');566 WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError);567 // The state transitions to "errored" before the sink abort() method runs, but the writer.closed promise is not568 // rejected until afterwards. This means that simply testing state will not work.569 WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError);570 stream._writer = undefined;571 writer._ownerWritableStream = undefined!;572}573function WritableStreamDefaultWriterWrite<W>(writer: WritableStreamDefaultWriter<W>, chunk: W): Promise<void> {574 const stream = writer._ownerWritableStream;575 assert(stream !== undefined);576 const controller = stream._writableStreamController;577 const chunkSize = WritableStreamDefaultControllerGetChunkSize(controller, chunk);578 if (stream !== writer._ownerWritableStream) {579 return Promise.reject(defaultWriterLockException('write to'));580 }581 const state = stream._state;582 if (state === 'errored') {583 return Promise.reject(stream._storedError);584 }585 if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {586 return Promise.reject(new TypeError('The stream is closing or closed and cannot be written to'));587 }588 if (state === 'erroring') {589 return Promise.reject(stream._storedError);590 }591 assert(state === 'writable');592 const promise = WritableStreamAddWriteRequest(stream);593 WritableStreamDefaultControllerWrite(controller, chunk, chunkSize);594 return promise;595}596interface WriteRecord<W> {597 chunk: W;598}599type QueueRecord<W> = WriteRecord<W> | 'close';600export type WritableStreamDefaultControllerType = WritableStreamDefaultController<any>;601class WritableStreamDefaultController<W = any> {602 /** @internal */603 _controlledWritableStream!: WritableStream<W>;604 /** @internal */605 _queue!: SimpleQueue<QueuePair<QueueRecord<W>>>;606 /** @internal */607 _queueTotalSize!: number;608 /** @internal */609 _started!: boolean;610 /** @internal */611 _strategySizeAlgorithm!: QueuingStrategySizeCallback<W>;612 /** @internal */613 _strategyHWM!: number;614 /** @internal */615 _writeAlgorithm!: (chunk: W) => Promise<void>;616 /** @internal */617 _closeAlgorithm!: () => Promise<void>;618 /** @internal */619 _abortAlgorithm!: (reason: any) => Promise<void>;620 /** @internal */621 constructor() {622 throw new TypeError('WritableStreamDefaultController cannot be constructed explicitly');623 }624 error(e: any) {625 if (IsWritableStreamDefaultController(this) === false) {626 throw new TypeError(627 'WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');628 }629 const state = this._controlledWritableStream._state;630 if (state !== 'writable') {631 // The stream is closed, errored or will be soon. The sink can't do anything useful if it gets an error here, so632 // just treat it as a no-op.633 return;634 }635 WritableStreamDefaultControllerError(this, e);636 }637 /** @internal */638 [AbortSteps](reason: any) {639 const result = this._abortAlgorithm(reason);640 WritableStreamDefaultControllerClearAlgorithms(this);641 return result;642 }643 /** @internal */644 [ErrorSteps]() {645 ResetQueue(this);646 }647}648// Abstract operations implementing interface required by the WritableStream.649function IsWritableStreamDefaultController<W>(x: any): x is WritableStreamDefaultController<W> {650 if (!typeIsObject(x)) {651 return false;652 }653 if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {654 return false;655 }656 return true;657}658function SetUpWritableStreamDefaultController<W>(stream: WritableStream<W>,659 controller: WritableStreamDefaultController<W>,660 startAlgorithm: () => void | PromiseLike<void>,661 writeAlgorithm: (chunk: W) => Promise<void>,662 closeAlgorithm: () => Promise<void>,663 abortAlgorithm: (reason: any) => Promise<void>,664 highWaterMark: number,665 sizeAlgorithm: QueuingStrategySizeCallback<W>) {666 assert(IsWritableStream(stream) === true);667 assert(stream._writableStreamController === undefined);668 controller._controlledWritableStream = stream;669 stream._writableStreamController = controller;670 // Need to set the slots so that the assert doesn't fire. In the spec the slots already exist implicitly.671 controller._queue = undefined!;672 controller._queueTotalSize = undefined!;673 ResetQueue(controller);674 controller._started = false;675 controller._strategySizeAlgorithm = sizeAlgorithm;676 controller._strategyHWM = highWaterMark;677 controller._writeAlgorithm = writeAlgorithm;678 controller._closeAlgorithm = closeAlgorithm;679 controller._abortAlgorithm = abortAlgorithm;680 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);681 WritableStreamUpdateBackpressure(stream, backpressure);682 const startResult = startAlgorithm();683 const startPromise = Promise.resolve(startResult);684 startPromise.then(685 () => {686 assert(stream._state === 'writable' || stream._state === 'erroring');687 controller._started = true;688 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);689 },690 r => {691 assert(stream._state === 'writable' || stream._state === 'erroring');692 controller._started = true;693 WritableStreamDealWithRejection(stream, r);694 }695 ).catch(rethrowAssertionErrorRejection);696}697function SetUpWritableStreamDefaultControllerFromUnderlyingSink<W>(stream: WritableStream<W>,698 underlyingSink: UnderlyingSink<W>,699 highWaterMark: number,700 sizeAlgorithm: QueuingStrategySizeCallback<W>) {701 assert(underlyingSink !== undefined);702 const controller = Object.create(WritableStreamDefaultController.prototype);703 function startAlgorithm() {704 return InvokeOrNoop<typeof underlyingSink, 'start'>(underlyingSink, 'start', [controller]);705 }706 const writeAlgorithm = CreateAlgorithmFromUnderlyingMethod<typeof underlyingSink, 'write'>(707 underlyingSink, 'write', 1, [controller]708 );709 const closeAlgorithm = CreateAlgorithmFromUnderlyingMethod<typeof underlyingSink, 'close'>(710 underlyingSink, 'close', 0, []711 );712 const abortAlgorithm = CreateAlgorithmFromUnderlyingMethod<typeof underlyingSink, 'abort'>(713 underlyingSink, 'abort', 1, []714 );715 SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm,716 abortAlgorithm, highWaterMark, sizeAlgorithm);717}718// ClearAlgorithms may be called twice. Erroring the same stream in multiple ways will often result in redundant calls.719function WritableStreamDefaultControllerClearAlgorithms(controller: WritableStreamDefaultController<any>) {720 controller._writeAlgorithm = undefined!;721 controller._closeAlgorithm = undefined!;722 controller._abortAlgorithm = undefined!;723 controller._strategySizeAlgorithm = undefined!;724}725function WritableStreamDefaultControllerClose(controller: WritableStreamDefaultController<any>) {726 EnqueueValueWithSize(controller, 'close', 0);727 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);728}729function WritableStreamDefaultControllerGetChunkSize<W>(controller: WritableStreamDefaultController<W>,730 chunk: W): number {731 try {732 return controller._strategySizeAlgorithm(chunk);733 } catch (chunkSizeE) {734 WritableStreamDefaultControllerErrorIfNeeded(controller, chunkSizeE);735 return 1;736 }737}738function WritableStreamDefaultControllerGetDesiredSize(controller: WritableStreamDefaultController<any>): number {739 return controller._strategyHWM - controller._queueTotalSize;740}741function WritableStreamDefaultControllerWrite<W>(controller: WritableStreamDefaultController<W>,742 chunk: W,743 chunkSize: number) {744 const writeRecord = { chunk };745 try {746 EnqueueValueWithSize(controller, writeRecord, chunkSize);747 } catch (enqueueE) {748 WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueE);749 return;750 }751 const stream = controller._controlledWritableStream;752 if (WritableStreamCloseQueuedOrInFlight(stream) === false && stream._state === 'writable') {753 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);754 WritableStreamUpdateBackpressure(stream, backpressure);755 }756 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);757}758// Abstract operations for the WritableStreamDefaultController.759function WritableStreamDefaultControllerAdvanceQueueIfNeeded<W>(controller: WritableStreamDefaultController<W>) {760 const stream = controller._controlledWritableStream;761 if (controller._started === false) {762 return;763 }764 if (stream._inFlightWriteRequest !== undefined) {765 return;766 }767 const state = stream._state;768 assert(state !== 'closed' && state !== 'errored');769 if (state === 'erroring') {770 WritableStreamFinishErroring(stream);771 return;772 }773 if (controller._queue.length === 0) {774 return;775 }776 const writeRecord = PeekQueueValue(controller);777 if (writeRecord === 'close') {778 WritableStreamDefaultControllerProcessClose(controller);779 } else {780 WritableStreamDefaultControllerProcessWrite(controller, writeRecord.chunk);781 }782}783function WritableStreamDefaultControllerErrorIfNeeded(controller: WritableStreamDefaultController<any>, error: any) {784 if (controller._controlledWritableStream._state === 'writable') {785 WritableStreamDefaultControllerError(controller, error);786 }787}788function WritableStreamDefaultControllerProcessClose(controller: WritableStreamDefaultController<any>) {789 const stream = controller._controlledWritableStream;790 WritableStreamMarkCloseRequestInFlight(stream);791 DequeueValue(controller);792 assert(controller._queue.length === 0);793 const sinkClosePromise = controller._closeAlgorithm();794 WritableStreamDefaultControllerClearAlgorithms(controller);795 sinkClosePromise.then(796 () => {797 WritableStreamFinishInFlightClose(stream);798 },799 reason => {800 WritableStreamFinishInFlightCloseWithError(stream, reason);801 }802 ).catch(rethrowAssertionErrorRejection);803}804function WritableStreamDefaultControllerProcessWrite<W>(controller: WritableStreamDefaultController<W>, chunk: W) {805 const stream = controller._controlledWritableStream;806 WritableStreamMarkFirstWriteRequestInFlight(stream);807 const sinkWritePromise = controller._writeAlgorithm(chunk);808 sinkWritePromise.then(809 () => {810 WritableStreamFinishInFlightWrite(stream);811 const state = stream._state;812 assert(state === 'writable' || state === 'erroring');813 DequeueValue(controller);814 if (WritableStreamCloseQueuedOrInFlight(stream) === false && state === 'writable') {815 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);816 WritableStreamUpdateBackpressure(stream, backpressure);817 }818 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);819 },820 reason => {821 if (stream._state === 'writable') {822 WritableStreamDefaultControllerClearAlgorithms(controller);823 }824 WritableStreamFinishInFlightWriteWithError(stream, reason);825 }826 ).catch(rethrowAssertionErrorRejection);827}828function WritableStreamDefaultControllerGetBackpressure(controller: WritableStreamDefaultController<any>): boolean {829 const desiredSize = WritableStreamDefaultControllerGetDesiredSize(controller);830 return desiredSize <= 0;831}832// A client of WritableStreamDefaultController may use these functions directly to bypass state check.833function WritableStreamDefaultControllerError(controller: WritableStreamDefaultController<any>, error: any) {834 const stream = controller._controlledWritableStream;835 assert(stream._state === 'writable');836 WritableStreamDefaultControllerClearAlgorithms(controller);837 WritableStreamStartErroring(stream, error);838}839// Helper functions for the WritableStream.840function streamBrandCheckException(name: string): TypeError {841 return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);842}843// Helper functions for the WritableStreamDefaultWriter.844function defaultWriterBrandCheckException(name: string): TypeError {845 return new TypeError(846 `WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);847}848function defaultWriterLockException(name: string): TypeError {849 return new TypeError('Cannot ' + name + ' a stream using a released writer');850}851function defaultWriterClosedPromiseInitialize(writer: WritableStreamDefaultWriter<any>) {852 writer._closedPromise = new Promise((resolve, reject) => {853 writer._closedPromise_resolve = resolve;854 writer._closedPromise_reject = reject;855 writer._closedPromiseState = 'pending';856 });857}858function defaultWriterClosedPromiseInitializeAsRejected(writer: WritableStreamDefaultWriter<any>, reason: any) {859 defaultWriterClosedPromiseInitialize(writer);860 defaultWriterClosedPromiseReject(writer, reason);861}862function defaultWriterClosedPromiseInitializeAsResolved(writer: WritableStreamDefaultWriter<any>) {863 defaultWriterClosedPromiseInitialize(writer);864 defaultWriterClosedPromiseResolve(writer);865}866function defaultWriterClosedPromiseReject(writer: WritableStreamDefaultWriter<any>, reason: any) {867 assert(writer._closedPromise_resolve !== undefined);868 assert(writer._closedPromise_reject !== undefined);869 assert(writer._closedPromiseState === 'pending');870 writer._closedPromise.catch(() => {});871 writer._closedPromise_reject!(reason);872 writer._closedPromise_resolve = undefined;873 writer._closedPromise_reject = undefined;874 writer._closedPromiseState = 'rejected';875}876function defaultWriterClosedPromiseResetToRejected(writer: WritableStreamDefaultWriter<any>, reason: any) {877 assert(writer._closedPromise_resolve === undefined);...
writable-stream.js
Source:writable-stream.js
...309 defaultWriterReadyPromiseInitialize(this);310 } else {311 defaultWriterReadyPromiseInitializeAsResolved(this);312 }313 defaultWriterClosedPromiseInitialize(this);314 } else if (state === 'erroring') {315 defaultWriterReadyPromiseInitializeAsRejected(this, stream._storedError);316 this._readyPromise.catch(() => {});317 defaultWriterClosedPromiseInitialize(this);318 } else if (state === 'closed') {319 defaultWriterReadyPromiseInitializeAsResolved(this);320 defaultWriterClosedPromiseInitializeAsResolved(this);321 } else {322 assert(state === 'errored', 'state must be errored');323 const storedError = stream._storedError;324 defaultWriterReadyPromiseInitializeAsRejected(this, storedError);325 this._readyPromise.catch(() => {});326 defaultWriterClosedPromiseInitializeAsRejected(this, storedError);327 this._closedPromise.catch(() => {});328 }329 }330 get closed() {331 if (IsWritableStreamDefaultWriter(this) === false) {332 return Promise.reject(defaultWriterBrandCheckException('closed'));333 }334 return this._closedPromise;335 }336 get desiredSize() {337 if (IsWritableStreamDefaultWriter(this) === false) {338 throw defaultWriterBrandCheckException('desiredSize');339 }340 if (this._ownerWritableStream === undefined) {341 throw defaultWriterLockException('desiredSize');342 }343 return WritableStreamDefaultWriterGetDesiredSize(this);344 }345 get ready() {346 if (IsWritableStreamDefaultWriter(this) === false) {347 return Promise.reject(defaultWriterBrandCheckException('ready'));348 }349 return this._readyPromise;350 }351 abort(reason) {352 if (IsWritableStreamDefaultWriter(this) === false) {353 return Promise.reject(defaultWriterBrandCheckException('abort'));354 }355 if (this._ownerWritableStream === undefined) {356 return Promise.reject(defaultWriterLockException('abort'));357 }358 return WritableStreamDefaultWriterAbort(this, reason);359 }360 close() {361 if (IsWritableStreamDefaultWriter(this) === false) {362 return Promise.reject(defaultWriterBrandCheckException('close'));363 }364 const stream = this._ownerWritableStream;365 if (stream === undefined) {366 return Promise.reject(defaultWriterLockException('close'));367 }368 if (WritableStreamCloseQueuedOrInFlight(stream) === true) {369 return Promise.reject(new TypeError('cannot close an already-closing stream'));370 }371 return WritableStreamDefaultWriterClose(this);372 }373 releaseLock() {374 if (IsWritableStreamDefaultWriter(this) === false) {375 throw defaultWriterBrandCheckException('releaseLock');376 }377 const stream = this._ownerWritableStream;378 if (stream === undefined) {379 return;380 }381 assert(stream._writer !== undefined);382 WritableStreamDefaultWriterRelease(this);383 }384 write(chunk) {385 if (IsWritableStreamDefaultWriter(this) === false) {386 return Promise.reject(defaultWriterBrandCheckException('write'));387 }388 if (this._ownerWritableStream === undefined) {389 return Promise.reject(defaultWriterLockException('write to'));390 }391 return WritableStreamDefaultWriterWrite(this, chunk);392 }393}394// Abstract operations for the WritableStreamDefaultWriter.395function IsWritableStreamDefaultWriter(x) {396 if (!typeIsObject(x)) {397 return false;398 }399 if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {400 return false;401 }402 return true;403}404// A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.405function WritableStreamDefaultWriterAbort(writer, reason) {406 const stream = writer._ownerWritableStream;407 assert(stream !== undefined);408 return WritableStreamAbort(stream, reason);409}410function WritableStreamDefaultWriterClose(writer) {411 const stream = writer._ownerWritableStream;412 assert(stream !== undefined);413 const state = stream._state;414 if (state === 'closed' || state === 'errored') {415 return Promise.reject(new TypeError(416 `The stream (in ${state} state) is not in the writable state and cannot be closed`));417 }418 assert(state === 'writable' || state === 'erroring');419 assert(WritableStreamCloseQueuedOrInFlight(stream) === false);420 const promise = new Promise((resolve, reject) => {421 const closeRequest = {422 _resolve: resolve,423 _reject: reject424 };425 stream._closeRequest = closeRequest;426 });427 if (stream._backpressure === true && state === 'writable') {428 defaultWriterReadyPromiseResolve(writer);429 }430 WritableStreamDefaultControllerClose(stream._writableStreamController);431 return promise;432}433function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) {434 const stream = writer._ownerWritableStream;435 assert(stream !== undefined);436 const state = stream._state;437 if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {438 return Promise.resolve();439 }440 if (state === 'errored') {441 return Promise.reject(stream._storedError);442 }443 assert(state === 'writable' || state === 'erroring');444 return WritableStreamDefaultWriterClose(writer);445}446function WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, error) {447 if (writer._closedPromiseState === 'pending') {448 defaultWriterClosedPromiseReject(writer, error);449 } else {450 defaultWriterClosedPromiseResetToRejected(writer, error);451 }452 writer._closedPromise.catch(() => {});453}454function WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error) {455 if (writer._readyPromiseState === 'pending') {456 defaultWriterReadyPromiseReject(writer, error);457 } else {458 defaultWriterReadyPromiseResetToRejected(writer, error);459 }460 writer._readyPromise.catch(() => {});461}462function WritableStreamDefaultWriterGetDesiredSize(writer) {463 const stream = writer._ownerWritableStream;464 const state = stream._state;465 if (state === 'errored' || state === 'erroring') {466 return null;467 }468 if (state === 'closed') {469 return 0;470 }471 return WritableStreamDefaultControllerGetDesiredSize(stream._writableStreamController);472}473function WritableStreamDefaultWriterRelease(writer) {474 const stream = writer._ownerWritableStream;475 assert(stream !== undefined);476 assert(stream._writer === writer);477 const releasedError = new TypeError(478 'Writer was released and can no longer be used to monitor the stream\'s closedness');479 WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError);480 // The state transitions to "errored" before the sink abort() method runs, but the writer.closed promise is not481 // rejected until afterwards. This means that simply testing state will not work.482 WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError);483 stream._writer = undefined;484 writer._ownerWritableStream = undefined;485}486function WritableStreamDefaultWriterWrite(writer, chunk) {487 const stream = writer._ownerWritableStream;488 assert(stream !== undefined);489 const controller = stream._writableStreamController;490 const chunkSize = WritableStreamDefaultControllerGetChunkSize(controller, chunk);491 if (stream !== writer._ownerWritableStream) {492 return Promise.reject(defaultWriterLockException('write to'));493 }494 const state = stream._state;495 if (state === 'errored') {496 return Promise.reject(stream._storedError);497 }498 if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {499 return Promise.reject(new TypeError('The stream is closing or closed and cannot be written to'));500 }501 if (state === 'erroring') {502 return Promise.reject(stream._storedError);503 }504 assert(state === 'writable');505 const promise = WritableStreamAddWriteRequest(stream);506 WritableStreamDefaultControllerWrite(controller, chunk, chunkSize);507 return promise;508}509class WritableStreamDefaultController {510 constructor(stream, underlyingSink, size, highWaterMark) {511 if (IsWritableStream(stream) === false) {512 throw new TypeError('WritableStreamDefaultController can only be constructed with a WritableStream instance');513 }514 if (stream._writableStreamController !== undefined) {515 throw new TypeError(516 'WritableStreamDefaultController instances can only be created by the WritableStream constructor');517 }518 this._controlledWritableStream = stream;519 this._underlyingSink = underlyingSink;520 // Need to set the slots so that the assert doesn't fire. In the spec the slots already exist implicitly.521 this._queue = undefined;522 this._queueTotalSize = undefined;523 ResetQueue(this);524 this._started = false;525 const normalizedStrategy = ValidateAndNormalizeQueuingStrategy(size, highWaterMark);526 this._strategySize = normalizedStrategy.size;527 this._strategyHWM = normalizedStrategy.highWaterMark;528 const backpressure = WritableStreamDefaultControllerGetBackpressure(this);529 WritableStreamUpdateBackpressure(stream, backpressure);530 }531 error(e) {532 if (IsWritableStreamDefaultController(this) === false) {533 throw new TypeError(534 'WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');535 }536 const state = this._controlledWritableStream._state;537 if (state !== 'writable') {538 // The stream is closed, errored or will be soon. The sink can't do anything useful if it gets an error here, so539 // just treat it as a no-op.540 return;541 }542 WritableStreamDefaultControllerError(this, e);543 }544 [AbortSteps](reason) {545 return PromiseInvokeOrNoop(this._underlyingSink, 'abort', [reason]);546 }547 [ErrorSteps]() {548 ResetQueue(this);549 }550 [StartSteps]() {551 const startResult = InvokeOrNoop(this._underlyingSink, 'start', [this]);552 const stream = this._controlledWritableStream;553 Promise.resolve(startResult).then(554 () => {555 assert(stream._state === 'writable' || stream._state === 'erroring');556 this._started = true;557 WritableStreamDefaultControllerAdvanceQueueIfNeeded(this);558 },559 r => {560 assert(stream._state === 'writable' || stream._state === 'erroring');561 this._started = true;562 WritableStreamDealWithRejection(stream, r);563 }564 )565 .catch(rethrowAssertionErrorRejection);566 }567}568// Abstract operations implementing interface required by the WritableStream.569function WritableStreamDefaultControllerClose(controller) {570 EnqueueValueWithSize(controller, 'close', 0);571 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);572}573function WritableStreamDefaultControllerGetChunkSize(controller, chunk) {574 const strategySize = controller._strategySize;575 if (strategySize === undefined) {576 return 1;577 }578 try {579 return strategySize(chunk);580 } catch (chunkSizeE) {581 WritableStreamDefaultControllerErrorIfNeeded(controller, chunkSizeE);582 return 1;583 }584}585function WritableStreamDefaultControllerGetDesiredSize(controller) {586 return controller._strategyHWM - controller._queueTotalSize;587}588function WritableStreamDefaultControllerWrite(controller, chunk, chunkSize) {589 const writeRecord = { chunk };590 try {591 EnqueueValueWithSize(controller, writeRecord, chunkSize);592 } catch (enqueueE) {593 WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueE);594 return;595 }596 const stream = controller._controlledWritableStream;597 if (WritableStreamCloseQueuedOrInFlight(stream) === false && stream._state === 'writable') {598 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);599 WritableStreamUpdateBackpressure(stream, backpressure);600 }601 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);602}603// Abstract operations for the WritableStreamDefaultController.604function IsWritableStreamDefaultController(x) {605 if (!typeIsObject(x)) {606 return false;607 }608 if (!Object.prototype.hasOwnProperty.call(x, '_underlyingSink')) {609 return false;610 }611 return true;612}613function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {614 const stream = controller._controlledWritableStream;615 if (controller._started === false) {616 return;617 }618 if (stream._inFlightWriteRequest !== undefined) {619 return;620 }621 const state = stream._state;622 if (state === 'closed' || state === 'errored') {623 return;624 }625 if (state === 'erroring') {626 WritableStreamFinishErroring(stream);627 return;628 }629 if (controller._queue.length === 0) {630 return;631 }632 const writeRecord = PeekQueueValue(controller);633 if (writeRecord === 'close') {634 WritableStreamDefaultControllerProcessClose(controller);635 } else {636 WritableStreamDefaultControllerProcessWrite(controller, writeRecord.chunk);637 }638}639function WritableStreamDefaultControllerErrorIfNeeded(controller, error) {640 if (controller._controlledWritableStream._state === 'writable') {641 WritableStreamDefaultControllerError(controller, error);642 }643}644function WritableStreamDefaultControllerProcessClose(controller) {645 const stream = controller._controlledWritableStream;646 WritableStreamMarkCloseRequestInFlight(stream);647 DequeueValue(controller);648 assert(controller._queue.length === 0, 'queue must be empty once the final write record is dequeued');649 const sinkClosePromise = PromiseInvokeOrNoop(controller._underlyingSink, 'close', []);650 sinkClosePromise.then(651 () => {652 WritableStreamFinishInFlightClose(stream);653 },654 reason => {655 WritableStreamFinishInFlightCloseWithError(stream, reason);656 }657 )658 .catch(rethrowAssertionErrorRejection);659}660function WritableStreamDefaultControllerProcessWrite(controller, chunk) {661 const stream = controller._controlledWritableStream;662 WritableStreamMarkFirstWriteRequestInFlight(stream);663 const sinkWritePromise = PromiseInvokeOrNoop(controller._underlyingSink, 'write', [chunk, controller]);664 sinkWritePromise.then(665 () => {666 WritableStreamFinishInFlightWrite(stream);667 const state = stream._state;668 assert(state === 'writable' || state === 'erroring');669 DequeueValue(controller);670 if (WritableStreamCloseQueuedOrInFlight(stream) === false && state === 'writable') {671 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);672 WritableStreamUpdateBackpressure(stream, backpressure);673 }674 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);675 },676 reason => {677 WritableStreamFinishInFlightWriteWithError(stream, reason);678 }679 )680 .catch(rethrowAssertionErrorRejection);681}682function WritableStreamDefaultControllerGetBackpressure(controller) {683 const desiredSize = WritableStreamDefaultControllerGetDesiredSize(controller);684 return desiredSize <= 0;685}686// A client of WritableStreamDefaultController may use these functions directly to bypass state check.687function WritableStreamDefaultControllerError(controller, error) {688 const stream = controller._controlledWritableStream;689 assert(stream._state === 'writable');690 WritableStreamStartErroring(stream, error);691}692// Helper functions for the WritableStream.693function streamBrandCheckException(name) {694 return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);695}696// Helper functions for the WritableStreamDefaultWriter.697function defaultWriterBrandCheckException(name) {698 return new TypeError(699 `WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);700}701function defaultWriterLockException(name) {702 return new TypeError('Cannot ' + name + ' a stream using a released writer');703}704function defaultWriterClosedPromiseInitialize(writer) {705 writer._closedPromise = new Promise((resolve, reject) => {706 writer._closedPromise_resolve = resolve;707 writer._closedPromise_reject = reject;708 writer._closedPromiseState = 'pending';709 });710}711function defaultWriterClosedPromiseInitializeAsRejected(writer, reason) {712 writer._closedPromise = Promise.reject(reason);713 writer._closedPromise_resolve = undefined;714 writer._closedPromise_reject = undefined;715 writer._closedPromiseState = 'rejected';716}717function defaultWriterClosedPromiseInitializeAsResolved(writer) {718 writer._closedPromise = Promise.resolve(undefined);...
Using AI Code Generation
1var wpt = require('web-platform-test');2wpt.defaultWriterClosedPromiseInitialize();3var wpt = require('web-platform-test');4wpt.defaultWriterClosedPromiseInitialize();5## wpt.defaultWriterClosedPromiseInitialize()6## wpt.isDefaultWriterClosed()7## wpt.getWriters()8## wpt.getWriter()9## wpt.releaseWriter(writer)10## wpt.abortWriter(writer)11## wpt.writeToStream(writer, chunk)12## wpt.closeWriter(writer)13## wpt.createWritableStream()14## wpt.createCountQueuingStrategy()15## wpt.createByteLengthQueuingStrategy()16## wpt.createTransformStream()17## wpt.createPipeThrough()18## wpt.createPipeTo()19## wpt.createReadableStream()20## wpt.createReadableByteStream()21## wpt.createReadableStreamDefaultController()22## wpt.createReadableStreamBYOBRequest()23## wpt.createReadableByteStreamController()24## wpt.createReadableStreamDefaultReader()25## wpt.createReadableStreamBYOBReader()26## wpt.createWritableStreamDefaultWriter()27## wpt.createWritableStreamDefaultController()28## wpt.createTransformStreamDefaultController()
Using AI Code Generation
1const wptools = require('wptools');2const defaultWriterClosedPromiseInitialize = wptools.defaultWriterClosedPromiseInitialize;3const defaultWriterClosedPromise = defaultWriterClosedPromiseInitialize();4console.log(defaultWriterClosedPromise);5Promise { <pending> }6const wptools = require('wptools');7const defaultWriterClosedPromiseInitialize = wptools.defaultWriterClosedPromiseInitialize;8const defaultWriterClosedPromise = defaultWriterClosedPromiseInitialize();9console.log(defaultWriterClosedPromise);10defaultWriterClosedPromise.then(() => {11 console.log('default writer closed');12});13Promise { <pending> }14const wptools = require('wptools');15const defaultWriterClosedPromiseInitialize = wptools.defaultWriterClosedPromiseInitialize;16const defaultWriterClosedPromise = defaultWriterClosedPromiseInitialize();17console.log(defaultWriterClosedPromise);18defaultWriterClosedPromise.catch(() => {19 console.log('default writer closed');20});21Promise { <pending> }
Using AI Code Generation
1const wpt = require('wpt');2wpt.defaultWriterClosedPromiseInitialize();3const wpt = require('wpt');4wpt.defaultWriterClosedPromiseInitialize();5const wpt = require('wpt');6wpt.defaultWriterClosedPromiseInitialize();7const wpt = require('wpt');8wpt.defaultWriterClosedPromiseInitialize();9const wpt = require('wpt');10wpt.defaultWriterClosedPromiseInitialize();11const wpt = require('wpt');12wpt.defaultWriterClosedPromiseInitialize();13const wpt = require('wpt');14wpt.defaultWriterClosedPromiseInitialize();15const wpt = require('wpt');16wpt.defaultWriterClosedPromiseInitialize();17const wpt = require('wpt');18wpt.defaultWriterClosedPromiseInitialize();19const wpt = require('wpt');20wpt.defaultWriterClosedPromiseInitialize();
Using AI Code Generation
1const wpt = require('wpt');2wpt.defaultWriterClosedPromiseInitialize();3const wpt = require('wpt');4wpt.defaultWriterClosedPromiseInitialize();5const wpt = require('wpt');6wpt.defaultWriterClosedPromiseInitialize();7const wpt = require('wpt');8wpt.defaultWriterClosedPromiseInitialize();9const wpt = require('wpt');10wpt.defaultWriterClosedPromiseInitialize();11const wpt = require('wpt');12wpt.defaultWriterClosedPromiseInitialize();13const wpt = require('wpt');14wpt.defaultWriterClosedPromiseInitialize();15const wpt = require('wpt');16wpt.defaultWriterClosedPromiseInitialize();
Using AI Code Generation
1const writer = new WritableStreamDefaultWriter(stream);2const promise = writer.closed;3writer.close();4promise.then(5 () => console.log('The stream was closed!'),6 e => console.log('The stream was errored: ' + e)7);8const writer = new WritableStreamDefaultWriter(stream);9const promise = writer.closed;10writer.close();11promise.then(12 () => console.log('The stream was closed!'),13 e => console.log('The stream was errored: ' + e)14);15const writer = new WritableStreamDefaultWriter(stream);16const promise = writer.closed;17writer.close();18promise.then(19 () => console.log('The stream was closed!'),20 e => console.log('The stream was errored: ' + e)21);22const writer = new WritableStreamDefaultWriter(stream);23const promise = writer.closed;24writer.close();25promise.then(26 () => console.log('The stream was closed!'),27 e => console.log('The stream was errored: ' + e)28);29WritableStreamDefaultWriter.close()30WritableStreamDefaultWriter.releaseLock()31WritableStreamDefaultWriter.abort()32WritableStreamDefaultWriter.write()33WritableStreamDefaultWriter.close()34WritableStreamDefaultWriter.releaseLock()35WritableStreamDefaultWriter.abort()36WritableStreamDefaultWriter.write()37WritableStreamDefaultWriter.close()38WritableStreamDefaultWriter.releaseLock()39WritableStreamDefaultWriter.abort()40WritableStreamDefaultWriter.write()41WritableStreamDefaultWriter.close()42WritableStreamDefaultWriter.releaseLock()43WritableStreamDefaultWriter.abort()44WritableStreamDefaultWriter.write()
Using AI Code Generation
1import { defaultWriterClosedPromiseInitialize } from "./wpt.js";2const writer = new WritableStreamDefaultWriter(stream);3const closedPromise = defaultWriterClosedPromiseInitialize(writer);4writer.write("a");5writer.releaseLock();6closedPromise.then(() => console.log("closed!"));7writer.write("b").catch(e => console.error("write should not succeed", e));8export function defaultWriterClosedPromiseInitialize(writer) {9 const promise = new Promise((resolve, reject) => {10 writer._closedPromise_resolve_ = resolve;11 writer._closedPromise_reject_ = reject;12 });13 writer._closedPromise_ = promise;14 return promise;15}16export class WritableStreamDefaultWriter {17 constructor(stream) {18 this._closedPromise_resolve_ = undefined;19 this._closedPromise_reject_ = undefined;20 this._closedPromise_ = undefined;21 this._ownerWritableStream_ = stream;22 }23 write(chunk) {24 if (this._ownerWritableStream_ === undefined) {25 return Promise.reject(new TypeError("Writer is not locked to a stream"));26 }27 return this._ownerWritableStream_.write(chunk);28 }29 releaseLock() {30 if (this._ownerWritableStream_ === undefined) {31 return;32 }33 this._ownerWritableStream_.releaseWriter();34 this._ownerWritableStream_ = undefined;35 }36}37export class WritableStream {38 constructor() {39 this._writer_ = undefined;40 }41 write(chunk) {42 if (this._writer_ === undefined) {43 return Promise.reject(new TypeError("Writer is not locked to a stream"));44 }45 return Promise.resolve();46 }47 releaseWriter() {48 if (this._writer_ === undefined) {49 return Promise.reject(new TypeError("Writer is not locked to a stream"));50 }51 this._writer_._ownerWritableStream_ = undefined;52 this._writer_._closedPromise_resolve_();53 this._writer_ = undefined;54 }55}
Using AI Code Generation
1const wptools = require('wptools');2wptools.defaultWriterClosedPromiseInitialize();3const wptools = require('wptools');4wptools.writerClosedPromiseInitialize();5const wptools = require('wptools');6wptools.writerClosedPromiseInitialize();7const wptools = require('wptools');8wptools.writerClosedPromiseInitialize();
Using AI Code Generation
1const wpt = require("worker-plugin-tools");2const defaultWriterClosedPromiseInitialize = wpt.defaultWriterClosedPromiseInitialize;3const { parentPort, workerData } = require("worker_threads");4const { writer, reader, writerClosedPromise } = workerData;5defaultWriterClosedPromiseInitialize(writerClosedPromise);6parentPort.postMessage({ writer, reader });7const wpt = require("worker-plugin-tools");8const defaultWriterClosedPromiseInitialize = wpt.defaultWriterClosedPromiseInitialize;9const wpt = require("worker-plugin-tools");10const defaultWriterClosedPromise = wpt.defaultWriterClosedPromise;11const wpt = require("worker-plugin-tools");12const defaultReaderClosedPromiseInitialize = wpt.defaultReaderClosedPromiseInitialize;13const wpt = require("worker-plugin-tools");14const defaultReaderClosedPromise = wpt.defaultReaderClosedPromise;15const wpt = require("worker-plugin-tools");16const defaultReaderMessagePromiseInitialize = wpt.defaultReaderMessagePromiseInitialize;17const wpt = require("worker-plugin-tools");18const defaultReaderMessagePromise = wpt.defaultReaderMessagePromise;19const wpt = require("worker-plugin-tools");20const defaultReaderMessagePromiseResolve = wpt.defaultReaderMessagePromiseResolve;21const wpt = require("worker-plugin-tools");22const defaultReaderMessagePromiseReject = wpt.defaultReaderMessagePromiseReject;23const wpt = require("worker-plugin-tools");24const closeWriter = wpt.closeWriter;
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!!