@naturalcycles/nodejs-lib 15.22.0 → 15.23.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/exec2/exec2.js +1 -0
- package/dist/stream/index.d.ts +1 -2
- package/dist/stream/index.js +1 -2
- package/dist/stream/ndjson/ndjsonMap.d.ts +1 -1
- package/dist/stream/ndjson/ndjsonMap.js +13 -15
- package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +2 -2
- package/dist/stream/ndjson/ndjsonStreamForEach.js +9 -15
- package/dist/stream/pipeline.d.ts +79 -0
- package/dist/stream/pipeline.js +220 -0
- package/dist/stream/stream.util.d.ts +1 -3
- package/dist/stream/stream.util.js +1 -20
- package/dist/stream/transform/transformChunk.d.ts +5 -8
- package/dist/stream/transform/transformChunk.js +4 -2
- package/dist/stream/transform/transformFlatten.d.ts +1 -0
- package/dist/stream/transform/transformFlatten.js +15 -4
- package/dist/stream/transform/transformLimit.d.ts +3 -26
- package/dist/stream/transform/transformLimit.js +14 -23
- package/dist/stream/transform/transformMap.d.ts +5 -0
- package/dist/stream/transform/transformMap.js +22 -18
- package/dist/stream/transform/transformMapSync.d.ts +5 -3
- package/dist/stream/transform/transformMapSync.js +7 -8
- package/dist/stream/transform/transformTee.js +4 -2
- package/dist/stream/writable/writableForEach.d.ts +2 -1
- package/dist/stream/writable/writableFork.js +2 -2
- package/package.json +1 -1
- package/src/exec2/exec2.ts +1 -0
- package/src/stream/index.ts +1 -2
- package/src/stream/ndjson/ndjsonMap.ts +12 -22
- package/src/stream/ndjson/ndjsonStreamForEach.ts +8 -15
- package/src/stream/pipeline.ts +301 -0
- package/src/stream/stream.util.ts +1 -29
- package/src/stream/transform/transformChunk.ts +8 -11
- package/src/stream/transform/transformFlatten.ts +16 -4
- package/src/stream/transform/transformLimit.ts +20 -51
- package/src/stream/transform/transformMap.ts +31 -20
- package/src/stream/transform/transformMapSync.ts +14 -8
- package/src/stream/transform/transformTee.ts +5 -2
- package/src/stream/writable/writableForEach.ts +2 -2
- package/src/stream/writable/writableFork.ts +2 -2
- package/dist/stream/pipeline/pipeline.d.ts +0 -36
- package/dist/stream/pipeline/pipeline.js +0 -82
- package/dist/stream/readable/readableForEach.d.ts +0 -19
- package/dist/stream/readable/readableForEach.js +0 -30
- package/src/stream/pipeline/pipeline.ts +0 -114
- package/src/stream/readable/readableForEach.ts +0 -42
|
@@ -1,22 +1,19 @@
|
|
|
1
1
|
import { Transform } from 'node:stream'
|
|
2
|
+
import type { PositiveInteger } from '@naturalcycles/js-lib/types'
|
|
2
3
|
import type { TransformOptions, TransformTyped } from '../stream.model.js'
|
|
3
4
|
|
|
4
|
-
export interface TransformChunkOptions extends TransformOptions {
|
|
5
|
-
/**
|
|
6
|
-
* How many items to include in each chunk.
|
|
7
|
-
* Last chunk will contain the remaining items, possibly less than chunkSize.
|
|
8
|
-
*/
|
|
9
|
-
chunkSize: number
|
|
10
|
-
}
|
|
11
|
-
|
|
12
5
|
/**
|
|
13
6
|
* Similar to RxJS bufferCount(),
|
|
14
7
|
* allows to "chunk" the input stream into chunks of `opt.chunkSize` size.
|
|
15
8
|
* Last chunk will contain the remaining items, possibly less than chunkSize.
|
|
9
|
+
*
|
|
10
|
+
* `chunkSize` indicates how many items to include in each chunk.
|
|
11
|
+
* Last chunk will contain the remaining items, possibly less than chunkSize.
|
|
16
12
|
*/
|
|
17
|
-
export function transformChunk<IN = any>(
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
export function transformChunk<IN = any>(
|
|
14
|
+
chunkSize: PositiveInteger,
|
|
15
|
+
opt?: TransformOptions,
|
|
16
|
+
): TransformTyped<IN, IN[]> {
|
|
20
17
|
let buf: IN[] = []
|
|
21
18
|
|
|
22
19
|
return new Transform({
|
|
@@ -5,13 +5,25 @@ export function transformFlatten<T>(): TransformTyped<T[], T> {
|
|
|
5
5
|
return new Transform({
|
|
6
6
|
objectMode: true,
|
|
7
7
|
transform(chunk: T[], _, cb) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
for (const item of chunk) {
|
|
9
|
+
this.push(item)
|
|
10
|
+
}
|
|
11
|
+
cb() // acknowledge
|
|
12
|
+
},
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function transformFlattenIfNeeded<T>(): TransformTyped<T[], T> {
|
|
17
|
+
return new Transform({
|
|
18
|
+
objectMode: true,
|
|
19
|
+
transform(chunk: T[], _, cb) {
|
|
20
|
+
if (Array.isArray(chunk)) {
|
|
12
21
|
for (const item of chunk) {
|
|
13
22
|
this.push(item)
|
|
14
23
|
}
|
|
24
|
+
} else {
|
|
25
|
+
// As a safety precaution, to not crash the pipeline - push as is
|
|
26
|
+
this.push(chunk)
|
|
15
27
|
}
|
|
16
28
|
cb() // acknowledge
|
|
17
29
|
},
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
import { AbortableTransform } from '../pipeline/pipeline.js'
|
|
1
|
+
import { Transform } from 'node:stream'
|
|
2
|
+
import type { AbortableSignal } from '@naturalcycles/js-lib'
|
|
4
3
|
import type { TransformOptions, TransformTyped } from '../stream.model.js'
|
|
5
|
-
import {
|
|
4
|
+
import { PIPELINE_GRACEFUL_ABORT } from '../stream.util.js'
|
|
6
5
|
import { transformNoOp } from './transformNoOp.js'
|
|
7
6
|
|
|
8
7
|
export interface TransformLimitOptions extends TransformOptions {
|
|
@@ -12,72 +11,42 @@ export interface TransformLimitOptions extends TransformOptions {
|
|
|
12
11
|
limit?: number
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
|
-
*
|
|
16
|
-
* Without it - it will only stop the downstream consumers, but won't stop
|
|
17
|
-
* the Readable ("source" of the stream).
|
|
18
|
-
* It is almost always crucial to stop the Source too, so, please provide the Readable here!
|
|
14
|
+
* Allows to abort (gracefully stop) the stream from inside the Transform.
|
|
19
15
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Please provide it (a Promise that resolves when the Stream is done, e.g finished consuming things)
|
|
24
|
-
* to be able to wait for Consumers before calling `readable.destroy`.
|
|
25
|
-
* Has no effect if `readable` is not provided.
|
|
26
|
-
*/
|
|
27
|
-
streamDone?: Promise<void>
|
|
28
|
-
|
|
29
|
-
logger?: CommonLogger
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Set to true to enable additional debug messages, e.g it'll log
|
|
33
|
-
* when readable still emits values after the limit is reached.
|
|
34
|
-
*/
|
|
35
|
-
debug?: boolean
|
|
16
|
+
signal: AbortableSignal
|
|
36
17
|
}
|
|
37
18
|
|
|
38
|
-
/**
|
|
39
|
-
* Class only exists to be able to do `instanceof TransformLimit`
|
|
40
|
-
* and to set sourceReadable+streamDone to it in `_pipeline`.
|
|
41
|
-
*/
|
|
42
|
-
export class TransformLimit extends AbortableTransform {}
|
|
43
|
-
|
|
44
19
|
export function transformLimit<IN>(opt: TransformLimitOptions): TransformTyped<IN, IN> {
|
|
45
|
-
const {
|
|
20
|
+
const { limit, signal } = opt
|
|
46
21
|
|
|
47
22
|
if (!limit) {
|
|
48
|
-
// No limit - returning pass-through transform
|
|
49
23
|
return transformNoOp()
|
|
50
24
|
}
|
|
51
25
|
|
|
52
26
|
let i = 0 // so we start first chunk with 1
|
|
53
27
|
let ended = false
|
|
54
|
-
return new
|
|
28
|
+
return new Transform({
|
|
55
29
|
objectMode: true,
|
|
56
30
|
...opt,
|
|
57
|
-
transform(
|
|
31
|
+
transform(chunk, _, cb) {
|
|
32
|
+
if (ended) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
58
36
|
i++
|
|
59
37
|
|
|
60
38
|
if (i === limit) {
|
|
61
39
|
ended = true
|
|
62
|
-
logger.log(`transformLimit of ${limit} reached`)
|
|
63
40
|
this.push(chunk)
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
logger,
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
cb() // after pause
|
|
74
|
-
} else if (!ended) {
|
|
75
|
-
cb(null, chunk)
|
|
76
|
-
} else {
|
|
77
|
-
if (debug) logger.log(`transformLimit.transform after limit`, i)
|
|
78
|
-
// If we ever HANG (don't call cb) - Node will do process.exit(0) to us
|
|
79
|
-
cb() // ended, don't emit anything
|
|
41
|
+
this.push(null) // tell downstream that we're done
|
|
42
|
+
cb()
|
|
43
|
+
queueMicrotask(() => {
|
|
44
|
+
signal.abort(new Error(PIPELINE_GRACEFUL_ABORT))
|
|
45
|
+
})
|
|
46
|
+
return
|
|
80
47
|
}
|
|
48
|
+
|
|
49
|
+
cb(null, chunk)
|
|
81
50
|
},
|
|
82
51
|
})
|
|
83
52
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { _hc } from '@naturalcycles/js-lib'
|
|
1
|
+
import { _hc, type AbortableSignal } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _since } from '@naturalcycles/js-lib/datetime/time.util.js'
|
|
3
|
-
import { _anyToError, ErrorMode } from '@naturalcycles/js-lib/error'
|
|
3
|
+
import { _anyToError, _assert, ErrorMode } from '@naturalcycles/js-lib/error'
|
|
4
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
5
5
|
import { _stringify } from '@naturalcycles/js-lib/string/stringify.js'
|
|
6
6
|
import {
|
|
@@ -15,9 +15,8 @@ import {
|
|
|
15
15
|
} from '@naturalcycles/js-lib/types'
|
|
16
16
|
import through2Concurrent from 'through2-concurrent'
|
|
17
17
|
import { yellow } from '../../colors/colors.js'
|
|
18
|
-
import type { AbortableTransform } from '../pipeline/pipeline.js'
|
|
19
18
|
import type { TransformTyped } from '../stream.model.js'
|
|
20
|
-
import {
|
|
19
|
+
import { PIPELINE_GRACEFUL_ABORT } from '../stream.util.js'
|
|
21
20
|
|
|
22
21
|
export interface TransformMapOptions<IN = any, OUT = IN> {
|
|
23
22
|
/**
|
|
@@ -81,6 +80,11 @@ export interface TransformMapOptions<IN = any, OUT = IN> {
|
|
|
81
80
|
metric?: string
|
|
82
81
|
|
|
83
82
|
logger?: CommonLogger
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Allows to abort (gracefully stop) the stream from inside the Transform.
|
|
86
|
+
*/
|
|
87
|
+
signal?: AbortableSignal
|
|
84
88
|
}
|
|
85
89
|
|
|
86
90
|
export interface TransformMapStats {
|
|
@@ -140,12 +144,14 @@ export function transformMap<IN = any, OUT = IN>(
|
|
|
140
144
|
onDone,
|
|
141
145
|
metric = 'stream',
|
|
142
146
|
logger = console,
|
|
147
|
+
signal,
|
|
143
148
|
} = opt
|
|
144
149
|
|
|
145
150
|
const started = Date.now() as UnixTimestampMillis
|
|
146
151
|
let index = -1
|
|
147
152
|
let countOut = 0
|
|
148
153
|
let isSettled = false
|
|
154
|
+
let ok = true
|
|
149
155
|
let errors = 0
|
|
150
156
|
const collectedErrors: Error[] = [] // only used if errorMode == THROW_AGGREGATED
|
|
151
157
|
|
|
@@ -185,7 +191,7 @@ export function transformMap<IN = any, OUT = IN>(
|
|
|
185
191
|
|
|
186
192
|
try {
|
|
187
193
|
await onDone?.({
|
|
188
|
-
ok
|
|
194
|
+
ok,
|
|
189
195
|
collectedErrors,
|
|
190
196
|
countErrors: errors,
|
|
191
197
|
countIn: index + 1,
|
|
@@ -200,7 +206,7 @@ export function transformMap<IN = any, OUT = IN>(
|
|
|
200
206
|
}
|
|
201
207
|
},
|
|
202
208
|
},
|
|
203
|
-
async function transformMapFn(
|
|
209
|
+
async function transformMapFn(chunk: IN, _, cb) {
|
|
204
210
|
// Stop processing if isSettled (either THROW_IMMEDIATELY was fired or END received)
|
|
205
211
|
if (isSettled) return cb()
|
|
206
212
|
|
|
@@ -214,7 +220,8 @@ export function transformMap<IN = any, OUT = IN>(
|
|
|
214
220
|
if (res === END) {
|
|
215
221
|
isSettled = true
|
|
216
222
|
logger.log(`transformMap END received at index ${currentIndex}`)
|
|
217
|
-
|
|
223
|
+
_assert(signal, 'signal is required when using END')
|
|
224
|
+
signal.abort(new Error(PIPELINE_GRACEFUL_ABORT))
|
|
218
225
|
return cb()
|
|
219
226
|
}
|
|
220
227
|
|
|
@@ -243,19 +250,23 @@ export function transformMap<IN = any, OUT = IN>(
|
|
|
243
250
|
|
|
244
251
|
if (errorMode === ErrorMode.THROW_IMMEDIATELY) {
|
|
245
252
|
isSettled = true
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
253
|
+
ok = false
|
|
254
|
+
|
|
255
|
+
// Tests show that onDone is still called at `final` (second time),
|
|
256
|
+
// so, we no longer call it here
|
|
257
|
+
|
|
258
|
+
// try {
|
|
259
|
+
// await onDone?.({
|
|
260
|
+
// ok: false,
|
|
261
|
+
// collectedErrors,
|
|
262
|
+
// countErrors: errors,
|
|
263
|
+
// countIn: index + 1,
|
|
264
|
+
// countOut,
|
|
265
|
+
// started,
|
|
266
|
+
// })
|
|
267
|
+
// } catch (err) {
|
|
268
|
+
// logger.error(err)
|
|
269
|
+
// }
|
|
259
270
|
|
|
260
271
|
return cb(err) // Emit error immediately
|
|
261
272
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Transform } from 'node:stream'
|
|
2
|
+
import type { AbortableSignal } from '@naturalcycles/js-lib'
|
|
3
|
+
import { _anyToError, _assert, ErrorMode } from '@naturalcycles/js-lib/error'
|
|
2
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
3
5
|
import type { IndexedMapper, Predicate, UnixTimestampMillis } from '@naturalcycles/js-lib/types'
|
|
4
6
|
import { END, SKIP } from '@naturalcycles/js-lib/types'
|
|
5
7
|
import { yellow } from '../../colors/colors.js'
|
|
6
|
-
import { AbortableTransform } from '../pipeline/pipeline.js'
|
|
7
8
|
import type { TransformTyped } from '../stream.model.js'
|
|
8
|
-
import {
|
|
9
|
+
import { PIPELINE_GRACEFUL_ABORT } from '../stream.util.js'
|
|
9
10
|
import type { TransformMapStats } from './transformMap.js'
|
|
10
11
|
|
|
11
12
|
export interface TransformMapSyncOptions<IN = any, OUT = IN> {
|
|
@@ -54,9 +55,12 @@ export interface TransformMapSyncOptions<IN = any, OUT = IN> {
|
|
|
54
55
|
metric?: string
|
|
55
56
|
|
|
56
57
|
logger?: CommonLogger
|
|
57
|
-
}
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Allows to abort (gracefully stop) the stream from inside the Transform.
|
|
61
|
+
*/
|
|
62
|
+
signal?: AbortableSignal
|
|
63
|
+
}
|
|
60
64
|
|
|
61
65
|
/**
|
|
62
66
|
* Sync (not async) version of transformMap.
|
|
@@ -74,6 +78,7 @@ export function transformMapSync<IN = any, OUT = IN>(
|
|
|
74
78
|
metric = 'stream',
|
|
75
79
|
objectMode = true,
|
|
76
80
|
logger = console,
|
|
81
|
+
signal,
|
|
77
82
|
} = opt
|
|
78
83
|
|
|
79
84
|
const started = Date.now() as UnixTimestampMillis
|
|
@@ -83,10 +88,10 @@ export function transformMapSync<IN = any, OUT = IN>(
|
|
|
83
88
|
let errors = 0
|
|
84
89
|
const collectedErrors: Error[] = [] // only used if errorMode == THROW_AGGREGATED
|
|
85
90
|
|
|
86
|
-
return new
|
|
91
|
+
return new Transform({
|
|
87
92
|
objectMode,
|
|
88
93
|
...opt,
|
|
89
|
-
transform(
|
|
94
|
+
transform(chunk: IN, _, cb) {
|
|
90
95
|
// Stop processing if isSettled
|
|
91
96
|
if (isSettled) return cb()
|
|
92
97
|
|
|
@@ -99,7 +104,8 @@ export function transformMapSync<IN = any, OUT = IN>(
|
|
|
99
104
|
if (v === END) {
|
|
100
105
|
isSettled = true // will be checked later
|
|
101
106
|
logger.log(`transformMapSync END received at index ${currentIndex}`)
|
|
102
|
-
|
|
107
|
+
_assert(signal, 'signal is required when using END')
|
|
108
|
+
signal.abort(new Error(PIPELINE_GRACEFUL_ABORT))
|
|
103
109
|
return cb()
|
|
104
110
|
}
|
|
105
111
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Transform } from 'node:stream'
|
|
2
|
-
import {
|
|
2
|
+
import { pipeline } from 'node:stream/promises'
|
|
3
3
|
import { readableCreate } from '../readable/readableCreate.js'
|
|
4
4
|
import type { TransformTyped } from '../stream.model.js'
|
|
5
5
|
|
|
@@ -17,11 +17,14 @@ type AnyStream = NodeJS.WritableStream | NodeJS.ReadWriteStream
|
|
|
17
17
|
export function transformTee<T>(streams: AnyStream[]): TransformTyped<T, T> {
|
|
18
18
|
const readable = readableCreate<T>()
|
|
19
19
|
|
|
20
|
-
const secondPipelinePromise =
|
|
20
|
+
const secondPipelinePromise = pipeline([readable, ...streams])
|
|
21
21
|
|
|
22
22
|
return new Transform({
|
|
23
23
|
objectMode: true,
|
|
24
24
|
transform(chunk: T, _, cb) {
|
|
25
|
+
// todo: it's possible to start respecting backpressure,
|
|
26
|
+
// if we start to listen to the boolean output of .push()
|
|
27
|
+
|
|
25
28
|
// pass to the "secondary" pipeline
|
|
26
29
|
readable.push(chunk)
|
|
27
30
|
|
|
@@ -2,7 +2,7 @@ import type { AsyncIndexedMapper, IndexedMapper } from '@naturalcycles/js-lib/ty
|
|
|
2
2
|
import { _passNothingPredicate } from '@naturalcycles/js-lib/types'
|
|
3
3
|
import type { WritableTyped } from '../stream.model.js'
|
|
4
4
|
import { transformMap, type TransformMapOptions } from '../transform/transformMap.js'
|
|
5
|
-
import { transformMapSync } from '../transform/transformMapSync.js'
|
|
5
|
+
import { transformMapSync, type TransformMapSyncOptions } from '../transform/transformMapSync.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Just an alias to transformMap that declares OUT as void.
|
|
@@ -19,7 +19,7 @@ export function writableForEach<IN = any>(
|
|
|
19
19
|
*/
|
|
20
20
|
export function writableForEachSync<IN = any>(
|
|
21
21
|
mapper: IndexedMapper<IN, void>,
|
|
22
|
-
opt:
|
|
22
|
+
opt: TransformMapSyncOptions<IN, void> = {},
|
|
23
23
|
): WritableTyped<IN> {
|
|
24
24
|
return transformMapSync<IN, void>(mapper, { ...opt, predicate: _passNothingPredicate })
|
|
25
25
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Writable } from 'node:stream'
|
|
2
|
-
import {
|
|
2
|
+
import { pipeline } from 'node:stream/promises'
|
|
3
3
|
import { readableCreate } from '../readable/readableCreate.js'
|
|
4
4
|
import type { ReadableTyped, TransformOptions, WritableTyped } from '../stream.model.js'
|
|
5
5
|
|
|
@@ -22,7 +22,7 @@ export function writableFork<T>(
|
|
|
22
22
|
const readable = readableCreate<T>()
|
|
23
23
|
readables.push(readable)
|
|
24
24
|
|
|
25
|
-
return await
|
|
25
|
+
return await pipeline([readable, ...chain])
|
|
26
26
|
}),
|
|
27
27
|
).catch(err => {
|
|
28
28
|
console.error(err) // ensure the error is logged
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { Readable } from 'node:stream';
|
|
2
|
-
import { Transform } from 'node:stream';
|
|
3
|
-
import type { DeferredPromise } from '@naturalcycles/js-lib/promise';
|
|
4
|
-
type AnyStream = NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream;
|
|
5
|
-
export interface PipelineOptions {
|
|
6
|
-
/**
|
|
7
|
-
* Set to true to allow ERR_STREAM_PREMATURE_CLOSE.
|
|
8
|
-
* Required to support graceful close when using transformLimit
|
|
9
|
-
*/
|
|
10
|
-
allowClose?: boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Set to true to allow graceful abort (via AbortSignal).
|
|
13
|
-
* "Graceful" means it'll swallow the AbortError and let the pipeline resolve normally.
|
|
14
|
-
*
|
|
15
|
-
* Default is false - AbortError will be thrown.
|
|
16
|
-
*/
|
|
17
|
-
allowGracefulAbort?: boolean;
|
|
18
|
-
signal?: AbortSignal;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Promisified `stream.pipeline`.
|
|
22
|
-
*
|
|
23
|
-
* Supports opt.allowClose, which allows transformLimit to work (to actually stop source Readable)
|
|
24
|
-
* without throwing an error (ERR_STREAM_PREMATURE_CLOSE).
|
|
25
|
-
*/
|
|
26
|
-
export declare function _pipeline(streams: AnyStream[], opt?: PipelineOptions): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* Convenience function to make _pipeline collect all items at the end of the stream (should be Transform, not Writeable!)
|
|
29
|
-
* and return.
|
|
30
|
-
*/
|
|
31
|
-
export declare function _pipelineToArray<T>(streams: AnyStream[], opt?: PipelineOptions): Promise<T[]>;
|
|
32
|
-
export declare class AbortableTransform extends Transform {
|
|
33
|
-
sourceReadable?: Readable;
|
|
34
|
-
streamDone?: DeferredPromise;
|
|
35
|
-
}
|
|
36
|
-
export {};
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Transform } from 'node:stream';
|
|
2
|
-
import { pipeline } from 'node:stream/promises';
|
|
3
|
-
import { _last } from '@naturalcycles/js-lib/array/array.util.js';
|
|
4
|
-
import { pDefer } from '@naturalcycles/js-lib/promise/pDefer.js';
|
|
5
|
-
import { writablePushToArray } from '../writable/writablePushToArray.js';
|
|
6
|
-
/**
|
|
7
|
-
* Promisified `stream.pipeline`.
|
|
8
|
-
*
|
|
9
|
-
* Supports opt.allowClose, which allows transformLimit to work (to actually stop source Readable)
|
|
10
|
-
* without throwing an error (ERR_STREAM_PREMATURE_CLOSE).
|
|
11
|
-
*/
|
|
12
|
-
export async function _pipeline(streams, opt = {}) {
|
|
13
|
-
const first = streams[0];
|
|
14
|
-
const rest = streams.slice(1);
|
|
15
|
-
if (opt.allowClose) {
|
|
16
|
-
// Do the magic of making the pipeline "abortable"
|
|
17
|
-
//
|
|
18
|
-
// How does it work:
|
|
19
|
-
// It finds `sourceReadable` (basically, it's just first item in the passed array of streams)
|
|
20
|
-
// Finds last "writable" (last item), patches the `_final` method of it to detect when the whole pipeline is "done",
|
|
21
|
-
// sets the `streamDone` DeferredPromise that resolves when the pipeline is done.
|
|
22
|
-
// Scans through all passed items, finds those that are capable of "closing" the stream
|
|
23
|
-
// (currently its `transformLimit` or `transformMap`)
|
|
24
|
-
// Patches them by attaching `sourceReadable` and `streamDone`.
|
|
25
|
-
// These items (transformLimit and transformMap), when they need to "close the stream" - call `pipelineClose`.
|
|
26
|
-
// `pipelineClose` is the result of 2 sleepless nights of googling and experimentation:)
|
|
27
|
-
// It does:
|
|
28
|
-
// 1. Stops the "downstream" by doing `this.push(null)`.
|
|
29
|
-
// 2. Pauses the `sourceReadable` by calling sourceReadable.unpipe()
|
|
30
|
-
// 3. Waits for `streamDone` to ensure that downstream chunks are fully processed (e.g written to disk).
|
|
31
|
-
// 4. Calls `sourceReadable.destroy()`, which emits ERR_STREAM_PREMATURE_CLOSE
|
|
32
|
-
// 5. _pipeline (this function) catches that specific error and suppresses it (because it's expected and
|
|
33
|
-
// inevitable in this flow). Know a better way to close the stream? Tell me!
|
|
34
|
-
const streamDone = pDefer();
|
|
35
|
-
const sourceReadable = first;
|
|
36
|
-
const last = _last(streams);
|
|
37
|
-
const lastFinal = last._final?.bind(last) || ((cb) => cb());
|
|
38
|
-
last._final = cb => {
|
|
39
|
-
lastFinal(() => {
|
|
40
|
-
cb();
|
|
41
|
-
streamDone.resolve();
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
rest.forEach(s => {
|
|
45
|
-
// console.log(s)
|
|
46
|
-
if (s instanceof AbortableTransform || s.constructor.name === 'AbortableTransform') {
|
|
47
|
-
// console.log(`found ${s.constructor.name}, setting props`)
|
|
48
|
-
;
|
|
49
|
-
s.sourceReadable = sourceReadable;
|
|
50
|
-
s.streamDone = streamDone;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
return await pipeline([first, ...rest], opt);
|
|
56
|
-
}
|
|
57
|
-
catch (err) {
|
|
58
|
-
if (opt.allowClose && err?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
59
|
-
console.log('_pipeline closed (as expected)');
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (opt.allowGracefulAbort && err?.name === 'AbortError') {
|
|
63
|
-
console.log('_pipeline closed via AbortSignal (as expected)');
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
// console.log(`_pipeline error`, err)
|
|
67
|
-
throw err;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Convenience function to make _pipeline collect all items at the end of the stream (should be Transform, not Writeable!)
|
|
72
|
-
* and return.
|
|
73
|
-
*/
|
|
74
|
-
export async function _pipelineToArray(streams, opt = {}) {
|
|
75
|
-
const a = [];
|
|
76
|
-
await _pipeline([...streams, writablePushToArray(a)], opt);
|
|
77
|
-
return a;
|
|
78
|
-
}
|
|
79
|
-
export class AbortableTransform extends Transform {
|
|
80
|
-
sourceReadable;
|
|
81
|
-
streamDone;
|
|
82
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { AbortableAsyncMapper, IndexedMapper } from '@naturalcycles/js-lib/types';
|
|
2
|
-
import type { ReadableTyped } from '../stream.model.js';
|
|
3
|
-
import type { TransformMapOptions } from '../transform/transformMap.js';
|
|
4
|
-
/**
|
|
5
|
-
* Convenience function to do `.forEach` over a Readable.
|
|
6
|
-
* Typed! (unlike default Readable).
|
|
7
|
-
*
|
|
8
|
-
* Try native readable.forEach() instead!
|
|
9
|
-
*
|
|
10
|
-
* @experimental
|
|
11
|
-
*/
|
|
12
|
-
export declare function readableForEach<T>(readable: ReadableTyped<T>, mapper: AbortableAsyncMapper<T, void>, opt?: TransformMapOptions<T, void>): Promise<void>;
|
|
13
|
-
/**
|
|
14
|
-
* Convenience function to do `.forEach` over a Readable.
|
|
15
|
-
* Typed! (unlike default Readable).
|
|
16
|
-
*
|
|
17
|
-
* @experimental
|
|
18
|
-
*/
|
|
19
|
-
export declare function readableForEachSync<T>(readable: ReadableTyped<T>, mapper: IndexedMapper<T, void>): Promise<void>;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { _passNothingPredicate } from '@naturalcycles/js-lib/types';
|
|
2
|
-
import { _pipeline } from '../pipeline/pipeline.js';
|
|
3
|
-
import { transformMap } from '../transform/transformMap.js';
|
|
4
|
-
/**
|
|
5
|
-
* Convenience function to do `.forEach` over a Readable.
|
|
6
|
-
* Typed! (unlike default Readable).
|
|
7
|
-
*
|
|
8
|
-
* Try native readable.forEach() instead!
|
|
9
|
-
*
|
|
10
|
-
* @experimental
|
|
11
|
-
*/
|
|
12
|
-
export async function readableForEach(readable, mapper, opt = {}) {
|
|
13
|
-
await _pipeline([
|
|
14
|
-
readable,
|
|
15
|
-
transformMap(mapper, { ...opt, predicate: _passNothingPredicate }),
|
|
16
|
-
]);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Convenience function to do `.forEach` over a Readable.
|
|
20
|
-
* Typed! (unlike default Readable).
|
|
21
|
-
*
|
|
22
|
-
* @experimental
|
|
23
|
-
*/
|
|
24
|
-
export async function readableForEachSync(readable, mapper) {
|
|
25
|
-
// async iteration
|
|
26
|
-
let index = 0;
|
|
27
|
-
for await (const item of readable) {
|
|
28
|
-
mapper(item, index++);
|
|
29
|
-
}
|
|
30
|
-
}
|