bini-server 1.0.4 → 1.0.6
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/README.md +5 -6
- package/dist/cli.js +17 -22
- package/dist/index.js +17 -22
- package/package.json +4 -7
package/README.md
CHANGED
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/bini-server)
|
|
6
6
|
[](./LICENSE)
|
|
7
|
-
[](https://www.npmjs.com/package/bini-env)
|
|
7
|
+
[](https://nodejs.org)
|
|
9
8
|
|
|
10
9
|
**Production server for [bini-router](https://www.npmjs.com/package/bini-router) apps.**
|
|
11
10
|
Serves your `dist/` statically and handles `/api/*` from your `src/app/api/` handlers — just like `vite preview`, but production-grade.
|
|
@@ -19,7 +18,7 @@ Serves your `dist/` statically and handles `/api/*` from your `src/app/api/` han
|
|
|
19
18
|
- 🗂️ **Static file serving** — streams `dist/` with correct MIME types, ETag, and cache headers
|
|
20
19
|
- 🌐 **API routes** — serves `/api/*` from your `src/app/api/` handlers (Hono apps + plain functions)
|
|
21
20
|
- 🔀 **SPA fallback** — unknown routes serve `dist/index.html`
|
|
22
|
-
- 🌿 **Auto env loading** — `.env`
|
|
21
|
+
- 🌿 **Auto env loading** — `.env` files detected and listed automatically at startup
|
|
23
22
|
- 🏷️ **ETag support** — `304 Not Modified` responses for unchanged files
|
|
24
23
|
- 🛡️ **CORS** — enabled by default on all API responses
|
|
25
24
|
- ⏱️ **Timeouts** — configurable body read + handler timeouts (default 30s each)
|
|
@@ -33,7 +32,7 @@ Serves your `dist/` statically and handles `/api/*` from your `src/app/api/` han
|
|
|
33
32
|
|
|
34
33
|
## Requirements
|
|
35
34
|
|
|
36
|
-
- Node.js **≥
|
|
35
|
+
- Node.js **≥ 20**
|
|
37
36
|
- A [bini-router](https://www.npmjs.com/package/bini-router) project with a built `dist/`
|
|
38
37
|
|
|
39
38
|
---
|
|
@@ -92,7 +91,7 @@ Once the server is running, type a key and press `enter`:
|
|
|
92
91
|
|
|
93
92
|
## Environment Variables
|
|
94
93
|
|
|
95
|
-
`.env`
|
|
94
|
+
`.env` files are detected automatically at startup and listed in the banner. All vars are available in `process.env` in your API handlers with no imports needed.
|
|
96
95
|
|
|
97
96
|
```env
|
|
98
97
|
PORT=3000
|
|
@@ -193,7 +192,7 @@ Set start command to `npm start`. Render injects `PORT` automatically.
|
|
|
193
192
|
| Body size limit | ❌ | ✅ 10MB |
|
|
194
193
|
| Handler timeout | ❌ | ✅ 30s |
|
|
195
194
|
| Graceful shutdown | ❌ | ✅ |
|
|
196
|
-
|
|
|
195
|
+
| Handler module cache | ❌ | ✅ |
|
|
197
196
|
| Configurable dirs | ❌ | ✅ |
|
|
198
197
|
|
|
199
198
|
---
|
package/dist/cli.js
CHANGED
|
@@ -12,22 +12,15 @@ import { exec } from "child_process";
|
|
|
12
12
|
import { pathToFileURL } from "url";
|
|
13
13
|
import { pipeline } from "stream/promises";
|
|
14
14
|
import { createReadStream } from "fs";
|
|
15
|
-
|
|
16
|
-
process.env.NODE_ENV
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
console.log = (...args) => {
|
|
25
|
-
if (!isDotenv(...args)) _log(...args);
|
|
26
|
-
};
|
|
27
|
-
console.error = (...args) => {
|
|
28
|
-
if (!isDotenv(...args)) _err(...args);
|
|
29
|
-
};
|
|
30
|
-
})();
|
|
15
|
+
function detectEnvFiles(root) {
|
|
16
|
+
const nodeEnv = process.env.NODE_ENV ?? "production";
|
|
17
|
+
return [
|
|
18
|
+
".env.local",
|
|
19
|
+
`.env.${nodeEnv}.local`,
|
|
20
|
+
`.env.${nodeEnv}`,
|
|
21
|
+
".env"
|
|
22
|
+
].filter((f) => fs.existsSync(path.join(root, f)));
|
|
23
|
+
}
|
|
31
24
|
var DEFAULT_PORT = parseInt(process.env.PORT ?? "3000", 10);
|
|
32
25
|
var BODY_TIMEOUT = parseInt(process.env.BINI_BODY_TIMEOUT_SECS ?? "30", 10) * 1e3;
|
|
33
26
|
var HANDLER_TIMEOUT = parseInt(process.env.BINI_HANDLER_TIMEOUT_SECS ?? "30", 10) * 1e3;
|
|
@@ -66,7 +59,7 @@ function getNetworkIp() {
|
|
|
66
59
|
return null;
|
|
67
60
|
}
|
|
68
61
|
function printBanner(port) {
|
|
69
|
-
const envFiles = detectEnvFiles(process.cwd())
|
|
62
|
+
const envFiles = detectEnvFiles(process.cwd());
|
|
70
63
|
const networkIp = getNetworkIp();
|
|
71
64
|
console.log(`
|
|
72
65
|
${C.BOLD}${C.CYAN}\xDF Bini.js${C.RESET} (production)`);
|
|
@@ -77,7 +70,7 @@ function printBanner(port) {
|
|
|
77
70
|
if (networkIp) {
|
|
78
71
|
console.log(` ${C.GREEN}\u279C${C.RESET} Network: ${C.CYAN}http://${networkIp}:${port}/${C.RESET}`);
|
|
79
72
|
}
|
|
80
|
-
console.log(` ${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}h
|
|
73
|
+
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}h + enter${C.DIM} to show help${C.RESET}`);
|
|
81
74
|
console.log("");
|
|
82
75
|
}
|
|
83
76
|
async function isPortFree(port) {
|
|
@@ -107,8 +100,8 @@ function startKeyboardLoop(port) {
|
|
|
107
100
|
case "h":
|
|
108
101
|
console.log("");
|
|
109
102
|
console.log(" Shortcuts");
|
|
110
|
-
console.log(` ${C.DIM}
|
|
111
|
-
console.log(` ${C.DIM}
|
|
103
|
+
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}o + enter${C.DIM} to open in browser${C.RESET}`);
|
|
104
|
+
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}q + enter${C.DIM} to quit${C.RESET}`);
|
|
112
105
|
console.log("");
|
|
113
106
|
break;
|
|
114
107
|
case "o": {
|
|
@@ -244,6 +237,9 @@ var CORS_HEADERS = {
|
|
|
244
237
|
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Request-ID",
|
|
245
238
|
"Vary": "Origin"
|
|
246
239
|
};
|
|
240
|
+
function invokeHono(honoApp, request) {
|
|
241
|
+
return honoApp.fetch(request, process.env);
|
|
242
|
+
}
|
|
247
243
|
var routeCache = null;
|
|
248
244
|
async function handleApiRequest(req, res, next) {
|
|
249
245
|
if (req.method === "OPTIONS") {
|
|
@@ -300,7 +296,7 @@ async function handleApiRequest(req, res, next) {
|
|
|
300
296
|
try {
|
|
301
297
|
if (typeof handler.fetch === "function") {
|
|
302
298
|
const honoUrl = `http://${host}${pathname.replace(/^\/api/, "") || "/"}${search}`;
|
|
303
|
-
webRes = await handler
|
|
299
|
+
webRes = await invokeHono(handler, new Request(honoUrl, {
|
|
304
300
|
method,
|
|
305
301
|
headers: normalizedHeaders,
|
|
306
302
|
body: body ? new Uint8Array(body) : null
|
|
@@ -438,7 +434,6 @@ function compose(middlewares) {
|
|
|
438
434
|
};
|
|
439
435
|
}
|
|
440
436
|
async function start() {
|
|
441
|
-
await loadEnv(process.cwd());
|
|
442
437
|
if (!fs.existsSync(DIST_DIR)) {
|
|
443
438
|
console.error(`
|
|
444
439
|
${C.RED}\u2717${C.RESET} dist/ not found at ${DIST_DIR}.
|
package/dist/index.js
CHANGED
|
@@ -10,22 +10,15 @@ import { exec } from "child_process";
|
|
|
10
10
|
import { pathToFileURL } from "url";
|
|
11
11
|
import { pipeline } from "stream/promises";
|
|
12
12
|
import { createReadStream } from "fs";
|
|
13
|
-
|
|
14
|
-
process.env.NODE_ENV
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
console.log = (...args) => {
|
|
23
|
-
if (!isDotenv(...args)) _log(...args);
|
|
24
|
-
};
|
|
25
|
-
console.error = (...args) => {
|
|
26
|
-
if (!isDotenv(...args)) _err(...args);
|
|
27
|
-
};
|
|
28
|
-
})();
|
|
13
|
+
function detectEnvFiles(root) {
|
|
14
|
+
const nodeEnv = process.env.NODE_ENV ?? "production";
|
|
15
|
+
return [
|
|
16
|
+
".env.local",
|
|
17
|
+
`.env.${nodeEnv}.local`,
|
|
18
|
+
`.env.${nodeEnv}`,
|
|
19
|
+
".env"
|
|
20
|
+
].filter((f) => fs.existsSync(path.join(root, f)));
|
|
21
|
+
}
|
|
29
22
|
var DEFAULT_PORT = parseInt(process.env.PORT ?? "3000", 10);
|
|
30
23
|
var BODY_TIMEOUT = parseInt(process.env.BINI_BODY_TIMEOUT_SECS ?? "30", 10) * 1e3;
|
|
31
24
|
var HANDLER_TIMEOUT = parseInt(process.env.BINI_HANDLER_TIMEOUT_SECS ?? "30", 10) * 1e3;
|
|
@@ -64,7 +57,7 @@ function getNetworkIp() {
|
|
|
64
57
|
return null;
|
|
65
58
|
}
|
|
66
59
|
function printBanner(port) {
|
|
67
|
-
const envFiles = detectEnvFiles(process.cwd())
|
|
60
|
+
const envFiles = detectEnvFiles(process.cwd());
|
|
68
61
|
const networkIp = getNetworkIp();
|
|
69
62
|
console.log(`
|
|
70
63
|
${C.BOLD}${C.CYAN}\xDF Bini.js${C.RESET} (production)`);
|
|
@@ -75,7 +68,7 @@ function printBanner(port) {
|
|
|
75
68
|
if (networkIp) {
|
|
76
69
|
console.log(` ${C.GREEN}\u279C${C.RESET} Network: ${C.CYAN}http://${networkIp}:${port}/${C.RESET}`);
|
|
77
70
|
}
|
|
78
|
-
console.log(` ${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}h
|
|
71
|
+
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}h + enter${C.DIM} to show help${C.RESET}`);
|
|
79
72
|
console.log("");
|
|
80
73
|
}
|
|
81
74
|
async function isPortFree(port) {
|
|
@@ -105,8 +98,8 @@ function startKeyboardLoop(port) {
|
|
|
105
98
|
case "h":
|
|
106
99
|
console.log("");
|
|
107
100
|
console.log(" Shortcuts");
|
|
108
|
-
console.log(` ${C.DIM}
|
|
109
|
-
console.log(` ${C.DIM}
|
|
101
|
+
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}o + enter${C.DIM} to open in browser${C.RESET}`);
|
|
102
|
+
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}q + enter${C.DIM} to quit${C.RESET}`);
|
|
110
103
|
console.log("");
|
|
111
104
|
break;
|
|
112
105
|
case "o": {
|
|
@@ -242,6 +235,9 @@ var CORS_HEADERS = {
|
|
|
242
235
|
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Request-ID",
|
|
243
236
|
"Vary": "Origin"
|
|
244
237
|
};
|
|
238
|
+
function invokeHono(honoApp, request) {
|
|
239
|
+
return honoApp.fetch(request, process.env);
|
|
240
|
+
}
|
|
245
241
|
var routeCache = null;
|
|
246
242
|
async function handleApiRequest(req, res, next) {
|
|
247
243
|
if (req.method === "OPTIONS") {
|
|
@@ -298,7 +294,7 @@ async function handleApiRequest(req, res, next) {
|
|
|
298
294
|
try {
|
|
299
295
|
if (typeof handler.fetch === "function") {
|
|
300
296
|
const honoUrl = `http://${host}${pathname.replace(/^\/api/, "") || "/"}${search}`;
|
|
301
|
-
webRes = await handler
|
|
297
|
+
webRes = await invokeHono(handler, new Request(honoUrl, {
|
|
302
298
|
method,
|
|
303
299
|
headers: normalizedHeaders,
|
|
304
300
|
body: body ? new Uint8Array(body) : null
|
|
@@ -436,7 +432,6 @@ function compose(middlewares) {
|
|
|
436
432
|
};
|
|
437
433
|
}
|
|
438
434
|
async function start() {
|
|
439
|
-
await loadEnv(process.cwd());
|
|
440
435
|
if (!fs.existsSync(DIST_DIR)) {
|
|
441
436
|
console.error(`
|
|
442
437
|
${C.RED}\u2717${C.RESET} dist/ not found at ${DIST_DIR}.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bini-server",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Production server for bini-router apps.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -31,9 +31,6 @@
|
|
|
31
31
|
"lint": "tsc --noEmit",
|
|
32
32
|
"clean": "rm -rf dist"
|
|
33
33
|
},
|
|
34
|
-
"dependencies": {
|
|
35
|
-
"bini-env": "1.0.8"
|
|
36
|
-
},
|
|
37
34
|
"engines": {
|
|
38
35
|
"node": ">=20.19.0"
|
|
39
36
|
},
|
|
@@ -41,7 +38,6 @@
|
|
|
41
38
|
"bini",
|
|
42
39
|
"bini-js",
|
|
43
40
|
"bini-router",
|
|
44
|
-
"bini-env",
|
|
45
41
|
"server",
|
|
46
42
|
"spa",
|
|
47
43
|
"vite",
|
|
@@ -63,8 +59,9 @@
|
|
|
63
59
|
"url": "https://github.com/Binidu01/bini-server/issues"
|
|
64
60
|
},
|
|
65
61
|
"devDependencies": {
|
|
66
|
-
"@types/node": "^25.
|
|
62
|
+
"@types/node": "^25.7.0",
|
|
67
63
|
"tsup": "^8.5.1",
|
|
68
|
-
"typescript": "^
|
|
64
|
+
"typescript": "^6.0.3",
|
|
65
|
+
"dotenv": "^17.4.2"
|
|
69
66
|
}
|
|
70
67
|
}
|