@ultimat3/render 31.25.0 → 31.27.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.
- package/README.md +1 -1
- package/package.json +7 -7
- package/src/decode-segment.ts +15 -0
- package/src/index.ts +3 -0
- package/src/registry.ts +37 -152
- package/src/route-path.ts +136 -0
package/README.md
CHANGED
|
@@ -636,7 +636,7 @@ a job boundary the class is gone and the `code` is what survives — match on th
|
|
|
636
636
|
| `island`, `islandCollector` | one interactive component on a static page |
|
|
637
637
|
| `asset`, `setAssetResolver`, `assetPathProblem`, `AssetPath` | `asset('assets/x.avif')` → the content-hashed URL of a public site file; the table is the CLI's ([Static Assets](../../wiki/Static-Assets.md)) |
|
|
638
638
|
| `MODE_SPECS`, `assertModeShape`, `assertModeInvariants` | the mode invariant table |
|
|
639
|
-
| `registerRoute`, `describePages`, `routeFor`, `routePathFromFile` | the route table |
|
|
639
|
+
| `registerRoute`, `describePages`, `routeFor`, `routePathFromFile` | the route table; `routeSnapshot()` / `restoreRoutes(snapshot)` put it back exactly (tests — `@ultimat3/testing`'s file boundary) |
|
|
640
640
|
| `registerMountedRoutes`, `RouteMount`, `RouteMountInput`, `MountedRouteInput` | routes a package mounts with no surface file: listed by `describePages()` with `mount: { by, permissions }`, absent from `routeEntries()` |
|
|
641
641
|
| `checkSurfaceBoundary`, `surfaceOf` | the hard boundary |
|
|
642
642
|
| `renderStatic`†, `enumeratePrerender`† | build-time render, content hashing |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/render",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.27.0",
|
|
4
4
|
"description": "The route primitive and the five render modes: static, isr, ssr, stream, spa.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -52,12 +52,12 @@
|
|
|
52
52
|
"test": "bun test"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@ultimat3/cache": "31.
|
|
56
|
-
"@ultimat3/core": "31.
|
|
57
|
-
"@ultimat3/http": "31.
|
|
58
|
-
"@ultimat3/i18n": "31.
|
|
59
|
-
"@ultimat3/schema": "31.
|
|
60
|
-
"@ultimat3/seo": "31.
|
|
55
|
+
"@ultimat3/cache": "31.27.0",
|
|
56
|
+
"@ultimat3/core": "31.27.0",
|
|
57
|
+
"@ultimat3/http": "31.27.0",
|
|
58
|
+
"@ultimat3/i18n": "31.27.0",
|
|
59
|
+
"@ultimat3/schema": "31.27.0",
|
|
60
|
+
"@ultimat3/seo": "31.27.0",
|
|
61
61
|
"sass": "1.106.1"
|
|
62
62
|
}
|
|
63
63
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Whether one URL path segment decodes: the answer to a malformed percent-escape on this side of
|
|
2
|
+
// the wire. Split from `registry.ts`, which re-exports it.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `undefined` for a malformed percent-escape: `decodeURIComponent('%zz')` throws a bare `URIError`
|
|
6
|
+
* where a router already has an answer, "no match". The one answer on this side of the wire to
|
|
7
|
+
* "is this segment decodable?" (`@ultimat3/http`'s trie serves requests, `stages.ts`).
|
|
8
|
+
*/
|
|
9
|
+
export function decodeSegment(value: string): string | undefined {
|
|
10
|
+
try {
|
|
11
|
+
return decodeURIComponent(value);
|
|
12
|
+
} catch {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -158,6 +158,7 @@ export type {
|
|
|
158
158
|
RouteEntry,
|
|
159
159
|
RouteMount,
|
|
160
160
|
RouteMountInput,
|
|
161
|
+
RouteSnapshot,
|
|
161
162
|
} from './registry';
|
|
162
163
|
export {
|
|
163
164
|
clearRoutes,
|
|
@@ -165,9 +166,11 @@ export {
|
|
|
165
166
|
ROUTE_FILENAME,
|
|
166
167
|
registerMountedRoutes,
|
|
167
168
|
registerRoute,
|
|
169
|
+
restoreRoutes,
|
|
168
170
|
routeEntries,
|
|
169
171
|
routeFor,
|
|
170
172
|
routePathFromFile,
|
|
173
|
+
routeSnapshot,
|
|
171
174
|
} from './registry';
|
|
172
175
|
export type {
|
|
173
176
|
InvalidateMode,
|
package/src/registry.ts
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
|
|
8
8
|
import type { HydrateStrategy, OfflineStrategy, RenderMode } from '@ultimat3/core';
|
|
9
9
|
import { byCodeUnit } from './code-unit-order';
|
|
10
|
-
import {
|
|
11
|
-
RouteDuplicateError,
|
|
12
|
-
RouteFileInvalidError,
|
|
13
|
-
RouteUnnormalizedError,
|
|
14
|
-
SurfaceBoundaryError,
|
|
15
|
-
} from './errors';
|
|
10
|
+
import { RouteDuplicateError, RouteUnnormalizedError } from './errors';
|
|
16
11
|
import { assertModeInvariants, defaultIslandBudget } from './modes';
|
|
17
|
-
import type {
|
|
12
|
+
import type {
|
|
13
|
+
MountedRoute,
|
|
14
|
+
MountedRouteInput,
|
|
15
|
+
RouteMount,
|
|
16
|
+
RouteMountInput,
|
|
17
|
+
} from './mounted-routes';
|
|
18
18
|
import {
|
|
19
19
|
claimedBy,
|
|
20
20
|
clearMountedRoutes,
|
|
@@ -27,22 +27,13 @@ import { assertPurgeableTags } from './revalidate-shape';
|
|
|
27
27
|
import type { InvalidateMode, RouteConfig, RouteData } from './route';
|
|
28
28
|
import { isRouteConfig, routeTagKeys } from './route';
|
|
29
29
|
import type { RouteComponent } from './route-component';
|
|
30
|
+
import { routePathFromFile } from './route-path';
|
|
30
31
|
import type { CompiledPattern } from './route-pattern';
|
|
31
32
|
import { compilePattern } from './route-pattern';
|
|
32
33
|
import type { RouteSurface, Surface } from './surfaces';
|
|
33
|
-
import { locateSurface } from './surfaces';
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
* MANDATORY: a dropped row would read as `undefined` in `assertRouteFilename`.
|
|
38
|
-
*/
|
|
39
|
-
export const ROUTE_FILENAME = Object.freeze<Record<RouteSurface, string>>({
|
|
40
|
-
site: 'page.tsx',
|
|
41
|
-
app: 'page.tsx',
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
/** Stems that already meant "this directory", so the repair is a rename in place, not a new folder. */
|
|
45
|
-
const DIRECTORY_STEMS = new Set(['index', 'page', 'route']);
|
|
35
|
+
export { decodeSegment } from './decode-segment'; // split out at the line ceiling
|
|
36
|
+
export { ROUTE_FILENAME, routePathFromFile } from './route-path'; // split out at the line ceiling
|
|
46
37
|
|
|
47
38
|
export interface RouteEntry<TData = RouteData> {
|
|
48
39
|
readonly file: string;
|
|
@@ -104,122 +95,6 @@ export interface RouteDescriptor {
|
|
|
104
95
|
|
|
105
96
|
export type { MountedRouteInput, RouteMount, RouteMountInput } from './mounted-routes';
|
|
106
97
|
|
|
107
|
-
/**
|
|
108
|
-
* `apps/web/site/blog/[slug]/page.tsx` → `{ surface: 'site', path: '/blog/:slug' }`.
|
|
109
|
-
*
|
|
110
|
-
* The URL is the **directory** path under the surface; the filename names the kind of file, never
|
|
111
|
-
* a URL segment. Anything else is `X_ROUTE_FILE_INVALID` — one spelling per surface, so an agent
|
|
112
|
-
* reading a folder knows which file is the route without opening any of them.
|
|
113
|
-
*
|
|
114
|
-
* | file | path |
|
|
115
|
-
* |---|---|
|
|
116
|
-
* | `site/page.tsx` | `/` |
|
|
117
|
-
* | `site/pricing/page.tsx` | `/pricing` |
|
|
118
|
-
* | `site/(marketing)/about/page.tsx` | `/about` |
|
|
119
|
-
* | `site/blog/[slug]/page.tsx` | `/blog/:slug` |
|
|
120
|
-
* | `site/docs/[...path]/page.tsx` | `/docs/*path` |
|
|
121
|
-
* | `app/dashboard/page.tsx` | `/dashboard` |
|
|
122
|
-
*/
|
|
123
|
-
export function routePathFromFile(file: string): { surface: RouteSurface; path: string } {
|
|
124
|
-
const normalized = file.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
125
|
-
// One reader of the surface segment, and it answers WHERE as well as WHICH. Slicing at
|
|
126
|
-
// `indexOf('app/')` instead matched inside `myapp/`, so `apps/myapp/app/page.tsx` resolved to
|
|
127
|
-
// `/app` rather than `/` — the surface came from an anchored regex and the URL from a substring.
|
|
128
|
-
const located = locateSurface(normalized);
|
|
129
|
-
if (located === null) {
|
|
130
|
-
throw new SurfaceBoundaryError(
|
|
131
|
-
`${file} is not inside a surface directory, so it has no URL and no bundle graph`,
|
|
132
|
-
`move ${file} under site/ or app/, named page.tsx`,
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
const surface = routeSurfaceOf(normalized, located.surface);
|
|
136
|
-
|
|
137
|
-
const rawSegments = located.rest.split('/').filter((s) => s.length > 0);
|
|
138
|
-
assertRouteFilename(normalized, surface, rawSegments[rawSegments.length - 1]);
|
|
139
|
-
|
|
140
|
-
const urlSegments = rawSegments
|
|
141
|
-
.slice(0, -1)
|
|
142
|
-
.filter((s) => !(s.startsWith('(') && s.endsWith(')')))
|
|
143
|
-
.map(toUrlSegment);
|
|
144
|
-
|
|
145
|
-
const path = `/${urlSegments.join('/')}`.replace(/\/+$/, '') || '/';
|
|
146
|
-
return { surface, path };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* POSIX single-quotes a filesystem-derived operand for a `fix:` command: close the quote, escape
|
|
151
|
-
* an embedded quote as `'\''`, reopen it. A `fix:` is copied and run verbatim (axiom 4), so a route
|
|
152
|
-
* filename carrying a space, an apostrophe or a shell metacharacter must not change what runs.
|
|
153
|
-
*/
|
|
154
|
-
const shellQuote = (value: string): string => `'${value.replaceAll("'", "'\\''")}'`;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Only `site/` and `app/` hold route files. `shared/` is a leaf of helpers with no URL; `api/` is
|
|
158
|
-
* the action and query projections `defineApi()` collects — a wire format an action cannot speak
|
|
159
|
-
* is a plain HTTP route in the `routes` runtime override, never a file under `api/`.
|
|
160
|
-
*/
|
|
161
|
-
function routeSurfaceOf(file: string, surface: Surface): RouteSurface {
|
|
162
|
-
if (surface === 'shared') {
|
|
163
|
-
throw new RouteFileInvalidError(
|
|
164
|
-
`${file} is under shared/, which is a leaf of helpers with no URL — a route cannot live there`,
|
|
165
|
-
`move ${file} under site/ or app/, named page.tsx`,
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
if (surface === 'api') {
|
|
169
|
-
throw new RouteFileInvalidError(
|
|
170
|
-
`${file} is under api/, which holds no route file: api/ is the actions and queries defineApi() collects`,
|
|
171
|
-
`JSON in and out: replace ${file} with an action (x g action <name>) collected by defineApi() in api/index.ts. Another wire format (a webhook, an OAuth endpoint): delete ${file} and add a plain HTTP route to the routes array exported as runtime from apps/<app>/runtime.ts`,
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
return surface;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Enforced rather than documented (axiom 3): a convention that is not a build error is not a
|
|
179
|
-
* convention. The fix is the move that makes the file a route, spelled out — the directory the
|
|
180
|
-
* author already meant, plus the one filename that surface accepts.
|
|
181
|
-
*/
|
|
182
|
-
function assertRouteFilename(
|
|
183
|
-
file: string,
|
|
184
|
-
surface: RouteSurface,
|
|
185
|
-
basename: string | undefined,
|
|
186
|
-
): void {
|
|
187
|
-
const expected = ROUTE_FILENAME[surface];
|
|
188
|
-
if (basename === expected) return;
|
|
189
|
-
|
|
190
|
-
// The directory the author meant is the file's own path minus its extension: `site/pricing.tsx`
|
|
191
|
-
// was always trying to be `/pricing`, so `site/pricing/page.tsx` is the move, not a guess.
|
|
192
|
-
// `index`, `page` and `route` are the exception — each already means "this directory", so the
|
|
193
|
-
// rename happens in place and no directory is created.
|
|
194
|
-
const stem = file.replace(/\.(tsx|ts|jsx|js)$/, '');
|
|
195
|
-
const dir = stem.slice(0, stem.lastIndexOf('/'));
|
|
196
|
-
const inPlace = DIRECTORY_STEMS.has(stem.slice(dir.length + 1));
|
|
197
|
-
const target = inPlace ? `${dir}/${expected}` : `${stem}/${expected}`;
|
|
198
|
-
// Plain `mv`, never `git mv`. `x new --no-git` scaffolds a tree with no repository, and there the
|
|
199
|
-
// shipped `git mv` answered `fatal: not a git repository` — a fix line that fails is worse than
|
|
200
|
-
// no fix line, because the reader debugs git instead of moving the file. `mv` works in both
|
|
201
|
-
// cases: git detects the rename at `git add` time, so the only thing given up is a nicety, and
|
|
202
|
-
// the instruction is the same one either way. `-n` keeps the one guarantee `git mv` did carry:
|
|
203
|
-
// an author who adds the correct `site/pricing/page.tsx` and leaves `site/pricing.tsx` behind is
|
|
204
|
-
// reported against the stale file, and `target` is then the GOOD file — a clobber deletes a
|
|
205
|
-
// working route, and this fix line is pasted unread.
|
|
206
|
-
throw new RouteFileInvalidError(
|
|
207
|
-
`${file} is a route on the ${surface} surface, so it must be named ${expected}: the URL is the ` +
|
|
208
|
-
'directory path and the filename names the kind of file',
|
|
209
|
-
inPlace
|
|
210
|
-
? `mv -n -- ${shellQuote(file)} ${shellQuote(target)}`
|
|
211
|
-
: `mkdir -p -- ${shellQuote(stem)} && mv -n -- ${shellQuote(file)} ${shellQuote(target)}`,
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function toUrlSegment(segment: string): string {
|
|
216
|
-
const catchAll = /^\[\.\.\.(.+)\]$/.exec(segment);
|
|
217
|
-
if (catchAll?.[1] !== undefined) return `*${catchAll[1]}`;
|
|
218
|
-
const dynamic = /^\[(.+)\]$/.exec(segment);
|
|
219
|
-
if (dynamic?.[1] !== undefined) return `:${dynamic[1]}`;
|
|
220
|
-
return segment;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
98
|
const routes = new Map<string, RouteEntry>();
|
|
224
99
|
/** route file → its URL pattern: what `render.page` reports a component's render under. */
|
|
225
100
|
const patternsByFile = new Map<string, string>();
|
|
@@ -359,6 +234,33 @@ export function clearRoutes(): void {
|
|
|
359
234
|
described = undefined;
|
|
360
235
|
}
|
|
361
236
|
|
|
237
|
+
/** The whole table: what `restoreRoutes` puts back. Test seam (`@ultimat3/testing`'s boundary). */
|
|
238
|
+
export interface RouteSnapshot {
|
|
239
|
+
readonly routes: readonly (readonly [string, RouteEntry, RouteConfig | undefined])[];
|
|
240
|
+
readonly patterns: readonly (readonly [string, string])[];
|
|
241
|
+
readonly mounted: readonly MountedRoute[];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export const routeSnapshot = (): RouteSnapshot => ({
|
|
245
|
+
routes: [...routes].map(([path, entry]) => [path, entry, declaredConfigs.get(path)] as const),
|
|
246
|
+
patterns: [...patternsByFile],
|
|
247
|
+
mounted: mountedRoutes(),
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
/** Exactly `snapshot` again — a REPLACE: a module-scope route never registers twice. */
|
|
251
|
+
export function restoreRoutes(snapshot: RouteSnapshot): void {
|
|
252
|
+
clearRoutes();
|
|
253
|
+
for (const [path, entry, config] of snapshot.routes) {
|
|
254
|
+
routes.set(path, entry);
|
|
255
|
+
if (config !== undefined) declaredConfigs.set(path, config);
|
|
256
|
+
}
|
|
257
|
+
for (const [file, path] of snapshot.patterns) patternsByFile.set(file, path);
|
|
258
|
+
const byMount = new Map<RouteMountInput, MountedRoute[]>();
|
|
259
|
+
for (const route of snapshot.mounted)
|
|
260
|
+
byMount.set(route.mount, [...(byMount.get(route.mount) ?? []), route]);
|
|
261
|
+
for (const [mount, set] of byMount) setMountedRoutes(mount, set);
|
|
262
|
+
}
|
|
263
|
+
|
|
362
264
|
export function routeCount(): number {
|
|
363
265
|
return routes.size;
|
|
364
266
|
}
|
|
@@ -464,20 +366,3 @@ function isPersonal(config: RouteConfig): boolean {
|
|
|
464
366
|
if (cache === undefined) return false;
|
|
465
367
|
return cache === 'no-store' || cache.mode === 'no-store' || cache.mode === 'private';
|
|
466
368
|
}
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* `undefined` for a malformed percent-escape. A pathname is whatever the client typed, and
|
|
470
|
-
* `decodeURIComponent('%zz')` throws a bare `URIError` — no code, no fix line — where a router
|
|
471
|
-
* already has an answer for "this branch does not match".
|
|
472
|
-
*
|
|
473
|
-
* Still exported after this package's own `matchRoute` was deleted for `@ultimat3/http`'s trie
|
|
474
|
-
* (`stages.ts`): it is the one answer to "is this segment decodable?" on this side of the wire,
|
|
475
|
-
* and a second copy of it is how one of the two ends up throwing where the other 404s.
|
|
476
|
-
*/
|
|
477
|
-
export function decodeSegment(value: string): string | undefined {
|
|
478
|
-
try {
|
|
479
|
-
return decodeURIComponent(value);
|
|
480
|
-
} catch {
|
|
481
|
-
return undefined;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// File path → URL for a route file under `site/` or `app/`: the one spelling each surface accepts,
|
|
2
|
+
// refused with the exact move that makes the file a route. Split out of `registry.ts`, which keeps
|
|
3
|
+
// the table itself.
|
|
4
|
+
|
|
5
|
+
import { RouteFileInvalidError, SurfaceBoundaryError } from './errors';
|
|
6
|
+
import type { RouteSurface, Surface } from './surfaces';
|
|
7
|
+
import { locateSurface } from './surfaces';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The one filename a route may carry, per route surface. Keyed by `RouteSurface`, so both rows are
|
|
11
|
+
* MANDATORY: a dropped row would read as `undefined` in `assertRouteFilename`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const ROUTE_FILENAME = Object.freeze<Record<RouteSurface, string>>({
|
|
15
|
+
site: 'page.tsx',
|
|
16
|
+
app: 'page.tsx',
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
/** Stems that already meant "this directory", so the repair is a rename in place, not a new folder. */
|
|
20
|
+
const DIRECTORY_STEMS = new Set(['index', 'page', 'route']);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* `apps/web/site/blog/[slug]/page.tsx` → `{ surface: 'site', path: '/blog/:slug' }`.
|
|
24
|
+
*
|
|
25
|
+
* The URL is the **directory** path under the surface; the filename names the kind of file, never
|
|
26
|
+
* a URL segment. Anything else is `X_ROUTE_FILE_INVALID` — one spelling per surface, so an agent
|
|
27
|
+
* reading a folder knows which file is the route without opening any of them.
|
|
28
|
+
*
|
|
29
|
+
* | file | path |
|
|
30
|
+
* |---|---|
|
|
31
|
+
* | `site/page.tsx` | `/` |
|
|
32
|
+
* | `site/pricing/page.tsx` | `/pricing` |
|
|
33
|
+
* | `site/(marketing)/about/page.tsx` | `/about` |
|
|
34
|
+
* | `site/blog/[slug]/page.tsx` | `/blog/:slug` |
|
|
35
|
+
* | `site/docs/[...path]/page.tsx` | `/docs/*path` |
|
|
36
|
+
* | `app/dashboard/page.tsx` | `/dashboard` |
|
|
37
|
+
*/
|
|
38
|
+
export function routePathFromFile(file: string): { surface: RouteSurface; path: string } {
|
|
39
|
+
const normalized = file.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
40
|
+
// One reader of the surface segment, and it answers WHERE as well as WHICH. Slicing at
|
|
41
|
+
// `indexOf('app/')` instead matched inside `myapp/`, so `apps/myapp/app/page.tsx` resolved to
|
|
42
|
+
// `/app` rather than `/` — the surface came from an anchored regex and the URL from a substring.
|
|
43
|
+
const located = locateSurface(normalized);
|
|
44
|
+
if (located === null) {
|
|
45
|
+
throw new SurfaceBoundaryError(
|
|
46
|
+
`${file} is not inside a surface directory, so it has no URL and no bundle graph`,
|
|
47
|
+
`move ${file} under site/ or app/, named page.tsx`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const surface = routeSurfaceOf(normalized, located.surface);
|
|
51
|
+
|
|
52
|
+
const rawSegments = located.rest.split('/').filter((s) => s.length > 0);
|
|
53
|
+
assertRouteFilename(normalized, surface, rawSegments[rawSegments.length - 1]);
|
|
54
|
+
|
|
55
|
+
const urlSegments = rawSegments
|
|
56
|
+
.slice(0, -1)
|
|
57
|
+
.filter((s) => !(s.startsWith('(') && s.endsWith(')')))
|
|
58
|
+
.map(toUrlSegment);
|
|
59
|
+
|
|
60
|
+
const path = `/${urlSegments.join('/')}`.replace(/\/+$/, '') || '/';
|
|
61
|
+
return { surface, path };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* POSIX single-quotes a filesystem-derived operand for a `fix:` command: close the quote, escape
|
|
66
|
+
* an embedded quote as `'\''`, reopen it. A `fix:` is copied and run verbatim (axiom 4), so a route
|
|
67
|
+
* filename carrying a space, an apostrophe or a shell metacharacter must not change what runs.
|
|
68
|
+
*/
|
|
69
|
+
const shellQuote = (value: string): string => `'${value.replaceAll("'", "'\\''")}'`;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Only `site/` and `app/` hold route files. `shared/` is a leaf of helpers with no URL; `api/` is
|
|
73
|
+
* the action and query projections `defineApi()` collects — a wire format an action cannot speak
|
|
74
|
+
* is a plain HTTP route in the `routes` runtime override, never a file under `api/`.
|
|
75
|
+
*/
|
|
76
|
+
function routeSurfaceOf(file: string, surface: Surface): RouteSurface {
|
|
77
|
+
if (surface === 'shared') {
|
|
78
|
+
throw new RouteFileInvalidError(
|
|
79
|
+
`${file} is under shared/, which is a leaf of helpers with no URL — a route cannot live there`,
|
|
80
|
+
`move ${file} under site/ or app/, named page.tsx`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (surface === 'api') {
|
|
84
|
+
throw new RouteFileInvalidError(
|
|
85
|
+
`${file} is under api/, which holds no route file: api/ is the actions and queries defineApi() collects`,
|
|
86
|
+
`JSON in and out: replace ${file} with an action (x g action <name>) collected by defineApi() in api/index.ts. Another wire format (a webhook, an OAuth endpoint): delete ${file} and add a plain HTTP route to the routes array exported as runtime from apps/<app>/runtime.ts`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return surface;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Enforced rather than documented (axiom 3): a convention that is not a build error is not a
|
|
94
|
+
* convention. The fix is the move that makes the file a route, spelled out — the directory the
|
|
95
|
+
* author already meant, plus the one filename that surface accepts.
|
|
96
|
+
*/
|
|
97
|
+
function assertRouteFilename(
|
|
98
|
+
file: string,
|
|
99
|
+
surface: RouteSurface,
|
|
100
|
+
basename: string | undefined,
|
|
101
|
+
): void {
|
|
102
|
+
const expected = ROUTE_FILENAME[surface];
|
|
103
|
+
if (basename === expected) return;
|
|
104
|
+
|
|
105
|
+
// The directory the author meant is the file's own path minus its extension: `site/pricing.tsx`
|
|
106
|
+
// was always trying to be `/pricing`, so `site/pricing/page.tsx` is the move, not a guess.
|
|
107
|
+
// `index`, `page` and `route` are the exception — each already means "this directory", so the
|
|
108
|
+
// rename happens in place and no directory is created.
|
|
109
|
+
const stem = file.replace(/\.(tsx|ts|jsx|js)$/, '');
|
|
110
|
+
const dir = stem.slice(0, stem.lastIndexOf('/'));
|
|
111
|
+
const inPlace = DIRECTORY_STEMS.has(stem.slice(dir.length + 1));
|
|
112
|
+
const target = inPlace ? `${dir}/${expected}` : `${stem}/${expected}`;
|
|
113
|
+
// Plain `mv`, never `git mv`. `x new --no-git` scaffolds a tree with no repository, and there the
|
|
114
|
+
// shipped `git mv` answered `fatal: not a git repository` — a fix line that fails is worse than
|
|
115
|
+
// no fix line, because the reader debugs git instead of moving the file. `mv` works in both
|
|
116
|
+
// cases: git detects the rename at `git add` time, so the only thing given up is a nicety, and
|
|
117
|
+
// the instruction is the same one either way. `-n` keeps the one guarantee `git mv` did carry:
|
|
118
|
+
// an author who adds the correct `site/pricing/page.tsx` and leaves `site/pricing.tsx` behind is
|
|
119
|
+
// reported against the stale file, and `target` is then the GOOD file — a clobber deletes a
|
|
120
|
+
// working route, and this fix line is pasted unread.
|
|
121
|
+
throw new RouteFileInvalidError(
|
|
122
|
+
`${file} is a route on the ${surface} surface, so it must be named ${expected}: the URL is the ` +
|
|
123
|
+
'directory path and the filename names the kind of file',
|
|
124
|
+
inPlace
|
|
125
|
+
? `mv -n -- ${shellQuote(file)} ${shellQuote(target)}`
|
|
126
|
+
: `mkdir -p -- ${shellQuote(stem)} && mv -n -- ${shellQuote(file)} ${shellQuote(target)}`,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function toUrlSegment(segment: string): string {
|
|
131
|
+
const catchAll = /^\[\.\.\.(.+)\]$/.exec(segment);
|
|
132
|
+
if (catchAll?.[1] !== undefined) return `*${catchAll[1]}`;
|
|
133
|
+
const dynamic = /^\[(.+)\]$/.exec(segment);
|
|
134
|
+
if (dynamic?.[1] !== undefined) return `:${dynamic[1]}`;
|
|
135
|
+
return segment;
|
|
136
|
+
}
|