@hono/node-server 1.19.14 → 2.0.0-rc.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/README.md +56 -18
- package/dist/conninfo.cjs +22 -0
- package/dist/{conninfo.d.ts → conninfo.d.cts} +4 -3
- package/dist/conninfo.d.mts +4 -3
- package/dist/conninfo.mjs +19 -16
- package/dist/constants-BLSFu_RU.mjs +5 -0
- package/dist/constants-BXAKTxRC.cjs +11 -0
- package/dist/index.cjs +1006 -0
- package/dist/index.d.cts +73 -0
- package/dist/index.d.mts +73 -8
- package/dist/index.mjs +976 -637
- package/dist/serve-static.cjs +135 -0
- package/dist/serve-static.d.cts +18 -0
- package/dist/serve-static.d.mts +14 -13
- package/dist/serve-static.mjs +127 -145
- package/dist/utils/response.cjs +8 -0
- package/dist/utils/response.d.cts +4 -0
- package/dist/utils/response.d.mts +3 -2
- package/dist/utils/response.mjs +6 -9
- package/package.json +53 -40
- package/dist/conninfo.js +0 -42
- 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/index.d.ts +0 -8
- package/dist/index.js +0 -702
- package/dist/listener.d.mts +0 -13
- package/dist/listener.d.ts +0 -13
- package/dist/listener.js +0 -670
- package/dist/listener.mjs +0 -635
- package/dist/request.d.mts +0 -25
- package/dist/request.d.ts +0 -25
- package/dist/request.js +0 -238
- package/dist/request.mjs +0 -206
- package/dist/response.d.mts +0 -26
- package/dist/response.d.ts +0 -26
- package/dist/response.js +0 -112
- package/dist/response.mjs +0 -85
- package/dist/serve-static.d.ts +0 -17
- package/dist/serve-static.js +0 -177
- package/dist/server.d.mts +0 -10
- package/dist/server.d.ts +0 -10
- package/dist/server.js +0 -696
- package/dist/server.mjs +0 -660
- 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/response.d.ts +0 -3
- package/dist/utils/response.js +0 -37
- 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/vercel.d.mts +0 -7
- package/dist/vercel.d.ts +0 -7
- package/dist/vercel.js +0 -677
- package/dist/vercel.mjs +0 -640
|
@@ -0,0 +1,135 @@
|
|
|
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");
|
|
7
|
+
|
|
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"
|
|
14
|
+
};
|
|
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;
|
|
19
|
+
};
|
|
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
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const getStats = (path) => {
|
|
41
|
+
let stats;
|
|
42
|
+
try {
|
|
43
|
+
stats = (0, node_fs.statSync)(path);
|
|
44
|
+
} catch {}
|
|
45
|
+
return stats;
|
|
46
|
+
};
|
|
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
|
+
}
|
|
59
|
+
};
|
|
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 (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}/.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
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
exports.serveStatic = serveStatic;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Context, Env, MiddlewareHandler } from "hono";
|
|
2
|
+
|
|
3
|
+
//#region src/serve-static.d.ts
|
|
4
|
+
type ServeStaticOptions<E extends Env = Env> = {
|
|
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>;
|
|
15
|
+
};
|
|
16
|
+
declare const serveStatic: <E extends Env = any>(options?: ServeStaticOptions<E>) => MiddlewareHandler<E>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ServeStaticOptions, serveStatic };
|
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 {
|
|
17
|
+
//#endregion
|
|
18
|
+
export { ServeStaticOptions, serveStatic };
|
package/dist/serve-static.mjs
CHANGED
|
@@ -1,152 +1,134 @@
|
|
|
1
|
-
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
2
|
import { getMimeType } from "hono/utils/mime";
|
|
3
|
-
import { createReadStream,
|
|
4
|
-
import { join } from "path";
|
|
5
|
-
import { versions } from "process";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { versions } from "node:process";
|
|
6
|
+
|
|
7
|
+
//#region src/serve-static.ts
|
|
8
|
+
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;
|
|
9
|
+
const ENCODINGS = {
|
|
10
|
+
br: ".br",
|
|
11
|
+
zstd: ".zst",
|
|
12
|
+
gzip: ".gz"
|
|
12
13
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
const ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS);
|
|
15
|
+
const pr54206Applied = () => {
|
|
16
|
+
const [major, minor] = versions.node.split(".").map((component) => parseInt(component));
|
|
17
|
+
return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18;
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
return body;
|
|
19
|
+
const useReadableToWeb = pr54206Applied();
|
|
20
|
+
const createStreamBody = (stream) => {
|
|
21
|
+
if (useReadableToWeb) return Readable.toWeb(stream);
|
|
22
|
+
return new ReadableStream({
|
|
23
|
+
start(controller) {
|
|
24
|
+
stream.on("data", (chunk) => {
|
|
25
|
+
controller.enqueue(chunk);
|
|
26
|
+
});
|
|
27
|
+
stream.on("error", (err) => {
|
|
28
|
+
controller.error(err);
|
|
29
|
+
});
|
|
30
|
+
stream.on("end", () => {
|
|
31
|
+
controller.close();
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
cancel() {
|
|
35
|
+
stream.destroy();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
40
38
|
};
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return stats;
|
|
39
|
+
const getStats = (path) => {
|
|
40
|
+
let stats;
|
|
41
|
+
try {
|
|
42
|
+
stats = statSync(path);
|
|
43
|
+
} catch {}
|
|
44
|
+
return stats;
|
|
48
45
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
46
|
+
const tryDecode = (str, decoder) => {
|
|
47
|
+
try {
|
|
48
|
+
return decoder(str);
|
|
49
|
+
} catch {
|
|
50
|
+
return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
|
|
51
|
+
try {
|
|
52
|
+
return decoder(match);
|
|
53
|
+
} catch {
|
|
54
|
+
return match;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
61
58
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
135
|
-
const start = parseInt(parts[0], 10) || 0;
|
|
136
|
-
let end = parseInt(parts[1], 10) || size - 1;
|
|
137
|
-
if (size < end - start + 1) {
|
|
138
|
-
end = size - 1;
|
|
139
|
-
}
|
|
140
|
-
const chunksize = end - start + 1;
|
|
141
|
-
const stream = createReadStream(path, { start, end });
|
|
142
|
-
c.header("Content-Length", chunksize.toString());
|
|
143
|
-
c.header("Content-Range", `bytes ${start}-${end}/${stats.size}`);
|
|
144
|
-
result = c.body(createStreamBody(stream), 206);
|
|
145
|
-
}
|
|
146
|
-
await options.onFound?.(path, c);
|
|
147
|
-
return result;
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
export {
|
|
151
|
-
serveStatic
|
|
59
|
+
const tryDecodeURI = (str) => tryDecode(str, decodeURI);
|
|
60
|
+
const serveStatic = (options = { root: "" }) => {
|
|
61
|
+
const root = options.root || "";
|
|
62
|
+
const optionPath = options.path;
|
|
63
|
+
if (root !== "" && !existsSync(root)) console.error(`serveStatic: root path '${root}' is not found, are you sure it's correct?`);
|
|
64
|
+
return async (c, next) => {
|
|
65
|
+
if (c.finalized) return next();
|
|
66
|
+
let filename;
|
|
67
|
+
if (optionPath) filename = optionPath;
|
|
68
|
+
else try {
|
|
69
|
+
filename = tryDecodeURI(c.req.path);
|
|
70
|
+
if (/(?:^|[\/\\])\.{1,2}(?:$|[\/\\])|[\/\\]{2,}/.test(filename)) throw new Error();
|
|
71
|
+
} catch {
|
|
72
|
+
await options.onNotFound?.(c.req.path, c);
|
|
73
|
+
return next();
|
|
74
|
+
}
|
|
75
|
+
let path = join(root, !optionPath && options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename);
|
|
76
|
+
let stats = getStats(path);
|
|
77
|
+
if (stats && stats.isDirectory()) {
|
|
78
|
+
const indexFile = options.index ?? "index.html";
|
|
79
|
+
path = join(path, indexFile);
|
|
80
|
+
stats = getStats(path);
|
|
81
|
+
}
|
|
82
|
+
if (!stats) {
|
|
83
|
+
await options.onNotFound?.(path, c);
|
|
84
|
+
return next();
|
|
85
|
+
}
|
|
86
|
+
const mimeType = getMimeType(path);
|
|
87
|
+
c.header("Content-Type", mimeType || "application/octet-stream");
|
|
88
|
+
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) {
|
|
89
|
+
const acceptEncodingSet = new Set(c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim()));
|
|
90
|
+
for (const encoding of ENCODINGS_ORDERED_KEYS) {
|
|
91
|
+
if (!acceptEncodingSet.has(encoding)) continue;
|
|
92
|
+
const precompressedStats = getStats(path + ENCODINGS[encoding]);
|
|
93
|
+
if (precompressedStats) {
|
|
94
|
+
c.header("Content-Encoding", encoding);
|
|
95
|
+
c.header("Vary", "Accept-Encoding", { append: true });
|
|
96
|
+
stats = precompressedStats;
|
|
97
|
+
path = path + ENCODINGS[encoding];
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
let result;
|
|
103
|
+
const size = stats.size;
|
|
104
|
+
const range = c.req.header("range") || "";
|
|
105
|
+
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
106
|
+
c.header("Content-Length", size.toString());
|
|
107
|
+
c.status(200);
|
|
108
|
+
result = c.body(null);
|
|
109
|
+
} else if (!range) {
|
|
110
|
+
c.header("Content-Length", size.toString());
|
|
111
|
+
result = c.body(createStreamBody(createReadStream(path)), 200);
|
|
112
|
+
} else {
|
|
113
|
+
c.header("Accept-Ranges", "bytes");
|
|
114
|
+
c.header("Date", stats.birthtime.toUTCString());
|
|
115
|
+
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
116
|
+
const start = parseInt(parts[0], 10) || 0;
|
|
117
|
+
let end = parseInt(parts[1], 10) || size - 1;
|
|
118
|
+
if (size < end - start + 1) end = size - 1;
|
|
119
|
+
const chunksize = end - start + 1;
|
|
120
|
+
const stream = createReadStream(path, {
|
|
121
|
+
start,
|
|
122
|
+
end
|
|
123
|
+
});
|
|
124
|
+
c.header("Content-Length", chunksize.toString());
|
|
125
|
+
c.header("Content-Range", `bytes ${start}-${end}/${stats.size}`);
|
|
126
|
+
result = c.body(createStreamBody(stream), 206);
|
|
127
|
+
}
|
|
128
|
+
await options.onFound?.(path, c);
|
|
129
|
+
return result;
|
|
130
|
+
};
|
|
152
131
|
};
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
export { serveStatic };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_constants = require('../constants-BXAKTxRC.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/utils/response.ts
|
|
5
|
+
const RESPONSE_ALREADY_SENT = new Response(null, { headers: { [require_constants.X_ALREADY_SENT]: "true" } });
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
exports.RESPONSE_ALREADY_SENT = RESPONSE_ALREADY_SENT;
|
package/dist/utils/response.mjs
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
1
|
+
import { t as X_ALREADY_SENT } from "../constants-BLSFu_RU.mjs";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
RESPONSE_ALREADY_SENT
|
|
10
|
-
};
|
|
3
|
+
//#region src/utils/response.ts
|
|
4
|
+
const RESPONSE_ALREADY_SENT = new Response(null, { headers: { [X_ALREADY_SENT]: "true" } });
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
export { RESPONSE_ALREADY_SENT };
|