rosetack 0.1.2 → 0.1.3

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 (54) hide show
  1. package/deploy/cloudflare/functions/[[path]].ts +61 -0
  2. package/deploy/cloudflare/wrangler.toml +14 -0
  3. package/deploy/netlify/functions/rosetack.ts +38 -0
  4. package/deploy/netlify.toml +17 -0
  5. package/devtools/devtools.html +7 -0
  6. package/devtools/devtools.js +1 -0
  7. package/devtools/manifest.json +8 -0
  8. package/devtools/panel.html +28 -0
  9. package/devtools/panel.js +54 -0
  10. package/dist-cli/compiler.mjs +818 -445
  11. package/dist-cli/edge.mjs +115 -39
  12. package/dist-cli/rosetack.mjs +1078 -529
  13. package/dist-cli/runtime.mjs +115 -34
  14. package/dist-cli/test.mjs +875 -464
  15. package/package.json +4 -3
  16. package/src/FRAMEWORK_VERSION +1 -1
  17. package/src/cli/edge.ts +69 -61
  18. package/src/cli/index.ts +243 -328
  19. package/src/cli/prompt.ts +4 -1
  20. package/src/cli/shell.ts +67 -69
  21. package/src/cli/test.ts +9 -70
  22. package/src/compiler/index.ts +921 -851
  23. package/src/runtime/index.ts +155 -158
  24. package/src/types/rosetack.d.ts +16 -66
  25. package/templates/admin/package.json +14 -0
  26. package/templates/admin/rosetack.config.js +1 -0
  27. package/templates/admin/src/pages/_middleware.rose +33 -0
  28. package/templates/admin/src/pages/api/items.rose +37 -0
  29. package/templates/admin/src/pages/api/login.rose +25 -0
  30. package/templates/admin/src/pages/index.rose +79 -0
  31. package/templates/api/package.json +14 -0
  32. package/templates/api/rosetack.config.js +1 -0
  33. package/templates/api/src/pages/api/health.rose +7 -0
  34. package/templates/api/src/pages/api/items.rose +46 -0
  35. package/templates/api/src/pages/api/login.rose +24 -0
  36. package/templates/api/src/pages/index.rose +25 -0
  37. package/templates/blog/package.json +14 -0
  38. package/templates/blog/rosetack.config.js +1 -0
  39. package/templates/blog/src/pages/404.rose +2 -0
  40. package/templates/blog/src/pages/_layout.rose +42 -0
  41. package/templates/blog/src/pages/_middleware.rose +74 -0
  42. package/templates/blog/src/pages/admin.rose +85 -0
  43. package/templates/blog/src/pages/api/login.rose +46 -0
  44. package/templates/blog/src/pages/api/posts/[id].rose +37 -0
  45. package/templates/blog/src/pages/api/posts.rose +72 -0
  46. package/templates/blog/src/pages/index.rose +31 -0
  47. package/templates/blog/src/pages/post/[id].rose +36 -0
  48. package/templates/site/package.json +14 -0
  49. package/templates/site/rosetack.config.js +1 -0
  50. package/templates/site/src/pages/404.rose +2 -0
  51. package/templates/site/src/pages/_layout.rose +40 -0
  52. package/templates/site/src/pages/about.rose +6 -0
  53. package/templates/site/src/pages/index.rose +13 -0
  54. package/templates/site/src/pages/pricing.rose +8 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rosetack",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Zero-hydration full-stack web framework with compile-time reactivity and zero-JS content routes.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -26,9 +26,9 @@
26
26
  "dist-cli",
27
27
  "src",
28
28
  "deploy",
29
+ "templates",
30
+ "devtools",
29
31
  "pack.mjs",
30
- "README.md",
31
- "VERSIONING.md",
32
32
  "LICENSE"
33
33
  ],
34
34
  "license": "MIT",
@@ -41,6 +41,7 @@
41
41
  "devDependencies": {
42
42
  "@types/node": "^22.0.0",
43
43
  "jsdom": "^29.1.1",
44
+ "pg": "^8.23.0",
44
45
  "tsx": "^4.7.0",
45
46
  "typescript": "^5.6.0"
46
47
  },
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
package/src/cli/edge.ts CHANGED
@@ -1,31 +1,16 @@
1
- /**
2
- * rosetack Edge adapter - Web-standard fetch handler.
3
- *
4
- * Works on any edge runtime that supports the Fetch API standard
5
- * (Cloudflare Workers, Deno Deploy, Vercel Edge, Bun, Node 18+):
6
- *
7
- * import { createEdgeHandler } from 'rosetack/edge';
8
- * const handle = createEdgeHandler('./dist');
9
- * export default { fetch: handle };
10
- *
11
- * The generated dist/server.js has zero Node built-in imports, so the same
12
- * bundle that runs the Node server runs at the edge. Static routes can be
13
- * served by the platform's asset layer; everything else renders on demand.
14
- * GETs to known routes stream (the static shell flushes before the render
15
- * completes, still one request); routes known-broken at build time
16
- * (dist/broken.json) stay buffered so their real 500 status survives, and
17
- * csr = false routes buffer so their document is complete without a client.
18
- */
1
+ // rosetack Edge adapter - Web-standard fetch handler.
19
2
 
20
3
  import { pathToFileURL } from 'url';
21
4
  import { join } from 'path';
22
5
  import { buildShell, shellOpen, clientScriptTag, securityHeaders, mintNonce } from './shell.js';
23
6
 
24
7
  type ServerModule = {
25
- renderPage(pathname: string, form?: FormData): Promise<{ html: string; state: string; head: string[]; status?: number; csr?: boolean; lang?: string; dir?: string }>;
8
+ renderPage(pathname: string, form?: FormData): Promise<{ html: string; state: string; head: string[]; status?: number; csr?: boolean; shell?: boolean; lang?: string; dir?: string }>;
26
9
  renderPageStream(pathname: string, write: (chunk: string) => void, shellOpen: string, clientTag: string): Promise<number>;
27
10
  canStream(pathname: string): boolean;
28
11
  isNoJs(pathname: string): boolean;
12
+ /** the matched route exported `shell = false` - the streaming shell must ask before it flushes */
13
+ shellOff?(pathname: string): boolean;
29
14
  /** P0-2: routes that exported `buffer = true` - buffered, never streamed */
30
15
  isBuffered(pathname: string): boolean;
31
16
  isApi(pathname: string): boolean;
@@ -57,33 +42,74 @@ type ServerModule = {
57
42
  setQuery(query: Record<string, string>): void;
58
43
  };
59
44
 
45
+ // CSRF: the edge twin of the Node server's guard.
46
+ function crossSiteWrite(request: Request): Response | null {
47
+ const m = request.method;
48
+ if (m !== 'POST' && m !== 'PUT' && m !== 'PATCH' && m !== 'DELETE') return null;
49
+ const origin = request.headers.get('origin');
50
+ if (origin) {
51
+ let host: string;
52
+ try {
53
+ host = new URL(origin).host;
54
+ } catch {
55
+ return new Response('Cross-site request blocked (unparsable Origin header)', { status: 403 });
56
+ }
57
+ return host === new URL(request.url).host
58
+ ? null
59
+ : new Response('Cross-site request blocked (Origin does not match Host)', { status: 403 });
60
+ }
61
+ const site = request.headers.get('sec-fetch-site');
62
+ if (site && site !== 'same-origin' && site !== 'same-site' && site !== 'none') {
63
+ return new Response(`Cross-site request blocked (sec-fetch-site: ${site})`, { status: 403 });
64
+ }
65
+ return null;
66
+ }
67
+
60
68
  /**
61
69
  * Create a fetch handler from a built rosetack project.
62
70
  * @param outDir directory containing server.js and client.js
71
+ * @param preloaded for runtimes with NO filesystem (Cloudflare Workers /
72
+ * Pages): the caller statically imports the server bundle (so the deploy
73
+ * compiler bundles it in) and fetches the site's own static assets once
74
+ * per isolate. Anything provided here is used verbatim; anything absent
75
+ * falls back to reading it from outDir, exactly as before.
63
76
  */
64
- export async function createEdgeHandler(outDir: string): Promise<(request: Request) => Promise<Response>> {
65
- const mod: ServerModule = await import(pathToFileURL(join(outDir, 'server.js')).href);
77
+ export async function createEdgeHandler(
78
+ outDir: string,
79
+ preloaded?: { server?: ServerModule; client?: string; styles?: string; broken?: string[] },
80
+ ): Promise<(request: Request) => Promise<Response>> {
81
+ const mod: ServerModule = preloaded?.server ?? await import(pathToFileURL(join(outDir, 'server.js')).href);
66
82
  let client: string | null = null;
67
83
  let styles = '';
68
- try {
69
- client = await (await import('fs/promises')).readFile(join(outDir, 'client.js'), 'utf-8');
70
- } catch {
71
- client = null; // no fs (Workers): platform should inline the bundle at deploy time
84
+ if (preloaded && 'client' in preloaded) {
85
+ client = preloaded.client ?? null;
86
+ } else {
87
+ try {
88
+ client = await (await import('fs/promises')).readFile(join(outDir, 'client.js'), 'utf-8');
89
+ } catch {
90
+ client = null;
91
+ }
72
92
  }
73
- try {
74
- styles = await (await import('fs/promises')).readFile(join(outDir, 'styles.css'), 'utf-8');
75
- } catch {
76
- styles = ''; // no component styles (or no fs): shell STYLES only
93
+ if (preloaded && 'styles' in preloaded) {
94
+ styles = preloaded.styles ?? '';
95
+ } else {
96
+ try {
97
+ styles = await (await import('fs/promises')).readFile(join(outDir, 'styles.css'), 'utf-8');
98
+ } catch {
99
+ styles = '';
100
+ }
77
101
  }
78
- // routes whose render failed at build time: buffered, so the 500 survives
79
102
  let broken = new Set<string>();
80
- try {
81
- broken = new Set(JSON.parse(await (await import('fs/promises')).readFile(join(outDir, 'broken.json'), 'utf-8')));
82
- } catch {
83
- broken = new Set(); // no manifest: nothing known-broken
103
+ if (preloaded && 'broken' in preloaded) {
104
+ broken = new Set(preloaded.broken);
105
+ } else {
106
+ try {
107
+ broken = new Set(JSON.parse(await (await import('fs/promises')).readFile(join(outDir, 'broken.json'), 'utf-8')));
108
+ } catch {
109
+ broken = new Set();
110
+ }
84
111
  }
85
112
 
86
- // Response headers for a page document: the strict CSP hashed over the exact inlined bytes …
87
113
  const pageHeaders = (pathname: string, nonce = ''): Record<string, string> => {
88
114
  const headers: Record<string, string> = {};
89
115
  if (client) for (const [k, v] of Object.entries(securityHeaders(client, nonce))) headers[k.toLowerCase()] = v;
@@ -93,12 +119,14 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
93
119
  };
94
120
 
95
121
  return async function handle(request: Request): Promise<Response> {
122
+ const refused = crossSiteWrite(request);
123
+ if (refused) return refused;
96
124
  const { pathname, searchParams } = new URL(request.url);
97
- // P0-1: the query reaches the render through $query() - routing itself
98
- // matches the pathname, exactly as the Node server does.
125
+ const canonical = pathname.replace(/\/+$/, '') || '/';
126
+ if (canonical !== pathname) {
127
+ return new Response(null, { status: 308, headers: { location: canonical + new URL(request.url).search } });
128
+ }
99
129
  mod.setQuery?.(Object.fromEntries(searchParams));
100
- // Runtime hooks: onRequest runs before middleware and routing and may short-circuit with
101
- // its own Response - an auth wall, a rate limit.
102
130
  const hookCtx = mod.hasRequestHooks || mod.hasResponseHooks
103
131
  ? { request, url: new URL(request.url), method: request.method, pathname, state: {} as Record<string, unknown> }
104
132
  : null;
@@ -106,28 +134,17 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
106
134
  const short = await mod.runRequestHooks(hookCtx);
107
135
  if (short) return mod.runResponseHooks(hookCtx, short);
108
136
  }
109
- // Middleware (pages/_middleware.rose) runs exactly once per request, before anything
110
- // else: a returned Response short-circuits the whole request (a redirect, an auth wall) …
111
137
  if (mod.middleware) {
112
138
  const mw = await mod.runMiddleware(request);
113
139
  if (mw) return hookCtx ? mod.runResponseHooks(hookCtx, mw) : mw;
114
140
  }
115
- // API routes (pages/api/*.rose): the /api namespace answers JSON, never the HTML shell …
116
141
  if (mod.isApi(pathname)) {
117
142
  const apiRes = await mod.handleApi(request.method, pathname, request);
118
143
  return hookCtx ? mod.runResponseHooks(hookCtx, apiRes) : apiRes;
119
144
  }
120
- // POST: progressive-enhancement form - run the route's server action with
121
- // the submitted FormData and re-render the page (works without JS).
122
145
  const form = request.method === 'POST' ? await request.formData() : undefined;
123
- // csr = false routes buffer too: a streamed no-JS document would carry the shell's default
124
- // <title> - the route head is applied client-side at boot, and there is no client.
125
146
  if (!form && !broken.has(pathname) && !mod.isBuffered?.(pathname) && mod.canStream(pathname) && !mod.isNoJs(pathname)) {
126
- // The nonce is minted before the Response exists, because its headers are fixed from then
127
- // on - and the document's script tag is written into that same stream …
128
147
  const nonce = mod.cspNonce?.(pathname) ? mintNonce() : '';
129
- // streaming GET: one chunked Response, still exactly one request. onResponse runs
130
- // before the stream is created, while the status and headers can still change …
131
148
  const streamed = new Response(null, {
132
149
  status: 200,
133
150
  headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname, nonce) }
@@ -136,13 +153,11 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
136
153
  const encoder = new TextEncoder();
137
154
  const stream = new ReadableStream<Uint8Array>({
138
155
  async start(controller) {
139
- // The shell opens before the render, so its <html lang>/<dir>
140
- // come from the pathname (the server bundle's docAttrs)
141
156
  const doc = mod.docAttrs(pathname);
142
157
  await mod.renderPageStream(
143
158
  pathname,
144
159
  (chunk) => controller.enqueue(encoder.encode(chunk)),
145
- shellOpen(styles, doc.lang, doc.dir),
160
+ shellOpen(styles, doc.lang, doc.dir, !mod.shellOff?.(pathname)),
146
161
  clientScriptTag(client, nonce)
147
162
  );
148
163
  controller.close();
@@ -150,16 +165,9 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
150
165
  });
151
166
  return new Response(stream, { status: head.status, headers: head.headers });
152
167
  }
153
- // buffered: POST actions, unmatched routes (404), known-broken routes (500).
154
- // The middleware already ran at the top of this handler, so the render
155
- // below sees its context.
156
168
  const page = await mod.renderPage(pathname, form);
157
- // '' for every hashed route, so the document is byte-identical to
158
- // what it was before nonce mode existed.
159
169
  const nonce = mod.cspNonce?.(pathname) ? mintNonce() : '';
160
- // js = page.csr !== false: a csr = false route ships no bundle, no state script - the
161
- // document is HTML + CSS only .
162
- const html = buildShell(page.html, page.state, client, page.head, styles, page.csr !== false, page.lang, page.dir, nonce);
170
+ const html = buildShell(page.html, page.state, client, page.head, styles, page.csr !== false, page.lang, page.dir, nonce, page.shell !== false);
163
171
  const buffered = new Response(html, {
164
172
  status: page.status ?? 200,
165
173
  headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname, nonce) }