diesel-core 1.6.7 → 1.6.9
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/adaptor/node/main.d.ts +2 -1
- package/dist/adaptor/node/main.js +3 -1
- package/dist/constant.js +11 -0
- package/dist/ctx.js +507 -52
- package/dist/handleRequest.js +82 -52
- package/dist/http-exception.js +20 -1
- package/dist/http-exception.test.js +50 -0
- package/dist/main.js +670 -258
- package/dist/middlewares/cors/index.test.js +94 -0
- package/dist/middlewares/file-route/index.js +69 -0
- package/dist/middlewares/filesave/index.test.js +73 -0
- package/dist/middlewares/jwt/index.test.js +74 -0
- package/dist/middlewares/powered-by/index.test.js +46 -0
- package/dist/middlewares/ratelimit/index.test.js +104 -0
- package/dist/middlewares/request-id/index.js +50 -0
- package/dist/middlewares/request-id/index.test.js +1 -0
- package/dist/middlewares/security/index.test.js +50 -0
- package/dist/request_pipeline.js +285 -95
- package/dist/router/find-my-way.js +18 -102
- package/dist/router/interface.js +22 -102
- package/dist/router/regex.js +3 -0
- package/dist/router/trie.js +148 -1
- package/dist/router/trie2.js +151 -0
- package/dist/router/trie2.test.js +162 -0
- package/dist/types.js +1 -0
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import * as http from 'node:http';
|
|
1
2
|
export interface options {
|
|
2
3
|
fetch: (req: Request, ...args: any) => Response | Promise<Response>;
|
|
3
4
|
port: number;
|
|
4
5
|
}
|
|
5
|
-
export declare function serve(options: options):
|
|
6
|
+
export declare function serve(options: options): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
@@ -89,6 +89,7 @@ function sendWebResToNodeRes(webRes, nodeRes) {
|
|
|
89
89
|
}
|
|
90
90
|
function serve(options) {
|
|
91
91
|
var _this = this;
|
|
92
|
+
var _a;
|
|
92
93
|
var server = http.createServer(function (request, response) { return __awaiter(_this, void 0, void 0, function () {
|
|
93
94
|
var webRequest, webRes;
|
|
94
95
|
return __generator(this, function (_a) {
|
|
@@ -106,5 +107,6 @@ function serve(options) {
|
|
|
106
107
|
}
|
|
107
108
|
});
|
|
108
109
|
}); });
|
|
109
|
-
server.listen(options.port, function () { return console.log(
|
|
110
|
+
server.listen((_a = options.port) !== null && _a !== void 0 ? _a : 3000, function () { var _a; return console.log("node server running on port ".concat((_a = options.port) !== null && _a !== void 0 ? _a : 3000)); });
|
|
111
|
+
return server;
|
|
110
112
|
}
|
package/dist/constant.js
ADDED
package/dist/ctx.js
CHANGED
|
@@ -1,54 +1,509 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function encode_char(c) {
|
|
10
|
-
return _ENCODE_HTML_RULES[c] || c;
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
11
9
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
`;for(var Y=0;Y<g.destructuredLocals.length;Y++){var z=g.destructuredLocals[Y];if(!O.test(z))throw new Error("destructuredLocals["+Y+"] is not a valid JS identifier.");if(Y>0)K+=`,
|
|
23
|
-
`;K+=z+" = __locals."+z}A+=K+`;
|
|
24
|
-
`}if(g._with!==!1)A+=" with ("+g.localsName+` || {}) {
|
|
25
|
-
`,C+=` }
|
|
26
|
-
`;C+=` return __output;
|
|
27
|
-
`,this.source=A+this.source+C}if(g.compileDebug)f=`var __line = 1
|
|
28
|
-
, __lines = `+JSON.stringify(this.templateText)+`
|
|
29
|
-
, __filename = `+j+`;
|
|
30
|
-
try {
|
|
31
|
-
`+this.source+`} catch (e) {
|
|
32
|
-
rethrow(e, __lines, __filename, __line, escapeFn);
|
|
10
|
+
import { getMimeType } from "./utils/mimeType";
|
|
11
|
+
let ejsInstance = null;
|
|
12
|
+
function getEjs() {
|
|
13
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
+
if (!ejsInstance) {
|
|
15
|
+
const mod = yield import("ejs");
|
|
16
|
+
ejsInstance = mod.default || mod;
|
|
17
|
+
}
|
|
18
|
+
return ejsInstance;
|
|
19
|
+
});
|
|
33
20
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
const typeMap = {
|
|
22
|
+
string: "text/plain; charset=utf-8",
|
|
23
|
+
object: "application/json; charset=utf-8",
|
|
24
|
+
Uint8Array: "application/octet-stream",
|
|
25
|
+
ArrayBuffer: "application/octet-stream",
|
|
26
|
+
};
|
|
27
|
+
export class Context {
|
|
28
|
+
constructor(req, server, path, routePattern, paramNames, env, executionContext) {
|
|
29
|
+
this.headers = new Headers();
|
|
30
|
+
// Lazily initialized
|
|
31
|
+
this.parsedQuery = null;
|
|
32
|
+
this.parsedParams = null;
|
|
33
|
+
this.parsedCookies = null;
|
|
34
|
+
this.parsedBody = null;
|
|
35
|
+
this.contextData = {};
|
|
36
|
+
this.urlObject = null;
|
|
37
|
+
this.req = req;
|
|
38
|
+
this.server = server;
|
|
39
|
+
this.path = path;
|
|
40
|
+
this.routePattern = routePattern;
|
|
41
|
+
this.executionContext = executionContext;
|
|
42
|
+
this.env = env;
|
|
43
|
+
this.paramNames = paramNames;
|
|
44
|
+
}
|
|
45
|
+
// Methods
|
|
46
|
+
setHeader(key, value) {
|
|
47
|
+
this.headers.set(key, value);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
removeHeader(key) {
|
|
51
|
+
this.headers.delete(key);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
set(key, value) {
|
|
55
|
+
this.contextData[key] = value;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
get(key) {
|
|
59
|
+
return this.contextData[key];
|
|
60
|
+
}
|
|
61
|
+
get ip() {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
if (this.server)
|
|
64
|
+
return (_b = (_a = this.server.requestIP(this.req)) === null || _a === void 0 ? void 0 : _a.address) !== null && _b !== void 0 ? _b : null;
|
|
65
|
+
return this.req.headers.get("CF-Connecting-IP") || null;
|
|
66
|
+
}
|
|
67
|
+
get url() {
|
|
68
|
+
if (!this.urlObject) {
|
|
69
|
+
this.urlObject = new URL(this.req.url);
|
|
70
|
+
}
|
|
71
|
+
return this.urlObject;
|
|
72
|
+
}
|
|
73
|
+
get query() {
|
|
74
|
+
if (!this.parsedQuery) {
|
|
75
|
+
this.parsedQuery = this.url.search ? Object.fromEntries(this.url.searchParams) : {};
|
|
76
|
+
}
|
|
77
|
+
return this.parsedQuery;
|
|
78
|
+
}
|
|
79
|
+
get params() {
|
|
80
|
+
var _a;
|
|
81
|
+
if (!Array.isArray(this.paramNames)) {
|
|
82
|
+
return this.paramNames;
|
|
83
|
+
}
|
|
84
|
+
if (!this.parsedParams) {
|
|
85
|
+
try {
|
|
86
|
+
this.parsedParams = extractParam(this.paramNames, this.path);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
90
|
+
throw new Error(`Failed to extract route parameters: ${message}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return (_a = this.parsedParams) !== null && _a !== void 0 ? _a : {};
|
|
94
|
+
}
|
|
95
|
+
get body() {
|
|
96
|
+
if (this.req.method === "GET") {
|
|
97
|
+
return Promise.resolve({});
|
|
98
|
+
}
|
|
99
|
+
if (!this.parsedBody) {
|
|
100
|
+
this.parsedBody = (() => __awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
try {
|
|
102
|
+
const result = yield parseBody(this.req);
|
|
103
|
+
if (result.error) {
|
|
104
|
+
throw new Error(result.error);
|
|
105
|
+
}
|
|
106
|
+
return Object.keys(result).length === 0 ? null : result;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw new Error("Invalid request body format");
|
|
110
|
+
// const message = error instanceof Error ? error.message : String(error);
|
|
111
|
+
// throw new Error(`Failed to parse request body: ${message}`);
|
|
112
|
+
}
|
|
113
|
+
}))();
|
|
114
|
+
}
|
|
115
|
+
return this.parsedBody;
|
|
116
|
+
}
|
|
117
|
+
text(data, status = 200) {
|
|
118
|
+
// if (!this.headers.has("Content-Type")) {
|
|
119
|
+
// this.headers.set("Content-Type", "text/plain; charset=utf-8");
|
|
120
|
+
// }
|
|
121
|
+
return new Response(data, {
|
|
122
|
+
status,
|
|
123
|
+
headers: this.headers
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
send(data, status = 200) {
|
|
127
|
+
// this.status = status;
|
|
128
|
+
var _a;
|
|
129
|
+
// const dataType = data instanceof Uint8Array ? "Uint8Array"
|
|
130
|
+
// : data instanceof ArrayBuffer ? "ArrayBuffer"
|
|
131
|
+
// : typeof data;
|
|
132
|
+
let dataType;
|
|
133
|
+
if (data instanceof Uint8Array)
|
|
134
|
+
dataType = "Uint8Array";
|
|
135
|
+
else if (data instanceof ArrayBuffer)
|
|
136
|
+
dataType = 'ArrayBuffer';
|
|
137
|
+
else
|
|
138
|
+
dataType = typeof data;
|
|
139
|
+
if (!this.headers.has("Content-Type")) {
|
|
140
|
+
this.headers.set("Content-Type", (_a = typeMap[dataType]) !== null && _a !== void 0 ? _a : "text/plain; charset=utf-8");
|
|
141
|
+
}
|
|
142
|
+
const responseData = dataType === "object" && data !== null ? JSON.stringify(data) : data;
|
|
143
|
+
return new Response(responseData, { status, headers: this.headers });
|
|
144
|
+
}
|
|
145
|
+
json(object, status = 200) {
|
|
146
|
+
// this.status = status;
|
|
147
|
+
// if (!this.headers.has("Content-Type")) {
|
|
148
|
+
// this.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
149
|
+
// }
|
|
150
|
+
return Response.json(object, { status, headers: this.headers });
|
|
151
|
+
}
|
|
152
|
+
file(filePath, mime_Type, status = 200) {
|
|
153
|
+
// this.status = status;
|
|
154
|
+
const file = Bun.file(filePath);
|
|
155
|
+
if (!this.headers.has("Content-Type")) {
|
|
156
|
+
this.headers.set("Content-Type", mime_Type !== null && mime_Type !== void 0 ? mime_Type : getMimeType(filePath));
|
|
157
|
+
}
|
|
158
|
+
return new Response(file, { status, headers: this.headers });
|
|
159
|
+
}
|
|
160
|
+
ejs(viewPath_1) {
|
|
161
|
+
return __awaiter(this, arguments, void 0, function* (viewPath, data = {}, status = 200) {
|
|
162
|
+
// this.status = status;
|
|
163
|
+
const ejs = yield getEjs();
|
|
164
|
+
try {
|
|
165
|
+
const template = yield Bun.file(viewPath).text();
|
|
166
|
+
const rendered = ejs.render(template, data);
|
|
167
|
+
const headers = new Headers({ "Content-Type": "text/html; charset=utf-8" });
|
|
168
|
+
return new Response(rendered, { status, headers });
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
console.error("EJS Rendering Error:", error);
|
|
172
|
+
return new Response("Error rendering template", { status: 500 });
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
redirect(path, status = 302) {
|
|
177
|
+
// this.status = status
|
|
178
|
+
this.headers.set("Location", path);
|
|
179
|
+
return new Response(null, { status, headers: this.headers });
|
|
180
|
+
}
|
|
181
|
+
setCookie(name, value, options = {}) {
|
|
182
|
+
let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
183
|
+
if (options.maxAge)
|
|
184
|
+
cookieString += `; Max-Age=${options.maxAge}`;
|
|
185
|
+
if (options.expires)
|
|
186
|
+
cookieString += `; Expires=${options.expires.toUTCString()}`;
|
|
187
|
+
if (options.path)
|
|
188
|
+
cookieString += `; Path=${options.path}`;
|
|
189
|
+
if (options.domain)
|
|
190
|
+
cookieString += `; Domain=${options.domain}`;
|
|
191
|
+
if (options.secure)
|
|
192
|
+
cookieString += `; Secure`;
|
|
193
|
+
if (options.httpOnly)
|
|
194
|
+
cookieString += `; HttpOnly`;
|
|
195
|
+
if (options.sameSite)
|
|
196
|
+
cookieString += `; SameSite=${options.sameSite}`;
|
|
197
|
+
this.headers.append("Set-Cookie", cookieString);
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
get cookies() {
|
|
201
|
+
if (!this.parsedCookies) {
|
|
202
|
+
const cookieHeader = this.req.headers.get("cookie");
|
|
203
|
+
this.parsedCookies = cookieHeader ? parseCookie(cookieHeader) : {};
|
|
204
|
+
}
|
|
205
|
+
return this.parsedCookies;
|
|
206
|
+
}
|
|
207
|
+
// Streams
|
|
208
|
+
stream(callback) {
|
|
209
|
+
const headers = new Headers(this.headers);
|
|
210
|
+
const stream = new ReadableStream({
|
|
211
|
+
start(controller) {
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
yield callback(controller);
|
|
214
|
+
controller.close();
|
|
215
|
+
});
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
return new Response(stream, { headers });
|
|
219
|
+
}
|
|
220
|
+
yieldStream(callback) {
|
|
221
|
+
return new Response(
|
|
222
|
+
// {
|
|
223
|
+
// async *[Symbol.asyncIterator]() {
|
|
224
|
+
// yield* callback();
|
|
225
|
+
// },
|
|
226
|
+
// },
|
|
227
|
+
// { headers: this.headers }
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// function parseCookie(cookieHeader: string | undefined): Record<string, string> {
|
|
232
|
+
// const cookies: Record<string, string> = {};
|
|
233
|
+
// const cookiesArray = cookieHeader?.split(";")!;
|
|
234
|
+
// for (let i = 0; i < cookiesArray?.length!; i++) {
|
|
235
|
+
// const [cookieName, ...cookieValeParts] = cookiesArray[i].trim().split("=");
|
|
236
|
+
// const cookieVale = cookieValeParts?.join("=").trim();
|
|
237
|
+
// if (cookieName) {
|
|
238
|
+
// cookies[cookieName.trim()] = decodeURIComponent(cookieVale);
|
|
239
|
+
// }
|
|
240
|
+
// }
|
|
241
|
+
// return cookies;
|
|
242
|
+
// }
|
|
243
|
+
function parseCookie(cookieHeader) {
|
|
244
|
+
return Object.fromEntries(cookieHeader.split(";").map((cookie) => {
|
|
245
|
+
const [name, ...valueParts] = cookie.trim().split("=");
|
|
246
|
+
return [name, decodeURIComponent(valueParts.join("="))];
|
|
247
|
+
}));
|
|
248
|
+
}
|
|
249
|
+
export function extractParam(paramNames, incomingPath) {
|
|
250
|
+
// ["id","name"]
|
|
251
|
+
const param = {};
|
|
252
|
+
// inComingpath = /user/2/pradeep
|
|
253
|
+
const [pathWithoutQuery] = incomingPath.split("?");
|
|
254
|
+
const pathSegments = pathWithoutQuery.split("/").filter(s => s !== '');
|
|
255
|
+
// let segmentStart = 0
|
|
256
|
+
// let segmentIndex = 0
|
|
257
|
+
// const segments: string[] = []
|
|
258
|
+
// for (let i = 0; i <= pathWithoutQuery.length; i++) {
|
|
259
|
+
// if (i === pathWithoutQuery.length || pathWithoutQuery.charCodeAt(i) === 47) { // '/'
|
|
260
|
+
// if (i > segmentStart) {
|
|
261
|
+
// segments[segmentIndex++] = pathWithoutQuery.slice(segmentStart, i)
|
|
262
|
+
// }
|
|
263
|
+
// segmentStart = i + 1
|
|
264
|
+
// }
|
|
265
|
+
// }
|
|
266
|
+
const start = pathSegments.length - paramNames.length;
|
|
267
|
+
for (let i = 0; i < paramNames.length; i++) {
|
|
268
|
+
param[paramNames[i]] = pathSegments[start + i];
|
|
269
|
+
}
|
|
270
|
+
return param;
|
|
271
|
+
}
|
|
272
|
+
export function extractDynamicParams(originalPath, incomingPath) {
|
|
273
|
+
const params = {};
|
|
274
|
+
const routeSegments = originalPath.split("/");
|
|
275
|
+
const [pathWithoutQuery] = incomingPath.split("?");
|
|
276
|
+
const pathSegments = pathWithoutQuery.split("/");
|
|
277
|
+
if (routeSegments.length !== pathSegments.length)
|
|
278
|
+
return null;
|
|
279
|
+
for (let i = 0; i < routeSegments.length; i++) {
|
|
280
|
+
const segment = routeSegments[i];
|
|
281
|
+
if (segment.charCodeAt(0) === 58) {
|
|
282
|
+
params[segment.slice(1)] = pathSegments[i];
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return params;
|
|
286
|
+
}
|
|
287
|
+
function parseBody(req) {
|
|
288
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
289
|
+
const contentType = req.headers.get("Content-Type") || '';
|
|
290
|
+
if (!contentType)
|
|
291
|
+
return {};
|
|
292
|
+
const contentLength = req.headers.get("Content-Length");
|
|
293
|
+
if (contentLength === "0" || !req.body) {
|
|
294
|
+
return {};
|
|
295
|
+
}
|
|
296
|
+
if (contentType.startsWith("application/json")) {
|
|
297
|
+
return yield req.json();
|
|
298
|
+
}
|
|
299
|
+
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
300
|
+
const body = yield req.text();
|
|
301
|
+
return Object.fromEntries(new URLSearchParams(body));
|
|
302
|
+
}
|
|
303
|
+
if (contentType.startsWith("multipart/form-data")) {
|
|
304
|
+
const formData = yield req.formData();
|
|
305
|
+
const obj = {};
|
|
306
|
+
for (const [key, value] of formData.entries()) {
|
|
307
|
+
obj[key] = value;
|
|
308
|
+
}
|
|
309
|
+
return obj;
|
|
310
|
+
}
|
|
311
|
+
return { error: "Unknown request body type" };
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
// Deprecated
|
|
315
|
+
// export default function createCtx(
|
|
316
|
+
// req: Request,
|
|
317
|
+
// server: Server,
|
|
318
|
+
// pathname: string,
|
|
319
|
+
// // onn: (event: string | symbol, listener: EventListener) => void,
|
|
320
|
+
// // emitter: (event: string | symbol, ...args: any) => void,
|
|
321
|
+
// routePattern: string | undefined
|
|
322
|
+
// ): ContextType {
|
|
323
|
+
// let parsedQuery: Record<string, string> | null = null;
|
|
324
|
+
// let parsedParams: Record<string, string> | null = null;
|
|
325
|
+
// let parsedCookies: Record<string, string> | null = null;
|
|
326
|
+
// let parsedBody: Promise<any> | null = null;
|
|
327
|
+
// let contextData: Record<string, any> = {};
|
|
328
|
+
// let urlObject: URL | null = null
|
|
329
|
+
// return {
|
|
330
|
+
// req,
|
|
331
|
+
// server,
|
|
332
|
+
// pathname,
|
|
333
|
+
// // status: 200,
|
|
334
|
+
// headers: new Headers(),
|
|
335
|
+
// // on(event: string | symbol, listener: EventListener) {
|
|
336
|
+
// // onn(event, listener)
|
|
337
|
+
// // },
|
|
338
|
+
// // emit(event: string | symbol, ...args: any) {
|
|
339
|
+
// // emitter(event, ...args)
|
|
340
|
+
// // },
|
|
341
|
+
// setHeader(key: string, value: string): ContextType {
|
|
342
|
+
// this.headers.set(key, value);
|
|
343
|
+
// return this;
|
|
344
|
+
// },
|
|
345
|
+
// removeHeader(key: string): ContextType {
|
|
346
|
+
// this.headers.delete(key)
|
|
347
|
+
// return this
|
|
348
|
+
// },
|
|
349
|
+
// set<T>(key: string, value: T): ContextType {
|
|
350
|
+
// contextData[key] = value;
|
|
351
|
+
// return this;
|
|
352
|
+
// },
|
|
353
|
+
// get<T>(key: string): T | undefined {
|
|
354
|
+
// return contextData[key];
|
|
355
|
+
// },
|
|
356
|
+
// get ip(): string | null {
|
|
357
|
+
// return this.server.requestIP(req)?.address ?? null;
|
|
358
|
+
// },
|
|
359
|
+
// get url(): URL {
|
|
360
|
+
// if (!urlObject) {
|
|
361
|
+
// urlObject = new URL(req.url)
|
|
362
|
+
// }
|
|
363
|
+
// return urlObject
|
|
364
|
+
// },
|
|
365
|
+
// get query(): Record<string, string> {
|
|
366
|
+
// if (!parsedQuery) {
|
|
367
|
+
// if (!this.url.search) return {};
|
|
368
|
+
// parsedQuery = Object.fromEntries(this.url.searchParams);
|
|
369
|
+
// }
|
|
370
|
+
// return parsedQuery;
|
|
371
|
+
// },
|
|
372
|
+
// get params(): Record<string, string> {
|
|
373
|
+
// if (!parsedParams && routePattern) {
|
|
374
|
+
// try {
|
|
375
|
+
// parsedParams = extractDynamicParams(routePattern, pathname);
|
|
376
|
+
// } catch (error) {
|
|
377
|
+
// const message = error instanceof Error ? error.message : String(error)
|
|
378
|
+
// throw new Error(`Failed to extract route parameters: ${message}`);
|
|
379
|
+
// }
|
|
380
|
+
// }
|
|
381
|
+
// return parsedParams ?? {};
|
|
382
|
+
// },
|
|
383
|
+
// get body(): Promise<any> {
|
|
384
|
+
// if (req.method === "GET") {
|
|
385
|
+
// return Promise.resolve({});
|
|
386
|
+
// }
|
|
387
|
+
// if (!parsedBody) {
|
|
388
|
+
// parsedBody = (async () => {
|
|
389
|
+
// try {
|
|
390
|
+
// const result = await parseBody(req);
|
|
391
|
+
// if (result.error) {
|
|
392
|
+
// throw new Error(result.error);
|
|
393
|
+
// }
|
|
394
|
+
// return Object.keys(result).length === 0 ? null : result;
|
|
395
|
+
// } catch (error) {
|
|
396
|
+
// throw new Error("Invalid request body format");
|
|
397
|
+
// // const message = error instanceof Error ? error.message : String(error);
|
|
398
|
+
// // throw new Error(`Failed to parse request body: ${message}`);
|
|
399
|
+
// }
|
|
400
|
+
// })();
|
|
401
|
+
// }
|
|
402
|
+
// return parsedBody;
|
|
403
|
+
// },
|
|
404
|
+
// text(data: string, status: number = 200) {
|
|
405
|
+
// return new Response(data, {
|
|
406
|
+
// status,
|
|
407
|
+
// headers: this.headers
|
|
408
|
+
// });
|
|
409
|
+
// },
|
|
410
|
+
// send<T>(data: T, status: number = 200): Response {
|
|
411
|
+
// // this.status = status;
|
|
412
|
+
// // const dataType = data instanceof Uint8Array ? "Uint8Array"
|
|
413
|
+
// // : data instanceof ArrayBuffer ? "ArrayBuffer"
|
|
414
|
+
// // : typeof data;
|
|
415
|
+
// let dataType: string
|
|
416
|
+
// if (data instanceof Uint8Array) dataType = "Uint8Array"
|
|
417
|
+
// else if (data instanceof ArrayBuffer) dataType = 'ArrayBuffer'
|
|
418
|
+
// else dataType = typeof data
|
|
419
|
+
// // if (!this.headers.has("Content-Type")) {
|
|
420
|
+
// // this.headers.set("Content-Type", typeMap[dataType] ?? "text/plain; charset=utf-8");
|
|
421
|
+
// // }
|
|
422
|
+
// const responseData =
|
|
423
|
+
// dataType === "object" && data !== null ? JSON.stringify(data) : (data as any);
|
|
424
|
+
// return new Response(responseData, { status, headers: this.headers });
|
|
425
|
+
// },
|
|
426
|
+
// json<T>(object: T, status: number = 200): Response {
|
|
427
|
+
// // this.status = status;
|
|
428
|
+
// // if (!this.headers.has("Content-Type")) {
|
|
429
|
+
// // this.headers.set("Content-Type", "application/json; charset=utf-8");
|
|
430
|
+
// // }
|
|
431
|
+
// return Response.json(object, { status, headers: this.headers })
|
|
432
|
+
// },
|
|
433
|
+
// file(filePath: string, mime_Type?: string, status: number = 200): Response {
|
|
434
|
+
// // this.status = status;
|
|
435
|
+
// const file = Bun.file(filePath);
|
|
436
|
+
// if (!this.headers.has("Content-Type")) {
|
|
437
|
+
// this.headers.set("Content-Type", mime_Type ?? getMimeType(filePath));
|
|
438
|
+
// }
|
|
439
|
+
// return new Response(file, { status, headers: this.headers });
|
|
440
|
+
// },
|
|
441
|
+
// async ejs(viewPath: string, data = {}, status: number = 200): Promise<Response> {
|
|
442
|
+
// // this.status = status;
|
|
443
|
+
// const ejs = await getEjs();
|
|
444
|
+
// try {
|
|
445
|
+
// const template = await Bun.file(viewPath).text()
|
|
446
|
+
// const rendered = ejs.render(template, data)
|
|
447
|
+
// const headers = new Headers({ "Content-Type": "text/html; charset=utf-8" });
|
|
448
|
+
// return new Response(rendered, { status, headers });
|
|
449
|
+
// } catch (error) {
|
|
450
|
+
// console.error("EJS Rendering Error:", error);
|
|
451
|
+
// return new Response("Error rendering template", { status: 500 });
|
|
452
|
+
// }
|
|
453
|
+
// },
|
|
454
|
+
// redirect(path: string, status: number = 302): Response {
|
|
455
|
+
// // this.status = status
|
|
456
|
+
// this.headers.set("Location", path);
|
|
457
|
+
// return new Response(null, { status, headers: this.headers });
|
|
458
|
+
// },
|
|
459
|
+
// stream(callback: (controller: ReadableStreamDefaultController) => void) {
|
|
460
|
+
// const headers = new Headers(this.headers)
|
|
461
|
+
// const stream = new ReadableStream({
|
|
462
|
+
// async start(controller) {
|
|
463
|
+
// await callback(controller);
|
|
464
|
+
// controller.close();
|
|
465
|
+
// },
|
|
466
|
+
// });
|
|
467
|
+
// return new Response(stream, {
|
|
468
|
+
// headers
|
|
469
|
+
// });
|
|
470
|
+
// },
|
|
471
|
+
// yieldStream(callback: () => AsyncIterable<any>): Response {
|
|
472
|
+
// return new Response("not working stream yet.")
|
|
473
|
+
// // return new Response(
|
|
474
|
+
// // {
|
|
475
|
+
// // async *[Symbol.asyncIterator ]() {
|
|
476
|
+
// // yield* callback();
|
|
477
|
+
// // },
|
|
478
|
+
// // },
|
|
479
|
+
// // { headers: this.headers }
|
|
480
|
+
// // );
|
|
481
|
+
// },
|
|
482
|
+
// setCookie(
|
|
483
|
+
// name: string,
|
|
484
|
+
// value: string,
|
|
485
|
+
// options: CookieOptions = {}
|
|
486
|
+
// ): ContextType {
|
|
487
|
+
// let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(
|
|
488
|
+
// value
|
|
489
|
+
// )}`;
|
|
490
|
+
// if (options.maxAge) cookieString += `; Max-Age=${options.maxAge}`;
|
|
491
|
+
// if (options.expires)
|
|
492
|
+
// cookieString += `; Expires=${options.expires.toUTCString()}`;
|
|
493
|
+
// if (options.path) cookieString += `; Path=${options.path}`;
|
|
494
|
+
// if (options.domain) cookieString += `; Domain=${options.domain}`;
|
|
495
|
+
// if (options.secure) cookieString += `; Secure`;
|
|
496
|
+
// if (options.httpOnly) cookieString += `; HttpOnly`;
|
|
497
|
+
// if (options.sameSite) cookieString += `; SameSite=${options.sameSite}`;
|
|
498
|
+
// this.headers.append("Set-Cookie", cookieString);
|
|
499
|
+
// return this;
|
|
500
|
+
// },
|
|
501
|
+
// get cookies(): Record<string, string> {
|
|
502
|
+
// if (!parsedCookies) {
|
|
503
|
+
// const cookieHeader = this.req.headers.get("cookie");
|
|
504
|
+
// parsedCookies = cookieHeader ? parseCookie(cookieHeader) : {};
|
|
505
|
+
// }
|
|
506
|
+
// return parsedCookies;
|
|
507
|
+
// },
|
|
508
|
+
// };
|
|
509
|
+
// }
|