2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-04 06:12:52 +00:00
|
|
|
* Merge multiple streams into a single one, taking order into account, and
|
|
|
|
* each stream will wait for a chunk to enqueue before the next stream can
|
2024-05-28 01:27:40 +00:00
|
|
|
* append another chunk.
|
2023-12-04 06:12:52 +00:00
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* If a stream ends before other ones, the others will continue adding data in
|
|
|
|
* order, and the finished one will not add any more data. If you want to cancel
|
|
|
|
* the other streams when one of them ends, use {@linkcode earlyZipReadableStreams}.
|
|
|
|
*
|
|
|
|
* @typeparam T The type of the chunks in the input/output streams.
|
2024-10-29 05:24:18 +00:00
|
|
|
* @param streams An iterable of `ReadableStream`s to merge.
|
2024-05-28 01:27:40 +00:00
|
|
|
* @returns A `ReadableStream` that will emit the zipped chunks.
|
|
|
|
*
|
|
|
|
* @example Zip 2 streams with the same length
|
2023-12-04 06:12:52 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-04 06:12:52 +00:00
|
|
|
*
|
|
|
|
* const stream1 = ReadableStream.from(["1", "2", "3"]);
|
|
|
|
* const stream2 = ReadableStream.from(["a", "b", "c"]);
|
|
|
|
* const zippedStream = zipReadableStreams(stream1, stream2);
|
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(zippedStream),
|
|
|
|
* ["1", "a", "2", "b", "3", "c"],
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Zip 2 streams with different length (first one is shorter)
|
|
|
|
* ```ts
|
|
|
|
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* const stream1 = ReadableStream.from(["1", "2"]);
|
|
|
|
* const stream2 = ReadableStream.from(["a", "b", "c", "d"]);
|
|
|
|
* const zippedStream = zipReadableStreams(stream1, stream2);
|
|
|
|
*
|
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(zippedStream),
|
|
|
|
* ["1", "a", "2", "b", "c", "d"],
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Zip 2 streams with different length (first one is longer)
|
|
|
|
* ```ts
|
|
|
|
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* const stream1 = ReadableStream.from(["1", "2", "3", "4"]);
|
|
|
|
* const stream2 = ReadableStream.from(["a", "b"]);
|
|
|
|
* const zippedStream = zipReadableStreams(stream1, stream2);
|
|
|
|
*
|
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(zippedStream),
|
|
|
|
* ["1", "a", "2", "b", "3", "4"],
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Zip 3 streams
|
|
|
|
* ```ts
|
|
|
|
* import { zipReadableStreams } from "@std/streams/zip-readable-streams";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* const stream1 = ReadableStream.from(["1"]);
|
|
|
|
* const stream2 = ReadableStream.from(["a", "b"]);
|
|
|
|
* const stream3 = ReadableStream.from(["A", "B", "C"]);
|
|
|
|
* const zippedStream = zipReadableStreams(stream1, stream2, stream3);
|
|
|
|
*
|
|
|
|
* assertEquals(
|
|
|
|
* await Array.fromAsync(zippedStream),
|
|
|
|
* ["1", "a", "A", "b", "B", "C"],
|
|
|
|
* );
|
2023-12-04 06:12:52 +00:00
|
|
|
* ```
|
2022-11-29 13:55:38 +00:00
|
|
|
*/
|
|
|
|
export function zipReadableStreams<T>(
|
|
|
|
...streams: ReadableStream<T>[]
|
|
|
|
): ReadableStream<T> {
|
2023-09-21 10:23:28 +00:00
|
|
|
const readers = new Set(streams.map((s) => s.getReader()));
|
2022-11-29 13:55:38 +00:00
|
|
|
return new ReadableStream<T>({
|
|
|
|
async start(controller) {
|
|
|
|
try {
|
|
|
|
let resolved = 0;
|
2023-08-25 09:04:43 +00:00
|
|
|
while (resolved !== streams.length) {
|
2023-09-21 10:23:28 +00:00
|
|
|
for (const reader of readers) {
|
2022-11-29 13:55:38 +00:00
|
|
|
const { value, done } = await reader.read();
|
|
|
|
if (!done) {
|
|
|
|
controller.enqueue(value!);
|
|
|
|
} else {
|
|
|
|
resolved++;
|
2023-09-21 10:23:28 +00:00
|
|
|
readers.delete(reader);
|
2022-11-29 13:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controller.close();
|
|
|
|
} catch (e) {
|
|
|
|
controller.error(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|