@kiln-js/adapter-node 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +62 -3
- package/dist/index.d.ts +8 -0
- package/dist/index.js +163 -0
- package/package.json +25 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zxsty
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @kiln-js/adapter-node
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@kiln-js%2Fadapter-node)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
Node.js standalone server output for the Kiln framework
|
|
7
|
+
|
|
8
|
+
Part of [Kiln](https://github.com/kiln-js/kiln), a file-based server-side rendering framework for React and Vite.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
bun add -d @kiln-js/adapter-node
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { adaptNode } from "@kiln-js/adapter-node";
|
|
20
|
+
|
|
21
|
+
await adaptNode({
|
|
22
|
+
root: process.cwd(),
|
|
23
|
+
clientOut: "dist/client",
|
|
24
|
+
serverOut: "dist/server",
|
|
25
|
+
clientRel: "app/client.tsx",
|
|
26
|
+
port: 3000,
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then start the generated server:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
node dist/node/server.mjs
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`adaptNode` writes `dist/node/server.mjs`, which serves static assets from `clientOut` (with immutable caching for hashed `assets/` files, `ETag` / `304` support, and level-1 `gzip` compression for compressible responses over 1 KB) and delegates remaining requests to `createHandler` from `serverOut/server.js`.
|
|
37
|
+
|
|
38
|
+
### Port precedence and configuration
|
|
39
|
+
|
|
40
|
+
When `dist/node/server.mjs` is executed directly, the listening port resolves in order:
|
|
41
|
+
|
|
42
|
+
1. `process.env.PORT`
|
|
43
|
+
2. `server.port` in `.kiln/kiln.config.cjs` (if present)
|
|
44
|
+
3. The `port` option passed to `adaptNode` (defaults to `3000`)
|
|
45
|
+
|
|
46
|
+
You can also import `start(port?)` from `dist/node/server.mjs` to start the server programmatically and receive the underlying `http.Server` instance.
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
Node.js at runtime (with `node:http`, `node:fs`, `node:path`, `node:zlib`, `node:url`, and `node:module` support) and a Vite client manifest at `<clientOut>/.vite/manifest.json` alongside a built `<serverOut>/server.js` exporting `createHandler`.
|
|
51
|
+
|
|
52
|
+
## Documentation
|
|
53
|
+
|
|
54
|
+
See the [Kiln repository](https://github.com/kiln-js/kiln) for the full API reference and guides.
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
This repository is generated from the Kiln monorepo. Make changes there, then run `scripts/publish-repo.ps1` to publish them here. Report issues in this repository, and send pull requests to the monorepo.
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// packages/adapter-node/src/index.ts
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
async function adaptNode(opts) {
|
|
5
|
+
const outDir = path.join(opts.root, "dist", "node");
|
|
6
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
7
|
+
const manifestPath = path.join(opts.clientOut, ".vite", "manifest.json");
|
|
8
|
+
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
9
|
+
const entry = manifest[opts.clientRel];
|
|
10
|
+
if (!entry) throw new Error(`No manifest entry for ${opts.clientRel}`);
|
|
11
|
+
const assets = {
|
|
12
|
+
head: (entry.css ?? []).map((href) => `<link rel="stylesheet" href="/${href}">`).join("\n"),
|
|
13
|
+
entry: `/${entry.file}`
|
|
14
|
+
};
|
|
15
|
+
const port = opts.port ?? 3e3;
|
|
16
|
+
const serverJs = path.relative(outDir, path.join(opts.serverOut, "server.js")).replace(/\\/g, "/");
|
|
17
|
+
const clientRel = path.relative(outDir, opts.clientOut).replace(/\\/g, "/");
|
|
18
|
+
const configRel = path.relative(outDir, path.join(opts.root, ".kiln", "kiln.config.cjs")).replace(/\\/g, "/");
|
|
19
|
+
const source = `import http from "node:http";
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import { gzipSync } from "node:zlib";
|
|
23
|
+
import { pathToFileURL, fileURLToPath } from "node:url";
|
|
24
|
+
import { createRequire } from "node:module";
|
|
25
|
+
|
|
26
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
27
|
+
const clientOut = path.resolve(here, ${JSON.stringify(clientRel)});
|
|
28
|
+
const require = createRequire(import.meta.url);
|
|
29
|
+
|
|
30
|
+
function readConfig() {
|
|
31
|
+
try {
|
|
32
|
+
const mod = require(path.resolve(here, ${JSON.stringify(configRel)}));
|
|
33
|
+
return mod.default ?? mod;
|
|
34
|
+
} catch {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const assets = ${JSON.stringify(assets)};
|
|
40
|
+
const { createHandler } = await import(pathToFileURL(path.resolve(here, ${JSON.stringify(serverJs)})).href);
|
|
41
|
+
const handler = createHandler(assets);
|
|
42
|
+
|
|
43
|
+
const MIME = {
|
|
44
|
+
".html": "text/html; charset=utf-8",
|
|
45
|
+
".js": "text/javascript; charset=utf-8",
|
|
46
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
47
|
+
".css": "text/css; charset=utf-8",
|
|
48
|
+
".json": "application/json; charset=utf-8",
|
|
49
|
+
".svg": "image/svg+xml",
|
|
50
|
+
".png": "image/png",
|
|
51
|
+
".jpg": "image/jpeg",
|
|
52
|
+
".jpeg": "image/jpeg",
|
|
53
|
+
".webp": "image/webp",
|
|
54
|
+
".ico": "image/x-icon",
|
|
55
|
+
".woff2": "font/woff2",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const fileCache = new Map();
|
|
59
|
+
|
|
60
|
+
function serveFile(req, filePath) {
|
|
61
|
+
let stat;
|
|
62
|
+
try { stat = fs.statSync(filePath); } catch { return null; }
|
|
63
|
+
if (!stat.isFile()) return null;
|
|
64
|
+
const type = MIME[path.extname(filePath).toLowerCase()] || "application/octet-stream";
|
|
65
|
+
const etag = 'W/"' + stat.size + "-" + stat.mtimeMs + '"';
|
|
66
|
+
const hashed = filePath.includes(path.sep + "assets" + path.sep);
|
|
67
|
+
const headers = {
|
|
68
|
+
"content-type": type,
|
|
69
|
+
etag,
|
|
70
|
+
"cache-control": hashed ? "public, max-age=31536000, immutable" : "no-cache",
|
|
71
|
+
};
|
|
72
|
+
if (req.headers["if-none-match"] === etag) return { status: 304, headers, body: null };
|
|
73
|
+
const key = filePath + "|" + etag;
|
|
74
|
+
let body = fileCache.get(key);
|
|
75
|
+
if (!body) {
|
|
76
|
+
body = fs.readFileSync(filePath);
|
|
77
|
+
const compressible = /^(text\\/|application\\/(javascript|json)|image\\/svg)/.test(type);
|
|
78
|
+
const accept = String(req.headers["accept-encoding"] || "");
|
|
79
|
+
if (compressible && body.length >= 1024 && /\\bgzip\\b/.test(accept)) {
|
|
80
|
+
body = gzipSync(body, { level: 1 });
|
|
81
|
+
headers["content-encoding"] = "gzip";
|
|
82
|
+
headers["vary"] = "Accept-Encoding";
|
|
83
|
+
}
|
|
84
|
+
if (fileCache.size > 64) fileCache.delete(fileCache.keys().next().value);
|
|
85
|
+
fileCache.set(key, body);
|
|
86
|
+
}
|
|
87
|
+
headers["content-length"] = String(body.length);
|
|
88
|
+
return { status: 200, headers, body };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function pathnameOf(url) {
|
|
92
|
+
const q = url.indexOf("?");
|
|
93
|
+
return q === -1 ? url : url.slice(0, q);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function start(port = Number(process.env.PORT || readConfig().server?.port || ${port})) {
|
|
97
|
+
const server = http.createServer(async (req, res) => {
|
|
98
|
+
try {
|
|
99
|
+
const pathname = pathnameOf(req.url || "/");
|
|
100
|
+
if (pathname !== "/" && path.extname(pathname)) {
|
|
101
|
+
let decoded;
|
|
102
|
+
try { decoded = decodeURIComponent(pathname); } catch { res.statusCode = 400; res.end("Bad Request"); return; }
|
|
103
|
+
const filePath = path.join(clientOut, decoded);
|
|
104
|
+
if (filePath.startsWith(clientOut + path.sep)) {
|
|
105
|
+
const file = serveFile(req, filePath);
|
|
106
|
+
if (file) {
|
|
107
|
+
res.statusCode = file.status;
|
|
108
|
+
for (const [k, v] of Object.entries(file.headers)) res.setHeader(k, v);
|
|
109
|
+
res.end(file.body);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const headers = new Headers();
|
|
115
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
116
|
+
if (v == null) continue;
|
|
117
|
+
for (const item of Array.isArray(v) ? v : [v]) headers.append(k, item);
|
|
118
|
+
}
|
|
119
|
+
const method = req.method || "GET";
|
|
120
|
+
const init = { method, headers };
|
|
121
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
122
|
+
const chunks = [];
|
|
123
|
+
for await (const chunk of req) chunks.push(chunk);
|
|
124
|
+
init.body = Buffer.concat(chunks);
|
|
125
|
+
init.duplex = "half";
|
|
126
|
+
}
|
|
127
|
+
const response = await handler(new Request("http://localhost:" + port + (req.url || "/"), init));
|
|
128
|
+
res.statusCode = response.status;
|
|
129
|
+
response.headers.forEach((value, key) => {
|
|
130
|
+
if (key !== "set-cookie") res.setHeader(key, value);
|
|
131
|
+
});
|
|
132
|
+
const cookies = response.headers.getSetCookie?.() ?? [];
|
|
133
|
+
if (cookies.length) res.setHeader("set-cookie", cookies);
|
|
134
|
+
if (!response.body) { res.end(); return; }
|
|
135
|
+
const reader = response.body.getReader();
|
|
136
|
+
for (;;) {
|
|
137
|
+
const { done, value } = await reader.read();
|
|
138
|
+
if (done) break;
|
|
139
|
+
res.write(value);
|
|
140
|
+
}
|
|
141
|
+
res.end();
|
|
142
|
+
} catch (err) {
|
|
143
|
+
console.error("[kiln] request failed", err);
|
|
144
|
+
if (!res.headersSent) res.statusCode = 500;
|
|
145
|
+
res.end("Internal Server Error");
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
server.keepAliveTimeout = 72000;
|
|
149
|
+
server.on("connection", (socket) => socket.setNoDelay(true));
|
|
150
|
+
server.listen(port, () => {
|
|
151
|
+
console.log("[kiln] node server http://localhost:" + port);
|
|
152
|
+
});
|
|
153
|
+
return server;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const invoked = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
157
|
+
if (invoked) start();
|
|
158
|
+
`;
|
|
159
|
+
fs.writeFileSync(path.join(outDir, "server.mjs"), source);
|
|
160
|
+
}
|
|
161
|
+
export {
|
|
162
|
+
adaptNode
|
|
163
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiln-js/adapter-node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Node.js production server for the Kiln framework",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"repository": "github:kiln-js/adapter-node",
|
|
14
|
+
"homepage": "https://kiln-js.vercel.app",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node ../../scripts/build.mjs adapter-node",
|
|
23
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^22.0.0"
|
|
27
|
+
}
|
|
6
28
|
}
|