@hono/node-server 1.2.2 → 1.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/dist/globals.js +78 -0
- package/dist/globals.mjs +78 -0
- package/dist/index.js +227 -80
- package/dist/index.mjs +227 -80
- package/dist/listener.d.mts +1 -1
- package/dist/listener.d.ts +1 -1
- package/dist/listener.js +227 -78
- package/dist/listener.mjs +227 -80
- package/dist/request.d.mts +6 -0
- package/dist/request.d.ts +6 -0
- package/dist/request.js +93 -0
- package/dist/request.mjs +68 -0
- package/dist/response.d.mts +15 -0
- package/dist/response.d.ts +15 -0
- package/dist/response.js +106 -0
- package/dist/response.mjs +77 -0
- package/dist/serve-static.d.mts +1 -1
- package/dist/serve-static.d.ts +1 -1
- package/dist/serve-static.js +3 -5
- package/dist/serve-static.mjs +3 -5
- package/dist/server.js +227 -80
- package/dist/server.mjs +227 -80
- package/dist/types.d.mts +9 -9
- package/dist/types.d.ts +9 -9
- package/dist/utils.d.mts +3 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +24 -7
- package/dist/utils.mjs +23 -7
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +227 -80
- package/dist/vercel.mjs +227 -80
- package/package.json +3 -4
package/dist/listener.mjs
CHANGED
|
@@ -1,40 +1,21 @@
|
|
|
1
|
-
// src/listener.ts
|
|
2
|
-
import { Readable } from "stream";
|
|
3
|
-
|
|
4
1
|
// src/globals.ts
|
|
5
2
|
import crypto from "crypto";
|
|
6
|
-
var webFetch = global.fetch;
|
|
7
|
-
if (typeof global.crypto === "undefined") {
|
|
8
|
-
global.crypto = crypto;
|
|
9
|
-
}
|
|
10
|
-
global.fetch = (info, init) => {
|
|
11
|
-
init = {
|
|
12
|
-
// Disable compression handling so people can return the result of a fetch
|
|
13
|
-
// directly in the loader without messing with the Content-Encoding header.
|
|
14
|
-
compress: false,
|
|
15
|
-
...init
|
|
16
|
-
};
|
|
17
|
-
return webFetch(info, init);
|
|
18
|
-
};
|
|
19
3
|
|
|
20
4
|
// src/utils.ts
|
|
21
5
|
function writeFromReadableStream(stream, writable) {
|
|
22
6
|
if (stream.locked) {
|
|
23
7
|
throw new TypeError("ReadableStream is locked.");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (writable.destroyed) {
|
|
27
|
-
reader.cancel();
|
|
8
|
+
} else if (writable.destroyed) {
|
|
9
|
+
stream.cancel();
|
|
28
10
|
return;
|
|
29
11
|
}
|
|
30
|
-
|
|
12
|
+
const reader = stream.getReader();
|
|
31
13
|
writable.on("close", cancel);
|
|
32
14
|
writable.on("error", cancel);
|
|
33
15
|
reader.read().then(flow, cancel);
|
|
34
16
|
return reader.closed.finally(() => {
|
|
35
17
|
writable.off("close", cancel);
|
|
36
18
|
writable.off("error", cancel);
|
|
37
|
-
writable.off("drain", onDrain);
|
|
38
19
|
});
|
|
39
20
|
function cancel(error) {
|
|
40
21
|
reader.cancel(error).catch(() => {
|
|
@@ -49,7 +30,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
49
30
|
try {
|
|
50
31
|
if (done) {
|
|
51
32
|
writable.end();
|
|
52
|
-
} else if (writable.write(value)) {
|
|
33
|
+
} else if (!writable.write(value)) {
|
|
34
|
+
writable.once("drain", onDrain);
|
|
35
|
+
} else {
|
|
53
36
|
return reader.read().then(flow, cancel);
|
|
54
37
|
}
|
|
55
38
|
} catch (e) {
|
|
@@ -57,75 +40,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
57
40
|
}
|
|
58
41
|
}
|
|
59
42
|
}
|
|
43
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
44
|
+
const res = {};
|
|
45
|
+
const cookies = [];
|
|
46
|
+
for (const [k, v] of headers) {
|
|
47
|
+
if (k === "set-cookie") {
|
|
48
|
+
cookies.push(v);
|
|
49
|
+
} else {
|
|
50
|
+
res[k] = v;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (cookies.length > 0) {
|
|
54
|
+
res["set-cookie"] = cookies;
|
|
55
|
+
}
|
|
56
|
+
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
57
|
+
return res;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/response.ts
|
|
61
|
+
var responseCache = Symbol("responseCache");
|
|
62
|
+
var cacheKey = Symbol("cache");
|
|
63
|
+
var globalResponse = global.Response;
|
|
64
|
+
var Response2 = class {
|
|
65
|
+
#body;
|
|
66
|
+
#init;
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
get cache() {
|
|
69
|
+
delete this[cacheKey];
|
|
70
|
+
return this[responseCache] ||= new globalResponse(this.#body, this.#init);
|
|
71
|
+
}
|
|
72
|
+
constructor(body, init) {
|
|
73
|
+
this.#body = body;
|
|
74
|
+
this.#init = init;
|
|
75
|
+
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
76
|
+
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
77
|
+
if (headers instanceof Headers) {
|
|
78
|
+
headers = buildOutgoingHttpHeaders(headers);
|
|
79
|
+
}
|
|
80
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
[
|
|
85
|
+
"body",
|
|
86
|
+
"bodyUsed",
|
|
87
|
+
"headers",
|
|
88
|
+
"ok",
|
|
89
|
+
"redirected",
|
|
90
|
+
"status",
|
|
91
|
+
"statusText",
|
|
92
|
+
"trailers",
|
|
93
|
+
"type",
|
|
94
|
+
"url"
|
|
95
|
+
].forEach((k) => {
|
|
96
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
97
|
+
get() {
|
|
98
|
+
return this.cache[k];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
103
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
104
|
+
value: function() {
|
|
105
|
+
return this.cache[k]();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
Object.setPrototypeOf(Response2, globalResponse);
|
|
110
|
+
Object.setPrototypeOf(Response2.prototype, globalResponse.prototype);
|
|
111
|
+
Object.defineProperty(global, "Response", {
|
|
112
|
+
value: Response2
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// src/globals.ts
|
|
116
|
+
Object.defineProperty(global, "Response", {
|
|
117
|
+
value: Response2
|
|
118
|
+
});
|
|
119
|
+
var webFetch = global.fetch;
|
|
120
|
+
if (typeof global.crypto === "undefined") {
|
|
121
|
+
global.crypto = crypto;
|
|
122
|
+
}
|
|
123
|
+
global.fetch = (info, init) => {
|
|
124
|
+
init = {
|
|
125
|
+
// Disable compression handling so people can return the result of a fetch
|
|
126
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
127
|
+
compress: false,
|
|
128
|
+
...init
|
|
129
|
+
};
|
|
130
|
+
return webFetch(info, init);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// src/request.ts
|
|
134
|
+
import { Readable } from "stream";
|
|
135
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
136
|
+
const headerRecord = [];
|
|
137
|
+
const len = incoming.rawHeaders.length;
|
|
138
|
+
for (let i = 0; i < len; i += 2) {
|
|
139
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
140
|
+
}
|
|
141
|
+
const init = {
|
|
142
|
+
method,
|
|
143
|
+
headers: headerRecord
|
|
144
|
+
};
|
|
145
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
146
|
+
init.body = Readable.toWeb(incoming);
|
|
147
|
+
init.duplex = "half";
|
|
148
|
+
}
|
|
149
|
+
return new Request(url, init);
|
|
150
|
+
};
|
|
151
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
152
|
+
var requestCache = Symbol("requestCache");
|
|
153
|
+
var incomingKey = Symbol("incomingKey");
|
|
154
|
+
var requestPrototype = {
|
|
155
|
+
get method() {
|
|
156
|
+
return this[incomingKey].method || "GET";
|
|
157
|
+
},
|
|
158
|
+
get url() {
|
|
159
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
160
|
+
},
|
|
161
|
+
[getRequestCache]() {
|
|
162
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
[
|
|
166
|
+
"body",
|
|
167
|
+
"bodyUsed",
|
|
168
|
+
"cache",
|
|
169
|
+
"credentials",
|
|
170
|
+
"destination",
|
|
171
|
+
"headers",
|
|
172
|
+
"integrity",
|
|
173
|
+
"mode",
|
|
174
|
+
"redirect",
|
|
175
|
+
"referrer",
|
|
176
|
+
"referrerPolicy",
|
|
177
|
+
"signal"
|
|
178
|
+
].forEach((k) => {
|
|
179
|
+
Object.defineProperty(requestPrototype, k, {
|
|
180
|
+
get() {
|
|
181
|
+
return this[getRequestCache]()[k];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
186
|
+
Object.defineProperty(requestPrototype, k, {
|
|
187
|
+
value: function() {
|
|
188
|
+
return this[getRequestCache]()[k]();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
193
|
+
var newRequest = (incoming) => {
|
|
194
|
+
const req = Object.create(requestPrototype);
|
|
195
|
+
req[incomingKey] = incoming;
|
|
196
|
+
return req;
|
|
197
|
+
};
|
|
60
198
|
|
|
61
199
|
// src/listener.ts
|
|
62
200
|
var regBuffer = /^no$/i;
|
|
63
201
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
202
|
+
var handleFetchError = (e) => new Response(null, {
|
|
203
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
204
|
+
});
|
|
205
|
+
var handleResponseError = (e, outgoing) => {
|
|
206
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
207
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
208
|
+
console.info("The user aborted a request.");
|
|
209
|
+
} else {
|
|
210
|
+
console.error(e);
|
|
211
|
+
outgoing.destroy(err);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
var responseViaCache = (res, outgoing) => {
|
|
215
|
+
const [status, body, header] = res[cacheKey];
|
|
216
|
+
if (typeof body === "string") {
|
|
217
|
+
header["content-length"] ||= "" + Buffer.byteLength(body);
|
|
218
|
+
outgoing.writeHead(status, header);
|
|
219
|
+
outgoing.end(body);
|
|
220
|
+
} else {
|
|
221
|
+
outgoing.writeHead(status, header);
|
|
222
|
+
return writeFromReadableStream(body, outgoing)?.catch(
|
|
223
|
+
(e) => handleResponseError(e, outgoing)
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var responseViaResponseObject = async (res, outgoing) => {
|
|
228
|
+
if (res instanceof Promise) {
|
|
229
|
+
res = await res.catch(handleFetchError);
|
|
230
|
+
}
|
|
231
|
+
if (cacheKey in res) {
|
|
82
232
|
try {
|
|
83
|
-
|
|
233
|
+
return responseViaCache(res, outgoing);
|
|
84
234
|
} catch (e) {
|
|
85
|
-
|
|
86
|
-
if (e instanceof Error) {
|
|
87
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
88
|
-
res = new Response(null, { status: 504 });
|
|
89
|
-
}
|
|
90
|
-
}
|
|
235
|
+
return handleResponseError(e, outgoing);
|
|
91
236
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
237
|
+
}
|
|
238
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
239
|
+
if (res.body) {
|
|
240
|
+
try {
|
|
241
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
242
|
+
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
243
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
244
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
97
245
|
} else {
|
|
98
|
-
|
|
246
|
+
const buffer = await res.arrayBuffer();
|
|
247
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
248
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
249
|
+
outgoing.end(new Uint8Array(buffer));
|
|
99
250
|
}
|
|
251
|
+
} catch (e) {
|
|
252
|
+
handleResponseError(e, outgoing);
|
|
100
253
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else {
|
|
121
|
-
console.error(e);
|
|
122
|
-
outgoing.destroy(err);
|
|
123
|
-
}
|
|
254
|
+
} else {
|
|
255
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
256
|
+
outgoing.end();
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
var getRequestListener = (fetchCallback) => {
|
|
260
|
+
return (incoming, outgoing) => {
|
|
261
|
+
let res;
|
|
262
|
+
const req = newRequest(incoming);
|
|
263
|
+
try {
|
|
264
|
+
res = fetchCallback(req);
|
|
265
|
+
if (cacheKey in res) {
|
|
266
|
+
return responseViaCache(res, outgoing);
|
|
267
|
+
}
|
|
268
|
+
} catch (e) {
|
|
269
|
+
if (!res) {
|
|
270
|
+
res = handleFetchError(e);
|
|
271
|
+
} else {
|
|
272
|
+
return handleResponseError(e, outgoing);
|
|
124
273
|
}
|
|
125
|
-
} else {
|
|
126
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
127
|
-
outgoing.end();
|
|
128
274
|
}
|
|
275
|
+
return responseViaResponseObject(res, outgoing);
|
|
129
276
|
};
|
|
130
277
|
};
|
|
131
278
|
export {
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/request.ts
|
|
21
|
+
var request_exports = {};
|
|
22
|
+
__export(request_exports, {
|
|
23
|
+
newRequest: () => newRequest
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(request_exports);
|
|
26
|
+
var import_node_stream = require("stream");
|
|
27
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
28
|
+
const headerRecord = [];
|
|
29
|
+
const len = incoming.rawHeaders.length;
|
|
30
|
+
for (let i = 0; i < len; i += 2) {
|
|
31
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
32
|
+
}
|
|
33
|
+
const init = {
|
|
34
|
+
method,
|
|
35
|
+
headers: headerRecord
|
|
36
|
+
};
|
|
37
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
38
|
+
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
39
|
+
init.duplex = "half";
|
|
40
|
+
}
|
|
41
|
+
return new Request(url, init);
|
|
42
|
+
};
|
|
43
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
44
|
+
var requestCache = Symbol("requestCache");
|
|
45
|
+
var incomingKey = Symbol("incomingKey");
|
|
46
|
+
var requestPrototype = {
|
|
47
|
+
get method() {
|
|
48
|
+
return this[incomingKey].method || "GET";
|
|
49
|
+
},
|
|
50
|
+
get url() {
|
|
51
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
52
|
+
},
|
|
53
|
+
[getRequestCache]() {
|
|
54
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
[
|
|
58
|
+
"body",
|
|
59
|
+
"bodyUsed",
|
|
60
|
+
"cache",
|
|
61
|
+
"credentials",
|
|
62
|
+
"destination",
|
|
63
|
+
"headers",
|
|
64
|
+
"integrity",
|
|
65
|
+
"mode",
|
|
66
|
+
"redirect",
|
|
67
|
+
"referrer",
|
|
68
|
+
"referrerPolicy",
|
|
69
|
+
"signal"
|
|
70
|
+
].forEach((k) => {
|
|
71
|
+
Object.defineProperty(requestPrototype, k, {
|
|
72
|
+
get() {
|
|
73
|
+
return this[getRequestCache]()[k];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
78
|
+
Object.defineProperty(requestPrototype, k, {
|
|
79
|
+
value: function() {
|
|
80
|
+
return this[getRequestCache]()[k]();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
85
|
+
var newRequest = (incoming) => {
|
|
86
|
+
const req = Object.create(requestPrototype);
|
|
87
|
+
req[incomingKey] = incoming;
|
|
88
|
+
return req;
|
|
89
|
+
};
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
newRequest
|
|
93
|
+
});
|
package/dist/request.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
};
|
|
66
|
+
export {
|
|
67
|
+
newRequest
|
|
68
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const cacheKey: unique symbol;
|
|
2
|
+
declare const globalResponse: {
|
|
3
|
+
new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): globalThis.Response;
|
|
4
|
+
prototype: globalThis.Response;
|
|
5
|
+
error(): globalThis.Response;
|
|
6
|
+
json(data: any, init?: ResponseInit | undefined): globalThis.Response;
|
|
7
|
+
redirect(url: string | URL, status?: number | undefined): globalThis.Response;
|
|
8
|
+
};
|
|
9
|
+
declare class Response {
|
|
10
|
+
#private;
|
|
11
|
+
private get cache();
|
|
12
|
+
constructor(body?: BodyInit | null, init?: ResponseInit);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { Response, cacheKey, globalResponse };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const cacheKey: unique symbol;
|
|
2
|
+
declare const globalResponse: {
|
|
3
|
+
new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): globalThis.Response;
|
|
4
|
+
prototype: globalThis.Response;
|
|
5
|
+
error(): globalThis.Response;
|
|
6
|
+
json(data: any, init?: ResponseInit | undefined): globalThis.Response;
|
|
7
|
+
redirect(url: string | URL, status?: number | undefined): globalThis.Response;
|
|
8
|
+
};
|
|
9
|
+
declare class Response {
|
|
10
|
+
#private;
|
|
11
|
+
private get cache();
|
|
12
|
+
constructor(body?: BodyInit | null, init?: ResponseInit);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { Response, cacheKey, globalResponse };
|
package/dist/response.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/response.ts
|
|
21
|
+
var response_exports = {};
|
|
22
|
+
__export(response_exports, {
|
|
23
|
+
Response: () => Response,
|
|
24
|
+
cacheKey: () => cacheKey,
|
|
25
|
+
globalResponse: () => globalResponse
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(response_exports);
|
|
28
|
+
|
|
29
|
+
// src/utils.ts
|
|
30
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
31
|
+
const res = {};
|
|
32
|
+
const cookies = [];
|
|
33
|
+
for (const [k, v] of headers) {
|
|
34
|
+
if (k === "set-cookie") {
|
|
35
|
+
cookies.push(v);
|
|
36
|
+
} else {
|
|
37
|
+
res[k] = v;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (cookies.length > 0) {
|
|
41
|
+
res["set-cookie"] = cookies;
|
|
42
|
+
}
|
|
43
|
+
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
44
|
+
return res;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/response.ts
|
|
48
|
+
var responseCache = Symbol("responseCache");
|
|
49
|
+
var cacheKey = Symbol("cache");
|
|
50
|
+
var globalResponse = global.Response;
|
|
51
|
+
var Response = class {
|
|
52
|
+
#body;
|
|
53
|
+
#init;
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
get cache() {
|
|
56
|
+
delete this[cacheKey];
|
|
57
|
+
return this[responseCache] ||= new globalResponse(this.#body, this.#init);
|
|
58
|
+
}
|
|
59
|
+
constructor(body, init) {
|
|
60
|
+
this.#body = body;
|
|
61
|
+
this.#init = init;
|
|
62
|
+
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
63
|
+
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
64
|
+
if (headers instanceof Headers) {
|
|
65
|
+
headers = buildOutgoingHttpHeaders(headers);
|
|
66
|
+
}
|
|
67
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
[
|
|
72
|
+
"body",
|
|
73
|
+
"bodyUsed",
|
|
74
|
+
"headers",
|
|
75
|
+
"ok",
|
|
76
|
+
"redirected",
|
|
77
|
+
"status",
|
|
78
|
+
"statusText",
|
|
79
|
+
"trailers",
|
|
80
|
+
"type",
|
|
81
|
+
"url"
|
|
82
|
+
].forEach((k) => {
|
|
83
|
+
Object.defineProperty(Response.prototype, k, {
|
|
84
|
+
get() {
|
|
85
|
+
return this.cache[k];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
90
|
+
Object.defineProperty(Response.prototype, k, {
|
|
91
|
+
value: function() {
|
|
92
|
+
return this.cache[k]();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
Object.setPrototypeOf(Response, globalResponse);
|
|
97
|
+
Object.setPrototypeOf(Response.prototype, globalResponse.prototype);
|
|
98
|
+
Object.defineProperty(global, "Response", {
|
|
99
|
+
value: Response
|
|
100
|
+
});
|
|
101
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
102
|
+
0 && (module.exports = {
|
|
103
|
+
Response,
|
|
104
|
+
cacheKey,
|
|
105
|
+
globalResponse
|
|
106
|
+
});
|