@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.
@@ -1,8 +1,17 @@
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
+
7
+ function validateKeyAndScope(key: string, scope: string): void {
8
+ if (key === "") {
9
+ throw new ConvexError({ code: "INVALID_KEY", message: "key must not be empty" });
10
+ }
11
+ if (scope === "") {
12
+ throw new ConvexError({ code: "INVALID_SCOPE", message: "scope must not be empty" });
13
+ }
14
+ }
6
15
 
7
16
  function validateBatch(batch: number): void {
8
17
  if (!Number.isFinite(batch) || !Number.isInteger(batch) || batch < 1 || batch > MAX_PURGE_BATCH) {
@@ -26,6 +35,8 @@ function validateBatch(batch: number): void {
26
35
  * correct recovery path — the done grace exists precisely to replay within the
27
36
  * window; outside it the operation may safely re-run.
28
37
  *
38
+ * @throws `ConvexError({ code: "INVALID_KEY" | "INVALID_SCOPE" })` when `key` or
39
+ * `scope` is the empty string.
29
40
  * @throws `ConvexError({ code: "INVALID_TTL" })` when `inflightTtlMs` is not a
30
41
  * positive finite number (≤ 0 or non-finite would produce `expiresAt ≤ now`,
31
42
  * immediately expiring the claim and breaking the dedup window).
@@ -38,6 +49,7 @@ export const begin = mutation({
38
49
  },
39
50
  returns: beginResult,
40
51
  handler: async (ctx, args) => {
52
+ validateKeyAndScope(args.key, args.scope);
41
53
  if (!(args.inflightTtlMs > 0 && isFinite(args.inflightTtlMs))) {
42
54
  throw new ConvexError({
43
55
  code: "INVALID_TTL",
@@ -97,10 +109,11 @@ export const begin = mutation({
97
109
  * Mark `key` `done`, recording `result` and extending the grace window by
98
110
  * `doneTtlMs`. Returns a discriminated outcome so the host can detect a lost
99
111
  * claim: `recorded: false` with `missing` (key never existed), `expired` (the
100
- * inflight lease lapsed before completion), or `already_done` (a prior attempt
101
- * won). When `upsertOnMissing` is set, a `missing`/`expired` key is written as
102
- * `done` anyway — for hosts that would rather record the finished work than drop
103
- * it. Time is server-sourced.
112
+ * inflight lease lapsed before completion), `superseded` (a newer claim replaced
113
+ * this worker's lease), or `already_done` (a prior attempt won). When
114
+ * `upsertOnMissing` is set, a `missing`/`expired` key is written as `done`
115
+ * anyway — for hosts that would rather record the finished work than drop it.
116
+ * Time is server-sourced.
104
117
  *
105
118
  * **Expiry check order (intentional asymmetry):** `complete` checks the
106
119
  * done-state BEFORE expiry. An expired done key therefore returns
@@ -110,6 +123,8 @@ export const begin = mutation({
110
123
  * The `expired` reason is reserved for inflight keys whose lease lapsed before
111
124
  * the holder could complete; it does not apply to keys already marked done.
112
125
  *
126
+ * @throws `ConvexError({ code: "INVALID_KEY" | "INVALID_SCOPE" })` when `key` or
127
+ * `scope` is the empty string.
113
128
  * @throws `ConvexError({ code: "INVALID_TTL" })` when `doneTtlMs` is not a
114
129
  * positive finite number (≤ 0 or non-finite would produce `expiresAt ≤ now`,
115
130
  * immediately expiring the done grace window on creation).
@@ -125,6 +140,7 @@ export const complete = mutation({
125
140
  },
126
141
  returns: completeResult,
127
142
  handler: async (ctx, args) => {
143
+ validateKeyAndScope(args.key, args.scope);
128
144
  if (!(args.doneTtlMs > 0 && isFinite(args.doneTtlMs))) {
129
145
  throw new ConvexError({
130
146
  code: "INVALID_TTL",
@@ -193,11 +209,12 @@ export const complete = mutation({
193
209
  /**
194
210
  * Delete up to `batch` keys whose `expiresAt < before`, oldest first via the
195
211
  * `by_expires` index. `before` defaults to the server clock (`Date.now()`) when
196
- * omitted, so the built-in cron sweeps exactly the keys expired as of the run —
197
- * a caller cannot pass a future cutoff it did not compute. If a full batch was
198
- * removed there may be more, so the sweep self-reschedules through
199
- * `ctx.scheduler` until a short batch signals the tail is clean. Idempotent:
200
- * re-running only ever removes already-expired rows.
212
+ * omitted, so the built-in cron sweeps exactly the keys expired as of the run.
213
+ * An explicit `before` is trusted as given: a future cutoff also deletes live
214
+ * keys, so never derive it from untrusted input. If a full batch was removed
215
+ * there may be more, so the sweep self-reschedules through `ctx.scheduler` until
216
+ * a short batch signals the tail is clean. Idempotent for a past cutoff:
217
+ * re-running it removes nothing new.
201
218
  */
202
219
  export const purge = mutation({
203
220
  args: { before: v.optional(v.number()), batch: v.number() },
@@ -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
 
5
5
  export const get = query({
6
6
  args: { key: v.string(), scope: v.string() },
@@ -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
  /**
6
6
  * Sandboxed tables — the idempotency ledger's own concern. A `key` is unique
@@ -32,8 +32,9 @@ export const beginResult = v.union(
32
32
  /**
33
33
  * Result of {@link complete}. `recorded: true` — the outcome was written.
34
34
  * `recorded: false` — the claim was lost; `reason` distinguishes a never-claimed
35
- * key (`missing`), a lease that expired before completion (`expired`), and a key
36
- * already marked done by a prior attempt (`already_done`). A host that sees
35
+ * key (`missing`), a lease that expired before completion (`expired`), a lease
36
+ * replaced by a newer claim (`superseded`), and a key already marked done by a
37
+ * prior attempt (`already_done`). A host that sees
37
38
  * `recorded: false` knows its work finished but the ledger row was gone.
38
39
  */
39
40
  export const completeResult = v.union(
package/src/test.ts CHANGED
@@ -1,5 +1,6 @@
1
+ /// <reference types="vite/client" />
1
2
  import type { TestConvex } from "convex-test";
2
- import schema from "./component/schema";
3
+ import schema from "./component/schema.js";
3
4
 
4
5
  const modules = import.meta.glob("./component/**/*.ts");
5
6