tezx 3.0.9-beta → 3.0.11-beta
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/bun/index.d.ts +4 -2
- package/bun/index.js +3 -2
- package/bun/ws.d.ts +37 -0
- package/bun/ws.js +23 -0
- package/cjs/bun/index.js +37 -5
- package/cjs/bun/ws.js +25 -0
- package/cjs/core/context.js +86 -88
- package/cjs/core/error.js +41 -0
- package/cjs/core/request.js +6 -6
- package/cjs/core/router.js +6 -6
- package/cjs/core/server.js +45 -63
- package/cjs/index.js +5 -2
- package/cjs/middleware/basic-auth.js +28 -54
- package/cjs/middleware/bearer-auth.js +34 -0
- package/cjs/middleware/cors.js +14 -24
- package/cjs/middleware/index.js +25 -0
- package/cjs/middleware/logger.js +6 -3
- package/cjs/middleware/pagination.js +1 -1
- package/cjs/middleware/powered-by.js +1 -1
- package/cjs/middleware/rate-limiter.js +20 -7
- package/cjs/middleware/request-id.js +4 -7
- package/cjs/middleware/sanitize-headers.js +8 -40
- package/cjs/middleware/xss-protection.js +2 -6
- package/cjs/registry/RadixRouter.js +72 -23
- package/cjs/utils/cookie.js +1 -1
- package/cjs/utils/rateLimit.js +2 -2
- package/cjs/utils/regexRouter.js +1 -0
- package/cjs/utils/response.js +21 -30
- package/core/context.d.ts +68 -68
- package/core/context.js +87 -89
- package/core/error.d.ts +95 -0
- package/core/error.js +37 -0
- package/core/request.d.ts +2 -2
- package/core/request.js +6 -6
- package/core/router.d.ts +11 -6
- package/core/router.js +6 -6
- package/core/server.js +45 -63
- package/index.d.ts +5 -3
- package/index.js +4 -2
- package/middleware/basic-auth.d.ts +38 -66
- package/middleware/basic-auth.js +28 -54
- package/middleware/bearer-auth.d.ts +52 -0
- package/middleware/bearer-auth.js +30 -0
- package/middleware/cors.d.ts +7 -21
- package/middleware/cors.js +14 -24
- package/middleware/index.d.ts +9 -0
- package/middleware/index.js +9 -0
- package/middleware/logger.d.ts +3 -1
- package/middleware/logger.js +6 -3
- package/middleware/pagination.d.ts +8 -6
- package/middleware/pagination.js +1 -1
- package/middleware/powered-by.js +1 -1
- package/middleware/rate-limiter.d.ts +0 -4
- package/middleware/rate-limiter.js +20 -7
- package/middleware/request-id.d.ts +1 -1
- package/middleware/request-id.js +3 -6
- package/middleware/sanitize-headers.d.ts +3 -11
- package/middleware/sanitize-headers.js +8 -40
- package/middleware/xss-protection.d.ts +1 -1
- package/middleware/xss-protection.js +1 -5
- package/package.json +6 -1
- package/registry/RadixRouter.js +72 -23
- package/types/index.d.ts +2 -1
- package/utils/cookie.js +1 -1
- package/utils/rateLimit.d.ts +1 -2
- package/utils/rateLimit.js +2 -2
- package/utils/regexRouter.js +1 -0
- package/utils/response.d.ts +12 -14
- package/utils/response.js +20 -29
- package/cjs/middleware/cache-control.js +0 -93
- package/cjs/middleware/detect-bot.js +0 -66
- package/cjs/middleware/detect-locale.js +0 -45
- package/cjs/middleware/i18n.js +0 -93
- package/cjs/middleware/lazy-loader.js +0 -74
- package/cjs/middleware/request-timeout.js +0 -43
- package/cjs/middleware/secure-headers.js +0 -43
- package/middleware/cache-control.d.ts +0 -56
- package/middleware/cache-control.js +0 -56
- package/middleware/detect-bot.d.ts +0 -111
- package/middleware/detect-bot.js +0 -62
- package/middleware/detect-locale.d.ts +0 -56
- package/middleware/detect-locale.js +0 -41
- package/middleware/i18n.d.ts +0 -102
- package/middleware/i18n.js +0 -89
- package/middleware/lazy-loader.d.ts +0 -73
- package/middleware/lazy-loader.js +0 -70
- package/middleware/request-timeout.d.ts +0 -26
- package/middleware/request-timeout.js +0 -39
- package/middleware/secure-headers.d.ts +0 -78
- package/middleware/secure-headers.js +0 -39
package/bun/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { loadEnv } from "../node/env.js";
|
|
2
2
|
import { serveStatic } from "../node/serveStatic.js";
|
|
3
3
|
import { getConnInfo } from "./getConnInfo.js";
|
|
4
|
-
import upgradeWebSocket from "./ws.js";
|
|
4
|
+
import upgradeWebSocket, { wsHandlers } from "./ws.js";
|
|
5
5
|
export type { ServeStatic, StaticFileArray, StaticServeOption, WebSocketCallback, WebSocketEvent, WebSocketOptions, } from "../types/index.js";
|
|
6
|
-
export {
|
|
6
|
+
export type { wsHandlersOptions } from "./ws.js";
|
|
7
|
+
export { getConnInfo, loadEnv, serveStatic, upgradeWebSocket, wsHandlers };
|
|
7
8
|
declare const _default: {
|
|
8
9
|
getConnInfo: typeof getConnInfo;
|
|
10
|
+
wsHandlers: (options?: import("./ws.js").wsHandlersOptions) => Bun.WebSocketHandler;
|
|
9
11
|
loadEnv: typeof loadEnv;
|
|
10
12
|
upgradeWebSocket: typeof upgradeWebSocket;
|
|
11
13
|
serveStatic: typeof serveStatic;
|
package/bun/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { loadEnv } from "../node/env.js";
|
|
2
2
|
import { serveStatic } from "../node/serveStatic.js";
|
|
3
3
|
import { getConnInfo } from "./getConnInfo.js";
|
|
4
|
-
import upgradeWebSocket from "./ws.js";
|
|
5
|
-
export { getConnInfo, loadEnv, serveStatic, upgradeWebSocket };
|
|
4
|
+
import upgradeWebSocket, { wsHandlers } from "./ws.js";
|
|
5
|
+
export { getConnInfo, loadEnv, serveStatic, upgradeWebSocket, wsHandlers };
|
|
6
6
|
export default {
|
|
7
7
|
getConnInfo,
|
|
8
|
+
wsHandlers,
|
|
8
9
|
loadEnv,
|
|
9
10
|
upgradeWebSocket,
|
|
10
11
|
serveStatic,
|
package/bun/ws.d.ts
CHANGED
|
@@ -8,4 +8,41 @@ import { Middleware, WebSocketCallback, WebSocketOptions } from "../types/index.
|
|
|
8
8
|
export declare function upgradeWebSocket<T extends Record<string, any> & {
|
|
9
9
|
wsProtocol: "wss" | "ws";
|
|
10
10
|
}, Path extends string = any>(callback: WebSocketCallback, options?: WebSocketOptions): Middleware<T, Path>;
|
|
11
|
+
export type wsHandlersOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Maximum payload size in bytes. Default: 16 MB.
|
|
14
|
+
*/
|
|
15
|
+
maxPayloadLength?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Maximum backpressure limit in bytes. Default: 16 MB.
|
|
18
|
+
*/
|
|
19
|
+
backpressureLimit?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Close connection when backpressure limit reached. Default: false.
|
|
22
|
+
*/
|
|
23
|
+
closeOnBackpressureLimit?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Idle timeout in seconds. Default: 120 (2 minutes).
|
|
26
|
+
*/
|
|
27
|
+
idleTimeout?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Whether ws.publish() sends message to itself if subscribed. Default: false.
|
|
30
|
+
*/
|
|
31
|
+
publishToSelf?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether the server sends/responds to pings automatically. Default: true.
|
|
34
|
+
*/
|
|
35
|
+
sendPings?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Compression settings for per-message deflate.
|
|
38
|
+
*/
|
|
39
|
+
perMessageDeflate?: boolean | {
|
|
40
|
+
compress?: Bun.WebSocketCompressor | boolean;
|
|
41
|
+
decompress?: Bun.WebSocketCompressor | boolean;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Returns a Bun-compatible WebSocketHandler with optional custom options.
|
|
46
|
+
*/
|
|
47
|
+
export declare const wsHandlers: (options?: wsHandlersOptions) => Bun.WebSocketHandler;
|
|
11
48
|
export default upgradeWebSocket;
|
package/bun/ws.js
CHANGED
|
@@ -31,4 +31,27 @@ export function upgradeWebSocket(callback, options = {}) {
|
|
|
31
31
|
return next();
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
+
export const wsHandlers = (options) => {
|
|
35
|
+
return {
|
|
36
|
+
open(ws) {
|
|
37
|
+
return ws.data?.open?.(ws);
|
|
38
|
+
},
|
|
39
|
+
message(ws, msg) {
|
|
40
|
+
return ws.data?.message?.(ws, msg);
|
|
41
|
+
},
|
|
42
|
+
close(ws, code, reason) {
|
|
43
|
+
return ws.data?.close?.(ws, { code, reason });
|
|
44
|
+
},
|
|
45
|
+
ping(ws, data) {
|
|
46
|
+
return ws.data?.ping?.(ws, data);
|
|
47
|
+
},
|
|
48
|
+
pong(ws, data) {
|
|
49
|
+
return ws.data?.pong?.(ws, data);
|
|
50
|
+
},
|
|
51
|
+
drain(ws) {
|
|
52
|
+
return ws.data?.drain?.(ws);
|
|
53
|
+
},
|
|
54
|
+
...options,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
34
57
|
export default upgradeWebSocket;
|
package/cjs/bun/index.js
CHANGED
|
@@ -1,19 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
5
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.upgradeWebSocket = exports.serveStatic = exports.loadEnv = exports.getConnInfo = void 0;
|
|
36
|
+
exports.wsHandlers = exports.upgradeWebSocket = exports.serveStatic = exports.loadEnv = exports.getConnInfo = void 0;
|
|
7
37
|
const env_js_1 = require("../node/env.js");
|
|
8
38
|
Object.defineProperty(exports, "loadEnv", { enumerable: true, get: function () { return env_js_1.loadEnv; } });
|
|
9
39
|
const serveStatic_js_1 = require("../node/serveStatic.js");
|
|
10
40
|
Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serveStatic_js_1.serveStatic; } });
|
|
11
41
|
const getConnInfo_js_1 = require("./getConnInfo.js");
|
|
12
42
|
Object.defineProperty(exports, "getConnInfo", { enumerable: true, get: function () { return getConnInfo_js_1.getConnInfo; } });
|
|
13
|
-
const ws_js_1 =
|
|
43
|
+
const ws_js_1 = __importStar(require("./ws.js"));
|
|
14
44
|
exports.upgradeWebSocket = ws_js_1.default;
|
|
45
|
+
Object.defineProperty(exports, "wsHandlers", { enumerable: true, get: function () { return ws_js_1.wsHandlers; } });
|
|
15
46
|
exports.default = {
|
|
16
47
|
getConnInfo: getConnInfo_js_1.getConnInfo,
|
|
48
|
+
wsHandlers: ws_js_1.wsHandlers,
|
|
17
49
|
loadEnv: env_js_1.loadEnv,
|
|
18
50
|
upgradeWebSocket: ws_js_1.default,
|
|
19
51
|
serveStatic: serveStatic_js_1.serveStatic,
|
package/cjs/bun/ws.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wsHandlers = void 0;
|
|
3
4
|
exports.upgradeWebSocket = upgradeWebSocket;
|
|
4
5
|
function upgradeWebSocket(callback, options = {}) {
|
|
5
6
|
const { onUpgradeError = (error, ctx) => {
|
|
@@ -34,4 +35,28 @@ function upgradeWebSocket(callback, options = {}) {
|
|
|
34
35
|
return next();
|
|
35
36
|
};
|
|
36
37
|
}
|
|
38
|
+
const wsHandlers = (options) => {
|
|
39
|
+
return {
|
|
40
|
+
open(ws) {
|
|
41
|
+
return ws.data?.open?.(ws);
|
|
42
|
+
},
|
|
43
|
+
message(ws, msg) {
|
|
44
|
+
return ws.data?.message?.(ws, msg);
|
|
45
|
+
},
|
|
46
|
+
close(ws, code, reason) {
|
|
47
|
+
return ws.data?.close?.(ws, { code, reason });
|
|
48
|
+
},
|
|
49
|
+
ping(ws, data) {
|
|
50
|
+
return ws.data?.ping?.(ws, data);
|
|
51
|
+
},
|
|
52
|
+
pong(ws, data) {
|
|
53
|
+
return ws.data?.pong?.(ws, data);
|
|
54
|
+
},
|
|
55
|
+
drain(ws) {
|
|
56
|
+
return ws.data?.drain?.(ws);
|
|
57
|
+
},
|
|
58
|
+
...options,
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
exports.wsHandlers = wsHandlers;
|
|
37
62
|
exports.default = upgradeWebSocket;
|
package/cjs/core/context.js
CHANGED
|
@@ -8,24 +8,24 @@ const response_js_1 = require("../utils/response.js");
|
|
|
8
8
|
const request_js_1 = require("./request.js");
|
|
9
9
|
class Context {
|
|
10
10
|
#status = 200;
|
|
11
|
-
#headers
|
|
11
|
+
#headers;
|
|
12
12
|
#req = null;
|
|
13
13
|
#params = {};
|
|
14
14
|
rawRequest;
|
|
15
15
|
#args;
|
|
16
16
|
#body;
|
|
17
|
+
url;
|
|
18
|
+
res;
|
|
17
19
|
env = {};
|
|
18
20
|
pathname;
|
|
19
21
|
method;
|
|
20
|
-
constructor(req,
|
|
21
|
-
this.#args =
|
|
22
|
+
constructor(req, pathname, method, env, args) {
|
|
23
|
+
this.#args = args;
|
|
22
24
|
this.rawRequest = req;
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
get url() {
|
|
28
|
-
return this.req.url;
|
|
25
|
+
this.url = req.url;
|
|
26
|
+
this.pathname = pathname;
|
|
27
|
+
this.env = env ?? {};
|
|
28
|
+
this.method = method;
|
|
29
29
|
}
|
|
30
30
|
get getStatus() {
|
|
31
31
|
return this.#status;
|
|
@@ -33,58 +33,28 @@ class Context {
|
|
|
33
33
|
set setStatus(code) {
|
|
34
34
|
this.#status = code;
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return this.#headers[header?.toLowerCase()];
|
|
39
|
-
}
|
|
40
|
-
return this.#headers;
|
|
41
|
-
}
|
|
42
|
-
set clearHeader(header) {
|
|
43
|
-
this.#headers = (header || {});
|
|
36
|
+
get headers() {
|
|
37
|
+
return this.res?.headers ?? (this.#headers ??= new Headers());
|
|
44
38
|
}
|
|
45
39
|
setHeader(key, value, options) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (!this.res) {
|
|
49
|
-
if (append && this.#headers[_key]) {
|
|
50
|
-
this.#headers[_key] += `, ${value}`;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
this.#headers[_key] = value;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
const resHeaders = this.res.headers;
|
|
58
|
-
if (append) {
|
|
59
|
-
resHeaders.append(_key, value);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
resHeaders.set(_key, value);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
deleteHeader(key) {
|
|
40
|
+
if (!value)
|
|
41
|
+
return this;
|
|
68
42
|
const _key = key.toLowerCase();
|
|
69
|
-
|
|
70
|
-
|
|
43
|
+
const append = options?.append || _key === "set-cookie";
|
|
44
|
+
const target = this.res?.headers ?? (this.#headers ??= new Headers());
|
|
45
|
+
if (append) {
|
|
46
|
+
target.append(_key, value);
|
|
71
47
|
}
|
|
72
48
|
else {
|
|
73
|
-
|
|
49
|
+
target.set(_key, value);
|
|
74
50
|
}
|
|
75
51
|
return this;
|
|
76
52
|
}
|
|
77
|
-
get params() {
|
|
78
|
-
return this.#params;
|
|
79
|
-
}
|
|
80
53
|
set params(params) {
|
|
81
54
|
this.#params = params;
|
|
82
55
|
}
|
|
83
56
|
get req() {
|
|
84
|
-
|
|
85
|
-
this.#req = new request_js_1.TezXRequest(this.rawRequest, this.method, this.pathname, this.#params);
|
|
86
|
-
}
|
|
87
|
-
return this.#req;
|
|
57
|
+
return (this.#req ??= new request_js_1.TezXRequest(this.rawRequest, this.method, this.pathname, this.#params));
|
|
88
58
|
}
|
|
89
59
|
get body() {
|
|
90
60
|
return this.#body;
|
|
@@ -96,66 +66,93 @@ class Context {
|
|
|
96
66
|
this.#status = status;
|
|
97
67
|
return this;
|
|
98
68
|
};
|
|
69
|
+
#newResponse(body, type, init = {}) {
|
|
70
|
+
const headers = new Headers(this.#headers);
|
|
71
|
+
headers.set("Content-Type", type);
|
|
72
|
+
if (init.headers) {
|
|
73
|
+
for (const key in init.headers) {
|
|
74
|
+
const value = init.headers[key];
|
|
75
|
+
if (!value)
|
|
76
|
+
continue;
|
|
77
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
78
|
+
headers.append(key, value);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
headers.set(key, value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return new Response(body, {
|
|
86
|
+
status: init.status ?? this.#status,
|
|
87
|
+
statusText: init.statusText,
|
|
88
|
+
headers,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
99
91
|
newResponse(body, init = {}) {
|
|
100
|
-
const headers =
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
92
|
+
const headers = new Headers(this.#headers);
|
|
93
|
+
if (init.headers) {
|
|
94
|
+
for (const key in init.headers) {
|
|
95
|
+
const value = init.headers[key];
|
|
96
|
+
if (!value) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
100
|
+
headers.append(key, value);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
headers.set(key, value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return new Response(body, {
|
|
108
|
+
status: init.status ?? this.#status,
|
|
109
|
+
statusText: init.statusText,
|
|
110
|
+
headers,
|
|
111
|
+
});
|
|
104
112
|
}
|
|
105
113
|
text(content, init) {
|
|
106
|
-
return
|
|
114
|
+
return this.#newResponse(content, "text/plain; charset=utf-8", init);
|
|
107
115
|
}
|
|
108
116
|
html(strings, ...args) {
|
|
109
|
-
|
|
110
|
-
if (Array.isArray(strings)) {
|
|
111
|
-
html = strings.reduce((result, str, i) => {
|
|
112
|
-
const value = args?.[i] ?? "";
|
|
113
|
-
return result + str + value;
|
|
114
|
-
}, "");
|
|
115
|
-
return (0, response_js_1.newResponse)(html, "text/html; charset=utf-8", {}, this.#headers, this.#status);
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
let init = args?.[0];
|
|
119
|
-
return (0, response_js_1.newResponse)(html, "text/html; charset=utf-8", init, this.#headers, this.#status);
|
|
120
|
-
}
|
|
117
|
+
return this.#newResponse((0, response_js_1.toString)(strings, args), "text/html; charset=utf-8", args?.[0]);
|
|
121
118
|
}
|
|
122
|
-
xml(
|
|
123
|
-
return (0, response_js_1.
|
|
119
|
+
xml(strings, ...args) {
|
|
120
|
+
return this.#newResponse((0, response_js_1.toString)(strings, args), "text/html; charset=utf-8", args?.[0]);
|
|
124
121
|
}
|
|
125
122
|
json(json, init) {
|
|
126
|
-
return
|
|
123
|
+
return this.#newResponse(JSON.stringify(json), "application/json; charset=utf-8", init);
|
|
127
124
|
}
|
|
128
125
|
send(body, init) {
|
|
129
126
|
let { body: _body, type } = (0, response_js_1.determineContentTypeBody)(body);
|
|
130
|
-
const contentType = init?.headers?.["Content-Type"]
|
|
131
|
-
init?.headers?.["content-type"]
|
|
127
|
+
const contentType = init?.headers?.["Content-Type"] ??
|
|
128
|
+
init?.headers?.["content-type"] ??
|
|
132
129
|
type;
|
|
133
|
-
return
|
|
130
|
+
return this.#newResponse(_body, contentType, init);
|
|
134
131
|
}
|
|
135
132
|
redirect(url, status = 302) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
});
|
|
133
|
+
const headers = new Headers(this.#headers);
|
|
134
|
+
headers.set("Location", url);
|
|
135
|
+
return new Response(null, { status, headers });
|
|
140
136
|
}
|
|
141
137
|
async download(filePath, filename) {
|
|
142
138
|
if (!(await (0, file_js_1.fileExists)(filePath)))
|
|
143
139
|
throw Error("File not found");
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
140
|
+
const buf = await (0, file_js_1.getFileBuffer)(filePath);
|
|
141
|
+
const headers = {
|
|
142
|
+
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
143
|
+
"Content-Length": buf.byteLength.toString(),
|
|
144
|
+
};
|
|
145
|
+
return this.#newResponse(buf, "application/octet-stream", {
|
|
146
|
+
status: this.#status,
|
|
147
|
+
headers,
|
|
148
|
+
});
|
|
152
149
|
}
|
|
153
150
|
async sendFile(filePath, init) {
|
|
154
151
|
if (!(await (0, file_js_1.fileExists)(filePath)))
|
|
155
152
|
throw Error("File not found");
|
|
156
153
|
let size = await (0, file_js_1.fileSize)(filePath);
|
|
157
|
-
const ext = (0, low_level_js_1.extensionExtract)(filePath)
|
|
158
|
-
const mimeType = mimeTypes_js_1.mimeTypes[ext]
|
|
154
|
+
const ext = (0, low_level_js_1.extensionExtract)(filePath);
|
|
155
|
+
const mimeType = mimeTypes_js_1.mimeTypes[ext] ?? mimeTypes_js_1.defaultMimeType;
|
|
159
156
|
let fileStream = await (0, file_js_1.readStream)(filePath);
|
|
160
157
|
let headers = {
|
|
161
158
|
"Content-Type": mimeType,
|
|
@@ -163,10 +160,11 @@ class Context {
|
|
|
163
160
|
...init?.headers,
|
|
164
161
|
};
|
|
165
162
|
let filename = init?.filename;
|
|
166
|
-
if (filename)
|
|
163
|
+
if (filename) {
|
|
167
164
|
headers["Content-Disposition"] = `attachment; filename="${filename}"`;
|
|
165
|
+
}
|
|
168
166
|
return this.newResponse(fileStream, {
|
|
169
|
-
status: init?.status
|
|
167
|
+
status: init?.status ?? this.#status,
|
|
170
168
|
statusText: init?.statusText,
|
|
171
169
|
headers,
|
|
172
170
|
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TezXError = void 0;
|
|
4
|
+
class TezXError extends Error {
|
|
5
|
+
statusCode;
|
|
6
|
+
details;
|
|
7
|
+
constructor(message, statusCode = 500, details) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.statusCode = statusCode;
|
|
10
|
+
this.details = details;
|
|
11
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
|
13
|
+
}
|
|
14
|
+
static badRequest(message = "Bad Request", details) {
|
|
15
|
+
return new TezXError(message, 400, details);
|
|
16
|
+
}
|
|
17
|
+
static unauthorized(message = "Unauthorized", details) {
|
|
18
|
+
return new TezXError(message, 401, details);
|
|
19
|
+
}
|
|
20
|
+
static forbidden(message = "Forbidden", details) {
|
|
21
|
+
return new TezXError(message, 403, details);
|
|
22
|
+
}
|
|
23
|
+
static notFound(message = "Resource Not Found", details) {
|
|
24
|
+
return new TezXError(message, 404, details);
|
|
25
|
+
}
|
|
26
|
+
static conflict(message = "Conflict", details) {
|
|
27
|
+
return new TezXError(message, 409, details);
|
|
28
|
+
}
|
|
29
|
+
static internal(message = "Internal Server Error", details) {
|
|
30
|
+
return new TezXError(message, 500, details);
|
|
31
|
+
}
|
|
32
|
+
toJSON() {
|
|
33
|
+
return {
|
|
34
|
+
error: true,
|
|
35
|
+
message: this.message,
|
|
36
|
+
statusCode: this.statusCode,
|
|
37
|
+
details: this.details ?? null,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.TezXError = TezXError;
|
package/cjs/core/request.js
CHANGED
|
@@ -17,22 +17,22 @@ class TezXRequest {
|
|
|
17
17
|
#headersCache;
|
|
18
18
|
#queryCache;
|
|
19
19
|
constructor(req, method, pathname, params) {
|
|
20
|
-
this.url = req.url
|
|
21
|
-
this.params = params
|
|
20
|
+
this.url = req.url;
|
|
21
|
+
this.params = params ?? {};
|
|
22
22
|
this.method = method;
|
|
23
23
|
this.#rawRequest = req;
|
|
24
|
-
this.pathname = pathname
|
|
24
|
+
this.pathname = pathname ?? "/";
|
|
25
25
|
}
|
|
26
26
|
header(header) {
|
|
27
27
|
if (header) {
|
|
28
|
-
return this.#rawRequest.headers.get(header
|
|
28
|
+
return this.#rawRequest.headers.get(header.toLowerCase());
|
|
29
29
|
}
|
|
30
30
|
if (this.#headersCache)
|
|
31
31
|
return this.#headersCache;
|
|
32
32
|
const obj = {};
|
|
33
|
-
|
|
33
|
+
this.#rawRequest.headers.forEach((value, key) => {
|
|
34
34
|
obj[key.toLowerCase()] = value;
|
|
35
|
-
}
|
|
35
|
+
});
|
|
36
36
|
this.#headersCache = obj;
|
|
37
37
|
return this.#headersCache;
|
|
38
38
|
}
|
package/cjs/core/router.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Router = void 0;
|
|
4
|
-
const low_level_js_1 = require("../utils/low-level.js");
|
|
5
4
|
const RadixRouter_js_1 = require("../registry/RadixRouter.js");
|
|
5
|
+
const low_level_js_1 = require("../utils/low-level.js");
|
|
6
6
|
class Router {
|
|
7
7
|
env = {};
|
|
8
8
|
router;
|
|
@@ -129,11 +129,9 @@ class Router {
|
|
|
129
129
|
}
|
|
130
130
|
return this;
|
|
131
131
|
}
|
|
132
|
-
#addRoute(method, path, handlers
|
|
132
|
+
#addRoute(method, path, handlers) {
|
|
133
133
|
let pattern = `/${(0, low_level_js_1.sanitizePathSplitBasePath)(this.basePath, path).join("/")}`;
|
|
134
|
-
|
|
135
|
-
this.router.addRoute(method, pattern, handlers);
|
|
136
|
-
}
|
|
134
|
+
this.router.addRoute(method, pattern, handlers);
|
|
137
135
|
this.route.push({
|
|
138
136
|
method: method,
|
|
139
137
|
pattern: pattern,
|
|
@@ -168,7 +166,9 @@ class Router {
|
|
|
168
166
|
}
|
|
169
167
|
#routeAddTriNode(path, router) {
|
|
170
168
|
this.env = { ...this.env, ...router.env };
|
|
171
|
-
if (this.router?.name &&
|
|
169
|
+
if (this.router?.name &&
|
|
170
|
+
router.router?.name &&
|
|
171
|
+
this.router?.name !== router.router?.name) {
|
|
172
172
|
throw new Error(`Router name mismatch: expected "${this.router.name}", got "${router.router.name}"`);
|
|
173
173
|
}
|
|
174
174
|
if (!(router instanceof Router)) {
|