fondu 0.13.0 → 0.15.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.
@@ -7,12 +7,11 @@ import { dirname, join, resolve } from 'node:path';
7
7
  import { pathToFileURL } from 'node:url';
8
8
  import { PAGE_DATA, staticFiles } from '../paths.js';
9
9
  /** the response headers a static document keeps: the security headers (hono/secure-headers' and the like) */
10
- const SECURITY_HEADERS = /^(content-security-policy(-report-only)?|strict-transport-security|referrer-policy|permissions-policy|x-content-type-options|x-frame-options|x-xss-protection|x-dns-prefetch-control|x-download-options|x-permitted-cross-domain-policies|origin-agent-cluster|cross-origin-(opener|embedder|resource)-policy)$/i;
10
+ const SECURITY_HEADERS = /^(content-security-policy(-report-only)?|strict-transport-security|referrer-policy|permissions-policy|reporting-endpoints|report-to|x-content-type-options|x-frame-options|x-xss-protection|x-dns-prefetch-control|x-download-options|x-permitted-cross-domain-policies|origin-agent-cluster|cross-origin-(opener|embedder|resource)-policy)$/i;
11
11
  /**
12
- * A static document's own tags: those with the build's nonce (its Content Security Policy's). The nonce of a static
13
- * document is replaced (Cloudflare: its inline scripts by their hashes; Node / Bun: by each response's nonce), which
14
- * only these may get: a `nonce` the document has otherwise (in a page's markup) is dropped. Then the hashes of these
15
- * inline scripts.
12
+ * A static document's own tags: those with the build's nonce (its Content Security Policy's). The policy of a static
13
+ * document has no nonce: its inline scripts are allowed by their hashes instead, and only these tags may have had the
14
+ * nonce: a `nonce` the document has otherwise (in a page's markup) is dropped. Then the hashes of these inline scripts.
16
15
  */
17
16
  export function ownScripts(html, policy) {
18
17
  const nonce = policy?.match(/'nonce-([^']+)'/)?.[1];
@@ -79,10 +78,11 @@ export function withHeaderRules(file, headers) {
79
78
  /**
80
79
  * After the build: the built server (the Worker in wrangler, or `node` / `bun dist/server/index.js`), started with
81
80
  * `FONDU_PRERENDER` set, lists the pages to prerender (`/__fondu/prerender`); each is asked for as a document and as
82
- * a navigation, and both are written to the client build. Cloudflare serves them before the Worker, with the security
83
- * headers written to `_headers`; Node and Bun before the routes, behind the app's own headers.
81
+ * a navigation, and both are written to the client build, with the security headers the build decided for them: one
82
+ * set, in `_headers` (Cloudflare serves the files before the Worker) or in `prerender.json` next to the built server
83
+ * (Node and Bun: its entry serves the files before the app).
84
84
  */
85
- export async function prerender(start, clientDir, logger, headersFile) {
85
+ export async function prerender(start, clientDir, logger, where) {
86
86
  const fail = (lines) => {
87
87
  for (const line of lines)
88
88
  logger.error(`fondu: ${line}`);
@@ -124,7 +124,7 @@ export async function prerender(start, clientDir, logger, headersFile) {
124
124
  name,
125
125
  /^content-security-policy/i.test(name) ? staticPolicy(value, hashes) : value,
126
126
  ]));
127
- if (headersFile && security && JSON.stringify(headers) !== JSON.stringify(security)) {
127
+ if (security && JSON.stringify(headers) !== JSON.stringify(security)) {
128
128
  errors.push(`${path}: its security headers differ from ${paths[0]}'s; one set is written for all`);
129
129
  continue;
130
130
  }
@@ -136,7 +136,10 @@ export async function prerender(start, clientDir, logger, headersFile) {
136
136
  }
137
137
  if (errors.length)
138
138
  return fail(errors);
139
- if (headersFile) {
139
+ if ('serverDir' in where) {
140
+ write(where.serverDir, 'prerender.json', JSON.stringify({ headers: security ?? {} }));
141
+ }
142
+ else {
140
143
  const file = join(clientDir, '_headers');
141
144
  const own = existsSync(file) ? readFileSync(file, 'utf8') : '';
142
145
  writeFileSync(file, withHeaderRules(own, security ?? {}));
@@ -14,6 +14,6 @@ type Node = {
14
14
  export declare function stripServerCode(code: string, program: {
15
15
  body: Node[];
16
16
  }, file: string): string;
17
- /** The browser's copy of every page's route file (`.tsx`, `.jsx` in `app/routes/`): its server code removed */
17
+ /** The browser's copy of every page's route file (`.tsx` in `app/routes/`): its server code removed */
18
18
  export declare function stripRoutes(): Plugin;
19
19
  export {};
@@ -272,7 +272,7 @@ function importOf(code, node, kept) {
272
272
  ].filter(Boolean);
273
273
  return `import ${clauses.join(', ')} from ${source};`;
274
274
  }
275
- /** The browser's copy of every page's route file (`.tsx`, `.jsx` in `app/routes/`): its server code removed */
275
+ /** The browser's copy of every page's route file (`.tsx` in `app/routes/`): its server code removed */
276
276
  export function stripRoutes() {
277
277
  let root = process.cwd();
278
278
  return {
@@ -25,7 +25,7 @@ function generateTypes(root) {
25
25
  declare module 'fondu' {
26
26
  interface Register {
27
27
  routes: {
28
- ${routes.flatMap((f) => patternsOf(f).map((p) => line(p, `./routes/${f.replace(/\.(?:ts|[jt]sx)$/, '')}`))).join('\n')}
28
+ ${routes.flatMap((f) => patternsOf(f).map((p) => line(p, `./routes/${f.replace(/\.tsx?$/, '')}`))).join('\n')}
29
29
  }
30
30
  }
31
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fondu",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "A small meta-framework for full-stack React apps, built on Hono and Vite. It runs on Bun, Cloudflare Workers and Node.",
5
5
  "keywords": [
6
6
  "bun",