@prometheus-ai/utils 0.5.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/types/abortable.d.ts +27 -0
- package/dist/types/async.d.ts +6 -0
- package/dist/types/cli.d.ts +117 -0
- package/dist/types/color.d.ts +102 -0
- package/dist/types/dirs.d.ts +171 -0
- package/dist/types/env.d.ts +55 -0
- package/dist/types/fetch-retry.d.ts +80 -0
- package/dist/types/format.d.ts +37 -0
- package/dist/types/frontmatter.d.ts +25 -0
- package/dist/types/fs-error.d.ts +31 -0
- package/dist/types/glob.d.ts +28 -0
- package/dist/types/hook-fetch.d.ts +16 -0
- package/dist/types/index.d.ts +29 -0
- package/dist/types/json.d.ts +4 -0
- package/dist/types/logger.d.ts +66 -0
- package/dist/types/mermaid-ascii.d.ts +11 -0
- package/dist/types/mime.d.ts +29 -0
- package/dist/types/peek-file.d.ts +29 -0
- package/dist/types/postmortem.d.ts +29 -0
- package/dist/types/procmgr.d.ts +25 -0
- package/dist/types/prompt.d.ts +18 -0
- package/dist/types/ptree.d.ts +108 -0
- package/dist/types/ring.d.ts +93 -0
- package/dist/types/sanitize-text.d.ts +14 -0
- package/dist/types/snowflake.d.ts +25 -0
- package/dist/types/stream.d.ts +68 -0
- package/dist/types/tab-spacing.d.ts +9 -0
- package/dist/types/temp.d.ts +14 -0
- package/dist/types/type-guards.d.ts +3 -0
- package/dist/types/which.d.ts +37 -0
- package/package.json +61 -0
- package/src/abortable.ts +73 -0
- package/src/async.ts +50 -0
- package/src/cli.ts +432 -0
- package/src/color.ts +302 -0
- package/src/dirs.ts +584 -0
- package/src/env.ts +172 -0
- package/src/fetch-retry.ts +325 -0
- package/src/format.ts +113 -0
- package/src/frontmatter.ts +128 -0
- package/src/fs-error.ts +56 -0
- package/src/glob.ts +189 -0
- package/src/hook-fetch.ts +30 -0
- package/src/index.ts +49 -0
- package/src/json.ts +10 -0
- package/src/logger.ts +417 -0
- package/src/mermaid-ascii.ts +31 -0
- package/src/mime.ts +159 -0
- package/src/peek-file.ts +188 -0
- package/src/postmortem.ts +196 -0
- package/src/procmgr.ts +195 -0
- package/src/prompt.ts +471 -0
- package/src/ptree.ts +390 -0
- package/src/ring.ts +169 -0
- package/src/sanitize-text.ts +38 -0
- package/src/snowflake.ts +136 -0
- package/src/stream.ts +403 -0
- package/src/tab-spacing.ts +342 -0
- package/src/temp.ts +77 -0
- package/src/type-guards.ts +11 -0
- package/src/which.ts +232 -0
package/src/env.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { getAgentDir, getConfigRootDir } from "./dirs";
|
|
5
|
+
|
|
6
|
+
const ENV_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Strict shell-identifier shape. Used for dotenv keys we accept into
|
|
10
|
+
* `Bun.env` — those should be referenceable as `$NAME` from POSIX shells,
|
|
11
|
+
* so we reject anything outside `[A-Za-z_][A-Za-z0-9_]*`.
|
|
12
|
+
*/
|
|
13
|
+
export function isValidEnvName(name: string): boolean {
|
|
14
|
+
return ENV_NAME_RE.test(name);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The only names that are genuinely unsafe to forward to a native `execve`
|
|
19
|
+
* spawn: empty, containing `=` (would corrupt the `KEY=VALUE` framing) or
|
|
20
|
+
* NUL (terminates the C string mid-entry). Windows ships standard variables
|
|
21
|
+
* whose names contain parentheses (e.g. `ProgramFiles(x86)`, `CommonProgramFiles(x86)`)
|
|
22
|
+
* — those MUST survive the scrub so downstream resolvers (Git Bash discovery
|
|
23
|
+
* in `procmgr.ts`, etc.) can still read them.
|
|
24
|
+
*/
|
|
25
|
+
export function isSafeEnvName(name: string): boolean {
|
|
26
|
+
return name.length > 0 && !name.includes("=") && !name.includes("\0");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isSafeEnvValue(value: string): boolean {
|
|
30
|
+
return !value.includes("\0");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function filterProcessEnv(env: Record<string, string | undefined>): Record<string, string> {
|
|
34
|
+
const result: Record<string, string> = {};
|
|
35
|
+
for (const key in env) {
|
|
36
|
+
const value = env[key];
|
|
37
|
+
if (!isSafeEnvName(key) || value === undefined || !isSafeEnvValue(value)) continue;
|
|
38
|
+
result[key] = value;
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parses a .env file synchronously and extracts key-value string pairs.
|
|
45
|
+
* Ignores lines that are empty or start with '#'. Trims whitespace.
|
|
46
|
+
* Allows values to be quoted with single or double quotes.
|
|
47
|
+
* Returns an object of key-value pairs.
|
|
48
|
+
*/
|
|
49
|
+
export function parseEnvFile(filePath: string): Record<string, string> {
|
|
50
|
+
const result: Record<string, string> = {};
|
|
51
|
+
try {
|
|
52
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
53
|
+
for (const line of content.split("\n")) {
|
|
54
|
+
const trimmed = line.trim();
|
|
55
|
+
// Skip comments and blank lines
|
|
56
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
57
|
+
|
|
58
|
+
const eqIndex = trimmed.indexOf("=");
|
|
59
|
+
if (eqIndex === -1) continue;
|
|
60
|
+
|
|
61
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
62
|
+
if (!isValidEnvName(key)) continue;
|
|
63
|
+
|
|
64
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
65
|
+
|
|
66
|
+
// Remove surrounding quotes (" or ')
|
|
67
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
68
|
+
value = value.slice(1, -1);
|
|
69
|
+
}
|
|
70
|
+
if (!isSafeEnvValue(value)) continue;
|
|
71
|
+
|
|
72
|
+
result[key] = value;
|
|
73
|
+
}
|
|
74
|
+
} catch {
|
|
75
|
+
// File doesn't exist or can't be read - return empty result
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Eagerly parse the user's $HOME/.env and the current project's .env (from cwd)
|
|
82
|
+
const homeEnv = parseEnvFile(path.join(os.homedir(), ".env"));
|
|
83
|
+
const configRootEnv = parseEnvFile(path.join(getConfigRootDir(), ".env"));
|
|
84
|
+
const agentEnv = parseEnvFile(path.join(getAgentDir(), ".env"));
|
|
85
|
+
const projectEnv = parseEnvFile(path.join(process.cwd(), ".env"));
|
|
86
|
+
|
|
87
|
+
for (const key of Object.keys(Bun.env)) {
|
|
88
|
+
const value = Bun.env[key];
|
|
89
|
+
if (!isSafeEnvName(key) || value === undefined || !isSafeEnvValue(value)) {
|
|
90
|
+
delete Bun.env[key];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const file of [projectEnv, agentEnv, configRootEnv, homeEnv]) {
|
|
95
|
+
for (const key in file) {
|
|
96
|
+
if (!Bun.env[key]) {
|
|
97
|
+
Bun.env[key] = file[key];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Intentional re-export of Bun.env.
|
|
104
|
+
*
|
|
105
|
+
* All users should import this env module (import { $env } from "@prometheus-ai/utils")
|
|
106
|
+
* before using environment variables. This ensures that .env files have been loaded and
|
|
107
|
+
* overrides (project, home) have been applied, so $env always reflects the correct values.
|
|
108
|
+
*/
|
|
109
|
+
export const $env: Record<string, string> = Bun.env as Record<string, string>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Resolve the first environment variable value from the given keys.
|
|
113
|
+
* @param keys - The keys to resolve.
|
|
114
|
+
* @returns The first environment variable value, or undefined if no value is found.
|
|
115
|
+
*/
|
|
116
|
+
export function $pickenv(...keys: string[]): string | undefined {
|
|
117
|
+
for (const key of keys) {
|
|
118
|
+
const value = Bun.env[key]?.trim();
|
|
119
|
+
if (value) {
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Parses a positive decimal integer from `$env[name]`.
|
|
128
|
+
* Empty, invalid, NaN, zero, or negative values return `defaultValue`.
|
|
129
|
+
*/
|
|
130
|
+
export function $envpos(name: string, defaultValue: number): number {
|
|
131
|
+
const raw = $env[name];
|
|
132
|
+
if (!raw) return defaultValue;
|
|
133
|
+
const parsed = Number.parseInt(raw, 10);
|
|
134
|
+
if (Number.isNaN(parsed) || parsed <= 0) return defaultValue;
|
|
135
|
+
return parsed;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** True when `BUN_ENV` or `NODE_ENV` is the string `test`. */
|
|
139
|
+
export function isBunTestRuntime(): boolean {
|
|
140
|
+
return Bun.env.BUN_ENV === "test" || Bun.env.NODE_ENV === "test";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* True when this code is running inside a `bun build --compile` standalone
|
|
145
|
+
* binary. Detects via the embedded virtual-filesystem path markers
|
|
146
|
+
* (`$bunfs`, `~BUN`, or its URL-encoded form `%7EBUN`) in `import.meta.url`,
|
|
147
|
+
* which Bun rewrites for every module bundled into the executable. The
|
|
148
|
+
* `PROMETHEUS_COMPILED` env var (set by the build script's `--define`) is checked
|
|
149
|
+
* first for cheap fast-path detection.
|
|
150
|
+
*/
|
|
151
|
+
export function isCompiledBinary(): boolean {
|
|
152
|
+
if (Bun.env.PROMETHEUS_COMPILED) return true;
|
|
153
|
+
const url = import.meta.url;
|
|
154
|
+
return url.includes("$bunfs") || url.includes("~BUN") || url.includes("%7EBUN");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const TRUTHY: Dict<boolean> = {
|
|
158
|
+
"1": true,
|
|
159
|
+
Y: true,
|
|
160
|
+
y: true,
|
|
161
|
+
TRUE: true,
|
|
162
|
+
true: true,
|
|
163
|
+
YES: true,
|
|
164
|
+
yes: true,
|
|
165
|
+
ON: true,
|
|
166
|
+
on: true,
|
|
167
|
+
};
|
|
168
|
+
export function $flag(name: string, def: boolean = false): boolean {
|
|
169
|
+
const value = $env[name];
|
|
170
|
+
if (!value) return def;
|
|
171
|
+
return TRUTHY[value] === true;
|
|
172
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { scheduler } from "node:timers/promises";
|
|
2
|
+
|
|
3
|
+
// "reset after 1h2m3s" / "10m15s" / "39s"
|
|
4
|
+
const QUOTA_RESET_PATTERN = /reset after (?:(\d+)h)?(?:(\d+)m)?(\d+(?:\.\d+)?)s/i;
|
|
5
|
+
// "Please retry in 250ms" / "Please retry in 12s"
|
|
6
|
+
const PLEASE_RETRY_PATTERN = /Please retry in ([0-9.]+)(ms|s)/i;
|
|
7
|
+
// JSON field: "retryDelay": "34.074824224s"
|
|
8
|
+
const RETRY_DELAY_FIELD_PATTERN = /"retryDelay":\s*"([0-9.]+)(ms|s)"/i;
|
|
9
|
+
// "try again in 250ms" / "try again in 12s" / "try again in 12sec" /
|
|
10
|
+
// "try again in 5 min" / "try again in ~158 min." / "try again in 2h" /
|
|
11
|
+
// "try again in 90 minutes" / "try again in 1 hour"
|
|
12
|
+
const TRY_AGAIN_PATTERN = /try again in\s+~?\s*([0-9.]+)\s*(ms|sec|s|minutes?|mins?|m|hours?|hrs?|h)\b/i;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Server-suggested retry delay extraction. Merges the patterns historically used
|
|
16
|
+
* by the OpenAI Codex and Google Gemini retry helpers.
|
|
17
|
+
*
|
|
18
|
+
* Header sources (checked in order):
|
|
19
|
+
* - `Retry-After` (numeric seconds, or HTTP date)
|
|
20
|
+
* - `x-ratelimit-reset` (Unix epoch seconds)
|
|
21
|
+
* - `x-ratelimit-reset-after` (seconds)
|
|
22
|
+
*
|
|
23
|
+
* Body patterns:
|
|
24
|
+
* - `Your quota will reset after 18h31m10s` / `10m15s` / `39s`
|
|
25
|
+
* - `Please retry in 250ms` / `Please retry in 12s`
|
|
26
|
+
* - `"retryDelay": "34.074824224s"` (JSON error detail field)
|
|
27
|
+
* - `try again in 250ms` / `try again in 12s` / `try again in 5 min` / `try again in ~158 min`
|
|
28
|
+
*
|
|
29
|
+
* Returns `undefined` if no signal is found.
|
|
30
|
+
*/
|
|
31
|
+
export function extractRetryHint(source: Response | Headers | null | undefined, body?: string): number | undefined {
|
|
32
|
+
const headers = source instanceof Headers ? source : (source?.headers ?? undefined);
|
|
33
|
+
if (headers) {
|
|
34
|
+
const retryAfter = headers.get("retry-after");
|
|
35
|
+
if (retryAfter) {
|
|
36
|
+
const seconds = Number(retryAfter);
|
|
37
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1000);
|
|
38
|
+
const parsedDate = Date.parse(retryAfter);
|
|
39
|
+
if (!Number.isNaN(parsedDate)) return Math.max(0, parsedDate - Date.now());
|
|
40
|
+
}
|
|
41
|
+
const rateLimitReset = headers.get("x-ratelimit-reset");
|
|
42
|
+
if (rateLimitReset) {
|
|
43
|
+
const resetSeconds = Number.parseInt(rateLimitReset, 10);
|
|
44
|
+
if (!Number.isNaN(resetSeconds)) {
|
|
45
|
+
const delta = resetSeconds * 1000 - Date.now();
|
|
46
|
+
if (delta > 0) return delta;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const rateLimitResetAfter = headers.get("x-ratelimit-reset-after");
|
|
50
|
+
if (rateLimitResetAfter) {
|
|
51
|
+
const seconds = Number(rateLimitResetAfter);
|
|
52
|
+
if (Number.isFinite(seconds) && seconds > 0) return seconds * 1000;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!body) return undefined;
|
|
57
|
+
|
|
58
|
+
const quotaMatch = QUOTA_RESET_PATTERN.exec(body);
|
|
59
|
+
if (quotaMatch) {
|
|
60
|
+
const hours = quotaMatch[1] ? Number.parseInt(quotaMatch[1], 10) : 0;
|
|
61
|
+
const minutes = quotaMatch[2] ? Number.parseInt(quotaMatch[2], 10) : 0;
|
|
62
|
+
const seconds = Number.parseFloat(quotaMatch[3]!);
|
|
63
|
+
if (!Number.isNaN(seconds)) {
|
|
64
|
+
const totalMs = ((hours * 60 + minutes) * 60 + seconds) * 1000;
|
|
65
|
+
if (totalMs > 0) return totalMs;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (const pattern of [PLEASE_RETRY_PATTERN, RETRY_DELAY_FIELD_PATTERN, TRY_AGAIN_PATTERN]) {
|
|
69
|
+
const match = pattern.exec(body);
|
|
70
|
+
if (match?.[1]) {
|
|
71
|
+
const value = Number.parseFloat(match[1]);
|
|
72
|
+
if (Number.isFinite(value) && value > 0) {
|
|
73
|
+
const unitMs = unitToMs(match[2]!);
|
|
74
|
+
if (unitMs !== undefined) return value * unitMs;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function unitToMs(unit: string): number | undefined {
|
|
82
|
+
switch (unit.toLowerCase()) {
|
|
83
|
+
case "ms":
|
|
84
|
+
return 1;
|
|
85
|
+
case "s":
|
|
86
|
+
case "sec":
|
|
87
|
+
return 1000;
|
|
88
|
+
case "m":
|
|
89
|
+
case "min":
|
|
90
|
+
case "mins":
|
|
91
|
+
case "minute":
|
|
92
|
+
case "minutes":
|
|
93
|
+
return 60_000;
|
|
94
|
+
case "h":
|
|
95
|
+
case "hr":
|
|
96
|
+
case "hrs":
|
|
97
|
+
case "hour":
|
|
98
|
+
case "hours":
|
|
99
|
+
return 60 * 60_000;
|
|
100
|
+
default:
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface FetchWithRetryOptions extends RequestInit {
|
|
106
|
+
/** Total fetch attempts (initial + retries). Default `5`. */
|
|
107
|
+
maxAttempts?: number;
|
|
108
|
+
/**
|
|
109
|
+
* Per-delay cap. Server-provided `Retry-After` hints exceeding this return
|
|
110
|
+
* the current response immediately — caller deals with the `!response.ok`.
|
|
111
|
+
* Default `60_000`.
|
|
112
|
+
*/
|
|
113
|
+
maxDelayMs?: number;
|
|
114
|
+
/**
|
|
115
|
+
* Fallback delay schedule when no server hint is present. Number, array
|
|
116
|
+
* (indexed by attempt, clamped to last), or function. Default exponential
|
|
117
|
+
* `500ms * 2 ** attempt` capped at `maxDelayMs`.
|
|
118
|
+
*/
|
|
119
|
+
defaultDelayMs?: number | readonly number[] | ((attempt: number) => number);
|
|
120
|
+
/**
|
|
121
|
+
* Optional per-attempt overlay merged into the base `RequestInit` each try.
|
|
122
|
+
* Headers from the overlay shallow-merge over the base. Useful for auth
|
|
123
|
+
* token refresh or user-agent rotation.
|
|
124
|
+
*/
|
|
125
|
+
prepareInit?: (attempt: number) => RequestInit | Promise<RequestInit>;
|
|
126
|
+
/**
|
|
127
|
+
* Optional `fetch` implementation override. Defaults to `globalThis.fetch`.
|
|
128
|
+
* Useful for routing requests through a proxy, instrumented transport, or
|
|
129
|
+
* mock during tests.
|
|
130
|
+
*/
|
|
131
|
+
fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const DEFAULT_MAX_DELAY_MS = 60_000;
|
|
135
|
+
const DEFAULT_MAX_ATTEMPTS = 5;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Fetch with bounded retries and sensible defaults. Retries on any
|
|
139
|
+
* `isRetryableStatus` (5xx, 408, 429) and on transient network errors. Server
|
|
140
|
+
* `Retry-After`/quota hints are honoured up to `maxDelayMs`; a hint that exceeds
|
|
141
|
+
* the cap returns the current response so the caller can fail fast. Aborts on
|
|
142
|
+
* `init.signal` propagate as `"Request was aborted"`.
|
|
143
|
+
*
|
|
144
|
+
* The caller is responsible for inspecting `!response.ok` once the call returns.
|
|
145
|
+
*/
|
|
146
|
+
export async function fetchWithRetry(
|
|
147
|
+
url: string | URL | ((attempt: number) => string | URL),
|
|
148
|
+
options: FetchWithRetryOptions = {},
|
|
149
|
+
): Promise<Response> {
|
|
150
|
+
const {
|
|
151
|
+
maxAttempts = DEFAULT_MAX_ATTEMPTS,
|
|
152
|
+
maxDelayMs = DEFAULT_MAX_DELAY_MS,
|
|
153
|
+
defaultDelayMs,
|
|
154
|
+
prepareInit,
|
|
155
|
+
fetch: fetchImpl = fetch,
|
|
156
|
+
...baseInit
|
|
157
|
+
} = options;
|
|
158
|
+
const signal = baseInit.signal as AbortSignal | undefined;
|
|
159
|
+
|
|
160
|
+
for (let attempt = 0; ; attempt++) {
|
|
161
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
162
|
+
const requestUrl = typeof url === "function" ? url(attempt) : url;
|
|
163
|
+
const init = prepareInit ? mergeInit(baseInit, await prepareInit(attempt)) : baseInit;
|
|
164
|
+
|
|
165
|
+
let response: Response;
|
|
166
|
+
try {
|
|
167
|
+
response = await fetchImpl(requestUrl, init);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (signal?.aborted) throw new Error("Request was aborted");
|
|
170
|
+
const wrapped = wrapNetworkError(error);
|
|
171
|
+
if (attempt + 1 >= maxAttempts) throw wrapped;
|
|
172
|
+
await scheduler.wait(resolveDefaultDelay(defaultDelayMs, attempt, maxDelayMs), { signal });
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!isRetryableStatus(response.status)) return response;
|
|
177
|
+
if (attempt + 1 >= maxAttempts) return response;
|
|
178
|
+
|
|
179
|
+
const hint = extractRetryHint(response, await response.clone().text());
|
|
180
|
+
if (hint !== undefined && hint > maxDelayMs) return response;
|
|
181
|
+
|
|
182
|
+
const delayMs = Math.min(hint ?? resolveDefaultDelay(defaultDelayMs, attempt, maxDelayMs), maxDelayMs);
|
|
183
|
+
await scheduler.wait(delayMs, { signal });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function mergeInit(base: RequestInit, overlay: RequestInit): RequestInit {
|
|
188
|
+
const merged: RequestInit = { ...base, ...overlay };
|
|
189
|
+
if (base.headers || overlay.headers) {
|
|
190
|
+
const baseHeaders = new Headers(base.headers ?? undefined);
|
|
191
|
+
const overlayHeaders = new Headers(overlay.headers ?? undefined);
|
|
192
|
+
overlayHeaders.forEach((value, key) => {
|
|
193
|
+
baseHeaders.set(key, value);
|
|
194
|
+
});
|
|
195
|
+
merged.headers = baseHeaders;
|
|
196
|
+
}
|
|
197
|
+
return merged;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function wrapNetworkError(error: unknown): Error {
|
|
201
|
+
if (error instanceof Error) {
|
|
202
|
+
if (error.name === "AbortError" || error.message === "Request was aborted") {
|
|
203
|
+
return new Error("Request was aborted");
|
|
204
|
+
}
|
|
205
|
+
if (error.message === "fetch failed" && error.cause instanceof Error) {
|
|
206
|
+
return new Error(`Network error: ${error.cause.message}`);
|
|
207
|
+
}
|
|
208
|
+
return error;
|
|
209
|
+
}
|
|
210
|
+
return new Error(String(error));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function resolveDefaultDelay(
|
|
214
|
+
option: FetchWithRetryOptions["defaultDelayMs"],
|
|
215
|
+
attempt: number,
|
|
216
|
+
maxDelayMs: number,
|
|
217
|
+
): number {
|
|
218
|
+
if (option === undefined) return Math.min(500 * 2 ** attempt, maxDelayMs);
|
|
219
|
+
if (typeof option === "number") return Math.min(option, maxDelayMs);
|
|
220
|
+
if (typeof option === "function") return Math.min(option(attempt), maxDelayMs);
|
|
221
|
+
return Math.min(option[Math.min(attempt, option.length - 1)] ?? 0, maxDelayMs);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Inspect an arbitrary error value (or its `cause` chain, up to depth 2) for an
|
|
226
|
+
* HTTP status code. Reads `status`, `statusCode`, and `response.status` fields,
|
|
227
|
+
* coerces string values, and falls back to scanning the error message for
|
|
228
|
+
* common patterns like `Error: 401`, `error (429)`, or `HTTP 503`.
|
|
229
|
+
*/
|
|
230
|
+
export function extractHttpStatusFromError(error: unknown): number | undefined {
|
|
231
|
+
return extractHttpStatusFromErrorInternal(error, 0);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
type HttpErrorLike = {
|
|
235
|
+
message?: string;
|
|
236
|
+
name?: string;
|
|
237
|
+
status?: number | string;
|
|
238
|
+
statusCode?: number | string;
|
|
239
|
+
response?: { status?: number | string };
|
|
240
|
+
cause?: unknown;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
function extractHttpStatusFromErrorInternal(error: unknown, depth: number): number | undefined {
|
|
244
|
+
if (!error || typeof error !== "object" || depth > 2) return undefined;
|
|
245
|
+
const info = error as HttpErrorLike;
|
|
246
|
+
const rawStatus = info.status ?? info.statusCode ?? info.response?.status;
|
|
247
|
+
|
|
248
|
+
let status: number | undefined;
|
|
249
|
+
if (typeof rawStatus === "number" && Number.isFinite(rawStatus)) {
|
|
250
|
+
status = rawStatus;
|
|
251
|
+
} else if (typeof rawStatus === "string") {
|
|
252
|
+
const parsed = Number(rawStatus);
|
|
253
|
+
if (Number.isFinite(parsed)) status = parsed;
|
|
254
|
+
}
|
|
255
|
+
if (status !== undefined && status >= 100 && status <= 599) return status;
|
|
256
|
+
|
|
257
|
+
if (info.message) {
|
|
258
|
+
const extracted = extractStatusFromMessage(info.message);
|
|
259
|
+
if (extracted !== undefined) return extracted;
|
|
260
|
+
}
|
|
261
|
+
if (info.cause) return extractHttpStatusFromErrorInternal(info.cause, depth + 1);
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const STATUS_MESSAGE_PATTERNS = [
|
|
266
|
+
/\berror\s*[:=]\s*(\d{3})\b/i,
|
|
267
|
+
/error\s*\((\d{3})\)/i,
|
|
268
|
+
/status\s*[:=]?\s*(\d{3})/i,
|
|
269
|
+
/\bhttp\s*(\d{3})\b/i,
|
|
270
|
+
/\b(\d{3})\s*(?:status|error)\b/i,
|
|
271
|
+
] as const;
|
|
272
|
+
|
|
273
|
+
function extractStatusFromMessage(message: string): number | undefined {
|
|
274
|
+
for (const pattern of STATUS_MESSAGE_PATTERNS) {
|
|
275
|
+
const match = pattern.exec(message);
|
|
276
|
+
if (!match) continue;
|
|
277
|
+
const value = Number(match[1]);
|
|
278
|
+
if (Number.isFinite(value) && value >= 100 && value <= 599) return value;
|
|
279
|
+
}
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* `true` if the given HTTP status code is one we treat as transient: 408
|
|
285
|
+
* (Request Timeout), 429 (Too Many Requests), or any 5xx (server error).
|
|
286
|
+
*/
|
|
287
|
+
export function isRetryableStatus(status: number): boolean {
|
|
288
|
+
return status >= 500 || status === 408 || status === 429;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* `true` if the message describes an unexpected socket closure — Bun and some
|
|
293
|
+
* proxies surface these for any HTTP/2 stream reset.
|
|
294
|
+
*/
|
|
295
|
+
export function isUnexpectedSocketCloseMessage(message: string): boolean {
|
|
296
|
+
return /\b(?:the\s+)?socket connection (?:was )?closed unexpectedly\b/i.test(message);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const TRANSIENT_MESSAGE_PATTERN =
|
|
300
|
+
/overloaded|rate.?limit|too many requests|service.?unavailable|server error|internal error|connection.?error|unable to connect|fetch failed|network error|stream stall|other side closed|HTTP2(?:StreamReset|RefusedStream|EnhanceYourCalm)/i;
|
|
301
|
+
|
|
302
|
+
const VALIDATION_MESSAGE_PATTERN =
|
|
303
|
+
/invalid|validation|bad request|unsupported|schema|missing required|not found|unauthorized|forbidden/i;
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Identify errors that should be retried: aborts/timeouts in the error name or
|
|
307
|
+
* message, retryable HTTP statuses (see `isRetryableStatus`), unexpected socket
|
|
308
|
+
* closes, and the standard transient phrases. 4xx statuses other than 408/429
|
|
309
|
+
* and validation-shaped messages short-circuit to `false`.
|
|
310
|
+
*/
|
|
311
|
+
export function isRetryableError(error: unknown): boolean {
|
|
312
|
+
const info = error as { message?: string; name?: string } | null;
|
|
313
|
+
const message = info?.message ?? "";
|
|
314
|
+
const name = info?.name ?? "";
|
|
315
|
+
if (name === "AbortError" || /timeout|timed out|aborted/i.test(message)) return true;
|
|
316
|
+
|
|
317
|
+
const status = extractHttpStatusFromError(error);
|
|
318
|
+
if (status !== undefined) {
|
|
319
|
+
if (isRetryableStatus(status)) return true;
|
|
320
|
+
if (status >= 400 && status < 500) return false;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (VALIDATION_MESSAGE_PATTERN.test(message)) return false;
|
|
324
|
+
return isUnexpectedSocketCloseMessage(message) || TRANSIENT_MESSAGE_PATTERN.test(message);
|
|
325
|
+
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const SEC = 1_000;
|
|
2
|
+
const MIN = 60 * SEC;
|
|
3
|
+
const HOUR = 60 * MIN;
|
|
4
|
+
const DAY = 24 * HOUR;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Format a duration in milliseconds to a short human-readable string.
|
|
8
|
+
* Examples: "123ms", "1.5s", "30m15s", "2h30m", "3d2h"
|
|
9
|
+
*/
|
|
10
|
+
export function formatDuration(ms: number): string {
|
|
11
|
+
if (!Number.isFinite(ms) || ms <= 0) return "0ms";
|
|
12
|
+
if (ms < SEC) return `${ms}ms`;
|
|
13
|
+
if (ms < MIN) return `${(ms / SEC).toFixed(1)}s`;
|
|
14
|
+
if (ms < HOUR) {
|
|
15
|
+
const mins = Math.floor(ms / MIN);
|
|
16
|
+
const secs = Math.floor((ms % MIN) / SEC);
|
|
17
|
+
return secs > 0 ? `${mins}m${secs}s` : `${mins}m`;
|
|
18
|
+
}
|
|
19
|
+
if (ms < DAY) {
|
|
20
|
+
const hours = Math.floor(ms / HOUR);
|
|
21
|
+
const mins = Math.floor((ms % HOUR) / MIN);
|
|
22
|
+
return mins > 0 ? `${hours}h${mins}m` : `${hours}h`;
|
|
23
|
+
}
|
|
24
|
+
const days = Math.floor(ms / DAY);
|
|
25
|
+
const hours = Math.floor((ms % DAY) / HOUR);
|
|
26
|
+
return hours > 0 ? `${days}d${hours}h` : `${days}d`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Format a number with K/M/B suffix for compact display.
|
|
31
|
+
* Uses 1 decimal for small leading digits when non-zero, rounded otherwise.
|
|
32
|
+
* Examples: "999", "1K", "1.5K", "25K", "1M", "1.5M", "25M", "1.5B"
|
|
33
|
+
*/
|
|
34
|
+
export function formatNumber(n: number): string {
|
|
35
|
+
if (n < 1_000) return n.toString();
|
|
36
|
+
if (n < 10_000) return `${trim1(n / 1_000)}K`;
|
|
37
|
+
if (n < 1_000_000) return `${Math.round(n / 1_000)}K`;
|
|
38
|
+
if (n < 10_000_000) return `${trim1(n / 1_000_000)}M`;
|
|
39
|
+
if (n < 1_000_000_000) return `${Math.round(n / 1_000_000)}M`;
|
|
40
|
+
if (n < 10_000_000_000) return `${trim1(n / 1_000_000_000)}B`;
|
|
41
|
+
return `${Math.round(n / 1_000_000_000)}B`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Format with up to 1 decimal place, dropping trailing `.0`. */
|
|
45
|
+
function trim1(n: number): string {
|
|
46
|
+
const s = n.toFixed(1);
|
|
47
|
+
return s.endsWith(".0") ? s.slice(0, -2) : s;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Format a byte count to a human-readable string.
|
|
52
|
+
* Examples: "512B", "1.5KB", "2.3MB", "1.2GB"
|
|
53
|
+
*/
|
|
54
|
+
export function formatBytes(bytes: number): string {
|
|
55
|
+
if (bytes < 1024) return `${bytes}B`;
|
|
56
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
|
|
57
|
+
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
|
|
58
|
+
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)}GB`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Truncate a string to maxLen characters, appending an ellipsis if truncated.
|
|
63
|
+
* For display-width-aware truncation (terminals), use truncateToWidth from @prometheus-ai/tui.
|
|
64
|
+
*/
|
|
65
|
+
export function truncate(str: string, maxLen: number, ellipsis = "…"): string {
|
|
66
|
+
if (str.length <= maxLen) return str;
|
|
67
|
+
const sliceLen = Math.max(0, maxLen - ellipsis.length);
|
|
68
|
+
return `${str.slice(0, sliceLen)}${ellipsis}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Format count with pluralized label (e.g., "3 files", "1 error").
|
|
73
|
+
*/
|
|
74
|
+
export function formatCount(label: string, count: number): string {
|
|
75
|
+
const safeCount = Number.isFinite(count) ? count : 0;
|
|
76
|
+
return `${safeCount} ${pluralize(label, safeCount)}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Format age from seconds to human-readable string.
|
|
81
|
+
*/
|
|
82
|
+
export function formatAge(ageSeconds: number | null | undefined): string {
|
|
83
|
+
if (!ageSeconds) return "";
|
|
84
|
+
const mins = Math.floor(ageSeconds / 60);
|
|
85
|
+
const hours = Math.floor(mins / 60);
|
|
86
|
+
const days = Math.floor(hours / 24);
|
|
87
|
+
const weeks = Math.floor(days / 7);
|
|
88
|
+
const months = Math.floor(days / 30);
|
|
89
|
+
|
|
90
|
+
if (months > 0) return `${months}mo ago`;
|
|
91
|
+
if (weeks > 0) return `${weeks}w ago`;
|
|
92
|
+
if (days > 0) return `${days}d ago`;
|
|
93
|
+
if (hours > 0) return `${hours}h ago`;
|
|
94
|
+
if (mins > 0) return `${mins}m ago`;
|
|
95
|
+
return "just now";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Pluralize a label based on the count.
|
|
100
|
+
*/
|
|
101
|
+
export function pluralize(label: string, count: number): string {
|
|
102
|
+
if (count === 1) return label;
|
|
103
|
+
if (/(?:ch|sh|s|x|z)$/i.test(label)) return `${label}es`;
|
|
104
|
+
if (/[^aeiou]y$/i.test(label)) return `${label.slice(0, -1)}ies`;
|
|
105
|
+
return `${label}s`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Format a ratio as a percentage.
|
|
110
|
+
*/
|
|
111
|
+
export function formatPercent(ratio: number): string {
|
|
112
|
+
return `${(ratio * 100).toFixed(1)}%`;
|
|
113
|
+
}
|