@naturalcycles/nodejs-lib 12.64.3 → 12.65.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.
@@ -178,6 +178,11 @@ function gotBeforeRetryHook(opt) {
178
178
  return (options, err, retryCount) => {
179
179
  // opt.logger!.log('beforeRetry', retryCount)
180
180
  const statusCode = err?.response?.statusCode || 0;
181
+ if (statusCode && statusCode < 300) {
182
+ // todo: possibly remove the log message completely in the future
183
+ opt.logger.log(`skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`);
184
+ return;
185
+ }
181
186
  const { method, url, prefixUrl } = options;
182
187
  const shortUrl = getShortUrl(opt, url, prefixUrl);
183
188
  const { started } = options.context;
@@ -26,5 +26,7 @@ export interface RunScriptOptions {
26
26
  * - No need to add `.catch(err => { console.error(err); process.exit(1) })`
27
27
  *
28
28
  * This function is kept light, dependency-free, exported separately.
29
+ *
30
+ * Set env DEBUG_RUN_SCRIPT for extra debugging.
29
31
  */
30
32
  export declare function runScript(fn: (...args: any[]) => any, opt?: RunScriptOptions): void;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runScript = void 0;
4
+ const { DEBUG_RUN_SCRIPT } = process.env;
4
5
  /**
5
6
  * Use it in your top-level scripts like this:
6
7
  *
@@ -16,6 +17,8 @@ exports.runScript = void 0;
16
17
  * - No need to add `.catch(err => { console.error(err); process.exit(1) })`
17
18
  *
18
19
  * This function is kept light, dependency-free, exported separately.
20
+ *
21
+ * Set env DEBUG_RUN_SCRIPT for extra debugging.
19
22
  */
20
23
  function runScript(fn, opt = {}) {
21
24
  const { logger = console, noExit } = opt;
@@ -25,9 +28,17 @@ function runScript(fn, opt = {}) {
25
28
  process.on('unhandledRejection', err => {
26
29
  logger.error('unhandledRejection:', err);
27
30
  });
31
+ if (DEBUG_RUN_SCRIPT) {
32
+ process.on('exit', code => logger.log(`process.exit event, code=${code}`));
33
+ process.on('beforeExit', code => logger.log(`process.beforeExit event, code=${code}`));
34
+ }
35
+ // fake timeout, to ensure node.js process won't exit until runScript main promise is resolved
36
+ const timeout = setTimeout(() => { }, 10000000);
28
37
  void (async () => {
29
38
  try {
30
39
  await fn();
40
+ if (DEBUG_RUN_SCRIPT)
41
+ logger.log(`runScript promise resolved`);
31
42
  if (!noExit) {
32
43
  setImmediate(() => process.exit(0));
33
44
  }
@@ -39,6 +50,9 @@ function runScript(fn, opt = {}) {
39
50
  setImmediate(() => process.exit(1));
40
51
  }
41
52
  }
53
+ finally {
54
+ clearTimeout(timeout);
55
+ }
42
56
  })();
43
57
  }
44
58
  exports.runScript = runScript;
@@ -23,7 +23,7 @@ class LRUMemoCache {
23
23
  this.lru.set(k, v);
24
24
  }
25
25
  clear() {
26
- this.lru.reset();
26
+ this.lru.clear();
27
27
  }
28
28
  }
29
29
  exports.LRUMemoCache = LRUMemoCache;
@@ -13,7 +13,7 @@ const ajvSchema_1 = require("./ajvSchema");
13
13
  * @experimental
14
14
  */
15
15
  function readJsonSchemas(patterns, opt) {
16
- return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf-8')));
16
+ return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')));
17
17
  }
18
18
  exports.readJsonSchemas = readJsonSchemas;
19
19
  /**
@@ -60,7 +60,7 @@ class AjvSchema {
60
60
  */
61
61
  static readJsonSync(filePath, cfg = {}) {
62
62
  (0, index_1.requireFileToExist)(filePath);
63
- const schema = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
63
+ const schema = JSON.parse(fs.readFileSync(filePath, 'utf8'));
64
64
  return new AjvSchema(schema, cfg);
65
65
  }
66
66
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.64.3",
3
+ "version": "12.65.2",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "@naturalcycles/js-lib": "^14.0.0",
18
18
  "@naturalcycles/time-lib": "^3.0.1",
19
- "@types/lru-cache": "^5.1.0",
19
+ "@types/lru-cache": "^7.4.0",
20
20
  "@types/through2-concurrent": "^2.0.0",
21
21
  "ajv": "^8.6.2",
22
22
  "ajv-formats": "^2.1.0",
@@ -32,7 +32,7 @@
32
32
  "globby": "^11.0.0",
33
33
  "got": "^11.0.1",
34
34
  "joi": "17.4.2",
35
- "lru-cache": "^6.0.0",
35
+ "lru-cache": "^7.4.0",
36
36
  "move-file": "^2.0.0",
37
37
  "nanoid": "^3.0.0",
38
38
  "through2-concurrent": "^2.0.0",
package/src/got/getGot.ts CHANGED
@@ -204,6 +204,15 @@ function gotBeforeRetryHook(opt: GetGotOptions): BeforeRetryHook {
204
204
  return (options, err, retryCount) => {
205
205
  // opt.logger!.log('beforeRetry', retryCount)
206
206
  const statusCode = err?.response?.statusCode || 0
207
+
208
+ if (statusCode && statusCode < 300) {
209
+ // todo: possibly remove the log message completely in the future
210
+ opt.logger!.log(
211
+ `skipping got.beforeRetry hook as statusCode is ${statusCode}, err.msg is ${err?.message}`,
212
+ )
213
+ return
214
+ }
215
+
207
216
  const { method, url, prefixUrl } = options
208
217
  const shortUrl = getShortUrl(opt, url, prefixUrl)
209
218
  const { started } = options.context as GotRequestContext
@@ -14,6 +14,8 @@ export interface RunScriptOptions {
14
14
  logger?: CommonLogger
15
15
  }
16
16
 
17
+ const { DEBUG_RUN_SCRIPT } = process.env
18
+
17
19
  /**
18
20
  * Use it in your top-level scripts like this:
19
21
  *
@@ -29,6 +31,8 @@ export interface RunScriptOptions {
29
31
  * - No need to add `.catch(err => { console.error(err); process.exit(1) })`
30
32
  *
31
33
  * This function is kept light, dependency-free, exported separately.
34
+ *
35
+ * Set env DEBUG_RUN_SCRIPT for extra debugging.
32
36
  */
33
37
  export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {}): void {
34
38
  const { logger = console, noExit } = opt
@@ -40,10 +44,20 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
40
44
  logger.error('unhandledRejection:', err)
41
45
  })
42
46
 
47
+ if (DEBUG_RUN_SCRIPT) {
48
+ process.on('exit', code => logger.log(`process.exit event, code=${code}`))
49
+ process.on('beforeExit', code => logger.log(`process.beforeExit event, code=${code}`))
50
+ }
51
+
52
+ // fake timeout, to ensure node.js process won't exit until runScript main promise is resolved
53
+ const timeout = setTimeout(() => {}, 10000000)
54
+
43
55
  void (async () => {
44
56
  try {
45
57
  await fn()
46
58
 
59
+ if (DEBUG_RUN_SCRIPT) logger.log(`runScript promise resolved`)
60
+
47
61
  if (!noExit) {
48
62
  setImmediate(() => process.exit(0))
49
63
  }
@@ -53,6 +67,8 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
53
67
  if (!noExit) {
54
68
  setImmediate(() => process.exit(1))
55
69
  }
70
+ } finally {
71
+ clearTimeout(timeout)
56
72
  }
57
73
  })()
58
74
  }
@@ -30,6 +30,6 @@ export class LRUMemoCache implements MemoCache {
30
30
  }
31
31
 
32
32
  clear(): void {
33
- this.lru.reset()
33
+ this.lru.clear()
34
34
  }
35
35
  }
@@ -13,7 +13,7 @@ import { AjvSchema, AjvSchemaCfg } from './ajvSchema'
13
13
  * @experimental
14
14
  */
15
15
  export function readJsonSchemas(patterns: string | string[], opt?: GlobbyOptions): JsonSchema[] {
16
- return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf-8')))
16
+ return globby.sync(patterns, opt).map(fileName => JSON.parse(fs.readFileSync(fileName, 'utf8')))
17
17
  }
18
18
 
19
19
  /**
@@ -132,7 +132,7 @@ export class AjvSchema<T = unknown> {
132
132
  cfg: Partial<AjvSchemaCfg> = {},
133
133
  ): AjvSchema<T> {
134
134
  requireFileToExist(filePath)
135
- const schema = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
135
+ const schema = JSON.parse(fs.readFileSync(filePath, 'utf8'))
136
136
  return new AjvSchema<T>(schema, cfg)
137
137
  }
138
138