@vllnt/convex-idempotency 0.1.0-canary.cd0c5d5 → 0.1.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 +2 -2
- package/dist/client/index.d.ts +5 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/types.d.ts +3 -1
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/_generated/component.d.ts +3 -1
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/_generated/server.d.ts +12 -0
- package/dist/component/_generated/server.d.ts.map +1 -1
- package/dist/component/_generated/server.js +2 -0
- package/dist/component/_generated/server.js.map +1 -1
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/crons.d.ts.map +1 -1
- package/dist/component/mutations.d.ts +12 -5
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +33 -6
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.d.ts +1 -1
- package/dist/component/schema.d.ts +10 -6
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +2 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/validators.d.ts +16 -13
- package/dist/component/validators.d.ts.map +1 -1
- package/dist/component/validators.js +2 -2
- package/dist/component/validators.js.map +1 -1
- package/dist/shared.d.ts +2 -0
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +2 -0
- package/dist/shared.js.map +1 -1
- package/package.json +18 -15
- package/src/client/index.ts +6 -2
- package/src/client/types.ts +3 -1
- package/src/component/_generated/component.ts +6 -2
- package/src/component/_generated/server.ts +13 -0
- package/src/component/mutations.ts +37 -6
- package/src/component/schema.ts +2 -0
- package/src/component/validators.ts +2 -1
- package/src/shared.ts +3 -0
package/README.md
CHANGED
|
@@ -82,8 +82,8 @@ export const charge = mutation({
|
|
|
82
82
|
|
|
83
83
|
| Method | Kind | Result |
|
|
84
84
|
|--------|------|--------|
|
|
85
|
-
| `begin(ctx, key, opts?)` | mutation | `{ state: "fresh" } \| { state: "inflight"; expiresAt; retryAfterMs } \| { state: "done"; result? }` |
|
|
86
|
-
| `complete(ctx, key, result?, opts?)` | mutation | `{ recorded: true } \| { recorded: false; reason: "missing" \| "expired" \| "already_done" }` |
|
|
85
|
+
| `begin(ctx, key, opts?)` | mutation | `{ state: "fresh"; claimId } \| { state: "inflight"; expiresAt; retryAfterMs } \| { state: "done"; result? }` |
|
|
86
|
+
| `complete(ctx, key, result?, opts?)` | mutation | `{ recorded: true } \| { recorded: false; reason: "missing" \| "expired" \| "superseded" \| "already_done" }` |
|
|
87
87
|
| `get(ctx, key, scope?)` | query | `{ status, result?, expiresAt } \| null` |
|
|
88
88
|
| `purge(ctx, opts?)` | mutation | `number` (keys removed in the first bounded pass) |
|
|
89
89
|
|
package/dist/client/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { BeginResult, CompleteResult, IdempotencyOptions, KeyState, ResultP
|
|
|
7
7
|
*/
|
|
8
8
|
type RawBegin = {
|
|
9
9
|
state: "fresh";
|
|
10
|
+
claimId: string;
|
|
10
11
|
} | {
|
|
11
12
|
state: "inflight";
|
|
12
13
|
expiresAt: number;
|
|
@@ -32,6 +33,7 @@ export interface IdempotencyComponent {
|
|
|
32
33
|
key: string;
|
|
33
34
|
scope: string;
|
|
34
35
|
result?: unknown;
|
|
36
|
+
claimId?: string;
|
|
35
37
|
doneTtlMs: number;
|
|
36
38
|
upsertOnMissing: boolean;
|
|
37
39
|
}, CompleteResult>;
|
|
@@ -65,6 +67,8 @@ interface BeginOptions {
|
|
|
65
67
|
/** Per-call overrides for `scope`, the done grace window, and lost-claim upsert. */
|
|
66
68
|
interface CompleteOptions {
|
|
67
69
|
scope?: string;
|
|
70
|
+
/** Fencing token returned by the successful `begin` claim. */
|
|
71
|
+
claimId?: string;
|
|
68
72
|
doneTtlMs?: number;
|
|
69
73
|
upsertOnMissing?: boolean;
|
|
70
74
|
}
|
|
@@ -86,7 +90,7 @@ interface CompleteOptions {
|
|
|
86
90
|
* if (r.state === "done") return r.result; // typed replay
|
|
87
91
|
* if (r.state === "inflight") throw new Error(`retry in ${r.retryAfterMs}ms`);
|
|
88
92
|
* const out = await doWork(); // r.state === "fresh"
|
|
89
|
-
* const done = await idem.complete(ctx, requestId, out);
|
|
93
|
+
* const done = await idem.complete(ctx, requestId, out, { claimId: r.claimId });
|
|
90
94
|
* if (!done.recorded) log.warn("claim lost", done.reason); // work ran, row gone
|
|
91
95
|
* ```
|
|
92
96
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,YAAY,EACb,MAAM,YAAY,CAAC;AAQpB;;;;GAIG;AACH,KAAK,QAAQ,GACT;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,YAAY,EACb,MAAM,YAAY,CAAC;AAQpB;;;;GAIG;AACH,KAAK,QAAQ,GACT;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACnC;IAAE,KAAK,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAC9D;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAA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,EAAE,OAAO,EAChB,IAAI,GAAE,eAAoB,GACzB,OAAO,CAAC,cAAc,CAAC,CAWzB;IAED,4EAA4E;IACtE,GAAG,CACP,GAAG,EAAE,WAAW,EAChB,GAAG,EAAE,MAAM,EACX,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAanC;IAED;;;;OAIG;IACH,KAAK,CACH,GAAG,EAAE,cAAc,EACnB,IAAI,GAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAO,GAC7C,OAAO,CAAC,MAAM,CAAC,CAKjB;CACF;AAED,YAAY,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,YAAY,GACb,CAAC"}
|
package/dist/client/index.js
CHANGED
|
@@ -17,7 +17,7 @@ import { DEFAULT_DONE_TTL_MS, DEFAULT_INFLIGHT_TTL_MS, DEFAULT_PURGE_BATCH, DEFA
|
|
|
17
17
|
* if (r.state === "done") return r.result; // typed replay
|
|
18
18
|
* if (r.state === "inflight") throw new Error(`retry in ${r.retryAfterMs}ms`);
|
|
19
19
|
* const out = await doWork(); // r.state === "fresh"
|
|
20
|
-
* const done = await idem.complete(ctx, requestId, out);
|
|
20
|
+
* const done = await idem.complete(ctx, requestId, out, { claimId: r.claimId });
|
|
21
21
|
* if (!done.recorded) log.warn("claim lost", done.reason); // work ran, row gone
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
@@ -83,6 +83,7 @@ export class Idempotency {
|
|
|
83
83
|
key,
|
|
84
84
|
scope: this.scopeOf(opts.scope),
|
|
85
85
|
result: validated,
|
|
86
|
+
claimId: opts.claimId,
|
|
86
87
|
doneTtlMs: opts.doneTtlMs ?? this.defaultDoneTtlMs,
|
|
87
88
|
upsertOnMissing: opts.upsertOnMissing ?? this.defaultUpsertOnMissing,
|
|
88
89
|
});
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAYA,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,mBAAmB,EACnB,aAAa,GACd,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAYA,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,mBAAmB,EACnB,aAAa,GACd,MAAM,cAAc,CAAC;AAyFtB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,WAAW;IAQH,SAAS;IAPX,YAAY,CAAS;IACrB,oBAAoB,CAAS;IAC7B,gBAAgB,CAAS;IACzB,sBAAsB,CAAU;IAChC,eAAe,CAAoC;IAEpE,YACmB,SAA+B,EAChD,OAAO,GAAgC,EAAE;yBADxB,SAAS;QAG1B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,aAAa,CAAC;QAC1D,IAAI,CAAC,oBAAoB;YACvB,OAAO,CAAC,oBAAoB,IAAI,uBAAuB,CAAC;QAC1D,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,mBAAmB,CAAC;QACxE,IAAI,CAAC,sBAAsB,GAAG,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC;QAC/D,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IACjD,CAAC;IAEO,OAAO,CAAC,KAAyB;QACvC,OAAO,KAAK,IAAI,IAAI,CAAC,YAAY,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACK,WAAW,CAAC,KAAc;QAChC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,IAAI,IAAI,CAAC,eAAe,KAAK,SAAS,EAAE,CAAC;YACvC,OAAO,KAAgB,CAAC;QAC1B,CAAC;QACD,OAAO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E,KAAK,CAAC,GAAG,CACP,GAAgB,EAChB,GAAW,EACX,KAAc;QAEd,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE;YACzD,GAAG;YACH,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;SAC3B,CAAC,CAAC;QACH,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO;YACL,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,MAAM,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC;YACpC,SAAS,EAAE,GAAG,CAAC,SAAS;SACzB,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,KAAK,CACH,GAAmB,EACnB,IAAI,GAAwC,EAAE;QAE9C,OAAO,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE;YACrD,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,mBAAmB;SACzC,CAAC,CAAC;IACL,CAAC;CACF"}
|
package/dist/client/types.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type ResultParser<TResult> = (value: unknown) => TResult;
|
|
|
12
12
|
export type BeginResult<TResult> = {
|
|
13
13
|
/** The key was just minted; do the work, then call `complete`. */
|
|
14
14
|
state: "fresh";
|
|
15
|
+
/** Opaque fencing token that must be passed to `complete`. */
|
|
16
|
+
claimId: string;
|
|
15
17
|
} | {
|
|
16
18
|
/** Another attempt holds the key; do not re-run. */
|
|
17
19
|
state: "inflight";
|
|
@@ -36,7 +38,7 @@ export type CompleteResult = {
|
|
|
36
38
|
* `missing` — no key for this id/scope. `expired` — the inflight lease
|
|
37
39
|
* lapsed before completion. `already_done` — a prior attempt won the race.
|
|
38
40
|
*/
|
|
39
|
-
reason: "missing" | "expired" | "already_done";
|
|
41
|
+
reason: "missing" | "expired" | "superseded" | "already_done";
|
|
40
42
|
};
|
|
41
43
|
/** Construction options for the {@link Idempotency} client. */
|
|
42
44
|
export interface IdempotencyOptions<TResult> {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAE5D;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,CAAC,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;AAEhE,sDAAsD;AACtD,MAAM,MAAM,WAAW,CAAC,OAAO,IAC3B;IACE,kEAAkE;IAClE,KAAK,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAE5D;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,CAAC,OAAO,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;AAEhE,sDAAsD;AACtD,MAAM,MAAM,WAAW,CAAC,OAAO,IAC3B;IACE,kEAAkE;IAClE,KAAK,EAAE,OAAO,CAAC;IACf,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;CACjB,GACD;IACE,oDAAoD;IACpD,KAAK,EAAE,UAAU,CAAC;IAClB,2DAA2D;IAC3D,SAAS,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,YAAY,EAAE,MAAM,CAAC;CACtB,GACD;IACE,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAC;IACd,8DAA8D;IAC9D,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AAEN,6FAA6F;AAC7F,MAAM,MAAM,cAAc,GACtB;IACE,6CAA6C;IAC7C,QAAQ,EAAE,IAAI,CAAC;CAChB,GACD;IACE,oEAAoE;IACpE,QAAQ,EAAE,KAAK,CAAC;IAChB;;;OAGG;IACH,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,YAAY,GAAG,cAAc,CAAC;CAC/D,CAAC;AAEN,+DAA+D;AAC/D,MAAM,WAAW,kBAAkB,CAAC,OAAO;IACzC,uEAAuE;IACvE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;OAIG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,eAAe,CAAC,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;CACzC;AAED,oEAAoE;AACpE,MAAM,WAAW,QAAQ,CAAC,OAAO;IAC/B,wEAAwE;IACxE,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC;IAC5B,kEAAkE;IAClE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,kEAAkE;IAClE,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
|
@@ -25,6 +25,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
25
25
|
key: string;
|
|
26
26
|
scope: string;
|
|
27
27
|
}, {
|
|
28
|
+
claimId: string;
|
|
28
29
|
state: "fresh";
|
|
29
30
|
} | {
|
|
30
31
|
expiresAt: number;
|
|
@@ -35,6 +36,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
35
36
|
state: "done";
|
|
36
37
|
}, Name>;
|
|
37
38
|
complete: FunctionReference<"mutation", "internal", {
|
|
39
|
+
claimId?: string;
|
|
38
40
|
doneTtlMs: number;
|
|
39
41
|
key: string;
|
|
40
42
|
result?: any;
|
|
@@ -43,7 +45,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
43
45
|
}, {
|
|
44
46
|
recorded: true;
|
|
45
47
|
} | {
|
|
46
|
-
reason: "missing" | "expired" | "already_done";
|
|
48
|
+
reason: "missing" | "expired" | "superseded" | "already_done";
|
|
47
49
|
recorded: false;
|
|
48
50
|
}, Name>;
|
|
49
51
|
purge: FunctionReference<"mutation", "internal", {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../../../src/component/_generated/component.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,YAAY,CAAC,IAAI,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,IAC3E;IACE,SAAS,EAAE;QACT,KAAK,EAAE,iBAAiB,CACtB,UAAU,EACV,UAAU,EACV;YAAE,aAAa,EAAE,MAAM,CAAC;YAAC,GAAG,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EACnD;YAAE,KAAK,EAAE,OAAO,CAAA;SAAE,
|
|
1
|
+
{"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../../../src/component/_generated/component.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,YAAY,CAAC,IAAI,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,IAC3E;IACE,SAAS,EAAE;QACT,KAAK,EAAE,iBAAiB,CACtB,UAAU,EACV,UAAU,EACV;YAAE,aAAa,EAAE,MAAM,CAAC;YAAC,GAAG,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EACnD;YAAE,OAAO,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,OAAO,CAAA;SAAE,GACnC;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,YAAY,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,UAAU,CAAA;SAAE,GAC9D;YAAE,MAAM,CAAC,EAAE,GAAG,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EACjC,IAAI,CACL,CAAC;QACF,QAAQ,EAAE,iBAAiB,CACzB,UAAU,EACV,UAAU,EACV;YACE,OAAO,CAAC,EAAE,MAAM,CAAC;YACjB,SAAS,EAAE,MAAM,CAAC;YAClB,GAAG,EAAE,MAAM,CAAC;YACZ,MAAM,CAAC,EAAE,GAAG,CAAC;YACb,KAAK,EAAE,MAAM,CAAC;YACd,eAAe,EAAE,OAAO,CAAC;SAC1B,EACC;YAAE,QAAQ,EAAE,IAAI,CAAA;SAAE,GAClB;YACE,MAAM,EAAE,SAAS,GAAG,SAAS,GAAG,YAAY,GAAG,cAAc,CAAC;YAC9D,QAAQ,EAAE,KAAK,CAAC;SACjB,EACH,IAAI,CACL,CAAC;QACF,KAAK,EAAE,iBAAiB,CACtB,UAAU,EACV,UAAU,EACV;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,MAAM,CAAA;SAAE,EAClC,MAAM,EACN,IAAI,CACL,CAAC;KACH,CAAC;IACF,OAAO,EAAE;QACP,GAAG,EAAE,iBAAiB,CACpB,OAAO,EACP,UAAU,EACV;YAAE,GAAG,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EAC9B,IAAI,GAAG;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,GAAG,CAAC;YAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAA;SAAE,EACvE,IAAI,CACL,CAAC;KACH,CAAC;CACH,CAAC"}
|
|
@@ -8,6 +8,16 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import type { ActionBuilder, HttpActionBuilder, MutationBuilder, QueryBuilder, GenericActionCtx, GenericMutationCtx, GenericQueryCtx, GenericDatabaseReader, GenericDatabaseWriter } from "convex/server";
|
|
10
10
|
import type { DataModel } from "./dataModel.js";
|
|
11
|
+
/**
|
|
12
|
+
* Typesafe environment variables.
|
|
13
|
+
*
|
|
14
|
+
* This includes platform-provided env vars and any variables declared in
|
|
15
|
+
* `convex.config.ts`.
|
|
16
|
+
*/
|
|
17
|
+
type Env = {
|
|
18
|
+
readonly CONVEX_CLOUD_URL: string;
|
|
19
|
+
readonly CONVEX_SITE_URL: string;
|
|
20
|
+
};
|
|
11
21
|
/**
|
|
12
22
|
* Define a query in this Convex app's public API.
|
|
13
23
|
*
|
|
@@ -75,6 +85,7 @@ export declare const internalAction: ActionBuilder<DataModel, "internal">;
|
|
|
75
85
|
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
76
86
|
*/
|
|
77
87
|
export declare const httpAction: HttpActionBuilder;
|
|
88
|
+
export declare const env: Env;
|
|
78
89
|
/**
|
|
79
90
|
* A set of services for use within Convex query functions.
|
|
80
91
|
*
|
|
@@ -118,4 +129,5 @@ export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
|
118
129
|
* for the guarantees Convex provides your functions.
|
|
119
130
|
*/
|
|
120
131
|
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
132
|
+
export {};
|
|
121
133
|
//# sourceMappingURL=server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../../src/component/_generated/server.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EACtB,MAAM,eAAe,CAAC;AAUvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,EAAE,YAAY,CAAC,SAAS,EAAE,QAAQ,CAAgB,CAAC;AAErE;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,EAAE,YAAY,CAAC,SAAS,EAAE,UAAU,CACxC,CAAC;AAEvB;;;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AAS,EAAE,UAAU,CAC3C,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM,EAAE,aAAa,CAAC,SAAS,EAAE,QAAQ,CAAiB,CAAC;AAExE;;;;;GAKG;AACH,eAAO,MAAM,cAAc,EAAE,aAAa,CAAC,SAAS,EAAE,UAAU,CACzC,CAAC;AAExB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,iBAAqC,CAAC;AAC/D,eAAO,MAAM,GAAG,EAAE,GACJ,CAAC;AAEf;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;AAExD;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC;AAE9D;;;;;;;;GAQG;AACH,MAAM,MAAM,cAAc,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC"}
|
|
@@ -75,4 +75,6 @@ export const internalAction = internalActionGeneric;
|
|
|
75
75
|
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
76
76
|
*/
|
|
77
77
|
export const httpAction = httpActionGeneric;
|
|
78
|
+
export const env = globalThis
|
|
79
|
+
.process.env;
|
|
78
80
|
//# sourceMappingURL=server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/component/_generated/server.ts"],"names":[],"mappings":"AAAA,oBAAoB;AACpB;;;;;;;GAOG;AAaH,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,qBAAqB,EACrB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/component/_generated/server.ts"],"names":[],"mappings":"AAAA,oBAAoB;AACpB;;;;;;;GAOG;AAaH,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,qBAAqB,EACrB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,eAAe,CAAC;AAcvB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,KAAK,GAAsC,YAAY,CAAC;AAErE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,aAAa,GACxB,oBAAoB,CAAC;AAEvB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAyC,eAAe,CAAC;AAE9E;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAC3B,uBAAuB,CAAC;AAE1B;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,MAAM,GAAuC,aAAa,CAAC;AAExE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GACzB,qBAAqB,CAAC;AAExB;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,UAAU,GAAsB,iBAAiB,CAAC;AAC/D,MAAM,CAAC,MAAM,GAAG,GAAS,UAAmD;KACzE,OAAO,CAAC,GAAG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convex.config.d.ts","sourceRoot":"","sources":["../../src/component/convex.config.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,SAAS,sDAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"convex.config.d.ts","sourceRoot":"","sources":["../../src/component/convex.config.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,SAAS,sDAAiC,CAAC;eAMlC,SAAS"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crons.d.ts","sourceRoot":"","sources":["../../src/component/crons.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc
|
|
1
|
+
{"version":3,"file":"crons.d.ts","sourceRoot":"","sources":["../../src/component/crons.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc;aAAK,KAAK,EAAE,EAAE;CAAW,CAAC;AAErD,uEAAuE;AACvE,eAAO,MAAM,WAAW,MAAM,CAAC;AAE/B,QAAA,MAAM,KAAK,+BAAa,CAAC;eAMV,KAAK"}
|
|
@@ -16,24 +16,27 @@
|
|
|
16
16
|
* immediately expiring the claim and breaking the dedup window).
|
|
17
17
|
*/
|
|
18
18
|
export declare const begin: import("convex/server").RegisteredMutation<"public", {
|
|
19
|
+
inflightTtlMs: number;
|
|
19
20
|
key: string;
|
|
20
21
|
scope: string;
|
|
21
|
-
inflightTtlMs: number;
|
|
22
22
|
}, Promise<{
|
|
23
23
|
state: "fresh";
|
|
24
|
+
claimId: `${string}-${string}-${string}-${string}-${string}`;
|
|
24
25
|
result?: undefined;
|
|
25
26
|
expiresAt?: undefined;
|
|
26
27
|
retryAfterMs?: undefined;
|
|
27
28
|
} | {
|
|
29
|
+
claimId?: undefined;
|
|
28
30
|
state: "done";
|
|
29
31
|
result: any;
|
|
30
32
|
expiresAt?: undefined;
|
|
31
33
|
retryAfterMs?: undefined;
|
|
32
34
|
} | {
|
|
35
|
+
claimId?: undefined;
|
|
36
|
+
result?: undefined;
|
|
33
37
|
state: "inflight";
|
|
34
38
|
expiresAt: number;
|
|
35
39
|
retryAfterMs: number;
|
|
36
|
-
result?: undefined;
|
|
37
40
|
}>>;
|
|
38
41
|
/**
|
|
39
42
|
* Mark `key` `done`, recording `result` and extending the grace window by
|
|
@@ -57,10 +60,11 @@ export declare const begin: import("convex/server").RegisteredMutation<"public",
|
|
|
57
60
|
* immediately expiring the done grace window on creation).
|
|
58
61
|
*/
|
|
59
62
|
export declare const complete: import("convex/server").RegisteredMutation<"public", {
|
|
60
|
-
|
|
63
|
+
claimId?: string | undefined;
|
|
64
|
+
doneTtlMs: number;
|
|
61
65
|
key: string;
|
|
66
|
+
result?: any;
|
|
62
67
|
scope: string;
|
|
63
|
-
doneTtlMs: number;
|
|
64
68
|
upsertOnMissing: boolean;
|
|
65
69
|
}, Promise<{
|
|
66
70
|
recorded: true;
|
|
@@ -71,6 +75,9 @@ export declare const complete: import("convex/server").RegisteredMutation<"publi
|
|
|
71
75
|
} | {
|
|
72
76
|
recorded: false;
|
|
73
77
|
reason: "already_done";
|
|
78
|
+
} | {
|
|
79
|
+
recorded: false;
|
|
80
|
+
reason: "superseded";
|
|
74
81
|
} | {
|
|
75
82
|
recorded: false;
|
|
76
83
|
reason: "expired";
|
|
@@ -85,7 +92,7 @@ export declare const complete: import("convex/server").RegisteredMutation<"publi
|
|
|
85
92
|
* re-running only ever removes already-expired rows.
|
|
86
93
|
*/
|
|
87
94
|
export declare const purge: import("convex/server").RegisteredMutation<"public", {
|
|
88
|
-
before?: number | undefined;
|
|
89
95
|
batch: number;
|
|
96
|
+
before?: number | undefined;
|
|
90
97
|
}, Promise<number>>;
|
|
91
98
|
//# sourceMappingURL=mutations.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,KAAK;;;;;WAmCI,OAAO;;;;;;;WAiBP,MAAM;;;;;;;WAIf,UAAU;;;GAKrB,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,QAAQ;;;;;;;;;;;;YAyC4B,SAAS;;;YAIT,cAAc;;;YAOd,YAAY;;;YAYZ,SAAS;GAUxD,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,KAAK;;;mBAwBhB,CAAC"}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { api } from "./_generated/api";
|
|
3
3
|
import { mutation } from "./_generated/server";
|
|
4
|
+
import { MAX_PURGE_BATCH } from "../shared";
|
|
4
5
|
import { beginResult, completeResult, jsonValue } from "./validators";
|
|
6
|
+
function validateBatch(batch) {
|
|
7
|
+
if (!Number.isFinite(batch) || !Number.isInteger(batch) || batch < 1 || batch > MAX_PURGE_BATCH) {
|
|
8
|
+
throw new ConvexError({
|
|
9
|
+
code: "INVALID_BATCH",
|
|
10
|
+
message: `batch must be an integer between 1 and ${MAX_PURGE_BATCH}`,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
5
14
|
/**
|
|
6
15
|
* Claim `key` within `scope`. Time is read from the server (`Date.now()`) inside
|
|
7
16
|
* the handler — never supplied by the caller — so an adversarial clock cannot
|
|
@@ -40,21 +49,28 @@ export const begin = mutation({
|
|
|
40
49
|
.unique();
|
|
41
50
|
const expiresAt = now + args.inflightTtlMs;
|
|
42
51
|
if (existing === null) {
|
|
52
|
+
const claimId = crypto.randomUUID();
|
|
43
53
|
await ctx.db.insert("keys", {
|
|
44
54
|
key: args.key,
|
|
45
55
|
scope: args.scope,
|
|
46
56
|
status: "inflight",
|
|
57
|
+
claimId,
|
|
58
|
+
claimGeneration: 1,
|
|
47
59
|
expiresAt,
|
|
48
60
|
});
|
|
49
|
-
return { state: "fresh" };
|
|
61
|
+
return { state: "fresh", claimId };
|
|
50
62
|
}
|
|
51
63
|
if (existing.expiresAt <= now) {
|
|
52
|
-
|
|
64
|
+
const claimId = crypto.randomUUID();
|
|
65
|
+
await ctx.db.patch("keys", existing._id, {
|
|
53
66
|
status: "inflight",
|
|
67
|
+
claimId,
|
|
68
|
+
// v8 ignore next -- migration fallback for rows written before claim generations existed
|
|
69
|
+
claimGeneration: (existing.claimGeneration ?? 1) + 1,
|
|
54
70
|
result: undefined,
|
|
55
71
|
expiresAt,
|
|
56
72
|
});
|
|
57
|
-
return { state: "fresh" };
|
|
73
|
+
return { state: "fresh", claimId };
|
|
58
74
|
}
|
|
59
75
|
if (existing.status === "done") {
|
|
60
76
|
return { state: "done", result: existing.result };
|
|
@@ -92,6 +108,7 @@ export const complete = mutation({
|
|
|
92
108
|
key: v.string(),
|
|
93
109
|
scope: v.string(),
|
|
94
110
|
result: v.optional(jsonValue),
|
|
111
|
+
claimId: v.optional(v.string()),
|
|
95
112
|
doneTtlMs: v.number(),
|
|
96
113
|
upsertOnMissing: v.boolean(),
|
|
97
114
|
},
|
|
@@ -115,6 +132,8 @@ export const complete = mutation({
|
|
|
115
132
|
key: args.key,
|
|
116
133
|
scope: args.scope,
|
|
117
134
|
status: "done",
|
|
135
|
+
claimId: args.claimId ?? crypto.randomUUID(),
|
|
136
|
+
claimGeneration: 1,
|
|
118
137
|
result: args.result,
|
|
119
138
|
expiresAt,
|
|
120
139
|
});
|
|
@@ -125,9 +144,13 @@ export const complete = mutation({
|
|
|
125
144
|
if (existing.status === "done") {
|
|
126
145
|
return { recorded: false, reason: "already_done" };
|
|
127
146
|
}
|
|
147
|
+
if ((args.claimId !== undefined && args.claimId !== existing.claimId) ||
|
|
148
|
+
(args.claimId === undefined && (existing.claimGeneration ?? 1) > 1)) {
|
|
149
|
+
return { recorded: false, reason: "superseded" };
|
|
150
|
+
}
|
|
128
151
|
if (existing.expiresAt <= now) {
|
|
129
152
|
if (args.upsertOnMissing) {
|
|
130
|
-
await ctx.db.patch(existing._id, {
|
|
153
|
+
await ctx.db.patch("keys", existing._id, {
|
|
131
154
|
status: "done",
|
|
132
155
|
result: args.result,
|
|
133
156
|
expiresAt,
|
|
@@ -136,7 +159,7 @@ export const complete = mutation({
|
|
|
136
159
|
}
|
|
137
160
|
return { recorded: false, reason: "expired" };
|
|
138
161
|
}
|
|
139
|
-
await ctx.db.patch(existing._id, {
|
|
162
|
+
await ctx.db.patch("keys", existing._id, {
|
|
140
163
|
status: "done",
|
|
141
164
|
result: args.result,
|
|
142
165
|
expiresAt,
|
|
@@ -157,13 +180,17 @@ export const purge = mutation({
|
|
|
157
180
|
args: { before: v.optional(v.number()), batch: v.number() },
|
|
158
181
|
returns: v.number(),
|
|
159
182
|
handler: async (ctx, args) => {
|
|
183
|
+
validateBatch(args.batch);
|
|
184
|
+
if (args.before !== undefined && !Number.isFinite(args.before)) {
|
|
185
|
+
throw new ConvexError({ code: "INVALID_BEFORE", message: "before must be finite" });
|
|
186
|
+
}
|
|
160
187
|
const before = args.before ?? Date.now();
|
|
161
188
|
const stale = await ctx.db
|
|
162
189
|
.query("keys")
|
|
163
190
|
.withIndex("by_expires", (q) => q.lt("expiresAt", before))
|
|
164
191
|
.take(args.batch);
|
|
165
192
|
for (const row of stale) {
|
|
166
|
-
await ctx.db.delete(row._id);
|
|
193
|
+
await ctx.db.delete("keys", row._id);
|
|
167
194
|
}
|
|
168
195
|
if (stale.length === args.batch) {
|
|
169
196
|
await ctx.scheduler.runAfter(0, api.mutations.purge, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEtE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,QAAQ,CAAC;IAC5B,IAAI,EAAE;QACJ,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;KAC1B;IACD,OAAO,EAAE,WAAW;IACpB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3B,IAAI,CAAC,CAA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CAAC,CAAC;QACpB,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;YACxB,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;QACvC,CAAC;QACD,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YAChC,MAAM,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE;gBACnD,MAAM;gBACN,KAAK,EAAE,IAAI,CAAC,KAAK;aAClB,CAAC,CAAC;QACL,CAAC;QACD,OAAO,KAAK,CAAC,MAAM,CAAC;IACtB,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -6,20 +6,24 @@
|
|
|
6
6
|
*/
|
|
7
7
|
declare const _default: import("convex/server").SchemaDefinition<{
|
|
8
8
|
keys: import("convex/server").TableDefinition<import("convex/values").VObject<{
|
|
9
|
-
|
|
9
|
+
claimGeneration?: number | undefined;
|
|
10
|
+
claimId?: string | undefined;
|
|
11
|
+
expiresAt: number;
|
|
10
12
|
key: string;
|
|
13
|
+
result?: any;
|
|
11
14
|
scope: string;
|
|
12
|
-
status: "
|
|
13
|
-
expiresAt: number;
|
|
15
|
+
status: "done" | "inflight";
|
|
14
16
|
}, {
|
|
15
17
|
key: import("convex/values").VString<string, "required">;
|
|
16
18
|
scope: import("convex/values").VString<string, "required">;
|
|
17
|
-
status: import("convex/values").VUnion<"
|
|
19
|
+
status: import("convex/values").VUnion<"done" | "inflight", [import("convex/values").VLiteral<"inflight", "required">, import("convex/values").VLiteral<"done", "required">], "required", never>;
|
|
20
|
+
claimId: import("convex/values").VString<string | undefined, "optional">;
|
|
21
|
+
claimGeneration: import("convex/values").VFloat64<number | undefined, "optional">;
|
|
18
22
|
result: import("convex/values").VAny<any, "optional", string>;
|
|
19
23
|
expiresAt: import("convex/values").VFloat64<number, "required">;
|
|
20
|
-
}, "required", "
|
|
21
|
-
by_scope_key: ["scope", "key", "_creationTime"];
|
|
24
|
+
}, "required", "claimGeneration" | "claimId" | "expiresAt" | "key" | "result" | "scope" | "status" | `result.${string}`>, {
|
|
22
25
|
by_expires: ["expiresAt", "_creationTime"];
|
|
26
|
+
by_scope_key: ["scope", "key", "_creationTime"];
|
|
23
27
|
}, {}, {}>;
|
|
24
28
|
}, true>;
|
|
25
29
|
export default _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAIA;;;;;GAKG
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAIA;;;;;GAKG"}
|
package/dist/component/schema.js
CHANGED
|
@@ -12,6 +12,8 @@ export default defineSchema({
|
|
|
12
12
|
key: v.string(),
|
|
13
13
|
scope: v.string(),
|
|
14
14
|
status: v.union(v.literal("inflight"), v.literal("done")),
|
|
15
|
+
claimId: v.optional(v.string()),
|
|
16
|
+
claimGeneration: v.optional(v.number()),
|
|
15
17
|
result: v.optional(jsonValue),
|
|
16
18
|
expiresAt: v.number(),
|
|
17
19
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;;;GAKG;AACH,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE,WAAW,CAAC;QAChB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE;QACf,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,UA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33 +17,36 @@ export declare const jsonValue: import("convex/values").VAny<any, "required", st
|
|
|
17
17
|
* the recorded outcome for a short-circuit replay.
|
|
18
18
|
*/
|
|
19
19
|
export declare const beginResult: import("convex/values").VUnion<{
|
|
20
|
+
claimId: string;
|
|
20
21
|
state: "fresh";
|
|
21
22
|
} | {
|
|
22
|
-
state: "inflight";
|
|
23
23
|
expiresAt: number;
|
|
24
24
|
retryAfterMs: number;
|
|
25
|
+
state: "inflight";
|
|
25
26
|
} | {
|
|
26
27
|
result?: any;
|
|
27
28
|
state: "done";
|
|
28
29
|
}, [import("convex/values").VObject<{
|
|
30
|
+
claimId: string;
|
|
29
31
|
state: "fresh";
|
|
30
32
|
}, {
|
|
31
33
|
state: import("convex/values").VLiteral<"fresh", "required">;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
claimId: import("convex/values").VString<string, "required">;
|
|
35
|
+
}, "required", "claimId" | "state">, import("convex/values").VObject<{
|
|
34
36
|
expiresAt: number;
|
|
35
37
|
retryAfterMs: number;
|
|
38
|
+
state: "inflight";
|
|
36
39
|
}, {
|
|
37
40
|
state: import("convex/values").VLiteral<"inflight", "required">;
|
|
38
41
|
expiresAt: import("convex/values").VFloat64<number, "required">;
|
|
39
42
|
retryAfterMs: import("convex/values").VFloat64<number, "required">;
|
|
40
|
-
}, "required", "
|
|
43
|
+
}, "required", "expiresAt" | "retryAfterMs" | "state">, import("convex/values").VObject<{
|
|
41
44
|
result?: any;
|
|
42
45
|
state: "done";
|
|
43
46
|
}, {
|
|
44
47
|
state: import("convex/values").VLiteral<"done", "required">;
|
|
45
48
|
result: import("convex/values").VAny<any, "optional", string>;
|
|
46
|
-
}, "required", "result" | "state" | `result.${string}`>], "required", "
|
|
49
|
+
}, "required", "result" | "state" | `result.${string}`>], "required", "claimId" | "expiresAt" | "result" | "retryAfterMs" | "state" | `result.${string}`>;
|
|
47
50
|
/**
|
|
48
51
|
* Result of {@link complete}. `recorded: true` — the outcome was written.
|
|
49
52
|
* `recorded: false` — the claim was lost; `reason` distinguishes a never-claimed
|
|
@@ -54,27 +57,27 @@ export declare const beginResult: import("convex/values").VUnion<{
|
|
|
54
57
|
export declare const completeResult: import("convex/values").VUnion<{
|
|
55
58
|
recorded: true;
|
|
56
59
|
} | {
|
|
60
|
+
reason: "already_done" | "expired" | "missing" | "superseded";
|
|
57
61
|
recorded: false;
|
|
58
|
-
reason: "missing" | "expired" | "already_done";
|
|
59
62
|
}, [import("convex/values").VObject<{
|
|
60
63
|
recorded: true;
|
|
61
64
|
}, {
|
|
62
65
|
recorded: import("convex/values").VLiteral<true, "required">;
|
|
63
66
|
}, "required", "recorded">, import("convex/values").VObject<{
|
|
67
|
+
reason: "already_done" | "expired" | "missing" | "superseded";
|
|
64
68
|
recorded: false;
|
|
65
|
-
reason: "missing" | "expired" | "already_done";
|
|
66
69
|
}, {
|
|
67
70
|
recorded: import("convex/values").VLiteral<false, "required">;
|
|
68
|
-
reason: import("convex/values").VUnion<"
|
|
69
|
-
}, "required", "
|
|
71
|
+
reason: import("convex/values").VUnion<"already_done" | "expired" | "missing" | "superseded", [import("convex/values").VLiteral<"missing", "required">, import("convex/values").VLiteral<"expired", "required">, import("convex/values").VLiteral<"superseded", "required">, import("convex/values").VLiteral<"already_done", "required">], "required", never>;
|
|
72
|
+
}, "required", "reason" | "recorded">], "required", "reason" | "recorded">;
|
|
70
73
|
/** Stored projection of a key returned by {@link get}. */
|
|
71
74
|
export declare const keyState: import("convex/values").VObject<{
|
|
72
|
-
result?: any;
|
|
73
|
-
status: "inflight" | "done";
|
|
74
75
|
expiresAt: number;
|
|
76
|
+
result?: any;
|
|
77
|
+
status: "done" | "inflight";
|
|
75
78
|
}, {
|
|
76
|
-
status: import("convex/values").VUnion<"
|
|
79
|
+
status: import("convex/values").VUnion<"done" | "inflight", [import("convex/values").VLiteral<"inflight", "required">, import("convex/values").VLiteral<"done", "required">], "required", never>;
|
|
77
80
|
result: import("convex/values").VAny<any, "optional", string>;
|
|
78
81
|
expiresAt: import("convex/values").VFloat64<number, "required">;
|
|
79
|
-
}, "required", "
|
|
82
|
+
}, "required", "expiresAt" | "result" | "status" | `result.${string}`>;
|
|
80
83
|
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS,uDAAU,CAAC;AAEjC;;;;;;GAMG;AACH,eAAO,MAAM,WAAW
|
|
1
|
+
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS,uDAAU,CAAC;AAEjC;;;;;;GAMG;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yJAQvB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;0EAW1B,CAAC;AAEF,0DAA0D;AAC1D,eAAO,MAAM,QAAQ;;;;;;;;sEAInB,CAAC"}
|
|
@@ -17,7 +17,7 @@ export const jsonValue = v.any();
|
|
|
17
17
|
* before retrying. `done` — a prior attempt already completed; `result` carries
|
|
18
18
|
* the recorded outcome for a short-circuit replay.
|
|
19
19
|
*/
|
|
20
|
-
export const beginResult = v.union(v.object({ state: v.literal("fresh") }), v.object({
|
|
20
|
+
export const beginResult = v.union(v.object({ state: v.literal("fresh"), claimId: v.string() }), v.object({
|
|
21
21
|
state: v.literal("inflight"),
|
|
22
22
|
expiresAt: v.number(),
|
|
23
23
|
retryAfterMs: v.number(),
|
|
@@ -31,7 +31,7 @@ export const beginResult = v.union(v.object({ state: v.literal("fresh") }), v.ob
|
|
|
31
31
|
*/
|
|
32
32
|
export const completeResult = v.union(v.object({ recorded: v.literal(true) }), v.object({
|
|
33
33
|
recorded: v.literal(false),
|
|
34
|
-
reason: v.union(v.literal("missing"), v.literal("expired"), v.literal("already_done")),
|
|
34
|
+
reason: v.union(v.literal("missing"), v.literal("expired"), v.literal("superseded"), v.literal("already_done")),
|
|
35
35
|
}));
|
|
36
36
|
/** Stored projection of a key returned by {@link get}. */
|
|
37
37
|
export const keyState = v.object({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;AAEjC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAChC,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/component/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;AAEjC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAChC,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,EAC5D,CAAC,CAAC,MAAM,CAAC;IACP,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC;IAC5B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;IACrB,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;CACzB,CAAC,EACF,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CACtE,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CACnC,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,EACvC,CAAC,CAAC,MAAM,CAAC;IACP,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC;IAC1B,MAAM,EAAE,CAAC,CAAC,KAAK,CACb,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,EACvB,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,CAC1B;CACF,CAAC,CACH,CAAC;AAEF,0DAA0D;AAC1D,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/B,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACzD,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC;IAC7B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC"}
|
package/dist/shared.d.ts
CHANGED
|
@@ -14,4 +14,6 @@ export declare const DEFAULT_INFLIGHT_TTL_MS = 60000;
|
|
|
14
14
|
export declare const DEFAULT_DONE_TTL_MS = 86400000;
|
|
15
15
|
/** Default page size for a `purge` pass before the sweep self-reschedules. */
|
|
16
16
|
export declare const DEFAULT_PURGE_BATCH = 200;
|
|
17
|
+
/** Maximum rows deleted in one purge transaction. */
|
|
18
|
+
export declare const MAX_PURGE_BATCH = 500;
|
|
17
19
|
//# sourceMappingURL=shared.d.ts.map
|
package/dist/shared.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,eAAO,MAAM,cAAc,gBAAgB,CAAC;AAE5C,4DAA4D;AAC5D,eAAO,MAAM,aAAa,WAAW,CAAC;AAEtC;;;GAGG;AACH,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C;;;GAGG;AACH,eAAO,MAAM,mBAAmB,WAAa,CAAC;AAE9C,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,MAAM,CAAC"}
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,eAAO,MAAM,cAAc,gBAAgB,CAAC;AAE5C,4DAA4D;AAC5D,eAAO,MAAM,aAAa,WAAW,CAAC;AAEtC;;;GAGG;AACH,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C;;;GAGG;AACH,eAAO,MAAM,mBAAmB,WAAa,CAAC;AAE9C,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAEvC,qDAAqD;AACrD,eAAO,MAAM,eAAe,MAAM,CAAC"}
|
package/dist/shared.js
CHANGED
|
@@ -14,4 +14,6 @@ export const DEFAULT_INFLIGHT_TTL_MS = 60_000;
|
|
|
14
14
|
export const DEFAULT_DONE_TTL_MS = 86_400_000;
|
|
15
15
|
/** Default page size for a `purge` pass before the sweep self-reschedules. */
|
|
16
16
|
export const DEFAULT_PURGE_BATCH = 200;
|
|
17
|
+
/** Maximum rows deleted in one purge transaction. */
|
|
18
|
+
export const MAX_PURGE_BATCH = 500;
|
|
17
19
|
//# sourceMappingURL=shared.js.map
|
package/dist/shared.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,MAAM,CAAC,MAAM,cAAc,GAAG,aAAa,CAAC;AAE5C,4DAA4D;AAC5D,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC;AAE9C;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,UAAU,CAAC;AAE9C,8EAA8E;AAC9E,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC"}
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAEhE,MAAM,CAAC,MAAM,cAAc,GAAG,aAAa,CAAC;AAE5C,4DAA4D;AAC5D,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;AAEtC;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC;AAE9C;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,UAAU,CAAC;AAE9C,8EAA8E;AAC9E,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAEvC,qDAAqD;AACrD,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-idempotency",
|
|
3
|
-
"version": "0.1.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Exactly-once idempotency key ledger for retried operations, as a Convex component",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"module": "./dist/client/index.js",
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsc --project ./tsconfig.build.json",
|
|
38
|
-
"build:codegen": "pnpm
|
|
38
|
+
"build:codegen": "pnpm codegen && pnpm build",
|
|
39
39
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
40
40
|
"typecheck": "tsc --noEmit",
|
|
41
41
|
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
42
|
-
"lint": "eslint .",
|
|
42
|
+
"lint": "pnpm check:agents && eslint .",
|
|
43
43
|
"test": "vitest run --passWithNoTests",
|
|
44
44
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
45
45
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
@@ -47,23 +47,26 @@
|
|
|
47
47
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
48
48
|
"prepublishOnly": "npm whoami || npm login",
|
|
49
49
|
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
50
|
-
"release": "npm version patch && npm publish && git push --follow-tags"
|
|
50
|
+
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
51
|
+
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
52
|
+
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
51
53
|
},
|
|
52
|
-
"dependencies": {},
|
|
53
54
|
"peerDependencies": {
|
|
54
|
-
"convex": "^1.
|
|
55
|
+
"convex": "^1.45.0"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
58
|
"@edge-runtime/vm": "^5.0.0",
|
|
58
|
-
"@
|
|
59
|
-
"@
|
|
60
|
-
"@vllnt/
|
|
61
|
-
"
|
|
62
|
-
"convex
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
59
|
+
"@typescript/native": "npm:typescript@7.0.2",
|
|
60
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
61
|
+
"@vllnt/eslint-config": "2.0.0",
|
|
62
|
+
"@vllnt/typescript": "2.0.0",
|
|
63
|
+
"convex": "^1.45.0",
|
|
64
|
+
"convex-test": "^0.0.56",
|
|
65
|
+
"eslint": "^9.39.5",
|
|
66
|
+
"prettier": "^3.9.6",
|
|
67
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
68
|
+
"vite": "^8.2.2",
|
|
69
|
+
"vitest": "^4.1.11"
|
|
67
70
|
},
|
|
68
71
|
"homepage": "https://github.com/vllnt/convex-idempotency#readme",
|
|
69
72
|
"bugs": {
|
package/src/client/index.ts
CHANGED
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
* the host `resultValidator` over it at its typed boundary.
|
|
24
24
|
*/
|
|
25
25
|
type RawBegin =
|
|
26
|
-
| { state: "fresh" }
|
|
26
|
+
| { state: "fresh"; claimId: string }
|
|
27
27
|
| { state: "inflight"; expiresAt: number; retryAfterMs: number }
|
|
28
28
|
| { state: "done"; result?: unknown };
|
|
29
29
|
|
|
@@ -48,6 +48,7 @@ export interface IdempotencyComponent {
|
|
|
48
48
|
key: string;
|
|
49
49
|
scope: string;
|
|
50
50
|
result?: unknown;
|
|
51
|
+
claimId?: string;
|
|
51
52
|
doneTtlMs: number;
|
|
52
53
|
upsertOnMissing: boolean;
|
|
53
54
|
},
|
|
@@ -97,6 +98,8 @@ interface BeginOptions {
|
|
|
97
98
|
/** Per-call overrides for `scope`, the done grace window, and lost-claim upsert. */
|
|
98
99
|
interface CompleteOptions {
|
|
99
100
|
scope?: string;
|
|
101
|
+
/** Fencing token returned by the successful `begin` claim. */
|
|
102
|
+
claimId?: string;
|
|
100
103
|
doneTtlMs?: number;
|
|
101
104
|
upsertOnMissing?: boolean;
|
|
102
105
|
}
|
|
@@ -119,7 +122,7 @@ interface CompleteOptions {
|
|
|
119
122
|
* if (r.state === "done") return r.result; // typed replay
|
|
120
123
|
* if (r.state === "inflight") throw new Error(`retry in ${r.retryAfterMs}ms`);
|
|
121
124
|
* const out = await doWork(); // r.state === "fresh"
|
|
122
|
-
* const done = await idem.complete(ctx, requestId, out);
|
|
125
|
+
* const done = await idem.complete(ctx, requestId, out, { claimId: r.claimId });
|
|
123
126
|
* if (!done.recorded) log.warn("claim lost", done.reason); // work ran, row gone
|
|
124
127
|
* ```
|
|
125
128
|
*/
|
|
@@ -201,6 +204,7 @@ export class Idempotency<TResult = unknown> {
|
|
|
201
204
|
key,
|
|
202
205
|
scope: this.scopeOf(opts.scope),
|
|
203
206
|
result: validated,
|
|
207
|
+
claimId: opts.claimId,
|
|
204
208
|
doneTtlMs: opts.doneTtlMs ?? this.defaultDoneTtlMs,
|
|
205
209
|
upsertOnMissing: opts.upsertOnMissing ?? this.defaultUpsertOnMissing,
|
|
206
210
|
});
|
package/src/client/types.ts
CHANGED
|
@@ -15,6 +15,8 @@ export type BeginResult<TResult> =
|
|
|
15
15
|
| {
|
|
16
16
|
/** The key was just minted; do the work, then call `complete`. */
|
|
17
17
|
state: "fresh";
|
|
18
|
+
/** Opaque fencing token that must be passed to `complete`. */
|
|
19
|
+
claimId: string;
|
|
18
20
|
}
|
|
19
21
|
| {
|
|
20
22
|
/** Another attempt holds the key; do not re-run. */
|
|
@@ -44,7 +46,7 @@ export type CompleteResult =
|
|
|
44
46
|
* `missing` — no key for this id/scope. `expired` — the inflight lease
|
|
45
47
|
* lapsed before completion. `already_done` — a prior attempt won the race.
|
|
46
48
|
*/
|
|
47
|
-
reason: "missing" | "expired" | "already_done";
|
|
49
|
+
reason: "missing" | "expired" | "superseded" | "already_done";
|
|
48
50
|
};
|
|
49
51
|
|
|
50
52
|
/** Construction options for the {@link Idempotency} client. */
|
|
@@ -28,7 +28,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
28
28
|
"mutation",
|
|
29
29
|
"internal",
|
|
30
30
|
{ inflightTtlMs: number; key: string; scope: string },
|
|
31
|
-
| { state: "fresh" }
|
|
31
|
+
| { claimId: string; state: "fresh" }
|
|
32
32
|
| { expiresAt: number; retryAfterMs: number; state: "inflight" }
|
|
33
33
|
| { result?: any; state: "done" },
|
|
34
34
|
Name
|
|
@@ -37,6 +37,7 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
37
37
|
"mutation",
|
|
38
38
|
"internal",
|
|
39
39
|
{
|
|
40
|
+
claimId?: string;
|
|
40
41
|
doneTtlMs: number;
|
|
41
42
|
key: string;
|
|
42
43
|
result?: any;
|
|
@@ -44,7 +45,10 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
44
45
|
upsertOnMissing: boolean;
|
|
45
46
|
},
|
|
46
47
|
| { recorded: true }
|
|
47
|
-
| {
|
|
48
|
+
| {
|
|
49
|
+
reason: "missing" | "expired" | "superseded" | "already_done";
|
|
50
|
+
recorded: false;
|
|
51
|
+
},
|
|
48
52
|
Name
|
|
49
53
|
>;
|
|
50
54
|
purge: FunctionReference<
|
|
@@ -30,6 +30,17 @@ import {
|
|
|
30
30
|
} from "convex/server";
|
|
31
31
|
import type { DataModel } from "./dataModel.js";
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Typesafe environment variables.
|
|
35
|
+
*
|
|
36
|
+
* This includes platform-provided env vars and any variables declared in
|
|
37
|
+
* `convex.config.ts`.
|
|
38
|
+
*/
|
|
39
|
+
type Env = {
|
|
40
|
+
readonly CONVEX_CLOUD_URL: string;
|
|
41
|
+
readonly CONVEX_SITE_URL: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
33
44
|
/**
|
|
34
45
|
* Define a query in this Convex app's public API.
|
|
35
46
|
*
|
|
@@ -106,6 +117,8 @@ export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
|
106
117
|
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
107
118
|
*/
|
|
108
119
|
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
120
|
+
export const env: Env = (globalThis as unknown as { process: { env: Env } })
|
|
121
|
+
.process.env;
|
|
109
122
|
|
|
110
123
|
/**
|
|
111
124
|
* A set of services for use within Convex query functions.
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { api } from "./_generated/api";
|
|
3
3
|
import { mutation } from "./_generated/server";
|
|
4
|
+
import { MAX_PURGE_BATCH } from "../shared";
|
|
4
5
|
import { beginResult, completeResult, jsonValue } from "./validators";
|
|
5
6
|
|
|
7
|
+
function validateBatch(batch: number): void {
|
|
8
|
+
if (!Number.isFinite(batch) || !Number.isInteger(batch) || batch < 1 || batch > MAX_PURGE_BATCH) {
|
|
9
|
+
throw new ConvexError({
|
|
10
|
+
code: "INVALID_BATCH",
|
|
11
|
+
message: `batch must be an integer between 1 and ${MAX_PURGE_BATCH}`,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
6
16
|
/**
|
|
7
17
|
* Claim `key` within `scope`. Time is read from the server (`Date.now()`) inside
|
|
8
18
|
* the handler — never supplied by the caller — so an adversarial clock cannot
|
|
@@ -46,22 +56,29 @@ export const begin = mutation({
|
|
|
46
56
|
const expiresAt = now + args.inflightTtlMs;
|
|
47
57
|
|
|
48
58
|
if (existing === null) {
|
|
59
|
+
const claimId = crypto.randomUUID();
|
|
49
60
|
await ctx.db.insert("keys", {
|
|
50
61
|
key: args.key,
|
|
51
62
|
scope: args.scope,
|
|
52
63
|
status: "inflight",
|
|
64
|
+
claimId,
|
|
65
|
+
claimGeneration: 1,
|
|
53
66
|
expiresAt,
|
|
54
67
|
});
|
|
55
|
-
return { state: "fresh" as const };
|
|
68
|
+
return { state: "fresh" as const, claimId };
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
if (existing.expiresAt <= now) {
|
|
59
|
-
|
|
72
|
+
const claimId = crypto.randomUUID();
|
|
73
|
+
await ctx.db.patch("keys", existing._id, {
|
|
60
74
|
status: "inflight",
|
|
75
|
+
claimId,
|
|
76
|
+
// v8 ignore next -- migration fallback for rows written before claim generations existed
|
|
77
|
+
claimGeneration: (existing.claimGeneration ?? 1) + 1,
|
|
61
78
|
result: undefined,
|
|
62
79
|
expiresAt,
|
|
63
80
|
});
|
|
64
|
-
return { state: "fresh" as const };
|
|
81
|
+
return { state: "fresh" as const, claimId };
|
|
65
82
|
}
|
|
66
83
|
|
|
67
84
|
if (existing.status === "done") {
|
|
@@ -102,6 +119,7 @@ export const complete = mutation({
|
|
|
102
119
|
key: v.string(),
|
|
103
120
|
scope: v.string(),
|
|
104
121
|
result: v.optional(jsonValue),
|
|
122
|
+
claimId: v.optional(v.string()),
|
|
105
123
|
doneTtlMs: v.number(),
|
|
106
124
|
upsertOnMissing: v.boolean(),
|
|
107
125
|
},
|
|
@@ -130,6 +148,8 @@ export const complete = mutation({
|
|
|
130
148
|
key: args.key,
|
|
131
149
|
scope: args.scope,
|
|
132
150
|
status: "done",
|
|
151
|
+
claimId: args.claimId ?? crypto.randomUUID(),
|
|
152
|
+
claimGeneration: 1,
|
|
133
153
|
result: args.result,
|
|
134
154
|
expiresAt,
|
|
135
155
|
});
|
|
@@ -142,9 +162,16 @@ export const complete = mutation({
|
|
|
142
162
|
return { recorded: false as const, reason: "already_done" as const };
|
|
143
163
|
}
|
|
144
164
|
|
|
165
|
+
if (
|
|
166
|
+
(args.claimId !== undefined && args.claimId !== existing.claimId) ||
|
|
167
|
+
(args.claimId === undefined && (existing.claimGeneration ?? 1) > 1)
|
|
168
|
+
) {
|
|
169
|
+
return { recorded: false as const, reason: "superseded" as const };
|
|
170
|
+
}
|
|
171
|
+
|
|
145
172
|
if (existing.expiresAt <= now) {
|
|
146
173
|
if (args.upsertOnMissing) {
|
|
147
|
-
await ctx.db.patch(existing._id, {
|
|
174
|
+
await ctx.db.patch("keys", existing._id, {
|
|
148
175
|
status: "done",
|
|
149
176
|
result: args.result,
|
|
150
177
|
expiresAt,
|
|
@@ -154,7 +181,7 @@ export const complete = mutation({
|
|
|
154
181
|
return { recorded: false as const, reason: "expired" as const };
|
|
155
182
|
}
|
|
156
183
|
|
|
157
|
-
await ctx.db.patch(existing._id, {
|
|
184
|
+
await ctx.db.patch("keys", existing._id, {
|
|
158
185
|
status: "done",
|
|
159
186
|
result: args.result,
|
|
160
187
|
expiresAt,
|
|
@@ -176,13 +203,17 @@ export const purge = mutation({
|
|
|
176
203
|
args: { before: v.optional(v.number()), batch: v.number() },
|
|
177
204
|
returns: v.number(),
|
|
178
205
|
handler: async (ctx, args) => {
|
|
206
|
+
validateBatch(args.batch);
|
|
207
|
+
if (args.before !== undefined && !Number.isFinite(args.before)) {
|
|
208
|
+
throw new ConvexError({ code: "INVALID_BEFORE", message: "before must be finite" });
|
|
209
|
+
}
|
|
179
210
|
const before = args.before ?? Date.now();
|
|
180
211
|
const stale = await ctx.db
|
|
181
212
|
.query("keys")
|
|
182
213
|
.withIndex("by_expires", (q) => q.lt("expiresAt", before))
|
|
183
214
|
.take(args.batch);
|
|
184
215
|
for (const row of stale) {
|
|
185
|
-
await ctx.db.delete(row._id);
|
|
216
|
+
await ctx.db.delete("keys", row._id);
|
|
186
217
|
}
|
|
187
218
|
if (stale.length === args.batch) {
|
|
188
219
|
await ctx.scheduler.runAfter(0, api.mutations.purge, {
|
package/src/component/schema.ts
CHANGED
|
@@ -13,6 +13,8 @@ export default defineSchema({
|
|
|
13
13
|
key: v.string(),
|
|
14
14
|
scope: v.string(),
|
|
15
15
|
status: v.union(v.literal("inflight"), v.literal("done")),
|
|
16
|
+
claimId: v.optional(v.string()),
|
|
17
|
+
claimGeneration: v.optional(v.number()),
|
|
16
18
|
result: v.optional(jsonValue),
|
|
17
19
|
expiresAt: v.number(),
|
|
18
20
|
})
|
|
@@ -20,7 +20,7 @@ export const jsonValue = v.any();
|
|
|
20
20
|
* the recorded outcome for a short-circuit replay.
|
|
21
21
|
*/
|
|
22
22
|
export const beginResult = v.union(
|
|
23
|
-
v.object({ state: v.literal("fresh") }),
|
|
23
|
+
v.object({ state: v.literal("fresh"), claimId: v.string() }),
|
|
24
24
|
v.object({
|
|
25
25
|
state: v.literal("inflight"),
|
|
26
26
|
expiresAt: v.number(),
|
|
@@ -43,6 +43,7 @@ export const completeResult = v.union(
|
|
|
43
43
|
reason: v.union(
|
|
44
44
|
v.literal("missing"),
|
|
45
45
|
v.literal("expired"),
|
|
46
|
+
v.literal("superseded"),
|
|
46
47
|
v.literal("already_done"),
|
|
47
48
|
),
|
|
48
49
|
}),
|
package/src/shared.ts
CHANGED
|
@@ -19,3 +19,6 @@ export const DEFAULT_DONE_TTL_MS = 86_400_000;
|
|
|
19
19
|
|
|
20
20
|
/** Default page size for a `purge` pass before the sweep self-reschedules. */
|
|
21
21
|
export const DEFAULT_PURGE_BATCH = 200;
|
|
22
|
+
|
|
23
|
+
/** Maximum rows deleted in one purge transaction. */
|
|
24
|
+
export const MAX_PURGE_BATCH = 500;
|