@rsc-kit/core 0.35.0 → 0.37.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/dist/filesManifest.d.ts +28 -4
- package/dist/filesManifest.js +117 -24
- package/dist/filesManifest.js.map +1 -1
- package/dist/host.js +15 -1
- package/dist/host.js.map +1 -1
- package/dist/vite.js +106 -17
- package/dist/vite.js.map +1 -1
- package/package.json +1 -1
package/dist/filesManifest.d.ts
CHANGED
|
@@ -2,7 +2,11 @@ export interface FilesManifest {
|
|
|
2
2
|
v: 1;
|
|
3
3
|
/** The directory the files are in, relative to the manifest. */
|
|
4
4
|
root: string;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Every file a request may be answered with, by url path, sorted. A path
|
|
7
|
+
* is the file's name as it is on disk, not percent-encoded: a request's
|
|
8
|
+
* path is decoded before it is looked up.
|
|
9
|
+
*/
|
|
6
10
|
files: string[];
|
|
7
11
|
/**
|
|
8
12
|
* The encodings some files also exist in, as a sibling with the suffix
|
|
@@ -11,9 +15,10 @@ export interface FilesManifest {
|
|
|
11
15
|
*/
|
|
12
16
|
precompressed: string[];
|
|
13
17
|
/**
|
|
14
|
-
* The headers a file goes out with beyond its Content-Type,
|
|
15
|
-
*
|
|
16
|
-
*
|
|
18
|
+
* The headers a file goes out with beyond its Content-Type, each applying
|
|
19
|
+
* to an exact path or, ending in `/**`, to every path under it. Every entry
|
|
20
|
+
* that matches applies, in order; a later one replaces an earlier one's
|
|
21
|
+
* header of the same name. A file matched by none gets none.
|
|
17
22
|
*/
|
|
18
23
|
headers: {
|
|
19
24
|
match: string;
|
|
@@ -22,9 +27,28 @@ export interface FilesManifest {
|
|
|
22
27
|
/** Whether anything of the app's runs for a file: never. */
|
|
23
28
|
middleware: false;
|
|
24
29
|
}
|
|
30
|
+
/** The route rules Nitro resolves for a url path, as its matcher merges them. */
|
|
31
|
+
export type ResolveRouteRules = (path: string) => Record<string, unknown>;
|
|
25
32
|
/** Whether a route rule's key matches a path: exact, or `/x/**` for everything under /x. */
|
|
26
33
|
export declare function ruleMatches(rule: string, path: string): boolean;
|
|
27
34
|
export declare function filesManifest(publicDir: string, routeRules: Record<string, unknown> | undefined, options?: {
|
|
28
35
|
root?: string;
|
|
29
36
|
exclude?: string[];
|
|
37
|
+
/** The app's base url; the files are under it. */
|
|
38
|
+
baseURL?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Nitro's own resolution of a path's rules. Without it a file is checked
|
|
41
|
+
* against the rules by their keys, and one under a pattern the manifest
|
|
42
|
+
* cannot state is left out by the pattern's literal prefix.
|
|
43
|
+
*/
|
|
44
|
+
resolve?: ResolveRouteRules | null;
|
|
45
|
+
/** Told about each file left to the app, and why. */
|
|
46
|
+
onLeftOut?: (path: string, why: string) => void;
|
|
30
47
|
}): FilesManifest;
|
|
48
|
+
/**
|
|
49
|
+
* The route rules Nitro resolves for a path, by the matcher it compiles them
|
|
50
|
+
* with - h3's, loaded from the app's own Nitro so the answer is that
|
|
51
|
+
* version's. Null when it cannot be loaded or refuses the rules: files.json
|
|
52
|
+
* then falls back to the rules' keys, leaving out more rather than less.
|
|
53
|
+
*/
|
|
54
|
+
export declare function nitroRouteRules(root: string, routeRules: Record<string, unknown> | undefined, baseURL: string): Promise<ResolveRouteRules | null>;
|
package/dist/filesManifest.js
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
1
|
// `.output/files.json`: what a server in front of the app may answer from
|
|
2
2
|
// disk, before the app runs.
|
|
3
3
|
//
|
|
4
|
-
// Everything under
|
|
5
|
-
// before the request reaches the renderer: no middleware.ts, no
|
|
6
|
-
// no rscKit({ headers }) - those are a page's. A proxy, a CDN or a
|
|
7
|
-
// controller serving these files itself only has to match what
|
|
8
|
-
// sends, and the build already knows all of it: the files it
|
|
9
|
-
// route rules Nitro serves them under (an asset directory's
|
|
10
|
-
// `/assets/**` immutable; the service worker's `max-age=0` is
|
|
11
|
-
// the build says so, rather than every front re-deriving it from
|
|
12
|
-
// Nitro and getting the service worker wrong.
|
|
4
|
+
// Everything under the public directory is answered by Nitro's static
|
|
5
|
+
// handler before the request reaches the renderer: no middleware.ts, no
|
|
6
|
+
// route.ts, no rscKit({ headers }) - those are a page's. A proxy, a CDN or a
|
|
7
|
+
// PHP front controller serving these files itself only has to match what
|
|
8
|
+
// that handler sends, and the build already knows all of it: the files it
|
|
9
|
+
// wrote, and the route rules Nitro serves them under (an asset directory's
|
|
10
|
+
// maxAge is one, `/assets/**` immutable; the service worker's `max-age=0` is
|
|
11
|
+
// another). So the build says so, rather than every front re-deriving it from
|
|
12
|
+
// a reading of Nitro and getting the service worker wrong.
|
|
13
|
+
//
|
|
14
|
+
// It fails closed. Nitro's route rules run before its static handler, so a
|
|
15
|
+
// rule decides what a file goes out with - and a rule the manifest cannot
|
|
16
|
+
// state (`/:name.js`, a redirect, cors, a proxy, a `headers: false` reset)
|
|
17
|
+
// would make a front's answer differ from the app's with nobody seeing it.
|
|
18
|
+
// A file under such a rule is left out of `files`: the front sends it to the
|
|
19
|
+
// app, which answers it the way it always would. Each file is checked against
|
|
20
|
+
// Nitro's own matcher (h3's, which Nitro compiles its rules with), not a
|
|
21
|
+
// reading of it: what the front would send has to equal what Nitro resolves.
|
|
13
22
|
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
23
|
+
import { createRequire } from "node:module";
|
|
14
24
|
import { join } from "node:path";
|
|
25
|
+
import { pathToFileURL } from "node:url";
|
|
15
26
|
/** The encodings Nitro precompresses to, by the suffix of the sibling it writes. */
|
|
16
27
|
const ENCODINGS = { ".br": "br", ".gz": "gzip", ".zst": "zstd" };
|
|
28
|
+
/**
|
|
29
|
+
* Rules that never touch a file Nitro's static handler answers: `prerender`
|
|
30
|
+
* is build-time, and the cache rule wraps route handlers, which run after it.
|
|
31
|
+
* Any other rule - redirect, proxy, cors, one an app or module added - leaves
|
|
32
|
+
* the file to the app.
|
|
33
|
+
*/
|
|
34
|
+
const INERT = new Set(["headers", "prerender", "cache"]);
|
|
17
35
|
function walk(dir, base, skip, out) {
|
|
18
36
|
for (const name of readdirSync(dir).sort()) {
|
|
19
37
|
const full = join(dir, name);
|
|
@@ -32,7 +50,30 @@ export function ruleMatches(rule, path) {
|
|
|
32
50
|
return path.startsWith(rule.slice(0, -2));
|
|
33
51
|
return rule === path;
|
|
34
52
|
}
|
|
53
|
+
/** Whether a front can follow a rule's key without Nitro's router. */
|
|
54
|
+
function expressible(rule) {
|
|
55
|
+
return !/[*:?(){}\\]/.test(rule.endsWith("/**") ? rule.slice(0, -3) : rule);
|
|
56
|
+
}
|
|
57
|
+
/** The literal part of a pattern, before its first dynamic segment. */
|
|
58
|
+
function literalPrefix(rule) {
|
|
59
|
+
const segments = rule.split("/");
|
|
60
|
+
const dynamic = segments.findIndex((segment) => /[*:?(){}\\]/.test(segment));
|
|
61
|
+
return segments.slice(0, dynamic).join("/") + "/";
|
|
62
|
+
}
|
|
63
|
+
function lower(headers) {
|
|
64
|
+
return Object.fromEntries(Object.entries(headers).map(([name, value]) => [name.toLowerCase(), String(value)]));
|
|
65
|
+
}
|
|
66
|
+
function same(a, b) {
|
|
67
|
+
const keys = Object.keys(a);
|
|
68
|
+
return keys.length === Object.keys(b).length && keys.every((key) => b[key] === a[key]);
|
|
69
|
+
}
|
|
70
|
+
function headersOf(rule) {
|
|
71
|
+
const set = rule?.headers;
|
|
72
|
+
return set && typeof set === "object" && Object.keys(set).length > 0 ? set : null;
|
|
73
|
+
}
|
|
35
74
|
export function filesManifest(publicDir, routeRules, options = {}) {
|
|
75
|
+
const base = (options.baseURL ?? "/").replace(/\/+$/, "");
|
|
76
|
+
const rules = Object.entries(routeRules ?? {});
|
|
36
77
|
const all = [];
|
|
37
78
|
const exclude = options.exclude ?? [];
|
|
38
79
|
if (existsSync(publicDir)) {
|
|
@@ -40,33 +81,85 @@ export function filesManifest(publicDir, routeRules, options = {}) {
|
|
|
40
81
|
}
|
|
41
82
|
const present = new Set(all);
|
|
42
83
|
const encodings = new Set();
|
|
43
|
-
const
|
|
84
|
+
const listed = all
|
|
85
|
+
.filter((path) => {
|
|
44
86
|
const suffix = Object.keys(ENCODINGS).find((ext) => path.endsWith(ext));
|
|
45
87
|
if (!suffix || !present.has(path.slice(0, -suffix.length)))
|
|
46
88
|
return true;
|
|
47
89
|
encodings.add(ENCODINGS[suffix]);
|
|
48
90
|
return false;
|
|
91
|
+
})
|
|
92
|
+
.map((path) => base + path);
|
|
93
|
+
// What a front can state, broadest first: a prefix rule before a longer
|
|
94
|
+
// one, an exact rule last - the order Nitro merges them in, so applying
|
|
95
|
+
// them in turn gives what its matcher resolves.
|
|
96
|
+
const stated = rules
|
|
97
|
+
.filter(([key, rule]) => expressible(key) && headersOf(rule))
|
|
98
|
+
.map(([key, rule]) => ({ match: base + key, headers: { ...headersOf(rule) } }))
|
|
99
|
+
.sort((a, b) => {
|
|
100
|
+
const rank = (match) => (match.endsWith("/**") ? match.length : Number.MAX_SAFE_INTEGER);
|
|
101
|
+
return rank(a.match) - rank(b.match);
|
|
102
|
+
});
|
|
103
|
+
const frontSends = (path) => stated.filter((entry) => ruleMatches(entry.match, path)).reduce((out, entry) => ({ ...out, ...lower(entry.headers) }), {});
|
|
104
|
+
const leftOut = (path) => {
|
|
105
|
+
if (options.resolve) {
|
|
106
|
+
const resolved = options.resolve(path);
|
|
107
|
+
const active = Object.keys(resolved).find((name) => !INERT.has(name) && resolved[name] !== false && resolved[name] != null);
|
|
108
|
+
if (active)
|
|
109
|
+
return `a ${active} route rule applies to it`;
|
|
110
|
+
const nitroSends = lower((resolved.headers && typeof resolved.headers === "object" ? resolved.headers : {}));
|
|
111
|
+
return same(nitroSends, frontSends(path)) ? null : "its route rules' headers cannot be stated as exact or /** matches";
|
|
112
|
+
}
|
|
113
|
+
for (const [key, rule] of rules) {
|
|
114
|
+
const match = base + key;
|
|
115
|
+
if (!expressible(key)) {
|
|
116
|
+
if (path.startsWith(base + literalPrefix(key)))
|
|
117
|
+
return `route rule ${key} cannot be stated as an exact or /** match`;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (!ruleMatches(match, path))
|
|
121
|
+
continue;
|
|
122
|
+
const active = Object.keys(rule ?? {}).find((name) => !INERT.has(name) && rule[name] !== false && rule[name] != null);
|
|
123
|
+
if (active)
|
|
124
|
+
return `a ${active} route rule applies to it`;
|
|
125
|
+
if (rule?.headers === false)
|
|
126
|
+
return `route rule ${key} resets its headers`;
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
};
|
|
130
|
+
const files = listed.filter((path) => {
|
|
131
|
+
const why = leftOut(path);
|
|
132
|
+
if (why)
|
|
133
|
+
options.onLeftOut?.(path, why);
|
|
134
|
+
return !why;
|
|
49
135
|
});
|
|
50
|
-
const headers = [];
|
|
51
|
-
for (const [match, rule] of Object.entries(routeRules ?? {})) {
|
|
52
|
-
const set = rule?.headers;
|
|
53
|
-
// Only the forms a front can follow without Nitro's router - and only a
|
|
54
|
-
// rule some file is under: a rule for pages is the renderer's business.
|
|
55
|
-
if (!set || Object.keys(set).length === 0)
|
|
56
|
-
continue;
|
|
57
|
-
if (!match.endsWith("/**") ? /[*:]/.test(match) : /[*:]/.test(match.slice(0, -3)))
|
|
58
|
-
continue;
|
|
59
|
-
if (!files.some((path) => ruleMatches(match, path)))
|
|
60
|
-
continue;
|
|
61
|
-
headers.push({ match, headers: { ...set } });
|
|
62
|
-
}
|
|
63
136
|
return {
|
|
64
137
|
v: 1,
|
|
65
138
|
root: options.root ?? "public",
|
|
66
139
|
files,
|
|
67
140
|
precompressed: [...encodings].sort(),
|
|
68
|
-
|
|
141
|
+
// Only a rule some served file is under: a rule for pages is the renderer's business.
|
|
142
|
+
headers: stated.filter((entry) => files.some((path) => ruleMatches(entry.match, path))),
|
|
69
143
|
middleware: false,
|
|
70
144
|
};
|
|
71
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* The route rules Nitro resolves for a path, by the matcher it compiles them
|
|
148
|
+
* with - h3's, loaded from the app's own Nitro so the answer is that
|
|
149
|
+
* version's. Null when it cannot be loaded or refuses the rules: files.json
|
|
150
|
+
* then falls back to the rules' keys, leaving out more rather than less.
|
|
151
|
+
*/
|
|
152
|
+
export async function nitroRouteRules(root, routeRules, baseURL) {
|
|
153
|
+
try {
|
|
154
|
+
const nitro = createRequire(join(root, "package.json")).resolve("nitro");
|
|
155
|
+
const rules = (await import(pathToFileURL(createRequire(nitro).resolve("h3/rules")).href));
|
|
156
|
+
if (!rules.normalizeRouteRules || !rules.createRouteRulesMatcher)
|
|
157
|
+
return null;
|
|
158
|
+
const match = rules.createRouteRulesMatcher(rules.normalizeRouteRules(routeRules ?? {}), { baseURL: baseURL || undefined });
|
|
159
|
+
return (path) => ({ ...(match("GET", path).routeRules ?? {}) });
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
72
165
|
//# sourceMappingURL=filesManifest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filesManifest.js","sourceRoot":"","sources":["../src/filesManifest.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6BAA6B;AAC7B,EAAE;AACF,0EAA0E;AAC1E,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,4EAA4E;AAC5E,2EAA2E;AAC3E,2EAA2E;AAC3E,8EAA8E;AAC9E,8CAA8C;AAE9C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,oFAAoF;AACpF,MAAM,SAAS,GAA2B,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAwBzF,SAAS,IAAI,CAAC,GAAW,EAAE,IAAY,EAAE,IAA+B,EAAE,GAAa;IACrF,KAAK,MAAM,IAAI,IAAI,WAAW,CAAC,GAAG,C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AC,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,GAAG,GAAG,EAAE,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO;QACL,CAAC,EAAE,CAAC;QACJ,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,QAAQ;QAC9B,KAAK;QACL,aAAa,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,IAAI,EAAE;QACpC,OAAO;QACP,UAAU,EAAE,KAAK;KAClB,CAAC;AACJ,CAAC","sourcesContent":["// `.output/files.json`: what a server in front of the app may answer from\n// disk, before the app runs.\n//\n// Everything under `.output/public` is answered by Nitro's static handler\n// before the request reaches the renderer: no middleware.ts, no route.ts,\n// no rscKit({ headers }) - those are a page's. A proxy, a CDN or a PHP front\n// controller serving these files itself only has to match what that handler\n// sends, and the build already knows all of it: the files it wrote, and the\n// route rules Nitro serves them under (an asset directory's maxAge is one,\n// `/assets/**` immutable; the service worker's `max-age=0` is another). So\n// the build says so, rather than every front re-deriving it from a reading of\n// Nitro and getting the service worker wrong.\n\nimport { existsSync, readdirSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\n/** The encodings Nitro precompresses to, by the suffix of the sibling it writes. */\nconst ENCODINGS: Record<string, string> = { \".br\": \"br\", \".gz\": \"gzip\", \".zst\": \"zstd\" };\n\nexport interface FilesManifest {\n v: 1;\n /** The directory the files are in, relative to the manifest. */\n root: string;\n /** Every file a request may be answered with, by url path, sorted. */\n files: string[];\n /**\n * The encodings some files also exist in, as a sibling with the suffix\n * (`/sw.js.br` is `/sw.js` in br). Served with `Content-Encoding` and\n * `Vary: Accept-Encoding` when the request accepts it.\n */\n precompressed: string[];\n /**\n * The headers a file goes out with beyond its Content-Type, in order, each\n * applying to an exact path or, ending in `/**`, to every path under it.\n * A file matched by none gets none.\n */\n headers: { match: string; headers: Record<string, string> }[];\n /** Whether anything of the app's runs for a file: never. */\n middleware: false;\n}\n\nfunction walk(dir: string, base: string, skip: (path: string) => boolean, out: string[]): void {\n for (const name of readdirSync(dir).sort()) {\n const full = join(dir, name);\n const path = `${base}/${name}`;\n\n if (skip(path)) continue;\n if (statSync(full).isDirectory()) walk(full, path, skip, out);\n else out.push(path);\n }\n}\n\n/** Whether a route rule's key matches a path: exact, or `/x/**` for everything under /x. */\nexport function ruleMatches(rule: string, path: string): boolean {\n if (rule.endsWith(\"/**\")) return path.startsWith(rule.slice(0, -2));\n\n return rule === path;\n}\n\nexport function filesManifest(\n publicDir: string,\n routeRules: Record<string, unknown> | undefined,\n options: { root?: string; exclude?: string[] } = {},\n): FilesManifest {\n const all: string[] = [];\n const exclude = options.exclude ?? [];\n\n if (existsSync(publicDir)) {\n walk(publicDir, \"\", (path) => exclude.some((prefix) => path === prefix || path.startsWith(prefix + \"/\")), all);\n }\n\n const present = new Set(all);\n const encodings = new Set<string>();\n const files = all.filter((path) => {\n const suffix = Object.keys(ENCODINGS).find((ext) => path.endsWith(ext));\n\n if (!suffix || !present.has(path.slice(0, -suffix.length))) return true;\n\n encodings.add(ENCODINGS[suffix]!);\n\n return false;\n });\n\n const headers: FilesManifest[\"headers\"] = [];\n\n for (const [match, rule] of Object.entries(routeRules ?? {})) {\n const set = (rule as { headers?: Record<string, string> } | null)?.headers;\n\n // Only the forms a front can follow without Nitro's router - and only a\n // rule some file is under: a rule for pages is the renderer's business.\n if (!set || Object.keys(set).length === 0) continue;\n if (!match.endsWith(\"/**\") ? /[*:]/.test(match) : /[*:]/.test(match.slice(0, -3))) continue;\n if (!files.some((path) => ruleMatches(match, path))) continue;\n\n headers.push({ match, headers: { ...set } });\n }\n\n return {\n v: 1,\n root: options.root ?? \"public\",\n files,\n precompressed: [...encodings].sort(),\n headers,\n middleware: false,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"filesManifest.js","sourceRoot":"","sources":["../src/filesManifest.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6BAA6B;AAC7B,EAAE;AACF,sEAAsE;AACtE,wEAAwE;AACxE,6EAA6E;AAC7E,yE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AC,GAAG,SAAS,CAAC,CAAC,IAAI,EAAE;QACpC,sFAAsF;QACtF,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;QACvF,UAAU,EAAE,KAAK;KAClB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IAAY,EACZ,UAA+C,EAC/C,OAAe;IAEf,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACzE,MAAM,KAAK,GAAG,CAAC,MAAM,MAAM,CAAC,aAAa,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,CAGxF,CAAC;QAEF,IAAI,CAAC,KAAK,CAAC,mBAAmB,IAAI,CAAC,KAAK,CAAC,uBAAuB;YAAE,OAAO,IAAI,CAAC;QAE9E,MAAM,KAAK,GAAG,KAAK,CAAC,uBAAuB,CAAC,KAAK,CAAC,mBAAmB,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,CAAC,CAAC;QAE5H,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;IAClE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["// `.output/files.json`: what a server in front of the app may answer from\n// disk, before the app runs.\n//\n// Everything under the public directory is answered by Nitro's static\n// handler before the request reaches the renderer: no middleware.ts, no\n// route.ts, no rscKit({ headers }) - those are a page's. A proxy, a CDN or a\n// PHP front controller serving these files itself only has to match what\n// that handler sends, and the build already knows all of it: the files it\n// wrote, and the route rules Nitro serves them under (an asset directory's\n// maxAge is one, `/assets/**` immutable; the service worker's `max-age=0` is\n// another). So the build says so, rather than every front re-deriving it from\n// a reading of Nitro and getting the service worker wrong.\n//\n// It fails closed. Nitro's route rules run before its static handler, so a\n// rule decides what a file goes out with - and a rule the manifest cannot\n// state (`/:name.js`, a redirect, cors, a proxy, a `headers: false` reset)\n// would make a front's answer differ from the app's with nobody seeing it.\n// A file under such a rule is left out of `files`: the front sends it to the\n// app, which answers it the way it always would. Each file is checked against\n// Nitro's own matcher (h3's, which Nitro compiles its rules with), not a\n// reading of it: what the front would send has to equal what Nitro resolves.\n\nimport { existsSync, readdirSync, statSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { join } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\n\n/** The encodings Nitro precompresses to, by the suffix of the sibling it writes. */\nconst ENCODINGS: Record<string, string> = { \".br\": \"br\", \".gz\": \"gzip\", \".zst\": \"zstd\" };\n\n/**\n * Rules that never touch a file Nitro's static handler answers: `prerender`\n * is build-time, and the cache rule wraps route handlers, which run after it.\n * Any other rule - redirect, proxy, cors, one an app or module added - leaves\n * the file to the app.\n */\nconst INERT = new Set([\"headers\", \"prerender\", \"cache\"]);\n\nexport interface FilesManifest {\n v: 1;\n /** The directory the files are in, relative to the manifest. */\n root: string;\n /**\n * Every file a request may be answered with, by url path, sorted. A path\n * is the file's name as it is on disk, not percent-encoded: a request's\n * path is decoded before it is looked up.\n */\n files: string[];\n /**\n * The encodings some files also exist in, as a sibling with the suffix\n * (`/sw.js.br` is `/sw.js` in br). Served with `Content-Encoding` and\n * `Vary: Accept-Encoding` when the request accepts it.\n */\n precompressed: string[];\n /**\n * The headers a file goes out with beyond its Content-Type, each applying\n * to an exact path or, ending in `/**`, to every path under it. Every entry\n * that matches applies, in order; a later one replaces an earlier one's\n * header of the same name. A file matched by none gets none.\n */\n headers: { match: string; headers: Record<string, string> }[];\n /** Whether anything of the app's runs for a file: never. */\n middleware: false;\n}\n\n/** The route rules Nitro resolves for a url path, as its matcher merges them. */\nexport type ResolveRouteRules = (path: string) => Record<string, unknown>;\n\nfunction walk(dir: string, base: string, skip: (path: string) => boolean, out: string[]): void {\n for (const name of readdirSync(dir).sort()) {\n const full = join(dir, name);\n const path = `${base}/${name}`;\n\n if (skip(path)) continue;\n if (statSync(full).isDirectory()) walk(full, path, skip, out);\n else out.push(path);\n }\n}\n\n/** Whether a route rule's key matches a path: exact, or `/x/**` for everything under /x. */\nexport function ruleMatches(rule: string, path: string): boolean {\n if (rule.endsWith(\"/**\")) return path.startsWith(rule.slice(0, -2));\n\n return rule === path;\n}\n\n/** Whether a front can follow a rule's key without Nitro's router. */\nfunction expressible(rule: string): boolean {\n return !/[*:?(){}\\\\]/.test(rule.endsWith(\"/**\") ? rule.slice(0, -3) : rule);\n}\n\n/** The literal part of a pattern, before its first dynamic segment. */\nfunction literalPrefix(rule: string): string {\n const segments = rule.split(\"/\");\n const dynamic = segments.findIndex((segment) => /[*:?(){}\\\\]/.test(segment));\n\n return segments.slice(0, dynamic).join(\"/\") + \"/\";\n}\n\nfunction lower(headers: Record<string, string>): Record<string, string> {\n return Object.fromEntries(Object.entries(headers).map(([name, value]) => [name.toLowerCase(), String(value)]));\n}\n\nfunction same(a: Record<string, string>, b: Record<string, string>): boolean {\n const keys = Object.keys(a);\n\n return keys.length === Object.keys(b).length && keys.every((key) => b[key] === a[key]);\n}\n\nfunction headersOf(rule: unknown): Record<string, string> | null {\n const set = (rule as { headers?: unknown } | null)?.headers;\n\n return set && typeof set === \"object\" && Object.keys(set).length > 0 ? (set as Record<string, string>) : null;\n}\n\nexport function filesManifest(\n publicDir: string,\n routeRules: Record<string, unknown> | undefined,\n options: {\n root?: string;\n exclude?: string[];\n /** The app's base url; the files are under it. */\n baseURL?: string;\n /**\n * Nitro's own resolution of a path's rules. Without it a file is checked\n * against the rules by their keys, and one under a pattern the manifest\n * cannot state is left out by the pattern's literal prefix.\n */\n resolve?: ResolveRouteRules | null;\n /** Told about each file left to the app, and why. */\n onLeftOut?: (path: string, why: string) => void;\n } = {},\n): FilesManifest {\n const base = (options.baseURL ?? \"/\").replace(/\\/+$/, \"\");\n const rules = Object.entries(routeRules ?? {});\n const all: string[] = [];\n const exclude = options.exclude ?? [];\n\n if (existsSync(publicDir)) {\n walk(publicDir, \"\", (path) => exclude.some((prefix) => path === prefix || path.startsWith(prefix + \"/\")), all);\n }\n\n const present = new Set(all);\n const encodings = new Set<string>();\n const listed = all\n .filter((path) => {\n const suffix = Object.keys(ENCODINGS).find((ext) => path.endsWith(ext));\n\n if (!suffix || !present.has(path.slice(0, -suffix.length))) return true;\n\n encodings.add(ENCODINGS[suffix]!);\n\n return false;\n })\n .map((path) => base + path);\n\n // What a front can state, broadest first: a prefix rule before a longer\n // one, an exact rule last - the order Nitro merges them in, so applying\n // them in turn gives what its matcher resolves.\n const stated = rules\n .filter(([key, rule]) => expressible(key) && headersOf(rule))\n .map(([key, rule]) => ({ match: base + key, headers: { ...headersOf(rule)! } }))\n .sort((a, b) => {\n const rank = (match: string) => (match.endsWith(\"/**\") ? match.length : Number.MAX_SAFE_INTEGER);\n\n return rank(a.match) - rank(b.match);\n });\n\n const frontSends = (path: string) =>\n stated.filter((entry) => ruleMatches(entry.match, path)).reduce<Record<string, string>>((out, entry) => ({ ...out, ...lower(entry.headers) }), {});\n\n const leftOut = (path: string): string | null => {\n if (options.resolve) {\n const resolved = options.resolve(path);\n const active = Object.keys(resolved).find((name) => !INERT.has(name) && resolved[name] !== false && resolved[name] != null);\n\n if (active) return `a ${active} route rule applies to it`;\n\n const nitroSends = lower((resolved.headers && typeof resolved.headers === \"object\" ? resolved.headers : {}) as Record<string, string>);\n\n return same(nitroSends, frontSends(path)) ? null : \"its route rules' headers cannot be stated as exact or /** matches\";\n }\n\n for (const [key, rule] of rules) {\n const match = base + key;\n\n if (!expressible(key)) {\n if (path.startsWith(base + literalPrefix(key))) return `route rule ${key} cannot be stated as an exact or /** match`;\n continue;\n }\n if (!ruleMatches(match, path)) continue;\n\n const active = Object.keys((rule as object | null) ?? {}).find(\n (name) => !INERT.has(name) && (rule as Record<string, unknown>)[name] !== false && (rule as Record<string, unknown>)[name] != null,\n );\n\n if (active) return `a ${active} route rule applies to it`;\n if ((rule as { headers?: unknown } | null)?.headers === false) return `route rule ${key} resets its headers`;\n }\n\n return null;\n };\n\n const files = listed.filter((path) => {\n const why = leftOut(path);\n\n if (why) options.onLeftOut?.(path, why);\n\n return !why;\n });\n\n return {\n v: 1,\n root: options.root ?? \"public\",\n files,\n precompressed: [...encodings].sort(),\n // Only a rule some served file is under: a rule for pages is the renderer's business.\n headers: stated.filter((entry) => files.some((path) => ruleMatches(entry.match, path))),\n middleware: false,\n };\n}\n\n/**\n * The route rules Nitro resolves for a path, by the matcher it compiles them\n * with - h3's, loaded from the app's own Nitro so the answer is that\n * version's. Null when it cannot be loaded or refuses the rules: files.json\n * then falls back to the rules' keys, leaving out more rather than less.\n */\nexport async function nitroRouteRules(\n root: string,\n routeRules: Record<string, unknown> | undefined,\n baseURL: string,\n): Promise<ResolveRouteRules | null> {\n try {\n const nitro = createRequire(join(root, \"package.json\")).resolve(\"nitro\");\n const rules = (await import(pathToFileURL(createRequire(nitro).resolve(\"h3/rules\")).href)) as {\n normalizeRouteRules?: (config: Record<string, unknown>) => Record<string, unknown>;\n createRouteRulesMatcher?: (rules: Record<string, unknown>, options: { baseURL?: string }) => (method: string, path: string) => { routeRules?: Record<string, unknown> };\n };\n\n if (!rules.normalizeRouteRules || !rules.createRouteRulesMatcher) return null;\n\n const match = rules.createRouteRulesMatcher(rules.normalizeRouteRules(routeRules ?? {}), { baseURL: baseURL || undefined });\n\n return (path) => ({ ...(match(\"GET\", path).routeRules ?? {}) });\n } catch {\n return null;\n }\n}\n"]}
|
package/dist/host.js
CHANGED
|
@@ -1607,7 +1607,21 @@ export function createRscHandler(options) {
|
|
|
1607
1607
|
nonce: edgeNonce ? EDGE.noncePlaceholder : null,
|
|
1608
1608
|
}),
|
|
1609
1609
|
}
|
|
1610
|
-
: pageHeaders;
|
|
1610
|
+
: { ...pageHeaders };
|
|
1611
|
+
// A static page's Cache-Control, as its document goes out from here:
|
|
1612
|
+
// the host's, which wins over the app's on the document too. Without it the
|
|
1613
|
+
// edge's copy went out with none, and a browser or a cache behind the
|
|
1614
|
+
// edge kept it by heuristic. Always REVALIDATE: a guarded page, the one
|
|
1615
|
+
// whose document is PER_CLIENT, never goes whole (pageAt answers 404). A
|
|
1616
|
+
// shell's document is the resumed one, rendered per request, so a shell
|
|
1617
|
+
// names none.
|
|
1618
|
+
if (complete) {
|
|
1619
|
+
for (const name of Object.keys(headers)) {
|
|
1620
|
+
if (name.toLowerCase() === "cache-control")
|
|
1621
|
+
delete headers[name];
|
|
1622
|
+
}
|
|
1623
|
+
headers["Cache-Control"] = REVALIDATE;
|
|
1624
|
+
}
|
|
1611
1625
|
return new Response(request.method === "HEAD"
|
|
1612
1626
|
? null
|
|
1613
1627
|
: JSON.stringify({
|