@naturalcycles/nodejs-lib 15.72.0 → 15.72.2

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.
@@ -129,7 +129,7 @@ class Exec2 {
129
129
  * log: true
130
130
  */
131
131
  async spawnAsync(cmd, opt = {}) {
132
- const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors } = opt;
132
+ const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors, stdio = 'inherit', } = opt;
133
133
  opt.log ??= true; // by default log should be true, as we are printing the output
134
134
  opt.logStart ??= opt.log;
135
135
  opt.logFinish ??= opt.log;
@@ -139,7 +139,7 @@ class Exec2 {
139
139
  const p = spawn(cmd, opt.args || [], {
140
140
  shell,
141
141
  cwd,
142
- stdio: 'inherit',
142
+ stdio,
143
143
  env: {
144
144
  ...(passProcessEnv ? process.env : {}),
145
145
  ...(forceColor ? { FORCE_COLOR: '1' } : {}),
@@ -43,11 +43,12 @@ class ProcessUtil {
43
43
  };
44
44
  }
45
45
  cpuInfo() {
46
- const c = os.cpus()[0];
46
+ const cpus = os.cpus();
47
+ const c = cpus[0];
47
48
  return {
48
- count: os.cpus().length,
49
- model: c.model,
50
- speed: c.speed,
49
+ count: cpus.length,
50
+ model: c?.model || '',
51
+ speed: c?.speed || 0,
51
52
  };
52
53
  }
53
54
  async cpuPercent(ms) {
@@ -61,6 +62,10 @@ class ProcessUtil {
61
62
  const endTotal = stats2.total;
62
63
  const idle = endIdle - startIdle;
63
64
  const total = endTotal - startTotal;
65
+ if (!total) {
66
+ resolve(0);
67
+ return;
68
+ }
64
69
  const perc = idle / total;
65
70
  resolve(Math.round((1 - perc) * 100));
66
71
  }, ms);
@@ -34,4 +34,5 @@ export interface BaseWorkerData {
34
34
  * @default 1000
35
35
  */
36
36
  logEvery: number;
37
+ silent?: boolean;
37
38
  }
@@ -1,8 +1,8 @@
1
1
  export const BASE62_REGEX = /^[a-zA-Z0-9]+$/;
2
2
  export const BASE64_REGEX = /^[a-zA-Z0-9+/]+={0,2}$/;
3
3
  export const BASE64URL_REGEX = /^[a-zA-Z0-9_-]+$/;
4
- // from `ajv-formats`
5
- export const UUID_REGEX = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
4
+ // from `ajv-formats`, case-insensitive via character class (not flag) for JSON Schema compatibility
5
+ export const UUID_REGEX = /^(?:urn:uuid:)?[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$/;
6
6
  export const COUNTRY_CODE_REGEX = /^[A-Z]{2}$/;
7
7
  export const CURRENCY_REGEX = /^[A-Z]{3}$/;
8
8
  /**
@@ -16,8 +16,8 @@ export const ID_REGEX = /^[a-zA-Z0-9_]{6,64}$/;
16
16
  export const IPV4_REGEX =
17
17
  // from `ajv-formats`
18
18
  /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
19
- // from `ajv-formats`
20
- export const IPV6_REGEX = /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i;
19
+ // from `ajv-formats`, case-insensitive via character class (not flag) for JSON Schema compatibility
20
+ export const IPV6_REGEX = /^((([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:))|(([0-9a-fA-F]{1,4}:){6}(:[0-9a-fA-F]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-fA-F]{1,4}:){5}(((:[0-9a-fA-F]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-fA-F]{1,4}:){4}(((:[0-9a-fA-F]{1,4}){1,3})|((:[0-9a-fA-F]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fA-F]{1,4}:){3}(((:[0-9a-fA-F]{1,4}){1,4})|((:[0-9a-fA-F]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fA-F]{1,4}:){2}(((:[0-9a-fA-F]{1,4}){1,5})|((:[0-9a-fA-F]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fA-F]{1,4}:){1}(((:[0-9a-fA-F]{1,4}){1,6})|((:[0-9a-fA-F]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-fA-F]{1,4}){1,7})|((:[0-9a-fA-F]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/;
21
21
  // IETF language tag (https://en.wikipedia.org/wiki/IETF_language_tag)
22
22
  export const LANGUAGE_TAG_REGEX = /^[a-z]{2}(-[A-Z]{2})?$/;
23
23
  export const MAC_ADDRESS_REGEX = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.72.0",
4
+ "version": "15.72.2",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -148,7 +148,14 @@ class Exec2 {
148
148
  * log: true
149
149
  */
150
150
  async spawnAsync(cmd: string, opt: SpawnOptions = {}): Promise<void> {
151
- const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors } = opt
151
+ const {
152
+ shell = true,
153
+ cwd,
154
+ env,
155
+ passProcessEnv = true,
156
+ forceColor = hasColors,
157
+ stdio = 'inherit',
158
+ } = opt
152
159
  opt.log ??= true // by default log should be true, as we are printing the output
153
160
  opt.logStart ??= opt.log
154
161
  opt.logFinish ??= opt.log
@@ -159,7 +166,7 @@ class Exec2 {
159
166
  const p = spawn(cmd, opt.args || [], {
160
167
  shell,
161
168
  cwd,
162
- stdio: 'inherit',
169
+ stdio,
163
170
  env: {
164
171
  ...(passProcessEnv ? process.env : {}),
165
172
  ...(forceColor ? { FORCE_COLOR: '1' } : {}),
@@ -64,11 +64,12 @@ class ProcessUtil {
64
64
  }
65
65
 
66
66
  cpuInfo(): { count: number; model: string; speed: number } {
67
- const c = os.cpus()[0]!
67
+ const cpus = os.cpus()
68
+ const c = cpus[0]
68
69
  return {
69
- count: os.cpus().length,
70
- model: c.model,
71
- speed: c.speed,
70
+ count: cpus.length,
71
+ model: c?.model || '',
72
+ speed: c?.speed || 0,
72
73
  }
73
74
  }
74
75
 
@@ -85,8 +86,12 @@ class ProcessUtil {
85
86
 
86
87
  const idle = endIdle - startIdle
87
88
  const total = endTotal - startTotal
88
- const perc = idle / total
89
+ if (!total) {
90
+ resolve(0)
91
+ return
92
+ }
89
93
 
94
+ const perc = idle / total
90
95
  resolve(Math.round((1 - perc) * 100))
91
96
  }, ms)
92
97
  })
@@ -42,4 +42,6 @@ export interface BaseWorkerData {
42
42
  * @default 1000
43
43
  */
44
44
  logEvery: number
45
+
46
+ silent?: boolean
45
47
  }
@@ -1,7 +1,7 @@
1
1
  const started = Date.now()
2
2
  import { workerData, parentPort } from 'node:worker_threads'
3
3
  import { inspect } from 'node:util'
4
- const { workerFile, workerIndex, logEvery = 1000, metric = 'worker' } = workerData || {}
4
+ const { workerFile, workerIndex, logEvery = 1000, metric = 'worker', silent } = workerData || {}
5
5
 
6
6
  if (!workerFile) {
7
7
  throw new Error('workerData.workerFile is required!')
@@ -17,7 +17,7 @@ try {
17
17
  const { WorkerClass } = await import(workerFile)
18
18
  const worker = new WorkerClass(workerData)
19
19
 
20
- console.log(`${metric}#${workerIndex} loaded in ${Date.now() - started} ms`)
20
+ log(`${metric}#${workerIndex} loaded in ${Date.now() - started} ms`)
21
21
 
22
22
  let errors = 0
23
23
  let processed = 0
@@ -52,7 +52,7 @@ parentPort.on('message', async msg => {
52
52
  })
53
53
 
54
54
  errors++
55
- console.log(`${metric}#${workerIndex} errors: ${errors}`)
55
+ log(`${metric}#${workerIndex} errors: ${errors}`)
56
56
  }
57
57
  })
58
58
 
@@ -64,7 +64,7 @@ const inspectOpt = {
64
64
  function logStats(final) {
65
65
  const { rss, heapUsed, heapTotal, external } = process.memoryUsage()
66
66
 
67
- console.log(
67
+ log(
68
68
  inspect(
69
69
  {
70
70
  [`${metric}${workerIndex}`]: processed,
@@ -83,3 +83,8 @@ function logStats(final) {
83
83
  function mb(b) {
84
84
  return Math.round(b / (1024 * 1024))
85
85
  }
86
+
87
+ function log(...args) {
88
+ if (silent) return
89
+ console.log(...args)
90
+ }
@@ -1,8 +1,8 @@
1
1
  export const BASE62_REGEX = /^[a-zA-Z0-9]+$/
2
2
  export const BASE64_REGEX = /^[a-zA-Z0-9+/]+={0,2}$/
3
3
  export const BASE64URL_REGEX = /^[a-zA-Z0-9_-]+$/
4
- // from `ajv-formats`
5
- export const UUID_REGEX = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i
4
+ // from `ajv-formats`, case-insensitive via character class (not flag) for JSON Schema compatibility
5
+ export const UUID_REGEX = /^(?:urn:uuid:)?[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$/
6
6
  export const COUNTRY_CODE_REGEX = /^[A-Z]{2}$/
7
7
  export const CURRENCY_REGEX = /^[A-Z]{3}$/
8
8
  /**
@@ -16,9 +16,9 @@ export const ID_REGEX = /^[a-zA-Z0-9_]{6,64}$/
16
16
  export const IPV4_REGEX =
17
17
  // from `ajv-formats`
18
18
  /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/
19
- // from `ajv-formats`
19
+ // from `ajv-formats`, case-insensitive via character class (not flag) for JSON Schema compatibility
20
20
  export const IPV6_REGEX =
21
- /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i
21
+ /^((([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:))|(([0-9a-fA-F]{1,4}:){6}(:[0-9a-fA-F]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-fA-F]{1,4}:){5}(((:[0-9a-fA-F]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-fA-F]{1,4}:){4}(((:[0-9a-fA-F]{1,4}){1,3})|((:[0-9a-fA-F]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fA-F]{1,4}:){3}(((:[0-9a-fA-F]{1,4}){1,4})|((:[0-9a-fA-F]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fA-F]{1,4}:){2}(((:[0-9a-fA-F]{1,4}){1,5})|((:[0-9a-fA-F]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-fA-F]{1,4}:){1}(((:[0-9a-fA-F]{1,4}){1,6})|((:[0-9a-fA-F]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-fA-F]{1,4}){1,7})|((:[0-9a-fA-F]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/
22
22
  // IETF language tag (https://en.wikipedia.org/wiki/IETF_language_tag)
23
23
  export const LANGUAGE_TAG_REGEX = /^[a-z]{2}(-[A-Z]{2})?$/
24
24
  export const MAC_ADDRESS_REGEX = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/