@kiln-js/server 0.0.0-stage → 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.
- package/dist/api.d.ts +2 -0
- package/dist/app.d.ts +10 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +189 -0
- package/dist/render.d.ts +2 -0
- package/dist/template.d.ts +1 -0
- package/dist/types.d.ts +42 -0
- package/package.json +31 -5
- package/README.md +0 -3
package/dist/api.d.ts
ADDED
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ApiRoute, KilnAssets, RouteEntry } from "./types";
|
|
2
|
+
export interface AppOptions {
|
|
3
|
+
routes?: RouteEntry[];
|
|
4
|
+
api?: ApiRoute[];
|
|
5
|
+
assets: KilnAssets;
|
|
6
|
+
template?: string;
|
|
7
|
+
notFound?: string;
|
|
8
|
+
defaultTitle?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function createAppHandler(options: AppOptions): (request: Request) => Promise<Response>;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/render.tsx
|
|
3
|
+
import { renderToString } from "react-dom/server";
|
|
4
|
+
|
|
5
|
+
// ../core/dist/index.js
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
import { createContext, useContext } from "react";
|
|
8
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as createContext2, useContext as useContext2, useEffect, useLayoutEffect } from "react";
|
|
10
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
11
|
+
function matchRoute(routes, pathname) {
|
|
12
|
+
const parts = pathname.split("/").filter(Boolean);
|
|
13
|
+
for (const route of routes) {
|
|
14
|
+
const segs = route.pattern.split("/").filter(Boolean);
|
|
15
|
+
const params = {};
|
|
16
|
+
let ok = true;
|
|
17
|
+
let i = 0;
|
|
18
|
+
for (const seg of segs) {
|
|
19
|
+
if (seg.startsWith("*")) {
|
|
20
|
+
params[seg.slice(1)] = parts.slice(i).map(decodeURIComponent).join("/");
|
|
21
|
+
i = parts.length;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
if (i >= parts.length) {
|
|
25
|
+
ok = false;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
if (seg.startsWith(":"))
|
|
29
|
+
params[seg.slice(1)] = decodeURIComponent(parts[i]);
|
|
30
|
+
else if (seg !== parts[i]) {
|
|
31
|
+
ok = false;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
if (ok && i === parts.length)
|
|
37
|
+
return { route, params };
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function compose(page, layouts) {
|
|
42
|
+
let tree = page;
|
|
43
|
+
for (const Layout of [...layouts].reverse())
|
|
44
|
+
tree = /* @__PURE__ */ jsx(Layout, {
|
|
45
|
+
children: tree
|
|
46
|
+
});
|
|
47
|
+
return tree;
|
|
48
|
+
}
|
|
49
|
+
var RouteContext = createContext({ pathname: "/", params: {} });
|
|
50
|
+
function RouteProvider({ value, children }) {
|
|
51
|
+
return /* @__PURE__ */ jsx2(RouteContext.Provider, {
|
|
52
|
+
value,
|
|
53
|
+
children
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
var HeadContext = createContext2(null);
|
|
57
|
+
function HeadProvider({ value, children }) {
|
|
58
|
+
return /* @__PURE__ */ jsx3(HeadContext.Provider, {
|
|
59
|
+
value,
|
|
60
|
+
children
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/api.ts
|
|
65
|
+
var METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
|
|
66
|
+
async function handleApi(request, pathname, routes) {
|
|
67
|
+
const match = matchRoute(routes, pathname);
|
|
68
|
+
if (!match)
|
|
69
|
+
return null;
|
|
70
|
+
const mod = await match.route.load();
|
|
71
|
+
const method = request.method.toUpperCase();
|
|
72
|
+
let handler = mod[method];
|
|
73
|
+
if (!handler && method === "HEAD")
|
|
74
|
+
handler = mod.GET;
|
|
75
|
+
if (typeof handler !== "function") {
|
|
76
|
+
const allow = METHODS.filter((m) => typeof mod[m] === "function");
|
|
77
|
+
return Response.json({ error: `Method ${method} not allowed` }, { status: 405, headers: { allow: allow.join(", ") } });
|
|
78
|
+
}
|
|
79
|
+
const ctx = { params: match.params, request };
|
|
80
|
+
const result = await handler(ctx);
|
|
81
|
+
if (result instanceof Response)
|
|
82
|
+
return result;
|
|
83
|
+
if (result === undefined)
|
|
84
|
+
return new Response(null, { status: 204 });
|
|
85
|
+
return Response.json(result);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/render.tsx
|
|
89
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
90
|
+
var htmlHeaders = { "content-type": "text/html; charset=utf-8" };
|
|
91
|
+
var serialize = (v) => JSON.stringify(v).replace(/</g, "\\u003c");
|
|
92
|
+
var escapeHtml = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
93
|
+
function setTitle(template, title) {
|
|
94
|
+
const tag = `<title>${escapeHtml(title)}</title>`;
|
|
95
|
+
return /<title>[\s\S]*?<\/title>/i.test(template) ? template.replace(/<title>[\s\S]*?<\/title>/i, () => tag) : template.replace("</head>", () => `${tag}
|
|
96
|
+
</head>`);
|
|
97
|
+
}
|
|
98
|
+
function createHandler(ctx) {
|
|
99
|
+
return async function handle(request) {
|
|
100
|
+
const url = new URL(request.url);
|
|
101
|
+
const routes = await ctx.manifest();
|
|
102
|
+
if (url.pathname === "/__kiln/data") {
|
|
103
|
+
const match = matchRoute(routes, url.searchParams.get("path") ?? "/");
|
|
104
|
+
if (!match?.route.load)
|
|
105
|
+
return Response.json(null);
|
|
106
|
+
const mod = await match.route.load();
|
|
107
|
+
const data = mod.loader ? await mod.loader({ params: match.params, request }) : null;
|
|
108
|
+
return Response.json(data ?? null);
|
|
109
|
+
}
|
|
110
|
+
if (ctx.api) {
|
|
111
|
+
const apiResponse = await handleApi(request, url.pathname, await ctx.api());
|
|
112
|
+
if (apiResponse)
|
|
113
|
+
return apiResponse;
|
|
114
|
+
}
|
|
115
|
+
const match = matchRoute(routes, url.pathname);
|
|
116
|
+
if (!match) {
|
|
117
|
+
return new Response(await ctx.notFound(), { status: 404, headers: htmlHeaders });
|
|
118
|
+
}
|
|
119
|
+
const page = await renderRoute(match.route, match.params, request, url.pathname, ctx);
|
|
120
|
+
return new Response(page, { status: 200, headers: htmlHeaders });
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async function renderRoute(route, params, request, pathname, ctx) {
|
|
124
|
+
const mod = route.load ? await route.load() : null;
|
|
125
|
+
const data = mod?.loader ? await mod.loader({ params, request }) : null;
|
|
126
|
+
const head = {};
|
|
127
|
+
let appHtml = "";
|
|
128
|
+
if (route.mode !== "client" && mod) {
|
|
129
|
+
const layouts = await Promise.all(route.layouts.map((l) => l().then((m) => m.default)));
|
|
130
|
+
const Page = mod.default;
|
|
131
|
+
appHtml = renderToString(/* @__PURE__ */ jsx4(HeadProvider, {
|
|
132
|
+
value: head,
|
|
133
|
+
children: /* @__PURE__ */ jsx4(RouteProvider, {
|
|
134
|
+
value: { pathname, params },
|
|
135
|
+
children: compose(/* @__PURE__ */ jsx4(Page, {
|
|
136
|
+
params,
|
|
137
|
+
data
|
|
138
|
+
}), layouts)
|
|
139
|
+
})
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
const template = await ctx.template();
|
|
143
|
+
const assets = await ctx.assets();
|
|
144
|
+
const title = head.title ?? ctx.defaultTitle ?? "Kiln App";
|
|
145
|
+
const boot = `window.__KILN__ = ${serialize({ mode: route.mode, pattern: route.pattern, data })};`;
|
|
146
|
+
const script = route.mode === "server" ? "" : `<script type="module" src="${assets.entry}"></script>`;
|
|
147
|
+
const page = template.replace("<!--kiln-head-->", () => assets.head).replace("<!--kiln-app-->", () => appHtml).replace("<!--kiln-data-->", () => `<script>${boot}</script>${script}`);
|
|
148
|
+
return setTitle(page, title);
|
|
149
|
+
}
|
|
150
|
+
// src/template.ts
|
|
151
|
+
var defaultTemplate = `<!doctype html>
|
|
152
|
+
<html lang="en">
|
|
153
|
+
<head>
|
|
154
|
+
<meta charset="UTF-8" />
|
|
155
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
156
|
+
<title>Kiln App</title>
|
|
157
|
+
<!--kiln-head-->
|
|
158
|
+
</head>
|
|
159
|
+
<body>
|
|
160
|
+
<div id="root"><!--kiln-app--></div>
|
|
161
|
+
<!--kiln-data-->
|
|
162
|
+
</body>
|
|
163
|
+
</html>`;
|
|
164
|
+
|
|
165
|
+
// src/app.ts
|
|
166
|
+
function injectedRoutes() {
|
|
167
|
+
const routes = globalThis.__KILN_ROUTES__;
|
|
168
|
+
if (!routes) {
|
|
169
|
+
throw new Error("No routes found. Pass createAppHandler({ routes, assets }) or run through the Kiln plugin.");
|
|
170
|
+
}
|
|
171
|
+
return routes;
|
|
172
|
+
}
|
|
173
|
+
function createAppHandler(options) {
|
|
174
|
+
const routes = options.routes ?? injectedRoutes();
|
|
175
|
+
const api = options.api ?? globalThis.__KILN_API__ ?? [];
|
|
176
|
+
return createHandler({
|
|
177
|
+
manifest: async () => routes,
|
|
178
|
+
api: async () => api,
|
|
179
|
+
template: async () => options.template ?? defaultTemplate,
|
|
180
|
+
assets: async () => options.assets,
|
|
181
|
+
notFound: async () => options.notFound ?? "<h1>404: Not found</h1>",
|
|
182
|
+
defaultTitle: options.defaultTitle ?? "Kiln App"
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
export {
|
|
186
|
+
createAppHandler,
|
|
187
|
+
createHandler,
|
|
188
|
+
defaultTemplate
|
|
189
|
+
};
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const defaultTemplate = "<!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 <title>Kiln App</title>\n <!--kiln-head-->\n </head>\n <body>\n <div id=\"root\"><!--kiln-app--></div>\n <!--kiln-data-->\n </body>\n</html>";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ComponentType } from "react";
|
|
2
|
+
export type RenderMode = "universal" | "server" | "client";
|
|
3
|
+
export interface LoaderContext {
|
|
4
|
+
params: Record<string, string>;
|
|
5
|
+
request: Request;
|
|
6
|
+
}
|
|
7
|
+
export interface PageModule {
|
|
8
|
+
default: ComponentType<any>;
|
|
9
|
+
loader?: (ctx: LoaderContext) => unknown;
|
|
10
|
+
}
|
|
11
|
+
export interface LayoutModule {
|
|
12
|
+
default: ComponentType<any>;
|
|
13
|
+
}
|
|
14
|
+
export interface RouteEntry {
|
|
15
|
+
pattern: string;
|
|
16
|
+
mode: RenderMode;
|
|
17
|
+
load: (() => Promise<PageModule>) | null;
|
|
18
|
+
layouts: (() => Promise<LayoutModule>)[];
|
|
19
|
+
}
|
|
20
|
+
export interface ApiContext {
|
|
21
|
+
params: Record<string, string>;
|
|
22
|
+
request: Request;
|
|
23
|
+
}
|
|
24
|
+
export interface ApiModule {
|
|
25
|
+
[method: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
export interface ApiRoute {
|
|
28
|
+
pattern: string;
|
|
29
|
+
load: () => Promise<ApiModule>;
|
|
30
|
+
}
|
|
31
|
+
export interface KilnAssets {
|
|
32
|
+
head: string;
|
|
33
|
+
entry: string;
|
|
34
|
+
}
|
|
35
|
+
export interface KilnContext {
|
|
36
|
+
manifest(): Promise<RouteEntry[]>;
|
|
37
|
+
api?(): Promise<ApiRoute[]>;
|
|
38
|
+
template(): Promise<string>;
|
|
39
|
+
assets(): Promise<KilnAssets>;
|
|
40
|
+
notFound(): Promise<string>;
|
|
41
|
+
defaultTitle?: string;
|
|
42
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"
|
|
5
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@kiln-js/server",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target bun --external react --external react-dom \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
|
|
16
|
+
"test": "bun test",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@kiln/core": "*"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": "\u003e=18",
|
|
24
|
+
"react-dom": "\u003e=18"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/react": "^18.3.0",
|
|
28
|
+
"@types/react-dom": "^18.3.0",
|
|
29
|
+
"react": "^18.3.1",
|
|
30
|
+
"react-dom": "^18.3.1"
|
|
31
|
+
}
|
|
6
32
|
}
|
package/README.md
DELETED