@vbarbarosh/node-helpers 3.41.0 → 3.42.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_chunk.js +27 -0
- package/src/stream_data_ln.test.js +1 -2
- package/src/stream_each.js +8 -3
- package/src/stream_filter.js +2 -2
- package/src/stream_group.js +4 -20
- package/src/stream_map.js +1 -1
- package/src/stream_map_parallel.js +2 -2
- package/src/stream_progress.js +1 -1
- package/src/stream_strpbrk.js +1 -1
- package/src/stream_tap.js +23 -0
- package/src/stream_through.js +4 -14
- package/src/stream_transform.js +3 -14
- package/src/stream_promise.js +0 -63
- package/src/stream_promise.test.js +0 -14
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Split a stream into chunks of objects.
|
|
5
|
+
*/
|
|
6
|
+
function stream_chunk(chunk_size)
|
|
7
|
+
{
|
|
8
|
+
const chunk = [];
|
|
9
|
+
return new stream.Transform({
|
|
10
|
+
objectMode: true,
|
|
11
|
+
transform: function (item, encoding, callback) {
|
|
12
|
+
chunk.push(item);
|
|
13
|
+
if (chunk.length >= chunk_size) {
|
|
14
|
+
this.push(chunk.splice(0));
|
|
15
|
+
}
|
|
16
|
+
callback();
|
|
17
|
+
},
|
|
18
|
+
flush: function (callback) {
|
|
19
|
+
if (chunk.length) {
|
|
20
|
+
this.push(chunk.splice(0));
|
|
21
|
+
}
|
|
22
|
+
callback();
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = stream_chunk;
|
|
@@ -3,14 +3,13 @@ const fs = require('fs');
|
|
|
3
3
|
const split = require('split');
|
|
4
4
|
const stream = require('stream');
|
|
5
5
|
const stream_data_ln = require('./stream_data_ln');
|
|
6
|
-
const stream_promise = require('./stream_promise');
|
|
7
6
|
|
|
8
7
|
describe('stream_data_ln', function () {
|
|
9
8
|
it('should handle basic input', async function () {
|
|
10
9
|
let actual = '';
|
|
11
10
|
const s = fs.createReadStream(__filename);
|
|
12
11
|
const off = stream_data_ln(s, v => actual += v + '\n');
|
|
13
|
-
await
|
|
12
|
+
await stream.promises.finished(s);
|
|
14
13
|
off();
|
|
15
14
|
assert.deepStrictEqual(actual, fs.readFileSync(__filename, {encoding: 'utf8'}));
|
|
16
15
|
});
|
package/src/stream_each.js
CHANGED
|
@@ -7,9 +7,14 @@ function stream_each(fn)
|
|
|
7
7
|
{
|
|
8
8
|
return stream.Writable({
|
|
9
9
|
objectMode: true,
|
|
10
|
-
write: function (item, encoding, callback) {
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
write: async function (item, encoding, callback) {
|
|
11
|
+
try {
|
|
12
|
+
await fn(item);
|
|
13
|
+
callback();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
callback(error);
|
|
17
|
+
}
|
|
13
18
|
}
|
|
14
19
|
});
|
|
15
20
|
}
|
package/src/stream_filter.js
CHANGED
|
@@ -7,9 +7,9 @@ function stream_filter(fn)
|
|
|
7
7
|
{
|
|
8
8
|
return new stream.Transform({
|
|
9
9
|
objectMode: true,
|
|
10
|
-
transform: function (item, encoding, callback) {
|
|
10
|
+
transform: async function (item, encoding, callback) {
|
|
11
11
|
try {
|
|
12
|
-
if (fn(item)) {
|
|
12
|
+
if (await fn(item)) {
|
|
13
13
|
this.push(item);
|
|
14
14
|
}
|
|
15
15
|
callback();
|
package/src/stream_group.js
CHANGED
|
@@ -1,29 +1,13 @@
|
|
|
1
|
-
const
|
|
1
|
+
const stream_chunk = require('./stream_chunk');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Split a stream into chunks of objects.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* @deprecated Deprecated in favor of `stream_chunk`
|
|
7
7
|
*/
|
|
8
8
|
function stream_group(chunk_size)
|
|
9
9
|
{
|
|
10
|
-
|
|
11
|
-
return new stream.Transform({
|
|
12
|
-
objectMode: true,
|
|
13
|
-
transform: function (item, encoding, callback) {
|
|
14
|
-
chunk.push(item);
|
|
15
|
-
if (chunk.length >= chunk_size) {
|
|
16
|
-
this.push(chunk.splice(0));
|
|
17
|
-
}
|
|
18
|
-
callback();
|
|
19
|
-
},
|
|
20
|
-
flush: function (callback) {
|
|
21
|
-
if (chunk.length) {
|
|
22
|
-
this.push(chunk.splice(0));
|
|
23
|
-
}
|
|
24
|
-
callback();
|
|
25
|
-
},
|
|
26
|
-
});
|
|
10
|
+
return stream_chunk(chunk_size);
|
|
27
11
|
}
|
|
28
12
|
|
|
29
13
|
module.exports = stream_group;
|
package/src/stream_map.js
CHANGED
|
@@ -2,7 +2,7 @@ const Promise = require('bluebird');
|
|
|
2
2
|
const stream = require('stream');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Transform each item
|
|
5
|
+
* Transform each item using a simple function in parallel.
|
|
6
6
|
*/
|
|
7
7
|
function stream_map_parallel({handler, concurrency})
|
|
8
8
|
{
|
|
@@ -58,7 +58,7 @@ function stream_map_parallel({handler, concurrency})
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
function fail(error) {
|
|
61
|
-
|
|
61
|
+
out.destroy(error);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
package/src/stream_progress.js
CHANGED
|
@@ -3,7 +3,7 @@ const make_progress = require('./make_progress');
|
|
|
3
3
|
const stream = require('stream');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Monitor the progress of data through a pipe
|
|
6
|
+
* Monitor the progress of data through a pipe, similar to the UNIX `pv` command.
|
|
7
7
|
*
|
|
8
8
|
* @similar https://www.npmjs.com/package/progress-stream
|
|
9
9
|
*/
|
package/src/stream_strpbrk.js
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const stream = require('stream');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Call `fn` on each item.
|
|
5
|
+
*/
|
|
6
|
+
function stream_tap(fn)
|
|
7
|
+
{
|
|
8
|
+
return new stream.Transform({
|
|
9
|
+
objectMode: true,
|
|
10
|
+
transform: async function (item, encoding, callback) {
|
|
11
|
+
try {
|
|
12
|
+
await fn(item);
|
|
13
|
+
this.push(item, encoding);
|
|
14
|
+
callback();
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
callback(error);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = stream_tap;
|
package/src/stream_through.js
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
const
|
|
1
|
+
const stream_tap = require('./stream_tap');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Call `fn` on each item.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Deprecated in favor of `stream_tap`.
|
|
5
7
|
*/
|
|
6
8
|
function stream_through(fn)
|
|
7
9
|
{
|
|
8
|
-
return
|
|
9
|
-
objectMode: true,
|
|
10
|
-
transform: function (item, encoding, callback) {
|
|
11
|
-
try {
|
|
12
|
-
fn(item);
|
|
13
|
-
this.push(item, encoding);
|
|
14
|
-
callback();
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
callback(error);
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
});
|
|
10
|
+
return stream_tap(fn);
|
|
21
11
|
}
|
|
22
12
|
|
|
23
13
|
module.exports = stream_through;
|
package/src/stream_transform.js
CHANGED
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
const
|
|
1
|
+
const stream_map = require('./stream_map');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @deprecated Deprecated in favor of stream_map
|
|
4
|
+
* @deprecated Deprecated in favor of `stream_map`
|
|
5
5
|
*/
|
|
6
6
|
function stream_transform(fn)
|
|
7
7
|
{
|
|
8
|
-
return
|
|
9
|
-
objectMode: true,
|
|
10
|
-
transform: function (item, encoding, next) {
|
|
11
|
-
try {
|
|
12
|
-
this.push(fn(item));
|
|
13
|
-
next();
|
|
14
|
-
}
|
|
15
|
-
catch (error) {
|
|
16
|
-
next(error);
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
});
|
|
8
|
+
return stream_map(fn);
|
|
20
9
|
}
|
|
21
10
|
|
|
22
11
|
module.exports = stream_transform;
|
package/src/stream_promise.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const {finished} = require('stream/promises');
|
|
2
|
-
|
|
3
|
-
// /**
|
|
4
|
-
// * Wait for a stream to finish with `await stream_promise(stream)`.
|
|
5
|
-
// *
|
|
6
|
-
// * @param stream
|
|
7
|
-
// * @returns {Promise}
|
|
8
|
-
// */
|
|
9
|
-
// function stream_promise(stream)
|
|
10
|
-
// {
|
|
11
|
-
// /**
|
|
12
|
-
// * https://stackoverflow.com/a/34310963/1478566
|
|
13
|
-
// * > end and finish are the same event BUT on different types of
|
|
14
|
-
// * > Streams.
|
|
15
|
-
// * > * stream.Readable fires ONLY end and NEVER finish
|
|
16
|
-
// * > * stream.Writable fires ONLY finish and NEVER end
|
|
17
|
-
// */
|
|
18
|
-
// return new Promise(function (resolve, reject) {
|
|
19
|
-
// stream.once('end', resolve); // Stream.Readable
|
|
20
|
-
// stream.once('finish', resolve); // Stream.Writable
|
|
21
|
-
// stream.once('error', reject);
|
|
22
|
-
// function end() { off(); resolve(); }
|
|
23
|
-
// function finish() { off(); resolve(); }
|
|
24
|
-
// function error() { off(); reject(); }
|
|
25
|
-
// function off() {
|
|
26
|
-
// stream.off('end', end);
|
|
27
|
-
// stream.off('finish', finish);
|
|
28
|
-
// stream.off('error', error);
|
|
29
|
-
// }
|
|
30
|
-
// });
|
|
31
|
-
// }
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Wait for a stream to finish with `await stream_promise(stream)`.
|
|
35
|
-
*
|
|
36
|
-
* https://nodejs.org/api/stream.html#stream_stream_finished_stream_options_callback
|
|
37
|
-
* > Especially useful in error handling scenarios where a stream
|
|
38
|
-
* > is destroyed prematurely (like an aborted HTTP request), and
|
|
39
|
-
* > will not emit 'end' or 'finish'.
|
|
40
|
-
*
|
|
41
|
-
* @param stream
|
|
42
|
-
* @returns {Promise<void>}
|
|
43
|
-
* @deprecated Deprecated in favor of `stream.promises.finished`. Should be removed in the next major release.
|
|
44
|
-
*/
|
|
45
|
-
function stream_promise(stream)
|
|
46
|
-
{
|
|
47
|
-
// https://nodejs.org/api/stream.html#stream_stream_finished_stream_options_callback
|
|
48
|
-
// > stream.finished() leaves dangling event listeners (in particular
|
|
49
|
-
// > 'error', 'end', 'finish' and 'close') after callback has been
|
|
50
|
-
// > invoked. The reason for this is so that unexpected 'error'
|
|
51
|
-
// > events (due to incorrect stream implementations) do not cause
|
|
52
|
-
// > unexpected crashes. If this is unwanted behavior then the
|
|
53
|
-
// > returned cleanup function needs to be invoked in the callback:
|
|
54
|
-
// >
|
|
55
|
-
// > const cleanup = finished(rs, (err) => {
|
|
56
|
-
// > cleanup();
|
|
57
|
-
// > // ...
|
|
58
|
-
// > });
|
|
59
|
-
// >
|
|
60
|
-
return finished(stream);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
module.exports = stream_promise;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const ignore = require('./ignore');
|
|
3
|
-
const stream_promise = require('./stream_promise');
|
|
4
|
-
|
|
5
|
-
describe('stream_promise', function () {
|
|
6
|
-
it('should handle ReadableStream', async function () {
|
|
7
|
-
this.timeout(100);
|
|
8
|
-
await stream_promise(fs.createReadStream(__filename).on('data', ignore));
|
|
9
|
-
});
|
|
10
|
-
it('should handle WritableStream', async function () {
|
|
11
|
-
this.timeout(100);
|
|
12
|
-
await stream_promise(fs.createWriteStream('/dev/null').end('hello\n'));
|
|
13
|
-
});
|
|
14
|
-
});
|