@vbarbarosh/node-helpers 3.45.0 → 3.47.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/README.md +2 -0
- package/package.json +1 -1
- package/src/stream_map_flatten.js +24 -0
- package/src/stream_progress.js +3 -1
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pass each item through a user-defined generator, which yields each item as it is passed down
|
|
5
|
+
*/
|
|
6
|
+
function stream_map_flatten(fn)
|
|
7
|
+
{
|
|
8
|
+
return new stream.Transform({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
transform: async function (item, encoding, callback) {
|
|
11
|
+
try {
|
|
12
|
+
for await (const chunk of fn(item)) {
|
|
13
|
+
this.push(chunk);
|
|
14
|
+
}
|
|
15
|
+
callback();
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
callback(error);
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = stream_map_flatten;
|
package/src/stream_progress.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const format_progress_bytes = require('./format_progress_bytes');
|
|
2
|
+
const format_progress_kilo = require('./format_progress_kilo');
|
|
2
3
|
const make_progress = require('./make_progress');
|
|
3
4
|
const stream = require('stream');
|
|
4
5
|
|
|
@@ -12,6 +13,7 @@ function stream_progress({objectMode = false, total, user_friendly_status = s =>
|
|
|
12
13
|
let done = 0;
|
|
13
14
|
const timer = setInterval(tick, 1000);
|
|
14
15
|
const progress = make_progress(total);
|
|
16
|
+
const format_progress = objectMode ? format_progress_kilo : format_progress_bytes;
|
|
15
17
|
return new stream.Transform({
|
|
16
18
|
objectMode,
|
|
17
19
|
destroy: function (error, callback) {
|
|
@@ -33,7 +35,7 @@ function stream_progress({objectMode = false, total, user_friendly_status = s =>
|
|
|
33
35
|
});
|
|
34
36
|
function tick() {
|
|
35
37
|
progress.update(done);
|
|
36
|
-
user_friendly_status(
|
|
38
|
+
user_friendly_status(format_progress(progress));
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
41
|
|