@naturalcycles/js-lib 15.101.0 → 15.103.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.
- package/dist/abort.d.ts +0 -8
- package/dist/abort.js +2 -19
- package/dist/array/array.util.d.ts +1 -3
- package/dist/array/array.util.js +1 -3
- package/dist/array/sort.js +1 -1
- package/dist/array/sortedSet.d.ts +1 -1
- package/dist/array/sortedSet.js +1 -1
- package/dist/asyncManager.js +3 -3
- package/dist/browser/analytics.util.js +1 -1
- package/dist/browser/topbar.js +1 -1
- package/dist/datetime/localDate.js +10 -5
- package/dist/datetime/localTime.d.ts +2 -19
- package/dist/datetime/localTime.js +12 -30
- package/dist/datetime/time.util.d.ts +18 -1
- package/dist/datetime/time.util.js +28 -5
- package/dist/decorators/createPromiseDecorator.js +1 -1
- package/dist/decorators/debounce.js +1 -1
- package/dist/decorators/logMethod.decorator.js +1 -2
- package/dist/error/tryCatch.js +1 -2
- package/dist/http/fetch.d.ts +43 -0
- package/dist/http/fetch.js +57 -0
- package/dist/http/fetcher.js +6 -8
- package/dist/http/index.d.ts +1 -0
- package/dist/http/index.js +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +0 -1
- package/dist/object/keySortedMap.js +1 -1
- package/dist/object/lazyKeySortedMap.js +1 -1
- package/dist/object/object.util.js +2 -2
- package/dist/promise/pGradualQueue.js +1 -1
- package/dist/promise/pRetry.js +1 -2
- package/dist/semver.js +10 -5
- package/dist/string/stringify.js +1 -2
- package/package.json +5 -2
- package/src/abort.ts +2 -21
- package/src/array/array.util.ts +1 -3
- package/src/array/sort.ts +1 -1
- package/src/array/sortedSet.ts +1 -1
- package/src/asyncManager.ts +3 -3
- package/src/browser/analytics.util.ts +1 -1
- package/src/browser/topbar.ts +1 -1
- package/src/datetime/localDate.ts +11 -7
- package/src/datetime/localTime.ts +12 -34
- package/src/datetime/time.util.ts +32 -5
- package/src/decorators/createPromiseDecorator.ts +1 -1
- package/src/decorators/debounce.ts +1 -1
- package/src/decorators/logMethod.decorator.ts +1 -2
- package/src/error/tryCatch.ts +1 -2
- package/src/http/fetch.ts +102 -0
- package/src/http/fetcher.ts +7 -9
- package/src/http/index.ts +1 -0
- package/src/index.ts +1 -1
- package/src/object/keySortedMap.ts +1 -1
- package/src/object/lazyKeySortedMap.ts +1 -1
- package/src/object/object.util.ts +2 -2
- package/src/promise/pGradualQueue.ts +1 -1
- package/src/promise/pRetry.ts +1 -2
- package/src/semver.ts +11 -7
- package/src/string/stringify.ts +2 -4
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.103.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2"
|
|
7
7
|
},
|
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@naturalcycles/dev-lib": "
|
|
17
|
+
"@naturalcycles/dev-lib": "0.0.0",
|
|
18
18
|
"@types/node": "^26",
|
|
19
19
|
"@types/semver": "^7",
|
|
20
|
+
"rolldown": "^1",
|
|
20
21
|
"typescript": "^7",
|
|
21
22
|
"dayjs": "^1"
|
|
22
23
|
},
|
|
@@ -35,9 +36,11 @@
|
|
|
35
36
|
"./decorators": "./dist/decorators/index.js",
|
|
36
37
|
"./decorators/*.js": "./dist/decorators/*.js",
|
|
37
38
|
"./deviceIdService": "./dist/deviceIdService.js",
|
|
39
|
+
"./env/*.js": "./dist/env/*.js",
|
|
38
40
|
"./error": "./dist/error/index.js",
|
|
39
41
|
"./error/*.js": "./dist/error/*.js",
|
|
40
42
|
"./http": "./dist/http/index.js",
|
|
43
|
+
"./http/*.js": "./dist/http/*.js",
|
|
41
44
|
"./intl": "./dist/intl/intl.js",
|
|
42
45
|
"./log": "./dist/log/commonLogger.js",
|
|
43
46
|
"./math": "./dist/math/index.js",
|
package/src/abort.ts
CHANGED
|
@@ -39,27 +39,7 @@ export function createAbortableSignal(): AbortableSignal {
|
|
|
39
39
|
export function abortSignalTimeoutOrUndefined(
|
|
40
40
|
ms: NumberOfMilliseconds | undefined,
|
|
41
41
|
): AbortSignal | undefined {
|
|
42
|
-
return ms ?
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Returns an AbortSignal that aborts after the given number of milliseconds.
|
|
47
|
-
* Uses native `AbortSignal.timeout()` when available, falls back to a polyfill.
|
|
48
|
-
*
|
|
49
|
-
* The abort reason is a DOMException with name "TimeoutError".
|
|
50
|
-
*/
|
|
51
|
-
export function abortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal {
|
|
52
|
-
return typeof AbortSignal.timeout === 'function'
|
|
53
|
-
? AbortSignal.timeout(ms)
|
|
54
|
-
: polyfilledAbortSignalTimeout(ms)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function polyfilledAbortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal {
|
|
58
|
-
const ac = new AbortController()
|
|
59
|
-
setTimeout(() => {
|
|
60
|
-
ac.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError'))
|
|
61
|
-
}, ms)
|
|
62
|
-
return ac.signal
|
|
42
|
+
return ms ? AbortSignal.timeout(ms) : undefined
|
|
63
43
|
}
|
|
64
44
|
|
|
65
45
|
/**
|
|
@@ -87,6 +67,7 @@ export function abortSignalAny(signals: readonly AbortSignal[]): AbortSignal {
|
|
|
87
67
|
return signals[0]!
|
|
88
68
|
}
|
|
89
69
|
|
|
70
|
+
// AbortSignal.any needs Chrome 116+ / Safari 17.4+, above our browser floor (Chrome 110, iOS 16.7)
|
|
90
71
|
return typeof AbortSignal.any === 'function'
|
|
91
72
|
? AbortSignal.any(signals as AbortSignal[]) // lib.dom types require a mutable array, but it is only read
|
|
92
73
|
: polyfilledAbortSignalAny(signals)
|
package/src/array/array.util.ts
CHANGED
|
@@ -195,9 +195,7 @@ export function _find<T>(items: readonly T[], predicate: AbortablePredicate<T>):
|
|
|
195
195
|
/**
|
|
196
196
|
* Similar to `Array.findLast`, but the `predicate` may return `END` to stop the iteration early.
|
|
197
197
|
*
|
|
198
|
-
* Use `Array.findLast` if you don't need to stop the iteration early
|
|
199
|
-
* - in Node since 18+
|
|
200
|
-
* - in iOS Safari since 15.4
|
|
198
|
+
* Use `Array.findLast` if you don't need to stop the iteration early.
|
|
201
199
|
*/
|
|
202
200
|
export function _findLast<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined {
|
|
203
201
|
return _find(items.toReversed(), predicate)
|
package/src/array/sort.ts
CHANGED
package/src/array/sortedSet.ts
CHANGED
package/src/asyncManager.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { nowUnixMillis } from './datetime/
|
|
1
|
+
import { nowUnixMillis } from './datetime/time.util.js'
|
|
2
2
|
import { consoleLogger } from './log/commonLogger.js'
|
|
3
3
|
import type { CommonLogger } from './log/commonLogger.js'
|
|
4
4
|
import type { NumberOfMilliseconds } from './types.js'
|
|
@@ -93,9 +93,9 @@ class AsyncManagerImpl {
|
|
|
93
93
|
*
|
|
94
94
|
* @experimental
|
|
95
95
|
*/
|
|
96
|
-
export const AsyncManager = new AsyncManagerImpl()
|
|
96
|
+
export const AsyncManager = /* @__PURE__ */ new AsyncManagerImpl()
|
|
97
97
|
|
|
98
98
|
// Shorthand alias
|
|
99
|
-
export const runInBackground = AsyncManager.runInBackground.bind(AsyncManager)
|
|
99
|
+
export const runInBackground = /* @__PURE__ */ AsyncManager.runInBackground.bind(AsyncManager)
|
|
100
100
|
|
|
101
101
|
export type OnErrorHook = (err: Error) => any
|
package/src/browser/topbar.ts
CHANGED
|
@@ -853,10 +853,14 @@ interface LocalDateFn extends LocalDateFactory {
|
|
|
853
853
|
(d: LocalDateInput): LocalDate
|
|
854
854
|
}
|
|
855
855
|
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
856
|
+
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
|
857
|
+
export const localDate = /* @__PURE__ */ createLocalDateFn()
|
|
858
|
+
|
|
859
|
+
function createLocalDateFn(): LocalDateFn {
|
|
860
|
+
const localDateFactory = new LocalDateFactory()
|
|
861
|
+
const fn = localDateFactory.fromInput.bind(localDateFactory) as LocalDateFn
|
|
862
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
863
|
+
// And probably 2023 as well.
|
|
864
|
+
Object.setPrototypeOf(fn, localDateFactory)
|
|
865
|
+
return fn
|
|
866
|
+
}
|
|
@@ -17,7 +17,7 @@ import type {
|
|
|
17
17
|
// oxlint-disable-next-line import/no-cycle -- intentional cycle
|
|
18
18
|
import type { LocalDate } from './localDate.js'
|
|
19
19
|
import { localDate } from './localDate.js'
|
|
20
|
-
import { _ms } from './time.util.js'
|
|
20
|
+
import { _ms, nowUnix, nowUnixMillis } from './time.util.js'
|
|
21
21
|
// oxlint-disable-next-line import/no-cycle -- intentional cycle
|
|
22
22
|
import { WallTime } from './wallTime.js'
|
|
23
23
|
|
|
@@ -817,7 +817,7 @@ class LocalTimeFactory {
|
|
|
817
817
|
Convenience function to return current Unix timestamp in seconds.
|
|
818
818
|
Like Date.now(), but in seconds.
|
|
819
819
|
|
|
820
|
-
Import nowUnix
|
|
820
|
+
Import nowUnix from time.util.js in places where importing localTime is too heavy.
|
|
821
821
|
*/
|
|
822
822
|
nowUnix(): UnixTimestamp {
|
|
823
823
|
return nowUnix()
|
|
@@ -826,7 +826,7 @@ class LocalTimeFactory {
|
|
|
826
826
|
/**
|
|
827
827
|
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
828
828
|
|
|
829
|
-
Import nowUnixMillis
|
|
829
|
+
Import nowUnixMillis from time.util.js in places where importing localTime is too heavy.
|
|
830
830
|
*/
|
|
831
831
|
nowUnixMillis(): UnixTimestampMillis {
|
|
832
832
|
return nowUnixMillis()
|
|
@@ -1212,36 +1212,14 @@ interface LocalTimeFn extends LocalTimeFactory {
|
|
|
1212
1212
|
(d: LocalTimeInput): LocalTime
|
|
1213
1213
|
}
|
|
1214
1214
|
|
|
1215
|
-
|
|
1215
|
+
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
|
1216
|
+
export const localTime = /* @__PURE__ */ createLocalTimeFn()
|
|
1216
1217
|
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
//
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
* Can be used instead of `oxlint-disable ...` in places where Date is intentionally needed
|
|
1225
|
-
*/
|
|
1226
|
-
export function newDate(): Date {
|
|
1227
|
-
return new Date()
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
|
-
/**
|
|
1231
|
-
Convenience function to return current Unix timestamp in seconds.
|
|
1232
|
-
Like Date.now(), but in seconds.
|
|
1233
|
-
|
|
1234
|
-
Import nowUnix function individually in places where importing localTime is too heavy.
|
|
1235
|
-
*/
|
|
1236
|
-
export function nowUnix(): UnixTimestamp {
|
|
1237
|
-
return Math.floor(Date.now() / 1000) as UnixTimestamp
|
|
1238
|
-
}
|
|
1239
|
-
|
|
1240
|
-
/**
|
|
1241
|
-
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
1242
|
-
|
|
1243
|
-
Import nowUnixMillis function individually in places where importing localTime is too heavy.
|
|
1244
|
-
*/
|
|
1245
|
-
export function nowUnixMillis(): UnixTimestampMillis {
|
|
1246
|
-
return Date.now() as UnixTimestampMillis
|
|
1218
|
+
function createLocalTimeFn(): LocalTimeFn {
|
|
1219
|
+
const localTimeFactory = new LocalTimeFactory()
|
|
1220
|
+
const fn = localTimeFactory.fromInput.bind(localTimeFactory) as LocalTimeFn
|
|
1221
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
1222
|
+
// And probably 2023 as well.
|
|
1223
|
+
Object.setPrototypeOf(fn, localTimeFactory)
|
|
1224
|
+
return fn
|
|
1247
1225
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { NumberOfMilliseconds, UnixTimestampMillis } from '../types.js'
|
|
1
|
+
import type { NumberOfMilliseconds, UnixTimestamp, UnixTimestampMillis } from '../types.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* using _ = blockTimer()
|
|
@@ -10,8 +10,7 @@ import type { NumberOfMilliseconds, UnixTimestampMillis } from '../types.js'
|
|
|
10
10
|
* @experimental
|
|
11
11
|
*/
|
|
12
12
|
export function _blockTimer(name?: string): Disposable {
|
|
13
|
-
|
|
14
|
-
const started = Date.now() as UnixTimestampMillis
|
|
13
|
+
const started = nowUnixMillis()
|
|
15
14
|
return {
|
|
16
15
|
[Symbol.dispose](): void {
|
|
17
16
|
console.log(`${name ? name + ' ' : ''}took ${_since(started)}`)
|
|
@@ -24,8 +23,7 @@ export function _blockTimer(name?: string): Disposable {
|
|
|
24
23
|
*/
|
|
25
24
|
export function _since(
|
|
26
25
|
from: UnixTimestampMillis,
|
|
27
|
-
|
|
28
|
-
until: UnixTimestampMillis = Date.now() as UnixTimestampMillis,
|
|
26
|
+
until: UnixTimestampMillis = nowUnixMillis(),
|
|
29
27
|
): string {
|
|
30
28
|
return _ms(until - from)
|
|
31
29
|
}
|
|
@@ -74,3 +72,32 @@ export function _ms(millis: NumberOfMilliseconds): string {
|
|
|
74
72
|
const days = Math.floor(hrs / 24)
|
|
75
73
|
return `${days} days`
|
|
76
74
|
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Can be used instead of `oxlint-disable ...` in places where Date is intentionally needed
|
|
78
|
+
*/
|
|
79
|
+
export function newDate(): Date {
|
|
80
|
+
// oxlint-disable-next-line no-restricted-globals -- the one sanctioned way to get a native Date
|
|
81
|
+
return new Date()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
Convenience function to return current Unix timestamp in seconds.
|
|
86
|
+
Like Date.now(), but in seconds.
|
|
87
|
+
|
|
88
|
+
Lives here rather than in localTime.ts, so importing it doesn't pull LocalTime into browser bundles.
|
|
89
|
+
*/
|
|
90
|
+
export function nowUnix(): UnixTimestamp {
|
|
91
|
+
// oxlint-disable-next-line no-restricted-globals -- the one sanctioned way to read the clock
|
|
92
|
+
return Math.floor(Date.now() / 1000) as UnixTimestamp
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
97
|
+
|
|
98
|
+
Lives here rather than in localTime.ts, so importing it doesn't pull LocalTime into browser bundles.
|
|
99
|
+
*/
|
|
100
|
+
export function nowUnixMillis(): UnixTimestampMillis {
|
|
101
|
+
// oxlint-disable-next-line no-restricted-globals -- the one sanctioned way to read the clock
|
|
102
|
+
return Date.now() as UnixTimestampMillis
|
|
103
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { nowUnixMillis } from '../datetime/
|
|
1
|
+
import { nowUnixMillis } from '../datetime/time.util.js'
|
|
2
2
|
import { _anyToError } from '../error/error.util.js'
|
|
3
3
|
import { pDefer } from '../promise/pDefer.js'
|
|
4
4
|
import type { DeferredPromise } from '../promise/pDefer.js'
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { nowUnixMillis } from '../datetime/
|
|
2
|
-
import { _ms } from '../datetime/time.util.js'
|
|
1
|
+
import { _ms, nowUnixMillis } from '../datetime/time.util.js'
|
|
3
2
|
import { _assert } from '../error/assert.js'
|
|
4
3
|
import { consoleLogger } from '../log/commonLogger.js'
|
|
5
4
|
import type { CommonLogger } from '../log/commonLogger.js'
|
package/src/error/tryCatch.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { nowUnixMillis } from '../datetime/
|
|
2
|
-
import { _since } from '../datetime/time.util.js'
|
|
1
|
+
import { _since, nowUnixMillis } from '../datetime/time.util.js'
|
|
3
2
|
import { consoleLogger } from '../log/commonLogger.js'
|
|
4
3
|
import type { CommonLogger } from '../log/commonLogger.js'
|
|
5
4
|
import type { AnyFunction } from '../types.js'
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/// <reference lib="dom" preserve="true" />
|
|
2
|
+
|
|
3
|
+
import type { HttpRequestErrorData } from '../error/error.model.js'
|
|
4
|
+
import type { NumberOfMilliseconds } from '../types.js'
|
|
5
|
+
import type { HttpMethod } from './http.model.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Minimal wrapper around native `fetch`:
|
|
9
|
+
*
|
|
10
|
+
* - returns json output via Promise
|
|
11
|
+
* - throws FetchError (a plain Error with `cause` and `data`) if anything goes wrong
|
|
12
|
+
* - retries 2 times with simple non-configurable exponential back-off
|
|
13
|
+
*
|
|
14
|
+
* For anything more complex - use the Fetcher.
|
|
15
|
+
* Use this wrapper for the simplest use cases, and where bundle size matters.
|
|
16
|
+
*/
|
|
17
|
+
export async function _fetch<T = unknown>(url: string, opt: FetchOptions = {}): Promise<T> {
|
|
18
|
+
const { method = 'GET', timeout = 30_000, json, ...init } = opt
|
|
19
|
+
if (json !== undefined) {
|
|
20
|
+
init.body = JSON.stringify(json)
|
|
21
|
+
init.headers = { 'content-type': 'application/json', ...init.headers }
|
|
22
|
+
}
|
|
23
|
+
let res: Response | undefined
|
|
24
|
+
let cause: unknown
|
|
25
|
+
|
|
26
|
+
for (let attempt = 0; ; attempt++) {
|
|
27
|
+
res = undefined
|
|
28
|
+
try {
|
|
29
|
+
res = await fetch(url, {
|
|
30
|
+
...init,
|
|
31
|
+
method,
|
|
32
|
+
// A fresh signal per attempt, as a timed-out signal stays aborted
|
|
33
|
+
signal: timeout ? AbortSignal.timeout(timeout) : undefined,
|
|
34
|
+
})
|
|
35
|
+
if (res.ok) {
|
|
36
|
+
const text = await res.text()
|
|
37
|
+
return (text ? JSON.parse(text) : {}) as T
|
|
38
|
+
}
|
|
39
|
+
cause = await res.text()
|
|
40
|
+
} catch (err) {
|
|
41
|
+
// Network failure, timeout, or failure to read/parse the body
|
|
42
|
+
cause = err
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (attempt === 2 || !shouldRetry(method, res)) break
|
|
46
|
+
await pDelay(1000 * 2 ** attempt) // 1s, then 2s
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const err: FetchError = Object.assign(
|
|
50
|
+
// E.g `503 GET https://example.com/api`, or `GET https://example.com/api` on a network failure
|
|
51
|
+
new Error([res?.status, method, url].filter(Boolean).join(' '), { cause }),
|
|
52
|
+
{ data: { responseStatusCode: res?.status || 0, requestUrl: url, requestMethod: method } },
|
|
53
|
+
)
|
|
54
|
+
throw err
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Retries network failures (including timeouts), 5xx, 408 and 429.
|
|
59
|
+
* Never retries a POST.
|
|
60
|
+
*/
|
|
61
|
+
function shouldRetry(method: HttpMethod, res?: Response): boolean {
|
|
62
|
+
return (
|
|
63
|
+
method !== 'POST' && (!res || res.status >= 500 || res.status === 408 || res.status === 429)
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Accepts the commonly used RequestInit options.
|
|
69
|
+
* `signal` is replaced by `timeout`.
|
|
70
|
+
*/
|
|
71
|
+
export interface FetchOptions extends Pick<
|
|
72
|
+
RequestInit,
|
|
73
|
+
'body' | 'cache' | 'credentials' | 'keepalive' | 'redirect'
|
|
74
|
+
> {
|
|
75
|
+
/**
|
|
76
|
+
* Defaults to GET
|
|
77
|
+
*/
|
|
78
|
+
method?: HttpMethod
|
|
79
|
+
headers?: Record<string, string>
|
|
80
|
+
/**
|
|
81
|
+
* Request body. It's JSON.stringified, and the Content-Type header is set to `application/json`.
|
|
82
|
+
* Takes precedence over `body`.
|
|
83
|
+
*/
|
|
84
|
+
json?: any
|
|
85
|
+
/**
|
|
86
|
+
* Defaults to 30 seconds. 0 disables the timeout.
|
|
87
|
+
* Covers the whole request, including reading the body.
|
|
88
|
+
*/
|
|
89
|
+
timeout?: NumberOfMilliseconds
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Thrown by `_fetch`: a plain Error, with the response body text (or the underlying error)
|
|
94
|
+
* as `cause`, and a subset of HttpRequestErrorData as `data`.
|
|
95
|
+
*/
|
|
96
|
+
export interface FetchError extends Error {
|
|
97
|
+
data: Pick<HttpRequestErrorData, 'responseStatusCode' | 'requestUrl' | 'requestMethod'>
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function pDelay<T>(ms: NumberOfMilliseconds): Promise<void> {
|
|
101
|
+
await new Promise<T>(resolve => setTimeout(resolve, ms))
|
|
102
|
+
}
|
package/src/http/fetcher.ts
CHANGED
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import type { ReadableStream as WebReadableStream } from 'node:stream/web'
|
|
6
6
|
import { abortSignalAnyOrUndefined, abortSignalTimeoutOrUndefined } from '../abort.js'
|
|
7
|
-
import { nowUnixMillis } from '../datetime/
|
|
8
|
-
import { _ms, _since } from '../datetime/time.util.js'
|
|
7
|
+
import { _ms, _since, nowUnixMillis } from '../datetime/time.util.js'
|
|
9
8
|
import { isServerSide } from '../env.js'
|
|
10
9
|
import { _assert, _assertErrorClassOrRethrow, _assertIsError } from '../error/assert.js'
|
|
11
10
|
import type { ErrorLike, ErrorObject } from '../error/error.model.js'
|
|
@@ -30,8 +29,8 @@ import {
|
|
|
30
29
|
} from '../object/object.util.js'
|
|
31
30
|
import { pDelaySignal } from '../promise/pDelay.js'
|
|
32
31
|
import { pTimeout } from '../promise/pTimeout.js'
|
|
33
|
-
import { _toUrlOrNull } from '../string/index.js'
|
|
34
32
|
import { _jsonParse, _jsonParseIfPossible } from '../string/json.util.js'
|
|
33
|
+
import { _toUrlOrNull } from '../string/url.util.js'
|
|
35
34
|
import type { AnyObject, ErrorDataTuple, NumberOfMilliseconds } from '../types.js'
|
|
36
35
|
import type {
|
|
37
36
|
FetcherAfterResponseHook,
|
|
@@ -56,6 +55,8 @@ import type {
|
|
|
56
55
|
import type { HttpStatusFamily } from './http.model.js'
|
|
57
56
|
import { HTTP_METHODS } from './http.model.js'
|
|
58
57
|
|
|
58
|
+
const FETCHER_VERSION = 5
|
|
59
|
+
|
|
59
60
|
/**
|
|
60
61
|
* Experimental wrapper around Fetch.
|
|
61
62
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
@@ -67,18 +68,15 @@ export class Fetcher {
|
|
|
67
68
|
*
|
|
68
69
|
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
69
70
|
*/
|
|
70
|
-
static readonly VERSION =
|
|
71
|
+
static readonly VERSION = FETCHER_VERSION
|
|
71
72
|
/**
|
|
72
73
|
* userAgent is statically exposed as Fetcher.userAgent.
|
|
73
74
|
* It can be modified globally, and will be used (read) at the start of every request.
|
|
74
75
|
*/
|
|
75
|
-
|
|
76
|
+
// Pure, and not reading `this.VERSION`, so bundlers can drop Fetcher when unused (see treeshake.test.ts)
|
|
77
|
+
static userAgent = /* @__PURE__ */ isServerSide() ? `fetcher/${FETCHER_VERSION}` : undefined
|
|
76
78
|
|
|
77
79
|
private constructor(cfg: FetcherCfg & FetcherOptions = {}) {
|
|
78
|
-
if (typeof globalThis.fetch !== 'function') {
|
|
79
|
-
throw new TypeError('globalThis.fetch is not available')
|
|
80
|
-
}
|
|
81
|
-
|
|
82
80
|
this.cfg = this.normalizeCfg(cfg)
|
|
83
81
|
|
|
84
82
|
// Dynamically create all helper methods
|
package/src/http/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ export * from './bot.model.js'
|
|
|
4
4
|
export * from './define.js'
|
|
5
5
|
export * from './enum.util.js'
|
|
6
6
|
export * from './env.js'
|
|
7
|
-
export * from './env/buildInfo.js'
|
|
7
|
+
export type * from './env/buildInfo.js'
|
|
8
8
|
export * from './form.util.js'
|
|
9
9
|
export * from './is.util.js'
|
|
10
10
|
export * from './iter/asyncIterable2.js'
|
|
@@ -452,7 +452,7 @@ export function _unset<T extends AnyObject>(obj: T, prop: string): void {
|
|
|
452
452
|
return
|
|
453
453
|
}
|
|
454
454
|
|
|
455
|
-
if (
|
|
455
|
+
if (Object.hasOwn(obj, prop)) {
|
|
456
456
|
delete obj[prop]
|
|
457
457
|
return
|
|
458
458
|
}
|
|
@@ -599,7 +599,7 @@ export function _deepFreeze(o: any): void {
|
|
|
599
599
|
|
|
600
600
|
Object.getOwnPropertyNames(o).forEach(prop => {
|
|
601
601
|
if (
|
|
602
|
-
|
|
602
|
+
Object.hasOwn(o, prop) &&
|
|
603
603
|
o[prop] !== null &&
|
|
604
604
|
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
|
605
605
|
!Object.isFrozen(o[prop])
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { nowUnixMillis } from '../datetime/
|
|
1
|
+
import { nowUnixMillis } from '../datetime/time.util.js'
|
|
2
2
|
import { ErrorMode } from '../error/errorMode.js'
|
|
3
3
|
import { createCommonLoggerAtLevel } from '../log/commonLogger.js'
|
|
4
4
|
import type { CommonLogger, CommonLogLevel } from '../log/commonLogger.js'
|
package/src/promise/pRetry.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { nowUnixMillis } from '../datetime/
|
|
2
|
-
import { _since } from '../datetime/time.util.js'
|
|
1
|
+
import { _since, nowUnixMillis } from '../datetime/time.util.js'
|
|
3
2
|
import type { ErrorData } from '../error/error.model.js'
|
|
4
3
|
import { _errorDataAppend } from '../error/error.util.js'
|
|
5
4
|
import { consoleLogger } from '../log/commonLogger.js'
|
package/src/semver.ts
CHANGED
|
@@ -147,13 +147,17 @@ interface SemverFn extends SemverFactory {
|
|
|
147
147
|
(input: SemverInput): Semver
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
|
151
|
+
export const semver2 = /* @__PURE__ */ createSemverFn()
|
|
152
|
+
|
|
153
|
+
function createSemverFn(): SemverFn {
|
|
154
|
+
const semverFactory = new SemverFactory()
|
|
155
|
+
const fn = semverFactory.fromInput.bind(semverFactory) as SemverFn
|
|
156
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
157
|
+
// And probably 2023 as well.
|
|
158
|
+
Object.setPrototypeOf(fn, semverFactory)
|
|
159
|
+
return fn
|
|
160
|
+
}
|
|
157
161
|
|
|
158
162
|
/**
|
|
159
163
|
* Returns 1 if a > b
|
package/src/string/stringify.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
+
import type { ErrorLike } from '../error/error.model.js'
|
|
1
2
|
// oxlint-disable-next-line import/no-cycle -- intentional cycle
|
|
2
3
|
import { _isBackendErrorResponseObject, _isErrorLike, _isErrorObject } from '../error/error.util.js'
|
|
3
|
-
import type { ErrorLike } from '../error/index.js'
|
|
4
4
|
import type { Reviver } from '../types.js'
|
|
5
5
|
// oxlint-disable-next-line import/no-cycle -- intentional cycle
|
|
6
6
|
import { _jsonParseIfPossible } from './json.util.js'
|
|
7
7
|
import { _safeJsonStringify } from './safeJsonStringify.js'
|
|
8
8
|
import { _truncateMiddle } from './string.util.js'
|
|
9
9
|
|
|
10
|
-
const supportsAggregateError = typeof globalThis.AggregateError === 'function'
|
|
11
|
-
|
|
12
10
|
let globalStringifyFunction: JsonStringifyFunction = _safeJsonStringify
|
|
13
11
|
|
|
14
12
|
/**
|
|
@@ -184,7 +182,7 @@ function stringifyErrorLike(obj: Error | ErrorLike, opt: StringifyOptions): stri
|
|
|
184
182
|
s = [s, ...obj.stack.split('\n').slice(sLines)].join('\n')
|
|
185
183
|
}
|
|
186
184
|
|
|
187
|
-
if (
|
|
185
|
+
if (obj instanceof AggregateError && obj.errors.length) {
|
|
188
186
|
s = [
|
|
189
187
|
s,
|
|
190
188
|
`${obj.errors.length} error(s):`,
|