@anderspitman/fetch-handler 0.1.0 → 0.3.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/fetch_handler.js +45 -11
- package/package.json +1 -3
- package/bun.lock +0 -16
package/fetch_handler.js
CHANGED
|
@@ -10,20 +10,54 @@ async function serve(opt) {
|
|
|
10
10
|
break;
|
|
11
11
|
}
|
|
12
12
|
default: {
|
|
13
|
-
|
|
13
|
+
const http = await import('node:http');
|
|
14
|
+
const { Readable, pipeline } = await import('node:stream');
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
function incomingToRequest(req) {
|
|
17
|
+
const protocol = req.socket.encrypted ? "https" : "http";
|
|
18
|
+
const url = `${protocol}://${req.headers.host}${req.url}`;
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
app.use('*', (c) => {
|
|
20
|
-
return opt.handler(c.req.raw);
|
|
21
|
-
});
|
|
20
|
+
const hasBody = req.method !== "GET" && req.method !== "HEAD";
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
return new Request(url, {
|
|
23
|
+
method: req.method,
|
|
24
|
+
headers: req.headers,
|
|
25
|
+
body: hasBody ? Readable.toWeb(req) : undefined,
|
|
26
|
+
duplex: hasBody ? "half" : undefined,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function sendResponse(nodeRes, res) {
|
|
31
|
+
nodeRes.statusCode = res.status;
|
|
32
|
+
nodeRes.statusMessage = res.statusText;
|
|
33
|
+
|
|
34
|
+
for (const [key, value] of res.headers) {
|
|
35
|
+
nodeRes.setHeader(key, value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!res.body) {
|
|
39
|
+
nodeRes.end();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
pipeline(Readable.fromWeb(res.body), nodeRes, (err) => {
|
|
44
|
+
if (err) {
|
|
45
|
+
nodeRes.destroy(err);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
http.createServer(async (nodeReq, nodeRes) => {
|
|
51
|
+
|
|
52
|
+
const req = incomingToRequest(nodeReq);
|
|
53
|
+
|
|
54
|
+
const res = await opt.handler(req, nodeReq, nodeRes);
|
|
55
|
+
|
|
56
|
+
if (res) {
|
|
57
|
+
sendResponse(nodeRes, res);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
}).listen(opt.port);
|
|
27
61
|
|
|
28
62
|
break;
|
|
29
63
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@hono/node-server": "^1.17.1",
|
|
5
|
-
"hono": "^4.8.10"
|
|
6
4
|
},
|
|
7
5
|
"name": "@anderspitman/fetch-handler",
|
|
8
6
|
"publishConfig": {
|
|
9
7
|
"access": "public"
|
|
10
8
|
},
|
|
11
|
-
"version": "0.
|
|
9
|
+
"version": "0.3.0",
|
|
12
10
|
"main": "fetch_handler.js",
|
|
13
11
|
"devDependencies": {},
|
|
14
12
|
"scripts": {
|
package/bun.lock
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"lockfileVersion": 1,
|
|
3
|
-
"workspaces": {
|
|
4
|
-
"": {
|
|
5
|
-
"dependencies": {
|
|
6
|
-
"@hono/node-server": "^1.17.1",
|
|
7
|
-
"hono": "^4.8.10",
|
|
8
|
-
},
|
|
9
|
-
},
|
|
10
|
-
},
|
|
11
|
-
"packages": {
|
|
12
|
-
"@hono/node-server": ["@hono/node-server@1.17.1", "", { "peerDependencies": { "hono": "^4" } }, "sha512-SY79W/C+2b1MyAzmIcV32Q47vO1b5XwLRwj8S9N6Jr5n1QCkIfAIH6umOSgqWZ4/v67hg6qq8Ha5vZonVidGsg=="],
|
|
13
|
-
|
|
14
|
-
"hono": ["hono@4.8.10", "", {}, "sha512-DRMYbR3aFk6YET1FCSAFbgF2cWYTz5j0YAFYPECx9fmrbKBDAYnWU+YCgRTpOaatxMYN6e68U/2IG39zRP4W/A=="],
|
|
15
|
-
}
|
|
16
|
-
}
|