@wooksjs/event-http 0.6.0 → 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 +71 -62
- package/dist/index.d.ts +44 -0
- package/dist/index.mjs +46 -36
- 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;
|
|
@@ -42,6 +41,10 @@ function createHttpContext(data, options) {
|
|
|
42
41
|
options
|
|
43
42
|
});
|
|
44
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Wrapper on useEventContext with HTTP event types
|
|
46
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
47
|
+
*/
|
|
45
48
|
function useHttpContext() {
|
|
46
49
|
return (0, __wooksjs_event_core.useAsyncEventContext)("HTTP");
|
|
47
50
|
}
|
|
@@ -76,12 +79,12 @@ function convertTime(time, unit = "ms") {
|
|
|
76
79
|
const units = {
|
|
77
80
|
ms: 1,
|
|
78
81
|
s: 1e3,
|
|
79
|
-
m:
|
|
80
|
-
h:
|
|
81
|
-
d:
|
|
82
|
-
w:
|
|
83
|
-
M:
|
|
84
|
-
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
|
|
85
88
|
};
|
|
86
89
|
|
|
87
90
|
//#endregion
|
|
@@ -191,7 +194,7 @@ var BaseHttpResponseRenderer = class {
|
|
|
191
194
|
if (!response.getContentType()) response.setContentType("text/plain");
|
|
192
195
|
return response.body.toString();
|
|
193
196
|
}
|
|
194
|
-
if (response.body ===
|
|
197
|
+
if (response.body === void 0) return "";
|
|
195
198
|
if (response.body instanceof Uint8Array) return response.body;
|
|
196
199
|
if (typeof response.body === "object") {
|
|
197
200
|
if (!response.getContentType()) response.setContentType("application/json");
|
|
@@ -268,7 +271,7 @@ const httpStatusCodes = {
|
|
|
268
271
|
510: "Not Extended",
|
|
269
272
|
511: "Network Authentication Required"
|
|
270
273
|
};
|
|
271
|
-
let EHttpStatusCode = function(EHttpStatusCode$1) {
|
|
274
|
+
let EHttpStatusCode = /* @__PURE__ */ function(EHttpStatusCode$1) {
|
|
272
275
|
EHttpStatusCode$1[EHttpStatusCode$1["Continue"] = 100] = "Continue";
|
|
273
276
|
EHttpStatusCode$1[EHttpStatusCode$1["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
274
277
|
EHttpStatusCode$1[EHttpStatusCode$1["Processing"] = 102] = "Processing";
|
|
@@ -347,12 +350,12 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
347
350
|
"error",
|
|
348
351
|
"message"
|
|
349
352
|
].includes(key));
|
|
350
|
-
return
|
|
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({
|
|
351
354
|
...data,
|
|
352
|
-
statusCode:
|
|
353
|
-
message:
|
|
354
|
-
error:
|
|
355
|
-
}, null, " ")}</pre>` : ""}
|
|
355
|
+
statusCode: void 0,
|
|
356
|
+
message: void 0,
|
|
357
|
+
error: void 0
|
|
358
|
+
}, null, " ")}</pre>` : ""}</body></html>`;
|
|
356
359
|
}
|
|
357
360
|
renderText(response) {
|
|
358
361
|
const data = response.body || {};
|
|
@@ -362,11 +365,11 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
362
365
|
"error",
|
|
363
366
|
"message"
|
|
364
367
|
].includes(key));
|
|
365
|
-
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}
|
|
368
|
+
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}\n\n${keys.length > 0 ? `${JSON.stringify({
|
|
366
369
|
...data,
|
|
367
|
-
statusCode:
|
|
368
|
-
message:
|
|
369
|
-
error:
|
|
370
|
+
statusCode: void 0,
|
|
371
|
+
message: void 0,
|
|
372
|
+
error: void 0
|
|
370
373
|
}, null, " ")}` : ""}`;
|
|
371
374
|
}
|
|
372
375
|
renderJson(response) {
|
|
@@ -377,15 +380,15 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
377
380
|
"error",
|
|
378
381
|
"message"
|
|
379
382
|
].includes(key));
|
|
380
|
-
return `{"statusCode":${escapeQuotes(data.statusCode)}
|
|
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(",")}` : ""}}`;
|
|
381
384
|
}
|
|
382
385
|
render(response) {
|
|
383
386
|
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
384
387
|
response.status = response.body?.statusCode || 500;
|
|
385
388
|
if (acceptsJson()) return this.renderJson(response);
|
|
386
|
-
else if (acceptsHtml()) return this.renderHtml(response);
|
|
387
|
-
else if (acceptsText()) return this.renderText(response);
|
|
388
|
-
else 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);
|
|
389
392
|
}
|
|
390
393
|
};
|
|
391
394
|
function escapeQuotes(s) {
|
|
@@ -426,8 +429,8 @@ var HttpError = class extends Error {
|
|
|
426
429
|
//#region packages/event-http/src/composables/request.ts
|
|
427
430
|
const xForwardedFor = "x-forwarded-for";
|
|
428
431
|
const DEFAULT_LIMITS = {
|
|
429
|
-
maxCompressed:
|
|
430
|
-
maxInflated:
|
|
432
|
+
maxCompressed: 1 * 1024 * 1024,
|
|
433
|
+
maxInflated: 10 * 1024 * 1024,
|
|
431
434
|
maxRatio: 100,
|
|
432
435
|
readTimeoutMs: 1e4
|
|
433
436
|
};
|
|
@@ -521,12 +524,12 @@ function useRequest() {
|
|
|
521
524
|
const reqId = (0, __wooksjs_event_core.useEventId)().getId;
|
|
522
525
|
const forwardedIp = () => init("forwardedIp", () => {
|
|
523
526
|
if (typeof req.headers[xForwardedFor] === "string" && req.headers[xForwardedFor]) return req.headers[xForwardedFor].split(",").shift()?.trim();
|
|
524
|
-
else return "";
|
|
527
|
+
else return "";
|
|
525
528
|
});
|
|
526
529
|
const remoteIp = () => init("remoteIp", () => req.socket.remoteAddress || req.connection.remoteAddress || "");
|
|
527
530
|
function getIp(options) {
|
|
528
531
|
if (options?.trustProxy) return forwardedIp() || getIp();
|
|
529
|
-
else return remoteIp();
|
|
532
|
+
else return remoteIp();
|
|
530
533
|
}
|
|
531
534
|
const getIpList = () => init("ipList", () => ({
|
|
532
535
|
remoteIp: req.socket.remoteAddress || req.connection.remoteAddress || "",
|
|
@@ -707,7 +710,7 @@ function useAuthorization() {
|
|
|
707
710
|
function renderCacheControl(data) {
|
|
708
711
|
let attrs = "";
|
|
709
712
|
for (const [a, v] of Object.entries(data)) {
|
|
710
|
-
if (v ===
|
|
713
|
+
if (v === void 0) continue;
|
|
711
714
|
const func = cacheControlFunc[a];
|
|
712
715
|
if (typeof func === "function") {
|
|
713
716
|
const val = func(v);
|
|
@@ -950,14 +953,14 @@ var BaseHttpResponse = class {
|
|
|
950
953
|
});
|
|
951
954
|
stream$1.on("close", () => {
|
|
952
955
|
stream$1.destroy();
|
|
953
|
-
resolve(
|
|
956
|
+
resolve(void 0);
|
|
954
957
|
});
|
|
955
958
|
stream$1.pipe(res);
|
|
956
959
|
});
|
|
957
960
|
} else if (globalThis.Response && this.body instanceof Response) {
|
|
958
961
|
this.mergeFetchStatus(this.body.status);
|
|
959
962
|
if (method === "HEAD") res.end();
|
|
960
|
-
else {
|
|
963
|
+
else {
|
|
961
964
|
const additionalHeaders = {};
|
|
962
965
|
if (this.body.headers.get("content-length")) additionalHeaders["content-length"] = this.body.headers.get("content-length");
|
|
963
966
|
if (this.body.headers.get("content-type")) additionalHeaders["content-type"] = this.body.headers.get("content-type");
|
|
@@ -994,11 +997,11 @@ function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRe
|
|
|
994
997
|
const r = new BaseHttpResponse(errorRenderer);
|
|
995
998
|
let httpError;
|
|
996
999
|
if (data instanceof HttpError) httpError = data;
|
|
997
|
-
else httpError = new HttpError(500, data.message);
|
|
1000
|
+
else httpError = new HttpError(500, data.message);
|
|
998
1001
|
r.setBody(httpError.body);
|
|
999
1002
|
return r;
|
|
1000
1003
|
} else if (data instanceof BaseHttpResponse) return data;
|
|
1001
|
-
else return new BaseHttpResponse(renderer).setBody(data);
|
|
1004
|
+
else return new BaseHttpResponse(renderer).setBody(data);
|
|
1002
1005
|
}
|
|
1003
1006
|
return {
|
|
1004
1007
|
createResponse,
|
|
@@ -1013,7 +1016,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1013
1016
|
constructor(opts, wooks$1) {
|
|
1014
1017
|
super(wooks$1, opts?.logger, opts?.router);
|
|
1015
1018
|
this.opts = opts;
|
|
1016
|
-
this.logger = opts?.logger || this.getLogger(
|
|
1019
|
+
this.logger = opts?.logger || this.getLogger(`[96m[wooks-http]`);
|
|
1017
1020
|
}
|
|
1018
1021
|
all(path, handler) {
|
|
1019
1022
|
return this.on("*", path, handler);
|
|
@@ -1051,7 +1054,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1051
1054
|
backlog,
|
|
1052
1055
|
listeningListener
|
|
1053
1056
|
];
|
|
1054
|
-
const ui = args.indexOf(
|
|
1057
|
+
const ui = args.indexOf(void 0);
|
|
1055
1058
|
if (ui >= 0) args = args.slice(0, ui);
|
|
1056
1059
|
server.listen(...args);
|
|
1057
1060
|
});
|
|
@@ -1093,7 +1096,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1093
1096
|
}
|
|
1094
1097
|
responder = createWooksResponder();
|
|
1095
1098
|
respond(data) {
|
|
1096
|
-
|
|
1099
|
+
this.responder.respond(data)?.catch((e) => {
|
|
1097
1100
|
this.logger.error("Uncaught response exception", e);
|
|
1098
1101
|
});
|
|
1099
1102
|
}
|
|
@@ -1124,7 +1127,7 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
|
|
|
1124
1127
|
this.respond(error);
|
|
1125
1128
|
return error;
|
|
1126
1129
|
}
|
|
1127
|
-
else {
|
|
1130
|
+
else {
|
|
1128
1131
|
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
1129
1132
|
const error = new HttpError(404);
|
|
1130
1133
|
this.respond(error);
|
|
@@ -1152,47 +1155,53 @@ else {
|
|
|
1152
1155
|
}
|
|
1153
1156
|
}
|
|
1154
1157
|
};
|
|
1158
|
+
/**
|
|
1159
|
+
* Factory for WooksHttp App
|
|
1160
|
+
* @param opts TWooksHttpOptions
|
|
1161
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
1162
|
+
* @returns WooksHttp
|
|
1163
|
+
*/
|
|
1155
1164
|
function createHttpApp(opts, wooks$1) {
|
|
1156
1165
|
return new WooksHttp(opts, wooks$1);
|
|
1157
1166
|
}
|
|
1158
1167
|
|
|
1159
1168
|
//#endregion
|
|
1160
|
-
exports.BaseHttpResponse = BaseHttpResponse
|
|
1161
|
-
exports.BaseHttpResponseRenderer = BaseHttpResponseRenderer
|
|
1162
|
-
exports.DEFAULT_LIMITS = DEFAULT_LIMITS
|
|
1163
|
-
exports.EHttpStatusCode = EHttpStatusCode
|
|
1164
|
-
exports.HttpError = HttpError
|
|
1165
|
-
exports.HttpErrorRenderer = HttpErrorRenderer
|
|
1166
|
-
exports.WooksHttp = WooksHttp
|
|
1167
|
-
exports.WooksURLSearchParams = WooksURLSearchParams
|
|
1168
|
-
exports.createHttpApp = createHttpApp
|
|
1169
|
-
exports.createHttpContext = createHttpContext
|
|
1170
|
-
exports.createWooksResponder = createWooksResponder
|
|
1171
|
-
exports.httpStatusCodes = httpStatusCodes
|
|
1172
|
-
exports.renderCacheControl = renderCacheControl
|
|
1173
|
-
exports.useAccept = useAccept
|
|
1174
|
-
exports.useAuthorization = useAuthorization
|
|
1175
|
-
exports.useCookies = useCookies
|
|
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;
|
|
1176
1185
|
Object.defineProperty(exports, 'useEventLogger', {
|
|
1177
1186
|
enumerable: true,
|
|
1178
1187
|
get: function () {
|
|
1179
1188
|
return __wooksjs_event_core.useEventLogger;
|
|
1180
1189
|
}
|
|
1181
1190
|
});
|
|
1182
|
-
exports.useHeaders = useHeaders
|
|
1183
|
-
exports.useHttpContext = useHttpContext
|
|
1184
|
-
exports.useRequest = useRequest
|
|
1185
|
-
exports.useResponse = useResponse
|
|
1191
|
+
exports.useHeaders = useHeaders;
|
|
1192
|
+
exports.useHttpContext = useHttpContext;
|
|
1193
|
+
exports.useRequest = useRequest;
|
|
1194
|
+
exports.useResponse = useResponse;
|
|
1186
1195
|
Object.defineProperty(exports, 'useRouteParams', {
|
|
1187
1196
|
enumerable: true,
|
|
1188
1197
|
get: function () {
|
|
1189
1198
|
return __wooksjs_event_core.useRouteParams;
|
|
1190
1199
|
}
|
|
1191
1200
|
});
|
|
1192
|
-
exports.useSearchParams = useSearchParams
|
|
1193
|
-
exports.useSetCacheControl = useSetCacheControl
|
|
1194
|
-
exports.useSetCookie = useSetCookie
|
|
1195
|
-
exports.useSetCookies = useSetCookies
|
|
1196
|
-
exports.useSetHeader = useSetHeader
|
|
1197
|
-
exports.useSetHeaders = useSetHeaders
|
|
1198
|
-
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;
|
package/dist/index.d.ts
CHANGED
|
@@ -401,6 +401,10 @@ declare class HttpErrorRenderer extends BaseHttpResponseRenderer<TWooksErrorBody
|
|
|
401
401
|
}
|
|
402
402
|
|
|
403
403
|
declare function createHttpContext(data: THttpEventData, options: TEventOptions): <T>(cb: (...a: any[]) => T) => T;
|
|
404
|
+
/**
|
|
405
|
+
* Wrapper on useEventContext with HTTP event types
|
|
406
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
407
|
+
*/
|
|
404
408
|
declare function useHttpContext<T extends TEmpty>(): TCtxHelpers<THttpContextStore & T & TGenericContextStore<THttpEventData>>;
|
|
405
409
|
|
|
406
410
|
declare function createWooksResponder(renderer?: TWooksResponseRenderer<any>, errorRenderer?: TWooksResponseRenderer<any>): {
|
|
@@ -427,6 +431,11 @@ declare class WooksHttp extends WooksAdapterBase {
|
|
|
427
431
|
head<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
|
|
428
432
|
options<ResType = unknown, ParamsType = Record<string, string | string[]>>(path: string, handler: TWooksHandler<ResType>): wooks.TProstoRouterPathHandle<ParamsType>;
|
|
429
433
|
protected server?: Server;
|
|
434
|
+
/**
|
|
435
|
+
* Starts the http(s) server.
|
|
436
|
+
*
|
|
437
|
+
* Use this only if you rely on Wooks server.
|
|
438
|
+
*/
|
|
430
439
|
listen(port?: number, hostname?: string, backlog?: number, listeningListener?: () => void): Promise<void>;
|
|
431
440
|
listen(port?: number, hostname?: string, listeningListener?: () => void): Promise<void>;
|
|
432
441
|
listen(port?: number, backlog?: number, listeningListener?: () => void): Promise<void>;
|
|
@@ -436,17 +445,52 @@ declare class WooksHttp extends WooksAdapterBase {
|
|
|
436
445
|
listen(options: ListenOptions, listeningListener?: () => void): Promise<void>;
|
|
437
446
|
listen(handle: any, backlog?: number, listeningListener?: () => void): Promise<void>;
|
|
438
447
|
listen(handle: any, listeningListener?: () => void): Promise<void>;
|
|
448
|
+
/**
|
|
449
|
+
* Stops the server if it was attached or passed via argument
|
|
450
|
+
* @param server
|
|
451
|
+
*/
|
|
439
452
|
close(server?: Server): Promise<unknown>;
|
|
453
|
+
/**
|
|
454
|
+
* Returns http(s) server that was attached to Wooks
|
|
455
|
+
*
|
|
456
|
+
* See attachServer method docs
|
|
457
|
+
* @returns Server
|
|
458
|
+
*/
|
|
440
459
|
getServer(): Server<typeof IncomingMessage, typeof ServerResponse> | undefined;
|
|
460
|
+
/**
|
|
461
|
+
* Attaches http(s) server instance
|
|
462
|
+
* to Wooks.
|
|
463
|
+
*
|
|
464
|
+
* Use it only if you want to `close` method to stop the server.
|
|
465
|
+
* @param server Server
|
|
466
|
+
*/
|
|
441
467
|
attachServer(server?: Server): void;
|
|
442
468
|
protected responder: {
|
|
443
469
|
createResponse: <T = unknown>(data: T) => BaseHttpResponse<T | TWooksErrorBodyExt> | null;
|
|
444
470
|
respond: (data: unknown) => Promise<unknown> | undefined;
|
|
445
471
|
};
|
|
446
472
|
protected respond(data: unknown): void;
|
|
473
|
+
/**
|
|
474
|
+
* Returns server callback function
|
|
475
|
+
* that can be passed to any node server:
|
|
476
|
+
* ```js
|
|
477
|
+
* import { createHttpApp } from '@wooksjs/event-http'
|
|
478
|
+
* import http from 'http'
|
|
479
|
+
*
|
|
480
|
+
* const app = createHttpApp()
|
|
481
|
+
* const server = http.createServer(app.getServerCb())
|
|
482
|
+
* server.listen(3000)
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
447
485
|
getServerCb(): (req: IncomingMessage, res: ServerResponse) => void;
|
|
448
486
|
protected processHandlers(handlers: TWooksHandler[]): Promise<unknown>;
|
|
449
487
|
}
|
|
488
|
+
/**
|
|
489
|
+
* Factory for WooksHttp App
|
|
490
|
+
* @param opts TWooksHttpOptions
|
|
491
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
492
|
+
* @returns WooksHttp
|
|
493
|
+
*/
|
|
450
494
|
declare function createHttpApp(opts?: TWooksHttpOptions, wooks?: Wooks | WooksAdapterBase): WooksHttp;
|
|
451
495
|
|
|
452
496
|
export { BaseHttpResponse, BaseHttpResponseRenderer, DEFAULT_LIMITS, EHttpStatusCode, HttpError, HttpErrorRenderer, type TAuthCache, type TCacheControl, type TCookieAttributes, type TCookieAttributesInput, type TCookieHook, type THeaderHook, type THttpContextStore, type THttpEvent, type THttpEventData, type TRequestCache, type TSearchParamsCache, type TSetCookieData, type TStatusHook, type TWooksErrorBody, type TWooksErrorBodyExt, type TWooksHttpOptions, type TWooksResponseRenderer, WooksHttp, WooksURLSearchParams, createHttpApp, createHttpContext, createWooksResponder, httpStatusCodes, renderCacheControl, useAccept, useAuthorization, useCookies, useHeaders, useHttpContext, useRequest, useResponse, useSearchParams, useSetCacheControl, useSetCookie, useSetCookies, useSetHeader, useSetHeaders, useStatus };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { attachHook, createAsyncEventContext, useAsyncEventContext, useEventId, useEventLogger, useRouteParams } from "@wooksjs/event-core";
|
|
1
|
+
import { attachHook, createAsyncEventContext, useAsyncEventContext, useEventId, useEventLogger, useEventLogger as useEventLogger$1, useRouteParams } from "@wooksjs/event-core";
|
|
2
2
|
import { Buffer as Buffer$1 } from "buffer";
|
|
3
3
|
import { Readable, pipeline } from "node:stream";
|
|
4
4
|
import { promisify } from "node:util";
|
|
@@ -18,6 +18,10 @@ function createHttpContext(data, options) {
|
|
|
18
18
|
options
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Wrapper on useEventContext with HTTP event types
|
|
23
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
24
|
+
*/
|
|
21
25
|
function useHttpContext() {
|
|
22
26
|
return useAsyncEventContext("HTTP");
|
|
23
27
|
}
|
|
@@ -52,12 +56,12 @@ function convertTime(time, unit = "ms") {
|
|
|
52
56
|
const units = {
|
|
53
57
|
ms: 1,
|
|
54
58
|
s: 1e3,
|
|
55
|
-
m:
|
|
56
|
-
h:
|
|
57
|
-
d:
|
|
58
|
-
w:
|
|
59
|
-
M:
|
|
60
|
-
Y:
|
|
59
|
+
m: 1e3 * 60,
|
|
60
|
+
h: 1e3 * 60 * 60,
|
|
61
|
+
d: 1e3 * 60 * 60 * 24,
|
|
62
|
+
w: 1e3 * 60 * 60 * 24 * 7,
|
|
63
|
+
M: 1e3 * 60 * 60 * 24 * 30,
|
|
64
|
+
Y: 1e3 * 60 * 60 * 24 * 365
|
|
61
65
|
};
|
|
62
66
|
|
|
63
67
|
//#endregion
|
|
@@ -167,7 +171,7 @@ var BaseHttpResponseRenderer = class {
|
|
|
167
171
|
if (!response.getContentType()) response.setContentType("text/plain");
|
|
168
172
|
return response.body.toString();
|
|
169
173
|
}
|
|
170
|
-
if (response.body ===
|
|
174
|
+
if (response.body === void 0) return "";
|
|
171
175
|
if (response.body instanceof Uint8Array) return response.body;
|
|
172
176
|
if (typeof response.body === "object") {
|
|
173
177
|
if (!response.getContentType()) response.setContentType("application/json");
|
|
@@ -244,7 +248,7 @@ const httpStatusCodes = {
|
|
|
244
248
|
510: "Not Extended",
|
|
245
249
|
511: "Network Authentication Required"
|
|
246
250
|
};
|
|
247
|
-
let EHttpStatusCode = function(EHttpStatusCode$1) {
|
|
251
|
+
let EHttpStatusCode = /* @__PURE__ */ function(EHttpStatusCode$1) {
|
|
248
252
|
EHttpStatusCode$1[EHttpStatusCode$1["Continue"] = 100] = "Continue";
|
|
249
253
|
EHttpStatusCode$1[EHttpStatusCode$1["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
250
254
|
EHttpStatusCode$1[EHttpStatusCode$1["Processing"] = 102] = "Processing";
|
|
@@ -323,12 +327,12 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
323
327
|
"error",
|
|
324
328
|
"message"
|
|
325
329
|
].includes(key));
|
|
326
|
-
return
|
|
330
|
+
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({
|
|
327
331
|
...data,
|
|
328
|
-
statusCode:
|
|
329
|
-
message:
|
|
330
|
-
error:
|
|
331
|
-
}, null, " ")}</pre>` : ""}
|
|
332
|
+
statusCode: void 0,
|
|
333
|
+
message: void 0,
|
|
334
|
+
error: void 0
|
|
335
|
+
}, null, " ")}</pre>` : ""}</body></html>`;
|
|
332
336
|
}
|
|
333
337
|
renderText(response) {
|
|
334
338
|
const data = response.body || {};
|
|
@@ -338,11 +342,11 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
338
342
|
"error",
|
|
339
343
|
"message"
|
|
340
344
|
].includes(key));
|
|
341
|
-
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}
|
|
345
|
+
return `${data.statusCode} ${httpStatusCodes[data.statusCode]}\n${data.message}\n\n${keys.length > 0 ? `${JSON.stringify({
|
|
342
346
|
...data,
|
|
343
|
-
statusCode:
|
|
344
|
-
message:
|
|
345
|
-
error:
|
|
347
|
+
statusCode: void 0,
|
|
348
|
+
message: void 0,
|
|
349
|
+
error: void 0
|
|
346
350
|
}, null, " ")}` : ""}`;
|
|
347
351
|
}
|
|
348
352
|
renderJson(response) {
|
|
@@ -353,15 +357,15 @@ var HttpErrorRenderer = class extends BaseHttpResponseRenderer {
|
|
|
353
357
|
"error",
|
|
354
358
|
"message"
|
|
355
359
|
].includes(key));
|
|
356
|
-
return `{"statusCode":${escapeQuotes(data.statusCode)}
|
|
360
|
+
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(",")}` : ""}}`;
|
|
357
361
|
}
|
|
358
362
|
render(response) {
|
|
359
363
|
const { acceptsJson, acceptsText, acceptsHtml } = useAccept();
|
|
360
364
|
response.status = response.body?.statusCode || 500;
|
|
361
365
|
if (acceptsJson()) return this.renderJson(response);
|
|
362
|
-
else if (acceptsHtml()) return this.renderHtml(response);
|
|
363
|
-
else if (acceptsText()) return this.renderText(response);
|
|
364
|
-
else return this.renderJson(response);
|
|
366
|
+
else if (acceptsHtml()) return this.renderHtml(response);
|
|
367
|
+
else if (acceptsText()) return this.renderText(response);
|
|
368
|
+
else return this.renderJson(response);
|
|
365
369
|
}
|
|
366
370
|
};
|
|
367
371
|
function escapeQuotes(s) {
|
|
@@ -402,8 +406,8 @@ var HttpError = class extends Error {
|
|
|
402
406
|
//#region packages/event-http/src/composables/request.ts
|
|
403
407
|
const xForwardedFor = "x-forwarded-for";
|
|
404
408
|
const DEFAULT_LIMITS = {
|
|
405
|
-
maxCompressed:
|
|
406
|
-
maxInflated:
|
|
409
|
+
maxCompressed: 1 * 1024 * 1024,
|
|
410
|
+
maxInflated: 10 * 1024 * 1024,
|
|
407
411
|
maxRatio: 100,
|
|
408
412
|
readTimeoutMs: 1e4
|
|
409
413
|
};
|
|
@@ -497,12 +501,12 @@ function useRequest() {
|
|
|
497
501
|
const reqId = useEventId().getId;
|
|
498
502
|
const forwardedIp = () => init("forwardedIp", () => {
|
|
499
503
|
if (typeof req.headers[xForwardedFor] === "string" && req.headers[xForwardedFor]) return req.headers[xForwardedFor].split(",").shift()?.trim();
|
|
500
|
-
else return "";
|
|
504
|
+
else return "";
|
|
501
505
|
});
|
|
502
506
|
const remoteIp = () => init("remoteIp", () => req.socket.remoteAddress || req.connection.remoteAddress || "");
|
|
503
507
|
function getIp(options) {
|
|
504
508
|
if (options?.trustProxy) return forwardedIp() || getIp();
|
|
505
|
-
else return remoteIp();
|
|
509
|
+
else return remoteIp();
|
|
506
510
|
}
|
|
507
511
|
const getIpList = () => init("ipList", () => ({
|
|
508
512
|
remoteIp: req.socket.remoteAddress || req.connection.remoteAddress || "",
|
|
@@ -683,7 +687,7 @@ function useAuthorization() {
|
|
|
683
687
|
function renderCacheControl(data) {
|
|
684
688
|
let attrs = "";
|
|
685
689
|
for (const [a, v] of Object.entries(data)) {
|
|
686
|
-
if (v ===
|
|
690
|
+
if (v === void 0) continue;
|
|
687
691
|
const func = cacheControlFunc[a];
|
|
688
692
|
if (typeof func === "function") {
|
|
689
693
|
const val = func(v);
|
|
@@ -904,7 +908,7 @@ var BaseHttpResponse = class {
|
|
|
904
908
|
async respond() {
|
|
905
909
|
const { rawResponse, hasResponded } = useResponse();
|
|
906
910
|
const { method, rawRequest } = useRequest();
|
|
907
|
-
const logger = useEventLogger("http-response") || console;
|
|
911
|
+
const logger = useEventLogger$1("http-response") || console;
|
|
908
912
|
if (hasResponded()) this.panic("The response was already sent.", logger);
|
|
909
913
|
this.mergeHeaders();
|
|
910
914
|
const res = rawResponse();
|
|
@@ -926,14 +930,14 @@ var BaseHttpResponse = class {
|
|
|
926
930
|
});
|
|
927
931
|
stream.on("close", () => {
|
|
928
932
|
stream.destroy();
|
|
929
|
-
resolve(
|
|
933
|
+
resolve(void 0);
|
|
930
934
|
});
|
|
931
935
|
stream.pipe(res);
|
|
932
936
|
});
|
|
933
937
|
} else if (globalThis.Response && this.body instanceof Response) {
|
|
934
938
|
this.mergeFetchStatus(this.body.status);
|
|
935
939
|
if (method === "HEAD") res.end();
|
|
936
|
-
else {
|
|
940
|
+
else {
|
|
937
941
|
const additionalHeaders = {};
|
|
938
942
|
if (this.body.headers.get("content-length")) additionalHeaders["content-length"] = this.body.headers.get("content-length");
|
|
939
943
|
if (this.body.headers.get("content-type")) additionalHeaders["content-type"] = this.body.headers.get("content-type");
|
|
@@ -970,11 +974,11 @@ function createWooksResponder(renderer = new BaseHttpResponseRenderer(), errorRe
|
|
|
970
974
|
const r = new BaseHttpResponse(errorRenderer);
|
|
971
975
|
let httpError;
|
|
972
976
|
if (data instanceof HttpError) httpError = data;
|
|
973
|
-
else httpError = new HttpError(500, data.message);
|
|
977
|
+
else httpError = new HttpError(500, data.message);
|
|
974
978
|
r.setBody(httpError.body);
|
|
975
979
|
return r;
|
|
976
980
|
} else if (data instanceof BaseHttpResponse) return data;
|
|
977
|
-
else return new BaseHttpResponse(renderer).setBody(data);
|
|
981
|
+
else return new BaseHttpResponse(renderer).setBody(data);
|
|
978
982
|
}
|
|
979
983
|
return {
|
|
980
984
|
createResponse,
|
|
@@ -989,7 +993,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
989
993
|
constructor(opts, wooks) {
|
|
990
994
|
super(wooks, opts?.logger, opts?.router);
|
|
991
995
|
this.opts = opts;
|
|
992
|
-
this.logger = opts?.logger || this.getLogger(
|
|
996
|
+
this.logger = opts?.logger || this.getLogger(`[96m[wooks-http]`);
|
|
993
997
|
}
|
|
994
998
|
all(path, handler) {
|
|
995
999
|
return this.on("*", path, handler);
|
|
@@ -1027,7 +1031,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
1027
1031
|
backlog,
|
|
1028
1032
|
listeningListener
|
|
1029
1033
|
];
|
|
1030
|
-
const ui = args.indexOf(
|
|
1034
|
+
const ui = args.indexOf(void 0);
|
|
1031
1035
|
if (ui >= 0) args = args.slice(0, ui);
|
|
1032
1036
|
server.listen(...args);
|
|
1033
1037
|
});
|
|
@@ -1069,7 +1073,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
1069
1073
|
}
|
|
1070
1074
|
responder = createWooksResponder();
|
|
1071
1075
|
respond(data) {
|
|
1072
|
-
|
|
1076
|
+
this.responder.respond(data)?.catch((e) => {
|
|
1073
1077
|
this.logger.error("Uncaught response exception", e);
|
|
1074
1078
|
});
|
|
1075
1079
|
}
|
|
@@ -1100,7 +1104,7 @@ var WooksHttp = class extends WooksAdapterBase {
|
|
|
1100
1104
|
this.respond(error);
|
|
1101
1105
|
return error;
|
|
1102
1106
|
}
|
|
1103
|
-
else {
|
|
1107
|
+
else {
|
|
1104
1108
|
this.logger.debug(`404 Not found (${req.method})${req.url}`);
|
|
1105
1109
|
const error = new HttpError(404);
|
|
1106
1110
|
this.respond(error);
|
|
@@ -1128,6 +1132,12 @@ else {
|
|
|
1128
1132
|
}
|
|
1129
1133
|
}
|
|
1130
1134
|
};
|
|
1135
|
+
/**
|
|
1136
|
+
* Factory for WooksHttp App
|
|
1137
|
+
* @param opts TWooksHttpOptions
|
|
1138
|
+
* @param wooks Wooks | WooksAdapterBase
|
|
1139
|
+
* @returns WooksHttp
|
|
1140
|
+
*/
|
|
1131
1141
|
function createHttpApp(opts, wooks) {
|
|
1132
1142
|
return new WooksHttp(opts, wooks);
|
|
1133
1143
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/event-http",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "@wooksjs/event-http",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -40,16 +40,15 @@
|
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@prostojs/router": "^0.2.1",
|
|
43
|
-
"@
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
"dependencies": {
|
|
47
|
-
"@prostojs/logger": "^0.4.3"
|
|
43
|
+
"@prostojs/logger": "^0.4.3",
|
|
44
|
+
"@wooksjs/event-core": "^0.6.1",
|
|
45
|
+
"wooks": "^0.6.1"
|
|
48
46
|
},
|
|
47
|
+
"dependencies": {},
|
|
49
48
|
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-http#readme",
|
|
50
49
|
"devDependencies": {
|
|
51
|
-
"typescript": "^5.
|
|
52
|
-
"vitest": "^2.
|
|
50
|
+
"typescript": "^5.8.3",
|
|
51
|
+
"vitest": "^3.2.4"
|
|
53
52
|
},
|
|
54
53
|
"scripts": {
|
|
55
54
|
"build": "rolldown -c ../../rolldown.config.mjs"
|