@naturalcycles/nodejs-lib 15.25.0 → 15.27.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 (76) hide show
  1. package/dist/slack/slack.service.d.ts +1 -0
  2. package/dist/slack/slack.service.js +4 -3
  3. package/dist/stream/index.d.ts +2 -8
  4. package/dist/stream/index.js +2 -8
  5. package/dist/stream/ndjson/createReadStreamAsNDJson.d.ts +16 -0
  6. package/dist/stream/ndjson/{createReadStreamAsNDJSON.js → createReadStreamAsNDJson.js} +10 -13
  7. package/dist/stream/ndjson/ndjsonMap.d.ts +2 -0
  8. package/dist/stream/ndjson/ndjsonMap.js +2 -0
  9. package/dist/stream/pipeline.d.ts +2 -1
  10. package/dist/stream/pipeline.js +25 -8
  11. package/dist/stream/progressLogger.d.ts +3 -3
  12. package/dist/stream/readable/readableCombined.d.ts +4 -2
  13. package/dist/stream/readable/readableCombined.js +16 -11
  14. package/dist/stream/readable/readableCreate.d.ts +1 -3
  15. package/dist/stream/readable/readableCreate.js +4 -4
  16. package/dist/stream/stream.model.d.ts +16 -0
  17. package/dist/stream/transform/transformFork.d.ts +10 -0
  18. package/dist/stream/transform/transformFork.js +62 -0
  19. package/dist/stream/transform/transformLimit.d.ts +2 -1
  20. package/dist/stream/transform/transformLimit.js +3 -3
  21. package/dist/stream/transform/transformLogProgress.js +3 -2
  22. package/dist/stream/transform/transformMap.d.ts +2 -4
  23. package/dist/stream/transform/transformMap.js +3 -2
  24. package/dist/stream/transform/transformMapSimple.d.ts +2 -4
  25. package/dist/stream/transform/transformMapSimple.js +3 -2
  26. package/dist/stream/transform/transformMapSync.d.ts +2 -4
  27. package/dist/stream/transform/transformMapSync.js +3 -1
  28. package/dist/stream/transform/transformSplit.js +2 -2
  29. package/dist/stream/transform/transformThrottle.d.ts +2 -3
  30. package/dist/stream/transform/transformThrottle.js +22 -27
  31. package/dist/stream/writable/writableVoid.d.ts +1 -8
  32. package/dist/stream/writable/writableVoid.js +0 -1
  33. package/package.json +1 -1
  34. package/src/slack/slack.service.ts +6 -3
  35. package/src/stream/index.ts +2 -8
  36. package/src/stream/ndjson/{createReadStreamAsNDJSON.ts → createReadStreamAsNDJson.ts} +10 -13
  37. package/src/stream/ndjson/ndjsonMap.ts +2 -0
  38. package/src/stream/pipeline.ts +33 -9
  39. package/src/stream/progressLogger.ts +3 -3
  40. package/src/stream/readable/readableCombined.ts +22 -11
  41. package/src/stream/readable/readableCreate.ts +4 -3
  42. package/src/stream/stream.model.ts +18 -0
  43. package/src/stream/transform/transformFork.ts +74 -0
  44. package/src/stream/transform/transformLimit.ts +5 -4
  45. package/src/stream/transform/transformLogProgress.ts +3 -2
  46. package/src/stream/transform/transformMap.ts +4 -8
  47. package/src/stream/transform/transformMapSimple.ts +10 -7
  48. package/src/stream/transform/transformMapSync.ts +4 -6
  49. package/src/stream/transform/transformSplit.ts +2 -2
  50. package/src/stream/transform/transformThrottle.ts +28 -36
  51. package/src/stream/writable/writableVoid.ts +1 -10
  52. package/dist/stream/ndjson/createReadStreamAsNDJSON.d.ts +0 -19
  53. package/dist/stream/ndjson/createWriteStreamAsNDJSON.d.ts +0 -11
  54. package/dist/stream/ndjson/createWriteStreamAsNDJSON.js +0 -27
  55. package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +0 -10
  56. package/dist/stream/ndjson/ndjsonStreamForEach.js +0 -15
  57. package/dist/stream/readable/readableToArray.d.ts +0 -9
  58. package/dist/stream/readable/readableToArray.js +0 -17
  59. package/dist/stream/transform/transformTee.d.ts +0 -13
  60. package/dist/stream/transform/transformTee.js +0 -37
  61. package/dist/stream/transform/transformToArray.d.ts +0 -5
  62. package/dist/stream/transform/transformToArray.js +0 -20
  63. package/dist/stream/writable/writableForEach.d.ts +0 -12
  64. package/dist/stream/writable/writableForEach.js +0 -15
  65. package/dist/stream/writable/writableFork.d.ts +0 -10
  66. package/dist/stream/writable/writableFork.js +0 -45
  67. package/dist/stream/writable/writableLimit.d.ts +0 -8
  68. package/dist/stream/writable/writableLimit.js +0 -25
  69. package/src/stream/ndjson/createWriteStreamAsNDJSON.ts +0 -30
  70. package/src/stream/ndjson/ndjsonStreamForEach.ts +0 -28
  71. package/src/stream/readable/readableToArray.ts +0 -19
  72. package/src/stream/transform/transformTee.ts +0 -48
  73. package/src/stream/transform/transformToArray.ts +0 -23
  74. package/src/stream/writable/writableForEach.ts +0 -25
  75. package/src/stream/writable/writableFork.ts +0 -56
  76. package/src/stream/writable/writableLimit.ts +0 -29
@@ -1,25 +0,0 @@
1
- import { Writable } from 'node:stream';
2
- /**
3
- * Allows to stop the Readable stream after the pipeline has processed X number of rows.
4
- * It counts OUTPUT rows (not input), because this Writable is always at the end of the Pipeline.
5
- * It ensures that everything has been processed before issuing a STOP on the readable.
6
- */
7
- export function writableLimit(readable, limit) {
8
- let i = 0;
9
- return new Writable({
10
- objectMode: true,
11
- write(_chunk, _, cb) {
12
- if (limit === 0)
13
- return cb(); // no limit, just passthrough
14
- i++;
15
- if (i === limit) {
16
- console.log(`writableLimit of ${limit} reached`);
17
- readable.destroy();
18
- cb(); // do we need it?
19
- }
20
- else {
21
- cb(); // passthrough
22
- }
23
- },
24
- });
25
- }
@@ -1,30 +0,0 @@
1
- import { createGzip } from 'node:zlib'
2
- import { _isTruthy } from '@naturalcycles/js-lib'
3
- import { fs2 } from '../../fs/fs2.js'
4
- import type { TransformTyped } from '../stream.model.js'
5
- import { transformToNDJson } from './transformToNDJson.js'
6
-
7
- /**
8
- Returns an array of Transforms, so that you can ...destructure them at
9
- the end of the _pipeline.
10
-
11
- Replaces a list of operations:
12
- - transformToNDJson
13
- - createGzip (only if path ends with '.gz')
14
- - fs.createWriteStream
15
- */
16
- export function createWriteStreamAsNDJSON(outputPath: string): TransformTyped<any, any>[] {
17
- fs2.ensureFile(outputPath)
18
-
19
- return [
20
- transformToNDJson(),
21
- outputPath.endsWith('.gz')
22
- ? createGzip({
23
- // chunkSize: 64 * 1024, // no observed speedup
24
- })
25
- : undefined,
26
- fs2.createWriteStream(outputPath, {
27
- // highWaterMark: 64 * 1024, // no observed speedup
28
- }),
29
- ].filter(_isTruthy) as TransformTyped<any, any>[]
30
- }
@@ -1,28 +0,0 @@
1
- import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js'
2
- import type { AbortableAsyncMapper } from '@naturalcycles/js-lib/types'
3
- import { Pipeline } from '../pipeline.js'
4
- import type { TransformLogProgressOptions } from '../transform/transformLogProgress.js'
5
- import type { TransformMapOptions } from '../transform/transformMap.js'
6
-
7
- export interface NDJSONStreamForEachOptions<IN = any>
8
- extends TransformMapOptions<IN, void>,
9
- TransformLogProgressOptions<IN> {
10
- inputFilePath: string
11
- }
12
-
13
- /**
14
- * Convenience function to `forEach` through an ndjson file.
15
- */
16
- export async function ndjsonStreamForEach<T>(
17
- mapper: AbortableAsyncMapper<T, void>,
18
- opt: NDJSONStreamForEachOptions<T>,
19
- ): Promise<void> {
20
- await Pipeline.fromNDJsonFile<T>(opt.inputFilePath)
21
- .map(mapper, {
22
- errorMode: ErrorMode.THROW_AGGREGATED,
23
- ...opt,
24
- predicate: () => true, // to log progress properly
25
- })
26
- .logProgress(opt)
27
- .run()
28
- }
@@ -1,19 +0,0 @@
1
- import type { ReadableTyped } from '../stream.model.js'
2
-
3
- /**
4
- * Convenience function to read the whole Readable stream into Array (in-memory)
5
- * and return that array.
6
- *
7
- * Native `await readable.toArray()` can be used instead.
8
- * This helper is kept for type-safery support.
9
- */
10
- export async function readableToArray<T>(readable: ReadableTyped<T>): Promise<T[]> {
11
- return await readable.toArray()
12
- // const a: T[] = []
13
- //
14
- // for await (const item of readable) {
15
- // a.push(item)
16
- // }
17
- //
18
- // return a
19
- }
@@ -1,48 +0,0 @@
1
- import { Transform } from 'node:stream'
2
- import { pipeline } from 'node:stream/promises'
3
- import { readableCreate } from '../readable/readableCreate.js'
4
- import type { TransformTyped } from '../stream.model.js'
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, 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
- // todo: it's possible to start respecting backpressure,
26
- // if we start to listen to the boolean output of .push()
27
-
28
- // pass to the "secondary" pipeline
29
- readable.push(chunk)
30
-
31
- // pass through to the "main" pipeline
32
- cb(null, chunk)
33
- },
34
- async final(cb) {
35
- console.log('transformTee final')
36
-
37
- // Pushing null "closes"/ends the secondary pipeline correctly
38
- readable.push(null)
39
-
40
- // Second pipeline is expected to finish now, let's await it
41
- await secondPipelinePromise
42
- console.log('transformTee final secondPipeline done')
43
-
44
- // Because second pipeline is done - now we can signal main pipeline to be done as well
45
- cb()
46
- },
47
- })
48
- }
@@ -1,23 +0,0 @@
1
- import { Transform } from 'node:stream'
2
- import type { TransformOptions, TransformTyped } from '../stream.model.js'
3
-
4
- /**
5
- * Will collect all stream results in the array (keeping it in memory) and emit in the end as one result.
6
- */
7
- export function transformToArray<IN>(opt: TransformOptions = {}): TransformTyped<IN, IN[]> {
8
- const res: IN[] = []
9
-
10
- return new Transform({
11
- objectMode: true,
12
- ...opt,
13
- transform(chunk: IN, _, cb) {
14
- res.push(chunk)
15
- // callback to signal that we processed input, but not emitting any output
16
- cb()
17
- },
18
- final(this: Transform, cb) {
19
- this.push(res)
20
- cb()
21
- },
22
- })
23
- }
@@ -1,25 +0,0 @@
1
- import type { AsyncIndexedMapper, IndexedMapper } from '@naturalcycles/js-lib/types'
2
- import { _passNothingPredicate } from '@naturalcycles/js-lib/types'
3
- import type { WritableTyped } from '../stream.model.js'
4
- import { transformMap, type TransformMapOptions } from '../transform/transformMap.js'
5
- import { transformMapSync, type TransformMapSyncOptions } from '../transform/transformMapSync.js'
6
-
7
- /**
8
- * Just an alias to transformMap that declares OUT as void.
9
- */
10
- export function writableForEach<IN = any>(
11
- mapper: AsyncIndexedMapper<IN, void>,
12
- opt: TransformMapOptions<IN, void> = {},
13
- ): WritableTyped<IN> {
14
- return transformMap<IN, void>(mapper, { ...opt, predicate: _passNothingPredicate })
15
- }
16
-
17
- /**
18
- * Just an alias to transformMap that declares OUT as void.
19
- */
20
- export function writableForEachSync<IN = any>(
21
- mapper: IndexedMapper<IN, void>,
22
- opt: TransformMapSyncOptions<IN, void> = {},
23
- ): WritableTyped<IN> {
24
- return transformMapSync<IN, void>(mapper, { ...opt, predicate: _passNothingPredicate })
25
- }
@@ -1,56 +0,0 @@
1
- import { Writable } from 'node:stream'
2
- import { pipeline } from 'node:stream/promises'
3
- import { readableCreate } from '../readable/readableCreate.js'
4
- import type { ReadableTyped, TransformOptions, WritableTyped } from '../stream.model.js'
5
-
6
- /**
7
- * Allows "forking" a stream inside pipeline into a number of pipeline chains (2 or more).
8
- * Currently does NOT (!) maintain backpressure.
9
- * Error in the forked pipeline will propagate up to the main pipeline (and log error, to be sure).
10
- * Will wait until all forked pipelines are completed before completing the stream.
11
- *
12
- * @experimental
13
- */
14
- export function writableFork<T>(
15
- chains: NodeJS.WritableStream[][],
16
- opt?: TransformOptions,
17
- ): WritableTyped<T> {
18
- const readables: ReadableTyped<T>[] = []
19
-
20
- const allChainsDone = Promise.all(
21
- chains.map(async chain => {
22
- const readable = readableCreate<T>()
23
- readables.push(readable)
24
-
25
- return await pipeline([readable, ...chain])
26
- }),
27
- ).catch(err => {
28
- console.error(err) // ensure the error is logged
29
- throw err
30
- })
31
-
32
- return new Writable({
33
- objectMode: true,
34
- ...opt,
35
- write(chunk: T, _, cb) {
36
- // Push/fork to all sub-streams
37
- // No backpressure is ensured here, it'll push regardless of the
38
- readables.forEach(readable => readable.push(chunk))
39
-
40
- cb()
41
- },
42
- async final(cb) {
43
- try {
44
- // Push null (complete) to all sub-streams
45
- readables.forEach(readable => readable.push(null))
46
-
47
- console.log(`writableFork.final is waiting for all chains to be done`)
48
- await allChainsDone
49
- console.log(`writableFork.final all chains done`)
50
- cb()
51
- } catch (err) {
52
- cb(err as Error)
53
- }
54
- },
55
- })
56
- }
@@ -1,29 +0,0 @@
1
- import type { Readable } from 'node:stream'
2
- import { Writable } from 'node:stream'
3
- import type { WritableTyped } from '../stream.model.js'
4
-
5
- /**
6
- * Allows to stop the Readable stream after the pipeline has processed X number of rows.
7
- * It counts OUTPUT rows (not input), because this Writable is always at the end of the Pipeline.
8
- * It ensures that everything has been processed before issuing a STOP on the readable.
9
- */
10
- export function writableLimit<T>(readable: Readable, limit: number): WritableTyped<T> {
11
- let i = 0
12
-
13
- return new Writable({
14
- objectMode: true,
15
- write(_chunk, _, cb) {
16
- if (limit === 0) return cb() // no limit, just passthrough
17
-
18
- i++
19
-
20
- if (i === limit) {
21
- console.log(`writableLimit of ${limit} reached`)
22
- readable.destroy()
23
- cb() // do we need it?
24
- } else {
25
- cb() // passthrough
26
- }
27
- },
28
- })
29
- }