@limitry/cli 1.0.0-rc.8 → 1.0.0-rc.9
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/CHANGELOG.md +40 -0
- package/dist/limitry.mjs +265 -146
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,46 @@
|
|
|
2
2
|
|
|
3
3
|
What changed in the Limitry CLI (`@limitry/cli`). Generated from the source on each release; newest first.
|
|
4
4
|
|
|
5
|
+
## 1.0.0-rc.9 (2026-10-22)
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- `POST /v1/checks` can now answer `503` with the code `busy` when
|
|
10
|
+
Limitry is deciding too many checks for those subjects at once. Nothing
|
|
11
|
+
was charged. `guard` and `reserve` (TypeScript and Python) send the
|
|
12
|
+
check again with the same key, within the same 30 seconds they give
|
|
13
|
+
`moving`, before the limit's fail mode decides. Each retry of `busy` or
|
|
14
|
+
`moving` now waits the answer's `Retry-After` plus a random backoff that
|
|
15
|
+
grows with each try, so clients don't all come back at the same moment.
|
|
16
|
+
Older SDK versions treat `busy` as Limitry being unreachable after their
|
|
17
|
+
usual two quick retries, so the fail mode decides; upgrade to retry it.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- A very busy workspace's counts are now spread over more of Limitry's
|
|
22
|
+
servers as its traffic grows. While some of them move, which takes a few
|
|
23
|
+
seconds, checks, commits and releases on those subjects answer `503`
|
|
24
|
+
with the new error code `moving` and `Retry-After: 1`; nothing is
|
|
25
|
+
counted. `guard` and `reserve` (TypeScript and Python) and the CLI send
|
|
26
|
+
the same request again with the same Idempotency-Key for up to 30
|
|
27
|
+
seconds, each try with its own time budget, before a limit's fail mode
|
|
28
|
+
applies, so work is neither run uncounted nor left uncommitted.
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
|
|
32
|
+
- A reservation (`mode: "reserve"`) now starts a subject's period, as a
|
|
33
|
+
plain check does: on a `billing_date` limit with no billing date set
|
|
34
|
+
yet, the first reservation's time becomes the subject's billing date,
|
|
35
|
+
and on a `first_use` limit it starts the period. Before, reservations
|
|
36
|
+
made before the subject's first commit could each count in a period of
|
|
37
|
+
their own, so a monthly limit could let more through than its amount.
|
|
38
|
+
A reservation you release still starts the period, with nothing used in
|
|
39
|
+
it.
|
|
40
|
+
- A `503 moving` answer with `Retry-After: 0` is now retried after at least
|
|
41
|
+
a second, not at once, and still for at most 30 seconds. A client made
|
|
42
|
+
with `maxRetries: 0` no longer sends the SDK's internal
|
|
43
|
+
`x-limitry-moving` header to the API.
|
|
44
|
+
|
|
5
45
|
## 1.0.0-rc.8 (2026-10-21)
|
|
6
46
|
|
|
7
47
|
### Added
|
package/dist/limitry.mjs
CHANGED
|
@@ -106,6 +106,13 @@ var brand = {
|
|
|
106
106
|
/** The support address the /contact page shows. Defaults to support@<domain>. */
|
|
107
107
|
supportContact: optional.supportContact ?? `support@${brand_default.domain}`,
|
|
108
108
|
colors: brand_default.colors,
|
|
109
|
+
/**
|
|
110
|
+
* True once the product has launched (brand.json `launched`, set at
|
|
111
|
+
* launch, never back): its data is customers', so the pre-launch
|
|
112
|
+
* product reset's `/ops` route is not mounted at all
|
|
113
|
+
* (docs/deployment.md "Resetting product data"). False when absent.
|
|
114
|
+
*/
|
|
115
|
+
launched: optional.launched === true,
|
|
109
116
|
/** True once the product has its own brand (brand.json `status: "ready"`), not the platform's placeholders. */
|
|
110
117
|
ready: brand_default.status === "ready"
|
|
111
118
|
};
|
|
@@ -690,66 +697,235 @@ function apiErrorFrom(error, response) {
|
|
|
690
697
|
return new ApiError(response.status, envelope.error?.code ?? "http_error", envelope.error?.message ?? `HTTP ${response.status}`, envelope.error?.requestId);
|
|
691
698
|
}
|
|
692
699
|
|
|
700
|
+
// node_modules/@limitry/sdk/dist/retry.js
|
|
701
|
+
var WRITES = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
702
|
+
var RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
703
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
704
|
+
var MAX_WAIT_MS = 2e4;
|
|
705
|
+
var MOVING_MS = 3e4;
|
|
706
|
+
var MOVING_HEADER = "x-limitry-moving";
|
|
707
|
+
var JITTER_BASE_MS = 250;
|
|
708
|
+
var JITTER_CAP_MS = 4e3;
|
|
709
|
+
var MAX_BASE_KEY = 200;
|
|
710
|
+
var wait = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
711
|
+
function backoff(attempt) {
|
|
712
|
+
const base = Math.min(500 * 2 ** attempt, 8e3);
|
|
713
|
+
return base * (0.75 + Math.random() * 0.25);
|
|
714
|
+
}
|
|
715
|
+
function retryAfter(response) {
|
|
716
|
+
const value = response.headers.get("retry-after");
|
|
717
|
+
if (!value)
|
|
718
|
+
return void 0;
|
|
719
|
+
const seconds = Number(value);
|
|
720
|
+
if (Number.isFinite(seconds))
|
|
721
|
+
return Math.max(0, seconds * 1e3);
|
|
722
|
+
const date = Date.parse(value);
|
|
723
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
724
|
+
}
|
|
725
|
+
function retryWaitMs(retryAfterMs2, attempt, random = Math.random) {
|
|
726
|
+
const floor = Math.max(1e3, retryAfterMs2 ?? 1e3);
|
|
727
|
+
const ceiling = Math.min(JITTER_CAP_MS, JITTER_BASE_MS * 2 ** attempt);
|
|
728
|
+
return floor + random() * ceiling;
|
|
729
|
+
}
|
|
730
|
+
async function retryLater(response) {
|
|
731
|
+
if (response.status !== 503)
|
|
732
|
+
return null;
|
|
733
|
+
try {
|
|
734
|
+
const body = await response.clone().json();
|
|
735
|
+
const code2 = body.error?.code;
|
|
736
|
+
return code2 === "moving" || code2 === "busy" ? code2 : null;
|
|
737
|
+
} catch {
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
async function retryable(response) {
|
|
742
|
+
if (RETRY_STATUSES.has(response.status))
|
|
743
|
+
return true;
|
|
744
|
+
if (response.status !== 409)
|
|
745
|
+
return false;
|
|
746
|
+
try {
|
|
747
|
+
const body = await response.clone().json();
|
|
748
|
+
return body.error?.code === "idempotency_in_progress";
|
|
749
|
+
} catch {
|
|
750
|
+
return false;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
function withoutMovingHeader(fetch2) {
|
|
754
|
+
return (request) => {
|
|
755
|
+
if (!request.headers.has(MOVING_HEADER))
|
|
756
|
+
return fetch2(request);
|
|
757
|
+
const headers = new Headers(request.headers);
|
|
758
|
+
headers.delete(MOVING_HEADER);
|
|
759
|
+
return fetch2(new Request(request, { headers }));
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
function withRetries(fetch2, options = {}) {
|
|
763
|
+
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
764
|
+
const sleep = options.sleep ?? wait;
|
|
765
|
+
const random = options.random ?? Math.random;
|
|
766
|
+
return async (request) => {
|
|
767
|
+
const returnMoving = request.headers.get(MOVING_HEADER) === "return";
|
|
768
|
+
if (request.headers.has(MOVING_HEADER)) {
|
|
769
|
+
const headers = new Headers(request.headers);
|
|
770
|
+
headers.delete(MOVING_HEADER);
|
|
771
|
+
request = new Request(request, { headers });
|
|
772
|
+
}
|
|
773
|
+
if (WRITES.has(request.method) && !request.headers.has("idempotency-key")) {
|
|
774
|
+
const url = new URL(request.url);
|
|
775
|
+
const key = options.key ? await options.key(request.method, url.pathname) : crypto.randomUUID();
|
|
776
|
+
const headers = new Headers(request.headers);
|
|
777
|
+
headers.set("idempotency-key", key);
|
|
778
|
+
request = new Request(request, { headers });
|
|
779
|
+
}
|
|
780
|
+
let movingWaited = 0;
|
|
781
|
+
let movingTries = 0;
|
|
782
|
+
for (let attempt = 0; ; attempt++) {
|
|
783
|
+
const last = attempt >= maxRetries;
|
|
784
|
+
const sending = request.clone();
|
|
785
|
+
let response;
|
|
786
|
+
try {
|
|
787
|
+
response = await fetch2(sending);
|
|
788
|
+
} catch (error) {
|
|
789
|
+
if (last || request.signal.aborted)
|
|
790
|
+
throw error;
|
|
791
|
+
await sleep(backoff(attempt));
|
|
792
|
+
continue;
|
|
793
|
+
}
|
|
794
|
+
const later = await retryLater(response);
|
|
795
|
+
if (later && returnMoving)
|
|
796
|
+
return response;
|
|
797
|
+
if (later) {
|
|
798
|
+
const delay2 = retryWaitMs(retryAfter(response), movingTries++, random);
|
|
799
|
+
if (movingWaited + delay2 > MOVING_MS)
|
|
800
|
+
return response;
|
|
801
|
+
movingWaited += delay2;
|
|
802
|
+
await response.body?.cancel();
|
|
803
|
+
await sleep(delay2);
|
|
804
|
+
attempt--;
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
if (last || !await retryable(response))
|
|
808
|
+
return response;
|
|
809
|
+
const delay = retryAfter(response) ?? backoff(attempt);
|
|
810
|
+
if (delay > MAX_WAIT_MS)
|
|
811
|
+
return response;
|
|
812
|
+
await response.body?.cancel();
|
|
813
|
+
await sleep(delay);
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
var hex = (bytes) => Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
818
|
+
function keysFrom(base) {
|
|
819
|
+
if (base !== void 0 && (base.length === 0 || base.length > MAX_BASE_KEY)) {
|
|
820
|
+
throw new Error(`the idempotency key must be 1\u2013${MAX_BASE_KEY} characters`);
|
|
821
|
+
}
|
|
822
|
+
if (base !== void 0 && !/^[\x21-\x7e]+$/.test(base)) {
|
|
823
|
+
throw new Error("the idempotency key must be printable ASCII with no spaces");
|
|
824
|
+
}
|
|
825
|
+
const seen = /* @__PURE__ */ new Map();
|
|
826
|
+
return async (method, path2) => {
|
|
827
|
+
if (base === void 0)
|
|
828
|
+
return crypto.randomUUID();
|
|
829
|
+
const target = `${method} ${path2}`;
|
|
830
|
+
const n = (seen.get(target) ?? 0) + 1;
|
|
831
|
+
seen.set(target, n);
|
|
832
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${target}#${n}`));
|
|
833
|
+
return `${base}:${hex(digest).slice(0, 16)}`;
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
|
|
693
837
|
// node_modules/@limitry/sdk/dist/product/limits.js
|
|
694
838
|
var MAX_COST = 1e9;
|
|
695
839
|
function checkedCost(units) {
|
|
696
840
|
if (!Number.isInteger(units) || units < 0 || units > MAX_COST) {
|
|
697
|
-
throw new RangeError(`
|
|
841
|
+
throw new RangeError(`reservation.cost(${units}): the cost must be a whole number from 0 to ${MAX_COST}`);
|
|
698
842
|
}
|
|
699
843
|
return units;
|
|
700
844
|
}
|
|
701
|
-
var
|
|
845
|
+
var LimitReached = class extends Error {
|
|
702
846
|
reason;
|
|
703
|
-
/** The limit that refused; null
|
|
704
|
-
|
|
847
|
+
/** The limit that refused; null for too many open reservations. */
|
|
848
|
+
limitId;
|
|
705
849
|
retryAfterSeconds;
|
|
706
850
|
decision;
|
|
707
851
|
constructor(decision) {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
this.name = "LimitExceeded";
|
|
714
|
-
this.reason = reason;
|
|
715
|
-
this.limit = decision?.reason?.limit ?? null;
|
|
716
|
-
this.retryAfterSeconds = decision?.retryAfterSeconds ?? null;
|
|
852
|
+
super(decision.reason?.message ?? "Refused");
|
|
853
|
+
this.name = "LimitReached";
|
|
854
|
+
this.reason = decision.reason?.code ?? "limit_reached";
|
|
855
|
+
this.limitId = decision.reason?.limitId ?? null;
|
|
856
|
+
this.retryAfterSeconds = decision.retryAfterSeconds ?? null;
|
|
717
857
|
this.decision = decision;
|
|
718
858
|
}
|
|
719
859
|
};
|
|
860
|
+
var Unavailable = class extends Error {
|
|
861
|
+
constructor() {
|
|
862
|
+
super("Limitry could not be reached and a limit refuses when unreachable");
|
|
863
|
+
this.name = "Unavailable";
|
|
864
|
+
}
|
|
865
|
+
};
|
|
720
866
|
var UNREACHABLE = /* @__PURE__ */ Symbol("unreachable");
|
|
721
867
|
var TIMED_OUT = /* @__PURE__ */ Symbol("timed out");
|
|
722
868
|
var SETTLE_TIMEOUT_MS = 5e3;
|
|
723
|
-
|
|
869
|
+
var wait2 = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
870
|
+
function retryAfterMs(response) {
|
|
871
|
+
const value = response.headers.get("retry-after");
|
|
872
|
+
const seconds = value === null ? NaN : Number(value);
|
|
873
|
+
return Number.isFinite(seconds) ? seconds * 1e3 : void 0;
|
|
874
|
+
}
|
|
875
|
+
function createLimits(client, options = {}, internals = {}) {
|
|
724
876
|
const timeoutMs = options.timeoutMs ?? 1500;
|
|
877
|
+
const sleep = internals.sleep ?? wait2;
|
|
878
|
+
const random = internals.random ?? Math.random;
|
|
725
879
|
const remembered = /* @__PURE__ */ new Map();
|
|
726
880
|
const key = (s, action) => `${s.kind}\0${s.id}\0${action}`;
|
|
727
881
|
const failMode = (s, action) => options.failMode ?? remembered.get(key(s, action)) ?? "open";
|
|
728
882
|
const remember = (s, action, decision) => {
|
|
729
883
|
if (options.failMode)
|
|
730
884
|
return;
|
|
731
|
-
const closed = decision.
|
|
885
|
+
const closed = decision.limits.some((r) => r.failMode === "closed");
|
|
732
886
|
remembered.set(key(s, action), closed ? "closed" : "open");
|
|
733
887
|
if (remembered.size > 1e4) {
|
|
734
888
|
remembered.delete(remembered.keys().next().value);
|
|
735
889
|
}
|
|
736
890
|
};
|
|
737
|
-
async function check(body, idempotencyKey,
|
|
891
|
+
async function check(body, idempotencyKey, wait3 = timeoutMs, finishOnly = false) {
|
|
892
|
+
let waited = 0;
|
|
893
|
+
for (let tries = 0; ; tries++) {
|
|
894
|
+
const answer2 = await checkOnce(body, idempotencyKey, wait3, finishOnly);
|
|
895
|
+
if (answer2.kind !== "later")
|
|
896
|
+
return answer2.value;
|
|
897
|
+
const pause = retryWaitMs(answer2.retryAfterMs, tries, random);
|
|
898
|
+
if (waited + pause > MOVING_MS)
|
|
899
|
+
return UNREACHABLE;
|
|
900
|
+
waited += pause;
|
|
901
|
+
await sleep(pause);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
async function checkOnce(body, idempotencyKey, wait3, finishOnly) {
|
|
738
905
|
let timer;
|
|
739
906
|
const late = new Promise((resolve2) => {
|
|
740
|
-
timer = setTimeout(() => resolve2(TIMED_OUT),
|
|
907
|
+
timer = setTimeout(() => resolve2(TIMED_OUT), wait3);
|
|
741
908
|
});
|
|
742
909
|
const asked = (async () => {
|
|
743
910
|
try {
|
|
744
911
|
const { data, error, response } = await client.POST("/checks", {
|
|
745
912
|
body,
|
|
746
|
-
headers: {
|
|
747
|
-
|
|
913
|
+
headers: {
|
|
914
|
+
"idempotency-key": idempotencyKey,
|
|
915
|
+
// `moving` and `busy` come back here at once: this loop
|
|
916
|
+
// retries them.
|
|
917
|
+
[MOVING_HEADER]: "return",
|
|
918
|
+
...finishOnly ? { "limitry-finish-only": "1" } : {}
|
|
919
|
+
},
|
|
920
|
+
signal: AbortSignal.timeout(wait3)
|
|
748
921
|
});
|
|
749
922
|
if (data)
|
|
750
923
|
return data;
|
|
751
|
-
if (response
|
|
924
|
+
if (isRetryLater(response, error))
|
|
925
|
+
return { later: retryAfterMs(response) };
|
|
926
|
+
if (response.status === 402 || response.status === 429) {
|
|
752
927
|
return UNREACHABLE;
|
|
928
|
+
}
|
|
753
929
|
if (response.status < 500 && error) {
|
|
754
930
|
return new Error(`Limitry refused the check (${response.status}): ${JSON.stringify(error)}`);
|
|
755
931
|
}
|
|
@@ -762,26 +938,41 @@ function createLimits(client, options = {}) {
|
|
|
762
938
|
const answer2 = await Promise.race([asked, late]);
|
|
763
939
|
if (answer2 instanceof Error)
|
|
764
940
|
throw answer2;
|
|
765
|
-
|
|
941
|
+
if (typeof answer2 === "object" && "later" in answer2)
|
|
942
|
+
return { kind: "later", retryAfterMs: answer2.later };
|
|
943
|
+
return { kind: "answer", value: answer2 };
|
|
766
944
|
} finally {
|
|
767
945
|
clearTimeout(timer);
|
|
768
946
|
}
|
|
769
947
|
}
|
|
770
948
|
async function settle(id, action, cost) {
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
949
|
+
const idempotencyKey = crypto.randomUUID();
|
|
950
|
+
let waited = 0;
|
|
951
|
+
for (let tries = 0; ; tries++) {
|
|
952
|
+
let retryMs = null;
|
|
953
|
+
try {
|
|
954
|
+
const headers = {
|
|
955
|
+
"idempotency-key": idempotencyKey,
|
|
956
|
+
[MOVING_HEADER]: "return"
|
|
957
|
+
};
|
|
958
|
+
const { error, response } = action === "commit" ? await client.POST("/reservations/{reservationId}/commit", {
|
|
774
959
|
params: { path: { reservationId: id } },
|
|
775
960
|
body: { cost: cost ?? 0 },
|
|
961
|
+
headers,
|
|
776
962
|
signal: AbortSignal.timeout(SETTLE_TIMEOUT_MS)
|
|
777
|
-
})
|
|
778
|
-
} else {
|
|
779
|
-
await client.POST("/reservations/{reservationId}/release", {
|
|
963
|
+
}) : await client.POST("/reservations/{reservationId}/release", {
|
|
780
964
|
params: { path: { reservationId: id } },
|
|
965
|
+
headers,
|
|
781
966
|
signal: AbortSignal.timeout(SETTLE_TIMEOUT_MS)
|
|
782
967
|
});
|
|
968
|
+
if (isRetryLater(response, error))
|
|
969
|
+
retryMs = retryWaitMs(retryAfterMs(response), tries, random);
|
|
970
|
+
} catch {
|
|
783
971
|
}
|
|
784
|
-
|
|
972
|
+
if (retryMs === null || waited + retryMs > MOVING_MS)
|
|
973
|
+
return;
|
|
974
|
+
waited += retryMs;
|
|
975
|
+
await sleep(retryMs);
|
|
785
976
|
}
|
|
786
977
|
}
|
|
787
978
|
async function releaseUnheard(body, idempotencyKey) {
|
|
@@ -802,8 +993,7 @@ function createLimits(client, options = {}) {
|
|
|
802
993
|
const { data, error, response } = await client.GET("/usage/standing", {
|
|
803
994
|
params: {
|
|
804
995
|
query: {
|
|
805
|
-
|
|
806
|
-
id: request.subject.id,
|
|
996
|
+
subject: `${request.subject.kind}:${request.subject.id}`,
|
|
807
997
|
...request.shared?.length ? {
|
|
808
998
|
shared: request.shared.map((s) => `${s.kind}:${s.id}`).join(",")
|
|
809
999
|
} : {}
|
|
@@ -816,36 +1006,42 @@ function createLimits(client, options = {}) {
|
|
|
816
1006
|
},
|
|
817
1007
|
/** Checks `cost`, runs `fn` only if allowed, and returns its result. */
|
|
818
1008
|
async guard(request, fn) {
|
|
819
|
-
const
|
|
1009
|
+
const body = {
|
|
820
1010
|
subject: request.subject,
|
|
821
1011
|
shared: request.shared,
|
|
822
1012
|
action: request.action,
|
|
823
1013
|
cost: request.cost ?? 1
|
|
824
|
-
}
|
|
1014
|
+
};
|
|
1015
|
+
const idempotencyKey = crypto.randomUUID();
|
|
1016
|
+
const decision = await check(body, idempotencyKey);
|
|
825
1017
|
if (decision === UNREACHABLE || decision === TIMED_OUT) {
|
|
826
1018
|
if (failMode(request.subject, request.action) === "closed") {
|
|
827
|
-
throw new
|
|
1019
|
+
throw new Unavailable();
|
|
1020
|
+
}
|
|
1021
|
+
if (decision === TIMED_OUT) {
|
|
1022
|
+
void check(body, idempotencyKey, SETTLE_TIMEOUT_MS, true).catch(() => {
|
|
1023
|
+
});
|
|
828
1024
|
}
|
|
829
1025
|
return fn();
|
|
830
1026
|
}
|
|
831
1027
|
remember(request.subject, request.action, decision);
|
|
832
1028
|
if (!decision.allowed)
|
|
833
|
-
throw new
|
|
1029
|
+
throw new LimitReached(decision);
|
|
834
1030
|
return fn();
|
|
835
1031
|
},
|
|
836
1032
|
/**
|
|
837
|
-
*
|
|
838
|
-
* (`
|
|
839
|
-
* `fn` throws, and rethrows. A concurrent limit's slot
|
|
840
|
-
* exactly as long as `fn` runs.
|
|
1033
|
+
* Reserves `cost`, runs `fn`, then commits the cost it reported
|
|
1034
|
+
* (`reservation.cost(n)`, default the reserved cost) — or releases the
|
|
1035
|
+
* reservation if `fn` throws, and rethrows. A concurrent limit's slot
|
|
1036
|
+
* is taken for exactly as long as `fn` runs.
|
|
841
1037
|
*/
|
|
842
|
-
async
|
|
843
|
-
const
|
|
1038
|
+
async reserve(request, fn) {
|
|
1039
|
+
const reserved = request.cost ?? 1;
|
|
844
1040
|
const body = {
|
|
845
1041
|
subject: request.subject,
|
|
846
1042
|
shared: request.shared,
|
|
847
1043
|
action: request.action,
|
|
848
|
-
cost:
|
|
1044
|
+
cost: reserved,
|
|
849
1045
|
mode: "reserve",
|
|
850
1046
|
ttlSeconds: request.ttlSeconds
|
|
851
1047
|
};
|
|
@@ -862,7 +1058,7 @@ function createLimits(client, options = {}) {
|
|
|
862
1058
|
const unheard = () => decision === TIMED_OUT ? releaseUnheard(body, idempotencyKey) : Promise.resolve();
|
|
863
1059
|
if (failMode(request.subject, request.action) === "closed") {
|
|
864
1060
|
await unheard();
|
|
865
|
-
throw new
|
|
1061
|
+
throw new Unavailable();
|
|
866
1062
|
}
|
|
867
1063
|
try {
|
|
868
1064
|
return await fn(unmetered);
|
|
@@ -872,11 +1068,11 @@ function createLimits(client, options = {}) {
|
|
|
872
1068
|
}
|
|
873
1069
|
remember(request.subject, request.action, decision);
|
|
874
1070
|
if (!decision.allowed || !decision.reservation) {
|
|
875
|
-
throw new
|
|
1071
|
+
throw new LimitReached(decision);
|
|
876
1072
|
}
|
|
877
1073
|
const id = decision.reservation.id;
|
|
878
|
-
let used =
|
|
879
|
-
const
|
|
1074
|
+
let used = reserved;
|
|
1075
|
+
const reservation = {
|
|
880
1076
|
cost(units) {
|
|
881
1077
|
used = checkedCost(units);
|
|
882
1078
|
},
|
|
@@ -892,7 +1088,7 @@ function createLimits(client, options = {}) {
|
|
|
892
1088
|
};
|
|
893
1089
|
let result;
|
|
894
1090
|
try {
|
|
895
|
-
result = await fn(
|
|
1091
|
+
result = await fn(reservation);
|
|
896
1092
|
} catch (error) {
|
|
897
1093
|
await settle(id, "release");
|
|
898
1094
|
throw error;
|
|
@@ -902,11 +1098,19 @@ function createLimits(client, options = {}) {
|
|
|
902
1098
|
}
|
|
903
1099
|
};
|
|
904
1100
|
}
|
|
1101
|
+
function isRetryLater(response, error) {
|
|
1102
|
+
const code2 = error?.error?.code;
|
|
1103
|
+
return response.status === 503 && (code2 === "moving" || code2 === "busy");
|
|
1104
|
+
}
|
|
905
1105
|
|
|
906
1106
|
// node_modules/@limitry/sdk/dist/product/index.js
|
|
907
1107
|
var productMethods = (client) => {
|
|
908
1108
|
const limits = createLimits(client);
|
|
909
|
-
return {
|
|
1109
|
+
return {
|
|
1110
|
+
guard: limits.guard,
|
|
1111
|
+
reserve: limits.reserve,
|
|
1112
|
+
standing: limits.standing
|
|
1113
|
+
};
|
|
910
1114
|
};
|
|
911
1115
|
|
|
912
1116
|
// node_modules/@limitry/sdk/dist/resources.js
|
|
@@ -958,20 +1162,20 @@ function resources(client) {
|
|
|
958
1162
|
/** Create a limit */
|
|
959
1163
|
(input, options) => call(client, { "method": "post", "path": "/limits", "query": [], "hasBody": true }, {}, input, options)
|
|
960
1164
|
),
|
|
961
|
-
|
|
962
|
-
/** Get a limit */
|
|
963
|
-
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}", "query": [], "hasBody": false }, { "limitId": limitId }, void 0, options)
|
|
964
|
-
),
|
|
965
|
-
grants: {
|
|
1165
|
+
credits: {
|
|
966
1166
|
create: (
|
|
967
1167
|
/** Add credits to a balance */
|
|
968
|
-
(limitId, input, options) => call(client, { "method": "post", "path": "/limits/{limitId}/
|
|
1168
|
+
(limitId, input, options) => call(client, { "method": "post", "path": "/limits/{limitId}/credits", "query": [], "hasBody": true }, { "limitId": limitId }, input, options)
|
|
969
1169
|
),
|
|
970
1170
|
list: (
|
|
971
|
-
/** List a balance's
|
|
972
|
-
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}/
|
|
1171
|
+
/** List a balance's credits */
|
|
1172
|
+
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}/credits", "query": [], "hasBody": false }, { "limitId": limitId }, void 0, options)
|
|
973
1173
|
)
|
|
974
1174
|
},
|
|
1175
|
+
get: (
|
|
1176
|
+
/** Get a limit */
|
|
1177
|
+
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}", "query": [], "hasBody": false }, { "limitId": limitId }, void 0, options)
|
|
1178
|
+
),
|
|
975
1179
|
list: (
|
|
976
1180
|
/** List limits */
|
|
977
1181
|
(input, options) => call(client, { "method": "get", "path": "/limits", "query": ["limit", "cursor", "subjectKind", "action", "archived"], "hasBody": false }, {}, input, options)
|
|
@@ -1013,18 +1217,18 @@ function resources(client) {
|
|
|
1013
1217
|
(subjectKind, subjectId, input, options) => call(client, { "method": "post", "path": "/subjects/{subjectKind}/{subjectId}/reset", "query": [], "hasBody": true }, { "subjectKind": subjectKind, "subjectId": subjectId }, input, options)
|
|
1014
1218
|
),
|
|
1015
1219
|
update: (
|
|
1016
|
-
/** Set a subject's
|
|
1220
|
+
/** Set a subject's billing date */
|
|
1017
1221
|
(subjectKind, subjectId, input, options) => call(client, { "method": "patch", "path": "/subjects/{subjectKind}/{subjectId}", "query": [], "hasBody": true }, { "subjectKind": subjectKind, "subjectId": subjectId }, input, options)
|
|
1018
1222
|
)
|
|
1019
1223
|
},
|
|
1020
1224
|
usage: {
|
|
1021
1225
|
query: (
|
|
1022
1226
|
/** Get a limit's usage over time */
|
|
1023
|
-
(input, options) => call(client, { "method": "get", "path": "/usage", "query": ["limitId", "
|
|
1227
|
+
(input, options) => call(client, { "method": "get", "path": "/usage", "query": ["limitId", "subject", "from", "to", "granularity"], "hasBody": false }, {}, input, options)
|
|
1024
1228
|
),
|
|
1025
1229
|
standing: (
|
|
1026
1230
|
/** Get where a subject stands */
|
|
1027
|
-
(input, options) => call(client, { "method": "get", "path": "/usage/standing", "query": ["
|
|
1231
|
+
(input, options) => call(client, { "method": "get", "path": "/usage/standing", "query": ["subject", "shared"], "hasBody": false }, {}, input, options)
|
|
1028
1232
|
),
|
|
1029
1233
|
subjects: (
|
|
1030
1234
|
/** List who is using a limit now */
|
|
@@ -1140,92 +1344,6 @@ function resources(client) {
|
|
|
1140
1344
|
};
|
|
1141
1345
|
}
|
|
1142
1346
|
|
|
1143
|
-
// node_modules/@limitry/sdk/dist/retry.js
|
|
1144
|
-
var WRITES = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
1145
|
-
var RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
1146
|
-
var DEFAULT_MAX_RETRIES = 2;
|
|
1147
|
-
var MAX_WAIT_MS = 2e4;
|
|
1148
|
-
var MAX_BASE_KEY = 200;
|
|
1149
|
-
var wait = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
1150
|
-
function backoff(attempt) {
|
|
1151
|
-
const base = Math.min(500 * 2 ** attempt, 8e3);
|
|
1152
|
-
return base * (0.75 + Math.random() * 0.25);
|
|
1153
|
-
}
|
|
1154
|
-
function retryAfter(response) {
|
|
1155
|
-
const value = response.headers.get("retry-after");
|
|
1156
|
-
if (!value)
|
|
1157
|
-
return void 0;
|
|
1158
|
-
const seconds = Number(value);
|
|
1159
|
-
if (Number.isFinite(seconds))
|
|
1160
|
-
return Math.max(0, seconds * 1e3);
|
|
1161
|
-
const date = Date.parse(value);
|
|
1162
|
-
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
1163
|
-
}
|
|
1164
|
-
async function retryable(response) {
|
|
1165
|
-
if (RETRY_STATUSES.has(response.status))
|
|
1166
|
-
return true;
|
|
1167
|
-
if (response.status !== 409)
|
|
1168
|
-
return false;
|
|
1169
|
-
try {
|
|
1170
|
-
const body = await response.clone().json();
|
|
1171
|
-
return body.error?.code === "idempotency_in_progress";
|
|
1172
|
-
} catch {
|
|
1173
|
-
return false;
|
|
1174
|
-
}
|
|
1175
|
-
}
|
|
1176
|
-
function withRetries(fetch2, options = {}) {
|
|
1177
|
-
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
1178
|
-
const sleep = options.sleep ?? wait;
|
|
1179
|
-
return async (request) => {
|
|
1180
|
-
if (WRITES.has(request.method) && !request.headers.has("idempotency-key")) {
|
|
1181
|
-
const url = new URL(request.url);
|
|
1182
|
-
const key = options.key ? await options.key(request.method, url.pathname) : crypto.randomUUID();
|
|
1183
|
-
const headers = new Headers(request.headers);
|
|
1184
|
-
headers.set("idempotency-key", key);
|
|
1185
|
-
request = new Request(request, { headers });
|
|
1186
|
-
}
|
|
1187
|
-
for (let attempt = 0; ; attempt++) {
|
|
1188
|
-
const last = attempt >= maxRetries;
|
|
1189
|
-
const sending = last ? request : request.clone();
|
|
1190
|
-
let response;
|
|
1191
|
-
try {
|
|
1192
|
-
response = await fetch2(sending);
|
|
1193
|
-
} catch (error) {
|
|
1194
|
-
if (last || request.signal.aborted)
|
|
1195
|
-
throw error;
|
|
1196
|
-
await sleep(backoff(attempt));
|
|
1197
|
-
continue;
|
|
1198
|
-
}
|
|
1199
|
-
if (last || !await retryable(response))
|
|
1200
|
-
return response;
|
|
1201
|
-
const delay = retryAfter(response) ?? backoff(attempt);
|
|
1202
|
-
if (delay > MAX_WAIT_MS)
|
|
1203
|
-
return response;
|
|
1204
|
-
await response.body?.cancel();
|
|
1205
|
-
await sleep(delay);
|
|
1206
|
-
}
|
|
1207
|
-
};
|
|
1208
|
-
}
|
|
1209
|
-
var hex = (bytes) => Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
1210
|
-
function keysFrom(base) {
|
|
1211
|
-
if (base !== void 0 && (base.length === 0 || base.length > MAX_BASE_KEY)) {
|
|
1212
|
-
throw new Error(`the idempotency key must be 1\u2013${MAX_BASE_KEY} characters`);
|
|
1213
|
-
}
|
|
1214
|
-
if (base !== void 0 && !/^[\x21-\x7e]+$/.test(base)) {
|
|
1215
|
-
throw new Error("the idempotency key must be printable ASCII with no spaces");
|
|
1216
|
-
}
|
|
1217
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1218
|
-
return async (method, path2) => {
|
|
1219
|
-
if (base === void 0)
|
|
1220
|
-
return crypto.randomUUID();
|
|
1221
|
-
const target = `${method} ${path2}`;
|
|
1222
|
-
const n = (seen.get(target) ?? 0) + 1;
|
|
1223
|
-
seen.set(target, n);
|
|
1224
|
-
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${target}#${n}`));
|
|
1225
|
-
return `${base}:${hex(digest).slice(0, 16)}`;
|
|
1226
|
-
};
|
|
1227
|
-
}
|
|
1228
|
-
|
|
1229
1347
|
// node_modules/@limitry/sdk/dist/webhooks.js
|
|
1230
1348
|
var WebhookVerificationError = class extends Error {
|
|
1231
1349
|
constructor(message) {
|
|
@@ -1320,7 +1438,7 @@ function createBaseClient(options) {
|
|
|
1320
1438
|
const send = options.fetch ?? ((request) => fetch(request));
|
|
1321
1439
|
const client = createClient({
|
|
1322
1440
|
baseUrl: `${origin}/v1`,
|
|
1323
|
-
fetch: options.maxRetries === 0 ? send : withRetries(send, { maxRetries: options.maxRetries })
|
|
1441
|
+
fetch: options.maxRetries === 0 ? withoutMovingHeader(send) : withRetries(send, { maxRetries: options.maxRetries })
|
|
1324
1442
|
});
|
|
1325
1443
|
client.use({
|
|
1326
1444
|
onRequest({ request }) {
|
|
@@ -7834,6 +7952,7 @@ A refusal exits 0: it's an answer. Errors exit 1.`
|
|
|
7834
7952
|
},
|
|
7835
7953
|
{ idempotencyKey: crypto.randomUUID() }
|
|
7836
7954
|
);
|
|
7955
|
+
if (!decision) throw new Error("Limitry answered without a decision");
|
|
7837
7956
|
ctx.io.stdout(options.json ? json(decision) : answer(decision));
|
|
7838
7957
|
}
|
|
7839
7958
|
);
|
|
@@ -7981,7 +8100,7 @@ function tokenEnvVar() {
|
|
|
7981
8100
|
function build(io) {
|
|
7982
8101
|
let actedIn = null;
|
|
7983
8102
|
let sending;
|
|
7984
|
-
const program2 = new Command().name(brand.name).description(`${brand.productName} command line`).version("1.0.0-rc.
|
|
8103
|
+
const program2 = new Command().name(brand.name).description(`${brand.productName} command line`).version("1.0.0-rc.9").addHelpText(
|
|
7985
8104
|
"after",
|
|
7986
8105
|
`
|
|
7987
8106
|
Token: \`${brand.name} login\` stores it in ~/.config/${brand.name}/; ${tokenEnvVar()} overrides.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@limitry/cli",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.9",
|
|
4
4
|
"description": "Limitry command line",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"typecheck": "tsc --noEmit"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@limitry/sdk": "1.0.0-rc.
|
|
20
|
+
"@limitry/sdk": "1.0.0-rc.9",
|
|
21
21
|
"@types/node": "26.6.5",
|
|
22
22
|
"commander": "15.0.0",
|
|
23
23
|
"esbuild": "0.28.2",
|