@vbarbarosh/node-helpers 3.11.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/mongo_stream_upsert.js +41 -0
- package/src/stream_group.js +27 -0
- package/src/stream_multiplex.js +51 -0
- package/src/stream_multiplex.test.js +67 -0
- package/src/wait_while.js +26 -0
package/package.json
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const Promise = require('bluebird');
|
|
2
|
+
const stream = require('stream');
|
|
3
|
+
const wait_while = require('./wait_while');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns a writable stream for upsert mongo collection.
|
|
7
|
+
*/
|
|
8
|
+
function mongo_stream_upsert({collection, concurrency = 1})
|
|
9
|
+
{
|
|
10
|
+
return new stream.Writable({
|
|
11
|
+
objectMode: true,
|
|
12
|
+
construct(next) {
|
|
13
|
+
this.running = 0;
|
|
14
|
+
next();
|
|
15
|
+
},
|
|
16
|
+
write: async function (items, enc, next) {
|
|
17
|
+
try {
|
|
18
|
+
if (!Array.isArray(items)) {
|
|
19
|
+
next(new Error('An array of objects is expected.'));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
await wait_while(() => this.running >= concurrency);
|
|
23
|
+
const operations = items.map(function (item) {
|
|
24
|
+
return {replaceOne: {filter: {_id: item._id}, replacement: item, upsert: true}};
|
|
25
|
+
});
|
|
26
|
+
this.running++;
|
|
27
|
+
Promise.resolve(collection.bulkWrite(operations, {multi: true})).finally(() => this.running--);
|
|
28
|
+
next();
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
next(error);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
final: async function (next) {
|
|
35
|
+
await wait_while(() => this.running > 0);
|
|
36
|
+
next();
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = mongo_stream_upsert;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a transform stream used to group object into chunks of objects.
|
|
5
|
+
*/
|
|
6
|
+
function stream_group(chunk_size)
|
|
7
|
+
{
|
|
8
|
+
const chunk = [];
|
|
9
|
+
return new stream.Transform({
|
|
10
|
+
objectMode: true,
|
|
11
|
+
transform: function (item, enc, next) {
|
|
12
|
+
chunk.push(item);
|
|
13
|
+
if (chunk.length >= chunk_size) {
|
|
14
|
+
this.push(chunk.splice(0));
|
|
15
|
+
}
|
|
16
|
+
next();
|
|
17
|
+
},
|
|
18
|
+
flush: function (next) {
|
|
19
|
+
if (chunk.length) {
|
|
20
|
+
this.push(chunk.splice(0));
|
|
21
|
+
next();
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = stream_group;
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const Promise = require('bluebird');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wait until `fn` returns `true`.
|
|
5
|
+
*/
|
|
6
|
+
function wait_while(fn)
|
|
7
|
+
{
|
|
8
|
+
return new Promise(function (resolve, reject) {
|
|
9
|
+
setTimeout(tick, 1);
|
|
10
|
+
async function tick() {
|
|
11
|
+
try {
|
|
12
|
+
if (await fn()) {
|
|
13
|
+
setTimeout(tick, 1);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
resolve();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
reject(error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = wait_while;
|