@vllnt/convex-idempotency 0.1.0 → 0.1.1-canary.28c3e26
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 +90 -0
- package/README.md +128 -42
- package/dist/client/index.d.ts +7 -6
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +7 -6
- package/dist/client/index.js.map +1 -1
- package/dist/client/types.d.ts +5 -3
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/crons.js +1 -1
- package/dist/component/crons.js.map +1 -1
- package/dist/component/mutations.d.ts +15 -9
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +29 -13
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.js +2 -2
- package/dist/component/queries.js.map +1 -1
- package/dist/component/schema.js +1 -1
- package/dist/component/schema.js.map +1 -1
- package/dist/component/validators.d.ts +3 -2
- package/dist/component/validators.d.ts.map +1 -1
- package/dist/component/validators.js +3 -2
- package/dist/component/validators.js.map +1 -1
- package/docs/API.md +178 -0
- package/llms.txt +32 -0
- package/package.json +41 -19
- package/src/client/index.ts +7 -6
- package/src/client/types.ts +5 -3
- package/src/component/crons.ts +1 -1
- package/src/component/mutations.ts +30 -13
- package/src/component/queries.ts +2 -2
- package/src/component/schema.ts +1 -1
- package/src/component/validators.ts +3 -2
- package/src/test.ts +2 -1
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
|
-
import { api } from "./_generated/api";
|
|
3
|
-
import { mutation } from "./_generated/server";
|
|
4
|
-
import { MAX_PURGE_BATCH } from "../shared";
|
|
5
|
-
import { beginResult, completeResult, jsonValue } from "./validators";
|
|
2
|
+
import { api } from "./_generated/api.js";
|
|
3
|
+
import { mutation } from "./_generated/server.js";
|
|
4
|
+
import { MAX_PURGE_BATCH } from "../shared.js";
|
|
5
|
+
import { beginResult, completeResult, jsonValue } from "./validators.js";
|
|
6
|
+
function validateKeyAndScope(key, scope) {
|
|
7
|
+
if (key === "") {
|
|
8
|
+
throw new ConvexError({ code: "INVALID_KEY", message: "key must not be empty" });
|
|
9
|
+
}
|
|
10
|
+
if (scope === "") {
|
|
11
|
+
throw new ConvexError({ code: "INVALID_SCOPE", message: "scope must not be empty" });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
6
14
|
function validateBatch(batch) {
|
|
7
15
|
if (!Number.isFinite(batch) || !Number.isInteger(batch) || batch < 1 || batch > MAX_PURGE_BATCH) {
|
|
8
16
|
throw new ConvexError({
|
|
@@ -24,6 +32,8 @@ function validateBatch(batch) {
|
|
|
24
32
|
* correct recovery path — the done grace exists precisely to replay within the
|
|
25
33
|
* window; outside it the operation may safely re-run.
|
|
26
34
|
*
|
|
35
|
+
* @throws `ConvexError({ code: "INVALID_KEY" | "INVALID_SCOPE" })` when `key` or
|
|
36
|
+
* `scope` is the empty string.
|
|
27
37
|
* @throws `ConvexError({ code: "INVALID_TTL" })` when `inflightTtlMs` is not a
|
|
28
38
|
* positive finite number (≤ 0 or non-finite would produce `expiresAt ≤ now`,
|
|
29
39
|
* immediately expiring the claim and breaking the dedup window).
|
|
@@ -36,6 +46,7 @@ export const begin = mutation({
|
|
|
36
46
|
},
|
|
37
47
|
returns: beginResult,
|
|
38
48
|
handler: async (ctx, args) => {
|
|
49
|
+
validateKeyAndScope(args.key, args.scope);
|
|
39
50
|
if (!(args.inflightTtlMs > 0 && isFinite(args.inflightTtlMs))) {
|
|
40
51
|
throw new ConvexError({
|
|
41
52
|
code: "INVALID_TTL",
|
|
@@ -86,10 +97,11 @@ export const begin = mutation({
|
|
|
86
97
|
* Mark `key` `done`, recording `result` and extending the grace window by
|
|
87
98
|
* `doneTtlMs`. Returns a discriminated outcome so the host can detect a lost
|
|
88
99
|
* claim: `recorded: false` with `missing` (key never existed), `expired` (the
|
|
89
|
-
* inflight lease lapsed before completion),
|
|
90
|
-
*
|
|
91
|
-
* `
|
|
92
|
-
*
|
|
100
|
+
* inflight lease lapsed before completion), `superseded` (a newer claim replaced
|
|
101
|
+
* this worker's lease), or `already_done` (a prior attempt won). When
|
|
102
|
+
* `upsertOnMissing` is set, a `missing`/`expired` key is written as `done`
|
|
103
|
+
* anyway — for hosts that would rather record the finished work than drop it.
|
|
104
|
+
* Time is server-sourced.
|
|
93
105
|
*
|
|
94
106
|
* **Expiry check order (intentional asymmetry):** `complete` checks the
|
|
95
107
|
* done-state BEFORE expiry. An expired done key therefore returns
|
|
@@ -99,6 +111,8 @@ export const begin = mutation({
|
|
|
99
111
|
* The `expired` reason is reserved for inflight keys whose lease lapsed before
|
|
100
112
|
* the holder could complete; it does not apply to keys already marked done.
|
|
101
113
|
*
|
|
114
|
+
* @throws `ConvexError({ code: "INVALID_KEY" | "INVALID_SCOPE" })` when `key` or
|
|
115
|
+
* `scope` is the empty string.
|
|
102
116
|
* @throws `ConvexError({ code: "INVALID_TTL" })` when `doneTtlMs` is not a
|
|
103
117
|
* positive finite number (≤ 0 or non-finite would produce `expiresAt ≤ now`,
|
|
104
118
|
* immediately expiring the done grace window on creation).
|
|
@@ -114,6 +128,7 @@ export const complete = mutation({
|
|
|
114
128
|
},
|
|
115
129
|
returns: completeResult,
|
|
116
130
|
handler: async (ctx, args) => {
|
|
131
|
+
validateKeyAndScope(args.key, args.scope);
|
|
117
132
|
if (!(args.doneTtlMs > 0 && isFinite(args.doneTtlMs))) {
|
|
118
133
|
throw new ConvexError({
|
|
119
134
|
code: "INVALID_TTL",
|
|
@@ -170,11 +185,12 @@ export const complete = mutation({
|
|
|
170
185
|
/**
|
|
171
186
|
* Delete up to `batch` keys whose `expiresAt < before`, oldest first via the
|
|
172
187
|
* `by_expires` index. `before` defaults to the server clock (`Date.now()`) when
|
|
173
|
-
* omitted, so the built-in cron sweeps exactly the keys expired as of the run
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
188
|
+
* omitted, so the built-in cron sweeps exactly the keys expired as of the run.
|
|
189
|
+
* An explicit `before` is trusted as given: a future cutoff also deletes live
|
|
190
|
+
* keys, so never derive it from untrusted input. If a full batch was removed
|
|
191
|
+
* there may be more, so the sweep self-reschedules through `ctx.scheduler` until
|
|
192
|
+
* a short batch signals the tail is clean. Idempotent for a past cutoff:
|
|
193
|
+
* re-running it removes nothing new.
|
|
178
194
|
*/
|
|
179
195
|
export const purge = mutation({
|
|
180
196
|
args: { before: v.optional(v.number()), batch: v.number() },
|
|
@@ -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,
|
|
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,qBAAqB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEzE,SAAS,mBAAmB,CAAC,GAAW,EAAE,KAAa;IACrD,IAAI,GAAG,KAAK,EAAE,EAAE,CAAC;QACf,MAAM,IAAI,WAAW,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,uBAAuB,EAAE,CAAC,CAAC;IACnF,CAAC;IACD,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,WAAW,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,yBAAyB,EAAE,CAAC,CAAC;IACvF,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,KAAa;I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EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;aACzD,IAAI,CAAC,IAAI,CAAC,KAAK,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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
|
-
import { query } from "./_generated/server";
|
|
3
|
-
import { keyState } from "./validators";
|
|
2
|
+
import { query } from "./_generated/server.js";
|
|
3
|
+
import { keyState } from "./validators.js";
|
|
4
4
|
export const get = query({
|
|
5
5
|
args: { key: v.string(), scope: v.string() },
|
|
6
6
|
returns: v.union(v.null(), keyState),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,CAAC;IACvB,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE;IAC5C,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,QAAQ,CAAC;IACpC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,EAAE;aACrB,KAAK,CAAC,MAAM,CAAC;aACb,SAAS,CAAC,cAAc,EAAE,CAAC,CAAC,EAAE,EAAE,CAC/B,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAC9C;aACA,MAAM,EAAE,CAAC;QACZ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO;YACL,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,SAAS,EAAE,GAAG,CAAC,SAAS;SACzB,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
package/dist/component/schema.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineSchema, defineTable } from "convex/server";
|
|
2
2
|
import { v } from "convex/values";
|
|
3
|
-
import { jsonValue } from "./validators";
|
|
3
|
+
import { jsonValue } from "./validators.js";
|
|
4
4
|
/**
|
|
5
5
|
* Sandboxed tables — the idempotency ledger's own concern. A `key` is unique
|
|
6
6
|
* within a `scope`; `result` carries the opaque host-owned outcome (never
|
|
@@ -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,
|
|
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,iBAAiB,CAAC;AAE5C;;;;;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,UAAU,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACzD,OAAO,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QAC/B,eAAe,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QACvC,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC;QAC7B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KACtB,CAAC;SACC,KAAK,CAAC,cAAc,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;SACvC,KAAK,CAAC,YAAY,EAAE,CAAC,WAAW,CAAC,CAAC;CACtC,CAAC,CAAC"}
|
|
@@ -50,8 +50,9 @@ export declare const beginResult: import("convex/values").VUnion<{
|
|
|
50
50
|
/**
|
|
51
51
|
* Result of {@link complete}. `recorded: true` — the outcome was written.
|
|
52
52
|
* `recorded: false` — the claim was lost; `reason` distinguishes a never-claimed
|
|
53
|
-
* key (`missing`), a lease that expired before completion (`expired`),
|
|
54
|
-
*
|
|
53
|
+
* key (`missing`), a lease that expired before completion (`expired`), a lease
|
|
54
|
+
* replaced by a newer claim (`superseded`), and a key already marked done by a
|
|
55
|
+
* prior attempt (`already_done`). A host that sees
|
|
55
56
|
* `recorded: false` knows its work finished but the ledger row was gone.
|
|
56
57
|
*/
|
|
57
58
|
export declare const completeResult: import("convex/values").VUnion<{
|
|
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yJAQvB,CAAC;AAEF
|
|
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;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;0EAW1B,CAAC;AAEF,0DAA0D;AAC1D,eAAO,MAAM,QAAQ;;;;;;;;sEAInB,CAAC"}
|
|
@@ -25,8 +25,9 @@ export const beginResult = v.union(v.object({ state: v.literal("fresh"), claimId
|
|
|
25
25
|
/**
|
|
26
26
|
* Result of {@link complete}. `recorded: true` — the outcome was written.
|
|
27
27
|
* `recorded: false` — the claim was lost; `reason` distinguishes a never-claimed
|
|
28
|
-
* key (`missing`), a lease that expired before completion (`expired`),
|
|
29
|
-
*
|
|
28
|
+
* key (`missing`), a lease that expired before completion (`expired`), a lease
|
|
29
|
+
* replaced by a newer claim (`superseded`), and a key already marked done by a
|
|
30
|
+
* prior attempt (`already_done`). A host that sees
|
|
30
31
|
* `recorded: false` knows its work finished but the ledger row was gone.
|
|
31
32
|
*/
|
|
32
33
|
export const completeResult = v.union(v.object({ recorded: v.literal(true) }), 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,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
|
|
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,CAA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"}
|
package/docs/API.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# API Reference — @vllnt/convex-idempotency
|
|
2
|
+
|
|
3
|
+
**Compatibility:** `convex@^1.45.0`. Error codes are listed at the end of this
|
|
4
|
+
page.
|
|
5
|
+
|
|
6
|
+
## Entry points
|
|
7
|
+
|
|
8
|
+
| Import | Provides |
|
|
9
|
+
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
|
|
10
|
+
| `@vllnt/convex-idempotency` | `Idempotency` class; types `IdempotencyComponent`, `IdempotencyOptions`, `BeginResult`, `CompleteResult`, `KeyState`, `ResultParser` |
|
|
11
|
+
| `@vllnt/convex-idempotency/convex.config` | The component definition to mount with `app.use(...)` (named `idempotency`) |
|
|
12
|
+
| `@vllnt/convex-idempotency/test` | `register(t, name = "idempotency")` to mount the component in a `convex-test` instance |
|
|
13
|
+
| `@vllnt/convex-idempotency/_generated/component` | Type-only `ComponentApi` of the mounted component |
|
|
14
|
+
|
|
15
|
+
Mount it in `convex/convex.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { defineApp } from "convex/server";
|
|
19
|
+
import idempotency from "@vllnt/convex-idempotency/convex.config";
|
|
20
|
+
|
|
21
|
+
const app = defineApp();
|
|
22
|
+
app.use(idempotency);
|
|
23
|
+
export default app;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Construct the client with the mounted component and optional config:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { Idempotency } from "@vllnt/convex-idempotency";
|
|
30
|
+
|
|
31
|
+
type MyResult = { id: string };
|
|
32
|
+
const idem = new Idempotency<MyResult>(components.idempotency, {
|
|
33
|
+
defaultScope: "global", // namespace applied when a call omits `scope`
|
|
34
|
+
defaultInflightTtlMs: 60_000, // inflight lease (ms) when `begin` omits `inflightTtlMs` (60s)
|
|
35
|
+
defaultDoneTtlMs: 86_400_000, // done grace (ms) when `complete` omits `doneTtlMs` (24h)
|
|
36
|
+
upsertOnMissing: false, // record finished work even if the claim row vanished
|
|
37
|
+
resultValidator: (value) => {
|
|
38
|
+
if (
|
|
39
|
+
typeof value !== "object" ||
|
|
40
|
+
value === null ||
|
|
41
|
+
!("id" in value) ||
|
|
42
|
+
typeof value.id !== "string"
|
|
43
|
+
) {
|
|
44
|
+
throw new Error("Invalid stored result");
|
|
45
|
+
}
|
|
46
|
+
return { id: value.id };
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`Idempotency<TResult = unknown>` is generic over the host's stored outcome type.
|
|
52
|
+
All methods take the host `ctx` as the first argument: `begin`, `complete` and
|
|
53
|
+
`purge` need a mutation or action context (`runMutation`); `get` works with any
|
|
54
|
+
context that has `runQuery`.
|
|
55
|
+
|
|
56
|
+
**Time is server-sourced.** Every handler reads `Date.now()` itself; `begin`,
|
|
57
|
+
`complete` and `get` accept no caller-supplied `now`, so a skewed or hostile
|
|
58
|
+
client clock cannot make a key look live or expired. The one time input is
|
|
59
|
+
`purge`'s optional `before` cutoff (see below).
|
|
60
|
+
|
|
61
|
+
**Result validation.** When `resultValidator` is set it runs at the client
|
|
62
|
+
boundary: over the value written by `complete` (before storage) and over the
|
|
63
|
+
value returned by `begin` (`done` replay) and `get`. It must return a typed
|
|
64
|
+
`TResult` or throw. A Zod `.parse` or a hand-written type guard fits
|
|
65
|
+
(`convex/values` validators have no `.parse`). Omit it to leave results
|
|
66
|
+
unvalidated.
|
|
67
|
+
|
|
68
|
+
## Mutations
|
|
69
|
+
|
|
70
|
+
### `begin(ctx, key, opts?) → BeginResult`
|
|
71
|
+
|
|
72
|
+
`opts`: `{ scope?: string; inflightTtlMs?: number }`.
|
|
73
|
+
|
|
74
|
+
Claim `key` for an operation. The discriminated result is:
|
|
75
|
+
|
|
76
|
+
- `{ state: "fresh"; claimId }` — the key was just minted (or a prior claim had
|
|
77
|
+
expired and was re-minted in place); the caller should perform the work and
|
|
78
|
+
pass the opaque `claimId` to `complete`.
|
|
79
|
+
- `{ state: "inflight"; expiresAt; retryAfterMs }` — another attempt holds an
|
|
80
|
+
unexpired claim; do not re-run. `expiresAt` is when the lease frees and
|
|
81
|
+
`retryAfterMs` is the backoff before retrying.
|
|
82
|
+
- `{ state: "done"; result? }` — a prior attempt already completed; `result`
|
|
83
|
+
carries the recorded outcome (validated) for a short-circuit replay.
|
|
84
|
+
|
|
85
|
+
Mints an inflight key with `expiresAt = now + inflightTtlMs`. An expired key
|
|
86
|
+
(any status) is re-minted `fresh` by patching the existing row in place (the id
|
|
87
|
+
is reused, the stale result cleared).
|
|
88
|
+
|
|
89
|
+
**Expiry check order:** `begin` checks expiry **before** the done-state. An
|
|
90
|
+
expired done key is therefore re-minted `fresh` — the grace window has elapsed
|
|
91
|
+
and the ledger permits another attempt. Whether external work is safe to repeat
|
|
92
|
+
is still the host/provider's responsibility.
|
|
93
|
+
|
|
94
|
+
**`key` and `scope` must not be empty.** An empty string throws
|
|
95
|
+
`ConvexError({ code: "INVALID_KEY" })` / `INVALID_SCOPE` before any write.
|
|
96
|
+
|
|
97
|
+
**`inflightTtlMs` must be a positive finite number.** Passing `0`, a negative
|
|
98
|
+
value, or `Infinity` throws `ConvexError({ code: "INVALID_TTL" })`. A zero or
|
|
99
|
+
negative TTL would produce `expiresAt ≤ now`, immediately expiring the claim.
|
|
100
|
+
|
|
101
|
+
### `complete(ctx, key, result?, opts?) → CompleteResult`
|
|
102
|
+
|
|
103
|
+
`opts`:
|
|
104
|
+
`{ scope?: string; claimId?: string; doneTtlMs?: number; upsertOnMissing?: boolean }`.
|
|
105
|
+
|
|
106
|
+
Mark `key` `done`, recording `result` and extending the grace window
|
|
107
|
+
(`expiresAt = now + doneTtlMs`). The discriminated result lets a host detect a
|
|
108
|
+
lost claim:
|
|
109
|
+
|
|
110
|
+
- `{ recorded: true }` — the outcome was written.
|
|
111
|
+
- `{ recorded: false; reason }` — the claim was lost; `reason` is `missing` (no
|
|
112
|
+
key), `expired` (the inflight lease lapsed before completion), `superseded` (a
|
|
113
|
+
newer claim replaced this worker's lease), or `already_done` (a prior attempt
|
|
114
|
+
won).
|
|
115
|
+
|
|
116
|
+
When `upsertOnMissing` is set, a `missing`/`expired` key is written as `done`
|
|
117
|
+
anyway — recording finished work rather than dropping it (returns
|
|
118
|
+
`{ recorded: true }`). A `superseded` claim is never upserted or allowed to
|
|
119
|
+
overwrite the current lease. For migration compatibility, an omitted claim ID is
|
|
120
|
+
accepted only for the first claim generation; after any re-mint it fails closed
|
|
121
|
+
as `superseded`.
|
|
122
|
+
|
|
123
|
+
**Expiry check order:** `complete` checks the done-state **before** expiry. An
|
|
124
|
+
expired done key therefore returns `already_done`, not `expired` — a prior
|
|
125
|
+
winner's recorded outcome must not be overwritten by a late attempt, even after
|
|
126
|
+
the grace window has lapsed. The `expired` reason applies only to inflight keys
|
|
127
|
+
whose lease lapsed before completion.
|
|
128
|
+
|
|
129
|
+
**`key` and `scope` must not be empty** (same errors as `begin`). `get` does not
|
|
130
|
+
reject them: an empty key simply finds nothing.
|
|
131
|
+
|
|
132
|
+
**`doneTtlMs` must be a positive finite number.** Passing `0`, a negative value,
|
|
133
|
+
or `Infinity` throws `ConvexError({ code: "INVALID_TTL" })`.
|
|
134
|
+
|
|
135
|
+
### `purge(ctx, opts?) → number`
|
|
136
|
+
|
|
137
|
+
`opts`: `{ before?: number; batch?: number }` (defaults: `before = Date.now()`,
|
|
138
|
+
`batch = 200`; valid batch range: 1–500).
|
|
139
|
+
|
|
140
|
+
Delete up to `batch` keys whose `expiresAt < before`, oldest first via the
|
|
141
|
+
`by_expires` index, and return the count removed in the first pass. If a full
|
|
142
|
+
batch was removed the sweep self-reschedules through the component scheduler
|
|
143
|
+
until the expired tail is clean. Idempotent for a past cutoff: re-running it
|
|
144
|
+
removes nothing new.
|
|
145
|
+
|
|
146
|
+
**`before` is trusted as given.** A future `before` also deletes live keys
|
|
147
|
+
(inflight and done), which lets the work run again. Omit it, or compute it on
|
|
148
|
+
the server; never pass a value from client input. A built-in daily cron
|
|
149
|
+
(`crons.ts`) drives this automatically; call `purge` directly only for an extra
|
|
150
|
+
or custom-cadence sweep.
|
|
151
|
+
|
|
152
|
+
## Queries
|
|
153
|
+
|
|
154
|
+
### `get(ctx, key, scope?) → { status, result?, expiresAt } | null`
|
|
155
|
+
|
|
156
|
+
The stored state for `key` in `scope`, or `null` if no row exists. `status` is
|
|
157
|
+
`inflight` or `done`; `result` is present only when `done` (validated);
|
|
158
|
+
`expiresAt` is the absolute ms timestamp after which the key may be re-minted.
|
|
159
|
+
`scope` defaults to the client's `defaultScope`.
|
|
160
|
+
|
|
161
|
+
`get` does not filter by time: an expired row is still returned until `purge`
|
|
162
|
+
deletes it or `begin` re-mints it. Compare `expiresAt` with the current time if
|
|
163
|
+
you need to know whether it is still live.
|
|
164
|
+
|
|
165
|
+
## Error codes
|
|
166
|
+
|
|
167
|
+
Invalid arguments throw a `ConvexError` whose `data.code` is one of:
|
|
168
|
+
|
|
169
|
+
| Code | Thrown by | Cause |
|
|
170
|
+
| ---------------- | ------------------- | ------------------------------------------------------------------ |
|
|
171
|
+
| `INVALID_KEY` | `begin`, `complete` | `key` is the empty string |
|
|
172
|
+
| `INVALID_SCOPE` | `begin`, `complete` | `scope` is the empty string |
|
|
173
|
+
| `INVALID_TTL` | `begin`, `complete` | `inflightTtlMs` / `doneTtlMs` is zero, negative, `NaN` or infinite |
|
|
174
|
+
| `INVALID_BATCH` | `purge` | `batch` is not an integer between 1 and 500 |
|
|
175
|
+
| `INVALID_BEFORE` | `purge` | `before` is not finite |
|
|
176
|
+
|
|
177
|
+
Auth and ownership are the host's job: a component cannot read `ctx.auth`, so
|
|
178
|
+
authorize the caller and derive `key`/`scope` before calling the client.
|
package/llms.txt
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @vllnt/convex-idempotency
|
|
2
|
+
|
|
3
|
+
> Lease-based idempotency key ledger for retried operations, as a Convex component
|
|
4
|
+
|
|
5
|
+
`@vllnt/convex-idempotency` is a publishable Convex component. It targets `convex@^1.45.0`.
|
|
6
|
+
It stores one sandboxed table - `keys` (unique per `(scope, key)`, holding a `status` of `inflight` or `done`, an opaque host `result`, and an `expiresAt` deadline). `begin` mints an inflight claim under a short server-timed lease (`inflightTtlMs`, default 60s) or replays a prior `done` outcome; `complete` returns a discriminated `recorded` outcome and extends a longer done grace window (`doneTtlMs`, default 24h); expired keys are re-minted in place; a bounded, self-rescheduling `purge` (and a built-in daily cron) sweeps them. Time is read from the server clock inside every handler - no caller-supplied `now` - so a hostile clock cannot force a key to look live or expired; the only time input is `purge`'s optional `before` cutoff, which must not come from client input. The ledger does not guarantee exactly-once external side effects: use the provider's idempotency key too. Dedup rides the Convex mutation transaction. The host owns the operation, auth, and the result type (the client is generic over it, with an optional `resultValidator` narrowing the opaque stored value at the boundary); the component owns only the dedup ledger.
|
|
7
|
+
|
|
8
|
+
## For coding agents
|
|
9
|
+
|
|
10
|
+
The docs are bundled in the npm package: `node_modules/@vllnt/convex-idempotency/docs/API.md`, with `README.md`, `CHANGELOG.md`, this `llms.txt` and the typed `src/` at the package root. They match the installed version. Read them before writing code.
|
|
11
|
+
|
|
12
|
+
## Docs
|
|
13
|
+
|
|
14
|
+
- [README](./README.md): Project overview, installation, usage examples, API summary
|
|
15
|
+
- [API Reference](./docs/API.md): Public class methods, return shapes, input validation
|
|
16
|
+
- [Changelog](./CHANGELOG.md): Version history and release notes
|
|
17
|
+
- [License](./LICENSE): MIT license
|
|
18
|
+
|
|
19
|
+
## Ecosystem
|
|
20
|
+
|
|
21
|
+
- [vllnt oss](https://oss.vllnt.com/llms.txt): Index of the vllnt open-source packages
|
|
22
|
+
|
|
23
|
+
## Optional
|
|
24
|
+
|
|
25
|
+
Repository only, not in the npm package:
|
|
26
|
+
|
|
27
|
+
- [Example component wrapper](https://github.com/vllnt/convex-idempotency/blob/main/example/convex/example.ts): Host-app wrapper functions
|
|
28
|
+
- [Example tests](https://github.com/vllnt/convex-idempotency/blob/main/example/convex/example.test.ts): End-to-end usage coverage with `convex-test`
|
|
29
|
+
- [Contributing](https://github.com/vllnt/convex-idempotency/blob/main/CONTRIBUTING.md): Development setup, validation commands, release workflow
|
|
30
|
+
- [AGENTS](https://github.com/vllnt/convex-idempotency/blob/main/AGENTS.md): Contributor instructions for this repository
|
|
31
|
+
- [Security Policy](https://github.com/vllnt/convex-idempotency/blob/main/SECURITY.md): Vulnerability reporting policy
|
|
32
|
+
- [Code of Conduct](https://github.com/vllnt/convex-idempotency/blob/main/CODE_OF_CONDUCT.md): Community participation guidelines
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-idempotency",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1-canary.28c3e26",
|
|
4
|
+
"description": "Lease-based idempotency key ledger for retried operations, as a Convex component",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@10.34.8",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"docs/API.md",
|
|
12
|
+
"CHANGELOG.md",
|
|
13
|
+
"llms.txt",
|
|
14
|
+
"!src/**/*.test.*",
|
|
15
|
+
"!src/**/*.spec.*"
|
|
11
16
|
],
|
|
12
17
|
"exports": {
|
|
13
18
|
"./package.json": "./package.json",
|
|
@@ -37,17 +42,15 @@
|
|
|
37
42
|
"build": "tsc --project ./tsconfig.build.json",
|
|
38
43
|
"build:codegen": "pnpm codegen && pnpm build",
|
|
39
44
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
40
|
-
"typecheck": "tsc --noEmit",
|
|
41
|
-
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
42
|
-
"lint": "pnpm check:agents && eslint .",
|
|
45
|
+
"typecheck": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck.tsbuildinfo",
|
|
46
|
+
"typecheck:ci": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-ci.tsbuildinfo --project tsconfig.ci.json",
|
|
47
|
+
"lint": "pnpm check:agents && eslint --cache --cache-location node_modules/.cache/eslint/ .",
|
|
43
48
|
"test": "vitest run --passWithNoTests",
|
|
44
49
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
45
50
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
46
51
|
"generate:llms": "node scripts/generate-llms.mjs",
|
|
52
|
+
"test:package": "node scripts/test-package.mjs",
|
|
47
53
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
48
|
-
"prepublishOnly": "npm whoami || npm login",
|
|
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",
|
|
51
54
|
"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
55
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
53
56
|
},
|
|
@@ -57,18 +60,33 @@
|
|
|
57
60
|
"devDependencies": {
|
|
58
61
|
"@edge-runtime/vm": "^5.0.0",
|
|
59
62
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
60
|
-
"@vitest/coverage-v8": "^
|
|
61
|
-
"@vllnt/eslint-config": "2.
|
|
63
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
64
|
+
"@vllnt/eslint-config": "2.1.0",
|
|
62
65
|
"@vllnt/typescript": "2.0.0",
|
|
63
|
-
"convex": "^1.
|
|
64
|
-
"convex-test": "^0.0.
|
|
66
|
+
"convex": "^1.50.0",
|
|
67
|
+
"convex-test": "^0.0.71",
|
|
65
68
|
"eslint": "^9.39.5",
|
|
66
|
-
"prettier": "^3.
|
|
69
|
+
"prettier": "^3.10.1",
|
|
67
70
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
68
|
-
"vite": "^8.
|
|
69
|
-
"vitest": "^
|
|
71
|
+
"vite": "^8.3.8",
|
|
72
|
+
"vitest": "^5.1.0"
|
|
70
73
|
},
|
|
71
|
-
"
|
|
74
|
+
"pnpm": {
|
|
75
|
+
"minimumReleaseAge": 10080,
|
|
76
|
+
"minimumReleaseAgeExclude": [
|
|
77
|
+
"@vllnt/*"
|
|
78
|
+
],
|
|
79
|
+
"ignoredBuiltDependencies": [
|
|
80
|
+
"esbuild"
|
|
81
|
+
],
|
|
82
|
+
"overrides": {
|
|
83
|
+
"minimatch@>=7.0.0 <11.0.1": "11.0.5",
|
|
84
|
+
"deepmerge-ts@<8.0.0": "8.0.2",
|
|
85
|
+
"esbuild@>=0.27.3 <0.28.1": "0.28.1",
|
|
86
|
+
"handlebars@<4.7.10": "4.7.10"
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"homepage": "https://oss.vllnt.com/packages/convex-idempotency",
|
|
72
90
|
"bugs": {
|
|
73
91
|
"url": "https://github.com/vllnt/convex-idempotency/issues"
|
|
74
92
|
},
|
|
@@ -94,6 +112,10 @@
|
|
|
94
112
|
"keywords": [
|
|
95
113
|
"convex",
|
|
96
114
|
"convex-component",
|
|
97
|
-
"idempotency"
|
|
115
|
+
"idempotency",
|
|
116
|
+
"idempotency-key",
|
|
117
|
+
"deduplication",
|
|
118
|
+
"retries",
|
|
119
|
+
"lease"
|
|
98
120
|
]
|
|
99
121
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -105,7 +105,7 @@ interface CompleteOptions {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
* Consumer-facing client for the
|
|
108
|
+
* Consumer-facing client for the lease-based idempotency key ledger. The host
|
|
109
109
|
* owns meaning and auth; it passes an opaque `key` and an optional `scope`, and
|
|
110
110
|
* stores an arbitrary `TResult` outcome that replays return verbatim. Pass a
|
|
111
111
|
* `resultValidator` to narrow the opaque stored value to `TResult` at the
|
|
@@ -116,7 +116,7 @@ interface CompleteOptions {
|
|
|
116
116
|
* @example
|
|
117
117
|
* ```ts
|
|
118
118
|
* const idem = new Idempotency(components.idempotency, {
|
|
119
|
-
* resultValidator:
|
|
119
|
+
* resultValidator: ChargeResult.parse, // e.g. a Zod schema, or a hand-written guard
|
|
120
120
|
* });
|
|
121
121
|
* const r = await idem.begin(ctx, requestId);
|
|
122
122
|
* if (r.state === "done") return r.result; // typed replay
|
|
@@ -189,8 +189,8 @@ export class Idempotency<TResult = unknown> {
|
|
|
189
189
|
* Mark `key` `done`, recording `result` and extending the done grace window.
|
|
190
190
|
* Returns a discriminated outcome: `recorded: true` on success, or
|
|
191
191
|
* `recorded: false` with a `reason` when the claim was lost (missing, expired,
|
|
192
|
-
* or already done) so the host can react to dropped work.
|
|
193
|
-
* against the host validator before it is stored.
|
|
192
|
+
* superseded, or already done) so the host can react to dropped work.
|
|
193
|
+
* `result` is validated against the host validator before it is stored.
|
|
194
194
|
*/
|
|
195
195
|
complete(
|
|
196
196
|
ctx: RunMutationCtx,
|
|
@@ -232,8 +232,9 @@ export class Idempotency<TResult = unknown> {
|
|
|
232
232
|
|
|
233
233
|
/**
|
|
234
234
|
* Delete expired keys in bounded batches, oldest first. `before` defaults to
|
|
235
|
-
* the server clock
|
|
236
|
-
*
|
|
235
|
+
* the server clock (a future `before` also deletes live keys); `batch` caps
|
|
236
|
+
* each pass and the sweep self-reschedules until the tail is clean. Returns
|
|
237
|
+
* the count removed in the first pass.
|
|
237
238
|
*/
|
|
238
239
|
purge(
|
|
239
240
|
ctx: RunMutationCtx,
|
package/src/client/types.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Validates and narrows an opaque stored `result` to the host's `TResult` at the
|
|
5
5
|
* client boundary. Receives the raw value the component replayed (`unknown`) and
|
|
6
|
-
* MUST return a typed `TResult` or throw. A
|
|
7
|
-
* (
|
|
6
|
+
* MUST return a typed `TResult` or throw. A Zod `.parse` or a hand-written type
|
|
7
|
+
* guard fits directly (`convex/values` validators have no `.parse`); omit it to
|
|
8
|
+
* keep results unvalidated.
|
|
8
9
|
*
|
|
9
10
|
* @typeParam TResult - The host's stored outcome type.
|
|
10
11
|
*/
|
|
@@ -44,7 +45,8 @@ export type CompleteResult =
|
|
|
44
45
|
recorded: false;
|
|
45
46
|
/**
|
|
46
47
|
* `missing` — no key for this id/scope. `expired` — the inflight lease
|
|
47
|
-
* lapsed before completion. `
|
|
48
|
+
* lapsed before completion. `superseded` — a newer claim replaced this
|
|
49
|
+
* worker's lease. `already_done` — a prior attempt won the race.
|
|
48
50
|
*/
|
|
49
51
|
reason: "missing" | "expired" | "superseded" | "already_done";
|
|
50
52
|
};
|
package/src/component/crons.ts
CHANGED