@dogsbay/site-files 0.2.0-beta.145 → 0.2.0-beta.146
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/host-files.d.ts +31 -0
- package/dist/host-files.js +144 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A project's own `_headers` and `_redirects` merged with the generated ones (plans/root-static-files.md):
|
|
3
|
+
* the project's rules first, ours after, and ours dropped where the project's already decide.
|
|
4
|
+
* Cloudflare and Netlify read both files with the same syntax. Netlify takes the first redirect that
|
|
5
|
+
* matches; Cloudflare tries static rules before dynamic ones, so a generated static rule would win over
|
|
6
|
+
* a project's splat there. Leaving out ours that a project's rule matches makes theirs win on both,
|
|
7
|
+
* keeps the file under Cloudflare's limits, and tells the author. Pure and browser-safe.
|
|
8
|
+
* Moved here from dogsbay-ssg (src/core/host-files.ts, c0a6aa5) so both pipelines merge alike.
|
|
9
|
+
*/
|
|
10
|
+
export interface MergedHostFile {
|
|
11
|
+
text: string;
|
|
12
|
+
/** What was dropped, and what is over a host's limit, for warnings. */
|
|
13
|
+
notes: string[];
|
|
14
|
+
}
|
|
15
|
+
/** Cloudflare's limits (Workers static assets and Pages alike). */
|
|
16
|
+
export declare const CLOUDFLARE_LIMITS: {
|
|
17
|
+
headerRules: number;
|
|
18
|
+
staticRedirects: number;
|
|
19
|
+
dynamicRedirects: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* `ours` is a generated `_redirects` (comments, then `from to status` lines). A generated rule whose
|
|
23
|
+
* source one of the project's unconditional rules already matches never applies, and is left out.
|
|
24
|
+
*/
|
|
25
|
+
export declare function mergeRedirects(theirs: string, ours: string | undefined, cloudflare: boolean): MergedHostFile;
|
|
26
|
+
/**
|
|
27
|
+
* `ours` is a generated `_headers`. A generated header the project's file already sets, with the same
|
|
28
|
+
* value on the same path, is left out (the host would send it twice, joined by a comma), and so is one
|
|
29
|
+
* it detaches there (`! Link`); others are appended after the project's rules, so both apply.
|
|
30
|
+
*/
|
|
31
|
+
export declare function mergeHeaders(theirs: string, ours: string | undefined, cloudflare: boolean): MergedHostFile;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A project's own `_headers` and `_redirects` merged with the generated ones (plans/root-static-files.md):
|
|
3
|
+
* the project's rules first, ours after, and ours dropped where the project's already decide.
|
|
4
|
+
* Cloudflare and Netlify read both files with the same syntax. Netlify takes the first redirect that
|
|
5
|
+
* matches; Cloudflare tries static rules before dynamic ones, so a generated static rule would win over
|
|
6
|
+
* a project's splat there. Leaving out ours that a project's rule matches makes theirs win on both,
|
|
7
|
+
* keeps the file under Cloudflare's limits, and tells the author. Pure and browser-safe.
|
|
8
|
+
* Moved here from dogsbay-ssg (src/core/host-files.ts, c0a6aa5) so both pipelines merge alike.
|
|
9
|
+
*/
|
|
10
|
+
/** Cloudflare's limits (Workers static assets and Pages alike). */
|
|
11
|
+
export const CLOUDFLARE_LIMITS = { headerRules: 100, staticRedirects: 2000, dynamicRedirects: 100 };
|
|
12
|
+
const withNewline = (s) => (s && !s.endsWith("\n") ? s + "\n" : s);
|
|
13
|
+
/** The project's file, then ours after a blank line. */
|
|
14
|
+
const joinBlocks = (theirs, ours) => (theirs.trim() && ours ? `${withNewline(theirs)}\n${withNewline(ours)}` : withNewline(theirs.trim() ? theirs : ours));
|
|
15
|
+
const meaningful = (line) => !!line.trim() && !line.trim().startsWith("#");
|
|
16
|
+
const isDynamic = (from) => /\*|:[A-Za-z]/.test(from);
|
|
17
|
+
function redirectLines(text) {
|
|
18
|
+
return text.split(/\r?\n/).filter(meaningful).map((line) => {
|
|
19
|
+
const [from, ...rest] = line.trim().split(/\s+/);
|
|
20
|
+
return { from, line, conditional: rest.some((t) => /^[\w-]+=/.test(t)) };
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A redirect source as both hosts match it: `*` any characters, `:name` one segment. A trailing slash
|
|
25
|
+
* counts: Cloudflare treats `/a` and `/a/` as different sources.
|
|
26
|
+
*/
|
|
27
|
+
function matcher(from) {
|
|
28
|
+
// Tokens: a literal character, `*` (any characters) or `:name` (one segment). Matched by dynamic
|
|
29
|
+
// programming over (token, position), O(tokens × length): a regex from a project's rule like
|
|
30
|
+
// `/*a*a*a*a*b` backtracked exponentially, and a repo's _redirects could hang the build.
|
|
31
|
+
const tokens = [];
|
|
32
|
+
for (let i = 0; i < from.length;) {
|
|
33
|
+
if (from[i] === "*") {
|
|
34
|
+
if (tokens[tokens.length - 1] !== "*")
|
|
35
|
+
tokens.push("*");
|
|
36
|
+
i++;
|
|
37
|
+
}
|
|
38
|
+
else if (from[i] === ":" && /[A-Za-z]/.test(from[i + 1] ?? "")) {
|
|
39
|
+
tokens.push(":");
|
|
40
|
+
i += 2;
|
|
41
|
+
while (i < from.length && /\w/.test(from[i]))
|
|
42
|
+
i++;
|
|
43
|
+
}
|
|
44
|
+
else
|
|
45
|
+
tokens.push(from[i++]);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
test(path) {
|
|
49
|
+
const n = path.length;
|
|
50
|
+
let prev = new Uint8Array(n + 1);
|
|
51
|
+
prev[0] = 1;
|
|
52
|
+
for (const t of tokens) {
|
|
53
|
+
const cur = new Uint8Array(n + 1);
|
|
54
|
+
for (let j = 0; j <= n; j++) {
|
|
55
|
+
if (t === "*")
|
|
56
|
+
cur[j] = prev[j] || (j > 0 ? cur[j - 1] : 0);
|
|
57
|
+
else if (t === ":")
|
|
58
|
+
cur[j] = j > 0 && path[j - 1] !== "/" ? prev[j - 1] || cur[j - 1] : 0;
|
|
59
|
+
else
|
|
60
|
+
cur[j] = j > 0 && path[j - 1] === t ? prev[j - 1] : 0;
|
|
61
|
+
}
|
|
62
|
+
prev = cur;
|
|
63
|
+
}
|
|
64
|
+
return prev[n] === 1;
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* `ours` is a generated `_redirects` (comments, then `from to status` lines). A generated rule whose
|
|
70
|
+
* source one of the project's unconditional rules already matches never applies, and is left out.
|
|
71
|
+
*/
|
|
72
|
+
export function mergeRedirects(theirs, ours, cloudflare) {
|
|
73
|
+
const notes = [];
|
|
74
|
+
const own = redirectLines(theirs);
|
|
75
|
+
const matchers = own.filter((r) => r.from.startsWith("/") && !r.conditional).map((r) => ({ from: r.from, re: matcher(r.from) }));
|
|
76
|
+
const kept = [];
|
|
77
|
+
const dropped = [];
|
|
78
|
+
for (const line of (ours ?? "").split("\n")) {
|
|
79
|
+
if (!meaningful(line)) {
|
|
80
|
+
if (line.trim())
|
|
81
|
+
kept.push(line);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const from = line.trim().split(/\s+/)[0];
|
|
85
|
+
const by = matchers.find((m) => m.re.test(from));
|
|
86
|
+
if (by)
|
|
87
|
+
dropped.push(by.from === from ? from : `${from} (by ${by.from})`);
|
|
88
|
+
else
|
|
89
|
+
kept.push(line);
|
|
90
|
+
}
|
|
91
|
+
if (dropped.length)
|
|
92
|
+
notes.push(`${dropped.length} generated redirect(s) left out: the project's _redirects already has a rule for them: ${dropped.slice(0, 5).join(", ")}${dropped.length > 5 ? ", …" : ""}`);
|
|
93
|
+
const generated = kept.filter(meaningful);
|
|
94
|
+
const text = joinBlocks(theirs, generated.length ? kept.join("\n") : "");
|
|
95
|
+
if (cloudflare) {
|
|
96
|
+
const froms = [...own.map((r) => r.from), ...generated.map((l) => l.trim().split(/\s+/)[0])];
|
|
97
|
+
const dynamic = froms.filter(isDynamic).length;
|
|
98
|
+
const fixed = froms.length - dynamic;
|
|
99
|
+
if (fixed > CLOUDFLARE_LIMITS.staticRedirects || dynamic > CLOUDFLARE_LIMITS.dynamicRedirects) {
|
|
100
|
+
notes.push(`_redirects has ${fixed} static and ${dynamic} dynamic rule(s); Cloudflare reads at most ${CLOUDFLARE_LIMITS.staticRedirects} static and ${CLOUDFLARE_LIMITS.dynamicRedirects} dynamic ones (use Bulk Redirects for more)`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { text, notes };
|
|
104
|
+
}
|
|
105
|
+
function headerRules(text) {
|
|
106
|
+
const rules = [];
|
|
107
|
+
for (const line of text.split(/\r?\n/)) {
|
|
108
|
+
if (!meaningful(line))
|
|
109
|
+
continue;
|
|
110
|
+
if (!/^\s/.test(line))
|
|
111
|
+
rules.push({ path: line.trim(), lines: [] });
|
|
112
|
+
else if (rules.length) {
|
|
113
|
+
const t = line.trim();
|
|
114
|
+
const at = t.indexOf(":");
|
|
115
|
+
rules[rules.length - 1].lines.push({ line, key: at > 0 ? `${t.slice(0, at).trim().toLowerCase()}:${t.slice(at + 1).trim().replace(/\s+/g, " ")}` : t.toLowerCase().replace(/\s+/g, " ") });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return rules;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* `ours` is a generated `_headers`. A generated header the project's file already sets, with the same
|
|
122
|
+
* value on the same path, is left out (the host would send it twice, joined by a comma), and so is one
|
|
123
|
+
* it detaches there (`! Link`); others are appended after the project's rules, so both apply.
|
|
124
|
+
*/
|
|
125
|
+
export function mergeHeaders(theirs, ours, cloudflare) {
|
|
126
|
+
const notes = [];
|
|
127
|
+
const own = headerRules(theirs);
|
|
128
|
+
const set = new Set(own.flatMap((r) => r.lines.map((l) => `${r.path}\0${l.key}`)));
|
|
129
|
+
const added = [];
|
|
130
|
+
let rules = own.length;
|
|
131
|
+
for (const r of headerRules(ours ?? "")) {
|
|
132
|
+
const detached = (l) => set.has(`${r.path}\0! ${l.key.slice(0, l.key.indexOf(":"))}`);
|
|
133
|
+
const lines = r.lines.filter((l) => !set.has(`${r.path}\0${l.key}`) && !detached(l));
|
|
134
|
+
if (lines.length < r.lines.length)
|
|
135
|
+
notes.push(`${r.lines.length - lines.length} generated header(s) on ${r.path} left out: the project's _headers already sets or detaches them`);
|
|
136
|
+
if (!lines.length)
|
|
137
|
+
continue;
|
|
138
|
+
added.push(r.path, ...lines.map((l) => l.line));
|
|
139
|
+
rules++;
|
|
140
|
+
}
|
|
141
|
+
if (cloudflare && rules > CLOUDFLARE_LIMITS.headerRules)
|
|
142
|
+
notes.push(`_headers has ${rules} rules; Cloudflare reads at most ${CLOUDFLARE_LIMITS.headerRules}`);
|
|
143
|
+
return { text: joinBlocks(theirs, added.join("\n")), notes };
|
|
144
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dogsbay/site-files",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.146",
|
|
4
4
|
"description": "robots.txt, _headers, llms.txt, sitemaps, redirects and JSON-LD for docs sites; shared by the dogsbay Astro pipeline and dogsbay-ssg, Worker-safe",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|