@johnhenry/sitefile 0.0.2 → 0.0.3
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/CHANGELOG.md +467 -0
- package/README.md +55 -6
- package/lib/src/archive-writers.d.ts +12 -2
- package/lib/src/archive-writers.js +53 -10
- package/lib/src/canonical.d.ts +16 -0
- package/lib/src/canonical.js +46 -0
- package/lib/src/car.d.ts +34 -0
- package/lib/src/car.js +203 -0
- package/lib/src/checklinks.js +8 -2
- package/lib/src/cli/bake.js +4 -3
- package/lib/src/cli/build-image.d.ts +1 -0
- package/lib/src/cli/build-image.js +1 -0
- package/lib/src/cli/common.js +4 -1
- package/lib/src/cli/hosts.d.ts +5 -0
- package/lib/src/cli/hosts.js +78 -0
- package/lib/src/cli/images.js +23 -5
- package/lib/src/cli/import-config.d.ts +2 -0
- package/lib/src/cli/import-config.js +56 -0
- package/lib/src/cli/index.d.ts +3 -1
- package/lib/src/cli/index.js +31 -2
- package/lib/src/cli/integrity.d.ts +2 -0
- package/lib/src/cli/integrity.js +77 -0
- package/lib/src/cli/network.d.ts +7 -0
- package/lib/src/cli/network.js +14 -3
- package/lib/src/cli/serve-keys.d.ts +39 -0
- package/lib/src/cli/serve-keys.js +106 -0
- package/lib/src/cli/serve-worker.js +46 -8
- package/lib/src/cli/servers.d.ts +124 -1
- package/lib/src/cli/servers.js +705 -62
- package/lib/src/cli/up.d.ts +2 -0
- package/lib/src/cli/up.js +193 -0
- package/lib/src/cli/watch.d.ts +14 -1
- package/lib/src/cli/watch.js +20 -3
- package/lib/src/cli.js +138 -18
- package/lib/src/csp.d.ts +17 -0
- package/lib/src/csp.js +52 -0
- package/lib/src/dictionary.d.ts +16 -0
- package/lib/src/dictionary.js +89 -0
- package/lib/src/epub.d.ts +5 -0
- package/lib/src/epub.js +113 -0
- package/lib/src/errors.d.ts +22 -9
- package/lib/src/errors.js +20 -1
- package/lib/src/executor.d.ts +31 -1
- package/lib/src/executor.js +192 -22
- package/lib/src/explain.js +33 -3
- package/lib/src/export-for.d.ts +42 -0
- package/lib/src/export-for.js +466 -0
- package/lib/src/export-formats.d.ts +2 -2
- package/lib/src/export-formats.js +52 -2
- package/lib/src/fingerprint.d.ts +3 -1
- package/lib/src/fingerprint.js +32 -5
- package/lib/src/host-config.d.ts +34 -0
- package/lib/src/host-config.js +645 -0
- package/lib/src/hosts.d.ts +47 -0
- package/lib/src/hosts.js +106 -0
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/integrity.d.ts +115 -0
- package/lib/src/integrity.js +273 -0
- package/lib/src/language/ast.d.ts +116 -3
- package/lib/src/language/ast.js +7 -1
- package/lib/src/language/parser.js +176 -7
- package/lib/src/language/validate.js +21 -0
- package/lib/src/links.d.ts +33 -0
- package/lib/src/links.js +271 -3
- package/lib/src/lint.d.ts +1 -1
- package/lib/src/lint.js +26 -1
- package/lib/src/mime-data.d.ts +2 -0
- package/lib/src/mime-data.js +92 -0
- package/lib/src/oci.d.ts +20 -2
- package/lib/src/oci.js +70 -28
- package/lib/src/outdated.d.ts +3 -0
- package/lib/src/outdated.js +4 -4
- package/lib/src/planner.d.ts +69 -2
- package/lib/src/planner.js +150 -8
- package/lib/src/plugins/cache.d.ts +11 -0
- package/lib/src/plugins/cache.js +29 -0
- package/lib/src/plugins/host.d.ts +51 -2
- package/lib/src/plugins/host.js +141 -16
- package/lib/src/plugins/wasm.js +1 -3
- package/lib/src/portable.d.ts +8 -0
- package/lib/src/portable.js +47 -0
- package/lib/src/precache.d.ts +33 -0
- package/lib/src/precache.js +75 -0
- package/lib/src/precompress.d.ts +19 -0
- package/lib/src/precompress.js +35 -0
- package/lib/src/qr.d.ts +18 -0
- package/lib/src/qr.js +372 -0
- package/lib/src/registry-ops.js +4 -2
- package/lib/src/reload-client.d.ts +2 -0
- package/lib/src/reload-client.js +304 -0
- package/lib/src/resources/archive.d.ts +36 -0
- package/lib/src/resources/archive.js +338 -67
- package/lib/src/resources/convert.js +3 -1
- package/lib/src/resources/https.d.ts +9 -1
- package/lib/src/resources/https.js +36 -0
- package/lib/src/resources/network.d.ts +25 -3
- package/lib/src/resources/network.js +77 -6
- package/lib/src/secrets.d.ts +12 -3
- package/lib/src/secrets.js +18 -5
- package/lib/src/serve-auth.d.ts +32 -0
- package/lib/src/serve-auth.js +112 -0
- package/lib/src/serve-rules.d.ts +83 -7
- package/lib/src/serve-rules.js +264 -53
- package/lib/src/server.d.ts +437 -10
- package/lib/src/server.js +1236 -52
- package/lib/src/servers-file.d.ts +36 -0
- package/lib/src/servers-file.js +101 -0
- package/lib/src/servers.d.ts +109 -7
- package/lib/src/servers.js +151 -29
- package/lib/src/single-file.d.ts +12 -0
- package/lib/src/single-file.js +147 -0
- package/lib/src/site.d.ts +54 -0
- package/lib/src/site.js +204 -6
- package/lib/src/store.js +9 -5
- package/lib/src/swbn.d.ts +10 -0
- package/lib/src/swbn.js +86 -0
- package/lib/src/toml.d.ts +17 -0
- package/lib/src/toml.js +323 -0
- package/lib/src/wacz.d.ts +6 -0
- package/lib/src/wacz.js +96 -0
- package/lib/src/warc.d.ts +28 -0
- package/lib/src/warc.js +252 -0
- package/lib/src/watch.d.ts +12 -1
- package/lib/src/watch.js +67 -1
- package/lib/src/wbn.d.ts +9 -1
- package/lib/src/wbn.js +23 -33
- package/lib/src/zstd.d.ts +4 -0
- package/lib/src/zstd.js +328 -0
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sitefile import-config` (issue #204): translate a host's config file into `_redirects` and `_headers`
|
|
3
|
+
* that `sitefile serve` (and REDIRECT/HEADER) understand today. Ported as rust/src/host_config.rs.
|
|
4
|
+
*
|
|
5
|
+
* Every rule that is written is one `serve` would apply; anything else (placeholders, conditions, proxying,
|
|
6
|
+
* regular expressions, settings with no `_redirects` form) is listed as unsupported with the reason, never
|
|
7
|
+
* written half-translated. See docs/CLI.md, "Host config files", for the mapping table.
|
|
8
|
+
*/
|
|
9
|
+
export declare const HOST_FORMATS: readonly ['netlify', 'vercel', 'firebase', 'azure', 'wrangler', 'htaccess'];
|
|
10
|
+
export type HostFormat = (typeof HOST_FORMATS)[number];
|
|
11
|
+
export interface HeaderBlock {
|
|
12
|
+
readonly pattern: string;
|
|
13
|
+
readonly headers: ReadonlyArray<readonly [string, string]>;
|
|
14
|
+
}
|
|
15
|
+
export interface Unsupported {
|
|
16
|
+
/** Where in the file: `redirects[2]`, `hosting.cleanUrls`, `line 7`. */
|
|
17
|
+
readonly entry: string;
|
|
18
|
+
readonly reason: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ImportedConfig {
|
|
21
|
+
readonly format: HostFormat;
|
|
22
|
+
/** `_redirects` lines (`from to status[!]`). */
|
|
23
|
+
readonly redirects: readonly string[];
|
|
24
|
+
readonly headers: readonly HeaderBlock[];
|
|
25
|
+
readonly unsupported: readonly Unsupported[];
|
|
26
|
+
}
|
|
27
|
+
/** The format a file name implies, if any. */
|
|
28
|
+
export declare function formatOf(file: string): HostFormat | undefined;
|
|
29
|
+
/** Translate the text of a host config file. `file` names it in messages. */
|
|
30
|
+
export declare function importHostConfig(text: string, file: string, format: HostFormat): ImportedConfig;
|
|
31
|
+
/** `_redirects` text (empty when there are no rules). */
|
|
32
|
+
export declare function redirectsText(c: ImportedConfig): string;
|
|
33
|
+
/** `_headers` text (empty when there are no blocks). */
|
|
34
|
+
export declare function headersText(c: ImportedConfig): string;
|
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { SitefileError } from './errors.js';
|
|
3
|
+
import { parseHeaderRules, parseRedirectRules } from './serve-rules.js';
|
|
4
|
+
import { parseToml } from './toml.js';
|
|
5
|
+
/**
|
|
6
|
+
* `sitefile import-config` (issue #204): translate a host's config file into `_redirects` and `_headers`
|
|
7
|
+
* that `sitefile serve` (and REDIRECT/HEADER) understand today. Ported as rust/src/host_config.rs.
|
|
8
|
+
*
|
|
9
|
+
* Every rule that is written is one `serve` would apply; anything else (placeholders, conditions, proxying,
|
|
10
|
+
* regular expressions, settings with no `_redirects` form) is listed as unsupported with the reason, never
|
|
11
|
+
* written half-translated. See docs/CLI.md, "Host config files", for the mapping table.
|
|
12
|
+
*/
|
|
13
|
+
export const HOST_FORMATS = ['netlify', 'vercel', 'firebase', 'azure', 'wrangler', 'htaccess'];
|
|
14
|
+
/** The format a file name implies, if any. */
|
|
15
|
+
export function formatOf(file) {
|
|
16
|
+
const base = path.basename(file).toLowerCase();
|
|
17
|
+
if (base === 'netlify.toml')
|
|
18
|
+
return 'netlify';
|
|
19
|
+
if (base === 'vercel.json')
|
|
20
|
+
return 'vercel';
|
|
21
|
+
if (base === 'firebase.json')
|
|
22
|
+
return 'firebase';
|
|
23
|
+
if (base === 'staticwebapp.config.json')
|
|
24
|
+
return 'azure';
|
|
25
|
+
if (base === 'wrangler.toml' || base === 'wrangler.json')
|
|
26
|
+
return 'wrangler';
|
|
27
|
+
if (base === '.htaccess')
|
|
28
|
+
return 'htaccess';
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
const PLACEHOLDERS = ':placeholders are supported in redirects, not in header patterns';
|
|
32
|
+
const MIDDLE_STAR = '* is supported only as a trailing /*';
|
|
33
|
+
const CONDITIONS = 'conditions are not supported';
|
|
34
|
+
const isMapping = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
35
|
+
const isStr = (v) => typeof v === 'string';
|
|
36
|
+
const isInt = (v) => typeof v === 'number' && Number.isSafeInteger(v);
|
|
37
|
+
class Out {
|
|
38
|
+
redirects = [];
|
|
39
|
+
/** Catch-all rules (404 pages, SPA fallbacks): written after every other rule, which they would shadow. */
|
|
40
|
+
fallbacks = [];
|
|
41
|
+
headers = [];
|
|
42
|
+
unsupported = [];
|
|
43
|
+
skip(entry, reason) {
|
|
44
|
+
this.unsupported.push({ entry, reason });
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Add `from [query…] to status[!] [conditions…]` when `serve` would apply it as written (and every
|
|
48
|
+
* `:name` the target uses is captured by the source); otherwise list why not.
|
|
49
|
+
*/
|
|
50
|
+
redirect(entry, from, to, status, force = false, fallback = false, query = [], conditions = []) {
|
|
51
|
+
const line = [from, ...query, to, `${status}${force ? '!' : ''}`, ...conditions].join(' ');
|
|
52
|
+
const unknown = unknownName(from, query, to);
|
|
53
|
+
if (parseRedirectRules(line).length === 1 && unknown === undefined) {
|
|
54
|
+
(fallback ? this.fallbacks : this.redirects).push(line);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
this.skip(entry, unknown !== undefined ? `:${unknown} in the target is not in the source` : redirectReason(from, to, status));
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
/** Add a `_headers` block when every header is one `serve` would set; otherwise list why not. */
|
|
61
|
+
header(entry, pattern, headers) {
|
|
62
|
+
if (headers.length === 0)
|
|
63
|
+
return;
|
|
64
|
+
const text = `${pattern}\n${headers.map(([n, v]) => ` ${n}: ${v}`).join('\n')}\n`;
|
|
65
|
+
const parsed = parseHeaderRules(text);
|
|
66
|
+
if (parsed.length !== 1 || parsed[0]?.pattern !== pattern || parsed[0].headers.length !== headers.length || /[ \t\n\r\f\v]/.test(pattern)) {
|
|
67
|
+
this.skip(entry, headerReason(pattern, headers));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const last = this.headers[this.headers.length - 1];
|
|
71
|
+
if (last && last.pattern === pattern)
|
|
72
|
+
this.headers[this.headers.length - 1] = { pattern, headers: [...last.headers, ...headers] };
|
|
73
|
+
else
|
|
74
|
+
this.headers.push({ pattern, headers });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** The `:name`s a target uses (at the start of a path segment, after its `https://host`). */
|
|
78
|
+
function targetNames(to) {
|
|
79
|
+
return [...to.replace(/^https?:\/\/[^/]*/i, '').matchAll(/(?:^|\/):([A-Za-z_][A-Za-z0-9_]*)/g)].map((m) => m[1]);
|
|
80
|
+
}
|
|
81
|
+
/** A name the target uses that the source (its `:name` segments, `/*` as `splat`, `name=:value` query fields) does not capture. */
|
|
82
|
+
function unknownName(from, query, to) {
|
|
83
|
+
const captured = new Set(from.split('/').filter((s) => s.startsWith(':')).map((s) => s.slice(1)));
|
|
84
|
+
if (from === '/*' || from.endsWith('/*'))
|
|
85
|
+
captured.add('splat');
|
|
86
|
+
for (const q of query) {
|
|
87
|
+
const v = q.slice(q.indexOf('=') + 1);
|
|
88
|
+
if (v.startsWith(':'))
|
|
89
|
+
captured.add(v.slice(1));
|
|
90
|
+
}
|
|
91
|
+
return targetNames(to).find((n) => !captured.has(n));
|
|
92
|
+
}
|
|
93
|
+
const REWRITES = [200, 404, 410, 451];
|
|
94
|
+
function redirectReason(from, to, status) {
|
|
95
|
+
if (![200, 301, 302, 303, 307, 308, ...REWRITES.slice(1)].includes(status))
|
|
96
|
+
return `status ${status} is not supported`;
|
|
97
|
+
if (from.slice(0, from.endsWith('/*') ? -2 : undefined).includes('*'))
|
|
98
|
+
return MIDDLE_STAR;
|
|
99
|
+
if (!from.startsWith('/'))
|
|
100
|
+
return `'${from}' is not a path`;
|
|
101
|
+
if (REWRITES.includes(status) && status !== 200 && /^https?:/i.test(to))
|
|
102
|
+
return `status ${status} needs a page path, not a URL`;
|
|
103
|
+
return `sitefile serve cannot express '${from} ${to} ${status}'`;
|
|
104
|
+
}
|
|
105
|
+
function headerReason(pattern, headers) {
|
|
106
|
+
if (!pattern.startsWith('/') || /[ \t\n\r\f\v]/.test(pattern))
|
|
107
|
+
return `'${pattern}' is not a path pattern`;
|
|
108
|
+
for (const [n, v] of headers) {
|
|
109
|
+
if (!/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(n))
|
|
110
|
+
return `'${n}' is not a header name`;
|
|
111
|
+
if (['content-length', 'transfer-encoding', 'connection', 'keep-alive', 'etag', 'location'].includes(n.toLowerCase()))
|
|
112
|
+
return `${n} cannot be set by _headers`;
|
|
113
|
+
if (!/^[\t\x20-\x7e]*$/.test(v))
|
|
114
|
+
return `the value of ${n} has characters _headers cannot carry`;
|
|
115
|
+
}
|
|
116
|
+
return 'sitefile serve cannot express these headers';
|
|
117
|
+
}
|
|
118
|
+
/** The keys of `o` in JavaScript's order: integer-like keys ascending first, then the rest as written (Rust sorts them the same way). */
|
|
119
|
+
function keysOf(o) {
|
|
120
|
+
return Object.keys(o);
|
|
121
|
+
}
|
|
122
|
+
// ------------------------------------------------------------------ netlify.toml
|
|
123
|
+
function netlify(doc, out) {
|
|
124
|
+
const redirects = doc['redirects'];
|
|
125
|
+
if (redirects !== undefined) {
|
|
126
|
+
if (!Array.isArray(redirects))
|
|
127
|
+
out.skip('redirects', 'expected [[redirects]] tables');
|
|
128
|
+
else
|
|
129
|
+
redirects.forEach((r, i) => {
|
|
130
|
+
const entry = `redirects[${i}]`;
|
|
131
|
+
if (!isMapping(r) || !isStr(r['from']) || !isStr(r['to']))
|
|
132
|
+
return out.skip(entry, 'expected from and to');
|
|
133
|
+
for (const k of ['headers', 'signed'])
|
|
134
|
+
if (r[k] !== undefined)
|
|
135
|
+
return out.skip(entry, 'proxy headers and signing are not supported');
|
|
136
|
+
const query = [];
|
|
137
|
+
if (r['query'] !== undefined) {
|
|
138
|
+
const q = r['query'];
|
|
139
|
+
if (!isMapping(q) || !Object.values(q).every(isStr))
|
|
140
|
+
return out.skip(entry, 'query must map names to strings');
|
|
141
|
+
for (const k of keysOf(q))
|
|
142
|
+
query.push(`${k}=${q[k]}`);
|
|
143
|
+
}
|
|
144
|
+
const conditions = [];
|
|
145
|
+
if (r['conditions'] !== undefined) {
|
|
146
|
+
const c = r['conditions'];
|
|
147
|
+
if (!isMapping(c))
|
|
148
|
+
return out.skip(entry, 'conditions must be a table');
|
|
149
|
+
for (const k of keysOf(c)) {
|
|
150
|
+
if (!['language', 'country', 'cookie'].includes(k.toLowerCase()))
|
|
151
|
+
return out.skip(entry, `conditions on ${k} are not supported`);
|
|
152
|
+
const v = c[k];
|
|
153
|
+
const values = isStr(v) ? [v] : Array.isArray(v) && v.every(isStr) ? v : undefined;
|
|
154
|
+
if (!values || values.length === 0)
|
|
155
|
+
return out.skip(entry, `conditions.${k} must be a string or a list of strings`);
|
|
156
|
+
conditions.push(`${k}=${values.join(',')}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const status = r['status'] ?? 301;
|
|
160
|
+
if (!isInt(status))
|
|
161
|
+
return out.skip(entry, 'status must be a number');
|
|
162
|
+
if (r['force'] !== undefined && typeof r['force'] !== 'boolean')
|
|
163
|
+
return out.skip(entry, 'force must be true or false');
|
|
164
|
+
out.redirect(entry, r['from'], r['to'], status, r['force'] === true, false, query, conditions);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const headers = doc['headers'];
|
|
168
|
+
if (headers !== undefined) {
|
|
169
|
+
if (!Array.isArray(headers))
|
|
170
|
+
out.skip('headers', 'expected [[headers]] tables');
|
|
171
|
+
else
|
|
172
|
+
headers.forEach((h, i) => {
|
|
173
|
+
const entry = `headers[${i}]`;
|
|
174
|
+
if (!isMapping(h) || !isStr(h['for']) || !isMapping(h['values']))
|
|
175
|
+
return out.skip(entry, 'expected for and values');
|
|
176
|
+
const values = h['values'];
|
|
177
|
+
const list = [];
|
|
178
|
+
for (const k of keysOf(values)) {
|
|
179
|
+
const v = values[k];
|
|
180
|
+
if (isStr(v))
|
|
181
|
+
list.push([k, v]);
|
|
182
|
+
else if (Array.isArray(v) && v.every(isStr))
|
|
183
|
+
list.push([k, v.join(', ')]);
|
|
184
|
+
else
|
|
185
|
+
return out.skip(entry, `the value of ${k} must be a string`);
|
|
186
|
+
}
|
|
187
|
+
out.header(entry, h['for'], list);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
for (const k of ['edge_functions', 'functions', 'plugins'])
|
|
191
|
+
if (doc[k] !== undefined)
|
|
192
|
+
out.skip(k, 'not part of serving files');
|
|
193
|
+
}
|
|
194
|
+
// ------------------------------------------------------------------ path-to-regexp style sources (Vercel)
|
|
195
|
+
/**
|
|
196
|
+
* A Vercel `source` as a `_redirects` path: literal segments, `:name` segments (with `params`), and a last
|
|
197
|
+
* segment `:name*` or `(.*)` (anything below), which becomes `/*`. `splat` lists what a destination may use
|
|
198
|
+
* for that splat. Header patterns (`params` false) take literal segments and the splat only.
|
|
199
|
+
*/
|
|
200
|
+
function vercelSource(source, params) {
|
|
201
|
+
if (!source.startsWith('/'))
|
|
202
|
+
return undefined;
|
|
203
|
+
const segs = source.slice(1).split('/');
|
|
204
|
+
const last = segs[segs.length - 1];
|
|
205
|
+
const star = /^:([A-Za-z_][A-Za-z0-9_]*)\*$/.exec(last);
|
|
206
|
+
const splat = star ? [`:${star[1]}*`, `:${star[1]}`] : last === '(.*)' ? ['$1'] : undefined;
|
|
207
|
+
const body = splat ? segs.slice(0, -1) : segs;
|
|
208
|
+
for (const [i, seg] of body.entries()) {
|
|
209
|
+
if (params && /^:[A-Za-z_][A-Za-z0-9_]*$/.test(seg))
|
|
210
|
+
continue;
|
|
211
|
+
if (/[:*()?+[\]{}\\]/.test(seg) || (seg === '' && (i < body.length - 1 || splat)))
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
return { from: splat ? `/${[...body, '*'].join('/')}` : source, splat: splat ?? [] };
|
|
215
|
+
}
|
|
216
|
+
function vercelTarget(dest, splat) {
|
|
217
|
+
let out = dest;
|
|
218
|
+
for (const p of splat)
|
|
219
|
+
out = out.split(p).join(':splat');
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
function vercelSourceReason(source, params) {
|
|
223
|
+
return /[()]/.test(source) ? 'regular expressions are not supported' : source.includes(':') ? (params ? 'only :name and a trailing :name* are supported' : PLACEHOLDERS) : MIDDLE_STAR;
|
|
224
|
+
}
|
|
225
|
+
function vercel(doc, out) {
|
|
226
|
+
const list = (key, fn) => {
|
|
227
|
+
const v = doc[key];
|
|
228
|
+
if (v === undefined)
|
|
229
|
+
return;
|
|
230
|
+
if (!Array.isArray(v))
|
|
231
|
+
return out.skip(key, 'expected a list');
|
|
232
|
+
v.forEach((r, i) => {
|
|
233
|
+
const entry = `${key}[${i}]`;
|
|
234
|
+
if (!isMapping(r))
|
|
235
|
+
return out.skip(entry, 'expected an object');
|
|
236
|
+
if (r['has'] !== undefined || r['missing'] !== undefined)
|
|
237
|
+
return out.skip(entry, CONDITIONS);
|
|
238
|
+
fn(r, entry);
|
|
239
|
+
});
|
|
240
|
+
};
|
|
241
|
+
list('redirects', (r, entry) => {
|
|
242
|
+
if (!isStr(r['source']) || !isStr(r['destination']))
|
|
243
|
+
return out.skip(entry, 'expected source and destination');
|
|
244
|
+
const src = vercelSource(r['source'], true);
|
|
245
|
+
if (!src)
|
|
246
|
+
return out.skip(entry, vercelSourceReason(r['source'], true));
|
|
247
|
+
const status = r['statusCode'] ?? (r['permanent'] === false ? 307 : 308);
|
|
248
|
+
if (!isInt(status))
|
|
249
|
+
return out.skip(entry, 'statusCode must be a number');
|
|
250
|
+
out.redirect(entry, src.from, vercelTarget(r['destination'], src.splat), status);
|
|
251
|
+
});
|
|
252
|
+
list('rewrites', (r, entry) => {
|
|
253
|
+
if (!isStr(r['source']) || !isStr(r['destination']))
|
|
254
|
+
return out.skip(entry, 'expected source and destination');
|
|
255
|
+
const src = vercelSource(r['source'], true);
|
|
256
|
+
if (!src)
|
|
257
|
+
return out.skip(entry, vercelSourceReason(r['source'], true));
|
|
258
|
+
out.redirect(entry, src.from, vercelTarget(r['destination'], src.splat), 200);
|
|
259
|
+
});
|
|
260
|
+
list('headers', (r, entry) => {
|
|
261
|
+
if (!isStr(r['source']) || !Array.isArray(r['headers']))
|
|
262
|
+
return out.skip(entry, 'expected source and headers');
|
|
263
|
+
const src = vercelSource(r['source'], false);
|
|
264
|
+
if (!src)
|
|
265
|
+
return out.skip(entry, vercelSourceReason(r['source'], false));
|
|
266
|
+
const hs = [];
|
|
267
|
+
for (const h of r['headers']) {
|
|
268
|
+
if (!isMapping(h) || !isStr(h['key']) || !isStr(h['value']))
|
|
269
|
+
return out.skip(entry, 'expected headers of key and value');
|
|
270
|
+
hs.push([h['key'], h['value']]);
|
|
271
|
+
}
|
|
272
|
+
out.header(entry, src.from, hs);
|
|
273
|
+
});
|
|
274
|
+
if (doc['cleanUrls'] !== undefined)
|
|
275
|
+
out.skip('cleanUrls', 'serve always answers /about with about.html; canonical redirects are johnhenry/sitefile#192');
|
|
276
|
+
if (doc['trailingSlash'] !== undefined)
|
|
277
|
+
out.skip('trailingSlash', 'trailing-slash modes are johnhenry/sitefile#192');
|
|
278
|
+
}
|
|
279
|
+
// ------------------------------------------------------------------ firebase.json
|
|
280
|
+
/**
|
|
281
|
+
* A Firebase source as a `_redirects` path: literal segments, `:name` segments (`params`), and a last
|
|
282
|
+
* segment `**` or `:name*` (anything below), which becomes `/*`; `**` alone is `/*`. `splat` lists what a
|
|
283
|
+
* destination may use for that splat. Header sources (`params` false) take literal segments and `**` only.
|
|
284
|
+
*/
|
|
285
|
+
function firebaseSource(glob, params) {
|
|
286
|
+
if (glob === '**')
|
|
287
|
+
return { from: '/*', splat: [] };
|
|
288
|
+
if (!glob.startsWith('/'))
|
|
289
|
+
return undefined;
|
|
290
|
+
const segs = glob.slice(1).split('/');
|
|
291
|
+
const last = segs[segs.length - 1];
|
|
292
|
+
const star = params ? /^:([A-Za-z_][A-Za-z0-9_]*)\*$/.exec(last) : null;
|
|
293
|
+
const splat = last === '**' ? [] : star ? [`:${star[1]}`] : undefined;
|
|
294
|
+
const body = splat ? segs.slice(0, -1) : segs;
|
|
295
|
+
for (const [i, seg] of body.entries()) {
|
|
296
|
+
if (params && /^:[A-Za-z_][A-Za-z0-9_]*$/.test(seg))
|
|
297
|
+
continue;
|
|
298
|
+
if (/[*?{}[\]!:]/.test(seg) || (seg === '' && (i < body.length - 1 || splat)))
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
return { from: splat ? `/${[...body, '*'].join('/')}` : glob, splat: splat ?? [] };
|
|
302
|
+
}
|
|
303
|
+
function firebaseReason(r, params) {
|
|
304
|
+
if (r['regex'] !== undefined)
|
|
305
|
+
return 'regular expressions are not supported';
|
|
306
|
+
const s = String(r['source'] ?? r['glob'] ?? '');
|
|
307
|
+
return s.includes(':') && !params ? PLACEHOLDERS : params ? 'only exact paths, :name segments and a trailing /** or :name* are supported' : 'only exact paths and a trailing /** are supported';
|
|
308
|
+
}
|
|
309
|
+
function firebase(doc, out) {
|
|
310
|
+
let hosting = doc['hosting'];
|
|
311
|
+
let at = 'hosting';
|
|
312
|
+
if (Array.isArray(hosting)) {
|
|
313
|
+
if (hosting.length !== 1)
|
|
314
|
+
return out.skip('hosting', 'several sites: import one site at a time');
|
|
315
|
+
hosting = hosting[0];
|
|
316
|
+
at = 'hosting[0]';
|
|
317
|
+
}
|
|
318
|
+
if (!isMapping(hosting)) {
|
|
319
|
+
if (hosting !== undefined)
|
|
320
|
+
out.skip('hosting', 'expected an object');
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const h = hosting;
|
|
324
|
+
const list = (key, fn) => {
|
|
325
|
+
const v = h[key];
|
|
326
|
+
if (v === undefined)
|
|
327
|
+
return;
|
|
328
|
+
if (!Array.isArray(v))
|
|
329
|
+
return out.skip(`${at}.${key}`, 'expected a list');
|
|
330
|
+
v.forEach((r, i) => {
|
|
331
|
+
const entry = `${at}.${key}[${i}]`;
|
|
332
|
+
if (!isMapping(r))
|
|
333
|
+
return out.skip(entry, 'expected an object');
|
|
334
|
+
fn(r, entry);
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
const sourceOf = (r, params) => {
|
|
338
|
+
const s = r['source'] ?? r['glob'];
|
|
339
|
+
return r['regex'] === undefined && isStr(s) ? firebaseSource(s, params) : undefined;
|
|
340
|
+
};
|
|
341
|
+
list('redirects', (r, entry) => {
|
|
342
|
+
const src = sourceOf(r, true);
|
|
343
|
+
if (src === undefined)
|
|
344
|
+
return out.skip(entry, firebaseReason(r, true));
|
|
345
|
+
if (!isStr(r['destination']))
|
|
346
|
+
return out.skip(entry, 'expected a destination');
|
|
347
|
+
const status = r['type'] ?? 301;
|
|
348
|
+
if (!isInt(status))
|
|
349
|
+
return out.skip(entry, 'type must be 301 or 302');
|
|
350
|
+
out.redirect(entry, src.from, vercelTarget(r['destination'], src.splat), status);
|
|
351
|
+
});
|
|
352
|
+
list('rewrites', (r, entry) => {
|
|
353
|
+
for (const k of ['function', 'run', 'dynamicLinks'])
|
|
354
|
+
if (r[k] !== undefined)
|
|
355
|
+
return out.skip(entry, `${k} rewrites are not supported`);
|
|
356
|
+
const src = sourceOf(r, true);
|
|
357
|
+
if (src === undefined)
|
|
358
|
+
return out.skip(entry, firebaseReason(r, true));
|
|
359
|
+
if (!isStr(r['destination']))
|
|
360
|
+
return out.skip(entry, 'expected a destination');
|
|
361
|
+
out.redirect(entry, src.from, vercelTarget(r['destination'], src.splat), 200);
|
|
362
|
+
});
|
|
363
|
+
list('headers', (r, entry) => {
|
|
364
|
+
const from = sourceOf(r, false)?.from;
|
|
365
|
+
if (from === undefined)
|
|
366
|
+
return out.skip(entry, firebaseReason(r, false));
|
|
367
|
+
if (!Array.isArray(r['headers']))
|
|
368
|
+
return out.skip(entry, 'expected headers');
|
|
369
|
+
const hs = [];
|
|
370
|
+
for (const x of r['headers']) {
|
|
371
|
+
if (!isMapping(x) || !isStr(x['key']) || !isStr(x['value']))
|
|
372
|
+
return out.skip(entry, 'expected headers of key and value');
|
|
373
|
+
hs.push([x['key'], x['value']]);
|
|
374
|
+
}
|
|
375
|
+
out.header(entry, from, hs);
|
|
376
|
+
});
|
|
377
|
+
if (h['cleanUrls'] !== undefined)
|
|
378
|
+
out.skip(`${at}.cleanUrls`, 'serve always answers /about with about.html; canonical redirects are johnhenry/sitefile#192');
|
|
379
|
+
if (h['trailingSlash'] !== undefined)
|
|
380
|
+
out.skip(`${at}.trailingSlash`, 'trailing-slash modes are johnhenry/sitefile#192');
|
|
381
|
+
if (h['i18n'] !== undefined)
|
|
382
|
+
out.skip(`${at}.i18n`, 'language and country rewrites are not supported');
|
|
383
|
+
}
|
|
384
|
+
// ------------------------------------------------------------------ staticwebapp.config.json (Azure)
|
|
385
|
+
function azureRoute(route) {
|
|
386
|
+
return /^\/[^*?{}[\]:]*(\/\*)?$/.test(route) || route === '/*' ? route : undefined;
|
|
387
|
+
}
|
|
388
|
+
function azure(doc, out) {
|
|
389
|
+
const routes = doc['routes'];
|
|
390
|
+
if (routes !== undefined) {
|
|
391
|
+
if (!Array.isArray(routes))
|
|
392
|
+
out.skip('routes', 'expected a list');
|
|
393
|
+
else
|
|
394
|
+
routes.forEach((r, i) => {
|
|
395
|
+
const entry = `routes[${i}]`;
|
|
396
|
+
if (!isMapping(r) || !isStr(r['route']))
|
|
397
|
+
return out.skip(entry, 'expected a route');
|
|
398
|
+
if (r['allowedRoles'] !== undefined || r['methods'] !== undefined)
|
|
399
|
+
return out.skip(entry, 'allowedRoles and methods are not supported');
|
|
400
|
+
const from = azureRoute(r['route']);
|
|
401
|
+
if (from === undefined)
|
|
402
|
+
return out.skip(entry, 'only exact routes and a trailing /* are supported');
|
|
403
|
+
if (r['headers'] !== undefined) {
|
|
404
|
+
const hv = r['headers'];
|
|
405
|
+
if (!isMapping(hv) || !Object.values(hv).every(isStr))
|
|
406
|
+
out.skip(entry, 'headers must map names to strings');
|
|
407
|
+
else
|
|
408
|
+
out.header(entry, from, keysOf(hv).map((k) => [k, hv[k]]));
|
|
409
|
+
}
|
|
410
|
+
if (isStr(r['redirect'])) {
|
|
411
|
+
const status = r['statusCode'] ?? 302;
|
|
412
|
+
if (!isInt(status))
|
|
413
|
+
return out.skip(entry, 'statusCode must be a number');
|
|
414
|
+
out.redirect(entry, from, r['redirect'], status);
|
|
415
|
+
}
|
|
416
|
+
else if (isStr(r['rewrite'])) {
|
|
417
|
+
if (r['statusCode'] !== undefined && r['statusCode'] !== 200)
|
|
418
|
+
return out.skip(entry, 'a rewrite with a statusCode is not supported');
|
|
419
|
+
out.redirect(entry, from, r['rewrite'], 200);
|
|
420
|
+
}
|
|
421
|
+
else if (r['statusCode'] !== undefined)
|
|
422
|
+
out.skip(entry, 'a statusCode without a redirect or rewrite is not supported');
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
const nav = doc['navigationFallback'];
|
|
426
|
+
if (nav !== undefined) {
|
|
427
|
+
if (!isMapping(nav) || !isStr(nav['rewrite']))
|
|
428
|
+
out.skip('navigationFallback', 'expected a rewrite');
|
|
429
|
+
else {
|
|
430
|
+
out.redirect('navigationFallback', '/*', nav['rewrite'], 200, false, true);
|
|
431
|
+
if (nav['exclude'] !== undefined)
|
|
432
|
+
out.skip('navigationFallback.exclude', 'not translated: the fallback answers only paths with no file, so excluded paths that are missing get it too');
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const overrides = doc['responseOverrides'];
|
|
436
|
+
if (overrides !== undefined) {
|
|
437
|
+
if (!isMapping(overrides))
|
|
438
|
+
out.skip('responseOverrides', 'expected an object');
|
|
439
|
+
else
|
|
440
|
+
for (const code of keysOf(overrides)) {
|
|
441
|
+
const entry = `responseOverrides.${code}`;
|
|
442
|
+
const o = overrides[code];
|
|
443
|
+
if (code !== '404') {
|
|
444
|
+
out.skip(entry, `only 404 can be overridden`);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (!isMapping(o) || !isStr(o['rewrite']) || o['redirect'] !== undefined) {
|
|
448
|
+
out.skip(entry, 'only a rewrite is supported');
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
out.redirect(entry, '/*', o['rewrite'], 404, false, true);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const g = doc['globalHeaders'];
|
|
455
|
+
if (g !== undefined) {
|
|
456
|
+
if (!isMapping(g) || !Object.values(g).every(isStr))
|
|
457
|
+
out.skip('globalHeaders', 'globalHeaders must map names to strings');
|
|
458
|
+
else
|
|
459
|
+
out.header('globalHeaders', '/*', keysOf(g).map((k) => [k, g[k]]));
|
|
460
|
+
}
|
|
461
|
+
if (doc['mimeTypes'] !== undefined)
|
|
462
|
+
out.skip('mimeTypes', 'custom content types are johnhenry/sitefile#220');
|
|
463
|
+
}
|
|
464
|
+
// ------------------------------------------------------------------ wrangler [assets]
|
|
465
|
+
function wrangler(doc, out) {
|
|
466
|
+
const a = doc['assets'];
|
|
467
|
+
if (a === undefined)
|
|
468
|
+
return;
|
|
469
|
+
if (!isMapping(a))
|
|
470
|
+
return out.skip('assets', 'expected a table');
|
|
471
|
+
const nf = a['not_found_handling'];
|
|
472
|
+
if (nf === 'single-page-application')
|
|
473
|
+
out.redirect('assets.not_found_handling', '/*', '/index.html', 200, false, true);
|
|
474
|
+
else if (nf !== undefined && nf !== '404-page' && nf !== 'none')
|
|
475
|
+
out.skip('assets.not_found_handling', isStr(nf) ? `unknown value '${nf}'` : 'expected a string');
|
|
476
|
+
const hh = a['html_handling'];
|
|
477
|
+
if (hh !== undefined && hh !== 'auto-trailing-slash')
|
|
478
|
+
out.skip('assets.html_handling', 'trailing-slash modes are johnhenry/sitefile#192');
|
|
479
|
+
if (a['run_worker_first'] !== undefined)
|
|
480
|
+
out.skip('assets.run_worker_first', 'Workers are not run');
|
|
481
|
+
}
|
|
482
|
+
// ------------------------------------------------------------------ .htaccess
|
|
483
|
+
/** Split a directive line into words, honouring "double quotes" (a backslash escapes the next character). */
|
|
484
|
+
function words(line) {
|
|
485
|
+
const out = [];
|
|
486
|
+
let i = 0;
|
|
487
|
+
for (;;) {
|
|
488
|
+
while (line[i] === ' ' || line[i] === '\t')
|
|
489
|
+
i++;
|
|
490
|
+
if (i >= line.length)
|
|
491
|
+
return out;
|
|
492
|
+
if (line[i] === '"') {
|
|
493
|
+
let w = '';
|
|
494
|
+
i++;
|
|
495
|
+
for (;;) {
|
|
496
|
+
if (i >= line.length)
|
|
497
|
+
return undefined;
|
|
498
|
+
if (line[i] === '\\' && i + 1 < line.length) {
|
|
499
|
+
w += line[i + 1];
|
|
500
|
+
i += 2;
|
|
501
|
+
}
|
|
502
|
+
else if (line[i] === '"') {
|
|
503
|
+
i++;
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
else
|
|
507
|
+
w += line[i++];
|
|
508
|
+
}
|
|
509
|
+
out.push(w);
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
const start = i;
|
|
513
|
+
while (i < line.length && line[i] !== ' ' && line[i] !== '\t')
|
|
514
|
+
i++;
|
|
515
|
+
out.push(line.slice(start, i));
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
const APACHE_STATUS = Object.assign(Object.create(null), { permanent: 301, temp: 302, seeother: 303 });
|
|
520
|
+
function htaccess(text, out) {
|
|
521
|
+
const containers = [];
|
|
522
|
+
text.split('\n').forEach((raw, n) => {
|
|
523
|
+
const entry = `line ${n + 1}`;
|
|
524
|
+
const t = (raw.endsWith('\r') ? raw.slice(0, -1) : raw).trim();
|
|
525
|
+
if (t === '' || t.startsWith('#'))
|
|
526
|
+
return;
|
|
527
|
+
const close = /^<\/([A-Za-z]+)[ \t]*>$/.exec(t);
|
|
528
|
+
if (close) {
|
|
529
|
+
containers.pop();
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
const open = /^<([A-Za-z]+)([ \t][^>]*)?>$/.exec(t);
|
|
533
|
+
if (open) {
|
|
534
|
+
const name = open[1];
|
|
535
|
+
containers.push(name.toLowerCase());
|
|
536
|
+
if (name.toLowerCase() !== 'ifmodule')
|
|
537
|
+
out.skip(entry, `<${name}> sections are not supported; their directives are skipped`);
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
if (containers.some((c) => c !== 'ifmodule'))
|
|
541
|
+
return;
|
|
542
|
+
const w = words(t);
|
|
543
|
+
if (!w || w.length === 0)
|
|
544
|
+
return out.skip(entry, 'unbalanced quotes');
|
|
545
|
+
const d = w[0].toLowerCase();
|
|
546
|
+
if (d === 'redirect' || d === 'redirectpermanent' || d === 'redirecttemp') {
|
|
547
|
+
let args = w.slice(1);
|
|
548
|
+
let status = d === 'redirectpermanent' ? 301 : 302;
|
|
549
|
+
const first = (args[0] ?? '').toLowerCase();
|
|
550
|
+
if (d === 'redirect' && (first in APACHE_STATUS || first === 'gone' || /^[0-9]{3}$/.test(first))) {
|
|
551
|
+
if (first === 'gone' || first === '410')
|
|
552
|
+
return out.skip(entry, 'Redirect gone names no page to serve; a 410 rule in _redirects needs one (/path /gone.html 410)');
|
|
553
|
+
status = APACHE_STATUS[first] ?? (/^3[0-9]{2}$/.test(first) ? Number(first) : 0);
|
|
554
|
+
if (status === 0)
|
|
555
|
+
return out.skip(entry, `status ${first} is not supported`);
|
|
556
|
+
args = args.slice(1);
|
|
557
|
+
}
|
|
558
|
+
if (args.length !== 2)
|
|
559
|
+
return out.skip(entry, 'expected a path and a URL');
|
|
560
|
+
const [from, to] = args;
|
|
561
|
+
// Apache matches the path and everything below it.
|
|
562
|
+
if (out.redirect(entry, from, to, status) && from !== '/')
|
|
563
|
+
out.redirect(entry, `${from.replace(/\/$/, '')}/*`, `${to.replace(/\/$/, '')}/:splat`, status);
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
if (d === 'redirectmatch') {
|
|
567
|
+
let args = w.slice(1);
|
|
568
|
+
let status = 302;
|
|
569
|
+
const first = (args[0] ?? '').toLowerCase();
|
|
570
|
+
if (first in APACHE_STATUS || first === 'gone' || /^[0-9]{3}$/.test(first)) {
|
|
571
|
+
if (first === 'gone' || first === '410')
|
|
572
|
+
return out.skip(entry, 'Redirect gone names no page to serve; a 410 rule in _redirects needs one (/path /gone.html 410)');
|
|
573
|
+
status = APACHE_STATUS[first] ?? (/^3[0-9]{2}$/.test(first) ? Number(first) : 0);
|
|
574
|
+
if (status === 0)
|
|
575
|
+
return out.skip(entry, `status ${first} is not supported`);
|
|
576
|
+
args = args.slice(1);
|
|
577
|
+
}
|
|
578
|
+
if (args.length !== 2)
|
|
579
|
+
return out.skip(entry, 'expected a pattern and a URL');
|
|
580
|
+
const [re, to] = args;
|
|
581
|
+
const exact = /^\^(\/[A-Za-z0-9._~\-/]*)\$$/.exec(re);
|
|
582
|
+
if (exact)
|
|
583
|
+
return void out.redirect(entry, exact[1], to, status);
|
|
584
|
+
const below = /^\^(\/(?:[A-Za-z0-9._~-]+\/)*)\(\.\*\)\$?$/.exec(re);
|
|
585
|
+
if (below && !/\$(?!1)/.test(to)) {
|
|
586
|
+
return void out.redirect(entry, `${below[1]}*`, to.split('$1').join(':splat'), status);
|
|
587
|
+
}
|
|
588
|
+
return out.skip(entry, 'only ^/path$ and ^/prefix/(.*)$ patterns are supported');
|
|
589
|
+
}
|
|
590
|
+
if (d === 'errordocument') {
|
|
591
|
+
if (w.length === 3 && w[1] === '404' && w[2].startsWith('/'))
|
|
592
|
+
return void out.redirect(entry, '/*', w[2], 404, false, true);
|
|
593
|
+
return out.skip(entry, w[1] === '404' ? 'only a local page is supported' : `ErrorDocument ${w[1]} is not supported`);
|
|
594
|
+
}
|
|
595
|
+
if (d === 'directoryindex') {
|
|
596
|
+
if (w.length === 2 && w[1] === 'index.html')
|
|
597
|
+
return;
|
|
598
|
+
return out.skip(entry, 'serve always uses index.html');
|
|
599
|
+
}
|
|
600
|
+
if (d === 'header') {
|
|
601
|
+
let args = w.slice(1);
|
|
602
|
+
if ((args[0] ?? '').toLowerCase() === 'always')
|
|
603
|
+
args = args.slice(1);
|
|
604
|
+
if (args.length !== 3 || args[0].toLowerCase() !== 'set')
|
|
605
|
+
return out.skip(entry, 'only Header set <name> <value> is supported');
|
|
606
|
+
return void out.header(entry, '/*', [[args[1], args[2]]]);
|
|
607
|
+
}
|
|
608
|
+
if (d === 'addtype')
|
|
609
|
+
return out.skip(entry, 'custom content types are johnhenry/sitefile#220');
|
|
610
|
+
out.skip(entry, `${w[0]} is not translated`);
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
// ------------------------------------------------------------------ entry point
|
|
614
|
+
/** Translate the text of a host config file. `file` names it in messages. */
|
|
615
|
+
export function importHostConfig(text, file, format) {
|
|
616
|
+
const out = new Out();
|
|
617
|
+
if (format === 'htaccess')
|
|
618
|
+
htaccess(text, out);
|
|
619
|
+
else {
|
|
620
|
+
let doc;
|
|
621
|
+
if (format === 'netlify' || (format === 'wrangler' && !file.toLowerCase().endsWith('.json')))
|
|
622
|
+
doc = parseToml(text, file);
|
|
623
|
+
else {
|
|
624
|
+
try {
|
|
625
|
+
doc = JSON.parse(text.replace(/^/, ''));
|
|
626
|
+
}
|
|
627
|
+
catch (e) {
|
|
628
|
+
throw new SitefileError('E_DATA', `${file} is not valid JSON: ${e.message}`, undefined, { cause: e });
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
if (!isMapping(doc))
|
|
632
|
+
throw new SitefileError('E_DATA', `${file}: expected an object at the top level`);
|
|
633
|
+
({ netlify, vercel, firebase, azure, wrangler })[format](doc, out);
|
|
634
|
+
}
|
|
635
|
+
return { format, redirects: [...out.redirects, ...out.fallbacks], headers: out.headers, unsupported: out.unsupported };
|
|
636
|
+
}
|
|
637
|
+
/** `_redirects` text (empty when there are no rules). */
|
|
638
|
+
export function redirectsText(c) {
|
|
639
|
+
return c.redirects.map((l) => `${l}\n`).join('');
|
|
640
|
+
}
|
|
641
|
+
/** `_headers` text (empty when there are no blocks). */
|
|
642
|
+
export function headersText(c) {
|
|
643
|
+
return c.headers.map((b) => `${b.pattern}\n${b.headers.map(([n, v]) => ` ${n}: ${v}\n`).join('')}`).join('');
|
|
644
|
+
}
|
|
645
|
+
//# sourceMappingURL=host-config.js.map
|