@vllnt/convex-slugs 0.2.0-canary.eb878e4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -88,12 +88,20 @@ interface RunMutationCtx {
88
88
  * Slug validation (length, charset, reserved words) runs here against the
89
89
  * resolved {@link SlugsOptions}; the component re-guards length at the trust
90
90
  * boundary. An invalid write short-circuits to a typed result with no round-trip.
91
+ *
92
+ * @see https://oss.vllnt.com/packages/convex-slugs
91
93
  */
92
94
  export class Slugs {
93
95
  private readonly defaultScope: string;
94
96
  private readonly foldCase: boolean;
95
97
  private readonly constraints: SlugConstraints;
96
98
 
99
+ /**
100
+ * @param component - The mounted component, `components.slugs`.
101
+ * @param options - Scope, case folding and validation rules; see {@link SlugsOptions}.
102
+ * @throws `ConvexError` with `data.code` `INVALID_OPTIONS` for an empty
103
+ * `defaultScope` or invalid length bounds.
104
+ */
97
105
  constructor(
98
106
  private readonly component: SlugsComponent,
99
107
  options: SlugsOptions = {},
@@ -143,6 +151,11 @@ export class Slugs {
143
151
  * Reserve `slug` for `resourceRef`. Returns `{ ok: false, reason }` when the
144
152
  * slug is invalid (`SLUG_INVALID`), reserved (`SLUG_RESERVED`), or already held
145
153
  * (`SLUG_TAKEN`).
154
+ *
155
+ * @param ctx - A mutation or action context.
156
+ * @param slug - Raw slug; normalized before validation and storage.
157
+ * @param resourceRef - Opaque host reference that will hold the slug.
158
+ * @param scope - Namespace; defaults to `defaultScope`.
146
159
  */
147
160
  reserve(
148
161
  ctx: RunMutationCtx,
@@ -163,7 +176,14 @@ export class Slugs {
163
176
  });
164
177
  }
165
178
 
166
- /** Release `slug` (idempotent — releasing an unheld slug is a no-op). */
179
+ /**
180
+ * Release `slug` (idempotent — releasing an unheld slug is a no-op) and
181
+ * delete the redirects that point at it.
182
+ *
183
+ * @param ctx - A mutation or action context.
184
+ * @param slug - Raw slug; normalized before lookup.
185
+ * @param scope - Namespace; defaults to `defaultScope`.
186
+ */
167
187
  release(ctx: RunMutationCtx, slug: string, scope?: string): Promise<null> {
168
188
  return ctx.runMutation(this.component.mutations.release, {
169
189
  scope: this.scopeOf(scope),
@@ -174,6 +194,13 @@ export class Slugs {
174
194
  /**
175
195
  * Rename `fromSlug` to `toSlug`, recording a redirect. The `toSlug` is
176
196
  * validated; an invalid target short-circuits to `{ ok: false, reason }`.
197
+ * Returns `SLUG_NOT_FOUND` when `fromSlug` is not held and `SLUG_TAKEN` when
198
+ * `toSlug` is held or equals `fromSlug`.
199
+ *
200
+ * @param ctx - A mutation or action context.
201
+ * @param fromSlug - The currently held slug.
202
+ * @param toSlug - The new slug; validated like `reserve`.
203
+ * @param scope - Namespace; defaults to `defaultScope`.
177
204
  */
178
205
  rename(
179
206
  ctx: RunMutationCtx,
@@ -194,7 +221,13 @@ export class Slugs {
194
221
  });
195
222
  }
196
223
 
197
- /** Resolve `slug` to its `resourceRef`, or `null`. */
224
+ /**
225
+ * Resolve `slug` to its `resourceRef`, or `null`. Does not follow redirects.
226
+ *
227
+ * @param ctx - A query, mutation or action context.
228
+ * @param slug - Raw slug; normalized before lookup.
229
+ * @param scope - Namespace; defaults to `defaultScope`.
230
+ */
198
231
  resolve(ctx: RunQueryCtx, slug: string, scope?: string): Promise<string | null> {
199
232
  return ctx.runQuery(this.component.queries.resolve, {
200
233
  scope: this.scopeOf(scope),
@@ -202,7 +235,14 @@ export class Slugs {
202
235
  });
203
236
  }
204
237
 
205
- /** Where a renamed `slug` now points (`toSlug`), or `null`. */
238
+ /**
239
+ * Where a renamed `slug` now points (the live slug, chains collapsed), or
240
+ * `null`.
241
+ *
242
+ * @param ctx - A query, mutation or action context.
243
+ * @param slug - Raw slug; normalized before lookup.
244
+ * @param scope - Namespace; defaults to `defaultScope`.
245
+ */
206
246
  redirectFor(
207
247
  ctx: RunQueryCtx,
208
248
  slug: string,
@@ -214,7 +254,13 @@ export class Slugs {
214
254
  });
215
255
  }
216
256
 
217
- /** The slug currently held by `resourceRef`, or `null`. */
257
+ /**
258
+ * The slug currently held by `resourceRef`, or `null`.
259
+ *
260
+ * @param ctx - A query, mutation or action context.
261
+ * @param resourceRef - Opaque host reference, compared exactly.
262
+ * @param scope - Namespace; defaults to `defaultScope`.
263
+ */
218
264
  slugForResource(
219
265
  ctx: RunQueryCtx,
220
266
  resourceRef: string,
@@ -20,7 +20,11 @@ export interface SlugsOptions {
20
20
  foldCase?: boolean;
21
21
  /** Minimum slug length after normalization. Default `1`. */
22
22
  minLength?: number;
23
- /** Maximum slug length after normalization. Default `256`. */
23
+ /**
24
+ * Maximum slug length after normalization, in UTF-16 code units. Default
25
+ * `256`. A value over 1024 is accepted, but the component still returns
26
+ * `SLUG_INVALID` for a slug over 1024 code units.
27
+ */
24
28
  maxLength?: number;
25
29
  /** Allowed charset; a slug failing it is rejected `SLUG_INVALID`. Default: none. */
26
30
  pattern?: RegExp;
@@ -1,5 +1,6 @@
1
1
  import { defineComponent } from "convex/server";
2
2
 
3
+ /** Component definition, named `slugs`. Mount it with `app.use(config)` in `convex/convex.config.ts`. */
3
4
  const component = defineComponent("slugs");
4
5
 
5
6
  // Nest official child components here, e.g.:
@@ -2,10 +2,19 @@ import { ConvexError, v } from "convex/values";
2
2
  import { internalMutation, mutation } from "./_generated/server.js";
3
3
  import { lengthBounds, reserveResult } from "./validators.js";
4
4
 
5
+ /**
6
+ * Hard cap, in UTF-16 code units (`string.length`), on the slug, `scope` and
7
+ * `resourceRef` that `reserve` and `rename` write. It holds whatever `bounds`
8
+ * the caller passes. `release` and the queries do not apply it, so rows stored
9
+ * by an older version with longer values stay readable and removable.
10
+ */
11
+ const MAX_INPUT_LENGTH = 1024;
12
+
5
13
  /**
6
14
  * Reject malformed arguments with `ConvexError({ code: "INVALID_ARGUMENT" })`.
7
15
  * Expected slug rejections stay typed `{ ok: false, reason }` results; this
8
- * covers input no correct client sends (empty scope/ref, non-integer bounds).
16
+ * covers input no correct client sends (empty scope/ref, non-integer bounds,
17
+ * and on writes a scope or ref over {@link MAX_INPUT_LENGTH}).
9
18
  */
10
19
  function assertArgs(args: {
11
20
  scope: string;
@@ -13,21 +22,38 @@ function assertArgs(args: {
13
22
  bounds?: { minLength: number; maxLength: number };
14
23
  }): void {
15
24
  const { scope, resourceRef, bounds } = args;
25
+ // Only `reserve` and `rename` pass `bounds`; they are the size-capped writes.
26
+ const isWrite = bounds !== undefined;
16
27
  const badBounds =
17
- bounds !== undefined &&
28
+ isWrite &&
18
29
  !(
19
30
  Number.isInteger(bounds.minLength) &&
20
31
  Number.isInteger(bounds.maxLength) &&
21
32
  bounds.minLength <= bounds.maxLength
22
33
  );
23
- if (scope === "" || resourceRef === "" || badBounds) {
34
+ const oversized =
35
+ isWrite &&
36
+ (scope.length > MAX_INPUT_LENGTH ||
37
+ (resourceRef ?? "").length > MAX_INPUT_LENGTH);
38
+ if (scope === "" || resourceRef === "" || badBounds || oversized) {
24
39
  throw new ConvexError({
25
40
  code: "INVALID_ARGUMENT",
26
- message: "scope and resourceRef must be non-empty; bounds must be integers with min <= max",
41
+ message: `scope and resourceRef must be non-empty, and at most ${String(MAX_INPUT_LENGTH)} UTF-16 code units on reserve and rename; bounds must be integers with min <= max`,
27
42
  });
28
43
  }
29
44
  }
30
45
 
46
+ /** Whether a slug to be written is empty, outside `bounds`, or over the cap. */
47
+ function slugOutOfBounds(
48
+ slug: string,
49
+ bounds: { minLength: number; maxLength: number },
50
+ ): boolean {
51
+ return (
52
+ slug.length < Math.max(1, bounds.minLength) ||
53
+ slug.length > Math.min(bounds.maxLength, MAX_INPUT_LENGTH)
54
+ );
55
+ }
56
+
31
57
  /**
32
58
  * Reserve `slug` for `resourceRef` within `scope`. The slug arrives already
33
59
  * normalized by the client; `bounds` re-guards empty/oversized input at the
@@ -35,7 +61,9 @@ function assertArgs(args: {
35
61
  * redirect row is removed (the slug is a live name again).
36
62
  *
37
63
  * @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
38
- * `SLUG_INVALID` (empty/over length) or `SLUG_TAKEN` (already held in scope).
64
+ * `SLUG_INVALID` (empty, outside `bounds` or over {@link MAX_INPUT_LENGTH}) or
65
+ * `SLUG_TAKEN` (already held in scope).
66
+ * @throws `INVALID_ARGUMENT` for an empty, or over-cap, `scope` or `resourceRef`.
39
67
  */
40
68
  export const reserve = mutation({
41
69
  args: {
@@ -47,10 +75,7 @@ export const reserve = mutation({
47
75
  returns: reserveResult,
48
76
  handler: async (ctx, args) => {
49
77
  assertArgs(args);
50
- if (args.slug.length < Math.max(1, args.bounds.minLength)) {
51
- return { ok: false, reason: "SLUG_INVALID" as const };
52
- }
53
- if (args.slug.length > args.bounds.maxLength) {
78
+ if (slugOutOfBounds(args.slug, args.bounds)) {
54
79
  return { ok: false, reason: "SLUG_INVALID" as const };
55
80
  }
56
81
  const existing = await ctx.db
@@ -129,9 +154,15 @@ export const release = mutation({
129
154
  * Self-rename (`fromSlug === toSlug`) is rejected with `SLUG_TAKEN` before any
130
155
  * DB read, preventing a self-loop redirect from being created.
131
156
  *
157
+ * `fromSlug` is not size-capped: it is only a lookup key, so a value over the
158
+ * cap writes nothing unless an older version stored it, and renaming such a
159
+ * legacy row to a short `toSlug` is the way to bring it under the cap.
160
+ *
132
161
  * @returns `{ ok: true }`, or `{ ok: false, reason }` where reason is
133
- * `SLUG_INVALID` (empty/over length `toSlug`), `SLUG_NOT_FOUND` (`fromSlug` not
134
- * held), or `SLUG_TAKEN` (`toSlug` already held or equals `fromSlug`).
162
+ * `SLUG_INVALID` (empty, outside `bounds` or over {@link MAX_INPUT_LENGTH}
163
+ * `toSlug`), `SLUG_NOT_FOUND` (`fromSlug` not held), or `SLUG_TAKEN` (`toSlug`
164
+ * already held or equals `fromSlug`).
165
+ * @throws `INVALID_ARGUMENT` for an empty, or over-cap, `scope`.
135
166
  */
136
167
  export const rename = mutation({
137
168
  args: {
@@ -146,10 +177,7 @@ export const rename = mutation({
146
177
  if (args.fromSlug === args.toSlug) {
147
178
  return { ok: false, reason: "SLUG_TAKEN" as const };
148
179
  }
149
- if (args.toSlug.length < Math.max(1, args.bounds.minLength)) {
150
- return { ok: false, reason: "SLUG_INVALID" as const };
151
- }
152
- if (args.toSlug.length > args.bounds.maxLength) {
180
+ if (slugOutOfBounds(args.toSlug, args.bounds)) {
153
181
  return { ok: false, reason: "SLUG_INVALID" as const };
154
182
  }
155
183
  const from = await ctx.db