@vllnt/convex-slugs 0.2.0 → 0.2.1-canary.0bc8b6e

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 CHANGED
@@ -4,6 +4,30 @@ All notable changes to this project are documented here. The format is based on
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
5
5
  adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [Unreleased]
8
+
9
+ ### Fixed
10
+
11
+ - Slugs are normalized with NFC again after lowercasing, so look-alike spellings such as `"H̱"`
12
+ and `"ẖ"` are one slug, and the slug `slugForResource` returns resolves back to its resource.
13
+ Rows stored by 0.2.0 in the old form keep resolving with the input that reserved them.
14
+ - New internal mutation `normalizeStoredSlugs` moves those old rows to the new form in one run
15
+ (`npx convex run --component slugs mutations:normalizeStoredSlugs '{}'`). It leaves and reports a
16
+ row whose new form another resource already holds, with both resources and both forms, in its
17
+ result and in the deployment logs. A redirect from an old name whose new form is a live slug is
18
+ deleted, as `reserve` does, so links to that name now reach the live slug instead of
19
+ redirecting.
20
+ - Renaming an old-form row to its new form no longer records a redirect to itself.
21
+ - Length, pattern and reserved-word checks in `reserve` and `rename` see the composed form. For
22
+ these letters the slug can be shorter than the input: with `minLength: 2`, `"H̱"` (2 characters
23
+ typed, 1 once composed) is now `SLUG_INVALID`, and a `pattern` that only allowed the decomposed
24
+ spelling now rejects it.
25
+ - The docs now say that `rename` and `release` update every redirect into the slug in one
26
+ mutation, so a resource renamed many thousands of times can hit Convex limits.
27
+ - Docs: in `docs/API.md`, the step that settles a `normalizeStoredSlugs` conflict by renaming now
28
+ ends with running `normalizeStoredSlugs` again, which deletes the leftover redirect from the old
29
+ name.
30
+
7
31
  ## [0.2.0] - 2026-11-28
8
32
 
9
33
  ### Fixed
package/README.md CHANGED
@@ -224,8 +224,9 @@ the component runs.
224
224
  ## Limits and gotchas
225
225
 
226
226
  - Slugs are normalized before every write and lookup: Unicode NFC, then
227
- `trim()`, then `toLowerCase()` unless `foldCase: false`. `" Ada "` and `"ada"`
228
- are the same slug by default. NFC does not merge look-alike characters from
227
+ `trim()`, then `toLowerCase()` unless `foldCase: false`, then NFC again (a
228
+ lowercased letter can compose with a following mark: `"H̱"` and `"ẖ"` are
229
+ one slug). `" Ada "` and `"ada"` are the same slug by default. NFC does not merge look-alike characters from
229
230
  different scripts, compatibility forms (NFKC) or zero-width characters; use
230
231
  `pattern` to restrict the alphabet.
231
232
  - `minLength` and `maxLength` count UTF-16 code units of the normalized slug
@@ -269,6 +270,26 @@ the component runs.
269
270
  - Rows that 0.1.0 stored under scope `""` can still be read with `resolve`,
270
271
  `redirectFor` and `slugForResource`, but `reserve`, `release` and `rename`
271
272
  reject `scope: ""` with `INVALID_ARGUMENT`.
273
+ - Up to 0.2.0 the last NFC step was missing, so a slug such as `"H̱"` was
274
+ stored as `"h"` + U+0331 instead of `"ẖ"`. Those rows keep working: every
275
+ lookup tries the key 0.2.0 used first, then the stored form, so the input
276
+ that reserved such a row still resolves, renames and releases it. Renaming or
277
+ re-reserving it stores the new form. Until then, neither the old form that
278
+ `slugForResource` returns nor `"ẖ"` typed as one character matches the row,
279
+ so another resource can reserve that spelling. To move every such row to the
280
+ new form, run the fix-up once after upgrading:
281
+ `npx convex run --component slugs mutations:normalizeStoredSlugs '{}'` (add
282
+ `--prod` for production). It pages through both tables on its own and is
283
+ safe to run again. A row whose new form another resource already holds is
284
+ left as is and reported as a conflict: the call returns the first page's
285
+ conflicts, and every page logs its own as
286
+ `normalizeStoredSlugs: NFC form already taken {...}`, so check the
287
+ deployment logs after the run. See
288
+ [docs/API.md](docs/API.md#normalization) for how to settle one.
289
+ - `rename` and `release` update every redirect that points at the slug in one
290
+ mutation, and each rename of a resource adds one. A resource renamed many
291
+ thousands of times can exceed Convex's per-mutation read or write limits, and
292
+ then neither call succeeds for it.
272
293
  - There is no list, search or history API. The component stores only the
273
294
  current holder of each slug and one redirect per old name.
274
295
 
@@ -71,7 +71,10 @@ export declare class Slugs {
71
71
  * `defaultScope` or invalid length bounds.
72
72
  */
73
73
  constructor(component: SlugsComponent, options?: SlugsOptions);
74
+ /** The form a slug is validated and stored in. */
74
75
  private norm;
76
+ /** The key sent to the component, which also matches rows stored by 0.2.0. */
77
+ private key;
75
78
  private scopeOf;
76
79
  private get bounds();
77
80
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAUhE,wEAAwE;AACxE,UAAU,YAAY;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE;QACT,OAAO,EAAE,iBAAiB,CACxB,UAAU,EACV,UAAU,EACV;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,WAAW,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,YAAY,CAAA;SAAE,EAC1E,eAAe,CAChB,CAAC;QACF,OAAO,EAAE,iBAAiB,CACxB,UAAU,EACV,UAAU,EACV;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,EAC/B,IAAI,CACL,CAAC;QACF,MAAM,EAAE,iBAAiB,CACvB,UAAU,EACV,UAAU,EACV;YAAE,KAAK,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,YAAY,CAAA;SAAE,EACzE,eAAe,CAChB,CAAC;KACH,CAAC;IACF,OAAO,EAAE;QACP,OAAO,EAAE,iBAAiB,CACxB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,EAChB,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,eAAe,CAAC,CAW1B;IAED;;;;;;OAMG;IACH,OAAO,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAK9E;IAED;;;;;;;OAOG;IACH,WAAW,CACT,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,MAAM,EACZ,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAKxB;IAED;;;;;;OAMG;IACH,eAAe,CACb,GAAG,EAAE,WAAW,EAChB,WAAW,EAAE,MAAM,EACnB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAKxB;CACF;AAED,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { ConvexError } from "convex/values";
2
- import { DEFAULT_MAX_LENGTH, DEFAULT_MIN_LENGTH, DEFAULT_SCOPE, normalizeSlug, validateSlug, } from "../shared.js";
2
+ import { DEFAULT_MAX_LENGTH, DEFAULT_MIN_LENGTH, DEFAULT_SCOPE, normalizeSlug, slugKey, validateSlug, } from "../shared.js";
3
3
  /**
4
4
  * Consumer-facing client for the unique slug / handle registry. The host owns
5
5
  * meaning and auth; it passes an opaque `resourceRef` and an optional `scope`.
@@ -45,9 +45,14 @@ export class Slugs {
45
45
  reservedWords: (options.reservedWords ?? []).map((w) => this.norm(w)),
46
46
  };
47
47
  }
48
+ /** The form a slug is validated and stored in. */
48
49
  norm(slug) {
49
50
  return normalizeSlug(slug, this.foldCase);
50
51
  }
52
+ /** The key sent to the component, which also matches rows stored by 0.2.0. */
53
+ key(slug) {
54
+ return slugKey(slug, this.foldCase);
55
+ }
51
56
  scopeOf(scope) {
52
57
  return scope ?? this.defaultScope;
53
58
  }
@@ -68,14 +73,13 @@ export class Slugs {
68
73
  * @param scope - Namespace; defaults to `defaultScope`.
69
74
  */
70
75
  reserve(ctx, slug, resourceRef, scope) {
71
- const normalized = this.norm(slug);
72
- const reason = validateSlug(normalized, this.constraints);
76
+ const reason = validateSlug(this.norm(slug), this.constraints);
73
77
  if (reason !== null) {
74
78
  return Promise.resolve({ ok: false, reason });
75
79
  }
76
80
  return ctx.runMutation(this.component.mutations.reserve, {
77
81
  scope: this.scopeOf(scope),
78
- slug: normalized,
82
+ slug: this.key(slug),
79
83
  resourceRef,
80
84
  bounds: this.bounds,
81
85
  });
@@ -91,7 +95,7 @@ export class Slugs {
91
95
  release(ctx, slug, scope) {
92
96
  return ctx.runMutation(this.component.mutations.release, {
93
97
  scope: this.scopeOf(scope),
94
- slug: this.norm(slug),
98
+ slug: this.key(slug),
95
99
  });
96
100
  }
97
101
  /**
@@ -106,15 +110,14 @@ export class Slugs {
106
110
  * @param scope - Namespace; defaults to `defaultScope`.
107
111
  */
108
112
  rename(ctx, fromSlug, toSlug, scope) {
109
- const normalizedTo = this.norm(toSlug);
110
- const reason = validateSlug(normalizedTo, this.constraints);
113
+ const reason = validateSlug(this.norm(toSlug), this.constraints);
111
114
  if (reason !== null) {
112
115
  return Promise.resolve({ ok: false, reason });
113
116
  }
114
117
  return ctx.runMutation(this.component.mutations.rename, {
115
118
  scope: this.scopeOf(scope),
116
- fromSlug: this.norm(fromSlug),
117
- toSlug: normalizedTo,
119
+ fromSlug: this.key(fromSlug),
120
+ toSlug: this.key(toSlug),
118
121
  bounds: this.bounds,
119
122
  });
120
123
  }
@@ -128,7 +131,7 @@ export class Slugs {
128
131
  resolve(ctx, slug, scope) {
129
132
  return ctx.runQuery(this.component.queries.resolve, {
130
133
  scope: this.scopeOf(scope),
131
- slug: this.norm(slug),
134
+ slug: this.key(slug),
132
135
  });
133
136
  }
134
137
  /**
@@ -142,7 +145,7 @@ export class Slugs {
142
145
  redirectFor(ctx, slug, scope) {
143
146
  return ctx.runQuery(this.component.queries.redirectFor, {
144
147
  scope: this.scopeOf(scope),
145
- slug: this.norm(slug),
148
+ slug: this.key(slug),
146
149
  });
147
150
  }
148
151
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAO5C,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,aAAa,EACb,YAAY,GACb,MAAM,cAAc,CAAC;AAsEtB;;;;;;;;;GASG;AACH,MAAM,OAAO,KAAK;IAYG,SAAS;IAXX,YAAY,CAAS;IACrB,QAAQ,CAAU;IAClB,WAAW,CAAkB;IAE9C;;;;;OAKG;IACH,YACmB,SAAyB,EAC1C,OAAO,GAAiB,EAAE;yBADT,SAAS;QAG1B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,aAAa,CAAC;QAC1D,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC;QACzC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,kBAAkB,CAAC;QAC1D,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,kBAAkB,CAAC;QAC1D,IACE,IAAI,CAAC,YAAY,KAAK,EAAE;YACxB,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC;YAC5B,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC;YAC5B,SAAS,GAAG,CAAC;YACb,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,CAAC,EAClC,CAAC;YACD,MAAM,IAAI,WAAW,CAAC;gBACpB,IAAI,EAAE,iBAAiB;gBACvB,OAAO,EACL,6IAA6I;aAChJ,CAAC,CAAC;QACL,CAAC;QACD,IAAI,CAAC,WAAW,GAAG;YACjB,SAAS;YACT,SAAS;YACT,OAAO,EAAE,OAAO,CAAC,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AM,CAAC;YACxB,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,OAAO,CAAC,GAAgB,EAAE,IAAY,EAAE,KAAc;QACpD,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE;YAClD,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YAC1B,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;SACrB,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACH,WAAW,CACT,GAAgB,EAChB,IAAY,EACZ,KAAc;QAEd,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,WAAW,EAAE;YACtD,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YAC1B,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;SACrB,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACH,eAAe,CACb,GAAgB,EAChB,WAAmB,EACnB,KAAc;QAEd,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,eAAe,EAAE;YAC1D,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YAC1B,WAAW;SACZ,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Reserve `slug` for `resourceRef` within `scope`. The slug arrives already
3
- * normalized by the client; `bounds` re-guards empty/oversized input at the
4
- * trust boundary. If the slug was a live redirect target's source, that stale
5
- * redirect row is removed (the slug is a live name again).
3
+ * normalized by the client and is stored in its NFC form; `bounds` re-guards
4
+ * empty/oversized input at the trust boundary. If the slug was a live redirect
5
+ * target's source, that stale redirect row is removed (the slug is a live name
6
+ * again).
6
7
  *
7
8
  * @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
8
9
  * `SLUG_INVALID` (empty, outside `bounds` or over {@link MAX_INPUT_LENGTH}) or
@@ -84,6 +85,45 @@ export declare const rename: import("convex/server").RegisteredMutation<"public"
84
85
  reason?: never;
85
86
  ok: boolean;
86
87
  }>>;
88
+ /**
89
+ * A row {@link normalizeStoredSlugs} left as is because its NFC form is taken.
90
+ * `slug` is the value as stored (not NFC); `nfcSlug` is the taken NFC form. For
91
+ * a `slugs` row, `resourceRef` holds `slug` and `heldBy` holds `nfcSlug`. For a
92
+ * `redirects` row, `slug` redirects to `toSlug` and `nfcSlug` to `otherToSlug`.
93
+ */
94
+ interface Conflict {
95
+ table: "slugs" | "redirects";
96
+ scope: string;
97
+ slug: string;
98
+ nfcSlug: string;
99
+ resourceRef?: string;
100
+ heldBy?: string;
101
+ toSlug?: string;
102
+ otherToSlug?: string;
103
+ }
104
+ /**
105
+ * One-off fix-up for rows stored by 0.2.0 and earlier, which could hold a slug
106
+ * that is not in NFC form (a letter that composes only after lowercasing, such
107
+ * as "h" + U+0331 for "H̱"). Pages through `slugs`, then `redirects`, and moves
108
+ * each such slug or old name to its NFC form. A row whose NFC form another row
109
+ * already holds is left as is and reported in `conflicts` (and logged), so the
110
+ * host can decide which resource keeps the name. Idempotent: rows already in
111
+ * NFC form are skipped. Schedules itself for the next page until both tables
112
+ * are done, so one call from `npx convex run` fixes everything.
113
+ *
114
+ * @returns This page's counts: rows `fixed`, `conflicts` left, and whether this
115
+ * was the last page of the last table.
116
+ * @throws `INVALID_ARGUMENT` when `batch` is not an integer from 1 to 500.
117
+ */
118
+ export declare const normalizeStoredSlugs: import("convex/server").RegisteredMutation<"internal", {
119
+ batch?: number;
120
+ cursor?: string | null;
121
+ table?: "redirects" | "slugs";
122
+ }, Promise<{
123
+ fixed: number;
124
+ conflicts: Conflict[];
125
+ isDone: boolean;
126
+ }>>;
87
127
  /**
88
128
  * Test/repair escape hatch: insert a `slugs` row with no uniqueness check.
89
129
  * `internalMutation`, so it is never part of the public client API. Used to
@@ -110,4 +150,5 @@ export declare const insertRedirectRaw: import("convex/server").RegisteredMutati
110
150
  scope: string;
111
151
  toSlug: string;
112
152
  }, Promise<null>>;
153
+ export {};
113
154
  //# sourceMappingURL=mutations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAwDA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,OAAO;;;;;;;;;;YAWc,cAAc;;;YASd,YAAY;;;;GAkB5C,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO;;;iBAyBlB,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;YAWe,YAAY;;;YAGZ,cAAc;;;YASd,gBAAgB;;;;GAoDhD,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS;;;;iBAWpB,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB;;;;iBAW5B,CAAC"}
1
+ {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAqGA;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,OAAO;;;;;;;;;;YAYc,cAAc;;;YAGd,YAAY;;;;GAU5C,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,OAAO;;;iBAclB,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;YAWe,YAAY;;;YAIZ,cAAc;;;YAId,gBAAgB;;;;GA4ChD,CAAC;AAMH;;;;;GAKG;AACH,UAAU,QAAQ;IAChB,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAmHD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,oBAAoB;;;;;WAcX,MAAM;eAAa,QAAQ,EAAE;YAAU,OAAO;GA+ClE,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS;;;;iBAWpB,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB;;;;iBAW5B,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import { ConvexError, v } from "convex/values";
2
+ import { internal } from "./_generated/api.js";
2
3
  import { internalMutation, mutation } from "./_generated/server.js";
3
4
  import { lengthBounds, reserveResult } from "./validators.js";
5
+ import { lookupKeys, storedSlug } from "../shared.js";
4
6
  /**
5
7
  * Hard cap, in UTF-16 code units (`string.length`), on the slug, `scope` and
6
8
  * `resourceRef` that `reserve` and `rename` write. It holds whatever `bounds`
@@ -37,11 +39,34 @@ function slugOutOfBounds(slug, bounds) {
37
39
  return (slug.length < Math.max(1, bounds.minLength) ||
38
40
  slug.length > Math.min(bounds.maxLength, MAX_INPUT_LENGTH));
39
41
  }
42
+ /**
43
+ * The row holding `slug` in `scope`: looked up as sent (rows stored by versions
44
+ * up to 0.2.0), then by its NFC form.
45
+ */
46
+ async function findSlug(ctx, scope, slug) {
47
+ const rows = await Promise.all(lookupKeys(slug).map((key) => ctx.db
48
+ .query("slugs")
49
+ .withIndex("by_scope_slug", (q) => q.eq("scope", scope).eq("slug", key))
50
+ .first()));
51
+ return rows.find((row) => row !== null) ?? null;
52
+ }
53
+ /** Delete every redirect in `scope` whose `field` is one of `keys`. */
54
+ async function deleteRedirects(ctx, scope, field, keys) {
55
+ const index = field === "fromSlug" ? "by_scope_from" : "by_scope_to";
56
+ const rows = await Promise.all(keys.map((key) => ctx.db
57
+ .query("redirects")
58
+ .withIndex(index, (q) => q.eq("scope", scope).eq(field, key))
59
+ .collect()));
60
+ for (const row of rows.flat()) {
61
+ await ctx.db.delete("redirects", row._id);
62
+ }
63
+ }
40
64
  /**
41
65
  * Reserve `slug` for `resourceRef` within `scope`. The slug arrives already
42
- * normalized by the client; `bounds` re-guards empty/oversized input at the
43
- * trust boundary. If the slug was a live redirect target's source, that stale
44
- * redirect row is removed (the slug is a live name again).
66
+ * normalized by the client and is stored in its NFC form; `bounds` re-guards
67
+ * empty/oversized input at the trust boundary. If the slug was a live redirect
68
+ * target's source, that stale redirect row is removed (the slug is a live name
69
+ * again).
45
70
  *
46
71
  * @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
47
72
  * `SLUG_INVALID` (empty, outside `bounds` or over {@link MAX_INPUT_LENGTH}) or
@@ -58,26 +83,17 @@ export const reserve = mutation({
58
83
  returns: reserveResult,
59
84
  handler: async (ctx, args) => {
60
85
  assertArgs(args);
61
- if (slugOutOfBounds(args.slug, args.bounds)) {
86
+ const slug = storedSlug(args.slug);
87
+ if (slugOutOfBounds(slug, args.bounds)) {
62
88
  return { ok: false, reason: "SLUG_INVALID" };
63
89
  }
64
- const existing = await ctx.db
65
- .query("slugs")
66
- .withIndex("by_scope_slug", (q) => q.eq("scope", args.scope).eq("slug", args.slug))
67
- .first();
68
- if (existing !== null) {
90
+ if ((await findSlug(ctx, args.scope, args.slug)) !== null) {
69
91
  return { ok: false, reason: "SLUG_TAKEN" };
70
92
  }
71
- const dangling = await ctx.db
72
- .query("redirects")
73
- .withIndex("by_scope_from", (q) => q.eq("scope", args.scope).eq("fromSlug", args.slug))
74
- .collect();
75
- for (const row of dangling) {
76
- await ctx.db.delete("redirects", row._id);
77
- }
93
+ await deleteRedirects(ctx, args.scope, "fromSlug", lookupKeys(args.slug));
78
94
  await ctx.db.insert("slugs", {
79
95
  scope: args.scope,
80
- slug: args.slug,
96
+ slug,
81
97
  resourceRef: args.resourceRef,
82
98
  });
83
99
  return { ok: true };
@@ -97,20 +113,12 @@ export const release = mutation({
97
113
  returns: v.null(),
98
114
  handler: async (ctx, args) => {
99
115
  assertArgs(args);
100
- const existing = await ctx.db
101
- .query("slugs")
102
- .withIndex("by_scope_slug", (q) => q.eq("scope", args.scope).eq("slug", args.slug))
103
- .first();
116
+ const existing = await findSlug(ctx, args.scope, args.slug);
104
117
  if (existing !== null) {
105
118
  await ctx.db.delete("slugs", existing._id);
106
119
  }
107
- const inbound = await ctx.db
108
- .query("redirects")
109
- .withIndex("by_scope_to", (q) => q.eq("scope", args.scope).eq("toSlug", args.slug))
110
- .collect();
111
- for (const row of inbound) {
112
- await ctx.db.delete("redirects", row._id);
113
- }
120
+ const released = existing === null ? lookupKeys(args.slug) : [existing.slug];
121
+ await deleteRedirects(ctx, args.scope, "toSlug", released);
114
122
  return null;
115
123
  },
116
124
  });
@@ -150,57 +158,221 @@ export const rename = mutation({
150
158
  if (args.fromSlug === args.toSlug) {
151
159
  return { ok: false, reason: "SLUG_TAKEN" };
152
160
  }
153
- if (slugOutOfBounds(args.toSlug, args.bounds)) {
161
+ const toSlug = storedSlug(args.toSlug);
162
+ if (slugOutOfBounds(toSlug, args.bounds)) {
154
163
  return { ok: false, reason: "SLUG_INVALID" };
155
164
  }
156
- const from = await ctx.db
157
- .query("slugs")
158
- .withIndex("by_scope_slug", (q) => q.eq("scope", args.scope).eq("slug", args.fromSlug))
159
- .first();
165
+ const from = await findSlug(ctx, args.scope, args.fromSlug);
160
166
  if (from === null) {
161
167
  return { ok: false, reason: "SLUG_NOT_FOUND" };
162
168
  }
163
- const taken = await ctx.db
164
- .query("slugs")
165
- .withIndex("by_scope_slug", (q) => q.eq("scope", args.scope).eq("slug", args.toSlug))
166
- .first();
167
- if (taken !== null) {
169
+ if ((await findSlug(ctx, args.scope, args.toSlug)) !== null) {
168
170
  return { ok: false, reason: "SLUG_TAKEN" };
169
171
  }
170
- await ctx.db.patch("slugs", from._id, { slug: args.toSlug });
172
+ // The key the row was stored under (the 0.2.0 form for an older row).
173
+ const fromSlug = from.slug;
174
+ await ctx.db.patch("slugs", from._id, { slug: toSlug });
171
175
  // `toSlug` is a live name again: drop its own redirect first, so renaming
172
176
  // back (A->B, B->A) cannot repoint it into a self-loop A->A below.
173
- const stale = await ctx.db
174
- .query("redirects")
175
- .withIndex("by_scope_from", (q) => q.eq("scope", args.scope).eq("fromSlug", args.toSlug))
176
- .collect();
177
- for (const row of stale) {
178
- await ctx.db.delete("redirects", row._id);
179
- }
177
+ await deleteRedirects(ctx, args.scope, "fromSlug", lookupKeys(args.toSlug));
180
178
  const inbound = await ctx.db
181
179
  .query("redirects")
182
- .withIndex("by_scope_to", (q) => q.eq("scope", args.scope).eq("toSlug", args.fromSlug))
180
+ .withIndex("by_scope_to", (q) => q.eq("scope", args.scope).eq("toSlug", fromSlug))
183
181
  .collect();
184
182
  for (const row of inbound) {
185
- await ctx.db.patch("redirects", row._id, { toSlug: args.toSlug });
183
+ await ctx.db.patch("redirects", row._id, { toSlug });
184
+ }
185
+ // Moving a 0.2.0 row to its NFC form keeps the same name: no redirect.
186
+ if (storedSlug(fromSlug) === toSlug) {
187
+ return { ok: true };
186
188
  }
187
189
  const existing = await ctx.db
188
190
  .query("redirects")
189
- .withIndex("by_scope_from", (q) => q.eq("scope", args.scope).eq("fromSlug", args.fromSlug))
191
+ .withIndex("by_scope_from", (q) => q.eq("scope", args.scope).eq("fromSlug", fromSlug))
190
192
  .first();
191
193
  if (existing !== null) {
192
- await ctx.db.patch("redirects", existing._id, { toSlug: args.toSlug });
194
+ await ctx.db.patch("redirects", existing._id, { toSlug });
193
195
  }
194
196
  else {
195
197
  await ctx.db.insert("redirects", {
196
198
  scope: args.scope,
197
- fromSlug: args.fromSlug,
198
- toSlug: args.toSlug,
199
+ fromSlug,
200
+ toSlug,
199
201
  });
200
202
  }
201
203
  return { ok: true };
202
204
  },
203
205
  });
206
+ /** Default and largest page size of {@link normalizeStoredSlugs}. */
207
+ const NORMALIZE_BATCH = 100;
208
+ const MAX_NORMALIZE_BATCH = 500;
209
+ const conflictValidator = v.object({
210
+ table: v.union(v.literal("slugs"), v.literal("redirects")),
211
+ scope: v.string(),
212
+ slug: v.string(),
213
+ nfcSlug: v.string(),
214
+ resourceRef: v.optional(v.string()),
215
+ heldBy: v.optional(v.string()),
216
+ toSlug: v.optional(v.string()),
217
+ otherToSlug: v.optional(v.string()),
218
+ });
219
+ /**
220
+ * JSON with every non-ASCII UTF-16 code unit escaped as `\uXXXX`, so an old
221
+ * form and its NFC form differ visibly in the logs and the values can be
222
+ * pasted into `npx convex run` arguments as is.
223
+ */
224
+ function asciiJson(value) {
225
+ let out = "";
226
+ for (const unit of JSON.stringify(value).split("")) {
227
+ const code = unit.charCodeAt(0);
228
+ out += code < 0x7f ? unit : `\\u${code.toString(16).padStart(4, "0")}`;
229
+ }
230
+ return out;
231
+ }
232
+ /**
233
+ * Move one `slugs` row stored by 0.2.0 to its NFC form, with the redirects that
234
+ * point at it. Returns the conflict, and changes nothing, when another row
235
+ * already holds the NFC form.
236
+ */
237
+ async function normalizeSlugRow(ctx, row) {
238
+ const slug = storedSlug(row.slug);
239
+ if (slug === row.slug) {
240
+ return "unchanged";
241
+ }
242
+ const holder = await ctx.db
243
+ .query("slugs")
244
+ .withIndex("by_scope_slug", (q) => q.eq("scope", row.scope).eq("slug", slug))
245
+ .first();
246
+ if (holder !== null) {
247
+ return {
248
+ table: "slugs",
249
+ scope: row.scope,
250
+ slug: row.slug,
251
+ nfcSlug: slug,
252
+ resourceRef: row.resourceRef,
253
+ heldBy: holder.resourceRef,
254
+ };
255
+ }
256
+ await ctx.db.patch("slugs", row._id, { slug });
257
+ const inbound = await ctx.db
258
+ .query("redirects")
259
+ .withIndex("by_scope_to", (q) => q.eq("scope", row.scope).eq("toSlug", row.slug))
260
+ .collect();
261
+ for (const redirect of inbound) {
262
+ await ctx.db.patch("redirects", redirect._id, { toSlug: slug });
263
+ }
264
+ // The NFC form is a live name now, so no redirect may start at it.
265
+ await deleteRedirects(ctx, row.scope, "fromSlug", [slug]);
266
+ return "fixed";
267
+ }
268
+ /**
269
+ * Move one `redirects` row whose old name 0.2.0 stored to the NFC form of that
270
+ * name. A redirect from a name that is live again is deleted, as `reserve`
271
+ * does. Returns the conflict, and changes nothing, when another redirect already
272
+ * starts at the NFC form.
273
+ */
274
+ async function normalizeRedirectRow(ctx, row) {
275
+ const fromSlug = storedSlug(row.fromSlug);
276
+ if (fromSlug === row.fromSlug) {
277
+ return "unchanged";
278
+ }
279
+ const [live, other] = await Promise.all([
280
+ ctx.db
281
+ .query("slugs")
282
+ .withIndex("by_scope_slug", (q) => q.eq("scope", row.scope).eq("slug", fromSlug))
283
+ .first(),
284
+ ctx.db
285
+ .query("redirects")
286
+ .withIndex("by_scope_from", (q) => q.eq("scope", row.scope).eq("fromSlug", fromSlug))
287
+ .first(),
288
+ ]);
289
+ if (live !== null) {
290
+ await ctx.db.delete("redirects", row._id);
291
+ return "fixed";
292
+ }
293
+ if (other !== null) {
294
+ return {
295
+ table: "redirects",
296
+ scope: row.scope,
297
+ slug: row.fromSlug,
298
+ nfcSlug: fromSlug,
299
+ toSlug: row.toSlug,
300
+ otherToSlug: other.toSlug,
301
+ };
302
+ }
303
+ await ctx.db.patch("redirects", row._id, { fromSlug });
304
+ return "fixed";
305
+ }
306
+ /**
307
+ * One-off fix-up for rows stored by 0.2.0 and earlier, which could hold a slug
308
+ * that is not in NFC form (a letter that composes only after lowercasing, such
309
+ * as "h" + U+0331 for "H̱"). Pages through `slugs`, then `redirects`, and moves
310
+ * each such slug or old name to its NFC form. A row whose NFC form another row
311
+ * already holds is left as is and reported in `conflicts` (and logged), so the
312
+ * host can decide which resource keeps the name. Idempotent: rows already in
313
+ * NFC form are skipped. Schedules itself for the next page until both tables
314
+ * are done, so one call from `npx convex run` fixes everything.
315
+ *
316
+ * @returns This page's counts: rows `fixed`, `conflicts` left, and whether this
317
+ * was the last page of the last table.
318
+ * @throws `INVALID_ARGUMENT` when `batch` is not an integer from 1 to 500.
319
+ */
320
+ export const normalizeStoredSlugs = internalMutation({
321
+ args: {
322
+ table: v.optional(v.union(v.literal("slugs"), v.literal("redirects"))),
323
+ cursor: v.optional(v.union(v.string(), v.null())),
324
+ batch: v.optional(v.number()),
325
+ },
326
+ returns: v.object({
327
+ fixed: v.number(),
328
+ conflicts: v.array(conflictValidator),
329
+ isDone: v.boolean(),
330
+ }),
331
+ handler: async (ctx, args) => {
332
+ const table = args.table ?? "slugs";
333
+ const batch = args.batch ?? NORMALIZE_BATCH;
334
+ if (!Number.isInteger(batch) || batch < 1 || batch > MAX_NORMALIZE_BATCH) {
335
+ throw new ConvexError({
336
+ code: "INVALID_ARGUMENT",
337
+ message: `batch must be an integer from 1 to ${String(MAX_NORMALIZE_BATCH)}`,
338
+ });
339
+ }
340
+ const paginationOpts = { cursor: args.cursor ?? null, numItems: batch };
341
+ const outcomes = [];
342
+ let page;
343
+ if (table === "slugs") {
344
+ const result = await ctx.db.query("slugs").paginate(paginationOpts);
345
+ for (const row of result.page) {
346
+ outcomes.push(await normalizeSlugRow(ctx, row));
347
+ }
348
+ page = result;
349
+ }
350
+ else {
351
+ const result = await ctx.db.query("redirects").paginate(paginationOpts);
352
+ for (const row of result.page) {
353
+ outcomes.push(await normalizeRedirectRow(ctx, row));
354
+ }
355
+ page = result;
356
+ }
357
+ const fixed = outcomes.filter((outcome) => outcome === "fixed").length;
358
+ const conflicts = outcomes.filter((outcome) => typeof outcome === "object");
359
+ for (const conflict of conflicts) {
360
+ console.warn(`normalizeStoredSlugs: NFC form already taken ${asciiJson(conflict)}`);
361
+ }
362
+ const next = page.isDone
363
+ ? table === "slugs"
364
+ ? { table: "redirects", cursor: null }
365
+ : null
366
+ : { table, cursor: page.continueCursor };
367
+ if (next !== null) {
368
+ await ctx.scheduler.runAfter(0, internal.mutations.normalizeStoredSlugs, {
369
+ ...next,
370
+ batch,
371
+ });
372
+ }
373
+ return { fixed, conflicts, isDone: next === null };
374
+ },
375
+ });
204
376
  /**
205
377
  * Test/repair escape hatch: insert a `slugs` row with no uniqueness check.
206
378
  * `internalMutation`, so it is never part of the public client API. Used to