@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.
Files changed (45) hide show
  1. package/dist/exec2/exec2.js +1 -0
  2. package/dist/stream/index.d.ts +1 -2
  3. package/dist/stream/index.js +1 -2
  4. package/dist/stream/ndjson/ndjsonMap.d.ts +1 -1
  5. package/dist/stream/ndjson/ndjsonMap.js +13 -15
  6. package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +2 -2
  7. package/dist/stream/ndjson/ndjsonStreamForEach.js +9 -15
  8. package/dist/stream/pipeline.d.ts +79 -0
  9. package/dist/stream/pipeline.js +220 -0
  10. package/dist/stream/stream.util.d.ts +1 -3
  11. package/dist/stream/stream.util.js +1 -20
  12. package/dist/stream/transform/transformChunk.d.ts +5 -8
  13. package/dist/stream/transform/transformChunk.js +4 -2
  14. package/dist/stream/transform/transformFlatten.d.ts +1 -0
  15. package/dist/stream/transform/transformFlatten.js +15 -4
  16. package/dist/stream/transform/transformLimit.d.ts +3 -26
  17. package/dist/stream/transform/transformLimit.js +14 -23
  18. package/dist/stream/transform/transformMap.d.ts +5 -0
  19. package/dist/stream/transform/transformMap.js +22 -18
  20. package/dist/stream/transform/transformMapSync.d.ts +5 -3
  21. package/dist/stream/transform/transformMapSync.js +7 -8
  22. package/dist/stream/transform/transformTee.js +4 -2
  23. package/dist/stream/writable/writableForEach.d.ts +2 -1
  24. package/dist/stream/writable/writableFork.js +2 -2
  25. package/package.json +1 -1
  26. package/src/exec2/exec2.ts +1 -0
  27. package/src/stream/index.ts +1 -2
  28. package/src/stream/ndjson/ndjsonMap.ts +12 -22
  29. package/src/stream/ndjson/ndjsonStreamForEach.ts +8 -15
  30. package/src/stream/pipeline.ts +301 -0
  31. package/src/stream/stream.util.ts +1 -29
  32. package/src/stream/transform/transformChunk.ts +8 -11
  33. package/src/stream/transform/transformFlatten.ts +16 -4
  34. package/src/stream/transform/transformLimit.ts +20 -51
  35. package/src/stream/transform/transformMap.ts +31 -20
  36. package/src/stream/transform/transformMapSync.ts +14 -8
  37. package/src/stream/transform/transformTee.ts +5 -2
  38. package/src/stream/writable/writableForEach.ts +2 -2
  39. package/src/stream/writable/writableFork.ts +2 -2
  40. package/dist/stream/pipeline/pipeline.d.ts +0 -36
  41. package/dist/stream/pipeline/pipeline.js +0 -82
  42. package/dist/stream/readable/readableForEach.d.ts +0 -19
  43. package/dist/stream/readable/readableForEach.js +0 -30
  44. package/src/stream/pipeline/pipeline.ts +0 -114
  45. package/src/stream/readable/readableForEach.ts +0 -42
@@ -250,6 +250,7 @@ class Exec2 {
250
250
  console.log([
251
251
  ' ',
252
252
  dimGrey(envString),
253
+ // todo: only before first space
253
254
  white(_substringAfterLast(cmd, '/')),
254
255
  ...(opt.args || []),
255
256
  ]
@@ -5,11 +5,10 @@ export * from './ndjson/ndjsonMap.js';
5
5
  export * from './ndjson/ndjsonStreamForEach.js';
6
6
  export * from './ndjson/transformJsonParse.js';
7
7
  export * from './ndjson/transformToNDJson.js';
8
- export * from './pipeline/pipeline.js';
8
+ export * from './pipeline.js';
9
9
  export * from './progressLogger.js';
10
10
  export * from './readable/readableCombined.js';
11
11
  export * from './readable/readableCreate.js';
12
- export * from './readable/readableForEach.js';
13
12
  export * from './readable/readableFromArray.js';
14
13
  export * from './readable/readableToArray.js';
15
14
  export * from './stream.model.js';
@@ -5,11 +5,10 @@ export * from './ndjson/ndjsonMap.js';
5
5
  export * from './ndjson/ndjsonStreamForEach.js';
6
6
  export * from './ndjson/transformJsonParse.js';
7
7
  export * from './ndjson/transformToNDJson.js';
8
- export * from './pipeline/pipeline.js';
8
+ export * from './pipeline.js';
9
9
  export * from './progressLogger.js';
10
10
  export * from './readable/readableCombined.js';
11
11
  export * from './readable/readableCreate.js';
12
- export * from './readable/readableForEach.js';
13
12
  export * from './readable/readableFromArray.js';
14
13
  export * from './readable/readableToArray.js';
15
14
  export * from './stream.model.js';
@@ -1,5 +1,5 @@
1
1
  import type { AbortableAsyncMapper } from '@naturalcycles/js-lib/types';
2
- import { type TransformLogProgressOptions, type TransformMapOptions } from '../index.js';
2
+ import type { TransformLogProgressOptions, TransformMapOptions } from '../index.js';
3
3
  export interface NDJSONMapOptions<IN = any, OUT = IN> extends TransformMapOptions<IN, OUT>, TransformLogProgressOptions<IN> {
4
4
  inputFilePath: string;
5
5
  outputFilePath: string;
@@ -1,6 +1,5 @@
1
1
  import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js';
2
- import { createReadStreamAsNDJSON, createWriteStreamAsNDJSON, transformFlatten, } from '../index.js';
3
- import { _pipeline, transformLimit, transformLogProgress, transformMap } from '../index.js';
2
+ import { Pipeline } from '../pipeline.js';
4
3
  /**
5
4
  * Unzips input file automatically, if it ends with `.gz`.
6
5
  * Zips output file automatically, if it ends with `.gz`.
@@ -11,17 +10,16 @@ export async function ndjsonMap(mapper, opt) {
11
10
  inputFilePath,
12
11
  outputFilePath,
13
12
  });
14
- const readable = createReadStreamAsNDJSON(inputFilePath).take(limitInput || Number.POSITIVE_INFINITY);
15
- await _pipeline([
16
- readable,
17
- transformLogProgress({ metric: 'read', ...opt }),
18
- transformMap(mapper, {
19
- errorMode: ErrorMode.SUPPRESS,
20
- ...opt,
21
- }),
22
- transformFlatten(),
23
- transformLimit({ limit: limitOutput, sourceReadable: readable }),
24
- transformLogProgress({ metric: 'saved', logEvery: logEveryOutput }),
25
- ...createWriteStreamAsNDJSON(outputFilePath),
26
- ]);
13
+ await Pipeline.fromNDJsonFile(inputFilePath)
14
+ .limitSource(limitInput)
15
+ .logProgress({ metric: 'read', ...opt })
16
+ .map(mapper, {
17
+ errorMode: ErrorMode.SUPPRESS,
18
+ ...opt,
19
+ })
20
+ .flattenIfNeeded()
21
+ // .typeCastAs<OUT>()
22
+ .limit(limitOutput)
23
+ .logProgress({ metric: 'saved', logEvery: logEveryOutput })
24
+ .toNDJsonFile(outputFilePath);
27
25
  }
@@ -1,6 +1,6 @@
1
1
  import type { AbortableAsyncMapper } from '@naturalcycles/js-lib/types';
2
- import { type TransformLogProgressOptions } from '../transform/transformLogProgress.js';
3
- import { type TransformMapOptions } from '../transform/transformMap.js';
2
+ import type { TransformLogProgressOptions } from '../transform/transformLogProgress.js';
3
+ import type { TransformMapOptions } from '../transform/transformMap.js';
4
4
  export interface NDJSONStreamForEachOptions<IN = any> extends TransformMapOptions<IN, void>, TransformLogProgressOptions<IN> {
5
5
  inputFilePath: string;
6
6
  }
@@ -1,21 +1,15 @@
1
1
  import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js';
2
- import { _pipeline } from '../pipeline/pipeline.js';
3
- import { transformLogProgress, } from '../transform/transformLogProgress.js';
4
- import { transformMap } from '../transform/transformMap.js';
5
- import { writableVoid } from '../writable/writableVoid.js';
6
- import { createReadStreamAsNDJSON } from './createReadStreamAsNDJSON.js';
2
+ import { Pipeline } from '../pipeline.js';
7
3
  /**
8
4
  * Convenience function to `forEach` through an ndjson file.
9
5
  */
10
6
  export async function ndjsonStreamForEach(mapper, opt) {
11
- await _pipeline([
12
- createReadStreamAsNDJSON(opt.inputFilePath),
13
- transformMap(mapper, {
14
- errorMode: ErrorMode.THROW_AGGREGATED,
15
- ...opt,
16
- predicate: () => true, // to log progress properly
17
- }),
18
- transformLogProgress(opt),
19
- writableVoid(),
20
- ]);
7
+ await Pipeline.fromNDJsonFile(opt.inputFilePath)
8
+ .map(mapper, {
9
+ errorMode: ErrorMode.THROW_AGGREGATED,
10
+ ...opt,
11
+ predicate: () => true, // to log progress properly
12
+ })
13
+ .logProgress(opt)
14
+ .run();
21
15
  }
@@ -0,0 +1,79 @@
1
+ import { type Transform } from 'node:stream';
2
+ import type { AbortableAsyncMapper, AsyncIndexedMapper, AsyncPredicate, END, IndexedMapper, NonNegativeInteger, PositiveInteger, Predicate, SKIP } from '@naturalcycles/js-lib/types';
3
+ import type { ReadableTyped, TransformOptions, TransformTyped, WritableTyped } from './stream.model.js';
4
+ import { type TransformLogProgressOptions } from './transform/transformLogProgress.js';
5
+ import { type TransformMapOptions } from './transform/transformMap.js';
6
+ import { type TransformMapSimpleOptions } from './transform/transformMapSimple.js';
7
+ import { type TransformMapSyncOptions } from './transform/transformMapSync.js';
8
+ import { type TransformOffsetOptions } from './transform/transformOffset.js';
9
+ import { type TransformTapOptions } from './transform/transformTap.js';
10
+ import { type TransformThrottleOptions } from './transform/transformThrottle.js';
11
+ export declare class Pipeline<T> {
12
+ private readonly source;
13
+ private transforms;
14
+ private destination?;
15
+ private readableLimit?;
16
+ private abortableSignal;
17
+ private constructor();
18
+ static from<T>(source: ReadableTyped<T>): Pipeline<T>;
19
+ /**
20
+ * Technically same as `fromIterable` (since Array is Iterable),
21
+ * but named a bit friendlier.
22
+ */
23
+ static fromArray<T>(input: T[]): Pipeline<T>;
24
+ static fromIterable<T>(input: Iterable<T> | AsyncIterable<T>): Pipeline<T>;
25
+ static fromNDJsonFile<T>(sourceFilePath: string): Pipeline<T>;
26
+ /**
27
+ * Limits the source Readable, but using `.take(limit)` on it.
28
+ * This is THE preferred way of limiting the source.
29
+ */
30
+ limitSource(limit: NonNegativeInteger | undefined): this;
31
+ /**
32
+ * If possible - STRONGLY PREFER applying `.take(limit)` on the source Readable,
33
+ * as it's a clean graceful way of limiting the Readable. Example:
34
+ *
35
+ * Pipeline.from(myReadable.take(10))
36
+ *
37
+ * or
38
+ *
39
+ * Pipeline
40
+ * .from(myReadable)
41
+ * .limitSource(10)
42
+ *
43
+ * If applying `take` on Readable is not possible - use this method at your own risk.
44
+ * Why warning?
45
+ * The limit works by aborting the stream, and then catching the error - certainly
46
+ * less clean than `.take()` on the source.
47
+ */
48
+ limit(limit: NonNegativeInteger | undefined): this;
49
+ chunk(chunkSize: PositiveInteger, opt?: TransformOptions): Pipeline<T[]>;
50
+ flatten<TO>(this: Pipeline<readonly TO[]>): Pipeline<TO>;
51
+ flattenIfNeeded(): Pipeline<T extends readonly (infer TO)[] ? TO : T>;
52
+ logProgress(opt?: TransformLogProgressOptions): this;
53
+ map<TO>(mapper: AbortableAsyncMapper<T, TO | typeof SKIP | typeof END>, opt?: TransformMapOptions<T, TO>): Pipeline<TO>;
54
+ mapSync<TO>(mapper: IndexedMapper<T, TO | typeof SKIP | typeof END>, opt?: TransformMapSyncOptions): Pipeline<TO>;
55
+ mapSimple<TO>(mapper: IndexedMapper<T, TO>, opt?: TransformMapSimpleOptions): Pipeline<TO>;
56
+ filter(predicate: AsyncPredicate<T>, opt?: TransformMapOptions): this;
57
+ filterSync(predicate: Predicate<T>, opt?: TransformOptions): this;
58
+ offset(opt: TransformOffsetOptions): this;
59
+ tap(fn: AsyncIndexedMapper<T, any>, opt?: TransformTapOptions): this;
60
+ throttle(opt: TransformThrottleOptions): this;
61
+ transform<TO>(transform: TransformTyped<T, TO>): Pipeline<TO>;
62
+ /**
63
+ * Helper method to add multiple transforms at once.
64
+ * Not type safe! Prefer using singular `transform()` multiple times for type safety.
65
+ */
66
+ transformMany<TO>(transforms: Transform[]): Pipeline<TO>;
67
+ /**
68
+ * Utility method just to conveniently type-cast the current Pipeline type.
69
+ * No runtime effect.
70
+ */
71
+ typeCastAs<TO>(): Pipeline<TO>;
72
+ toArray(opt?: TransformOptions): Promise<T[]>;
73
+ toFile(outputFilePath: string): Promise<void>;
74
+ toNDJsonFile(outputFilePath: string): Promise<void>;
75
+ to(destination: WritableTyped<T>): Promise<void>;
76
+ forEach(fn: AsyncIndexedMapper<T, void>, opt?: TransformMapOptions<T, void>): Promise<void>;
77
+ forEachSync(fn: IndexedMapper<T, void>, opt?: TransformMapSyncOptions<T, void>): Promise<void>;
78
+ run(): Promise<void>;
79
+ }
@@ -0,0 +1,220 @@
1
+ import { Readable } from 'node:stream';
2
+ import { pipeline } from 'node:stream/promises';
3
+ import { createGzip } from 'node:zlib';
4
+ import { createAbortableSignal } from '@naturalcycles/js-lib';
5
+ import { fs2 } from '../fs/fs2.js';
6
+ import { createReadStreamAsNDJSON } from './ndjson/createReadStreamAsNDJSON.js';
7
+ import { transformToNDJson } from './ndjson/transformToNDJson.js';
8
+ import { PIPELINE_GRACEFUL_ABORT } from './stream.util.js';
9
+ import { transformChunk } from './transform/transformChunk.js';
10
+ import { transformFilterSync } from './transform/transformFilter.js';
11
+ import { transformFlatten, transformFlattenIfNeeded } from './transform/transformFlatten.js';
12
+ import { transformLimit } from './transform/transformLimit.js';
13
+ import { transformLogProgress, } from './transform/transformLogProgress.js';
14
+ import { transformMap } from './transform/transformMap.js';
15
+ import { transformMapSimple, } from './transform/transformMapSimple.js';
16
+ import { transformMapSync } from './transform/transformMapSync.js';
17
+ import { transformOffset } from './transform/transformOffset.js';
18
+ import { transformTap } from './transform/transformTap.js';
19
+ import { transformThrottle } from './transform/transformThrottle.js';
20
+ import { writablePushToArray } from './writable/writablePushToArray.js';
21
+ import { writableVoid } from './writable/writableVoid.js';
22
+ export class Pipeline {
23
+ // biome-ignore lint/correctness/noUnusedPrivateClassMembers: ok
24
+ source;
25
+ transforms = [];
26
+ destination;
27
+ readableLimit;
28
+ abortableSignal = createAbortableSignal();
29
+ constructor(source) {
30
+ this.source = source;
31
+ }
32
+ static from(source) {
33
+ return new Pipeline(source);
34
+ }
35
+ /**
36
+ * Technically same as `fromIterable` (since Array is Iterable),
37
+ * but named a bit friendlier.
38
+ */
39
+ static fromArray(input) {
40
+ return new Pipeline(Readable.from(input));
41
+ }
42
+ static fromIterable(input) {
43
+ return new Pipeline(Readable.from(input));
44
+ }
45
+ static fromNDJsonFile(sourceFilePath) {
46
+ return new Pipeline(createReadStreamAsNDJSON(sourceFilePath));
47
+ }
48
+ /**
49
+ * Limits the source Readable, but using `.take(limit)` on it.
50
+ * This is THE preferred way of limiting the source.
51
+ */
52
+ limitSource(limit) {
53
+ this.readableLimit = limit;
54
+ return this;
55
+ }
56
+ /**
57
+ * If possible - STRONGLY PREFER applying `.take(limit)` on the source Readable,
58
+ * as it's a clean graceful way of limiting the Readable. Example:
59
+ *
60
+ * Pipeline.from(myReadable.take(10))
61
+ *
62
+ * or
63
+ *
64
+ * Pipeline
65
+ * .from(myReadable)
66
+ * .limitSource(10)
67
+ *
68
+ * If applying `take` on Readable is not possible - use this method at your own risk.
69
+ * Why warning?
70
+ * The limit works by aborting the stream, and then catching the error - certainly
71
+ * less clean than `.take()` on the source.
72
+ */
73
+ limit(limit) {
74
+ this.transforms.push(transformLimit({
75
+ limit,
76
+ signal: this.abortableSignal,
77
+ }));
78
+ return this;
79
+ }
80
+ chunk(chunkSize, opt) {
81
+ this.transforms.push(transformChunk(chunkSize, opt));
82
+ return this;
83
+ }
84
+ flatten() {
85
+ this.transforms.push(transformFlatten());
86
+ return this;
87
+ }
88
+ flattenIfNeeded() {
89
+ this.transforms.push(transformFlattenIfNeeded());
90
+ return this;
91
+ }
92
+ // TransformLogProgressOptions intentionally doesn't have <T> passed, as it's inconvenient in many cases
93
+ logProgress(opt) {
94
+ this.transforms.push(transformLogProgress(opt));
95
+ return this;
96
+ }
97
+ map(mapper, opt) {
98
+ this.transforms.push(transformMap(mapper, {
99
+ ...opt,
100
+ signal: this.abortableSignal,
101
+ }));
102
+ return this;
103
+ }
104
+ mapSync(mapper, opt) {
105
+ this.transforms.push(transformMapSync(mapper, {
106
+ ...opt,
107
+ signal: this.abortableSignal,
108
+ }));
109
+ return this;
110
+ }
111
+ mapSimple(mapper, opt) {
112
+ this.transforms.push(transformMapSimple(mapper, opt));
113
+ return this;
114
+ }
115
+ filter(predicate, opt) {
116
+ this.transforms.push(transformMap(v => v, {
117
+ predicate,
118
+ ...opt,
119
+ signal: this.abortableSignal,
120
+ }));
121
+ return this;
122
+ }
123
+ filterSync(predicate, opt) {
124
+ this.transforms.push(transformFilterSync(predicate, opt));
125
+ return this;
126
+ }
127
+ offset(opt) {
128
+ this.transforms.push(transformOffset(opt));
129
+ return this;
130
+ }
131
+ tap(fn, opt) {
132
+ this.transforms.push(transformTap(fn, opt));
133
+ return this;
134
+ }
135
+ throttle(opt) {
136
+ this.transforms.push(transformThrottle(opt));
137
+ return this;
138
+ }
139
+ // todo: tee/fork
140
+ transform(transform) {
141
+ this.transforms.push(transform);
142
+ return this;
143
+ }
144
+ /**
145
+ * Helper method to add multiple transforms at once.
146
+ * Not type safe! Prefer using singular `transform()` multiple times for type safety.
147
+ */
148
+ transformMany(transforms) {
149
+ this.transforms.push(...transforms);
150
+ return this;
151
+ }
152
+ /**
153
+ * Utility method just to conveniently type-cast the current Pipeline type.
154
+ * No runtime effect.
155
+ */
156
+ typeCastAs() {
157
+ return this;
158
+ }
159
+ async toArray(opt) {
160
+ const arr = [];
161
+ this.destination = writablePushToArray(arr, opt);
162
+ await this.run();
163
+ return arr;
164
+ }
165
+ async toFile(outputFilePath) {
166
+ fs2.ensureFile(outputFilePath);
167
+ this.destination = fs2.createWriteStream(outputFilePath);
168
+ await this.run();
169
+ }
170
+ async toNDJsonFile(outputFilePath) {
171
+ fs2.ensureFile(outputFilePath);
172
+ this.transforms.push(transformToNDJson());
173
+ if (outputFilePath.endsWith('.gz')) {
174
+ this.transforms.push(createGzip({
175
+ // chunkSize: 64 * 1024, // no observed speedup
176
+ }));
177
+ }
178
+ this.destination = fs2.createWriteStream(outputFilePath, {
179
+ // highWaterMark: 64 * 1024, // no observed speedup
180
+ });
181
+ await this.run();
182
+ }
183
+ async to(destination) {
184
+ this.destination = destination;
185
+ await this.run();
186
+ }
187
+ async forEach(fn, opt) {
188
+ this.transforms.push(transformMap(fn, {
189
+ ...opt,
190
+ signal: this.abortableSignal,
191
+ }));
192
+ await this.run();
193
+ }
194
+ async forEachSync(fn, opt) {
195
+ this.transforms.push(transformMapSync(fn, {
196
+ ...opt,
197
+ signal: this.abortableSignal,
198
+ }));
199
+ await this.run();
200
+ }
201
+ async run() {
202
+ this.destination ||= writableVoid();
203
+ let { source } = this;
204
+ if (this.readableLimit) {
205
+ source = source.take(this.readableLimit);
206
+ }
207
+ try {
208
+ await pipeline([source, ...this.transforms, this.destination], {
209
+ signal: this.abortableSignal,
210
+ });
211
+ }
212
+ catch (err) {
213
+ if (err instanceof Error && err.cause?.message === PIPELINE_GRACEFUL_ABORT) {
214
+ console.log('pipeline gracefully aborted'); // todo: this message may be removed later
215
+ return;
216
+ }
217
+ throw err;
218
+ }
219
+ }
220
+ }
@@ -1,3 +1 @@
1
- import type { Readable } from 'node:stream';
2
- import type { CommonLogger } from '@naturalcycles/js-lib/log';
3
- export declare function pipelineClose(name: string, readableDownstream: Readable, sourceReadable: Readable | undefined, streamDone: Promise<void> | undefined, logger: CommonLogger): void;
1
+ export declare const PIPELINE_GRACEFUL_ABORT = "PIPELINE_GRACEFUL_ABORT";
@@ -1,20 +1 @@
1
- export function pipelineClose(name, readableDownstream, sourceReadable, streamDone, logger) {
2
- readableDownstream.push(null); // this closes the stream, so downstream Readable will receive `end` and won't write anything
3
- if (!sourceReadable) {
4
- logger.warn(`${name} sourceReadable is not provided, readable stream will not be stopped`);
5
- }
6
- else {
7
- logger.log(`${name} is calling readable.unpipe() to pause the stream`);
8
- sourceReadable.unpipe(); // it is expected to pause the stream
9
- if (!streamDone) {
10
- logger.log(`${name} streamDone is not provided, will do readable.destroy right away`);
11
- sourceReadable.destroy();
12
- }
13
- else {
14
- void streamDone.then(() => {
15
- logger.log(`${name} streamDone, calling readable.destroy()`);
16
- sourceReadable.destroy(); // this throws ERR_STREAM_PREMATURE_CLOSE
17
- });
18
- }
19
- }
20
- }
1
+ export const PIPELINE_GRACEFUL_ABORT = 'PIPELINE_GRACEFUL_ABORT';
@@ -1,14 +1,11 @@
1
+ import type { PositiveInteger } from '@naturalcycles/js-lib/types';
1
2
  import type { TransformOptions, TransformTyped } from '../stream.model.js';
2
- export interface TransformChunkOptions extends TransformOptions {
3
- /**
4
- * How many items to include in each chunk.
5
- * Last chunk will contain the remaining items, possibly less than chunkSize.
6
- */
7
- chunkSize: number;
8
- }
9
3
  /**
10
4
  * Similar to RxJS bufferCount(),
11
5
  * allows to "chunk" the input stream into chunks of `opt.chunkSize` size.
12
6
  * Last chunk will contain the remaining items, possibly less than chunkSize.
7
+ *
8
+ * `chunkSize` indicates how many items to include in each chunk.
9
+ * Last chunk will contain the remaining items, possibly less than chunkSize.
13
10
  */
14
- export declare function transformChunk<IN = any>(opt: TransformChunkOptions): TransformTyped<IN, IN[]>;
11
+ export declare function transformChunk<IN = any>(chunkSize: PositiveInteger, opt?: TransformOptions): TransformTyped<IN, IN[]>;
@@ -3,9 +3,11 @@ import { Transform } from 'node:stream';
3
3
  * Similar to RxJS bufferCount(),
4
4
  * allows to "chunk" the input stream into chunks of `opt.chunkSize` size.
5
5
  * Last chunk will contain the remaining items, possibly less than chunkSize.
6
+ *
7
+ * `chunkSize` indicates how many items to include in each chunk.
8
+ * Last chunk will contain the remaining items, possibly less than chunkSize.
6
9
  */
7
- export function transformChunk(opt) {
8
- const { chunkSize } = opt;
10
+ export function transformChunk(chunkSize, opt) {
9
11
  let buf = [];
10
12
  return new Transform({
11
13
  objectMode: true,
@@ -1,2 +1,3 @@
1
1
  import type { TransformTyped } from '../stream.model.js';
2
2
  export declare function transformFlatten<T>(): TransformTyped<T[], T>;
3
+ export declare function transformFlattenIfNeeded<T>(): TransformTyped<T[], T>;
@@ -3,15 +3,26 @@ export function transformFlatten() {
3
3
  return new Transform({
4
4
  objectMode: true,
5
5
  transform(chunk, _, cb) {
6
- if (!Array.isArray(chunk)) {
7
- // As a safety precaution, to not crash the pipeline - push as is
8
- this.push(chunk);
6
+ for (const item of chunk) {
7
+ this.push(item);
9
8
  }
10
- else {
9
+ cb(); // acknowledge
10
+ },
11
+ });
12
+ }
13
+ export function transformFlattenIfNeeded() {
14
+ return new Transform({
15
+ objectMode: true,
16
+ transform(chunk, _, cb) {
17
+ if (Array.isArray(chunk)) {
11
18
  for (const item of chunk) {
12
19
  this.push(item);
13
20
  }
14
21
  }
22
+ else {
23
+ // As a safety precaution, to not crash the pipeline - push as is
24
+ this.push(chunk);
25
+ }
15
26
  cb(); // acknowledge
16
27
  },
17
28
  });
@@ -1,6 +1,4 @@
1
- import type { Readable } from 'node:stream';
2
- import type { CommonLogger } from '@naturalcycles/js-lib/log';
3
- import { AbortableTransform } from '../pipeline/pipeline.js';
1
+ import type { AbortableSignal } from '@naturalcycles/js-lib';
4
2
  import type { TransformOptions, TransformTyped } from '../stream.model.js';
5
3
  export interface TransformLimitOptions extends TransformOptions {
6
4
  /**
@@ -8,29 +6,8 @@ export interface TransformLimitOptions extends TransformOptions {
8
6
  */
9
7
  limit?: number;
10
8
  /**
11
- * If provided (recommended!) - it will call readable.destroy() on limit.
12
- * Without it - it will only stop the downstream consumers, but won't stop
13
- * the Readable ("source" of the stream).
14
- * It is almost always crucial to stop the Source too, so, please provide the Readable here!
9
+ * Allows to abort (gracefully stop) the stream from inside the Transform.
15
10
  */
16
- sourceReadable?: Readable;
17
- /**
18
- * Please provide it (a Promise that resolves when the Stream is done, e.g finished consuming things)
19
- * to be able to wait for Consumers before calling `readable.destroy`.
20
- * Has no effect if `readable` is not provided.
21
- */
22
- streamDone?: Promise<void>;
23
- logger?: CommonLogger;
24
- /**
25
- * Set to true to enable additional debug messages, e.g it'll log
26
- * when readable still emits values after the limit is reached.
27
- */
28
- debug?: boolean;
29
- }
30
- /**
31
- * Class only exists to be able to do `instanceof TransformLimit`
32
- * and to set sourceReadable+streamDone to it in `_pipeline`.
33
- */
34
- export declare class TransformLimit extends AbortableTransform {
11
+ signal: AbortableSignal;
35
12
  }
36
13
  export declare function transformLimit<IN>(opt: TransformLimitOptions): TransformTyped<IN, IN>;
@@ -1,41 +1,32 @@
1
- import { AbortableTransform } from '../pipeline/pipeline.js';
2
- import { pipelineClose } from '../stream.util.js';
1
+ import { Transform } from 'node:stream';
2
+ import { PIPELINE_GRACEFUL_ABORT } from '../stream.util.js';
3
3
  import { transformNoOp } from './transformNoOp.js';
4
- /**
5
- * Class only exists to be able to do `instanceof TransformLimit`
6
- * and to set sourceReadable+streamDone to it in `_pipeline`.
7
- */
8
- export class TransformLimit extends AbortableTransform {
9
- }
10
4
  export function transformLimit(opt) {
11
- const { logger = console, limit, debug } = opt;
5
+ const { limit, signal } = opt;
12
6
  if (!limit) {
13
- // No limit - returning pass-through transform
14
7
  return transformNoOp();
15
8
  }
16
9
  let i = 0; // so we start first chunk with 1
17
10
  let ended = false;
18
- return new TransformLimit({
11
+ return new Transform({
19
12
  objectMode: true,
20
13
  ...opt,
21
14
  transform(chunk, _, cb) {
15
+ if (ended) {
16
+ return;
17
+ }
22
18
  i++;
23
19
  if (i === limit) {
24
20
  ended = true;
25
- logger.log(`transformLimit of ${limit} reached`);
26
21
  this.push(chunk);
27
- pipelineClose('transformLimit', this, opt.sourceReadable || this.sourceReadable, opt.streamDone || this.streamDone, logger);
28
- cb(); // after pause
29
- }
30
- else if (!ended) {
31
- cb(null, chunk);
32
- }
33
- else {
34
- if (debug)
35
- logger.log(`transformLimit.transform after limit`, i);
36
- // If we ever HANG (don't call cb) - Node will do process.exit(0) to us
37
- cb(); // ended, don't emit anything
22
+ this.push(null); // tell downstream that we're done
23
+ cb();
24
+ queueMicrotask(() => {
25
+ signal.abort(new Error(PIPELINE_GRACEFUL_ABORT));
26
+ });
27
+ return;
38
28
  }
29
+ cb(null, chunk);
39
30
  },
40
31
  });
41
32
  }
@@ -1,3 +1,4 @@
1
+ import { type AbortableSignal } from '@naturalcycles/js-lib';
1
2
  import { ErrorMode } from '@naturalcycles/js-lib/error';
2
3
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
3
4
  import { type AbortableAsyncMapper, type AsyncPredicate, END, type PositiveInteger, type Promisable, SKIP, type StringMap, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
@@ -57,6 +58,10 @@ export interface TransformMapOptions<IN = any, OUT = IN> {
57
58
  */
58
59
  metric?: string;
59
60
  logger?: CommonLogger;
61
+ /**
62
+ * Allows to abort (gracefully stop) the stream from inside the Transform.
63
+ */
64
+ signal?: AbortableSignal;
60
65
  }
61
66
  export interface TransformMapStats {
62
67
  /**