@hono/node-server 1.19.11 → 2.0.0-rc.1
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 +1 -7
- package/dist/conninfo.d.mts +4 -3
- package/dist/conninfo.d.ts +4 -3
- package/dist/conninfo.js +20 -40
- package/dist/conninfo.mjs +19 -16
- package/dist/constants-B7DBcQew.js +11 -0
- package/dist/constants-DEKKqoym.mjs +5 -0
- package/dist/index.d.mts +62 -8
- package/dist/index.d.ts +62 -8
- package/dist/index.js +28 -632
- package/dist/index.mjs +24 -592
- package/dist/listener-Brd4yZ5d.js +726 -0
- package/dist/listener-RIBxK9_x.mjs +715 -0
- package/dist/serve-static.d.mts +14 -13
- package/dist/serve-static.d.ts +14 -13
- package/dist/serve-static.js +128 -170
- package/dist/serve-static.mjs +127 -145
- package/dist/utils/response.d.mts +3 -2
- package/dist/utils/response.d.ts +3 -2
- package/dist/utils/response.js +6 -35
- package/dist/utils/response.mjs +6 -9
- package/dist/vercel.d.mts +6 -5
- package/dist/vercel.d.ts +6 -5
- package/dist/vercel.js +7 -604
- package/dist/vercel.mjs +6 -567
- package/package.json +11 -13
- package/dist/globals.d.mts +0 -2
- package/dist/globals.d.ts +0 -2
- package/dist/globals.js +0 -29
- package/dist/globals.mjs +0 -5
- package/dist/listener.d.mts +0 -13
- package/dist/listener.d.ts +0 -13
- package/dist/listener.js +0 -600
- package/dist/listener.mjs +0 -565
- package/dist/request.d.mts +0 -25
- package/dist/request.d.ts +0 -25
- package/dist/request.js +0 -227
- package/dist/request.mjs +0 -195
- package/dist/response.d.mts +0 -26
- package/dist/response.d.ts +0 -26
- package/dist/response.js +0 -101
- package/dist/response.mjs +0 -74
- package/dist/server.d.mts +0 -10
- package/dist/server.d.ts +0 -10
- package/dist/server.js +0 -626
- package/dist/server.mjs +0 -590
- package/dist/types.d.mts +0 -44
- package/dist/types.d.ts +0 -44
- package/dist/types.js +0 -18
- package/dist/types.mjs +0 -0
- package/dist/utils/response/constants.d.mts +0 -3
- package/dist/utils/response/constants.d.ts +0 -3
- package/dist/utils/response/constants.js +0 -30
- package/dist/utils/response/constants.mjs +0 -5
- package/dist/utils.d.mts +0 -9
- package/dist/utils.d.ts +0 -9
- package/dist/utils.js +0 -99
- package/dist/utils.mjs +0 -71
package/dist/serve-static.d.mts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Context, Env, MiddlewareHandler } from "hono";
|
|
2
2
|
|
|
3
|
+
//#region src/serve-static.d.ts
|
|
3
4
|
type ServeStaticOptions<E extends Env = Env> = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Root path, relative to current working directory from which the app was started. Absolute paths are not supported.
|
|
7
|
+
*/
|
|
8
|
+
root?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
index?: string;
|
|
11
|
+
precompressed?: boolean;
|
|
12
|
+
rewriteRequestPath?: (path: string, c: Context<E>) => string;
|
|
13
|
+
onFound?: (path: string, c: Context<E>) => void | Promise<void>;
|
|
14
|
+
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>;
|
|
14
15
|
};
|
|
15
16
|
declare const serveStatic: <E extends Env = any>(options?: ServeStaticOptions<E>) => MiddlewareHandler<E>;
|
|
16
|
-
|
|
17
|
-
export { ServeStaticOptions, serveStatic };
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ServeStaticOptions, serveStatic };
|
package/dist/serve-static.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Context, Env, MiddlewareHandler } from "hono";
|
|
2
2
|
|
|
3
|
+
//#region src/serve-static.d.ts
|
|
3
4
|
type ServeStaticOptions<E extends Env = Env> = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Root path, relative to current working directory from which the app was started. Absolute paths are not supported.
|
|
7
|
+
*/
|
|
8
|
+
root?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
index?: string;
|
|
11
|
+
precompressed?: boolean;
|
|
12
|
+
rewriteRequestPath?: (path: string, c: Context<E>) => string;
|
|
13
|
+
onFound?: (path: string, c: Context<E>) => void | Promise<void>;
|
|
14
|
+
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>;
|
|
14
15
|
};
|
|
15
16
|
declare const serveStatic: <E extends Env = any>(options?: ServeStaticOptions<E>) => MiddlewareHandler<E>;
|
|
16
|
-
|
|
17
|
-
export { ServeStaticOptions, serveStatic };
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ServeStaticOptions, serveStatic };
|
package/dist/serve-static.js
CHANGED
|
@@ -1,177 +1,135 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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);
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let node_stream = require("node:stream");
|
|
3
|
+
let hono_utils_mime = require("hono/utils/mime");
|
|
4
|
+
let node_fs = require("node:fs");
|
|
5
|
+
let node_path = require("node:path");
|
|
6
|
+
let node_process = require("node:process");
|
|
19
7
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var import_mime = require("hono/utils/mime");
|
|
27
|
-
var import_node_fs = require("fs");
|
|
28
|
-
var import_node_path = require("path");
|
|
29
|
-
var import_node_process = require("process");
|
|
30
|
-
var import_node_stream = require("stream");
|
|
31
|
-
var COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
32
|
-
var ENCODINGS = {
|
|
33
|
-
br: ".br",
|
|
34
|
-
zstd: ".zst",
|
|
35
|
-
gzip: ".gz"
|
|
8
|
+
//#region src/serve-static.ts
|
|
9
|
+
const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
10
|
+
const ENCODINGS = {
|
|
11
|
+
br: ".br",
|
|
12
|
+
zstd: ".zst",
|
|
13
|
+
gzip: ".gz"
|
|
36
14
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
15
|
+
const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS);
|
|
16
|
+
const pr54206Applied = () => {
|
|
17
|
+
const [major, minor] = node_process.versions.node.split(".").map((component) => parseInt(component));
|
|
18
|
+
return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18;
|
|
41
19
|
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
return body;
|
|
20
|
+
const useReadableToWeb = pr54206Applied();
|
|
21
|
+
const createStreamBody = (stream) => {
|
|
22
|
+
if (useReadableToWeb) return node_stream.Readable.toWeb(stream);
|
|
23
|
+
return new ReadableStream({
|
|
24
|
+
start(controller) {
|
|
25
|
+
stream.on("data", (chunk) => {
|
|
26
|
+
controller.enqueue(chunk);
|
|
27
|
+
});
|
|
28
|
+
stream.on("error", (err) => {
|
|
29
|
+
controller.error(err);
|
|
30
|
+
});
|
|
31
|
+
stream.on("end", () => {
|
|
32
|
+
controller.close();
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
cancel() {
|
|
36
|
+
stream.destroy();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
64
39
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return stats;
|
|
40
|
+
const getStats = (path) => {
|
|
41
|
+
let stats;
|
|
42
|
+
try {
|
|
43
|
+
stats = (0, node_fs.statSync)(path);
|
|
44
|
+
} catch {}
|
|
45
|
+
return stats;
|
|
72
46
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
47
|
+
const tryDecode = (str, decoder) => {
|
|
48
|
+
try {
|
|
49
|
+
return decoder(str);
|
|
50
|
+
} catch {
|
|
51
|
+
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
52
|
+
try {
|
|
53
|
+
return decoder(match);
|
|
54
|
+
} catch {
|
|
55
|
+
return match;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
85
59
|
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
159
|
-
const start = parseInt(parts[0], 10) || 0;
|
|
160
|
-
let end = parseInt(parts[1], 10) || size - 1;
|
|
161
|
-
if (size < end - start + 1) {
|
|
162
|
-
end = size - 1;
|
|
163
|
-
}
|
|
164
|
-
const chunksize = end - start + 1;
|
|
165
|
-
const stream = (0, import_node_fs.createReadStream)(path, { start, end });
|
|
166
|
-
c.header("Content-Length", chunksize.toString());
|
|
167
|
-
c.header("Content-Range", `bytes ${start}-${end}/${stats.size}`);
|
|
168
|
-
result = c.body(createStreamBody(stream), 206);
|
|
169
|
-
}
|
|
170
|
-
await options.onFound?.(path, c);
|
|
171
|
-
return result;
|
|
172
|
-
};
|
|
60
|
+
const tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
61
|
+
const serveStatic = (options = { root: "" }) => {
|
|
62
|
+
const root = options.root || "";
|
|
63
|
+
const optionPath = options.path;
|
|
64
|
+
if (root !== "" && !(0, node_fs.existsSync)(root)) console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`);
|
|
65
|
+
return async (c, next) => {
|
|
66
|
+
if (c.finalized) return next();
|
|
67
|
+
let filename;
|
|
68
|
+
if (optionPath) filename = optionPath;
|
|
69
|
+
else try {
|
|
70
|
+
filename = tryDecodeURI(c.req.path);
|
|
71
|
+
if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) throw new Error();
|
|
72
|
+
} catch {
|
|
73
|
+
await options.onNotFound?.(c.req.path, c);
|
|
74
|
+
return next();
|
|
75
|
+
}
|
|
76
|
+
let path = (0, node_path.join)(root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename);
|
|
77
|
+
let stats = getStats(path);
|
|
78
|
+
if (stats && stats.isDirectory()) {
|
|
79
|
+
const indexFile = options.index ?? "index.html";
|
|
80
|
+
path = (0, node_path.join)(path, indexFile);
|
|
81
|
+
stats = getStats(path);
|
|
82
|
+
}
|
|
83
|
+
if (!stats) {
|
|
84
|
+
await options.onNotFound?.(path, c);
|
|
85
|
+
return next();
|
|
86
|
+
}
|
|
87
|
+
const mimeType = (0, hono_utils_mime.getMimeType)(path);
|
|
88
|
+
c.header("Content-Type", mimeType || "application/octet-stream");
|
|
89
|
+
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
|
|
90
|
+
const acceptEncodingSet = new Set(c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim()));
|
|
91
|
+
for (const encoding of ENCODINGS_ORDERED_KEYS) {
|
|
92
|
+
if (!acceptEncodingSet.has(encoding)) continue;
|
|
93
|
+
const precompressedStats = getStats(path + ENCODINGS[encoding]);
|
|
94
|
+
if (precompressedStats) {
|
|
95
|
+
c.header("Content-Encoding", encoding);
|
|
96
|
+
c.header("Vary", "Accept-Encoding", { append: true });
|
|
97
|
+
stats = precompressedStats;
|
|
98
|
+
path = path + ENCODINGS[encoding];
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let result;
|
|
104
|
+
const size = stats.size;
|
|
105
|
+
const range = c.req.header("range") || "";
|
|
106
|
+
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
107
|
+
c.header("Content-Length", size.toString());
|
|
108
|
+
c.status(200);
|
|
109
|
+
result = c.body(null);
|
|
110
|
+
} else if (!range) {
|
|
111
|
+
c.header("Content-Length", size.toString());
|
|
112
|
+
result = c.body(createStreamBody((0, node_fs.createReadStream)(path)), 200);
|
|
113
|
+
} else {
|
|
114
|
+
c.header("Accept-Ranges", "bytes");
|
|
115
|
+
c.header("Date", stats.birthtime.toUTCString());
|
|
116
|
+
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
117
|
+
const start = parseInt(parts[0], 10) || 0;
|
|
118
|
+
let end = parseInt(parts[1], 10) || size - 1;
|
|
119
|
+
if (size < end - start + 1) end = size - 1;
|
|
120
|
+
const chunksize = end - start + 1;
|
|
121
|
+
const stream = (0, node_fs.createReadStream)(path, {
|
|
122
|
+
start,
|
|
123
|
+
end
|
|
124
|
+
});
|
|
125
|
+
c.header("Content-Length", chunksize.toString());
|
|
126
|
+
c.header("Content-Range", `bytes ${start}-${end}/${stats.size}`);
|
|
127
|
+
result = c.body(createStreamBody(stream), 206);
|
|
128
|
+
}
|
|
129
|
+
await options.onFound?.(path, c);
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
173
132
|
};
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
});
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
exports.serveStatic = serveStatic;
|