fondu 0.12.1 → 0.14.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.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/dist/client/app.d.ts +36 -8
  3. package/dist/client/app.js +65 -41
  4. package/dist/client/form.d.ts +3 -1
  5. package/dist/client/form.js +11 -7
  6. package/dist/client/navigation.d.ts +4 -1
  7. package/dist/client/navigation.js +173 -29
  8. package/dist/client.d.ts +12 -8
  9. package/dist/client.js +7 -1
  10. package/dist/form.d.ts +2 -2
  11. package/dist/form.js +3 -1
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.js +1 -1
  14. package/dist/paths.d.ts +13 -2
  15. package/dist/paths.js +18 -7
  16. package/dist/server/app.d.ts +7 -3
  17. package/dist/server/app.js +38 -27
  18. package/dist/server/document.d.ts +10 -2
  19. package/dist/server/document.js +71 -19
  20. package/dist/server/errors.d.ts +0 -2
  21. package/dist/server/errors.js +4 -3
  22. package/dist/server/flash.d.ts +0 -2
  23. package/dist/server/flash.js +1 -1
  24. package/dist/server/http.d.ts +4 -0
  25. package/dist/server/http.js +5 -0
  26. package/dist/server/prerender.js +7 -49
  27. package/dist/server/protocol.d.ts +16 -2
  28. package/dist/server/protocol.js +95 -15
  29. package/dist/server/routes.d.ts +4 -1
  30. package/dist/server/routes.js +9 -1
  31. package/dist/server/static.d.ts +14 -0
  32. package/dist/server/static.js +37 -0
  33. package/dist/server/validate.d.ts +2 -6
  34. package/dist/virtual.d.ts +5 -1
  35. package/dist/vite/index.d.ts +4 -1
  36. package/dist/vite/index.js +33 -25
  37. package/dist/vite/prerender.d.ts +12 -9
  38. package/dist/vite/prerender.js +17 -13
  39. package/dist/vite/strip.d.ts +1 -3
  40. package/dist/vite/strip.js +3 -15
  41. package/dist/vite/typegen.d.ts +0 -6
  42. package/dist/vite/typegen.js +3 -3
  43. package/package.json +1 -1
@@ -6,14 +6,12 @@ import { createServer } from 'node:net';
6
6
  import { dirname, join, resolve } from 'node:path';
7
7
  import { pathToFileURL } from 'node:url';
8
8
  import { PAGE_DATA, staticFiles } from '../paths.js';
9
- export { staticFiles };
10
9
  /** the response headers a static document keeps: the security headers (hono/secure-headers' and the like) */
11
- 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;
12
11
  /**
13
- * A static document's own tags: those with the build's nonce (its Content Security Policy's). The nonce of a static
14
- * document is replaced (Cloudflare: its inline scripts by their hashes; Node / Bun: by each response's nonce), which
15
- * only these may get: a `nonce` the document has otherwise (in a page's markup) is dropped. Then the hashes of these
16
- * 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.
17
15
  */
18
16
  export function ownScripts(html, policy) {
19
17
  const nonce = policy?.match(/'nonce-([^']+)'/)?.[1];
@@ -80,10 +78,11 @@ export function withHeaderRules(file, headers) {
80
78
  /**
81
79
  * After the build: the built server (the Worker in wrangler, or `node` / `bun dist/server/index.js`), started with
82
80
  * `FONDU_PRERENDER` set, lists the pages to prerender (`/__fondu/prerender`); each is asked for as a document and as
83
- * a navigation, and both are written to the client build. Cloudflare serves them before the Worker, with the security
84
- * 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).
85
84
  */
86
- export async function prerender(start, clientDir, logger, headersFile) {
85
+ export async function prerender(start, clientDir, logger, where) {
87
86
  const fail = (lines) => {
88
87
  for (const line of lines)
89
88
  logger.error(`fondu: ${line}`);
@@ -111,8 +110,10 @@ export async function prerender(start, clientDir, logger, headersFile) {
111
110
  ...(version && { 'X-Fondu-Version': version }),
112
111
  });
113
112
  if (doc.status !== 200 || visit.status !== 200) {
114
- const to = doc.headers.get('location');
115
- errors.push(`${path}: ${doc.status}${to ? ` → ${to}` : ''} (a prerendered page must answer 200 to anyone)`);
113
+ // the request that failed: the document's, else the navigation's
114
+ const [failed, as] = doc.status !== 200 ? [doc, 'a document'] : [visit, 'a navigation'];
115
+ const to = failed.headers.get('location');
116
+ errors.push(`${path}: ${failed.status}${to ? ` → ${to}` : ''} as ${as} (a prerendered page must answer 200 to anyone)`);
116
117
  continue;
117
118
  }
118
119
  const page = (await visit.json());
@@ -123,7 +124,7 @@ export async function prerender(start, clientDir, logger, headersFile) {
123
124
  name,
124
125
  /^content-security-policy/i.test(name) ? staticPolicy(value, hashes) : value,
125
126
  ]));
126
- if (headersFile && security && JSON.stringify(headers) !== JSON.stringify(security)) {
127
+ if (security && JSON.stringify(headers) !== JSON.stringify(security)) {
127
128
  errors.push(`${path}: its security headers differ from ${paths[0]}'s; one set is written for all`);
128
129
  continue;
129
130
  }
@@ -135,7 +136,10 @@ export async function prerender(start, clientDir, logger, headersFile) {
135
136
  }
136
137
  if (errors.length)
137
138
  return fail(errors);
138
- if (headersFile) {
139
+ if ('serverDir' in where) {
140
+ write(where.serverDir, 'prerender.json', JSON.stringify({ headers: security ?? {} }));
141
+ }
142
+ else {
139
143
  const file = join(clientDir, '_headers');
140
144
  const own = existsSync(file) ? readFileSync(file, 'utf8') : '';
141
145
  writeFileSync(file, withHeaderRules(own, security ?? {}));
@@ -1,6 +1,4 @@
1
1
  import type { Plugin } from 'vite';
2
- /** the exports of a page's route file that are server code: the browser gets the page (default) alone */
3
- export declare const SERVER_EXPORTS: Set<string>;
4
2
  type Node = {
5
3
  type: string;
6
4
  start: number;
@@ -16,6 +14,6 @@ type Node = {
16
14
  export declare function stripServerCode(code: string, program: {
17
15
  body: Node[];
18
16
  }, file: string): string;
19
- /** 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 */
20
18
  export declare function stripRoutes(): Plugin;
21
19
  export {};
@@ -1,19 +1,7 @@
1
1
  import { relative, sep } from 'node:path';
2
- import { isPageFile } from '../paths.js';
2
+ import { CONFIG, HANDLERS, isPageFile, METHODS } from '../paths.js';
3
3
  /** the exports of a page's route file that are server code: the browser gets the page (default) alone */
4
- export const SERVER_EXPORTS = new Set([
5
- 'loader',
6
- 'action',
7
- 'actions',
8
- 'prerender',
9
- 'params',
10
- 'GET',
11
- 'POST',
12
- 'PUT',
13
- 'PATCH',
14
- 'DELETE',
15
- 'OPTIONS',
16
- ]);
4
+ const SERVER_EXPORTS = new Set([...HANDLERS, ...CONFIG, ...METHODS]);
17
5
  const nameOf = (n) => (n.name ?? n.value);
18
6
  /** the names a declaration binds: `const { a, b: [c] } = …` → a, c */
19
7
  function bound(node, out = []) {
@@ -284,7 +272,7 @@ function importOf(code, node, kept) {
284
272
  ].filter(Boolean);
285
273
  return `import ${clauses.join(', ')} from ${source};`;
286
274
  }
287
- /** 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 */
288
276
  export function stripRoutes() {
289
277
  let root = process.cwd();
290
278
  return {
@@ -1,9 +1,3 @@
1
1
  import type { Plugin } from 'vite';
2
2
  export declare function walk(dir: string, out?: string[], base?: string): string[];
3
- /**
4
- * The registry the types are read from: URL pattern → route module (`href()`). Only `typeof import`s; an MDX page's
5
- * keeps its extension, which `declare module '*.mdx'` (@types/mdx) types.
6
- */
7
- export declare function generateTypes(root: string): string;
8
- export declare function writeTypes(root: string): void;
9
3
  export declare function typegen(): Plugin;
@@ -18,14 +18,14 @@ export function walk(dir, out = [], base = dir) {
18
18
  * The registry the types are read from: URL pattern → route module (`href()`). Only `typeof import`s; an MDX page's
19
19
  * keeps its extension, which `declare module '*.mdx'` (@types/mdx) types.
20
20
  */
21
- export function generateTypes(root) {
21
+ function generateTypes(root) {
22
22
  const routes = walk(join(root, 'app/routes')).filter(isRouteFile).sort();
23
23
  const line = (key, path) => ` ${JSON.stringify(key)}: typeof import(${JSON.stringify(path)})`;
24
24
  return `// Generated by fondu (vite dev / vite build). Commit it; do not edit.
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
  }
@@ -41,7 +41,7 @@ function patternsOf(file) {
41
41
  return [];
42
42
  }
43
43
  }
44
- export function writeTypes(root) {
44
+ function writeTypes(root) {
45
45
  const file = join(root, OUT);
46
46
  const content = generateTypes(root);
47
47
  if (!existsSync(file) || readFileSync(file, 'utf8') !== content)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fondu",
3
- "version": "0.12.1",
3
+ "version": "0.14.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",