@pyreon/server 0.12.0 → 0.12.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/lib/analysis/index.js.html +1 -1
- package/lib/index.js +68 -5
- package/lib/index.js.map +1 -1
- package/lib/types/index.d.ts +35 -1
- package/lib/types/index.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/handler.ts +30 -1
- package/src/index.ts +1 -0
- package/src/middleware.ts +42 -0
|
@@ -5386,7 +5386,7 @@ var drawChart = (function (exports) {
|
|
|
5386
5386
|
</script>
|
|
5387
5387
|
<script>
|
|
5388
5388
|
/*<!--*/
|
|
5389
|
-
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"home/runner/work/pyreon/pyreon/packages/core/head/lib/ssr.js","uid":"
|
|
5389
|
+
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"home/runner/work/pyreon/pyreon/packages/core/head/lib/ssr.js","uid":"dc208be0-1"},{"name":"src","children":[{"uid":"dc208be0-3","name":"html.ts"},{"uid":"dc208be0-5","name":"middleware.ts"},{"uid":"dc208be0-7","name":"handler.ts"},{"uid":"dc208be0-9","name":"island.ts"},{"uid":"dc208be0-11","name":"ssg.ts"},{"uid":"dc208be0-13","name":"index.ts"}]}]}],"isRoot":true},"nodeParts":{"dc208be0-1":{"renderedLength":2713,"gzipLength":1078,"brotliLength":0,"metaUid":"dc208be0-0"},"dc208be0-3":{"renderedLength":2797,"gzipLength":1166,"brotliLength":0,"metaUid":"dc208be0-2"},"dc208be0-5":{"renderedLength":1738,"gzipLength":835,"brotliLength":0,"metaUid":"dc208be0-4"},"dc208be0-7":{"renderedLength":3104,"gzipLength":1337,"brotliLength":0,"metaUid":"dc208be0-6"},"dc208be0-9":{"renderedLength":1444,"gzipLength":673,"brotliLength":0,"metaUid":"dc208be0-8"},"dc208be0-11":{"renderedLength":2806,"gzipLength":1214,"brotliLength":0,"metaUid":"dc208be0-10"},"dc208be0-13":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"metaUid":"dc208be0-12"}},"nodeMetas":{"dc208be0-0":{"id":"/home/runner/work/pyreon/pyreon/packages/core/head/lib/ssr.js","moduleParts":{"index.js":"dc208be0-1"},"imported":[{"uid":"dc208be0-14"},{"uid":"dc208be0-16"}],"importedBy":[{"uid":"dc208be0-6"}]},"dc208be0-2":{"id":"/src/html.ts","moduleParts":{"index.js":"dc208be0-3"},"imported":[],"importedBy":[{"uid":"dc208be0-12"},{"uid":"dc208be0-6"}]},"dc208be0-4":{"id":"/src/middleware.ts","moduleParts":{"index.js":"dc208be0-5"},"imported":[{"uid":"dc208be0-14"}],"importedBy":[{"uid":"dc208be0-12"},{"uid":"dc208be0-6"}]},"dc208be0-6":{"id":"/src/handler.ts","moduleParts":{"index.js":"dc208be0-7"},"imported":[{"uid":"dc208be0-14"},{"uid":"dc208be0-0"},{"uid":"dc208be0-15"},{"uid":"dc208be0-16"},{"uid":"dc208be0-2"},{"uid":"dc208be0-4"}],"importedBy":[{"uid":"dc208be0-12"}]},"dc208be0-8":{"id":"/src/island.ts","moduleParts":{"index.js":"dc208be0-9"},"imported":[{"uid":"dc208be0-14"}],"importedBy":[{"uid":"dc208be0-12"}]},"dc208be0-10":{"id":"/src/ssg.ts","moduleParts":{"index.js":"dc208be0-11"},"imported":[{"uid":"dc208be0-17"},{"uid":"dc208be0-18"}],"importedBy":[{"uid":"dc208be0-12"}]},"dc208be0-12":{"id":"/src/index.ts","moduleParts":{"index.js":"dc208be0-13"},"imported":[{"uid":"dc208be0-6"},{"uid":"dc208be0-2"},{"uid":"dc208be0-8"},{"uid":"dc208be0-4"},{"uid":"dc208be0-10"}],"importedBy":[],"isEntry":true},"dc208be0-14":{"id":"@pyreon/core","moduleParts":{},"imported":[],"importedBy":[{"uid":"dc208be0-6"},{"uid":"dc208be0-8"},{"uid":"dc208be0-4"},{"uid":"dc208be0-0"}]},"dc208be0-15":{"id":"@pyreon/router","moduleParts":{},"imported":[],"importedBy":[{"uid":"dc208be0-6"}]},"dc208be0-16":{"id":"@pyreon/runtime-server","moduleParts":{},"imported":[],"importedBy":[{"uid":"dc208be0-6"},{"uid":"dc208be0-0"}]},"dc208be0-17":{"id":"node:fs/promises","moduleParts":{},"imported":[],"importedBy":[{"uid":"dc208be0-10"}]},"dc208be0-18":{"id":"node:path","moduleParts":{},"imported":[],"importedBy":[{"uid":"dc208be0-10"}]}},"env":{"rollup":"4.23.0"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
|
|
5390
5390
|
|
|
5391
5391
|
const run = () => {
|
|
5392
5392
|
const width = window.innerWidth;
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, h, pushContext } from "@pyreon/core";
|
|
1
|
+
import { createContext, h, provide, pushContext, useContext } from "@pyreon/core";
|
|
2
2
|
import { renderToStream, renderToString, runWithRequestContext } from "@pyreon/runtime-server";
|
|
3
3
|
import { RouterProvider, createRouter, prefetchLoaderData, serializeLoaderData } from "@pyreon/router";
|
|
4
4
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
@@ -175,11 +175,71 @@ function buildScriptsFast(clientEntryTag, loaderData) {
|
|
|
175
175
|
return clientEntryTag;
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/middleware.ts
|
|
180
|
+
/**
|
|
181
|
+
* SSR middleware — simple request processing pipeline.
|
|
182
|
+
*
|
|
183
|
+
* Middleware runs before rendering. Return a Response to short-circuit
|
|
184
|
+
* (e.g. for redirects, auth checks, or static file serving).
|
|
185
|
+
* Return void / undefined to continue to the next middleware or rendering.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* const authMiddleware: Middleware = async (ctx) => {
|
|
189
|
+
* const token = ctx.req.headers.get("Authorization")
|
|
190
|
+
* if (!token) return new Response("Unauthorized", { status: 401 })
|
|
191
|
+
* ctx.locals.user = await verifyToken(token)
|
|
192
|
+
* }
|
|
193
|
+
*
|
|
194
|
+
* const handler = createHandler({
|
|
195
|
+
* App,
|
|
196
|
+
* routes,
|
|
197
|
+
* middleware: [authMiddleware],
|
|
198
|
+
* })
|
|
199
|
+
*/
|
|
200
|
+
/**
|
|
201
|
+
* Context for per-request locals — populated by the SSR handler from
|
|
202
|
+
* middleware `ctx.locals`. Components access it via `useRequestLocals()`.
|
|
203
|
+
*
|
|
204
|
+
* This bridges the middleware → component gap: middleware sets `ctx.locals`,
|
|
205
|
+
* the handler provides it into the Pyreon context system, and components
|
|
206
|
+
* read it without coupling to the middleware layer.
|
|
207
|
+
*/
|
|
208
|
+
const RequestLocalsCtx = createContext({});
|
|
209
|
+
/**
|
|
210
|
+
* Read per-request locals inside a component (SSR only).
|
|
211
|
+
*
|
|
212
|
+
* Returns the `ctx.locals` object populated by middleware.
|
|
213
|
+
* On the client, returns an empty object.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```tsx
|
|
217
|
+
* import { useRequestLocals } from "@pyreon/server"
|
|
218
|
+
*
|
|
219
|
+
* function MyComponent() {
|
|
220
|
+
* const locals = useRequestLocals()
|
|
221
|
+
* const nonce = locals.cspNonce as string ?? ''
|
|
222
|
+
* return <script nonce={nonce}>...<\/script>
|
|
223
|
+
* }
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
function useRequestLocals() {
|
|
227
|
+
return useContext(RequestLocalsCtx);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Provide request locals into the component tree.
|
|
231
|
+
* Called by the SSR handler — not for direct use.
|
|
232
|
+
* @internal
|
|
233
|
+
*/
|
|
234
|
+
function provideRequestLocals(locals) {
|
|
235
|
+
provide(RequestLocalsCtx, locals);
|
|
236
|
+
}
|
|
237
|
+
|
|
178
238
|
//#endregion
|
|
179
239
|
//#region src/handler.ts
|
|
180
240
|
const __DEV__ = typeof process !== "undefined" && process.env.NODE_ENV !== "production";
|
|
181
241
|
function createHandler(options) {
|
|
182
|
-
const { App, routes, template = DEFAULT_TEMPLATE, clientEntry = "/src/entry-client.ts", middleware = [], mode = "string" } = options;
|
|
242
|
+
const { App, routes, template = DEFAULT_TEMPLATE, clientEntry = "/src/entry-client.ts", middleware = [], mode = "string", collectStyles } = options;
|
|
183
243
|
const compiled = compileTemplate(template);
|
|
184
244
|
const clientEntryTag = buildClientEntryTag(clientEntry);
|
|
185
245
|
return async function handler(req) {
|
|
@@ -203,14 +263,17 @@ function createHandler(options) {
|
|
|
203
263
|
});
|
|
204
264
|
return runWithRequestContext(async () => {
|
|
205
265
|
try {
|
|
266
|
+
provideRequestLocals(ctx.locals);
|
|
206
267
|
await prefetchLoaderData(router, path);
|
|
207
268
|
const app = h(RouterProvider, { router }, h(App, null));
|
|
208
269
|
if (mode === "stream") return renderStreamResponse(app, router, compiled, clientEntryTag, ctx.headers);
|
|
209
270
|
const { html: appHtml, head } = await renderWithHead(app);
|
|
271
|
+
const styleTag = collectStyles ? collectStyles() : "";
|
|
272
|
+
const scripts = buildScriptsFast(clientEntryTag, serializeLoaderData(router));
|
|
210
273
|
const fullHtml = processCompiledTemplate(compiled, {
|
|
211
|
-
head,
|
|
274
|
+
head: styleTag ? `${styleTag}\n${head}` : head,
|
|
212
275
|
app: appHtml,
|
|
213
|
-
scripts
|
|
276
|
+
scripts
|
|
214
277
|
});
|
|
215
278
|
return new Response(fullHtml, {
|
|
216
279
|
status: 200,
|
|
@@ -423,5 +486,5 @@ function resolveOutputPath(outDir, path) {
|
|
|
423
486
|
}
|
|
424
487
|
|
|
425
488
|
//#endregion
|
|
426
|
-
export { DEFAULT_TEMPLATE, buildScripts, compileTemplate, createHandler, island, prerender, processCompiledTemplate, processTemplate };
|
|
489
|
+
export { DEFAULT_TEMPLATE, buildScripts, compileTemplate, createHandler, island, prerender, processCompiledTemplate, processTemplate, useRequestLocals };
|
|
427
490
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../head/lib/ssr.js","../src/html.ts","../src/handler.ts","../src/island.ts","../src/ssg.ts"],"sourcesContent":["import { createContext, h, pushContext } from \"@pyreon/core\";\nimport { renderToString } from \"@pyreon/runtime-server\";\n\n//#region src/context.ts\nfunction createHeadContext() {\n\tconst map = /* @__PURE__ */ new Map();\n\tlet dirty = true;\n\tlet cachedTags = [];\n\tlet cachedTitleTemplate;\n\tlet cachedHtmlAttrs = {};\n\tlet cachedBodyAttrs = {};\n\tfunction rebuild() {\n\t\tif (!dirty) return;\n\t\tdirty = false;\n\t\tconst keyed = /* @__PURE__ */ new Map();\n\t\tconst unkeyed = [];\n\t\tlet titleTemplate;\n\t\tconst htmlAttrs = {};\n\t\tconst bodyAttrs = {};\n\t\tfor (const entry of map.values()) {\n\t\t\tfor (const tag of entry.tags) if (tag.key) keyed.set(tag.key, tag);\n\t\t\telse unkeyed.push(tag);\n\t\t\tif (entry.titleTemplate !== void 0) titleTemplate = entry.titleTemplate;\n\t\t\tif (entry.htmlAttrs) Object.assign(htmlAttrs, entry.htmlAttrs);\n\t\t\tif (entry.bodyAttrs) Object.assign(bodyAttrs, entry.bodyAttrs);\n\t\t}\n\t\tcachedTags = [...keyed.values(), ...unkeyed];\n\t\tcachedTitleTemplate = titleTemplate;\n\t\tcachedHtmlAttrs = htmlAttrs;\n\t\tcachedBodyAttrs = bodyAttrs;\n\t}\n\treturn {\n\t\tadd(id, entry) {\n\t\t\tmap.set(id, entry);\n\t\t\tdirty = true;\n\t\t},\n\t\tremove(id) {\n\t\t\tmap.delete(id);\n\t\t\tdirty = true;\n\t\t},\n\t\tresolve() {\n\t\t\trebuild();\n\t\t\treturn cachedTags;\n\t\t},\n\t\tresolveTitleTemplate() {\n\t\t\trebuild();\n\t\t\treturn cachedTitleTemplate;\n\t\t},\n\t\tresolveHtmlAttrs() {\n\t\t\trebuild();\n\t\t\treturn cachedHtmlAttrs;\n\t\t},\n\t\tresolveBodyAttrs() {\n\t\t\trebuild();\n\t\t\treturn cachedBodyAttrs;\n\t\t}\n\t};\n}\nconst HeadContext = createContext(null);\n\n//#endregion\n//#region src/ssr.ts\nconst VOID_TAGS = new Set([\n\t\"meta\",\n\t\"link\",\n\t\"base\"\n]);\nasync function renderWithHead(app) {\n\tconst ctx = createHeadContext();\n\tfunction HeadInjector() {\n\t\tpushContext(new Map([[HeadContext.id, ctx]]));\n\t\treturn app;\n\t}\n\tconst html = await renderToString(h(HeadInjector, null));\n\tconst titleTemplate = ctx.resolveTitleTemplate();\n\treturn {\n\t\thtml,\n\t\thead: ctx.resolve().map((tag) => serializeTag(tag, titleTemplate)).join(\"\\n \"),\n\t\thtmlAttrs: ctx.resolveHtmlAttrs(),\n\t\tbodyAttrs: ctx.resolveBodyAttrs()\n\t};\n}\nfunction serializeTag(tag, titleTemplate) {\n\tif (tag.tag === \"title\") {\n\t\tconst raw = tag.children || \"\";\n\t\treturn `<title>${esc(titleTemplate ? typeof titleTemplate === \"function\" ? titleTemplate(raw) : titleTemplate.replace(/%s/g, raw) : raw)}</title>`;\n\t}\n\tconst props = tag.props;\n\tconst attrs = props ? Object.entries(props).map(([k, v]) => `${k}=\"${esc(v)}\"`).join(\" \") : \"\";\n\tconst open = attrs ? `<${tag.tag} ${attrs}` : `<${tag.tag}`;\n\tif (VOID_TAGS.has(tag.tag)) return `${open} />`;\n\treturn `${open}>${(tag.children || \"\").replace(/<\\/(script|style|noscript)/gi, \"<\\\\/$1\").replace(/<!--/g, \"<\\\\!--\")}</${tag.tag}>`;\n}\nconst ESC_RE = /[&<>\"]/g;\nconst ESC_MAP = {\n\t\"&\": \"&\",\n\t\"<\": \"<\",\n\t\">\": \">\",\n\t\"\\\"\": \""\"\n};\nfunction esc(s) {\n\treturn ESC_RE.test(s) ? s.replace(ESC_RE, (ch) => ESC_MAP[ch]) : s;\n}\n\n//#endregion\nexport { renderWithHead };\n//# sourceMappingURL=ssr.js.map","/**\n * HTML template processing for SSR/SSG.\n *\n * Templates use comment placeholders:\n * <!--pyreon-head--> — replaced with <head> tags (title, meta, link, etc.)\n * <!--pyreon-app--> — replaced with rendered application HTML\n * <!--pyreon-scripts--> — replaced with client entry script + inline loader data\n */\n\nexport const DEFAULT_TEMPLATE = `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <!--pyreon-head-->\n</head>\n<body>\n <div id=\"app\"><!--pyreon-app--></div>\n <!--pyreon-scripts-->\n</body>\n</html>`\n\nexport interface TemplateData {\n head: string\n app: string\n scripts: string\n}\n\n/**\n * Pre-compiled template — splits the template string once so that\n * each request only concatenates 6 parts instead of scanning 3x with `.replace()`.\n */\nexport interface CompiledTemplate {\n /** [before-head, between-head-app, between-app-scripts, after-scripts] */\n parts: [string, string, string, string]\n}\n\nexport function compileTemplate(template: string): CompiledTemplate {\n if (!template.includes('<!--pyreon-app-->')) {\n throw new Error('[pyreon/server] Template must contain <!--pyreon-app--> placeholder')\n }\n const [beforeHead, afterHead] = splitOnce(template, '<!--pyreon-head-->')\n const [betweenHeadApp, afterApp] = splitOnce(afterHead, '<!--pyreon-app-->')\n const [betweenAppScripts, afterScripts] = splitOnce(afterApp, '<!--pyreon-scripts-->')\n return { parts: [beforeHead, betweenHeadApp, betweenAppScripts, afterScripts] }\n}\n\nfunction splitOnce(str: string, delimiter: string): [string, string] {\n const idx = str.indexOf(delimiter)\n if (idx === -1) return [str, '']\n return [str.slice(0, idx), str.slice(idx + delimiter.length)]\n}\n\nexport function processTemplate(template: string, data: TemplateData): string {\n return template\n .replace('<!--pyreon-head-->', data.head)\n .replace('<!--pyreon-app-->', data.app)\n .replace('<!--pyreon-scripts-->', data.scripts)\n}\n\n/** Fast path using a pre-compiled template */\nexport function processCompiledTemplate(compiled: CompiledTemplate, data: TemplateData): string {\n const [p0, p1, p2, p3] = compiled.parts\n return p0 + data.head + p1 + data.app + p2 + data.scripts + p3\n}\n\n/**\n * Build the script tags for client hydration.\n *\n * Emits:\n * 1. Inline script with serialized loader data (if any)\n * 2. Module script tag pointing to the client entry\n */\nexport function buildScripts(\n clientEntry: string,\n loaderData: Record<string, unknown> | null,\n): string {\n const parts: string[] = []\n\n if (loaderData && Object.keys(loaderData).length > 0) {\n // Escape </script> inside JSON to prevent premature tag close\n const json = JSON.stringify(loaderData).replace(/<\\//g, '<\\\\/')\n parts.push(`<script>window.__PYREON_LOADER_DATA__=${json}</script>`)\n }\n\n parts.push(`<script type=\"module\" src=\"${clientEntry}\"></script>`)\n\n return parts.join('\\n ')\n}\n\n/** Pre-build the static client entry script tag (invariant across requests) */\nexport function buildClientEntryTag(clientEntry: string): string {\n return `<script type=\"module\" src=\"${clientEntry}\"></script>`\n}\n\n/** Fast path: build scripts with a pre-built client entry tag */\nexport function buildScriptsFast(\n clientEntryTag: string,\n loaderData: Record<string, unknown> | null,\n): string {\n if (loaderData && Object.keys(loaderData).length > 0) {\n const json = JSON.stringify(loaderData).replace(/<\\//g, '<\\\\/')\n return `<script>window.__PYREON_LOADER_DATA__=${json}</script>\\n ${clientEntryTag}`\n }\n return clientEntryTag\n}\n","/**\n * SSR request handler.\n *\n * Creates a Web-standard `(Request) => Promise<Response>` handler that:\n * 1. Runs middleware (auth, redirects, headers, etc.)\n * 2. Creates a per-request router with the matched URL\n * 3. Prefetches loader data for matched routes\n * 4. Renders the app to HTML with head tag collection\n * 5. Injects everything into an HTML template\n * 6. Returns a Response\n *\n * Compatible with Bun.serve, Deno.serve, Cloudflare Workers,\n * Express (via adapter), and any Web-standard server.\n *\n * @example\n * import { createHandler } from \"@pyreon/server\"\n *\n * const handler = createHandler({\n * App,\n * routes,\n * template: await Bun.file(\"index.html\").text(),\n * })\n *\n * Bun.serve({ fetch: handler })\n */\n\nimport type { ComponentFn } from '@pyreon/core'\nimport { h } from '@pyreon/core'\nimport { renderWithHead } from '@pyreon/head/ssr'\nimport {\n createRouter,\n prefetchLoaderData,\n type RouteRecord,\n RouterProvider,\n serializeLoaderData,\n} from '@pyreon/router'\nimport { renderToStream, runWithRequestContext } from '@pyreon/runtime-server'\nimport {\n buildClientEntryTag,\n buildScriptsFast,\n type CompiledTemplate,\n compileTemplate,\n DEFAULT_TEMPLATE,\n processCompiledTemplate,\n} from './html'\nimport type { Middleware, MiddlewareContext } from './middleware'\n\nconst __DEV__ = typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'\n\nexport interface HandlerOptions {\n /** Root application component */\n App: ComponentFn\n /** Route definitions */\n routes: RouteRecord[]\n /**\n * HTML template with placeholders:\n * <!--pyreon-head--> — head tags (title, meta, link, etc.)\n * <!--pyreon-app--> — rendered app HTML\n * <!--pyreon-scripts--> — client entry + loader data\n *\n * Defaults to a minimal HTML5 template.\n */\n template?: string\n /** Path to the client entry module (default: \"/src/entry-client.ts\") */\n clientEntry?: string\n /** Middleware chain — runs before rendering */\n middleware?: Middleware[]\n /**\n * Rendering mode:\n * \"string\" (default) — full renderToString, complete HTML in one response\n * \"stream\" — progressive streaming via renderToStream (Suspense out-of-order)\n */\n mode?: 'string' | 'stream'\n}\n\nexport function createHandler(options: HandlerOptions): (req: Request) => Promise<Response> {\n const {\n App,\n routes,\n template = DEFAULT_TEMPLATE,\n clientEntry = '/src/entry-client.ts',\n middleware = [],\n mode = 'string',\n } = options\n\n // Pre-compile once at handler creation — avoids 3x string scan per request\n const compiled = compileTemplate(template)\n const clientEntryTag = buildClientEntryTag(clientEntry)\n\n return async function handler(req: Request): Promise<Response> {\n const url = new URL(req.url)\n const path = url.pathname + url.search\n\n // ── Middleware pipeline ────────────────────────────────────────────────────\n const ctx: MiddlewareContext = {\n req,\n url,\n path,\n headers: new Headers({ 'Content-Type': 'text/html; charset=utf-8' }),\n locals: {},\n }\n\n for (const mw of middleware) {\n const result = await mw(ctx)\n if (result instanceof Response) return result\n }\n\n // ── Per-request router ────────────────────────────────────────────────────\n const router = createRouter({ routes, mode: 'history', url: path })\n\n return runWithRequestContext(async () => {\n try {\n // Pre-run loaders so data is available during render\n await prefetchLoaderData(router as never, path)\n\n // Build the VNode tree\n const app = h(RouterProvider, { router }, h(App, null))\n\n if (mode === 'stream') {\n return renderStreamResponse(app, router, compiled, clientEntryTag, ctx.headers)\n }\n\n // ── String mode (default) ─────────────────────────────────────────────\n const { html: appHtml, head } = await renderWithHead(app)\n const loaderData = serializeLoaderData(router as never)\n const scripts = buildScriptsFast(clientEntryTag, loaderData)\n const fullHtml = processCompiledTemplate(compiled, { head, app: appHtml, scripts })\n\n return new Response(fullHtml, { status: 200, headers: ctx.headers })\n } catch (err) {\n if (__DEV__) {\n console.error('[Pyreon Server] SSR render failed:', err)\n }\n return new Response('Internal Server Error', {\n status: 500,\n headers: { 'Content-Type': 'text/plain' },\n })\n }\n })\n }\n}\n\n/**\n * Streaming mode: shell is emitted immediately, app content streams progressively.\n *\n * Head tags from the initial synchronous render are included in the shell.\n * Suspense boundaries resolve out-of-order via inline <template> + swap scripts.\n */\nasync function renderStreamResponse(\n app: ReturnType<typeof h>,\n router: ReturnType<typeof createRouter>,\n compiled: CompiledTemplate,\n clientEntryTag: string,\n extraHeaders: Headers,\n): Promise<Response> {\n const loaderData = serializeLoaderData(router as never)\n const scripts = buildScriptsFast(clientEntryTag, loaderData)\n\n // Use pre-split parts: [before-head, between-head-app, between-app-scripts, after-scripts]\n const [p0, p1, p2, p3] = compiled.parts\n const shellHead = p0 + p1\n const shellTail = p2 + scripts + p3\n\n const appStream = renderToStream(app)\n const reader = appStream.getReader()\n\n const stream = new ReadableStream<Uint8Array>({\n async start(controller) {\n const encoder = new TextEncoder()\n const push = (s: string) => controller.enqueue(encoder.encode(s))\n\n try {\n push(shellHead)\n\n // Stream app content\n let done = false\n while (!done) {\n const result = await reader.read()\n done = result.done\n if (result.value) push(result.value)\n }\n\n push(shellTail)\n } catch (err) {\n if (__DEV__) {\n console.error('[Pyreon Server] Stream render failed:', err)\n }\n // Emit an inline error indicator — status code is already sent (200)\n push(`<script>console.error(\"[pyreon/server] Stream render failed\")</script>`)\n push(shellTail)\n } finally {\n controller.close()\n }\n },\n })\n\n return new Response(stream, {\n status: 200,\n headers: extraHeaders,\n })\n}\n","/**\n * Island architecture — partial hydration for content-heavy sites.\n *\n * Islands are interactive components embedded in otherwise-static HTML.\n * Only island components ship JavaScript to the client — the rest of the\n * page stays as zero-JS server-rendered HTML.\n *\n * ## Server side\n *\n * `island()` wraps an async component import and returns a ComponentFn.\n * During SSR, it renders the component output inside a `<pyreon-island>` element\n * with serialized props, so the client knows what to hydrate.\n *\n * ```tsx\n * import { island } from \"@pyreon/server\"\n *\n * const Counter = island(() => import(\"./Counter\"), { name: \"Counter\" })\n * const Search = island(() => import(\"./Search\"), { name: \"Search\" })\n *\n * function Page() {\n * return <div>\n * <h1>Static heading (no JS)</h1>\n * <Counter initial={5} /> // hydrated on client\n * <p>Static paragraph</p>\n * <Search /> // hydrated on client\n * </div>\n * }\n * ```\n *\n * ## Client side\n *\n * Use `hydrateIslands()` from `@pyreon/server/client` to hydrate all islands\n * on the page. Only the island components' JavaScript is loaded.\n *\n * ```ts\n * // entry-client.ts (island mode)\n * import { hydrateIslands } from \"@pyreon/server/client\"\n *\n * hydrateIslands({\n * Counter: () => import(\"./Counter\"),\n * Search: () => import(\"./Search\"),\n * })\n * ```\n *\n * ## Hydration strategies\n *\n * Control when an island hydrates via the `hydrate` option:\n * - \"load\" (default) — hydrate immediately on page load\n * - \"idle\" — hydrate when the browser is idle (requestIdleCallback)\n * - \"visible\" — hydrate when the island scrolls into the viewport\n * - \"media(query)\" — hydrate when a media query matches\n * - \"never\" — never hydrate (render-only, no client JS)\n */\n\nimport type { ComponentFn, Props, VNode } from '@pyreon/core'\nimport { h } from '@pyreon/core'\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport type HydrationStrategy = 'load' | 'idle' | 'visible' | 'never' | `media(${string})`\n\nexport interface IslandOptions {\n /** Unique name — must match the key in the client-side hydrateIslands() registry */\n name: string\n /** When to hydrate on the client (default: \"load\") */\n hydrate?: HydrationStrategy\n}\n\nexport interface IslandMeta {\n readonly __island: true\n readonly name: string\n readonly hydrate: HydrationStrategy\n}\n\n// ─── Server-side island factory ──────────────────────────────────────────────\n\n/**\n * Create an island component.\n *\n * Returns an async ComponentFn that:\n * 1. Resolves the dynamic import\n * 2. Renders the component to VNodes\n * 3. Wraps the output in `<pyreon-island>` with serialized props + hydration strategy\n */\nexport function island<P extends Props = Props>(\n loader: () => Promise<{ default: ComponentFn<P> } | ComponentFn<P>>,\n options: IslandOptions,\n): ComponentFn<P> & IslandMeta {\n const { name, hydrate = 'load' } = options\n\n const IslandWrapper = async function IslandWrapper(props: P): Promise<VNode | null> {\n const mod = await loader()\n const Comp = typeof mod === 'function' ? mod : mod.default\n const serializedProps = serializeIslandProps(props)\n\n return h(\n 'pyreon-island',\n {\n 'data-component': name,\n 'data-props': serializedProps,\n 'data-hydrate': hydrate,\n },\n h(Comp, props),\n )\n }\n\n // Attach metadata so the Vite plugin can detect islands for code-splitting\n const wrapper = IslandWrapper as unknown as ComponentFn<P> & IslandMeta\n Object.defineProperties(wrapper, {\n __island: { value: true, enumerable: true },\n name: { value: name, enumerable: true, writable: false, configurable: true },\n hydrate: { value: hydrate, enumerable: true },\n })\n\n return wrapper\n}\n\n// ─── Helpers ─────────────────────────────────────────────────────────────────\n\n/**\n * Serialize component props to a JSON string for embedding in HTML attributes.\n * Strips non-serializable values (functions, symbols, children).\n */\nfunction serializeIslandProps(props: Record<string, unknown>): string {\n const clean: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(props)) {\n // Skip non-serializable or internal props\n if (key === 'children') continue\n if (typeof value === 'function') continue\n if (typeof value === 'symbol') continue\n if (value === undefined) continue\n clean[key] = value\n }\n // The SSR renderer's renderProp() already applies escapeHtml() to attribute\n // values, so the JSON is safe to embed in HTML attributes without double-escaping.\n return JSON.stringify(clean)\n}\n","/**\n * Static Site Generation — pre-render routes to HTML files at build time.\n *\n * @example\n * // ssg.ts (run with: bun run ssg.ts)\n * import { createHandler } from \"@pyreon/server\"\n * import { prerender } from \"@pyreon/server\"\n * import { App } from \"./src/App\"\n * import { routes } from \"./src/routes\"\n *\n * const handler = createHandler({ App, routes })\n *\n * await prerender({\n * handler,\n * paths: [\"/\", \"/about\", \"/blog\", \"/blog/hello-world\"],\n * outDir: \"dist\",\n * })\n *\n * @example\n * // Dynamic paths from a CMS or filesystem\n * await prerender({\n * handler,\n * paths: async () => {\n * const posts = await fetchAllPosts()\n * return [\"/\", \"/about\", ...posts.map(p => `/blog/${p.slug}`)]\n * },\n * outDir: \"dist\",\n * })\n */\n\nimport { mkdir, writeFile } from 'node:fs/promises'\nimport { dirname, join, resolve } from 'node:path'\n\nexport interface PrerenderOptions {\n /** SSR handler created by createHandler() */\n handler: (req: Request) => Promise<Response>\n /** Routes to pre-render — array of URL paths or async function that returns them */\n paths: string[] | (() => string[] | Promise<string[]>)\n /** Output directory for the generated HTML files */\n outDir: string\n /** Origin for constructing full URLs (default: \"http://localhost\") */\n origin?: string\n /**\n * Called after each page is rendered — use for logging or progress tracking.\n * Return false to skip writing this page.\n */\n onPage?: (path: string, html: string) => void | boolean | Promise<void | boolean>\n}\n\nexport interface PrerenderResult {\n /** Number of pages generated */\n pages: number\n /** Paths that failed to render */\n errors: { path: string; error: unknown }[]\n /** Total elapsed time in milliseconds */\n elapsed: number\n}\n\n/**\n * Pre-render a list of routes to static HTML files.\n *\n * For each path:\n * 1. Constructs a Request for the path\n * 2. Calls the SSR handler to render to HTML\n * 3. Writes the HTML to `outDir/<path>/index.html`\n *\n * The root path \"/\" becomes `outDir/index.html`.\n * Paths like \"/about\" become `outDir/about/index.html`.\n */\nexport async function prerender(options: PrerenderOptions): Promise<PrerenderResult> {\n const { handler, outDir, origin = 'http://localhost', onPage } = options\n\n const start = Date.now()\n\n // Resolve paths (may be async)\n const paths = typeof options.paths === 'function' ? await options.paths() : options.paths\n\n let pages = 0\n const errors: PrerenderResult['errors'] = []\n\n async function renderPage(path: string): Promise<void> {\n const url = new URL(path, origin)\n const req = new Request(url.href)\n const res = await Promise.race([\n handler(req),\n new Promise<never>((_, reject) =>\n setTimeout(() => reject(new Error(`Prerender timeout for \"${path}\" (30s)`)), 30_000),\n ),\n ])\n\n if (!res.ok) {\n errors.push({ path, error: new Error(`HTTP ${res.status}`) })\n return\n }\n\n const html = await res.text()\n\n if (onPage) {\n const result = await onPage(path, html)\n if (result === false) return\n }\n\n const filePath = resolveOutputPath(outDir, path)\n\n const resolvedOut = resolve(outDir)\n if (!resolve(filePath).startsWith(resolvedOut)) {\n errors.push({ path, error: new Error(`Path traversal detected: \"${path}\"`) })\n return\n }\n\n await mkdir(dirname(filePath), { recursive: true })\n await writeFile(filePath, html, 'utf-8')\n pages++\n }\n\n // Process paths concurrently (batch of 10 to avoid overwhelming)\n const BATCH_SIZE = 10\n for (let i = 0; i < paths.length; i += BATCH_SIZE) {\n const batch = paths.slice(i, i + BATCH_SIZE)\n await Promise.all(\n batch.map(async (path) => {\n try {\n await renderPage(path)\n } catch (error) {\n errors.push({ path, error })\n }\n }),\n )\n }\n\n return {\n pages,\n errors,\n elapsed: Date.now() - start,\n }\n}\n\nfunction resolveOutputPath(outDir: string, path: string): string {\n if (path === '/') return join(outDir, 'index.html')\n if (path.endsWith('.html')) return join(outDir, path)\n return join(outDir, path, 'index.html')\n}\n"],"mappings":";;;;;;;AAIA,SAAS,oBAAoB;CAC5B,MAAM,sBAAsB,IAAI,KAAK;CACrC,IAAI,QAAQ;CACZ,IAAI,aAAa,EAAE;CACnB,IAAI;CACJ,IAAI,kBAAkB,EAAE;CACxB,IAAI,kBAAkB,EAAE;CACxB,SAAS,UAAU;AAClB,MAAI,CAAC,MAAO;AACZ,UAAQ;EACR,MAAM,wBAAwB,IAAI,KAAK;EACvC,MAAM,UAAU,EAAE;EAClB,IAAI;EACJ,MAAM,YAAY,EAAE;EACpB,MAAM,YAAY,EAAE;AACpB,OAAK,MAAM,SAAS,IAAI,QAAQ,EAAE;AACjC,QAAK,MAAM,OAAO,MAAM,KAAM,KAAI,I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MAAM,QAAQ,KAAK,YAAY;EACjD,MAAM,QAAQ,MAAM,MAAM,GAAG,IAAI,WAAW;AAC5C,QAAM,QAAQ,IACZ,MAAM,IAAI,OAAO,SAAS;AACxB,OAAI;AACF,UAAM,WAAW,KAAK;YACf,OAAO;AACd,WAAO,KAAK;KAAE;KAAM;KAAO,CAAC;;IAE9B,CACH;;AAGH,QAAO;EACL;EACA;EACA,SAAS,KAAK,KAAK,GAAG;EACvB;;AAGH,SAAS,kBAAkB,QAAgB,MAAsB;AAC/D,KAAI,SAAS,IAAK,QAAO,KAAK,QAAQ,aAAa;AACnD,KAAI,KAAK,SAAS,QAAQ,CAAE,QAAO,KAAK,QAAQ,KAAK;AACrD,QAAO,KAAK,QAAQ,MAAM,aAAa"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../head/lib/ssr.js","../src/html.ts","../src/middleware.ts","../src/handler.ts","../src/island.ts","../src/ssg.ts"],"sourcesContent":["import { createContext, h, pushContext } from \"@pyreon/core\";\nimport { renderToString } from \"@pyreon/runtime-server\";\n\n//#region src/context.ts\nfunction createHeadContext() {\n\tconst map = /* @__PURE__ */ new Map();\n\tlet dirty = true;\n\tlet cachedTags = [];\n\tlet cachedTitleTemplate;\n\tlet cachedHtmlAttrs = {};\n\tlet cachedBodyAttrs = {};\n\tfunction rebuild() {\n\t\tif (!dirty) return;\n\t\tdirty = false;\n\t\tconst keyed = /* @__PURE__ */ new Map();\n\t\tconst unkeyed = [];\n\t\tlet titleTemplate;\n\t\tconst htmlAttrs = {};\n\t\tconst bodyAttrs = {};\n\t\tfor (const entry of map.values()) {\n\t\t\tfor (const tag of entry.tags) if (tag.key) keyed.set(tag.key, tag);\n\t\t\telse unkeyed.push(tag);\n\t\t\tif (entry.titleTemplate !== void 0) titleTemplate = entry.titleTemplate;\n\t\t\tif (entry.htmlAttrs) Object.assign(htmlAttrs, entry.htmlAttrs);\n\t\t\tif (entry.bodyAttrs) Object.assign(bodyAttrs, entry.bodyAttrs);\n\t\t}\n\t\tcachedTags = [...keyed.values(), ...unkeyed];\n\t\tcachedTitleTemplate = titleTemplate;\n\t\tcachedHtmlAttrs = htmlAttrs;\n\t\tcachedBodyAttrs = bodyAttrs;\n\t}\n\treturn {\n\t\tadd(id, entry) {\n\t\t\tmap.set(id, entry);\n\t\t\tdirty = true;\n\t\t},\n\t\tremove(id) {\n\t\t\tmap.delete(id);\n\t\t\tdirty = true;\n\t\t},\n\t\tresolve() {\n\t\t\trebuild();\n\t\t\treturn cachedTags;\n\t\t},\n\t\tresolveTitleTemplate() {\n\t\t\trebuild();\n\t\t\treturn cachedTitleTemplate;\n\t\t},\n\t\tresolveHtmlAttrs() {\n\t\t\trebuild();\n\t\t\treturn cachedHtmlAttrs;\n\t\t},\n\t\tresolveBodyAttrs() {\n\t\t\trebuild();\n\t\t\treturn cachedBodyAttrs;\n\t\t}\n\t};\n}\nconst HeadContext = createContext(null);\n\n//#endregion\n//#region src/ssr.ts\nconst VOID_TAGS = new Set([\n\t\"meta\",\n\t\"link\",\n\t\"base\"\n]);\nasync function renderWithHead(app) {\n\tconst ctx = createHeadContext();\n\tfunction HeadInjector() {\n\t\tpushContext(new Map([[HeadContext.id, ctx]]));\n\t\treturn app;\n\t}\n\tconst html = await renderToString(h(HeadInjector, null));\n\tconst titleTemplate = ctx.resolveTitleTemplate();\n\treturn {\n\t\thtml,\n\t\thead: ctx.resolve().map((tag) => serializeTag(tag, titleTemplate)).join(\"\\n \"),\n\t\thtmlAttrs: ctx.resolveHtmlAttrs(),\n\t\tbodyAttrs: ctx.resolveBodyAttrs()\n\t};\n}\nfunction serializeTag(tag, titleTemplate) {\n\tif (tag.tag === \"title\") {\n\t\tconst raw = tag.children || \"\";\n\t\treturn `<title>${esc(titleTemplate ? typeof titleTemplate === \"function\" ? titleTemplate(raw) : titleTemplate.replace(/%s/g, raw) : raw)}</title>`;\n\t}\n\tconst props = tag.props;\n\tconst attrs = props ? Object.entries(props).map(([k, v]) => `${k}=\"${esc(v)}\"`).join(\" \") : \"\";\n\tconst open = attrs ? `<${tag.tag} ${attrs}` : `<${tag.tag}`;\n\tif (VOID_TAGS.has(tag.tag)) return `${open} />`;\n\treturn `${open}>${(tag.children || \"\").replace(/<\\/(script|style|noscript)/gi, \"<\\\\/$1\").replace(/<!--/g, \"<\\\\!--\")}</${tag.tag}>`;\n}\nconst ESC_RE = /[&<>\"]/g;\nconst ESC_MAP = {\n\t\"&\": \"&\",\n\t\"<\": \"<\",\n\t\">\": \">\",\n\t\"\\\"\": \""\"\n};\nfunction esc(s) {\n\treturn ESC_RE.test(s) ? s.replace(ESC_RE, (ch) => ESC_MAP[ch]) : s;\n}\n\n//#endregion\nexport { renderWithHead };\n//# sourceMappingURL=ssr.js.map","/**\n * HTML template processing for SSR/SSG.\n *\n * Templates use comment placeholders:\n * <!--pyreon-head--> — replaced with <head> tags (title, meta, link, etc.)\n * <!--pyreon-app--> — replaced with rendered application HTML\n * <!--pyreon-scripts--> — replaced with client entry script + inline loader data\n */\n\nexport const DEFAULT_TEMPLATE = `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <!--pyreon-head-->\n</head>\n<body>\n <div id=\"app\"><!--pyreon-app--></div>\n <!--pyreon-scripts-->\n</body>\n</html>`\n\nexport interface TemplateData {\n head: string\n app: string\n scripts: string\n}\n\n/**\n * Pre-compiled template — splits the template string once so that\n * each request only concatenates 6 parts instead of scanning 3x with `.replace()`.\n */\nexport interface CompiledTemplate {\n /** [before-head, between-head-app, between-app-scripts, after-scripts] */\n parts: [string, string, string, string]\n}\n\nexport function compileTemplate(template: string): CompiledTemplate {\n if (!template.includes('<!--pyreon-app-->')) {\n throw new Error('[pyreon/server] Template must contain <!--pyreon-app--> placeholder')\n }\n const [beforeHead, afterHead] = splitOnce(template, '<!--pyreon-head-->')\n const [betweenHeadApp, afterApp] = splitOnce(afterHead, '<!--pyreon-app-->')\n const [betweenAppScripts, afterScripts] = splitOnce(afterApp, '<!--pyreon-scripts-->')\n return { parts: [beforeHead, betweenHeadApp, betweenAppScripts, afterScripts] }\n}\n\nfunction splitOnce(str: string, delimiter: string): [string, string] {\n const idx = str.indexOf(delimiter)\n if (idx === -1) return [str, '']\n return [str.slice(0, idx), str.slice(idx + delimiter.length)]\n}\n\nexport function processTemplate(template: string, data: TemplateData): string {\n return template\n .replace('<!--pyreon-head-->', data.head)\n .replace('<!--pyreon-app-->', data.app)\n .replace('<!--pyreon-scripts-->', data.scripts)\n}\n\n/** Fast path using a pre-compiled template */\nexport function processCompiledTemplate(compiled: CompiledTemplate, data: TemplateData): string {\n const [p0, p1, p2, p3] = compiled.parts\n return p0 + data.head + p1 + data.app + p2 + data.scripts + p3\n}\n\n/**\n * Build the script tags for client hydration.\n *\n * Emits:\n * 1. Inline script with serialized loader data (if any)\n * 2. Module script tag pointing to the client entry\n */\nexport function buildScripts(\n clientEntry: string,\n loaderData: Record<string, unknown> | null,\n): string {\n const parts: string[] = []\n\n if (loaderData && Object.keys(loaderData).length > 0) {\n // Escape </script> inside JSON to prevent premature tag close\n const json = JSON.stringify(loaderData).replace(/<\\//g, '<\\\\/')\n parts.push(`<script>window.__PYREON_LOADER_DATA__=${json}</script>`)\n }\n\n parts.push(`<script type=\"module\" src=\"${clientEntry}\"></script>`)\n\n return parts.join('\\n ')\n}\n\n/** Pre-build the static client entry script tag (invariant across requests) */\nexport function buildClientEntryTag(clientEntry: string): string {\n return `<script type=\"module\" src=\"${clientEntry}\"></script>`\n}\n\n/** Fast path: build scripts with a pre-built client entry tag */\nexport function buildScriptsFast(\n clientEntryTag: string,\n loaderData: Record<string, unknown> | null,\n): string {\n if (loaderData && Object.keys(loaderData).length > 0) {\n const json = JSON.stringify(loaderData).replace(/<\\//g, '<\\\\/')\n return `<script>window.__PYREON_LOADER_DATA__=${json}</script>\\n ${clientEntryTag}`\n }\n return clientEntryTag\n}\n","/**\n * SSR middleware — simple request processing pipeline.\n *\n * Middleware runs before rendering. Return a Response to short-circuit\n * (e.g. for redirects, auth checks, or static file serving).\n * Return void / undefined to continue to the next middleware or rendering.\n *\n * @example\n * const authMiddleware: Middleware = async (ctx) => {\n * const token = ctx.req.headers.get(\"Authorization\")\n * if (!token) return new Response(\"Unauthorized\", { status: 401 })\n * ctx.locals.user = await verifyToken(token)\n * }\n *\n * const handler = createHandler({\n * App,\n * routes,\n * middleware: [authMiddleware],\n * })\n */\n\nimport { createContext, useContext, provide } from '@pyreon/core'\n\nexport interface MiddlewareContext {\n /** The incoming request */\n req: Request\n /** Parsed URL */\n url: URL\n /** Pathname + search (passed to router) */\n path: string\n /** Response headers — middleware can set custom headers */\n headers: Headers\n /** Arbitrary per-request data shared between middleware and components */\n locals: Record<string, unknown>\n}\n\n/**\n * Middleware function. Return a Response to short-circuit, or void to continue.\n */\nexport type Middleware = (ctx: MiddlewareContext) => Response | void | Promise<Response | void>\n\n/**\n * Context for per-request locals — populated by the SSR handler from\n * middleware `ctx.locals`. Components access it via `useRequestLocals()`.\n *\n * This bridges the middleware → component gap: middleware sets `ctx.locals`,\n * the handler provides it into the Pyreon context system, and components\n * read it without coupling to the middleware layer.\n */\nexport const RequestLocalsCtx = createContext<Record<string, unknown>>({})\n\n/**\n * Read per-request locals inside a component (SSR only).\n *\n * Returns the `ctx.locals` object populated by middleware.\n * On the client, returns an empty object.\n *\n * @example\n * ```tsx\n * import { useRequestLocals } from \"@pyreon/server\"\n *\n * function MyComponent() {\n * const locals = useRequestLocals()\n * const nonce = locals.cspNonce as string ?? ''\n * return <script nonce={nonce}>...</script>\n * }\n * ```\n */\nexport function useRequestLocals(): Record<string, unknown> {\n return useContext(RequestLocalsCtx)\n}\n\n/**\n * Provide request locals into the component tree.\n * Called by the SSR handler — not for direct use.\n * @internal\n */\nexport function provideRequestLocals(locals: Record<string, unknown>): void {\n provide(RequestLocalsCtx, locals)\n}\n","/**\n * SSR request handler.\n *\n * Creates a Web-standard `(Request) => Promise<Response>` handler that:\n * 1. Runs middleware (auth, redirects, headers, etc.)\n * 2. Creates a per-request router with the matched URL\n * 3. Prefetches loader data for matched routes\n * 4. Renders the app to HTML with head tag collection\n * 5. Injects everything into an HTML template\n * 6. Returns a Response\n *\n * Compatible with Bun.serve, Deno.serve, Cloudflare Workers,\n * Express (via adapter), and any Web-standard server.\n *\n * @example\n * import { createHandler } from \"@pyreon/server\"\n *\n * const handler = createHandler({\n * App,\n * routes,\n * template: await Bun.file(\"index.html\").text(),\n * })\n *\n * Bun.serve({ fetch: handler })\n */\n\nimport type { ComponentFn } from '@pyreon/core'\nimport { h } from '@pyreon/core'\nimport { renderWithHead } from '@pyreon/head/ssr'\nimport {\n createRouter,\n prefetchLoaderData,\n type RouteRecord,\n RouterProvider,\n serializeLoaderData,\n} from '@pyreon/router'\nimport { renderToStream, runWithRequestContext } from '@pyreon/runtime-server'\nimport {\n buildClientEntryTag,\n buildScriptsFast,\n type CompiledTemplate,\n compileTemplate,\n DEFAULT_TEMPLATE,\n processCompiledTemplate,\n} from './html'\nimport type { Middleware, MiddlewareContext } from './middleware'\nimport { provideRequestLocals } from './middleware'\n\nconst __DEV__ = typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'\n\nexport interface HandlerOptions {\n /** Root application component */\n App: ComponentFn\n /** Route definitions */\n routes: RouteRecord[]\n /**\n * HTML template with placeholders:\n * <!--pyreon-head--> — head tags (title, meta, link, etc.)\n * <!--pyreon-app--> — rendered app HTML\n * <!--pyreon-scripts--> — client entry + loader data\n *\n * Defaults to a minimal HTML5 template.\n */\n template?: string\n /** Path to the client entry module (default: \"/src/entry-client.ts\") */\n clientEntry?: string\n /** Middleware chain — runs before rendering */\n middleware?: Middleware[]\n /**\n * Rendering mode:\n * \"string\" (default) — full renderToString, complete HTML in one response\n * \"stream\" — progressive streaming via renderToStream (Suspense out-of-order)\n */\n mode?: 'string' | 'stream'\n /**\n * Collect CSS styles after rendering. Called after renderToString/renderWithHead.\n * Return a `<style>` tag string to inject into `<head>`.\n * Used by @pyreon/styler's sheet.getStyleTag() to prevent FOUC in SSG.\n *\n * @example\n * import { sheet } from '@pyreon/styler'\n * createHandler({\n * collectStyles: () => {\n * const tag = sheet.getStyleTag()\n * sheet.reset()\n * return tag\n * },\n * })\n */\n collectStyles?: () => string\n}\n\nexport function createHandler(options: HandlerOptions): (req: Request) => Promise<Response> {\n const {\n App,\n routes,\n template = DEFAULT_TEMPLATE,\n clientEntry = '/src/entry-client.ts',\n middleware = [],\n mode = 'string',\n collectStyles,\n } = options\n\n // Pre-compile once at handler creation — avoids 3x string scan per request\n const compiled = compileTemplate(template)\n const clientEntryTag = buildClientEntryTag(clientEntry)\n\n return async function handler(req: Request): Promise<Response> {\n const url = new URL(req.url)\n const path = url.pathname + url.search\n\n // ── Middleware pipeline ────────────────────────────────────────────────────\n const ctx: MiddlewareContext = {\n req,\n url,\n path,\n headers: new Headers({ 'Content-Type': 'text/html; charset=utf-8' }),\n locals: {},\n }\n\n for (const mw of middleware) {\n const result = await mw(ctx)\n if (result instanceof Response) return result\n }\n\n // ── Per-request router ────────────────────────────────────────────────────\n const router = createRouter({ routes, mode: 'history', url: path })\n\n return runWithRequestContext(async () => {\n try {\n // Bridge middleware locals → Pyreon context system so components\n // can access per-request data (CSP nonce, auth user, etc.)\n provideRequestLocals(ctx.locals)\n\n // Pre-run loaders so data is available during render\n await prefetchLoaderData(router as never, path)\n\n // Build the VNode tree\n const app = h(RouterProvider, { router }, h(App, null))\n\n if (mode === 'stream') {\n return renderStreamResponse(app, router, compiled, clientEntryTag, ctx.headers)\n }\n\n // ── String mode (default) ─────────────────────────────────────────────\n const { html: appHtml, head } = await renderWithHead(app)\n\n // Collect CSS-in-JS styles if a collector was provided.\n // The consumer passes collectStyles (e.g. sheet.getStyleTag from @pyreon/styler)\n // to inject scoped CSS into <head> and prevent FOUC in SSG pages.\n const styleTag = collectStyles ? collectStyles() : ''\n\n const loaderData = serializeLoaderData(router as never)\n const scripts = buildScriptsFast(clientEntryTag, loaderData)\n const headWithStyles = styleTag ? `${styleTag}\\n${head}` : head\n const fullHtml = processCompiledTemplate(compiled, { head: headWithStyles, app: appHtml, scripts })\n\n return new Response(fullHtml, { status: 200, headers: ctx.headers })\n } catch (err) {\n if (__DEV__) {\n console.error('[Pyreon Server] SSR render failed:', err)\n }\n return new Response('Internal Server Error', {\n status: 500,\n headers: { 'Content-Type': 'text/plain' },\n })\n }\n })\n }\n}\n\n/**\n * Streaming mode: shell is emitted immediately, app content streams progressively.\n *\n * Head tags from the initial synchronous render are included in the shell.\n * Suspense boundaries resolve out-of-order via inline <template> + swap scripts.\n */\nasync function renderStreamResponse(\n app: ReturnType<typeof h>,\n router: ReturnType<typeof createRouter>,\n compiled: CompiledTemplate,\n clientEntryTag: string,\n extraHeaders: Headers,\n): Promise<Response> {\n const loaderData = serializeLoaderData(router as never)\n const scripts = buildScriptsFast(clientEntryTag, loaderData)\n\n // Use pre-split parts: [before-head, between-head-app, between-app-scripts, after-scripts]\n const [p0, p1, p2, p3] = compiled.parts\n const shellHead = p0 + p1\n const shellTail = p2 + scripts + p3\n\n const appStream = renderToStream(app)\n const reader = appStream.getReader()\n\n const stream = new ReadableStream<Uint8Array>({\n async start(controller) {\n const encoder = new TextEncoder()\n const push = (s: string) => controller.enqueue(encoder.encode(s))\n\n try {\n push(shellHead)\n\n // Stream app content\n let done = false\n while (!done) {\n const result = await reader.read()\n done = result.done\n if (result.value) push(result.value)\n }\n\n push(shellTail)\n } catch (err) {\n if (__DEV__) {\n console.error('[Pyreon Server] Stream render failed:', err)\n }\n // Emit an inline error indicator — status code is already sent (200)\n push(`<script>console.error(\"[pyreon/server] Stream render failed\")</script>`)\n push(shellTail)\n } finally {\n controller.close()\n }\n },\n })\n\n return new Response(stream, {\n status: 200,\n headers: extraHeaders,\n })\n}\n","/**\n * Island architecture — partial hydration for content-heavy sites.\n *\n * Islands are interactive components embedded in otherwise-static HTML.\n * Only island components ship JavaScript to the client — the rest of the\n * page stays as zero-JS server-rendered HTML.\n *\n * ## Server side\n *\n * `island()` wraps an async component import and returns a ComponentFn.\n * During SSR, it renders the component output inside a `<pyreon-island>` element\n * with serialized props, so the client knows what to hydrate.\n *\n * ```tsx\n * import { island } from \"@pyreon/server\"\n *\n * const Counter = island(() => import(\"./Counter\"), { name: \"Counter\" })\n * const Search = island(() => import(\"./Search\"), { name: \"Search\" })\n *\n * function Page() {\n * return <div>\n * <h1>Static heading (no JS)</h1>\n * <Counter initial={5} /> // hydrated on client\n * <p>Static paragraph</p>\n * <Search /> // hydrated on client\n * </div>\n * }\n * ```\n *\n * ## Client side\n *\n * Use `hydrateIslands()` from `@pyreon/server/client` to hydrate all islands\n * on the page. Only the island components' JavaScript is loaded.\n *\n * ```ts\n * // entry-client.ts (island mode)\n * import { hydrateIslands } from \"@pyreon/server/client\"\n *\n * hydrateIslands({\n * Counter: () => import(\"./Counter\"),\n * Search: () => import(\"./Search\"),\n * })\n * ```\n *\n * ## Hydration strategies\n *\n * Control when an island hydrates via the `hydrate` option:\n * - \"load\" (default) — hydrate immediately on page load\n * - \"idle\" — hydrate when the browser is idle (requestIdleCallback)\n * - \"visible\" — hydrate when the island scrolls into the viewport\n * - \"media(query)\" — hydrate when a media query matches\n * - \"never\" — never hydrate (render-only, no client JS)\n */\n\nimport type { ComponentFn, Props, VNode } from '@pyreon/core'\nimport { h } from '@pyreon/core'\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport type HydrationStrategy = 'load' | 'idle' | 'visible' | 'never' | `media(${string})`\n\nexport interface IslandOptions {\n /** Unique name — must match the key in the client-side hydrateIslands() registry */\n name: string\n /** When to hydrate on the client (default: \"load\") */\n hydrate?: HydrationStrategy\n}\n\nexport interface IslandMeta {\n readonly __island: true\n readonly name: string\n readonly hydrate: HydrationStrategy\n}\n\n// ─── Server-side island factory ──────────────────────────────────────────────\n\n/**\n * Create an island component.\n *\n * Returns an async ComponentFn that:\n * 1. Resolves the dynamic import\n * 2. Renders the component to VNodes\n * 3. Wraps the output in `<pyreon-island>` with serialized props + hydration strategy\n */\nexport function island<P extends Props = Props>(\n loader: () => Promise<{ default: ComponentFn<P> } | ComponentFn<P>>,\n options: IslandOptions,\n): ComponentFn<P> & IslandMeta {\n const { name, hydrate = 'load' } = options\n\n const IslandWrapper = async function IslandWrapper(props: P): Promise<VNode | null> {\n const mod = await loader()\n const Comp = typeof mod === 'function' ? mod : mod.default\n const serializedProps = serializeIslandProps(props)\n\n return h(\n 'pyreon-island',\n {\n 'data-component': name,\n 'data-props': serializedProps,\n 'data-hydrate': hydrate,\n },\n h(Comp, props),\n )\n }\n\n // Attach metadata so the Vite plugin can detect islands for code-splitting\n const wrapper = IslandWrapper as unknown as ComponentFn<P> & IslandMeta\n Object.defineProperties(wrapper, {\n __island: { value: true, enumerable: true },\n name: { value: name, enumerable: true, writable: false, configurable: true },\n hydrate: { value: hydrate, enumerable: true },\n })\n\n return wrapper\n}\n\n// ─── Helpers ─────────────────────────────────────────────────────────────────\n\n/**\n * Serialize component props to a JSON string for embedding in HTML attributes.\n * Strips non-serializable values (functions, symbols, children).\n */\nfunction serializeIslandProps(props: Record<string, unknown>): string {\n const clean: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(props)) {\n // Skip non-serializable or internal props\n if (key === 'children') continue\n if (typeof value === 'function') continue\n if (typeof value === 'symbol') continue\n if (value === undefined) continue\n clean[key] = value\n }\n // The SSR renderer's renderProp() already applies escapeHtml() to attribute\n // values, so the JSON is safe to embed in HTML attributes without double-escaping.\n return JSON.stringify(clean)\n}\n","/**\n * Static Site Generation — pre-render routes to HTML files at build time.\n *\n * @example\n * // ssg.ts (run with: bun run ssg.ts)\n * import { createHandler } from \"@pyreon/server\"\n * import { prerender } from \"@pyreon/server\"\n * import { App } from \"./src/App\"\n * import { routes } from \"./src/routes\"\n *\n * const handler = createHandler({ App, routes })\n *\n * await prerender({\n * handler,\n * paths: [\"/\", \"/about\", \"/blog\", \"/blog/hello-world\"],\n * outDir: \"dist\",\n * })\n *\n * @example\n * // Dynamic paths from a CMS or filesystem\n * await prerender({\n * handler,\n * paths: async () => {\n * const posts = await fetchAllPosts()\n * return [\"/\", \"/about\", ...posts.map(p => `/blog/${p.slug}`)]\n * },\n * outDir: \"dist\",\n * })\n */\n\nimport { mkdir, writeFile } from 'node:fs/promises'\nimport { dirname, join, resolve } from 'node:path'\n\nexport interface PrerenderOptions {\n /** SSR handler created by createHandler() */\n handler: (req: Request) => Promise<Response>\n /** Routes to pre-render — array of URL paths or async function that returns them */\n paths: string[] | (() => string[] | Promise<string[]>)\n /** Output directory for the generated HTML files */\n outDir: string\n /** Origin for constructing full URLs (default: \"http://localhost\") */\n origin?: string\n /**\n * Called after each page is rendered — use for logging or progress tracking.\n * Return false to skip writing this page.\n */\n onPage?: (path: string, html: string) => void | boolean | Promise<void | boolean>\n}\n\nexport interface PrerenderResult {\n /** Number of pages generated */\n pages: number\n /** Paths that failed to render */\n errors: { path: string; error: unknown }[]\n /** Total elapsed time in milliseconds */\n elapsed: number\n}\n\n/**\n * Pre-render a list of routes to static HTML files.\n *\n * For each path:\n * 1. Constructs a Request for the path\n * 2. Calls the SSR handler to render to HTML\n * 3. Writes the HTML to `outDir/<path>/index.html`\n *\n * The root path \"/\" becomes `outDir/index.html`.\n * Paths like \"/about\" become `outDir/about/index.html`.\n */\nexport async function prerender(options: PrerenderOptions): Promise<PrerenderResult> {\n const { handler, outDir, origin = 'http://localhost', onPage } = options\n\n const start = Date.now()\n\n // Resolve paths (may be async)\n const paths = typeof options.paths === 'function' ? await options.paths() : options.paths\n\n let pages = 0\n const errors: PrerenderResult['errors'] = []\n\n async function renderPage(path: string): Promise<void> {\n const url = new URL(path, origin)\n const req = new Request(url.href)\n const res = await Promise.race([\n handler(req),\n new Promise<never>((_, reject) =>\n setTimeout(() => reject(new Error(`Prerender timeout for \"${path}\" (30s)`)), 30_000),\n ),\n ])\n\n if (!res.ok) {\n errors.push({ path, error: new Error(`HTTP ${res.status}`) })\n return\n }\n\n const html = await res.text()\n\n if (onPage) {\n const result = await onPage(path, html)\n if (result === false) return\n }\n\n const filePath = resolveOutputPath(outDir, path)\n\n const resolvedOut = resolve(outDir)\n if (!resolve(filePath).startsWith(resolvedOut)) {\n errors.push({ path, error: new Error(`Path traversal detected: \"${path}\"`) })\n return\n }\n\n await mkdir(dirname(filePath), { recursive: true })\n await writeFile(filePath, html, 'utf-8')\n pages++\n }\n\n // Process paths concurrently (batch of 10 to avoid overwhelming)\n const BATCH_SIZE = 10\n for (let i = 0; i < paths.length; i += BATCH_SIZE) {\n const batch = paths.slice(i, i + BATCH_SIZE)\n await Promise.all(\n batch.map(async (path) => {\n try {\n await renderPage(path)\n } catch (error) {\n errors.push({ path, error })\n }\n }),\n )\n }\n\n return {\n pages,\n errors,\n elapsed: Date.now() - start,\n }\n}\n\nfunction resolveOutputPath(outDir: string, path: string): string {\n if (path === '/') return join(outDir, 'index.html')\n if (path.endsWith('.html')) return join(outDir, path)\n return join(outDir, path, 'index.html')\n}\n"],"mappings":";;;;;;;AAIA,SAAS,oBAAoB;CAC5B,MAAM,sBAAsB,IAAI,KAAK;CACrC,IAAI,QAAQ;CACZ,IAAI,aAAa,EAAE;CACnB,IAAI;CACJ,IAAI,kBAAkB,EAAE;CACxB,IAAI,kBAAkB,EAAE;CACxB,SAAS,UAAU;AAClB,MAAI,CAAC,MAAO;AACZ,UAAQ;EACR,MAAM,wBAAwB,IAAI,KAAK;EACvC,MAAM,UAAU,EAAE;EAClB,IAAI;EACJ,MAAM,YAAY,EAAE;EACpB,MAAM,YAAY,EAAE;AACpB,OAAK,MAAM,SAAS,IAAI,QAAQ,EAAE;AACjC,QAAK,MAAM,OAAO,MAAM,KAAM,KAAI,IAAI,IAAK,OAAM,IAAI,IAAI,KAAK,IAAI;OAC7D,SAAQ,KAAK,IAAI;AACtB,OAAI,MAAM,kBAAkB,KAAK,EAAG,iBAAgB,MAAM;AAC1D,OAAI,MAAM,UAAW,QAAO,OAAO,WAAW,MAAM,UAAU;AAC9D,OAAI,MAAM,UAAW,Q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AQ,SAAS,EAAE,EAAE,WAAW,MAAM,CAAC;AACnD,QAAM,UAAU,UAAU,MAAM,QAAQ;AACxC;;CAIF,MAAM,aAAa;AACnB,MAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,YAAY;EACjD,MAAM,QAAQ,MAAM,MAAM,GAAG,IAAI,WAAW;AAC5C,QAAM,QAAQ,IACZ,MAAM,IAAI,OAAO,SAAS;AACxB,OAAI;AACF,UAAM,WAAW,KAAK;YACf,OAAO;AACd,WAAO,KAAK;KAAE;KAAM;KAAO,CAAC;;IAE9B,CACH;;AAGH,QAAO;EACL;EACA;EACA,SAAS,KAAK,KAAK,GAAG;EACvB;;AAGH,SAAS,kBAAkB,QAAgB,MAAsB;AAC/D,KAAI,SAAS,IAAK,QAAO,KAAK,QAAQ,aAAa;AACnD,KAAI,KAAK,SAAS,QAAQ,CAAE,QAAO,KAAK,QAAQ,KAAK;AACrD,QAAO,KAAK,QAAQ,MAAM,aAAa"}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -38,6 +38,24 @@ interface MiddlewareContext {
|
|
|
38
38
|
* Middleware function. Return a Response to short-circuit, or void to continue.
|
|
39
39
|
*/
|
|
40
40
|
type Middleware = (ctx: MiddlewareContext) => Response | void | Promise<Response | void>;
|
|
41
|
+
/**
|
|
42
|
+
* Read per-request locals inside a component (SSR only).
|
|
43
|
+
*
|
|
44
|
+
* Returns the `ctx.locals` object populated by middleware.
|
|
45
|
+
* On the client, returns an empty object.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```tsx
|
|
49
|
+
* import { useRequestLocals } from "@pyreon/server"
|
|
50
|
+
*
|
|
51
|
+
* function MyComponent() {
|
|
52
|
+
* const locals = useRequestLocals()
|
|
53
|
+
* const nonce = locals.cspNonce as string ?? ''
|
|
54
|
+
* return <script nonce={nonce}>...</script>
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function useRequestLocals(): Record<string, unknown>;
|
|
41
59
|
//#endregion
|
|
42
60
|
//#region src/handler.d.ts
|
|
43
61
|
interface HandlerOptions {
|
|
@@ -64,6 +82,22 @@ interface HandlerOptions {
|
|
|
64
82
|
* "stream" — progressive streaming via renderToStream (Suspense out-of-order)
|
|
65
83
|
*/
|
|
66
84
|
mode?: 'string' | 'stream';
|
|
85
|
+
/**
|
|
86
|
+
* Collect CSS styles after rendering. Called after renderToString/renderWithHead.
|
|
87
|
+
* Return a `<style>` tag string to inject into `<head>`.
|
|
88
|
+
* Used by @pyreon/styler's sheet.getStyleTag() to prevent FOUC in SSG.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* import { sheet } from '@pyreon/styler'
|
|
92
|
+
* createHandler({
|
|
93
|
+
* collectStyles: () => {
|
|
94
|
+
* const tag = sheet.getStyleTag()
|
|
95
|
+
* sheet.reset()
|
|
96
|
+
* return tag
|
|
97
|
+
* },
|
|
98
|
+
* })
|
|
99
|
+
*/
|
|
100
|
+
collectStyles?: () => string;
|
|
67
101
|
}
|
|
68
102
|
declare function createHandler(options: HandlerOptions): (req: Request) => Promise<Response>;
|
|
69
103
|
//#endregion
|
|
@@ -197,5 +231,5 @@ interface PrerenderResult {
|
|
|
197
231
|
*/
|
|
198
232
|
declare function prerender(options: PrerenderOptions): Promise<PrerenderResult>;
|
|
199
233
|
//#endregion
|
|
200
|
-
export { type CompiledTemplate, DEFAULT_TEMPLATE, type HandlerOptions, type HydrationStrategy, type IslandMeta, type IslandOptions, type Middleware, type MiddlewareContext, type PrerenderOptions, type PrerenderResult, type TemplateData, buildScripts, compileTemplate, createHandler, island, prerender, processCompiledTemplate, processTemplate };
|
|
234
|
+
export { type CompiledTemplate, DEFAULT_TEMPLATE, type HandlerOptions, type HydrationStrategy, type IslandMeta, type IslandOptions, type Middleware, type MiddlewareContext, type PrerenderOptions, type PrerenderResult, type TemplateData, buildScripts, compileTemplate, createHandler, island, prerender, processCompiledTemplate, processTemplate, useRequestLocals };
|
|
201
235
|
//# sourceMappingURL=index2.d.ts.map
|
package/lib/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../../src/middleware.ts","../../../src/handler.ts","../../../src/html.ts","../../../src/island.ts","../../../src/ssg.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../../src/middleware.ts","../../../src/handler.ts","../../../src/html.ts","../../../src/island.ts","../../../src/ssg.ts"],"mappings":";;;;;;;;AAuBA;;;;;;;;;;;;;;;;UAAiB,iBAAA;EAUP;EARR,GAAA,EAAK,OAAA;EAQS;EANd,GAAA,EAAK,GAAA;EAYe;EAVpB,IAAA;EAU6B;EAR7B,OAAA,EAAS,OAAA;EAQoE;EAN7E,MAAA,EAAQ,MAAA;AAAA;;;;KAME,UAAA,IAAc,GAAA,EAAK,iBAAA,KAAsB,QAAA,UAAkB,OAAA,CAAQ,QAAA;;;;ACW/E;;;;;;;;;;;;;;iBDkBgB,gBAAA,CAAA,GAAoB,MAAA;;;UClBnB,cAAA;EDX8D;ECa7E,GAAA,EAAK,WAAA;EDbuE;ECe5E,MAAA,EAAQ,WAAA;EDfqB;;;;;;;AA6B/B;ECLE,QAAA;;EAEA,WAAA;EDGwC;ECDxC,UAAA,GAAa,UAAA;;;AAjBf;;;EAuBE,IAAA;EAnBQ;;;;;;;;;;;;;;;EAmCR,aAAA;AAAA;AAAA,iBAGc,aAAA,CAAc,OAAA,EAAS,cAAA,IAAkB,GAAA,EAAK,OAAA,KAAY,OAAA,CAAQ,QAAA;;;;;;;ADrElF;;;;cEda,gBAAA;AAAA,UAaI,YAAA;EACf,IAAA;EACA,GAAA;EACA,OAAA;AAAA;;;;;UAOe,gBAAA;EFDN;EEGT,KAAA;AAAA;AAAA,iBAGc,eAAA,CAAgB,QAAA,WAAmB,gBAAA;AAAA,iBAgBnC,eAAA,CAAgB,QAAA,UAAkB,IAAA,EAAM,YAAA;AFdxD;AAAA,iBEsBgB,uBAAA,CAAwB,QAAA,EAAU,gBAAA,EAAkB,IAAA,EAAM,YAAA;;;;;;;;iBAY1D,YAAA,CACd,WAAA,UACA,UAAA,EAAY,MAAA;;;KChBF,iBAAA;AAAA,UAEK,aAAA;EFTf;EEWA,IAAA;EFTA;EEWA,OAAA,GAAU,iBAAA;AAAA;AAAA,UAGK,UAAA;EAAA,SACN,QAAA;EAAA,SACA,IAAA;EAAA,SACA,OAAA,EAAS,iBAAA;AAAA;;;AFqBpB;;;;;;iBERgB,MAAA,WAAiB,KAAA,GAAQ,KAAA,CAAA,CACvC,MAAA,QAAc,OAAA;EAAU,OAAA,EAAS,WAAA,CAAY,CAAA;AAAA,IAAO,WAAA,CAAY,CAAA,IAChE,OAAA,EAAS,aAAA,GACR,WAAA,CAAY,CAAA,IAAK,UAAA;;;;;;;AHhEpB;;;;;;;;;;;;;;;;;;;;AAgBA;;;;;UINiB,gBAAA;EJMsD;EIJrE,OAAA,GAAU,GAAA,EAAK,OAAA,KAAY,OAAA,CAAQ,QAAA;EJIyC;EIF5E,KAAA,+BAAoC,OAAA;EJEZ;EIAxB,MAAA;EJAqE;EIErE,MAAA;EJFqF;;AA6BvF;;EItBE,MAAA,IAAU,IAAA,UAAc,IAAA,8BAAkC,OAAA;AAAA;AAAA,UAG3C,eAAA;;EAEf,KAAA;;EAEA,MAAA;IAAU,IAAA;IAAc,KAAA;EAAA;EHChB;EGCR,OAAA;AAAA;;;;;;;;;;;;iBAcoB,SAAA,CAAU,OAAA,EAAS,gBAAA,GAAmB,OAAA,CAAQ,eAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/server",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.2",
|
|
4
4
|
"description": "SSR handler, SSG prerender, and island architecture for Pyreon",
|
|
5
5
|
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/server#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"prepublishOnly": "bun run build"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@pyreon/core": "^0.12.
|
|
51
|
-
"@pyreon/head": "^0.12.
|
|
52
|
-
"@pyreon/reactivity": "^0.12.
|
|
53
|
-
"@pyreon/router": "^0.12.
|
|
54
|
-
"@pyreon/runtime-dom": "^0.12.
|
|
55
|
-
"@pyreon/runtime-server": "^0.12.
|
|
50
|
+
"@pyreon/core": "^0.12.2",
|
|
51
|
+
"@pyreon/head": "^0.12.2",
|
|
52
|
+
"@pyreon/reactivity": "^0.12.2",
|
|
53
|
+
"@pyreon/router": "^0.12.2",
|
|
54
|
+
"@pyreon/runtime-dom": "^0.12.2",
|
|
55
|
+
"@pyreon/runtime-server": "^0.12.2"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/handler.ts
CHANGED
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
processCompiledTemplate,
|
|
45
45
|
} from './html'
|
|
46
46
|
import type { Middleware, MiddlewareContext } from './middleware'
|
|
47
|
+
import { provideRequestLocals } from './middleware'
|
|
47
48
|
|
|
48
49
|
const __DEV__ = typeof process !== 'undefined' && process.env.NODE_ENV !== 'production'
|
|
49
50
|
|
|
@@ -71,6 +72,22 @@ export interface HandlerOptions {
|
|
|
71
72
|
* "stream" — progressive streaming via renderToStream (Suspense out-of-order)
|
|
72
73
|
*/
|
|
73
74
|
mode?: 'string' | 'stream'
|
|
75
|
+
/**
|
|
76
|
+
* Collect CSS styles after rendering. Called after renderToString/renderWithHead.
|
|
77
|
+
* Return a `<style>` tag string to inject into `<head>`.
|
|
78
|
+
* Used by @pyreon/styler's sheet.getStyleTag() to prevent FOUC in SSG.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* import { sheet } from '@pyreon/styler'
|
|
82
|
+
* createHandler({
|
|
83
|
+
* collectStyles: () => {
|
|
84
|
+
* const tag = sheet.getStyleTag()
|
|
85
|
+
* sheet.reset()
|
|
86
|
+
* return tag
|
|
87
|
+
* },
|
|
88
|
+
* })
|
|
89
|
+
*/
|
|
90
|
+
collectStyles?: () => string
|
|
74
91
|
}
|
|
75
92
|
|
|
76
93
|
export function createHandler(options: HandlerOptions): (req: Request) => Promise<Response> {
|
|
@@ -81,6 +98,7 @@ export function createHandler(options: HandlerOptions): (req: Request) => Promis
|
|
|
81
98
|
clientEntry = '/src/entry-client.ts',
|
|
82
99
|
middleware = [],
|
|
83
100
|
mode = 'string',
|
|
101
|
+
collectStyles,
|
|
84
102
|
} = options
|
|
85
103
|
|
|
86
104
|
// Pre-compile once at handler creation — avoids 3x string scan per request
|
|
@@ -110,6 +128,10 @@ export function createHandler(options: HandlerOptions): (req: Request) => Promis
|
|
|
110
128
|
|
|
111
129
|
return runWithRequestContext(async () => {
|
|
112
130
|
try {
|
|
131
|
+
// Bridge middleware locals → Pyreon context system so components
|
|
132
|
+
// can access per-request data (CSP nonce, auth user, etc.)
|
|
133
|
+
provideRequestLocals(ctx.locals)
|
|
134
|
+
|
|
113
135
|
// Pre-run loaders so data is available during render
|
|
114
136
|
await prefetchLoaderData(router as never, path)
|
|
115
137
|
|
|
@@ -122,9 +144,16 @@ export function createHandler(options: HandlerOptions): (req: Request) => Promis
|
|
|
122
144
|
|
|
123
145
|
// ── String mode (default) ─────────────────────────────────────────────
|
|
124
146
|
const { html: appHtml, head } = await renderWithHead(app)
|
|
147
|
+
|
|
148
|
+
// Collect CSS-in-JS styles if a collector was provided.
|
|
149
|
+
// The consumer passes collectStyles (e.g. sheet.getStyleTag from @pyreon/styler)
|
|
150
|
+
// to inject scoped CSS into <head> and prevent FOUC in SSG pages.
|
|
151
|
+
const styleTag = collectStyles ? collectStyles() : ''
|
|
152
|
+
|
|
125
153
|
const loaderData = serializeLoaderData(router as never)
|
|
126
154
|
const scripts = buildScriptsFast(clientEntryTag, loaderData)
|
|
127
|
-
const
|
|
155
|
+
const headWithStyles = styleTag ? `${styleTag}\n${head}` : head
|
|
156
|
+
const fullHtml = processCompiledTemplate(compiled, { head: headWithStyles, app: appHtml, scripts })
|
|
128
157
|
|
|
129
158
|
return new Response(fullHtml, { status: 200, headers: ctx.headers })
|
|
130
159
|
} catch (err) {
|
package/src/index.ts
CHANGED
|
@@ -70,6 +70,7 @@ export { island } from './island'
|
|
|
70
70
|
|
|
71
71
|
// Middleware
|
|
72
72
|
export type { Middleware, MiddlewareContext } from './middleware'
|
|
73
|
+
export { useRequestLocals } from './middleware'
|
|
73
74
|
export type { PrerenderOptions, PrerenderResult } from './ssg'
|
|
74
75
|
// SSG
|
|
75
76
|
export { prerender } from './ssg'
|
package/src/middleware.ts
CHANGED
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
* })
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
+
import { createContext, useContext, provide } from '@pyreon/core'
|
|
23
|
+
|
|
22
24
|
export interface MiddlewareContext {
|
|
23
25
|
/** The incoming request */
|
|
24
26
|
req: Request
|
|
@@ -36,3 +38,43 @@ export interface MiddlewareContext {
|
|
|
36
38
|
* Middleware function. Return a Response to short-circuit, or void to continue.
|
|
37
39
|
*/
|
|
38
40
|
export type Middleware = (ctx: MiddlewareContext) => Response | void | Promise<Response | void>
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Context for per-request locals — populated by the SSR handler from
|
|
44
|
+
* middleware `ctx.locals`. Components access it via `useRequestLocals()`.
|
|
45
|
+
*
|
|
46
|
+
* This bridges the middleware → component gap: middleware sets `ctx.locals`,
|
|
47
|
+
* the handler provides it into the Pyreon context system, and components
|
|
48
|
+
* read it without coupling to the middleware layer.
|
|
49
|
+
*/
|
|
50
|
+
export const RequestLocalsCtx = createContext<Record<string, unknown>>({})
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Read per-request locals inside a component (SSR only).
|
|
54
|
+
*
|
|
55
|
+
* Returns the `ctx.locals` object populated by middleware.
|
|
56
|
+
* On the client, returns an empty object.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* import { useRequestLocals } from "@pyreon/server"
|
|
61
|
+
*
|
|
62
|
+
* function MyComponent() {
|
|
63
|
+
* const locals = useRequestLocals()
|
|
64
|
+
* const nonce = locals.cspNonce as string ?? ''
|
|
65
|
+
* return <script nonce={nonce}>...</script>
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function useRequestLocals(): Record<string, unknown> {
|
|
70
|
+
return useContext(RequestLocalsCtx)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Provide request locals into the component tree.
|
|
75
|
+
* Called by the SSR handler — not for direct use.
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
export function provideRequestLocals(locals: Record<string, unknown>): void {
|
|
79
|
+
provide(RequestLocalsCtx, locals)
|
|
80
|
+
}
|