@naturalcycles/nodejs-lib 12.57.0 → 12.61.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/got/getGot.js +98 -28
- package/dist/got/got.model.d.ts +6 -0
- package/dist/index.d.ts +37 -36
- package/dist/index.js +36 -129
- package/dist/log/log.util.d.ts +4 -0
- package/dist/log/log.util.js +11 -0
- package/dist/stream/ndjson/ndjsonMap.d.ts +2 -2
- package/dist/stream/ndjson/ndjsonMap.js +4 -3
- package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +2 -2
- package/dist/stream/ndjson/transformJsonParse.js +3 -3
- package/dist/stream/ndjson/transformToNDJson.js +2 -2
- package/dist/stream/pipeline/pipeline.d.ts +25 -3
- package/dist/stream/pipeline/pipeline.js +76 -9
- package/dist/stream/readable/readableCreate.d.ts +8 -0
- package/dist/stream/readable/readableCreate.js +9 -1
- package/dist/stream/readable/readableForEach.d.ts +2 -2
- package/dist/stream/readable/readableFromArray.d.ts +2 -2
- package/dist/stream/readable/readableFromArray.js +17 -13
- package/dist/stream/readable/readableMap.d.ts +2 -2
- package/dist/stream/readable/readableMap.js +22 -17
- package/dist/stream/sizeStack.d.ts +9 -0
- package/dist/stream/sizeStack.js +48 -0
- package/dist/stream/stream.util.d.ts +4 -0
- package/dist/stream/stream.util.js +24 -0
- package/dist/stream/transform/transformBuffer.js +1 -1
- package/dist/stream/transform/transformFilter.d.ts +3 -4
- package/dist/stream/transform/transformFilter.js +5 -20
- package/dist/stream/transform/transformLimit.d.ts +32 -1
- package/dist/stream/transform/transformLimit.js +33 -16
- package/dist/stream/transform/transformLogProgress.d.ts +20 -0
- package/dist/stream/transform/transformLogProgress.js +36 -18
- package/dist/stream/transform/transformMap.d.ts +4 -10
- package/dist/stream/transform/transformMap.js +52 -64
- package/dist/stream/transform/transformMapSimple.js +1 -1
- package/dist/stream/transform/transformMapSync.d.ts +5 -3
- package/dist/stream/transform/transformMapSync.js +28 -22
- package/dist/stream/transform/transformNoOp.js +1 -1
- package/dist/stream/transform/transformTap.js +3 -3
- package/dist/stream/transform/transformToArray.js +1 -1
- package/dist/stream/transform/transformToString.js +2 -2
- package/dist/stream/transform/worker/transformMultiThreaded.js +1 -1
- package/dist/stream/transform/worker/workerClassProxy.js +1 -0
- package/dist/stream/writable/writableFork.js +1 -1
- package/dist/stream/writable/writableLimit.d.ts +9 -0
- package/dist/stream/writable/writableLimit.js +29 -0
- package/dist/stream/writable/writablePushToArray.js +1 -1
- package/dist/stream/writable/writableVoid.d.ts +8 -1
- package/dist/stream/writable/writableVoid.js +6 -2
- package/dist/util/zip.util.d.ts +10 -2
- package/dist/util/zip.util.js +10 -3
- package/package.json +1 -1
- package/src/got/getGot.ts +120 -31
- package/src/got/got.model.ts +8 -0
- package/src/index.ts +36 -194
- package/src/log/log.util.ts +9 -0
- package/src/stream/ndjson/ndjsonMap.ts +7 -5
- package/src/stream/ndjson/ndjsonStreamForEach.ts +2 -2
- package/src/stream/ndjson/transformJsonParse.ts +3 -3
- package/src/stream/ndjson/transformToNDJson.ts +2 -2
- package/src/stream/pipeline/pipeline.ts +102 -9
- package/src/stream/readable/readableCreate.ts +9 -1
- package/src/stream/readable/readableForEach.ts +2 -2
- package/src/stream/readable/readableFromArray.ts +18 -21
- package/src/stream/readable/readableMap.ts +24 -21
- package/src/stream/sizeStack.ts +56 -0
- package/src/stream/stream.util.ts +29 -0
- package/src/stream/transform/transformBuffer.ts +1 -1
- package/src/stream/transform/transformFilter.ts +6 -20
- package/src/stream/transform/transformLimit.ts +66 -20
- package/src/stream/transform/transformLogProgress.ts +72 -23
- package/src/stream/transform/transformMap.ts +74 -94
- package/src/stream/transform/transformMapSimple.ts +1 -1
- package/src/stream/transform/transformMapSync.ts +40 -26
- package/src/stream/transform/transformNoOp.ts +1 -1
- package/src/stream/transform/transformTap.ts +3 -3
- package/src/stream/transform/transformToArray.ts +1 -1
- package/src/stream/transform/transformToString.ts +2 -2
- package/src/stream/transform/worker/transformMultiThreaded.ts +1 -1
- package/src/stream/transform/worker/workerClassProxy.js +1 -0
- package/src/stream/writable/writableFork.ts +1 -1
- package/src/stream/writable/writableLimit.ts +28 -0
- package/src/stream/writable/writablePushToArray.ts +1 -1
- package/src/stream/writable/writableVoid.ts +14 -2
- package/src/util/zip.util.ts +11 -3
- package/dist/stream/transform/legacy/transformMap.d.ts +0 -17
- package/dist/stream/transform/legacy/transformMap.js +0 -94
- package/src/stream/transform/legacy/transformMap.ts +0 -133
|
@@ -33,17 +33,17 @@ export function transformJsonParse<OUT = any>(
|
|
|
33
33
|
const { strict = true, reviver } = opt
|
|
34
34
|
|
|
35
35
|
return new Transform({
|
|
36
|
-
|
|
36
|
+
writableObjectMode: false,
|
|
37
37
|
readableObjectMode: true,
|
|
38
|
-
transform(chunk: string,
|
|
38
|
+
transform(chunk: string, _, cb) {
|
|
39
39
|
try {
|
|
40
40
|
const data = JSON.parse(chunk, reviver)
|
|
41
41
|
cb(null, data)
|
|
42
42
|
} catch (err) {
|
|
43
|
-
// console.error(err)
|
|
44
43
|
if (strict) {
|
|
45
44
|
cb(err as Error) // emit error
|
|
46
45
|
} else {
|
|
46
|
+
console.error(err)
|
|
47
47
|
cb() // emit no error, but no result neither
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -37,9 +37,9 @@ export function transformToNDJson<IN = any>(
|
|
|
37
37
|
const { strict = true, separator = '\n', sortObjects = false, useFlatstr = false } = opt
|
|
38
38
|
|
|
39
39
|
return new Transform({
|
|
40
|
-
|
|
40
|
+
writableObjectMode: true,
|
|
41
41
|
readableObjectMode: false,
|
|
42
|
-
transform(chunk: IN,
|
|
42
|
+
transform(chunk: IN, _, cb) {
|
|
43
43
|
try {
|
|
44
44
|
if (sortObjects) {
|
|
45
45
|
chunk = _sortObjectDeep(chunk as any)
|
|
@@ -1,16 +1,109 @@
|
|
|
1
|
-
import { pipeline } from 'stream'
|
|
2
|
-
import {
|
|
1
|
+
import { pipeline, Readable, Transform, Writable } from 'stream'
|
|
2
|
+
import { _last, AnyFunction, DeferredPromise, pDefer } from '@naturalcycles/js-lib'
|
|
3
|
+
import { writablePushToArray } from '../../index'
|
|
3
4
|
|
|
4
5
|
type AnyStream = NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream
|
|
5
6
|
|
|
7
|
+
// /**
|
|
8
|
+
// * Promisified stream.pipeline()
|
|
9
|
+
// */
|
|
10
|
+
// export let _pipeline = promisify(pipeline)
|
|
11
|
+
//
|
|
12
|
+
// // Workaround https://github.com/nodejs/node/issues/40191
|
|
13
|
+
// // todo: remove it when fix is released in 16.x and in AppEngine 16.x
|
|
14
|
+
// if (process.version >= 'v16.10') {
|
|
15
|
+
// const { pipeline } = require('stream/promises')
|
|
16
|
+
// _pipeline = ((streams: AnyStream[]) => pipeline(...streams)) as any
|
|
17
|
+
// }
|
|
18
|
+
|
|
19
|
+
export interface PipelineOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Set to true to allow ERR_STREAM_PREMATURE_CLOSE.
|
|
22
|
+
* Required to support graceful close when using transformLimit
|
|
23
|
+
*/
|
|
24
|
+
allowClose?: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Promisified `stream.pipeline`.
|
|
29
|
+
*
|
|
30
|
+
* Supports opt.allowClose, which allows transformLimit to work (to actually stop source Readable)
|
|
31
|
+
* without throwing an error (ERR_STREAM_PREMATURE_CLOSE).
|
|
32
|
+
*/
|
|
33
|
+
export async function _pipeline(streams: AnyStream[], opt: PipelineOptions = {}): Promise<void> {
|
|
34
|
+
const first = streams[0] as any
|
|
35
|
+
const rest = streams.slice(1)
|
|
36
|
+
|
|
37
|
+
if (opt.allowClose) {
|
|
38
|
+
// Do the magic of making the pipeline "abortable"
|
|
39
|
+
//
|
|
40
|
+
// How does it work:
|
|
41
|
+
// It finds `sourceReadable` (basically, it's just first item in the passed array of streams)
|
|
42
|
+
// Finds last "writable" (last item), patches the `_final` method of it to detect when the whole pipeline is "done",
|
|
43
|
+
// sets the `streamDone` DeferredPromise that resolves when the pipeline is done.
|
|
44
|
+
// Scans through all passed items, finds those that are capable of "closing" the stream
|
|
45
|
+
// (currently its `transformLimit` or `transformMap`)
|
|
46
|
+
// Patches them by attaching `sourceReadable` and `streamDone`.
|
|
47
|
+
// These items (transformLimit and transformMap), when they need to "close the stream" - call `pipelineClose`.
|
|
48
|
+
// `pipelineClose` is the result of 2 sleepless nights of googling and experimentation:)
|
|
49
|
+
// It does:
|
|
50
|
+
// 1. Stops the "downstream" by doing `this.push(null)`.
|
|
51
|
+
// 2. Pauses the `sourceReadable` by calling sourceReadable.unpipe()
|
|
52
|
+
// 3. Waits for `streamDone` to ensure that downstream chunks are fully processed (e.g written to disk).
|
|
53
|
+
// 4. Calls `sourceReadable.destroy()`, which emits ERR_STREAM_PREMATURE_CLOSE
|
|
54
|
+
// 5. _pipeline (this function) catches that specific error and suppresses it (because it's expected and
|
|
55
|
+
// inevitable in this flow). Know a better way to close the stream? Tell me!
|
|
56
|
+
const streamDone = pDefer()
|
|
57
|
+
const sourceReadable = first as Readable
|
|
58
|
+
const last = _last(streams) as Writable
|
|
59
|
+
const lastFinal = last._final?.bind(last) || ((cb: AnyFunction) => cb())
|
|
60
|
+
last._final = cb => {
|
|
61
|
+
lastFinal(() => {
|
|
62
|
+
cb()
|
|
63
|
+
streamDone.resolve()
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
rest.forEach(s => {
|
|
68
|
+
// console.log(s)
|
|
69
|
+
if (s instanceof AbortableTransform || s.constructor.name === 'DestroyableTransform') {
|
|
70
|
+
// console.log(`found ${s.constructor.name}, setting props`)
|
|
71
|
+
;(s as AbortableTransform).sourceReadable = sourceReadable
|
|
72
|
+
;(s as AbortableTransform).streamDone = streamDone
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return new Promise<void>((resolve, reject) => {
|
|
78
|
+
pipeline(first, ...(rest as any[]), (err: Error) => {
|
|
79
|
+
if (err) {
|
|
80
|
+
if (opt.allowClose && (err as any)?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
81
|
+
console.log('_pipeline closed (as expected)')
|
|
82
|
+
return resolve()
|
|
83
|
+
}
|
|
84
|
+
// console.log(`_pipeline error`, err)
|
|
85
|
+
return reject(err)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
resolve()
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
6
93
|
/**
|
|
7
|
-
*
|
|
94
|
+
* Convenience function to make _pipeline collect all items at the end of the stream (should be Transform, not Writeable!)
|
|
95
|
+
* and return.
|
|
8
96
|
*/
|
|
9
|
-
export
|
|
97
|
+
export async function _pipelineToArray<T>(
|
|
98
|
+
streams: AnyStream[],
|
|
99
|
+
opt: PipelineOptions = {},
|
|
100
|
+
): Promise<T[]> {
|
|
101
|
+
const a: T[] = []
|
|
102
|
+
await _pipeline([...streams, writablePushToArray(a)], opt)
|
|
103
|
+
return a
|
|
104
|
+
}
|
|
10
105
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const { pipeline } = require('stream/promises')
|
|
15
|
-
_pipeline = ((streams: AnyStream[]) => pipeline(...streams)) as any
|
|
106
|
+
export class AbortableTransform extends Transform {
|
|
107
|
+
sourceReadable?: Readable
|
|
108
|
+
streamDone?: DeferredPromise
|
|
16
109
|
}
|
|
@@ -6,6 +6,14 @@ import { ReadableTyped } from '../stream.model'
|
|
|
6
6
|
* Push `null` to it to complete (similar to RxJS `.complete()`).
|
|
7
7
|
*
|
|
8
8
|
* Difference from Readable.from() is that this readable is not "finished" yet and allows pushing more to it.
|
|
9
|
+
*
|
|
10
|
+
* Caution!
|
|
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))
|
|
13
|
+
* if read() will be called AFTER everything was pushed and Readable is closed (by pushing `null`).
|
|
14
|
+
* Beware of it when e.g doing unit testing! Jest prefers to hang (not exit-0).
|
|
15
|
+
*
|
|
16
|
+
* @deprecated because of the caution above
|
|
9
17
|
*/
|
|
10
18
|
export function readableCreate<T>(
|
|
11
19
|
items: Iterable<T> = [],
|
|
@@ -14,7 +22,7 @@ export function readableCreate<T>(
|
|
|
14
22
|
const readable = new Readable({
|
|
15
23
|
objectMode: true,
|
|
16
24
|
...opt,
|
|
17
|
-
read() {},
|
|
25
|
+
read() {}, // Caution, if this is called and Readable has not finished yet (null wasn't pushed) - it'll hang the process!
|
|
18
26
|
})
|
|
19
27
|
for (const item of items) {
|
|
20
28
|
readable.push(item)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Mapper, _passNothingPredicate, AbortableAsyncMapper } from '@naturalcycles/js-lib'
|
|
2
2
|
import { ReadableTyped, _pipeline } from '../../index'
|
|
3
3
|
import { transformMap, TransformMapOptions } from '../transform/transformMap'
|
|
4
4
|
|
|
@@ -10,7 +10,7 @@ import { transformMap, TransformMapOptions } from '../transform/transformMap'
|
|
|
10
10
|
*/
|
|
11
11
|
export async function readableForEach<T>(
|
|
12
12
|
readable: ReadableTyped<T>,
|
|
13
|
-
mapper:
|
|
13
|
+
mapper: AbortableAsyncMapper<T, void>,
|
|
14
14
|
opt: TransformMapOptions<T, void> = {},
|
|
15
15
|
): Promise<void> {
|
|
16
16
|
await _pipeline([
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable, ReadableOptions } from 'stream'
|
|
2
|
-
import {
|
|
2
|
+
import { _passthroughMapper, AbortableAsyncMapper } from '@naturalcycles/js-lib'
|
|
3
3
|
import { ReadableTyped } from '../stream.model'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -10,29 +10,26 @@ import { ReadableTyped } from '../stream.model'
|
|
|
10
10
|
*/
|
|
11
11
|
export function readableFromArray<IN, OUT>(
|
|
12
12
|
items: IN[],
|
|
13
|
-
mapper:
|
|
13
|
+
mapper: AbortableAsyncMapper<IN, OUT> = _passthroughMapper,
|
|
14
14
|
opt?: ReadableOptions,
|
|
15
15
|
): ReadableTyped<OUT> {
|
|
16
|
-
|
|
16
|
+
let i = -1
|
|
17
|
+
|
|
18
|
+
return new Readable({
|
|
17
19
|
objectMode: true,
|
|
18
20
|
...opt,
|
|
19
|
-
read() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
async read() {
|
|
22
|
+
i++
|
|
23
|
+
if (i < items.length) {
|
|
24
|
+
try {
|
|
25
|
+
this.push(await mapper(items[i]!, i))
|
|
26
|
+
} catch (err) {
|
|
27
|
+
console.error(err)
|
|
28
|
+
this.destroy(err as Error)
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
this.push(null) // end
|
|
32
|
+
}
|
|
26
33
|
},
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
.then(() => {
|
|
30
|
-
readable.push(null) // done
|
|
31
|
-
})
|
|
32
|
-
.catch(err => {
|
|
33
|
-
console.error(err)
|
|
34
|
-
readable.push(err)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
return readable
|
|
34
|
+
})
|
|
38
35
|
}
|
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Transform } from 'stream'
|
|
2
|
+
import { AbortableAsyncMapper, SKIP } from '@naturalcycles/js-lib'
|
|
3
3
|
import { ReadableTyped } from '../stream.model'
|
|
4
4
|
|
|
5
5
|
export function readableMap<IN, OUT>(
|
|
6
6
|
readable: ReadableTyped<IN>,
|
|
7
|
-
mapper:
|
|
7
|
+
mapper: AbortableAsyncMapper<IN, OUT>,
|
|
8
8
|
): ReadableTyped<OUT> {
|
|
9
|
-
|
|
9
|
+
let i = -1
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
11
|
+
// todo: check if we need to handle errors somehow specifically
|
|
12
|
+
return readable.pipe(
|
|
13
|
+
new Transform({
|
|
14
|
+
objectMode: true,
|
|
15
|
+
async transform(chunk, _enc, cb) {
|
|
16
|
+
try {
|
|
17
|
+
const r = await mapper(chunk, ++i)
|
|
18
|
+
if (r === SKIP) {
|
|
19
|
+
cb()
|
|
20
|
+
} else {
|
|
21
|
+
// _assert(r !== END, `readableMap END not supported`)
|
|
22
|
+
cb(null, r)
|
|
23
|
+
}
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.error(err)
|
|
26
|
+
cb(err as Error)
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
28
31
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { _hb, CommonLogger, NumberStack } from '@naturalcycles/js-lib'
|
|
2
|
+
import { yellow } from '../colors'
|
|
3
|
+
import { gzipBuffer } from '../util/zip.util'
|
|
4
|
+
|
|
5
|
+
export class SizeStack extends NumberStack {
|
|
6
|
+
constructor(public name: string, size: number) {
|
|
7
|
+
super(size)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
total = 0
|
|
11
|
+
|
|
12
|
+
override push(item: any): this {
|
|
13
|
+
this.total += item
|
|
14
|
+
return super.push(item)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getStats(): string {
|
|
18
|
+
// const pcs = this.percentiles([50, 90])
|
|
19
|
+
|
|
20
|
+
return [
|
|
21
|
+
' ' + this.name,
|
|
22
|
+
'avg',
|
|
23
|
+
yellow(_hb(this.avg())),
|
|
24
|
+
// 'p50',
|
|
25
|
+
// yellow(_hb(pcs[50])),
|
|
26
|
+
// 'p90',
|
|
27
|
+
// yellow(_hb(pcs[90])),
|
|
28
|
+
'total',
|
|
29
|
+
yellow(_hb(this.total)),
|
|
30
|
+
].join(' ')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static async countItem(
|
|
34
|
+
item: any,
|
|
35
|
+
logger: CommonLogger,
|
|
36
|
+
sizes?: SizeStack,
|
|
37
|
+
sizesZipped?: SizeStack,
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
if (!sizes) return
|
|
40
|
+
|
|
41
|
+
// try-catch, because we don't want to fail the pipeline on logProgress
|
|
42
|
+
try {
|
|
43
|
+
const buf = Buffer.from(JSON.stringify(item))
|
|
44
|
+
sizes.push(buf.byteLength)
|
|
45
|
+
|
|
46
|
+
if (sizesZipped) {
|
|
47
|
+
const { byteLength } = await gzipBuffer(buf)
|
|
48
|
+
sizesZipped.push(byteLength)
|
|
49
|
+
}
|
|
50
|
+
} catch (err) {
|
|
51
|
+
logger.warn(
|
|
52
|
+
`transformLogProgress failed to JSON.stringify the chunk: ${(err as Error).message}`,
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Readable } from 'stream'
|
|
2
|
+
import { CommonLogger } from '@naturalcycles/js-lib'
|
|
3
|
+
|
|
4
|
+
export function pipelineClose(
|
|
5
|
+
name: string,
|
|
6
|
+
readableDownstream: Readable,
|
|
7
|
+
sourceReadable: Readable | undefined,
|
|
8
|
+
streamDone: Promise<void> | undefined,
|
|
9
|
+
logger: CommonLogger,
|
|
10
|
+
): void {
|
|
11
|
+
readableDownstream.push(null) // this closes the stream, so downstream Readable will receive `end` and won't write anything
|
|
12
|
+
|
|
13
|
+
if (!sourceReadable) {
|
|
14
|
+
logger.warn(`${name} sourceReadable is not provided, readable stream will not be stopped`)
|
|
15
|
+
} else {
|
|
16
|
+
logger.log(`${name} is calling readable.unpipe() to pause the stream`)
|
|
17
|
+
sourceReadable.unpipe() // it is expected to pause the stream
|
|
18
|
+
|
|
19
|
+
if (!streamDone) {
|
|
20
|
+
logger.log(`${name} streamDone is not provided, will do readable.destroy right away`)
|
|
21
|
+
sourceReadable.destroy()
|
|
22
|
+
} else {
|
|
23
|
+
void streamDone.then(() => {
|
|
24
|
+
logger.log(`${name} streamDone, calling readable.destroy()`)
|
|
25
|
+
sourceReadable.destroy() // this throws ERR_STREAM_PREMATURE_CLOSE
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -18,7 +18,7 @@ export function transformBuffer<IN = any>(opt: TransformBufferOptions): Transfor
|
|
|
18
18
|
return new Transform({
|
|
19
19
|
objectMode: true,
|
|
20
20
|
...opt,
|
|
21
|
-
transform(chunk,
|
|
21
|
+
transform(chunk, _, cb) {
|
|
22
22
|
buf.push(chunk)
|
|
23
23
|
|
|
24
24
|
if (buf.length >= batchSize) {
|
|
@@ -1,32 +1,18 @@
|
|
|
1
1
|
import { Transform } from 'stream'
|
|
2
2
|
import { AsyncPredicate, Predicate } from '@naturalcycles/js-lib'
|
|
3
3
|
import { TransformOptions, TransformTyped } from '../stream.model'
|
|
4
|
+
import { transformMap, TransformMapOptions } from './transformMap'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
-
* So, it's recommended to use transformMap instead, that is both concurrent and has
|
|
8
|
-
* filtering feature by default.
|
|
7
|
+
* Just a convenience wrapper around `transformMap` that has built-in predicate filtering support.
|
|
9
8
|
*/
|
|
10
9
|
export function transformFilter<IN = any>(
|
|
11
10
|
predicate: AsyncPredicate<IN>,
|
|
12
|
-
opt:
|
|
11
|
+
opt: TransformMapOptions = {},
|
|
13
12
|
): TransformTyped<IN, IN> {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return new Transform({
|
|
17
|
-
objectMode: true,
|
|
13
|
+
return transformMap(v => v, {
|
|
14
|
+
predicate,
|
|
18
15
|
...opt,
|
|
19
|
-
async transform(chunk: IN, _encoding, cb) {
|
|
20
|
-
try {
|
|
21
|
-
if (await predicate(chunk, index++)) {
|
|
22
|
-
cb(null, chunk) // pass through
|
|
23
|
-
} else {
|
|
24
|
-
cb() // signal that we've finished processing, but emit no output here
|
|
25
|
-
}
|
|
26
|
-
} catch (err) {
|
|
27
|
-
cb(err as Error)
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
16
|
})
|
|
31
17
|
}
|
|
32
18
|
|
|
@@ -42,7 +28,7 @@ export function transformFilterSync<IN = any>(
|
|
|
42
28
|
return new Transform({
|
|
43
29
|
objectMode: true,
|
|
44
30
|
...opt,
|
|
45
|
-
|
|
31
|
+
transform(chunk: IN, _, cb) {
|
|
46
32
|
try {
|
|
47
33
|
if (predicate(chunk, index++)) {
|
|
48
34
|
cb(null, chunk) // pass through
|
|
@@ -1,38 +1,84 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Readable } from 'stream'
|
|
2
2
|
import { CommonLogger } from '@naturalcycles/js-lib'
|
|
3
|
+
import { AbortableTransform, transformNoOp } from '../../index'
|
|
3
4
|
import { TransformOptions, TransformTyped } from '../stream.model'
|
|
5
|
+
import { pipelineClose } from '../stream.util'
|
|
4
6
|
|
|
5
7
|
export interface TransformLimitOptions extends TransformOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Nullish value (e.g 0 or undefined) would mean "no limit"
|
|
10
|
+
*/
|
|
11
|
+
limit?: number
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* If provided (recommended!) - it will call readable.destroy() on limit.
|
|
15
|
+
* Without it - it will only stop the downstream consumers, but won't stop
|
|
16
|
+
* the Readable ("source" of the stream).
|
|
17
|
+
* It is almost always crucial to stop the Source too, so, please provide the Readable here!
|
|
18
|
+
*/
|
|
19
|
+
sourceReadable?: Readable
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Please provide it (a Promise that resolves when the Stream is done, e.g finished consuming things)
|
|
23
|
+
* to be able to wait for Consumers before calling `readable.destroy`.
|
|
24
|
+
* Has no effect if `readable` is not provided.
|
|
25
|
+
*/
|
|
26
|
+
streamDone?: Promise<void>
|
|
27
|
+
|
|
6
28
|
logger?: CommonLogger
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Set to true to enable additional debug messages, e.g it'll log
|
|
32
|
+
* when readable still emits values after the limit is reached.
|
|
33
|
+
*/
|
|
34
|
+
debug?: boolean
|
|
7
35
|
}
|
|
8
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Class only exists to be able to do `instanceof TransformLimit`
|
|
39
|
+
* and to set sourceReadable+streamDone to it in `_pipeline`.
|
|
40
|
+
*/
|
|
41
|
+
export class TransformLimit extends AbortableTransform {}
|
|
42
|
+
|
|
9
43
|
/**
|
|
10
44
|
* 0 or falsy value means "no limit"
|
|
11
45
|
*/
|
|
12
|
-
export function transformLimit<IN>(
|
|
13
|
-
limit
|
|
14
|
-
opt: TransformLimitOptions = {},
|
|
15
|
-
): TransformTyped<IN, IN> {
|
|
16
|
-
const { logger = console } = opt
|
|
17
|
-
let index = 0
|
|
18
|
-
let ended = false
|
|
46
|
+
export function transformLimit<IN>(opt: TransformLimitOptions = {}): TransformTyped<IN, IN> {
|
|
47
|
+
const { logger = console, limit, debug } = opt
|
|
19
48
|
|
|
20
|
-
|
|
49
|
+
if (!limit) {
|
|
50
|
+
// No limit - returning pass-through transform
|
|
51
|
+
return transformNoOp()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let i = 0 // so we start first chunk with 1
|
|
55
|
+
let ended = false
|
|
56
|
+
return new TransformLimit({
|
|
21
57
|
objectMode: true,
|
|
22
58
|
...opt,
|
|
23
|
-
transform(this:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!ended) {
|
|
27
|
-
cb(null, chunk) // pass through the item
|
|
28
|
-
} else {
|
|
29
|
-
cb(null) // pass-through empty
|
|
30
|
-
}
|
|
59
|
+
transform(this: TransformLimit, chunk, _, cb) {
|
|
60
|
+
i++
|
|
31
61
|
|
|
32
|
-
if (
|
|
62
|
+
if (i === limit) {
|
|
33
63
|
ended = true
|
|
34
|
-
logger.log(`transformLimit
|
|
35
|
-
|
|
64
|
+
logger.log(`transformLimit of ${limit} reached`)
|
|
65
|
+
this.push(chunk)
|
|
66
|
+
|
|
67
|
+
pipelineClose(
|
|
68
|
+
'transformLimit',
|
|
69
|
+
this,
|
|
70
|
+
opt.sourceReadable || this.sourceReadable,
|
|
71
|
+
opt.streamDone || this.streamDone,
|
|
72
|
+
logger,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
cb() // after pause
|
|
76
|
+
} else if (!ended) {
|
|
77
|
+
cb(null, chunk)
|
|
78
|
+
} else {
|
|
79
|
+
if (debug) logger.log(`transformLimit.transform after limit`, i)
|
|
80
|
+
// If we ever HANG (don't call cb) - Node will do process.exit(0) to us
|
|
81
|
+
cb() // ended, don't emit anything
|
|
36
82
|
}
|
|
37
83
|
},
|
|
38
84
|
})
|