@naturalcycles/nodejs-lib 12.99.0 → 12.100.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/dist/index.d.ts CHANGED
@@ -51,6 +51,7 @@ export * from './stream/transform/transformSplit';
51
51
  export * from './stream/transform/transformTap';
52
52
  export * from './stream/transform/transformToArray';
53
53
  export * from './stream/transform/transformToString';
54
+ export * from './stream/transform/transformTee';
54
55
  export * from './stream/transform/worker/baseWorkerClass';
55
56
  export * from './stream/transform/worker/transformMultiThreaded';
56
57
  export * from './stream/transform/worker/transformMultiThreaded.model';
package/dist/index.js CHANGED
@@ -56,6 +56,7 @@ tslib_1.__exportStar(require("./stream/transform/transformSplit"), exports);
56
56
  tslib_1.__exportStar(require("./stream/transform/transformTap"), exports);
57
57
  tslib_1.__exportStar(require("./stream/transform/transformToArray"), exports);
58
58
  tslib_1.__exportStar(require("./stream/transform/transformToString"), exports);
59
+ tslib_1.__exportStar(require("./stream/transform/transformTee"), exports);
59
60
  tslib_1.__exportStar(require("./stream/transform/worker/baseWorkerClass"), exports);
60
61
  tslib_1.__exportStar(require("./stream/transform/worker/transformMultiThreaded"), exports);
61
62
  tslib_1.__exportStar(require("./stream/transform/worker/transformMultiThreaded.model"), exports);
@@ -9,7 +9,7 @@ import { ReadableTyped } from '../stream.model';
9
9
  *
10
10
  * Caution!
11
11
  * The implementation of this Readable is not fully compliant,
12
- * e.g the read() method doesn't return anything, so, it will hand the Node process (or cause it to process.exit(0))
12
+ * e.g the read() method doesn't return anything, so, it will hang the Node process (or cause it to process.exit(0))
13
13
  * if read() will be called AFTER everything was pushed and Readable is closed (by pushing `null`).
14
14
  * Beware of it when e.g doing unit testing! Jest prefers to hang (not exit-0).
15
15
  *
@@ -10,7 +10,7 @@ const node_stream_1 = require("node:stream");
10
10
  *
11
11
  * Caution!
12
12
  * The implementation of this Readable is not fully compliant,
13
- * e.g the read() method doesn't return anything, so, it will hand the Node process (or cause it to process.exit(0))
13
+ * e.g the read() method doesn't return anything, so, it will hang the Node process (or cause it to process.exit(0))
14
14
  * if read() will be called AFTER everything was pushed and Readable is closed (by pushing `null`).
15
15
  * Beware of it when e.g doing unit testing! Jest prefers to hang (not exit-0).
16
16
  *
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import { TransformTyped } from '../stream.model';
3
+ type AnyStream = NodeJS.WritableStream | NodeJS.ReadWriteStream;
4
+ /**
5
+ * Allows to "tee"/"fork" away from the "main pipeline" into the "secondary pipeline".
6
+ *
7
+ * Important, that the main pipeline works "as normal", keeps backpressure, etc.
8
+ * Secondary pipeline DOES NOT keep backpressure.
9
+ * Therefor, the "slowest" pipeline should be made Primary (to keep backpressure).
10
+ *
11
+ * @experimental
12
+ */
13
+ export declare function transformTee<T>(streams: AnyStream[]): TransformTyped<T>;
14
+ export {};
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transformTee = void 0;
4
+ const node_stream_1 = require("node:stream");
5
+ const pipeline_1 = require("../pipeline/pipeline");
6
+ const readableCreate_1 = require("../readable/readableCreate");
7
+ /**
8
+ * Allows to "tee"/"fork" away from the "main pipeline" into the "secondary pipeline".
9
+ *
10
+ * Important, that the main pipeline works "as normal", keeps backpressure, etc.
11
+ * Secondary pipeline DOES NOT keep backpressure.
12
+ * Therefor, the "slowest" pipeline should be made Primary (to keep backpressure).
13
+ *
14
+ * @experimental
15
+ */
16
+ function transformTee(streams) {
17
+ const readable = (0, readableCreate_1.readableCreate)();
18
+ const secondPipelinePromise = (0, pipeline_1._pipeline)([readable, ...streams]);
19
+ return new node_stream_1.Transform({
20
+ objectMode: true,
21
+ transform(chunk, _, cb) {
22
+ // pass to the "secondary" pipeline
23
+ readable.push(chunk);
24
+ // pass through to the "main" pipeline
25
+ cb(null, chunk);
26
+ },
27
+ async final(cb) {
28
+ console.log('transformTee final');
29
+ // Pushing null "closes"/ends the secondary pipeline correctly
30
+ readable.push(null);
31
+ // Second pipeline is expected to finish now, let's await it
32
+ await secondPipelinePromise;
33
+ console.log('transformTee final secondPipeline done');
34
+ // Because second pipeline is done - now we can signal main pipeline to be done as well
35
+ cb();
36
+ },
37
+ });
38
+ }
39
+ exports.transformTee = transformTee;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.99.0",
3
+ "version": "12.100.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
package/src/index.ts CHANGED
@@ -51,6 +51,7 @@ export * from './stream/transform/transformSplit'
51
51
  export * from './stream/transform/transformTap'
52
52
  export * from './stream/transform/transformToArray'
53
53
  export * from './stream/transform/transformToString'
54
+ export * from './stream/transform/transformTee'
54
55
  export * from './stream/transform/worker/baseWorkerClass'
55
56
  export * from './stream/transform/worker/transformMultiThreaded'
56
57
  export * from './stream/transform/worker/transformMultiThreaded.model'
@@ -9,7 +9,7 @@ import { ReadableTyped } from '../stream.model'
9
9
  *
10
10
  * Caution!
11
11
  * The implementation of this Readable is not fully compliant,
12
- * e.g the read() method doesn't return anything, so, it will hand the Node process (or cause it to process.exit(0))
12
+ * e.g the read() method doesn't return anything, so, it will hang the Node process (or cause it to process.exit(0))
13
13
  * if read() will be called AFTER everything was pushed and Readable is closed (by pushing `null`).
14
14
  * Beware of it when e.g doing unit testing! Jest prefers to hang (not exit-0).
15
15
  *
@@ -0,0 +1,45 @@
1
+ import { Transform } from 'node:stream'
2
+ import { _pipeline } from '../pipeline/pipeline'
3
+ import { readableCreate } from '../readable/readableCreate'
4
+ import { TransformTyped } from '../stream.model'
5
+
6
+ type AnyStream = NodeJS.WritableStream | NodeJS.ReadWriteStream
7
+
8
+ /**
9
+ * Allows to "tee"/"fork" away from the "main pipeline" into the "secondary pipeline".
10
+ *
11
+ * Important, that the main pipeline works "as normal", keeps backpressure, etc.
12
+ * Secondary pipeline DOES NOT keep backpressure.
13
+ * Therefor, the "slowest" pipeline should be made Primary (to keep backpressure).
14
+ *
15
+ * @experimental
16
+ */
17
+ export function transformTee<T>(streams: AnyStream[]): TransformTyped<T> {
18
+ const readable = readableCreate<T>()
19
+
20
+ const secondPipelinePromise = _pipeline([readable, ...streams])
21
+
22
+ return new Transform({
23
+ objectMode: true,
24
+ transform(chunk: T, _, cb) {
25
+ // pass to the "secondary" pipeline
26
+ readable.push(chunk)
27
+
28
+ // pass through to the "main" pipeline
29
+ cb(null, chunk)
30
+ },
31
+ async final(cb) {
32
+ console.log('transformTee final')
33
+
34
+ // Pushing null "closes"/ends the secondary pipeline correctly
35
+ readable.push(null)
36
+
37
+ // Second pipeline is expected to finish now, let's await it
38
+ await secondPipelinePromise
39
+ console.log('transformTee final secondPipeline done')
40
+
41
+ // Because second pipeline is done - now we can signal main pipeline to be done as well
42
+ cb()
43
+ },
44
+ })
45
+ }