Best JavaScript code snippet using wpt
WritableStream.js
Source:WritableStream.js
...222 const writer = stream[_writer];223 if (writer !== undefined) {224 v8.rejectPromise(writer[_closedPromise], e);225 if (state === WRITABLE &&226 WritableStreamDefaultControllerGetBackpressure(227 stream[_writableStreamController])) {228 v8.rejectPromise(writer[_readyPromise], e);229 } else {230 writer[_readyPromise] = Promise_reject(e);231 }232 }233 stream[_state] = ERRORED;234 stream[_storedError] = e;235 }236 function WritableStreamFinishClose(stream) {237 TEMP_ASSERT(stream[_state] === CLOSING,238 'stream.[[state]] is "closing".');239 TEMP_ASSERT(stream[_writer] !== undefined,240 'stream.[[writer]] is not undefined.');241 stream[_state] = CLOSED;242 v8.resolvePromise(stream[_writer][_closedPromise], undefined);243 }244 function WritableStreamFulfillWriteRequest(stream) {245 TEMP_ASSERT(stream[_writeRequests].length !== 0,246 'stream.[[writeRequests]] is not empty.');247 const writeRequest = stream[_writeRequests].shift();248 v8.resolvePromise(writeRequest, undefined);249 }250 function WritableStreamUpdateBackpressure(stream, backpressure) {251 TEMP_ASSERT(stream[_state] === WRITABLE,252 'stream.[[state]] is "writable".');253 const writer = stream[_writer];254 if (writer === undefined) {255 return;256 }257 if (backpressure) {258 writer[_readyPromise] = v8.createPromise();259 } else {260 TEMP_ASSERT(backpressure === false,261 'backpressure is false.');262 v8.resolvePromise(writer[_readyPromise], undefined);263 }264 }265 class WritableStreamDefaultWriter {266 constructor(stream) {267 if (!IsWritableStream(stream)) {268 throw new TypeError(errIllegalConstructor);269 }270 if (IsWritableStreamLocked(stream)) {271 throw new TypeError(errIllegalConstructor);272 }273 this[_ownerWritableStream] = stream;274 stream[_writer] = this;275 const state = stream[_state];276 if (state === WRITABLE || state === CLOSING) {277 this[_closedPromise] = v8.createPromise();278 } else if (state === CLOSED) {279 this[_closedPromise] = Promise_resolve(undefined);280 } else {281 TEMP_ASSERT(state === ERRORED,282 'state is "errored".');283 this[_closedPromise] = Promise_reject(stream[_storedError]);284 }285 if (state === WRITABLE &&286 WritableStreamDefaultControllerGetBackpressure(287 stream[_writableStreamController])) {288 this[_readyPromise] = v8.createPromise();289 } else {290 this[_readyPromise] = Promise_resolve(undefined);291 }292 }293 get closed() {294 if (!IsWritableStreamDefaultWriter(this)) {295 return Promise_reject(new TypeError(errIllegalInvocation));296 }297 return this[_closedPromise];298 }299 get desiredSize() {300 if (!IsWritableStreamDefaultWriter(this)) {301 throw new TypeError(errIllegalInvocation);302 }303 if (this[_ownerWritableStream] === undefined) {304 throw createWriterLockReleasedError(verbUsedToGetTheDesiredSize);305 }306 return WritableStreamDefaultWriterGetDesiredSize(this);307 }308 get ready() {309 if (!IsWritableStreamDefaultWriter(this)) {310 return Promise_reject(new TypeError(errIllegalInvocation));311 }312 return this[_readyPromise];313 }314 abort(reason) {315 if (!IsWritableStreamDefaultWriter(this)) {316 return Promise_reject(new TypeError(errIllegalInvocation));317 }318 if (this[_ownerWritableStream] === undefined) {319 return Promise_reject(createWriterLockReleasedError(verbAborted));320 }321 return WritableStreamDefaultWriterAbort(this, reason);322 }323 close() {324 if (!IsWritableStreamDefaultWriter(this)) {325 return Promise_reject(new TypeError(errIllegalInvocation));326 }327 const stream = this[_ownerWritableStream];328 if (stream === undefined) {329 return Promise_reject(createWriterLockReleasedError(verbClosed));330 }331 if (stream[_state] === CLOSING) {332 return Promise_reject(new TypeError(errCloseCloseRequestedStream));333 }334 return WritableStreamDefaultWriterClose(this);335 }336 releaseLock() {337 if (!IsWritableStreamDefaultWriter(this)) {338 throw new TypeError(errIllegalInvocation);339 }340 const stream = this[_ownerWritableStream];341 if (stream === undefined) {342 return;343 }344 TEMP_ASSERT(stream[_writer] !== undefined,345 'stream.[[writer]] is not undefined.');346 WritableStreamDefaultWriterRelease(this);347 }348 write(chunk) {349 if (!IsWritableStreamDefaultWriter(this)) {350 return Promise_reject(new TypeError(errIllegalInvocation));351 }352 const stream = this[_ownerWritableStream];353 if (stream === undefined) {354 return Promise_reject(createWriterLockReleasedError(verbWrittenTo));355 }356 if (stream[_state] === CLOSING) {357 return Promise_reject(new TypeError(errWriteCloseRequestedStream));358 }359 return WritableStreamDefaultWriterWrite(this, chunk);360 }361 }362 // Writable Stream Writer Abstract Operations363 function IsWritableStreamDefaultWriter(x) {364 return hasOwnProperty(x, _ownerWritableStream);365 }366 function WritableStreamDefaultWriterAbort(writer, reason) {367 const stream = writer[_ownerWritableStream];368 TEMP_ASSERT(stream !== undefined,369 'stream is not undefined.');370 return WritableStreamAbort(stream, reason);371 }372 function WritableStreamDefaultWriterClose(writer) {373 const stream = writer[_ownerWritableStream];374 TEMP_ASSERT(stream !== undefined,375 'stream is not undefined.');376 const state = stream[_state];377 if (state === CLOSED || state === ERRORED) {378 return Promise_reject(379 createCannotActionOnStateStreamError('close', state));380 }381 TEMP_ASSERT(state === WRITABLE,382 'state is "writable".');383 const promise = WritableStreamAddWriteRequest(stream);384 if (WritableStreamDefaultControllerGetBackpressure(385 stream[_writableStreamController])) {386 v8.resolvePromise(writer[_readyPromise], undefined);387 }388 stream[_state] = CLOSING;389 WritableStreamDefaultControllerClose(stream[_writableStreamController]);390 return promise;391 }392 function WritableStreamDefaultWriterGetDesiredSize(writer) {393 const stream = writer[_ownerWritableStream];394 const state = stream[_state];395 if (state === ERRORED) {396 return null;397 }398 if (state === CLOSED) {399 return 0;400 }401 return WritableStreamDefaultControllerGetDesiredSize(402 stream[_writableStreamController]);403 }404 function WritableStreamDefaultWriterRelease(writer) {405 const stream = writer[_ownerWritableStream];406 TEMP_ASSERT(stream !== undefined,407 'stream is not undefined.');408 TEMP_ASSERT(stream[_writer] === writer,409 'stream.[[writer]] is writer.');410 const releasedError = new TypeError(errReleasedWriterClosedPromise);411 const state = stream[_state];412 if (state === WRITABLE || state === CLOSING) {413 v8.rejectPromise(writer[_closedPromise], releasedError);414 } else {415 writer[_closedPromise] = Promise_reject(releasedError);416 }417 if (state === WRITABLE &&418 WritableStreamDefaultControllerGetBackpressure(419 stream[_writableStreamController])) {420 v8.rejectPromise(writer[_readyPromise], releasedError);421 } else {422 writer[_readyPromise] = Promise_reject(releasedError);423 }424 stream[_writer] = undefined;425 writer[_ownerWritableStream] = undefined;426 }427 function WritableStreamDefaultWriterWrite(writer, chunk) {428 const stream = writer[_ownerWritableStream];429 TEMP_ASSERT(stream !== undefined,430 'stream is not undefined.');431 const state = stream[_state];432 if (state === CLOSED || state === ERRORED) {433 return Promise_reject(434 createCannotActionOnStateStreamError('write to', state));435 }436 TEMP_ASSERT(state === WRITABLE,437 'state is "writable".');438 const promise = WritableStreamAddWriteRequest(stream);439 WritableStreamDefaultControllerWrite(stream[_writableStreamController],440 chunk);441 return promise;442 }443 class WritableStreamDefaultController {444 constructor(stream, underlyingSink, size, highWaterMark) {445 if (!IsWritableStream(stream)) {446 throw new TypeError(errIllegalConstructor);447 }448 if (stream[_writableStreamController] !== undefined) {449 throw new TypeError(errIllegalConstructor);450 }451 this[_controlledWritableStream] = stream;452 this[_underlyingSink] = underlyingSink;453 this[_queue] = new v8.InternalPackedArray();454 this[_queueSize] = 0;455 this[_defaultControllerFlags] = 0;456 const normalizedStrategy =457 ValidateAndNormalizeQueuingStrategy(size, highWaterMark);458 this[_strategySize] = normalizedStrategy.size;459 this[_strategyHWM] = normalizedStrategy.highWaterMark;460 const backpressure = WritableStreamDefaultControllerGetBackpressure(this);461 if (backpressure) {462 WritableStreamUpdateBackpressure(stream, backpressure);463 }464 const controller = this;465 const startResult = InvokeOrNoop(underlyingSink, 'start', [this]);466 const onFulfilled = () => {467 setDefaultControllerStartedFlag(controller, true);468 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);469 };470 const onRejected = r => {471 WritableStreamDefaultControllerErrorIfNeeded(controller, r);472 };473 thenPromise(Promise_resolve(startResult), onFulfilled, onRejected);474 }475 error(e) {476 if (!IsWritableStreamDefaultController(this)) {477 throw new TypeError(errIllegalInvocation);478 }479 const state = this[_controlledWritableStream][_state];480 if (state === CLOSED || state === ERRORED) {481 throw createCannotActionOnStateStreamError('error', state);482 }483 WritableStreamDefaultControllerError(this, e);484 }485 }486 // Writable Stream Default Controller Abstract Operations487 function IsWritableStreamDefaultController(x) {488 return hasOwnProperty(x, _underlyingSink);489 }490 function WritableStreamDefaultControllerAbort(controller, reason) {491 controller[_queue] = v8.InternalPackedArray();492 controller[_queueSize] = 0;493 const sinkAbortPromise =494 PromiseInvokeOrFallbackOrNoop(controller[_underlyingSink],495 'abort', [reason], 'close', [controller]);496 return thenPromise(sinkAbortPromise, () => undefined);497 }498 function WritableStreamDefaultControllerClose(controller) {499 EnqueueValueWithSizeForController(controller, 'close', 0);500 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);501 }502 function WritableStreamDefaultControllerGetDesiredSize(controller) {503 const queueSize = GetTotalQueueSizeForController(controller);504 return controller[_strategyHWM] - queueSize;505 }506 function WritableStreamDefaultControllerWrite(controller, chunk) {507 const stream = controller[_controlledWritableStream];508 TEMP_ASSERT(stream[_state] === WRITABLE,509 'stream.[[state]] is "writable".');510 let chunkSize = 1;511 if (controller[_strategySize] !== undefined) {512 try {513 chunkSize = Function_call(controller[_strategySize], undefined, chunk);514 } catch (e) {515 WritableStreamDefaultControllerErrorIfNeeded(controller, e);516 return Promise_reject(e);517 }518 }519 const writeRecord = {chunk};520 const lastBackpressure =521 WritableStreamDefaultControllerGetBackpressure(controller);522 try {523 const enqueueResult =524 EnqueueValueWithSizeForController(controller, writeRecord, chunkSize);525 } catch (e) {526 WritableStreamDefaultControllerErrorIfNeeded(controller, e);527 return Promise_reject(e);528 }529 if (stream[_state] === WRITABLE) {530 const backpressure =531 WritableStreamDefaultControllerGetBackpressure(controller);532 if (lastBackpressure !== backpressure) {533 WritableStreamUpdateBackpressure(stream, backpressure);534 }535 }536 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);537 }538 function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {539 const state = controller[_controlledWritableStream][_state];540 if (state === CLOSED || state === ERRORED) {541 return;542 }543 if (!getDefaultControllerStartedFlag(controller)) {544 return;545 }546 if (getDefaultControllerWritingFlag(controller)) {547 return;548 }549 if (controller[_queue].length === 0) {550 return;551 }552 const writeRecord = PeekQueueValue(controller[_queue]);553 if (writeRecord === 'close') {554 WritableStreamDefaultControllerProcessClose(controller);555 } else {556 WritableStreamDefaultControllerProcessWrite(controller,557 writeRecord.chunk);558 }559 }560 function WritableStreamDefaultControllerErrorIfNeeded(controller, e) {561 const state = controller[_controlledWritableStream][_state];562 if (state === WRITABLE || state === CLOSING) {563 WritableStreamDefaultControllerError(controller, e);564 }565 }566 function WritableStreamDefaultControllerProcessClose(controller) {567 const stream = controller[_controlledWritableStream];568 TEMP_ASSERT(stream[_state] === CLOSING,569 'stream.[[state]] is "closing".');570 DequeueValueForController(controller);571 TEMP_ASSERT(controller[_queue].length === 0,572 'controller.[[queue]] is empty.');573 const sinkClosePromise = PromiseInvokeOrNoop(controller[_underlyingSink],574 'close', [controller]);575 thenPromise(sinkClosePromise,576 () => {577 if (stream[_state] !== CLOSING) {578 return;579 }580 WritableStreamFulfillWriteRequest(stream);581 WritableStreamFinishClose(stream);582 },583 r => WritableStreamDefaultControllerErrorIfNeeded(controller, r)584 );585 }586 function WritableStreamDefaultControllerProcessWrite(controller, chunk) {587 setDefaultControllerWritingFlag(controller, true);588 const sinkWritePromise = PromiseInvokeOrNoop(controller[_underlyingSink],589 'write', [chunk, controller]);590 thenPromise(591 sinkWritePromise,592 () => {593 const stream = controller[_controlledWritableStream];594 const state = stream[_state];595 if (state === ERRORED || state === CLOSED) {596 return;597 }598 setDefaultControllerWritingFlag(controller, false);599 WritableStreamFulfillWriteRequest(stream);600 const lastBackpressure =601 WritableStreamDefaultControllerGetBackpressure(controller);602 DequeueValueForController(controller);603 if (state !== CLOSING) {604 const backpressure =605 WritableStreamDefaultControllerGetBackpressure(controller);606 if (lastBackpressure !== backpressure) {607 WritableStreamUpdateBackpressure(608 controller[_controlledWritableStream], backpressure);609 }610 }611 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);612 },613 r => WritableStreamDefaultControllerErrorIfNeeded(controller, r)614 );615 }616 function WritableStreamDefaultControllerGetBackpressure(controller) {617 const desiredSize =618 WritableStreamDefaultControllerGetDesiredSize(controller);619 return desiredSize <= 0;620 }621 function WritableStreamDefaultControllerError(controller, e) {622 const stream = controller[_controlledWritableStream];623 const state = stream[_state];624 TEMP_ASSERT(state === WRITABLE || state === CLOSING,625 'stream.[[state]] is "writable" or "closing".');626 WritableStreamError(stream, e);627 controller[_queue] = new v8.InternalPackedArray();628 controller[_queueSize] = 0;629 }630 // Queue-with-Sizes Operations...
writable-stream.js
Source:writable-stream.js
...111 if (writer !== undefined) {112 defaultWriterClosedPromiseReject(writer, e);113 writer._closedPromise.catch(() => {});114 if (state === 'writable' &&115 WritableStreamDefaultControllerGetBackpressure(stream._writableStreamController) === true) {116 defaultWriterReadyPromiseReject(writer, e);117 } else {118 defaultWriterReadyPromiseResetToRejected(writer, e);119 }120 writer._readyPromise.catch(() => {});121 }122 stream._state = 'errored';123 stream._storedError = e;124}125function WritableStreamFinishClose(stream) {126 assert(stream._state === 'closing');127 // writer cannot be released while close() is ongoing. So, we can assert that128 // there's an active writer.129 assert(stream._writer !== undefined);130 stream._state = 'closed';131 defaultWriterClosedPromiseResolve(stream._writer);132}133function WritableStreamFulfillWriteRequest(stream) {134 assert(stream._writeRequests.length > 0);135 const writeRequest = stream._writeRequests.shift();136 writeRequest._resolve(undefined);137}138function WritableStreamUpdateBackpressure(stream, backpressure) {139 assert(stream._state === 'writable');140 const writer = stream._writer;141 if (writer === undefined) {142 return;143 }144 if (backpressure === true) {145 defaultWriterReadyPromiseReset(writer);146 } else {147 assert(backpressure === false);148 defaultWriterReadyPromiseResolve(writer);149 }150}151class WritableStreamDefaultWriter {152 constructor(stream) {153 if (IsWritableStream(stream) === false) {154 throw new TypeError('WritableStreamDefaultWriter can only be constructed with a WritableStream instance');155 }156 if (IsWritableStreamLocked(stream) === true) {157 throw new TypeError('This stream has already been locked for exclusive writing by another writer');158 }159 this._ownerWritableStream = stream;160 stream._writer = this;161 const state = stream._state;162 if (state === 'writable' || state === 'closing') {163 defaultWriterClosedPromiseInitialize(this);164 } else if (state === 'closed') {165 defaultWriterClosedPromiseInitializeAsResolved(this);166 } else {167 assert(state === 'errored', 'state must be errored');168 defaultWriterClosedPromiseInitializeAsRejected(this, stream._storedError);169 this._closedPromise.catch(() => {});170 }171 if (state === 'writable' &&172 WritableStreamDefaultControllerGetBackpressure(stream._writableStreamController) === true) {173 defaultWriterReadyPromiseInitialize(this);174 } else {175 defaultWriterReadyPromiseInitializeAsResolved(this, undefined);176 }177 }178 get closed() {179 if (IsWritableStreamDefaultWriter(this) === false) {180 return Promise.reject(defaultWriterBrandCheckException('closed'));181 }182 return this._closedPromise;183 }184 get desiredSize() {185 if (IsWritableStreamDefaultWriter(this) === false) {186 throw defaultWriterBrandCheckException('desiredSize');187 }188 if (this._ownerWritableStream === undefined) {189 throw defaultWriterLockException('desiredSize');190 }191 return WritableStreamDefaultWriterGetDesiredSize(this);192 }193 get ready() {194 if (IsWritableStreamDefaultWriter(this) === false) {195 return Promise.reject(defaultWriterBrandCheckException('ready'));196 }197 return this._readyPromise;198 }199 abort(reason) {200 if (IsWritableStreamDefaultWriter(this) === false) {201 return Promise.reject(defaultWriterBrandCheckException('abort'));202 }203 if (this._ownerWritableStream === undefined) {204 return Promise.reject(defaultWriterLockException('abort'));205 }206 return WritableStreamDefaultWriterAbort(this, reason);207 }208 close() {209 if (IsWritableStreamDefaultWriter(this) === false) {210 return Promise.reject(defaultWriterBrandCheckException('close'));211 }212 const stream = this._ownerWritableStream;213 if (stream === undefined) {214 return Promise.reject(defaultWriterLockException('close'));215 }216 if (stream._state === 'closing') {217 return Promise.reject(new TypeError('cannot close an already-closing stream'));218 }219 return WritableStreamDefaultWriterClose(this);220 }221 releaseLock() {222 if (IsWritableStreamDefaultWriter(this) === false) {223 throw defaultWriterBrandCheckException('releaseLock');224 }225 const stream = this._ownerWritableStream;226 if (stream === undefined) {227 return;228 }229 assert(stream._writer !== undefined);230 WritableStreamDefaultWriterRelease(this);231 }232 write(chunk) {233 if (IsWritableStreamDefaultWriter(this) === false) {234 return Promise.reject(defaultWriterBrandCheckException('write'));235 }236 const stream = this._ownerWritableStream;237 if (stream === undefined) {238 return Promise.reject(defaultWriterLockException('write to'));239 }240 if (stream._state === 'closing') {241 return Promise.reject(new TypeError('Cannot write to an already-closed stream'));242 }243 return WritableStreamDefaultWriterWrite(this, chunk);244 }245}246// Abstract operations for the WritableStreamDefaultWriter.247function IsWritableStreamDefaultWriter(x) {248 if (!typeIsObject(x)) {249 return false;250 }251 if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {252 return false;253 }254 return true;255}256// A client of WritableStreamDefaultWriter may use these functions directly to bypass state check.257function WritableStreamDefaultWriterAbort(writer, reason) {258 const stream = writer._ownerWritableStream;259 assert(stream !== undefined);260 return WritableStreamAbort(stream, reason);261}262function WritableStreamDefaultWriterClose(writer) {263 const stream = writer._ownerWritableStream;264 assert(stream !== undefined);265 const state = stream._state;266 if (state === 'closed' || state === 'errored') {267 return Promise.reject(new TypeError(268 `The stream (in ${state} state) is not in the writable state and cannot be closed`));269 }270 assert(state === 'writable');271 const promise = WritableStreamAddWriteRequest(stream);272 if (WritableStreamDefaultControllerGetBackpressure(stream._writableStreamController) === true) {273 defaultWriterReadyPromiseResolve(writer);274 }275 stream._state = 'closing';276 WritableStreamDefaultControllerClose(stream._writableStreamController);277 return promise;278}279function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) {280 const stream = writer._ownerWritableStream;281 assert(stream !== undefined);282 const state = stream._state;283 if (state === 'closing' || state === 'closed') {284 return Promise.resolve();285 }286 if (state === 'errored') {287 return Promise.reject(stream._storedError);288 }289 assert(state === 'writable');290 return WritableStreamDefaultWriterClose(writer);291}292function WritableStreamDefaultWriterGetDesiredSize(writer) {293 const stream = writer._ownerWritableStream;294 const state = stream._state;295 if (state === 'errored') {296 return null;297 }298 if (state === 'closed') {299 return 0;300 }301 return WritableStreamDefaultControllerGetDesiredSize(stream._writableStreamController);302}303function WritableStreamDefaultWriterRelease(writer) {304 const stream = writer._ownerWritableStream;305 assert(stream !== undefined);306 assert(stream._writer === writer);307 const releasedError = new TypeError(308 'Writer was released and can no longer be used to monitor the stream\'s closedness');309 const state = stream._state;310 if (state === 'writable' || state === 'closing') {311 defaultWriterClosedPromiseReject(writer, releasedError);312 } else {313 defaultWriterClosedPromiseResetToRejected(writer, releasedError);314 }315 writer._closedPromise.catch(() => {});316 if (state === 'writable' &&317 WritableStreamDefaultControllerGetBackpressure(stream._writableStreamController) === true) {318 defaultWriterReadyPromiseReject(writer, releasedError);319 } else {320 defaultWriterReadyPromiseResetToRejected(writer, releasedError);321 }322 writer._readyPromise.catch(() => {});323 stream._writer = undefined;324 writer._ownerWritableStream = undefined;325}326function WritableStreamDefaultWriterWrite(writer, chunk) {327 const stream = writer._ownerWritableStream;328 assert(stream !== undefined);329 const state = stream._state;330 if (state === 'closed' || state === 'errored') {331 return Promise.reject(new TypeError(332 `The stream (in ${state} state) is not in the writable state and cannot be written to`));333 }334 assert(state === 'writable');335 const promise = WritableStreamAddWriteRequest(stream);336 WritableStreamDefaultControllerWrite(stream._writableStreamController, chunk);337 return promise;338}339class WritableStreamDefaultController {340 constructor(stream, underlyingSink, size, highWaterMark) {341 if (IsWritableStream(stream) === false) {342 throw new TypeError('WritableStreamDefaultController can only be constructed with a WritableStream instance');343 }344 if (stream._writableStreamController !== undefined) {345 throw new TypeError(346 'WritableStreamDefaultController instances can only be created by the WritableStream constructor');347 }348 this._controlledWritableStream = stream;349 this._underlyingSink = underlyingSink;350 this._queue = [];351 this._started = false;352 this._writing = false;353 const normalizedStrategy = ValidateAndNormalizeQueuingStrategy(size, highWaterMark);354 this._strategySize = normalizedStrategy.size;355 this._strategyHWM = normalizedStrategy.highWaterMark;356 const backpressure = WritableStreamDefaultControllerGetBackpressure(this);357 if (backpressure === true) {358 WritableStreamUpdateBackpressure(stream, backpressure);359 }360 const controller = this;361 const startResult = InvokeOrNoop(underlyingSink, 'start', [this]);362 Promise.resolve(startResult).then(363 () => {364 controller._started = true;365 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);366 },367 r => {368 WritableStreamDefaultControllerErrorIfNeeded(controller, r);369 }370 )371 .catch(rethrowAssertionErrorRejection);372 }373 error(e) {374 if (IsWritableStreamDefaultController(this) === false) {375 throw new TypeError(376 'WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');377 }378 const state = this._controlledWritableStream._state;379 if (state === 'closed' || state === 'errored') {380 throw new TypeError(`The stream is ${state} and so cannot be errored`);381 }382 WritableStreamDefaultControllerError(this, e);383 }384}385// Abstract operations implementing interface required by the WritableStream.386function WritableStreamDefaultControllerAbort(controller, reason) {387 controller._queue = [];388 const sinkAbortPromise = PromiseInvokeOrFallbackOrNoop(controller._underlyingSink, 'abort', [reason],389 'close', [controller]);390 return sinkAbortPromise.then(() => undefined);391}392function WritableStreamDefaultControllerClose(controller) {393 EnqueueValueWithSize(controller._queue, 'close', 0);394 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);395}396function WritableStreamDefaultControllerGetDesiredSize(controller) {397 const queueSize = GetTotalQueueSize(controller._queue);398 return controller._strategyHWM - queueSize;399}400function WritableStreamDefaultControllerWrite(controller, chunk) {401 const stream = controller._controlledWritableStream;402 assert(stream._state === 'writable');403 let chunkSize = 1;404 if (controller._strategySize !== undefined) {405 try {406 chunkSize = controller._strategySize(chunk);407 } catch (chunkSizeE) {408 // TODO: Should we notify the sink of this error?409 WritableStreamDefaultControllerErrorIfNeeded(controller, chunkSizeE);410 return;411 }412 }413 const writeRecord = { chunk };414 const lastBackpressure = WritableStreamDefaultControllerGetBackpressure(controller);415 try {416 EnqueueValueWithSize(controller._queue, writeRecord, chunkSize);417 } catch (enqueueE) {418 WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueE);419 return;420 }421 if (stream._state === 'writable') {422 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);423 if (lastBackpressure !== backpressure) {424 WritableStreamUpdateBackpressure(stream, backpressure);425 }426 }427 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);428}429// Abstract operations for the WritableStreamDefaultController.430function IsWritableStreamDefaultController(x) {431 if (!typeIsObject(x)) {432 return false;433 }434 if (!Object.prototype.hasOwnProperty.call(x, '_underlyingSink')) {435 return false;436 }437 return true;438}439function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {440 if (controller._controlledWritableStream._state === 'closed' ||441 controller._controlledWritableStream._state === 'errored') {442 return;443 }444 if (controller._started === false) {445 return;446 }447 if (controller._writing === true) {448 return;449 }450 if (controller._queue.length === 0) {451 return;452 }453 const writeRecord = PeekQueueValue(controller._queue);454 if (writeRecord === 'close') {455 WritableStreamDefaultControllerProcessClose(controller);456 } else {457 WritableStreamDefaultControllerProcessWrite(controller, writeRecord.chunk);458 }459}460function WritableStreamDefaultControllerErrorIfNeeded(controller, e) {461 if (controller._controlledWritableStream._state === 'writable' ||462 controller._controlledWritableStream._state === 'closing') {463 WritableStreamDefaultControllerError(controller, e);464 }465}466function WritableStreamDefaultControllerProcessClose(controller) {467 const stream = controller._controlledWritableStream;468 assert(stream._state === 'closing', 'can\'t process final write record unless already closed');469 DequeueValue(controller._queue);470 assert(controller._queue.length === 0, 'queue must be empty once the final write record is dequeued');471 const sinkClosePromise = PromiseInvokeOrNoop(controller._underlyingSink, 'close', [controller]);472 sinkClosePromise.then(473 () => {474 if (stream._state !== 'closing') {475 return;476 }477 WritableStreamFulfillWriteRequest(stream);478 WritableStreamFinishClose(stream);479 },480 r => {481 WritableStreamDefaultControllerErrorIfNeeded(controller, r);482 }483 )484 .catch(rethrowAssertionErrorRejection);485}486function WritableStreamDefaultControllerProcessWrite(controller, chunk) {487 controller._writing = true;488 const sinkWritePromise = PromiseInvokeOrNoop(controller._underlyingSink, 'write', [chunk, controller]);489 sinkWritePromise.then(490 () => {491 const stream = controller._controlledWritableStream;492 const state = stream._state;493 if (state === 'errored' || state === 'closed') {494 return;495 }496 controller._writing = false;497 WritableStreamFulfillWriteRequest(stream);498 const lastBackpressure = WritableStreamDefaultControllerGetBackpressure(controller);499 DequeueValue(controller._queue);500 if (state !== 'closing') {501 const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);502 if (lastBackpressure !== backpressure) {503 WritableStreamUpdateBackpressure(controller._controlledWritableStream, backpressure);504 }505 }506 WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);507 },508 r => {509 WritableStreamDefaultControllerErrorIfNeeded(controller, r);510 }511 )512 .catch(rethrowAssertionErrorRejection);513}514function WritableStreamDefaultControllerGetBackpressure(controller) {515 const desiredSize = WritableStreamDefaultControllerGetDesiredSize(controller);516 return desiredSize <= 0;517}518// A client of WritableStreamDefaultController may use these functions directly to bypass state check.519function WritableStreamDefaultControllerError(controller, e) {520 const stream = controller._controlledWritableStream;521 assert(stream._state === 'writable' || stream._state === 'closing');522 WritableStreamError(stream, e);523 controller._queue = [];524}525// Helper functions for the WritableStream.526function streamBrandCheckException(name) {527 return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);528}...
Using AI Code Generation
1'use strict';2const { WritableStream, CountQueuingStrategy } = require('stream/web');3const ws = new WritableStream({4 write() {}5}, new CountQueuingStrategy({ highWaterMark: 0 }));6const writer = ws.getWriter();7const backpressure = writer.desiredSize;8console.log(backpressure);
Using AI Code Generation
1'use strict';2const { WritableStream } = require('stream/web');3const { WritableStreamDefaultControllerGetBackpressure } = require('internal/webstreams/writablestream');4const assert = require('assert');5const ws = new WritableStream({6 write() {}7});8const writer = ws.getWriter();9const controller = writer[kState].controller;10assert.strictEqual(WritableStreamDefaultControllerGetBackpressure(controller), false);11writer.close();12'use strict';13const { WritableStream } = require('stream/web');14const { WritableStreamDefaultControllerGetBackpressure } = require('internal/webstreams/writablestream');15const assert = require('assert');16const ws = new WritableStream({17 write() {}18});19const writer = ws.getWriter();20const controller = writer[kState].controller;21assert.strictEqual(WritableStreamDefaultControllerGetBackpressure(controller), false);22writer.close();23'use strict';24const { WritableStream } = require('stream/web');25const { WritableStreamDefaultControllerGetBackpressure } = require('internal/webstreams/writablestream');26const assert = require('assert');27const ws = new WritableStream({28 write() {}29});30const writer = ws.getWriter();31const controller = writer[kState].controller;32assert.strictEqual(WritableStreamDefaultControllerGetBackpressure(controller), false);33writer.close();34'use strict';35const { WritableStream } = require('stream/web');36const { WritableStreamDefaultControllerGetBackpressure } = require('internal/webstreams/writablestream');37const assert = require('assert');38const ws = new WritableStream({39 write() {}40});41const writer = ws.getWriter();42const controller = writer[kState].controller;43assert.strictEqual(WritableStreamDefaultControllerGetBackpressure(controller), false);44writer.close();45'use strict';46const { WritableStream } = require('stream/web');47const { W
Using AI Code Generation
1const assert = require('assert');2const {3} = require('stream/web');4const ws = new WritableStream({5 write() { }6});7const writer = new WritableStreamDefaultWriter(ws);8const controller = writer[kState].controller;9assert.strictEqual(controller[kState].backpressure, false);10assert.strictEqual(11 WritableStreamDefaultControllerGetBackpressure(controller),12 false);13controller[kState].backpressure = true;14assert.strictEqual(controller[kState].backpressure, true);15assert.strictEqual(16 WritableStreamDefaultControllerGetBackpressure(controller),17 true);18assert.strictEqual(19 WritableStreamDefaultControllerGetBackpressure({}),20 undefined);21assert.throws(() => WritableStreamDefaultControllerGetBackpressure(), {22});
Using AI Code Generation
1'use strict';2const assert = require('assert');3const { WritableStream, CountQueuingStrategy } = require('stream/web');4const ws = new WritableStream({5 write() {6 return new Promise((resolve) => setTimeout(resolve, 100));7 }8}, new CountQueuingStrategy({ highWaterMark: 1 }));9const writer = ws.getWriter();10writer.write('a');11writer.write('b');12writer.write('c');13setTimeout(() => {14 assert.strictEqual(writer.desiredSize, 0);15 assert.strictEqual(writer.ready, null);16 writer.releaseLock();17}, 200);
Using AI Code Generation
1'use strict';2const { WritableStream, WritableStreamDefaultControllerGetBackpressure } = require('stream/web');3const ws = new WritableStream({4 write() {}5});6const writer = ws.getWriter();7const controller = writer[kState].controller;8const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);9console.log(backpressure);10{11 "dependencies": {12 }13}14{15 "packages": {16 "": {17 "dependencies": {18 }19 },20 "node": {21 }22 }23}24{25 "bugs": {
Using AI Code Generation
1'use strict';2const { WritableStream, WritableStreamDefaultControllerGetBackpressure } = require('stream/web');3const { ReadableStream, CountQueuingStrategy } = require('stream/web');4const { Writable, Readable } = require('stream');5const ws = new WritableStream({6 write() {}7});8const ws1 = new WritableStream({9 write() {}10}, new CountQueuingStrategy({ highWaterMark: 0 }));11const ws2 = new WritableStream({12 write() {}13});14const ws3 = new WritableStream({15 write() {}16}, new CountQueuingStrategy({ highWaterMark: 0 }));17const rs = new ReadableStream({18 start(controller) {19 controller.enqueue('a');20 controller.enqueue('b');21 controller.enqueue('c');22 }23});24const writer = ws.getWriter();25const writer1 = ws1.getWriter();26const writer2 = ws2.getWriter();27const writer3 = ws3.getWriter();28const reader = rs.getReader();29async function writeAll() {30 for (let result = await reader.read(); !result.done; result = await reader.read()) {31 writer.write(result.value);32 writer1.write(result.value);33 writer2.write(result.value);34 writer3.write(result.value);35 }36 await writer.close();37 await writer1.close();38 await writer2.close();39 await writer3.close();40}41writeAll().then(() => {42 console.log(WritableStreamDefaultControllerGetBackpressure(writer._writableStreamController));43 console.log(WritableStreamDefaultControllerGetBackpressure(writer1._writableStreamController));44 console.log(WritableStreamDefaultControllerGetBackpressure(writer2._writableStreamController));45 console.log(WritableStreamDefaultControllerGetBackpressure(writer3._writableStreamController));46});
Using AI Code Generation
1'use strict';2const { WritableStream, CountQueuingStrategy } = require('stream/web');3const ws = new WritableStream({4 write() {},5}, new CountQueuingStrategy({ highWaterMark: 1 }));6const writer = ws.getWriter();7assert_equals(writer.desiredSize, 0, 'desiredSize should be 0');8writer.write('a');9assert_equals(writer.desiredSize, -1, 'desiredSize should be -1');10writer.write('b');11assert_equals(writer.desiredSize, -2, 'desiredSize should be -2');12writer.releaseLock();13const writer2 = ws.getWriter();14assert_equals(writer2.desiredSize, 0, 'desiredSize should be 0');15'use strict';16const assert = require('assert');17const {18} = require('./abstract-ops/queue-with-sizes');19const {20} = require('./abstract-ops/algorithm');21const {22} = require('./abstract-ops/writable-streams');23const {24} = require('./abstract-ops/writable-stream-abstract-ops');25const {26} = require('./abstract-ops/queuing-strategy');27const {
Using AI Code Generation
1var WritableStream = require('stream').Writable;2var assert = require('assert');3const ws = new WritableStream({4 write(chunk, controller) {5 controller.getBackpressure();6 }7});8assert.throws(() => ws.write('a'), TypeError);9assert.throws(() => ws.write('a'), TypeError)10at Object.<anonymous> (/home/ashish/Desktop/Node.js/Streams/test.js:12:8)11at Module._compile (internal/modules/cjs/loader.js:1158:30)12at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)13at Module.load (internal/modules/cjs/loader.js:1002:32)14at Function.Module._load (internal/modules/cjs/loader.js:901:14)15at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
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!!