tezx 1.0.48 → 1.0.49
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/adapter/index.d.ts +2 -1
- package/adapter/index.js +22 -10
- package/cjs/adapter/index.js +25 -13
- package/cjs/core/MiddlewareConfigure.js +7 -7
- package/cjs/core/common.js +3 -3
- package/cjs/core/config.js +8 -7
- package/cjs/core/context.js +14 -16
- package/cjs/core/environment.js +0 -11
- package/cjs/core/request.js +26 -18
- package/cjs/core/router.js +19 -19
- package/cjs/core/server.js +18 -18
- package/cjs/helper/env-parser.js +2 -2
- package/cjs/index.js +5 -5
- package/cjs/middleware/basicAuth.js +7 -7
- package/cjs/middleware/detectBot.js +2 -2
- package/cjs/middleware/detectLocale.js +2 -2
- package/cjs/middleware/i18nMiddleware.js +3 -3
- package/cjs/middleware/lazyLoadModules.js +5 -5
- package/cjs/middleware/logger.js +5 -5
- package/cjs/middleware/rateLimiter.js +3 -3
- package/cjs/middleware/request-id.js +2 -2
- package/cjs/middleware/sanitizeHeader.js +5 -5
- package/cjs/middleware/xssProtection.js +4 -4
- package/cjs/utils/debugging.js +7 -7
- package/cjs/utils/formData.js +5 -5
- package/cjs/utils/staticFile.js +2 -2
- package/core/MiddlewareConfigure.d.ts +2 -2
- package/core/MiddlewareConfigure.js +3 -3
- package/core/common.d.ts +2 -2
- package/core/common.js +1 -1
- package/core/config.d.ts +4 -2
- package/core/config.js +3 -2
- package/core/context.d.ts +5 -4
- package/core/context.js +9 -11
- package/core/environment.d.ts +1 -2
- package/core/environment.js +0 -11
- package/core/request.d.ts +3 -2
- package/core/request.js +17 -9
- package/core/router.d.ts +3 -3
- package/core/router.js +4 -4
- package/core/server.d.ts +6 -3
- package/core/server.js +9 -9
- package/helper/env-parser.js +1 -1
- package/index.d.ts +7 -7
- package/index.js +3 -3
- package/middleware/basicAuth.d.ts +3 -3
- package/middleware/basicAuth.js +3 -3
- package/middleware/cors.d.ts +1 -1
- package/middleware/detectBot.d.ts +2 -2
- package/middleware/detectBot.js +1 -1
- package/middleware/detectLocale.d.ts +1 -1
- package/middleware/detectLocale.js +1 -1
- package/middleware/i18nMiddleware.d.ts +1 -1
- package/middleware/i18nMiddleware.js +1 -1
- package/middleware/lazyLoadModules.d.ts +1 -1
- package/middleware/lazyLoadModules.js +1 -1
- package/middleware/logger.d.ts +1 -1
- package/middleware/logger.js +1 -1
- package/middleware/pagination.d.ts +2 -2
- package/middleware/powered-by.d.ts +1 -1
- package/middleware/rateLimiter.d.ts +2 -2
- package/middleware/rateLimiter.js +1 -1
- package/middleware/request-id.d.ts +1 -1
- package/middleware/request-id.js +1 -1
- package/middleware/sanitizeHeader.d.ts +1 -1
- package/middleware/sanitizeHeader.js +1 -1
- package/middleware/secureHeaders.d.ts +2 -2
- package/middleware/xssProtection.d.ts +2 -2
- package/middleware/xssProtection.js +1 -1
- package/package.json +1 -1
- package/utils/debugging.js +1 -1
- package/utils/formData.d.ts +1 -1
- package/utils/formData.js +1 -1
- package/utils/staticFile.d.ts +1 -1
- package/utils/staticFile.js +1 -1
package/adapter/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TezX } from "../core/server";
|
|
1
|
+
import { TezX } from "../core/server.js";
|
|
2
|
+
export type AdapterType = "bun" | "deno" | "node";
|
|
2
3
|
export declare function denoAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>): {
|
|
3
4
|
listen: (port: number, callback?: (message: string) => void) => any;
|
|
4
5
|
};
|
package/adapter/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GlobalConfig } from "../core/config";
|
|
1
|
+
import { GlobalConfig } from "../core/config.js";
|
|
2
2
|
export function denoAdapter(TezX) {
|
|
3
3
|
function listen(port, callback) {
|
|
4
4
|
const isDeno = typeof Deno !== "undefined";
|
|
@@ -20,7 +20,10 @@ export function denoAdapter(TezX) {
|
|
|
20
20
|
family: localAddr?.family,
|
|
21
21
|
},
|
|
22
22
|
};
|
|
23
|
-
|
|
23
|
+
let options = {
|
|
24
|
+
connInfo: address,
|
|
25
|
+
};
|
|
26
|
+
const response = await TezX.serve(req, options);
|
|
24
27
|
if (response instanceof Response) {
|
|
25
28
|
return response;
|
|
26
29
|
}
|
|
@@ -33,10 +36,11 @@ export function denoAdapter(TezX) {
|
|
|
33
36
|
}
|
|
34
37
|
}
|
|
35
38
|
const server = isDeno ? Deno.serve({ port }, handleRequest) : null;
|
|
36
|
-
GlobalConfig.serverInfo = server;
|
|
37
39
|
if (!server) {
|
|
38
40
|
throw new Error("Deno is not find");
|
|
39
41
|
}
|
|
42
|
+
GlobalConfig.adapter = "deno";
|
|
43
|
+
GlobalConfig.server = server;
|
|
40
44
|
const protocol = "\x1b[1;34mhttp\x1b[0m";
|
|
41
45
|
const message = `\x1b[1m🚀 Deno TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
|
|
42
46
|
if (typeof callback === "function") {
|
|
@@ -62,13 +66,17 @@ export function bunAdapter(TezX) {
|
|
|
62
66
|
if (!serve) {
|
|
63
67
|
throw new Error("Bun is not find");
|
|
64
68
|
}
|
|
69
|
+
GlobalConfig.adapter = "bun";
|
|
65
70
|
const server = serve({
|
|
66
71
|
port: port,
|
|
67
72
|
async fetch(req) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
let options = {
|
|
74
|
+
connInfo: {
|
|
75
|
+
remoteAddr: server.requestIP(req),
|
|
76
|
+
localAddr: server.address,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
const response = await TezX.serve(req, options);
|
|
72
80
|
if (response instanceof Response) {
|
|
73
81
|
return response;
|
|
74
82
|
}
|
|
@@ -81,7 +89,7 @@ export function bunAdapter(TezX) {
|
|
|
81
89
|
}
|
|
82
90
|
},
|
|
83
91
|
});
|
|
84
|
-
GlobalConfig.
|
|
92
|
+
GlobalConfig.server = server;
|
|
85
93
|
const protocol = "\x1b[1;34mhttp\x1b[0m";
|
|
86
94
|
const message = `\x1b[1m Bun TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
|
|
87
95
|
if (typeof callback == "function") {
|
|
@@ -104,6 +112,7 @@ export function nodeAdapter(TezX) {
|
|
|
104
112
|
function listen(port, callback) {
|
|
105
113
|
import("http")
|
|
106
114
|
.then((r) => {
|
|
115
|
+
GlobalConfig.adapter = "node";
|
|
107
116
|
let server = r.createServer(async (req, res) => {
|
|
108
117
|
let address = {};
|
|
109
118
|
if (req.socket) {
|
|
@@ -120,7 +129,10 @@ export function nodeAdapter(TezX) {
|
|
|
120
129
|
},
|
|
121
130
|
};
|
|
122
131
|
}
|
|
123
|
-
|
|
132
|
+
let options = {
|
|
133
|
+
connInfo: address,
|
|
134
|
+
};
|
|
135
|
+
const response = await TezX.serve(req, options);
|
|
124
136
|
const statusText = response?.statusText;
|
|
125
137
|
if (!(response instanceof Response)) {
|
|
126
138
|
throw new Error("Invalid response from TezX.serve");
|
|
@@ -145,7 +157,7 @@ export function nodeAdapter(TezX) {
|
|
|
145
157
|
server.listen(port, () => {
|
|
146
158
|
const protocol = "\x1b[1;34mhttp\x1b[0m";
|
|
147
159
|
const message = `\x1b[1m NodeJS TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
|
|
148
|
-
GlobalConfig.
|
|
160
|
+
GlobalConfig.server = server;
|
|
149
161
|
if (typeof callback == "function") {
|
|
150
162
|
callback(message);
|
|
151
163
|
}
|
package/cjs/adapter/index.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.denoAdapter = denoAdapter;
|
|
4
4
|
exports.bunAdapter = bunAdapter;
|
|
5
5
|
exports.nodeAdapter = nodeAdapter;
|
|
6
|
-
const
|
|
6
|
+
const config_js_1 = require("../core/config.js");
|
|
7
7
|
function denoAdapter(TezX) {
|
|
8
8
|
function listen(port, callback) {
|
|
9
9
|
const isDeno = typeof Deno !== "undefined";
|
|
@@ -25,7 +25,10 @@ function denoAdapter(TezX) {
|
|
|
25
25
|
family: localAddr?.family,
|
|
26
26
|
},
|
|
27
27
|
};
|
|
28
|
-
|
|
28
|
+
let options = {
|
|
29
|
+
connInfo: address,
|
|
30
|
+
};
|
|
31
|
+
const response = await TezX.serve(req, options);
|
|
29
32
|
if (response instanceof Response) {
|
|
30
33
|
return response;
|
|
31
34
|
}
|
|
@@ -38,17 +41,18 @@ function denoAdapter(TezX) {
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
const server = isDeno ? Deno.serve({ port }, handleRequest) : null;
|
|
41
|
-
config_1.GlobalConfig.serverInfo = server;
|
|
42
44
|
if (!server) {
|
|
43
45
|
throw new Error("Deno is not find");
|
|
44
46
|
}
|
|
47
|
+
config_js_1.GlobalConfig.adapter = "deno";
|
|
48
|
+
config_js_1.GlobalConfig.server = server;
|
|
45
49
|
const protocol = "\x1b[1;34mhttp\x1b[0m";
|
|
46
50
|
const message = `\x1b[1m🚀 Deno TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
|
|
47
51
|
if (typeof callback === "function") {
|
|
48
52
|
callback(message);
|
|
49
53
|
}
|
|
50
54
|
else {
|
|
51
|
-
|
|
55
|
+
config_js_1.GlobalConfig.debugging.success(message);
|
|
52
56
|
}
|
|
53
57
|
return server;
|
|
54
58
|
}
|
|
@@ -67,13 +71,17 @@ function bunAdapter(TezX) {
|
|
|
67
71
|
if (!serve) {
|
|
68
72
|
throw new Error("Bun is not find");
|
|
69
73
|
}
|
|
74
|
+
config_js_1.GlobalConfig.adapter = "bun";
|
|
70
75
|
const server = serve({
|
|
71
76
|
port: port,
|
|
72
77
|
async fetch(req) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
let options = {
|
|
79
|
+
connInfo: {
|
|
80
|
+
remoteAddr: server.requestIP(req),
|
|
81
|
+
localAddr: server.address,
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
const response = await TezX.serve(req, options);
|
|
77
85
|
if (response instanceof Response) {
|
|
78
86
|
return response;
|
|
79
87
|
}
|
|
@@ -86,14 +94,14 @@ function bunAdapter(TezX) {
|
|
|
86
94
|
}
|
|
87
95
|
},
|
|
88
96
|
});
|
|
89
|
-
|
|
97
|
+
config_js_1.GlobalConfig.server = server;
|
|
90
98
|
const protocol = "\x1b[1;34mhttp\x1b[0m";
|
|
91
99
|
const message = `\x1b[1m Bun TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
|
|
92
100
|
if (typeof callback == "function") {
|
|
93
101
|
callback(message);
|
|
94
102
|
}
|
|
95
103
|
else {
|
|
96
|
-
|
|
104
|
+
config_js_1.GlobalConfig.debugging.success(message);
|
|
97
105
|
}
|
|
98
106
|
return server;
|
|
99
107
|
}
|
|
@@ -108,6 +116,7 @@ function bunAdapter(TezX) {
|
|
|
108
116
|
function nodeAdapter(TezX) {
|
|
109
117
|
function listen(port, callback) {
|
|
110
118
|
Promise.resolve().then(() => require("http")).then((r) => {
|
|
119
|
+
config_js_1.GlobalConfig.adapter = "node";
|
|
111
120
|
let server = r.createServer(async (req, res) => {
|
|
112
121
|
let address = {};
|
|
113
122
|
if (req.socket) {
|
|
@@ -124,7 +133,10 @@ function nodeAdapter(TezX) {
|
|
|
124
133
|
},
|
|
125
134
|
};
|
|
126
135
|
}
|
|
127
|
-
|
|
136
|
+
let options = {
|
|
137
|
+
connInfo: address,
|
|
138
|
+
};
|
|
139
|
+
const response = await TezX.serve(req, options);
|
|
128
140
|
const statusText = response?.statusText;
|
|
129
141
|
if (!(response instanceof Response)) {
|
|
130
142
|
throw new Error("Invalid response from TezX.serve");
|
|
@@ -149,12 +161,12 @@ function nodeAdapter(TezX) {
|
|
|
149
161
|
server.listen(port, () => {
|
|
150
162
|
const protocol = "\x1b[1;34mhttp\x1b[0m";
|
|
151
163
|
const message = `\x1b[1m NodeJS TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
|
|
152
|
-
|
|
164
|
+
config_js_1.GlobalConfig.server = server;
|
|
153
165
|
if (typeof callback == "function") {
|
|
154
166
|
callback(message);
|
|
155
167
|
}
|
|
156
168
|
else {
|
|
157
|
-
|
|
169
|
+
config_js_1.GlobalConfig.debugging.success(message);
|
|
158
170
|
}
|
|
159
171
|
return server;
|
|
160
172
|
});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TriMiddleware = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
const url_js_1 = require("../utils/url.js");
|
|
5
|
+
const common_js_1 = require("./common.js");
|
|
6
|
+
const config_js_1 = require("./config.js");
|
|
7
7
|
class TriMiddleware {
|
|
8
8
|
children = new Map();
|
|
9
9
|
middlewares = new Set();
|
|
@@ -11,7 +11,7 @@ class TriMiddleware {
|
|
|
11
11
|
pathname;
|
|
12
12
|
constructor(pathname = "/") {
|
|
13
13
|
this.pathname = pathname;
|
|
14
|
-
if (
|
|
14
|
+
if (config_js_1.GlobalConfig.allowDuplicateMw) {
|
|
15
15
|
this.middlewares = [];
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
@@ -20,7 +20,7 @@ class TriMiddleware {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
exports.TriMiddleware = TriMiddleware;
|
|
23
|
-
class MiddlewareConfigure extends
|
|
23
|
+
class MiddlewareConfigure extends common_js_1.CommonHandler {
|
|
24
24
|
triMiddlewares = new TriMiddleware();
|
|
25
25
|
basePath;
|
|
26
26
|
constructor(basePath = "/") {
|
|
@@ -28,7 +28,7 @@ class MiddlewareConfigure extends common_1.CommonHandler {
|
|
|
28
28
|
this.basePath = basePath;
|
|
29
29
|
}
|
|
30
30
|
addMiddleware(pathname, middlewares) {
|
|
31
|
-
const parts = (0,
|
|
31
|
+
const parts = (0, url_js_1.sanitizePathSplit)(this.basePath, pathname);
|
|
32
32
|
let node = this.triMiddlewares;
|
|
33
33
|
for (const part of parts) {
|
|
34
34
|
if (part.startsWith("*")) {
|
|
@@ -55,7 +55,7 @@ class MiddlewareConfigure extends common_1.CommonHandler {
|
|
|
55
55
|
node = node.children.get(part);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
if (
|
|
58
|
+
if (config_js_1.GlobalConfig.allowDuplicateMw) {
|
|
59
59
|
node.middlewares.push(...middlewares);
|
|
60
60
|
}
|
|
61
61
|
else {
|
package/cjs/core/common.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CommonHandler = void 0;
|
|
4
|
-
const
|
|
4
|
+
const config_js_1 = require("./config.js");
|
|
5
5
|
class CommonHandler {
|
|
6
6
|
notFound(callback) {
|
|
7
|
-
|
|
7
|
+
config_js_1.GlobalConfig.notFound = callback;
|
|
8
8
|
return this;
|
|
9
9
|
}
|
|
10
10
|
onError(callback) {
|
|
11
|
-
|
|
11
|
+
config_js_1.GlobalConfig.onError = callback;
|
|
12
12
|
return this;
|
|
13
13
|
}
|
|
14
14
|
}
|
package/cjs/core/config.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GlobalConfig = void 0;
|
|
4
|
-
const
|
|
4
|
+
const debugging_js_1 = require("../utils/debugging.js");
|
|
5
5
|
let GlobalConfig = class {
|
|
6
6
|
static notFound = (ctx) => {
|
|
7
7
|
const { method, urlRef: { pathname }, } = ctx.req;
|
|
@@ -13,15 +13,16 @@ let GlobalConfig = class {
|
|
|
13
13
|
static allowDuplicateMw = false;
|
|
14
14
|
static overwriteMethod = true;
|
|
15
15
|
static debugMode = false;
|
|
16
|
-
static
|
|
16
|
+
static server;
|
|
17
|
+
static adapter;
|
|
17
18
|
static get debugging() {
|
|
18
19
|
return this.debugMode
|
|
19
20
|
? {
|
|
20
|
-
info: (msg, ...args) => (0,
|
|
21
|
-
warn: (msg, ...args) => (0,
|
|
22
|
-
error: (msg, ...args) => (0,
|
|
23
|
-
debug: (msg, ...args) => (0,
|
|
24
|
-
success: (msg, ...args) => (0,
|
|
21
|
+
info: (msg, ...args) => (0, debugging_js_1.loggerOutput)("info", msg, ...args),
|
|
22
|
+
warn: (msg, ...args) => (0, debugging_js_1.loggerOutput)("warn", msg, ...args),
|
|
23
|
+
error: (msg, ...args) => (0, debugging_js_1.loggerOutput)("error", msg, ...args),
|
|
24
|
+
debug: (msg, ...args) => (0, debugging_js_1.loggerOutput)("debug", msg, ...args),
|
|
25
|
+
success: (msg, ...args) => (0, debugging_js_1.loggerOutput)("success", msg, ...args),
|
|
25
26
|
}
|
|
26
27
|
: {
|
|
27
28
|
info: (msg, ...args) => { },
|
package/cjs/core/context.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Context = exports.httpStatusMap = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
4
|
+
const state_js_1 = require("../utils/state.js");
|
|
5
|
+
const staticFile_js_1 = require("../utils/staticFile.js");
|
|
6
|
+
const environment_js_1 = require("./environment.js");
|
|
7
|
+
const header_js_1 = require("./header.js");
|
|
8
|
+
const request_js_1 = require("./request.js");
|
|
9
9
|
exports.httpStatusMap = {
|
|
10
10
|
100: "Continue",
|
|
11
11
|
101: "Switching Protocols",
|
|
@@ -74,21 +74,19 @@ exports.httpStatusMap = {
|
|
|
74
74
|
class Context {
|
|
75
75
|
#rawRequest;
|
|
76
76
|
env = {};
|
|
77
|
-
headers = new
|
|
77
|
+
headers = new header_js_1.HeadersParser();
|
|
78
78
|
pathname;
|
|
79
79
|
url;
|
|
80
80
|
method;
|
|
81
81
|
#status = 200;
|
|
82
|
-
state = new
|
|
82
|
+
state = new state_js_1.State();
|
|
83
83
|
#params = {};
|
|
84
84
|
resBody;
|
|
85
85
|
#body;
|
|
86
|
-
#
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
#options;
|
|
87
|
+
constructor(req, options) {
|
|
88
|
+
this.#options = options;
|
|
89
89
|
this.#rawRequest = req;
|
|
90
|
-
this.#remoteAddress = connInfo.remoteAddr;
|
|
91
|
-
this.#localAddress = connInfo.localAddr;
|
|
92
90
|
this.method = req?.method?.toUpperCase();
|
|
93
91
|
this.pathname = this.req.urlRef.pathname;
|
|
94
92
|
this.url = this.req.url;
|
|
@@ -266,7 +264,7 @@ class Context {
|
|
|
266
264
|
async download(filePath, fileName) {
|
|
267
265
|
try {
|
|
268
266
|
let fileExists = false;
|
|
269
|
-
const runtime =
|
|
267
|
+
const runtime = environment_js_1.EnvironmentDetector.getEnvironment;
|
|
270
268
|
if (runtime === "node") {
|
|
271
269
|
const { existsSync } = await Promise.resolve().then(() => require("fs"));
|
|
272
270
|
fileExists = existsSync(filePath);
|
|
@@ -314,7 +312,7 @@ class Context {
|
|
|
314
312
|
}
|
|
315
313
|
async sendFile(filePath, ...args) {
|
|
316
314
|
try {
|
|
317
|
-
const runtime =
|
|
315
|
+
const runtime = environment_js_1.EnvironmentDetector.getEnvironment;
|
|
318
316
|
const resolvedPath = filePath;
|
|
319
317
|
let fileExists = false;
|
|
320
318
|
if (runtime === "node") {
|
|
@@ -349,7 +347,7 @@ class Context {
|
|
|
349
347
|
fileSize = fileInfo.size;
|
|
350
348
|
}
|
|
351
349
|
const ext = filePath.split(".").pop()?.toLowerCase() || "";
|
|
352
|
-
const mimeType =
|
|
350
|
+
const mimeType = staticFile_js_1.mimeTypes[ext] || staticFile_js_1.defaultMimeType;
|
|
353
351
|
let fileStream;
|
|
354
352
|
if (runtime === "node") {
|
|
355
353
|
const { createReadStream } = await Promise.resolve().then(() => require("fs"));
|
|
@@ -397,7 +395,7 @@ class Context {
|
|
|
397
395
|
return response;
|
|
398
396
|
}
|
|
399
397
|
get req() {
|
|
400
|
-
return new
|
|
398
|
+
return new request_js_1.Request(this.#rawRequest, this.params, this.#options);
|
|
401
399
|
}
|
|
402
400
|
set params(params) {
|
|
403
401
|
this.#params = params;
|
package/cjs/core/environment.js
CHANGED
|
@@ -11,17 +11,6 @@ class EnvironmentDetector {
|
|
|
11
11
|
return "node";
|
|
12
12
|
return "unknown";
|
|
13
13
|
}
|
|
14
|
-
static detectProtocol(req) {
|
|
15
|
-
try {
|
|
16
|
-
if (this.getEnvironment === "node") {
|
|
17
|
-
return req?.socket?.encrypted ? "https" : "http";
|
|
18
|
-
}
|
|
19
|
-
return "unknown";
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
throw new Error("Failed to detect protocol.");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
14
|
static getHost(headers) {
|
|
26
15
|
try {
|
|
27
16
|
return headers?.get("host") || "unknown";
|
package/cjs/core/request.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Request = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
4
|
+
const formData_js_1 = require("../utils/formData.js");
|
|
5
|
+
const url_js_1 = require("../utils/url.js");
|
|
6
|
+
const config_js_1 = require("./config.js");
|
|
7
|
+
const environment_js_1 = require("./environment.js");
|
|
8
|
+
const header_js_1 = require("./header.js");
|
|
8
9
|
class Request {
|
|
9
|
-
#headers = new
|
|
10
|
+
#headers = new header_js_1.HeadersParser();
|
|
10
11
|
url;
|
|
11
12
|
method;
|
|
12
13
|
urlRef = {
|
|
@@ -25,21 +26,28 @@ class Request {
|
|
|
25
26
|
rawRequest;
|
|
26
27
|
params = {};
|
|
27
28
|
remoteAddress = {};
|
|
28
|
-
constructor(req, params,
|
|
29
|
-
this.remoteAddress =
|
|
30
|
-
this.#headers = new
|
|
29
|
+
constructor(req, params, options) {
|
|
30
|
+
this.remoteAddress = options?.connInfo?.remoteAddr;
|
|
31
|
+
this.#headers = new header_js_1.HeadersParser(req?.headers);
|
|
31
32
|
this.method = req?.method?.toUpperCase();
|
|
32
33
|
this.params = params;
|
|
33
34
|
this.rawRequest = req;
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
if (environment_js_1.EnvironmentDetector.getEnvironment == "node" ||
|
|
36
|
+
config_js_1.GlobalConfig.adapter == "node") {
|
|
37
|
+
let encrypted = req?.socket?.encrypted;
|
|
38
|
+
const protocol = typeof encrypted === "boolean"
|
|
39
|
+
? encrypted
|
|
40
|
+
? "https"
|
|
41
|
+
: "http"
|
|
42
|
+
: "http";
|
|
43
|
+
const host = environment_js_1.EnvironmentDetector.getHost(this.#headers);
|
|
44
|
+
const path = req.url || "/";
|
|
45
|
+
this.url = `${protocol}://${host}${path}`;
|
|
38
46
|
}
|
|
39
47
|
else {
|
|
40
48
|
this.url = req.url;
|
|
41
49
|
}
|
|
42
|
-
this.urlRef = (0,
|
|
50
|
+
this.urlRef = (0, url_js_1.urlParse)(this.url);
|
|
43
51
|
this.query = this.urlRef.query;
|
|
44
52
|
}
|
|
45
53
|
get headers() {
|
|
@@ -72,12 +80,12 @@ class Request {
|
|
|
72
80
|
};
|
|
73
81
|
}
|
|
74
82
|
async text() {
|
|
75
|
-
return await (0,
|
|
83
|
+
return await (0, formData_js_1.parseTextBody)(this.rawRequest);
|
|
76
84
|
}
|
|
77
85
|
async json() {
|
|
78
86
|
const contentType = this.#headers.get("content-type") || "";
|
|
79
87
|
if (contentType.includes("application/json")) {
|
|
80
|
-
return await (0,
|
|
88
|
+
return await (0, formData_js_1.parseJsonBody)(this.rawRequest);
|
|
81
89
|
}
|
|
82
90
|
else {
|
|
83
91
|
return {};
|
|
@@ -89,17 +97,17 @@ class Request {
|
|
|
89
97
|
throw Error("Invalid Content-Type");
|
|
90
98
|
}
|
|
91
99
|
if (contentType.includes("application/json")) {
|
|
92
|
-
return await (0,
|
|
100
|
+
return await (0, formData_js_1.parseJsonBody)(this.rawRequest);
|
|
93
101
|
}
|
|
94
102
|
else if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
95
|
-
return (0,
|
|
103
|
+
return (0, formData_js_1.parseUrlEncodedBody)(this.rawRequest);
|
|
96
104
|
}
|
|
97
105
|
else if (contentType.includes("multipart/form-data")) {
|
|
98
106
|
const boundary = contentType?.split("; ")?.[1]?.split("=")?.[1];
|
|
99
107
|
if (!boundary) {
|
|
100
108
|
throw Error("Boundary not found");
|
|
101
109
|
}
|
|
102
|
-
return await (0,
|
|
110
|
+
return await (0, formData_js_1.parseMultipartBody)(this.rawRequest, boundary, options);
|
|
103
111
|
}
|
|
104
112
|
else {
|
|
105
113
|
return {};
|
package/cjs/core/router.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Router = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
4
|
+
const staticFile_js_1 = require("../utils/staticFile.js");
|
|
5
|
+
const url_js_1 = require("../utils/url.js");
|
|
6
|
+
const config_js_1 = require("./config.js");
|
|
7
|
+
const MiddlewareConfigure_js_1 = require("./MiddlewareConfigure.js");
|
|
8
8
|
class TrieRouter {
|
|
9
9
|
children = new Map();
|
|
10
10
|
handlers = new Map();
|
|
@@ -16,7 +16,7 @@ class TrieRouter {
|
|
|
16
16
|
this.pathname = pathname;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
class Router extends
|
|
19
|
+
class Router extends MiddlewareConfigure_js_1.default {
|
|
20
20
|
routers = new Map();
|
|
21
21
|
env = {};
|
|
22
22
|
triRouter;
|
|
@@ -56,7 +56,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
56
56
|
default:
|
|
57
57
|
throw new Error(`\x1b[1;31m404 Not Found\x1b[0m \x1b[1;32mInvalid arguments\x1b[0m`);
|
|
58
58
|
}
|
|
59
|
-
(0,
|
|
59
|
+
(0, staticFile_js_1.getFiles)(dir, route, this, options);
|
|
60
60
|
return this;
|
|
61
61
|
}
|
|
62
62
|
get(path, ...args) {
|
|
@@ -173,9 +173,9 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
173
173
|
this.#addRoute(method, path, callback, middlewares);
|
|
174
174
|
}
|
|
175
175
|
#addRoute(method, path, callback, middlewares) {
|
|
176
|
-
const parts = (0,
|
|
176
|
+
const parts = (0, url_js_1.sanitizePathSplit)(this.basePath, path);
|
|
177
177
|
let finalMiddleware = middlewares;
|
|
178
|
-
if (!
|
|
178
|
+
if (!config_js_1.GlobalConfig.allowDuplicateMw) {
|
|
179
179
|
finalMiddleware = new Set(middlewares);
|
|
180
180
|
}
|
|
181
181
|
let p = parts.join("/");
|
|
@@ -189,7 +189,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
189
189
|
});
|
|
190
190
|
return this.routers.set(p, handler);
|
|
191
191
|
}
|
|
192
|
-
if (!
|
|
192
|
+
if (!config_js_1.GlobalConfig.overwriteMethod && handler.has(method))
|
|
193
193
|
return;
|
|
194
194
|
return handler.set(method, { callback, middlewares: finalMiddleware });
|
|
195
195
|
}
|
|
@@ -212,7 +212,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
212
212
|
node = node.children.get(part);
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
|
-
if (!
|
|
215
|
+
if (!config_js_1.GlobalConfig.overwriteMethod && node.handlers.has(method))
|
|
216
216
|
return;
|
|
217
217
|
node.handlers.set(method, {
|
|
218
218
|
callback: callback,
|
|
@@ -228,14 +228,14 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
228
228
|
if (!(router instanceof Router)) {
|
|
229
229
|
throw new Error("Router instance is required.");
|
|
230
230
|
}
|
|
231
|
-
const parts = (0,
|
|
231
|
+
const parts = (0, url_js_1.sanitizePathSplit)(this.basePath, path);
|
|
232
232
|
if (router.routers.size) {
|
|
233
233
|
for (const [segment, handlers] of router.routers) {
|
|
234
234
|
let path = parts.length ? parts.join("/") + "/" + segment : segment;
|
|
235
235
|
if (this.routers.has(path)) {
|
|
236
236
|
const baseRouter = this.routers.get(path);
|
|
237
237
|
for (const [method, handler] of handlers) {
|
|
238
|
-
if (!
|
|
238
|
+
if (!config_js_1.GlobalConfig.overwriteMethod && baseRouter.has(method))
|
|
239
239
|
return;
|
|
240
240
|
baseRouter.set(method, handler);
|
|
241
241
|
}
|
|
@@ -272,7 +272,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
272
272
|
for (const part of parts) {
|
|
273
273
|
if (part.startsWith("*")) {
|
|
274
274
|
if (!rootMiddlewares.children.has("*")) {
|
|
275
|
-
rootMiddlewares.children.set("*", new
|
|
275
|
+
rootMiddlewares.children.set("*", new MiddlewareConfigure_js_1.TriMiddleware());
|
|
276
276
|
}
|
|
277
277
|
rootMiddlewares = rootMiddlewares.children.get("*");
|
|
278
278
|
}
|
|
@@ -283,13 +283,13 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
283
283
|
continue;
|
|
284
284
|
}
|
|
285
285
|
if (!rootMiddlewares.children.has(":")) {
|
|
286
|
-
rootMiddlewares.children.set(":", new
|
|
286
|
+
rootMiddlewares.children.set(":", new MiddlewareConfigure_js_1.TriMiddleware());
|
|
287
287
|
}
|
|
288
288
|
rootMiddlewares = rootMiddlewares.children.get(":");
|
|
289
289
|
}
|
|
290
290
|
else {
|
|
291
291
|
if (!rootMiddlewares.children.has(part)) {
|
|
292
|
-
rootMiddlewares.children.set(part, new
|
|
292
|
+
rootMiddlewares.children.set(part, new MiddlewareConfigure_js_1.TriMiddleware());
|
|
293
293
|
}
|
|
294
294
|
rootMiddlewares = rootMiddlewares.children.get(part);
|
|
295
295
|
}
|
|
@@ -306,7 +306,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
306
306
|
if (rtN.children.has(pathSegment)) {
|
|
307
307
|
let findNode = rtN.children.get(pathSegment);
|
|
308
308
|
for (const [method, handlers] of subRouter.handlers) {
|
|
309
|
-
if (!
|
|
309
|
+
if (!config_js_1.GlobalConfig.overwriteMethod && node.handlers.has(method))
|
|
310
310
|
return;
|
|
311
311
|
findNode.handlers.set(method, handlers);
|
|
312
312
|
}
|
|
@@ -322,7 +322,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
322
322
|
let routerNode = router.triRouter;
|
|
323
323
|
const routerMiddlewares = router.triMiddlewares;
|
|
324
324
|
for (const [method, handlers] of routerNode.handlers) {
|
|
325
|
-
if (!
|
|
325
|
+
if (!config_js_1.GlobalConfig.overwriteMethod) {
|
|
326
326
|
rootNode.handlers.set(method, handlers);
|
|
327
327
|
continue;
|
|
328
328
|
}
|
|
@@ -338,7 +338,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
338
338
|
for (const [path, middlewareNode] of children) {
|
|
339
339
|
if (n.children.has(path)) {
|
|
340
340
|
let findNode = n.children.get(path);
|
|
341
|
-
if (
|
|
341
|
+
if (config_js_1.GlobalConfig.allowDuplicateMw) {
|
|
342
342
|
findNode.middlewares.push(...middlewareNode.middlewares);
|
|
343
343
|
}
|
|
344
344
|
else {
|
|
@@ -358,7 +358,7 @@ class Router extends MiddlewareConfigure_1.default {
|
|
|
358
358
|
}
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
|
-
if (
|
|
361
|
+
if (config_js_1.GlobalConfig.allowDuplicateMw) {
|
|
362
362
|
rootMiddlewares.middlewares.push(...routerMiddlewares.middlewares);
|
|
363
363
|
}
|
|
364
364
|
else {
|