tezx 2.0.11 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -89
- package/bun/getConnInfo.d.ts +21 -0
- package/bun/getConnInfo.js +9 -0
- package/bun/index.d.ts +10 -4
- package/bun/index.js +8 -4
- package/bun/ws.d.ts +48 -0
- package/bun/ws.js +58 -0
- package/cjs/bun/getConnInfo.js +12 -0
- package/cjs/bun/index.js +35 -7
- package/cjs/bun/ws.js +63 -0
- package/cjs/core/config.js +2 -12
- package/cjs/core/context.js +131 -379
- package/cjs/core/error.js +49 -0
- package/cjs/core/request.js +79 -131
- package/cjs/core/router.js +54 -387
- package/cjs/core/server.js +83 -202
- package/cjs/deno/env.js +4 -4
- package/cjs/deno/getConnInfo.js +18 -0
- package/cjs/deno/index.js +11 -18
- package/cjs/deno/serveStatic.js +53 -0
- package/cjs/deno/ws.js +39 -0
- package/cjs/helper/index.js +46 -10
- package/cjs/index.js +5 -7
- package/cjs/jwt/node.js +94 -0
- package/cjs/jwt/web.js +178 -0
- package/cjs/middleware/basic-auth.js +42 -0
- package/cjs/middleware/bearer-auth.js +34 -0
- package/cjs/middleware/cache-control.js +44 -0
- package/cjs/middleware/cors.js +11 -21
- package/cjs/middleware/detect-bot.js +57 -0
- package/cjs/middleware/i18n.js +73 -60
- package/cjs/middleware/index.js +8 -46
- package/cjs/middleware/logger.js +9 -4
- package/cjs/middleware/pagination.js +3 -2
- package/cjs/middleware/powered-by.js +3 -2
- package/cjs/middleware/rate-limiter.js +38 -0
- package/cjs/middleware/request-id.js +4 -5
- package/cjs/middleware/sanitize-headers.js +22 -0
- package/cjs/middleware/secure-headers copy.js +143 -0
- package/cjs/middleware/secure-headers.js +157 -0
- package/cjs/middleware/{xssProtection.js → xss-protection.js} +5 -8
- package/cjs/node/env.js +7 -7
- package/cjs/node/getConnInfo.js +16 -0
- package/cjs/node/index.js +17 -18
- package/cjs/node/mount-node.js +59 -0
- package/cjs/node/serveStatic.js +56 -0
- package/cjs/node/toWebRequest.js +25 -0
- package/cjs/node/ws.js +82 -0
- package/cjs/registry/RadixRouter.js +148 -0
- package/cjs/registry/index.js +17 -0
- package/cjs/types/headers.js +2 -0
- package/cjs/types/index.js +13 -0
- package/cjs/utils/buffer.js +17 -0
- package/cjs/utils/colors.js +2 -0
- package/cjs/utils/cookie.js +59 -0
- package/cjs/utils/file.js +136 -0
- package/cjs/utils/formData.js +60 -10
- package/cjs/utils/generateID.js +37 -0
- package/cjs/utils/low-level.js +115 -0
- package/cjs/utils/{staticFile.js → mimeTypes.js} +0 -87
- package/cjs/utils/rateLimit.js +41 -0
- package/cjs/utils/response.js +65 -0
- package/cjs/{core/environment.js → utils/runtime.js} +2 -1
- package/cjs/utils/url.js +65 -30
- package/core/config.d.ts +2 -7
- package/core/config.js +2 -12
- package/core/context.d.ts +209 -164
- package/core/context.js +131 -346
- package/core/error.d.ts +96 -0
- package/core/error.js +44 -0
- package/core/request.d.ts +67 -107
- package/core/request.js +78 -130
- package/core/router.d.ts +138 -133
- package/core/router.js +53 -352
- package/core/server.d.ts +99 -38
- package/core/server.js +83 -202
- package/deno/env.js +3 -3
- package/deno/getConnInfo.d.ts +21 -0
- package/deno/getConnInfo.js +15 -0
- package/deno/index.d.ts +9 -4
- package/deno/index.js +7 -4
- package/deno/serveStatic.d.ts +28 -0
- package/deno/serveStatic.js +49 -0
- package/deno/ws.d.ts +42 -0
- package/deno/ws.js +36 -0
- package/helper/index.d.ts +29 -15
- package/helper/index.js +27 -7
- package/index.d.ts +10 -8
- package/index.js +4 -5
- package/jwt/node.d.ts +39 -0
- package/jwt/node.js +87 -0
- package/jwt/web.d.ts +14 -0
- package/jwt/web.js +174 -0
- package/middleware/basic-auth.d.ts +56 -0
- package/middleware/basic-auth.js +38 -0
- package/middleware/bearer-auth.d.ts +53 -0
- package/middleware/bearer-auth.js +30 -0
- package/middleware/cache-control.d.ts +30 -0
- package/middleware/cache-control.js +40 -0
- package/middleware/cors.d.ts +30 -3
- package/middleware/cors.js +12 -22
- package/middleware/detect-bot.d.ts +113 -0
- package/middleware/detect-bot.js +53 -0
- package/middleware/i18n.d.ts +166 -73
- package/middleware/i18n.js +73 -60
- package/middleware/index.d.ts +8 -32
- package/middleware/index.js +8 -44
- package/middleware/logger.d.ts +5 -2
- package/middleware/logger.js +9 -4
- package/middleware/pagination.d.ts +9 -6
- package/middleware/pagination.js +3 -2
- package/middleware/powered-by.d.ts +2 -1
- package/middleware/powered-by.js +3 -2
- package/middleware/{rateLimiter.d.ts → rate-limiter.d.ts} +15 -9
- package/middleware/rate-limiter.js +34 -0
- package/middleware/request-id.d.ts +2 -1
- package/middleware/request-id.js +5 -6
- package/middleware/{sanitizeHeader.d.ts → sanitize-headers.d.ts} +5 -19
- package/middleware/sanitize-headers.js +18 -0
- package/middleware/secure-headers copy.d.ts +15 -0
- package/middleware/secure-headers copy.js +136 -0
- package/middleware/secure-headers.d.ts +132 -0
- package/middleware/secure-headers.js +153 -0
- package/middleware/{xssProtection.d.ts → xss-protection.d.ts} +2 -1
- package/middleware/xss-protection.js +19 -0
- package/node/env.js +4 -4
- package/node/getConnInfo.d.ts +21 -0
- package/node/getConnInfo.js +13 -0
- package/node/index.d.ts +13 -4
- package/node/index.js +11 -4
- package/node/mount-node.d.ts +11 -0
- package/node/mount-node.js +56 -0
- package/node/serveStatic.d.ts +36 -0
- package/node/serveStatic.js +52 -0
- package/node/toWebRequest.js +22 -0
- package/node/ws.d.ts +56 -0
- package/node/ws.js +46 -0
- package/package.json +39 -30
- package/registry/RadixRouter.d.ts +40 -0
- package/registry/RadixRouter.js +144 -0
- package/registry/index.d.ts +2 -0
- package/registry/index.js +1 -0
- package/types/headers.d.ts +2 -0
- package/types/headers.js +1 -0
- package/types/index.d.ts +318 -18
- package/types/index.js +12 -1
- package/utils/buffer.d.ts +1 -0
- package/utils/buffer.js +14 -0
- package/utils/colors.d.ts +24 -0
- package/utils/colors.js +2 -0
- package/utils/cookie.d.ts +55 -0
- package/utils/cookie.js +53 -0
- package/utils/file.d.ts +38 -0
- package/utils/file.js +96 -0
- package/utils/formData.d.ts +41 -1
- package/utils/formData.js +58 -9
- package/utils/generateID.d.ts +42 -0
- package/utils/generateID.js +32 -0
- package/utils/httpStatusMap.d.ts +14 -0
- package/utils/low-level.d.ts +58 -0
- package/utils/low-level.js +108 -0
- package/utils/mimeTypes.d.ts +4 -0
- package/utils/{staticFile.js → mimeTypes.js} +0 -53
- package/utils/rateLimit.d.ts +18 -0
- package/utils/rateLimit.js +37 -0
- package/utils/response.d.ts +18 -0
- package/utils/response.js +58 -0
- package/{core/environment.d.ts → utils/runtime.d.ts} +1 -0
- package/{core/environment.js → utils/runtime.js} +1 -0
- package/utils/url.d.ts +42 -14
- package/utils/url.js +61 -27
- package/bun/adapter.d.ts +0 -127
- package/bun/adapter.js +0 -97
- package/cjs/bun/adapter.js +0 -100
- package/cjs/core/MiddlewareConfigure.js +0 -68
- package/cjs/core/common.js +0 -15
- package/cjs/deno/adpater.js +0 -67
- package/cjs/helper/common.js +0 -17
- package/cjs/middleware/basicAuth.js +0 -71
- package/cjs/middleware/cacheControl.js +0 -90
- package/cjs/middleware/detectBot.js +0 -104
- package/cjs/middleware/detectLocale.js +0 -43
- package/cjs/middleware/lazyLoadModules.js +0 -73
- package/cjs/middleware/rateLimiter.js +0 -24
- package/cjs/middleware/requestTimeout.js +0 -42
- package/cjs/middleware/sanitizeHeader.js +0 -51
- package/cjs/middleware/secureHeaders.js +0 -42
- package/cjs/node/adapter.js +0 -138
- package/cjs/utils/regexRouter.js +0 -58
- package/cjs/utils/state.js +0 -34
- package/cjs/utils/toWebRequest.js +0 -35
- package/cjs/ws/deno.js +0 -20
- package/cjs/ws/index.js +0 -53
- package/cjs/ws/node.js +0 -65
- package/core/MiddlewareConfigure.d.ts +0 -15
- package/core/MiddlewareConfigure.js +0 -63
- package/core/common.d.ts +0 -21
- package/core/common.js +0 -11
- package/deno/adpater.d.ts +0 -38
- package/deno/adpater.js +0 -64
- package/helper/common.d.ts +0 -5
- package/helper/common.js +0 -14
- package/middleware/basicAuth.d.ts +0 -81
- package/middleware/basicAuth.js +0 -67
- package/middleware/cacheControl.d.ts +0 -48
- package/middleware/cacheControl.js +0 -53
- package/middleware/detectBot.d.ts +0 -121
- package/middleware/detectBot.js +0 -98
- package/middleware/detectLocale.d.ts +0 -55
- package/middleware/detectLocale.js +0 -39
- package/middleware/lazyLoadModules.d.ts +0 -72
- package/middleware/lazyLoadModules.js +0 -69
- package/middleware/rateLimiter.js +0 -20
- package/middleware/requestTimeout.d.ts +0 -25
- package/middleware/requestTimeout.js +0 -38
- package/middleware/sanitizeHeader.js +0 -47
- package/middleware/secureHeaders.d.ts +0 -78
- package/middleware/secureHeaders.js +0 -38
- package/middleware/xssProtection.js +0 -22
- package/node/adapter.d.ts +0 -46
- package/node/adapter.js +0 -102
- package/utils/regexRouter.d.ts +0 -66
- package/utils/regexRouter.js +0 -53
- package/utils/state.d.ts +0 -50
- package/utils/state.js +0 -30
- package/utils/staticFile.d.ts +0 -10
- package/utils/toWebRequest.js +0 -32
- package/ws/deno.d.ts +0 -6
- package/ws/deno.js +0 -16
- package/ws/index.d.ts +0 -180
- package/ws/index.js +0 -50
- package/ws/node.d.ts +0 -7
- package/ws/node.js +0 -28
- /package/{utils → node}/toWebRequest.d.ts +0 -0
package/cjs/core/config.js
CHANGED
|
@@ -2,20 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GlobalConfig = void 0;
|
|
4
4
|
const debugging_js_1 = require("../utils/debugging.js");
|
|
5
|
-
const
|
|
5
|
+
const runtime_js_1 = require("../utils/runtime.js");
|
|
6
6
|
let GlobalConfig = class {
|
|
7
|
-
static notFound = (ctx) => {
|
|
8
|
-
const { method, urlRef: { pathname }, } = ctx.req;
|
|
9
|
-
return ctx.text(`${method}: '${pathname}' could not find\n`, 404);
|
|
10
|
-
};
|
|
11
|
-
static onError = (err, ctx) => {
|
|
12
|
-
return ctx.text(err, 500);
|
|
13
|
-
};
|
|
14
|
-
static allowDuplicateMw = false;
|
|
15
|
-
static overwriteMethod = true;
|
|
16
7
|
static debugMode = false;
|
|
17
|
-
static
|
|
18
|
-
static adapter = environment_js_1.Environment.getEnvironment;
|
|
8
|
+
static adapter = runtime_js_1.runtime;
|
|
19
9
|
static get debugging() {
|
|
20
10
|
return this.debugMode
|
|
21
11
|
? {
|
package/cjs/core/context.js
CHANGED
|
@@ -1,425 +1,177 @@
|
|
|
1
1
|
"use strict";
|
|
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
|
-
})();
|
|
35
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
3
|
exports.Context = void 0;
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
4
|
+
const file_js_1 = require("../utils/file.js");
|
|
5
|
+
const low_level_js_1 = require("../utils/low-level.js");
|
|
6
|
+
const mimeTypes_js_1 = require("../utils/mimeTypes.js");
|
|
7
|
+
const response_js_1 = require("../utils/response.js");
|
|
8
|
+
const error_js_1 = require("./error.js");
|
|
42
9
|
const request_js_1 = require("./request.js");
|
|
43
10
|
class Context {
|
|
11
|
+
#status = 200;
|
|
12
|
+
#headers;
|
|
13
|
+
#req = null;
|
|
14
|
+
#params = {};
|
|
44
15
|
rawRequest;
|
|
16
|
+
#args;
|
|
17
|
+
#body;
|
|
18
|
+
url;
|
|
19
|
+
res;
|
|
45
20
|
env = {};
|
|
46
|
-
headers = new Headers();
|
|
47
21
|
pathname;
|
|
48
|
-
url;
|
|
49
22
|
method;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.#options = options;
|
|
58
|
-
this.method = req?.method?.toUpperCase();
|
|
59
|
-
if (config_js_1.GlobalConfig.adapter == "node") {
|
|
60
|
-
let request = (0, toWebRequest_js_1.toWebRequest)(req, this.method);
|
|
61
|
-
this.url = request.url;
|
|
62
|
-
this.rawRequest = request;
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
this.url = req.url;
|
|
66
|
-
this.rawRequest = req;
|
|
67
|
-
}
|
|
68
|
-
this.pathname = this.req.urlRef.pathname;
|
|
23
|
+
constructor(req, pathname, method, env, args) {
|
|
24
|
+
this.#args = args;
|
|
25
|
+
this.rawRequest = req;
|
|
26
|
+
this.url = req.url;
|
|
27
|
+
this.pathname = pathname;
|
|
28
|
+
this.env = env ?? {};
|
|
29
|
+
this.method = method;
|
|
69
30
|
}
|
|
70
|
-
|
|
71
|
-
|
|
31
|
+
get getStatus() {
|
|
32
|
+
return this.#status;
|
|
33
|
+
}
|
|
34
|
+
set setStatus(code) {
|
|
35
|
+
this.#status = code;
|
|
36
|
+
}
|
|
37
|
+
get headers() {
|
|
38
|
+
return this.res?.headers ?? (this.#headers ??= new Headers());
|
|
39
|
+
}
|
|
40
|
+
setHeader(key, value, options) {
|
|
41
|
+
if (!value)
|
|
42
|
+
return this;
|
|
43
|
+
const _key = key.toLowerCase();
|
|
44
|
+
const append = options?.append || _key === "set-cookie";
|
|
45
|
+
const target = this.res?.headers ?? (this.#headers ??= new Headers());
|
|
72
46
|
if (append) {
|
|
73
|
-
|
|
47
|
+
target.append(_key, value);
|
|
74
48
|
}
|
|
75
49
|
else {
|
|
76
|
-
|
|
50
|
+
target.set(_key, value);
|
|
77
51
|
}
|
|
78
52
|
return this;
|
|
79
53
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
let cookies = {};
|
|
83
|
-
if (typeof c == "string") {
|
|
84
|
-
const cookieHeader = c.split(";");
|
|
85
|
-
for (const pair of cookieHeader) {
|
|
86
|
-
const [key, value] = pair?.trim()?.split("=");
|
|
87
|
-
cookies[key] = decodeURIComponent(value);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
get: (cookie) => {
|
|
92
|
-
return cookies?.[cookie];
|
|
93
|
-
},
|
|
94
|
-
all: () => {
|
|
95
|
-
return cookies;
|
|
96
|
-
},
|
|
97
|
-
delete: (name, options) => {
|
|
98
|
-
const value = "";
|
|
99
|
-
const cookieOptions = {
|
|
100
|
-
...options,
|
|
101
|
-
expires: new Date(0),
|
|
102
|
-
};
|
|
103
|
-
const cookieHeader = `${name}=${value};${serializeOptions(cookieOptions)}`;
|
|
104
|
-
this.headers.set("Set-Cookie", cookieHeader);
|
|
105
|
-
},
|
|
106
|
-
set: (name, value, options) => {
|
|
107
|
-
const cookieHeader = `${name}=${value};${serializeOptions(options || {})}`;
|
|
108
|
-
this.headers.set("Set-Cookie", cookieHeader);
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
json(body, ...args) {
|
|
113
|
-
let status = this.#status;
|
|
114
|
-
let headers = {
|
|
115
|
-
"Content-Type": "application/json; charset=utf-8",
|
|
116
|
-
};
|
|
117
|
-
if (typeof args[0] === "number") {
|
|
118
|
-
status = args[0];
|
|
119
|
-
if (typeof args[1] === "object") {
|
|
120
|
-
headers = { ...headers, ...args[1] };
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
else if (typeof args[0] === "object") {
|
|
124
|
-
headers = { ...headers, ...args[0] };
|
|
125
|
-
}
|
|
126
|
-
return this.#handleResponse(JSON.stringify(body), {
|
|
127
|
-
status: status,
|
|
128
|
-
headers: headers,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
send(body, ...args) {
|
|
132
|
-
let status = this.#status;
|
|
133
|
-
let headers = {};
|
|
134
|
-
if (typeof args[0] === "number") {
|
|
135
|
-
status = args[0];
|
|
136
|
-
if (typeof args[1] === "object") {
|
|
137
|
-
headers = args[1];
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else if (typeof args[0] === "object") {
|
|
141
|
-
headers = args[0];
|
|
142
|
-
}
|
|
143
|
-
const contentTypeHeader = headers["Content-Type"] || headers["content-type"] || "";
|
|
144
|
-
if (!contentTypeHeader) {
|
|
145
|
-
if (typeof body === "string") {
|
|
146
|
-
headers["Content-Type"] = "text/plain; charset=utf-8";
|
|
147
|
-
}
|
|
148
|
-
else if (typeof body === "number" || typeof body === "boolean") {
|
|
149
|
-
headers["Content-Type"] = "text/plain; charset=utf-8";
|
|
150
|
-
}
|
|
151
|
-
else if (typeof body === "object" &&
|
|
152
|
-
body !== null &&
|
|
153
|
-
!(body instanceof ReadableStream)) {
|
|
154
|
-
headers["Content-Type"] = "application/json; charset=utf-8";
|
|
155
|
-
body = JSON.stringify(body);
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
headers["Content-Type"] = "application/octet-stream";
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return this.#handleResponse(body, {
|
|
162
|
-
status: status,
|
|
163
|
-
headers,
|
|
164
|
-
});
|
|
54
|
+
set params(params) {
|
|
55
|
+
this.#params = params;
|
|
165
56
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
let data = strings;
|
|
169
|
-
if (Array.isArray(strings)) {
|
|
170
|
-
data = strings.reduce((result, str, i) => {
|
|
171
|
-
const value = args?.[i] ?? "";
|
|
172
|
-
return result + str + value;
|
|
173
|
-
}, "");
|
|
174
|
-
}
|
|
175
|
-
let headers = {
|
|
176
|
-
"Content-Type": "text/html; charset=utf-8",
|
|
177
|
-
};
|
|
178
|
-
if (typeof args[0] === "number") {
|
|
179
|
-
status = args[0];
|
|
180
|
-
if (typeof args[1] === "object") {
|
|
181
|
-
headers = { ...headers, ...args[1] };
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
else if (typeof args[0] === "object") {
|
|
185
|
-
headers = { ...headers, ...args[0] };
|
|
186
|
-
}
|
|
187
|
-
return this.#handleResponse(data, {
|
|
188
|
-
status: status,
|
|
189
|
-
headers: headers,
|
|
190
|
-
});
|
|
57
|
+
get req() {
|
|
58
|
+
return (this.#req ??= new request_js_1.TezXRequest(this.rawRequest, this.method, this.pathname, this.#params));
|
|
191
59
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
let headers = {
|
|
195
|
-
"Content-Type": "text/plain; charset=utf-8",
|
|
196
|
-
};
|
|
197
|
-
if (typeof args[0] === "number") {
|
|
198
|
-
status = args[0];
|
|
199
|
-
if (typeof args[1] === "object") {
|
|
200
|
-
headers = { ...headers, ...args[1] };
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
else if (typeof args[0] === "object") {
|
|
204
|
-
headers = { ...headers, ...args[0] };
|
|
205
|
-
}
|
|
206
|
-
return this.#handleResponse(data, {
|
|
207
|
-
status: status,
|
|
208
|
-
headers: headers,
|
|
209
|
-
});
|
|
60
|
+
get body() {
|
|
61
|
+
return this.#body;
|
|
210
62
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
let headers = {
|
|
214
|
-
"Content-Type": "application/xml; charset=utf-8",
|
|
215
|
-
};
|
|
216
|
-
if (typeof args[0] === "number") {
|
|
217
|
-
status = args[0];
|
|
218
|
-
if (typeof args[1] === "object") {
|
|
219
|
-
headers = { ...headers, ...args[1] };
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
else if (typeof args[0] === "object") {
|
|
223
|
-
headers = { ...headers, ...args[0] };
|
|
224
|
-
}
|
|
225
|
-
return this.#handleResponse(data, {
|
|
226
|
-
status: status,
|
|
227
|
-
headers: headers,
|
|
228
|
-
});
|
|
63
|
+
set body(value) {
|
|
64
|
+
this.#body = value;
|
|
229
65
|
}
|
|
230
66
|
status = (status) => {
|
|
231
67
|
this.#status = status;
|
|
232
68
|
return this;
|
|
233
69
|
};
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
async download(filePath, fileName) {
|
|
247
|
-
try {
|
|
248
|
-
let fileExists = false;
|
|
249
|
-
const runtime = environment_js_1.Environment.getEnvironment;
|
|
250
|
-
if (runtime === "node") {
|
|
251
|
-
const { existsSync } = await Promise.resolve().then(() => __importStar(require("node:fs")));
|
|
252
|
-
fileExists = existsSync(filePath);
|
|
253
|
-
}
|
|
254
|
-
else if (runtime === "bun") {
|
|
255
|
-
fileExists = Bun.file(filePath).exists();
|
|
256
|
-
}
|
|
257
|
-
else if (runtime === "deno") {
|
|
258
|
-
try {
|
|
259
|
-
await Deno.stat(filePath);
|
|
260
|
-
fileExists = true;
|
|
70
|
+
#newResponse(body, type, init = {}) {
|
|
71
|
+
const headers = new Headers(this.#headers);
|
|
72
|
+
headers.set("Content-Type", type);
|
|
73
|
+
if (init.headers) {
|
|
74
|
+
for (const key in init.headers) {
|
|
75
|
+
const value = init.headers[key];
|
|
76
|
+
if (!value)
|
|
77
|
+
continue;
|
|
78
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
79
|
+
headers.append(key, value);
|
|
261
80
|
}
|
|
262
|
-
|
|
263
|
-
|
|
81
|
+
else {
|
|
82
|
+
headers.set(key, value);
|
|
264
83
|
}
|
|
265
84
|
}
|
|
266
|
-
if (!fileExists) {
|
|
267
|
-
throw Error("File not found");
|
|
268
|
-
}
|
|
269
|
-
let fileBuffer;
|
|
270
|
-
if (runtime === "node") {
|
|
271
|
-
const { readFileSync } = await Promise.resolve().then(() => __importStar(require("node:fs")));
|
|
272
|
-
fileBuffer = await readFileSync(filePath);
|
|
273
|
-
}
|
|
274
|
-
else if (runtime === "bun") {
|
|
275
|
-
fileBuffer = await Bun.file(filePath)
|
|
276
|
-
.arrayBuffer()
|
|
277
|
-
.then((buf) => new Uint8Array(buf));
|
|
278
|
-
}
|
|
279
|
-
else if (runtime === "deno") {
|
|
280
|
-
fileBuffer = await Deno.readFile(filePath);
|
|
281
|
-
}
|
|
282
|
-
return this.#handleResponse(fileBuffer, {
|
|
283
|
-
status: 200,
|
|
284
|
-
headers: {
|
|
285
|
-
"Content-Disposition": `attachment; filename="${fileName}"`,
|
|
286
|
-
"Content-Type": "application/octet-stream",
|
|
287
|
-
"Content-Length": fileBuffer.byteLength.toString(),
|
|
288
|
-
},
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
catch (error) {
|
|
292
|
-
throw Error("Internal Server Error" + error?.message);
|
|
293
85
|
}
|
|
86
|
+
return new Response(body, {
|
|
87
|
+
status: init.status ?? this.#status,
|
|
88
|
+
statusText: init.statusText,
|
|
89
|
+
headers,
|
|
90
|
+
});
|
|
294
91
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
fileExists = existsSync(resolvedPath);
|
|
303
|
-
}
|
|
304
|
-
else if (runtime === "bun") {
|
|
305
|
-
fileExists = Bun.file(resolvedPath).exists();
|
|
306
|
-
}
|
|
307
|
-
else if (runtime === "deno") {
|
|
308
|
-
try {
|
|
309
|
-
await Deno.stat(resolvedPath);
|
|
310
|
-
fileExists = true;
|
|
92
|
+
newResponse(body, init = {}) {
|
|
93
|
+
const headers = new Headers(this.#headers);
|
|
94
|
+
if (init.headers) {
|
|
95
|
+
for (const key in init.headers) {
|
|
96
|
+
const value = init.headers[key];
|
|
97
|
+
if (!value) {
|
|
98
|
+
continue;
|
|
311
99
|
}
|
|
312
|
-
|
|
313
|
-
|
|
100
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
101
|
+
headers.append(key, value);
|
|
314
102
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
throw Error("File not found");
|
|
318
|
-
}
|
|
319
|
-
let fileSize = 0;
|
|
320
|
-
if (runtime === "node") {
|
|
321
|
-
const { statSync } = await Promise.resolve().then(() => __importStar(require("node:fs")));
|
|
322
|
-
fileSize = statSync(resolvedPath).size;
|
|
323
|
-
}
|
|
324
|
-
else if (runtime === "bun") {
|
|
325
|
-
fileSize = (await Bun.file(resolvedPath).arrayBuffer()).byteLength;
|
|
326
|
-
}
|
|
327
|
-
else if (runtime === "deno") {
|
|
328
|
-
const fileInfo = await Deno.stat(resolvedPath);
|
|
329
|
-
fileSize = fileInfo.size;
|
|
330
|
-
}
|
|
331
|
-
const ext = filePath.split(".").pop()?.toLowerCase() || "";
|
|
332
|
-
const mimeType = staticFile_js_1.mimeTypes[ext] || staticFile_js_1.defaultMimeType;
|
|
333
|
-
let fileStream;
|
|
334
|
-
if (runtime === "node") {
|
|
335
|
-
const { createReadStream } = await Promise.resolve().then(() => __importStar(require("node:fs")));
|
|
336
|
-
fileStream = createReadStream(resolvedPath);
|
|
337
|
-
}
|
|
338
|
-
else if (runtime === "bun") {
|
|
339
|
-
fileStream = Bun.file(resolvedPath).stream();
|
|
340
|
-
}
|
|
341
|
-
else if (runtime === "deno") {
|
|
342
|
-
const file = await Deno.open(resolvedPath, { read: true });
|
|
343
|
-
fileStream = file.readable;
|
|
344
|
-
}
|
|
345
|
-
let headers = {
|
|
346
|
-
"Content-Type": mimeType,
|
|
347
|
-
"Content-Length": fileSize.toString(),
|
|
348
|
-
};
|
|
349
|
-
let fileName = "";
|
|
350
|
-
if (typeof args[0] === "string") {
|
|
351
|
-
fileName = args[0];
|
|
352
|
-
if (typeof args[1] === "object") {
|
|
353
|
-
headers = { ...headers, ...args[1] };
|
|
103
|
+
else {
|
|
104
|
+
headers.set(key, value);
|
|
354
105
|
}
|
|
355
106
|
}
|
|
356
|
-
else if (typeof args[0] === "object") {
|
|
357
|
-
headers = { ...headers, ...args[0] };
|
|
358
|
-
}
|
|
359
|
-
if (fileName) {
|
|
360
|
-
headers["Content-Disposition"] = `attachment; filename="${fileName}"`;
|
|
361
|
-
}
|
|
362
|
-
return this.#handleResponse(fileStream, {
|
|
363
|
-
status: 200,
|
|
364
|
-
headers,
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
catch (error) {
|
|
368
|
-
throw Error("Internal Server Error" + error?.message);
|
|
369
107
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
status: status,
|
|
108
|
+
return new Response(body, {
|
|
109
|
+
status: init.status ?? this.#status,
|
|
110
|
+
statusText: init.statusText,
|
|
374
111
|
headers,
|
|
375
112
|
});
|
|
376
|
-
this.resBody = body;
|
|
377
|
-
return response;
|
|
378
|
-
}
|
|
379
|
-
get req() {
|
|
380
|
-
return new request_js_1.Request({
|
|
381
|
-
method: this.method,
|
|
382
|
-
req: this.rawRequest,
|
|
383
|
-
options: this.#options,
|
|
384
|
-
params: this.#params,
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
set params(params) {
|
|
388
|
-
this.#params = params;
|
|
389
|
-
}
|
|
390
|
-
set body(body) {
|
|
391
|
-
this.#body = body;
|
|
392
|
-
}
|
|
393
|
-
get body() {
|
|
394
|
-
return this.#body;
|
|
395
113
|
}
|
|
396
|
-
|
|
397
|
-
return this.#
|
|
114
|
+
text(content, init) {
|
|
115
|
+
return this.#newResponse(content, "text/plain; charset=utf-8", init);
|
|
398
116
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
function serializeOptions(options) {
|
|
402
|
-
const parts = [];
|
|
403
|
-
if (options.maxAge) {
|
|
404
|
-
parts.push(`Max-Age=${options.maxAge}`);
|
|
117
|
+
html(strings, ...args) {
|
|
118
|
+
return this.#newResponse((0, response_js_1.toString)(strings, args), "text/html; charset=utf-8", args?.[0]);
|
|
405
119
|
}
|
|
406
|
-
|
|
407
|
-
|
|
120
|
+
xml(strings, ...args) {
|
|
121
|
+
return this.#newResponse((0, response_js_1.toString)(strings, args), "text/html; charset=utf-8", args?.[0]);
|
|
408
122
|
}
|
|
409
|
-
|
|
410
|
-
|
|
123
|
+
json(json, init) {
|
|
124
|
+
return this.#newResponse(JSON.stringify(json), "application/json; charset=utf-8", init);
|
|
411
125
|
}
|
|
412
|
-
|
|
413
|
-
|
|
126
|
+
send(body, init) {
|
|
127
|
+
let { body: _body, type } = (0, response_js_1.determineContentTypeBody)(body);
|
|
128
|
+
const contentType = init?.headers?.["Content-Type"] ??
|
|
129
|
+
init?.headers?.["content-type"] ??
|
|
130
|
+
type;
|
|
131
|
+
return this.#newResponse(_body, contentType, init);
|
|
414
132
|
}
|
|
415
|
-
|
|
416
|
-
|
|
133
|
+
redirect(url, status = 302) {
|
|
134
|
+
const headers = new Headers(this.#headers);
|
|
135
|
+
headers.set("Location", url);
|
|
136
|
+
return new Response(null, { status, headers });
|
|
137
|
+
}
|
|
138
|
+
async download(filePath, filename) {
|
|
139
|
+
if (!(await (0, file_js_1.fileExists)(filePath)))
|
|
140
|
+
throw error_js_1.TezXError.notFound("File not found");
|
|
141
|
+
const buf = await (0, file_js_1.getFileBuffer)(filePath);
|
|
142
|
+
const headers = {
|
|
143
|
+
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
144
|
+
"Content-Length": buf.byteLength.toString(),
|
|
145
|
+
};
|
|
146
|
+
return this.#newResponse(buf, "application/octet-stream", {
|
|
147
|
+
status: this.#status,
|
|
148
|
+
headers,
|
|
149
|
+
});
|
|
417
150
|
}
|
|
418
|
-
|
|
419
|
-
|
|
151
|
+
async sendFile(filePath, init) {
|
|
152
|
+
if (!(await (0, file_js_1.fileExists)(filePath)))
|
|
153
|
+
throw error_js_1.TezXError.notFound("File not found");
|
|
154
|
+
let { size, mtime } = await (0, file_js_1.fileSize)(filePath);
|
|
155
|
+
const ext = (0, low_level_js_1.extensionExtract)(filePath);
|
|
156
|
+
const mimeType = mimeTypes_js_1.mimeTypes[ext] ?? mimeTypes_js_1.defaultMimeType;
|
|
157
|
+
let fileStream = await (0, file_js_1.readStream)(filePath);
|
|
158
|
+
let headers = {
|
|
159
|
+
"Content-Type": mimeType,
|
|
160
|
+
"Content-Length": size.toString(),
|
|
161
|
+
...init?.headers,
|
|
162
|
+
};
|
|
163
|
+
let filename = init?.filename;
|
|
164
|
+
if (filename) {
|
|
165
|
+
headers["Content-Disposition"] = `attachment; filename="${filename}"`;
|
|
166
|
+
}
|
|
167
|
+
return this.newResponse(fileStream, {
|
|
168
|
+
status: init?.status ?? this.#status,
|
|
169
|
+
statusText: init?.statusText,
|
|
170
|
+
headers,
|
|
171
|
+
});
|
|
420
172
|
}
|
|
421
|
-
|
|
422
|
-
|
|
173
|
+
get args() {
|
|
174
|
+
return this.#args;
|
|
423
175
|
}
|
|
424
|
-
return parts.join("; ");
|
|
425
176
|
}
|
|
177
|
+
exports.Context = Context;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TezXError = void 0;
|
|
4
|
+
exports.TezXErrorParse = TezXErrorParse;
|
|
5
|
+
class TezXError extends Error {
|
|
6
|
+
statusCode;
|
|
7
|
+
details;
|
|
8
|
+
constructor(message, statusCode = 500, details) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.statusCode = statusCode;
|
|
11
|
+
this.details = details;
|
|
12
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
13
|
+
Error.captureStackTrace(this, this.constructor);
|
|
14
|
+
}
|
|
15
|
+
static badRequest(message = "Bad Request", details) {
|
|
16
|
+
return new TezXError(message, 400, details);
|
|
17
|
+
}
|
|
18
|
+
static unauthorized(message = "Unauthorized", details) {
|
|
19
|
+
return new TezXError(message, 401, details);
|
|
20
|
+
}
|
|
21
|
+
static forbidden(message = "Forbidden", details) {
|
|
22
|
+
return new TezXError(message, 403, details);
|
|
23
|
+
}
|
|
24
|
+
static notFound(message = "Resource Not Found", details) {
|
|
25
|
+
return new TezXError(message, 404, details);
|
|
26
|
+
}
|
|
27
|
+
static conflict(message = "Conflict", details) {
|
|
28
|
+
return new TezXError(message, 409, details);
|
|
29
|
+
}
|
|
30
|
+
static internal(message = "Internal Server Error", details) {
|
|
31
|
+
return new TezXError(message, 500, details);
|
|
32
|
+
}
|
|
33
|
+
toJSON() {
|
|
34
|
+
return {
|
|
35
|
+
error: true,
|
|
36
|
+
message: this.message,
|
|
37
|
+
statusCode: this.statusCode,
|
|
38
|
+
details: this.details ?? null,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.TezXError = TezXError;
|
|
43
|
+
function TezXErrorParse(err, statusCode) {
|
|
44
|
+
if (err instanceof TezXError)
|
|
45
|
+
return err;
|
|
46
|
+
else if (err instanceof Error)
|
|
47
|
+
return new TezXError(err?.message, 500, err?.stack);
|
|
48
|
+
return new TezXError(String(err), statusCode);
|
|
49
|
+
}
|