@naturalcycles/js-lib 15.29.0 → 15.30.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.
@@ -624,6 +624,7 @@ export class Fetcher {
624
624
  }),
625
625
  credentials: cfg.credentials,
626
626
  redirect: cfg.redirect,
627
+ dispatcher: cfg.dispatcher,
627
628
  },
628
629
  hooks: {},
629
630
  throwHttpErrors: true,
@@ -1,10 +1,11 @@
1
1
  /// <reference lib="es2022" preserve="true" />
2
2
  /// <reference lib="dom" preserve="true" />
3
+ import type { Dispatcher } from 'undici';
3
4
  import type { ErrorData } from '../error/error.model.js';
4
5
  import type { CommonLogger } from '../log/commonLogger.js';
5
6
  import type { AnyObject, NumberOfMilliseconds, Promisable, Reviver, UnixTimestampMillis } from '../types.js';
6
7
  import type { HttpMethod, HttpStatusFamily } from './http.model.js';
7
- export interface FetcherNormalizedCfg extends Required<FetcherCfg>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody' | 'debug' | 'redirect' | 'credentials' | 'throwHttpErrors' | 'errorData'> {
8
+ export interface FetcherNormalizedCfg extends Required<Omit<FetcherCfg, 'dispatcher'>>, Omit<FetcherRequest, 'started' | 'fullUrl' | 'logRequest' | 'logRequestBody' | 'logResponse' | 'logResponseBody' | 'debug' | 'redirect' | 'credentials' | 'throwHttpErrors' | 'errorData'> {
8
9
  logger: CommonLogger;
9
10
  searchParams: Record<string, any>;
10
11
  }
@@ -16,6 +17,10 @@ export type FetcherBeforeRetryHook = <BODY = unknown>(res: FetcherResponse<BODY>
16
17
  * Cannot cancel/prevent the error - AfterResponseHook can be used for that instead.
17
18
  */
18
19
  export type FetcherOnErrorHook = (err: Error) => Promisable<void>;
20
+ /**
21
+ * FetcherCfg: configuration of the Fetcher instance. One per instance.
22
+ * FetcherOptions: options for a single request. One per request.
23
+ */
19
24
  export interface FetcherCfg {
20
25
  /**
21
26
  * Should **not** contain trailing slash.
@@ -83,6 +88,13 @@ export interface FetcherCfg {
83
88
  */
84
89
  logger?: CommonLogger;
85
90
  throwHttpErrors?: boolean;
91
+ /**
92
+ * Pass an Undici Dispatcher.
93
+ * (Node.js only)
94
+ *
95
+ * @experimental
96
+ */
97
+ dispatcher?: Dispatcher;
86
98
  }
87
99
  export interface FetcherRetryStatus {
88
100
  retryAttempt: number;
@@ -131,6 +143,10 @@ export interface FetcherGraphQLOptions extends FetcherOptions {
131
143
  */
132
144
  unwrapObject?: string;
133
145
  }
146
+ /**
147
+ * FetcherCfg: configuration of the Fetcher instance. One per instance.
148
+ * FetcherOptions: options for a single request. One per request.
149
+ */
134
150
  export interface FetcherOptions {
135
151
  method?: HttpMethod;
136
152
  /**
@@ -237,6 +253,7 @@ export interface FetcherOptions {
237
253
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
238
254
  method: HttpMethod;
239
255
  headers: Record<string, any>;
256
+ dispatcher?: Dispatcher;
240
257
  };
241
258
  export interface FetcherSuccessResponse<BODY = unknown> {
242
259
  ok: true;
@@ -9,5 +9,7 @@ export declare function _hb(b?: number): string;
9
9
  * hc stands for "human count", similar to "human bytes" `_hb` function.
10
10
  * Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
11
11
  * them more readable.
12
+ *
13
+ * Implementation rule of thumb: aim to have up to 3 significant digits, cut the rest.
12
14
  */
13
15
  export declare function _hc(c?: number): string;
@@ -1,11 +1,15 @@
1
+ const kb = 1024;
2
+ const mb = 1024 ** 2;
3
+ const gb = 1024 ** 3;
4
+ const tb = 1024 ** 4;
1
5
  export function _gb(b) {
2
- return Math.round(b / 1024 ** 3);
6
+ return Math.round(b / gb);
3
7
  }
4
8
  export function _mb(b) {
5
- return Math.round(b / 1024 ** 2);
9
+ return Math.round(b / mb);
6
10
  }
7
11
  export function _kb(b) {
8
- return Math.round(b / 1024);
12
+ return Math.round(b / kb);
9
13
  }
10
14
  /**
11
15
  * Byte size to Human byte size string
@@ -13,35 +17,61 @@ export function _kb(b) {
13
17
  export function _hb(b = 0) {
14
18
  if (b < 100)
15
19
  return `${Math.round(b)} byte(s)`;
16
- if (b < 1000)
17
- return `${(b / 1024).toFixed(2)} Kb`;
18
- if (b < 0.9 * 1024 ** 2)
19
- return `${Math.round(b / 1024)} Kb`;
20
- if (b < 0.9 * 1024 ** 3)
21
- return `${Math.round(b / 1024 ** 2)} Mb`;
22
- if (b < 0.09 * 1024 ** 4)
23
- return `${(b / 1024 ** 3).toFixed(2)} Gb`;
24
- if (b < 0.9 * 1024 ** 4)
25
- return `${Math.round(b / 1024 ** 3)} Gb`;
26
- if (b < 0.9 * 1024 ** 5)
27
- return `${(b / 1024 ** 4).toFixed(2)} Tb`;
28
- return `${Math.round(b / 1024 ** 4)} Tb`;
20
+ if (b < 10 ** 4)
21
+ return `${(b / kb).toFixed(2)} Kb`;
22
+ if (b < 10 ** 5)
23
+ return `${(b / kb).toFixed(1)} Kb`;
24
+ if (b < 10 ** 6)
25
+ return `${Math.round(b / kb)} Kb`;
26
+ if (b < 10 ** 7)
27
+ return `${(b / mb).toFixed(2)} Mb`;
28
+ if (b < 10 ** 8)
29
+ return `${(b / mb).toFixed(1)} Mb`;
30
+ if (b < 10 ** 9)
31
+ return `${Math.round(b / mb)} Mb`;
32
+ if (b < 10 ** 10)
33
+ return `${(b / gb).toFixed(2)} Gb`;
34
+ if (b < 10 ** 11)
35
+ return `${(b / gb).toFixed(1)} Gb`;
36
+ if (b < 10 ** 12)
37
+ return `${Math.round(b / gb)} Gb`;
38
+ if (b < 10 ** 13)
39
+ return `${(b / tb).toFixed(2)} Tb`;
40
+ if (b < 10 ** 14)
41
+ return `${(b / tb).toFixed(1)} Tb`;
42
+ return `${Math.round(b / tb)} Tb`;
29
43
  }
30
44
  /**
31
45
  * hc stands for "human count", similar to "human bytes" `_hb` function.
32
46
  * Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
33
47
  * them more readable.
48
+ *
49
+ * Implementation rule of thumb: aim to have up to 3 significant digits, cut the rest.
34
50
  */
35
51
  export function _hc(c = 0) {
36
- if (c < 10 ** 4)
52
+ if (c < 10 ** 3)
37
53
  return String(Math.round(c));
54
+ if (c < 10 ** 4)
55
+ return (c / 10 ** 3).toFixed(2) + ' K';
56
+ if (c < 10 ** 5)
57
+ return (c / 10 ** 3).toFixed(1) + ' K';
38
58
  if (c < 10 ** 6)
39
59
  return Math.round(c / 10 ** 3) + ' K';
60
+ if (c < 10 ** 7)
61
+ return (c / 10 ** 6).toFixed(2) + ' M';
62
+ if (c < 10 ** 8)
63
+ return (c / 10 ** 6).toFixed(1) + ' M';
40
64
  if (c < 10 ** 9)
41
- return Math.round(c / 10 ** 6) + ' M'; // million
65
+ return Math.round(c / 10 ** 6) + ' M';
66
+ if (c < 10 ** 10)
67
+ return (c / 10 ** 9).toFixed(2) + ' B';
68
+ if (c < 10 ** 11)
69
+ return (c / 10 ** 9).toFixed(1) + ' B';
42
70
  if (c < 10 ** 12)
43
- return Math.round(c / 10 ** 9) + ' B'; // billion
44
- if (c < 10 ** 15)
45
- return Math.round(c / 10 ** 12) + ' T'; // trillion
71
+ return Math.round(c / 10 ** 9) + ' B';
72
+ if (c < 10 ** 13)
73
+ return (c / 10 ** 12).toFixed(2) + ' T';
74
+ if (c < 10 ** 14)
75
+ return (c / 10 ** 12).toFixed(1) + ' T';
46
76
  return Math.round(c / 10 ** 12) + ' T';
47
77
  }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.29.0",
4
+ "version": "15.30.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
+ "undici": "^7",
7
8
  "zod": "^4"
8
9
  },
9
10
  "devDependencies": {
@@ -12,7 +13,7 @@
12
13
  "@types/semver": "^7",
13
14
  "crypto-js": "^4",
14
15
  "dayjs": "^1",
15
- "@naturalcycles/dev-lib": "19.33.0"
16
+ "@naturalcycles/dev-lib": "18.4.2"
16
17
  },
17
18
  "exports": {
18
19
  ".": "./dist/index.js",
@@ -1,6 +1,7 @@
1
1
  /// <reference lib="es2022" preserve="true" />
2
2
  /// <reference lib="dom" preserve="true" />
3
3
 
4
+ import type { Dispatcher } from 'undici'
4
5
  import type { ErrorData } from '../error/error.model.js'
5
6
  import type { CommonLogger } from '../log/commonLogger.js'
6
7
  import type {
@@ -13,7 +14,7 @@ import type {
13
14
  import type { HttpMethod, HttpStatusFamily } from './http.model.js'
14
15
 
15
16
  export interface FetcherNormalizedCfg
16
- extends Required<FetcherCfg>,
17
+ extends Required<Omit<FetcherCfg, 'dispatcher'>>,
17
18
  Omit<
18
19
  FetcherRequest,
19
20
  | 'started'
@@ -45,6 +46,10 @@ export type FetcherBeforeRetryHook = <BODY = unknown>(
45
46
  */
46
47
  export type FetcherOnErrorHook = (err: Error) => Promisable<void>
47
48
 
49
+ /**
50
+ * FetcherCfg: configuration of the Fetcher instance. One per instance.
51
+ * FetcherOptions: options for a single request. One per request.
52
+ */
48
53
  export interface FetcherCfg {
49
54
  /**
50
55
  * Should **not** contain trailing slash.
@@ -120,6 +125,14 @@ export interface FetcherCfg {
120
125
  logger?: CommonLogger
121
126
 
122
127
  throwHttpErrors?: boolean
128
+
129
+ /**
130
+ * Pass an Undici Dispatcher.
131
+ * (Node.js only)
132
+ *
133
+ * @experimental
134
+ */
135
+ dispatcher?: Dispatcher
123
136
  }
124
137
 
125
138
  export interface FetcherRetryStatus {
@@ -174,6 +187,10 @@ export interface FetcherGraphQLOptions extends FetcherOptions {
174
187
  unwrapObject?: string
175
188
  }
176
189
 
190
+ /**
191
+ * FetcherCfg: configuration of the Fetcher instance. One per instance.
192
+ * FetcherOptions: options for a single request. One per request.
193
+ */
177
194
  export interface FetcherOptions {
178
195
  method?: HttpMethod
179
196
 
@@ -303,6 +320,7 @@ export interface FetcherOptions {
303
320
  export type RequestInitNormalized = Omit<RequestInit, 'method' | 'headers'> & {
304
321
  method: HttpMethod
305
322
  headers: Record<string, any>
323
+ dispatcher?: Dispatcher
306
324
  }
307
325
 
308
326
  export interface FetcherSuccessResponse<BODY = unknown> {
@@ -750,6 +750,7 @@ export class Fetcher {
750
750
  }),
751
751
  credentials: cfg.credentials,
752
752
  redirect: cfg.redirect,
753
+ dispatcher: cfg.dispatcher,
753
754
  },
754
755
  hooks: {},
755
756
  throwHttpErrors: true,
@@ -1,13 +1,18 @@
1
+ const kb = 1024
2
+ const mb = 1024 ** 2
3
+ const gb = 1024 ** 3
4
+ const tb = 1024 ** 4
5
+
1
6
  export function _gb(b: number): number {
2
- return Math.round(b / 1024 ** 3)
7
+ return Math.round(b / gb)
3
8
  }
4
9
 
5
10
  export function _mb(b: number): number {
6
- return Math.round(b / 1024 ** 2)
11
+ return Math.round(b / mb)
7
12
  }
8
13
 
9
14
  export function _kb(b: number): number {
10
- return Math.round(b / 1024)
15
+ return Math.round(b / kb)
11
16
  }
12
17
 
13
18
  /**
@@ -15,25 +20,39 @@ export function _kb(b: number): number {
15
20
  */
16
21
  export function _hb(b = 0): string {
17
22
  if (b < 100) return `${Math.round(b)} byte(s)`
18
- if (b < 1000) return `${(b / 1024).toFixed(2)} Kb`
19
- if (b < 0.9 * 1024 ** 2) return `${Math.round(b / 1024)} Kb`
20
- if (b < 0.9 * 1024 ** 3) return `${Math.round(b / 1024 ** 2)} Mb`
21
- if (b < 0.09 * 1024 ** 4) return `${(b / 1024 ** 3).toFixed(2)} Gb`
22
- if (b < 0.9 * 1024 ** 4) return `${Math.round(b / 1024 ** 3)} Gb`
23
- if (b < 0.9 * 1024 ** 5) return `${(b / 1024 ** 4).toFixed(2)} Tb`
24
- return `${Math.round(b / 1024 ** 4)} Tb`
23
+ if (b < 10 ** 4) return `${(b / kb).toFixed(2)} Kb`
24
+ if (b < 10 ** 5) return `${(b / kb).toFixed(1)} Kb`
25
+ if (b < 10 ** 6) return `${Math.round(b / kb)} Kb`
26
+ if (b < 10 ** 7) return `${(b / mb).toFixed(2)} Mb`
27
+ if (b < 10 ** 8) return `${(b / mb).toFixed(1)} Mb`
28
+ if (b < 10 ** 9) return `${Math.round(b / mb)} Mb`
29
+ if (b < 10 ** 10) return `${(b / gb).toFixed(2)} Gb`
30
+ if (b < 10 ** 11) return `${(b / gb).toFixed(1)} Gb`
31
+ if (b < 10 ** 12) return `${Math.round(b / gb)} Gb`
32
+ if (b < 10 ** 13) return `${(b / tb).toFixed(2)} Tb`
33
+ if (b < 10 ** 14) return `${(b / tb).toFixed(1)} Tb`
34
+ return `${Math.round(b / tb)} Tb`
25
35
  }
26
36
 
27
37
  /**
28
38
  * hc stands for "human count", similar to "human bytes" `_hb` function.
29
39
  * Helpful to print big numbers, as it adds `K` (kilo), `M` (mega), etc to make
30
40
  * them more readable.
41
+ *
42
+ * Implementation rule of thumb: aim to have up to 3 significant digits, cut the rest.
31
43
  */
32
44
  export function _hc(c = 0): string {
33
- if (c < 10 ** 4) return String(Math.round(c))
45
+ if (c < 10 ** 3) return String(Math.round(c))
46
+ if (c < 10 ** 4) return (c / 10 ** 3).toFixed(2) + ' K'
47
+ if (c < 10 ** 5) return (c / 10 ** 3).toFixed(1) + ' K'
34
48
  if (c < 10 ** 6) return Math.round(c / 10 ** 3) + ' K'
35
- if (c < 10 ** 9) return Math.round(c / 10 ** 6) + ' M' // million
36
- if (c < 10 ** 12) return Math.round(c / 10 ** 9) + ' B' // billion
37
- if (c < 10 ** 15) return Math.round(c / 10 ** 12) + ' T' // trillion
49
+ if (c < 10 ** 7) return (c / 10 ** 6).toFixed(2) + ' M'
50
+ if (c < 10 ** 8) return (c / 10 ** 6).toFixed(1) + ' M'
51
+ if (c < 10 ** 9) return Math.round(c / 10 ** 6) + ' M'
52
+ if (c < 10 ** 10) return (c / 10 ** 9).toFixed(2) + ' B'
53
+ if (c < 10 ** 11) return (c / 10 ** 9).toFixed(1) + ' B'
54
+ if (c < 10 ** 12) return Math.round(c / 10 ** 9) + ' B'
55
+ if (c < 10 ** 13) return (c / 10 ** 12).toFixed(2) + ' T'
56
+ if (c < 10 ** 14) return (c / 10 ** 12).toFixed(1) + ' T'
38
57
  return Math.round(c / 10 ** 12) + ' T'
39
58
  }