* @param {{ * preventCancel? : boolean, * }} [options] * @returns {AsyncIterable}
(options = kEmptyObject)
| 487 | * @returns {AsyncIterable} |
| 488 | */ |
| 489 | values(options = kEmptyObject) { |
| 490 | if (!isReadableStream(this)) |
| 491 | throw new ERR_INVALID_THIS('ReadableStream'); |
| 492 | validateObject(options, 'options', kValidateObjectAllowObjectsAndNull); |
| 493 | const preventCancel = !!(options?.preventCancel); |
| 494 | |
| 495 | // eslint-disable-next-line no-use-before-define |
| 496 | const reader = new ReadableStreamDefaultReader(this); |
| 497 | |
| 498 | // No __proto__ here to avoid the performance hit. |
| 499 | const state = { |
| 500 | done: false, |
| 501 | current: undefined, |
| 502 | }; |
| 503 | let started = false; |
| 504 | |
| 505 | // The nextSteps function is not an async function in order |
| 506 | // to make it more efficient. Because nextSteps explicitly |
| 507 | // creates a Promise and returns it in the common case, |
| 508 | // making it an async function just causes two additional |
| 509 | // unnecessary Promise allocations to occur, which just add |
| 510 | // cost. |
| 511 | function nextSteps() { |
| 512 | if (state.done) |
| 513 | return PromiseResolve({ done: true, value: undefined }); |
| 514 | |
| 515 | if (reader[kState].stream === undefined) { |
| 516 | return PromiseReject( |
| 517 | new ERR_INVALID_STATE.TypeError( |
| 518 | 'The reader is not bound to a ReadableStream')); |
| 519 | } |
| 520 | const promise = PromiseWithResolvers(); |
| 521 | |
| 522 | // eslint-disable-next-line no-use-before-define |
| 523 | readableStreamDefaultReaderRead(reader, new ReadableStreamAsyncIteratorReadRequest(reader, state, promise)); |
| 524 | return promise.promise; |
| 525 | } |
| 526 | |
| 527 | async function returnSteps(value) { |
| 528 | if (state.done) |
| 529 | return { done: true, value }; // eslint-disable-line node-core/avoid-prototype-pollution |
| 530 | state.done = true; |
| 531 | |
| 532 | if (reader[kState].stream === undefined) { |
| 533 | throw new ERR_INVALID_STATE.TypeError( |
| 534 | 'The reader is not bound to a ReadableStream'); |
| 535 | } |
| 536 | assert(!reader[kState].readRequests.length); |
| 537 | if (!preventCancel) { |
| 538 | const result = readableStreamReaderGenericCancel(reader, value); |
| 539 | readableStreamReaderGenericRelease(reader); |
| 540 | await result; |
| 541 | return { done: true, value }; // eslint-disable-line node-core/avoid-prototype-pollution |
| 542 | } |
| 543 | |
| 544 | readableStreamReaderGenericRelease(reader); |
| 545 | return { done: true, value }; // eslint-disable-line node-core/avoid-prototype-pollution |
| 546 | } |
no test coverage detected