@wooksjs/event-http 0.5.25 → 0.6.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/index.cjs +475 -299
- package/dist/index.d.ts +66 -3
- package/dist/index.mjs +453 -277
- package/package.json +7 -8
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
//#region rolldown:runtime
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -24,6 +23,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
23
|
//#endregion
|
|
25
24
|
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
26
25
|
const buffer = __toESM(require("buffer"));
|
|
26
|
+
const node_stream = __toESM(require("node:stream"));
|
|
27
|
+
const node_util = __toESM(require("node:util"));
|
|
28
|
+
const node_zlib = __toESM(require("node:zlib"));
|
|
27
29
|
const url = __toESM(require("url"));
|
|
28
30
|
const http = __toESM(require("http"));
|
|
29
31
|
const wooks = __toESM(require("wooks"));
|
|
@@ -39,6 +41,10 @@ function createHttpContext(data, options) {
|
|
|
39
41
|
options
|
|
40
42
|
});
|
|
41
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Wrapper on useEventContext with HTTP event types
|
|
46
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
47
|
+
*/
|
|
42
48
|
function useHttpContext() {
|
|
43
49
|
return (0, __wooksjs_event_core.useAsyncEventContext)("HTTP");
|
|
44
50
|
}
|
|
@@ -73,12 +79,12 @@ function convertTime(time, unit = "ms") {
|
|
|
73
79
|
const units = {
|
|
74
80
|
ms: 1,
|
|
75
81
|
s: 1e3,
|
|
76
|
-
m:
|
|
77
|
-
h:
|
|
78
|
-
d:
|
|
79
|
-
w:
|
|
80
|
-
M:
|
|
81
|
-
Y:
|
|
82
|
+
m: 1e3 * 60,
|
|
83
|
+
h: 1e3 * 60 * 60,
|
|
84
|
+
d: 1e3 * 60 * 60 * 24,
|
|
85
|
+
w: 1e3 * 60 * 60 * 24 * 7,
|
|
86
|
+
M: 1e3 * 60 * 60 * 24 * 30,
|
|
87
|
+
Y: 1e3 * 60 * 60 * 24 * 365
|
|
82
88
|
};
|
|
83
89
|
|
|
84
90
|
//#endregion
|
|
@@ -104,35 +110,426 @@ const cookieAttrFunc = {
|
|
|
104
110
|
sameSite: (v) => v ? `SameSite=${typeof v === "string" ? v : "Strict"}` : ""
|
|
105
111
|
};
|
|
106
112
|
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region packages/event-http/src/compressor/body-compressor.ts
|
|
115
|
+
const compressors = { identity: {
|
|
116
|
+
compress: (v) => v,
|
|
117
|
+
uncompress: (v) => v,
|
|
118
|
+
stream: {
|
|
119
|
+
compress: (data) => data,
|
|
120
|
+
uncompress: (data) => data
|
|
121
|
+
}
|
|
122
|
+
} };
|
|
123
|
+
function encodingSupportsStream(encodings) {
|
|
124
|
+
return encodings.every((enc) => compressors[enc]?.stream);
|
|
125
|
+
}
|
|
126
|
+
async function uncompressBody(encodings, compressed) {
|
|
127
|
+
let buf = compressed;
|
|
128
|
+
for (const enc of encodings.slice().reverse()) {
|
|
129
|
+
const c = compressors[enc];
|
|
130
|
+
if (!c) throw new Error(`Unsupported compression type "${enc}".`);
|
|
131
|
+
buf = await c.uncompress(buf);
|
|
132
|
+
}
|
|
133
|
+
return buf;
|
|
134
|
+
}
|
|
135
|
+
async function uncompressBodyStream(encodings, src) {
|
|
136
|
+
if (!encodingSupportsStream(encodings)) throw new Error("Some encodings lack a streaming decompressor");
|
|
137
|
+
let out = src;
|
|
138
|
+
for (const enc of Array.from(encodings).reverse()) out = await compressors[enc].stream.uncompress(out);
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region packages/event-http/src/compressor/zlib-compressors.ts
|
|
144
|
+
const pipeline = node_stream.pipeline;
|
|
145
|
+
function iterableToReadable(src) {
|
|
146
|
+
return node_stream.Readable.from(src, { objectMode: false });
|
|
147
|
+
}
|
|
148
|
+
function pump(src, transform) {
|
|
149
|
+
pipeline(iterableToReadable(src), transform, (err) => {
|
|
150
|
+
if (err) transform.destroy(err);
|
|
151
|
+
});
|
|
152
|
+
return transform;
|
|
153
|
+
}
|
|
154
|
+
function addStreamCodec(name, createDeflater, createInflater) {
|
|
155
|
+
const c = compressors[name] ?? (compressors[name] = {
|
|
156
|
+
compress: (v) => v,
|
|
157
|
+
uncompress: (v) => v
|
|
158
|
+
});
|
|
159
|
+
c.stream = {
|
|
160
|
+
compress: async (src) => pump(src, createDeflater()),
|
|
161
|
+
uncompress: async (src) => pump(src, createInflater())
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
addStreamCodec("gzip", node_zlib.createGzip, node_zlib.createGunzip);
|
|
165
|
+
addStreamCodec("deflate", node_zlib.createDeflate, node_zlib.createInflate);
|
|
166
|
+
addStreamCodec("br", node_zlib.createBrotliCompress, node_zlib.createBrotliDecompress);
|
|
167
|
+
let zp;
|
|
168
|
+
async function zlib() {
|
|
169
|
+
if (!zp) {
|
|
170
|
+
const { gzip, gunzip, deflate, inflate, brotliCompress, brotliDecompress } = await import("node:zlib");
|
|
171
|
+
zp = {
|
|
172
|
+
gzip: (0, node_util.promisify)(gzip),
|
|
173
|
+
gunzip: (0, node_util.promisify)(gunzip),
|
|
174
|
+
deflate: (0, node_util.promisify)(deflate),
|
|
175
|
+
inflate: (0, node_util.promisify)(inflate),
|
|
176
|
+
brotliCompress: (0, node_util.promisify)(brotliCompress),
|
|
177
|
+
brotliDecompress: (0, node_util.promisify)(brotliDecompress)
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return zp;
|
|
181
|
+
}
|
|
182
|
+
compressors.gzip.compress = async (b) => (await zlib()).gzip(b);
|
|
183
|
+
compressors.gzip.uncompress = async (b) => (await zlib()).gunzip(b);
|
|
184
|
+
compressors.deflate.compress = async (b) => (await zlib()).deflate(b);
|
|
185
|
+
compressors.deflate.uncompress = async (b) => (await zlib()).inflate(b);
|
|
186
|
+
compressors.br.compress = async (b) => (await zlib()).brotliCompress(b);
|
|
187
|
+
compressors.br.uncompress = async (b) => (await zlib()).brotliDecompress(b);
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region packages/event-http/src/response/renderer.ts
|
|
191
|
+
var BaseHttpResponseRenderer = class {
|
|
192
|
+
render(response) {
|
|
193
|
+
if (typeof response.body === "string" || typeof response.body === "boolean" || typeof response.body === "number") {
|
|
194
|
+
if (!response.getContentType()) response.setContentType("text/plain");
|
|
195
|
+
return response.body.toString();
|
|
196
|
+
}
|
|
197
|
+
if (response.body === void 0) return "";
|
|
198
|
+
if (response.body instanceof Uint8Array) return response.body;
|
|
199
|
+
if (typeof response.body === "object") {
|
|
200
|
+
if (!response.getContentType()) response.setContentType("application/json");
|
|
201
|
+
return JSON.stringify(response.body);
|
|
202
|
+
}
|
|
203
|
+
throw new Error(`Unsupported body format "${typeof response.body}"`);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region packages/event-http/src/utils/status-codes.ts
|
|
209
|
+
const httpStatusCodes = {
|
|
210
|
+
100: "Continue",
|
|
211
|
+
101: "Switching protocols",
|
|
212
|
+
102: "Processing",
|
|
213
|
+
103: "Early Hints",
|
|
214
|
+
200: "OK",
|
|
215
|
+
201: "Created",
|
|
216
|
+
202: "Accepted",
|
|
217
|
+
203: "Non-Authoritative Information",
|
|
218
|
+
204: "No Content",
|
|
219
|
+
205: "Reset Content",
|
|
220
|
+
206: "Partial Content",
|
|
221
|
+
207: "Multi-Status",
|
|
222
|
+
208: "Already Reported",
|
|
223
|
+
226: "IM Used",
|
|
224
|
+
300: "Multiple Choices",
|
|
225
|
+
301: "Moved Permanently",
|
|
226
|
+
302: "Found (Previously \"Moved Temporarily\")",
|
|
227
|
+
303: "See Other",
|
|
228
|
+
304: "Not Modified",
|
|
229
|
+
305: "Use Proxy",
|
|
230
|
+
306: "Switch Proxy",
|
|
231
|
+
307: "Temporary Redirect",
|
|
232
|
+
308: "Permanent Redirect",
|
|
233
|
+
400: "Bad Request",
|
|
234
|
+
401: "Unauthorized",
|
|
235
|
+
402: "Payment Required",
|
|
236
|
+
403: "Forbidden",
|
|
237
|
+
404: "Not Found",
|
|
238
|
+
405: "Method Not Allowed",
|
|
239
|
+
406: "Not Acceptable",
|
|
240
|
+
407: "Proxy Authentication Required",
|
|
241
|
+
408: "Request Timeout",
|
|
242
|
+
409: "Conflict",
|
|
243
|
+
410: "Gone",
|
|
244
|
+
411: "Length Required",
|
|
245
|
+
412: "Precondition Failed",
|
|
246
|
+
413: "Payload Too Large",
|
|
247
|
+
414: "URI Too Long",
|
|
248
|
+
415: "Unsupported Media Type",
|
|
249
|
+
416: "Range Not Satisfiable",
|
|
250
|
+
417: "Expectation Failed",
|
|
251
|
+
418: "I'm a Teapot",
|
|
252
|
+
421: "Misdirected Request",
|
|
253
|
+
422: "Unprocessable Entity",
|
|
254
|
+
423: "Locked",
|
|
255
|
+
424: "Failed Dependency",
|
|
256
|
+
425: "Too Early",
|
|
257
|
+
426: "Upgrade Required",
|
|
258
|
+
428: "Precondition Required",
|
|
259
|
+
429: "Too Many Requests",
|
|
260
|
+
431: "Request Header Fields Too Large",
|
|
261
|
+
451: "Unavailable For Legal Reasons",
|
|
262
|
+
500: "Internal Server Error",
|
|
263
|
+
501: "Not Implemented",
|
|
264
|
+
502: "Bad Gateway",
|
|
265
|
+
503: "Service Unavailable",
|
|
266
|
+
504: "Gateway Timeout",
|
|
267
|
+
505: "HTTP Version Not Supported",
|
|
268
|
+
506: "Variant Also Negotiates",
|
|
269
|
+
507: "Insufficient Storage",
|
|
270
|
+
508: "Loop Detected",
|
|
271
|
+
510: "Not Extended",
|
|
272
|
+
511: "Network Authentication Required"
|
|
273
|
+
};
|
|
274
|
+
let EHttpStatusCode = /* @__PURE__ */ function(EHttpStatusCode$1) {
|
|
275
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Continue"] = 100] = "Continue";
|
|
276
|
+
EHttpStatusCode$1[EHttpStatusCode$1["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
277
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Processing"] = 102] = "Processing";
|
|
278
|
+
EHttpStatusCode$1[EHttpStatusCode$1["EarlyHints"] = 103] = "EarlyHints";
|
|
279
|
+
EHttpStatusCode$1[EHttpStatusCode$1["OK"] = 200] = "OK";
|
|
280
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Created"] = 201] = "Created";
|
|
281
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Accepted"] = 202] = "Accepted";
|
|
282
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
|
|
283
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NoContent"] = 204] = "NoContent";
|
|
284
|
+
EHttpStatusCode$1[EHttpStatusCode$1["ResetContent"] = 205] = "ResetContent";
|
|
285
|
+
EHttpStatusCode$1[EHttpStatusCode$1["PartialContent"] = 206] = "PartialContent";
|
|
286
|
+
EHttpStatusCode$1[EHttpStatusCode$1["MultiStatus"] = 207] = "MultiStatus";
|
|
287
|
+
EHttpStatusCode$1[EHttpStatusCode$1["AlreadyReported"] = 208] = "AlreadyReported";
|
|
288
|
+
EHttpStatusCode$1[EHttpStatusCode$1["IMUsed"] = 226] = "IMUsed";
|
|
289
|
+
EHttpStatusCode$1[EHttpStatusCode$1["MultipleChoices"] = 300] = "MultipleChoices";
|
|
290
|
+
EHttpStatusCode$1[EHttpStatusCode$1["MovedPermanently"] = 301] = "MovedPermanently";
|
|
291
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Found"] = 302] = "Found";
|
|
292
|
+
EHttpStatusCode$1[EHttpStatusCode$1["SeeOther"] = 303] = "SeeOther";
|
|
293
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NotModified"] = 304] = "NotModified";
|
|
294
|
+
EHttpStatusCode$1[EHttpStatusCode$1["UseProxy"] = 305] = "UseProxy";
|
|
295
|
+
EHttpStatusCode$1[EHttpStatusCode$1["SwitchProxy"] = 306] = "SwitchProxy";
|
|
296
|
+
EHttpStatusCode$1[EHttpStatusCode$1["TemporaryRedirect"] = 307] = "TemporaryRedirect";
|
|
297
|
+
EHttpStatusCode$1[EHttpStatusCode$1["PermanentRedirect"] = 308] = "PermanentRedirect";
|
|
298
|
+
EHttpStatusCode$1[EHttpStatusCode$1["BadRequest"] = 400] = "BadRequest";
|
|
299
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Unauthorized"] = 401] = "Unauthorized";
|
|
300
|
+
EHttpStatusCode$1[EHttpStatusCode$1["PaymentRequired"] = 402] = "PaymentRequired";
|
|
301
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Forbidden"] = 403] = "Forbidden";
|
|
302
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NotFound"] = 404] = "NotFound";
|
|
303
|
+
EHttpStatusCode$1[EHttpStatusCode$1["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
304
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NotAcceptable"] = 406] = "NotAcceptable";
|
|
305
|
+
EHttpStatusCode$1[EHttpStatusCode$1["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
|
|
306
|
+
EHttpStatusCode$1[EHttpStatusCode$1["RequestTimeout"] = 408] = "RequestTimeout";
|
|
307
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Conflict"] = 409] = "Conflict";
|
|
308
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Gone"] = 410] = "Gone";
|
|
309
|
+
EHttpStatusCode$1[EHttpStatusCode$1["LengthRequired"] = 411] = "LengthRequired";
|
|
310
|
+
EHttpStatusCode$1[EHttpStatusCode$1["PreconditionFailed"] = 412] = "PreconditionFailed";
|
|
311
|
+
EHttpStatusCode$1[EHttpStatusCode$1["PayloadTooLarge"] = 413] = "PayloadTooLarge";
|
|
312
|
+
EHttpStatusCode$1[EHttpStatusCode$1["URITooLong"] = 414] = "URITooLong";
|
|
313
|
+
EHttpStatusCode$1[EHttpStatusCode$1["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
|
|
314
|
+
EHttpStatusCode$1[EHttpStatusCode$1["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
|
|
315
|
+
EHttpStatusCode$1[EHttpStatusCode$1["ExpectationFailed"] = 417] = "ExpectationFailed";
|
|
316
|
+
EHttpStatusCode$1[EHttpStatusCode$1["ImATeapot"] = 418] = "ImATeapot";
|
|
317
|
+
EHttpStatusCode$1[EHttpStatusCode$1["MisdirectedRequest"] = 421] = "MisdirectedRequest";
|
|
318
|
+
EHttpStatusCode$1[EHttpStatusCode$1["UnprocessableEntity"] = 422] = "UnprocessableEntity";
|
|
319
|
+
EHttpStatusCode$1[EHttpStatusCode$1["Locked"] = 423] = "Locked";
|
|
320
|
+
EHttpStatusCode$1[EHttpStatusCode$1["FailedDependency"] = 424] = "FailedDependency";
|
|
321
|
+
EHttpStatusCode$1[EHttpStatusCode$1["TooEarly"] = 425] = "TooEarly";
|
|
322
|
+
EHttpStatusCode$1[EHttpStatusCode$1["UpgradeRequired"] = 426] = "UpgradeRequired";
|
|
323
|
+
EHttpStatusCode$1[EHttpStatusCode$1["PreconditionRequired"] = 428] = "PreconditionRequired";
|
|
324
|
+
EHttpStatusCode$1[EHttpStatusCode$1["TooManyRequests"] = 429] = "TooManyRequests";
|
|
325
|
+
EHttpStatusCode$1[EHttpStatusCode$1["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
|
|
326
|
+
EHttpStatusCode$1[EHttpStatusCode$1["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
|
|
327
|
+
EHttpStatusCode$1[EHttpStatusCode$1["InternalServerError"] = 500] = "InternalServerError";
|
|
328
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NotImplemented"] = 501] = "NotImplemented";
|
|
329
|
+
EHttpStatusCode$1[EHttpStatusCode$1["BadGateway"] = 502] = "BadGateway";
|
|
330
|
+
EHttpStatusCode$1[EHttpStatusCode$1["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
331
|
+
EHttpStatusCode$1[EHttpStatusCode$1["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
332
|
+
EHttpStatusCode$1[EHttpStatusCode$1["HTTPVersionNotSupported"] = 505] = "HTTPVersionNotSupported";
|
|
333
|
+
EHttpStatusCode$1[EHttpStatusCode$1["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
|
|
334
|
+
EHttpStatusCode$1[EHttpStatusCode$1["InsufficientStorage"] = 507] = "InsufficientStorage";
|
|
335
|
+
EHttpStatusCode$1[EHttpStatusCode$1["LoopDetected"] = 508] = "LoopDetected";
|
|
336
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NotExtended"] = 510] = "NotExtended";
|
|
337
|
+
EHttpStatusCode$1[EHttpStatusCode$1["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
|
|
338
|
+
return EHttpStatusCode$1;
|
|
339
|
+
}({});
|
|
340
|
+
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region packages/event-http/src/errors/error-renderer.ts
|
|
343
|
+
const preStyles = "font-family: monospace;width: 100%;max-width: 900px;padding: 10px;margin: 20px auto;border-radius: 8px;background-color: #494949;box-shadow: 0px 0px 3px 2px rgb(255 255 255 / 20%);";
|
|
344
|
+
var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
345
|
+
renderHtml(response) {
|
|
346
|
+
const data = response.body || {};
|
|
347
|
+
response.setContentType("text/html");
|
|
348
|
+
const keys = Object.keys(data).filter((key) => ![
|
|
349
|
+
"statusCode",
|
|
350
|
+
"error",
|
|
351
|
+
"message"
|
|
352
|
+
].includes(key));
|
|
353
|
+
return `<html style="background-color: #333; color: #bbb;"><head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head><body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center><center><h4>${data.message}</h1></center><hr color="#666"><center style="color: #666;"> Wooks v0.6.0 </center>${keys.length > 0 ? `<pre style="${preStyles}">${JSON.stringify({
|
|
354
|
+
...data,
|
|
355
|
+
statusCode: void 0,
|
|
356
|
+
message: void 0,
|
|
357
|
+
error: void 0
|
|
358
|
+
}, null, " ")}</pre>` : ""}</body></html>`;
|
|
359
|
+
}
|
|
360
|
+
renderText(response) {
|
|
361
|
+
const data = response.body || {};
|
|
362
|
+
response.setContentType("text/plain");
|
|
363
|
+
const keys = Object.keys(data).filter((key) => ![
|
|
364
|
+
"statusCode",
|
|
365
|
+
"error",
|
|
366
|
+
"message"
|
|
367
|
+
].includes(key));
|
|
368
|
+
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}\n\n${keys.length > 0 ? `${JSON.stringify({
|
|
369
|
+
...data,
|
|
370
|
+
statusCode: void 0,
|
|
371
|
+
message: void 0,
|
|
372
|
+
error: void 0
|
|
373
|
+
}, null, " ")}` : ""}`;
|
|
374
|
+
}
|
|
375
|
+
renderJson(response) {
|
|
376
|
+
const data = response.body || {};
|
|
377
|
+
response.setContentType("application/json");
|
|
378
|
+
const keys = Object.keys(data).filter((key) => ![
|
|
379
|
+
"statusCode",
|
|
380
|
+
"error",
|
|
381
|
+
"message"
|
|
382
|
+
].includes(key));
|
|
383
|
+
return `{"statusCode":${escapeQuotes(data.statusCode)},"error":"${escapeQuotes(data.error)}","message":"${escapeQuotes(data.message)}"${keys.length > 0 ? `,${keys.map((k) => `"${escapeQuotes(k)}":${JSON.stringify(data[k])}`).join(",")}` : ""}}`;
|
|
384
|
+
}
|
|
385
|
+
render(response) {
|
|
386
|
+
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
387
|
+
response.status = response.body?.statusCode || 500;
|
|
388
|
+
if (acceptsJson()) return this.renderJson(response);
|
|
389
|
+
else if (acceptsHtml()) return this.renderHtml(response);
|
|
390
|
+
else if (acceptsText()) return this.renderText(response);
|
|
391
|
+
else return this.renderJson(response);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
function escapeQuotes(s) {
|
|
395
|
+
return (typeof s === "number" ? s : s || "").toString().replace(/"/gu, "\\\"");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region packages/event-http/src/errors/http-error.ts
|
|
400
|
+
var HttpError = class extends Error {
|
|
401
|
+
name = "HttpError";
|
|
402
|
+
constructor(code = 500, _body = "") {
|
|
403
|
+
super(typeof _body === "string" ? _body : _body.message);
|
|
404
|
+
this.code = code;
|
|
405
|
+
this._body = _body;
|
|
406
|
+
}
|
|
407
|
+
get body() {
|
|
408
|
+
return typeof this._body === "string" ? {
|
|
409
|
+
statusCode: this.code,
|
|
410
|
+
message: this.message,
|
|
411
|
+
error: httpStatusCodes[this.code]
|
|
412
|
+
} : {
|
|
413
|
+
...this._body,
|
|
414
|
+
statusCode: this.code,
|
|
415
|
+
message: this.message,
|
|
416
|
+
error: httpStatusCodes[this.code]
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
renderer;
|
|
420
|
+
attachRenderer(renderer) {
|
|
421
|
+
this.renderer = renderer;
|
|
422
|
+
}
|
|
423
|
+
getRenderer() {
|
|
424
|
+
return this.renderer;
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
|
|
107
428
|
//#endregion
|
|
108
429
|
//#region packages/event-http/src/composables/request.ts
|
|
109
430
|
const xForwardedFor = "x-forwarded-for";
|
|
431
|
+
const DEFAULT_LIMITS = {
|
|
432
|
+
maxCompressed: 1 * 1024 * 1024,
|
|
433
|
+
maxInflated: 10 * 1024 * 1024,
|
|
434
|
+
maxRatio: 100,
|
|
435
|
+
readTimeoutMs: 1e4
|
|
436
|
+
};
|
|
110
437
|
function useRequest() {
|
|
111
438
|
const { store } = useHttpContext();
|
|
112
|
-
const { init } = store("request");
|
|
439
|
+
const { init, get, set } = store("request");
|
|
113
440
|
const event = store("event");
|
|
114
441
|
const req = event.get("req");
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
442
|
+
const contentEncoding = req.headers["content-encoding"];
|
|
443
|
+
const contentEncodings = () => init("contentEncodings", () => (contentEncoding || "").split(",").map((p) => p.trim()).filter((p) => !!p));
|
|
444
|
+
const isCompressed = () => init("isCompressed", () => {
|
|
445
|
+
const parts = contentEncodings();
|
|
446
|
+
for (const p of parts) if ([
|
|
447
|
+
"deflate",
|
|
448
|
+
"gzip",
|
|
449
|
+
"br"
|
|
450
|
+
].includes(p)) return true;
|
|
451
|
+
return false;
|
|
452
|
+
});
|
|
453
|
+
const getMaxCompressed = () => get("maxCompressed") ?? DEFAULT_LIMITS.maxCompressed;
|
|
454
|
+
const setMaxCompressed = (limit) => set("maxCompressed", limit);
|
|
455
|
+
const getReadTimeoutMs = () => get("readTimeoutMs") ?? DEFAULT_LIMITS.readTimeoutMs;
|
|
456
|
+
const setReadTimeoutMs = (limit) => set("readTimeoutMs", limit);
|
|
457
|
+
const getMaxInflated = () => get("maxInflated") ?? DEFAULT_LIMITS.maxInflated;
|
|
458
|
+
const setMaxInflated = (limit) => set("maxInflated", limit);
|
|
459
|
+
const rawBody = () => init("rawBody", async () => {
|
|
460
|
+
const encs = contentEncodings();
|
|
461
|
+
const isZip = isCompressed();
|
|
462
|
+
const streamable = isZip && encodingSupportsStream(encs);
|
|
463
|
+
const maxCompressed = getMaxCompressed();
|
|
464
|
+
const maxInflated = getMaxInflated();
|
|
465
|
+
const timeoutMs = getReadTimeoutMs();
|
|
466
|
+
const cl = Number(req.headers["content-length"] ?? 0);
|
|
467
|
+
const upfrontLimit = isZip ? maxCompressed : maxInflated;
|
|
468
|
+
if (cl && cl > upfrontLimit) throw new HttpError(413, "Payload Too Large");
|
|
469
|
+
for (const enc of encs) if (!compressors[enc]) throw new HttpError(415, `Unsupported Content-Encoding "${enc}"`);
|
|
470
|
+
let timer = null;
|
|
471
|
+
function resetTimer() {
|
|
472
|
+
if (timeoutMs === 0) return;
|
|
473
|
+
clearTimer();
|
|
474
|
+
timer = setTimeout(() => {
|
|
475
|
+
clearTimer();
|
|
476
|
+
req.destroy();
|
|
477
|
+
}, timeoutMs);
|
|
478
|
+
}
|
|
479
|
+
function clearTimer() {
|
|
480
|
+
if (timer) {
|
|
481
|
+
clearTimeout(timer);
|
|
482
|
+
timer = null;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
let rawBytes = 0;
|
|
486
|
+
async function* limitedCompressed() {
|
|
487
|
+
resetTimer();
|
|
488
|
+
try {
|
|
489
|
+
for await (const chunk of req) {
|
|
490
|
+
rawBytes += chunk.length;
|
|
491
|
+
if (rawBytes > upfrontLimit) {
|
|
492
|
+
req.destroy();
|
|
493
|
+
throw new HttpError(413, "Payload Too Large");
|
|
494
|
+
}
|
|
495
|
+
resetTimer();
|
|
496
|
+
yield chunk;
|
|
497
|
+
}
|
|
498
|
+
} finally {
|
|
499
|
+
clearTimer();
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
let stream$1 = limitedCompressed();
|
|
503
|
+
if (streamable) stream$1 = await uncompressBodyStream(encs, stream$1);
|
|
504
|
+
const chunks = [];
|
|
505
|
+
let inflatedBytes = 0;
|
|
506
|
+
try {
|
|
507
|
+
for await (const chunk of stream$1) {
|
|
508
|
+
inflatedBytes += chunk.length;
|
|
509
|
+
if (inflatedBytes > maxInflated) throw new HttpError(413, "Inflated body too large");
|
|
510
|
+
chunks.push(chunk);
|
|
511
|
+
}
|
|
512
|
+
} catch (error) {
|
|
513
|
+
if (error instanceof HttpError) throw error;
|
|
514
|
+
throw new HttpError(408, "Request body timeout");
|
|
515
|
+
}
|
|
516
|
+
let body = buffer.Buffer.concat(chunks);
|
|
517
|
+
if (!streamable && isZip) {
|
|
518
|
+
body = await uncompressBody(encs, body);
|
|
519
|
+
inflatedBytes = body.byteLength;
|
|
520
|
+
if (inflatedBytes > maxInflated) throw new HttpError(413, "Inflated body too large");
|
|
521
|
+
}
|
|
522
|
+
return body;
|
|
523
|
+
});
|
|
127
524
|
const reqId = (0, __wooksjs_event_core.useEventId)().getId;
|
|
128
525
|
const forwardedIp = () => init("forwardedIp", () => {
|
|
129
526
|
if (typeof req.headers[xForwardedFor] === "string" && req.headers[xForwardedFor]) return req.headers[xForwardedFor].split(",").shift()?.trim();
|
|
130
|
-
else return "";
|
|
527
|
+
else return "";
|
|
131
528
|
});
|
|
132
529
|
const remoteIp = () => init("remoteIp", () => req.socket.remoteAddress || req.connection.remoteAddress || "");
|
|
133
530
|
function getIp(options) {
|
|
134
531
|
if (options?.trustProxy) return forwardedIp() || getIp();
|
|
135
|
-
else return remoteIp();
|
|
532
|
+
else return remoteIp();
|
|
136
533
|
}
|
|
137
534
|
const getIpList = () => init("ipList", () => ({
|
|
138
535
|
remoteIp: req.socket.remoteAddress || req.connection.remoteAddress || "",
|
|
@@ -146,7 +543,14 @@ else return remoteIp();
|
|
|
146
543
|
rawBody,
|
|
147
544
|
reqId,
|
|
148
545
|
getIp,
|
|
149
|
-
getIpList
|
|
546
|
+
getIpList,
|
|
547
|
+
isCompressed,
|
|
548
|
+
getMaxCompressed,
|
|
549
|
+
setMaxCompressed,
|
|
550
|
+
getReadTimeoutMs,
|
|
551
|
+
setReadTimeoutMs,
|
|
552
|
+
getMaxInflated,
|
|
553
|
+
setMaxInflated
|
|
150
554
|
};
|
|
151
555
|
}
|
|
152
556
|
|
|
@@ -306,7 +710,7 @@ function useAuthorization() {
|
|
|
306
710
|
function renderCacheControl(data) {
|
|
307
711
|
let attrs = "";
|
|
308
712
|
for (const [a, v] of Object.entries(data)) {
|
|
309
|
-
if (v ===
|
|
713
|
+
if (v === void 0) continue;
|
|
310
714
|
const func = cacheControlFunc[a];
|
|
311
715
|
if (typeof func === "function") {
|
|
312
716
|
const val = func(v);
|
|
@@ -387,11 +791,15 @@ function useStatus() {
|
|
|
387
791
|
//#region packages/event-http/src/utils/url-search-params.ts
|
|
388
792
|
var WooksURLSearchParams = class extends url.URLSearchParams {
|
|
389
793
|
toJson() {
|
|
390
|
-
const json =
|
|
794
|
+
const json = Object.create(null);
|
|
391
795
|
for (const [key, value] of this.entries()) if (isArrayParam(key)) {
|
|
392
796
|
const a = json[key] = json[key] || [];
|
|
393
797
|
a.push(value);
|
|
394
|
-
} else
|
|
798
|
+
} else {
|
|
799
|
+
if (key === "__proto__") throw new HttpError(400, `Illegal key name "${key}"`);
|
|
800
|
+
if (key in json) throw new HttpError(400, `Duplicate key "${key}"`);
|
|
801
|
+
json[key] = value;
|
|
802
|
+
}
|
|
395
803
|
return json;
|
|
396
804
|
}
|
|
397
805
|
};
|
|
@@ -417,245 +825,6 @@ function useSearchParams() {
|
|
|
417
825
|
};
|
|
418
826
|
}
|
|
419
827
|
|
|
420
|
-
//#endregion
|
|
421
|
-
//#region packages/event-http/src/response/renderer.ts
|
|
422
|
-
var BaseHttpResponseRenderer = class {
|
|
423
|
-
render(response) {
|
|
424
|
-
if (typeof response.body === "string" || typeof response.body === "boolean" || typeof response.body === "number") {
|
|
425
|
-
if (!response.getContentType()) response.setContentType("text/plain");
|
|
426
|
-
return response.body.toString();
|
|
427
|
-
}
|
|
428
|
-
if (response.body === undefined) return "";
|
|
429
|
-
if (response.body instanceof Uint8Array) return response.body;
|
|
430
|
-
if (typeof response.body === "object") {
|
|
431
|
-
if (!response.getContentType()) response.setContentType("application/json");
|
|
432
|
-
return JSON.stringify(response.body);
|
|
433
|
-
}
|
|
434
|
-
throw new Error(`Unsupported body format "${typeof response.body}"`);
|
|
435
|
-
}
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
//#endregion
|
|
439
|
-
//#region packages/event-http/src/utils/status-codes.ts
|
|
440
|
-
const httpStatusCodes = {
|
|
441
|
-
100: "Continue",
|
|
442
|
-
101: "Switching protocols",
|
|
443
|
-
102: "Processing",
|
|
444
|
-
103: "Early Hints",
|
|
445
|
-
200: "OK",
|
|
446
|
-
201: "Created",
|
|
447
|
-
202: "Accepted",
|
|
448
|
-
203: "Non-Authoritative Information",
|
|
449
|
-
204: "No Content",
|
|
450
|
-
205: "Reset Content",
|
|
451
|
-
206: "Partial Content",
|
|
452
|
-
207: "Multi-Status",
|
|
453
|
-
208: "Already Reported",
|
|
454
|
-
226: "IM Used",
|
|
455
|
-
300: "Multiple Choices",
|
|
456
|
-
301: "Moved Permanently",
|
|
457
|
-
302: "Found (Previously \"Moved Temporarily\")",
|
|
458
|
-
303: "See Other",
|
|
459
|
-
304: "Not Modified",
|
|
460
|
-
305: "Use Proxy",
|
|
461
|
-
306: "Switch Proxy",
|
|
462
|
-
307: "Temporary Redirect",
|
|
463
|
-
308: "Permanent Redirect",
|
|
464
|
-
400: "Bad Request",
|
|
465
|
-
401: "Unauthorized",
|
|
466
|
-
402: "Payment Required",
|
|
467
|
-
403: "Forbidden",
|
|
468
|
-
404: "Not Found",
|
|
469
|
-
405: "Method Not Allowed",
|
|
470
|
-
406: "Not Acceptable",
|
|
471
|
-
407: "Proxy Authentication Required",
|
|
472
|
-
408: "Request Timeout",
|
|
473
|
-
409: "Conflict",
|
|
474
|
-
410: "Gone",
|
|
475
|
-
411: "Length Required",
|
|
476
|
-
412: "Precondition Failed",
|
|
477
|
-
413: "Payload Too Large",
|
|
478
|
-
414: "URI Too Long",
|
|
479
|
-
415: "Unsupported Media Type",
|
|
480
|
-
416: "Range Not Satisfiable",
|
|
481
|
-
417: "Expectation Failed",
|
|
482
|
-
418: "I'm a Teapot",
|
|
483
|
-
421: "Misdirected Request",
|
|
484
|
-
422: "Unprocessable Entity",
|
|
485
|
-
423: "Locked",
|
|
486
|
-
424: "Failed Dependency",
|
|
487
|
-
425: "Too Early",
|
|
488
|
-
426: "Upgrade Required",
|
|
489
|
-
428: "Precondition Required",
|
|
490
|
-
429: "Too Many Requests",
|
|
491
|
-
431: "Request Header Fields Too Large",
|
|
492
|
-
451: "Unavailable For Legal Reasons",
|
|
493
|
-
500: "Internal Server Error",
|
|
494
|
-
501: "Not Implemented",
|
|
495
|
-
502: "Bad Gateway",
|
|
496
|
-
503: "Service Unavailable",
|
|
497
|
-
504: "Gateway Timeout",
|
|
498
|
-
505: "HTTP Version Not Supported",
|
|
499
|
-
506: "Variant Also Negotiates",
|
|
500
|
-
507: "Insufficient Storage",
|
|
501
|
-
508: "Loop Detected",
|
|
502
|
-
510: "Not Extended",
|
|
503
|
-
511: "Network Authentication Required"
|
|
504
|
-
};
|
|
505
|
-
let EHttpStatusCode = function(EHttpStatusCode$1) {
|
|
506
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Continue"] = 100] = "Continue";
|
|
507
|
-
EHttpStatusCode$1[EHttpStatusCode$1["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
508
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Processing"] = 102] = "Processing";
|
|
509
|
-
EHttpStatusCode$1[EHttpStatusCode$1["EarlyHints"] = 103] = "EarlyHints";
|
|
510
|
-
EHttpStatusCode$1[EHttpStatusCode$1["OK"] = 200] = "OK";
|
|
511
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Created"] = 201] = "Created";
|
|
512
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Accepted"] = 202] = "Accepted";
|
|
513
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
|
|
514
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NoContent"] = 204] = "NoContent";
|
|
515
|
-
EHttpStatusCode$1[EHttpStatusCode$1["ResetContent"] = 205] = "ResetContent";
|
|
516
|
-
EHttpStatusCode$1[EHttpStatusCode$1["PartialContent"] = 206] = "PartialContent";
|
|
517
|
-
EHttpStatusCode$1[EHttpStatusCode$1["MultiStatus"] = 207] = "MultiStatus";
|
|
518
|
-
EHttpStatusCode$1[EHttpStatusCode$1["AlreadyReported"] = 208] = "AlreadyReported";
|
|
519
|
-
EHttpStatusCode$1[EHttpStatusCode$1["IMUsed"] = 226] = "IMUsed";
|
|
520
|
-
EHttpStatusCode$1[EHttpStatusCode$1["MultipleChoices"] = 300] = "MultipleChoices";
|
|
521
|
-
EHttpStatusCode$1[EHttpStatusCode$1["MovedPermanently"] = 301] = "MovedPermanently";
|
|
522
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Found"] = 302] = "Found";
|
|
523
|
-
EHttpStatusCode$1[EHttpStatusCode$1["SeeOther"] = 303] = "SeeOther";
|
|
524
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NotModified"] = 304] = "NotModified";
|
|
525
|
-
EHttpStatusCode$1[EHttpStatusCode$1["UseProxy"] = 305] = "UseProxy";
|
|
526
|
-
EHttpStatusCode$1[EHttpStatusCode$1["SwitchProxy"] = 306] = "SwitchProxy";
|
|
527
|
-
EHttpStatusCode$1[EHttpStatusCode$1["TemporaryRedirect"] = 307] = "TemporaryRedirect";
|
|
528
|
-
EHttpStatusCode$1[EHttpStatusCode$1["PermanentRedirect"] = 308] = "PermanentRedirect";
|
|
529
|
-
EHttpStatusCode$1[EHttpStatusCode$1["BadRequest"] = 400] = "BadRequest";
|
|
530
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Unauthorized"] = 401] = "Unauthorized";
|
|
531
|
-
EHttpStatusCode$1[EHttpStatusCode$1["PaymentRequired"] = 402] = "PaymentRequired";
|
|
532
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Forbidden"] = 403] = "Forbidden";
|
|
533
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NotFound"] = 404] = "NotFound";
|
|
534
|
-
EHttpStatusCode$1[EHttpStatusCode$1["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
535
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NotAcceptable"] = 406] = "NotAcceptable";
|
|
536
|
-
EHttpStatusCode$1[EHttpStatusCode$1["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
|
|
537
|
-
EHttpStatusCode$1[EHttpStatusCode$1["RequestTimeout"] = 408] = "RequestTimeout";
|
|
538
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Conflict"] = 409] = "Conflict";
|
|
539
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Gone"] = 410] = "Gone";
|
|
540
|
-
EHttpStatusCode$1[EHttpStatusCode$1["LengthRequired"] = 411] = "LengthRequired";
|
|
541
|
-
EHttpStatusCode$1[EHttpStatusCode$1["PreconditionFailed"] = 412] = "PreconditionFailed";
|
|
542
|
-
EHttpStatusCode$1[EHttpStatusCode$1["PayloadTooLarge"] = 413] = "PayloadTooLarge";
|
|
543
|
-
EHttpStatusCode$1[EHttpStatusCode$1["URITooLong"] = 414] = "URITooLong";
|
|
544
|
-
EHttpStatusCode$1[EHttpStatusCode$1["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
|
|
545
|
-
EHttpStatusCode$1[EHttpStatusCode$1["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
|
|
546
|
-
EHttpStatusCode$1[EHttpStatusCode$1["ExpectationFailed"] = 417] = "ExpectationFailed";
|
|
547
|
-
EHttpStatusCode$1[EHttpStatusCode$1["ImATeapot"] = 418] = "ImATeapot";
|
|
548
|
-
EHttpStatusCode$1[EHttpStatusCode$1["MisdirectedRequest"] = 421] = "MisdirectedRequest";
|
|
549
|
-
EHttpStatusCode$1[EHttpStatusCode$1["UnprocessableEntity"] = 422] = "UnprocessableEntity";
|
|
550
|
-
EHttpStatusCode$1[EHttpStatusCode$1["Locked"] = 423] = "Locked";
|
|
551
|
-
EHttpStatusCode$1[EHttpStatusCode$1["FailedDependency"] = 424] = "FailedDependency";
|
|
552
|
-
EHttpStatusCode$1[EHttpStatusCode$1["TooEarly"] = 425] = "TooEarly";
|
|
553
|
-
EHttpStatusCode$1[EHttpStatusCode$1["UpgradeRequired"] = 426] = "UpgradeRequired";
|
|
554
|
-
EHttpStatusCode$1[EHttpStatusCode$1["PreconditionRequired"] = 428] = "PreconditionRequired";
|
|
555
|
-
EHttpStatusCode$1[EHttpStatusCode$1["TooManyRequests"] = 429] = "TooManyRequests";
|
|
556
|
-
EHttpStatusCode$1[EHttpStatusCode$1["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
|
|
557
|
-
EHttpStatusCode$1[EHttpStatusCode$1["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
|
|
558
|
-
EHttpStatusCode$1[EHttpStatusCode$1["InternalServerError"] = 500] = "InternalServerError";
|
|
559
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NotImplemented"] = 501] = "NotImplemented";
|
|
560
|
-
EHttpStatusCode$1[EHttpStatusCode$1["BadGateway"] = 502] = "BadGateway";
|
|
561
|
-
EHttpStatusCode$1[EHttpStatusCode$1["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
562
|
-
EHttpStatusCode$1[EHttpStatusCode$1["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
563
|
-
EHttpStatusCode$1[EHttpStatusCode$1["HTTPVersionNotSupported"] = 505] = "HTTPVersionNotSupported";
|
|
564
|
-
EHttpStatusCode$1[EHttpStatusCode$1["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
|
|
565
|
-
EHttpStatusCode$1[EHttpStatusCode$1["InsufficientStorage"] = 507] = "InsufficientStorage";
|
|
566
|
-
EHttpStatusCode$1[EHttpStatusCode$1["LoopDetected"] = 508] = "LoopDetected";
|
|
567
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NotExtended"] = 510] = "NotExtended";
|
|
568
|
-
EHttpStatusCode$1[EHttpStatusCode$1["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
|
|
569
|
-
return EHttpStatusCode$1;
|
|
570
|
-
}({});
|
|
571
|
-
|
|
572
|
-
//#endregion
|
|
573
|
-
//#region packages/event-http/src/errors/error-renderer.ts
|
|
574
|
-
const preStyles = "font-family: monospace;width: 100%;max-width: 900px;padding: 10px;margin: 20px auto;border-radius: 8px;background-color: #494949;box-shadow: 0px 0px 3px 2px rgb(255 255 255 / 20%);";
|
|
575
|
-
var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
576
|
-
renderHtml(response) {
|
|
577
|
-
const data = response.body || {};
|
|
578
|
-
response.setContentType("text/html");
|
|
579
|
-
const keys = Object.keys(data).filter((key) => ![
|
|
580
|
-
"statusCode",
|
|
581
|
-
"error",
|
|
582
|
-
"message"
|
|
583
|
-
].includes(key));
|
|
584
|
-
return "<html style=\"background-color: #333; color: #bbb;\">" + `<head><title>${data.statusCode} ${httpStatusCodes[data.statusCode]}</title></head>` + `<body><center><h1>${data.statusCode} ${httpStatusCodes[data.statusCode]}</h1></center>` + `<center><h4>${data.message}</h1></center><hr color="#666">` + `<center style="color: #666;"> Wooks v${"0.5.24"} </center>` + `${keys.length > 0 ? `<pre style="${preStyles}">${JSON.stringify({
|
|
585
|
-
...data,
|
|
586
|
-
statusCode: undefined,
|
|
587
|
-
message: undefined,
|
|
588
|
-
error: undefined
|
|
589
|
-
}, null, " ")}</pre>` : ""}` + "</body></html>";
|
|
590
|
-
}
|
|
591
|
-
renderText(response) {
|
|
592
|
-
const data = response.body || {};
|
|
593
|
-
response.setContentType("text/plain");
|
|
594
|
-
const keys = Object.keys(data).filter((key) => ![
|
|
595
|
-
"statusCode",
|
|
596
|
-
"error",
|
|
597
|
-
"message"
|
|
598
|
-
].includes(key));
|
|
599
|
-
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}` + `\n\n${keys.length > 0 ? `${JSON.stringify({
|
|
600
|
-
...data,
|
|
601
|
-
statusCode: undefined,
|
|
602
|
-
message: undefined,
|
|
603
|
-
error: undefined
|
|
604
|
-
}, null, " ")}` : ""}`;
|
|
605
|
-
}
|
|
606
|
-
renderJson(response) {
|
|
607
|
-
const data = response.body || {};
|
|
608
|
-
response.setContentType("application/json");
|
|
609
|
-
const keys = Object.keys(data).filter((key) => ![
|
|
610
|
-
"statusCode",
|
|
611
|
-
"error",
|
|
612
|
-
"message"
|
|
613
|
-
].includes(key));
|
|
614
|
-
return `{"statusCode":${escapeQuotes(data.statusCode)},` + `"error":"${escapeQuotes(data.error)}",` + `"message":"${escapeQuotes(data.message)}"` + `${keys.length > 0 ? `,${keys.map((k) => `"${escapeQuotes(k)}":${JSON.stringify(data[k])}`).join(",")}` : ""}}`;
|
|
615
|
-
}
|
|
616
|
-
render(response) {
|
|
617
|
-
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
618
|
-
response.status = response.body?.statusCode || 500;
|
|
619
|
-
if (acceptsJson()) return this.renderJson(response);
|
|
620
|
-
else if (acceptsHtml()) return this.renderHtml(response);
|
|
621
|
-
else if (acceptsText()) return this.renderText(response);
|
|
622
|
-
else return this.renderJson(response);
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
function escapeQuotes(s) {
|
|
626
|
-
return (typeof s === "number" ? s : s || "").toString().replace(/"/gu, "\\\"");
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
//#endregion
|
|
630
|
-
//#region packages/event-http/src/errors/http-error.ts
|
|
631
|
-
var HttpError = class extends Error {
|
|
632
|
-
name = "HttpError";
|
|
633
|
-
constructor(code = 500, _body = "") {
|
|
634
|
-
super(typeof _body === "string" ? _body : _body.message);
|
|
635
|
-
this.code = code;
|
|
636
|
-
this._body = _body;
|
|
637
|
-
}
|
|
638
|
-
get body() {
|
|
639
|
-
return typeof this._body === "string" ? {
|
|
640
|
-
statusCode: this.code,
|
|
641
|
-
message: this.message,
|
|
642
|
-
error: httpStatusCodes[this.code]
|
|
643
|
-
} : {
|
|
644
|
-
...this._body,
|
|
645
|
-
statusCode: this.code,
|
|
646
|
-
message: this.message,
|
|
647
|
-
error: httpStatusCodes[this.code]
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
|
-
renderer;
|
|
651
|
-
attachRenderer(renderer) {
|
|
652
|
-
this.renderer = renderer;
|
|
653
|
-
}
|
|
654
|
-
getRenderer() {
|
|
655
|
-
return this.renderer;
|
|
656
|
-
}
|
|
657
|
-
};
|
|
658
|
-
|
|
659
828
|
//#endregion
|
|
660
829
|
//#region packages/event-http/src/response/core.ts
|
|
661
830
|
const defaultStatus = {
|
|
@@ -784,14 +953,14 @@ var BaseHttpResponse = class {
|
|
|
784
953
|
});
|
|
785
954
|
stream$1.on("close", () => {
|
|
786
955
|
stream$1.destroy();
|
|
787
|
-
resolve(
|
|
956
|
+
resolve(void 0);
|
|
788
957
|
});
|
|
789
958
|
stream$1.pipe(res);
|
|
790
959
|
});
|
|
791
960
|
} else if (globalThis.Response && this.body instanceof Response) {
|
|
792
961
|
this.mergeFetchStatus(this.body.status);
|
|
793
962
|
if (method === "HEAD") res.end();
|
|
794
|
-
else {
|
|
963
|
+
else {
|
|
795
964
|
const additionalHeaders = {};
|
|
796
965
|
if (this.body.headers.get("content-length")) additionalHeaders["content-length"] = this.body.headers.get("content-length");
|
|
797
966
|
if (this.body.headers.get("content-type")) additionalHeaders["content-type"] = this.body.headers.get("content-type");
|
|
@@ -828,11 +997,11 @@ function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRe
|
|
|
828
997
|
const r = new BaseHttpResponse(errorRenderer);
|
|
829
998
|
let httpError;
|
|
830
999
|
if (data instanceof HttpError) httpError = data;
|
|
831
|
-
else httpError = new HttpError(500, data.message);
|
|
1000
|
+
else httpError = new HttpError(500, data.message);
|
|
832
1001
|
r.setBody(httpError.body);
|
|
833
1002
|
return r;
|
|
834
1003
|
} else if (data instanceof BaseHttpResponse) return data;
|
|
835
|
-
else return new BaseHttpResponse(renderer).setBody(data);
|
|
1004
|
+
else return new BaseHttpResponse(renderer).setBody(data);
|
|
836
1005
|
}
|
|
837
1006
|
return {
|
|
838
1007
|
createResponse,
|
|
@@ -847,7 +1016,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
847
1016
|
constructor(opts, wooks$1) {
|
|
848
1017
|
super(wooks$1, opts?.logger, opts?.router);
|
|
849
1018
|
this.opts = opts;
|
|
850
|
-
this.logger = opts?.logger || this.getLogger(
|
|
1019
|
+
this.logger = opts?.logger || this.getLogger(`[96m[wooks-http]`);
|
|
851
1020
|
}
|
|
852
1021
|
all(path, handler) {
|
|
853
1022
|
return this.on("*", path, handler);
|
|
@@ -885,7 +1054,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
885
1054
|
backlog,
|
|
886
1055
|
listeningListener
|
|
887
1056
|
];
|
|
888
|
-
const ui = args.indexOf(
|
|
1057
|
+
const ui = args.indexOf(void 0);
|
|
889
1058
|
if (ui >= 0) args = args.slice(0, ui);
|
|
890
1059
|
server.listen(...args);
|
|
891
1060
|
});
|
|
@@ -927,7 +1096,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
927
1096
|
}
|
|
928
1097
|
responder = createWooksResponder();
|
|
929
1098
|
respond(data) {
|
|
930
|
-
|
|
1099
|
+
this.responder.respond(data)?.catch((e) => {
|
|
931
1100
|
this.logger.error("Uncaught response exception", e);
|
|
932
1101
|
});
|
|
933
1102
|
}
|
|
@@ -958,7 +1127,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
958
1127
|
this.respond(error);
|
|
959
1128
|
return error;
|
|
960
1129
|
}
|
|
961
|
-
else {
|
|
1130
|
+
else {
|
|
962
1131
|
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
963
1132
|
const error = new HttpError(404);
|
|
964
1133
|
this.respond(error);
|
|
@@ -986,46 +1155,53 @@ else {
|
|
|
986
1155
|
}
|
|
987
1156
|
}
|
|
988
1157
|
};
|
|
1158
|
+
/**
|
|
1159
|
+
* Factory for WooksHttp App
|
|
1160
|
+
* @param opts TWooksHttpOptions
|
|
1161
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
1162
|
+
* @returns WooksHttp
|
|
1163
|
+
*/
|
|
989
1164
|
function createHttpApp(opts, wooks$1) {
|
|
990
1165
|
return new WooksHttp(opts, wooks$1);
|
|
991
1166
|
}
|
|
992
1167
|
|
|
993
1168
|
//#endregion
|
|
994
|
-
exports.BaseHttpResponse = BaseHttpResponse
|
|
995
|
-
exports.BaseHttpResponseRenderer = BaseHttpResponseRenderer
|
|
996
|
-
exports.
|
|
997
|
-
exports.
|
|
998
|
-
exports.
|
|
999
|
-
exports.
|
|
1000
|
-
exports.
|
|
1001
|
-
exports.
|
|
1002
|
-
exports.
|
|
1003
|
-
exports.
|
|
1004
|
-
exports.
|
|
1005
|
-
exports.
|
|
1006
|
-
exports.
|
|
1007
|
-
exports.
|
|
1008
|
-
exports.
|
|
1169
|
+
exports.BaseHttpResponse = BaseHttpResponse;
|
|
1170
|
+
exports.BaseHttpResponseRenderer = BaseHttpResponseRenderer;
|
|
1171
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
1172
|
+
exports.EHttpStatusCode = EHttpStatusCode;
|
|
1173
|
+
exports.HttpError = HttpError;
|
|
1174
|
+
exports.HttpErrorRenderer = HttpErrorRenderer;
|
|
1175
|
+
exports.WooksHttp = WooksHttp;
|
|
1176
|
+
exports.WooksURLSearchParams = WooksURLSearchParams;
|
|
1177
|
+
exports.createHttpApp = createHttpApp;
|
|
1178
|
+
exports.createHttpContext = createHttpContext;
|
|
1179
|
+
exports.createWooksResponder = createWooksResponder;
|
|
1180
|
+
exports.httpStatusCodes = httpStatusCodes;
|
|
1181
|
+
exports.renderCacheControl = renderCacheControl;
|
|
1182
|
+
exports.useAccept = useAccept;
|
|
1183
|
+
exports.useAuthorization = useAuthorization;
|
|
1184
|
+
exports.useCookies = useCookies;
|
|
1009
1185
|
Object.defineProperty(exports, 'useEventLogger', {
|
|
1010
1186
|
enumerable: true,
|
|
1011
1187
|
get: function () {
|
|
1012
1188
|
return __wooksjs_event_core.useEventLogger;
|
|
1013
1189
|
}
|
|
1014
1190
|
});
|
|
1015
|
-
exports.useHeaders = useHeaders
|
|
1016
|
-
exports.useHttpContext = useHttpContext
|
|
1017
|
-
exports.useRequest = useRequest
|
|
1018
|
-
exports.useResponse = useResponse
|
|
1191
|
+
exports.useHeaders = useHeaders;
|
|
1192
|
+
exports.useHttpContext = useHttpContext;
|
|
1193
|
+
exports.useRequest = useRequest;
|
|
1194
|
+
exports.useResponse = useResponse;
|
|
1019
1195
|
Object.defineProperty(exports, 'useRouteParams', {
|
|
1020
1196
|
enumerable: true,
|
|
1021
1197
|
get: function () {
|
|
1022
1198
|
return __wooksjs_event_core.useRouteParams;
|
|
1023
1199
|
}
|
|
1024
1200
|
});
|
|
1025
|
-
exports.useSearchParams = useSearchParams
|
|
1026
|
-
exports.useSetCacheControl = useSetCacheControl
|
|
1027
|
-
exports.useSetCookie = useSetCookie
|
|
1028
|
-
exports.useSetCookies = useSetCookies
|
|
1029
|
-
exports.useSetHeader = useSetHeader
|
|
1030
|
-
exports.useSetHeaders = useSetHeaders
|
|
1031
|
-
exports.useStatus = useStatus
|
|
1201
|
+
exports.useSearchParams = useSearchParams;
|
|
1202
|
+
exports.useSetCacheControl = useSetCacheControl;
|
|
1203
|
+
exports.useSetCookie = useSetCookie;
|
|
1204
|
+
exports.useSetCookies = useSetCookies;
|
|
1205
|
+
exports.useSetHeader = useSetHeader;
|
|
1206
|
+
exports.useSetHeaders = useSetHeaders;
|
|
1207
|
+
exports.useStatus = useStatus;
|