@hono/node-server 1.3.2 → 1.3.4
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.js +14 -10
- package/dist/globals.mjs +14 -10
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +90 -78
- package/dist/index.mjs +90 -78
- package/dist/listener.js +90 -78
- package/dist/listener.mjs +90 -78
- package/dist/response.d.mts +0 -2
- package/dist/response.d.ts +0 -2
- package/dist/response.js +14 -10
- package/dist/response.mjs +14 -10
- package/dist/server.d.mts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +90 -78
- package/dist/server.mjs +90 -78
- package/dist/types.d.mts +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils.d.mts +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/vercel.js +90 -78
- package/dist/vercel.mjs +90 -78
- package/package.json +7 -3
package/dist/vercel.mjs
CHANGED
|
@@ -1,5 +1,68 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import
|
|
1
|
+
// src/request.ts
|
|
2
|
+
import { Readable } from "stream";
|
|
3
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
4
|
+
const headerRecord = [];
|
|
5
|
+
const len = incoming.rawHeaders.length;
|
|
6
|
+
for (let i = 0; i < len; i += 2) {
|
|
7
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
8
|
+
}
|
|
9
|
+
const init = {
|
|
10
|
+
method,
|
|
11
|
+
headers: headerRecord
|
|
12
|
+
};
|
|
13
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
14
|
+
init.body = Readable.toWeb(incoming);
|
|
15
|
+
init.duplex = "half";
|
|
16
|
+
}
|
|
17
|
+
return new Request(url, init);
|
|
18
|
+
};
|
|
19
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
20
|
+
var requestCache = Symbol("requestCache");
|
|
21
|
+
var incomingKey = Symbol("incomingKey");
|
|
22
|
+
var requestPrototype = {
|
|
23
|
+
get method() {
|
|
24
|
+
return this[incomingKey].method || "GET";
|
|
25
|
+
},
|
|
26
|
+
get url() {
|
|
27
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
28
|
+
},
|
|
29
|
+
[getRequestCache]() {
|
|
30
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
[
|
|
34
|
+
"body",
|
|
35
|
+
"bodyUsed",
|
|
36
|
+
"cache",
|
|
37
|
+
"credentials",
|
|
38
|
+
"destination",
|
|
39
|
+
"headers",
|
|
40
|
+
"integrity",
|
|
41
|
+
"mode",
|
|
42
|
+
"redirect",
|
|
43
|
+
"referrer",
|
|
44
|
+
"referrerPolicy",
|
|
45
|
+
"signal"
|
|
46
|
+
].forEach((k) => {
|
|
47
|
+
Object.defineProperty(requestPrototype, k, {
|
|
48
|
+
get() {
|
|
49
|
+
return this[getRequestCache]()[k];
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
54
|
+
Object.defineProperty(requestPrototype, k, {
|
|
55
|
+
value: function() {
|
|
56
|
+
return this[getRequestCache]()[k]();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
61
|
+
var newRequest = (incoming) => {
|
|
62
|
+
const req = Object.create(requestPrototype);
|
|
63
|
+
req[incomingKey] = incoming;
|
|
64
|
+
return req;
|
|
65
|
+
};
|
|
3
66
|
|
|
4
67
|
// src/utils.ts
|
|
5
68
|
function writeFromReadableStream(stream, writable) {
|
|
@@ -59,31 +122,35 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
59
122
|
|
|
60
123
|
// src/response.ts
|
|
61
124
|
var responseCache = Symbol("responseCache");
|
|
62
|
-
var newGlobalResponseKey = Symbol("newGlobalResponse");
|
|
63
125
|
var cacheKey = Symbol("cache");
|
|
64
126
|
var GlobalResponse = global.Response;
|
|
65
127
|
var Response2 = class _Response {
|
|
66
128
|
#body;
|
|
67
129
|
#init;
|
|
68
|
-
[newGlobalResponseKey]() {
|
|
69
|
-
return new GlobalResponse(
|
|
70
|
-
this.#body,
|
|
71
|
-
this.#init instanceof _Response ? this.#init[newGlobalResponseKey]() : this.#init
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
// @ts-ignore
|
|
75
130
|
get cache() {
|
|
76
131
|
delete this[cacheKey];
|
|
77
|
-
return this[responseCache] ||= this
|
|
132
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
78
133
|
}
|
|
79
134
|
constructor(body, init) {
|
|
80
135
|
this.#body = body;
|
|
81
|
-
|
|
136
|
+
if (init instanceof _Response) {
|
|
137
|
+
const cachedGlobalResponse = init[responseCache];
|
|
138
|
+
if (cachedGlobalResponse) {
|
|
139
|
+
this.#init = cachedGlobalResponse;
|
|
140
|
+
this.cache;
|
|
141
|
+
return;
|
|
142
|
+
} else {
|
|
143
|
+
this.#init = init.#init;
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
this.#init = init;
|
|
147
|
+
}
|
|
82
148
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
83
149
|
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
84
150
|
if (headers instanceof Headers) {
|
|
85
151
|
headers = buildOutgoingHttpHeaders(headers);
|
|
86
152
|
}
|
|
153
|
+
;
|
|
87
154
|
this[cacheKey] = [init?.status || 200, body, headers];
|
|
88
155
|
}
|
|
89
156
|
}
|
|
@@ -120,6 +187,7 @@ Object.defineProperty(global, "Response", {
|
|
|
120
187
|
});
|
|
121
188
|
|
|
122
189
|
// src/globals.ts
|
|
190
|
+
import crypto from "crypto";
|
|
123
191
|
Object.defineProperty(global, "Response", {
|
|
124
192
|
value: Response2
|
|
125
193
|
});
|
|
@@ -137,72 +205,6 @@ global.fetch = (info, init) => {
|
|
|
137
205
|
return webFetch(info, init);
|
|
138
206
|
};
|
|
139
207
|
|
|
140
|
-
// src/request.ts
|
|
141
|
-
import { Readable } from "stream";
|
|
142
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
143
|
-
const headerRecord = [];
|
|
144
|
-
const len = incoming.rawHeaders.length;
|
|
145
|
-
for (let i = 0; i < len; i += 2) {
|
|
146
|
-
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
147
|
-
}
|
|
148
|
-
const init = {
|
|
149
|
-
method,
|
|
150
|
-
headers: headerRecord
|
|
151
|
-
};
|
|
152
|
-
if (!(method === "GET" || method === "HEAD")) {
|
|
153
|
-
init.body = Readable.toWeb(incoming);
|
|
154
|
-
init.duplex = "half";
|
|
155
|
-
}
|
|
156
|
-
return new Request(url, init);
|
|
157
|
-
};
|
|
158
|
-
var getRequestCache = Symbol("getRequestCache");
|
|
159
|
-
var requestCache = Symbol("requestCache");
|
|
160
|
-
var incomingKey = Symbol("incomingKey");
|
|
161
|
-
var requestPrototype = {
|
|
162
|
-
get method() {
|
|
163
|
-
return this[incomingKey].method || "GET";
|
|
164
|
-
},
|
|
165
|
-
get url() {
|
|
166
|
-
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
167
|
-
},
|
|
168
|
-
[getRequestCache]() {
|
|
169
|
-
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
[
|
|
173
|
-
"body",
|
|
174
|
-
"bodyUsed",
|
|
175
|
-
"cache",
|
|
176
|
-
"credentials",
|
|
177
|
-
"destination",
|
|
178
|
-
"headers",
|
|
179
|
-
"integrity",
|
|
180
|
-
"mode",
|
|
181
|
-
"redirect",
|
|
182
|
-
"referrer",
|
|
183
|
-
"referrerPolicy",
|
|
184
|
-
"signal"
|
|
185
|
-
].forEach((k) => {
|
|
186
|
-
Object.defineProperty(requestPrototype, k, {
|
|
187
|
-
get() {
|
|
188
|
-
return this[getRequestCache]()[k];
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
193
|
-
Object.defineProperty(requestPrototype, k, {
|
|
194
|
-
value: function() {
|
|
195
|
-
return this[getRequestCache]()[k]();
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
200
|
-
var newRequest = (incoming) => {
|
|
201
|
-
const req = Object.create(requestPrototype);
|
|
202
|
-
req[incomingKey] = incoming;
|
|
203
|
-
return req;
|
|
204
|
-
};
|
|
205
|
-
|
|
206
208
|
// src/listener.ts
|
|
207
209
|
var regBuffer = /^no$/i;
|
|
208
210
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
@@ -215,6 +217,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
215
217
|
console.info("The user aborted a request.");
|
|
216
218
|
} else {
|
|
217
219
|
console.error(e);
|
|
220
|
+
if (!outgoing.headersSent)
|
|
221
|
+
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
222
|
+
outgoing.end(`Error: ${err.message}`);
|
|
218
223
|
outgoing.destroy(err);
|
|
219
224
|
}
|
|
220
225
|
};
|
|
@@ -235,6 +240,13 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
235
240
|
if (res instanceof Promise) {
|
|
236
241
|
res = await res.catch(handleFetchError);
|
|
237
242
|
}
|
|
243
|
+
if (!(res instanceof Response)) {
|
|
244
|
+
return handleResponseError(
|
|
245
|
+
// @ts-expect-error the object must have `toString()`
|
|
246
|
+
new Error(`The response is not an instance of Response, but ${res.toString()}`),
|
|
247
|
+
outgoing
|
|
248
|
+
);
|
|
249
|
+
}
|
|
238
250
|
if (cacheKey in res) {
|
|
239
251
|
try {
|
|
240
252
|
return responseViaCache(res, outgoing);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/node-server",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
4
|
"description": "Node.js Adapter for Hono",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -43,7 +43,9 @@
|
|
|
43
43
|
"watch": "tsup --watch",
|
|
44
44
|
"postbuild": "publint",
|
|
45
45
|
"prerelease": "yarn build && yarn test",
|
|
46
|
-
"release": "np"
|
|
46
|
+
"release": "np",
|
|
47
|
+
"lint": "eslint --ext js,ts src test",
|
|
48
|
+
"lint:fix": "eslint --ext js,ts src test --fix"
|
|
47
49
|
},
|
|
48
50
|
"license": "MIT",
|
|
49
51
|
"repository": {
|
|
@@ -60,10 +62,12 @@
|
|
|
60
62
|
"node": ">=18.14.1"
|
|
61
63
|
},
|
|
62
64
|
"devDependencies": {
|
|
65
|
+
"@hono/eslint-config": "^0.0.2",
|
|
63
66
|
"@types/jest": "^29.5.3",
|
|
64
67
|
"@types/node": "^20.10.0",
|
|
65
68
|
"@types/supertest": "^2.0.12",
|
|
66
|
-
"
|
|
69
|
+
"eslint": "^8.55.0",
|
|
70
|
+
"hono": "^3.11.7",
|
|
67
71
|
"jest": "^29.6.1",
|
|
68
72
|
"np": "^7.7.0",
|
|
69
73
|
"publint": "^0.1.16",
|