@baukit/api-runtime 0.30.6 → 0.32.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/README.md +56 -9
- package/dist/backoff.d.ts +1 -1
- package/dist/backoff.d.ts.map +1 -1
- package/dist/backoff.js +6 -3
- package/dist/backoff.js.map +1 -1
- package/dist/cooldown.d.ts +31 -0
- package/dist/cooldown.d.ts.map +1 -0
- package/dist/cooldown.js +92 -0
- package/dist/cooldown.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -1
- package/dist/fetch.d.ts +8 -0
- package/dist/fetch.d.ts.map +1 -1
- package/dist/fetch.js +112 -17
- package/dist/fetch.js.map +1 -1
- package/dist/index.d.ts +18 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/retry.d.ts +15 -2
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +36 -4
- package/dist/retry.js.map +1 -1
- package/dist/timeout.d.ts +13 -0
- package/dist/timeout.d.ts.map +1 -0
- package/dist/timeout.js +34 -0
- package/dist/timeout.js.map +1 -0
- package/package.json +4 -2
- package/src/backoff.ts +6 -3
- package/src/cooldown.test.ts +62 -0
- package/src/cooldown.ts +123 -0
- package/src/errors.ts +20 -0
- package/src/fetch.ts +140 -16
- package/src/index.ts +19 -0
- package/src/origin.property.test.ts +122 -0
- package/src/origin.test.ts +87 -0
- package/src/resilience-vectors.test.ts +128 -0
- package/src/resilience.test.ts +344 -0
- package/src/retry.ts +48 -4
- package/src/timeout.ts +39 -0
package/README.md
CHANGED
|
@@ -114,20 +114,53 @@ The backend `message` is public, safe fallback text. Localized clients should re
|
|
|
114
114
|
|
|
115
115
|
Retries use exponential backoff with full jitter, capped by `maxDelayMs`. The same request ID is retained across attempts.
|
|
116
116
|
|
|
117
|
-
| Request/result
|
|
118
|
-
|
|
|
119
|
-
| `GET`, `HEAD` + network error
|
|
120
|
-
| `GET`, `HEAD` + 502/503/504
|
|
121
|
-
| `
|
|
122
|
-
|
|
|
123
|
-
|
|
|
124
|
-
|
|
|
125
|
-
|
|
|
117
|
+
| Request/result | Default | Configurable |
|
|
118
|
+
| ------------------------------------------ | --------------------------- | -------------------------------------------- |
|
|
119
|
+
| `GET`, `HEAD` + network error | Retry | `maxRetries`, delays, or disable |
|
|
120
|
+
| `GET`, `HEAD` + 502/503/504 | Retry | `maxRetries`, delays, or disable |
|
|
121
|
+
| `GET`, `HEAD` + 429/503 with `Retry-After` | Retry after the given delay | `maxRetryAfterMs` (default 30 s) |
|
|
122
|
+
| `GET`, `HEAD` + timeout | Retry | `requestTimeoutMs` |
|
|
123
|
+
| `OPTIONS`, `PUT`, `DELETE` | No retry | May be explicitly opted in through `methods` |
|
|
124
|
+
| Keyed `POST`, `PATCH` | No retry | May be explicitly opted in through `methods` |
|
|
125
|
+
| Unkeyed `POST`, `PATCH` | Never | No |
|
|
126
|
+
| 4xx other than 429 with `Retry-After` | Never | No |
|
|
127
|
+
| Abort | Stop immediately | No |
|
|
126
128
|
|
|
127
129
|
Defaults are two retries, a 100 ms initial ceiling, and a 2,000 ms maximum ceiling. Use `retry: false` to disable retries. A `POST` or `PATCH` counts as keyed when the request carries the `Idempotency-Key` header (`IDEMPOTENCY_KEY_HEADER`). Every attempt resends the same headers and body, so the server sees one key.
|
|
128
130
|
|
|
129
131
|
The delay formula is public as `fullJitterBackoffMs(retryIndex, { baseDelayMs, maxDelayMs, random })` in `@baukit/api-runtime/backoff`. It returns a delay between 0 and `min(maxDelayMs, baseDelayMs * 2 ** retryIndex)` and clamps the random sample to `[0, 1]`. That entry imports nothing, so a sync loop or job runner can share the policy without loading the fetch runtime. `@baukit/sync-client`'s scheduler takes it as its retry delay.
|
|
130
132
|
|
|
133
|
+
## Timeouts and cooldown
|
|
134
|
+
|
|
135
|
+
Each attempt gets `requestTimeoutMs` (default 30,000 ms, `false` disables it). The timer is linked to
|
|
136
|
+
the caller's `AbortSignal`: a timeout rejects with a `NetworkError` whose `aborted` is `false` and whose
|
|
137
|
+
message is `API request timed out`, so it is retried like any network error.
|
|
138
|
+
|
|
139
|
+
The runtime keeps a cooldown per origin. A `429` or `503` with `Retry-After` blocks the origin for that
|
|
140
|
+
delay (clamped to `maxRetryAfterMs`). Five consecutive network failures or timeouts block it for
|
|
141
|
+
30,000 ms minus up to 20% random jitter. Each further run of failures without a response in between
|
|
142
|
+
doubles that time, capped at `maxCooldownMs` (300,000 ms), which also caps `Retry-After` holds. Any
|
|
143
|
+
HTTP response resets the failure count and the doubling, and caller aborts do not count. The open time
|
|
144
|
+
and the backoff match the shared vectors in `fixtures/resilience`. While an
|
|
145
|
+
origin is blocked, every call rejects with `ApiUnavailableError` and its `retryAt` (epoch milliseconds)
|
|
146
|
+
without sending a request. A UI can show "unavailable, retrying at HH:MM" from it; use
|
|
147
|
+
`isApiUnavailableError` to narrow.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
createApiFetch({
|
|
151
|
+
...environment,
|
|
152
|
+
cooldown: {
|
|
153
|
+
networkFailureThreshold: 5,
|
|
154
|
+
networkFailureCooldownMs: 30_000,
|
|
155
|
+
maxCooldownMs: 300_000,
|
|
156
|
+
jitterPercent: 20,
|
|
157
|
+
},
|
|
158
|
+
now: Date.now,
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Pass `cooldown: false` to turn it off. `now` is the clock for cooldowns and `Retry-After` dates.
|
|
163
|
+
|
|
131
164
|
## Idempotent mutations
|
|
132
165
|
|
|
133
166
|
`@baukit/api-runtime/idempotency` keeps one `Idempotency-Key` per account, operation, and body until the outcome is definite or the key expires. The server half is `baukit-http`'s `IdempotencyKeyRule`, and the protocol is [replay-safe mutations](../../../docs/platform/replay-safe-mutations.md).
|
|
@@ -248,3 +281,17 @@ The auth templates capture a short-lived access token for this operation before
|
|
|
248
281
|
clearing local authentication. Status checks use that token while the screen
|
|
249
282
|
stays open, without refreshing or restoring a signed-out session. A failed
|
|
250
283
|
status check does not undo deletion; the server worker continues.
|
|
284
|
+
|
|
285
|
+
## Base origin and path
|
|
286
|
+
|
|
287
|
+
String inputs resolve against `baseUrl`. A path with one leading `/` stays under
|
|
288
|
+
the base path: with `https://api.example.test/v1`, `/items` becomes
|
|
289
|
+
`https://api.example.test/v1/items`. `//host` and `/\host` are other origins.
|
|
290
|
+
The runtime adds `Authorization` and runs `onUnauthorized`, the 401 replay and
|
|
291
|
+
`onProfileErased` only for requests whose origin equals the base origin and
|
|
292
|
+
whose path is at or under the base path. Other URLs, `Request` inputs included,
|
|
293
|
+
go out without the token and their 401s fire no hook.
|
|
294
|
+
|
|
295
|
+
Pass `sessionSubject` to stamp each request with the signed-in subject at send
|
|
296
|
+
time. A 401 whose subject changed before it arrived fires no hook, and the hook
|
|
297
|
+
context carries the stamped `subject`.
|
package/dist/backoff.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface BackoffPolicy {
|
|
|
8
8
|
readonly random?: () => number;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
|
-
* Returns
|
|
11
|
+
* Returns `floor(min(maxDelayMs, baseDelayMs * 2 ** retryIndex) * random)` in whole milliseconds.
|
|
12
12
|
*
|
|
13
13
|
* Full jitter spreads clients that failed together, so they do not retry together.
|
|
14
14
|
*/
|
package/dist/backoff.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../src/backoff.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,MAAM,WAAW,aAAa;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,yCAAyC;IACzC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,wFAAwF;IACxF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;CAChC;
|
|
1
|
+
{"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../src/backoff.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,MAAM,WAAW,aAAa;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,yCAAyC;IACzC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,wFAAwF;IACxF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;CAChC;AAMD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAG,MAAM,CAKrF"}
|
package/dist/backoff.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
const EXPONENT_BASE = 2;
|
|
2
|
+
/** Products saturate here, as in `fixtures/resilience/backoff-v1.json`. */
|
|
3
|
+
const SATURATION_MS = 2 ** 32 - 1;
|
|
2
4
|
/**
|
|
3
|
-
* Returns
|
|
5
|
+
* Returns `floor(min(maxDelayMs, baseDelayMs * 2 ** retryIndex) * random)` in whole milliseconds.
|
|
4
6
|
*
|
|
5
7
|
* Full jitter spreads clients that failed together, so they do not retry together.
|
|
6
8
|
*/
|
|
7
9
|
export function fullJitterBackoffMs(retryIndex, policy) {
|
|
8
|
-
const
|
|
10
|
+
const product = Math.min(SATURATION_MS, policy.baseDelayMs * EXPONENT_BASE ** retryIndex);
|
|
11
|
+
const ceiling = Math.min(policy.maxDelayMs, product);
|
|
9
12
|
const sample = (policy.random ?? Math.random)();
|
|
10
|
-
return ceiling * Math.max(0, Math.min(1, sample));
|
|
13
|
+
return Math.floor(ceiling * Math.max(0, Math.min(1, sample)));
|
|
11
14
|
}
|
|
12
15
|
//# sourceMappingURL=backoff.js.map
|
package/dist/backoff.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backoff.js","sourceRoot":"","sources":["../src/backoff.ts"],"names":[],"mappings":"AAUA,MAAM,aAAa,GAAG,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"backoff.js","sourceRoot":"","sources":["../src/backoff.ts"],"names":[],"mappings":"AAUA,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,2EAA2E;AAC3E,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AAElC;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,UAAkB,EAAE,MAAqB;IAC3E,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,WAAW,GAAG,aAAa,IAAI,UAAU,CAAC,CAAC;IAC1F,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACrD,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;IAChD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Per-origin cooldown options. */
|
|
2
|
+
export interface CooldownOptions {
|
|
3
|
+
/** Consecutive network failures (timeouts included) that start a cooldown. Defaults to 5. */
|
|
4
|
+
readonly networkFailureThreshold?: number;
|
|
5
|
+
/** First cooldown started by the network failure threshold, in milliseconds. Defaults to 30,000. */
|
|
6
|
+
readonly networkFailureCooldownMs?: number;
|
|
7
|
+
/** Cap for every cooldown, including doubled ones and `Retry-After` holds. Defaults to 300,000. */
|
|
8
|
+
readonly maxCooldownMs?: number;
|
|
9
|
+
/** Share in percent taken off a network failure cooldown at random. Defaults to 20. */
|
|
10
|
+
readonly jitterPercent?: number;
|
|
11
|
+
/** Random source in `[0, 1]` for the jitter. Values outside are clamped. Defaults to `Math.random`. */
|
|
12
|
+
readonly random?: () => number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Tracks when each origin may be called again (plan D9).
|
|
16
|
+
*
|
|
17
|
+
* A `429`/`503` with `Retry-After`, or a run of network failures, blocks the origin until `retryAt`.
|
|
18
|
+
* Each run of network failures without a response in between doubles the cooldown up to
|
|
19
|
+
* `maxCooldownMs`, minus jitter, matching the open time in `fixtures/resilience/breaker-v1.json`.
|
|
20
|
+
*/
|
|
21
|
+
export declare class OriginCooldown {
|
|
22
|
+
#private;
|
|
23
|
+
constructor(options: CooldownOptions, now: () => number);
|
|
24
|
+
/** Returns the time the origin unblocks, or null when calls may proceed. */
|
|
25
|
+
blockedUntil(origin: string): number | null;
|
|
26
|
+
/** Blocks the origin for `delayMs` from now, capped, never shortening an existing cooldown. */
|
|
27
|
+
coolDown(origin: string, delayMs: number): void;
|
|
28
|
+
recordResponse(origin: string): void;
|
|
29
|
+
recordNetworkFailure(origin: string): void;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=cooldown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cooldown.d.ts","sourceRoot":"","sources":["../src/cooldown.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,MAAM,WAAW,eAAe;IAC9B,6FAA6F;IAC7F,QAAQ,CAAC,uBAAuB,CAAC,EAAE,MAAM,CAAC;IAC1C,oGAAoG;IACpG,QAAQ,CAAC,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAC3C,mGAAmG;IACnG,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,uFAAuF;IACvF,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,uGAAuG;IACvG,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;CAChC;AAqBD;;;;;;GAMG;AACH,qBAAa,cAAc;;gBASb,OAAO,EAAE,eAAe,EAAE,GAAG,EAAE,MAAM,MAAM;IAmBvD,4EAA4E;IAC5E,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAK3C,+FAA+F;IAC/F,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAM/C,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAYpC,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CA6B3C"}
|
package/dist/cooldown.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const DEFAULT_NETWORK_FAILURE_THRESHOLD = 5;
|
|
2
|
+
const DEFAULT_NETWORK_FAILURE_COOLDOWN_MS = 30_000;
|
|
3
|
+
const DEFAULT_MAX_COOLDOWN_MS = 300_000;
|
|
4
|
+
const DEFAULT_JITTER_PERCENT = 20;
|
|
5
|
+
const PERCENT = 100;
|
|
6
|
+
const EXPONENT_BASE = 2;
|
|
7
|
+
function assertNonNegative(value, name) {
|
|
8
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
9
|
+
throw new Error(`${name} must be a finite non-negative number`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Tracks when each origin may be called again (plan D9).
|
|
14
|
+
*
|
|
15
|
+
* A `429`/`503` with `Retry-After`, or a run of network failures, blocks the origin until `retryAt`.
|
|
16
|
+
* Each run of network failures without a response in between doubles the cooldown up to
|
|
17
|
+
* `maxCooldownMs`, minus jitter, matching the open time in `fixtures/resilience/breaker-v1.json`.
|
|
18
|
+
*/
|
|
19
|
+
export class OriginCooldown {
|
|
20
|
+
#threshold;
|
|
21
|
+
#networkCooldownMs;
|
|
22
|
+
#maxCooldownMs;
|
|
23
|
+
#jitterRatio;
|
|
24
|
+
#random;
|
|
25
|
+
#now;
|
|
26
|
+
#origins = new Map();
|
|
27
|
+
constructor(options, now) {
|
|
28
|
+
this.#threshold = options.networkFailureThreshold ?? DEFAULT_NETWORK_FAILURE_THRESHOLD;
|
|
29
|
+
this.#networkCooldownMs =
|
|
30
|
+
options.networkFailureCooldownMs ?? DEFAULT_NETWORK_FAILURE_COOLDOWN_MS;
|
|
31
|
+
this.#maxCooldownMs = options.maxCooldownMs ?? DEFAULT_MAX_COOLDOWN_MS;
|
|
32
|
+
const jitterPercent = options.jitterPercent ?? DEFAULT_JITTER_PERCENT;
|
|
33
|
+
if (!Number.isInteger(this.#threshold) || this.#threshold < 1) {
|
|
34
|
+
throw new Error('networkFailureThreshold must be a positive integer');
|
|
35
|
+
}
|
|
36
|
+
assertNonNegative(this.#networkCooldownMs, 'networkFailureCooldownMs');
|
|
37
|
+
assertNonNegative(this.#maxCooldownMs, 'maxCooldownMs');
|
|
38
|
+
if (!Number.isFinite(jitterPercent) || jitterPercent < 0 || jitterPercent > PERCENT) {
|
|
39
|
+
throw new Error('jitterPercent must be between 0 and 100');
|
|
40
|
+
}
|
|
41
|
+
this.#jitterRatio = jitterPercent / PERCENT;
|
|
42
|
+
this.#random = options.random ?? Math.random;
|
|
43
|
+
this.#now = now;
|
|
44
|
+
}
|
|
45
|
+
/** Returns the time the origin unblocks, or null when calls may proceed. */
|
|
46
|
+
blockedUntil(origin) {
|
|
47
|
+
const retryAt = this.#origins.get(origin)?.retryAt ?? 0;
|
|
48
|
+
return this.#now() < retryAt ? retryAt : null;
|
|
49
|
+
}
|
|
50
|
+
/** Blocks the origin for `delayMs` from now, capped, never shortening an existing cooldown. */
|
|
51
|
+
coolDown(origin, delayMs) {
|
|
52
|
+
const state = this.#state(origin);
|
|
53
|
+
const held = Math.min(delayMs, this.#maxCooldownMs);
|
|
54
|
+
state.retryAt = Math.max(state.retryAt, this.#now() + held);
|
|
55
|
+
}
|
|
56
|
+
recordResponse(origin) {
|
|
57
|
+
const state = this.#origins.get(origin);
|
|
58
|
+
if (state === undefined) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
state.consecutiveNetworkFailures = 0;
|
|
62
|
+
state.networkOpenings = 0;
|
|
63
|
+
if (this.#now() >= state.retryAt) {
|
|
64
|
+
this.#origins.delete(origin);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
recordNetworkFailure(origin) {
|
|
68
|
+
const state = this.#state(origin);
|
|
69
|
+
state.consecutiveNetworkFailures += 1;
|
|
70
|
+
if (state.consecutiveNetworkFailures < this.#threshold) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
state.consecutiveNetworkFailures = 0;
|
|
74
|
+
state.networkOpenings += 1;
|
|
75
|
+
this.coolDown(origin, this.#networkCooldownFor(state.networkOpenings));
|
|
76
|
+
}
|
|
77
|
+
#networkCooldownFor(opening) {
|
|
78
|
+
const open = Math.min(this.#maxCooldownMs, this.#networkCooldownMs * EXPONENT_BASE ** (opening - 1));
|
|
79
|
+
const fraction = Math.max(0, Math.min(1, this.#random()));
|
|
80
|
+
return open - Math.floor(open * (this.#jitterRatio * fraction));
|
|
81
|
+
}
|
|
82
|
+
#state(origin) {
|
|
83
|
+
const existing = this.#origins.get(origin);
|
|
84
|
+
if (existing !== undefined) {
|
|
85
|
+
return existing;
|
|
86
|
+
}
|
|
87
|
+
const created = { consecutiveNetworkFailures: 0, networkOpenings: 0, retryAt: 0 };
|
|
88
|
+
this.#origins.set(origin, created);
|
|
89
|
+
return created;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=cooldown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cooldown.js","sourceRoot":"","sources":["../src/cooldown.ts"],"names":[],"mappings":"AAcA,MAAM,iCAAiC,GAAG,CAAC,CAAC;AAC5C,MAAM,mCAAmC,GAAG,MAAM,CAAC;AACnD,MAAM,uBAAuB,GAAG,OAAO,CAAC;AACxC,MAAM,sBAAsB,GAAG,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,GAAG,CAAC;AACpB,MAAM,aAAa,GAAG,CAAC,CAAC;AAQxB,SAAS,iBAAiB,CAAC,KAAa,EAAE,IA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
@@ -27,6 +27,14 @@ export declare class NetworkError extends Error {
|
|
|
27
27
|
readonly aborted: boolean;
|
|
28
28
|
constructor(message: string, requestId: string, cause: unknown, aborted?: boolean);
|
|
29
29
|
}
|
|
30
|
+
/** The origin is cooling down after a `Retry-After` or repeated network failures; no request was sent. */
|
|
31
|
+
export declare class ApiUnavailableError extends Error {
|
|
32
|
+
readonly kind: "unavailable";
|
|
33
|
+
readonly requestId: string;
|
|
34
|
+
/** Epoch milliseconds after which the origin may be called again. */
|
|
35
|
+
readonly retryAt: number;
|
|
36
|
+
constructor(requestId: string, retryAt: number);
|
|
37
|
+
}
|
|
30
38
|
/** A non-success HTTP response that did not contain a valid Baukit error envelope. */
|
|
31
39
|
export declare class HttpError extends Error {
|
|
32
40
|
readonly kind: "http";
|
|
@@ -40,6 +48,8 @@ export declare function isApiError(value: unknown): value is ApiError;
|
|
|
40
48
|
export declare function isApiError<Code extends string>(value: unknown, code: Code): value is ApiError<Code>;
|
|
41
49
|
/** Narrows an unknown value to a NetworkError. */
|
|
42
50
|
export declare function isNetworkError(value: unknown): value is NetworkError;
|
|
51
|
+
/** Narrows an unknown value to an ApiUnavailableError. */
|
|
52
|
+
export declare function isApiUnavailableError(value: unknown): value is ApiUnavailableError;
|
|
43
53
|
/** Narrows an unknown value to an HttpError. */
|
|
44
54
|
export declare function isHttpError(value: unknown): value is HttpError;
|
|
45
55
|
/** Parses the exact Baukit error envelope shape, returning null for any other value. */
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAIA,oDAAoD;AACpD,MAAM,MAAM,SAAS,GACnB,IAAI,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,SAAS,EAAE,GAAG;IAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAElG,yCAAyC;AACzC,MAAM,WA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AQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;gBAEX,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO;CAQ9C;AAED,kFAAkF;AAClF,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAAC;AAC9D,wBAAgB,UAAU,CAAC,IAAI,SAAS,MAAM,EAC5C,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,IAAI,GACT,KAAK,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC;AAK3B,kDAAkD;AAClD,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED,0DAA0D;AAC1D,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,mBAAmB,CAElF;AAED,gDAAgD;AAChD,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D;AAED,wFAAwF;AACxF,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CA0BrF;AAED,0EAA0E;AAC1E,wBAAsB,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAgB9F"}
|
package/dist/errors.js
CHANGED
|
@@ -28,6 +28,19 @@ export class NetworkError extends Error {
|
|
|
28
28
|
this.aborted = aborted;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
/** The origin is cooling down after a `Retry-After` or repeated network failures; no request was sent. */
|
|
32
|
+
export class ApiUnavailableError extends Error {
|
|
33
|
+
kind = 'unavailable';
|
|
34
|
+
requestId;
|
|
35
|
+
/** Epoch milliseconds after which the origin may be called again. */
|
|
36
|
+
retryAt;
|
|
37
|
+
constructor(requestId, retryAt) {
|
|
38
|
+
super('API is temporarily unavailable');
|
|
39
|
+
this.name = 'ApiUnavailableError';
|
|
40
|
+
this.requestId = requestId;
|
|
41
|
+
this.retryAt = retryAt;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
31
44
|
/** A non-success HTTP response that did not contain a valid Baukit error envelope. */
|
|
32
45
|
export class HttpError extends Error {
|
|
33
46
|
kind = 'http';
|
|
@@ -50,6 +63,10 @@ export function isApiError(value, code) {
|
|
|
50
63
|
export function isNetworkError(value) {
|
|
51
64
|
return value instanceof NetworkError;
|
|
52
65
|
}
|
|
66
|
+
/** Narrows an unknown value to an ApiUnavailableError. */
|
|
67
|
+
export function isApiUnavailableError(value) {
|
|
68
|
+
return value instanceof ApiUnavailableError;
|
|
69
|
+
}
|
|
53
70
|
/** Narrows an unknown value to an HttpError. */
|
|
54
71
|
export function isHttpError(value) {
|
|
55
72
|
return value instanceof HttpError;
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAgBjD,0EAA0E;AAC1E,MAAM,OAAO,QAAuC,SAAQ,KAAK;IACtD,IAAI,GAAG,KAAc,CAAC;IACtB,IAAI,CAAO;IACX,SAAS,CAAS;IAClB,OAAO,CAAsC;IAC7C,MAAM,CAAS;IAExB,YAAY,QAAgC,EAAE,MAAc;QAC1D,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC9B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC;QAChC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC;QAC1C,IAAI,CAAC,OAAO,GA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C,QAAQ,EAAE,IAAI,CAAC,CAAC;AACvF,CAAC;AAED,SAAS,YAAY,CAAC,KAAc;IAClC,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACpE,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,IACE,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,SAAS;QAC1B,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EACrD,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC;AAC7B,CAAC"}
|
package/dist/fetch.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OriginCooldown } from './cooldown.js';
|
|
1
2
|
import type { ApiRuntimeOptions, FetchImplementation } from './index.js';
|
|
2
3
|
import { type NormalizedRetryOptions } from './retry.js';
|
|
3
4
|
export interface FetchSettings {
|
|
@@ -6,8 +7,15 @@ export interface FetchSettings {
|
|
|
6
7
|
readonly transport: FetchImplementation;
|
|
7
8
|
readonly requestIdFactory: () => string;
|
|
8
9
|
readonly retry: NormalizedRetryOptions;
|
|
10
|
+
readonly requestTimeoutMs: number | null;
|
|
11
|
+
readonly cooldown: OriginCooldown | null;
|
|
12
|
+
readonly now: () => number;
|
|
9
13
|
readonly options: ApiRuntimeOptions;
|
|
10
14
|
}
|
|
11
15
|
export declare function fetchSettings(options: ApiRuntimeOptions, baseUrl: string): FetchSettings;
|
|
12
16
|
export declare function runFetch(settings: FetchSettings, input: RequestInfo | URL, init: RequestInit | undefined): Promise<Response>;
|
|
17
|
+
/** Paths starting with a single `/` stay under the base path; `//host` and `/\host` do not. */
|
|
18
|
+
export declare function resolveApiUrl(input: string | URL, baseUrl: string): URL;
|
|
19
|
+
/** True when `url` has the base origin and sits at or under the base path. */
|
|
20
|
+
export declare function isApiRequest(url: string, baseUrl: string): boolean;
|
|
13
21
|
//# sourceMappingURL=fetch.d.ts.map
|
package/dist/fetch.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAU/C,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EAIpB,MAAM,YAAY,CAAC;AACpB,OAAO,EAUL,KAAK,sBAAsB,EAC5B,MAAM,YAAY,CAAC;AAQpB,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,kBAAkB,EAAE,OAAO,OAAO,CAAC;IAC5C,QAAQ,CAAC,SAAS,EAAE,mBAAmB,CAAC;IACxC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,MAAM,CAAC;IACxC,QAAQ,CAAC,KAAK,EAAE,sBAAsB,CAAC;IACvC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,GAAG,EAAE,MAAM,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAAC;CACrC;AAiBD,wBAAgB,aAAa,CAAC,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,GAAG,aAAa,CAaxF;AAED,wBAAsB,QAAQ,CAC5B,QAAQ,EAAE,aAAa,EACvB,KAAK,EAAE,WAAW,GAAG,GAAG,EACxB,IAAI,EAAE,WAAW,GAAG,SAAS,GAC5B,OAAO,CAAC,QAAQ,CAAC,CAuBnB;AAsSD,+FAA+F;AAC/F,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,GAAG,CAIvE;AAED,8EAA8E;AAC9E,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAalE"}
|
package/dist/fetch.js
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OriginCooldown } from './cooldown.js';
|
|
2
|
+
import { ApiError, ApiUnavailableError, HttpError, isApiError, NetworkError, normalizeResponseError, } from './errors.js';
|
|
2
3
|
import { REQUEST_ID_HEADER } from './headers.js';
|
|
3
|
-
import { isAbortError, networkError, normalizeRetryOptions, retryDelay, shouldRetryMethod, shouldRetryResponse, throwIfAborted, } from './retry.js';
|
|
4
|
+
import { isAbortError, networkError, nextRetryDelayMs, normalizeRetryOptions, retryAfterDelayMs, retryDelay, shouldRetryMethod, shouldRetryResponse, throwIfAborted, } from './retry.js';
|
|
5
|
+
import { attemptSignal } from './timeout.js';
|
|
4
6
|
const UNAUTHORIZED_STATUS = 401;
|
|
7
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
5
8
|
const RETRY = Symbol('retry');
|
|
6
9
|
export function fetchSettings(options, baseUrl) {
|
|
10
|
+
const now = options.now ?? Date.now;
|
|
7
11
|
return {
|
|
8
12
|
baseUrl,
|
|
9
13
|
requestConstructor: options.requestConstructor ?? globalThis.Request,
|
|
10
14
|
transport: options.fetch ?? globalThis.fetch.bind(globalThis),
|
|
11
15
|
requestIdFactory: options.requestIdFactory ?? createUuid,
|
|
12
16
|
retry: normalizeRetryOptions(options.retry),
|
|
17
|
+
requestTimeoutMs: normalizeRequestTimeout(options.requestTimeoutMs),
|
|
18
|
+
cooldown: options.cooldown === false ? null : new OriginCooldown(options.cooldown ?? {}, now),
|
|
19
|
+
now,
|
|
13
20
|
options,
|
|
14
21
|
};
|
|
15
22
|
}
|
|
16
23
|
export async function runFetch(settings, input, init) {
|
|
17
|
-
const context = {
|
|
24
|
+
const context = {
|
|
25
|
+
...settings,
|
|
26
|
+
requestId: settings.requestIdFactory(),
|
|
27
|
+
sessionSubject: settings.options.sessionSubject?.(),
|
|
28
|
+
};
|
|
18
29
|
const request = await prepareRequest(context, input, init);
|
|
19
30
|
const state = {
|
|
20
31
|
request,
|
|
@@ -22,9 +33,11 @@ export async function runFetch(settings, input, init) {
|
|
|
22
33
|
useOriginalRequest: true,
|
|
23
34
|
retries: 0,
|
|
24
35
|
unauthorizedReplayUsed: false,
|
|
36
|
+
timedOut: false,
|
|
25
37
|
};
|
|
26
38
|
for (;;) {
|
|
27
39
|
throwIfAborted(state.request.signal, context.requestId);
|
|
40
|
+
throwIfCoolingDown(context, state);
|
|
28
41
|
const outcome = await runAttempt(context, state);
|
|
29
42
|
if (outcome !== RETRY) {
|
|
30
43
|
return outcome;
|
|
@@ -52,27 +65,62 @@ async function runAttempt(context, state) {
|
|
|
52
65
|
return handleFailure(context, state, cause);
|
|
53
66
|
}
|
|
54
67
|
}
|
|
68
|
+
function normalizeRequestTimeout(timeoutMs) {
|
|
69
|
+
if (timeoutMs === false) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
const value = timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
73
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
74
|
+
throw new Error('requestTimeoutMs must be a finite positive number');
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
function originOf(state) {
|
|
79
|
+
return new URL(state.request.url).origin;
|
|
80
|
+
}
|
|
81
|
+
function throwIfCoolingDown(context, state) {
|
|
82
|
+
const retryAt = context.cooldown?.blockedUntil(originOf(state)) ?? null;
|
|
83
|
+
if (retryAt !== null) {
|
|
84
|
+
throw new ApiUnavailableError(context.requestId, retryAt);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
55
87
|
async function sendAttempt(context, state) {
|
|
56
88
|
const attempt = state.useOriginalRequest ? state.request : state.replaySource?.clone();
|
|
57
89
|
if (attempt === undefined) {
|
|
58
90
|
throw new TypeError('API request body cannot be replayed');
|
|
59
91
|
}
|
|
60
92
|
state.useOriginalRequest = false;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
93
|
+
state.timedOut = false;
|
|
94
|
+
const scope = attemptSignal(state.request.signal, context.requestTimeoutMs);
|
|
95
|
+
try {
|
|
96
|
+
const response = await context.transport(attempt, { signal: scope.signal });
|
|
97
|
+
throwIfAborted(state.request.signal, context.requestId);
|
|
98
|
+
context.cooldown?.recordResponse(originOf(state));
|
|
99
|
+
return response;
|
|
100
|
+
}
|
|
101
|
+
catch (cause) {
|
|
102
|
+
state.timedOut = scope.timedOut();
|
|
103
|
+
throw cause;
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
scope.dispose();
|
|
107
|
+
}
|
|
64
108
|
}
|
|
65
109
|
async function handleResponse(context, state, response) {
|
|
110
|
+
const retryAfterMs = retryAfterDelayMs(response, context.retry, context.now());
|
|
111
|
+
if (retryAfterMs !== null) {
|
|
112
|
+
context.cooldown?.coolDown(originOf(state), retryAfterMs);
|
|
113
|
+
}
|
|
66
114
|
if (state.replaySource !== undefined &&
|
|
67
|
-
shouldRetryResponse(state.request, response.status, state.retries, context.retry)) {
|
|
68
|
-
await backOff(context, state);
|
|
115
|
+
shouldRetryResponse(state.request, { status: response.status, retryAfterMs }, state.retries, context.retry)) {
|
|
116
|
+
await backOff(context, state, retryAfterMs);
|
|
69
117
|
return RETRY;
|
|
70
118
|
}
|
|
71
119
|
if (response.ok) {
|
|
72
120
|
return response;
|
|
73
121
|
}
|
|
74
122
|
const error = await normalizeResponseError(response);
|
|
75
|
-
if (response.status !== UNAUTHORIZED_STATUS) {
|
|
123
|
+
if (response.status !== UNAUTHORIZED_STATUS || !acceptsSessionSignals(context, state.request)) {
|
|
76
124
|
throw error;
|
|
77
125
|
}
|
|
78
126
|
return handleUnauthorized(context, state, { response, error });
|
|
@@ -84,6 +132,7 @@ async function handleUnauthorized(context, state, failure) {
|
|
|
84
132
|
...failure,
|
|
85
133
|
request: state.replaySource?.clone() ?? state.request,
|
|
86
134
|
canRetry,
|
|
135
|
+
...(context.sessionSubject === undefined ? {} : { subject: context.sessionSubject }),
|
|
87
136
|
});
|
|
88
137
|
if (isApiError(error, 'profile_erased')) {
|
|
89
138
|
await notifyObserver(() => options.onProfileErased?.(unauthorizedContext(false)));
|
|
@@ -147,21 +196,37 @@ async function replayWithFreshCredentials(context, state, replaySource, error) {
|
|
|
147
196
|
state.retries = 0;
|
|
148
197
|
}
|
|
149
198
|
async function handleFailure(context, state, cause) {
|
|
150
|
-
if (cause instanceof ApiError ||
|
|
199
|
+
if (cause instanceof ApiError ||
|
|
200
|
+
cause instanceof HttpError ||
|
|
201
|
+
cause instanceof ApiUnavailableError) {
|
|
151
202
|
throw cause;
|
|
152
203
|
}
|
|
153
|
-
const aborted = state.request.signal.aborted || isAbortError(cause);
|
|
204
|
+
const aborted = state.request.signal.aborted || (!state.timedOut && isAbortError(cause));
|
|
205
|
+
if (!aborted) {
|
|
206
|
+
context.cooldown?.recordNetworkFailure(originOf(state));
|
|
207
|
+
}
|
|
154
208
|
if (!aborted &&
|
|
155
209
|
state.replaySource !== undefined &&
|
|
156
210
|
shouldRetryMethod(state.request, context.retry) &&
|
|
157
211
|
state.retries < context.retry.maxRetries) {
|
|
158
|
-
await backOff(context, state);
|
|
212
|
+
await backOff(context, state, null);
|
|
159
213
|
return RETRY;
|
|
160
214
|
}
|
|
161
|
-
throw new NetworkError(aborted
|
|
215
|
+
throw new NetworkError(failureMessage(aborted, state.timedOut), context.requestId, cause, aborted);
|
|
162
216
|
}
|
|
163
|
-
|
|
164
|
-
|
|
217
|
+
function failureMessage(aborted, timedOut) {
|
|
218
|
+
if (aborted) {
|
|
219
|
+
return 'API request was aborted';
|
|
220
|
+
}
|
|
221
|
+
return timedOut ? 'API request timed out' : 'API network request failed';
|
|
222
|
+
}
|
|
223
|
+
async function backOff(context, state, retryAfterMs) {
|
|
224
|
+
const delayMs = nextRetryDelayMs(state.retries, context.retry, retryAfterMs);
|
|
225
|
+
const retryAt = context.cooldown?.blockedUntil(originOf(state)) ?? null;
|
|
226
|
+
if (retryAt !== null && retryAt > context.now() + delayMs) {
|
|
227
|
+
throw new ApiUnavailableError(context.requestId, retryAt);
|
|
228
|
+
}
|
|
229
|
+
await retryDelay(delayMs, context.retry, state.request.signal, context.requestId);
|
|
165
230
|
state.retries += 1;
|
|
166
231
|
}
|
|
167
232
|
async function decorateRequest(context, request) {
|
|
@@ -169,7 +234,7 @@ async function decorateRequest(context, request) {
|
|
|
169
234
|
throwIfAborted(request.signal, requestId);
|
|
170
235
|
const headers = new Headers(request.headers);
|
|
171
236
|
headers.set(REQUEST_ID_HEADER, requestId);
|
|
172
|
-
if (options.tokenProvider !== undefined) {
|
|
237
|
+
if (options.tokenProvider !== undefined && isApiRequest(request.url, context.baseUrl)) {
|
|
173
238
|
const token = await options.tokenProvider();
|
|
174
239
|
throwIfAborted(request.signal, requestId);
|
|
175
240
|
headers.delete('authorization');
|
|
@@ -196,10 +261,40 @@ function cloneRequest(request) {
|
|
|
196
261
|
}
|
|
197
262
|
function createRequest(input, init, baseUrl, requestConstructor) {
|
|
198
263
|
if (typeof input === 'string' || input instanceof URL) {
|
|
199
|
-
return new requestConstructor(
|
|
264
|
+
return new requestConstructor(resolveApiUrl(input, baseUrl), init);
|
|
200
265
|
}
|
|
201
266
|
return new requestConstructor(input, init);
|
|
202
267
|
}
|
|
268
|
+
const BASE_RELATIVE_PATH = /^\/(?![/\\])/u;
|
|
269
|
+
/** Paths starting with a single `/` stay under the base path; `//host` and `/\host` do not. */
|
|
270
|
+
export function resolveApiUrl(input, baseUrl) {
|
|
271
|
+
if (input instanceof URL)
|
|
272
|
+
return input;
|
|
273
|
+
if (BASE_RELATIVE_PATH.test(input))
|
|
274
|
+
return new URL(`${baseUrl}${input}`);
|
|
275
|
+
return new URL(input, `${baseUrl}/`);
|
|
276
|
+
}
|
|
277
|
+
/** True when `url` has the base origin and sits at or under the base path. */
|
|
278
|
+
export function isApiRequest(url, baseUrl) {
|
|
279
|
+
let target;
|
|
280
|
+
try {
|
|
281
|
+
target = new URL(url);
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
const base = new URL(baseUrl);
|
|
287
|
+
if (target.origin !== base.origin)
|
|
288
|
+
return false;
|
|
289
|
+
const basePath = base.pathname.replace(/\/$/u, '');
|
|
290
|
+
return (basePath === '' || target.pathname === basePath || target.pathname.startsWith(`${basePath}/`));
|
|
291
|
+
}
|
|
292
|
+
function acceptsSessionSignals(context, request) {
|
|
293
|
+
if (!isApiRequest(request.url, context.baseUrl))
|
|
294
|
+
return false;
|
|
295
|
+
const currentSubject = context.options.sessionSubject;
|
|
296
|
+
return currentSubject === undefined || currentSubject() === context.sessionSubject;
|
|
297
|
+
}
|
|
203
298
|
function createUuid() {
|
|
204
299
|
// Runtime probe: React Native and older runtimes may lack `crypto` despite the DOM lib typing.
|
|
205
300
|
const runtime = globalThis;
|