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/dist-cli/edge.mjs CHANGED
@@ -22,7 +22,25 @@ function getStyles() {
22
22
  }
23
23
  }
24
24
  var BOOTSTRAP = esbuild.transformSync(`
25
- const __app = document.getElementById('app');
25
+ // shell = false: the document's SSR content sits directly in <body> - no
26
+ // #app wrapper, no demo styles, so a real theme's CSS owns the document from
27
+ // the first byte (and a JS-less visitor never sees either). The bundle still
28
+ // runs, and it still needs its container: wrap the content once, here, before
29
+ // any theme script executes (module scripts run before DOMContentLoaded). The
30
+ // wrapper is a plain unstyled div, so the re-parent is visually inert; every
31
+ // later path - adoption, navigation, forms - is then the ordinary one.
32
+ let __app = document.getElementById('app');
33
+ if (!__app) {
34
+ __app = document.createElement('div');
35
+ __app.id = 'app';
36
+ // scripts stay outside the wrapper, exactly where the dressed document
37
+ // keeps them: the state element and the running module must survive a
38
+ // navigation's innerHTML swap (they are the document's, not the route's).
39
+ for (const node of Array.from(document.body.children)) {
40
+ if (node.tagName !== 'SCRIPT') __app.appendChild(node);
41
+ }
42
+ document.body.appendChild(__app);
43
+ }
26
44
  // Native View Transitions around client-side navigation (progressive:
27
45
  // browsers without the API navigate normally, no polyfill, no CSS required).
28
46
  function __navigate(pathname) {
@@ -85,13 +103,15 @@ document.addEventListener('submit', (e) => {
85
103
  e.preventDefault();
86
104
  postForm(new FormData(f));
87
105
  });
88
- // Dev seam: the live signal graph, readable as JSON. Two callers, one
106
+ // Dev seam: the live signal graph, readable as JSON. Three callers, one
89
107
  // implementation: the dev server's reload bridge snapshots it before a
90
108
  // rebuild-triggered reload so the edit lands on the state the developer had,
91
- // and a devtools extension reads it the same way. It is the same
92
- // serializeState the zero-hydration resume already ships, so the only new
93
- // bytes are the assignment.
94
- window.__rosetack = { state: () => serializeState() };
109
+ // and the devtools extension (devtools/) reads it the same way - the signal
110
+ // graph plus the prefetch counters, which is the whole observable state of a
111
+ // running page. Both are DIRECT references (no wrapper arrow): the names take
112
+ // no arguments, so the call is identical and every document that carries the
113
+ // bundle pays eleven bytes for the pair instead of forty.
114
+ window.__rosetack = { state: serializeState, prefetch: prefetchStats };
95
115
  // initial=true: DOM is SSR output, resume state + wire markers (zero hydration).
96
116
  // If the document was rendered for a DIFFERENT route (a static-file server's
97
117
  // SPA fallback serves index.html for unknown paths), adopt nothing - let the
@@ -110,7 +130,14 @@ if (__resumed) {
110
130
  __state = Object.assign(__state, __resumed);
111
131
  __state.__route = __route;
112
132
  }
113
- if (__resumed) {
133
+ // A NOT-FOUND document (the server stamps __notFound onto its state) boots
134
+ // into the app's own 404 route: the URL matches no route - or a catch-all
135
+ // whose resolver threw notFound(), whose key sits null in the resumed state
136
+ // and whose template would dereference it into a 500. startNotFound paints
137
+ // the not-found route from the resumed state instead (zero requests).
138
+ if (__state.__notFound) {
139
+ startNotFound(__app, window.location.pathname);
140
+ } else if (__resumed) {
114
141
  start(__app, window.location.pathname);
115
142
  } else if (__state.__route === window.location.pathname) {
116
143
  start(__app, window.location.pathname, true);
@@ -118,10 +145,10 @@ if (__resumed) {
118
145
  start(__app, window.location.pathname);
119
146
  }
120
147
  `, { minify: true }).code;
121
- var CSP_TAIL = `style-src 'unsafe-inline'; img-src data:; font-src data:; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'; object-src 'none'`;
148
+ var CSP_TAIL = `style-src 'unsafe-inline'; img-src 'self' data: https://fastly.picsum.photos; font-src 'self' data:; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'; object-src 'none'`;
122
149
  var cspCache = null;
123
150
  var EXTERNAL_BOOTSTRAP = `
124
- import { start, prefetch, postForm } from '/client.js';
151
+ import { start, prefetch, postForm, prefetchStats } from '/client.js';
125
152
  ${BOOTSTRAP}`;
126
153
  function inlineScript(client) {
127
154
  return `
@@ -205,71 +232,114 @@ function clientScriptTag(client, nonce = "") {
205
232
  return client ? `<script type="module"${n}>${inlineScript(client)}</script>` : `<script type="module"${n} src="/client.js"></script>
206
233
  <script type="module"${n}>${EXTERNAL_BOOTSTRAP}</script>`;
207
234
  }
208
- function shellOpen(styles = getStyles(), lang = "en", dir = "") {
235
+ function shellOpen(styles = getStyles(), lang = "en", dir = "", shell = true) {
209
236
  const styleTag = styles ? `
210
237
  <style>${styles}</style>` : "";
238
+ const demoStyles = shell ? `
239
+ <style>${STYLES_MIN}</style>` : "";
240
+ const appOpen = shell ? `
241
+ <div id="app">` : "";
211
242
  return `<!DOCTYPE html>
212
243
  <html lang="${lang}"${dir ? ` dir="${dir}"` : ""}>
213
244
  <head>
214
245
  <meta charset="UTF-8">
215
246
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
216
- <title>RoseTack</title>
217
- <style>${STYLES_MIN}</style>${styleTag}
247
+ <title>RoseTack</title>${demoStyles}${styleTag}
218
248
  ${FAVICON}
219
249
  </head>
220
- <body>
221
- <div id="app">`;
250
+ <body>${appOpen}`;
222
251
  }
223
- function buildShell(ssrHtml, stateJson, client, head = [], styles = getStyles(), js = true, lang = "en", dir = "", nonce = "") {
252
+ function buildShell(ssrHtml, stateJson, client, head = [], styles = getStyles(), js = true, lang = "en", dir = "", nonce = "", shell = true) {
224
253
  const clientTag = clientScriptTag(client, nonce);
225
254
  const mergedHead = mergeHeadBlocks(head);
255
+ const headStamped = nonce ? mergedHead.replace(/<script\b([^>]*)>/gi, (m, attrs) => /\bnonce=/.test(attrs) ? m : `<script${attrs} nonce="${nonce}">`) : mergedHead;
256
+ let htmlAttrs = ` lang="${lang}"${dir ? ` dir="${dir}"` : ""}`;
257
+ let bodyAttrs = "";
258
+ const headClean = headStamped.replace(/<(html|body)\b([^>]*)>/gi, (_m, tag, attrs) => {
259
+ const clean = attrs.replace(/\s*data-rosetack-head="[^"]*"/, "");
260
+ if (tag === "html") htmlAttrs = clean;
261
+ else bodyAttrs = clean;
262
+ return "";
263
+ });
226
264
  const titleTag = /<title>/i.test(mergedHead) ? "" : "<title>RoseTack</title>";
227
265
  const iconTag = /rel=["']?icon/i.test(mergedHead) ? "" : `
228
266
  ${FAVICON}`;
229
- const headTags = mergedHead ? `
230
- ${mergedHead}` : "";
267
+ const headTags = headClean ? `
268
+ ${headClean}` : "";
231
269
  const styleTag = styles ? `
232
270
  <style>${styles}</style>` : "";
271
+ const stateSafe = stateJson.replace(/</g, "\\u003c");
233
272
  const jsTail = js ? `
234
- <script type="application/json" id="__rosetack_state">${stateJson}</script>
273
+ <script type="application/json" id="__rosetack_state">${stateSafe}</script>
235
274
  ${clientTag}` : "";
236
- const open = js ? '<div id="app">' : "";
237
- const close = js ? "</div>" : "";
238
- const shellStyles = js ? `
275
+ const open = js && shell ? '<div id="app">' : "";
276
+ const close = js && shell ? "</div>" : "";
277
+ const shellStyles = js && shell ? `
239
278
  <style>${STYLES_MIN}</style>` : "";
240
279
  return `<!DOCTYPE html>
241
- <html lang="${lang}"${dir ? ` dir="${dir}"` : ""}>
280
+ <html${htmlAttrs}>
242
281
  <head>
243
282
  <meta charset="UTF-8">
244
283
  <meta name="viewport" content="width=device-width, initial-scale=1.0">${headTags}${iconTag}
245
284
  ${titleTag}${shellStyles}${styleTag}
246
285
  </head>
247
- <body>
286
+ <body${bodyAttrs}>
248
287
  ${open}${ssrHtml}${close}${jsTail}
249
288
  </body>
250
289
  </html>`;
251
290
  }
252
291
 
253
292
  // src/cli/edge.ts
254
- async function createEdgeHandler(outDir) {
255
- const mod = await import(pathToFileURL(join2(outDir, "server.js")).href);
293
+ function crossSiteWrite(request) {
294
+ const m = request.method;
295
+ if (m !== "POST" && m !== "PUT" && m !== "PATCH" && m !== "DELETE") return null;
296
+ const origin = request.headers.get("origin");
297
+ if (origin) {
298
+ let host;
299
+ try {
300
+ host = new URL(origin).host;
301
+ } catch {
302
+ return new Response("Cross-site request blocked (unparsable Origin header)", { status: 403 });
303
+ }
304
+ return host === new URL(request.url).host ? null : new Response("Cross-site request blocked (Origin does not match Host)", { status: 403 });
305
+ }
306
+ const site = request.headers.get("sec-fetch-site");
307
+ if (site && site !== "same-origin" && site !== "same-site" && site !== "none") {
308
+ return new Response(`Cross-site request blocked (sec-fetch-site: ${site})`, { status: 403 });
309
+ }
310
+ return null;
311
+ }
312
+ async function createEdgeHandler(outDir, preloaded) {
313
+ const mod = preloaded?.server ?? await import(pathToFileURL(join2(outDir, "server.js")).href);
256
314
  let client = null;
257
315
  let styles = "";
258
- try {
259
- client = await (await import("fs/promises")).readFile(join2(outDir, "client.js"), "utf-8");
260
- } catch {
261
- client = null;
316
+ if (preloaded && "client" in preloaded) {
317
+ client = preloaded.client ?? null;
318
+ } else {
319
+ try {
320
+ client = await (await import("fs/promises")).readFile(join2(outDir, "client.js"), "utf-8");
321
+ } catch {
322
+ client = null;
323
+ }
262
324
  }
263
- try {
264
- styles = await (await import("fs/promises")).readFile(join2(outDir, "styles.css"), "utf-8");
265
- } catch {
266
- styles = "";
325
+ if (preloaded && "styles" in preloaded) {
326
+ styles = preloaded.styles ?? "";
327
+ } else {
328
+ try {
329
+ styles = await (await import("fs/promises")).readFile(join2(outDir, "styles.css"), "utf-8");
330
+ } catch {
331
+ styles = "";
332
+ }
267
333
  }
268
334
  let broken = /* @__PURE__ */ new Set();
269
- try {
270
- broken = new Set(JSON.parse(await (await import("fs/promises")).readFile(join2(outDir, "broken.json"), "utf-8")));
271
- } catch {
272
- broken = /* @__PURE__ */ new Set();
335
+ if (preloaded && "broken" in preloaded) {
336
+ broken = new Set(preloaded.broken);
337
+ } else {
338
+ try {
339
+ broken = new Set(JSON.parse(await (await import("fs/promises")).readFile(join2(outDir, "broken.json"), "utf-8")));
340
+ } catch {
341
+ broken = /* @__PURE__ */ new Set();
342
+ }
273
343
  }
274
344
  const pageHeaders = (pathname, nonce = "") => {
275
345
  const headers = {};
@@ -279,7 +349,13 @@ async function createEdgeHandler(outDir) {
279
349
  return headers;
280
350
  };
281
351
  return async function handle(request) {
352
+ const refused = crossSiteWrite(request);
353
+ if (refused) return refused;
282
354
  const { pathname, searchParams } = new URL(request.url);
355
+ const canonical = pathname.replace(/\/+$/, "") || "/";
356
+ if (canonical !== pathname) {
357
+ return new Response(null, { status: 308, headers: { location: canonical + new URL(request.url).search } });
358
+ }
283
359
  mod.setQuery?.(Object.fromEntries(searchParams));
284
360
  const hookCtx = mod.hasRequestHooks || mod.hasResponseHooks ? { request, url: new URL(request.url), method: request.method, pathname, state: {} } : null;
285
361
  if (hookCtx) {
@@ -309,7 +385,7 @@ async function createEdgeHandler(outDir) {
309
385
  await mod.renderPageStream(
310
386
  pathname,
311
387
  (chunk) => controller.enqueue(encoder.encode(chunk)),
312
- shellOpen(styles, doc.lang, doc.dir),
388
+ shellOpen(styles, doc.lang, doc.dir, !mod.shellOff?.(pathname)),
313
389
  clientScriptTag(client, nonce2)
314
390
  );
315
391
  controller.close();
@@ -319,7 +395,7 @@ async function createEdgeHandler(outDir) {
319
395
  }
320
396
  const page = await mod.renderPage(pathname, form);
321
397
  const nonce = mod.cspNonce?.(pathname) ? mintNonce() : "";
322
- const html = buildShell(page.html, page.state, client, page.head, styles, page.csr !== false, page.lang, page.dir, nonce);
398
+ const html = buildShell(page.html, page.state, client, page.head, styles, page.csr !== false, page.lang, page.dir, nonce, page.shell !== false);
323
399
  const buffered = new Response(html, {
324
400
  status: page.status ?? 200,
325
401
  headers: { "content-type": "text/html; charset=utf-8", ...pageHeaders(pathname, nonce) }