@vllnt/convex-slugs 0.2.0 → 0.2.1-canary.e77c017
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 +17 -0
- package/README.md +23 -2
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +14 -11
- package/dist/client/index.js.map +1 -1
- package/dist/component/mutations.d.ts +44 -3
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +224 -52
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.d.ts +4 -3
- package/dist/component/queries.d.ts.map +1 -1
- package/dist/component/queries.js +13 -11
- package/dist/component/queries.js.map +1 -1
- package/dist/shared.d.ts +31 -5
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +40 -7
- package/dist/shared.js.map +1 -1
- package/docs/API.md +52 -1
- package/llms.txt +2 -2
- package/package.json +1 -1
- package/src/client/index.ts +15 -10
- package/src/component/mutations.ts +287 -66
- package/src/component/queries.ts +27 -17
- package/src/shared.ts +43 -7
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
|
+
import { internal } from "./_generated/api.js";
|
|
3
|
+
import type { Doc } from "./_generated/dataModel.js";
|
|
2
4
|
import { internalMutation, mutation } from "./_generated/server.js";
|
|
5
|
+
import type { MutationCtx } from "./_generated/server.js";
|
|
3
6
|
import { lengthBounds, reserveResult } from "./validators.js";
|
|
7
|
+
import { lookupKeys, storedSlug } from "../shared.js";
|
|
4
8
|
|
|
5
9
|
/**
|
|
6
10
|
* Hard cap, in UTF-16 code units (`string.length`), on the slug, `scope` and
|
|
@@ -54,11 +58,53 @@ function slugOutOfBounds(
|
|
|
54
58
|
);
|
|
55
59
|
}
|
|
56
60
|
|
|
61
|
+
/**
|
|
62
|
+
* The row holding `slug` in `scope`: looked up as sent (rows stored by versions
|
|
63
|
+
* up to 0.2.0), then by its NFC form.
|
|
64
|
+
*/
|
|
65
|
+
async function findSlug(
|
|
66
|
+
ctx: MutationCtx,
|
|
67
|
+
scope: string,
|
|
68
|
+
slug: string,
|
|
69
|
+
): Promise<Doc<"slugs"> | null> {
|
|
70
|
+
const rows = await Promise.all(
|
|
71
|
+
lookupKeys(slug).map((key) =>
|
|
72
|
+
ctx.db
|
|
73
|
+
.query("slugs")
|
|
74
|
+
.withIndex("by_scope_slug", (q) => q.eq("scope", scope).eq("slug", key))
|
|
75
|
+
.first(),
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
return rows.find((row) => row !== null) ?? null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Delete every redirect in `scope` whose `field` is one of `keys`. */
|
|
82
|
+
async function deleteRedirects(
|
|
83
|
+
ctx: MutationCtx,
|
|
84
|
+
scope: string,
|
|
85
|
+
field: "fromSlug" | "toSlug",
|
|
86
|
+
keys: string[],
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
const index = field === "fromSlug" ? "by_scope_from" : "by_scope_to";
|
|
89
|
+
const rows = await Promise.all(
|
|
90
|
+
keys.map((key) =>
|
|
91
|
+
ctx.db
|
|
92
|
+
.query("redirects")
|
|
93
|
+
.withIndex(index, (q) => q.eq("scope", scope).eq(field, key))
|
|
94
|
+
.collect(),
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
for (const row of rows.flat()) {
|
|
98
|
+
await ctx.db.delete("redirects", row._id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
57
102
|
/**
|
|
58
103
|
* Reserve `slug` for `resourceRef` within `scope`. The slug arrives already
|
|
59
|
-
* normalized by the client; `bounds` re-guards
|
|
60
|
-
* trust boundary. If the slug was a live redirect
|
|
61
|
-
* redirect row is removed (the slug is a live name
|
|
104
|
+
* normalized by the client and is stored in its NFC form; `bounds` re-guards
|
|
105
|
+
* empty/oversized input at the trust boundary. If the slug was a live redirect
|
|
106
|
+
* target's source, that stale redirect row is removed (the slug is a live name
|
|
107
|
+
* again).
|
|
62
108
|
*
|
|
63
109
|
* @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
|
|
64
110
|
* `SLUG_INVALID` (empty, outside `bounds` or over {@link MAX_INPUT_LENGTH}) or
|
|
@@ -75,30 +121,17 @@ export const reserve = mutation({
|
|
|
75
121
|
returns: reserveResult,
|
|
76
122
|
handler: async (ctx, args) => {
|
|
77
123
|
assertArgs(args);
|
|
78
|
-
|
|
124
|
+
const slug = storedSlug(args.slug);
|
|
125
|
+
if (slugOutOfBounds(slug, args.bounds)) {
|
|
79
126
|
return { ok: false, reason: "SLUG_INVALID" as const };
|
|
80
127
|
}
|
|
81
|
-
|
|
82
|
-
.query("slugs")
|
|
83
|
-
.withIndex("by_scope_slug", (q) =>
|
|
84
|
-
q.eq("scope", args.scope).eq("slug", args.slug),
|
|
85
|
-
)
|
|
86
|
-
.first();
|
|
87
|
-
if (existing !== null) {
|
|
128
|
+
if ((await findSlug(ctx, args.scope, args.slug)) !== null) {
|
|
88
129
|
return { ok: false, reason: "SLUG_TAKEN" as const };
|
|
89
130
|
}
|
|
90
|
-
|
|
91
|
-
.query("redirects")
|
|
92
|
-
.withIndex("by_scope_from", (q) =>
|
|
93
|
-
q.eq("scope", args.scope).eq("fromSlug", args.slug),
|
|
94
|
-
)
|
|
95
|
-
.collect();
|
|
96
|
-
for (const row of dangling) {
|
|
97
|
-
await ctx.db.delete("redirects", row._id);
|
|
98
|
-
}
|
|
131
|
+
await deleteRedirects(ctx, args.scope, "fromSlug", lookupKeys(args.slug));
|
|
99
132
|
await ctx.db.insert("slugs", {
|
|
100
133
|
scope: args.scope,
|
|
101
|
-
slug
|
|
134
|
+
slug,
|
|
102
135
|
resourceRef: args.resourceRef,
|
|
103
136
|
});
|
|
104
137
|
return { ok: true };
|
|
@@ -119,24 +152,13 @@ export const release = mutation({
|
|
|
119
152
|
returns: v.null(),
|
|
120
153
|
handler: async (ctx, args) => {
|
|
121
154
|
assertArgs(args);
|
|
122
|
-
const existing = await ctx.
|
|
123
|
-
.query("slugs")
|
|
124
|
-
.withIndex("by_scope_slug", (q) =>
|
|
125
|
-
q.eq("scope", args.scope).eq("slug", args.slug),
|
|
126
|
-
)
|
|
127
|
-
.first();
|
|
155
|
+
const existing = await findSlug(ctx, args.scope, args.slug);
|
|
128
156
|
if (existing !== null) {
|
|
129
157
|
await ctx.db.delete("slugs", existing._id);
|
|
130
158
|
}
|
|
131
|
-
const
|
|
132
|
-
.
|
|
133
|
-
|
|
134
|
-
q.eq("scope", args.scope).eq("toSlug", args.slug),
|
|
135
|
-
)
|
|
136
|
-
.collect();
|
|
137
|
-
for (const row of inbound) {
|
|
138
|
-
await ctx.db.delete("redirects", row._id);
|
|
139
|
-
}
|
|
159
|
+
const released =
|
|
160
|
+
existing === null ? lookupKeys(args.slug) : [existing.slug];
|
|
161
|
+
await deleteRedirects(ctx, args.scope, "toSlug", released);
|
|
140
162
|
return null;
|
|
141
163
|
},
|
|
142
164
|
});
|
|
@@ -177,70 +199,269 @@ export const rename = mutation({
|
|
|
177
199
|
if (args.fromSlug === args.toSlug) {
|
|
178
200
|
return { ok: false, reason: "SLUG_TAKEN" as const };
|
|
179
201
|
}
|
|
180
|
-
|
|
202
|
+
const toSlug = storedSlug(args.toSlug);
|
|
203
|
+
if (slugOutOfBounds(toSlug, args.bounds)) {
|
|
181
204
|
return { ok: false, reason: "SLUG_INVALID" as const };
|
|
182
205
|
}
|
|
183
|
-
const from = await ctx.
|
|
184
|
-
.query("slugs")
|
|
185
|
-
.withIndex("by_scope_slug", (q) =>
|
|
186
|
-
q.eq("scope", args.scope).eq("slug", args.fromSlug),
|
|
187
|
-
)
|
|
188
|
-
.first();
|
|
206
|
+
const from = await findSlug(ctx, args.scope, args.fromSlug);
|
|
189
207
|
if (from === null) {
|
|
190
208
|
return { ok: false, reason: "SLUG_NOT_FOUND" as const };
|
|
191
209
|
}
|
|
192
|
-
|
|
193
|
-
.query("slugs")
|
|
194
|
-
.withIndex("by_scope_slug", (q) =>
|
|
195
|
-
q.eq("scope", args.scope).eq("slug", args.toSlug),
|
|
196
|
-
)
|
|
197
|
-
.first();
|
|
198
|
-
if (taken !== null) {
|
|
210
|
+
if ((await findSlug(ctx, args.scope, args.toSlug)) !== null) {
|
|
199
211
|
return { ok: false, reason: "SLUG_TAKEN" as const };
|
|
200
212
|
}
|
|
201
|
-
|
|
213
|
+
// The key the row was stored under (the 0.2.0 form for an older row).
|
|
214
|
+
const fromSlug = from.slug;
|
|
215
|
+
await ctx.db.patch("slugs", from._id, { slug: toSlug });
|
|
202
216
|
|
|
203
217
|
// `toSlug` is a live name again: drop its own redirect first, so renaming
|
|
204
218
|
// back (A->B, B->A) cannot repoint it into a self-loop A->A below.
|
|
205
|
-
|
|
206
|
-
.query("redirects")
|
|
207
|
-
.withIndex("by_scope_from", (q) =>
|
|
208
|
-
q.eq("scope", args.scope).eq("fromSlug", args.toSlug),
|
|
209
|
-
)
|
|
210
|
-
.collect();
|
|
211
|
-
for (const row of stale) {
|
|
212
|
-
await ctx.db.delete("redirects", row._id);
|
|
213
|
-
}
|
|
219
|
+
await deleteRedirects(ctx, args.scope, "fromSlug", lookupKeys(args.toSlug));
|
|
214
220
|
|
|
215
221
|
const inbound = await ctx.db
|
|
216
222
|
.query("redirects")
|
|
217
223
|
.withIndex("by_scope_to", (q) =>
|
|
218
|
-
q.eq("scope", args.scope).eq("toSlug",
|
|
224
|
+
q.eq("scope", args.scope).eq("toSlug", fromSlug),
|
|
219
225
|
)
|
|
220
226
|
.collect();
|
|
221
227
|
for (const row of inbound) {
|
|
222
|
-
await ctx.db.patch("redirects", row._id, { toSlug
|
|
228
|
+
await ctx.db.patch("redirects", row._id, { toSlug });
|
|
223
229
|
}
|
|
224
230
|
|
|
231
|
+
// Moving a 0.2.0 row to its NFC form keeps the same name: no redirect.
|
|
232
|
+
if (storedSlug(fromSlug) === toSlug) {
|
|
233
|
+
return { ok: true };
|
|
234
|
+
}
|
|
225
235
|
const existing = await ctx.db
|
|
226
236
|
.query("redirects")
|
|
227
237
|
.withIndex("by_scope_from", (q) =>
|
|
228
|
-
q.eq("scope", args.scope).eq("fromSlug",
|
|
238
|
+
q.eq("scope", args.scope).eq("fromSlug", fromSlug),
|
|
229
239
|
)
|
|
230
240
|
.first();
|
|
231
241
|
if (existing !== null) {
|
|
232
|
-
await ctx.db.patch("redirects", existing._id, { toSlug
|
|
242
|
+
await ctx.db.patch("redirects", existing._id, { toSlug });
|
|
233
243
|
} else {
|
|
234
244
|
await ctx.db.insert("redirects", {
|
|
235
245
|
scope: args.scope,
|
|
236
|
-
fromSlug
|
|
237
|
-
toSlug
|
|
246
|
+
fromSlug,
|
|
247
|
+
toSlug,
|
|
238
248
|
});
|
|
239
249
|
}
|
|
240
250
|
return { ok: true };
|
|
241
251
|
},
|
|
242
252
|
});
|
|
243
253
|
|
|
254
|
+
/** Default and largest page size of {@link normalizeStoredSlugs}. */
|
|
255
|
+
const NORMALIZE_BATCH = 100;
|
|
256
|
+
const MAX_NORMALIZE_BATCH = 500;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* A row {@link normalizeStoredSlugs} left as is because its NFC form is taken.
|
|
260
|
+
* `slug` is the value as stored (not NFC); `nfcSlug` is the taken NFC form. For
|
|
261
|
+
* a `slugs` row, `resourceRef` holds `slug` and `heldBy` holds `nfcSlug`. For a
|
|
262
|
+
* `redirects` row, `slug` redirects to `toSlug` and `nfcSlug` to `otherToSlug`.
|
|
263
|
+
*/
|
|
264
|
+
interface Conflict {
|
|
265
|
+
table: "slugs" | "redirects";
|
|
266
|
+
scope: string;
|
|
267
|
+
slug: string;
|
|
268
|
+
nfcSlug: string;
|
|
269
|
+
resourceRef?: string;
|
|
270
|
+
heldBy?: string;
|
|
271
|
+
toSlug?: string;
|
|
272
|
+
otherToSlug?: string;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const conflictValidator = v.object({
|
|
276
|
+
table: v.union(v.literal("slugs"), v.literal("redirects")),
|
|
277
|
+
scope: v.string(),
|
|
278
|
+
slug: v.string(),
|
|
279
|
+
nfcSlug: v.string(),
|
|
280
|
+
resourceRef: v.optional(v.string()),
|
|
281
|
+
heldBy: v.optional(v.string()),
|
|
282
|
+
toSlug: v.optional(v.string()),
|
|
283
|
+
otherToSlug: v.optional(v.string()),
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* JSON with every non-ASCII UTF-16 code unit escaped as `\uXXXX`, so an old
|
|
288
|
+
* form and its NFC form differ visibly in the logs and the values can be
|
|
289
|
+
* pasted into `npx convex run` arguments as is.
|
|
290
|
+
*/
|
|
291
|
+
function asciiJson(value: unknown): string {
|
|
292
|
+
let out = "";
|
|
293
|
+
for (const unit of JSON.stringify(value).split("")) {
|
|
294
|
+
const code = unit.charCodeAt(0);
|
|
295
|
+
out += code < 0x7f ? unit : `\\u${code.toString(16).padStart(4, "0")}`;
|
|
296
|
+
}
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Move one `slugs` row stored by 0.2.0 to its NFC form, with the redirects that
|
|
302
|
+
* point at it. Returns the conflict, and changes nothing, when another row
|
|
303
|
+
* already holds the NFC form.
|
|
304
|
+
*/
|
|
305
|
+
async function normalizeSlugRow(
|
|
306
|
+
ctx: MutationCtx,
|
|
307
|
+
row: Doc<"slugs">,
|
|
308
|
+
): Promise<"unchanged" | "fixed" | Conflict> {
|
|
309
|
+
const slug = storedSlug(row.slug);
|
|
310
|
+
if (slug === row.slug) {
|
|
311
|
+
return "unchanged";
|
|
312
|
+
}
|
|
313
|
+
const holder = await ctx.db
|
|
314
|
+
.query("slugs")
|
|
315
|
+
.withIndex("by_scope_slug", (q) => q.eq("scope", row.scope).eq("slug", slug))
|
|
316
|
+
.first();
|
|
317
|
+
if (holder !== null) {
|
|
318
|
+
return {
|
|
319
|
+
table: "slugs",
|
|
320
|
+
scope: row.scope,
|
|
321
|
+
slug: row.slug,
|
|
322
|
+
nfcSlug: slug,
|
|
323
|
+
resourceRef: row.resourceRef,
|
|
324
|
+
heldBy: holder.resourceRef,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
await ctx.db.patch("slugs", row._id, { slug });
|
|
328
|
+
const inbound = await ctx.db
|
|
329
|
+
.query("redirects")
|
|
330
|
+
.withIndex("by_scope_to", (q) =>
|
|
331
|
+
q.eq("scope", row.scope).eq("toSlug", row.slug),
|
|
332
|
+
)
|
|
333
|
+
.collect();
|
|
334
|
+
for (const redirect of inbound) {
|
|
335
|
+
await ctx.db.patch("redirects", redirect._id, { toSlug: slug });
|
|
336
|
+
}
|
|
337
|
+
// The NFC form is a live name now, so no redirect may start at it.
|
|
338
|
+
await deleteRedirects(ctx, row.scope, "fromSlug", [slug]);
|
|
339
|
+
return "fixed";
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Move one `redirects` row whose old name 0.2.0 stored to the NFC form of that
|
|
344
|
+
* name. A redirect from a name that is live again is deleted, as `reserve`
|
|
345
|
+
* does. Returns the conflict, and changes nothing, when another redirect already
|
|
346
|
+
* starts at the NFC form.
|
|
347
|
+
*/
|
|
348
|
+
async function normalizeRedirectRow(
|
|
349
|
+
ctx: MutationCtx,
|
|
350
|
+
row: Doc<"redirects">,
|
|
351
|
+
): Promise<"unchanged" | "fixed" | Conflict> {
|
|
352
|
+
const fromSlug = storedSlug(row.fromSlug);
|
|
353
|
+
if (fromSlug === row.fromSlug) {
|
|
354
|
+
return "unchanged";
|
|
355
|
+
}
|
|
356
|
+
const [live, other] = await Promise.all([
|
|
357
|
+
ctx.db
|
|
358
|
+
.query("slugs")
|
|
359
|
+
.withIndex("by_scope_slug", (q) =>
|
|
360
|
+
q.eq("scope", row.scope).eq("slug", fromSlug),
|
|
361
|
+
)
|
|
362
|
+
.first(),
|
|
363
|
+
ctx.db
|
|
364
|
+
.query("redirects")
|
|
365
|
+
.withIndex("by_scope_from", (q) =>
|
|
366
|
+
q.eq("scope", row.scope).eq("fromSlug", fromSlug),
|
|
367
|
+
)
|
|
368
|
+
.first(),
|
|
369
|
+
]);
|
|
370
|
+
if (live !== null) {
|
|
371
|
+
await ctx.db.delete("redirects", row._id);
|
|
372
|
+
return "fixed";
|
|
373
|
+
}
|
|
374
|
+
if (other !== null) {
|
|
375
|
+
return {
|
|
376
|
+
table: "redirects",
|
|
377
|
+
scope: row.scope,
|
|
378
|
+
slug: row.fromSlug,
|
|
379
|
+
nfcSlug: fromSlug,
|
|
380
|
+
toSlug: row.toSlug,
|
|
381
|
+
otherToSlug: other.toSlug,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
await ctx.db.patch("redirects", row._id, { fromSlug });
|
|
385
|
+
return "fixed";
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* One-off fix-up for rows stored by 0.2.0 and earlier, which could hold a slug
|
|
390
|
+
* that is not in NFC form (a letter that composes only after lowercasing, such
|
|
391
|
+
* as "h" + U+0331 for "H̱"). Pages through `slugs`, then `redirects`, and moves
|
|
392
|
+
* each such slug or old name to its NFC form. A row whose NFC form another row
|
|
393
|
+
* already holds is left as is and reported in `conflicts` (and logged), so the
|
|
394
|
+
* host can decide which resource keeps the name. Idempotent: rows already in
|
|
395
|
+
* NFC form are skipped. Schedules itself for the next page until both tables
|
|
396
|
+
* are done, so one call from `npx convex run` fixes everything.
|
|
397
|
+
*
|
|
398
|
+
* @returns This page's counts: rows `fixed`, `conflicts` left, and whether this
|
|
399
|
+
* was the last page of the last table.
|
|
400
|
+
* @throws `INVALID_ARGUMENT` when `batch` is not an integer from 1 to 500.
|
|
401
|
+
*/
|
|
402
|
+
export const normalizeStoredSlugs = internalMutation({
|
|
403
|
+
args: {
|
|
404
|
+
table: v.optional(v.union(v.literal("slugs"), v.literal("redirects"))),
|
|
405
|
+
cursor: v.optional(v.union(v.string(), v.null())),
|
|
406
|
+
batch: v.optional(v.number()),
|
|
407
|
+
},
|
|
408
|
+
returns: v.object({
|
|
409
|
+
fixed: v.number(),
|
|
410
|
+
conflicts: v.array(conflictValidator),
|
|
411
|
+
isDone: v.boolean(),
|
|
412
|
+
}),
|
|
413
|
+
handler: async (
|
|
414
|
+
ctx,
|
|
415
|
+
args,
|
|
416
|
+
): Promise<{ fixed: number; conflicts: Conflict[]; isDone: boolean }> => {
|
|
417
|
+
const table = args.table ?? "slugs";
|
|
418
|
+
const batch = args.batch ?? NORMALIZE_BATCH;
|
|
419
|
+
if (!Number.isInteger(batch) || batch < 1 || batch > MAX_NORMALIZE_BATCH) {
|
|
420
|
+
throw new ConvexError({
|
|
421
|
+
code: "INVALID_ARGUMENT",
|
|
422
|
+
message: `batch must be an integer from 1 to ${String(MAX_NORMALIZE_BATCH)}`,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
const paginationOpts = { cursor: args.cursor ?? null, numItems: batch };
|
|
426
|
+
const outcomes: ("unchanged" | "fixed" | Conflict)[] = [];
|
|
427
|
+
let page: { isDone: boolean; continueCursor: string };
|
|
428
|
+
if (table === "slugs") {
|
|
429
|
+
const result = await ctx.db.query("slugs").paginate(paginationOpts);
|
|
430
|
+
for (const row of result.page) {
|
|
431
|
+
outcomes.push(await normalizeSlugRow(ctx, row));
|
|
432
|
+
}
|
|
433
|
+
page = result;
|
|
434
|
+
} else {
|
|
435
|
+
const result = await ctx.db.query("redirects").paginate(paginationOpts);
|
|
436
|
+
for (const row of result.page) {
|
|
437
|
+
outcomes.push(await normalizeRedirectRow(ctx, row));
|
|
438
|
+
}
|
|
439
|
+
page = result;
|
|
440
|
+
}
|
|
441
|
+
const fixed = outcomes.filter((outcome) => outcome === "fixed").length;
|
|
442
|
+
const conflicts = outcomes.filter(
|
|
443
|
+
(outcome): outcome is Conflict => typeof outcome === "object",
|
|
444
|
+
);
|
|
445
|
+
for (const conflict of conflicts) {
|
|
446
|
+
console.warn(
|
|
447
|
+
`normalizeStoredSlugs: NFC form already taken ${asciiJson(conflict)}`,
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
const next = page.isDone
|
|
451
|
+
? table === "slugs"
|
|
452
|
+
? { table: "redirects" as const, cursor: null }
|
|
453
|
+
: null
|
|
454
|
+
: { table, cursor: page.continueCursor };
|
|
455
|
+
if (next !== null) {
|
|
456
|
+
await ctx.scheduler.runAfter(0, internal.mutations.normalizeStoredSlugs, {
|
|
457
|
+
...next,
|
|
458
|
+
batch,
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
return { fixed, conflicts, isDone: next === null };
|
|
462
|
+
},
|
|
463
|
+
});
|
|
464
|
+
|
|
244
465
|
/**
|
|
245
466
|
* Test/repair escape hatch: insert a `slugs` row with no uniqueness check.
|
|
246
467
|
* `internalMutation`, so it is never part of the public client API. Used to
|
package/src/component/queries.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
2
|
import { internalQuery, query } from "./_generated/server.js";
|
|
3
|
+
import { lookupKeys } from "../shared.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Resolve `slug` to the `resourceRef` currently holding it within `scope`, or
|
|
6
|
-
* `null`.
|
|
7
|
-
* instead of
|
|
7
|
+
* `null`. Looks `slug` up as sent, then its NFC form (see `lookupKeys`). Uses
|
|
8
|
+
* `.first()` so a stray duplicate row degrades to one answer instead of
|
|
9
|
+
* throwing.
|
|
8
10
|
*
|
|
9
11
|
* @returns The held `resourceRef`, or `null` when the slug is free.
|
|
10
12
|
*/
|
|
@@ -12,20 +14,24 @@ export const resolve = query({
|
|
|
12
14
|
args: { scope: v.string(), slug: v.string() },
|
|
13
15
|
returns: v.union(v.null(), v.string()),
|
|
14
16
|
handler: async (ctx, args) => {
|
|
15
|
-
const
|
|
16
|
-
.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const rows = await Promise.all(
|
|
18
|
+
lookupKeys(args.slug).map((key) =>
|
|
19
|
+
ctx.db
|
|
20
|
+
.query("slugs")
|
|
21
|
+
.withIndex("by_scope_slug", (q) =>
|
|
22
|
+
q.eq("scope", args.scope).eq("slug", key),
|
|
23
|
+
)
|
|
24
|
+
.first(),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
return rows.find((row) => row !== null)?.resourceRef ?? null;
|
|
22
28
|
},
|
|
23
29
|
});
|
|
24
30
|
|
|
25
31
|
/**
|
|
26
32
|
* Where a renamed `slug` now points within `scope`. After chain repointing this
|
|
27
33
|
* is always the live slug (A→B→C resolves A to C), or `null` when `slug` was
|
|
28
|
-
* never renamed away.
|
|
34
|
+
* never renamed away. Looks `slug` up as sent, then its NFC form.
|
|
29
35
|
*
|
|
30
36
|
* @returns The current target slug, or `null`.
|
|
31
37
|
*/
|
|
@@ -33,13 +39,17 @@ export const redirectFor = query({
|
|
|
33
39
|
args: { scope: v.string(), slug: v.string() },
|
|
34
40
|
returns: v.union(v.null(), v.string()),
|
|
35
41
|
handler: async (ctx, args) => {
|
|
36
|
-
const
|
|
37
|
-
.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
const rows = await Promise.all(
|
|
43
|
+
lookupKeys(args.slug).map((key) =>
|
|
44
|
+
ctx.db
|
|
45
|
+
.query("redirects")
|
|
46
|
+
.withIndex("by_scope_from", (q) =>
|
|
47
|
+
q.eq("scope", args.scope).eq("fromSlug", key),
|
|
48
|
+
)
|
|
49
|
+
.first(),
|
|
50
|
+
),
|
|
51
|
+
);
|
|
52
|
+
return rows.find((row) => row !== null)?.toSlug ?? null;
|
|
43
53
|
},
|
|
44
54
|
});
|
|
45
55
|
|
package/src/shared.ts
CHANGED
|
@@ -33,20 +33,56 @@ export interface SlugConstraints {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
36
|
+
* The lookup key the client sends: NFC, then `trim()`, then lowercase when
|
|
37
|
+
* `foldCase` is `true`. Versions up to 0.2.0 stored this key as is. It is not
|
|
38
|
+
* always NFC: lowercasing can make a sequence that NFC composes further
|
|
39
|
+
* ("H" + U+0331 becomes "h" + U+0331, whose NFC form is U+1E96). The component
|
|
40
|
+
* looks the key up as sent first, so rows stored that way keep matching, and
|
|
41
|
+
* stores its NFC form ({@link storedSlug}).
|
|
38
42
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
43
|
+
* @param slug - Raw, host-supplied slug.
|
|
44
|
+
* @param foldCase - Lowercase when `true`.
|
|
45
|
+
* @returns The lookup key.
|
|
46
|
+
*/
|
|
47
|
+
export function slugKey(slug: string, foldCase: boolean): string {
|
|
48
|
+
const normalized = slug.normalize("NFC").trim();
|
|
49
|
+
return foldCase ? normalized.toLowerCase() : normalized;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The form a slug key is stored in: its NFC form, so spellings that look the
|
|
54
|
+
* same are one slug.
|
|
55
|
+
*
|
|
56
|
+
* @param key - A lookup key (output of {@link slugKey}).
|
|
57
|
+
* @returns The NFC form of `key`.
|
|
58
|
+
*/
|
|
59
|
+
export function storedSlug(key: string): string {
|
|
60
|
+
return key.normalize("NFC");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The keys to look a slug up by, in order: the key as sent (rows stored by
|
|
65
|
+
* versions up to 0.2.0), then its stored form when that differs.
|
|
66
|
+
*
|
|
67
|
+
* @param key - A lookup key (output of {@link slugKey}).
|
|
68
|
+
* @returns One or two keys.
|
|
69
|
+
*/
|
|
70
|
+
export function lookupKeys(key: string): string[] {
|
|
71
|
+
const stored = storedSlug(key);
|
|
72
|
+
return stored === key ? [key] : [key, stored];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Normalize a slug to the form it is validated and stored in: {@link slugKey},
|
|
77
|
+
* then NFC again. `foldCase` lowercases (the default — handles are
|
|
78
|
+
* case-insensitive); when false the raw case is preserved.
|
|
42
79
|
*
|
|
43
80
|
* @param slug - Raw, host-supplied slug.
|
|
44
81
|
* @param foldCase - Lowercase before comparison when `true`.
|
|
45
82
|
* @returns The NFC-normalized, trimmed (and optionally lowercased) slug.
|
|
46
83
|
*/
|
|
47
84
|
export function normalizeSlug(slug: string, foldCase: boolean): string {
|
|
48
|
-
|
|
49
|
-
return foldCase ? normalized.toLowerCase() : normalized;
|
|
85
|
+
return storedSlug(slugKey(slug, foldCase));
|
|
50
86
|
}
|
|
51
87
|
|
|
52
88
|
/**
|