@naturalcycles/js-lib 14.153.2 → 14.153.4

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.
@@ -8,6 +8,7 @@ const httpRequestError_1 = require("../error/httpRequestError");
8
8
  const number_util_1 = require("../number/number.util");
9
9
  const object_util_1 = require("../object/object.util");
10
10
  const pDelay_1 = require("../promise/pDelay");
11
+ const pTimeout_1 = require("../promise/pTimeout");
11
12
  const json_util_1 = require("../string/json.util");
12
13
  const stringifyAny_1 = require("../string/stringifyAny");
13
14
  const time_util_1 = require("../time/time.util");
@@ -119,7 +120,11 @@ class Fetcher {
119
120
  const abortController = new AbortController();
120
121
  req.init.signal = abortController.signal;
121
122
  timeout = setTimeout(() => {
122
- abortController.abort(`timeout of ${timeoutSeconds} sec`);
123
+ // Apparently, providing a `string` reason to abort() causes Undici to throw `invalid_argument` error,
124
+ // so, we're wrapping it in a TimeoutError instance
125
+ abortController.abort(new pTimeout_1.TimeoutError(`request timed out after ${timeoutSeconds} sec`));
126
+ // abortController.abort(`timeout of ${timeoutSeconds} sec`)
127
+ // abortController.abort()
123
128
  }, timeoutSeconds * 1000);
124
129
  }
125
130
  for (const hook of this.cfg.hooks.beforeRequest || []) {
@@ -246,7 +251,7 @@ class Fetcher {
246
251
  ]
247
252
  .filter(Boolean)
248
253
  .join(' '));
249
- if (this.cfg.logResponseBody) {
254
+ if (this.cfg.logResponseBody && res.body !== undefined) {
250
255
  logger.log(res.body);
251
256
  }
252
257
  }
@@ -447,9 +452,10 @@ class Fetcher {
447
452
  method: cfg.method || 'GET',
448
453
  headers: cfg.headers || {},
449
454
  credentials: cfg.credentials,
455
+ redirect: cfg.redirect,
450
456
  },
451
457
  hooks: {},
452
- }, (0, object_util_1._omit)(cfg, ['method', 'credentials', 'headers', 'logger']));
458
+ }, (0, object_util_1._omit)(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger']));
453
459
  norm.init.headers = (0, object_util_1._mapKeys)(norm.init.headers, k => k.toLowerCase());
454
460
  return norm;
455
461
  }
@@ -480,7 +486,7 @@ class Fetcher {
480
486
  ...this.cfg.init,
481
487
  method: opt.method || this.cfg.init.method,
482
488
  credentials: opt.credentials || this.cfg.init.credentials,
483
- redirect: opt.redirect || 'follow',
489
+ redirect: opt.redirect || this.cfg.init.redirect || 'follow',
484
490
  }, {
485
491
  headers: (0, object_util_1._mapKeys)(opt.headers || {}, k => k.toLowerCase()),
486
492
  }),
@@ -2,7 +2,7 @@ import type { CommonLogger } from '../log/commonLogger';
2
2
  import type { Promisable } from '../typeFest';
3
3
  import type { Reviver, UnixTimestampMillisNumber } from '../types';
4
4
  import type { HttpMethod, HttpStatusFamily } from './http.model';
5
- export interface FetcherNormalizedCfg extends Required<FetcherCfg>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody'> {
5
+ export interface FetcherNormalizedCfg extends Required<FetcherCfg>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody' | 'redirect' | 'credentials'> {
6
6
  logger: CommonLogger;
7
7
  searchParams: Record<string, any>;
8
8
  }
@@ -5,6 +5,7 @@ import { HttpRequestError } from '../error/httpRequestError';
5
5
  import { _clamp } from '../number/number.util';
6
6
  import { _filterNullishValues, _filterUndefinedValues, _mapKeys, _merge, _omit, _pick, } from '../object/object.util';
7
7
  import { pDelay } from '../promise/pDelay';
8
+ import { TimeoutError } from '../promise/pTimeout';
8
9
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util';
9
10
  import { _stringifyAny } from '../string/stringifyAny';
10
11
  import { _since } from '../time/time.util';
@@ -104,7 +105,11 @@ export class Fetcher {
104
105
  const abortController = new AbortController();
105
106
  req.init.signal = abortController.signal;
106
107
  timeout = setTimeout(() => {
107
- abortController.abort(`timeout of ${timeoutSeconds} sec`);
108
+ // Apparently, providing a `string` reason to abort() causes Undici to throw `invalid_argument` error,
109
+ // so, we're wrapping it in a TimeoutError instance
110
+ abortController.abort(new TimeoutError(`request timed out after ${timeoutSeconds} sec`));
111
+ // abortController.abort(`timeout of ${timeoutSeconds} sec`)
112
+ // abortController.abort()
108
113
  }, timeoutSeconds * 1000);
109
114
  }
110
115
  for (const hook of this.cfg.hooks.beforeRequest || []) {
@@ -231,7 +236,7 @@ export class Fetcher {
231
236
  ]
232
237
  .filter(Boolean)
233
238
  .join(' '));
234
- if (this.cfg.logResponseBody) {
239
+ if (this.cfg.logResponseBody && res.body !== undefined) {
235
240
  logger.log(res.body);
236
241
  }
237
242
  }
@@ -437,9 +442,10 @@ export class Fetcher {
437
442
  method: cfg.method || 'GET',
438
443
  headers: cfg.headers || {},
439
444
  credentials: cfg.credentials,
445
+ redirect: cfg.redirect,
440
446
  },
441
447
  hooks: {},
442
- }, _omit(cfg, ['method', 'credentials', 'headers', 'logger']));
448
+ }, _omit(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger']));
443
449
  norm.init.headers = _mapKeys(norm.init.headers, k => k.toLowerCase());
444
450
  return norm;
445
451
  }
@@ -456,7 +462,7 @@ export class Fetcher {
456
462
  'logRequestBody',
457
463
  'logResponse',
458
464
  'logResponseBody',
459
- ])), { started: Date.now() }), _omit(opt, ['method', 'headers', 'credentials'])), { inputUrl: opt.url || '', fullUrl: opt.url || '', retry: Object.assign(Object.assign({}, this.cfg.retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: opt.redirect || 'follow' }), {
465
+ ])), { started: Date.now() }), _omit(opt, ['method', 'headers', 'credentials'])), { inputUrl: opt.url || '', fullUrl: opt.url || '', retry: Object.assign(Object.assign({}, this.cfg.retry), _filterUndefinedValues(opt.retry || {})), init: _merge(Object.assign(Object.assign({}, this.cfg.init), { method: opt.method || this.cfg.init.method, credentials: opt.credentials || this.cfg.init.credentials, redirect: opt.redirect || this.cfg.init.redirect || 'follow' }), {
460
466
  headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),
461
467
  }) });
462
468
  // setup url
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.153.2",
3
+ "version": "14.153.4",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -7,7 +7,14 @@ export interface FetcherNormalizedCfg
7
7
  extends Required<FetcherCfg>,
8
8
  Omit<
9
9
  FetcherRequest,
10
- 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody'
10
+ | 'started'
11
+ | 'fullUrl'
12
+ | 'logRequest'
13
+ | 'logRequestBody'
14
+ | 'logResponse'
15
+ | 'logResponseBody'
16
+ | 'redirect'
17
+ | 'credentials'
11
18
  > {
12
19
  logger: CommonLogger
13
20
  searchParams: Record<string, any>
@@ -14,6 +14,7 @@ import {
14
14
  _pick,
15
15
  } from '../object/object.util'
16
16
  import { pDelay } from '../promise/pDelay'
17
+ import { TimeoutError } from '../promise/pTimeout'
17
18
  import { _jsonParse, _jsonParseIfPossible } from '../string/json.util'
18
19
  import { _stringifyAny } from '../string/stringifyAny'
19
20
  import { _since } from '../time/time.util'
@@ -177,7 +178,11 @@ export class Fetcher {
177
178
  const abortController = new AbortController()
178
179
  req.init.signal = abortController.signal
179
180
  timeout = setTimeout(() => {
180
- abortController.abort(`timeout of ${timeoutSeconds} sec`)
181
+ // Apparently, providing a `string` reason to abort() causes Undici to throw `invalid_argument` error,
182
+ // so, we're wrapping it in a TimeoutError instance
183
+ abortController.abort(new TimeoutError(`request timed out after ${timeoutSeconds} sec`))
184
+ // abortController.abort(`timeout of ${timeoutSeconds} sec`)
185
+ // abortController.abort()
181
186
  }, timeoutSeconds * 1000) as any as number
182
187
  }
183
188
 
@@ -319,7 +324,7 @@ export class Fetcher {
319
324
  .join(' '),
320
325
  )
321
326
 
322
- if (this.cfg.logResponseBody) {
327
+ if (this.cfg.logResponseBody && res.body !== undefined) {
323
328
  logger.log(res.body)
324
329
  }
325
330
  }
@@ -548,10 +553,11 @@ export class Fetcher {
548
553
  method: cfg.method || 'GET',
549
554
  headers: cfg.headers || {},
550
555
  credentials: cfg.credentials,
556
+ redirect: cfg.redirect,
551
557
  },
552
558
  hooks: {},
553
559
  },
554
- _omit(cfg, ['method', 'credentials', 'headers', 'logger']),
560
+ _omit(cfg, ['method', 'credentials', 'headers', 'redirect', 'logger']),
555
561
  )
556
562
 
557
563
  norm.init.headers = _mapKeys(norm.init.headers, k => k.toLowerCase())
@@ -587,7 +593,7 @@ export class Fetcher {
587
593
  ...this.cfg.init,
588
594
  method: opt.method || this.cfg.init.method,
589
595
  credentials: opt.credentials || this.cfg.init.credentials,
590
- redirect: opt.redirect || 'follow',
596
+ redirect: opt.redirect || this.cfg.init.redirect || 'follow',
591
597
  },
592
598
  {
593
599
  headers: _mapKeys(opt.headers || {}, k => k.toLowerCase()),