@hono/node-server 1.0.1 → 1.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/README.md +9 -3
- package/dist/globals.js +31 -14
- package/dist/globals.mjs +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +32 -5
- package/dist/index.mjs +8 -0
- package/dist/listener.d.ts +2 -2
- package/dist/listener.js +85 -85
- package/dist/listener.mjs +67 -0
- package/dist/serve-static.d.ts +2 -3
- package/dist/serve-static.js +92 -35
- package/dist/serve-static.mjs +71 -0
- package/dist/server.d.ts +2 -2
- package/dist/server.js +41 -17
- package/dist/server.mjs +22 -0
- package/dist/vercel.js +28 -5
- package/dist/vercel.mjs +8 -0
- package/package.json +29 -9
- package/dist/types.js +0 -2
package/README.md
CHANGED
|
@@ -8,7 +8,13 @@ However, it's worth noting that it is still faster than Express.
|
|
|
8
8
|
|
|
9
9
|
## Requirement
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
It works on Node.js versions greater than 18.x. The specific required Node.js versions are as follows:
|
|
12
|
+
|
|
13
|
+
- 18.x => 18.14.1+
|
|
14
|
+
- 19.x => 19.7.0+
|
|
15
|
+
- 20.x => 20.0.0+
|
|
16
|
+
|
|
17
|
+
Essentially, you can simply use the latest version of each major release.
|
|
12
18
|
|
|
13
19
|
## Install
|
|
14
20
|
|
|
@@ -81,7 +87,7 @@ serve({
|
|
|
81
87
|
## Middleware
|
|
82
88
|
|
|
83
89
|
Most built-in middleware also works with Node.js.
|
|
84
|
-
Read [the documentation](https://
|
|
90
|
+
Read [the documentation](https://hono.dev/middleware/builtin/basic-auth) and use the Middleware of your liking.
|
|
85
91
|
|
|
86
92
|
```ts
|
|
87
93
|
import { serve } from '@hono/node-server'
|
|
@@ -128,7 +134,7 @@ app.use(
|
|
|
128
134
|
|
|
129
135
|
## Related projects
|
|
130
136
|
|
|
131
|
-
- Hono - <https://
|
|
137
|
+
- Hono - <https://hono.dev>
|
|
132
138
|
- Hono GitHub repository - <https://github.com/honojs/hono>
|
|
133
139
|
|
|
134
140
|
## Author
|
package/dist/globals.js
CHANGED
|
@@ -1,20 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
4
15
|
};
|
|
5
|
-
|
|
6
|
-
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var import_node_crypto = __toESM(require("node:crypto"));
|
|
7
25
|
const webFetch = global.fetch;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
global.crypto = node_crypto_1.default;
|
|
26
|
+
if (typeof global.crypto === "undefined") {
|
|
27
|
+
global.crypto = import_node_crypto.default;
|
|
11
28
|
}
|
|
12
29
|
global.fetch = (info, init) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
init = {
|
|
31
|
+
// Disable compression handling so people can return the result of a fetch
|
|
32
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
33
|
+
compress: false,
|
|
34
|
+
...init
|
|
35
|
+
};
|
|
36
|
+
return webFetch(info, init);
|
|
20
37
|
};
|
package/dist/globals.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/globals.ts
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
var webFetch = global.fetch;
|
|
4
|
+
if (typeof global.crypto === "undefined") {
|
|
5
|
+
global.crypto = crypto;
|
|
6
|
+
}
|
|
7
|
+
global.fetch = (info, init) => {
|
|
8
|
+
init = {
|
|
9
|
+
// Disable compression handling so people can return the result of a fetch
|
|
10
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
11
|
+
compress: false,
|
|
12
|
+
...init
|
|
13
|
+
};
|
|
14
|
+
return webFetch(info, init);
|
|
15
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
createAdaptorServer: () => import_server.createAdaptorServer,
|
|
22
|
+
getRequestListener: () => import_listener.getRequestListener,
|
|
23
|
+
serve: () => import_server.serve
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_server = require("./server");
|
|
27
|
+
var import_listener = require("./listener");
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
createAdaptorServer,
|
|
31
|
+
getRequestListener,
|
|
32
|
+
serve
|
|
33
|
+
});
|
package/dist/index.mjs
ADDED
package/dist/listener.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
-
import { FetchCallback } from './types';
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { FetchCallback } from './types';
|
|
3
3
|
import './globals';
|
|
4
4
|
export declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage, outgoing: ServerResponse) => Promise<void>;
|
package/dist/listener.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var listener_exports = {};
|
|
20
|
+
__export(listener_exports, {
|
|
21
|
+
getRequestListener: () => getRequestListener
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(listener_exports);
|
|
24
|
+
var import_node_stream = require("node:stream");
|
|
25
|
+
var import_promises = require("node:stream/promises");
|
|
26
|
+
var import_globals = require("./globals");
|
|
7
27
|
const getRequestListener = (fetchCallback) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// timeout error emits 504 timeout
|
|
33
|
-
if (e.name === 'TimeoutError' || e.constructor.name === 'TimeoutError') {
|
|
34
|
-
res = new Response(null, { status: 504 });
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const contentType = res.headers.get('content-type') || '';
|
|
39
|
-
// nginx buffering variant
|
|
40
|
-
const buffering = res.headers.get('x-accel-buffering') || '';
|
|
41
|
-
const contentEncoding = res.headers.get('content-encoding');
|
|
42
|
-
const contentLength = res.headers.get('content-length');
|
|
43
|
-
const transferEncoding = res.headers.get('transfer-encoding');
|
|
44
|
-
for (const [k, v] of res.headers) {
|
|
45
|
-
if (k === 'set-cookie') {
|
|
46
|
-
// node native Headers.prototype has getSetCookie method
|
|
47
|
-
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
outgoing.setHeader(k, v);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
outgoing.statusCode = res.status;
|
|
54
|
-
if (res.body) {
|
|
55
|
-
try {
|
|
56
|
-
/**
|
|
57
|
-
* If content-encoding is set, we assume that the response should be not decoded.
|
|
58
|
-
* Else if transfer-encoding is set, we assume that the response should be streamed.
|
|
59
|
-
* Else if content-length is set, we assume that the response content has been taken care of.
|
|
60
|
-
* Else if x-accel-buffering is set to no, we assume that the response should be streamed.
|
|
61
|
-
* Else if content-type is not application/json nor text/* but can be text/event-stream,
|
|
62
|
-
* we assume that the response should be streamed.
|
|
63
|
-
*/
|
|
64
|
-
if (contentEncoding ||
|
|
65
|
-
transferEncoding ||
|
|
66
|
-
contentLength ||
|
|
67
|
-
/^no$/i.test(buffering) ||
|
|
68
|
-
!/^(application\/json\b|text\/(?!event-stream\b))/i.test(contentType)) {
|
|
69
|
-
await (0, promises_1.pipeline)(node_stream_1.Readable.fromWeb(res.body), outgoing);
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
const text = await res.text();
|
|
73
|
-
outgoing.setHeader('Content-Length', Buffer.byteLength(text));
|
|
74
|
-
outgoing.end(text);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
catch (e) {
|
|
78
|
-
// try to catch any error, to avoid crash
|
|
79
|
-
console.error(e);
|
|
80
|
-
const err = e instanceof Error ? e : new Error('unknown error', { cause: e });
|
|
81
|
-
// destroy error must accept an instance of Error
|
|
82
|
-
outgoing.destroy(err);
|
|
83
|
-
}
|
|
28
|
+
return async (incoming, outgoing) => {
|
|
29
|
+
const method = incoming.method || "GET";
|
|
30
|
+
const url = `http://${incoming.headers.host}${incoming.url}`;
|
|
31
|
+
const headerRecord = [];
|
|
32
|
+
const len = incoming.rawHeaders.length;
|
|
33
|
+
for (let i = 0; i < len; i += 2) {
|
|
34
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
35
|
+
}
|
|
36
|
+
const init = {
|
|
37
|
+
method,
|
|
38
|
+
headers: headerRecord
|
|
39
|
+
};
|
|
40
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
41
|
+
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
42
|
+
init.duplex = "half";
|
|
43
|
+
}
|
|
44
|
+
let res;
|
|
45
|
+
try {
|
|
46
|
+
res = await fetchCallback(new Request(url.toString(), init));
|
|
47
|
+
} catch (e) {
|
|
48
|
+
res = new Response(null, { status: 500 });
|
|
49
|
+
if (e instanceof Error) {
|
|
50
|
+
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
51
|
+
res = new Response(null, { status: 504 });
|
|
84
52
|
}
|
|
85
|
-
|
|
86
|
-
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const contentType = res.headers.get("content-type") || "";
|
|
56
|
+
const buffering = res.headers.get("x-accel-buffering") || "";
|
|
57
|
+
const contentEncoding = res.headers.get("content-encoding");
|
|
58
|
+
const contentLength = res.headers.get("content-length");
|
|
59
|
+
const transferEncoding = res.headers.get("transfer-encoding");
|
|
60
|
+
for (const [k, v] of res.headers) {
|
|
61
|
+
if (k === "set-cookie") {
|
|
62
|
+
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
63
|
+
} else {
|
|
64
|
+
outgoing.setHeader(k, v);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
outgoing.statusCode = res.status;
|
|
68
|
+
if (res.body) {
|
|
69
|
+
try {
|
|
70
|
+
if (contentEncoding || transferEncoding || contentLength || /^no$/i.test(buffering) || !/^(application\/json\b|text\/(?!event-stream\b))/i.test(contentType)) {
|
|
71
|
+
await (0, import_promises.pipeline)(import_node_stream.Readable.fromWeb(res.body), outgoing);
|
|
72
|
+
} else {
|
|
73
|
+
const text = await res.text();
|
|
74
|
+
outgoing.setHeader("Content-Length", Buffer.byteLength(text));
|
|
75
|
+
outgoing.end(text);
|
|
87
76
|
}
|
|
88
|
-
|
|
77
|
+
} catch (e) {
|
|
78
|
+
console.error(e);
|
|
79
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
80
|
+
outgoing.destroy(err);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
outgoing.end();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
89
86
|
};
|
|
90
|
-
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
getRequestListener
|
|
90
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/listener.ts
|
|
2
|
+
import { Readable } from "node:stream";
|
|
3
|
+
import { pipeline } from "node:stream/promises";
|
|
4
|
+
import "./globals.mjs";
|
|
5
|
+
var getRequestListener = (fetchCallback) => {
|
|
6
|
+
return async (incoming, outgoing) => {
|
|
7
|
+
const method = incoming.method || "GET";
|
|
8
|
+
const url = `http://${incoming.headers.host}${incoming.url}`;
|
|
9
|
+
const headerRecord = [];
|
|
10
|
+
const len = incoming.rawHeaders.length;
|
|
11
|
+
for (let i = 0; i < len; i += 2) {
|
|
12
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
13
|
+
}
|
|
14
|
+
const init = {
|
|
15
|
+
method,
|
|
16
|
+
headers: headerRecord
|
|
17
|
+
};
|
|
18
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
19
|
+
init.body = Readable.toWeb(incoming);
|
|
20
|
+
init.duplex = "half";
|
|
21
|
+
}
|
|
22
|
+
let res;
|
|
23
|
+
try {
|
|
24
|
+
res = await fetchCallback(new Request(url.toString(), init));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
res = new Response(null, { status: 500 });
|
|
27
|
+
if (e instanceof Error) {
|
|
28
|
+
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
29
|
+
res = new Response(null, { status: 504 });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const contentType = res.headers.get("content-type") || "";
|
|
34
|
+
const buffering = res.headers.get("x-accel-buffering") || "";
|
|
35
|
+
const contentEncoding = res.headers.get("content-encoding");
|
|
36
|
+
const contentLength = res.headers.get("content-length");
|
|
37
|
+
const transferEncoding = res.headers.get("transfer-encoding");
|
|
38
|
+
for (const [k, v] of res.headers) {
|
|
39
|
+
if (k === "set-cookie") {
|
|
40
|
+
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
41
|
+
} else {
|
|
42
|
+
outgoing.setHeader(k, v);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
outgoing.statusCode = res.status;
|
|
46
|
+
if (res.body) {
|
|
47
|
+
try {
|
|
48
|
+
if (contentEncoding || transferEncoding || contentLength || /^no$/i.test(buffering) || !/^(application\/json\b|text\/(?!event-stream\b))/i.test(contentType)) {
|
|
49
|
+
await pipeline(Readable.fromWeb(res.body), outgoing);
|
|
50
|
+
} else {
|
|
51
|
+
const text = await res.text();
|
|
52
|
+
outgoing.setHeader("Content-Length", Buffer.byteLength(text));
|
|
53
|
+
outgoing.end(text);
|
|
54
|
+
}
|
|
55
|
+
} catch (e) {
|
|
56
|
+
console.error(e);
|
|
57
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
58
|
+
outgoing.destroy(err);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
outgoing.end();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
getRequestListener
|
|
67
|
+
};
|
package/dist/serve-static.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { Context } from 'hono';
|
|
1
|
+
import type { MiddlewareHandler } from 'hono';
|
|
3
2
|
export declare type ServeStaticOptions = {
|
|
4
3
|
/**
|
|
5
4
|
* Root path, relative to current working directory. (absolute paths are not supported)
|
|
@@ -9,4 +8,4 @@ export declare type ServeStaticOptions = {
|
|
|
9
8
|
index?: string;
|
|
10
9
|
rewriteRequestPath?: (path: string) => string;
|
|
11
10
|
};
|
|
12
|
-
export declare const serveStatic: (options?: ServeStaticOptions) =>
|
|
11
|
+
export declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
|
package/dist/serve-static.js
CHANGED
|
@@ -1,37 +1,94 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// Do nothing if Response is already set
|
|
10
|
-
if (c.finalized) {
|
|
11
|
-
await next();
|
|
12
|
-
}
|
|
13
|
-
const url = new URL(c.req.url);
|
|
14
|
-
const filename = options.path ?? decodeURI(url.pathname);
|
|
15
|
-
let path = (0, filepath_1.getFilePath)({
|
|
16
|
-
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
|
|
17
|
-
root: options.root,
|
|
18
|
-
defaultDocument: options.index ?? 'index.html',
|
|
19
|
-
});
|
|
20
|
-
path = `./${path}`;
|
|
21
|
-
if ((0, fs_1.existsSync)(path)) {
|
|
22
|
-
const content = (0, fs_1.readFileSync)(path);
|
|
23
|
-
if (content) {
|
|
24
|
-
const mimeType = (0, mime_1.getMimeType)(path);
|
|
25
|
-
if (mimeType) {
|
|
26
|
-
c.header('Content-Type', mimeType);
|
|
27
|
-
}
|
|
28
|
-
// Return Response object
|
|
29
|
-
return c.body(content);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
console.warn(`Static file: ${path} is not found`);
|
|
33
|
-
await next();
|
|
34
|
-
return;
|
|
35
|
-
};
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
36
9
|
};
|
|
37
|
-
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var serve_static_exports = {};
|
|
20
|
+
__export(serve_static_exports, {
|
|
21
|
+
serveStatic: () => serveStatic
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(serve_static_exports);
|
|
24
|
+
var import_fs = require("fs");
|
|
25
|
+
var import_filepath = require("hono/utils/filepath");
|
|
26
|
+
var import_mime = require("hono/utils/mime");
|
|
27
|
+
const createStreamBody = (stream) => {
|
|
28
|
+
const body = new ReadableStream({
|
|
29
|
+
start(controller) {
|
|
30
|
+
stream.on("data", (chunk) => {
|
|
31
|
+
controller.enqueue(chunk);
|
|
32
|
+
});
|
|
33
|
+
stream.on("end", () => {
|
|
34
|
+
controller.close();
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
cancel() {
|
|
38
|
+
stream.destroy();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return body;
|
|
42
|
+
};
|
|
43
|
+
const serveStatic = (options = { root: "" }) => {
|
|
44
|
+
return async (c, next) => {
|
|
45
|
+
if (c.finalized)
|
|
46
|
+
return next();
|
|
47
|
+
const url = new URL(c.req.url);
|
|
48
|
+
const filename = options.path ?? decodeURIComponent(url.pathname);
|
|
49
|
+
let path = (0, import_filepath.getFilePath)({
|
|
50
|
+
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
|
|
51
|
+
root: options.root,
|
|
52
|
+
defaultDocument: options.index ?? "index.html"
|
|
53
|
+
});
|
|
54
|
+
path = `./${path}`;
|
|
55
|
+
if (!(0, import_fs.existsSync)(path)) {
|
|
56
|
+
return next();
|
|
57
|
+
}
|
|
58
|
+
const mimeType = (0, import_mime.getMimeType)(path);
|
|
59
|
+
if (mimeType) {
|
|
60
|
+
c.header("Content-Type", mimeType);
|
|
61
|
+
}
|
|
62
|
+
const stat = (0, import_fs.lstatSync)(path);
|
|
63
|
+
const size = stat.size;
|
|
64
|
+
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
65
|
+
c.header("Content-Length", size.toString());
|
|
66
|
+
c.status(200);
|
|
67
|
+
return c.body(null);
|
|
68
|
+
}
|
|
69
|
+
const range = c.req.header("range") || "";
|
|
70
|
+
if (!range) {
|
|
71
|
+
c.header("Content-Length", size.toString());
|
|
72
|
+
return c.body(createStreamBody((0, import_fs.createReadStream)(path)), 200);
|
|
73
|
+
}
|
|
74
|
+
c.header("Accept-Ranges", "bytes");
|
|
75
|
+
c.header("Date", stat.birthtime.toUTCString());
|
|
76
|
+
let start = 0;
|
|
77
|
+
let end = stat.size - 1;
|
|
78
|
+
const parts = range.replace(/bytes=/, "").split("-");
|
|
79
|
+
start = parseInt(parts[0], 10);
|
|
80
|
+
end = parts[1] ? parseInt(parts[1], 10) : end;
|
|
81
|
+
if (size < end - start + 1) {
|
|
82
|
+
end = size - 1;
|
|
83
|
+
}
|
|
84
|
+
const chunksize = end - start + 1;
|
|
85
|
+
const stream = (0, import_fs.createReadStream)(path, { start, end });
|
|
86
|
+
c.header("Content-Length", chunksize.toString());
|
|
87
|
+
c.header("Content-Range", `bytes ${start}-${end}/${stat.size}`);
|
|
88
|
+
return c.body(createStreamBody(stream), 206);
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
serveStatic
|
|
94
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// src/serve-static.ts
|
|
2
|
+
import { createReadStream, existsSync, lstatSync } from "fs";
|
|
3
|
+
import { getFilePath } from "hono/utils/filepath";
|
|
4
|
+
import { getMimeType } from "hono/utils/mime";
|
|
5
|
+
var createStreamBody = (stream) => {
|
|
6
|
+
const body = new ReadableStream({
|
|
7
|
+
start(controller) {
|
|
8
|
+
stream.on("data", (chunk) => {
|
|
9
|
+
controller.enqueue(chunk);
|
|
10
|
+
});
|
|
11
|
+
stream.on("end", () => {
|
|
12
|
+
controller.close();
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
cancel() {
|
|
16
|
+
stream.destroy();
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return body;
|
|
20
|
+
};
|
|
21
|
+
var serveStatic = (options = { root: "" }) => {
|
|
22
|
+
return async (c, next) => {
|
|
23
|
+
if (c.finalized)
|
|
24
|
+
return next();
|
|
25
|
+
const url = new URL(c.req.url);
|
|
26
|
+
const filename = options.path ?? decodeURIComponent(url.pathname);
|
|
27
|
+
let path = getFilePath({
|
|
28
|
+
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
|
|
29
|
+
root: options.root,
|
|
30
|
+
defaultDocument: options.index ?? "index.html"
|
|
31
|
+
});
|
|
32
|
+
path = `./${path}`;
|
|
33
|
+
if (!existsSync(path)) {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
const mimeType = getMimeType(path);
|
|
37
|
+
if (mimeType) {
|
|
38
|
+
c.header("Content-Type", mimeType);
|
|
39
|
+
}
|
|
40
|
+
const stat = lstatSync(path);
|
|
41
|
+
const size = stat.size;
|
|
42
|
+
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
43
|
+
c.header("Content-Length", size.toString());
|
|
44
|
+
c.status(200);
|
|
45
|
+
return c.body(null);
|
|
46
|
+
}
|
|
47
|
+
const range = c.req.header("range") || "";
|
|
48
|
+
if (!range) {
|
|
49
|
+
c.header("Content-Length", size.toString());
|
|
50
|
+
return c.body(createStreamBody(createReadStream(path)), 200);
|
|
51
|
+
}
|
|
52
|
+
c.header("Accept-Ranges", "bytes");
|
|
53
|
+
c.header("Date", stat.birthtime.toUTCString());
|
|
54
|
+
let start = 0;
|
|
55
|
+
let end = stat.size - 1;
|
|
56
|
+
const parts = range.replace(/bytes=/, "").split("-");
|
|
57
|
+
start = parseInt(parts[0], 10);
|
|
58
|
+
end = parts[1] ? parseInt(parts[1], 10) : end;
|
|
59
|
+
if (size < end - start + 1) {
|
|
60
|
+
end = size - 1;
|
|
61
|
+
}
|
|
62
|
+
const chunksize = end - start + 1;
|
|
63
|
+
const stream = createReadStream(path, { start, end });
|
|
64
|
+
c.header("Content-Length", chunksize.toString());
|
|
65
|
+
c.header("Content-Range", `bytes ${start}-${end}/${stat.size}`);
|
|
66
|
+
return c.body(createStreamBody(stream), 206);
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
export {
|
|
70
|
+
serveStatic
|
|
71
|
+
};
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Server } from 'node:http';
|
|
2
|
-
import { Options } from './types';
|
|
1
|
+
import { type Server } from 'node:http';
|
|
3
2
|
import type { AddressInfo } from 'node:net';
|
|
3
|
+
import type { Options } from './types';
|
|
4
4
|
export declare const createAdaptorServer: (options: Options) => Server;
|
|
5
5
|
export declare const serve: (options: Options, listeningListener?: ((info: AddressInfo) => void) | undefined) => Server;
|
package/dist/server.js
CHANGED
|
@@ -1,22 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var server_exports = {};
|
|
20
|
+
__export(server_exports, {
|
|
21
|
+
createAdaptorServer: () => createAdaptorServer,
|
|
22
|
+
serve: () => serve
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(server_exports);
|
|
25
|
+
var import_node_http = require("node:http");
|
|
26
|
+
var import_listener = require("./listener");
|
|
6
27
|
const createAdaptorServer = (options) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
28
|
+
const fetchCallback = options.fetch;
|
|
29
|
+
const requestListener = (0, import_listener.getRequestListener)(fetchCallback);
|
|
30
|
+
const createServer = options.createServer || import_node_http.createServer;
|
|
31
|
+
const server = createServer(options.serverOptions || {}, requestListener);
|
|
32
|
+
return server;
|
|
12
33
|
};
|
|
13
|
-
exports.createAdaptorServer = createAdaptorServer;
|
|
14
34
|
const serve = (options, listeningListener) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
35
|
+
const server = createAdaptorServer(options);
|
|
36
|
+
server.listen(options?.port ?? 3e3, options.hostname ?? "0.0.0.0", () => {
|
|
37
|
+
const serverInfo = server.address();
|
|
38
|
+
listeningListener && listeningListener(serverInfo);
|
|
39
|
+
});
|
|
40
|
+
return server;
|
|
21
41
|
};
|
|
22
|
-
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
createAdaptorServer,
|
|
45
|
+
serve
|
|
46
|
+
});
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import { createServer as createServerHTTP } from "node:http";
|
|
3
|
+
import { getRequestListener } from "./listener.mjs";
|
|
4
|
+
var createAdaptorServer = (options) => {
|
|
5
|
+
const fetchCallback = options.fetch;
|
|
6
|
+
const requestListener = getRequestListener(fetchCallback);
|
|
7
|
+
const createServer = options.createServer || createServerHTTP;
|
|
8
|
+
const server = createServer(options.serverOptions || {}, requestListener);
|
|
9
|
+
return server;
|
|
10
|
+
};
|
|
11
|
+
var serve = (options, listeningListener) => {
|
|
12
|
+
const server = createAdaptorServer(options);
|
|
13
|
+
server.listen(options?.port ?? 3e3, options.hostname ?? "0.0.0.0", () => {
|
|
14
|
+
const serverInfo = server.address();
|
|
15
|
+
listeningListener && listeningListener(serverInfo);
|
|
16
|
+
});
|
|
17
|
+
return server;
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
createAdaptorServer,
|
|
21
|
+
serve
|
|
22
|
+
};
|
package/dist/vercel.js
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var vercel_exports = {};
|
|
20
|
+
__export(vercel_exports, {
|
|
21
|
+
handle: () => handle
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(vercel_exports);
|
|
24
|
+
var import_listener = require("./listener");
|
|
5
25
|
const handle = (app) => {
|
|
6
|
-
|
|
26
|
+
return (0, import_listener.getRequestListener)(app.fetch);
|
|
7
27
|
};
|
|
8
|
-
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
handle
|
|
31
|
+
});
|
package/dist/vercel.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/node-server",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Node.js Adapter for Hono",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -8,12 +8,27 @@
|
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
10
10
|
"exports": {
|
|
11
|
-
".":
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"import": "./dist/index.mjs"
|
|
15
|
+
},
|
|
16
|
+
"./serve-static": {
|
|
17
|
+
"types": "./dist/serve-static.d.ts",
|
|
18
|
+
"require": "./dist/serve-static.js",
|
|
19
|
+
"import": "./dist/serve-static.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./vercel": {
|
|
22
|
+
"types": "./dist/vercel.d.ts",
|
|
23
|
+
"require": "./dist/vercel.js",
|
|
24
|
+
"import": "./dist/vercel.mjs"
|
|
25
|
+
}
|
|
14
26
|
},
|
|
15
27
|
"typesVersions": {
|
|
16
28
|
"*": {
|
|
29
|
+
".": [
|
|
30
|
+
"./dist/index.d.ts"
|
|
31
|
+
],
|
|
17
32
|
"serve-static": [
|
|
18
33
|
"./dist/serve-static.d.ts"
|
|
19
34
|
],
|
|
@@ -24,7 +39,8 @@
|
|
|
24
39
|
},
|
|
25
40
|
"scripts": {
|
|
26
41
|
"test": "jest",
|
|
27
|
-
"build": "rimraf dist &&
|
|
42
|
+
"build": "rimraf dist && tsx ./build.ts",
|
|
43
|
+
"postbuild": "publint",
|
|
28
44
|
"prerelease": "yarn build && yarn test",
|
|
29
45
|
"release": "np"
|
|
30
46
|
},
|
|
@@ -44,15 +60,19 @@
|
|
|
44
60
|
},
|
|
45
61
|
"dependencies": {},
|
|
46
62
|
"devDependencies": {
|
|
47
|
-
"@types/
|
|
63
|
+
"@types/glob": "^8.1.0",
|
|
64
|
+
"@types/jest": "^29.5.3",
|
|
48
65
|
"@types/node": "^18.7.16",
|
|
49
66
|
"@types/supertest": "^2.0.12",
|
|
50
|
-
"
|
|
51
|
-
"
|
|
67
|
+
"esbuild": "^0.18.13",
|
|
68
|
+
"hono": "^3.3.0",
|
|
69
|
+
"jest": "^29.6.1",
|
|
52
70
|
"np": "^7.7.0",
|
|
71
|
+
"publint": "^0.1.16",
|
|
53
72
|
"rimraf": "^3.0.2",
|
|
54
73
|
"supertest": "^6.2.4",
|
|
55
|
-
"ts-jest": "^29.
|
|
74
|
+
"ts-jest": "^29.1.1",
|
|
75
|
+
"tsx": "^3.12.7",
|
|
56
76
|
"typescript": "^4.8.3"
|
|
57
77
|
}
|
|
58
78
|
}
|
package/dist/types.js
DELETED