fondu 0.9.5 → 0.9.6

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.
@@ -16,8 +16,12 @@ export declare function ownScripts(html: string, policy: string | undefined): {
16
16
  * allowed by their hashes instead, and `'self'` scripts
17
17
  */
18
18
  export declare function staticPolicy(policy: string, hashes?: string[]): string;
19
- /** the `_headers` rules: the documents' security headers, and the page objects' X-Inertia (Inertia requires it) */
20
- export declare function headerRules(headers: Record<string, string>): string;
19
+ /**
20
+ * `_headers` with Fondu's rules in it: the documents' security headers (`/*`), and the page objects' X-Inertia
21
+ * (Inertia requires it). A path the file has a rule for gets the headers it doesn't set there, so the app's own stay
22
+ * (Workers keep one rule per path: a second `/*` would replace the first); any other, a rule at the end.
23
+ */
24
+ export declare function withHeaderRules(file: string, headers: Record<string, string>): string;
21
25
  /** the built server, running for the prerender: fetch a path of it, stop it */
22
26
  interface Server {
23
27
  fetch(path: string, headers?: Record<string, string>): Promise<Response>;
@@ -1,6 +1,6 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
- import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs';
3
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
4
4
  import { createRequire } from 'node:module';
5
5
  import { createServer } from 'node:net';
6
6
  import { dirname, join, resolve } from 'node:path';
@@ -47,10 +47,35 @@ export function staticPolicy(policy, hashes = []) {
47
47
  .filter(Boolean)
48
48
  .join('; ');
49
49
  }
50
- /** the `_headers` rules: the documents' security headers, and the page objects' X-Inertia (Inertia requires it) */
51
- export function headerRules(headers) {
52
- const lines = Object.entries(headers).map(([name, value]) => ` ${name}: ${value}`);
53
- return `\n/*\n${lines.join('\n')}\n${PAGE_DATA}/*\n X-Inertia: true\n`;
50
+ /**
51
+ * `_headers` with Fondu's rules in it: the documents' security headers (`/*`), and the page objects' X-Inertia
52
+ * (Inertia requires it). A path the file has a rule for gets the headers it doesn't set there, so the app's own stay
53
+ * (Workers keep one rule per path: a second `/*` would replace the first); any other, a rule at the end.
54
+ */
55
+ export function withHeaderRules(file, headers) {
56
+ const lines = file.replace(/\n+$/, '').split('\n');
57
+ const rules = [
58
+ ['/*', headers],
59
+ [`${PAGE_DATA}/*`, { 'X-Inertia': 'true' }],
60
+ ];
61
+ for (const [path, set] of rules) {
62
+ const start = lines.findIndex((line) => line.trimEnd() === path);
63
+ let end = start + 1;
64
+ while (start !== -1 && /^\s+\S/.test(lines[end] ?? ''))
65
+ end++;
66
+ // the names set or removed (`! name`) there
67
+ const own = new Set(lines
68
+ .slice(start + 1, end)
69
+ .map((l) => l.trim().replace(/^!\s*/, '').split(':')[0].toLowerCase()));
70
+ const added = Object.entries(set)
71
+ .filter(([name]) => start === -1 || !own.has(name.toLowerCase()))
72
+ .map(([name, value]) => ` ${name}: ${value}`);
73
+ if (start === -1)
74
+ lines.push(...(lines.at(-1) ? [''] : []), path, ...added);
75
+ else
76
+ lines.splice(end, 0, ...added);
77
+ }
78
+ return `${lines.join('\n')}\n`;
54
79
  }
55
80
  /**
56
81
  * After the build: the built server (the Worker in wrangler, or `node` / `bun dist/server/index.js`), started with
@@ -115,8 +140,11 @@ export async function prerender(start, clientDir, logger, headersFile) {
115
140
  }
116
141
  if (errors.length)
117
142
  return fail(errors);
118
- if (headersFile)
119
- appendFileSync(join(clientDir, '_headers'), headerRules(security ?? {}));
143
+ if (headersFile) {
144
+ const file = join(clientDir, '_headers');
145
+ const own = existsSync(file) ? readFileSync(file, 'utf8') : '';
146
+ writeFileSync(file, withHeaderRules(own, security ?? {}));
147
+ }
120
148
  }
121
149
  finally {
122
150
  await server.dispose();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fondu",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "description": "A small meta-framework for full-stack React apps, built on Hono, Vite and Inertia. It runs on Bun, Cloudflare Workers and Node.",
5
5
  "keywords": [
6
6
  "bun",