@bedrock-rbx/ocale 0.1.2 → 0.1.4
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/badges.d.mts +21 -6
- package/dist/badges.d.mts.map +1 -1
- package/dist/badges.mjs +10 -11
- package/dist/badges.mjs.map +1 -1
- package/dist/developer-products.d.mts +22 -11
- package/dist/developer-products.d.mts.map +1 -1
- package/dist/developer-products.mjs +21 -20
- package/dist/developer-products.mjs.map +1 -1
- package/dist/game-passes.d.mts +5 -5
- package/dist/game-passes.mjs +18 -20
- package/dist/game-passes.mjs.map +1 -1
- package/dist/index.d.mts +9 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -4
- package/dist/luau-execution.d.mts +5 -3
- package/dist/luau-execution.d.mts.map +1 -1
- package/dist/luau-execution.mjs +22 -18
- package/dist/luau-execution.mjs.map +1 -1
- package/dist/places.d.mts +2 -2
- package/dist/places.d.mts.map +1 -1
- package/dist/places.mjs +31 -31
- package/dist/places.mjs.map +1 -1
- package/dist/{poll-timeout-BfxUWWCZ.mjs → poll-timeout-DXfJVZzR.mjs} +2 -2
- package/dist/{poll-timeout-BfxUWWCZ.mjs.map → poll-timeout-DXfJVZzR.mjs.map} +1 -1
- package/dist/{polling-rj0dGREg.d.mts → polling-CG12rHaz.d.mts} +29 -11
- package/dist/polling-CG12rHaz.d.mts.map +1 -0
- package/dist/{polling-helpers-P17IyI3R.mjs → polling-helpers-SBN188o5.mjs} +47 -53
- package/dist/polling-helpers-SBN188o5.mjs.map +1 -0
- package/dist/{price-information-CJoPbH8p.mjs → price-information-DziMfTQ6.mjs} +2 -2
- package/dist/{price-information-CJoPbH8p.mjs.map → price-information-DziMfTQ6.mjs.map} +1 -1
- package/dist/{rate-limit-Dh2leqaB.mjs → rate-limit-B20REZEY.mjs} +6 -4
- package/dist/{rate-limit-Dh2leqaB.mjs.map → rate-limit-B20REZEY.mjs.map} +1 -1
- package/dist/{resource-client-vf9lL4Qq.mjs → resource-client-rQ9bBn4L.mjs} +229 -63
- package/dist/resource-client-rQ9bBn4L.mjs.map +1 -0
- package/dist/{retry-DKkVbItD.d.mts → retry-DGY51INM.d.mts} +28 -13
- package/dist/{retry-DKkVbItD.d.mts.map → retry-DGY51INM.d.mts.map} +1 -1
- package/dist/{retry-CkPd2BcU.mjs → retry-PmpgEwFN.mjs} +45 -28
- package/dist/retry-PmpgEwFN.mjs.map +1 -0
- package/dist/storage.d.mts +33 -8
- package/dist/storage.d.mts.map +1 -1
- package/dist/storage.mjs +24 -33
- package/dist/storage.mjs.map +1 -1
- package/dist/testing.d.mts +50 -16
- package/dist/testing.d.mts.map +1 -1
- package/dist/testing.mjs +5 -3
- package/dist/testing.mjs.map +1 -1
- package/dist/{types-CRiJg5EQ.d.mts → types-DXai6-hU.d.mts} +5 -3
- package/dist/{types-CRiJg5EQ.d.mts.map → types-DXai6-hU.d.mts.map} +1 -1
- package/dist/universes.d.mts +7 -4
- package/dist/universes.d.mts.map +1 -1
- package/dist/universes.mjs +20 -25
- package/dist/universes.mjs.map +1 -1
- package/dist/{validation-xJZRa8tX.mjs → validation-BXhEM3OP.mjs} +2 -2
- package/dist/{validation-xJZRa8tX.mjs.map → validation-BXhEM3OP.mjs.map} +1 -1
- package/package.json +2 -1
- package/dist/polling-helpers-P17IyI3R.mjs.map +0 -1
- package/dist/polling-rj0dGREg.d.mts.map +0 -1
- package/dist/resource-client-vf9lL4Qq.mjs.map +0 -1
- package/dist/retry-CkPd2BcU.mjs.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as NetworkError, r as ApiError, t as RateLimitError } from "./rate-limit-
|
|
2
|
-
import { a as computeRetryWaitMs, c as shouldRetry, n as IDEMPOTENT_METHOD_DEFAULTS, o as defaultRetryDelay, s as mergeConfig, u as PermissionError } from "./retry-
|
|
1
|
+
import { n as NetworkError, r as ApiError, t as RateLimitError } from "./rate-limit-B20REZEY.mjs";
|
|
2
|
+
import { a as computeRetryWaitMs, c as shouldRetry, n as IDEMPOTENT_METHOD_DEFAULTS, o as defaultRetryDelay, s as mergeConfig, u as PermissionError } from "./retry-PmpgEwFN.mjs";
|
|
3
3
|
import { setTimeout } from "node:timers/promises";
|
|
4
4
|
//#region src/internal/utils/is-date-time-string.ts
|
|
5
5
|
/**
|
|
@@ -32,6 +32,52 @@ function isRecord(value) {
|
|
|
32
32
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
33
33
|
}
|
|
34
34
|
//#endregion
|
|
35
|
+
//#region src/internal/utils/to-json-details.ts
|
|
36
|
+
/**
|
|
37
|
+
* Narrows an untyped response body to the `JSONValue` accepted by
|
|
38
|
+
* `ApiError.details`. `HttpResponse.body` is `unknown` because a transport may
|
|
39
|
+
* hand back anything, but only a JSON graph is safe to retain on an error for
|
|
40
|
+
* diagnostics. Anything else — functions, symbols, bigints, class instances,
|
|
41
|
+
* or a cyclic graph — is dropped rather than asserted through.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The parsed response body, or `undefined` for an empty body.
|
|
44
|
+
* @returns The value as a `JSONValue`, or `undefined` when it is absent or not
|
|
45
|
+
* JSON-representable.
|
|
46
|
+
*/
|
|
47
|
+
function toJsonDetails(value) {
|
|
48
|
+
return asJsonValue(value, /* @__PURE__ */ new Set());
|
|
49
|
+
}
|
|
50
|
+
function asJsonValue(value, seen) {
|
|
51
|
+
if (value === null) return null;
|
|
52
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
53
|
+
if (Array.isArray(value)) return asJsonArray(value, seen);
|
|
54
|
+
if (isRecord(value)) return asJsonObject(value, seen);
|
|
55
|
+
}
|
|
56
|
+
function asJsonArray(value, seen) {
|
|
57
|
+
if (seen.has(value)) return;
|
|
58
|
+
seen.add(value);
|
|
59
|
+
const items = [];
|
|
60
|
+
for (const item of value) {
|
|
61
|
+
const converted = asJsonValue(item, seen);
|
|
62
|
+
if (converted === void 0) return;
|
|
63
|
+
items.push(converted);
|
|
64
|
+
}
|
|
65
|
+
seen.delete(value);
|
|
66
|
+
return items;
|
|
67
|
+
}
|
|
68
|
+
function asJsonObject(value, seen) {
|
|
69
|
+
if (seen.has(value)) return;
|
|
70
|
+
seen.add(value);
|
|
71
|
+
const entries = [];
|
|
72
|
+
for (const [key, item] of Object.entries(value)) {
|
|
73
|
+
const converted = asJsonValue(item, seen);
|
|
74
|
+
if (converted === void 0) return;
|
|
75
|
+
entries.push([key, converted]);
|
|
76
|
+
}
|
|
77
|
+
seen.delete(value);
|
|
78
|
+
return Object.fromEntries(entries);
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
35
81
|
//#region src/internal/http/budget-tracker.ts
|
|
36
82
|
const MS_PER_SECOND = 1e3;
|
|
37
83
|
/**
|
|
@@ -131,10 +177,10 @@ var BudgetGate = class {
|
|
|
131
177
|
*
|
|
132
178
|
* @param scope - The scope key to gate on (the effective API key).
|
|
133
179
|
*/
|
|
134
|
-
async
|
|
180
|
+
async gateAsync(scope) {
|
|
135
181
|
const previous = this.#chains.get(scope) ?? Promise.resolve();
|
|
136
|
-
const
|
|
137
|
-
const mine = previous.then(
|
|
182
|
+
const runGateAsync = async () => this.#gateOnce(scope);
|
|
183
|
+
const mine = previous.then(runGateAsync, runGateAsync);
|
|
138
184
|
this.#chains.set(scope, mine);
|
|
139
185
|
await mine;
|
|
140
186
|
}
|
|
@@ -143,7 +189,7 @@ var BudgetGate = class {
|
|
|
143
189
|
* sample (headers absent or non-numeric) is ignored, leaving the scope on
|
|
144
190
|
* static pacing.
|
|
145
191
|
*
|
|
146
|
-
* @param scope - The same scope key passed to {@link
|
|
192
|
+
* @param scope - The same scope key passed to {@link gateAsync}.
|
|
147
193
|
* @param sample - Parsed sample, or `undefined` when none was reported.
|
|
148
194
|
*/
|
|
149
195
|
observe(scope, sample) {
|
|
@@ -177,13 +223,12 @@ var BudgetGate = class {
|
|
|
177
223
|
* @param options - The transport callback, resolved config, hooks, and sleep.
|
|
178
224
|
* @returns The first success, or the final error after retries are exhausted.
|
|
179
225
|
*/
|
|
180
|
-
async function
|
|
181
|
-
|
|
182
|
-
async function attempt() {
|
|
226
|
+
async function executeWithRetryAsync(request, { config, hooks, send, sleep }) {
|
|
227
|
+
async function attemptAsync() {
|
|
183
228
|
hooks.onRequest?.(request);
|
|
184
229
|
return send(request);
|
|
185
230
|
}
|
|
186
|
-
let result = await
|
|
231
|
+
let result = await attemptAsync();
|
|
187
232
|
for (let retry = 0; retry < config.maxRetries; retry++) {
|
|
188
233
|
if (result.success || !shouldRetry(result.err, config)) return result;
|
|
189
234
|
const { err } = result;
|
|
@@ -194,7 +239,7 @@ async function executeWithRetry(request, options) {
|
|
|
194
239
|
});
|
|
195
240
|
hooks.onRateLimit?.(waitMs);
|
|
196
241
|
await sleep(waitMs);
|
|
197
|
-
result = await
|
|
242
|
+
result = await attemptAsync();
|
|
198
243
|
}
|
|
199
244
|
return result;
|
|
200
245
|
}
|
|
@@ -229,8 +274,12 @@ function reduceRateLimitTokens(headerValue, combine) {
|
|
|
229
274
|
* @returns The parsed sample, or `undefined` when the budget cannot be read.
|
|
230
275
|
*/
|
|
231
276
|
function parseRateLimitHeaders(headers) {
|
|
232
|
-
const remaining = reduceRateLimitTokens(headers["x-ratelimit-remaining"], (a, b) =>
|
|
233
|
-
|
|
277
|
+
const remaining = reduceRateLimitTokens(headers["x-ratelimit-remaining"], (a, b) => {
|
|
278
|
+
return Math.min(a, b);
|
|
279
|
+
});
|
|
280
|
+
const resetSeconds = reduceRateLimitTokens(headers["x-ratelimit-reset"], (a, b) => {
|
|
281
|
+
return Math.max(a, b);
|
|
282
|
+
});
|
|
234
283
|
if (remaining === void 0 || resetSeconds === void 0) return;
|
|
235
284
|
return {
|
|
236
285
|
remaining,
|
|
@@ -262,12 +311,16 @@ function rateLimitSampleFromResult(result) {
|
|
|
262
311
|
/**
|
|
263
312
|
* Token-bucket rate limiter for a single `(apiKey, operation)` pair. Every
|
|
264
313
|
* call to `acquire` consumes one token; when the bucket is empty the call
|
|
265
|
-
* waits until a token regenerates before invoking the task.
|
|
266
|
-
*
|
|
314
|
+
* waits until a token regenerates before invoking the task. Tokens refill at
|
|
315
|
+
* `maxPerSecond` per second, up to the operation's `burstCapacity`.
|
|
267
316
|
*
|
|
268
317
|
* Implemented as a leaky bucket tracking drain debt in ms. `#lastCheck`
|
|
269
318
|
* advances by `waitMs` after every sleep so the algorithm stays correct
|
|
270
|
-
* whether or not the injected sleep moves `Date.now()` forward.
|
|
319
|
+
* whether or not the injected sleep moves `Date.now()` forward. `#bucketLevel`
|
|
320
|
+
* and `#maxBucketLevel` are both ms of drain debt, so the ceiling is the burst
|
|
321
|
+
* expressed in that unit: `burstCapacity` refill intervals. Deriving it any
|
|
322
|
+
* other way (notably `maxPerSecond * intervalMs`, whose units cancel to a
|
|
323
|
+
* constant 1000) starves every operation slower than one request per second.
|
|
271
324
|
*/
|
|
272
325
|
var RateLimitQueue = class {
|
|
273
326
|
#hooks;
|
|
@@ -287,7 +340,8 @@ var RateLimitQueue = class {
|
|
|
287
340
|
*/
|
|
288
341
|
constructor(limit, hooks, sleep) {
|
|
289
342
|
this.#intervalMs = 1e3 / limit.maxPerSecond;
|
|
290
|
-
|
|
343
|
+
const burstCapacity = limit.burstCapacity ?? Math.max(1, limit.maxPerSecond);
|
|
344
|
+
this.#maxBucketLevel = burstCapacity * this.#intervalMs;
|
|
291
345
|
this.#hooks = hooks;
|
|
292
346
|
this.#sleep = sleep;
|
|
293
347
|
}
|
|
@@ -300,7 +354,7 @@ var RateLimitQueue = class {
|
|
|
300
354
|
* @param task - The request to run once a token is available.
|
|
301
355
|
* @returns The value produced by `task`.
|
|
302
356
|
*/
|
|
303
|
-
async
|
|
357
|
+
async acquireAsync(task) {
|
|
304
358
|
const myTurn = this.#chain.then(async () => this.#waitForToken());
|
|
305
359
|
this.#chain = myTurn;
|
|
306
360
|
await myTurn;
|
|
@@ -330,7 +384,7 @@ var RateLimitQueue = class {
|
|
|
330
384
|
* @param promise - The promise to wrap.
|
|
331
385
|
* @returns A Result containing the resolved value or the rejection error.
|
|
332
386
|
*/
|
|
333
|
-
async function
|
|
387
|
+
async function tryCatchAsync(promise) {
|
|
334
388
|
try {
|
|
335
389
|
return {
|
|
336
390
|
data: await promise,
|
|
@@ -352,9 +406,9 @@ const DIAGNOSTIC_HEADER_ALLOWLIST = new Set([
|
|
|
352
406
|
"x-request-id"
|
|
353
407
|
]);
|
|
354
408
|
const DIAGNOSTIC_HEADER_PREFIX = "x-roblox-";
|
|
355
|
-
const
|
|
356
|
-
const
|
|
357
|
-
const TAG_PATTERN = /<[
|
|
409
|
+
const TITLE_OPEN_PATTERN = /<title[^>]*>/i;
|
|
410
|
+
const H1_OPEN_PATTERN = /<h1[^>]*>/i;
|
|
411
|
+
const TAG_PATTERN = /<[^<>]*>/g;
|
|
358
412
|
const WHITESPACE_PATTERN = /\s+/g;
|
|
359
413
|
/**
|
|
360
414
|
* Extracts a one-line human summary from an HTML gateway error page, or returns
|
|
@@ -373,7 +427,13 @@ const WHITESPACE_PATTERN = /\s+/g;
|
|
|
373
427
|
*/
|
|
374
428
|
function extractGatewaySummary(contentType, rawText) {
|
|
375
429
|
if (!isHtmlBody(contentType, rawText)) return;
|
|
376
|
-
return firstTagText(rawText,
|
|
430
|
+
return firstTagText(rawText, {
|
|
431
|
+
close: "</title>",
|
|
432
|
+
open: TITLE_OPEN_PATTERN
|
|
433
|
+
}) ?? firstTagText(rawText, {
|
|
434
|
+
close: "</h1>",
|
|
435
|
+
open: H1_OPEN_PATTERN
|
|
436
|
+
});
|
|
377
437
|
}
|
|
378
438
|
/**
|
|
379
439
|
* Filters a lowercased header record down to the diagnostic allowlist: a few
|
|
@@ -388,10 +448,13 @@ function pickDiagnosticHeaders(headers) {
|
|
|
388
448
|
for (const [name, value] of Object.entries(headers)) if (DIAGNOSTIC_HEADER_ALLOWLIST.has(name) || name.startsWith(DIAGNOSTIC_HEADER_PREFIX)) picked[name] = value;
|
|
389
449
|
return picked;
|
|
390
450
|
}
|
|
391
|
-
function firstTagText(html,
|
|
392
|
-
const
|
|
393
|
-
if (
|
|
394
|
-
const
|
|
451
|
+
function firstTagText(html, tag) {
|
|
452
|
+
const open = tag.open.exec(html);
|
|
453
|
+
if (open === null) return;
|
|
454
|
+
const start = open.index + open[0].length;
|
|
455
|
+
const end = html.toLowerCase().indexOf(tag.close, start);
|
|
456
|
+
if (end === -1) return;
|
|
457
|
+
const text = html.slice(start, end).replace(TAG_PATTERN, " ").replace(WHITESPACE_PATTERN, " ").trim();
|
|
395
458
|
return text === "" ? void 0 : text;
|
|
396
459
|
}
|
|
397
460
|
function isHtmlBody(contentType, rawText) {
|
|
@@ -400,6 +463,84 @@ function isHtmlBody(contentType, rawText) {
|
|
|
400
463
|
return head.startsWith("<html") || head.startsWith("<!doctype html");
|
|
401
464
|
}
|
|
402
465
|
//#endregion
|
|
466
|
+
//#region src/internal/http/http1-dispatcher.ts
|
|
467
|
+
/**
|
|
468
|
+
* Well-known globals under which undici publishes its global dispatcher,
|
|
469
|
+
* newest first. The suffix is undici's own contract version: it moved from
|
|
470
|
+
* `.1` to `.2` in undici 8, so a future major may move it again. Both are
|
|
471
|
+
* probed, and {@link createHttp1Dispatcher} degrades to `undefined` when
|
|
472
|
+
* neither is present — which is also the Bun path, since Bun's `fetch` is not
|
|
473
|
+
* undici-backed and never publishes these.
|
|
474
|
+
*/
|
|
475
|
+
const GLOBAL_DISPATCHER_KEYS = Object.freeze(["undici.globalDispatcher.2", "undici.globalDispatcher.1"]);
|
|
476
|
+
/**
|
|
477
|
+
* Narrow a value read off the global dispatcher to something
|
|
478
|
+
* `Reflect.construct` accepts. Exported so the narrowing is unit-testable: at
|
|
479
|
+
* the call site every rejected shape funnels into the same `undefined`, which
|
|
480
|
+
* makes the branch unobservable from `createHttp1Dispatcher` alone.
|
|
481
|
+
*
|
|
482
|
+
* @param value - The candidate `constructor` read off the global dispatcher.
|
|
483
|
+
* @returns Whether the value can be constructed.
|
|
484
|
+
*/
|
|
485
|
+
function isAgentConstructor(value) {
|
|
486
|
+
return typeof value === "function";
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Builds a request dispatcher that negotiates HTTP/1.1 only, by reusing the
|
|
490
|
+
* class of the runtime's own global dispatcher.
|
|
491
|
+
*
|
|
492
|
+
* From Node 26 (undici 8) `fetch` offers h2 in ALPN, which defeats the
|
|
493
|
+
* `connection: close` an upload sets: `connection` is a connection-specific
|
|
494
|
+
* header, so an h2 transport drops it before the wire and pools every upload
|
|
495
|
+
* onto one multiplexed session. One session death then fails every upload in
|
|
496
|
+
* flight. Constructing the same agent with `allowH2: false` restores the
|
|
497
|
+
* per-upload connection the directive was written for.
|
|
498
|
+
*
|
|
499
|
+
* There is no standard `fetch` option for this, so the agent is reached
|
|
500
|
+
* through undici's versioned global symbol and its class rather than through
|
|
501
|
+
* an import — which keeps the package dependency-free. The contract is
|
|
502
|
+
* internal, so every step is guarded: an absent symbol, a non-constructible
|
|
503
|
+
* value, or a constructor that throws all yield `undefined`, and the caller
|
|
504
|
+
* sends the request unmodified.
|
|
505
|
+
*
|
|
506
|
+
* @param scope - The object to read the global dispatcher from. Defaults to
|
|
507
|
+
* `globalThis`; injectable so tests need not mutate the real global.
|
|
508
|
+
* @returns A dispatcher restricted to HTTP/1.1, or `undefined` when this
|
|
509
|
+
* runtime offers no reachable one.
|
|
510
|
+
*/
|
|
511
|
+
function createHttp1Dispatcher(scope = globalThis) {
|
|
512
|
+
for (const key of GLOBAL_DISPATCHER_KEYS) {
|
|
513
|
+
const dispatcher = constructHttp1(Reflect.get(scope, Symbol.for(key)));
|
|
514
|
+
if (dispatcher !== void 0) return dispatcher;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Reconstructs `agent`'s class with h2 disabled.
|
|
519
|
+
*
|
|
520
|
+
* Most ways `agent` can be wrong already throw: `Reflect.get` rejects a
|
|
521
|
+
* primitive, `null`, and an absent key, and `Reflect.construct` rejects a
|
|
522
|
+
* `constructor` that is not one, or that refuses the options. One shape gets
|
|
523
|
+
* through — a plain object, whose `constructor` is `Object`, so constructing
|
|
524
|
+
* it hands back the options bag. The result is therefore checked against the
|
|
525
|
+
* one part of undici's surface that is public and stable: a `Dispatcher`
|
|
526
|
+
* exposes `dispatch`.
|
|
527
|
+
*
|
|
528
|
+
* @param agent - The candidate global dispatcher; any value, since it comes
|
|
529
|
+
* from an undocumented global.
|
|
530
|
+
* @returns The new dispatcher, or `undefined` when `agent` is not an object
|
|
531
|
+
* whose constructor accepts undici's agent options.
|
|
532
|
+
*/
|
|
533
|
+
function constructHttp1(agent) {
|
|
534
|
+
try {
|
|
535
|
+
const agentClass = Reflect.get(agent, "constructor");
|
|
536
|
+
if (!isAgentConstructor(agentClass)) return;
|
|
537
|
+
const dispatcher = Reflect.construct(agentClass, [{ allowH2: false }]);
|
|
538
|
+
return typeof Reflect.get(dispatcher, "dispatch") === "function" ? dispatcher : void 0;
|
|
539
|
+
} catch {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
//#endregion
|
|
403
544
|
//#region src/internal/http/upload-request.ts
|
|
404
545
|
/**
|
|
405
546
|
* Reports whether a request is an upload: its body is `FormData`
|
|
@@ -462,11 +603,11 @@ function extractErrorMessage(body) {
|
|
|
462
603
|
return extractLegacyMessage(body);
|
|
463
604
|
}
|
|
464
605
|
/**
|
|
465
|
-
* Parses the `x-ratelimit-reset` header value into seconds. On a 429 the
|
|
466
|
-
* is a comma-separated list of per-window reset times (e.g. `"22, 0"`,
|
|
467
|
-
* per rate-limit window); the largest value is the longest-resetting
|
|
468
|
-
* the only safe wait that won't retry into a still-exhausted
|
|
469
|
-
* value is treated as a one-element list.
|
|
606
|
+
* Parses the `x-ratelimit-reset` header value into seconds. On a 429 the
|
|
607
|
+
* header is a comma-separated list of per-window reset times (e.g. `"22, 0"`,
|
|
608
|
+
* one entry per rate-limit window); the largest value is the longest-resetting
|
|
609
|
+
* window and the only safe wait that won't retry into a still-exhausted
|
|
610
|
+
* window. A single value is treated as a one-element list.
|
|
470
611
|
*
|
|
471
612
|
* @param headerValue - The raw header value, or `undefined` if missing.
|
|
472
613
|
* @returns The number of seconds to wait, or 0 if missing/invalid.
|
|
@@ -513,19 +654,23 @@ function buildFetchOptions(request, config) {
|
|
|
513
654
|
* Creates an {@link HttpClient} backed by the Fetch API.
|
|
514
655
|
*
|
|
515
656
|
* @param fetchFunc - The fetch implementation to use. Defaults to `globalThis.fetch`.
|
|
516
|
-
* @param
|
|
517
|
-
*
|
|
657
|
+
* @param seams - Injectable clock and dispatcher factory, so tests need not
|
|
658
|
+
* depend on wall-clock time or on the runtime's global dispatcher.
|
|
518
659
|
* @returns An HttpClient that classifies responses into typed Results.
|
|
519
660
|
*/
|
|
520
|
-
function createFetchHttpClient(fetchFunc =
|
|
661
|
+
function createFetchHttpClient(fetchFunc = fetch, { createDispatcher = createHttp1Dispatcher, now = Date.now } = {}) {
|
|
662
|
+
const dispatcherFor = createUploadDispatcherCache(createDispatcher);
|
|
521
663
|
return { async request(httpRequest, config) {
|
|
522
664
|
const url = buildUrl(httpRequest, config);
|
|
523
665
|
const options = buildFetchOptions(httpRequest, config);
|
|
666
|
+
options.dispatcher = dispatcherFor(httpRequest);
|
|
524
667
|
const target = {
|
|
525
668
|
method: httpRequest.method,
|
|
526
669
|
url
|
|
527
670
|
};
|
|
528
|
-
const { elapsedMs, fetchResult } = await
|
|
671
|
+
const { elapsedMs, fetchResult } = await timedFetchAsync(now, async () => {
|
|
672
|
+
return fetchFunc(url, options);
|
|
673
|
+
});
|
|
529
674
|
if (!fetchResult.success) return {
|
|
530
675
|
err: networkError(fetchResult.err, target),
|
|
531
676
|
success: false
|
|
@@ -535,7 +680,7 @@ function createFetchHttpClient(fetchFunc = globalThis.fetch, now = Date.now) {
|
|
|
535
680
|
method: target.method,
|
|
536
681
|
url: target.url
|
|
537
682
|
};
|
|
538
|
-
const classified = await
|
|
683
|
+
const classified = await tryCatchAsync(classifyResponseAsync(fetchResult.data, context));
|
|
539
684
|
if (!classified.success) return {
|
|
540
685
|
err: networkError(classified.err, target),
|
|
541
686
|
success: false
|
|
@@ -581,6 +726,28 @@ function applyRequestHeaders(headers, request) {
|
|
|
581
726
|
if (isUploadRequest(request)) headers.set(CONNECTION_HEADER, "close");
|
|
582
727
|
}
|
|
583
728
|
/**
|
|
729
|
+
* Wraps a dispatcher factory in the caching policy uploads need.
|
|
730
|
+
*
|
|
731
|
+
* Resolution happens on the first upload rather than at construction: undici
|
|
732
|
+
* publishes its global dispatcher lazily, so before a process's first `fetch`
|
|
733
|
+
* there is nothing to read. A resolved dispatcher is kept, and an unresolved
|
|
734
|
+
* one is retried on the next upload — so a runtime that publishes late is
|
|
735
|
+
* still picked up, and the first request of a process, which has no pooled
|
|
736
|
+
* connection to lose, is safe either way.
|
|
737
|
+
*
|
|
738
|
+
* @param createDispatcher - Builds the HTTP/1.1-only transport.
|
|
739
|
+
* @returns A function yielding the dispatcher for a request, or `undefined`
|
|
740
|
+
* when the request is not an upload or no dispatcher is available.
|
|
741
|
+
*/
|
|
742
|
+
function createUploadDispatcherCache(createDispatcher) {
|
|
743
|
+
let cached;
|
|
744
|
+
return (request) => {
|
|
745
|
+
if (!isUploadRequest(request)) return;
|
|
746
|
+
cached ??= createDispatcher();
|
|
747
|
+
return cached;
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
584
751
|
* Runs `send` and reports both its Result and how long it was in flight,
|
|
585
752
|
* measured with `now`. Isolated so the timing start need not sit in the request
|
|
586
753
|
* body ahead of the transport-failure early return.
|
|
@@ -589,9 +756,9 @@ function applyRequestHeaders(headers, request) {
|
|
|
589
756
|
* @param send - A thunk that issues the fetch.
|
|
590
757
|
* @returns The fetch Result and the elapsed milliseconds.
|
|
591
758
|
*/
|
|
592
|
-
async function
|
|
759
|
+
async function timedFetchAsync(now, send) {
|
|
593
760
|
const start = now();
|
|
594
|
-
const fetchResult = await
|
|
761
|
+
const fetchResult = await tryCatchAsync(send());
|
|
595
762
|
return {
|
|
596
763
|
elapsedMs: Math.max(0, now() - start),
|
|
597
764
|
fetchResult
|
|
@@ -604,8 +771,7 @@ function networkError(cause, target) {
|
|
|
604
771
|
url: target.url
|
|
605
772
|
});
|
|
606
773
|
}
|
|
607
|
-
function formatApiErrorMessage(
|
|
608
|
-
const { code, message, status } = parts;
|
|
774
|
+
function formatApiErrorMessage({ code, message, status }) {
|
|
609
775
|
const base = `HTTP ${status}`;
|
|
610
776
|
if (message === void 0 && code === void 0) return base;
|
|
611
777
|
if (message === void 0) return `${base} (code ${code})`;
|
|
@@ -617,7 +783,7 @@ function formatApiErrorMessage(parts) {
|
|
|
617
783
|
* it parsed, otherwise the raw text truncated to {@link MAX_DETAIL_LENGTH}.
|
|
618
784
|
*
|
|
619
785
|
* @param text - The raw response body text.
|
|
620
|
-
* @param parsed - The best-effort parse result from {@link
|
|
786
|
+
* @param parsed - The best-effort parse result from {@link readResponseBodyAsync}.
|
|
621
787
|
* @returns The parsed body, or the truncated raw text on a parse failure.
|
|
622
788
|
*/
|
|
623
789
|
function bodyDetail(text, parsed) {
|
|
@@ -653,7 +819,8 @@ function createApiError(args) {
|
|
|
653
819
|
}
|
|
654
820
|
/**
|
|
655
821
|
* Parses response text as JSON, returning the underlying `SyntaxError` on
|
|
656
|
-
* failure rather than throwing. The synchronous sibling of {@link
|
|
822
|
+
* failure rather than throwing. The synchronous sibling of {@link
|
|
823
|
+
* tryCatchAsync}.
|
|
657
824
|
*
|
|
658
825
|
* @param text - The raw response body text.
|
|
659
826
|
* @returns A Result wrapping the parsed value, or the parse error.
|
|
@@ -681,7 +848,7 @@ function parseJson(text) {
|
|
|
681
848
|
* @param response - The Response whose body to read.
|
|
682
849
|
* @returns The parse result and the raw text.
|
|
683
850
|
*/
|
|
684
|
-
async function
|
|
851
|
+
async function readResponseBodyAsync(response) {
|
|
685
852
|
const text = await response.text();
|
|
686
853
|
return {
|
|
687
854
|
parsed: text === "" ? {
|
|
@@ -691,12 +858,14 @@ async function readResponseBody(response) {
|
|
|
691
858
|
text
|
|
692
859
|
};
|
|
693
860
|
}
|
|
694
|
-
async function
|
|
861
|
+
async function createRateLimitErrorAsync(response) {
|
|
695
862
|
const headers = headersToRecord(response.headers);
|
|
696
|
-
const { parsed, text } = await
|
|
863
|
+
const { parsed, text } = await readResponseBodyAsync(response);
|
|
697
864
|
return new RateLimitError("Rate limited", {
|
|
698
865
|
details: bodyDetail(text, parsed),
|
|
699
|
-
remaining: reduceRateLimitTokens(headers["x-ratelimit-remaining"], (a, b) =>
|
|
866
|
+
remaining: reduceRateLimitTokens(headers["x-ratelimit-remaining"], (a, b) => {
|
|
867
|
+
return Math.min(a, b);
|
|
868
|
+
}),
|
|
700
869
|
retryAfterSeconds: parseRetryAfterSeconds(headers["x-ratelimit-reset"]),
|
|
701
870
|
statusCode: response.status
|
|
702
871
|
});
|
|
@@ -730,12 +899,12 @@ function parseFailureError({ cause, response, text }) {
|
|
|
730
899
|
* onto any {@link ApiError} built for an error response.
|
|
731
900
|
* @returns A Result containing an HttpResponse on success or an OpenCloudError on failure.
|
|
732
901
|
*/
|
|
733
|
-
async function
|
|
902
|
+
async function classifyResponseAsync(response, context) {
|
|
734
903
|
if (response.status === 429) return {
|
|
735
|
-
err: await
|
|
904
|
+
err: await createRateLimitErrorAsync(response),
|
|
736
905
|
success: false
|
|
737
906
|
};
|
|
738
|
-
const { parsed, text } = await
|
|
907
|
+
const { parsed, text } = await readResponseBodyAsync(response);
|
|
739
908
|
if (response.status >= 300) return {
|
|
740
909
|
err: createApiError({
|
|
741
910
|
context,
|
|
@@ -825,7 +994,7 @@ const CLIENT_DEFAULTS = Object.freeze({
|
|
|
825
994
|
* the frozen client config, observability hooks, injected HTTP client and
|
|
826
995
|
* sleep, and the per-effective-key rate-limit queue registry. Resource
|
|
827
996
|
* classes compose one instance and dispatch every public method through
|
|
828
|
-
* {@link ResourceClient.
|
|
997
|
+
* {@link ResourceClient.executeAsync} with a per-method {@link ResourceMethodSpec}.
|
|
829
998
|
* Not exported from any package subpath; reachable only via sibling
|
|
830
999
|
* `src/resources/**` modules in this package.
|
|
831
1000
|
*/
|
|
@@ -844,8 +1013,7 @@ var ResourceClient = class {
|
|
|
844
1013
|
* @param options - Client-level configuration including the API key
|
|
845
1014
|
* and optional construction-time test seams.
|
|
846
1015
|
*/
|
|
847
|
-
constructor(
|
|
848
|
-
const { apiKey, hooks, httpClient, sleep, ...overrides } = options;
|
|
1016
|
+
constructor({ apiKey, hooks, httpClient, sleep, ...overrides }) {
|
|
849
1017
|
const resolved = resolveDependencies({
|
|
850
1018
|
httpClient,
|
|
851
1019
|
sleep
|
|
@@ -872,8 +1040,7 @@ var ResourceClient = class {
|
|
|
872
1040
|
* @returns The parsed success payload or the {@link OpenCloudError} that
|
|
873
1041
|
* caused the request to fail. Never throws.
|
|
874
1042
|
*/
|
|
875
|
-
async
|
|
876
|
-
const { options, parameters, spec } = call;
|
|
1043
|
+
async executeAsync({ options, parameters, spec }) {
|
|
877
1044
|
const merged = mergeConfig(this.#config, {
|
|
878
1045
|
methodDefaults: spec.methodDefaults,
|
|
879
1046
|
methodKind: spec.methodKind,
|
|
@@ -886,8 +1053,8 @@ var ResourceClient = class {
|
|
|
886
1053
|
options,
|
|
887
1054
|
request: requestResult.data
|
|
888
1055
|
});
|
|
889
|
-
const httpResult = await this.#getQueue(merged.apiKey, spec.operationLimit).
|
|
890
|
-
return
|
|
1056
|
+
const httpResult = await this.#getQueue(merged.apiKey, spec.operationLimit).acquireAsync(async () => {
|
|
1057
|
+
return executeWithRetryAsync(requestResult.data, {
|
|
891
1058
|
config: merged,
|
|
892
1059
|
hooks: this.#hooks,
|
|
893
1060
|
send: this.#gatedSend(merged.apiKey, requestConfig),
|
|
@@ -916,11 +1083,11 @@ var ResourceClient = class {
|
|
|
916
1083
|
*
|
|
917
1084
|
* @param apiKey - The effective API key to gate on.
|
|
918
1085
|
* @param requestConfig - The resolved per-request transport config.
|
|
919
|
-
* @returns A send callback for {@link
|
|
1086
|
+
* @returns A send callback for {@link executeWithRetryAsync}.
|
|
920
1087
|
*/
|
|
921
1088
|
#gatedSend(apiKey, requestConfig) {
|
|
922
1089
|
return async (toSend) => {
|
|
923
|
-
await this.#budgets.
|
|
1090
|
+
await this.#budgets.gateAsync(apiKey);
|
|
924
1091
|
const sendResult = await this.#httpClient.request(toSend, requestConfig);
|
|
925
1092
|
this.#budgets.observe(apiKey, rateLimitSampleFromResult(sendResult));
|
|
926
1093
|
return sendResult;
|
|
@@ -946,8 +1113,7 @@ var ResourceClient = class {
|
|
|
946
1113
|
* @returns The config to hand to the transport, with `timeout` omitted when
|
|
947
1114
|
* no client-side deadline should apply.
|
|
948
1115
|
*/
|
|
949
|
-
function buildRequestConfig(
|
|
950
|
-
const { merged, options, request } = inputs;
|
|
1116
|
+
function buildRequestConfig({ merged, options, request }) {
|
|
951
1117
|
const shouldOmitDefaultTimeout = options?.timeout === void 0 && isUploadRequest(request);
|
|
952
1118
|
return {
|
|
953
1119
|
apiKey: merged.apiKey,
|
|
@@ -970,6 +1136,6 @@ function enrichPermissionError(err, spec) {
|
|
|
970
1136
|
});
|
|
971
1137
|
}
|
|
972
1138
|
//#endregion
|
|
973
|
-
export {
|
|
1139
|
+
export { isRecord as a, toJsonDetails as i, okRequest as n, isDateTimeString as o, parseEmptyResponse as r, ResourceClient as t };
|
|
974
1140
|
|
|
975
|
-
//# sourceMappingURL=resource-client-
|
|
1141
|
+
//# sourceMappingURL=resource-client-rQ9bBn4L.mjs.map
|