@vbarbarosh/node-helpers 3.12.0 → 3.13.0
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 +1 -1
- package/src/stream_multiplex.js +51 -0
- package/src/stream_multiplex.test.js +67 -0
package/package.json
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const ignore = require('./ignore');
|
|
2
|
+
const stream = require('stream');
|
|
3
|
+
const wait_while = require('./wait_while');
|
|
4
|
+
|
|
5
|
+
function stream_multiplex(...streams)
|
|
6
|
+
{
|
|
7
|
+
// `new stream.Writable()` will ignore second, third, etc. calls to `destroy`
|
|
8
|
+
return stream.Writable({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
construct: function (next) {
|
|
11
|
+
streams.forEach(s => s.once('error', e => this.destroy(e)));
|
|
12
|
+
next();
|
|
13
|
+
},
|
|
14
|
+
destroy: async function (error, next) {
|
|
15
|
+
let running = streams.length;
|
|
16
|
+
streams.forEach(s => s.destroy(error, () => running--));
|
|
17
|
+
await wait_while(() => running > 0);
|
|
18
|
+
next();
|
|
19
|
+
},
|
|
20
|
+
write: async function (chunk, enc, next) {
|
|
21
|
+
let running = streams.length;
|
|
22
|
+
streams.forEach(function (stream) {
|
|
23
|
+
stream.write(chunk, function (error) {
|
|
24
|
+
running--;
|
|
25
|
+
if (error) {
|
|
26
|
+
next(error);
|
|
27
|
+
next = ignore;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
await wait_while(() => running > 0);
|
|
32
|
+
next();
|
|
33
|
+
},
|
|
34
|
+
final: async function (next) {
|
|
35
|
+
let running = streams.length;
|
|
36
|
+
streams.forEach(function (stream) {
|
|
37
|
+
stream.end(function (error) {
|
|
38
|
+
running--;
|
|
39
|
+
if (error) {
|
|
40
|
+
next(error);
|
|
41
|
+
next = ignore;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
await wait_while(() => running > 0);
|
|
46
|
+
next();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = stream_multiplex;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const ignore = require('./ignore');
|
|
3
|
+
const stream = require('stream');
|
|
4
|
+
const stream_multiplex = require('./stream_multiplex');
|
|
5
|
+
|
|
6
|
+
describe('stream_multiplex', function () {
|
|
7
|
+
it('basic', async function () {
|
|
8
|
+
const input = [1, 2, 3, 'str1'];
|
|
9
|
+
const rs = stream.Readable.from(input);
|
|
10
|
+
const ws1 = stream_debug();
|
|
11
|
+
const ws2 = stream_debug();
|
|
12
|
+
await stream.promises.pipeline(rs, stream_multiplex(ws1, ws2));
|
|
13
|
+
assert.deepStrictEqual(ws1.items, input);
|
|
14
|
+
assert.deepStrictEqual(ws2.items, input);
|
|
15
|
+
assert.deepStrictEqual(ws1.events.map(v => v.name), ['finish', 'close']);
|
|
16
|
+
assert.deepStrictEqual(ws2.events.map(v => v.name), ['finish', 'close']);
|
|
17
|
+
});
|
|
18
|
+
it('should throw after first error', async function () {
|
|
19
|
+
const input = [1, 2, 3, 'str1'];
|
|
20
|
+
const fail_at = 2;
|
|
21
|
+
const rs = stream.Readable.from(input);
|
|
22
|
+
const ws1 = stream_debug();
|
|
23
|
+
const ws2 = stream_debug();
|
|
24
|
+
const p = stream.promises.pipeline(rs, stream_multiplex(ws1, ws2, stream_fail_at(fail_at)));
|
|
25
|
+
await assert.rejects(p, new Error(`Failed at ${fail_at}`));
|
|
26
|
+
assert.deepStrictEqual(ws1.items, input.slice(0, fail_at));
|
|
27
|
+
assert.deepStrictEqual(ws2.items, input.slice(0, fail_at));
|
|
28
|
+
assert.deepStrictEqual(ws1.events.map(v => v.name), ['error', 'close']);
|
|
29
|
+
assert.deepStrictEqual(ws2.events.map(v => v.name), ['error', 'close']);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
function stream_debug()
|
|
34
|
+
{
|
|
35
|
+
const out = new stream.Writable({
|
|
36
|
+
objectMode: true,
|
|
37
|
+
write: function (item, enc, next) {
|
|
38
|
+
out.items.push(item);
|
|
39
|
+
next();
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
out.items = [];
|
|
43
|
+
out.events = [];
|
|
44
|
+
out.once('close', (...args) => out.events.push({name: 'close', args})); // result of `destroy`
|
|
45
|
+
out.once('drain', (...args) => out.events.push({name: 'drain', args}));
|
|
46
|
+
out.once('error', (...args) => out.events.push({name: 'error', args}));
|
|
47
|
+
out.once('finish', (...args) => out.events.push({name: 'finish', args})); // result of `end`
|
|
48
|
+
out.once('pipe', (...args) => out.events.push({name: 'pipe', args}));
|
|
49
|
+
out.once('unpipe', (...args) => out.events.push({name: 'unpipe', args}));
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function stream_fail_at(at)
|
|
54
|
+
{
|
|
55
|
+
let counter = 0;
|
|
56
|
+
return new stream.Writable({
|
|
57
|
+
objectMode: true,
|
|
58
|
+
write: function (item, enc, next) {
|
|
59
|
+
if (++counter >= at) {
|
|
60
|
+
next(new Error(`Failed at ${at}`));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
next();
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|