@vbarbarosh/node-helpers 3.10.1 → 3.12.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/urlclean.js +21 -0
- package/src/urlclean.test.js +18 -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;
|
package/src/urlclean.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const querystring = require('querystring');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Replace unnecessary encodes symbols with chars. The returned url will be
|
|
5
|
+
* more human frieldy, at the same time it still be valid url.
|
|
6
|
+
*/
|
|
7
|
+
function urlclean(url)
|
|
8
|
+
{
|
|
9
|
+
const i = url.indexOf('?') + 1;
|
|
10
|
+
if (i) {
|
|
11
|
+
const s = querystring.stringify(querystring.parse(url.slice(i)), '&', '=', {
|
|
12
|
+
encodeURIComponent: function (v) {
|
|
13
|
+
return encodeURIComponent(v).replaceAll('%3A', ':').replaceAll('%2F', '/');
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
return url.slice(0, i) + s;
|
|
17
|
+
}
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = urlclean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const urlclean = require('./urlclean');
|
|
3
|
+
|
|
4
|
+
const items = [
|
|
5
|
+
['', ''],
|
|
6
|
+
['http://127.0.0.1:3000/proxy', 'http://127.0.0.1:3000/proxy'],
|
|
7
|
+
['http://127.0.0.1:3000/proxy?url=http://127.0.0.1:3000/static/&redirects=2', 'http://127.0.0.1:3000/proxy?url=http://127.0.0.1:3000/static/&redirects=2'],
|
|
8
|
+
['http://127.0.0.1:3000/proxy?url=http%3A%2F%2F127.0.0.1%3A3000%2Fstatic%2F&redirects=1', 'http://127.0.0.1:3000/proxy?url=http://127.0.0.1:3000/static/&redirects=1'],
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
describe('urlclean', function () {
|
|
12
|
+
items.forEach(function ([input, expected]) {
|
|
13
|
+
it(input, function () {
|
|
14
|
+
const actual = urlclean(input);
|
|
15
|
+
assert.strictEqual(actual, expected);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -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;
|