How to use ReadableStreamDefaultControllerError method in wpt

Best JavaScript code snippet using wpt

ReadableStream.js

Source:ReadableStream.js Github

copy

Full Screen

...184 ReadableStreamDefaultControllerCallPullIfNeeded(controller);185 },186 r => {187 if (ReadableStreamGetState(stream) === STATE_READABLE) {188 ReadableStreamDefaultControllerError(controller, r);189 }190 });191 }192 get desiredSize() {193 if (IsReadableStreamDefaultController(this) === false) {194 throw new TypeError(errIllegalInvocation);195 }196 return ReadableStreamDefaultControllerGetDesiredSize(this);197 }198 close() {199 if (IsReadableStreamDefaultController(this) === false) {200 throw new TypeError(errIllegalInvocation);201 }202 const stream = this[_controlledReadableStream];203 if (this[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) {204 throw new TypeError(errCloseCloseRequestedStream);205 }206 const state = ReadableStreamGetState(stream);207 if (state === STATE_ERRORED) {208 throw new TypeError(errCloseErroredStream);209 }210 if (state === STATE_CLOSED) {211 throw new TypeError(errCloseClosedStream);212 }213 return ReadableStreamDefaultControllerClose(this);214 }215 enqueue(chunk) {216 if (IsReadableStreamDefaultController(this) === false) {217 throw new TypeError(errIllegalInvocation);218 }219 const stream = this[_controlledReadableStream];220 if (this[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) {221 throw new TypeError(errEnqueueCloseRequestedStream);222 }223 const state = ReadableStreamGetState(stream);224 if (state === STATE_ERRORED) {225 throw new TypeError(errEnqueueErroredStream);226 }227 if (state === STATE_CLOSED) {228 throw new TypeError(errEnqueueClosedStream);229 }230 return ReadableStreamDefaultControllerEnqueue(this, chunk);231 }232 error(e) {233 if (IsReadableStreamDefaultController(this) === false) {234 throw new TypeError(errIllegalInvocation);235 }236 const stream = this[_controlledReadableStream];237 const state = ReadableStreamGetState(stream);238 if (state === STATE_ERRORED) {239 throw new TypeError(errErrorErroredStream);240 }241 if (state === STATE_CLOSED) {242 throw new TypeError(errErrorClosedStream);243 }244 return ReadableStreamDefaultControllerError(this, e);245 }246 }247 function ReadableStreamDefaultControllerCancel(controller, reason) {248 controller[_queue] = new v8.InternalPackedArray();249 const underlyingSource = controller[_underlyingSource];250 return PromiseCallOrNoop(underlyingSource, 'cancel', reason, 'underlyingSource.cancel');251 }252 function ReadableStreamDefaultControllerPull(controller) {253 const stream = controller[_controlledReadableStream];254 if (controller[_queue].length > 0) {255 const chunk = DequeueValue(controller);256 if ((controller[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) &&257 controller[_queue].length === 0) {258 ReadableStreamClose(stream);259 } else {260 ReadableStreamDefaultControllerCallPullIfNeeded(controller);261 }262 return Promise_resolve(CreateIterResultObject(chunk, false));263 }264 const pendingPromise = ReadableStreamAddReadRequest(stream);265 ReadableStreamDefaultControllerCallPullIfNeeded(controller);266 return pendingPromise;267 }268 function ReadableStreamAddReadRequest(stream) {269 const promise = v8.createPromise();270 stream[_reader][_readRequests].push(promise);271 return promise;272 }273 class ReadableStreamDefaultReader {274 constructor(stream) {275 if (IsReadableStream(stream) === false) {276 throw new TypeError(errReaderConstructorBadArgument);277 }278 if (IsReadableStreamLocked(stream) === true) {279 throw new TypeError(errReaderConstructorStreamAlreadyLocked);280 }281 ReadableStreamReaderGenericInitialize(this, stream);282 this[_readRequests] = new v8.InternalPackedArray();283 }284 get closed() {285 if (IsReadableStreamDefaultReader(this) === false) {286 return Promise_reject(new TypeError(errIllegalInvocation));287 }288 return this[_closedPromise];289 }290 cancel(reason) {291 if (IsReadableStreamDefaultReader(this) === false) {292 return Promise_reject(new TypeError(errIllegalInvocation));293 }294 const stream = this[_ownerReadableStream];295 if (stream === undefined) {296 return Promise_reject(new TypeError(errCancelReleasedReader));297 }298 return ReadableStreamReaderGenericCancel(this, reason);299 }300 read() {301 if (IsReadableStreamDefaultReader(this) === false) {302 return Promise_reject(new TypeError(errIllegalInvocation));303 }304 if (this[_ownerReadableStream] === undefined) {305 return Promise_reject(new TypeError(errReadReleasedReader));306 }307 return ReadableStreamDefaultReaderRead(this);308 }309 releaseLock() {310 if (IsReadableStreamDefaultReader(this) === false) {311 throw new TypeError(errIllegalInvocation);312 }313 const stream = this[_ownerReadableStream];314 if (stream === undefined) {315 return undefined;316 }317 if (this[_readRequests].length > 0) {318 throw new TypeError(errReleaseReaderWithPendingRead);319 }320 ReadableStreamReaderGenericRelease(this);321 }322 }323 function ReadableStreamReaderGenericCancel(reader, reason) {324 return ReadableStreamCancel(reader[_ownerReadableStream], reason);325 }326 //327 // Readable stream abstract operations328 //329 function AcquireReadableStreamDefaultReader(stream) {330 return new ReadableStreamDefaultReader(stream);331 }332 function ReadableStreamCancel(stream, reason) {333 stream[_readableStreamBits] |= DISTURBED;334 const state = ReadableStreamGetState(stream);335 if (state === STATE_CLOSED) {336 return Promise_resolve(undefined);337 }338 if (state === STATE_ERRORED) {339 return Promise_reject(stream[_storedError]);340 }341 ReadableStreamClose(stream);342 const sourceCancelPromise = ReadableStreamDefaultControllerCancel(stream[_controller], reason);343 return thenPromise(sourceCancelPromise, () => undefined);344 }345 function ReadableStreamDefaultControllerClose(controller) {346 const stream = controller[_controlledReadableStream];347 controller[_readableStreamDefaultControllerBits] |= CLOSE_REQUESTED;348 if (controller[_queue].length === 0) {349 ReadableStreamClose(stream);350 }351 }352 function ReadableStreamFulfillReadRequest(stream, chunk, done) {353 const reader = stream[_reader];354 const readRequest = stream[_reader][_readRequests].shift();355 v8.resolvePromise(readRequest, CreateIterResultObject(chunk, done));356 }357 function ReadableStreamDefaultControllerEnqueue(controller, chunk) {358 const stream = controller[_controlledReadableStream];359 if (IsReadableStreamLocked(stream) === true && ReadableStreamGetNumReadRequests(stream) > 0) {360 ReadableStreamFulfillReadRequest(stream, chunk, false);361 } else {362 let chunkSize = 1;363 const strategySize = controller[_strategySize];364 if (strategySize !== undefined) {365 try {366 chunkSize = strategySize(chunk);367 } catch (chunkSizeE) {368 if (ReadableStreamGetState(stream) === STATE_READABLE) {369 ReadableStreamDefaultControllerError(controller, chunkSizeE);370 }371 throw chunkSizeE;372 }373 }374 try {375 EnqueueValueWithSize(controller, chunk, chunkSize);376 } catch (enqueueE) {377 if (ReadableStreamGetState(stream) === STATE_READABLE) {378 ReadableStreamDefaultControllerError(controller, enqueueE);379 }380 throw enqueueE;381 }382 }383 ReadableStreamDefaultControllerCallPullIfNeeded(controller);384 }385 function ReadableStreamGetState(stream) {386 return (stream[_readableStreamBits] & STATE_MASK) >> STATE_BITS_OFFSET;387 }388 function ReadableStreamSetState(stream, state) {389 stream[_readableStreamBits] = (stream[_readableStreamBits] & ~STATE_MASK) |390 (state << STATE_BITS_OFFSET);391 }392 function ReadableStreamDefaultControllerError(controller, e) {393 controller[_queue] = new v8.InternalPackedArray();394 const stream = controller[_controlledReadableStream];395 ReadableStreamError(stream, e);396 }397 function ReadableStreamError(stream, e) {398 stream[_storedError] = e;399 ReadableStreamSetState(stream, STATE_ERRORED);400 const reader = stream[_reader];401 if (reader === undefined) {402 return undefined;403 }404 if (IsReadableStreamDefaultReader(reader) === true) {405 const readRequests = reader[_readRequests];406 for (let i = 0; i < readRequests.length; i++) {407 v8.rejectPromise(readRequests[i], e);408 }409 reader[_readRequests] = new v8.InternalPackedArray();410 }411 v8.rejectPromise(reader[_closedPromise], e);412 }413 function ReadableStreamClose(stream) {414 ReadableStreamSetState(stream, STATE_CLOSED);415 const reader = stream[_reader];416 if (reader === undefined) {417 return undefined;418 }419 if (IsReadableStreamDefaultReader(reader) === true) {420 const readRequests = reader[_readRequests];421 for (let i = 0; i < readRequests.length; i++) {422 v8.resolvePromise(423 readRequests[i], CreateIterResultObject(undefined, true));424 }425 reader[_readRequests] = new v8.InternalPackedArray();426 }427 v8.resolvePromise(reader[_closedPromise], undefined);428 }429 function ReadableStreamDefaultControllerGetDesiredSize(controller) {430 const queueSize = GetTotalQueueSize(controller);431 return controller[_strategyHWM] - queueSize;432 }433 function IsReadableStream(x) {434 return hasOwnProperty(x, _controller);435 }436 function IsReadableStreamDisturbed(stream) {437 return stream[_readableStreamBits] & DISTURBED;438 }439 function IsReadableStreamLocked(stream) {440 return stream[_reader] !== undefined;441 }442 function IsReadableStreamDefaultController(x) {443 return hasOwnProperty(x, _controlledReadableStream);444 }445 function IsReadableStreamDefaultReader(x) {446 return hasOwnProperty(x, _readRequests);447 }448 function IsReadableStreamReadable(stream) {449 return ReadableStreamGetState(stream) === STATE_READABLE;450 }451 function IsReadableStreamClosed(stream) {452 return ReadableStreamGetState(stream) === STATE_CLOSED;453 }454 function IsReadableStreamErrored(stream) {455 return ReadableStreamGetState(stream) === STATE_ERRORED;456 }457 function ReadableStreamReaderGenericInitialize(reader, stream) {458 // TODO(yhirano): Remove this when we don't need hasPendingActivity in459 // blink::UnderlyingSourceBase.460 const controller = stream[_controller];461 if (controller[_readableStreamDefaultControllerBits] & EXTERNALLY_CONTROLLED) {462 // The stream is created with an external controller (i.e. made in463 // Blink).464 const underlyingSource = controller[_underlyingSource];465 callFunction(underlyingSource.notifyLockAcquired, underlyingSource);466 }467 reader[_ownerReadableStream] = stream;468 stream[_reader] = reader;469 switch (ReadableStreamGetState(stream)) {470 case STATE_READABLE:471 reader[_closedPromise] = v8.createPromise();472 break;473 case STATE_CLOSED:474 reader[_closedPromise] = Promise_resolve(undefined);475 break;476 case STATE_ERRORED:477 reader[_closedPromise] = Promise_reject(stream[_storedError]);478 break;479 }480 }481 function ReadableStreamReaderGenericRelease(reader) {482 // TODO(yhirano): Remove this when we don't need hasPendingActivity in483 // blink::UnderlyingSourceBase.484 const controller = reader[_ownerReadableStream][_controller];485 if (controller[_readableStreamDefaultControllerBits] & EXTERNALLY_CONTROLLED) {486 // The stream is created with an external controller (i.e. made in487 // Blink).488 const underlyingSource = controller[_underlyingSource];489 callFunction(underlyingSource.notifyLockReleased, underlyingSource);490 }491 if (ReadableStreamGetState(reader[_ownerReadableStream]) === STATE_READABLE) {492 v8.rejectPromise(reader[_closedPromise], new TypeError(errReleasedReaderClosedPromise));493 } else {494 reader[_closedPromise] = Promise_reject(new TypeError(errReleasedReaderClosedPromise));495 }496 reader[_ownerReadableStream][_reader] = undefined;497 reader[_ownerReadableStream] = undefined;498 }499 function ReadableStreamDefaultReaderRead(reader) {500 const stream = reader[_ownerReadableStream];501 stream[_readableStreamBits] |= DISTURBED;502 if (ReadableStreamGetState(stream) === STATE_CLOSED) {503 return Promise_resolve(CreateIterResultObject(undefined, true));504 }505 if (ReadableStreamGetState(stream) === STATE_ERRORED) {506 return Promise_reject(stream[_storedError]);507 }508 return ReadableStreamDefaultControllerPull(stream[_controller]);509 }510 function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {511 const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);512 if (shouldPull === false) {513 return undefined;514 }515 if (controller[_readableStreamDefaultControllerBits] & PULLING) {516 controller[_readableStreamDefaultControllerBits] |= PULL_AGAIN;517 return undefined;518 }519 controller[_readableStreamDefaultControllerBits] |= PULLING;520 const underlyingSource = controller[_underlyingSource];521 const pullPromise = PromiseCallOrNoop(522 underlyingSource, 'pull', controller, 'underlyingSource.pull');523 thenPromise(pullPromise,524 () => {525 controller[_readableStreamDefaultControllerBits] &= ~PULLING;526 if (controller[_readableStreamDefaultControllerBits] & PULL_AGAIN) {527 controller[_readableStreamDefaultControllerBits] &= ~PULL_AGAIN;528 ReadableStreamDefaultControllerCallPullIfNeeded(controller);529 }530 },531 e => {532 if (ReadableStreamGetState(controller[_controlledReadableStream]) === STATE_READABLE) {533 ReadableStreamDefaultControllerError(controller, e);534 }535 });536 }537 function ReadableStreamDefaultControllerShouldCallPull(controller) {538 const stream = controller[_controlledReadableStream];539 const state = ReadableStreamGetState(stream);540 if (state === STATE_CLOSED || state === STATE_ERRORED) {541 return false;542 }543 if (controller[_readableStreamDefaultControllerBits] & CLOSE_REQUESTED) {544 return false;545 }546 if (!(controller[_readableStreamDefaultControllerBits] & STARTED)) {547 return false;548 }549 if (IsReadableStreamLocked(stream) === true && ReadableStreamGetNumReadRequests(stream) > 0) {550 return true;551 }552 const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);553 if (desiredSize > 0) {554 return true;555 }556 return false;557 }558 function ReadableStreamGetNumReadRequests(stream) {559 const reader = stream[_reader];560 const readRequests = reader[_readRequests];561 return readRequests.length;562 }563 // Potential future optimization: use class instances for the underlying564 // sources, so that we don't re-create565 // closures every time.566 // TODO(domenic): shouldClone argument from spec not supported yet567 function ReadableStreamTee(stream) {568 const reader = AcquireReadableStreamDefaultReader(stream);569 let closedOrErrored = false;570 let canceled1 = false;571 let canceled2 = false;572 let reason1;573 let reason2;574 let promise = v8.createPromise();575 const branch1Stream = new ReadableStream({pull, cancel: cancel1});576 const branch2Stream = new ReadableStream({pull, cancel: cancel2});577 const branch1 = branch1Stream[_controller];578 const branch2 = branch2Stream[_controller];579 thenPromise(580 reader[_closedPromise], undefined, function(r) {581 if (closedOrErrored === true) {582 return;583 }584 ReadableStreamDefaultControllerError(branch1, r);585 ReadableStreamDefaultControllerError(branch2, r);586 closedOrErrored = true;587 });588 return [branch1Stream, branch2Stream];589 function pull() {590 return thenPromise(591 ReadableStreamDefaultReaderRead(reader), function(result) {592 const value = result.value;593 const done = result.done;594 if (done === true && closedOrErrored === false) {595 if (canceled1 === false) {596 ReadableStreamDefaultControllerClose(branch1);597 }598 if (canceled2 === false) {599 ReadableStreamDefaultControllerClose(branch2);...

Full Screen

Full Screen

default-controller.ts

Source:default-controller.ts Github

copy

Full Screen

...86 error(e: any = undefined): void {87 if (!IsReadableStreamDefaultController(this)) {88 throw defaultControllerBrandCheckException('error');89 }90 ReadableStreamDefaultControllerError(this, e);91 }92 /** @internal */93 [CancelSteps](reason: any): Promise<void> {94 ResetQueue(this);95 const result = this._cancelAlgorithm(reason);96 ReadableStreamDefaultControllerClearAlgorithms(this);97 return result;98 }99 /** @internal */100 [PullSteps](readRequest: ReadRequest<R>): void {101 const stream = this._controlledReadableStream;102 if (this._queue.length > 0) {103 const chunk = DequeueValue(this);104 if (this._closeRequested && this._queue.length === 0) {105 ReadableStreamDefaultControllerClearAlgorithms(this);106 ReadableStreamClose(stream);107 } else {108 ReadableStreamDefaultControllerCallPullIfNeeded(this);109 }110 readRequest._chunkSteps(chunk);111 } else {112 ReadableStreamAddReadRequest(stream, readRequest);113 ReadableStreamDefaultControllerCallPullIfNeeded(this);114 }115 }116}117Object.defineProperties(ReadableStreamDefaultController.prototype, {118 close: { enumerable: true },119 enqueue: { enumerable: true },120 error: { enumerable: true },121 desiredSize: { enumerable: true }122});123if (typeof Symbol.toStringTag === 'symbol') {124 Object.defineProperty(ReadableStreamDefaultController.prototype, Symbol.toStringTag, {125 value: 'ReadableStreamDefaultController',126 configurable: true127 });128}129// Abstract operations for the ReadableStreamDefaultController.130function IsReadableStreamDefaultController<R = any>(x: any): x is ReadableStreamDefaultController<R> {131 if (!typeIsObject(x)) {132 return false;133 }134 if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {135 return false;136 }137 return true;138}139function ReadableStreamDefaultControllerCallPullIfNeeded(controller: ReadableStreamDefaultController<any>): void {140 const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);141 if (!shouldPull) {142 return;143 }144 if (controller._pulling) {145 controller._pullAgain = true;146 return;147 }148 assert(!controller._pullAgain);149 controller._pulling = true;150 const pullPromise = controller._pullAlgorithm();151 uponPromise(152 pullPromise,153 () => {154 controller._pulling = false;155 if (controller._pullAgain) {156 controller._pullAgain = false;157 ReadableStreamDefaultControllerCallPullIfNeeded(controller);158 }159 },160 e => {161 ReadableStreamDefaultControllerError(controller, e);162 }163 );164}165function ReadableStreamDefaultControllerShouldCallPull(controller: ReadableStreamDefaultController<any>): boolean {166 const stream = controller._controlledReadableStream;167 if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {168 return false;169 }170 if (!controller._started) {171 return false;172 }173 if (IsReadableStreamLocked(stream) && ReadableStreamGetNumReadRequests(stream) > 0) {174 return true;175 }176 const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);177 assert(desiredSize !== null);178 if (desiredSize! > 0) {179 return true;180 }181 return false;182}183function ReadableStreamDefaultControllerClearAlgorithms(controller: ReadableStreamDefaultController<any>) {184 controller._pullAlgorithm = undefined!;185 controller._cancelAlgorithm = undefined!;186 controller._strategySizeAlgorithm = undefined!;187}188// A client of ReadableStreamDefaultController may use these functions directly to bypass state check.189export function ReadableStreamDefaultControllerClose(controller: ReadableStreamDefaultController<any>) {190 if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {191 return;192 }193 const stream = controller._controlledReadableStream;194 controller._closeRequested = true;195 if (controller._queue.length === 0) {196 ReadableStreamDefaultControllerClearAlgorithms(controller);197 ReadableStreamClose(stream);198 }199}200export function ReadableStreamDefaultControllerEnqueue<R>(controller: ReadableStreamDefaultController<R>, chunk: R): void {201 if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {202 return;203 }204 const stream = controller._controlledReadableStream;205 if (IsReadableStreamLocked(stream) && ReadableStreamGetNumReadRequests(stream) > 0) {206 ReadableStreamFulfillReadRequest(stream, chunk, false);207 } else {208 let chunkSize;209 try {210 chunkSize = controller._strategySizeAlgorithm(chunk);211 } catch (chunkSizeE) {212 ReadableStreamDefaultControllerError(controller, chunkSizeE);213 throw chunkSizeE;214 }215 try {216 EnqueueValueWithSize(controller, chunk, chunkSize);217 } catch (enqueueE) {218 ReadableStreamDefaultControllerError(controller, enqueueE);219 throw enqueueE;220 }221 }222 ReadableStreamDefaultControllerCallPullIfNeeded(controller);223}224export function ReadableStreamDefaultControllerError(controller: ReadableStreamDefaultController<any>, e: any) {225 const stream = controller._controlledReadableStream;226 if (stream._state !== 'readable') {227 return;228 }229 ResetQueue(controller);230 ReadableStreamDefaultControllerClearAlgorithms(controller);231 ReadableStreamError(stream, e);232}233export function ReadableStreamDefaultControllerGetDesiredSize(controller: ReadableStreamDefaultController<any>): number | null {234 const stream = controller._controlledReadableStream;235 const state = stream._state;236 if (state === 'errored') {237 return null;238 }239 if (state === 'closed') {240 return 0;241 }242 return controller._strategyHWM - controller._queueTotalSize;243}244// This is used in the implementation of TransformStream.245export function ReadableStreamDefaultControllerHasBackpressure(controller: ReadableStreamDefaultController<any>): boolean {246 if (ReadableStreamDefaultControllerShouldCallPull(controller)) {247 return false;248 }249 return true;250}251export function ReadableStreamDefaultControllerCanCloseOrEnqueue(controller: ReadableStreamDefaultController<any>): boolean {252 const state = controller._controlledReadableStream._state;253 if (!controller._closeRequested && state === 'readable') {254 return true;255 }256 return false;257}258export function SetUpReadableStreamDefaultController<R>(stream: ReadableStream<R>,259 controller: ReadableStreamDefaultController<R>,260 startAlgorithm: () => void | PromiseLike<void>,261 pullAlgorithm: () => Promise<void>,262 cancelAlgorithm: (reason: any) => Promise<void>,263 highWaterMark: number,264 sizeAlgorithm: QueuingStrategySizeCallback<R>) {265 assert(stream._readableStreamController === undefined);266 controller._controlledReadableStream = stream;267 controller._queue = undefined!;268 controller._queueTotalSize = undefined!;269 ResetQueue(controller);270 controller._started = false;271 controller._closeRequested = false;272 controller._pullAgain = false;273 controller._pulling = false;274 controller._strategySizeAlgorithm = sizeAlgorithm;275 controller._strategyHWM = highWaterMark;276 controller._pullAlgorithm = pullAlgorithm;277 controller._cancelAlgorithm = cancelAlgorithm;278 stream._readableStreamController = controller;279 const startResult = startAlgorithm();280 uponPromise(281 promiseResolvedWith(startResult),282 () => {283 controller._started = true;284 assert(!controller._pulling);285 assert(!controller._pullAgain);286 ReadableStreamDefaultControllerCallPullIfNeeded(controller);287 },288 r => {289 ReadableStreamDefaultControllerError(controller, r);290 }291 );292}293export function SetUpReadableStreamDefaultControllerFromUnderlyingSource<R>(294 stream: ReadableStream<R>,295 underlyingSource: ValidatedUnderlyingSource<R>,296 highWaterMark: number,297 sizeAlgorithm: QueuingStrategySizeCallback<R>298) {299 const controller: ReadableStreamDefaultController<R> = Object.create(ReadableStreamDefaultController.prototype);300 let startAlgorithm: () => void | PromiseLike<void> = () => undefined;301 let pullAlgorithm: () => Promise<void> = () => promiseResolvedWith(undefined);302 let cancelAlgorithm: (reason: any) => Promise<void> = () => promiseResolvedWith(undefined);303 if (underlyingSource.start !== undefined) {...

Full Screen

Full Screen

readable_stream_controller.ts

Source:readable_stream_controller.ts Github

copy

Full Screen

...92 error(e): void {93 if (!IsReadableStreamDefaultController(this)) {94 throw new TypeError();95 }96 ReadableStreamDefaultControllerError(this, e);97 }98 CancelSteps(reason): Promise<any> {99 ResetQueue(this);100 const result = this.cancelAlgorithm(reason);101 ReadableStreamDefaultControllerClearAlgorithms(this);102 return result;103 }104 PullSteps(forAuthorCode?: boolean): Promise<any> {105 const stream = this.controlledReadableStream;106 if (this.queue.length > 0) {107 const chunk = DequeueValue(this);108 if (this.closeRequested && this.queue.length === 0) {109 ReadableStreamDefaultControllerClearAlgorithms(this);110 ReadableStreamClose(stream);111 } else {112 ReadableStreamDefaultControllerCallPullIfNeeded(this);113 }114 return Promise.resolve(115 ReadableStreamCreateReadResult(chunk, false, forAuthorCode)116 );117 }118 const pendingPromise = ReadableStreamAddReadRequest(stream, forAuthorCode);119 ReadableStreamDefaultControllerCallPullIfNeeded(this);120 return pendingPromise;121 }122}123export function IsReadableStreamDefaultController<T>(124 x125): x is ReadableStreamDefaultController<T> {126 return typeof x === "object" && x.hasOwnProperty("controlledReadableStream");127}128export function ReadableStreamDefaultControllerCallPullIfNeeded<T>(129 controller: ReadableStreamDefaultController<T>130) {131 const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);132 if (!shouldPull) {133 return;134 }135 if (controller.pulling) {136 controller.pullAgain = true;137 return;138 }139 Assert(!controller.pullAgain);140 controller.pulling = true;141 controller142 .pullAlgorithm()143 .then(() => {144 controller.pulling = false;145 if (controller.pullAgain) {146 controller.pullAgain = false;147 ReadableStreamDefaultControllerCallPullIfNeeded(controller);148 }149 })150 .catch(r => {151 ReadableStreamDefaultControllerError(controller, r);152 });153}154export function ReadableStreamDefaultControllerShouldCallPull<T>(155 controller: ReadableStreamDefaultController<T>156) {157 const stream = controller.controlledReadableStream;158 if (!ReadableStreamDefaultControllerCanCloseOrEnqueue(controller)) {159 return false;160 }161 if (!controller.started) {162 return false;163 }164 if (165 IsReadableStreamLocked(stream) &&166 ReadableStreamGetNumReadRequests(stream) > 0167 ) {168 return true;169 }170 const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);171 Assert(desiredSize !== null);172 return desiredSize > 0;173}174export function ReadableStreamDefaultControllerClearAlgorithms<T>(175 controller: ReadableStreamDefaultController<T>176) {177 controller.pullAlgorithm = void 0;178 controller.cancelAlgorithm = void 0;179 controller.strategySizeAlgorithm = void 0;180}181export function ReadableStreamDefaultControllerClose<T>(controller) {182 const stream = controller.controlledReadableStream;183 Assert(ReadableStreamDefaultControllerCanCloseOrEnqueue(controller));184 controller.closeRequested = true;185 if (controller.queue.length === 0) {186 ReadableStreamDefaultControllerClearAlgorithms(controller);187 ReadableStreamClose(stream);188 }189}190export function ReadableStreamDefaultControllerEnqueue<T>(controller, chunk) {191 if (IsReadableStreamDefaultController(controller)) {192 const stream = controller.controlledReadableStream;193 Assert(ReadableStreamDefaultControllerCanCloseOrEnqueue(controller));194 if (195 IsReadableStreamLocked(stream) &&196 ReadableStreamGetNumReadRequests(stream) > 0197 ) {198 ReadableStreamFulfillReadRequest(stream, chunk, false);199 } else {200 let result: number;201 try {202 result = controller.strategySizeAlgorithm(chunk);203 } catch (e) {204 ReadableStreamDefaultControllerError(controller, e);205 return e;206 }207 const chunkSize = result;208 try {209 EnqueueValueWithSize(controller, chunk, chunkSize);210 } catch (e) {211 ReadableStreamDefaultControllerError(controller, e);212 return e;213 }214 ReadableStreamDefaultControllerCallPullIfNeeded(controller);215 }216 }217}218export function ReadableStreamDefaultControllerError<T>(controller, e) {219 if (IsReadableStreamDefaultController(controller)) {220 const stream = controller.controlledReadableStream;221 if (stream.state !== "readable") {222 return;223 }224 ResetQueue(controller);225 ReadableStreamDefaultControllerClearAlgorithms(controller);226 ReadableStreamError(stream, e);227 }228}229export function ReadableStreamDefaultControllerGetDesiredSize<T>(230 controller: ReadableStreamDefaultController<T>231): number | null {232 const stream = controller.controlledReadableStream;233 const state = stream.state;234 if (state === "errored") {235 return null;236 }237 if (state === "closed") {238 return 0;239 }240 return controller.strategyHWM - controller.queueTotalSize;241}242export function ReadableStreamDefaultControllerHasBackpressure<T>(243 controller: ReadableStreamDefaultController<T>244): boolean {245 return !ReadableStreamDefaultControllerShouldCallPull(controller);246}247export function ReadableStreamDefaultControllerCanCloseOrEnqueue<T>(248 controller: ReadableStreamDefaultController<T>249): boolean {250 const state = controller.controlledReadableStream.state;251 return !controller.closeRequested && state === "readable";252}253export function SetUpReadableStreamDefaultController<T>(params: {254 stream: ReadableStream;255 controller: ReadableStreamDefaultController<T>;256 startAlgorithm: StartAlgorithm;257 pullAlgorithm: PullAlgorithm;258 cancelAlgorithm: CancelAlgorithm;259 highWaterMark: number;260 sizeAlgorithm: SizeAlgorithm;261}) {262 const {263 stream,264 controller,265 startAlgorithm,266 pullAlgorithm,267 cancelAlgorithm268 } = params;269 let { highWaterMark, sizeAlgorithm } = params;270 Assert(stream.readableStreamController === void 0);271 controller.controlledReadableStream = stream;272 controller.queue = void 0;273 controller.queueTotalSize = void 0;274 ResetQueue(controller);275 controller.started = false;276 controller.closeRequested = false;277 controller.pullAgain = false;278 controller.pulling = false;279 controller.strategySizeAlgorithm = sizeAlgorithm;280 controller.strategyHWM = highWaterMark;281 controller.pullAlgorithm = pullAlgorithm;282 controller.cancelAlgorithm = cancelAlgorithm;283 stream.readableStreamController = controller;284 Promise.resolve(startAlgorithm())285 .then(() => {286 controller.started = true;287 Assert(controller.pulling == false);288 Assert(controller.pullAgain == false);289 ReadableStreamDefaultControllerCallPullIfNeeded(controller);290 })291 .catch(r => {292 ReadableStreamDefaultControllerError(controller, r);293 });294}295export function SetUpReadableStreamDefaultControllerFromUnderlyingSource(params: {296 stream: ReadableStream;297 underlyingSource: UnderlyingSource;298 highWaterMark: number;299 sizeAlgorithm: SizeAlgorithm;300}) {301 const { stream, underlyingSource, highWaterMark, sizeAlgorithm } = params;302 Assert(underlyingSource !== void 0);303 const controller = Object.create(ReadableStreamDefaultController.prototype);304 const startAlgorithm = () =>305 InvokeOrNoop(underlyingSource, "start", controller);306 const pullAlgorithm = CreateAlgorithmFromUnderlyingMethod(...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 controller.enqueue('c');6 }7});8var reader = rs.getReader();9var readPromise = reader.read();10readPromise.then(({value, done}) => {11 if (done) {12 console.log('[readPromise] Done.');13 return;14 }15 console.log('[readPromise] value: "' + value + '"');16 reader.releaseLock();17 rs.getReader().closed.catch(e => console.log(e));18});

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 controller.enqueue('c');6 controller.error(new Error('boom'));7 }8});9rs.getReader().read().then(10 result => console.log(result),11 err => console.log(err)12);13var rs = new ReadableStream({14 start(controller) {15 controller.enqueue('a');16 controller.enqueue('b');17 controller.enqueue('c');18 controller.error(new Error('boom'));19 controller.error(new Error('boom2'));20 }21});22rs.getReader().read().then(23 result => console.log(result),24 err => console.log(err)25);26var rs = new ReadableStream({27 start(controller) {28 controller.enqueue('a');29 controller.enqueue('b');30 controller.enqueue('c');31 controller.error(new Error('boom'));32 controller.close();33 }34});35rs.getReader().read().then(36 result => console.log(result),37 err => console.log(err)38);

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.enqueue("c");6 }7});8var reader = rs.getReader();9var read = reader.read();10read.then(function(result) {11 assert_equals(result.value, "a", "chunk matches");12 assert_false(result.done, "done is false");13 return read;14}).then(function(result) {15 assert_equals(result.value, "b", "chunk matches");16 assert_false(result.done, "done is false");17 return read;18}).then(function(result) {19 assert_equals(result.value, "c", "chunk matches");20 assert_false(result.done, "done is false");21 return read;22}).then(function(result) {23 assert_equals(result.value, undefined, "chunk matches");24 assert_true(result.done, "done is true");25 return read;26}).then(function(result) {27 assert_equals(result.value, undefined, "chunk matches");28 assert_true(result.done, "done is true");29 var e = new Error("boo!");30 var read2 = reader.read();31 reader.releaseLock();32 return read2;33}).then(function(result) {34 assert_unreached("read() should not fulfill when the stream is errored");35}, function(r) {36 assert_equals(r.message, "boo!", "the stream should be errored with the right error");37}).catch(unreached_rejection);38var read2 = reader.read();39read2.then(function(result) {40 assert_unreached("read() should not fulfill when the stream is errored");41}, function(r) {42 assert_equals(r.message, "boo!", "the stream should be errored with the right error");43}).catch(unreached_rejection);44rs.cancel();45var rs = new ReadableStream({46 start(controller) {47 controller.enqueue("a");48 controller.enqueue("b");49 controller.enqueue("c");50 }51});52var reader = rs.getReader();53var read = reader.read();54read.then(function(result) {55 assert_equals(result.value, "a", "chunk matches");56 assert_false(result.done, "done is false");57 return read;58}).then(function(result) {59 assert_equals(result.value, "b", "chunk matches");60 assert_false(result.done, "done is false");61 return read;

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ReadableStream, CountQueuingStrategy } = require('stream/web');2const { ReadableStreamDefaultControllerError } = require('stream/web');3const rs = new ReadableStream({4 start(controller) {5 controller.enqueue('a');6 controller.enqueue('b');7 controller.enqueue('c');8 ReadableStreamDefaultControllerError(controller, new Error('boom'));9 }10}, new CountQueuingStrategy({ highWaterMark: 3 }));11rs.pipeTo(new WritableStream({12 write(chunk) {13 console.log(chunk);14 }15})).catch(e => console.error(e));16ReadableStreamDefaultControllerError ( controller , error )17const { ReadableStream , CountQueuingStrategy } = require ( 'stream/web' ) ;18 const { ReadableStreamDefaultControllerError } = require ( 'stream/web' ) ;19 const rs = new ReadableStream ( {20 start ( controller ) {21 controller . enqueue ( 'a' ) ;22 controller . enqueue ( 'b' ) ;23 controller . enqueue ( 'c' ) ;24 ReadableStreamDefaultControllerError ( controller , new Error ( 'boom' ) ) ;25 }26 } , new CountQueuingStrategy ( { highWaterMark : 3 } ) ) ;27rs . pipeTo ( new WritableStream ( {28 write ( chunk ) {29 console . log ( chunk ) ;30 }31 } ) ) . catch ( e => console . error ( e ) ) ;

Full Screen

Using AI Code Generation

copy

Full Screen

1let rs = new ReadableStream({2 start(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 controller.enqueue('c');6 controller.error(new Error('Oops!'));7 }8});9let reader = rs.getReader();10let read = reader.read();11read.then(result => {12 return reader.read();13}).then(result => {14 return reader.read();15}).then(result => {16 return reader.read();17}).then(result => {18});19let rs = new ReadableStream({20 start(controller) {21 controller.enqueue('a');22 controller.enqueue('b');23 controller.enqueue('c');24 controller.close();25 }26});27let reader = rs.getReader();28let read = reader.read();29read.then(result => {30 return reader.read();31}).then(result => {32 return reader.read();33}).then(result => {34 return reader.read();35}).then(result => {36});37let rs = new ReadableStream({38 start(controller) {39 controller.enqueue('a');40 controller.enqueue('b');41 controller.enqueue('c');42 }43});44let reader = rs.getReader();45let read = reader.read();46read.then(result => {47 return reader.read();48}).then(result => {49 return reader.read();50}).then(result => {

Full Screen

Using AI Code Generation

copy

Full Screen

1var rs = new ReadableStream({2 start(c) {3 c.error(new Error('foo'));4 }5});6rs.getReader().read().then(7 r => console.log('read() should not resolve: ' + r),8 e => console.log('read() rejects with ' + e)9);10var rs = new ReadableStream({11 start(c) {12 c.error(new Error('foo'));13 }14});15rs.getReader().read().then(16 r => console.log('read() should not resolve: ' + r),17 e => console.log('read() rejects with ' + e)18);19var rs = new ReadableStream({20 start(c) {21 c.error(new Error('foo'));22 }23});24rs.getReader().read().then(25 r => console.log('read() should not resolve: ' + r),26 e => console.log('read() rejects with ' + e)27);28var rs = new ReadableStream({29 start(c) {30 c.error(new Error('foo'));31 }32});33rs.getReader().read().then(34 r => console.log('read() should not resolve: ' + r),35 e => console.log('read() rejects with ' + e)36);37var rs = new ReadableStream({38 start(c) {39 c.error(new Error('foo'));40 }41});42rs.getReader().read().then(43 r => console.log('read() should not resolve: ' + r),44 e => console.log('read() rejects with ' + e)45);46var rs = new ReadableStream({47 start(c

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ReadableStream, ReadableStreamDefaultControllerError } = require('web-streams-polyfill/ponyfill');2const rs = new ReadableStream({3 start(controller) {4 controller.enqueue("a");5 controller.enqueue("b");6 controller.enqueue("c");7 ReadableStreamDefaultControllerError(controller, new Error("boom"));8 }9});10const reader = rs.getReader();11const read = () => reader.read().then(result => {12 console.log(result);13 if (!result.done) {14 return read();15 }16});17read().catch(e => console.log(e));18{ value: 'a', done: false }19{ value: 'b', done: false }20{ value: 'c', done: false }21 at ReadableStream.start (test.js:5:31)22 at new ReadableStream (test.js:1:1)23 at Object.<anonymous> (test.js:14:1)24 at Module._compile (internal/modules/cjs/loader.js:955:30)25 at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)26 at Module.load (internal/modules/cjs/loader.js:811:32)27 at Function.Module._load (internal/modules/cjs/loader.js:723:14)28 at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)

Full Screen

Using AI Code Generation

copy

Full Screen

1const rs = new ReadableStream({2 start(controller) {3 controller.error(new Error('boom'));4 }5});6rs.getReader().read().catch(e => console.error(e));7const rs = new ReadableStream({8 start(controller) {9 controller.error(new Error('boom'));10 }11});12rs.getReader().read().catch(e => console.error(e));13const rs = new ReadableStream({14 start(controller) {15 controller.error(new Error('boom'));16 }17});18rs.getReader().read().catch(e => console.error(e));19const rs = new ReadableStream({20 start(controller) {21 controller.error(new Error('boom'));22 }23});24rs.getReader().read().catch(e => console.error(e));25const rs = new ReadableStream({26 start(controller) {27 controller.error(new Error('boom'));28 }29});30rs.getReader().read().catch(e => console.error(e));31const rs = new ReadableStream({32 start(controller) {33 controller.error(new Error('boom'));34 }35});36rs.getReader().read().catch(e => console.error(e));37const rs = new ReadableStream({38 start(controller) {39 controller.error(new Error('boom'));40 }41});42rs.getReader().read().catch(e => console.error(e));43const rs = new ReadableStream({44 start(controller) {45 controller.error(new Error('boom'));46 }47});48rs.getReader().read().catch(e => console.error(e));

Full Screen

Using AI Code Generation

copy

Full Screen

1const rs = new ReadableStream({2 pull(controller) {3 controller.error(new Error('oops'));4 }5});6rs.getReader().read().catch(e => console.log(e));7const rs = new ReadableStream({8 pull(controller) {9 controller.error(new Error('oops'));10 }11});12rs.getReader().read().catch(e => console.log(e));13const rs = new ReadableStream({14 pull(controller) {15 controller.error(new Error('oops'));16 }17});18rs.getReader().read().catch(e => console.log(e));19const rs = new ReadableStream({20 pull(controller) {21 controller.error(new Error('oops'));22 }23});24rs.getReader().read().catch(e => console.log(e));25const rs = new ReadableStream({26 pull(controller) {27 controller.error(new Error('oops'));28 }29});30rs.getReader().read().catch(e => console.log(e));31const rs = new ReadableStream({32 pull(controller) {33 controller.error(new Error('oops'));34 }35});36rs.getReader().read().catch(e => console.log(e));37const rs = new ReadableStream({38 pull(controller) {39 controller.error(new Error('oops'));40 }41});42rs.getReader().read().catch(e => console.log(e));43const rs = new ReadableStream({44 pull(controller) {45 controller.error(new Error('oops'));46 }47});48rs.getReader().read().catch(e => console.log(e

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful