tezx 1.0.2 → 1.0.4
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 +243 -0
- package/dist/index.d.ts +849 -705
- package/dist/index.js +218 -184
- package/dist/index.mjs +232 -185
- package/package.json +11 -5
package/dist/index.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
let GlobalConfig = class {
|
|
4
4
|
static notFound = (ctx2) => {
|
|
5
5
|
const {
|
|
6
6
|
method,
|
|
7
|
-
urlRef: { pathname }
|
|
7
|
+
urlRef: { pathname },
|
|
8
8
|
} = ctx2.req;
|
|
9
|
-
return ctx2.text(
|
|
10
|
-
|
|
9
|
+
return ctx2.text(
|
|
10
|
+
`${method}: '${pathname}' could not find
|
|
11
|
+
`,
|
|
12
|
+
404,
|
|
13
|
+
);
|
|
11
14
|
};
|
|
12
15
|
static onError = (err, ctx2) => {
|
|
13
16
|
return ctx2.text(err, 500);
|
|
@@ -29,7 +32,7 @@ function denoAdapter(TezX2) {
|
|
|
29
32
|
return new Response(response.body, {
|
|
30
33
|
status: response.status,
|
|
31
34
|
statusText: response.statusText || "",
|
|
32
|
-
headers: new Headers(response.headers)
|
|
35
|
+
headers: new Headers(response.headers),
|
|
33
36
|
});
|
|
34
37
|
}
|
|
35
38
|
}
|
|
@@ -56,7 +59,7 @@ function denoAdapter(TezX2) {
|
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
61
|
return {
|
|
59
|
-
listen
|
|
62
|
+
listen,
|
|
60
63
|
};
|
|
61
64
|
}
|
|
62
65
|
function bunAdapter(TezX2) {
|
|
@@ -76,10 +79,10 @@ function bunAdapter(TezX2) {
|
|
|
76
79
|
return new Response(response.body, {
|
|
77
80
|
status: response.status,
|
|
78
81
|
statusText: response.statusText || "",
|
|
79
|
-
headers: new Headers(response.headers)
|
|
82
|
+
headers: new Headers(response.headers),
|
|
80
83
|
});
|
|
81
84
|
}
|
|
82
|
-
}
|
|
85
|
+
},
|
|
83
86
|
});
|
|
84
87
|
const protocol = "\x1B[1;34mhttp\x1B[0m";
|
|
85
88
|
const message = `\x1B[1m Bun TezX Server running at ${protocol}://localhost:${port}/\x1B[0m`;
|
|
@@ -97,50 +100,52 @@ function bunAdapter(TezX2) {
|
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
102
|
return {
|
|
100
|
-
listen
|
|
103
|
+
listen,
|
|
101
104
|
};
|
|
102
105
|
}
|
|
103
106
|
function nodeAdapter(TezX2) {
|
|
104
107
|
function listen(port, callback) {
|
|
105
|
-
import(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const headers = Object.fromEntries(await response.headers.entries());
|
|
113
|
-
if (statusText) {
|
|
114
|
-
res.statusMessage = statusText;
|
|
115
|
-
}
|
|
116
|
-
res.writeHead(response.status, headers);
|
|
117
|
-
const { Readable } = await import('stream');
|
|
118
|
-
if (response.body instanceof Readable) {
|
|
119
|
-
response.body.pipe(res);
|
|
120
|
-
} else {
|
|
121
|
-
const body = await response.arrayBuffer();
|
|
122
|
-
res.end(Buffer.from(body));
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
server.listen(port, () => {
|
|
126
|
-
const protocol = "\x1B[1;34mhttp\x1B[0m";
|
|
127
|
-
const message = `\x1B[1m NodeJS TezX Server running at ${protocol}://localhost:${port}/\x1B[0m`;
|
|
128
|
-
if (typeof callback == "function") {
|
|
129
|
-
callback(message);
|
|
130
|
-
} else {
|
|
131
|
-
const logger = GlobalConfig.loggerFn();
|
|
132
|
-
if (logger.success) {
|
|
133
|
-
logger.success(message);
|
|
108
|
+
import("http")
|
|
109
|
+
.then((r) => {
|
|
110
|
+
let server = r.createServer(async (req, res) => {
|
|
111
|
+
const response = await TezX2.serve(req);
|
|
112
|
+
const statusText = response?.statusText;
|
|
113
|
+
if (!(response instanceof Response)) {
|
|
114
|
+
throw new Error("Invalid response from TezX.serve");
|
|
134
115
|
}
|
|
135
|
-
|
|
136
|
-
|
|
116
|
+
const headers = Object.fromEntries(await response.headers.entries());
|
|
117
|
+
if (statusText) {
|
|
118
|
+
res.statusMessage = statusText;
|
|
119
|
+
}
|
|
120
|
+
res.writeHead(response.status, headers);
|
|
121
|
+
const { Readable } = await import("stream");
|
|
122
|
+
if (response.body instanceof Readable) {
|
|
123
|
+
response.body.pipe(res);
|
|
124
|
+
} else {
|
|
125
|
+
const body = await response.arrayBuffer();
|
|
126
|
+
res.end(Buffer.from(body));
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
server.listen(port, () => {
|
|
130
|
+
const protocol = "\x1B[1;34mhttp\x1B[0m";
|
|
131
|
+
const message = `\x1B[1m NodeJS TezX Server running at ${protocol}://localhost:${port}/\x1B[0m`;
|
|
132
|
+
if (typeof callback == "function") {
|
|
133
|
+
callback(message);
|
|
134
|
+
} else {
|
|
135
|
+
const logger = GlobalConfig.loggerFn();
|
|
136
|
+
if (logger.success) {
|
|
137
|
+
logger.success(message);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return server;
|
|
141
|
+
});
|
|
142
|
+
})
|
|
143
|
+
.catch((r) => {
|
|
144
|
+
throw Error(r.message);
|
|
137
145
|
});
|
|
138
|
-
}).catch((r) => {
|
|
139
|
-
throw Error(r.message);
|
|
140
|
-
});
|
|
141
146
|
}
|
|
142
147
|
return {
|
|
143
|
-
listen
|
|
148
|
+
listen,
|
|
144
149
|
};
|
|
145
150
|
}
|
|
146
151
|
|
|
@@ -286,7 +291,7 @@ const mimeTypes = {
|
|
|
286
291
|
ai: "application/postscript",
|
|
287
292
|
swf: "application/x-shockwave-flash",
|
|
288
293
|
jar: "application/java-archive",
|
|
289
|
-
gcode: "text/x.gcode"
|
|
294
|
+
gcode: "text/x.gcode",
|
|
290
295
|
};
|
|
291
296
|
const defaultMimeType = "application/octet-stream";
|
|
292
297
|
async function getFiles(dir, basePath = "/", ref, option) {
|
|
@@ -297,36 +302,36 @@ async function getFiles(dir, basePath = "/", ref, option) {
|
|
|
297
302
|
const path = `${dir}/${entry.name}`;
|
|
298
303
|
if (entry.isDirectory) {
|
|
299
304
|
files.push(
|
|
300
|
-
...await getFiles(path, `${basePath}/${entry.name}`, ref, option)
|
|
305
|
+
...(await getFiles(path, `${basePath}/${entry.name}`, ref, option)),
|
|
301
306
|
);
|
|
302
307
|
} else {
|
|
303
308
|
const x = `${basePath}/${entry.name}`;
|
|
304
309
|
files.push({
|
|
305
310
|
file: path,
|
|
306
|
-
path: x.replace(/\\/g, "/")
|
|
311
|
+
path: x.replace(/\\/g, "/"),
|
|
307
312
|
});
|
|
308
313
|
}
|
|
309
314
|
}
|
|
310
315
|
} else {
|
|
311
|
-
const fs = await import(
|
|
312
|
-
const path = await import(
|
|
316
|
+
const fs = await import("fs/promises");
|
|
317
|
+
const path = await import("path");
|
|
313
318
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
314
319
|
for (const entry of entries) {
|
|
315
320
|
const fullPath = path.join(dir, entry.name);
|
|
316
321
|
if (entry.isDirectory()) {
|
|
317
322
|
files.push(
|
|
318
|
-
...await getFiles(
|
|
323
|
+
...(await getFiles(
|
|
319
324
|
fullPath,
|
|
320
325
|
`${basePath}/${entry.name}`,
|
|
321
326
|
ref,
|
|
322
|
-
option
|
|
323
|
-
)
|
|
327
|
+
option,
|
|
328
|
+
)),
|
|
324
329
|
);
|
|
325
330
|
} else {
|
|
326
331
|
const path2 = `${basePath}/${entry.name}`;
|
|
327
332
|
files.push({
|
|
328
333
|
file: fullPath,
|
|
329
|
-
path: path2.replace(/\\/g, "/")
|
|
334
|
+
path: path2.replace(/\\/g, "/"),
|
|
330
335
|
});
|
|
331
336
|
}
|
|
332
337
|
}
|
|
@@ -349,7 +354,7 @@ class TezResponse {
|
|
|
349
354
|
static json(body, ...args) {
|
|
350
355
|
let status = 200;
|
|
351
356
|
let headers = {
|
|
352
|
-
"Content-Type": "application/json; charset=utf-8"
|
|
357
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
353
358
|
};
|
|
354
359
|
if (typeof args[0] === "number") {
|
|
355
360
|
status = args[0];
|
|
@@ -361,13 +366,13 @@ class TezResponse {
|
|
|
361
366
|
}
|
|
362
367
|
return new Response(JSON.stringify(body), {
|
|
363
368
|
status,
|
|
364
|
-
headers
|
|
369
|
+
headers,
|
|
365
370
|
});
|
|
366
371
|
}
|
|
367
372
|
static html(data, ...args) {
|
|
368
373
|
let status = 200;
|
|
369
374
|
let headers = {
|
|
370
|
-
"Content-Type": "text/html; charset=utf-8"
|
|
375
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
371
376
|
};
|
|
372
377
|
if (typeof args[0] === "number") {
|
|
373
378
|
status = args[0];
|
|
@@ -379,13 +384,13 @@ class TezResponse {
|
|
|
379
384
|
}
|
|
380
385
|
return new Response(data, {
|
|
381
386
|
status,
|
|
382
|
-
headers
|
|
387
|
+
headers,
|
|
383
388
|
});
|
|
384
389
|
}
|
|
385
390
|
static text(data, ...args) {
|
|
386
391
|
let status = 200;
|
|
387
392
|
let headers = {
|
|
388
|
-
"Content-Type": "text/plain; charset=utf-8"
|
|
393
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
389
394
|
};
|
|
390
395
|
if (typeof args[0] === "number") {
|
|
391
396
|
status = args[0];
|
|
@@ -397,13 +402,13 @@ class TezResponse {
|
|
|
397
402
|
}
|
|
398
403
|
return new Response(data, {
|
|
399
404
|
status,
|
|
400
|
-
headers
|
|
405
|
+
headers,
|
|
401
406
|
});
|
|
402
407
|
}
|
|
403
408
|
static xml(data, ...args) {
|
|
404
409
|
let status = 200;
|
|
405
410
|
let headers = {
|
|
406
|
-
"Content-Type": "application/xml; charset=utf-8"
|
|
411
|
+
"Content-Type": "application/xml; charset=utf-8",
|
|
407
412
|
};
|
|
408
413
|
if (typeof args[0] === "number") {
|
|
409
414
|
status = args[0];
|
|
@@ -415,7 +420,7 @@ class TezResponse {
|
|
|
415
420
|
}
|
|
416
421
|
return new Response(data, {
|
|
417
422
|
status,
|
|
418
|
-
headers
|
|
423
|
+
headers,
|
|
419
424
|
});
|
|
420
425
|
}
|
|
421
426
|
static send(body, ...args) {
|
|
@@ -441,7 +446,7 @@ class TezResponse {
|
|
|
441
446
|
}
|
|
442
447
|
return new Response(body, {
|
|
443
448
|
status,
|
|
444
|
-
headers
|
|
449
|
+
headers,
|
|
445
450
|
});
|
|
446
451
|
}
|
|
447
452
|
/**
|
|
@@ -454,7 +459,7 @@ class TezResponse {
|
|
|
454
459
|
static redirect(url, status = 302, headers) {
|
|
455
460
|
return new Response(null, {
|
|
456
461
|
status,
|
|
457
|
-
headers: { Location: url }
|
|
462
|
+
headers: { Location: url },
|
|
458
463
|
});
|
|
459
464
|
}
|
|
460
465
|
/**
|
|
@@ -468,7 +473,7 @@ class TezResponse {
|
|
|
468
473
|
let fileExists = false;
|
|
469
474
|
const runtime = EnvironmentDetector.getEnvironment;
|
|
470
475
|
if (runtime === "node") {
|
|
471
|
-
const { existsSync } = await import(
|
|
476
|
+
const { existsSync } = await import("fs");
|
|
472
477
|
fileExists = existsSync(filePath);
|
|
473
478
|
} else if (runtime === "bun") {
|
|
474
479
|
fileExists = Bun.file(filePath).exists();
|
|
@@ -485,10 +490,12 @@ class TezResponse {
|
|
|
485
490
|
}
|
|
486
491
|
let fileBuffer;
|
|
487
492
|
if (runtime === "node") {
|
|
488
|
-
const { readFileSync } = await import(
|
|
493
|
+
const { readFileSync } = await import("fs");
|
|
489
494
|
fileBuffer = await readFileSync(filePath);
|
|
490
495
|
} else if (runtime === "bun") {
|
|
491
|
-
fileBuffer = await Bun.file(filePath)
|
|
496
|
+
fileBuffer = await Bun.file(filePath)
|
|
497
|
+
.arrayBuffer()
|
|
498
|
+
.then((buf) => new Uint8Array(buf));
|
|
492
499
|
} else if (runtime === "deno") {
|
|
493
500
|
fileBuffer = await Deno.readFile(filePath);
|
|
494
501
|
}
|
|
@@ -497,8 +504,8 @@ class TezResponse {
|
|
|
497
504
|
headers: {
|
|
498
505
|
"Content-Disposition": `attachment; filename="${fileName}"`,
|
|
499
506
|
"Content-Type": "application/octet-stream",
|
|
500
|
-
"Content-Length": fileBuffer.byteLength.toString()
|
|
501
|
-
}
|
|
507
|
+
"Content-Length": fileBuffer.byteLength.toString(),
|
|
508
|
+
},
|
|
502
509
|
});
|
|
503
510
|
} catch (error) {
|
|
504
511
|
throw Error("Internal Server Error" + error?.message);
|
|
@@ -510,7 +517,7 @@ class TezResponse {
|
|
|
510
517
|
const resolvedPath = filePath;
|
|
511
518
|
let fileExists = false;
|
|
512
519
|
if (runtime === "node") {
|
|
513
|
-
const { existsSync } = await import(
|
|
520
|
+
const { existsSync } = await import("fs");
|
|
514
521
|
fileExists = existsSync(resolvedPath);
|
|
515
522
|
} else if (runtime === "bun") {
|
|
516
523
|
fileExists = Bun.file(resolvedPath).exists();
|
|
@@ -527,7 +534,7 @@ class TezResponse {
|
|
|
527
534
|
}
|
|
528
535
|
let fileSize = 0;
|
|
529
536
|
if (runtime === "node") {
|
|
530
|
-
const { statSync } = await import(
|
|
537
|
+
const { statSync } = await import("fs");
|
|
531
538
|
fileSize = statSync(resolvedPath).size;
|
|
532
539
|
} else if (runtime === "bun") {
|
|
533
540
|
fileSize = (await Bun.file(resolvedPath).arrayBuffer()).byteLength;
|
|
@@ -539,7 +546,7 @@ class TezResponse {
|
|
|
539
546
|
const mimeType = mimeTypes[ext] || defaultMimeType;
|
|
540
547
|
let fileStream;
|
|
541
548
|
if (runtime === "node") {
|
|
542
|
-
const { createReadStream } = await import(
|
|
549
|
+
const { createReadStream } = await import("fs");
|
|
543
550
|
fileStream = createReadStream(resolvedPath);
|
|
544
551
|
} else if (runtime === "bun") {
|
|
545
552
|
fileStream = Bun.file(resolvedPath).stream();
|
|
@@ -549,7 +556,7 @@ class TezResponse {
|
|
|
549
556
|
}
|
|
550
557
|
let headers = {
|
|
551
558
|
"Content-Type": mimeType,
|
|
552
|
-
"Content-Length": fileSize.toString()
|
|
559
|
+
"Content-Length": fileSize.toString(),
|
|
553
560
|
};
|
|
554
561
|
let fileName = "";
|
|
555
562
|
if (typeof args[0] === "string") {
|
|
@@ -565,7 +572,7 @@ class TezResponse {
|
|
|
565
572
|
}
|
|
566
573
|
return new Response(fileStream, {
|
|
567
574
|
status: 200,
|
|
568
|
-
headers
|
|
575
|
+
headers,
|
|
569
576
|
});
|
|
570
577
|
} catch (error) {
|
|
571
578
|
throw Error("Internal Server Error" + error?.message);
|
|
@@ -596,11 +603,15 @@ class CommonHandler {
|
|
|
596
603
|
}
|
|
597
604
|
|
|
598
605
|
function sanitizePathSplit(basePath, path) {
|
|
599
|
-
const parts = `${basePath}/${path}
|
|
606
|
+
const parts = `${basePath}/${path}`
|
|
607
|
+
.replace(/\\/g, "")
|
|
608
|
+
?.split("/")
|
|
609
|
+
.filter(Boolean);
|
|
600
610
|
return parts;
|
|
601
611
|
}
|
|
602
612
|
function urlParse(url) {
|
|
603
|
-
const urlPattern =
|
|
613
|
+
const urlPattern =
|
|
614
|
+
/^(?:(\w+):\/\/)?(?:([^:@]+)?(?::([^@]+))?@)?([^:/?#]+)?(?::(\d+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/;
|
|
604
615
|
let matches = url.match(urlPattern);
|
|
605
616
|
const [
|
|
606
617
|
_,
|
|
@@ -611,7 +622,7 @@ function urlParse(url) {
|
|
|
611
622
|
port,
|
|
612
623
|
path,
|
|
613
624
|
queryString,
|
|
614
|
-
hash
|
|
625
|
+
hash,
|
|
615
626
|
] = matches;
|
|
616
627
|
let origin = hostname;
|
|
617
628
|
if (protocol) {
|
|
@@ -626,15 +637,13 @@ function urlParse(url) {
|
|
|
626
637
|
if (queryString) {
|
|
627
638
|
const queryPart = decodeURIComponent(queryString);
|
|
628
639
|
const keyValuePairs = queryPart.split("&");
|
|
629
|
-
const paramsObj = keyValuePairs?.map(
|
|
630
|
-
(
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
);
|
|
637
|
-
return paramsObj.reduce(function(total, value) {
|
|
640
|
+
const paramsObj = keyValuePairs?.map((keyValue) => {
|
|
641
|
+
const [key, value] = keyValue.split("=");
|
|
642
|
+
return {
|
|
643
|
+
[key]: value,
|
|
644
|
+
};
|
|
645
|
+
});
|
|
646
|
+
return paramsObj.reduce(function (total, value) {
|
|
638
647
|
return { ...total, ...value };
|
|
639
648
|
}, {});
|
|
640
649
|
} else {
|
|
@@ -651,7 +660,7 @@ function urlParse(url) {
|
|
|
651
660
|
hostname,
|
|
652
661
|
href: url,
|
|
653
662
|
port,
|
|
654
|
-
query: query()
|
|
663
|
+
query: query(),
|
|
655
664
|
};
|
|
656
665
|
}
|
|
657
666
|
|
|
@@ -763,7 +772,7 @@ class Router extends MiddlewareConfigure {
|
|
|
763
772
|
break;
|
|
764
773
|
default:
|
|
765
774
|
throw new Error(
|
|
766
|
-
`\x1B[1;31m404 Not Found\x1B[0m \x1B[1;32mInvalid arguments\x1B[0m
|
|
775
|
+
`\x1B[1;31m404 Not Found\x1B[0m \x1B[1;32mInvalid arguments\x1B[0m`,
|
|
767
776
|
);
|
|
768
777
|
}
|
|
769
778
|
getFiles(dir, route, this, options);
|
|
@@ -832,7 +841,7 @@ class Router extends MiddlewareConfigure {
|
|
|
832
841
|
*/
|
|
833
842
|
group(prefix, callback) {
|
|
834
843
|
const router = new Router({
|
|
835
|
-
basePath: prefix
|
|
844
|
+
basePath: prefix,
|
|
836
845
|
// env: this.env
|
|
837
846
|
});
|
|
838
847
|
callback(router);
|
|
@@ -895,7 +904,7 @@ class Router extends MiddlewareConfigure {
|
|
|
895
904
|
}
|
|
896
905
|
if (!middlewares.every((middleware) => typeof middleware === "function")) {
|
|
897
906
|
throw new Error(
|
|
898
|
-
"Middleware must be a function or an array of functions."
|
|
907
|
+
"Middleware must be a function or an array of functions.",
|
|
899
908
|
);
|
|
900
909
|
}
|
|
901
910
|
this.#addRoute(method, path, callback, middlewares);
|
|
@@ -913,7 +922,7 @@ class Router extends MiddlewareConfigure {
|
|
|
913
922
|
handler = /* @__PURE__ */ new Map();
|
|
914
923
|
handler.set(method, {
|
|
915
924
|
callback,
|
|
916
|
-
middlewares: finalMiddleware
|
|
925
|
+
middlewares: finalMiddleware,
|
|
917
926
|
});
|
|
918
927
|
return this.routers.set(p, handler);
|
|
919
928
|
}
|
|
@@ -941,7 +950,7 @@ class Router extends MiddlewareConfigure {
|
|
|
941
950
|
if (!GlobalConfig.overwriteMethod && node.handlers.has(method)) return;
|
|
942
951
|
node.handlers.set(method, {
|
|
943
952
|
callback,
|
|
944
|
-
middlewares: finalMiddleware
|
|
953
|
+
middlewares: finalMiddleware,
|
|
945
954
|
});
|
|
946
955
|
node.pathname = path;
|
|
947
956
|
}
|
|
@@ -1057,9 +1066,7 @@ class Router extends MiddlewareConfigure {
|
|
|
1057
1066
|
if (n.children.has(path)) {
|
|
1058
1067
|
let findNode = n.children.get(path);
|
|
1059
1068
|
if (GlobalConfig.allowDuplicateMw) {
|
|
1060
|
-
findNode.middlewares.push(
|
|
1061
|
-
...middlewareNode.middlewares
|
|
1062
|
-
);
|
|
1069
|
+
findNode.middlewares.push(...middlewareNode.middlewares);
|
|
1063
1070
|
} else {
|
|
1064
1071
|
for (const mw of middlewareNode.middlewares) {
|
|
1065
1072
|
if (findNode.middlewares.has(mw)) {
|
|
@@ -1077,9 +1084,7 @@ class Router extends MiddlewareConfigure {
|
|
|
1077
1084
|
}
|
|
1078
1085
|
}
|
|
1079
1086
|
if (GlobalConfig.allowDuplicateMw) {
|
|
1080
|
-
rootMiddlewares.middlewares.push(
|
|
1081
|
-
...routerMiddlewares.middlewares
|
|
1082
|
-
);
|
|
1087
|
+
rootMiddlewares.middlewares.push(...routerMiddlewares.middlewares);
|
|
1083
1088
|
} else {
|
|
1084
1089
|
for (const mw of routerMiddlewares.middlewares) {
|
|
1085
1090
|
if (rootMiddlewares.middlewares.has(mw)) {
|
|
@@ -1333,40 +1338,53 @@ async function parseMultipartBody(req, boundary, options) {
|
|
|
1333
1338
|
const fieldName = fieldNameMatch[1];
|
|
1334
1339
|
const contentType = contentTypeMatch[1];
|
|
1335
1340
|
if (options?.sanitized) {
|
|
1336
|
-
filename =
|
|
1341
|
+
filename =
|
|
1342
|
+
`${Date.now()}-${filename.replace(/\s+/g, "")?.replace(/[^a-zA-Z0-9.-]/g, "-")}`?.toLowerCase();
|
|
1337
1343
|
}
|
|
1338
|
-
if (
|
|
1344
|
+
if (
|
|
1345
|
+
Array.isArray(options?.allowedTypes) &&
|
|
1346
|
+
!options.allowedTypes?.includes(contentType)
|
|
1347
|
+
) {
|
|
1339
1348
|
reject(
|
|
1340
|
-
`Invalid file type: "${contentType}". Allowed types: ${options.allowedTypes.join(", ")}
|
|
1349
|
+
`Invalid file type: "${contentType}". Allowed types: ${options.allowedTypes.join(", ")}`,
|
|
1341
1350
|
);
|
|
1342
1351
|
}
|
|
1343
1352
|
const fileContentStartIndex = part.indexOf("\r\n\r\n") + 4;
|
|
1344
1353
|
const fileContent = Buffer.from(
|
|
1345
1354
|
part.substring(fileContentStartIndex),
|
|
1346
|
-
"binary"
|
|
1355
|
+
"binary",
|
|
1347
1356
|
);
|
|
1348
1357
|
const arrayBuffer = fileContent.buffer.slice(
|
|
1349
1358
|
fileContent.byteOffset,
|
|
1350
|
-
fileContent.byteOffset + fileContent.byteLength
|
|
1359
|
+
fileContent.byteOffset + fileContent.byteLength,
|
|
1351
1360
|
);
|
|
1352
|
-
if (
|
|
1361
|
+
if (
|
|
1362
|
+
typeof options?.maxSize !== "undefined" &&
|
|
1363
|
+
fileContent.byteLength > options.maxSize
|
|
1364
|
+
) {
|
|
1353
1365
|
reject(
|
|
1354
|
-
`File size exceeds the limit: ${fileContent.byteLength} bytes (Max: ${options.maxSize} bytes)
|
|
1366
|
+
`File size exceeds the limit: ${fileContent.byteLength} bytes (Max: ${options.maxSize} bytes)`,
|
|
1355
1367
|
);
|
|
1356
1368
|
}
|
|
1357
1369
|
const file = new File([arrayBuffer], filename, {
|
|
1358
|
-
type: contentType
|
|
1370
|
+
type: contentType,
|
|
1359
1371
|
});
|
|
1360
|
-
if (
|
|
1372
|
+
if (
|
|
1373
|
+
typeof options?.maxFiles != "undefined" &&
|
|
1374
|
+
options.maxFiles == 0
|
|
1375
|
+
) {
|
|
1361
1376
|
reject(
|
|
1362
|
-
`Field "${fieldName}" exceeds the maximum allowed file count of ${options.maxFiles}
|
|
1377
|
+
`Field "${fieldName}" exceeds the maximum allowed file count of ${options.maxFiles}.`,
|
|
1363
1378
|
);
|
|
1364
1379
|
}
|
|
1365
1380
|
if (formDataField[fieldName]) {
|
|
1366
1381
|
if (Array.isArray(formDataField[fieldName])) {
|
|
1367
|
-
if (
|
|
1382
|
+
if (
|
|
1383
|
+
typeof options?.maxFiles != "undefined" &&
|
|
1384
|
+
formDataField[fieldName]?.length >= options.maxFiles
|
|
1385
|
+
) {
|
|
1368
1386
|
reject(
|
|
1369
|
-
`Field "${fieldName}" exceeds the maximum allowed file count of ${options.maxFiles}
|
|
1387
|
+
`Field "${fieldName}" exceeds the maximum allowed file count of ${options.maxFiles}.`,
|
|
1370
1388
|
);
|
|
1371
1389
|
}
|
|
1372
1390
|
formDataField[fieldName].push(file);
|
|
@@ -1380,8 +1398,7 @@ async function parseMultipartBody(req, boundary, options) {
|
|
|
1380
1398
|
}
|
|
1381
1399
|
}
|
|
1382
1400
|
resolve(formDataField);
|
|
1383
|
-
} catch {
|
|
1384
|
-
}
|
|
1401
|
+
} catch {}
|
|
1385
1402
|
});
|
|
1386
1403
|
});
|
|
1387
1404
|
} else if (runtime === "deno" || runtime === "bun") {
|
|
@@ -1392,32 +1409,43 @@ async function parseMultipartBody(req, boundary, options) {
|
|
|
1392
1409
|
if (val instanceof File && typeof options == "object") {
|
|
1393
1410
|
let filename = val.name;
|
|
1394
1411
|
if (options?.sanitized) {
|
|
1395
|
-
filename =
|
|
1412
|
+
filename =
|
|
1413
|
+
`${Date.now()}-${filename.replace(/\s+/g, "")?.replace(/[^a-zA-Z0-9.-]/g, "-")}`?.toLowerCase();
|
|
1396
1414
|
}
|
|
1397
|
-
if (
|
|
1415
|
+
if (
|
|
1416
|
+
Array.isArray(options?.allowedTypes) &&
|
|
1417
|
+
!options.allowedTypes?.includes(val.type)
|
|
1418
|
+
) {
|
|
1398
1419
|
throw new Error(
|
|
1399
|
-
`Invalid file type: "${val.type}". Allowed types: ${options.allowedTypes.join(", ")}
|
|
1420
|
+
`Invalid file type: "${val.type}". Allowed types: ${options.allowedTypes.join(", ")}`,
|
|
1400
1421
|
);
|
|
1401
1422
|
}
|
|
1402
|
-
if (
|
|
1423
|
+
if (
|
|
1424
|
+
typeof options?.maxSize !== "undefined" &&
|
|
1425
|
+
val.size > options.maxSize
|
|
1426
|
+
) {
|
|
1403
1427
|
throw new Error(
|
|
1404
|
-
`File size exceeds the limit: ${val.size} bytes (Max: ${options.maxSize} bytes)
|
|
1428
|
+
`File size exceeds the limit: ${val.size} bytes (Max: ${options.maxSize} bytes)`,
|
|
1405
1429
|
);
|
|
1406
1430
|
}
|
|
1407
1431
|
if (typeof options?.maxFiles != "undefined" && options.maxFiles == 0) {
|
|
1408
1432
|
throw new Error(
|
|
1409
|
-
`Field "${key}" exceeds the maximum allowed file count of ${options.maxFiles}
|
|
1433
|
+
`Field "${key}" exceeds the maximum allowed file count of ${options.maxFiles}.`,
|
|
1410
1434
|
);
|
|
1411
1435
|
}
|
|
1412
1436
|
val = new File([await val.arrayBuffer()], filename, {
|
|
1413
|
-
type: val.type
|
|
1437
|
+
type: val.type,
|
|
1414
1438
|
});
|
|
1415
1439
|
}
|
|
1416
1440
|
if (result[key]) {
|
|
1417
1441
|
if (Array.isArray(result[key])) {
|
|
1418
|
-
if (
|
|
1442
|
+
if (
|
|
1443
|
+
val instanceof File &&
|
|
1444
|
+
typeof options?.maxFiles != "undefined" &&
|
|
1445
|
+
result[key]?.length >= options.maxFiles
|
|
1446
|
+
) {
|
|
1419
1447
|
throw new Error(
|
|
1420
|
-
`Field "${key}" exceeds the maximum allowed file count of ${options.maxFiles}
|
|
1448
|
+
`Field "${key}" exceeds the maximum allowed file count of ${options.maxFiles}.`,
|
|
1421
1449
|
);
|
|
1422
1450
|
}
|
|
1423
1451
|
result[key].push(val);
|
|
@@ -1457,7 +1485,7 @@ class Request {
|
|
|
1457
1485
|
port: void 0,
|
|
1458
1486
|
href: void 0,
|
|
1459
1487
|
query: {},
|
|
1460
|
-
pathname: "/"
|
|
1488
|
+
pathname: "/",
|
|
1461
1489
|
};
|
|
1462
1490
|
/** Query parameters extracted from the URL */
|
|
1463
1491
|
query;
|
|
@@ -1664,7 +1692,7 @@ const httpStatusMap = {
|
|
|
1664
1692
|
507: "Insufficient Storage",
|
|
1665
1693
|
508: "Loop Detected",
|
|
1666
1694
|
510: "Not Extended",
|
|
1667
|
-
511: "Network Authentication Required"
|
|
1695
|
+
511: "Network Authentication Required",
|
|
1668
1696
|
};
|
|
1669
1697
|
class Context {
|
|
1670
1698
|
#rawRequest;
|
|
@@ -1848,7 +1876,7 @@ class Context {
|
|
|
1848
1876
|
const value = "";
|
|
1849
1877
|
const cookieOptions = {
|
|
1850
1878
|
...options,
|
|
1851
|
-
expires: /* @__PURE__ */ new Date(0)
|
|
1879
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
1852
1880
|
// Set expiration time to the past
|
|
1853
1881
|
};
|
|
1854
1882
|
const cookieHeader = `${name}=${value};${serializeOptions(cookieOptions)}`;
|
|
@@ -1863,13 +1891,13 @@ class Context {
|
|
|
1863
1891
|
set: (name, value, options) => {
|
|
1864
1892
|
const cookieHeader = `${name}=${value};${serializeOptions(options || {})}`;
|
|
1865
1893
|
this.headers.set("Set-Cookie", cookieHeader);
|
|
1866
|
-
}
|
|
1894
|
+
},
|
|
1867
1895
|
};
|
|
1868
1896
|
}
|
|
1869
1897
|
json(body, ...args) {
|
|
1870
1898
|
let status = this.#status;
|
|
1871
1899
|
let headers = {
|
|
1872
|
-
"Content-Type": "application/json; charset=utf-8"
|
|
1900
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
1873
1901
|
};
|
|
1874
1902
|
if (typeof args[0] === "number") {
|
|
1875
1903
|
status = args[0];
|
|
@@ -1881,7 +1909,7 @@ class Context {
|
|
|
1881
1909
|
}
|
|
1882
1910
|
return new Response(JSON.stringify(body), {
|
|
1883
1911
|
status,
|
|
1884
|
-
headers
|
|
1912
|
+
headers,
|
|
1885
1913
|
});
|
|
1886
1914
|
}
|
|
1887
1915
|
send(body, ...args) {
|
|
@@ -1907,13 +1935,13 @@ class Context {
|
|
|
1907
1935
|
}
|
|
1908
1936
|
return new Response(body, {
|
|
1909
1937
|
status,
|
|
1910
|
-
headers
|
|
1938
|
+
headers,
|
|
1911
1939
|
});
|
|
1912
1940
|
}
|
|
1913
1941
|
html(data, ...args) {
|
|
1914
1942
|
let status = this.#status;
|
|
1915
1943
|
let headers = {
|
|
1916
|
-
"Content-Type": "text/html; charset=utf-8"
|
|
1944
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
1917
1945
|
};
|
|
1918
1946
|
if (typeof args[0] === "number") {
|
|
1919
1947
|
status = args[0];
|
|
@@ -1925,13 +1953,13 @@ class Context {
|
|
|
1925
1953
|
}
|
|
1926
1954
|
return new Response(data, {
|
|
1927
1955
|
status,
|
|
1928
|
-
headers
|
|
1956
|
+
headers,
|
|
1929
1957
|
});
|
|
1930
1958
|
}
|
|
1931
1959
|
text(data, ...args) {
|
|
1932
1960
|
let status = this.#status;
|
|
1933
1961
|
let headers = {
|
|
1934
|
-
"Content-Type": "text/plain; charset=utf-8"
|
|
1962
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
1935
1963
|
};
|
|
1936
1964
|
if (typeof args[0] === "number") {
|
|
1937
1965
|
status = args[0];
|
|
@@ -1943,13 +1971,13 @@ class Context {
|
|
|
1943
1971
|
}
|
|
1944
1972
|
return new Response(data, {
|
|
1945
1973
|
status,
|
|
1946
|
-
headers
|
|
1974
|
+
headers,
|
|
1947
1975
|
});
|
|
1948
1976
|
}
|
|
1949
1977
|
xml(data, ...args) {
|
|
1950
1978
|
let status = this.#status;
|
|
1951
1979
|
let headers = {
|
|
1952
|
-
"Content-Type": "application/xml; charset=utf-8"
|
|
1980
|
+
"Content-Type": "application/xml; charset=utf-8",
|
|
1953
1981
|
};
|
|
1954
1982
|
if (typeof args[0] === "number") {
|
|
1955
1983
|
status = args[0];
|
|
@@ -1961,7 +1989,7 @@ class Context {
|
|
|
1961
1989
|
}
|
|
1962
1990
|
return new Response(data, {
|
|
1963
1991
|
status,
|
|
1964
|
-
headers
|
|
1992
|
+
headers,
|
|
1965
1993
|
});
|
|
1966
1994
|
}
|
|
1967
1995
|
/**
|
|
@@ -2047,10 +2075,7 @@ function serializeOptions(options) {
|
|
|
2047
2075
|
return parts.join("; ");
|
|
2048
2076
|
}
|
|
2049
2077
|
|
|
2050
|
-
function useParams({
|
|
2051
|
-
path,
|
|
2052
|
-
urlPattern
|
|
2053
|
-
}) {
|
|
2078
|
+
function useParams({ path, urlPattern }) {
|
|
2054
2079
|
let params = {};
|
|
2055
2080
|
path = path.replace(/^\/+|\/+$/g, "");
|
|
2056
2081
|
urlPattern = urlPattern.replace(/^\/+|\/+$/g, "");
|
|
@@ -2066,11 +2091,16 @@ function useParams({
|
|
|
2066
2091
|
const patternSegment = patternSegments[i];
|
|
2067
2092
|
if (patternSegment?.startsWith("*")) {
|
|
2068
2093
|
const trailingPatterns = patternSegments.slice(i + 1);
|
|
2069
|
-
let paramName =
|
|
2094
|
+
let paramName =
|
|
2095
|
+
patternSegment.length == 1 ? "*" : patternSegment?.slice(1);
|
|
2070
2096
|
if (trailingPatterns.length > 0) {
|
|
2071
2097
|
const expectedTrailing = trailingPatterns.join("/");
|
|
2072
|
-
const actualTrailing = pathSegments
|
|
2073
|
-
|
|
2098
|
+
const actualTrailing = pathSegments
|
|
2099
|
+
.slice(pathLength - trailingPatterns.length)
|
|
2100
|
+
.join("/");
|
|
2101
|
+
const wildcardPath = pathSegments
|
|
2102
|
+
.slice(pathIndex, pathLength - trailingPatterns.length)
|
|
2103
|
+
.join("/");
|
|
2074
2104
|
if (expectedTrailing !== actualTrailing || !wildcardPath) {
|
|
2075
2105
|
return { success: false, params: {} };
|
|
2076
2106
|
}
|
|
@@ -2088,15 +2118,20 @@ function useParams({
|
|
|
2088
2118
|
if (patternSegment.startsWith(":") && patternSegment.endsWith("?")) {
|
|
2089
2119
|
const paramName = patternSegment.slice(1, -1);
|
|
2090
2120
|
const nextPattern = patternSegments[i + 1];
|
|
2091
|
-
if (
|
|
2092
|
-
|
|
2093
|
-
|
|
2121
|
+
if (
|
|
2122
|
+
nextPattern &&
|
|
2123
|
+
!nextPattern.startsWith(":") &&
|
|
2124
|
+
nextPattern !== "*" &&
|
|
2125
|
+
pathIndex < pathLength && // !/test == /:user?/test
|
|
2126
|
+
// বর্তমান পথ যদি পরবর্তী প্যাটার্ন মানে স্ট্যাটিক পথ এর সাথে মাইল তাহলে
|
|
2127
|
+
pathSegments[pathIndex] === nextPattern
|
|
2128
|
+
) {
|
|
2094
2129
|
params[paramName] = null;
|
|
2095
2130
|
continue;
|
|
2096
2131
|
}
|
|
2097
2132
|
const remainingPatterns = patternSegments.slice(i + 1);
|
|
2098
2133
|
const requiredCount = remainingPatterns.filter(
|
|
2099
|
-
(seg) => !(seg.startsWith(":") && seg.endsWith("?"))
|
|
2134
|
+
(seg) => !(seg.startsWith(":") && seg.endsWith("?")),
|
|
2100
2135
|
).length;
|
|
2101
2136
|
const remainingPath = pathLength - pathIndex;
|
|
2102
2137
|
if (remainingPath === requiredCount) {
|
|
@@ -2140,7 +2175,7 @@ class TezX extends Router {
|
|
|
2140
2175
|
env = {},
|
|
2141
2176
|
logger = void 0,
|
|
2142
2177
|
allowDuplicateMw = false,
|
|
2143
|
-
overwriteMethod = true
|
|
2178
|
+
overwriteMethod = true,
|
|
2144
2179
|
} = {}) {
|
|
2145
2180
|
GlobalConfig.allowDuplicateMw = allowDuplicateMw;
|
|
2146
2181
|
GlobalConfig.overwriteMethod = overwriteMethod;
|
|
@@ -2156,14 +2191,15 @@ class TezX extends Router {
|
|
|
2156
2191
|
for (let pattern of this.routers.keys()) {
|
|
2157
2192
|
const { success, params } = useParams({
|
|
2158
2193
|
path: pathname,
|
|
2159
|
-
urlPattern: pattern
|
|
2194
|
+
urlPattern: pattern,
|
|
2160
2195
|
});
|
|
2161
|
-
const handlers =
|
|
2196
|
+
const handlers =
|
|
2197
|
+
routers.get(pattern)?.get(method) || routers.get(pattern)?.get("ALL");
|
|
2162
2198
|
if (success && handlers) {
|
|
2163
2199
|
return {
|
|
2164
2200
|
callback: handlers.callback,
|
|
2165
2201
|
middlewares: handlers.middlewares,
|
|
2166
|
-
params
|
|
2202
|
+
params,
|
|
2167
2203
|
};
|
|
2168
2204
|
}
|
|
2169
2205
|
}
|
|
@@ -2189,7 +2225,7 @@ class TezX extends Router {
|
|
|
2189
2225
|
return {
|
|
2190
2226
|
middlewares: handlers.middlewares,
|
|
2191
2227
|
callback: handlers.callback,
|
|
2192
|
-
params
|
|
2228
|
+
params,
|
|
2193
2229
|
};
|
|
2194
2230
|
}
|
|
2195
2231
|
return null;
|
|
@@ -2197,11 +2233,12 @@ class TezX extends Router {
|
|
|
2197
2233
|
return null;
|
|
2198
2234
|
}
|
|
2199
2235
|
findRoute(method, pathname) {
|
|
2200
|
-
const route =
|
|
2236
|
+
const route =
|
|
2237
|
+
this.#triRouter(method, pathname) || this.#hashRouter(method, pathname);
|
|
2201
2238
|
if (route) {
|
|
2202
2239
|
return {
|
|
2203
2240
|
...route,
|
|
2204
|
-
middlewares: [...route.middlewares]
|
|
2241
|
+
middlewares: [...route.middlewares],
|
|
2205
2242
|
};
|
|
2206
2243
|
}
|
|
2207
2244
|
return null;
|
|
@@ -2259,12 +2296,13 @@ class TezX extends Router {
|
|
|
2259
2296
|
let middlewares2 = find.middlewares;
|
|
2260
2297
|
const response = await this.#createHandler(
|
|
2261
2298
|
middlewares2,
|
|
2262
|
-
callback
|
|
2299
|
+
callback,
|
|
2263
2300
|
)(ctx2);
|
|
2264
2301
|
if (response?.headers) {
|
|
2265
2302
|
ctx2.headers.add(response.headers);
|
|
2266
2303
|
}
|
|
2267
|
-
const statusText =
|
|
2304
|
+
const statusText =
|
|
2305
|
+
response?.statusText || httpStatusMap[response?.status] || "";
|
|
2268
2306
|
const status = response.status || 200;
|
|
2269
2307
|
let headers = ctx2.headers.toObject();
|
|
2270
2308
|
if (logger.response) {
|
|
@@ -2274,13 +2312,13 @@ class TezX extends Router {
|
|
|
2274
2312
|
return new Response(response.body, {
|
|
2275
2313
|
status,
|
|
2276
2314
|
statusText,
|
|
2277
|
-
headers
|
|
2315
|
+
headers,
|
|
2278
2316
|
});
|
|
2279
2317
|
}
|
|
2280
2318
|
return new Response(response.body, {
|
|
2281
2319
|
status,
|
|
2282
2320
|
statusText,
|
|
2283
|
-
headers
|
|
2321
|
+
headers,
|
|
2284
2322
|
});
|
|
2285
2323
|
} else {
|
|
2286
2324
|
if (logger.response) {
|
|
@@ -2288,7 +2326,7 @@ class TezX extends Router {
|
|
|
2288
2326
|
}
|
|
2289
2327
|
return GlobalConfig.notFound(ctx2);
|
|
2290
2328
|
}
|
|
2291
|
-
}
|
|
2329
|
+
},
|
|
2292
2330
|
)(ctx);
|
|
2293
2331
|
} catch (err) {
|
|
2294
2332
|
let error = err;
|
|
@@ -2330,7 +2368,7 @@ function parseEnvFile(filePath, result) {
|
|
|
2330
2368
|
fileContent = readFileSync(filePath, "utf8");
|
|
2331
2369
|
} else if (runtime === "deno") {
|
|
2332
2370
|
fileContent = new TextDecoder("utf-8").decode(
|
|
2333
|
-
Deno.readFileSync(filePath)
|
|
2371
|
+
Deno.readFileSync(filePath),
|
|
2334
2372
|
);
|
|
2335
2373
|
}
|
|
2336
2374
|
const lines = fileContent.split("\n");
|
|
@@ -2339,7 +2377,9 @@ function parseEnvFile(filePath, result) {
|
|
|
2339
2377
|
if (!trimmedLine || trimmedLine.startsWith("#")) continue;
|
|
2340
2378
|
const [key, value] = trimmedLine.split("=", 2).map((part) => part.trim());
|
|
2341
2379
|
if (key && value) {
|
|
2342
|
-
const parsedValue = value
|
|
2380
|
+
const parsedValue = value
|
|
2381
|
+
.replace(/^"(.*)"$/, "$1")
|
|
2382
|
+
.replace(/^'(.*)'$/, "$1");
|
|
2343
2383
|
result[key] = parsedValue;
|
|
2344
2384
|
if (runtime === "node" || runtime === "bun") {
|
|
2345
2385
|
process.env[key] = parsedValue;
|
|
@@ -2359,7 +2399,7 @@ function loadEnv(basePath = "./") {
|
|
|
2359
2399
|
".env.local",
|
|
2360
2400
|
`.env.${process?.env?.NODE_ENV || "development"}`,
|
|
2361
2401
|
// Supports NODE_ENV (e.g., .env.development, .env.production)
|
|
2362
|
-
`.env.${process?.env?.NODE_ENV || "development"}.local
|
|
2402
|
+
`.env.${process?.env?.NODE_ENV || "development"}.local`,
|
|
2363
2403
|
];
|
|
2364
2404
|
for (const envFile of envFiles) {
|
|
2365
2405
|
parseEnvFile(`${basePath}${envFile}`, result);
|
|
@@ -2378,15 +2418,16 @@ const COLORS = {
|
|
|
2378
2418
|
magenta: "\x1B[35m",
|
|
2379
2419
|
cyan: "\x1B[36m",
|
|
2380
2420
|
bgBlue: "\x1B[44m",
|
|
2381
|
-
bgMagenta: "\x1B[45m"
|
|
2421
|
+
bgMagenta: "\x1B[45m",
|
|
2422
|
+
};
|
|
2382
2423
|
const loggerOutput = (level, message, ...args) => {
|
|
2383
|
-
const timestamp =
|
|
2424
|
+
const timestamp = /* @__PURE__ */ new Date().toISOString();
|
|
2384
2425
|
const LEVEL_COLORS = {
|
|
2385
2426
|
info: COLORS.blue,
|
|
2386
2427
|
warn: COLORS.yellow,
|
|
2387
2428
|
error: COLORS.red,
|
|
2388
2429
|
debug: COLORS.cyan,
|
|
2389
|
-
success: COLORS.green
|
|
2430
|
+
success: COLORS.green,
|
|
2390
2431
|
};
|
|
2391
2432
|
const prefix = `${COLORS.gray}[${timestamp}]${COLORS.reset}`;
|
|
2392
2433
|
const levelText = `${LEVEL_COLORS[level]}[${level.toUpperCase()}]${COLORS.reset}`;
|
|
@@ -2398,37 +2439,30 @@ function logger() {
|
|
|
2398
2439
|
return {
|
|
2399
2440
|
request: (method, pathname) => {
|
|
2400
2441
|
console.log(
|
|
2401
|
-
`${COLORS.bold}<-- ${COLORS.reset}${COLORS.bgMagenta} ${method} ${COLORS.reset} ${pathname}
|
|
2442
|
+
`${COLORS.bold}<-- ${COLORS.reset}${COLORS.bgMagenta} ${method} ${COLORS.reset} ${pathname}`,
|
|
2402
2443
|
);
|
|
2403
2444
|
},
|
|
2404
2445
|
response: (method, pathname, status) => {
|
|
2405
2446
|
const elapsed = performance.now() - startTime;
|
|
2406
2447
|
console.log(
|
|
2407
|
-
`${COLORS.bold}--> ${COLORS.reset}${COLORS.bgBlue} ${method} ${COLORS.reset} ${pathname} ${COLORS.yellow}${status}${COLORS.reset} ${COLORS.magenta}${elapsed.toFixed(2)}ms${COLORS.reset}
|
|
2448
|
+
`${COLORS.bold}--> ${COLORS.reset}${COLORS.bgBlue} ${method} ${COLORS.reset} ${pathname} ${COLORS.yellow}${status}${COLORS.reset} ${COLORS.magenta}${elapsed.toFixed(2)}ms${COLORS.reset}`,
|
|
2408
2449
|
);
|
|
2409
2450
|
},
|
|
2410
2451
|
info: (msg, ...args) => loggerOutput("info", msg, ...args),
|
|
2411
2452
|
warn: (msg, ...args) => loggerOutput("warn", msg, ...args),
|
|
2412
2453
|
error: (msg, ...args) => loggerOutput("error", msg, ...args),
|
|
2413
2454
|
debug: (msg, ...args) => loggerOutput("debug", msg, ...args),
|
|
2414
|
-
success: (msg, ...args) => loggerOutput("success", msg, ...args)
|
|
2455
|
+
success: (msg, ...args) => loggerOutput("success", msg, ...args),
|
|
2415
2456
|
};
|
|
2416
2457
|
}
|
|
2417
2458
|
return {
|
|
2418
|
-
request: (method, pathname) => {
|
|
2419
|
-
},
|
|
2420
|
-
|
|
2421
|
-
},
|
|
2422
|
-
|
|
2423
|
-
},
|
|
2424
|
-
|
|
2425
|
-
},
|
|
2426
|
-
error: (msg, ...args) => {
|
|
2427
|
-
},
|
|
2428
|
-
debug: (msg, ...args) => {
|
|
2429
|
-
},
|
|
2430
|
-
success: (msg, ...args) => {
|
|
2431
|
-
}
|
|
2459
|
+
request: (method, pathname) => {},
|
|
2460
|
+
response: (method, pathname, status) => {},
|
|
2461
|
+
info: (msg, ...args) => {},
|
|
2462
|
+
warn: (msg, ...args) => {},
|
|
2463
|
+
error: (msg, ...args) => {},
|
|
2464
|
+
debug: (msg, ...args) => {},
|
|
2465
|
+
success: (msg, ...args) => {},
|
|
2432
2466
|
};
|
|
2433
2467
|
}
|
|
2434
2468
|
|
|
@@ -2439,7 +2473,7 @@ function cors(option = {}) {
|
|
|
2439
2473
|
credentials,
|
|
2440
2474
|
exposedHeaders,
|
|
2441
2475
|
maxAge,
|
|
2442
|
-
origin
|
|
2476
|
+
origin,
|
|
2443
2477
|
} = option;
|
|
2444
2478
|
return async (ctx, next) => {
|
|
2445
2479
|
const reqOrigin = ctx.req.headers.get("origin") || "";
|
|
@@ -2463,16 +2497,16 @@ function cors(option = {}) {
|
|
|
2463
2497
|
ctx.headers.set("Access-Control-Allow-Origin", allowOrigin);
|
|
2464
2498
|
ctx.headers.set(
|
|
2465
2499
|
"Access-Control-Allow-Methods",
|
|
2466
|
-
(methods || ["GET", "POST", "PUT", "DELETE"]).join(", ")
|
|
2500
|
+
(methods || ["GET", "POST", "PUT", "DELETE"]).join(", "),
|
|
2467
2501
|
);
|
|
2468
2502
|
ctx.headers.set(
|
|
2469
2503
|
"Access-Control-Allow-Headers",
|
|
2470
|
-
(allowedHeaders || ["Content-Type", "Authorization"]).join(", ")
|
|
2504
|
+
(allowedHeaders || ["Content-Type", "Authorization"]).join(", "),
|
|
2471
2505
|
);
|
|
2472
2506
|
if (exposedHeaders) {
|
|
2473
2507
|
ctx.headers.set(
|
|
2474
2508
|
"Access-Control-Expose-Headers",
|
|
2475
|
-
exposedHeaders.join(", ")
|
|
2509
|
+
exposedHeaders.join(", "),
|
|
2476
2510
|
);
|
|
2477
2511
|
}
|
|
2478
2512
|
if (credentials) {
|
|
@@ -2484,7 +2518,7 @@ function cors(option = {}) {
|
|
|
2484
2518
|
if (ctx.req.method === "OPTIONS") {
|
|
2485
2519
|
return new Response(null, {
|
|
2486
2520
|
status: 204,
|
|
2487
|
-
headers: ctx.headers.toObject()
|
|
2521
|
+
headers: ctx.headers.toObject(),
|
|
2488
2522
|
});
|
|
2489
2523
|
}
|
|
2490
2524
|
return await next();
|