@naturalcycles/nodejs-lib 12.58.0 → 12.62.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.
Files changed (87) hide show
  1. package/dist/got/getGot.js +98 -28
  2. package/dist/got/got.model.d.ts +6 -0
  3. package/dist/index.d.ts +37 -36
  4. package/dist/index.js +36 -129
  5. package/dist/log/log.util.d.ts +4 -0
  6. package/dist/log/log.util.js +11 -0
  7. package/dist/stream/ndjson/ndjsonMap.d.ts +2 -2
  8. package/dist/stream/ndjson/ndjsonMap.js +4 -3
  9. package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +2 -2
  10. package/dist/stream/ndjson/transformJsonParse.js +3 -3
  11. package/dist/stream/ndjson/transformToNDJson.js +2 -2
  12. package/dist/stream/pipeline/pipeline.d.ts +25 -3
  13. package/dist/stream/pipeline/pipeline.js +76 -9
  14. package/dist/stream/readable/readableCreate.d.ts +8 -0
  15. package/dist/stream/readable/readableCreate.js +9 -1
  16. package/dist/stream/readable/readableForEach.d.ts +2 -2
  17. package/dist/stream/readable/readableFromArray.d.ts +2 -2
  18. package/dist/stream/readable/readableFromArray.js +17 -13
  19. package/dist/stream/readable/readableMap.d.ts +2 -2
  20. package/dist/stream/readable/readableMap.js +22 -17
  21. package/dist/stream/sizeStack.d.ts +9 -0
  22. package/dist/stream/sizeStack.js +48 -0
  23. package/dist/stream/stream.util.d.ts +4 -0
  24. package/dist/stream/stream.util.js +24 -0
  25. package/dist/stream/transform/transformBuffer.js +1 -1
  26. package/dist/stream/transform/transformFilter.d.ts +3 -4
  27. package/dist/stream/transform/transformFilter.js +5 -20
  28. package/dist/stream/transform/transformLimit.d.ts +32 -1
  29. package/dist/stream/transform/transformLimit.js +33 -16
  30. package/dist/stream/transform/transformLogProgress.d.ts +20 -0
  31. package/dist/stream/transform/transformLogProgress.js +36 -18
  32. package/dist/stream/transform/transformMap.d.ts +4 -15
  33. package/dist/stream/transform/transformMap.js +54 -71
  34. package/dist/stream/transform/transformMapSimple.js +1 -1
  35. package/dist/stream/transform/transformMapSync.d.ts +5 -3
  36. package/dist/stream/transform/transformMapSync.js +28 -22
  37. package/dist/stream/transform/transformNoOp.js +1 -1
  38. package/dist/stream/transform/transformTap.js +3 -3
  39. package/dist/stream/transform/transformToArray.js +1 -1
  40. package/dist/stream/transform/transformToString.js +2 -2
  41. package/dist/stream/transform/worker/transformMultiThreaded.js +1 -1
  42. package/dist/stream/transform/worker/workerClassProxy.js +1 -0
  43. package/dist/stream/writable/writableFork.js +1 -1
  44. package/dist/stream/writable/writableLimit.d.ts +9 -0
  45. package/dist/stream/writable/writableLimit.js +29 -0
  46. package/dist/stream/writable/writablePushToArray.js +1 -1
  47. package/dist/stream/writable/writableVoid.d.ts +8 -1
  48. package/dist/stream/writable/writableVoid.js +6 -2
  49. package/dist/util/zip.util.d.ts +15 -7
  50. package/dist/util/zip.util.js +27 -22
  51. package/package.json +1 -1
  52. package/src/got/getGot.ts +120 -31
  53. package/src/got/got.model.ts +8 -0
  54. package/src/index.ts +36 -194
  55. package/src/log/log.util.ts +9 -0
  56. package/src/stream/ndjson/ndjsonMap.ts +7 -5
  57. package/src/stream/ndjson/ndjsonStreamForEach.ts +2 -2
  58. package/src/stream/ndjson/transformJsonParse.ts +3 -3
  59. package/src/stream/ndjson/transformToNDJson.ts +2 -2
  60. package/src/stream/pipeline/pipeline.ts +102 -9
  61. package/src/stream/readable/readableCreate.ts +9 -1
  62. package/src/stream/readable/readableForEach.ts +2 -2
  63. package/src/stream/readable/readableFromArray.ts +18 -21
  64. package/src/stream/readable/readableMap.ts +24 -21
  65. package/src/stream/sizeStack.ts +56 -0
  66. package/src/stream/stream.util.ts +29 -0
  67. package/src/stream/transform/transformBuffer.ts +1 -1
  68. package/src/stream/transform/transformFilter.ts +6 -20
  69. package/src/stream/transform/transformLimit.ts +66 -20
  70. package/src/stream/transform/transformLogProgress.ts +72 -23
  71. package/src/stream/transform/transformMap.ts +75 -101
  72. package/src/stream/transform/transformMapSimple.ts +1 -1
  73. package/src/stream/transform/transformMapSync.ts +40 -26
  74. package/src/stream/transform/transformNoOp.ts +1 -1
  75. package/src/stream/transform/transformTap.ts +3 -3
  76. package/src/stream/transform/transformToArray.ts +1 -1
  77. package/src/stream/transform/transformToString.ts +2 -2
  78. package/src/stream/transform/worker/transformMultiThreaded.ts +1 -1
  79. package/src/stream/transform/worker/workerClassProxy.js +1 -0
  80. package/src/stream/writable/writableFork.ts +1 -1
  81. package/src/stream/writable/writableLimit.ts +28 -0
  82. package/src/stream/writable/writablePushToArray.ts +1 -1
  83. package/src/stream/writable/writableVoid.ts +14 -2
  84. package/src/util/zip.util.ts +26 -20
  85. package/dist/stream/transform/legacy/transformMap.d.ts +0 -17
  86. package/dist/stream/transform/legacy/transformMap.js +0 -94
  87. package/src/stream/transform/legacy/transformMap.ts +0 -133
@@ -1,6 +1,6 @@
1
1
  import { createReadStream, createWriteStream } from 'fs'
2
2
  import { createGzip, createUnzip } from 'zlib'
3
- import { AsyncMapper, ErrorMode } from '@naturalcycles/js-lib'
3
+ import { AbortableAsyncMapper, ErrorMode } from '@naturalcycles/js-lib'
4
4
  import {
5
5
  requireFileToExist,
6
6
  transformJsonParse,
@@ -41,7 +41,7 @@ export interface NDJSONMapOptions<IN = any, OUT = IN>
41
41
  * Zips output file automatically, if it ends with `.gz`.
42
42
  */
43
43
  export async function ndjsonMap<IN = any, OUT = any>(
44
- mapper: AsyncMapper<IN, OUT>,
44
+ mapper: AbortableAsyncMapper<IN, OUT>,
45
45
  opt: NDJSONMapOptions<IN, OUT>,
46
46
  ): Promise<void> {
47
47
  const { inputFilePath, outputFilePath, logEveryOutput = 100_000, limitInput, limitOutput } = opt
@@ -56,19 +56,21 @@ export async function ndjsonMap<IN = any, OUT = any>(
56
56
  const transformUnzip = inputFilePath.endsWith('.gz') ? [createUnzip()] : []
57
57
  const transformZip = outputFilePath.endsWith('.gz') ? [createGzip()] : []
58
58
 
59
+ const readable = createReadStream(inputFilePath)
60
+
59
61
  await _pipeline([
60
- createReadStream(inputFilePath),
62
+ readable,
61
63
  ...transformUnzip,
62
64
  transformSplit(), // splits by \n
63
65
  transformJsonParse(),
64
- transformLimit(limitInput),
66
+ transformLimit({ limit: limitInput, sourceReadable: readable }),
65
67
  transformLogProgress({ metric: 'read', ...opt }),
66
68
  transformMap(mapper, {
67
69
  flattenArrayOutput: true,
68
70
  errorMode: ErrorMode.SUPPRESS,
69
71
  ...opt,
70
72
  }),
71
- transformLimit(limitOutput),
73
+ transformLimit({ limit: limitOutput, sourceReadable: readable }),
72
74
  transformLogProgress({ metric: 'saved', logEvery: logEveryOutput }),
73
75
  transformToNDJson(),
74
76
  ...transformZip,
@@ -1,6 +1,6 @@
1
1
  import * as fs from 'fs'
2
2
  import { createUnzip } from 'zlib'
3
- import { AsyncMapper, ErrorMode } from '@naturalcycles/js-lib'
3
+ import { AbortableAsyncMapper, ErrorMode } from '@naturalcycles/js-lib'
4
4
  import {
5
5
  requireFileToExist,
6
6
  transformJsonParse,
@@ -23,7 +23,7 @@ export interface NDJSONStreamForEachOptions<IN = any>
23
23
  * Convenience function to `forEach` through an ndjson file.
24
24
  */
25
25
  export async function ndjsonStreamForEach<T>(
26
- mapper: AsyncMapper<T, void>,
26
+ mapper: AbortableAsyncMapper<T, void>,
27
27
  opt: NDJSONStreamForEachOptions<T>,
28
28
  ): Promise<void> {
29
29
  requireFileToExist(opt.inputFilePath)
@@ -33,17 +33,17 @@ export function transformJsonParse<OUT = any>(
33
33
  const { strict = true, reviver } = opt
34
34
 
35
35
  return new Transform({
36
- objectMode: false,
36
+ writableObjectMode: false,
37
37
  readableObjectMode: true,
38
- transform(chunk: string, _encoding, cb) {
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
- objectMode: true,
40
+ writableObjectMode: true,
41
41
  readableObjectMode: false,
42
- transform(chunk: IN, _encoding, cb) {
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 { promisify } from 'util'
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
- * Promisified stream.pipeline()
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 let _pipeline = promisify(pipeline)
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
- // Workaround https://github.com/nodejs/node/issues/40191
12
- // todo: remove it when fix is released in 16.x and in AppEngine 16.x
13
- if (process.version >= 'v16.10') {
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 { AsyncMapper, Mapper, _passNothingPredicate } from '@naturalcycles/js-lib'
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: AsyncMapper<T, void>,
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 { AsyncMapper, pMap, _passthroughMapper } from '@naturalcycles/js-lib'
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: AsyncMapper<IN, OUT> = _passthroughMapper,
13
+ mapper: AbortableAsyncMapper<IN, OUT> = _passthroughMapper,
14
14
  opt?: ReadableOptions,
15
15
  ): ReadableTyped<OUT> {
16
- const readable = new Readable({
16
+ let i = -1
17
+
18
+ return new Readable({
17
19
  objectMode: true,
18
20
  ...opt,
19
- read() {},
20
- })
21
-
22
- void pMap(
23
- items,
24
- async (item, index) => {
25
- readable.push(await mapper(item, index))
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
- { concurrency: 1 },
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 { AsyncMapper } from '@naturalcycles/js-lib'
2
- import { readableCreate } from '../../index'
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: AsyncMapper<IN, OUT>,
7
+ mapper: AbortableAsyncMapper<IN, OUT>,
8
8
  ): ReadableTyped<OUT> {
9
- const out = readableCreate<OUT>()
9
+ let i = -1
10
10
 
11
- void (async () => {
12
- try {
13
- let index = 0
14
- for await (const item of readable) {
15
- const v = await mapper(item, index++)
16
- out.push(v)
17
- }
18
-
19
- // We're done
20
- out.push(null)
21
- } catch (err) {
22
- console.error(err)
23
- out.emit('error', err)
24
- }
25
- })()
26
-
27
- return out
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, _encoding, cb) {
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
- * Note, that currently it's NOT concurrent! (concurrency = 1)
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: TransformOptions = {},
11
+ opt: TransformMapOptions = {},
13
12
  ): TransformTyped<IN, IN> {
14
- let index = 0
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
- async transform(chunk: IN, _encoding, cb) {
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 { Transform } from 'stream'
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?: number,
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
- return new Transform({
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: Transform, chunk: IN, _encoding, cb) {
24
- index++
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 (limit && index === limit) {
62
+ if (i === limit) {
33
63
  ended = true
34
- logger.log(`transformLimit: limit of ${limit} reached`)
35
- // this.emit('end') // this makes it "halt" on Node 14 lts
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
  })