rosetack 0.1.0 → 0.1.2
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.
- package/deploy/k8s/rosetack.yaml +107 -0
- package/dist-cli/compiler.mjs +2783 -0
- package/dist-cli/edge.mjs +332 -0
- package/dist-cli/rosetack.mjs +4230 -0
- package/dist-cli/runtime.mjs +703 -0
- package/dist-cli/test.mjs +3202 -0
- package/pack.mjs +49 -0
- package/package.json +25 -7
- package/src/FRAMEWORK_VERSION +1 -1
- package/src/cli/edge.ts +67 -25
- package/src/cli/index.ts +858 -94
- package/src/cli/prompt.ts +127 -0
- package/src/cli/shell.ts +133 -30
- package/src/cli/test.ts +400 -0
- package/src/compiler/errors.ts +74 -0
- package/src/compiler/index.ts +1955 -264
- package/src/runtime/index.ts +465 -66
- package/src/types/rosetack.d.ts +151 -0
- package/.github/workflows/ci.yml +0 -72
- package/.github/workflows/release.yml +0 -122
- package/benchmark.ts +0 -227
- package/example/public/favicon.ico +0 -0
- package/example/public/logo.svg +0 -148
- package/example/public/rose-mark-black.png +0 -0
- package/example/public/rose-mark.png +0 -0
- package/example/public/rose-mono.png +0 -0
- package/example/rosetack.config.js +0 -10
- package/example/src/locales/en.json +0 -8
- package/example/src/locales/zh.json +0 -8
- package/example/src/pages/404.rose +0 -15
- package/example/src/pages/500.rose +0 -10
- package/example/src/pages/[lang]/about.rose +0 -40
- package/example/src/pages/_layout.rose +0 -33
- package/example/src/pages/_middleware.rose +0 -37
- package/example/src/pages/about.rose +0 -67
- package/example/src/pages/admin.rose +0 -27
- package/example/src/pages/api/hello.rose +0 -19
- package/example/src/pages/api/login.rose +0 -55
- package/example/src/pages/api/posts.rose +0 -80
- package/example/src/pages/api/secret.rose +0 -14
- package/example/src/pages/blog/[id].rose +0 -25
- package/example/src/pages/broken.rose +0 -16
- package/example/src/pages/fresh.rose +0 -22
- package/example/src/pages/garden.rose +0 -27
- package/example/src/pages/index.rose +0 -84
- package/example/src/pages/sign.rose +0 -45
- package/example/src/pages/thorn.rose +0 -23
- package/example/src/pages/vault.rose +0 -14
- package/go.mod +0 -3
- package/main.go +0 -470
- package/tools/release-notes.mjs +0 -113
- package/tsconfig.json +0 -17
package/pack.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pack.mjs - build the PUBLISHED package: the CLI and its library entries as
|
|
3
|
+
* self-contained ESM bundles, so `npm i -g rosetack` needs nothing but Node.
|
|
4
|
+
*
|
|
5
|
+
* Why a script and not a shell one-liner: the bin needs a shebang, and '#'
|
|
6
|
+
* is a comment character in two of the three shells npm might run a script
|
|
7
|
+
* through. esbuild's JS API takes it as data instead of as shell text.
|
|
8
|
+
*
|
|
9
|
+
* esbuild stays EXTERNAL: the compiler drives it at build time (it bundles
|
|
10
|
+
* the app), and its platform binary resolves relative to its own package -
|
|
11
|
+
* bundling it here would break that. It is a runtime dependency for exactly
|
|
12
|
+
* this reason.
|
|
13
|
+
*
|
|
14
|
+
* The bundles resolve two things relative to themselves (src/runtime for the
|
|
15
|
+
* app's bundles, package.json for the version banner), so src/ ships in the
|
|
16
|
+
* tarball beside dist-cli/ - see "files" in package.json.
|
|
17
|
+
*/
|
|
18
|
+
import { build } from 'esbuild';
|
|
19
|
+
import * as fs from 'node:fs';
|
|
20
|
+
|
|
21
|
+
const OUT = 'dist-cli';
|
|
22
|
+
fs.rmSync(OUT, { recursive: true, force: true });
|
|
23
|
+
fs.mkdirSync(OUT, { recursive: true });
|
|
24
|
+
|
|
25
|
+
const entries = [
|
|
26
|
+
// [source, output, is-the-bin]
|
|
27
|
+
['src/cli/index.ts', `${OUT}/rosetack.mjs`, true],
|
|
28
|
+
['src/cli/edge.ts', `${OUT}/edge.mjs`, false],
|
|
29
|
+
['src/cli/test.ts', `${OUT}/test.mjs`, false],
|
|
30
|
+
['src/runtime/index.ts', `${OUT}/runtime.mjs`, false],
|
|
31
|
+
['src/compiler/index.ts', `${OUT}/compiler.mjs`, false],
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
for (const [entry, outfile, isBin] of entries) {
|
|
35
|
+
await build({
|
|
36
|
+
entryPoints: [entry],
|
|
37
|
+
outfile,
|
|
38
|
+
bundle: true,
|
|
39
|
+
platform: 'node',
|
|
40
|
+
format: 'esm',
|
|
41
|
+
target: 'node22',
|
|
42
|
+
external: ['esbuild'],
|
|
43
|
+
banner: isBin ? { js: '#!/usr/bin/env node' } : {},
|
|
44
|
+
logLevel: 'warning',
|
|
45
|
+
});
|
|
46
|
+
const { size } = fs.statSync(outfile);
|
|
47
|
+
console.log(`packed ${outfile.padEnd(22)} ${(size / 1024).toFixed(1).padStart(6)} KB <- ${entry}`);
|
|
48
|
+
}
|
|
49
|
+
console.log(`\n${entries.length} bundles in ${OUT}/ - the tarball also ships src/ (the runtime source the compiler feeds esbuild), deploy/, and the docs`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rosetack",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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": {
|
|
@@ -9,14 +9,32 @@
|
|
|
9
9
|
"preview": "tsx src/cli/index.ts preview example",
|
|
10
10
|
"serve": "tsx src/cli/index.ts serve example",
|
|
11
11
|
"release:notes": "node tools/release-notes.mjs",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"test:cli": "node test-cli.mjs",
|
|
15
|
-
"test:e2e": "tsx test-e2e.ts",
|
|
16
|
-
"test:edge": "tsx test-edge.ts",
|
|
17
|
-
"test:go": "tsx src/cli/index.ts build example && node test-go.mjs",
|
|
12
|
+
"pack": "node pack.mjs",
|
|
13
|
+
"prepublishOnly": "npm run pack",
|
|
18
14
|
"build:go": "go build -o rosetack-server ."
|
|
19
15
|
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"rosetack": "dist-cli/rosetack.mjs"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
"./edge": "./dist-cli/edge.mjs",
|
|
21
|
+
"./runtime": "./dist-cli/runtime.mjs",
|
|
22
|
+
"./compiler": "./dist-cli/compiler.mjs",
|
|
23
|
+
"./test": "./dist-cli/test.mjs"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist-cli",
|
|
27
|
+
"src",
|
|
28
|
+
"deploy",
|
|
29
|
+
"pack.mjs",
|
|
30
|
+
"README.md",
|
|
31
|
+
"VERSIONING.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=22"
|
|
37
|
+
},
|
|
20
38
|
"dependencies": {
|
|
21
39
|
"esbuild": "^0.24.0"
|
|
22
40
|
},
|
package/src/FRAMEWORK_VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.2
|
package/src/cli/edge.ts
CHANGED
|
@@ -19,25 +19,42 @@
|
|
|
19
19
|
|
|
20
20
|
import { pathToFileURL } from 'url';
|
|
21
21
|
import { join } from 'path';
|
|
22
|
-
import { buildShell, shellOpen, clientScriptTag, securityHeaders } from './shell.js';
|
|
22
|
+
import { buildShell, shellOpen, clientScriptTag, securityHeaders, mintNonce } from './shell.js';
|
|
23
23
|
|
|
24
24
|
type ServerModule = {
|
|
25
|
-
renderPage(pathname: string, form?: FormData): Promise<{ html: string; state: string; head: string[]; status?: number; csr?: boolean }>;
|
|
25
|
+
renderPage(pathname: string, form?: FormData): Promise<{ html: string; state: string; head: string[]; status?: number; csr?: boolean; lang?: string; dir?: string }>;
|
|
26
26
|
renderPageStream(pathname: string, write: (chunk: string) => void, shellOpen: string, clientTag: string): Promise<number>;
|
|
27
27
|
canStream(pathname: string): boolean;
|
|
28
28
|
isNoJs(pathname: string): boolean;
|
|
29
|
+
/** P0-2: routes that exported `buffer = true` - buffered, never streamed */
|
|
30
|
+
isBuffered(pathname: string): boolean;
|
|
29
31
|
isApi(pathname: string): boolean;
|
|
30
32
|
handleApi(method: string, pathname: string, request: Request): Promise<Response>;
|
|
33
|
+
/** The document's <html lang>/<dir> for a pathname (streaming needs them before the render) */
|
|
34
|
+
docAttrs(pathname: string): { lang: string; dir: string };
|
|
31
35
|
/** pages/_middleware.rose's handler, or null when the project has none */
|
|
32
36
|
middleware: ((request: Request) => Promise<Response | void | null>) | null;
|
|
33
37
|
/** run the middleware once per request; returns its short-circuit Response or null */
|
|
34
38
|
runMiddleware(rawReq: any, form?: FormData): Promise<Response | null>;
|
|
39
|
+
/** The runtime plugin hooks, and whether the project declares any */
|
|
40
|
+
runRequestHooks(ctx: {
|
|
41
|
+
request: Request; url: URL; method: string; pathname: string; state: Record<string, unknown>;
|
|
42
|
+
}): Promise<Response | null>;
|
|
43
|
+
runResponseHooks(ctx: {
|
|
44
|
+
request: Request; url: URL; method: string; pathname: string; state: Record<string, unknown>;
|
|
45
|
+
}, res: Response): Promise<Response>;
|
|
46
|
+
hasRequestHooks: boolean;
|
|
47
|
+
hasResponseHooks: boolean;
|
|
35
48
|
/** routes whose component reads getContext(): never prerendered, always live */
|
|
36
49
|
dynamicRoutes: string[];
|
|
37
|
-
|
|
50
|
+
/** per-route response headers [] when no route exports any */
|
|
38
51
|
routeHeaders: Array<{ pattern: string; headers: Record<string, string> }>;
|
|
39
52
|
/** the route's own exported headers for a pathname, or null */
|
|
40
53
|
headersFor(pathname: string): Record<string, string> | null;
|
|
54
|
+
/** Routes that exported `csp = { nonce: true }` - nonce policy, never baked */
|
|
55
|
+
cspNonce(pathname: string): boolean;
|
|
56
|
+
/** P0-1: the query of the request being rendered, for $query() */
|
|
57
|
+
setQuery(query: Record<string, string>): void;
|
|
41
58
|
};
|
|
42
59
|
|
|
43
60
|
/**
|
|
@@ -66,62 +83,87 @@ export async function createEdgeHandler(outDir: string): Promise<(request: Reque
|
|
|
66
83
|
broken = new Set(); // no manifest: nothing known-broken
|
|
67
84
|
}
|
|
68
85
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const pageHeaders = (pathname: string): Record<string, string> => {
|
|
86
|
+
// Response headers for a page document: the strict CSP hashed over the exact inlined bytes …
|
|
87
|
+
const pageHeaders = (pathname: string, nonce = ''): Record<string, string> => {
|
|
72
88
|
const headers: Record<string, string> = {};
|
|
73
|
-
if (client) for (const [k, v] of Object.entries(securityHeaders(client))) headers[k.toLowerCase()] = v;
|
|
89
|
+
if (client) for (const [k, v] of Object.entries(securityHeaders(client, nonce))) headers[k.toLowerCase()] = v;
|
|
74
90
|
const own = mod.headersFor?.(pathname);
|
|
75
91
|
if (own) for (const [k, v] of Object.entries(own)) headers[k.toLowerCase()] = v;
|
|
76
92
|
return headers;
|
|
77
93
|
};
|
|
78
94
|
|
|
79
95
|
return async function handle(request: Request): Promise<Response> {
|
|
80
|
-
const { pathname } = new URL(request.url);
|
|
96
|
+
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.
|
|
99
|
+
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
|
+
const hookCtx = mod.hasRequestHooks || mod.hasResponseHooks
|
|
103
|
+
? { request, url: new URL(request.url), method: request.method, pathname, state: {} as Record<string, unknown> }
|
|
104
|
+
: null;
|
|
105
|
+
if (hookCtx) {
|
|
106
|
+
const short = await mod.runRequestHooks(hookCtx);
|
|
107
|
+
if (short) return mod.runResponseHooks(hookCtx, short);
|
|
108
|
+
}
|
|
81
109
|
// Middleware (pages/_middleware.rose) runs exactly once per request, before anything
|
|
82
110
|
// else: a returned Response short-circuits the whole request (a redirect, an auth wall) …
|
|
83
111
|
if (mod.middleware) {
|
|
84
112
|
const mw = await mod.runMiddleware(request);
|
|
85
|
-
if (mw) return mw;
|
|
113
|
+
if (mw) return hookCtx ? mod.runResponseHooks(hookCtx, mw) : mw;
|
|
86
114
|
}
|
|
87
115
|
// API routes (pages/api/*.rose): the /api namespace answers JSON, never the HTML shell …
|
|
88
116
|
if (mod.isApi(pathname)) {
|
|
89
|
-
|
|
117
|
+
const apiRes = await mod.handleApi(request.method, pathname, request);
|
|
118
|
+
return hookCtx ? mod.runResponseHooks(hookCtx, apiRes) : apiRes;
|
|
90
119
|
}
|
|
91
120
|
// POST: progressive-enhancement form - run the route's server action with
|
|
92
121
|
// the submitted FormData and re-render the page (works without JS).
|
|
93
122
|
const form = request.method === 'POST' ? await request.formData() : undefined;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (!form && !broken.has(pathname) && mod.canStream(pathname) && !mod.isNoJs(pathname)) {
|
|
97
|
-
|
|
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
|
+
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
|
+
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
|
+
const streamed = new Response(null, {
|
|
132
|
+
status: 200,
|
|
133
|
+
headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname, nonce) }
|
|
134
|
+
});
|
|
135
|
+
const head = hookCtx ? await mod.runResponseHooks(hookCtx, streamed) : streamed;
|
|
98
136
|
const encoder = new TextEncoder();
|
|
99
137
|
const stream = new ReadableStream<Uint8Array>({
|
|
100
138
|
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
|
+
const doc = mod.docAttrs(pathname);
|
|
101
142
|
await mod.renderPageStream(
|
|
102
143
|
pathname,
|
|
103
144
|
(chunk) => controller.enqueue(encoder.encode(chunk)),
|
|
104
|
-
shellOpen(styles),
|
|
105
|
-
clientScriptTag(client)
|
|
145
|
+
shellOpen(styles, doc.lang, doc.dir),
|
|
146
|
+
clientScriptTag(client, nonce)
|
|
106
147
|
);
|
|
107
148
|
controller.close();
|
|
108
149
|
}
|
|
109
150
|
});
|
|
110
|
-
return new Response(stream, {
|
|
111
|
-
status: 200,
|
|
112
|
-
headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname) }
|
|
113
|
-
});
|
|
151
|
+
return new Response(stream, { status: head.status, headers: head.headers });
|
|
114
152
|
}
|
|
115
153
|
// buffered: POST actions, unmatched routes (404), known-broken routes (500).
|
|
116
154
|
// The middleware already ran at the top of this handler, so the render
|
|
117
155
|
// below sees its context.
|
|
118
156
|
const page = await mod.renderPage(pathname, form);
|
|
119
|
-
|
|
120
|
-
//
|
|
121
|
-
const
|
|
122
|
-
|
|
157
|
+
// '' for every hashed route, so the document is byte-identical to
|
|
158
|
+
// what it was before nonce mode existed.
|
|
159
|
+
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);
|
|
163
|
+
const buffered = new Response(html, {
|
|
123
164
|
status: page.status ?? 200,
|
|
124
|
-
headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname) }
|
|
165
|
+
headers: { 'content-type': 'text/html; charset=utf-8', ...pageHeaders(pathname, nonce) }
|
|
125
166
|
});
|
|
167
|
+
return hookCtx ? mod.runResponseHooks(hookCtx, buffered) : buffered;
|
|
126
168
|
};
|
|
127
169
|
}
|