@bedrock-rbx/ocale 0.3.1 → 0.3.2
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 +13 -1
- package/dist/badges.d.mts +1 -1
- package/dist/badges.mjs +3 -3
- package/dist/{polling-0t1wQyVY.d.mts → capacity-admission-BvnJjY0S.d.mts} +35 -3
- package/dist/capacity-admission-BvnJjY0S.d.mts.map +1 -0
- package/dist/developer-products.d.mts +1 -1
- package/dist/developer-products.mjs +4 -4
- package/dist/game-passes.d.mts +1 -1
- package/dist/game-passes.mjs +4 -4
- package/dist/index.d.mts +112 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/luau-execution.d.mts +39 -5
- package/dist/luau-execution.d.mts.map +1 -1
- package/dist/luau-execution.mjs +8 -13
- package/dist/luau-execution.mjs.map +1 -1
- package/dist/{network-error-DmNy_j-u.mjs → network-error-DR9FX5hb.mjs} +32 -7
- package/dist/network-error-DR9FX5hb.mjs.map +1 -0
- package/dist/places.d.mts +4 -4
- package/dist/places.d.mts.map +1 -1
- package/dist/places.mjs +8 -13
- package/dist/places.mjs.map +1 -1
- package/dist/{poll-timeout-wrPTnGl_.mjs → poll-timeout-3PXLsabj.mjs} +2 -2
- package/dist/{poll-timeout-wrPTnGl_.mjs.map → poll-timeout-3PXLsabj.mjs.map} +1 -1
- package/dist/{polling-helpers-CzdT2FS5.mjs → polling-helpers-CFuSt2O5.mjs} +323 -21
- package/dist/polling-helpers-CFuSt2O5.mjs.map +1 -0
- package/dist/{price-information-Czs5cWR1.mjs → price-information-D8BxwgTS.mjs} +2 -2
- package/dist/{price-information-Czs5cWR1.mjs.map → price-information-D8BxwgTS.mjs.map} +1 -1
- package/dist/{resource-client-aqk_V4TD.mjs → resource-client--yYybltk.mjs} +254 -72
- package/dist/resource-client--yYybltk.mjs.map +1 -0
- package/dist/resource-client-BivRq3JZ.d.mts +164 -0
- package/dist/resource-client-BivRq3JZ.d.mts.map +1 -0
- package/dist/{retry-DIK_T_oF.mjs → retry-Cc4LcXYZ.mjs} +266 -132
- package/dist/retry-Cc4LcXYZ.mjs.map +1 -0
- package/dist/{retry-Cj5OXCdL.d.mts → retry-DXEklFrt.d.mts} +51 -16
- package/dist/{retry-Cj5OXCdL.d.mts.map → retry-DXEklFrt.d.mts.map} +1 -1
- package/dist/storage.d.mts +2 -158
- package/dist/storage.d.mts.map +1 -1
- package/dist/storage.mjs +3 -3
- package/dist/testing.d.mts +1 -1
- package/dist/testing.mjs +1 -1
- package/dist/{types-D_njb3qW.d.mts → types-C3Egi37J.d.mts} +14 -8
- package/dist/types-C3Egi37J.d.mts.map +1 -0
- package/dist/universes.d.mts +1 -1
- package/dist/universes.mjs +4 -4
- package/dist/{validation-BwjwOJPD.mjs → validation-B_Zs1So2.mjs} +2 -2
- package/dist/{validation-BwjwOJPD.mjs.map → validation-B_Zs1So2.mjs.map} +1 -1
- package/package.json +1 -1
- package/dist/network-error-DmNy_j-u.mjs.map +0 -1
- package/dist/polling-0t1wQyVY.d.mts.map +0 -1
- package/dist/polling-helpers-CzdT2FS5.mjs.map +0 -1
- package/dist/resource-client-aqk_V4TD.mjs.map +0 -1
- package/dist/retry-DIK_T_oF.mjs.map +0 -1
- package/dist/types-D_njb3qW.d.mts.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as ApiError, n as RateLimitError, o as requestContextOf, r as hasServerRetryGuidance } from "./network-error-
|
|
2
|
-
import { _ as
|
|
1
|
+
import { a as ApiError, n as RateLimitError, o as requestContextOf, r as hasServerRetryGuidance } from "./network-error-DR9FX5hb.mjs";
|
|
2
|
+
import { _ as ABORTED, b as RequestAbortedError, c as defaultRetryDelay, f as RetryDelayExceededError, g as isUploadRequest, h as createFetchHttpClient, l as mergeConfig, m as PermissionError, p as RequestDeadlineExceededError, r as IDEMPOTENT_METHOD_DEFAULTS, s as computeRetryWaitMs, u as shouldRetry, v as raceWithAbortAsync, x as parseRateLimitHeaders, y as requestAbortedError } from "./retry-Cc4LcXYZ.mjs";
|
|
3
3
|
import { setTimeout } from "node:timers/promises";
|
|
4
4
|
//#region src/internal/utils/is-date-time-string.ts
|
|
5
5
|
/**
|
|
@@ -177,7 +177,94 @@ var BudgetTracker = class {
|
|
|
177
177
|
}
|
|
178
178
|
};
|
|
179
179
|
//#endregion
|
|
180
|
+
//#region src/internal/http/request-deadline.ts
|
|
181
|
+
const DEADLINE_ELAPSED = Symbol("request deadline elapsed");
|
|
182
|
+
const MAX_ABORT_TIMEOUT_MS = 2147483647;
|
|
183
|
+
/**
|
|
184
|
+
* Resolves one signal that spans the whole logical request.
|
|
185
|
+
*
|
|
186
|
+
* @param deadlineMs - Absolute deadline, as Unix epoch milliseconds.
|
|
187
|
+
* @param callerSignal - Optional caller cancellation signal.
|
|
188
|
+
* @returns Signals that span and classify the logical request.
|
|
189
|
+
*/
|
|
190
|
+
function requestLifecycle(deadlineMs, callerSignal) {
|
|
191
|
+
if (deadlineMs === void 0) return {
|
|
192
|
+
deadlineMs,
|
|
193
|
+
deadlineSignal: void 0,
|
|
194
|
+
signal: callerSignal
|
|
195
|
+
};
|
|
196
|
+
const deadlineSignal = deadlineTimeout(deadlineMs);
|
|
197
|
+
return {
|
|
198
|
+
deadlineMs,
|
|
199
|
+
deadlineSignal,
|
|
200
|
+
signal: callerSignal === void 0 ? deadlineSignal : AbortSignal.any([callerSignal, deadlineSignal])
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Returns a typed failure when the deadline signal ended the request.
|
|
205
|
+
*
|
|
206
|
+
* @param lifecycle - Signals and timestamp for the logical request.
|
|
207
|
+
* @returns A deadline failure, or `undefined` when another signal won.
|
|
208
|
+
*/
|
|
209
|
+
function elapsedDeadlineFailure({ deadlineMs, deadlineSignal, signal }) {
|
|
210
|
+
if (deadlineMs === void 0 || deadlineSignal?.aborted !== true || !Object.is(signal?.reason, deadlineSignal.reason)) return;
|
|
211
|
+
return new RequestDeadlineExceededError("Request deadline elapsed", {
|
|
212
|
+
deadlineMs,
|
|
213
|
+
remainingMs: 0
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Reclassifies an internal abort when the request deadline supplied its signal.
|
|
218
|
+
*
|
|
219
|
+
* @param error - Error returned by the request pipeline.
|
|
220
|
+
* @param lifecycle - Signals and timestamp for the logical request.
|
|
221
|
+
* @returns A deadline failure, or `undefined` for other failures.
|
|
222
|
+
*/
|
|
223
|
+
function deadlineFailureFromError(error, lifecycle) {
|
|
224
|
+
return error instanceof RequestAbortedError ? elapsedDeadlineFailure(lifecycle) : void 0;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Refuses a known admission wait that cannot fit before the deadline.
|
|
228
|
+
*
|
|
229
|
+
* @param inputs - Deadline, wait duration, reason, and optional cause.
|
|
230
|
+
* @returns A typed refusal, or `undefined` when the wait fits.
|
|
231
|
+
*/
|
|
232
|
+
function waitDeadlineFailure({ cause, deadlineMs, waitMs, waitReason }) {
|
|
233
|
+
if (deadlineMs === void 0) return;
|
|
234
|
+
const now = Date.now();
|
|
235
|
+
const remainingMs = Math.max(0, deadlineMs - now);
|
|
236
|
+
if (now < deadlineMs && waitMs <= remainingMs) return;
|
|
237
|
+
const options = {
|
|
238
|
+
cause,
|
|
239
|
+
deadlineMs,
|
|
240
|
+
remainingMs,
|
|
241
|
+
waitMs,
|
|
242
|
+
waitReason
|
|
243
|
+
};
|
|
244
|
+
return new RequestDeadlineExceededError(waitMessage(waitMs, remainingMs), options);
|
|
245
|
+
}
|
|
246
|
+
function armDeadlineTimeout(controller, deadlineMs) {
|
|
247
|
+
const remainingMs = deadlineMs - Date.now();
|
|
248
|
+
if (remainingMs <= 0) {
|
|
249
|
+
controller.abort(DEADLINE_ELAPSED);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
AbortSignal.timeout(Math.min(Math.ceil(remainingMs), MAX_ABORT_TIMEOUT_MS)).addEventListener("abort", () => {
|
|
253
|
+
armDeadlineTimeout(controller, deadlineMs);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
function deadlineTimeout(deadlineMs) {
|
|
257
|
+
const controller = new AbortController();
|
|
258
|
+
if (Number.isFinite(deadlineMs)) armDeadlineTimeout(controller, deadlineMs);
|
|
259
|
+
else controller.abort(DEADLINE_ELAPSED);
|
|
260
|
+
return controller.signal;
|
|
261
|
+
}
|
|
262
|
+
function waitMessage(waitMs, remainingMs) {
|
|
263
|
+
return `Admission wait would take ${waitMs / 1e3}s; ${remainingMs / 1e3}s remain before the request deadline`;
|
|
264
|
+
}
|
|
265
|
+
//#endregion
|
|
180
266
|
//#region src/internal/http/budget-gate.ts
|
|
267
|
+
const REPORTED_BUDGET_REASON = "reported-budget";
|
|
181
268
|
/**
|
|
182
269
|
* Header-primed rate-limit gate shared across a client. Holds one
|
|
183
270
|
* {@link BudgetTracker} per {@link BudgetScope}. Before each request the caller
|
|
@@ -213,13 +300,14 @@ var BudgetGate = class {
|
|
|
213
300
|
* @param context - Request-local observer and cancellation signal.
|
|
214
301
|
* @rejects {@link RequestAbortedError} when the caller cancels while waiting.
|
|
215
302
|
*/
|
|
216
|
-
async gateAsync(scope, { observer, signal } = {}) {
|
|
303
|
+
async gateAsync(scope, { deadlineMs, observer, signal } = {}) {
|
|
217
304
|
const key = scopeKey(scope);
|
|
218
305
|
const pendingGates = this.#pendingGates.get(key) ?? 0;
|
|
219
306
|
const waitsForEarlierGate = pendingGates > 0;
|
|
220
307
|
this.#pendingGates.set(key, pendingGates + 1);
|
|
221
308
|
const completed = (this.#chains.get(key) ?? Promise.resolve()).catch(ignoreRejection$1).then(async () => {
|
|
222
309
|
return this.#gateOnce(key, {
|
|
310
|
+
deadlineMs,
|
|
223
311
|
observer: waitsForEarlierGate ? void 0 : observer,
|
|
224
312
|
signal
|
|
225
313
|
});
|
|
@@ -230,7 +318,7 @@ var BudgetGate = class {
|
|
|
230
318
|
this.#chains.set(key, completed.catch(ignoreRejection$1));
|
|
231
319
|
if (waitsForEarlierGate) await observeAdmissionWaitAsync({
|
|
232
320
|
observer,
|
|
233
|
-
reason:
|
|
321
|
+
reason: REPORTED_BUDGET_REASON,
|
|
234
322
|
waitAsync: async () => waitForGateAsync(completed, signal)
|
|
235
323
|
});
|
|
236
324
|
else await waitForGateAsync(completed, signal);
|
|
@@ -247,17 +335,14 @@ var BudgetGate = class {
|
|
|
247
335
|
if (sample === void 0) return;
|
|
248
336
|
this.#tracker(scopeKey(scope)).observe(sample, Date.now());
|
|
249
337
|
}
|
|
250
|
-
async #gateOnce(key, { observer, signal }) {
|
|
338
|
+
async #gateOnce(key, { deadlineMs, observer, signal }) {
|
|
251
339
|
if (signal?.aborted === true) throw requestAbortedError(signal);
|
|
252
340
|
const tracker = this.#tracker(key);
|
|
253
341
|
const waitMs = tracker.waitMs(Date.now());
|
|
254
|
-
if (waitMs > 0) await
|
|
255
|
-
|
|
342
|
+
if (waitMs > 0) await this.#waitAsync(waitMs, {
|
|
343
|
+
deadlineMs,
|
|
256
344
|
observer,
|
|
257
|
-
|
|
258
|
-
waitAsync: async () => {
|
|
259
|
-
if (await raceWithAbortAsync(async () => this.#sleep(waitMs, signal), signal) === ABORTED) throw requestAbortedError(signal);
|
|
260
|
-
}
|
|
345
|
+
signal
|
|
261
346
|
});
|
|
262
347
|
tracker.reserve(Date.now());
|
|
263
348
|
}
|
|
@@ -268,6 +353,22 @@ var BudgetGate = class {
|
|
|
268
353
|
this.#trackers.set(key, tracker);
|
|
269
354
|
return tracker;
|
|
270
355
|
}
|
|
356
|
+
async #waitAsync(waitMs, { deadlineMs, observer, signal }) {
|
|
357
|
+
const refusal = waitDeadlineFailure({
|
|
358
|
+
deadlineMs,
|
|
359
|
+
waitMs,
|
|
360
|
+
waitReason: REPORTED_BUDGET_REASON
|
|
361
|
+
});
|
|
362
|
+
if (refusal !== void 0) throw refusal;
|
|
363
|
+
await observeAdmissionWaitAsync({
|
|
364
|
+
durationMs: waitMs,
|
|
365
|
+
observer,
|
|
366
|
+
reason: REPORTED_BUDGET_REASON,
|
|
367
|
+
waitAsync: async () => {
|
|
368
|
+
if (await raceWithAbortAsync(async () => this.#sleep(waitMs, signal), signal) === ABORTED) throw requestAbortedError(signal);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
271
372
|
};
|
|
272
373
|
function ignoreRejection$1() {}
|
|
273
374
|
async function waitForGateAsync(gate, signal) {
|
|
@@ -296,18 +397,31 @@ function scopeKey({ apiKey, operationKey }) {
|
|
|
296
397
|
* @returns The first success, or the final error after retries are exhausted.
|
|
297
398
|
*/
|
|
298
399
|
async function executeWithRetryAsync(request, options) {
|
|
299
|
-
const { admissionWaitObserver, config, hooks, signal, sleep } = options;
|
|
400
|
+
const { admissionWaitObserver, config, deadlineMs, hooks, signal, sleep } = options;
|
|
300
401
|
if (signal?.aborted === true) return abortedResult(signal);
|
|
301
402
|
let result = await attemptAsync(request, options);
|
|
302
403
|
for (let retry = 0; retry < config.maxRetries; retry++) {
|
|
303
404
|
if (result.success || !shouldRetry(result.err, config)) return result;
|
|
304
405
|
const { err } = result;
|
|
305
|
-
hooks.onRetry?.(retry + 1, err);
|
|
306
406
|
const waitMs = computeRetryWaitMs(err, {
|
|
307
407
|
attempt: retry,
|
|
308
408
|
retryDelay: config.retryDelay
|
|
309
409
|
});
|
|
310
|
-
|
|
410
|
+
const refusal = retryRefusal({
|
|
411
|
+
cause: err,
|
|
412
|
+
deadlineMs,
|
|
413
|
+
retryAfterMs: waitMs
|
|
414
|
+
});
|
|
415
|
+
if (refusal !== void 0) return {
|
|
416
|
+
err: refusal,
|
|
417
|
+
success: false
|
|
418
|
+
};
|
|
419
|
+
announceRetry({
|
|
420
|
+
attempt: retry + 1,
|
|
421
|
+
error: err,
|
|
422
|
+
hooks,
|
|
423
|
+
waitMs
|
|
424
|
+
});
|
|
311
425
|
if (await observeAdmissionWaitAsync({
|
|
312
426
|
durationMs: waitMs,
|
|
313
427
|
observer: admissionWaitObserver,
|
|
@@ -318,6 +432,27 @@ async function executeWithRetryAsync(request, options) {
|
|
|
318
432
|
}
|
|
319
433
|
return result;
|
|
320
434
|
}
|
|
435
|
+
function announceRetry({ attempt, error, hooks, waitMs }) {
|
|
436
|
+
hooks.onRetry?.(attempt, error);
|
|
437
|
+
hooks.onRateLimit?.(waitMs);
|
|
438
|
+
}
|
|
439
|
+
function retryRefusal({ cause, deadlineMs, retryAfterMs }) {
|
|
440
|
+
if (deadlineMs === void 0) return;
|
|
441
|
+
const refusal = waitDeadlineFailure({
|
|
442
|
+
cause,
|
|
443
|
+
deadlineMs,
|
|
444
|
+
waitMs: retryAfterMs,
|
|
445
|
+
waitReason: "retry-delay"
|
|
446
|
+
});
|
|
447
|
+
if (refusal === void 0) return;
|
|
448
|
+
const { remainingMs } = refusal;
|
|
449
|
+
return new RetryDelayExceededError(`Retry delay would wait ${retryAfterMs / 1e3}s; ${remainingMs / 1e3}s remain before the request deadline`, {
|
|
450
|
+
cause,
|
|
451
|
+
deadlineMs,
|
|
452
|
+
remainingMs,
|
|
453
|
+
retryAfterMs
|
|
454
|
+
});
|
|
455
|
+
}
|
|
321
456
|
function abortedResult(signal) {
|
|
322
457
|
return {
|
|
323
458
|
err: requestAbortedError(signal),
|
|
@@ -334,9 +469,9 @@ async function attemptAsync(request, { hooks, send, signal }) {
|
|
|
334
469
|
/**
|
|
335
470
|
* Extracts a {@link RateLimitSample} from a transport result so the budget gate
|
|
336
471
|
* can be fed from every attempt. A 2xx carries the budget in its headers. A 429
|
|
337
|
-
* only primes the gate when it reports
|
|
338
|
-
* a
|
|
339
|
-
*
|
|
472
|
+
* only primes the gate when it reports zero remaining and valid guidance. This
|
|
473
|
+
* is a conservative scheduling condition, not a semantic classification of the
|
|
474
|
+
* 429. Any other error yields `undefined`.
|
|
340
475
|
*
|
|
341
476
|
* @param result - The classified transport result for one attempt.
|
|
342
477
|
* @returns The parsed sample, or `undefined` when none was reported.
|
|
@@ -351,6 +486,7 @@ function rateLimitSampleFromResult(result) {
|
|
|
351
486
|
}
|
|
352
487
|
//#endregion
|
|
353
488
|
//#region src/internal/http/rate-limit-queue.ts
|
|
489
|
+
const OPERATION_QUEUE_REASON = "operation-queue";
|
|
354
490
|
/**
|
|
355
491
|
* Token-bucket rate limiter for a single `(apiKey, operation)` pair. Every
|
|
356
492
|
* call to `acquire` consumes one token; when the bucket is empty the call
|
|
@@ -400,11 +536,12 @@ var RateLimitQueue = class {
|
|
|
400
536
|
* @returns The value produced by `task`.
|
|
401
537
|
* @rejects {@link RequestAbortedError} when the caller cancels while queued.
|
|
402
538
|
*/
|
|
403
|
-
async acquireAsync(task, { observer, signal } = {}) {
|
|
539
|
+
async acquireAsync(task, { deadlineMs, observer, signal } = {}) {
|
|
404
540
|
const waitsForEarlierAcquisition = this.#pendingAcquisitions > 0;
|
|
405
541
|
this.#pendingAcquisitions++;
|
|
406
542
|
const waitForTokenAsync = async () => {
|
|
407
543
|
return this.#waitForToken({
|
|
544
|
+
deadlineMs,
|
|
408
545
|
observer: waitsForEarlierAcquisition ? void 0 : observer,
|
|
409
546
|
signal
|
|
410
547
|
});
|
|
@@ -415,13 +552,32 @@ var RateLimitQueue = class {
|
|
|
415
552
|
this.#chain = completed.catch(ignoreRejection);
|
|
416
553
|
if (waitsForEarlierAcquisition) await observeAdmissionWaitAsync({
|
|
417
554
|
observer,
|
|
418
|
-
reason:
|
|
555
|
+
reason: OPERATION_QUEUE_REASON,
|
|
419
556
|
waitAsync: async () => waitForTurnAsync(completed, signal)
|
|
420
557
|
});
|
|
421
558
|
else await waitForTurnAsync(completed, signal);
|
|
422
559
|
return task();
|
|
423
560
|
}
|
|
424
|
-
async #
|
|
561
|
+
async #waitAsync({ deadlineMs, now, observer, signal, waitMs }) {
|
|
562
|
+
const refusal = waitDeadlineFailure({
|
|
563
|
+
deadlineMs,
|
|
564
|
+
waitMs,
|
|
565
|
+
waitReason: OPERATION_QUEUE_REASON
|
|
566
|
+
});
|
|
567
|
+
if (refusal !== void 0) throw refusal;
|
|
568
|
+
this.#hooks.onRateLimit?.(waitMs);
|
|
569
|
+
await observeAdmissionWaitAsync({
|
|
570
|
+
durationMs: waitMs,
|
|
571
|
+
observer,
|
|
572
|
+
reason: OPERATION_QUEUE_REASON,
|
|
573
|
+
waitAsync: async () => {
|
|
574
|
+
if (await raceWithAbortAsync(async () => this.#sleep(waitMs, signal), signal) === ABORTED) throw requestAbortedError(signal);
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
this.#bucketLevel = this.#maxBucketLevel;
|
|
578
|
+
this.#lastCheck = now + waitMs;
|
|
579
|
+
}
|
|
580
|
+
async #waitForToken({ deadlineMs, observer, signal }) {
|
|
425
581
|
if (signal?.aborted === true) throw requestAbortedError(signal);
|
|
426
582
|
const now = Math.max(Date.now(), this.#lastCheck);
|
|
427
583
|
const drained = Math.max(0, this.#bucketLevel - (now - this.#lastCheck));
|
|
@@ -431,17 +587,13 @@ var RateLimitQueue = class {
|
|
|
431
587
|
return;
|
|
432
588
|
}
|
|
433
589
|
const waitMs = drained + this.#intervalMs - this.#maxBucketLevel;
|
|
434
|
-
this.#
|
|
435
|
-
|
|
436
|
-
|
|
590
|
+
await this.#waitAsync({
|
|
591
|
+
deadlineMs,
|
|
592
|
+
now,
|
|
437
593
|
observer,
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
if (await raceWithAbortAsync(async () => this.#sleep(waitMs, signal), signal) === ABORTED) throw requestAbortedError(signal);
|
|
441
|
-
}
|
|
594
|
+
signal,
|
|
595
|
+
waitMs
|
|
442
596
|
});
|
|
443
|
-
this.#bucketLevel = this.#maxBucketLevel;
|
|
444
|
-
this.#lastCheck = now + waitMs;
|
|
445
597
|
}
|
|
446
598
|
};
|
|
447
599
|
function ignoreRejection() {}
|
|
@@ -569,13 +721,10 @@ var ResourceClient = class {
|
|
|
569
721
|
* caused the request to fail. Never throws.
|
|
570
722
|
* @rejects An unexpected collaborator failure unrelated to caller cancellation.
|
|
571
723
|
*/
|
|
572
|
-
async executeAsync({ options, parameters, spec }) {
|
|
573
|
-
const
|
|
574
|
-
if (
|
|
575
|
-
|
|
576
|
-
success: false
|
|
577
|
-
};
|
|
578
|
-
const { onAdmissionWait, signal: _signal, ...requestOptions } = options ?? {};
|
|
724
|
+
async executeAsync({ options, parameters, refineError, spec }) {
|
|
725
|
+
const start = startRequest(options);
|
|
726
|
+
if (!start.success) return start;
|
|
727
|
+
const { admission, lifecycle, requestOptions } = start.data;
|
|
579
728
|
const merged = mergeConfig(this.#config, {
|
|
580
729
|
methodDefaults: spec.methodDefaults,
|
|
581
730
|
methodKind: spec.methodKind,
|
|
@@ -584,24 +733,25 @@ var ResourceClient = class {
|
|
|
584
733
|
const requestResult = spec.buildRequest(parameters);
|
|
585
734
|
if (!requestResult.success) return requestResult;
|
|
586
735
|
const request = requestResult.data;
|
|
736
|
+
const { signal } = admission;
|
|
587
737
|
const requestConfig = buildRequestConfig({
|
|
588
738
|
merged,
|
|
589
739
|
options,
|
|
590
|
-
request
|
|
591
|
-
});
|
|
592
|
-
const httpResult = await this.#dispatchAsync({
|
|
593
|
-
merged,
|
|
594
|
-
observer: onAdmissionWait,
|
|
595
|
-
operationLimit: spec.operationLimit,
|
|
596
740
|
request,
|
|
597
|
-
requestConfig,
|
|
598
741
|
signal
|
|
599
742
|
});
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
743
|
+
return finishRequest({
|
|
744
|
+
httpResult: await this.#dispatchAsync({
|
|
745
|
+
admission,
|
|
746
|
+
merged,
|
|
747
|
+
operationLimit: spec.operationLimit,
|
|
748
|
+
refineError,
|
|
749
|
+
request,
|
|
750
|
+
requestConfig
|
|
751
|
+
}),
|
|
752
|
+
lifecycle,
|
|
753
|
+
spec
|
|
754
|
+
});
|
|
605
755
|
}
|
|
606
756
|
/**
|
|
607
757
|
* Returns the sleep function used by this client instance.
|
|
@@ -611,30 +761,28 @@ var ResourceClient = class {
|
|
|
611
761
|
get sleep() {
|
|
612
762
|
return this.#sleep;
|
|
613
763
|
}
|
|
614
|
-
async #dispatchAsync({
|
|
764
|
+
async #dispatchAsync({ admission, merged, operationLimit, refineError, request, requestConfig }) {
|
|
615
765
|
const queue = this.#getQueue(merged.apiKey, operationLimit);
|
|
616
766
|
try {
|
|
617
767
|
return await queue.acquireAsync(async () => {
|
|
618
768
|
return executeWithRetryAsync(request, {
|
|
619
|
-
admissionWaitObserver: observer,
|
|
769
|
+
admissionWaitObserver: admission.observer,
|
|
620
770
|
config: merged,
|
|
771
|
+
deadlineMs: admission.deadlineMs,
|
|
621
772
|
hooks: this.#hooks,
|
|
622
773
|
send: this.#gatedSend({
|
|
623
|
-
|
|
774
|
+
admission,
|
|
775
|
+
refineError,
|
|
624
776
|
requestConfig,
|
|
625
777
|
scope: {
|
|
626
778
|
apiKey: merged.apiKey,
|
|
627
779
|
operationKey: operationLimit.operationKey
|
|
628
|
-
}
|
|
629
|
-
signal
|
|
780
|
+
}
|
|
630
781
|
}),
|
|
631
|
-
signal,
|
|
782
|
+
signal: admission.signal,
|
|
632
783
|
sleep: this.#sleep
|
|
633
784
|
});
|
|
634
|
-
},
|
|
635
|
-
observer,
|
|
636
|
-
signal
|
|
637
|
-
});
|
|
785
|
+
}, admission);
|
|
638
786
|
} catch (err) {
|
|
639
787
|
return dispatchFailure(err);
|
|
640
788
|
}
|
|
@@ -648,14 +796,15 @@ var ResourceClient = class {
|
|
|
648
796
|
* @param inputs - Budget scope, transport config, observer, and caller signal.
|
|
649
797
|
* @returns A send callback for {@link executeWithRetryAsync}.
|
|
650
798
|
*/
|
|
651
|
-
#gatedSend({
|
|
799
|
+
#gatedSend({ admission, refineError, requestConfig, scope }) {
|
|
652
800
|
return async (toSend) => {
|
|
653
|
-
await this.#budgets.gateAsync(scope,
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
801
|
+
await this.#budgets.gateAsync(scope, admission);
|
|
802
|
+
const transportResult = await this.#httpClient.request(toSend, requestConfig);
|
|
803
|
+
const sendResult = refineError === void 0 || transportResult.success ? transportResult : {
|
|
804
|
+
err: refineError(transportResult.err),
|
|
805
|
+
success: false
|
|
806
|
+
};
|
|
807
|
+
this.#budgets.observe(scope, rateLimitSampleFromResult(transportResult));
|
|
659
808
|
return sendResult;
|
|
660
809
|
};
|
|
661
810
|
}
|
|
@@ -668,8 +817,34 @@ var ResourceClient = class {
|
|
|
668
817
|
return queue;
|
|
669
818
|
}
|
|
670
819
|
};
|
|
820
|
+
function startRequest(options) {
|
|
821
|
+
const callerSignal = options?.signal;
|
|
822
|
+
if (callerSignal?.aborted === true) return {
|
|
823
|
+
err: requestAbortedError(callerSignal),
|
|
824
|
+
success: false
|
|
825
|
+
};
|
|
826
|
+
const lifecycle = requestLifecycle(options?.deadlineMs, callerSignal);
|
|
827
|
+
const deadlineFailure = elapsedDeadlineFailure(lifecycle);
|
|
828
|
+
if (deadlineFailure !== void 0) return {
|
|
829
|
+
err: deadlineFailure,
|
|
830
|
+
success: false
|
|
831
|
+
};
|
|
832
|
+
const { deadlineMs, onAdmissionWait, signal: _signal, ...requestOptions } = options ?? {};
|
|
833
|
+
return {
|
|
834
|
+
data: {
|
|
835
|
+
admission: {
|
|
836
|
+
deadlineMs,
|
|
837
|
+
observer: onAdmissionWait,
|
|
838
|
+
signal: lifecycle.signal
|
|
839
|
+
},
|
|
840
|
+
lifecycle,
|
|
841
|
+
requestOptions
|
|
842
|
+
},
|
|
843
|
+
success: true
|
|
844
|
+
};
|
|
845
|
+
}
|
|
671
846
|
function dispatchFailure(err) {
|
|
672
|
-
if (err instanceof RequestAbortedError) return {
|
|
847
|
+
if (err instanceof RequestAbortedError || err instanceof RequestDeadlineExceededError) return {
|
|
673
848
|
err,
|
|
674
849
|
success: false
|
|
675
850
|
};
|
|
@@ -678,20 +853,20 @@ function dispatchFailure(err) {
|
|
|
678
853
|
/**
|
|
679
854
|
* Resolves the per-request {@link RequestConfig}. Upload requests
|
|
680
855
|
* ({@link isUploadRequest}) carry no default timeout: a multi-megabyte place
|
|
681
|
-
* file over a slow link is bandwidth-bound, so a
|
|
856
|
+
* file over a slow link is bandwidth-bound, so a transport-attempt timeout only
|
|
682
857
|
* fires spuriously. An explicit `options.timeout` still applies to any
|
|
683
858
|
* request; every non-upload request keeps the merged default.
|
|
684
859
|
*
|
|
685
860
|
* @param inputs - The merged config, the built request, and per-request overrides.
|
|
686
861
|
* @returns The config to hand to the transport, with `timeout` omitted when
|
|
687
|
-
* no
|
|
862
|
+
* no transport-attempt timeout should apply.
|
|
688
863
|
*/
|
|
689
|
-
function buildRequestConfig({ merged, options, request }) {
|
|
864
|
+
function buildRequestConfig({ merged, options, request, signal }) {
|
|
690
865
|
const shouldOmitDefaultTimeout = options?.timeout === void 0 && isUploadRequest(request);
|
|
691
866
|
return {
|
|
692
867
|
apiKey: merged.apiKey,
|
|
693
868
|
baseUrl: merged.baseUrl,
|
|
694
|
-
...
|
|
869
|
+
...signal === void 0 ? {} : { signal },
|
|
695
870
|
...shouldOmitDefaultTimeout ? {} : { timeout: merged.timeout }
|
|
696
871
|
};
|
|
697
872
|
}
|
|
@@ -711,7 +886,14 @@ function enrichPermissionError(err, spec) {
|
|
|
711
886
|
statusCode: err.statusCode
|
|
712
887
|
});
|
|
713
888
|
}
|
|
889
|
+
function finishRequest({ httpResult, lifecycle, spec }) {
|
|
890
|
+
if (httpResult.success) return spec.parse(httpResult.data);
|
|
891
|
+
return {
|
|
892
|
+
err: deadlineFailureFromError(httpResult.err, lifecycle) ?? enrichPermissionError(httpResult.err, spec),
|
|
893
|
+
success: false
|
|
894
|
+
};
|
|
895
|
+
}
|
|
714
896
|
//#endregion
|
|
715
|
-
export {
|
|
897
|
+
export { observeAdmissionWaitAsync as a, isDateTimeString as c, waitDeadlineFailure as i, okRequest as n, toJsonDetails as o, parseEmptyResponse as r, isRecord as s, ResourceClient as t };
|
|
716
898
|
|
|
717
|
-
//# sourceMappingURL=resource-client
|
|
899
|
+
//# sourceMappingURL=resource-client--yYybltk.mjs.map
|