@cloudhal/astro-renderer 0.6.0 → 0.6.1
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/src/csp-middleware.d.ts +5 -0
- package/dist/src/csp-middleware.js +33 -0
- package/dist/src/csp.d.ts +6 -0
- package/dist/src/csp.js +15 -0
- package/dist/src/integration.js +17 -9
- package/package.json +10 -6
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `cloudhal dev` sends the deployed site's policy as `Content-Security-Policy-Report-Only` on
|
|
3
|
+
* HTML pages that set none of their own: the browser (and `render_page`) reports what the Site
|
|
4
|
+
* Worker's `SITE_CSP` would block, an inline theme script say, without breaking Vite's dev client.
|
|
5
|
+
*/
|
|
6
|
+
import { SITE_CSP } from "./csp.js";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export async function onRequest(
|
|
13
|
+
_context ,
|
|
14
|
+
next ,
|
|
15
|
+
) {
|
|
16
|
+
const response = await next();
|
|
17
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/html")) return response;
|
|
18
|
+
if (response.headers.has("content-security-policy")) return response;
|
|
19
|
+
if (response.headers.has("content-security-policy-report-only")) return response;
|
|
20
|
+
try {
|
|
21
|
+
response.headers.set("content-security-policy-report-only", SITE_CSP);
|
|
22
|
+
return response;
|
|
23
|
+
} catch {
|
|
24
|
+
// Immutable headers (a fetched response): copy them.
|
|
25
|
+
const headers = new Headers(response.headers);
|
|
26
|
+
headers.set("content-security-policy-report-only", SITE_CSP);
|
|
27
|
+
return new Response(response.body, {
|
|
28
|
+
status: response.status,
|
|
29
|
+
statusText: response.statusText,
|
|
30
|
+
headers,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/src/csp.d.ts
CHANGED
|
@@ -18,3 +18,9 @@ export declare const TURNSTILE_ORIGIN = "https://challenges.cloudflare.com";
|
|
|
18
18
|
* keeps `SITE_CSP` (the entry keeps a policy a route set).
|
|
19
19
|
*/
|
|
20
20
|
export declare function turnstileCsp(): string;
|
|
21
|
+
/**
|
|
22
|
+
* Vite's `build.assetsInlineLimit` for a site build: never inline a script chunk. Astro inlines a
|
|
23
|
+
* component's `<script>` under the limit (4 KB by default) into the page, where `SITE_CSP`'s
|
|
24
|
+
* `script-src 'self'` blocks it on the deployed site. Other assets keep the project's own limit.
|
|
25
|
+
*/
|
|
26
|
+
export declare function scriptsNotInlined<C extends Uint8Array>(limit: number | ((file: string, content: C) => boolean | undefined) | undefined): (file: string, content: C) => boolean | undefined;
|
package/dist/src/csp.js
CHANGED
|
@@ -26,3 +26,18 @@ export function turnstileCsp() {
|
|
|
26
26
|
`frame-src ${TURNSTILE_ORIGIN}; object-src 'none'`,
|
|
27
27
|
);
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Vite's `build.assetsInlineLimit` for a site build: never inline a script chunk. Astro inlines a
|
|
32
|
+
* component's `<script>` under the limit (4 KB by default) into the page, where `SITE_CSP`'s
|
|
33
|
+
* `script-src 'self'` blocks it on the deployed site. Other assets keep the project's own limit.
|
|
34
|
+
*/
|
|
35
|
+
export function scriptsNotInlined (
|
|
36
|
+
limit ,
|
|
37
|
+
) {
|
|
38
|
+
return (file, content) => {
|
|
39
|
+
if (/\.[cm]?js$/.test(file)) return false;
|
|
40
|
+
if (typeof limit === "function") return limit(file, content);
|
|
41
|
+
return limit === undefined ? undefined : content.byteLength < limit;
|
|
42
|
+
};
|
|
43
|
+
}
|
package/dist/src/integration.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
import { scriptsNotInlined } from "./csp.js";
|
|
4
5
|
|
|
5
6
|
/** Options for the `cloudhal()` integration; made available to pages via `virtual:cloudhal/config`. */
|
|
6
7
|
|
|
@@ -81,6 +82,17 @@ function configPlugin(modules
|
|
|
81
82
|
};
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
/**
|
|
86
|
+
* What only `cloudhal dev` runs as middleware (the Site Worker's entry and builds do these jobs):
|
|
87
|
+
* the analytics beacon (RFC-0006 §5.1); the click-to-load video player (RFC-0015 §4) and the
|
|
88
|
+
* interaction runtime (RFC-0020 §4), which builds write as files; and the Site Worker's CSP sent
|
|
89
|
+
* report-only, so the console shows what a deploy would block.
|
|
90
|
+
*/
|
|
91
|
+
const DEV_MIDDLEWARE = ["analytics-middleware", "embed-middleware", "csp-middleware"].map((m) => ({
|
|
92
|
+
entrypoint: `@cloudhal/astro-renderer/${m}`,
|
|
93
|
+
order: "pre" ,
|
|
94
|
+
}));
|
|
95
|
+
|
|
84
96
|
export function cloudhal(config ) {
|
|
85
97
|
let resolved = config;
|
|
86
98
|
let client ;
|
|
@@ -115,15 +127,7 @@ export function cloudhal(config ) {
|
|
|
115
127
|
client = await buildAnalyticsClient();
|
|
116
128
|
resolved = { ...resolved, analyticsScript: client.script.path };
|
|
117
129
|
}
|
|
118
|
-
if (!cloudflare && command === "dev")
|
|
119
|
-
addMiddleware({
|
|
120
|
-
entrypoint: "@cloudhal/astro-renderer/analytics-middleware",
|
|
121
|
-
order: "pre",
|
|
122
|
-
});
|
|
123
|
-
// The click-to-load video player (RFC-0015 §4) and the interaction runtime (RFC-0020 §4):
|
|
124
|
-
// served here in dev, written by builds.
|
|
125
|
-
if (!cloudflare && command === "dev")
|
|
126
|
-
addMiddleware({ entrypoint: "@cloudhal/astro-renderer/embed-middleware", order: "pre" });
|
|
130
|
+
if (!cloudflare && command === "dev") for (const m of DEV_MIDDLEWARE) addMiddleware(m);
|
|
127
131
|
// Reader comments (RFC-0007 §8.2, §15.2), on the same targets: the Site Worker build ships
|
|
128
132
|
// the module and its proof-of-work worker as hashed static assets and its entry answers the
|
|
129
133
|
// endpoints; `cloudhal dev` serves both and answers them from a middleware on SQLite.
|
|
@@ -154,6 +158,10 @@ export function cloudhal(config ) {
|
|
|
154
158
|
comments: cloudflare ? null : (comments ?? null),
|
|
155
159
|
}),
|
|
156
160
|
],
|
|
161
|
+
// `SITE_CSP` allows no inline script, so theme `<script>`s ship as files (any size).
|
|
162
|
+
build: {
|
|
163
|
+
assetsInlineLimit: scriptsNotInlined(astroConfig.vite?.build?.assetsInlineLimit),
|
|
164
|
+
},
|
|
157
165
|
// Native module must stay external in Node SSR; workerd never loads it.
|
|
158
166
|
...(cloudflare ? {} : { ssr: { external: ["better-sqlite3"] } }),
|
|
159
167
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudhal/astro-renderer",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Render a Cloudhal Site Graph with Astro: data sources (live/snapshot), section components and the cloudhal() integration",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://cloudhal.dev",
|
|
@@ -149,6 +149,10 @@
|
|
|
149
149
|
"types": "./dist/src/embed-middleware.d.ts",
|
|
150
150
|
"default": "./dist/src/embed-middleware.js"
|
|
151
151
|
},
|
|
152
|
+
"./csp-middleware": {
|
|
153
|
+
"types": "./dist/src/csp-middleware.d.ts",
|
|
154
|
+
"default": "./dist/src/csp-middleware.js"
|
|
155
|
+
},
|
|
152
156
|
"./embeds": {
|
|
153
157
|
"types": "./dist/src/embed-client.d.ts",
|
|
154
158
|
"default": "./dist/src/embed-client.js"
|
|
@@ -203,14 +207,14 @@
|
|
|
203
207
|
"README.md"
|
|
204
208
|
],
|
|
205
209
|
"dependencies": {
|
|
206
|
-
"@cloudhal/core-contracts": "0.6.
|
|
207
|
-
"@cloudhal/core-ts": "0.6.
|
|
208
|
-
"@cloudhal/storage": "0.6.
|
|
210
|
+
"@cloudhal/core-contracts": "0.6.1",
|
|
211
|
+
"@cloudhal/core-ts": "0.6.1",
|
|
212
|
+
"@cloudhal/storage": "0.6.1",
|
|
209
213
|
"marked": "^18.1.0",
|
|
210
214
|
"parse5": "8.0.1",
|
|
211
|
-
"@cloudhal/analytics": "0.6.
|
|
215
|
+
"@cloudhal/analytics": "0.6.1",
|
|
212
216
|
"vite": "^8.3.3",
|
|
213
|
-
"@cloudhal/comments": "0.6.
|
|
217
|
+
"@cloudhal/comments": "0.6.1"
|
|
214
218
|
},
|
|
215
219
|
"peerDependencies": {
|
|
216
220
|
"astro": "^7.3.0",
|