rosetack 0.0.0 → 0.1.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 (50) hide show
  1. package/.github/workflows/ci.yml +72 -0
  2. package/.github/workflows/release.yml +122 -0
  3. package/LICENSE +21 -0
  4. package/benchmark.ts +227 -0
  5. package/deploy/Dockerfile.node +25 -0
  6. package/deploy/Dockerfile.static +34 -0
  7. package/deploy/api/rosetack.ts +29 -0
  8. package/deploy/docker-compose.yml +33 -0
  9. package/deploy/fly.toml +28 -0
  10. package/deploy/vercel.json +13 -0
  11. package/example/public/favicon.ico +0 -0
  12. package/example/public/logo.svg +148 -0
  13. package/example/public/rose-mark-black.png +0 -0
  14. package/example/public/rose-mark.png +0 -0
  15. package/example/public/rose-mono.png +0 -0
  16. package/example/rosetack.config.js +10 -0
  17. package/example/src/locales/en.json +8 -0
  18. package/example/src/locales/zh.json +8 -0
  19. package/example/src/pages/404.rose +15 -0
  20. package/example/src/pages/500.rose +10 -0
  21. package/example/src/pages/[lang]/about.rose +40 -0
  22. package/example/src/pages/_layout.rose +33 -0
  23. package/example/src/pages/_middleware.rose +37 -0
  24. package/example/src/pages/about.rose +67 -0
  25. package/example/src/pages/admin.rose +27 -0
  26. package/example/src/pages/api/hello.rose +19 -0
  27. package/example/src/pages/api/login.rose +55 -0
  28. package/example/src/pages/api/posts.rose +80 -0
  29. package/example/src/pages/api/secret.rose +14 -0
  30. package/example/src/pages/blog/[id].rose +25 -0
  31. package/example/src/pages/broken.rose +16 -0
  32. package/example/src/pages/fresh.rose +22 -0
  33. package/example/src/pages/garden.rose +27 -0
  34. package/example/src/pages/index.rose +84 -0
  35. package/example/src/pages/sign.rose +45 -0
  36. package/example/src/pages/thorn.rose +23 -0
  37. package/example/src/pages/vault.rose +14 -0
  38. package/go.mod +3 -0
  39. package/main.go +470 -0
  40. package/package.json +32 -11
  41. package/src/FRAMEWORK_VERSION +1 -0
  42. package/src/cli/edge.ts +127 -0
  43. package/src/cli/index.ts +777 -0
  44. package/src/cli/shell.ts +284 -0
  45. package/src/compiler/index.ts +1990 -0
  46. package/src/runtime/index.ts +624 -0
  47. package/tools/release-notes.mjs +113 -0
  48. package/tsconfig.json +17 -0
  49. package/README.md +0 -3
  50. package/index.js +0 -12
@@ -0,0 +1,80 @@
1
+ <script>
2
+ import { $cookies } from './runtime.js';
3
+
4
+ // The blog's backstage skeleton (Phase 2): a real database behind the API.
5
+ // node:sqlite ships inside Node 22+ - zero dependencies, zero native
6
+ // modules, zero build steps. A Postgres deploy swaps this block for a
7
+ // pg.Pool (same shape: prepare/run/all), nothing else in the file moves.
8
+
9
+ // The connection opens on first use, never at module load:
10
+ // 1. node:sqlite is imported LAZILY - a static import would put it in the
11
+ // server bundle, and the edge adapter loads that same bundle, so the
12
+ // whole app would fail to boot on an edge runtime instead of just
13
+ // this route (Node caches the module: one microtask per call).
14
+ // 2. no side effect at build time: `rosetack build` imports the server
15
+ // bundle (route headers, prerender) and must not create a database.
16
+ // ponytail: one connection per worker process - that IS the pool; a real
17
+ // deploy replaces it with pg.Pool right here. The file is relative to the
18
+ // working directory you started the server from (your project root), never
19
+ // inside dist/: dist is a build artifact and the Go single binary embeds
20
+ // it verbatim - a database in dist would ship inside the binary.
21
+ let conn;
22
+ const getDb = async () => {
23
+ if (!conn) {
24
+ const { DatabaseSync } = await import('node:sqlite');
25
+ conn = new DatabaseSync('blog.db');
26
+ conn.exec(`
27
+ CREATE TABLE IF NOT EXISTS posts (
28
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
29
+ title TEXT NOT NULL,
30
+ body TEXT NOT NULL,
31
+ author TEXT NOT NULL DEFAULT 'anonymous'
32
+ );
33
+ INSERT OR IGNORE INTO posts (id, title, body, author) VALUES
34
+ (1, 'Hello RoseTack', 'One request, zero hydration - and a database too.', 'ada'),
35
+ (2, 'The dynamic half', 'Every route keeps the single-request contract, DB or not.', 'grace');
36
+ `);
37
+ }
38
+ return conn;
39
+ };
40
+
41
+ // The demo's single author: session cookie value '1' is the users row id
42
+ // (see api/login.rose). A real app stores a signed token and verifies it;
43
+ // the cookie helpers (#35) build the cookie either way.
44
+ const signedIn = (req) => $cookies(req).session === '1';
45
+
46
+ // API answers are JSON, errors included - never an HTML error page.
47
+ const json = (obj, status = 200) =>
48
+ new Response(JSON.stringify(obj), { status, headers: { 'content-type': 'application/json' } });
49
+
50
+ // Public read: the list, newest first. A page or a client calls this the
51
+ // same way; on the Node server it is one request with no hydration.
52
+ export async function GET() {
53
+ const db = await getDb();
54
+ return { posts: db.prepare('SELECT id, title, body, author FROM posts ORDER BY id DESC').all() };
55
+ }
56
+
57
+ // Create. Writes need the session: a missing or wrong cookie is a 401 with
58
+ // a JSON body, so a browser and a script get the same contract.
59
+ export async function POST(req) {
60
+ if (!signedIn(req)) return json({ error: 'sign in first' }, 401);
61
+ const form = await req.formData();
62
+ const title = String(form.get('title') ?? '').trim();
63
+ const body = String(form.get('body') ?? '').trim();
64
+ if (!title || !body) return json({ error: 'title and body are required' }, 400);
65
+ const db = await getDb();
66
+ const info = db.prepare('INSERT INTO posts (title, body, author) VALUES (?, ?, ?)').run(title, body, 'ada');
67
+ return json({ id: Number(info.lastInsertRowid) }, 201);
68
+ }
69
+
70
+ // Delete by ?id=. Same wall as POST - the CSRF guard (#35) already refuses
71
+ // cross-site writes before a handler runs.
72
+ export async function DELETE(req) {
73
+ if (!signedIn(req)) return json({ error: 'sign in first' }, 401);
74
+ const id = Number(new URL(req.url).searchParams.get('id'));
75
+ if (!id) return json({ error: 'id is required' }, 400);
76
+ const db = await getDb();
77
+ db.prepare('DELETE FROM posts WHERE id = ?').run(id);
78
+ return json({ deleted: id });
79
+ }
80
+ </script>
@@ -0,0 +1,14 @@
1
+ <script>
2
+ // The middleware guards the api namespace with the same contract: this
3
+ // handler answers 401 JSON unless the request carried a session.
4
+ export async function GET() {
5
+ const user = getContext().user;
6
+ if (!user) {
7
+ return new Response(JSON.stringify({ error: 'unauthorized' }), {
8
+ status: 401,
9
+ headers: { 'content-type': 'application/json' },
10
+ });
11
+ }
12
+ return { secret: 'the thorn is sharp', for: user.name };
13
+ }
14
+ </script>
@@ -0,0 +1,25 @@
1
+ <script>
2
+ export const params = { id: ['1', '2'] };
3
+
4
+ let id = $state('1');
5
+ let posts = $state({
6
+ '1': { title: 'Hello RoseTack', body: 'This is the first post on our revolutionary framework.' },
7
+ '2': { title: 'Zero-hydration magic', body: 'RoseTack resumes state without replaying events or re-rendering the DOM.' }
8
+ });
9
+ let post = $data(async () => posts()[id()] || { title: 'Not found', body: 'This post does not exist.' });
10
+ </script>
11
+
12
+ <head>
13
+ <title>{post().title} - RoseTack blog</title>
14
+ <meta name="description" content={post().body}>
15
+ </head>
16
+
17
+ <style>
18
+ :global(.rlogo) { height: 1em; width: 1em; vertical-align: -0.08em; margin-right: 0.2em; }
19
+ </style>
20
+
21
+ <div>
22
+ <h1>Blog Post #{id()}</h1>
23
+ <h2>{post().title}</h2>
24
+ <p>{post().body}</p>
25
+ </div>
@@ -0,0 +1,16 @@
1
+ <script>
2
+ // A deliberately broken page: its $data source throws, so the whole route
3
+ // render fails. The route-level error boundary (pages/500.rose) renders
4
+ // instead - HTTP 500 on the server, the same page on the client router -
5
+ // and every other route keeps working.
6
+ let post = $data(async () => {
7
+ throw new Error('data source is down');
8
+ });
9
+ </script>
10
+
11
+ <template>
12
+ <div>
13
+ <h1>Broken page</h1>
14
+ <p>{post().title}</p>
15
+ </div>
16
+ </template>
@@ -0,0 +1,22 @@
1
+ <script>
2
+ // ISR (innovation #24): the build bakes this page once, and the preview
3
+ // server keeps it fresh - serving the baked file for up to `revalidate`
4
+ // seconds, then answering the next request STALE (instant, from disk)
5
+ // while a background pass re-renders and swaps the file. Next.js calls
6
+ // this ISR; here it is one exported constant and zero configuration.
7
+ export const revalidate = 2; // seconds
8
+
9
+ // module-level: one per render pass, so the stamp proves which render
10
+ // produced the document you are reading
11
+ export const renders = { n: 0 };
12
+
13
+ let stamp = $data(async () => {
14
+ renders.n += 1;
15
+ return 'render #' + renders.n;
16
+ });
17
+ </script>
18
+
19
+ <h1>Fresh</h1>
20
+ <p>this document is {stamp()} - the baked file is at most 2 seconds old</p>
21
+ <p>the stamp advances without a rebuild: stale-while-revalidate</p>
22
+ <p><a href="/">Back to the garden</a></p>
@@ -0,0 +1,27 @@
1
+ <script>
2
+ // Pure content: no $state, no events, no lifecycle - and the document
3
+ // proves it. `csr = false` (innovation #25) keeps the client bundle OUT
4
+ // of this document entirely: no runtime, no state script, zero
5
+ // JavaScript for whoever lands here. The route's render function still
6
+ // rides in the bundle, so a client-side navigation from an interactive
7
+ // page paints it instantly and a static deploy's SPA fallback can
8
+ // render it too - only the DOCUMENT is JS-free.
9
+ export const csr = false;
10
+
11
+ // $data is server-side: it resolves during the render, on the baked and
12
+ // the on-demand path alike, with no client code involved.
13
+ let bloomed = $data(async () => 'in full bloom');
14
+ // The `@thorn` token is rewritten by the demo plugin (example/rosetack.config.js)
15
+ // before compilation - the source on disk keeps the token.
16
+ let mark = $data(async () => '@thorn');
17
+ </script>
18
+
19
+ <head>
20
+ <title>The Garden - RoseTack</title>
21
+ <meta name="description" content="A pure content page that ships zero JavaScript.">
22
+ </head>
23
+
24
+ <h1>The Garden</h1>
25
+ <p>This page is {bloomed()} {mark()} - and it shipped without a single byte of JavaScript.</p>
26
+ <p>No bundle, no state script, no hydration: the document is the whole cost, the way a static site would be - with the framework's data, layouts and head blocks still behind it.</p>
27
+ <p><a href="/">Back to the gate</a></p>
@@ -0,0 +1,84 @@
1
+ <script>
2
+ // Server-side store, hoisted to module scope: shared across requests, the
3
+ // action, and the page state below. (A real app would use a database.)
4
+ export const store = { likes: 41 };
5
+
6
+ let count = $state(0);
7
+ let name = $state('RoseTack');
8
+ let items = $state(['Performance', 'Simplicity', 'Zero-hydration']);
9
+ let showFeatures = $state(true);
10
+ let guests = $state(['the founder']);
11
+ let likes = $state(store.likes);
12
+
13
+ function increment() { $setState('count', count() + 1); }
14
+ function addItem() { $setState('items', [...items(), 'Item ' + items().length + 1]); }
15
+ function toggleFeatures() { $setState('showFeatures', !showFeatures()); }
16
+ // Zero-request form: no POST, no fetch - the action runs client-side and
17
+ // fine-grained reactivity repaints only what changed. (htmx-style stacks
18
+ // pay one server round-trip per submit.)
19
+ function signGuestbook(e) {
20
+ e.preventDefault();
21
+ const guest = String(new FormData(e.target).get('name') || '').trim();
22
+ if (guest) $setState('guests', [guest, ...guests()]);
23
+ e.target.reset();
24
+ }
25
+ // A widget whose data source is offline. Inside {#boundary} its throw is
26
+ // contained: only this section degrades, the rest of the page renders.
27
+ function weather() {
28
+ throw new Error('weather service unreachable');
29
+ }
30
+
31
+ // A named server action, reachable from any event handler:
32
+ // on:click={like} below compiles to $action('like') - one POST, one
33
+ // request, in-place adopt, fine-grained patch. Server-only: this body
34
+ // never ships to the client bundle (only forms work without JS).
35
+ export async function like() {
36
+ store.likes += 1;
37
+ return { likes: store.likes };
38
+ }
39
+ </script>
40
+
41
+ <style>
42
+ .rcard { background: #fff0f5; border-left: 4px solid #e91e63; padding: 0.75rem 1rem; border-radius: 0 6px 6px 0; }
43
+ </style>
44
+
45
+ <div>
46
+ <h1>Welcome to {name()} 🌹</h1>
47
+ <p>Count: <strong>{count()}</strong></p>
48
+ <button on:click={increment}>Increment</button>
49
+ <button on:click={toggleFeatures}>{showFeatures() ? 'Hide' : 'Show'} Features</button>
50
+
51
+ {#if showFeatures()}
52
+ <h2>Features</h2>
53
+ <ul>
54
+ {#each items() as item}
55
+ <li>{item}</li>
56
+ {/each}
57
+ </ul>
58
+ <button on:click={addItem}>Add Item</button>
59
+ {/if}
60
+
61
+ <h2>Guestbook</h2>
62
+ <form on:submit={signGuestbook}>
63
+ <input name="name" placeholder="Your name" aria-label="Guestbook name">
64
+ <button type="submit">Sign</button>
65
+ </form>
66
+ <ul>
67
+ {#each guests() as guest}
68
+ <li>🌹 {guest}</li>
69
+ {/each}
70
+ </ul>
71
+
72
+ <h2>Likes</h2>
73
+ <div class="rcard">
74
+ <p>🌹 <strong>{likes()}</strong> likes</p>
75
+ <button on:click={like}>Like</button>
76
+ </div>
77
+
78
+ {#boundary}
79
+ <section>
80
+ <h2>Weather</h2>
81
+ <p>{weather()}</p>
82
+ </section>
83
+ {/boundary}
84
+ </div>
@@ -0,0 +1,45 @@
1
+ <script>
2
+ // Server-side store. A real app would read/write a database here; $store
3
+ // is what makes it shared across requests AND across cluster workers
4
+ // (Phase 2): the write broadcasts to every worker, reads stay local, and
5
+ // the action below and the page state see the same box. A plain
6
+ // module-scope object would live in exactly one worker - signatures
7
+ // would appear and vanish depending on which core answered.
8
+ // (exported: the action is hoisted to module scope, so the store must
9
+ // live there too - both sides of the component share this one box.)
10
+ export const store = $store('guests', ['Ada Lovelace', 'Grace Hopper']);
11
+
12
+ // Server action for the form below. Progressive enhancement:
13
+ // no JS -> the browser POSTs natively, this runs on the server and the
14
+ // response re-renders the page (Server Actions elsewhere need JS)
15
+ // with JS-> the bootstrap intercepts the submit, POSTs via fetch, and
16
+ // adopts the response in place: one request, no reload.
17
+ // The returned patch seeds this render's state.
18
+ export async function action(form) {
19
+ const name = String(form.get('name') ?? '').trim();
20
+ if (name) store.update((g) => [name, ...g]);
21
+ return { guests: [...store.get()] };
22
+ }
23
+
24
+ // The page renders from state; the action's patch replaces it per request.
25
+ let guests = $state(store.get());
26
+ </script>
27
+
28
+ <template>
29
+ <div>
30
+ <h1>Guestbook 🌹</h1>
31
+ <p>Sign the book. This form works with JavaScript disabled - and with
32
+ JavaScript it submits without a page reload.</p>
33
+ <form method="POST">
34
+ <input name="name" placeholder="Your name" aria-label="Guestbook name" required>
35
+ <button type="submit">Sign</button>
36
+ </form>
37
+ <h2>Signatures</h2>
38
+ <ul>
39
+ {#each guests() as guest}
40
+ <li>🌹 {guest}</li>
41
+ {/each}
42
+ </ul>
43
+ <p><a href="/">Back to the garden</a></p>
44
+ </div>
45
+ </template>
@@ -0,0 +1,23 @@
1
+ <script>
2
+ // The thorn: a page that ships zero JavaScript can also ship a
3
+ // zero-script policy. `csr = false` (innovation #25) keeps the bundle
4
+ // and the bootstrap out of this document, and `headers` (innovation #26)
5
+ // replaces the framework defaults with a CSP that says exactly that -
6
+ // stricter than any other page in the framework, and it still renders
7
+ // perfectly, because there is nothing left to allow.
8
+ export const csr = false;
9
+ export const headers = {
10
+ 'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; img-src data:",
11
+ 'x-robots-tag': 'noindex',
12
+ };
13
+ </script>
14
+
15
+ <head>
16
+ <title>The Thorn - RoseTack</title>
17
+ <meta name="description" content="A no-JS page with a no-script policy.">
18
+ </head>
19
+
20
+ <h1>The Thorn</h1>
21
+ <p>Every other page in this framework carries a strict CSP hashed over its exact inlined bytes. This one carries <code>default-src 'none'</code> - no scripts, no connections, no frames - and nothing breaks, because <code>csr = false</code> already kept every script out of the document.</p>
22
+ <p>The policy is per route: an app tightens or relaxes the defaults exactly where it needs to, and the servers (Node, edge, and the Go single binary, which reads the same rules from <code>dist/headers.json</code>) all send it.</p>
23
+ <p><a href="/">Back to the gate</a></p>
@@ -0,0 +1,14 @@
1
+ <script>
2
+ // The plain wall: the gate lives entirely in pages/_middleware.rose -
3
+ // this page never touches the request context, so the build sees a
4
+ // cookie-less request as walled and never bakes it (no gated content in
5
+ // the static deploy). Contrast /admin, the deep wall: its content IS the
6
+ // secret, so it reads the context and renders per request even on the
7
+ // Go binary.
8
+ let thorn = $state('sharp');
9
+ </script>
10
+
11
+ <h1>The Vault</h1>
12
+ <p>The gate is the middleware - the page itself carries no auth code.</p>
13
+ <p>Every rose has its thorn: {thorn()}</p>
14
+ <p><a href="/">Back to the garden</a></p>
package/go.mod ADDED
@@ -0,0 +1,3 @@
1
+ module rosetack
2
+
3
+ go 1.27