@naturalcycles/nodejs-lib 12.47.2 → 12.48.1

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.
@@ -9,7 +9,7 @@ const util_1 = require("util");
9
9
  exports._pipeline = (0, util_1.promisify)(stream_1.pipeline);
10
10
  // Workaround https://github.com/nodejs/node/issues/40191
11
11
  // todo: remove it when fix is released in 16.x and in AppEngine 16.x
12
- if (process.version > 'v16.13') {
12
+ if (process.version >= 'v16.10') {
13
13
  const { pipeline } = require('stream/promises');
14
14
  exports._pipeline = ((streams) => pipeline(...streams));
15
15
  }
@@ -75,6 +75,14 @@ export interface TransformLogProgressOptions<IN = any> extends TransformOptions
75
75
  * chunk is undefined for "final" stats, otherwise is defined.
76
76
  */
77
77
  extra?: (chunk: IN | undefined, index: number) => AnyObject;
78
+ /**
79
+ * If specified - will multiply the counter by this number.
80
+ * Useful e.g when using `transformBuffer({ batchSize: 500 })`, so
81
+ * it'll accurately represent the number of processed entries (not batches).
82
+ *
83
+ * Defaults to 1.
84
+ */
85
+ batchSize?: number;
78
86
  }
79
87
  /**
80
88
  * Pass-through transform that optionally logs progress.
@@ -15,7 +15,7 @@ const inspectOpt = {
15
15
  * Pass-through transform that optionally logs progress.
16
16
  */
17
17
  function transformLogProgress(opt = {}) {
18
- const { metric = 'progress', heapTotal: logHeapTotal = false, heapUsed: logHeapUsed = false, rss: logRss = true, peakRSS: logPeakRSS = true, logRPS = true, logEvery = 1000, extra, } = opt;
18
+ const { metric = 'progress', heapTotal: logHeapTotal = false, heapUsed: logHeapUsed = false, rss: logRss = true, peakRSS: logPeakRSS = true, logRPS = true, logEvery = 1000, batchSize = 1, extra, } = opt;
19
19
  const logProgress = opt.logProgress !== false && logEvery !== 0; // true by default
20
20
  const logEvery10 = logEvery * 10;
21
21
  const started = Date.now();
@@ -46,15 +46,16 @@ function transformLogProgress(opt = {}) {
46
46
  return;
47
47
  const mem = process.memoryUsage();
48
48
  const now = Date.now();
49
- const lastRPS = processedLastSecond / ((now - lastSecondStarted) / 1000) || 0;
50
- const rpsTotal = Math.round(progress / ((now - started) / 1000)) || 0;
49
+ const lastRPS = (processedLastSecond * batchSize) / ((now - lastSecondStarted) / 1000) || 0;
50
+ const rpsTotal = Math.round((progress * batchSize) / ((now - started) / 1000)) || 0;
51
51
  lastSecondStarted = now;
52
52
  processedLastSecond = 0;
53
53
  const rps10 = Math.round(sma.push(lastRPS));
54
54
  if (mem.rss > peakRSS)
55
55
  peakRSS = mem.rss;
56
+ const batchedProgress = progress * batchSize;
56
57
  console.log((0, util_1.inspect)({
57
- [final ? `${metric}_final` : metric]: progress,
58
+ [final ? `${metric}_final` : metric]: batchedProgress,
58
59
  ...(extra ? extra(chunk, progress) : {}),
59
60
  ...(logHeapUsed ? { heapUsed: (0, js_lib_1._mb)(mem.heapUsed) } : {}),
60
61
  ...(logHeapTotal ? { heapTotal: (0, js_lib_1._mb)(mem.heapTotal) } : {}),
@@ -130,6 +130,7 @@ function createError(value, err, objectName) {
130
130
  // Make annotation non-enumerable, to not get it automatically printed,
131
131
  // but still accessible
132
132
  Object.defineProperty(data, 'annotation', {
133
+ configurable: true,
133
134
  enumerable: false,
134
135
  value: annotation,
135
136
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.47.2",
3
+ "version": "12.48.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -10,7 +10,7 @@ export let _pipeline = promisify(pipeline)
10
10
 
11
11
  // Workaround https://github.com/nodejs/node/issues/40191
12
12
  // todo: remove it when fix is released in 16.x and in AppEngine 16.x
13
- if (process.version > 'v16.13') {
13
+ if (process.version >= 'v16.10') {
14
14
  const { pipeline } = require('stream/promises')
15
15
  _pipeline = ((streams: AnyStream[]) => pipeline(...streams)) as any
16
16
  }
@@ -92,6 +92,15 @@ export interface TransformLogProgressOptions<IN = any> extends TransformOptions
92
92
  * chunk is undefined for "final" stats, otherwise is defined.
93
93
  */
94
94
  extra?: (chunk: IN | undefined, index: number) => AnyObject
95
+
96
+ /**
97
+ * If specified - will multiply the counter by this number.
98
+ * Useful e.g when using `transformBuffer({ batchSize: 500 })`, so
99
+ * it'll accurately represent the number of processed entries (not batches).
100
+ *
101
+ * Defaults to 1.
102
+ */
103
+ batchSize?: number
95
104
  }
96
105
 
97
106
  const inspectOpt: InspectOptions = {
@@ -113,6 +122,7 @@ export function transformLogProgress<IN = any>(
113
122
  peakRSS: logPeakRSS = true,
114
123
  logRPS = true,
115
124
  logEvery = 1000,
125
+ batchSize = 1,
116
126
  extra,
117
127
  } = opt
118
128
  const logProgress = opt.logProgress !== false && logEvery !== 0 // true by default
@@ -153,18 +163,20 @@ export function transformLogProgress<IN = any>(
153
163
  const mem = process.memoryUsage()
154
164
 
155
165
  const now = Date.now()
156
- const lastRPS = processedLastSecond / ((now - lastSecondStarted) / 1000) || 0
157
- const rpsTotal = Math.round(progress / ((now - started) / 1000)) || 0
166
+ const lastRPS = (processedLastSecond * batchSize) / ((now - lastSecondStarted) / 1000) || 0
167
+ const rpsTotal = Math.round((progress * batchSize) / ((now - started) / 1000)) || 0
158
168
  lastSecondStarted = now
159
169
  processedLastSecond = 0
160
170
 
161
171
  const rps10 = Math.round(sma.push(lastRPS))
162
172
  if (mem.rss > peakRSS) peakRSS = mem.rss
163
173
 
174
+ const batchedProgress = progress * batchSize
175
+
164
176
  console.log(
165
177
  inspect(
166
178
  {
167
- [final ? `${metric}_final` : metric]: progress,
179
+ [final ? `${metric}_final` : metric]: batchedProgress,
168
180
  ...(extra ? extra(chunk, progress) : {}),
169
181
  ...(logHeapUsed ? { heapUsed: _mb(mem.heapUsed) } : {}),
170
182
  ...(logHeapTotal ? { heapTotal: _mb(mem.heapTotal) } : {}),
@@ -172,6 +172,7 @@ function createError(value: any, err: ValidationError, objectName?: string): Joi
172
172
  // Make annotation non-enumerable, to not get it automatically printed,
173
173
  // but still accessible
174
174
  Object.defineProperty(data, 'annotation', {
175
+ configurable: true,
175
176
  enumerable: false,
176
177
  value: annotation,
177
178
  })