* @param {ReadableStream} readableStream * @param {{ * highWaterMark? : number, * encoding? : string, * objectMode? : boolean, * signal? : AbortSignal, * }} [options] * @returns {Readable}
(readableStream, options = kEmptyObject)
| 572 | * @returns {Readable} |
| 573 | */ |
| 574 | function newStreamReadableFromReadableStream(readableStream, options = kEmptyObject) { |
| 575 | if (!isReadableStream(readableStream)) { |
| 576 | throw new ERR_INVALID_ARG_TYPE( |
| 577 | 'readableStream', |
| 578 | 'ReadableStream', |
| 579 | readableStream); |
| 580 | } |
| 581 | |
| 582 | validateObject(options, 'options'); |
| 583 | const { |
| 584 | highWaterMark, |
| 585 | encoding, |
| 586 | objectMode = false, |
| 587 | signal, |
| 588 | } = options; |
| 589 | |
| 590 | if (encoding !== undefined && !Buffer.isEncoding(encoding)) |
| 591 | throw new ERR_INVALID_ARG_VALUE('options.encoding', encoding); |
| 592 | validateBoolean(objectMode, 'options.objectMode'); |
| 593 | |
| 594 | const reader = readableStream.getReader(); |
| 595 | let closed = false; |
| 596 | |
| 597 | const readable = new Readable({ |
| 598 | objectMode, |
| 599 | highWaterMark, |
| 600 | encoding, |
| 601 | signal, |
| 602 | |
| 603 | read() { |
| 604 | PromisePrototypeThen( |
| 605 | reader.read(), |
| 606 | (chunk) => { |
| 607 | if (chunk.done) { |
| 608 | // Value should always be undefined here. |
| 609 | readable.push(null); |
| 610 | } else { |
| 611 | readable.push(chunk.value); |
| 612 | } |
| 613 | }, |
| 614 | (error) => destroy(readable, error)); |
| 615 | }, |
| 616 | |
| 617 | destroy(error, callback) { |
| 618 | function done() { |
| 619 | try { |
| 620 | callback(error); |
| 621 | } catch (error) { |
| 622 | // In a next tick because this is happening within |
| 623 | // a promise context, and if there are any errors |
| 624 | // thrown we don't want those to cause an unhandled |
| 625 | // rejection. Let's just escape the promise and |
| 626 | // handle it separately. |
| 627 | process.nextTick(() => { throw error; }); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (!closed) { |
no test coverage detected
searching dependent graphs…