@owox/internal-helpers 0.15.0-next-20251210123721 → 0.15.0-next-20251210232724
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/utils/fetchWithBackoff.d.ts.map +1 -1
- package/dist/utils/fetchWithBackoff.js +5 -9
- package/dist/utils/formatDuration.d.ts +6 -0
- package/dist/utils/formatDuration.d.ts.map +1 -0
- package/dist/utils/formatDuration.js +23 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -10,5 +10,6 @@ export * from './utils/runWithConcurrency.js';
|
|
|
10
10
|
export * from './utils/zodSchemaParser.js';
|
|
11
11
|
export * from './utils/extractJsonFromText.js';
|
|
12
12
|
export * from './utils/trimString.js';
|
|
13
|
+
export * from './utils/formatDuration.js';
|
|
13
14
|
export * from './integrations/event-bus/index.js';
|
|
14
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kCAAkC,CAAC;AACjD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mCAAmC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kCAAkC,CAAC;AACjD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mCAAmC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,4 +10,5 @@ export * from './utils/runWithConcurrency.js';
|
|
|
10
10
|
export * from './utils/zodSchemaParser.js';
|
|
11
11
|
export * from './utils/extractJsonFromText.js';
|
|
12
12
|
export * from './utils/trimString.js';
|
|
13
|
+
export * from './utils/formatDuration.js';
|
|
13
14
|
export * from './integrations/event-bus/index.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchWithBackoff.d.ts","sourceRoot":"","sources":["../../src/utils/fetchWithBackoff.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchWithBackoff.d.ts","sourceRoot":"","sources":["../../src/utils/fetchWithBackoff.ts"],"names":[],"mappings":"AAIA,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,EACpB,UAAU,SAAI,EACd,YAAY,SAAM,EAClB,SAAS,SAAS,GACjB,OAAO,CAAC,QAAQ,CAAC,CAyCnB;AAED,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,WAAgB,EACzB,SAAS,SAAS,GACjB,OAAO,CAAC,QAAQ,CAAC,CAWnB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { LoggerFactory } from '../logging/logger-factory.js';
|
|
2
2
|
import { castError } from './castError.js';
|
|
3
|
+
import { formatDuration } from './formatDuration.js';
|
|
3
4
|
export async function fetchWithBackoff(url, options, maxRetries = 3, initialDelay = 300, timeoutMs = 25_000) {
|
|
4
5
|
const logger = LoggerFactory.createNamedLogger('fetchWithBackoff');
|
|
5
6
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
@@ -9,7 +10,7 @@ export async function fetchWithBackoff(url, options, maxRetries = 3, initialDela
|
|
|
9
10
|
if (res.status >= 500 || res.status === 429) {
|
|
10
11
|
const retryAfter = parseInt(res.headers.get('retry-after') ?? '0', 10);
|
|
11
12
|
const backoff = retryAfter > 0 ? retryAfter * 1000 : initialDelay * Math.pow(2, attempt - 1);
|
|
12
|
-
logger.debug(`[fetchWithBackoff] ${res.status} retrying in ${backoff}
|
|
13
|
+
logger.debug(`[fetchWithBackoff] ${res.status} retrying in ${formatDuration(backoff)}`);
|
|
13
14
|
await new Promise(r => setTimeout(r, backoff));
|
|
14
15
|
continue;
|
|
15
16
|
}
|
|
@@ -36,18 +37,13 @@ export async function fetchWithBackoff(url, options, maxRetries = 3, initialDela
|
|
|
36
37
|
throw new Error('fetchWithBackoff: exceeded max retries');
|
|
37
38
|
}
|
|
38
39
|
export async function fetchWithTimeout(url, options = {}, timeoutMs = 25_000) {
|
|
39
|
-
const controller = new AbortController();
|
|
40
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
41
40
|
try {
|
|
42
|
-
return await fetch(url, { ...options, signal:
|
|
41
|
+
return await fetch(url, { ...options, signal: AbortSignal.timeout(timeoutMs) });
|
|
43
42
|
}
|
|
44
43
|
catch (err) {
|
|
45
|
-
if (castError(err).name === '
|
|
46
|
-
throw new Error(`Fetch timeout: request did not complete within ${timeoutMs}
|
|
44
|
+
if (castError(err).name === 'TimeoutError') {
|
|
45
|
+
throw new Error(`Fetch timeout: request did not complete within ${formatDuration(timeoutMs)}`);
|
|
47
46
|
}
|
|
48
47
|
throw err;
|
|
49
48
|
}
|
|
50
|
-
finally {
|
|
51
|
-
clearTimeout(timer);
|
|
52
|
-
}
|
|
53
49
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDuration.d.ts","sourceRoot":"","sources":["../../src/utils/formatDuration.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAsBjD"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats milliseconds into human-readable string.
|
|
3
|
+
* Automatically chooses ms, seconds, minutes, hours, days.
|
|
4
|
+
*/
|
|
5
|
+
export function formatDuration(ms) {
|
|
6
|
+
if (ms < 1000) {
|
|
7
|
+
return `${ms.toFixed(0)}ms`;
|
|
8
|
+
}
|
|
9
|
+
const sec = ms / 1000;
|
|
10
|
+
if (sec < 60) {
|
|
11
|
+
return `${sec.toFixed(2)}s`;
|
|
12
|
+
}
|
|
13
|
+
const min = sec / 60;
|
|
14
|
+
if (min < 60) {
|
|
15
|
+
return `${min.toFixed(2)}min`;
|
|
16
|
+
}
|
|
17
|
+
const hours = min / 60;
|
|
18
|
+
if (hours < 24) {
|
|
19
|
+
return `${hours.toFixed(2)}h`;
|
|
20
|
+
}
|
|
21
|
+
const days = hours / 24;
|
|
22
|
+
return `${days.toFixed(2)}d`;
|
|
23
|
+
}
|