@dogsbay/site-files 0.2.0-beta.145 → 0.2.0-beta.147
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 +148 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/products.d.ts +12 -0
- package/dist/products.js +78 -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,148 @@
|
|
|
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
|
+
// The two special tokens are longer than one character, so no literal (one character) equals them:
|
|
32
|
+
// a literal `:` (not followed by a letter) stays a colon, not a placeholder
|
|
33
|
+
const ANY = "**any";
|
|
34
|
+
const SEGMENT = "**segment";
|
|
35
|
+
const tokens = [];
|
|
36
|
+
for (let i = 0; i < from.length;) {
|
|
37
|
+
if (from[i] === "*") {
|
|
38
|
+
if (tokens[tokens.length - 1] !== ANY)
|
|
39
|
+
tokens.push(ANY);
|
|
40
|
+
i++;
|
|
41
|
+
}
|
|
42
|
+
else if (from[i] === ":" && /[A-Za-z]/.test(from[i + 1] ?? "")) {
|
|
43
|
+
tokens.push(SEGMENT);
|
|
44
|
+
i += 2;
|
|
45
|
+
while (i < from.length && /\w/.test(from[i]))
|
|
46
|
+
i++;
|
|
47
|
+
}
|
|
48
|
+
else
|
|
49
|
+
tokens.push(from[i++]);
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
test(path) {
|
|
53
|
+
const n = path.length;
|
|
54
|
+
let prev = new Uint8Array(n + 1);
|
|
55
|
+
prev[0] = 1;
|
|
56
|
+
for (const t of tokens) {
|
|
57
|
+
const cur = new Uint8Array(n + 1);
|
|
58
|
+
for (let j = 0; j <= n; j++) {
|
|
59
|
+
if (t === ANY)
|
|
60
|
+
cur[j] = prev[j] || (j > 0 ? cur[j - 1] : 0);
|
|
61
|
+
else if (t === SEGMENT)
|
|
62
|
+
cur[j] = j > 0 && path[j - 1] !== "/" ? prev[j - 1] || cur[j - 1] : 0;
|
|
63
|
+
else
|
|
64
|
+
cur[j] = j > 0 && path[j - 1] === t ? prev[j - 1] : 0;
|
|
65
|
+
}
|
|
66
|
+
prev = cur;
|
|
67
|
+
}
|
|
68
|
+
return prev[n] === 1;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* `ours` is a generated `_redirects` (comments, then `from to status` lines). A generated rule whose
|
|
74
|
+
* source one of the project's unconditional rules already matches never applies, and is left out.
|
|
75
|
+
*/
|
|
76
|
+
export function mergeRedirects(theirs, ours, cloudflare) {
|
|
77
|
+
const notes = [];
|
|
78
|
+
const own = redirectLines(theirs);
|
|
79
|
+
const matchers = own.filter((r) => r.from.startsWith("/") && !r.conditional).map((r) => ({ from: r.from, re: matcher(r.from) }));
|
|
80
|
+
const kept = [];
|
|
81
|
+
const dropped = [];
|
|
82
|
+
for (const line of (ours ?? "").split("\n")) {
|
|
83
|
+
if (!meaningful(line)) {
|
|
84
|
+
if (line.trim())
|
|
85
|
+
kept.push(line);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const from = line.trim().split(/\s+/)[0];
|
|
89
|
+
const by = matchers.find((m) => m.re.test(from));
|
|
90
|
+
if (by)
|
|
91
|
+
dropped.push(by.from === from ? from : `${from} (by ${by.from})`);
|
|
92
|
+
else
|
|
93
|
+
kept.push(line);
|
|
94
|
+
}
|
|
95
|
+
if (dropped.length)
|
|
96
|
+
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 ? ", …" : ""}`);
|
|
97
|
+
const generated = kept.filter(meaningful);
|
|
98
|
+
const text = joinBlocks(theirs, generated.length ? kept.join("\n") : "");
|
|
99
|
+
if (cloudflare) {
|
|
100
|
+
const froms = [...own.map((r) => r.from), ...generated.map((l) => l.trim().split(/\s+/)[0])];
|
|
101
|
+
const dynamic = froms.filter(isDynamic).length;
|
|
102
|
+
const fixed = froms.length - dynamic;
|
|
103
|
+
if (fixed > CLOUDFLARE_LIMITS.staticRedirects || dynamic > CLOUDFLARE_LIMITS.dynamicRedirects) {
|
|
104
|
+
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)`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { text, notes };
|
|
108
|
+
}
|
|
109
|
+
function headerRules(text) {
|
|
110
|
+
const rules = [];
|
|
111
|
+
for (const line of text.split(/\r?\n/)) {
|
|
112
|
+
if (!meaningful(line))
|
|
113
|
+
continue;
|
|
114
|
+
if (!/^\s/.test(line))
|
|
115
|
+
rules.push({ path: line.trim(), lines: [] });
|
|
116
|
+
else if (rules.length) {
|
|
117
|
+
const t = line.trim();
|
|
118
|
+
const at = t.indexOf(":");
|
|
119
|
+
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, " ") });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return rules;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* `ours` is a generated `_headers`. A generated header the project's file already sets, with the same
|
|
126
|
+
* value on the same path, is left out (the host would send it twice, joined by a comma), and so is one
|
|
127
|
+
* it detaches there (`! Link`); others are appended after the project's rules, so both apply.
|
|
128
|
+
*/
|
|
129
|
+
export function mergeHeaders(theirs, ours, cloudflare) {
|
|
130
|
+
const notes = [];
|
|
131
|
+
const own = headerRules(theirs);
|
|
132
|
+
const set = new Set(own.flatMap((r) => r.lines.map((l) => `${r.path}\0${l.key}`)));
|
|
133
|
+
const added = [];
|
|
134
|
+
let rules = own.length;
|
|
135
|
+
for (const r of headerRules(ours ?? "")) {
|
|
136
|
+
const detached = (l) => set.has(`${r.path}\0! ${l.key.slice(0, l.key.indexOf(":"))}`);
|
|
137
|
+
const lines = r.lines.filter((l) => !set.has(`${r.path}\0${l.key}`) && !detached(l));
|
|
138
|
+
if (lines.length < r.lines.length)
|
|
139
|
+
notes.push(`${r.lines.length - lines.length} generated header(s) on ${r.path} left out: the project's _headers already sets or detaches them`);
|
|
140
|
+
if (!lines.length)
|
|
141
|
+
continue;
|
|
142
|
+
added.push(r.path, ...lines.map((l) => l.line));
|
|
143
|
+
rules++;
|
|
144
|
+
}
|
|
145
|
+
if (cloudflare && rules > CLOUDFLARE_LIMITS.headerRules)
|
|
146
|
+
notes.push(`_headers has ${rules} rules; Cloudflare reads at most ${CLOUDFLARE_LIMITS.headerRules}`);
|
|
147
|
+
return { text: joinBlocks(theirs, added.join("\n")), notes };
|
|
148
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/products.d.ts
CHANGED
|
@@ -267,3 +267,15 @@ export declare function toPagesScript(key: string, pages: readonly string[]): st
|
|
|
267
267
|
* close a `<script>` or break the statement.
|
|
268
268
|
*/
|
|
269
269
|
export declare function toScript(global: string, value: unknown): string;
|
|
270
|
+
/**
|
|
271
|
+
* A later selection added to a project's parsed config (`content` as written): its products and
|
|
272
|
+
* versions merged in, its sources appended. A product or version already there keeps its
|
|
273
|
+
* settings and default (the project's word wins); a source already there is not added twice.
|
|
274
|
+
* Site-wide `versions:` merge the same way. The result is checked by `productModel` (throws
|
|
275
|
+
* `ProductConfigError`), so a merge that would not build is refused here. The host writes YAML.
|
|
276
|
+
*/
|
|
277
|
+
export declare function addToConfig<C extends ProductsContent & {
|
|
278
|
+
sources?: ProductSource[];
|
|
279
|
+
}>(content: C, addition: ProductsContent & {
|
|
280
|
+
sources?: ProductSource[];
|
|
281
|
+
}): C;
|
package/dist/products.js
CHANGED
|
@@ -485,3 +485,81 @@ export function toScript(global, value) {
|
|
|
485
485
|
const json = JSON.stringify(value).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
486
486
|
return `window.${global}=${json};\n`;
|
|
487
487
|
}
|
|
488
|
+
/** A source as a config writes it; compared by everything that says what it reads and where it goes. */
|
|
489
|
+
const sourceKey = (s) => JSON.stringify(["name", "version", "locale", "path", "git", "ref", "subdir", "from", "mount"].map((k) => s[k] ?? null));
|
|
490
|
+
/**
|
|
491
|
+
* A later selection added to a project's parsed config (`content` as written): its products and
|
|
492
|
+
* versions merged in, its sources appended. A product or version already there keeps its
|
|
493
|
+
* settings and default (the project's word wins); a source already there is not added twice.
|
|
494
|
+
* Site-wide `versions:` merge the same way. The result is checked by `productModel` (throws
|
|
495
|
+
* `ProductConfigError`), so a merge that would not build is refused here. The host writes YAML.
|
|
496
|
+
*/
|
|
497
|
+
export function addToConfig(content, addition) {
|
|
498
|
+
// A version added to versions already there is not the default: the root ("" path) stays the
|
|
499
|
+
// default's, so an added one is served at its id (products) or without a path (site-wide)
|
|
500
|
+
const mergeVersions = (into, more, siteWide = false) => {
|
|
501
|
+
if (!more?.length)
|
|
502
|
+
return into;
|
|
503
|
+
const out = [...(into ?? [])];
|
|
504
|
+
const ids = new Set(out.map((v) => String(v.id)));
|
|
505
|
+
for (const v of more) {
|
|
506
|
+
const add = v;
|
|
507
|
+
if (ids.has(String(add.id)))
|
|
508
|
+
continue;
|
|
509
|
+
if (into?.length && add.path === "") {
|
|
510
|
+
const { path: _root, ...rest } = add;
|
|
511
|
+
out.push(siteWide ? rest : { ...rest, path: String(add.id) });
|
|
512
|
+
}
|
|
513
|
+
else
|
|
514
|
+
out.push(v);
|
|
515
|
+
}
|
|
516
|
+
return out;
|
|
517
|
+
};
|
|
518
|
+
const out = { ...content };
|
|
519
|
+
if (addition.products) {
|
|
520
|
+
const products = Object.assign(Object.create(null), content.products ?? {});
|
|
521
|
+
for (const [name, add] of Object.entries(addition.products)) {
|
|
522
|
+
const have = products[name];
|
|
523
|
+
if (!have) {
|
|
524
|
+
products[name] = add;
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
const versions = mergeVersions(have.versions, add.versions);
|
|
528
|
+
products[name] = {
|
|
529
|
+
...add,
|
|
530
|
+
...have,
|
|
531
|
+
...(versions ? { versions } : {}),
|
|
532
|
+
...(have.defaultVersion === undefined && add.defaultVersion !== undefined && !have.versions ? { defaultVersion: add.defaultVersion } : {}),
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
out.products = products;
|
|
536
|
+
}
|
|
537
|
+
if (addition.versions?.length && out.products && Object.keys(out.products).length) {
|
|
538
|
+
throw new Error("a config with a products: block takes versions per product, not site-wide versions:");
|
|
539
|
+
}
|
|
540
|
+
if (addition.products && Object.keys(addition.products).length && content.versions?.length) {
|
|
541
|
+
throw new Error("a config with site-wide versions: cannot take a products: block; import the first product as a product too");
|
|
542
|
+
}
|
|
543
|
+
const versions = mergeVersions(content.versions, addition.versions, true);
|
|
544
|
+
if (versions)
|
|
545
|
+
out.versions = versions;
|
|
546
|
+
if (content.defaultVersion === undefined && addition.defaultVersion !== undefined)
|
|
547
|
+
out.defaultVersion = addition.defaultVersion;
|
|
548
|
+
// Site-wide versions: productModel checks only a products: block, so check the rule the loader has
|
|
549
|
+
for (const v of (out.versions ?? [])) {
|
|
550
|
+
if (v.path === "" && v.id !== out.defaultVersion)
|
|
551
|
+
throw new Error(`version "${String(v.id)}" is served at the site's root, which only the default version "${String(out.defaultVersion)}" may be`);
|
|
552
|
+
}
|
|
553
|
+
const seen = new Set((content.sources ?? []).map((s) => sourceKey(s)));
|
|
554
|
+
const folders = new Map((content.sources ?? []).map((s) => [String(s.path), sourceKey(s)]));
|
|
555
|
+
const added = (addition.sources ?? []).filter((s) => !seen.has(sourceKey(s)));
|
|
556
|
+
for (const s of added) {
|
|
557
|
+
const path = s.path;
|
|
558
|
+
// Another source's folder: the host would write the addition's files over it
|
|
559
|
+
if (typeof path === "string" && folders.has(path))
|
|
560
|
+
throw new Error(`source path "${path}" is already another source's; import into another folder`);
|
|
561
|
+
}
|
|
562
|
+
out.sources = [...(content.sources ?? []), ...added];
|
|
563
|
+
productModel(out);
|
|
564
|
+
return out;
|
|
565
|
+
}
|
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.147",
|
|
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",
|