@hono/node-server 2.0.1 → 2.0.3
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/dist/index.cjs +7 -2
- package/dist/index.mjs +7 -2
- package/dist/serve-static.cjs +4 -29
- package/dist/serve-static.d.cts +1 -1
- package/dist/serve-static.d.mts +1 -1
- package/dist/serve-static.mjs +2 -27
- package/dist/utils/stream.cjs +65 -0
- package/dist/utils/stream.d.cts +6 -0
- package/dist/utils/stream.d.mts +6 -0
- package/dist/utils/stream.mjs +64 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -400,8 +400,13 @@ var Response$1 = class Response$1 {
|
|
|
400
400
|
#body;
|
|
401
401
|
#init;
|
|
402
402
|
[getResponseCache]() {
|
|
403
|
+
const cache = this[cacheKey];
|
|
404
|
+
const liveHeaders = cache && cache[2] instanceof Headers ? cache[2] : void 0;
|
|
403
405
|
delete this[cacheKey];
|
|
404
|
-
return this[responseCache] ||= new GlobalResponse(this.#body,
|
|
406
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, liveHeaders ? {
|
|
407
|
+
...this.#init,
|
|
408
|
+
headers: liveHeaders
|
|
409
|
+
} : this.#init);
|
|
405
410
|
}
|
|
406
411
|
constructor(body, init) {
|
|
407
412
|
let headers;
|
|
@@ -414,7 +419,7 @@ var Response$1 = class Response$1 {
|
|
|
414
419
|
return;
|
|
415
420
|
} else {
|
|
416
421
|
this.#init = init.#init;
|
|
417
|
-
headers = new Headers(init
|
|
422
|
+
headers = new Headers(init.headers);
|
|
418
423
|
}
|
|
419
424
|
} else this.#init = init;
|
|
420
425
|
if (body == null || typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) this[cacheKey] = [
|
package/dist/index.mjs
CHANGED
|
@@ -399,8 +399,13 @@ var Response$1 = class Response$1 {
|
|
|
399
399
|
#body;
|
|
400
400
|
#init;
|
|
401
401
|
[getResponseCache]() {
|
|
402
|
+
const cache = this[cacheKey];
|
|
403
|
+
const liveHeaders = cache && cache[2] instanceof Headers ? cache[2] : void 0;
|
|
402
404
|
delete this[cacheKey];
|
|
403
|
-
return this[responseCache] ||= new GlobalResponse(this.#body,
|
|
405
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, liveHeaders ? {
|
|
406
|
+
...this.#init,
|
|
407
|
+
headers: liveHeaders
|
|
408
|
+
} : this.#init);
|
|
404
409
|
}
|
|
405
410
|
constructor(body, init) {
|
|
406
411
|
let headers;
|
|
@@ -413,7 +418,7 @@ var Response$1 = class Response$1 {
|
|
|
413
418
|
return;
|
|
414
419
|
} else {
|
|
415
420
|
this.#init = init.#init;
|
|
416
|
-
headers = new Headers(init
|
|
421
|
+
headers = new Headers(init.headers);
|
|
417
422
|
}
|
|
418
423
|
} else this.#init = init;
|
|
419
424
|
if (body == null || typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) this[cacheKey] = [
|
package/dist/serve-static.cjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
2
|
+
const require_utils_stream = require('./utils/stream.cjs');
|
|
3
3
|
let hono_utils_mime = require("hono/utils/mime");
|
|
4
4
|
let node_fs = require("node:fs");
|
|
5
5
|
let node_path = require("node:path");
|
|
6
|
-
let node_process = require("node:process");
|
|
7
6
|
|
|
8
7
|
//#region src/serve-static.ts
|
|
9
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;
|
|
@@ -13,30 +12,6 @@ const ENCODINGS = {
|
|
|
13
12
|
gzip: ".gz"
|
|
14
13
|
};
|
|
15
14
|
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
15
|
const getStats = (path) => {
|
|
41
16
|
let stats;
|
|
42
17
|
try {
|
|
@@ -103,16 +78,16 @@ const serveStatic = (options = { root: "" }) => {
|
|
|
103
78
|
let result;
|
|
104
79
|
const size = stats.size;
|
|
105
80
|
const range = c.req.header("range") || "";
|
|
81
|
+
c.header("Last-Modified", stats.mtime.toUTCString());
|
|
106
82
|
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
107
83
|
c.header("Content-Length", size.toString());
|
|
108
84
|
c.status(200);
|
|
109
85
|
result = c.body(null);
|
|
110
86
|
} else if (!range) {
|
|
111
87
|
c.header("Content-Length", size.toString());
|
|
112
|
-
result = c.body(createStreamBody((0, node_fs.createReadStream)(path)), 200);
|
|
88
|
+
result = c.body(require_utils_stream.createStreamBody((0, node_fs.createReadStream)(path)), 200);
|
|
113
89
|
} else {
|
|
114
90
|
c.header("Accept-Ranges", "bytes");
|
|
115
|
-
c.header("Date", stats.birthtime.toUTCString());
|
|
116
91
|
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
117
92
|
const start = parseInt(parts[0], 10) || 0;
|
|
118
93
|
let end = parseInt(parts[1], 10) || size - 1;
|
|
@@ -124,7 +99,7 @@ const serveStatic = (options = { root: "" }) => {
|
|
|
124
99
|
});
|
|
125
100
|
c.header("Content-Length", chunksize.toString());
|
|
126
101
|
c.header("Content-Range", `bytes ${start}-${end}/${stats.size}`);
|
|
127
|
-
result = c.body(createStreamBody(stream), 206);
|
|
102
|
+
result = c.body(require_utils_stream.createStreamBody(stream), 206);
|
|
128
103
|
}
|
|
129
104
|
await options.onFound?.(path, c);
|
|
130
105
|
return result;
|
package/dist/serve-static.d.cts
CHANGED
|
@@ -3,7 +3,7 @@ import { Context, Env, MiddlewareHandler } from "hono";
|
|
|
3
3
|
//#region src/serve-static.d.ts
|
|
4
4
|
type ServeStaticOptions<E extends Env = Env> = {
|
|
5
5
|
/**
|
|
6
|
-
* Root path
|
|
6
|
+
* Root path. Relative path is based on current working directory from which the app was started.
|
|
7
7
|
*/
|
|
8
8
|
root?: string;
|
|
9
9
|
path?: string;
|
package/dist/serve-static.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { Context, Env, MiddlewareHandler } from "hono";
|
|
|
3
3
|
//#region src/serve-static.d.ts
|
|
4
4
|
type ServeStaticOptions<E extends Env = Env> = {
|
|
5
5
|
/**
|
|
6
|
-
* Root path
|
|
6
|
+
* Root path. Relative path is based on current working directory from which the app was started.
|
|
7
7
|
*/
|
|
8
8
|
root?: string;
|
|
9
9
|
path?: string;
|
package/dist/serve-static.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createStreamBody } from "./utils/stream.mjs";
|
|
2
2
|
import { getMimeType } from "hono/utils/mime";
|
|
3
3
|
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import { versions } from "node:process";
|
|
6
5
|
|
|
7
6
|
//#region src/serve-static.ts
|
|
8
7
|
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;
|
|
@@ -12,30 +11,6 @@ const ENCODINGS = {
|
|
|
12
11
|
gzip: ".gz"
|
|
13
12
|
};
|
|
14
13
|
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;
|
|
18
|
-
};
|
|
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
|
-
});
|
|
38
|
-
};
|
|
39
14
|
const getStats = (path) => {
|
|
40
15
|
let stats;
|
|
41
16
|
try {
|
|
@@ -102,6 +77,7 @@ const serveStatic = (options = { root: "" }) => {
|
|
|
102
77
|
let result;
|
|
103
78
|
const size = stats.size;
|
|
104
79
|
const range = c.req.header("range") || "";
|
|
80
|
+
c.header("Last-Modified", stats.mtime.toUTCString());
|
|
105
81
|
if (c.req.method == "HEAD" || c.req.method == "OPTIONS") {
|
|
106
82
|
c.header("Content-Length", size.toString());
|
|
107
83
|
c.status(200);
|
|
@@ -111,7 +87,6 @@ const serveStatic = (options = { root: "" }) => {
|
|
|
111
87
|
result = c.body(createStreamBody(createReadStream(path)), 200);
|
|
112
88
|
} else {
|
|
113
89
|
c.header("Accept-Ranges", "bytes");
|
|
114
|
-
c.header("Date", stats.birthtime.toUTCString());
|
|
115
90
|
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
116
91
|
const start = parseInt(parts[0], 10) || 0;
|
|
117
92
|
let end = parseInt(parts[1], 10) || size - 1;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let node_stream = require("node:stream");
|
|
3
|
+
let node_process = require("node:process");
|
|
4
|
+
|
|
5
|
+
//#region src/utils/stream.ts
|
|
6
|
+
const pr54206Applied = () => {
|
|
7
|
+
const [major, minor] = node_process.versions.node.split(".").map((component) => parseInt(component));
|
|
8
|
+
return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18;
|
|
9
|
+
};
|
|
10
|
+
const useReadableToWeb = pr54206Applied();
|
|
11
|
+
const createStreamBody = (stream, useNativeReadableToWeb = useReadableToWeb) => {
|
|
12
|
+
if (useNativeReadableToWeb) return node_stream.Readable.toWeb(stream);
|
|
13
|
+
let controller;
|
|
14
|
+
let settled = false;
|
|
15
|
+
const cleanup = () => {
|
|
16
|
+
stream.off("data", onData);
|
|
17
|
+
stream.off("error", onError);
|
|
18
|
+
stream.off("end", onTerminate);
|
|
19
|
+
stream.off("close", onTerminate);
|
|
20
|
+
};
|
|
21
|
+
const settle = (callback) => {
|
|
22
|
+
if (settled) return;
|
|
23
|
+
settled = true;
|
|
24
|
+
cleanup();
|
|
25
|
+
callback?.();
|
|
26
|
+
};
|
|
27
|
+
const onData = (chunk) => {
|
|
28
|
+
if (settled || !controller) return;
|
|
29
|
+
controller.enqueue(chunk);
|
|
30
|
+
if ((controller.desiredSize ?? 0) <= 0) stream.pause();
|
|
31
|
+
};
|
|
32
|
+
const onError = (error) => {
|
|
33
|
+
settle(() => {
|
|
34
|
+
controller?.error(error);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const onTerminate = () => {
|
|
38
|
+
settle(() => {
|
|
39
|
+
controller?.close();
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
return new ReadableStream({
|
|
43
|
+
start(streamController) {
|
|
44
|
+
controller = streamController;
|
|
45
|
+
stream.on("data", onData);
|
|
46
|
+
stream.on("error", onError);
|
|
47
|
+
stream.on("end", onTerminate);
|
|
48
|
+
stream.on("close", onTerminate);
|
|
49
|
+
stream.pause();
|
|
50
|
+
},
|
|
51
|
+
pull() {
|
|
52
|
+
if (!settled) stream.resume();
|
|
53
|
+
},
|
|
54
|
+
cancel() {
|
|
55
|
+
settle();
|
|
56
|
+
const ignoreError = () => {};
|
|
57
|
+
stream.on("error", ignoreError);
|
|
58
|
+
stream.once("close", () => stream.off("error", ignoreError));
|
|
59
|
+
stream.destroy();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
exports.createStreamBody = createStreamBody;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Readable } from "node:stream";
|
|
2
|
+
import { versions } from "node:process";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/stream.ts
|
|
5
|
+
const pr54206Applied = () => {
|
|
6
|
+
const [major, minor] = versions.node.split(".").map((component) => parseInt(component));
|
|
7
|
+
return major >= 23 || major === 22 && minor >= 7 || major === 20 && minor >= 18;
|
|
8
|
+
};
|
|
9
|
+
const useReadableToWeb = pr54206Applied();
|
|
10
|
+
const createStreamBody = (stream, useNativeReadableToWeb = useReadableToWeb) => {
|
|
11
|
+
if (useNativeReadableToWeb) return Readable.toWeb(stream);
|
|
12
|
+
let controller;
|
|
13
|
+
let settled = false;
|
|
14
|
+
const cleanup = () => {
|
|
15
|
+
stream.off("data", onData);
|
|
16
|
+
stream.off("error", onError);
|
|
17
|
+
stream.off("end", onTerminate);
|
|
18
|
+
stream.off("close", onTerminate);
|
|
19
|
+
};
|
|
20
|
+
const settle = (callback) => {
|
|
21
|
+
if (settled) return;
|
|
22
|
+
settled = true;
|
|
23
|
+
cleanup();
|
|
24
|
+
callback?.();
|
|
25
|
+
};
|
|
26
|
+
const onData = (chunk) => {
|
|
27
|
+
if (settled || !controller) return;
|
|
28
|
+
controller.enqueue(chunk);
|
|
29
|
+
if ((controller.desiredSize ?? 0) <= 0) stream.pause();
|
|
30
|
+
};
|
|
31
|
+
const onError = (error) => {
|
|
32
|
+
settle(() => {
|
|
33
|
+
controller?.error(error);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const onTerminate = () => {
|
|
37
|
+
settle(() => {
|
|
38
|
+
controller?.close();
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
return new ReadableStream({
|
|
42
|
+
start(streamController) {
|
|
43
|
+
controller = streamController;
|
|
44
|
+
stream.on("data", onData);
|
|
45
|
+
stream.on("error", onError);
|
|
46
|
+
stream.on("end", onTerminate);
|
|
47
|
+
stream.on("close", onTerminate);
|
|
48
|
+
stream.pause();
|
|
49
|
+
},
|
|
50
|
+
pull() {
|
|
51
|
+
if (!settled) stream.resume();
|
|
52
|
+
},
|
|
53
|
+
cancel() {
|
|
54
|
+
settle();
|
|
55
|
+
const ignoreError = () => {};
|
|
56
|
+
stream.on("error", ignoreError);
|
|
57
|
+
stream.once("close", () => stream.off("error", ignoreError));
|
|
58
|
+
stream.destroy();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createStreamBody };
|