@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/dist/abort.d.ts
CHANGED
|
@@ -28,14 +28,6 @@ export declare function createAbortableSignal(): AbortableSignal;
|
|
|
28
28
|
* Otherwise returns undefined.
|
|
29
29
|
*/
|
|
30
30
|
export declare function abortSignalTimeoutOrUndefined(ms: NumberOfMilliseconds | undefined): AbortSignal | undefined;
|
|
31
|
-
/**
|
|
32
|
-
* Returns an AbortSignal that aborts after the given number of milliseconds.
|
|
33
|
-
* Uses native `AbortSignal.timeout()` when available, falls back to a polyfill.
|
|
34
|
-
*
|
|
35
|
-
* The abort reason is a DOMException with name "TimeoutError".
|
|
36
|
-
*/
|
|
37
|
-
export declare function abortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal;
|
|
38
|
-
export declare function polyfilledAbortSignalTimeout(ms: NumberOfMilliseconds): AbortSignal;
|
|
39
31
|
/**
|
|
40
32
|
* Returns AbortSignal.any(signals) is the array (after filtering undefined inputs) is not empty,
|
|
41
33
|
* otherwise undefined.
|
package/dist/abort.js
CHANGED
|
@@ -16,25 +16,7 @@ export function createAbortableSignal() {
|
|
|
16
16
|
* Otherwise returns undefined.
|
|
17
17
|
*/
|
|
18
18
|
export function abortSignalTimeoutOrUndefined(ms) {
|
|
19
|
-
return ms ?
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Returns an AbortSignal that aborts after the given number of milliseconds.
|
|
23
|
-
* Uses native `AbortSignal.timeout()` when available, falls back to a polyfill.
|
|
24
|
-
*
|
|
25
|
-
* The abort reason is a DOMException with name "TimeoutError".
|
|
26
|
-
*/
|
|
27
|
-
export function abortSignalTimeout(ms) {
|
|
28
|
-
return typeof AbortSignal.timeout === 'function'
|
|
29
|
-
? AbortSignal.timeout(ms)
|
|
30
|
-
: polyfilledAbortSignalTimeout(ms);
|
|
31
|
-
}
|
|
32
|
-
export function polyfilledAbortSignalTimeout(ms) {
|
|
33
|
-
const ac = new AbortController();
|
|
34
|
-
setTimeout(() => {
|
|
35
|
-
ac.abort(new DOMException('The operation was aborted due to timeout', 'TimeoutError'));
|
|
36
|
-
}, ms);
|
|
37
|
-
return ac.signal;
|
|
19
|
+
return ms ? AbortSignal.timeout(ms) : undefined;
|
|
38
20
|
}
|
|
39
21
|
/**
|
|
40
22
|
* Returns AbortSignal.any(signals) is the array (after filtering undefined inputs) is not empty,
|
|
@@ -57,6 +39,7 @@ export function abortSignalAny(signals) {
|
|
|
57
39
|
if (signals.length === 1) {
|
|
58
40
|
return signals[0];
|
|
59
41
|
}
|
|
42
|
+
// AbortSignal.any needs Chrome 116+ / Safari 17.4+, above our browser floor (Chrome 110, iOS 16.7)
|
|
60
43
|
return typeof AbortSignal.any === 'function'
|
|
61
44
|
? AbortSignal.any(signals) // lib.dom types require a mutable array, but it is only read
|
|
62
45
|
: polyfilledAbortSignalAny(signals);
|
|
@@ -99,9 +99,7 @@ export declare function _find<T>(items: readonly T[], predicate: AbortablePredic
|
|
|
99
99
|
/**
|
|
100
100
|
* Similar to `Array.findLast`, but the `predicate` may return `END` to stop the iteration early.
|
|
101
101
|
*
|
|
102
|
-
* Use `Array.findLast` if you don't need to stop the iteration early
|
|
103
|
-
* - in Node since 18+
|
|
104
|
-
* - in iOS Safari since 15.4
|
|
102
|
+
* Use `Array.findLast` if you don't need to stop the iteration early.
|
|
105
103
|
*/
|
|
106
104
|
export declare function _findLast<T>(items: readonly T[], predicate: AbortablePredicate<T>): T | undefined;
|
|
107
105
|
export declare function _takeWhile<T>(items: readonly T[], predicate: Predicate<T>): T[];
|
package/dist/array/array.util.js
CHANGED
|
@@ -174,9 +174,7 @@ export function _find(items, predicate) {
|
|
|
174
174
|
/**
|
|
175
175
|
* Similar to `Array.findLast`, but the `predicate` may return `END` to stop the iteration early.
|
|
176
176
|
*
|
|
177
|
-
* Use `Array.findLast` if you don't need to stop the iteration early
|
|
178
|
-
* - in Node since 18+
|
|
179
|
-
* - in iOS Safari since 15.4
|
|
177
|
+
* Use `Array.findLast` if you don't need to stop the iteration early.
|
|
180
178
|
*/
|
|
181
179
|
export function _findLast(items, predicate) {
|
|
182
180
|
return _find(items.toReversed(), predicate);
|
package/dist/array/sort.js
CHANGED
|
@@ -42,7 +42,7 @@ class Comparators {
|
|
|
42
42
|
return b.created - a.created;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
|
-
export const comparators = new Comparators();
|
|
45
|
+
export const comparators = /* @__PURE__ */ new Comparators();
|
|
46
46
|
/**
|
|
47
47
|
* _sortBy([{age: 20}, {age: 10}], 'age')
|
|
48
48
|
* // => [{age: 10}, {age: 20}]
|
package/dist/array/sortedSet.js
CHANGED
package/dist/asyncManager.js
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
|
class AsyncManagerImpl {
|
|
4
4
|
logger = consoleLogger;
|
|
@@ -82,6 +82,6 @@ class AsyncManagerImpl {
|
|
|
82
82
|
*
|
|
83
83
|
* @experimental
|
|
84
84
|
*/
|
|
85
|
-
export const AsyncManager = new AsyncManagerImpl();
|
|
85
|
+
export const AsyncManager = /* @__PURE__ */ new AsyncManagerImpl();
|
|
86
86
|
// Shorthand alias
|
|
87
|
-
export const runInBackground = AsyncManager.runInBackground.bind(AsyncManager);
|
|
87
|
+
export const runInBackground = /* @__PURE__ */ AsyncManager.runInBackground.bind(AsyncManager);
|
package/dist/browser/topbar.js
CHANGED
|
@@ -719,8 +719,13 @@ class LocalDateFactory {
|
|
|
719
719
|
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
720
720
|
}
|
|
721
721
|
}
|
|
722
|
-
|
|
723
|
-
export const localDate =
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
722
|
+
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
|
723
|
+
export const localDate = /* @__PURE__ */ createLocalDateFn();
|
|
724
|
+
function createLocalDateFn() {
|
|
725
|
+
const localDateFactory = new LocalDateFactory();
|
|
726
|
+
const fn = localDateFactory.fromInput.bind(localDateFactory);
|
|
727
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
728
|
+
// And probably 2023 as well.
|
|
729
|
+
Object.setPrototypeOf(fn, localDateFactory);
|
|
730
|
+
return fn;
|
|
731
|
+
}
|
|
@@ -258,13 +258,13 @@ declare class LocalTimeFactory {
|
|
|
258
258
|
Convenience function to return current Unix timestamp in seconds.
|
|
259
259
|
Like Date.now(), but in seconds.
|
|
260
260
|
|
|
261
|
-
Import nowUnix
|
|
261
|
+
Import nowUnix from time.util.js in places where importing localTime is too heavy.
|
|
262
262
|
*/
|
|
263
263
|
nowUnix(): UnixTimestamp;
|
|
264
264
|
/**
|
|
265
265
|
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
266
266
|
|
|
267
|
-
Import nowUnixMillis
|
|
267
|
+
Import nowUnixMillis from time.util.js in places where importing localTime is too heavy.
|
|
268
268
|
*/
|
|
269
269
|
nowUnixMillis(): UnixTimestampMillis;
|
|
270
270
|
/**
|
|
@@ -335,21 +335,4 @@ interface LocalTimeFn extends LocalTimeFactory {
|
|
|
335
335
|
(d: LocalTimeInput): LocalTime;
|
|
336
336
|
}
|
|
337
337
|
export declare const localTime: LocalTimeFn;
|
|
338
|
-
/**
|
|
339
|
-
* Can be used instead of `oxlint-disable ...` in places where Date is intentionally needed
|
|
340
|
-
*/
|
|
341
|
-
export declare function newDate(): Date;
|
|
342
|
-
/**
|
|
343
|
-
Convenience function to return current Unix timestamp in seconds.
|
|
344
|
-
Like Date.now(), but in seconds.
|
|
345
|
-
|
|
346
|
-
Import nowUnix function individually in places where importing localTime is too heavy.
|
|
347
|
-
*/
|
|
348
|
-
export declare function nowUnix(): UnixTimestamp;
|
|
349
|
-
/**
|
|
350
|
-
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
351
|
-
|
|
352
|
-
Import nowUnixMillis function individually in places where importing localTime is too heavy.
|
|
353
|
-
*/
|
|
354
|
-
export declare function nowUnixMillis(): UnixTimestampMillis;
|
|
355
338
|
export {};
|
|
@@ -2,7 +2,7 @@ import { comparators } from '../array/sort.js';
|
|
|
2
2
|
import { _assert } from '../error/assert.js';
|
|
3
3
|
import { Intl2 } from '../intl/intl.js';
|
|
4
4
|
import { localDate } from './localDate.js';
|
|
5
|
-
import { _ms } from './time.util.js';
|
|
5
|
+
import { _ms, nowUnix, nowUnixMillis } from './time.util.js';
|
|
6
6
|
// oxlint-disable-next-line import/no-cycle -- intentional cycle
|
|
7
7
|
import { WallTime } from './wallTime.js';
|
|
8
8
|
export var ISODayOfWeek;
|
|
@@ -677,7 +677,7 @@ class LocalTimeFactory {
|
|
|
677
677
|
Convenience function to return current Unix timestamp in seconds.
|
|
678
678
|
Like Date.now(), but in seconds.
|
|
679
679
|
|
|
680
|
-
Import nowUnix
|
|
680
|
+
Import nowUnix from time.util.js in places where importing localTime is too heavy.
|
|
681
681
|
*/
|
|
682
682
|
nowUnix() {
|
|
683
683
|
return nowUnix();
|
|
@@ -685,7 +685,7 @@ class LocalTimeFactory {
|
|
|
685
685
|
/**
|
|
686
686
|
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
687
687
|
|
|
688
|
-
Import nowUnixMillis
|
|
688
|
+
Import nowUnixMillis from time.util.js in places where importing localTime is too heavy.
|
|
689
689
|
*/
|
|
690
690
|
nowUnixMillis() {
|
|
691
691
|
return nowUnixMillis();
|
|
@@ -1031,31 +1031,13 @@ function getWallComponentsInTimezone(date, tz) {
|
|
|
1031
1031
|
}
|
|
1032
1032
|
return dto;
|
|
1033
1033
|
}
|
|
1034
|
-
|
|
1035
|
-
export const localTime =
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
return new Date();
|
|
1044
|
-
}
|
|
1045
|
-
/**
|
|
1046
|
-
Convenience function to return current Unix timestamp in seconds.
|
|
1047
|
-
Like Date.now(), but in seconds.
|
|
1048
|
-
|
|
1049
|
-
Import nowUnix function individually in places where importing localTime is too heavy.
|
|
1050
|
-
*/
|
|
1051
|
-
export function nowUnix() {
|
|
1052
|
-
return Math.floor(Date.now() / 1000);
|
|
1053
|
-
}
|
|
1054
|
-
/**
|
|
1055
|
-
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
1056
|
-
|
|
1057
|
-
Import nowUnixMillis function individually in places where importing localTime is too heavy.
|
|
1058
|
-
*/
|
|
1059
|
-
export function nowUnixMillis() {
|
|
1060
|
-
return Date.now();
|
|
1034
|
+
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
|
1035
|
+
export const localTime = /* @__PURE__ */ createLocalTimeFn();
|
|
1036
|
+
function createLocalTimeFn() {
|
|
1037
|
+
const localTimeFactory = new LocalTimeFactory();
|
|
1038
|
+
const fn = localTimeFactory.fromInput.bind(localTimeFactory);
|
|
1039
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
1040
|
+
// And probably 2023 as well.
|
|
1041
|
+
Object.setPrototypeOf(fn, localTimeFactory);
|
|
1042
|
+
return fn;
|
|
1061
1043
|
}
|
|
@@ -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
|
* using _ = blockTimer()
|
|
4
4
|
* // will log "took 1.234 sec" on dispose
|
|
@@ -23,3 +23,20 @@ export declare function _since(from: UnixTimestampMillis, until?: UnixTimestampM
|
|
|
23
23
|
* 1h3m12s
|
|
24
24
|
*/
|
|
25
25
|
export declare function _ms(millis: NumberOfMilliseconds): string;
|
|
26
|
+
/**
|
|
27
|
+
* Can be used instead of `oxlint-disable ...` in places where Date is intentionally needed
|
|
28
|
+
*/
|
|
29
|
+
export declare function newDate(): Date;
|
|
30
|
+
/**
|
|
31
|
+
Convenience function to return current Unix timestamp in seconds.
|
|
32
|
+
Like Date.now(), but in seconds.
|
|
33
|
+
|
|
34
|
+
Lives here rather than in localTime.ts, so importing it doesn't pull LocalTime into browser bundles.
|
|
35
|
+
*/
|
|
36
|
+
export declare function nowUnix(): UnixTimestamp;
|
|
37
|
+
/**
|
|
38
|
+
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
39
|
+
|
|
40
|
+
Lives here rather than in localTime.ts, so importing it doesn't pull LocalTime into browser bundles.
|
|
41
|
+
*/
|
|
42
|
+
export declare function nowUnixMillis(): UnixTimestampMillis;
|
|
@@ -8,8 +8,7 @@
|
|
|
8
8
|
* @experimental
|
|
9
9
|
*/
|
|
10
10
|
export function _blockTimer(name) {
|
|
11
|
-
|
|
12
|
-
const started = Date.now();
|
|
11
|
+
const started = nowUnixMillis();
|
|
13
12
|
return {
|
|
14
13
|
[Symbol.dispose]() {
|
|
15
14
|
console.log(`${name ? name + ' ' : ''}took ${_since(started)}`);
|
|
@@ -19,9 +18,7 @@ export function _blockTimer(name) {
|
|
|
19
18
|
/**
|
|
20
19
|
* Returns time passed since `from` until `until` (default to Date.now())
|
|
21
20
|
*/
|
|
22
|
-
export function _since(from,
|
|
23
|
-
// oxlint-disable-next-line no-restricted-globals -- localTime imports this module, so it can't import localTime back
|
|
24
|
-
until = Date.now()) {
|
|
21
|
+
export function _since(from, until = nowUnixMillis()) {
|
|
25
22
|
return _ms(until - from);
|
|
26
23
|
}
|
|
27
24
|
/**
|
|
@@ -62,3 +59,29 @@ export function _ms(millis) {
|
|
|
62
59
|
const days = Math.floor(hrs / 24);
|
|
63
60
|
return `${days} days`;
|
|
64
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Can be used instead of `oxlint-disable ...` in places where Date is intentionally needed
|
|
64
|
+
*/
|
|
65
|
+
export function newDate() {
|
|
66
|
+
// oxlint-disable-next-line no-restricted-globals -- the one sanctioned way to get a native Date
|
|
67
|
+
return new Date();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
Convenience function to return current Unix timestamp in seconds.
|
|
71
|
+
Like Date.now(), but in seconds.
|
|
72
|
+
|
|
73
|
+
Lives here rather than in localTime.ts, so importing it doesn't pull LocalTime into browser bundles.
|
|
74
|
+
*/
|
|
75
|
+
export function nowUnix() {
|
|
76
|
+
// oxlint-disable-next-line no-restricted-globals -- the one sanctioned way to read the clock
|
|
77
|
+
return Math.floor(Date.now() / 1000);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
Convenience function that retuns the same as Date.now(), but with proper type of UnixTimestampMillis.
|
|
81
|
+
|
|
82
|
+
Lives here rather than in localTime.ts, so importing it doesn't pull LocalTime into browser bundles.
|
|
83
|
+
*/
|
|
84
|
+
export function nowUnixMillis() {
|
|
85
|
+
// oxlint-disable-next-line no-restricted-globals -- the one sanctioned way to read the clock
|
|
86
|
+
return Date.now();
|
|
87
|
+
}
|
|
@@ -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
|
export function _debounce(func, wait, opt = {}) {
|
|
@@ -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 { SimpleMovingAverage } from '../math/sma.js';
|
package/dist/error/tryCatch.js
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 { _anyToError } from './error.util.js';
|
|
5
4
|
/**
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/// <reference lib="dom" preserve="true" />
|
|
2
|
+
import type { HttpRequestErrorData } from '../error/error.model.js';
|
|
3
|
+
import type { NumberOfMilliseconds } from '../types.js';
|
|
4
|
+
import type { HttpMethod } from './http.model.js';
|
|
5
|
+
/**
|
|
6
|
+
* Minimal wrapper around native `fetch`:
|
|
7
|
+
*
|
|
8
|
+
* - returns json output via Promise
|
|
9
|
+
* - throws FetchError (a plain Error with `cause` and `data`) if anything goes wrong
|
|
10
|
+
* - retries 2 times with simple non-configurable exponential back-off
|
|
11
|
+
*
|
|
12
|
+
* For anything more complex - use the Fetcher.
|
|
13
|
+
* Use this wrapper for the simplest use cases, and where bundle size matters.
|
|
14
|
+
*/
|
|
15
|
+
export declare function _fetch<T = unknown>(url: string, opt?: FetchOptions): Promise<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Accepts the commonly used RequestInit options.
|
|
18
|
+
* `signal` is replaced by `timeout`.
|
|
19
|
+
*/
|
|
20
|
+
export interface FetchOptions extends Pick<RequestInit, 'body' | 'cache' | 'credentials' | 'keepalive' | 'redirect'> {
|
|
21
|
+
/**
|
|
22
|
+
* Defaults to GET
|
|
23
|
+
*/
|
|
24
|
+
method?: HttpMethod;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
/**
|
|
27
|
+
* Request body. It's JSON.stringified, and the Content-Type header is set to `application/json`.
|
|
28
|
+
* Takes precedence over `body`.
|
|
29
|
+
*/
|
|
30
|
+
json?: any;
|
|
31
|
+
/**
|
|
32
|
+
* Defaults to 30 seconds. 0 disables the timeout.
|
|
33
|
+
* Covers the whole request, including reading the body.
|
|
34
|
+
*/
|
|
35
|
+
timeout?: NumberOfMilliseconds;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Thrown by `_fetch`: a plain Error, with the response body text (or the underlying error)
|
|
39
|
+
* as `cause`, and a subset of HttpRequestErrorData as `data`.
|
|
40
|
+
*/
|
|
41
|
+
export interface FetchError extends Error {
|
|
42
|
+
data: Pick<HttpRequestErrorData, 'responseStatusCode' | 'requestUrl' | 'requestMethod'>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/// <reference lib="dom" preserve="true" />
|
|
2
|
+
/**
|
|
3
|
+
* Minimal wrapper around native `fetch`:
|
|
4
|
+
*
|
|
5
|
+
* - returns json output via Promise
|
|
6
|
+
* - throws FetchError (a plain Error with `cause` and `data`) if anything goes wrong
|
|
7
|
+
* - retries 2 times with simple non-configurable exponential back-off
|
|
8
|
+
*
|
|
9
|
+
* For anything more complex - use the Fetcher.
|
|
10
|
+
* Use this wrapper for the simplest use cases, and where bundle size matters.
|
|
11
|
+
*/
|
|
12
|
+
export async function _fetch(url, opt = {}) {
|
|
13
|
+
const { method = 'GET', timeout = 30_000, json, ...init } = opt;
|
|
14
|
+
if (json !== undefined) {
|
|
15
|
+
init.body = JSON.stringify(json);
|
|
16
|
+
init.headers = { 'content-type': 'application/json', ...init.headers };
|
|
17
|
+
}
|
|
18
|
+
let res;
|
|
19
|
+
let cause;
|
|
20
|
+
for (let attempt = 0;; attempt++) {
|
|
21
|
+
res = undefined;
|
|
22
|
+
try {
|
|
23
|
+
res = await fetch(url, {
|
|
24
|
+
...init,
|
|
25
|
+
method,
|
|
26
|
+
// A fresh signal per attempt, as a timed-out signal stays aborted
|
|
27
|
+
signal: timeout ? AbortSignal.timeout(timeout) : undefined,
|
|
28
|
+
});
|
|
29
|
+
if (res.ok) {
|
|
30
|
+
const text = await res.text();
|
|
31
|
+
return (text ? JSON.parse(text) : {});
|
|
32
|
+
}
|
|
33
|
+
cause = await res.text();
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
// Network failure, timeout, or failure to read/parse the body
|
|
37
|
+
cause = err;
|
|
38
|
+
}
|
|
39
|
+
if (attempt === 2 || !shouldRetry(method, res))
|
|
40
|
+
break;
|
|
41
|
+
await pDelay(1000 * 2 ** attempt); // 1s, then 2s
|
|
42
|
+
}
|
|
43
|
+
const err = Object.assign(
|
|
44
|
+
// E.g `503 GET https://example.com/api`, or `GET https://example.com/api` on a network failure
|
|
45
|
+
new Error([res?.status, method, url].filter(Boolean).join(' '), { cause }), { data: { responseStatusCode: res?.status || 0, requestUrl: url, requestMethod: method } });
|
|
46
|
+
throw err;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Retries network failures (including timeouts), 5xx, 408 and 429.
|
|
50
|
+
* Never retries a POST.
|
|
51
|
+
*/
|
|
52
|
+
function shouldRetry(method, res) {
|
|
53
|
+
return (method !== 'POST' && (!res || res.status >= 500 || res.status === 408 || res.status === 429));
|
|
54
|
+
}
|
|
55
|
+
async function pDelay(ms) {
|
|
56
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
57
|
+
}
|
package/dist/http/fetcher.js
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
/// <reference lib="dom" preserve="true" />
|
|
3
3
|
/// <reference lib="dom.iterable" preserve="true" />
|
|
4
4
|
import { abortSignalAnyOrUndefined, abortSignalTimeoutOrUndefined } from '../abort.js';
|
|
5
|
-
import { nowUnixMillis } from '../datetime/
|
|
6
|
-
import { _ms, _since } from '../datetime/time.util.js';
|
|
5
|
+
import { _ms, _since, nowUnixMillis } from '../datetime/time.util.js';
|
|
7
6
|
import { isServerSide } from '../env.js';
|
|
8
7
|
import { _assert, _assertErrorClassOrRethrow, _assertIsError } from '../error/assert.js';
|
|
9
8
|
import { _anyToError, _anyToErrorObject, _errorDataAppend, _errorLikeToErrorObject, HttpRequestError, UnexpectedPassError, } from '../error/error.util.js';
|
|
@@ -12,9 +11,10 @@ import { _clamp } from '../number/number.util.js';
|
|
|
12
11
|
import { _filterFalsyValues, _filterNullishValues, _filterUndefinedValues, _mapKeys, _merge, _omit, _pick, } from '../object/object.util.js';
|
|
13
12
|
import { pDelaySignal } from '../promise/pDelay.js';
|
|
14
13
|
import { pTimeout } from '../promise/pTimeout.js';
|
|
15
|
-
import { _toUrlOrNull } from '../string/index.js';
|
|
16
14
|
import { _jsonParse, _jsonParseIfPossible } from '../string/json.util.js';
|
|
15
|
+
import { _toUrlOrNull } from '../string/url.util.js';
|
|
17
16
|
import { HTTP_METHODS } from './http.model.js';
|
|
17
|
+
const FETCHER_VERSION = 5;
|
|
18
18
|
/**
|
|
19
19
|
* Experimental wrapper around Fetch.
|
|
20
20
|
* Works in both Browser and Node, using `globalThis.fetch`.
|
|
@@ -26,16 +26,14 @@ export class Fetcher {
|
|
|
26
26
|
*
|
|
27
27
|
* Version is to be incremented every time a difference in behaviour (or a bugfix) is done.
|
|
28
28
|
*/
|
|
29
|
-
static VERSION =
|
|
29
|
+
static VERSION = FETCHER_VERSION;
|
|
30
30
|
/**
|
|
31
31
|
* userAgent is statically exposed as Fetcher.userAgent.
|
|
32
32
|
* It can be modified globally, and will be used (read) at the start of every request.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
// Pure, and not reading `this.VERSION`, so bundlers can drop Fetcher when unused (see treeshake.test.ts)
|
|
35
|
+
static userAgent = /* @__PURE__ */ isServerSide() ? `fetcher/${FETCHER_VERSION}` : undefined;
|
|
35
36
|
constructor(cfg = {}) {
|
|
36
|
-
if (typeof globalThis.fetch !== 'function') {
|
|
37
|
-
throw new TypeError('globalThis.fetch is not available');
|
|
38
|
-
}
|
|
39
37
|
this.cfg = this.normalizeCfg(cfg);
|
|
40
38
|
// Dynamically create all helper methods
|
|
41
39
|
for (const method of HTTP_METHODS) {
|
package/dist/http/index.d.ts
CHANGED
package/dist/http/index.js
CHANGED
package/dist/index.d.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';
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ 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';
|
|
8
7
|
export * from './form.util.js';
|
|
9
8
|
export * from './is.util.js';
|
|
10
9
|
export * from './iter/asyncIterable2.js';
|
|
@@ -387,7 +387,7 @@ export function _unset(obj, prop) {
|
|
|
387
387
|
if (!_isObject(obj)) {
|
|
388
388
|
return;
|
|
389
389
|
}
|
|
390
|
-
if (
|
|
390
|
+
if (Object.hasOwn(obj, prop)) {
|
|
391
391
|
delete obj[prop];
|
|
392
392
|
return;
|
|
393
393
|
}
|
|
@@ -512,7 +512,7 @@ export function _deepFreeze(o) {
|
|
|
512
512
|
// Object.freeze — never assigns properties from one object to another.
|
|
513
513
|
Object.freeze(o);
|
|
514
514
|
Object.getOwnPropertyNames(o).forEach(prop => {
|
|
515
|
-
if (
|
|
515
|
+
if (Object.hasOwn(o, prop) &&
|
|
516
516
|
o[prop] !== null &&
|
|
517
517
|
(typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
|
|
518
518
|
!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 { pDefer } from './pDefer.js';
|
package/dist/promise/pRetry.js
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 { _errorDataAppend } from '../error/error.util.js';
|
|
4
3
|
import { consoleLogger } from '../log/commonLogger.js';
|
|
5
4
|
import { pDelay } from './pDelay.js';
|
package/dist/semver.js
CHANGED
|
@@ -126,11 +126,16 @@ class SemverFactory {
|
|
|
126
126
|
return opt.mutate ? items.sort(cmp) : items.toSorted(cmp);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
|
-
|
|
130
|
-
export const semver2 =
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
129
|
+
// Built inside a function, so the whole construct can be marked pure and tree-shaken (see treeshake.test.ts)
|
|
130
|
+
export const semver2 = /* @__PURE__ */ createSemverFn();
|
|
131
|
+
function createSemverFn() {
|
|
132
|
+
const semverFactory = new SemverFactory();
|
|
133
|
+
const fn = semverFactory.fromInput.bind(semverFactory);
|
|
134
|
+
// The line below is the blackest of black magic I have ever written in 2024.
|
|
135
|
+
// And probably 2023 as well.
|
|
136
|
+
Object.setPrototypeOf(fn, semverFactory);
|
|
137
|
+
return fn;
|
|
138
|
+
}
|
|
134
139
|
/**
|
|
135
140
|
* Returns 1 if a > b
|
|
136
141
|
* returns 0 if they are equal
|
package/dist/string/stringify.js
CHANGED
|
@@ -4,7 +4,6 @@ import { _isBackendErrorResponseObject, _isErrorLike, _isErrorObject } from '../
|
|
|
4
4
|
import { _jsonParseIfPossible } from './json.util.js';
|
|
5
5
|
import { _safeJsonStringify } from './safeJsonStringify.js';
|
|
6
6
|
import { _truncateMiddle } from './string.util.js';
|
|
7
|
-
const supportsAggregateError = typeof globalThis.AggregateError === 'function';
|
|
8
7
|
let globalStringifyFunction = _safeJsonStringify;
|
|
9
8
|
/**
|
|
10
9
|
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
@@ -127,7 +126,7 @@ function stringifyErrorLike(obj, opt) {
|
|
|
127
126
|
const sLines = s.split('\n').length;
|
|
128
127
|
s = [s, ...obj.stack.split('\n').slice(sLines)].join('\n');
|
|
129
128
|
}
|
|
130
|
-
if (
|
|
129
|
+
if (obj instanceof AggregateError && obj.errors.length) {
|
|
131
130
|
s = [
|
|
132
131
|
s,
|
|
133
132
|
`${obj.errors.length} error(s):`,
|