rosetack 0.1.1 → 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 (56) hide show
  1. package/deploy/cloudflare/functions/[[path]].ts +61 -0
  2. package/deploy/cloudflare/wrangler.toml +14 -0
  3. package/deploy/k8s/rosetack.yaml +107 -0
  4. package/deploy/netlify/functions/rosetack.ts +38 -0
  5. package/deploy/netlify.toml +17 -0
  6. package/devtools/devtools.html +7 -0
  7. package/devtools/devtools.js +1 -0
  8. package/devtools/manifest.json +8 -0
  9. package/devtools/panel.html +28 -0
  10. package/devtools/panel.js +54 -0
  11. package/dist-cli/compiler.mjs +1346 -492
  12. package/dist-cli/edge.mjs +152 -41
  13. package/dist-cli/rosetack.mjs +1974 -606
  14. package/dist-cli/runtime.mjs +205 -40
  15. package/dist-cli/test.mjs +1425 -508
  16. package/package.json +4 -3
  17. package/src/FRAMEWORK_VERSION +1 -1
  18. package/src/cli/edge.ts +76 -61
  19. package/src/cli/index.ts +516 -293
  20. package/src/cli/prompt.ts +130 -0
  21. package/src/cli/shell.ts +113 -65
  22. package/src/cli/test.ts +9 -70
  23. package/src/compiler/errors.ts +74 -0
  24. package/src/compiler/index.ts +1514 -839
  25. package/src/runtime/index.ts +255 -150
  26. package/src/types/rosetack.d.ts +18 -59
  27. package/templates/admin/package.json +14 -0
  28. package/templates/admin/rosetack.config.js +1 -0
  29. package/templates/admin/src/pages/_middleware.rose +33 -0
  30. package/templates/admin/src/pages/api/items.rose +37 -0
  31. package/templates/admin/src/pages/api/login.rose +25 -0
  32. package/templates/admin/src/pages/index.rose +79 -0
  33. package/templates/api/package.json +14 -0
  34. package/templates/api/rosetack.config.js +1 -0
  35. package/templates/api/src/pages/api/health.rose +7 -0
  36. package/templates/api/src/pages/api/items.rose +46 -0
  37. package/templates/api/src/pages/api/login.rose +24 -0
  38. package/templates/api/src/pages/index.rose +25 -0
  39. package/templates/blog/package.json +14 -0
  40. package/templates/blog/rosetack.config.js +1 -0
  41. package/templates/blog/src/pages/404.rose +2 -0
  42. package/templates/blog/src/pages/_layout.rose +42 -0
  43. package/templates/blog/src/pages/_middleware.rose +74 -0
  44. package/templates/blog/src/pages/admin.rose +85 -0
  45. package/templates/blog/src/pages/api/login.rose +46 -0
  46. package/templates/blog/src/pages/api/posts/[id].rose +37 -0
  47. package/templates/blog/src/pages/api/posts.rose +72 -0
  48. package/templates/blog/src/pages/index.rose +31 -0
  49. package/templates/blog/src/pages/post/[id].rose +36 -0
  50. package/templates/site/package.json +14 -0
  51. package/templates/site/rosetack.config.js +1 -0
  52. package/templates/site/src/pages/404.rose +2 -0
  53. package/templates/site/src/pages/_layout.rose +40 -0
  54. package/templates/site/src/pages/about.rose +6 -0
  55. package/templates/site/src/pages/index.rose +13 -0
  56. 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.1",
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.1
1
+ 0.1.3
package/src/cli/edge.ts CHANGED
@@ -1,31 +1,18 @@
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;
14
+ /** P0-2: routes that exported `buffer = true` - buffered, never streamed */
15
+ isBuffered(pathname: string): boolean;
29
16
  isApi(pathname: string): boolean;
30
17
  handleApi(method: string, pathname: string, request: Request): Promise<Response>;
31
18
  /** The document's <html lang>/<dir> for a pathname (streaming needs them before the render) */
@@ -51,35 +38,78 @@ type ServerModule = {
51
38
  headersFor(pathname: string): Record<string, string> | null;
52
39
  /** Routes that exported `csp = { nonce: true }` - nonce policy, never baked */
53
40
  cspNonce(pathname: string): boolean;
41
+ /** P0-1: the query of the request being rendered, for $query() */
42
+ setQuery(query: Record<string, string>): void;
54
43
  };
55
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
+
56
68
  /**
57
69
  * Create a fetch handler from a built rosetack project.
58
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.
59
76
  */
60
- export async function createEdgeHandler(outDir: string): Promise<(request: Request) => Promise<Response>> {
61
- 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);
62
82
  let client: string | null = null;
63
83
  let styles = '';
64
- try {
65
- client = await (await import('fs/promises')).readFile(join(outDir, 'client.js'), 'utf-8');
66
- } catch {
67
- 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
+ }
68
92
  }
69
- try {
70
- styles = await (await import('fs/promises')).readFile(join(outDir, 'styles.css'), 'utf-8');
71
- } catch {
72
- 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
+ }
73
101
  }
74
- // routes whose render failed at build time: buffered, so the 500 survives
75
102
  let broken = new Set<string>();
76
- try {
77
- broken = new Set(JSON.parse(await (await import('fs/promises')).readFile(join(outDir, 'broken.json'), 'utf-8')));
78
- } catch {
79
- 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
+ }
80
111
  }
81
112
 
82
- // Response headers for a page document: the strict CSP hashed over the exact inlined bytes …
83
113
  const pageHeaders = (pathname: string, nonce = ''): Record<string, string> => {
84
114
  const headers: Record<string, string> = {};
85
115
  if (client) for (const [k, v] of Object.entries(securityHeaders(client, nonce))) headers[k.toLowerCase()] = v;
@@ -89,9 +119,14 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
89
119
  };
90
120
 
91
121
  return async function handle(request: Request): Promise<Response> {
92
- const { pathname } = new URL(request.url);
93
- // Runtime hooks: onRequest runs before middleware and routing and may short-circuit with
94
- // its own Response - an auth wall, a rate limit.
122
+ const refused = crossSiteWrite(request);
123
+ if (refused) return refused;
124
+ const { pathname, searchParams } = new URL(request.url);
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
+ }
129
+ mod.setQuery?.(Object.fromEntries(searchParams));
95
130
  const hookCtx = mod.hasRequestHooks || mod.hasResponseHooks
96
131
  ? { request, url: new URL(request.url), method: request.method, pathname, state: {} as Record<string, unknown> }
97
132
  : null;
@@ -99,28 +134,17 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
99
134
  const short = await mod.runRequestHooks(hookCtx);
100
135
  if (short) return mod.runResponseHooks(hookCtx, short);
101
136
  }
102
- // Middleware (pages/_middleware.rose) runs exactly once per request, before anything
103
- // else: a returned Response short-circuits the whole request (a redirect, an auth wall) …
104
137
  if (mod.middleware) {
105
138
  const mw = await mod.runMiddleware(request);
106
139
  if (mw) return hookCtx ? mod.runResponseHooks(hookCtx, mw) : mw;
107
140
  }
108
- // API routes (pages/api/*.rose): the /api namespace answers JSON, never the HTML shell …
109
141
  if (mod.isApi(pathname)) {
110
142
  const apiRes = await mod.handleApi(request.method, pathname, request);
111
143
  return hookCtx ? mod.runResponseHooks(hookCtx, apiRes) : apiRes;
112
144
  }
113
- // POST: progressive-enhancement form - run the route's server action with
114
- // the submitted FormData and re-render the page (works without JS).
115
145
  const form = request.method === 'POST' ? await request.formData() : undefined;
116
- // csr = false routes buffer too: a streamed no-JS document would carry the shell's default
117
- // <title> - the route head is applied client-side at boot, and there is no client.
118
- if (!form && !broken.has(pathname) && mod.canStream(pathname) && !mod.isNoJs(pathname)) {
119
- // The nonce is minted before the Response exists, because its headers are fixed from then
120
- // on - and the document's script tag is written into that same stream …
146
+ if (!form && !broken.has(pathname) && !mod.isBuffered?.(pathname) && mod.canStream(pathname) && !mod.isNoJs(pathname)) {
121
147
  const nonce = mod.cspNonce?.(pathname) ? mintNonce() : '';
122
- // streaming GET: one chunked Response, still exactly one request. onResponse runs
123
- // before the stream is created, while the status and headers can still change …
124
148
  const streamed = new Response(null, {
125
149
  status: 200,
126
150
  headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname, nonce) }
@@ -129,13 +153,11 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
129
153
  const encoder = new TextEncoder();
130
154
  const stream = new ReadableStream<Uint8Array>({
131
155
  async start(controller) {
132
- // The shell opens before the render, so its <html lang>/<dir>
133
- // come from the pathname (the server bundle's docAttrs)
134
156
  const doc = mod.docAttrs(pathname);
135
157
  await mod.renderPageStream(
136
158
  pathname,
137
159
  (chunk) => controller.enqueue(encoder.encode(chunk)),
138
- shellOpen(styles, doc.lang, doc.dir),
160
+ shellOpen(styles, doc.lang, doc.dir, !mod.shellOff?.(pathname)),
139
161
  clientScriptTag(client, nonce)
140
162
  );
141
163
  controller.close();
@@ -143,16 +165,9 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
143
165
  });
144
166
  return new Response(stream, { status: head.status, headers: head.headers });
145
167
  }
146
- // buffered: POST actions, unmatched routes (404), known-broken routes (500).
147
- // The middleware already ran at the top of this handler, so the render
148
- // below sees its context.
149
168
  const page = await mod.renderPage(pathname, form);
150
- // '' for every hashed route, so the document is byte-identical to
151
- // what it was before nonce mode existed.
152
169
  const nonce = mod.cspNonce?.(pathname) ? mintNonce() : '';
153
- // js = page.csr !== false: a csr = false route ships no bundle, no state script - the
154
- // document is HTML + CSS only .
155
- 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);
156
171
  const buffered = new Response(html, {
157
172
  status: page.status ?? 200,
158
173
  headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname, nonce) }