@naturalcycles/nodejs-lib 15.36.0 → 15.37.1
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/stream/index.d.ts +1 -0
- package/dist/stream/index.js +1 -0
- package/dist/stream/transform/transformMultiFork.d.ts +14 -0
- package/dist/stream/transform/transformMultiFork.js +79 -0
- package/package.json +1 -1
- package/src/stream/index.ts +1 -0
- package/src/stream/transform/transformMultiFork.ts +97 -0
package/dist/stream/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export * from './transform/transformLogProgress.js';
|
|
|
18
18
|
export * from './transform/transformMap.js';
|
|
19
19
|
export * from './transform/transformMapSimple.js';
|
|
20
20
|
export * from './transform/transformMapSync.js';
|
|
21
|
+
export * from './transform/transformMultiFork.js';
|
|
21
22
|
export * from './transform/transformNoOp.js';
|
|
22
23
|
export * from './transform/transformOffset.js';
|
|
23
24
|
export * from './transform/transformSplit.js';
|
package/dist/stream/index.js
CHANGED
|
@@ -18,6 +18,7 @@ export * from './transform/transformLogProgress.js';
|
|
|
18
18
|
export * from './transform/transformMap.js';
|
|
19
19
|
export * from './transform/transformMapSimple.js';
|
|
20
20
|
export * from './transform/transformMapSync.js';
|
|
21
|
+
export * from './transform/transformMultiFork.js';
|
|
21
22
|
export * from './transform/transformNoOp.js';
|
|
22
23
|
export * from './transform/transformOffset.js';
|
|
23
24
|
export * from './transform/transformSplit.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { NonNegativeInteger, Predicate } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import { Pipeline } from '../pipeline.js';
|
|
3
|
+
import type { TransformOptions, TransformTyped } from '../stream.model.js';
|
|
4
|
+
/**
|
|
5
|
+
* Like transformFork, but allows to fork multiple times,
|
|
6
|
+
* aka "split the stream" into chunks, and attach a Pipeline to
|
|
7
|
+
* each of the chunks.
|
|
8
|
+
*
|
|
9
|
+
* Example use case: you want to write to Cloud Storage, 1000 rows per file,
|
|
10
|
+
* each file needs its own destination Pipeline.
|
|
11
|
+
*
|
|
12
|
+
* @experimental
|
|
13
|
+
*/
|
|
14
|
+
export declare function transformMultiFork<T>(splitPredicate: Predicate<T>, fn: (pipeline: Pipeline<T>, splitIndex: NonNegativeInteger) => Promise<void>, opt?: TransformOptions): TransformTyped<T, T>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log';
|
|
3
|
+
import { pDefer } from '@naturalcycles/js-lib/promise/pDefer.js';
|
|
4
|
+
import { Pipeline } from '../pipeline.js';
|
|
5
|
+
import { createReadable } from '../readable/createReadable.js';
|
|
6
|
+
/**
|
|
7
|
+
* Like transformFork, but allows to fork multiple times,
|
|
8
|
+
* aka "split the stream" into chunks, and attach a Pipeline to
|
|
9
|
+
* each of the chunks.
|
|
10
|
+
*
|
|
11
|
+
* Example use case: you want to write to Cloud Storage, 1000 rows per file,
|
|
12
|
+
* each file needs its own destination Pipeline.
|
|
13
|
+
*
|
|
14
|
+
* @experimental
|
|
15
|
+
*/
|
|
16
|
+
export function transformMultiFork(splitPredicate, fn, opt = {}) {
|
|
17
|
+
const { objectMode = true, highWaterMark } = opt;
|
|
18
|
+
const logger = createCommonLoggerAtLevel(opt.logger, opt.logLevel);
|
|
19
|
+
let indexWritten = 0;
|
|
20
|
+
let splitIndex = 0;
|
|
21
|
+
let lock;
|
|
22
|
+
let fork = createNewFork();
|
|
23
|
+
return new Transform({
|
|
24
|
+
objectMode,
|
|
25
|
+
highWaterMark,
|
|
26
|
+
async transform(chunk, _, cb) {
|
|
27
|
+
// pass through to the "main" pipeline
|
|
28
|
+
// Main pipeline should handle backpressure "automatically",
|
|
29
|
+
// so, we're not maintaining a Lock for it
|
|
30
|
+
this.push(chunk);
|
|
31
|
+
if (lock) {
|
|
32
|
+
// Forked pipeline is locked - let's wait for it to call _read
|
|
33
|
+
await lock;
|
|
34
|
+
// lock is undefined at this point
|
|
35
|
+
}
|
|
36
|
+
// pass to the "forked" pipeline
|
|
37
|
+
const shouldContinue = fork.push(chunk);
|
|
38
|
+
if (!shouldContinue && !lock) {
|
|
39
|
+
// Forked pipeline indicates that we should Pause
|
|
40
|
+
lock = pDefer();
|
|
41
|
+
logger.debug(`TransformMultiFork(${splitIndex}): pause`);
|
|
42
|
+
}
|
|
43
|
+
if (splitPredicate(chunk, ++indexWritten)) {
|
|
44
|
+
logger.log(`TransformMultiFork(${splitIndex}): splitting to ${splitIndex + 1}`);
|
|
45
|
+
splitIndex++;
|
|
46
|
+
fork.push(null);
|
|
47
|
+
lock?.resolve();
|
|
48
|
+
lock = undefined;
|
|
49
|
+
fork = createNewFork();
|
|
50
|
+
}
|
|
51
|
+
// acknowledge that we've finished processing the input chunk
|
|
52
|
+
cb();
|
|
53
|
+
},
|
|
54
|
+
async final(cb) {
|
|
55
|
+
logger.log(`TransformMultiFork: final`);
|
|
56
|
+
// Pushing null "closes"/ends the secondary pipeline correctly
|
|
57
|
+
fork.push(null);
|
|
58
|
+
// Acknowledge that we've received `null` and passed it through to the fork
|
|
59
|
+
cb();
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
function createNewFork() {
|
|
63
|
+
const currentSplitIndex = splitIndex;
|
|
64
|
+
const readable = createReadable([], {}, () => {
|
|
65
|
+
// `_read` is called
|
|
66
|
+
if (!lock)
|
|
67
|
+
return;
|
|
68
|
+
// We had a lock - let's Resume
|
|
69
|
+
logger.debug(`TransformMultiFork(${currentSplitIndex}): resume`);
|
|
70
|
+
const lockCopy = lock;
|
|
71
|
+
lock = undefined;
|
|
72
|
+
lockCopy.resolve();
|
|
73
|
+
});
|
|
74
|
+
void fn(Pipeline.from(readable), currentSplitIndex).then(() => {
|
|
75
|
+
logger.log(`TransformMultiFork(${currentSplitIndex}): done`);
|
|
76
|
+
});
|
|
77
|
+
return readable;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/package.json
CHANGED
package/src/stream/index.ts
CHANGED
|
@@ -18,6 +18,7 @@ export * from './transform/transformLogProgress.js'
|
|
|
18
18
|
export * from './transform/transformMap.js'
|
|
19
19
|
export * from './transform/transformMapSimple.js'
|
|
20
20
|
export * from './transform/transformMapSync.js'
|
|
21
|
+
export * from './transform/transformMultiFork.js'
|
|
21
22
|
export * from './transform/transformNoOp.js'
|
|
22
23
|
export * from './transform/transformOffset.js'
|
|
23
24
|
export * from './transform/transformSplit.js'
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Transform } from 'node:stream'
|
|
2
|
+
import { createCommonLoggerAtLevel } from '@naturalcycles/js-lib/log'
|
|
3
|
+
import { type DeferredPromise, pDefer } from '@naturalcycles/js-lib/promise/pDefer.js'
|
|
4
|
+
import type { NonNegativeInteger, Predicate } from '@naturalcycles/js-lib/types'
|
|
5
|
+
import { Pipeline } from '../pipeline.js'
|
|
6
|
+
import { createReadable } from '../readable/createReadable.js'
|
|
7
|
+
import type { ReadableTyped, TransformOptions, TransformTyped } from '../stream.model.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Like transformFork, but allows to fork multiple times,
|
|
11
|
+
* aka "split the stream" into chunks, and attach a Pipeline to
|
|
12
|
+
* each of the chunks.
|
|
13
|
+
*
|
|
14
|
+
* Example use case: you want to write to Cloud Storage, 1000 rows per file,
|
|
15
|
+
* each file needs its own destination Pipeline.
|
|
16
|
+
*
|
|
17
|
+
* @experimental
|
|
18
|
+
*/
|
|
19
|
+
export function transformMultiFork<T>(
|
|
20
|
+
splitPredicate: Predicate<T>,
|
|
21
|
+
fn: (pipeline: Pipeline<T>, splitIndex: NonNegativeInteger) => Promise<void>,
|
|
22
|
+
opt: TransformOptions = {},
|
|
23
|
+
): TransformTyped<T, T> {
|
|
24
|
+
const { objectMode = true, highWaterMark } = opt
|
|
25
|
+
const logger = createCommonLoggerAtLevel(opt.logger, opt.logLevel)
|
|
26
|
+
let indexWritten = 0
|
|
27
|
+
let splitIndex = 0
|
|
28
|
+
|
|
29
|
+
let lock: DeferredPromise | undefined
|
|
30
|
+
let fork = createNewFork()
|
|
31
|
+
|
|
32
|
+
return new Transform({
|
|
33
|
+
objectMode,
|
|
34
|
+
highWaterMark,
|
|
35
|
+
async transform(chunk: T, _, cb) {
|
|
36
|
+
// pass through to the "main" pipeline
|
|
37
|
+
// Main pipeline should handle backpressure "automatically",
|
|
38
|
+
// so, we're not maintaining a Lock for it
|
|
39
|
+
this.push(chunk)
|
|
40
|
+
|
|
41
|
+
if (lock) {
|
|
42
|
+
// Forked pipeline is locked - let's wait for it to call _read
|
|
43
|
+
await lock
|
|
44
|
+
// lock is undefined at this point
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// pass to the "forked" pipeline
|
|
48
|
+
const shouldContinue = fork.push(chunk)
|
|
49
|
+
if (!shouldContinue && !lock) {
|
|
50
|
+
// Forked pipeline indicates that we should Pause
|
|
51
|
+
lock = pDefer()
|
|
52
|
+
logger.debug(`TransformMultiFork(${splitIndex}): pause`)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (splitPredicate(chunk, ++indexWritten)) {
|
|
56
|
+
logger.log(`TransformMultiFork(${splitIndex}): splitting to ${splitIndex + 1}`)
|
|
57
|
+
splitIndex++
|
|
58
|
+
fork.push(null)
|
|
59
|
+
lock?.resolve()
|
|
60
|
+
lock = undefined
|
|
61
|
+
fork = createNewFork()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// acknowledge that we've finished processing the input chunk
|
|
65
|
+
cb()
|
|
66
|
+
},
|
|
67
|
+
async final(cb) {
|
|
68
|
+
logger.log(`TransformMultiFork: final`)
|
|
69
|
+
|
|
70
|
+
// Pushing null "closes"/ends the secondary pipeline correctly
|
|
71
|
+
fork.push(null)
|
|
72
|
+
|
|
73
|
+
// Acknowledge that we've received `null` and passed it through to the fork
|
|
74
|
+
cb()
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
function createNewFork(): ReadableTyped<T> {
|
|
79
|
+
const currentSplitIndex = splitIndex
|
|
80
|
+
|
|
81
|
+
const readable = createReadable<T>([], {}, () => {
|
|
82
|
+
// `_read` is called
|
|
83
|
+
if (!lock) return
|
|
84
|
+
// We had a lock - let's Resume
|
|
85
|
+
logger.debug(`TransformMultiFork(${currentSplitIndex}): resume`)
|
|
86
|
+
const lockCopy = lock
|
|
87
|
+
lock = undefined
|
|
88
|
+
lockCopy.resolve()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
void fn(Pipeline.from<T>(readable), currentSplitIndex).then(() => {
|
|
92
|
+
logger.log(`TransformMultiFork(${currentSplitIndex}): done`)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
return readable
|
|
96
|
+
}
|
|
97
|
+
}
|