@caelo-cms/shared 0.10.49 → 0.10.50
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/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/platform-updates.d.ts +7 -6
- package/dist/platform-updates.d.ts.map +1 -1
- package/dist/platform-updates.js +7 -6
- package/dist/platform-updates.js.map +1 -1
- package/dist/redirect-emit-caddy.d.ts +44 -0
- package/dist/redirect-emit-caddy.d.ts.map +1 -0
- package/dist/redirect-emit-caddy.js +76 -0
- package/dist/redirect-emit-caddy.js.map +1 -0
- package/dist/redirect-emit-cloudfront.d.ts +55 -0
- package/dist/redirect-emit-cloudfront.d.ts.map +1 -0
- package/dist/redirect-emit-cloudfront.js +99 -0
- package/dist/redirect-emit-cloudfront.js.map +1 -0
- package/dist/redirect-emit-front-door.d.ts +63 -0
- package/dist/redirect-emit-front-door.d.ts.map +1 -0
- package/dist/redirect-emit-front-door.js +79 -0
- package/dist/redirect-emit-front-door.js.map +1 -0
- package/dist/redirect-emit-gcp.d.ts +89 -0
- package/dist/redirect-emit-gcp.d.ts.map +1 -0
- package/dist/redirect-emit-gcp.js +91 -0
- package/dist/redirect-emit-gcp.js.map +1 -0
- package/dist/redirect-plan.d.ts +69 -0
- package/dist/redirect-plan.d.ts.map +1 -0
- package/dist/redirect-plan.js +218 -0
- package/dist/redirect-plan.js.map +1 -0
- package/dist/version.d.ts +2 -2
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/index.ts +5 -0
- package/src/platform-updates.ts +7 -6
- package/src/redirect-emit-caddy.ts +103 -0
- package/src/redirect-emit-cloudfront.ts +145 -0
- package/src/redirect-emit-front-door.ts +140 -0
- package/src/redirect-emit-gcp.ts +165 -0
- package/src/redirect-emit.test.ts +291 -0
- package/src/redirect-plan.test.ts +143 -0
- package/src/redirect-plan.ts +298 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The redirect plan: what every serving layer answers for the rows of the
|
|
5
|
+
* `redirects` table (#587).
|
|
6
|
+
*
|
|
7
|
+
* Each provider serves redirects through its own edge mechanism (Caddy
|
|
8
|
+
* `map` + `redir`, a GCP URL map, Firebase Hosting `config.redirects`, a
|
|
9
|
+
* CloudFront Function with a KeyValueStore, Front Door rules). They all
|
|
10
|
+
* consume this one plan, so a redirect means the same thing on every
|
|
11
|
+
* provider:
|
|
12
|
+
*
|
|
13
|
+
* - **Exact paths.** A row matches its `from` path exactly (case-sensitive)
|
|
14
|
+
* and the same path with a trailing slash — in the directory page style
|
|
15
|
+
* `/old/` is the URL people link to. A from path whose last segment
|
|
16
|
+
* looks like a file (`/old.html`) matches only as written.
|
|
17
|
+
* - **The build wins.** A row whose from path a file of the build serves
|
|
18
|
+
* is left out (#575): a live page always beats a recorded redirect, as
|
|
19
|
+
* in the admin's 404 fallback (`redirects.lookup`). The edge layers
|
|
20
|
+
* evaluate redirects BEFORE static files, so without this a slug moved
|
|
21
|
+
* back to its old path would be shadowed (or loop with the reverse row).
|
|
22
|
+
* - **Chains collapse.** `/a → /b` plus `/b → /c` serves `/a → /c` in one
|
|
23
|
+
* hop; crawlers follow few hops and every hop costs a round-trip. A hop
|
|
24
|
+
* is followed only through rows that are themselves in the plan. The
|
|
25
|
+
* collapsed status is permanent only when every hop is, and preserves
|
|
26
|
+
* the method (307/308) only when every hop does.
|
|
27
|
+
* - **Loops fail the build.** A cycle can only ever answer "too many
|
|
28
|
+
* redirects"; the error names it.
|
|
29
|
+
* - **Location.** In the directory style a page's canonical URL ends in
|
|
30
|
+
* `/`; the Location points there directly (the host would otherwise add
|
|
31
|
+
* a second hop). The visitor's query string is carried over unless the
|
|
32
|
+
* target has its own query or fragment.
|
|
33
|
+
* - **410.** Rows with status 410 become `gone` entries. Layers that can
|
|
34
|
+
* answer a bare status (Caddy, CloudFront) send 410; the others answer
|
|
35
|
+
* the 404 a path without a file gets anyway.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import { redirectTargetIssue } from "./redirect-target.js";
|
|
39
|
+
import { trimTrailingSlashes } from "./url.js";
|
|
40
|
+
|
|
41
|
+
/** The redirect statuses the redirects table stores (besides 410). */
|
|
42
|
+
export type RedirectStatusCode = 301 | 302 | 307 | 308;
|
|
43
|
+
|
|
44
|
+
/** A row of the redirects table, as the static generator dumps it. */
|
|
45
|
+
export interface RedirectTableRow {
|
|
46
|
+
readonly fromPath: string;
|
|
47
|
+
readonly toPath: string;
|
|
48
|
+
readonly statusCode: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** How the deploy target emits page URLs (`DeployTarget.pageUrlStyle`). */
|
|
52
|
+
export type RedirectPageUrlStyle = "directory" | "no-extension";
|
|
53
|
+
|
|
54
|
+
/** One redirect the serving layer answers. */
|
|
55
|
+
export interface PlannedRedirect {
|
|
56
|
+
/** The stored from path without a trailing slash (`/` for the root). */
|
|
57
|
+
readonly fromPath: string;
|
|
58
|
+
/** Every request path this entry answers, exactly (`/old`, `/old/`). */
|
|
59
|
+
readonly matchPaths: readonly string[];
|
|
60
|
+
/** The Location header value (a same-site path, see module comment). */
|
|
61
|
+
readonly location: string;
|
|
62
|
+
readonly statusCode: RedirectStatusCode;
|
|
63
|
+
/** Append the request's query string to {@link location}. */
|
|
64
|
+
readonly preserveQuery: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One path the serving layer answers with 410 Gone (where it can). */
|
|
68
|
+
export interface PlannedGone {
|
|
69
|
+
readonly fromPath: string;
|
|
70
|
+
readonly matchPaths: readonly string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Everything a serving layer needs to answer the redirects table. */
|
|
74
|
+
export interface RedirectPlan {
|
|
75
|
+
readonly redirects: readonly PlannedRedirect[];
|
|
76
|
+
readonly gone: readonly PlannedGone[];
|
|
77
|
+
/** From paths left out because a build file serves them (#575). */
|
|
78
|
+
readonly shadowedByBuild: readonly string[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const REDIRECT_STATUSES: ReadonlySet<number> = new Set([301, 302, 307, 308]);
|
|
82
|
+
const GONE_STATUS = 410;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Parse the generator's `_redirects` file (`<from> <to> <status>` per line,
|
|
86
|
+
* `#` comments). Throws on a malformed line — a rule silently dropped is a
|
|
87
|
+
* 404 on a URL someone links to.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* parseRedirectsFile("# generated\n/old /new 301\n")
|
|
91
|
+
* // → [{ fromPath: "/old", toPath: "/new", statusCode: 301 }]
|
|
92
|
+
*/
|
|
93
|
+
export function parseRedirectsFile(text: string): RedirectTableRow[] {
|
|
94
|
+
const rows: RedirectTableRow[] = [];
|
|
95
|
+
for (const [index, raw] of text.split("\n").entries()) {
|
|
96
|
+
const line = raw.trim();
|
|
97
|
+
if (line.length === 0 || line.startsWith("#")) continue;
|
|
98
|
+
const parts = line.split(" ");
|
|
99
|
+
const [fromPath, toPath, status] = parts;
|
|
100
|
+
const statusCode = Number(status);
|
|
101
|
+
if (parts.length !== 3 || !fromPath || !toPath || !Number.isInteger(statusCode)) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`redirect-plan: _redirects line ${index + 1} is not "<from> <to> <status>": ${JSON.stringify(raw)}. The static generator writes this file from the redirects table — fix the row there (find_redirects / bulk_create_redirects) and rebuild.`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
rows.push({ fromPath, toPath, statusCode });
|
|
107
|
+
}
|
|
108
|
+
return rows;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Serialise rows into the `_redirects` file format {@link parseRedirectsFile}
|
|
113
|
+
* reads (also the Netlify / Cloudflare Pages format).
|
|
114
|
+
*/
|
|
115
|
+
export function formatRedirectsFile(rows: readonly RedirectTableRow[]): string {
|
|
116
|
+
return [
|
|
117
|
+
"# Auto-generated by Caelo static-generator. Do not edit by hand.",
|
|
118
|
+
...rows.map((r) => `${r.fromPath} ${r.toPath} ${r.statusCode}`),
|
|
119
|
+
"",
|
|
120
|
+
].join("\n");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Arguments of {@link buildRedirectPlan}. */
|
|
124
|
+
export interface BuildRedirectPlanArgs {
|
|
125
|
+
readonly rows: readonly RedirectTableRow[];
|
|
126
|
+
readonly pageUrlStyle: RedirectPageUrlStyle;
|
|
127
|
+
/**
|
|
128
|
+
* Every file of the build, relative to the build root without a leading
|
|
129
|
+
* slash (`de/preise/index.html`). A from path one of them serves is left
|
|
130
|
+
* out of the plan.
|
|
131
|
+
*/
|
|
132
|
+
readonly servedFiles: ReadonlySet<string>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface NormalisedRow {
|
|
136
|
+
readonly bare: string;
|
|
137
|
+
readonly toPath: string;
|
|
138
|
+
readonly statusCode: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Build the redirect plan every provider serves (see the module comment for
|
|
143
|
+
* the semantics). Pure and deterministic: entries keep the rows' order.
|
|
144
|
+
* Throws with an actionable message on rows no edge layer can serve
|
|
145
|
+
* correctly — a malformed path, an unsafe target, conflicting duplicates, a
|
|
146
|
+
* loop.
|
|
147
|
+
*/
|
|
148
|
+
export function buildRedirectPlan(args: BuildRedirectPlanArgs): RedirectPlan {
|
|
149
|
+
const rows = normaliseRows(args.rows);
|
|
150
|
+
const shadowedByBuild: string[] = [];
|
|
151
|
+
const live = new Map<string, NormalisedRow>();
|
|
152
|
+
for (const row of rows) {
|
|
153
|
+
if (servedByBuild(row.bare, args.servedFiles)) {
|
|
154
|
+
shadowedByBuild.push(row.bare);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
live.set(row.bare, row);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const redirects: PlannedRedirect[] = [];
|
|
161
|
+
const gone: PlannedGone[] = [];
|
|
162
|
+
for (const row of live.values()) {
|
|
163
|
+
const matchPaths = matchPathsFor(row.bare);
|
|
164
|
+
if (row.statusCode === GONE_STATUS) {
|
|
165
|
+
gone.push({ fromPath: row.bare, matchPaths });
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const { target, statusCode } = collapseChain(row, live);
|
|
169
|
+
const location = locationFor(target, args.pageUrlStyle);
|
|
170
|
+
redirects.push({
|
|
171
|
+
fromPath: row.bare,
|
|
172
|
+
matchPaths,
|
|
173
|
+
location,
|
|
174
|
+
statusCode,
|
|
175
|
+
preserveQuery: !/[?#]/.test(location),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return { redirects, gone, shadowedByBuild };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function normaliseRows(rows: readonly RedirectTableRow[]): NormalisedRow[] {
|
|
182
|
+
const byBare = new Map<string, NormalisedRow>();
|
|
183
|
+
const out: NormalisedRow[] = [];
|
|
184
|
+
for (const row of rows) {
|
|
185
|
+
const { fromPath, toPath, statusCode } = row;
|
|
186
|
+
const shown = JSON.stringify(fromPath);
|
|
187
|
+
if (!fromPath.startsWith("/") || /[\s\p{Cc}]/u.test(fromPath)) {
|
|
188
|
+
throw new Error(
|
|
189
|
+
`redirect-plan: from path ${shown} must start with '/' and contain no whitespace or control characters. Fix the row with bulk_create_redirects (upsert) or delete it with bulk_delete_redirects, then rebuild.`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
if (/[?#]/.test(fromPath)) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`redirect-plan: from path ${shown} contains '?' or '#'. Redirects match the URL path only, so this row can never match. Re-create it for the path alone (e.g. '${pathBeforeQuery(fromPath)}') or delete it with bulk_delete_redirects, then rebuild.`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
if (statusCode !== GONE_STATUS && !REDIRECT_STATUSES.has(statusCode)) {
|
|
198
|
+
throw new Error(
|
|
199
|
+
`redirect-plan: the redirect for ${shown} has status ${statusCode}; the redirects table allows 301, 302, 307, 308 or 410. Fix the row and rebuild.`,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (statusCode !== GONE_STATUS) {
|
|
203
|
+
const targetIssue = redirectTargetIssue(toPath);
|
|
204
|
+
if (targetIssue) throw new Error(`redirect-plan: the redirect for ${shown}: ${targetIssue}`);
|
|
205
|
+
}
|
|
206
|
+
const bare = stripTrailingSlash(fromPath);
|
|
207
|
+
const normalised: NormalisedRow = { bare, toPath, statusCode };
|
|
208
|
+
const existing = byBare.get(bare);
|
|
209
|
+
if (existing) {
|
|
210
|
+
if (existing.toPath === toPath && existing.statusCode === statusCode) continue;
|
|
211
|
+
throw new Error(
|
|
212
|
+
`redirect-plan: two redirects answer ${JSON.stringify(bare)} (with and without the trailing slash) differently: → ${existing.toPath} (${existing.statusCode}) and → ${toPath} (${statusCode}). Delete one with bulk_delete_redirects, then rebuild.`,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
byBare.set(bare, normalised);
|
|
216
|
+
out.push(normalised);
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Follow `row`'s target through the plan's own rows to the final hop. */
|
|
222
|
+
function collapseChain(
|
|
223
|
+
row: NormalisedRow,
|
|
224
|
+
live: ReadonlyMap<string, NormalisedRow>,
|
|
225
|
+
): { target: string; statusCode: RedirectStatusCode } {
|
|
226
|
+
const visited = [row.bare];
|
|
227
|
+
let target = row.toPath;
|
|
228
|
+
let permanent = isPermanent(row.statusCode);
|
|
229
|
+
let preservesMethod = preservesRequestMethod(row.statusCode);
|
|
230
|
+
for (;;) {
|
|
231
|
+
// A target with its own query or fragment is a different URL than the
|
|
232
|
+
// path the next row matches; the chain ends there.
|
|
233
|
+
if (/[?#]/.test(target)) break;
|
|
234
|
+
const next = live.get(stripTrailingSlash(target));
|
|
235
|
+
if (!next || next.statusCode === GONE_STATUS) break;
|
|
236
|
+
if (visited.includes(next.bare)) {
|
|
237
|
+
const cycle = [...visited.slice(visited.indexOf(next.bare)), next.bare].join(" → ");
|
|
238
|
+
throw new Error(
|
|
239
|
+
`redirect-plan: the redirects ${cycle} form a loop, which browsers answer with "too many redirects". Delete the row that should no longer apply with bulk_delete_redirects (find_redirects lists them), then rebuild.`,
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
visited.push(next.bare);
|
|
243
|
+
permanent &&= isPermanent(next.statusCode);
|
|
244
|
+
preservesMethod &&= preservesRequestMethod(next.statusCode);
|
|
245
|
+
target = next.toPath;
|
|
246
|
+
}
|
|
247
|
+
return { target, statusCode: combinedStatus(permanent, preservesMethod) };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function isPermanent(status: number): boolean {
|
|
251
|
+
return status === 301 || status === 308;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function preservesRequestMethod(status: number): boolean {
|
|
255
|
+
return status === 307 || status === 308;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function combinedStatus(permanent: boolean, preservesMethod: boolean): RedirectStatusCode {
|
|
259
|
+
if (permanent) return preservesMethod ? 308 : 301;
|
|
260
|
+
return preservesMethod ? 307 : 302;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function stripTrailingSlash(path: string): string {
|
|
264
|
+
return path.length > 1 ? trimTrailingSlashes(path) || "/" : path;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The part of a stored path before its first `?` or `#` (`/` if empty). */
|
|
268
|
+
function pathBeforeQuery(path: string): string {
|
|
269
|
+
const cut = path.search(/[?#]/);
|
|
270
|
+
return (cut === -1 ? path : path.slice(0, cut)) || "/";
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Whether the last path segment names a file (`/a/b.html`). */
|
|
274
|
+
function looksLikeFile(path: string): boolean {
|
|
275
|
+
return path.slice(path.lastIndexOf("/") + 1).includes(".");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function matchPathsFor(bare: string): readonly string[] {
|
|
279
|
+
if (bare === "/" || looksLikeFile(bare)) return [bare];
|
|
280
|
+
return [bare, `${bare}/`];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Does a file of the build answer `bare`? Hosts serve `/x` from the file
|
|
284
|
+
* `x` and from the directory index `x/index.html`. */
|
|
285
|
+
function servedByBuild(bare: string, servedFiles: ReadonlySet<string>): boolean {
|
|
286
|
+
if (bare === "/") return servedFiles.has("index.html");
|
|
287
|
+
const rel = bare.slice(1);
|
|
288
|
+
return servedFiles.has(rel) || servedFiles.has(`${rel}/index.html`);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** In the directory style, internal page paths end in "/"; files (anything
|
|
292
|
+
* with an extension in its last segment) and URLs with a query or fragment
|
|
293
|
+
* stay as they are. */
|
|
294
|
+
function locationFor(toPath: string, pageUrlStyle: RedirectPageUrlStyle): string {
|
|
295
|
+
if (pageUrlStyle !== "directory" || toPath.endsWith("/")) return toPath;
|
|
296
|
+
if (/[?#]/.test(toPath) || looksLikeFile(toPath)) return toPath;
|
|
297
|
+
return `${toPath}/`;
|
|
298
|
+
}
|