@vbarbarosh/node-helpers 3.12.0 → 3.14.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 +43 -4
- package/src/sanitize_dash_name.js +7 -0
- package/src/sanitize_dash_name.test.js +59 -0
- package/src/sanitize_filename.js +8 -0
- package/src/stream_multiplex.js +51 -0
- package/src/stream_multiplex.test.js +67 -0
- package/src/fs_path_sanitize.js +0 -8
package/package.json
CHANGED
|
@@ -3,17 +3,28 @@ const stream = require('stream');
|
|
|
3
3
|
const wait_while = require('./wait_while');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Returns a writable stream for
|
|
6
|
+
* Returns a writable stream for inserting, or conditionally updating, documents in mongo collection.
|
|
7
7
|
*/
|
|
8
|
-
function mongo_stream_upsert({collection, concurrency = 1})
|
|
8
|
+
function mongo_stream_upsert({collection, update_if, concurrency = 1})
|
|
9
9
|
{
|
|
10
|
+
const errors = [];
|
|
10
11
|
return new stream.Writable({
|
|
11
12
|
objectMode: true,
|
|
12
13
|
construct(next) {
|
|
13
14
|
this.running = 0;
|
|
14
15
|
next();
|
|
15
16
|
},
|
|
17
|
+
destroy: async function (error, next) {
|
|
18
|
+
// console.log('mongo_stream_upsert.destroy', errors);
|
|
19
|
+
await wait_while(() => this.running > 0);
|
|
20
|
+
next();
|
|
21
|
+
},
|
|
16
22
|
write: async function (items, enc, next) {
|
|
23
|
+
// console.log('mongo_stream_upsert.write', errors);
|
|
24
|
+
if (errors.length) {
|
|
25
|
+
next(errors[0]);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
17
28
|
try {
|
|
18
29
|
if (!Array.isArray(items)) {
|
|
19
30
|
next(new Error('An array of objects is expected.'));
|
|
@@ -21,10 +32,33 @@ function mongo_stream_upsert({collection, concurrency = 1})
|
|
|
21
32
|
}
|
|
22
33
|
await wait_while(() => this.running >= concurrency);
|
|
23
34
|
const operations = items.map(function (item) {
|
|
24
|
-
|
|
35
|
+
if (!update_if) {
|
|
36
|
+
return {
|
|
37
|
+
replaceOne: {
|
|
38
|
+
filter: {_id: item._id},
|
|
39
|
+
replacement: item,
|
|
40
|
+
upsert: true,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
updateOne: {
|
|
46
|
+
filter: {_id: item._id},
|
|
47
|
+
update: [[
|
|
48
|
+
{
|
|
49
|
+
$replaceRoot: {
|
|
50
|
+
newRoot: {
|
|
51
|
+
$cond: [update_if, item, '$$ROOT'],
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
]],
|
|
56
|
+
upsert: true,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
25
59
|
});
|
|
26
60
|
this.running++;
|
|
27
|
-
Promise.resolve(collection.bulkWrite(operations, {multi: true})).finally(() => this.running--);
|
|
61
|
+
Promise.resolve(collection.bulkWrite(operations, {multi: true})).catch(e => errors.push(e)).finally(() => this.running--);
|
|
28
62
|
next();
|
|
29
63
|
}
|
|
30
64
|
catch (error) {
|
|
@@ -32,6 +66,11 @@ function mongo_stream_upsert({collection, concurrency = 1})
|
|
|
32
66
|
}
|
|
33
67
|
},
|
|
34
68
|
final: async function (next) {
|
|
69
|
+
// console.log('mongo_stream_upsert.final', errors);
|
|
70
|
+
if (errors.length) {
|
|
71
|
+
next(errors[0]);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
35
74
|
await wait_while(() => this.running > 0);
|
|
36
75
|
next();
|
|
37
76
|
},
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function sanitize_dash_name(s)
|
|
2
|
+
{
|
|
3
|
+
// https://stackoverflow.com/a/37511463
|
|
4
|
+
return s.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[^a-zA-Z0-9]/g, '-').replace(/-+/g, '-').replace(/^-+|-+$/g, '').toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
module.exports = sanitize_dash_name;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const sanitize_dash_name = require('./sanitize_dash_name');
|
|
3
|
+
|
|
4
|
+
const items = [
|
|
5
|
+
['', ''],
|
|
6
|
+
['', '.'],
|
|
7
|
+
['', 'привет'],
|
|
8
|
+
['1-2-3', '1-2-3'],
|
|
9
|
+
['1-2-3', '1.2.3'],
|
|
10
|
+
['1000-stories', '1000 Stories'],
|
|
11
|
+
['1800', '1800'],
|
|
12
|
+
['3-a', '3-A'],
|
|
13
|
+
['43', '43'],
|
|
14
|
+
['600-d', '600+D'],
|
|
15
|
+
['9-lives', '9 Lives'],
|
|
16
|
+
['a-c-d', 'A, C, D'],
|
|
17
|
+
['a-f', 'A F'],
|
|
18
|
+
['accua-aseptic-solucion', 'Accua Aséptic Solución'],
|
|
19
|
+
['acido-acetilsalicilico', 'Ácido Acetilsalicílico'],
|
|
20
|
+
['acido-nalidixico-fenazopiridina', 'Ácido Nalidixico-Fenazopiridina'],
|
|
21
|
+
['aeropostale', 'Aéropostale'],
|
|
22
|
+
['arnica-nartex', 'Árnica Nártex'],
|
|
23
|
+
['art-n-chef', 'Art \'n Chef'],
|
|
24
|
+
['b-a-r-t-e-r', 'B.A.R.T.E.R.'],
|
|
25
|
+
['b-oost', 'b:oost'],
|
|
26
|
+
['betametasona-clotrimazol-gentamicina', 'Betametasona - Clotrimazol - Gentamicina'],
|
|
27
|
+
['black-decker', 'Black & Decker'],
|
|
28
|
+
['black-decker', 'Black + Decker'],
|
|
29
|
+
['black-panther', 'Black Panther'],
|
|
30
|
+
['black-white', 'Black & White'],
|
|
31
|
+
['blanca-nieves', 'Blanca Nieves'],
|
|
32
|
+
['de-rancho', 'de Rancho'],
|
|
33
|
+
['del-valle', 'del Valle'],
|
|
34
|
+
['exportacion-selecto', 'Exportación Selecto'],
|
|
35
|
+
['extra', 'Extra'],
|
|
36
|
+
['ezetimiba-simvastatina', 'Ezetimiba Simvastatina'],
|
|
37
|
+
['facicam-gel-90g', 'FACICAM GEL 90G'],
|
|
38
|
+
['folios', 'FOLIOS'],
|
|
39
|
+
['fresh-garden', 'FRESH GARDEN'],
|
|
40
|
+
['gi-valproato-de-magnesio', '(GI) Valproato de Magnesio'],
|
|
41
|
+
['itrac-100mg-c-f-15c', 'ITRAC 100MG C/F 15C'],
|
|
42
|
+
['m-m-s', 'M&M\'s'],
|
|
43
|
+
['m-m-s', 'M&M´s'],
|
|
44
|
+
['meticel-0-5', 'Meticel 0.5 %'],
|
|
45
|
+
['meticel-2', 'Meticel 2%'],
|
|
46
|
+
['opera-prima', 'Ópera Prima'],
|
|
47
|
+
['oxido-de-zinc', 'Óxido de Zinc'],
|
|
48
|
+
['r-u-m-b-a', 'r u m b a'],
|
|
49
|
+
['r-u-m-b-a', 'r_u_m_b_a'],
|
|
50
|
+
['ssh-authorized-keys', '~/.\u0000ssh/authorized_keys";\n'],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
describe('sanitize_dash_name', function () {
|
|
54
|
+
items.forEach(function ([expected, input],) {
|
|
55
|
+
it(`${input} → ${expected}`, function () {
|
|
56
|
+
assert.deepStrictEqual(sanitize_dash_name(input), expected);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -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
|
+
}
|