@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
@@ -7,46 +7,52 @@ const inflate = promisify(zlib.inflate.bind(zlib))
7
7
  const gzip = promisify(zlib.gzip.bind(zlib))
8
8
  const gunzip = promisify(zlib.gunzip.bind(zlib))
9
9
 
10
- // string > zip
10
+ // string > compressed buffer
11
+
11
12
  /**
12
- * zipBuffer uses `deflate`.
13
- * It's 9 bytes shorter than gzip.
13
+ * deflateBuffer uses `deflate`.
14
+ * It's 9 bytes shorter than `gzip`.
14
15
  */
15
- export async function zipBuffer(buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
16
+ export async function deflateBuffer(buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
16
17
  return await deflate(buf, options)
17
18
  }
18
19
 
20
+ export async function inflateBuffer(buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
21
+ return await inflate(buf, options)
22
+ }
23
+
24
+ /**
25
+ * deflateString uses `deflate`.
26
+ * It's 9 bytes shorter than `gzip`.
27
+ */
28
+ export async function deflateString(s: string, options?: ZlibOptions): Promise<Buffer> {
29
+ return await deflateBuffer(Buffer.from(s), options)
30
+ }
31
+
32
+ export async function inflateToString(buf: Buffer, options?: ZlibOptions): Promise<string> {
33
+ return (await inflateBuffer(buf, options)).toString()
34
+ }
35
+
19
36
  /**
20
37
  * gzipBuffer uses `gzip`
21
- * It's 9 bytes longer than deflate.
38
+ * It's 9 bytes longer than `deflate`.
22
39
  */
23
40
  export async function gzipBuffer(buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
24
41
  return await gzip(buf, options)
25
42
  }
26
43
 
27
- // zip > buffer
28
- export async function unzipBuffer(buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
29
- return await inflate(buf, options)
30
- }
31
-
32
44
  export async function gunzipBuffer(buf: Buffer, options: ZlibOptions = {}): Promise<Buffer> {
33
45
  return await gunzip(buf, options)
34
46
  }
35
47
 
36
- // convenience
37
- export async function zipString(s: string, options?: ZlibOptions): Promise<Buffer> {
38
- return await zipBuffer(Buffer.from(s), options)
39
- }
40
-
48
+ /**
49
+ * gzipString uses `gzip`.
50
+ * It's 9 bytes longer than `deflate`.
51
+ */
41
52
  export async function gzipString(s: string, options?: ZlibOptions): Promise<Buffer> {
42
53
  return await gzipBuffer(Buffer.from(s), options)
43
54
  }
44
55
 
45
- // convenience
46
- export async function unzipToString(buf: Buffer, options?: ZlibOptions): Promise<string> {
47
- return (await unzipBuffer(buf, options)).toString()
48
- }
49
-
50
56
  export async function gunzipToString(buf: Buffer, options?: ZlibOptions): Promise<string> {
51
57
  return (await gunzipBuffer(buf, options)).toString()
52
58
  }
@@ -1,17 +0,0 @@
1
- import { AsyncMapper } from '@naturalcycles/js-lib';
2
- import { TransformTyped } from '../../stream.model';
3
- import { TransformMapOptions } from '../transformMap';
4
- export declare function notNullishPredicate(item: any): boolean;
5
- /**
6
- * Like pMap, but for streams.
7
- * Inspired by `through2`.
8
- * Main feature is concurrency control (implemented via `through2-concurrent`) and convenient options.
9
- * Using this allows native stream .pipe() to work and use backpressure.
10
- *
11
- * Only works in objectMode (due to through2Concurrent).
12
- *
13
- * Concurrency defaults to 16.
14
- *
15
- * If an Array is returned by `mapper` - it will be flattened and multiple results will be emitted from it. Tested by Array.isArray().
16
- */
17
- export declare function transformMapLegacy<IN = any, OUT = IN>(mapper: AsyncMapper<IN, OUT>, opt?: TransformMapOptions<IN, OUT>): TransformTyped<IN, OUT>;
@@ -1,94 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformMapLegacy = exports.notNullishPredicate = void 0;
4
- const js_lib_1 = require("@naturalcycles/js-lib");
5
- const through2Concurrent = require("through2-concurrent");
6
- const colors_1 = require("../../../colors");
7
- function notNullishPredicate(item) {
8
- return item !== undefined && item !== null;
9
- }
10
- exports.notNullishPredicate = notNullishPredicate;
11
- /**
12
- * Like pMap, but for streams.
13
- * Inspired by `through2`.
14
- * Main feature is concurrency control (implemented via `through2-concurrent`) and convenient options.
15
- * Using this allows native stream .pipe() to work and use backpressure.
16
- *
17
- * Only works in objectMode (due to through2Concurrent).
18
- *
19
- * Concurrency defaults to 16.
20
- *
21
- * If an Array is returned by `mapper` - it will be flattened and multiple results will be emitted from it. Tested by Array.isArray().
22
- */
23
- function transformMapLegacy(mapper, opt = {}) {
24
- const { concurrency = 16, predicate = notNullishPredicate, errorMode = js_lib_1.ErrorMode.THROW_IMMEDIATELY, flattenArrayOutput, onError, beforeFinal, metric = 'stream', logger = console, } = opt;
25
- let index = -1;
26
- let isRejected = false;
27
- let errors = 0;
28
- const collectedErrors = []; // only used if errorMode == THROW_AGGREGATED
29
- return through2Concurrent.obj({
30
- maxConcurrency: concurrency,
31
- // autoDestroy: true,
32
- async final(cb) {
33
- // console.log('transformMap final')
34
- logErrorStats(logger, true);
35
- await beforeFinal?.(); // call beforeFinal if defined
36
- if (collectedErrors.length) {
37
- // emit Aggregated error
38
- cb(new js_lib_1.AggregatedError(collectedErrors));
39
- }
40
- else {
41
- // emit no error
42
- cb();
43
- }
44
- },
45
- }, async function transformMapFn(chunk, _encoding, cb) {
46
- index++;
47
- // console.log({chunk, _encoding})
48
- // Stop processing if THROW_IMMEDIATELY mode is used
49
- if (isRejected && errorMode === js_lib_1.ErrorMode.THROW_IMMEDIATELY)
50
- return cb();
51
- try {
52
- const currentIndex = index; // because we need to pass it to 2 functions - mapper and predicate. Refers to INPUT index (since it may return multiple outputs)
53
- const res = await mapper(chunk, currentIndex);
54
- const passedResults = await (0, js_lib_1.pFilter)(flattenArrayOutput && Array.isArray(res) ? res : [res], async (r) => await predicate(r, currentIndex));
55
- if (passedResults.length === 0) {
56
- cb(); // 0 results
57
- }
58
- else {
59
- passedResults.forEach(r => {
60
- this.push(r);
61
- // cb(null, r)
62
- });
63
- cb(); // done processing
64
- }
65
- }
66
- catch (err) {
67
- logger.error(err);
68
- errors++;
69
- logErrorStats(logger);
70
- if (onError) {
71
- try {
72
- onError(err, chunk);
73
- }
74
- catch { }
75
- }
76
- if (errorMode === js_lib_1.ErrorMode.THROW_IMMEDIATELY) {
77
- isRejected = true;
78
- // Emit error immediately
79
- return cb(err);
80
- }
81
- if (errorMode === js_lib_1.ErrorMode.THROW_AGGREGATED) {
82
- collectedErrors.push(err);
83
- }
84
- // Tell input stream that we're done processing, but emit nothing to output - not error nor result
85
- cb();
86
- }
87
- });
88
- function logErrorStats(logger, final = false) {
89
- if (!errors)
90
- return;
91
- logger.log(`${metric} ${final ? 'final ' : ''}errors: ${(0, colors_1.yellow)(errors)}`);
92
- }
93
- }
94
- exports.transformMapLegacy = transformMapLegacy;
@@ -1,133 +0,0 @@
1
- import { Transform } from 'stream'
2
- import {
3
- AggregatedError,
4
- AsyncMapper,
5
- CommonLogger,
6
- ErrorMode,
7
- pFilter,
8
- } from '@naturalcycles/js-lib'
9
- import through2Concurrent = require('through2-concurrent')
10
- import { yellow } from '../../../colors'
11
- import { TransformTyped } from '../../stream.model'
12
- import { TransformMapOptions } from '../transformMap'
13
-
14
- export function notNullishPredicate(item: any): boolean {
15
- return item !== undefined && item !== null
16
- }
17
-
18
- /**
19
- * Like pMap, but for streams.
20
- * Inspired by `through2`.
21
- * Main feature is concurrency control (implemented via `through2-concurrent`) and convenient options.
22
- * Using this allows native stream .pipe() to work and use backpressure.
23
- *
24
- * Only works in objectMode (due to through2Concurrent).
25
- *
26
- * Concurrency defaults to 16.
27
- *
28
- * If an Array is returned by `mapper` - it will be flattened and multiple results will be emitted from it. Tested by Array.isArray().
29
- */
30
- export function transformMapLegacy<IN = any, OUT = IN>(
31
- mapper: AsyncMapper<IN, OUT>,
32
- opt: TransformMapOptions<IN, OUT> = {},
33
- ): TransformTyped<IN, OUT> {
34
- const {
35
- concurrency = 16,
36
- predicate = notNullishPredicate,
37
- errorMode = ErrorMode.THROW_IMMEDIATELY,
38
- flattenArrayOutput,
39
- onError,
40
- beforeFinal,
41
- metric = 'stream',
42
- logger = console,
43
- } = opt
44
-
45
- let index = -1
46
- let isRejected = false
47
- let errors = 0
48
- const collectedErrors: Error[] = [] // only used if errorMode == THROW_AGGREGATED
49
-
50
- return through2Concurrent.obj(
51
- {
52
- maxConcurrency: concurrency,
53
- // autoDestroy: true,
54
- async final(cb) {
55
- // console.log('transformMap final')
56
-
57
- logErrorStats(logger, true)
58
-
59
- await beforeFinal?.() // call beforeFinal if defined
60
-
61
- if (collectedErrors.length) {
62
- // emit Aggregated error
63
- cb(new AggregatedError(collectedErrors))
64
- } else {
65
- // emit no error
66
- cb()
67
- }
68
- },
69
- },
70
- async function transformMapFn(
71
- this: Transform,
72
- chunk: IN,
73
- _encoding: any,
74
- cb: (...args: any[]) => any,
75
- ) {
76
- index++
77
- // console.log({chunk, _encoding})
78
-
79
- // Stop processing if THROW_IMMEDIATELY mode is used
80
- if (isRejected && errorMode === ErrorMode.THROW_IMMEDIATELY) return cb()
81
-
82
- try {
83
- const currentIndex = index // because we need to pass it to 2 functions - mapper and predicate. Refers to INPUT index (since it may return multiple outputs)
84
- const res = await mapper(chunk, currentIndex)
85
- const passedResults = await pFilter(
86
- flattenArrayOutput && Array.isArray(res) ? res : [res],
87
- async r => await predicate(r, currentIndex),
88
- )
89
-
90
- if (passedResults.length === 0) {
91
- cb() // 0 results
92
- } else {
93
- passedResults.forEach(r => {
94
- this.push(r)
95
- // cb(null, r)
96
- })
97
- cb() // done processing
98
- }
99
- } catch (err) {
100
- logger.error(err)
101
-
102
- errors++
103
-
104
- logErrorStats(logger)
105
-
106
- if (onError) {
107
- try {
108
- onError(err, chunk)
109
- } catch {}
110
- }
111
-
112
- if (errorMode === ErrorMode.THROW_IMMEDIATELY) {
113
- isRejected = true
114
- // Emit error immediately
115
- return cb(err)
116
- }
117
-
118
- if (errorMode === ErrorMode.THROW_AGGREGATED) {
119
- collectedErrors.push(err as Error)
120
- }
121
-
122
- // Tell input stream that we're done processing, but emit nothing to output - not error nor result
123
- cb()
124
- }
125
- },
126
- )
127
-
128
- function logErrorStats(logger: CommonLogger, final = false): void {
129
- if (!errors) return
130
-
131
- logger.log(`${metric} ${final ? 'final ' : ''}errors: ${yellow(errors)}`)
132
- }
133
- }