@vbarbarosh/node-helpers 3.30.0 → 3.31.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_discard.js +2 -2
- package/src/stream_each.js +17 -0
- package/src/stream_filter.js +24 -0
- package/src/stream_group.js +6 -4
- package/src/stream_hash.js +4 -4
- package/src/stream_lines.js +11 -0
- package/src/stream_map.js +22 -0
- package/src/stream_md5.js +7 -4
- package/src/stream_multiplex.js +10 -10
- package/src/stream_parse_xml.js +4 -4
- package/src/stream_progress.js +14 -7
- package/src/stream_promise.js +1 -0
- package/src/stream_strpbrk.js +5 -5
- package/src/stream_through.js +3 -3
- package/src/stream_transform.js +3 -0
package/package.json
CHANGED
package/src/stream_discard.js
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pass each item to a simple function. This is a final step.
|
|
5
|
+
*/
|
|
6
|
+
function stream_each(fn)
|
|
7
|
+
{
|
|
8
|
+
return stream.Writable({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
write: function (item, encoding, callback) {
|
|
11
|
+
fn(item);
|
|
12
|
+
callback();
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = stream_each;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pass down only those items passed user-defined criteria.
|
|
5
|
+
*/
|
|
6
|
+
function stream_filter(fn)
|
|
7
|
+
{
|
|
8
|
+
return new stream.Transform({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
transform: function (item, encoding, callback) {
|
|
11
|
+
try {
|
|
12
|
+
if (fn(item)) {
|
|
13
|
+
this.push(item);
|
|
14
|
+
}
|
|
15
|
+
callback();
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
callback(error);
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = stream_filter;
|
package/src/stream_group.js
CHANGED
|
@@ -2,24 +2,26 @@ const stream = require('stream');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns a transform stream used to group object into chunks of objects.
|
|
5
|
+
*
|
|
6
|
+
* TODO Rename to stream_chunk
|
|
5
7
|
*/
|
|
6
8
|
function stream_group(chunk_size)
|
|
7
9
|
{
|
|
8
10
|
const chunk = [];
|
|
9
11
|
return new stream.Transform({
|
|
10
12
|
objectMode: true,
|
|
11
|
-
transform: function (item, encoding,
|
|
13
|
+
transform: function (item, encoding, callback) {
|
|
12
14
|
chunk.push(item);
|
|
13
15
|
if (chunk.length >= chunk_size) {
|
|
14
16
|
this.push(chunk.splice(0));
|
|
15
17
|
}
|
|
16
|
-
|
|
18
|
+
callback();
|
|
17
19
|
},
|
|
18
|
-
flush: function (
|
|
20
|
+
flush: function (callback) {
|
|
19
21
|
if (chunk.length) {
|
|
20
22
|
this.push(chunk.splice(0));
|
|
21
23
|
}
|
|
22
|
-
|
|
24
|
+
callback();
|
|
23
25
|
},
|
|
24
26
|
});
|
|
25
27
|
}
|
package/src/stream_hash.js
CHANGED
|
@@ -5,13 +5,13 @@ function stream_hash(algorithm = 'md5', options)
|
|
|
5
5
|
{
|
|
6
6
|
const hash = crypto.createHash(algorithm, options);
|
|
7
7
|
return new stream.Transform({
|
|
8
|
-
transform: function (chunk, encoding,
|
|
8
|
+
transform: function (chunk, encoding, callback) {
|
|
9
9
|
hash.update(chunk);
|
|
10
|
-
|
|
10
|
+
callback();
|
|
11
11
|
},
|
|
12
|
-
flush: function (
|
|
12
|
+
flush: function (callback) {
|
|
13
13
|
this.push(hash.digest('hex'));
|
|
14
|
-
|
|
14
|
+
callback();
|
|
15
15
|
},
|
|
16
16
|
});
|
|
17
17
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transform each item by simple function.
|
|
5
|
+
*/
|
|
6
|
+
function stream_map(fn)
|
|
7
|
+
{
|
|
8
|
+
return new stream.Transform({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
transform: function (item, encoding, callback) {
|
|
11
|
+
try {
|
|
12
|
+
this.push(fn(item));
|
|
13
|
+
callback();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
callback(error);
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = stream_map;
|
package/src/stream_md5.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
const crypto = require('crypto');
|
|
2
2
|
const stream = require('stream');
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* TODO Rename to stream_hash_md5
|
|
6
|
+
*/
|
|
4
7
|
function stream_md5()
|
|
5
8
|
{
|
|
6
9
|
const md5 = crypto.createHash('md5');
|
|
7
10
|
return new stream.Transform({
|
|
8
|
-
transform: function (chunk, encoding,
|
|
11
|
+
transform: function (chunk, encoding, callback) {
|
|
9
12
|
md5.update(chunk, encoding);
|
|
10
|
-
|
|
13
|
+
callback();
|
|
11
14
|
},
|
|
12
|
-
flush: function (
|
|
15
|
+
flush: function (callback) {
|
|
13
16
|
this.push(md5.digest('hex'));
|
|
14
|
-
|
|
17
|
+
callback();
|
|
15
18
|
},
|
|
16
19
|
});
|
|
17
20
|
}
|
package/src/stream_multiplex.js
CHANGED
|
@@ -10,38 +10,38 @@ function stream_multiplex(...streams)
|
|
|
10
10
|
// `new stream.Writable()` will ignore second, third, etc. calls to `destroy`
|
|
11
11
|
return stream.Writable({
|
|
12
12
|
objectMode: true,
|
|
13
|
-
construct: function (
|
|
13
|
+
construct: function (callback) {
|
|
14
14
|
streams.forEach(s => s.once('error', e => this.destroy(e)));
|
|
15
|
-
|
|
15
|
+
callback();
|
|
16
16
|
},
|
|
17
|
-
destroy: async function (error,
|
|
17
|
+
destroy: async function (error, callback) {
|
|
18
18
|
let done = 0;
|
|
19
19
|
streams.forEach(function (stream) {
|
|
20
20
|
stream.destroy(error, function () {
|
|
21
21
|
if (++done === streams.length) {
|
|
22
|
-
|
|
22
|
+
callback();
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
25
|
});
|
|
26
26
|
},
|
|
27
|
-
write: async function (chunk, encoding,
|
|
27
|
+
write: async function (chunk, encoding, callback) {
|
|
28
28
|
let done = 0;
|
|
29
29
|
streams.forEach(function (stream) {
|
|
30
30
|
stream.write(chunk, encoding, function (error) {
|
|
31
31
|
if (++done === streams.length || error) {
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
callback(error);
|
|
33
|
+
callback = ignore;
|
|
34
34
|
}
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
},
|
|
38
|
-
final: async function (
|
|
38
|
+
final: async function (callback) {
|
|
39
39
|
let done = 0;
|
|
40
40
|
streams.forEach(function (stream) {
|
|
41
41
|
stream.end(function (error) {
|
|
42
42
|
if (++done === streams.length || error) {
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
callback(error);
|
|
44
|
+
callback = ignore;
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
});
|
package/src/stream_parse_xml.js
CHANGED
|
@@ -47,13 +47,13 @@ function stream_parse_xml(selector, mapper = stream_parse_xml.guess)
|
|
|
47
47
|
const parser = new htmlparser2.Parser(events, options);
|
|
48
48
|
return out = new stream.Transform({
|
|
49
49
|
objectMode: true,
|
|
50
|
-
transform: function (buf, encoding,
|
|
50
|
+
transform: function (buf, encoding, callback) {
|
|
51
51
|
parser.write(buf.toString());
|
|
52
|
-
|
|
52
|
+
callback();
|
|
53
53
|
},
|
|
54
|
-
flush: function (
|
|
54
|
+
flush: function (callback) {
|
|
55
55
|
parser.end();
|
|
56
|
-
|
|
56
|
+
callback();
|
|
57
57
|
},
|
|
58
58
|
});
|
|
59
59
|
}
|
package/src/stream_progress.js
CHANGED
|
@@ -7,21 +7,28 @@ const stream = require('stream');
|
|
|
7
7
|
*
|
|
8
8
|
* @similar https://www.npmjs.com/package/progress-stream
|
|
9
9
|
*/
|
|
10
|
-
function stream_progress({total, user_friendly_status = s => console.log(s)} = {})
|
|
10
|
+
function stream_progress({objectMode = false, total, user_friendly_status = s => console.log(s)} = {})
|
|
11
11
|
{
|
|
12
12
|
let done = 0;
|
|
13
13
|
const timer = setInterval(tick, 1000);
|
|
14
14
|
const progress = make_progress(total);
|
|
15
15
|
return new stream.Transform({
|
|
16
|
-
|
|
16
|
+
objectMode,
|
|
17
|
+
destroy: function (error, callback) {
|
|
17
18
|
tick();
|
|
18
19
|
clearInterval(timer);
|
|
19
|
-
|
|
20
|
+
callback();
|
|
20
21
|
},
|
|
21
|
-
transform: function (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
transform: function (buffer, encoding, callback) {
|
|
23
|
+
if (objectMode) {
|
|
24
|
+
done++;
|
|
25
|
+
this.push(buffer);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
done += buffer.length;
|
|
29
|
+
this.push(buffer, encoding);
|
|
30
|
+
}
|
|
31
|
+
callback();
|
|
25
32
|
},
|
|
26
33
|
});
|
|
27
34
|
function tick() {
|
package/src/stream_promise.js
CHANGED
package/src/stream_strpbrk.js
CHANGED
|
@@ -8,7 +8,7 @@ function stream_strpbrk(chars = '\r\n')
|
|
|
8
8
|
const pending = [];
|
|
9
9
|
return new stream.Transform({
|
|
10
10
|
objectMode: true,
|
|
11
|
-
transform: function (str, encoding,
|
|
11
|
+
transform: function (str, encoding, callback) {
|
|
12
12
|
str = Buffer.isBuffer(str) ? str.toString() : str;
|
|
13
13
|
|
|
14
14
|
let off = 0;
|
|
@@ -55,7 +55,7 @@ function stream_strpbrk(chars = '\r\n')
|
|
|
55
55
|
if (tmp.length) {
|
|
56
56
|
pending.push(tmp);
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
callback();
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
|
|
@@ -64,13 +64,13 @@ function stream_strpbrk(chars = '\r\n')
|
|
|
64
64
|
this.push(''.concat(...pending.splice(0)));
|
|
65
65
|
off = delims[0];
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
callback();
|
|
68
68
|
},
|
|
69
|
-
flush: function (
|
|
69
|
+
flush: function (callback) {
|
|
70
70
|
if (pending.length) {
|
|
71
71
|
this.push(''.concat(...pending.splice(0)));
|
|
72
72
|
}
|
|
73
|
-
|
|
73
|
+
callback();
|
|
74
74
|
},
|
|
75
75
|
});
|
|
76
76
|
}
|
package/src/stream_through.js
CHANGED
|
@@ -7,14 +7,14 @@ function stream_through(fn)
|
|
|
7
7
|
{
|
|
8
8
|
return new stream.Transform({
|
|
9
9
|
objectMode: true,
|
|
10
|
-
transform: function (item, encoding,
|
|
10
|
+
transform: function (item, encoding, callback) {
|
|
11
11
|
try {
|
|
12
12
|
fn(item);
|
|
13
13
|
this.push(item, encoding);
|
|
14
|
-
|
|
14
|
+
callback();
|
|
15
15
|
}
|
|
16
16
|
catch (error) {
|
|
17
|
-
|
|
17
|
+
callback(error);
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
});
|