Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export function readToEnd<T extends Data, JoinFn extends (chunks: T[]) => any =
join?: JoinFn
): Promise<ReturnType<JoinFn>>;

export function consumeToEnd<T extends Data>(input: MaybeStream<T>): Promise<undefined>;

export function toStream<T extends Data, InputType extends MaybeStream<T>>(
input: InputType
): InputType extends T ? Stream<InputType> : InputType;
Expand Down
12 changes: 12 additions & 0 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ Reader.prototype.readToEnd = async function(join=streams.concat) {
return join(result);
};

/**
* Consume the entire stream and wait until it ends.
* @async
*/
Reader.prototype.consumeToEnd = async function() {
// eslint-disable-next-line no-constant-condition
while (true) {
const { done } = await this.read();
if (done) break;
}
};

export { Reader, externalBuffer };
15 changes: 15 additions & 0 deletions lib/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,20 @@ async function readToEnd(input, join=concat) {
return input;
}

/**
* Consume a stream to the end. Throws if the input stream errors.
* @param {ReadableStream|Uint8array|String} input
* @async
*/
async function consumeToEnd(input) {
if (isArrayStream(input)) {
return input.consumeToEnd();
}
if (isStream(input)) {
return getReader(input).consumeToEnd();
}
}

/**
* Cancel a stream.
* @param {ReadableStream|Uint8array|String} input
Expand Down Expand Up @@ -576,6 +590,7 @@ export {
passiveClone,
slice,
readToEnd,
consumeToEnd,
cancel,
fromAsync
};
4 changes: 4 additions & 0 deletions lib/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ArrayStream.prototype.readToEnd = async function(join) {
return result;
};

ArrayStream.prototype.consumeToEnd = async function() {
await this[doneWritingPromise];
};

ArrayStream.prototype.clone = function() {
const clone = new ArrayStream();
clone[doneWritingPromise] = this[doneWritingPromise].then(() => {
Expand Down