@naturalcycles/nodejs-lib 13.10.0 → 13.11.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/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/stream/progressLogger.d.ts +14 -7
- package/dist/stream/progressLogger.js +14 -1
- package/dist/stream/readable/readableCreate.d.ts +1 -1
- package/dist/stream/readable/readableCreate.js +2 -2
- package/dist/stream/readable/readableToArray.d.ts +3 -0
- package/dist/stream/readable/readableToArray.js +11 -5
- package/dist/stream/stream.model.d.ts +24 -2
- package/dist/stream/transform/transformTee.d.ts +1 -1
- package/package.json +1 -1
- package/src/index.ts +0 -2
- package/src/stream/progressLogger.ts +23 -8
- package/src/stream/readable/readableCreate.ts +2 -2
- package/src/stream/readable/readableToArray.ts +11 -7
- package/src/stream/stream.model.ts +46 -4
- package/src/stream/transform/transformTee.ts +1 -1
- package/dist/stream/readable/readableMap.d.ts +0 -3
- package/dist/stream/readable/readableMap.js +0 -31
- package/dist/stream/readable/readableMapToArray.d.ts +0 -12
- package/dist/stream/readable/readableMapToArray.js +0 -18
- package/src/stream/readable/readableMap.ts +0 -34
- package/src/stream/readable/readableMapToArray.ts +0 -24
package/dist/index.d.ts
CHANGED
|
@@ -31,8 +31,6 @@ export * from './stream/pipeline/pipeline';
|
|
|
31
31
|
export * from './stream/readable/readableCreate';
|
|
32
32
|
export * from './stream/readable/readableForEach';
|
|
33
33
|
export * from './stream/readable/readableFromArray';
|
|
34
|
-
export * from './stream/readable/readableMap';
|
|
35
|
-
export * from './stream/readable/readableMapToArray';
|
|
36
34
|
export * from './stream/readable/readableToArray';
|
|
37
35
|
export * from './stream/stream.model';
|
|
38
36
|
export * from './stream/progressLogger';
|
package/dist/index.js
CHANGED
|
@@ -35,8 +35,6 @@ tslib_1.__exportStar(require("./stream/pipeline/pipeline"), exports);
|
|
|
35
35
|
tslib_1.__exportStar(require("./stream/readable/readableCreate"), exports);
|
|
36
36
|
tslib_1.__exportStar(require("./stream/readable/readableForEach"), exports);
|
|
37
37
|
tslib_1.__exportStar(require("./stream/readable/readableFromArray"), exports);
|
|
38
|
-
tslib_1.__exportStar(require("./stream/readable/readableMap"), exports);
|
|
39
|
-
tslib_1.__exportStar(require("./stream/readable/readableMapToArray"), exports);
|
|
40
38
|
tslib_1.__exportStar(require("./stream/readable/readableToArray"), exports);
|
|
41
39
|
tslib_1.__exportStar(require("./stream/stream.model"), exports);
|
|
42
40
|
tslib_1.__exportStar(require("./stream/progressLogger"), exports);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AnyObject, CommonLogger } from '@naturalcycles/js-lib';
|
|
2
|
-
|
|
2
|
+
import { ReadableMapper } from './stream.model';
|
|
3
|
+
export interface ProgressLoggerCfg<T = any> {
|
|
3
4
|
/**
|
|
4
5
|
* Progress metric
|
|
5
6
|
*
|
|
@@ -74,7 +75,7 @@ export interface ProgressLoggerCfg<IN = any> {
|
|
|
74
75
|
*
|
|
75
76
|
* chunk is undefined for "final" stats, otherwise is defined.
|
|
76
77
|
*/
|
|
77
|
-
extra?: (chunk:
|
|
78
|
+
extra?: (chunk: T | undefined, index: number) => AnyObject;
|
|
78
79
|
/**
|
|
79
80
|
* If specified - will multiply the counter by this number.
|
|
80
81
|
* Useful e.g when using `transformBuffer({ batchSize: 500 })`, so
|
|
@@ -115,9 +116,9 @@ export interface ProgressLogItem extends AnyObject {
|
|
|
115
116
|
rps10?: number;
|
|
116
117
|
rpsTotal?: number;
|
|
117
118
|
}
|
|
118
|
-
export declare class ProgressLogger<
|
|
119
|
-
constructor(cfg?: ProgressLoggerCfg<
|
|
120
|
-
cfg: ProgressLoggerCfg<
|
|
119
|
+
export declare class ProgressLogger<T> implements Disposable {
|
|
120
|
+
constructor(cfg?: ProgressLoggerCfg<T>);
|
|
121
|
+
cfg: ProgressLoggerCfg<T> & {
|
|
121
122
|
logEvery: number;
|
|
122
123
|
logSizesBuffer: number;
|
|
123
124
|
batchSize: number;
|
|
@@ -134,7 +135,7 @@ export declare class ProgressLogger<IN> implements Disposable {
|
|
|
134
135
|
private sizes?;
|
|
135
136
|
private sizesZipped?;
|
|
136
137
|
private start;
|
|
137
|
-
log(chunk?:
|
|
138
|
+
log(chunk?: T): void;
|
|
138
139
|
done(): void;
|
|
139
140
|
[Symbol.dispose](): void;
|
|
140
141
|
private logStats;
|
|
@@ -142,4 +143,10 @@ export declare class ProgressLogger<IN> implements Disposable {
|
|
|
142
143
|
/**
|
|
143
144
|
* Create new ProgressLogger.
|
|
144
145
|
*/
|
|
145
|
-
export declare function progressLogger<
|
|
146
|
+
export declare function progressLogger<T>(cfg?: ProgressLoggerCfg<T>): ProgressLogger<T>;
|
|
147
|
+
/**
|
|
148
|
+
* Limitation: I don't know how to catch the `final` callback to log final stats.
|
|
149
|
+
*
|
|
150
|
+
* @experimental
|
|
151
|
+
*/
|
|
152
|
+
export declare function progressReadableMapper<T>(cfg?: ProgressLoggerCfg<T>): ReadableMapper<T, T>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.progressLogger = exports.ProgressLogger = void 0;
|
|
3
|
+
exports.progressReadableMapper = exports.progressLogger = exports.ProgressLogger = void 0;
|
|
4
4
|
const node_util_1 = require("node:util");
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const colors_1 = require("../colors/colors");
|
|
@@ -118,3 +118,16 @@ function progressLogger(cfg = {}) {
|
|
|
118
118
|
return new ProgressLogger(cfg);
|
|
119
119
|
}
|
|
120
120
|
exports.progressLogger = progressLogger;
|
|
121
|
+
/**
|
|
122
|
+
* Limitation: I don't know how to catch the `final` callback to log final stats.
|
|
123
|
+
*
|
|
124
|
+
* @experimental
|
|
125
|
+
*/
|
|
126
|
+
function progressReadableMapper(cfg = {}) {
|
|
127
|
+
const progress = new ProgressLogger(cfg);
|
|
128
|
+
return chunk => {
|
|
129
|
+
progress.log(chunk);
|
|
130
|
+
return chunk;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
exports.progressReadableMapper = progressReadableMapper;
|
|
@@ -19,4 +19,4 @@ export declare function readableCreate<T>(items?: Iterable<T>, opt?: ReadableOpt
|
|
|
19
19
|
/**
|
|
20
20
|
* Convenience type-safe wrapper around Readable.from() that infers the Type of input.
|
|
21
21
|
*/
|
|
22
|
-
export declare function readableFrom<T>(
|
|
22
|
+
export declare function readableFrom<T>(iterable: Iterable<T> | AsyncIterable<T>, opt?: ReadableOptions): ReadableTyped<T>;
|
|
@@ -31,7 +31,7 @@ exports.readableCreate = readableCreate;
|
|
|
31
31
|
/**
|
|
32
32
|
* Convenience type-safe wrapper around Readable.from() that infers the Type of input.
|
|
33
33
|
*/
|
|
34
|
-
function readableFrom(
|
|
35
|
-
return node_stream_1.Readable.from(
|
|
34
|
+
function readableFrom(iterable, opt) {
|
|
35
|
+
return node_stream_1.Readable.from(iterable, opt);
|
|
36
36
|
}
|
|
37
37
|
exports.readableFrom = readableFrom;
|
|
@@ -2,5 +2,8 @@ import { ReadableTyped } from '../stream.model';
|
|
|
2
2
|
/**
|
|
3
3
|
* Convenience function to read the whole Readable stream into Array (in-memory)
|
|
4
4
|
* and return that array.
|
|
5
|
+
*
|
|
6
|
+
* Native `await readable.toArray()` can be used instead.
|
|
7
|
+
* This helper is kept for type-safery support.
|
|
5
8
|
*/
|
|
6
9
|
export declare function readableToArray<T>(readable: ReadableTyped<T>): Promise<T[]>;
|
|
@@ -4,12 +4,18 @@ exports.readableToArray = void 0;
|
|
|
4
4
|
/**
|
|
5
5
|
* Convenience function to read the whole Readable stream into Array (in-memory)
|
|
6
6
|
* and return that array.
|
|
7
|
+
*
|
|
8
|
+
* Native `await readable.toArray()` can be used instead.
|
|
9
|
+
* This helper is kept for type-safery support.
|
|
7
10
|
*/
|
|
8
11
|
async function readableToArray(readable) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
return await readable.toArray();
|
|
13
|
+
// const a: T[] = []
|
|
14
|
+
//
|
|
15
|
+
// for await (const item of readable) {
|
|
16
|
+
// a.push(item)
|
|
17
|
+
// }
|
|
18
|
+
//
|
|
19
|
+
// return a
|
|
14
20
|
}
|
|
15
21
|
exports.readableToArray = readableToArray;
|
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Readable, Transform, Writable } from 'node:stream';
|
|
2
|
+
import type { Readable, Transform, Writable } from 'node:stream';
|
|
3
|
+
import type { Promisable } from '@naturalcycles/js-lib';
|
|
4
|
+
export interface ReadableSignalOptions {
|
|
5
|
+
/** allows destroying the stream if the signal is aborted. */
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export interface ReadableArrayOptions {
|
|
9
|
+
/** the maximum concurrent invocations of `fn` to call on the stream at once. **Default: 1**. */
|
|
10
|
+
concurrency?: number;
|
|
11
|
+
/** allows destroying the stream if the signal is aborted. */
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}
|
|
14
|
+
export type ReadableMapper<IN, OUT> = (data: IN, opt?: ReadableSignalOptions) => Promisable<OUT>;
|
|
15
|
+
export type ReadableFlatMapper<IN, OUT> = (data: IN, opt?: ReadableSignalOptions) => Promisable<OUT[]>;
|
|
16
|
+
export type ReadableVoidMapper<IN> = (data: IN, opt?: ReadableSignalOptions) => void | Promise<void>;
|
|
17
|
+
export type ReadablePredicate<IN> = (data: IN, opt?: ReadableSignalOptions) => boolean | Promise<boolean>;
|
|
3
18
|
export interface ReadableTyped<T> extends Readable {
|
|
19
|
+
toArray: (opt?: ReadableSignalOptions) => Promise<T[]>;
|
|
20
|
+
map: <OUT>(mapper: ReadableMapper<T, OUT>, opt?: ReadableArrayOptions) => ReadableTyped<OUT>;
|
|
21
|
+
flatMap: <OUT>(mapper: ReadableFlatMapper<T, OUT>, opt?: ReadableArrayOptions) => ReadableTyped<OUT>;
|
|
22
|
+
filter: (predicate: ReadablePredicate<T>, opt?: ReadableArrayOptions) => ReadableTyped<T>;
|
|
23
|
+
forEach: (mapper: ReadableVoidMapper<T>, opt?: ReadableArrayOptions) => Promise<void>;
|
|
24
|
+
take: (limit: number, opt?: ReadableSignalOptions) => ReadableTyped<T>;
|
|
25
|
+
drop: (limit: number, opt?: ReadableSignalOptions) => ReadableTyped<T>;
|
|
4
26
|
}
|
|
5
27
|
export interface WritableTyped<T> extends Writable {
|
|
6
28
|
}
|
|
7
|
-
export interface TransformTyped<IN, OUT
|
|
29
|
+
export interface TransformTyped<IN, OUT> extends Transform {
|
|
8
30
|
}
|
|
9
31
|
export interface TransformOptions {
|
|
10
32
|
/**
|
|
@@ -10,5 +10,5 @@ type AnyStream = NodeJS.WritableStream | NodeJS.ReadWriteStream;
|
|
|
10
10
|
*
|
|
11
11
|
* @experimental
|
|
12
12
|
*/
|
|
13
|
-
export declare function transformTee<T>(streams: AnyStream[]): TransformTyped<T>;
|
|
13
|
+
export declare function transformTee<T>(streams: AnyStream[]): TransformTyped<T, T>;
|
|
14
14
|
export {};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -41,8 +41,6 @@ export * from './stream/pipeline/pipeline'
|
|
|
41
41
|
export * from './stream/readable/readableCreate'
|
|
42
42
|
export * from './stream/readable/readableForEach'
|
|
43
43
|
export * from './stream/readable/readableFromArray'
|
|
44
|
-
export * from './stream/readable/readableMap'
|
|
45
|
-
export * from './stream/readable/readableMapToArray'
|
|
46
44
|
export * from './stream/readable/readableToArray'
|
|
47
45
|
export * from './stream/stream.model'
|
|
48
46
|
export * from './stream/progressLogger'
|
|
@@ -10,8 +10,9 @@ import {
|
|
|
10
10
|
} from '@naturalcycles/js-lib'
|
|
11
11
|
import { boldWhite, dimGrey, hasColors, white, yellow } from '../colors/colors'
|
|
12
12
|
import { SizeStack } from './sizeStack'
|
|
13
|
+
import { ReadableMapper } from './stream.model'
|
|
13
14
|
|
|
14
|
-
export interface ProgressLoggerCfg<
|
|
15
|
+
export interface ProgressLoggerCfg<T = any> {
|
|
15
16
|
/**
|
|
16
17
|
* Progress metric
|
|
17
18
|
*
|
|
@@ -98,7 +99,7 @@ export interface ProgressLoggerCfg<IN = any> {
|
|
|
98
99
|
*
|
|
99
100
|
* chunk is undefined for "final" stats, otherwise is defined.
|
|
100
101
|
*/
|
|
101
|
-
extra?: (chunk:
|
|
102
|
+
extra?: (chunk: T | undefined, index: number) => AnyObject
|
|
102
103
|
|
|
103
104
|
/**
|
|
104
105
|
* If specified - will multiply the counter by this number.
|
|
@@ -150,8 +151,8 @@ const inspectOpt: InspectOptions = {
|
|
|
150
151
|
breakLength: 300,
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
export class ProgressLogger<
|
|
154
|
-
constructor(cfg: ProgressLoggerCfg<
|
|
154
|
+
export class ProgressLogger<T> implements Disposable {
|
|
155
|
+
constructor(cfg: ProgressLoggerCfg<T> = {}) {
|
|
155
156
|
this.cfg = {
|
|
156
157
|
metric: 'progress',
|
|
157
158
|
rss: true,
|
|
@@ -170,7 +171,7 @@ export class ProgressLogger<IN> implements Disposable {
|
|
|
170
171
|
this.logStats() // initial
|
|
171
172
|
}
|
|
172
173
|
|
|
173
|
-
cfg!: ProgressLoggerCfg<
|
|
174
|
+
cfg!: ProgressLoggerCfg<T> & {
|
|
174
175
|
logEvery: number
|
|
175
176
|
logSizesBuffer: number
|
|
176
177
|
batchSize: number
|
|
@@ -201,7 +202,7 @@ export class ProgressLogger<IN> implements Disposable {
|
|
|
201
202
|
: undefined
|
|
202
203
|
}
|
|
203
204
|
|
|
204
|
-
log(chunk?:
|
|
205
|
+
log(chunk?: T): void {
|
|
205
206
|
this.progress++
|
|
206
207
|
this.processedLastSecond++
|
|
207
208
|
|
|
@@ -223,7 +224,7 @@ export class ProgressLogger<IN> implements Disposable {
|
|
|
223
224
|
this.done()
|
|
224
225
|
}
|
|
225
226
|
|
|
226
|
-
private logStats(chunk?:
|
|
227
|
+
private logStats(chunk?: T, final = false, tenx = false): void {
|
|
227
228
|
if (!this.cfg.logProgress) return
|
|
228
229
|
|
|
229
230
|
const {
|
|
@@ -304,6 +305,20 @@ export class ProgressLogger<IN> implements Disposable {
|
|
|
304
305
|
/**
|
|
305
306
|
* Create new ProgressLogger.
|
|
306
307
|
*/
|
|
307
|
-
export function progressLogger<
|
|
308
|
+
export function progressLogger<T>(cfg: ProgressLoggerCfg<T> = {}): ProgressLogger<T> {
|
|
308
309
|
return new ProgressLogger(cfg)
|
|
309
310
|
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Limitation: I don't know how to catch the `final` callback to log final stats.
|
|
314
|
+
*
|
|
315
|
+
* @experimental
|
|
316
|
+
*/
|
|
317
|
+
export function progressReadableMapper<T>(cfg: ProgressLoggerCfg<T> = {}): ReadableMapper<T, T> {
|
|
318
|
+
const progress = new ProgressLogger(cfg)
|
|
319
|
+
|
|
320
|
+
return chunk => {
|
|
321
|
+
progress.log(chunk)
|
|
322
|
+
return chunk
|
|
323
|
+
}
|
|
324
|
+
}
|
|
@@ -34,8 +34,8 @@ export function readableCreate<T>(
|
|
|
34
34
|
* Convenience type-safe wrapper around Readable.from() that infers the Type of input.
|
|
35
35
|
*/
|
|
36
36
|
export function readableFrom<T>(
|
|
37
|
-
|
|
37
|
+
iterable: Iterable<T> | AsyncIterable<T>,
|
|
38
38
|
opt?: ReadableOptions,
|
|
39
39
|
): ReadableTyped<T> {
|
|
40
|
-
return Readable.from(
|
|
40
|
+
return Readable.from(iterable, opt)
|
|
41
41
|
}
|
|
@@ -3,13 +3,17 @@ import { ReadableTyped } from '../stream.model'
|
|
|
3
3
|
/**
|
|
4
4
|
* Convenience function to read the whole Readable stream into Array (in-memory)
|
|
5
5
|
* and return that array.
|
|
6
|
+
*
|
|
7
|
+
* Native `await readable.toArray()` can be used instead.
|
|
8
|
+
* This helper is kept for type-safery support.
|
|
6
9
|
*/
|
|
7
10
|
export async function readableToArray<T>(readable: ReadableTyped<T>): Promise<T[]> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
15
19
|
}
|
|
@@ -1,13 +1,55 @@
|
|
|
1
|
-
import { Readable, Transform, Writable } from 'node:stream'
|
|
1
|
+
import type { Readable, Transform, Writable } from 'node:stream'
|
|
2
|
+
import type { Promisable } from '@naturalcycles/js-lib'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
export interface ReadableSignalOptions {
|
|
5
|
+
/** allows destroying the stream if the signal is aborted. */
|
|
6
|
+
signal?: AbortSignal
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ReadableArrayOptions {
|
|
10
|
+
/** the maximum concurrent invocations of `fn` to call on the stream at once. **Default: 1**. */
|
|
11
|
+
concurrency?: number
|
|
12
|
+
/** allows destroying the stream if the signal is aborted. */
|
|
13
|
+
signal?: AbortSignal
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ReadableMapper<IN, OUT> = (data: IN, opt?: ReadableSignalOptions) => Promisable<OUT>
|
|
17
|
+
|
|
18
|
+
export type ReadableFlatMapper<IN, OUT> = (
|
|
19
|
+
data: IN,
|
|
20
|
+
opt?: ReadableSignalOptions,
|
|
21
|
+
) => Promisable<OUT[]>
|
|
22
|
+
|
|
23
|
+
export type ReadableVoidMapper<IN> = (data: IN, opt?: ReadableSignalOptions) => void | Promise<void>
|
|
24
|
+
|
|
25
|
+
export type ReadablePredicate<IN> = (
|
|
26
|
+
data: IN,
|
|
27
|
+
opt?: ReadableSignalOptions,
|
|
28
|
+
) => boolean | Promise<boolean>
|
|
29
|
+
|
|
30
|
+
export interface ReadableTyped<T> extends Readable {
|
|
31
|
+
toArray: (opt?: ReadableSignalOptions) => Promise<T[]>
|
|
32
|
+
|
|
33
|
+
map: <OUT>(mapper: ReadableMapper<T, OUT>, opt?: ReadableArrayOptions) => ReadableTyped<OUT>
|
|
34
|
+
|
|
35
|
+
flatMap: <OUT>(
|
|
36
|
+
mapper: ReadableFlatMapper<T, OUT>,
|
|
37
|
+
opt?: ReadableArrayOptions,
|
|
38
|
+
) => ReadableTyped<OUT>
|
|
39
|
+
|
|
40
|
+
filter: (predicate: ReadablePredicate<T>, opt?: ReadableArrayOptions) => ReadableTyped<T>
|
|
41
|
+
|
|
42
|
+
forEach: (mapper: ReadableVoidMapper<T>, opt?: ReadableArrayOptions) => Promise<void>
|
|
43
|
+
|
|
44
|
+
take: (limit: number, opt?: ReadableSignalOptions) => ReadableTyped<T>
|
|
45
|
+
drop: (limit: number, opt?: ReadableSignalOptions) => ReadableTyped<T>
|
|
46
|
+
}
|
|
5
47
|
|
|
6
48
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
7
49
|
export interface WritableTyped<T> extends Writable {}
|
|
8
50
|
|
|
9
51
|
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
10
|
-
export interface TransformTyped<IN, OUT
|
|
52
|
+
export interface TransformTyped<IN, OUT> extends Transform {}
|
|
11
53
|
|
|
12
54
|
export interface TransformOptions {
|
|
13
55
|
/**
|
|
@@ -14,7 +14,7 @@ type AnyStream = NodeJS.WritableStream | NodeJS.ReadWriteStream
|
|
|
14
14
|
*
|
|
15
15
|
* @experimental
|
|
16
16
|
*/
|
|
17
|
-
export function transformTee<T>(streams: AnyStream[]): TransformTyped<T> {
|
|
17
|
+
export function transformTee<T>(streams: AnyStream[]): TransformTyped<T, T> {
|
|
18
18
|
const readable = readableCreate<T>()
|
|
19
19
|
|
|
20
20
|
const secondPipelinePromise = _pipeline([readable, ...streams])
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readableMap = void 0;
|
|
4
|
-
const node_stream_1 = require("node:stream");
|
|
5
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
|
-
function readableMap(readable, mapper) {
|
|
7
|
-
let i = -1;
|
|
8
|
-
const stream = readable
|
|
9
|
-
.on('error', err => stream.emit('error', err))
|
|
10
|
-
.pipe(new node_stream_1.Transform({
|
|
11
|
-
objectMode: true,
|
|
12
|
-
async transform(chunk, _enc, cb) {
|
|
13
|
-
try {
|
|
14
|
-
const r = await mapper(chunk, ++i);
|
|
15
|
-
if (r === js_lib_1.SKIP) {
|
|
16
|
-
cb();
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
// _assert(r !== END, `readableMap END not supported`)
|
|
20
|
-
cb(null, r);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
console.error(err);
|
|
25
|
-
cb(err);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
}));
|
|
29
|
-
return stream;
|
|
30
|
-
}
|
|
31
|
-
exports.readableMap = readableMap;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { AsyncMapper } from '@naturalcycles/js-lib';
|
|
2
|
-
import { ReadableTyped } from '../stream.model';
|
|
3
|
-
import { TransformMapOptions } from '../transform/transformMap';
|
|
4
|
-
/**
|
|
5
|
-
* Map Readable items to array of results (in memory),
|
|
6
|
-
* passing each result via `transformMap`.
|
|
7
|
-
*
|
|
8
|
-
* Warning! All results are stored in memory (no backpressure).
|
|
9
|
-
*
|
|
10
|
-
* Try native readable.toArray instead!
|
|
11
|
-
*/
|
|
12
|
-
export declare function readableMapToArray<IN, OUT = IN>(stream: ReadableTyped<IN>, mapper?: AsyncMapper<IN, OUT>, opt?: TransformMapOptions<IN, OUT>): Promise<OUT[]>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readableMapToArray = void 0;
|
|
4
|
-
const index_1 = require("../../index");
|
|
5
|
-
/**
|
|
6
|
-
* Map Readable items to array of results (in memory),
|
|
7
|
-
* passing each result via `transformMap`.
|
|
8
|
-
*
|
|
9
|
-
* Warning! All results are stored in memory (no backpressure).
|
|
10
|
-
*
|
|
11
|
-
* Try native readable.toArray instead!
|
|
12
|
-
*/
|
|
13
|
-
async function readableMapToArray(stream, mapper = item => item, opt) {
|
|
14
|
-
const res = [];
|
|
15
|
-
await (0, index_1._pipeline)([stream, (0, index_1.transformMap)(mapper, opt), (0, index_1.writablePushToArray)(res)]);
|
|
16
|
-
return res;
|
|
17
|
-
}
|
|
18
|
-
exports.readableMapToArray = readableMapToArray;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { Transform } from 'node:stream'
|
|
2
|
-
import { AbortableAsyncMapper, SKIP } from '@naturalcycles/js-lib'
|
|
3
|
-
import { ReadableTyped } from '../stream.model'
|
|
4
|
-
|
|
5
|
-
export function readableMap<IN, OUT>(
|
|
6
|
-
readable: ReadableTyped<IN>,
|
|
7
|
-
mapper: AbortableAsyncMapper<IN, OUT>,
|
|
8
|
-
): ReadableTyped<OUT> {
|
|
9
|
-
let i = -1
|
|
10
|
-
|
|
11
|
-
const stream: ReadableTyped<OUT> = readable
|
|
12
|
-
.on('error', err => stream.emit('error', err))
|
|
13
|
-
.pipe(
|
|
14
|
-
new Transform({
|
|
15
|
-
objectMode: true,
|
|
16
|
-
async transform(chunk, _enc, cb) {
|
|
17
|
-
try {
|
|
18
|
-
const r = await mapper(chunk, ++i)
|
|
19
|
-
if (r === SKIP) {
|
|
20
|
-
cb()
|
|
21
|
-
} else {
|
|
22
|
-
// _assert(r !== END, `readableMap END not supported`)
|
|
23
|
-
cb(null, r)
|
|
24
|
-
}
|
|
25
|
-
} catch (err) {
|
|
26
|
-
console.error(err)
|
|
27
|
-
cb(err as Error)
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
}),
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
return stream
|
|
34
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { AsyncMapper } from '@naturalcycles/js-lib'
|
|
2
|
-
import { transformMap, writablePushToArray, _pipeline } from '../../index'
|
|
3
|
-
import { ReadableTyped } from '../stream.model'
|
|
4
|
-
import { TransformMapOptions } from '../transform/transformMap'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Map Readable items to array of results (in memory),
|
|
8
|
-
* passing each result via `transformMap`.
|
|
9
|
-
*
|
|
10
|
-
* Warning! All results are stored in memory (no backpressure).
|
|
11
|
-
*
|
|
12
|
-
* Try native readable.toArray instead!
|
|
13
|
-
*/
|
|
14
|
-
export async function readableMapToArray<IN, OUT = IN>(
|
|
15
|
-
stream: ReadableTyped<IN>,
|
|
16
|
-
mapper: AsyncMapper<IN, OUT> = item => item as any,
|
|
17
|
-
opt?: TransformMapOptions<IN, OUT>,
|
|
18
|
-
): Promise<OUT[]> {
|
|
19
|
-
const res: OUT[] = []
|
|
20
|
-
|
|
21
|
-
await _pipeline([stream, transformMap(mapper, opt), writablePushToArray(res)])
|
|
22
|
-
|
|
23
|
-
return res
|
|
24
|
-
}
|