@miso.ai/server-commons 0.6.6-beta.2 → 0.6.6-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/stream/index.js +1 -0
- package/src/stream/joint-stream.js +23 -0
package/package.json
CHANGED
package/src/stream/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './misc.js';
|
|
2
2
|
export * from './transform.js';
|
|
3
|
+
export * from './joint-stream.js';
|
|
3
4
|
export { default as BufferedReadStream } from './buffered-read.js';
|
|
4
5
|
export { default as BufferedWriteStream } from './buffered-write.js';
|
|
5
6
|
export { default as OutputStream } from './output.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
|
|
3
|
+
export function joinStreams(streams, joinFn = defaultJoinFn) {
|
|
4
|
+
if (typeof joinFn !== 'function') {
|
|
5
|
+
throw new Error(`joinFn must be a function: ${joinFn}`);
|
|
6
|
+
}
|
|
7
|
+
return Readable.from(generate(streams, joinFn));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function defaultJoinFn(...args) {
|
|
11
|
+
return args;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function * generate(streams, joinFn) {
|
|
15
|
+
const iterators = streams.map(stream => stream[Symbol.asyncIterator]());
|
|
16
|
+
while (true) {
|
|
17
|
+
const entries = await Promise.all(iterators.map(iterator => iterator.next()));
|
|
18
|
+
if (entries.every(entry => entry.done)) {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
yield joinFn(...entries.map(entry => entry.value));
|
|
22
|
+
}
|
|
23
|
+
}
|