Skip to content
Merged
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
105 changes: 82 additions & 23 deletions lib/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
* @returns {ReadableStream} Concatenated list
*/
function concatStream(list) {
list = list.map(toStream);

Check warning on line 69 in lib/streams.js

View workflow job for this annotation

GitHub Actions / ESLint

Assignment to function parameter 'list'
const transform = transformWithCancel(async function(reason) {
await Promise.all(transforms.map(stream => cancel(stream, reason)));
});
Expand Down Expand Up @@ -111,7 +111,7 @@
preventCancel = false
} = {}) {
if (isStream(input) && !isArrayStream(input) && !isArrayStream(target)) {
input = toStream(input);

Check warning on line 114 in lib/streams.js

View workflow job for this annotation

GitHub Actions / ESLint

Assignment to function parameter 'input'
try {
if (input[externalBuffer]) {
const writer = getWriter(target);
Expand All @@ -130,7 +130,7 @@
return;
}
if (!isStream(input)) {
input = toArrayStream(input);

Check warning on line 133 in lib/streams.js

View workflow job for this annotation

GitHub Actions / ESLint

Assignment to function parameter 'input'
}
const reader = getReader(input);
const writer = getWriter(target);
Expand Down Expand Up @@ -222,12 +222,19 @@

/**
* Transform a stream using helper functions which are called on each chunk, and on stream close, respectively.
* Takes an optional queuing strategy for the resulting readable stream;
* see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#queuingstrategy.
* By default, the queueing strategy is non-buffering. When the `process`
* function is asynchronous, it may be useful to pass a buffering
* queuing strategy to enable multiple chunks to be processed in parallel;
* e.g. pass `{ highWaterMark: 4 }` to process up to 4 chunks in parallel.
* @param {ReadableStream|Uint8array|String} input
* @param {Function} process
* @param {Function} finish
* @param {Object} queuingStrategy
* @returns {ReadableStream|Uint8array|String}
*/
function transform(input, process = () => undefined, finish = () => undefined) {
function transform(input, process = () => undefined, finish = () => undefined, queuingStrategy = { highWaterMark: 0 }) {
if (isArrayStream(input)) {
const output = new ArrayStream();
(async () => {
Expand All @@ -248,24 +255,47 @@
return output;
}
if (isStream(input)) {
return transformRaw(input, {
async transform(value, controller) {
try {
const result = await process(value);
if (result !== undefined) controller.enqueue(result);
} catch(e) {
controller.error(e);
}
let reader;
let allDone = false;
return new ReadableStream({
start() {
reader = input.getReader();
},
async flush(controller) {
async pull(controller) {
if (allDone) {
controller.close();
input.releaseLock();
return;
}
try {
const result = await finish();
if (result !== undefined) controller.enqueue(result);
} catch(e) {
// Read repeatedly until we have a chunk to enqueue or until
// we can close the stream, as `pull` won't get called again
// until we call `enqueue` or `close`.
while (true) { // eslint-disable-line no-constant-condition
const { value, done } = await reader.read();
allDone = done;
const result = await (done ? finish : process)(value);
if (result !== undefined) {
controller.enqueue(result);
return; // `pull` will get called again
}
if (done) {
// If `finish` didn't return a chunk to enqueue, call
// `close` here. Otherwise, it will get called in the
// next call to `pull`, above (since `allDone == true`).
controller.close();
input.releaseLock();
return;
}
}
} catch (e) {
controller.error(e);
}
},
async cancel(reason) {
await reader.cancel(reason);
}
});
}, queuingStrategy);
}
const result1 = process(input);
const result2 = finish();
Expand Down Expand Up @@ -301,7 +331,7 @@
fn(incoming.readable, outgoing.writable);
return outgoing.readable;
}
input = toArrayStream(input);

Check warning on line 334 in lib/streams.js

View workflow job for this annotation

GitHub Actions / ESLint

Assignment to function parameter 'input'
const output = new ArrayStream();
fn(input, output);
return output;
Expand Down Expand Up @@ -443,19 +473,48 @@
}
if (isStream(input)) {
if (begin >= 0 && end >= 0) {
let reader;
let bytesRead = 0;
return transformRaw(input, {
transform(value, controller) {
if (bytesRead < end) {
if (bytesRead + value.length >= begin) {
controller.enqueue(slice(value, Math.max(begin - bytesRead, 0), end - bytesRead));
return new ReadableStream({
start() {
reader = input.getReader();
},
async pull(controller) {
try {
// Read repeatedly until we have a chunk to enqueue or until
// we can close the stream, as `pull` won't get called again
// until we call `enqueue` or `close`.
while (true) { // eslint-disable-line no-constant-condition
if (bytesRead < end) {
const { value, done } = await reader.read();
if (done) {
controller.close();
input.releaseLock();
return;
}
let valueToEnqueue;
if (bytesRead + value.length >= begin) {
valueToEnqueue = slice(value, Math.max(begin - bytesRead, 0), end - bytesRead);
}
bytesRead += value.length;
if (valueToEnqueue) {
controller.enqueue(valueToEnqueue);
return; // `pull` will get called again
}
} else {
controller.close();
input.releaseLock();
return;
}
}
bytesRead += value.length;
} else {
controller.terminate();
} catch (e) {
controller.error(e);
}
},
async cancel(reason) {
await reader.cancel(reason);
}
});
}, { highWaterMark: 0 });
}
if (begin < 0 && (end < 0 || end === Infinity)) {
let lastBytes = [];
Expand All @@ -479,7 +538,7 @@
return fromAsync(async () => slice(await readToEnd(input), begin, end));
}
if (input[externalBuffer]) {
input = concat(input[externalBuffer].concat([input]));

Check warning on line 541 in lib/streams.js

View workflow job for this annotation

GitHub Actions / ESLint

Assignment to function parameter 'input'
}
if (isUint8Array(input)) {
return input.subarray(begin, end === Infinity ? input.length : end);
Expand Down