@naturalcycles/nodejs-lib 12.47.4 → 12.49.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.
@@ -1 +1,6 @@
1
- export declare function hasColors(): boolean;
1
+ /**
2
+ * Based on: https://github.com/sindresorhus/yoctocolors/pull/5
3
+ *
4
+ * @experimental
5
+ */
6
+ export declare const hasColors: boolean;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.hasColors = void 0;
4
- const supporsColorLib = require('supports-color');
5
- function hasColors() {
6
- if (process.env['NO_COLOR'])
7
- return false; // https://no-color.org/
8
- return !!supporsColorLib.stdout;
9
- }
10
- exports.hasColors = hasColors;
4
+ const tty = require("tty");
5
+ /**
6
+ * Based on: https://github.com/sindresorhus/yoctocolors/pull/5
7
+ *
8
+ * @experimental
9
+ */
10
+ exports.hasColors = !process.env['NO_COLOR'] && tty.WriteStream.prototype.hasColors();
@@ -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.
@@ -8,14 +8,14 @@ const time_lib_1 = require("@naturalcycles/time-lib");
8
8
  const colors_1 = require("../../colors");
9
9
  const colors_2 = require("../../colors/colors");
10
10
  const inspectOpt = {
11
- colors: (0, colors_2.hasColors)(),
11
+ colors: colors_2.hasColors,
12
12
  breakLength: 300,
13
13
  };
14
14
  /**
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 batchedProgress = progress * batchSize;
50
+ const lastRPS = (processedLastSecond * batchedProgress) / ((now - lastSecondStarted) / 1000) || 0;
51
+ const rpsTotal = Math.round(batchedProgress / ((now - started) / 1000)) || 0;
51
52
  lastSecondStarted = now;
52
53
  processedLastSecond = 0;
53
54
  const rps10 = Math.round(sma.push(lastRPS));
54
55
  if (mem.rss > peakRSS)
55
56
  peakRSS = mem.rss;
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) } : {}),
@@ -71,14 +72,14 @@ function transformLogProgress(opt = {}) {
71
72
  : {}),
72
73
  }, inspectOpt));
73
74
  if (tenx) {
74
- let perHour = Math.round((progress * 1000 * 60 * 60) / (now - started)) || 0;
75
+ let perHour = Math.round((batchedProgress * 1000 * 60 * 60) / (now - started)) || 0;
75
76
  if (perHour > 900) {
76
77
  perHour = Math.round(perHour / 1000) + 'K';
77
78
  }
78
- console.log(`${(0, colors_1.dimGrey)((0, time_lib_1.dayjs)().toPretty())} ${(0, colors_1.white)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} so far to process ${(0, colors_1.yellow)(progress)} rows, ~${(0, colors_1.yellow)(perHour)}/hour`);
79
+ console.log(`${(0, colors_1.dimGrey)((0, time_lib_1.dayjs)().toPretty())} ${(0, colors_1.white)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} so far to process ${(0, colors_1.yellow)(batchedProgress)} rows, ~${(0, colors_1.yellow)(perHour)}/hour`);
79
80
  }
80
81
  else if (final) {
81
- console.log(`${(0, colors_1.boldWhite)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} to process ${(0, colors_1.yellow)(progress)} rows with total RPS of ${(0, colors_1.yellow)(rpsTotal)}`);
82
+ console.log(`${(0, colors_1.boldWhite)(metric)} took ${(0, colors_1.yellow)((0, js_lib_1._since)(started))} to process ${(0, colors_1.yellow)(batchedProgress)} rows with total RPS of ${(0, colors_1.yellow)(rpsTotal)}`);
82
83
  }
83
84
  }
84
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.47.4",
3
+ "version": "12.49.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -37,7 +37,6 @@
37
37
  "move-file": "^2.0.0",
38
38
  "nanoid": "^3.0.0",
39
39
  "sanitize-html": "^2.5.2",
40
- "supports-color": "^8.0.0",
41
40
  "through2-concurrent": "^2.0.0",
42
41
  "yargs": "^17.0.0"
43
42
  },
@@ -1,6 +1,8 @@
1
- const supporsColorLib = require('supports-color')
1
+ import * as tty from 'tty'
2
2
 
3
- export function hasColors(): boolean {
4
- if (process.env['NO_COLOR']) return false // https://no-color.org/
5
- return !!supporsColorLib.stdout
6
- }
3
+ /**
4
+ * Based on: https://github.com/sindresorhus/yoctocolors/pull/5
5
+ *
6
+ * @experimental
7
+ */
8
+ export const hasColors = !process.env['NO_COLOR'] && tty.WriteStream.prototype.hasColors()
@@ -92,10 +92,19 @@ 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 = {
98
- colors: hasColors(),
107
+ colors: hasColors,
99
108
  breakLength: 300,
100
109
  }
101
110
 
@@ -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,8 +163,10 @@ 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 batchedProgress = progress * batchSize
167
+ const lastRPS =
168
+ (processedLastSecond * batchedProgress) / ((now - lastSecondStarted) / 1000) || 0
169
+ const rpsTotal = Math.round(batchedProgress / ((now - started) / 1000)) || 0
158
170
  lastSecondStarted = now
159
171
  processedLastSecond = 0
160
172
 
@@ -164,7 +176,7 @@ export function transformLogProgress<IN = any>(
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) } : {}),
@@ -185,7 +197,8 @@ export function transformLogProgress<IN = any>(
185
197
  )
186
198
 
187
199
  if (tenx) {
188
- let perHour: number | string = Math.round((progress * 1000 * 60 * 60) / (now - started)) || 0
200
+ let perHour: number | string =
201
+ Math.round((batchedProgress * 1000 * 60 * 60) / (now - started)) || 0
189
202
  if (perHour > 900) {
190
203
  perHour = Math.round(perHour / 1000) + 'K'
191
204
  }
@@ -193,12 +206,12 @@ export function transformLogProgress<IN = any>(
193
206
  console.log(
194
207
  `${dimGrey(dayjs().toPretty())} ${white(metric)} took ${yellow(
195
208
  _since(started),
196
- )} so far to process ${yellow(progress)} rows, ~${yellow(perHour)}/hour`,
209
+ )} so far to process ${yellow(batchedProgress)} rows, ~${yellow(perHour)}/hour`,
197
210
  )
198
211
  } else if (final) {
199
212
  console.log(
200
213
  `${boldWhite(metric)} took ${yellow(_since(started))} to process ${yellow(
201
- progress,
214
+ batchedProgress,
202
215
  )} rows with total RPS of ${yellow(rpsTotal)}`,
203
216
  )
204
217
  }