@kiln-js/adapter-bun 0.0.0-stage → 0.4.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/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
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @kiln-js/adapter-bun
2
+
3
+ [![npm](https://img.shields.io/npm/v/%40kiln-js%2Fadapter-bun)](https://www.npmjs.com/package/@kiln-js/adapter-bun)
4
+ [![license](https://img.shields.io/npm/l/%40kiln-js%2Fadapter-bun)](LICENSE)
5
+
6
+ Bun 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-bun
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { adaptBun } from "@kiln-js/adapter-bun";
20
+
21
+ await adaptBun({
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
+ bun dist/bun/server.mjs
34
+ ```
35
+
36
+ `adaptBun` writes `dist/bun/server.mjs`, which serves static assets from `clientOut` via `Bun.file` (avoiding extra filesystem checks on HTML routes) and delegates all other requests to `createHandler` from `serverOut/server.js` using `Bun.serve`.
37
+
38
+ ### Port precedence and configuration
39
+
40
+ When `dist/bun/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 `adaptBun` (defaults to `3000`)
45
+
46
+ You can also import `start(port?)` from `dist/bun/server.mjs` to start the server programmatically and receive the underlying `Bun.Server` instance.
47
+
48
+ ## Requirements
49
+
50
+ Bun at runtime (for `Bun.serve` and `Bun.file`) 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
@@ -0,0 +1,8 @@
1
+ export interface BunAdapterOptions {
2
+ root: string;
3
+ clientOut: string;
4
+ serverOut: string;
5
+ clientRel: string;
6
+ port?: number;
7
+ }
8
+ export declare function adaptBun(opts: BunAdapterOptions): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
1
+ // packages/adapter-bun/src/index.ts
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ async function adaptBun(opts) {
5
+ const outDir = path.join(opts.root, "dist", "bun");
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 path from "node:path";
20
+ import { pathToFileURL, fileURLToPath } from "node:url";
21
+ import { createRequire } from "node:module";
22
+
23
+ const here = path.dirname(fileURLToPath(import.meta.url));
24
+ const clientOut = path.resolve(here, ${JSON.stringify(clientRel)});
25
+ const require = createRequire(import.meta.url);
26
+
27
+ function readConfig() {
28
+ try {
29
+ const mod = require(path.resolve(here, ${JSON.stringify(configRel)}));
30
+ return mod.default ?? mod;
31
+ } catch {
32
+ return {};
33
+ }
34
+ }
35
+
36
+ const assets = ${JSON.stringify(assets)};
37
+ const { createHandler } = await import(pathToFileURL(path.resolve(here, ${JSON.stringify(serverJs)})).href);
38
+ const handler = createHandler(assets);
39
+
40
+ function pathnameOf(url) {
41
+ const q = url.indexOf("?");
42
+ const h = url.indexOf("#");
43
+ let stop = url.length;
44
+ if (q !== -1) stop = q;
45
+ if (h !== -1 && h < stop) stop = h;
46
+ return url.slice(0, stop);
47
+ }
48
+
49
+ export function start(port = Number(process.env.PORT || readConfig().server?.port || ${port})) {
50
+ return Bun.serve({
51
+ port,
52
+ async fetch(req) {
53
+ const url = new URL(req.url);
54
+ const pathname = pathnameOf(url.pathname + url.search);
55
+ if (pathname !== "/" && path.extname(pathname)) {
56
+ let decoded;
57
+ try { decoded = decodeURIComponent(pathname); } catch { return new Response("Bad Request", { status: 400 }); }
58
+ const filePath = path.join(clientOut, decoded);
59
+ if (filePath.startsWith(clientOut + path.sep)) {
60
+ const file = Bun.file(filePath);
61
+ if (await file.exists()) return new Response(file);
62
+ }
63
+ }
64
+ return handler(req);
65
+ },
66
+ });
67
+ }
68
+
69
+ const invoked = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
70
+ if (invoked) {
71
+ const server = start();
72
+ console.log("[kiln] bun server http://localhost:" + server.port);
73
+ }
74
+ `;
75
+ fs.writeFileSync(path.join(outDir, "server.mjs"), source);
76
+ }
77
+ export {
78
+ adaptBun
79
+ };
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
- {
2
- "name": "@kiln-js/adapter-bun",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@kiln-js/adapter-bun",
3
+ "version": "0.4.2",
4
+ "description": "Bun 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-bun",
14
+ "homepage": "https://kiln-js.vercel.app",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "engines": {
19
+ "node": "\u003e=18"
20
+ },
21
+ "scripts": {
22
+ "build": "node ../../scripts/build.mjs adapter-bun",
23
+ "typecheck": "tsc -p tsconfig.json --noEmit"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^22.0.0"
27
+ }
28
+ }