@hono/node-server 1.2.0 → 1.2.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/dist/globals.d.mts +2 -0
- package/dist/globals.d.ts +2 -1
- package/dist/globals.js +4 -2
- package/dist/globals.mjs +1 -1
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +121 -5
- package/dist/index.mjs +105 -3
- package/dist/listener.d.mts +8 -0
- package/dist/listener.d.ts +8 -5
- package/dist/listener.js +39 -10
- package/dist/listener.mjs +26 -9
- package/dist/serve-static.d.mts +14 -0
- package/dist/serve-static.d.ts +6 -3
- package/dist/serve-static.js +122 -6
- package/dist/serve-static.mjs +116 -2
- package/dist/server.d.mts +10 -0
- package/dist/server.d.ts +10 -4
- package/dist/server.js +104 -5
- package/dist/server.mjs +89 -2
- package/dist/types.d.mts +33 -0
- package/dist/types.d.ts +17 -18
- package/dist/types.js +18 -0
- package/dist/types.mjs +0 -0
- package/dist/vercel.d.mts +7 -0
- package/dist/vercel.d.ts +7 -4
- package/dist/vercel.js +102 -3
- package/dist/vercel.mjs +86 -1
- package/package.json +6 -8
package/dist/globals.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
export { }
|
package/dist/globals.js
CHANGED
|
@@ -21,8 +21,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
22
|
mod
|
|
23
23
|
));
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
|
|
25
|
+
// src/globals.ts
|
|
26
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
27
|
+
var webFetch = global.fetch;
|
|
26
28
|
if (typeof global.crypto === "undefined") {
|
|
27
29
|
global.crypto = import_node_crypto.default;
|
|
28
30
|
}
|
package/dist/globals.mjs
CHANGED
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { getRequestListener } from './listener';
|
|
1
|
+
export { createAdaptorServer, serve } from './server.js';
|
|
2
|
+
export { getRequestListener } from './listener.js';
|
|
3
|
+
import 'node:net';
|
|
4
|
+
import './types.js';
|
|
5
|
+
import 'node:http';
|
|
6
|
+
import 'node:https';
|
|
7
|
+
import 'node:http2';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,16 +17,130 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
19
31
|
var src_exports = {};
|
|
20
32
|
__export(src_exports, {
|
|
21
|
-
createAdaptorServer: () =>
|
|
22
|
-
getRequestListener: () =>
|
|
23
|
-
serve: () =>
|
|
33
|
+
createAdaptorServer: () => createAdaptorServer,
|
|
34
|
+
getRequestListener: () => getRequestListener,
|
|
35
|
+
serve: () => serve
|
|
24
36
|
});
|
|
25
37
|
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
|
|
38
|
+
|
|
39
|
+
// src/server.ts
|
|
40
|
+
var import_node_http = require("http");
|
|
41
|
+
|
|
42
|
+
// src/listener.ts
|
|
43
|
+
var import_node_stream = require("stream");
|
|
44
|
+
var import_promises = require("stream/promises");
|
|
45
|
+
|
|
46
|
+
// src/globals.ts
|
|
47
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
48
|
+
var webFetch = global.fetch;
|
|
49
|
+
if (typeof global.crypto === "undefined") {
|
|
50
|
+
global.crypto = import_node_crypto.default;
|
|
51
|
+
}
|
|
52
|
+
global.fetch = (info, init) => {
|
|
53
|
+
init = {
|
|
54
|
+
// Disable compression handling so people can return the result of a fetch
|
|
55
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
56
|
+
compress: false,
|
|
57
|
+
...init
|
|
58
|
+
};
|
|
59
|
+
return webFetch(info, init);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// src/listener.ts
|
|
63
|
+
var regBuffer = /^no$/i;
|
|
64
|
+
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
65
|
+
var getRequestListener = (fetchCallback) => {
|
|
66
|
+
return async (incoming, outgoing) => {
|
|
67
|
+
const method = incoming.method || "GET";
|
|
68
|
+
const url = `http://${incoming.headers.host}${incoming.url}`;
|
|
69
|
+
const headerRecord = [];
|
|
70
|
+
const len = incoming.rawHeaders.length;
|
|
71
|
+
for (let i = 0; i < len; i += 2) {
|
|
72
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
73
|
+
}
|
|
74
|
+
const init = {
|
|
75
|
+
method,
|
|
76
|
+
headers: headerRecord
|
|
77
|
+
};
|
|
78
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
79
|
+
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
80
|
+
init.duplex = "half";
|
|
81
|
+
}
|
|
82
|
+
let res;
|
|
83
|
+
try {
|
|
84
|
+
res = await fetchCallback(new Request(url.toString(), init));
|
|
85
|
+
} catch (e) {
|
|
86
|
+
res = new Response(null, { status: 500 });
|
|
87
|
+
if (e instanceof Error) {
|
|
88
|
+
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
89
|
+
res = new Response(null, { status: 504 });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const resHeaderRecord = {};
|
|
94
|
+
for (const [k, v] of res.headers) {
|
|
95
|
+
resHeaderRecord[k] = v;
|
|
96
|
+
if (k === "set-cookie") {
|
|
97
|
+
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
98
|
+
} else {
|
|
99
|
+
outgoing.setHeader(k, v);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
outgoing.statusCode = res.status;
|
|
103
|
+
if (res.body) {
|
|
104
|
+
try {
|
|
105
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
106
|
+
regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
107
|
+
await (0, import_promises.pipeline)(import_node_stream.Readable.fromWeb(res.body), outgoing);
|
|
108
|
+
} else {
|
|
109
|
+
const text = await res.text();
|
|
110
|
+
outgoing.setHeader("Content-Length", Buffer.byteLength(text));
|
|
111
|
+
outgoing.end(text);
|
|
112
|
+
}
|
|
113
|
+
} catch (e) {
|
|
114
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
115
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
116
|
+
console.info("The user aborted a request.");
|
|
117
|
+
} else {
|
|
118
|
+
console.error(e);
|
|
119
|
+
outgoing.destroy(err);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
outgoing.end();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/server.ts
|
|
129
|
+
var createAdaptorServer = (options) => {
|
|
130
|
+
const fetchCallback = options.fetch;
|
|
131
|
+
const requestListener = getRequestListener(fetchCallback);
|
|
132
|
+
const createServer = options.createServer || import_node_http.createServer;
|
|
133
|
+
const server = createServer(options.serverOptions || {}, requestListener);
|
|
134
|
+
return server;
|
|
135
|
+
};
|
|
136
|
+
var serve = (options, listeningListener) => {
|
|
137
|
+
const server = createAdaptorServer(options);
|
|
138
|
+
server.listen(options?.port ?? 3e3, options.hostname ?? "0.0.0.0", () => {
|
|
139
|
+
const serverInfo = server.address();
|
|
140
|
+
listeningListener && listeningListener(serverInfo);
|
|
141
|
+
});
|
|
142
|
+
return server;
|
|
143
|
+
};
|
|
28
144
|
// Annotate the CommonJS export names for ESM import in node:
|
|
29
145
|
0 && (module.exports = {
|
|
30
146
|
createAdaptorServer,
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,108 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import { createServer as createServerHTTP } from "http";
|
|
3
|
+
|
|
4
|
+
// src/listener.ts
|
|
5
|
+
import { Readable } from "stream";
|
|
6
|
+
import { pipeline } from "stream/promises";
|
|
7
|
+
|
|
8
|
+
// src/globals.ts
|
|
9
|
+
import crypto from "crypto";
|
|
10
|
+
var webFetch = global.fetch;
|
|
11
|
+
if (typeof global.crypto === "undefined") {
|
|
12
|
+
global.crypto = crypto;
|
|
13
|
+
}
|
|
14
|
+
global.fetch = (info, init) => {
|
|
15
|
+
init = {
|
|
16
|
+
// Disable compression handling so people can return the result of a fetch
|
|
17
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
18
|
+
compress: false,
|
|
19
|
+
...init
|
|
20
|
+
};
|
|
21
|
+
return webFetch(info, init);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/listener.ts
|
|
25
|
+
var regBuffer = /^no$/i;
|
|
26
|
+
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
27
|
+
var getRequestListener = (fetchCallback) => {
|
|
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 = 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 });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const resHeaderRecord = {};
|
|
56
|
+
for (const [k, v] of res.headers) {
|
|
57
|
+
resHeaderRecord[k] = v;
|
|
58
|
+
if (k === "set-cookie") {
|
|
59
|
+
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
60
|
+
} else {
|
|
61
|
+
outgoing.setHeader(k, v);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
outgoing.statusCode = res.status;
|
|
65
|
+
if (res.body) {
|
|
66
|
+
try {
|
|
67
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
68
|
+
regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
69
|
+
await pipeline(Readable.fromWeb(res.body), outgoing);
|
|
70
|
+
} else {
|
|
71
|
+
const text = await res.text();
|
|
72
|
+
outgoing.setHeader("Content-Length", Buffer.byteLength(text));
|
|
73
|
+
outgoing.end(text);
|
|
74
|
+
}
|
|
75
|
+
} catch (e) {
|
|
76
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
77
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
78
|
+
console.info("The user aborted a request.");
|
|
79
|
+
} else {
|
|
80
|
+
console.error(e);
|
|
81
|
+
outgoing.destroy(err);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
outgoing.end();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/server.ts
|
|
91
|
+
var createAdaptorServer = (options) => {
|
|
92
|
+
const fetchCallback = options.fetch;
|
|
93
|
+
const requestListener = getRequestListener(fetchCallback);
|
|
94
|
+
const createServer = options.createServer || createServerHTTP;
|
|
95
|
+
const server = createServer(options.serverOptions || {}, requestListener);
|
|
96
|
+
return server;
|
|
97
|
+
};
|
|
98
|
+
var serve = (options, listeningListener) => {
|
|
99
|
+
const server = createAdaptorServer(options);
|
|
100
|
+
server.listen(options?.port ?? 3e3, options.hostname ?? "0.0.0.0", () => {
|
|
101
|
+
const serverInfo = server.address();
|
|
102
|
+
listeningListener && listeningListener(serverInfo);
|
|
103
|
+
});
|
|
104
|
+
return server;
|
|
105
|
+
};
|
|
4
106
|
export {
|
|
5
107
|
createAdaptorServer,
|
|
6
108
|
getRequestListener,
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
3
|
+
import { FetchCallback } from './types.mjs';
|
|
4
|
+
import 'node:https';
|
|
5
|
+
|
|
6
|
+
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
|
|
7
|
+
|
|
8
|
+
export { getRequestListener };
|
package/dist/listener.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import '
|
|
5
|
-
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
3
|
+
import { FetchCallback } from './types.js';
|
|
4
|
+
import 'node:https';
|
|
5
|
+
|
|
6
|
+
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
|
|
7
|
+
|
|
8
|
+
export { getRequestListener };
|
package/dist/listener.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,16 +17,45 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/listener.ts
|
|
19
31
|
var listener_exports = {};
|
|
20
32
|
__export(listener_exports, {
|
|
21
33
|
getRequestListener: () => getRequestListener
|
|
22
34
|
});
|
|
23
35
|
module.exports = __toCommonJS(listener_exports);
|
|
24
|
-
var import_node_stream = require("
|
|
25
|
-
var import_promises = require("
|
|
26
|
-
|
|
27
|
-
|
|
36
|
+
var import_node_stream = require("stream");
|
|
37
|
+
var import_promises = require("stream/promises");
|
|
38
|
+
|
|
39
|
+
// src/globals.ts
|
|
40
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
41
|
+
var webFetch = global.fetch;
|
|
42
|
+
if (typeof global.crypto === "undefined") {
|
|
43
|
+
global.crypto = import_node_crypto.default;
|
|
44
|
+
}
|
|
45
|
+
global.fetch = (info, init) => {
|
|
46
|
+
init = {
|
|
47
|
+
// Disable compression handling so people can return the result of a fetch
|
|
48
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
49
|
+
compress: false,
|
|
50
|
+
...init
|
|
51
|
+
};
|
|
52
|
+
return webFetch(info, init);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/listener.ts
|
|
56
|
+
var regBuffer = /^no$/i;
|
|
57
|
+
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
58
|
+
var getRequestListener = (fetchCallback) => {
|
|
28
59
|
return async (incoming, outgoing) => {
|
|
29
60
|
const method = incoming.method || "GET";
|
|
30
61
|
const url = `http://${incoming.headers.host}${incoming.url}`;
|
|
@@ -52,12 +83,9 @@ const getRequestListener = (fetchCallback) => {
|
|
|
52
83
|
}
|
|
53
84
|
}
|
|
54
85
|
}
|
|
55
|
-
const
|
|
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");
|
|
86
|
+
const resHeaderRecord = {};
|
|
60
87
|
for (const [k, v] of res.headers) {
|
|
88
|
+
resHeaderRecord[k] = v;
|
|
61
89
|
if (k === "set-cookie") {
|
|
62
90
|
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
63
91
|
} else {
|
|
@@ -67,7 +95,8 @@ const getRequestListener = (fetchCallback) => {
|
|
|
67
95
|
outgoing.statusCode = res.status;
|
|
68
96
|
if (res.body) {
|
|
69
97
|
try {
|
|
70
|
-
if (
|
|
98
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
99
|
+
regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
71
100
|
await (0, import_promises.pipeline)(import_node_stream.Readable.fromWeb(res.body), outgoing);
|
|
72
101
|
} else {
|
|
73
102
|
const text = await res.text();
|
package/dist/listener.mjs
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
1
|
// src/listener.ts
|
|
2
|
-
import { Readable } from "
|
|
3
|
-
import { pipeline } from "
|
|
4
|
-
|
|
2
|
+
import { Readable } from "stream";
|
|
3
|
+
import { pipeline } from "stream/promises";
|
|
4
|
+
|
|
5
|
+
// src/globals.ts
|
|
6
|
+
import crypto from "crypto";
|
|
7
|
+
var webFetch = global.fetch;
|
|
8
|
+
if (typeof global.crypto === "undefined") {
|
|
9
|
+
global.crypto = crypto;
|
|
10
|
+
}
|
|
11
|
+
global.fetch = (info, init) => {
|
|
12
|
+
init = {
|
|
13
|
+
// Disable compression handling so people can return the result of a fetch
|
|
14
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
15
|
+
compress: false,
|
|
16
|
+
...init
|
|
17
|
+
};
|
|
18
|
+
return webFetch(info, init);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/listener.ts
|
|
22
|
+
var regBuffer = /^no$/i;
|
|
23
|
+
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
5
24
|
var getRequestListener = (fetchCallback) => {
|
|
6
25
|
return async (incoming, outgoing) => {
|
|
7
26
|
const method = incoming.method || "GET";
|
|
@@ -30,12 +49,9 @@ var getRequestListener = (fetchCallback) => {
|
|
|
30
49
|
}
|
|
31
50
|
}
|
|
32
51
|
}
|
|
33
|
-
const
|
|
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");
|
|
52
|
+
const resHeaderRecord = {};
|
|
38
53
|
for (const [k, v] of res.headers) {
|
|
54
|
+
resHeaderRecord[k] = v;
|
|
39
55
|
if (k === "set-cookie") {
|
|
40
56
|
outgoing.setHeader(k, res.headers.getSetCookie(k));
|
|
41
57
|
} else {
|
|
@@ -45,7 +61,8 @@ var getRequestListener = (fetchCallback) => {
|
|
|
45
61
|
outgoing.statusCode = res.status;
|
|
46
62
|
if (res.body) {
|
|
47
63
|
try {
|
|
48
|
-
if (
|
|
64
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
65
|
+
regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
49
66
|
await pipeline(Readable.fromWeb(res.body), outgoing);
|
|
50
67
|
} else {
|
|
51
68
|
const text = await res.text();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MiddlewareHandler } from 'hono';
|
|
2
|
+
|
|
3
|
+
declare type ServeStaticOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Root path, relative to current working directory. (absolute paths are not supported)
|
|
6
|
+
*/
|
|
7
|
+
root?: string;
|
|
8
|
+
path?: string;
|
|
9
|
+
index?: string;
|
|
10
|
+
rewriteRequestPath?: (path: string) => string;
|
|
11
|
+
};
|
|
12
|
+
declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
|
|
13
|
+
|
|
14
|
+
export { ServeStaticOptions, serveStatic };
|
package/dist/serve-static.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { MiddlewareHandler } from 'hono';
|
|
2
|
+
|
|
3
|
+
declare type ServeStaticOptions = {
|
|
3
4
|
/**
|
|
4
5
|
* Root path, relative to current working directory. (absolute paths are not supported)
|
|
5
6
|
*/
|
|
@@ -8,4 +9,6 @@ export declare type ServeStaticOptions = {
|
|
|
8
9
|
index?: string;
|
|
9
10
|
rewriteRequestPath?: (path: string) => string;
|
|
10
11
|
};
|
|
11
|
-
|
|
12
|
+
declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
|
|
13
|
+
|
|
14
|
+
export { ServeStaticOptions, serveStatic };
|
package/dist/serve-static.js
CHANGED
|
@@ -16,15 +16,131 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/serve-static.ts
|
|
19
21
|
var serve_static_exports = {};
|
|
20
22
|
__export(serve_static_exports, {
|
|
21
23
|
serveStatic: () => serveStatic
|
|
22
24
|
});
|
|
23
25
|
module.exports = __toCommonJS(serve_static_exports);
|
|
24
26
|
var import_fs = require("fs");
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
|
|
28
|
+
// node_modules/hono/dist/utils/filepath.js
|
|
29
|
+
var getFilePath = (options) => {
|
|
30
|
+
let filename = options.filename;
|
|
31
|
+
if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename))
|
|
32
|
+
return;
|
|
33
|
+
let root = options.root || "";
|
|
34
|
+
const defaultDocument = options.defaultDocument || "index.html";
|
|
35
|
+
if (filename.endsWith("/")) {
|
|
36
|
+
filename = filename.concat(defaultDocument);
|
|
37
|
+
} else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {
|
|
38
|
+
filename = filename.concat("/" + defaultDocument);
|
|
39
|
+
}
|
|
40
|
+
filename = filename.replace(/^\.?[\/\\]/, "");
|
|
41
|
+
filename = filename.replace(/\\/, "/");
|
|
42
|
+
root = root.replace(/\/$/, "");
|
|
43
|
+
let path = root ? root + "/" + filename : filename;
|
|
44
|
+
path = path.replace(/^\.?\//, "");
|
|
45
|
+
return path;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// node_modules/hono/dist/utils/mime.js
|
|
49
|
+
var getMimeType = (filename) => {
|
|
50
|
+
const regexp = /\.([a-zA-Z0-9]+?)$/;
|
|
51
|
+
const match = filename.match(regexp);
|
|
52
|
+
if (!match)
|
|
53
|
+
return;
|
|
54
|
+
let mimeType = mimes[match[1]];
|
|
55
|
+
if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
|
|
56
|
+
mimeType += "; charset=utf-8";
|
|
57
|
+
}
|
|
58
|
+
return mimeType;
|
|
59
|
+
};
|
|
60
|
+
var mimes = {
|
|
61
|
+
aac: "audio/aac",
|
|
62
|
+
abw: "application/x-abiword",
|
|
63
|
+
arc: "application/x-freearc",
|
|
64
|
+
avi: "video/x-msvideo",
|
|
65
|
+
avif: "image/avif",
|
|
66
|
+
av1: "video/av1",
|
|
67
|
+
azw: "application/vnd.amazon.ebook",
|
|
68
|
+
bin: "application/octet-stream",
|
|
69
|
+
bmp: "image/bmp",
|
|
70
|
+
bz: "application/x-bzip",
|
|
71
|
+
bz2: "application/x-bzip2",
|
|
72
|
+
csh: "application/x-csh",
|
|
73
|
+
css: "text/css",
|
|
74
|
+
csv: "text/csv",
|
|
75
|
+
doc: "application/msword",
|
|
76
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
77
|
+
eot: "application/vnd.ms-fontobject",
|
|
78
|
+
epub: "application/epub+zip",
|
|
79
|
+
gif: "image/gif",
|
|
80
|
+
gz: "application/gzip",
|
|
81
|
+
htm: "text/html",
|
|
82
|
+
html: "text/html",
|
|
83
|
+
ico: "image/x-icon",
|
|
84
|
+
ics: "text/calendar",
|
|
85
|
+
jar: "application/java-archive",
|
|
86
|
+
jpeg: "image/jpeg",
|
|
87
|
+
jpg: "image/jpeg",
|
|
88
|
+
js: "text/javascript",
|
|
89
|
+
json: "application/json",
|
|
90
|
+
jsonld: "application/ld+json",
|
|
91
|
+
map: "application/json",
|
|
92
|
+
mid: "audio/x-midi",
|
|
93
|
+
midi: "audio/x-midi",
|
|
94
|
+
mjs: "text/javascript",
|
|
95
|
+
mp3: "audio/mpeg",
|
|
96
|
+
mp4: "video/mp4",
|
|
97
|
+
mpeg: "video/mpeg",
|
|
98
|
+
mpkg: "application/vnd.apple.installer+xml",
|
|
99
|
+
odp: "application/vnd.oasis.opendocument.presentation",
|
|
100
|
+
ods: "application/vnd.oasis.opendocument.spreadsheet",
|
|
101
|
+
odt: "application/vnd.oasis.opendocument.text",
|
|
102
|
+
oga: "audio/ogg",
|
|
103
|
+
ogv: "video/ogg",
|
|
104
|
+
ogx: "application/ogg",
|
|
105
|
+
opus: "audio/opus",
|
|
106
|
+
otf: "font/otf",
|
|
107
|
+
pdf: "application/pdf",
|
|
108
|
+
php: "application/php",
|
|
109
|
+
png: "image/png",
|
|
110
|
+
ppt: "application/vnd.ms-powerpoint",
|
|
111
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
112
|
+
rtf: "application/rtf",
|
|
113
|
+
sh: "application/x-sh",
|
|
114
|
+
svg: "image/svg+xml",
|
|
115
|
+
swf: "application/x-shockwave-flash",
|
|
116
|
+
tar: "application/x-tar",
|
|
117
|
+
tif: "image/tiff",
|
|
118
|
+
tiff: "image/tiff",
|
|
119
|
+
ts: "video/mp2t",
|
|
120
|
+
ttf: "font/ttf",
|
|
121
|
+
txt: "text/plain",
|
|
122
|
+
vsd: "application/vnd.visio",
|
|
123
|
+
wasm: "application/wasm",
|
|
124
|
+
webm: "video/webm",
|
|
125
|
+
weba: "audio/webm",
|
|
126
|
+
webp: "image/webp",
|
|
127
|
+
woff: "font/woff",
|
|
128
|
+
woff2: "font/woff2",
|
|
129
|
+
xhtml: "application/xhtml+xml",
|
|
130
|
+
xls: "application/vnd.ms-excel",
|
|
131
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
132
|
+
xml: "application/xml",
|
|
133
|
+
xul: "application/vnd.mozilla.xul+xml",
|
|
134
|
+
zip: "application/zip",
|
|
135
|
+
"3gp": "video/3gpp",
|
|
136
|
+
"3g2": "video/3gpp2",
|
|
137
|
+
"7z": "application/x-7z-compressed",
|
|
138
|
+
gltf: "model/gltf+json",
|
|
139
|
+
glb: "model/gltf-binary"
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/serve-static.ts
|
|
143
|
+
var createStreamBody = (stream) => {
|
|
28
144
|
const body = new ReadableStream({
|
|
29
145
|
start(controller) {
|
|
30
146
|
stream.on("data", (chunk) => {
|
|
@@ -40,13 +156,13 @@ const createStreamBody = (stream) => {
|
|
|
40
156
|
});
|
|
41
157
|
return body;
|
|
42
158
|
};
|
|
43
|
-
|
|
159
|
+
var serveStatic = (options = { root: "" }) => {
|
|
44
160
|
return async (c, next) => {
|
|
45
161
|
if (c.finalized)
|
|
46
162
|
return next();
|
|
47
163
|
const url = new URL(c.req.url);
|
|
48
164
|
const filename = options.path ?? decodeURIComponent(url.pathname);
|
|
49
|
-
let path =
|
|
165
|
+
let path = getFilePath({
|
|
50
166
|
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
|
|
51
167
|
root: options.root,
|
|
52
168
|
defaultDocument: options.index ?? "index.html"
|
|
@@ -57,7 +173,7 @@ const serveStatic = (options = { root: "" }) => {
|
|
|
57
173
|
if (!(0, import_fs.existsSync)(path)) {
|
|
58
174
|
return next();
|
|
59
175
|
}
|
|
60
|
-
const mimeType =
|
|
176
|
+
const mimeType = getMimeType(path);
|
|
61
177
|
if (mimeType) {
|
|
62
178
|
c.header("Content-Type", mimeType);
|
|
63
179
|
}
|