@bool-ts/core 2.3.0 → 2.3.1

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.
@@ -5,26 +5,26 @@ type AnsiOptions = {
5
5
  underline?: boolean;
6
6
  };
7
7
  declare const ansiColors: Readonly<{
8
- black: 30;
9
- red: 31;
10
- green: 32;
11
- yellow: 33;
12
- blue: 34;
13
- magenta: 35;
14
- cyan: 36;
15
- white: 37;
16
- gray: 90;
8
+ black: "38;5;16";
9
+ red: "38;5;196";
10
+ green: "38;5;46";
11
+ yellow: "38;5;226";
12
+ blue: "38;5;21";
13
+ magenta: "38;5;201";
14
+ cyan: "38;5;51";
15
+ white: "38;5;231";
16
+ gray: "38;5;244";
17
17
  }>;
18
18
  declare const backgroundColors: Readonly<{
19
- black: 40;
20
- red: 41;
21
- green: 42;
22
- yellow: 43;
23
- blue: 44;
24
- magenta: 45;
25
- cyan: 46;
26
- white: 47;
27
- gray: 100;
19
+ black: "48;5;16";
20
+ red: "48;5;196";
21
+ green: "48;5;46";
22
+ yellow: "48;5;226";
23
+ blue: "48;5;21";
24
+ magenta: "48;5;201";
25
+ cyan: "48;5;51";
26
+ white: "48;5;231";
27
+ gray: "48;5;244";
28
28
  }>;
29
29
  export declare const ansiText: (text: string, options?: AnsiOptions) => string;
30
30
  export {};
package/package.json CHANGED
@@ -44,5 +44,5 @@
44
44
  "test:socket": "bun --hot run __test/client/socket.ts"
45
45
  },
46
46
  "types": "./dist/index.d.ts",
47
- "version": "2.3.0"
47
+ "version": "2.3.1"
48
48
  }
@@ -268,15 +268,15 @@ export class Application<TRootClass extends Object = Object> {
268
268
  method = request.method.toUpperCase(),
269
269
  responseHeaders = new Headers();
270
270
 
271
- try {
272
- const context = new Context()
273
- .setOptions({ isStatic: true })
274
- .set(httpServerArgsKey, server)
275
- .set(requestArgsKey, request)
276
- .set(requestHeaderArgsKey, request.headers)
277
- .set(responseHeadersArgsKey, responseHeaders)
278
- .set(queryArgsKey, query);
271
+ const context = new Context()
272
+ .setOptions({ isStatic: true })
273
+ .set(httpServerArgsKey, server)
274
+ .set(requestArgsKey, request)
275
+ .set(requestHeaderArgsKey, request.headers)
276
+ .set(responseHeadersArgsKey, responseHeaders)
277
+ .set(queryArgsKey, query);
279
278
 
279
+ try {
280
280
  [
281
281
  ...(!allowCredentials
282
282
  ? []
@@ -369,9 +369,12 @@ export class Application<TRootClass extends Object = Object> {
369
369
  } finally {
370
370
  if (allowLogsMethods) {
371
371
  const end = performance.now();
372
+ const responseStatus = context.get(responseStatusArgsKey, { isStatic: false });
373
+ const inferedResponseStatus =
374
+ typeof responseStatus !== "number" || !responseStatus ? 0 : responseStatus;
372
375
  const pathname = ansiText(url.pathname, { color: "blue" });
373
376
  const convertedPID = `${Bun.color("yellow", "ansi")}${process.pid}`;
374
- const convertedMethod = ansiText(request.method, {
377
+ const convertedMethod = ansiText(` ${request.method} `, {
375
378
  color: "yellow",
376
379
  backgroundColor: "blue"
377
380
  });
@@ -387,18 +390,64 @@ export class Application<TRootClass extends Object = Object> {
387
390
  }
388
391
  );
389
392
  const convertedTime = ansiText(
390
- `${Math.round((end - start + Number.EPSILON) * 10 ** 2) / 10 ** 2}ms`,
393
+ ` ${Math.round((end - start + Number.EPSILON) * 10 ** 2) / 10 ** 2}ms `,
391
394
  {
392
395
  color: "yellow",
393
396
  backgroundColor: "blue"
394
397
  }
395
398
  );
399
+ const convertedResponseStatus = ansiText(
400
+ ` ${inferedResponseStatus} (${inferStatusText(inferedResponseStatus)}) `,
401
+ (() => {
402
+ if (inferedResponseStatus >= 100 && inferedResponseStatus < 200)
403
+ return {
404
+ color: "white",
405
+ backgroundColor: "cyan"
406
+ };
407
+ else if (inferedResponseStatus >= 200 && inferedResponseStatus < 300)
408
+ return {
409
+ color: "white",
410
+ backgroundColor: "blue"
411
+ };
412
+ else if (inferedResponseStatus >= 300 && inferedResponseStatus < 400)
413
+ return {
414
+ color: "black",
415
+ backgroundColor: "magenta"
416
+ };
417
+ else if (inferedResponseStatus >= 400 && inferedResponseStatus < 500)
418
+ return {
419
+ color: "black",
420
+ backgroundColor: "yellow"
421
+ };
422
+ else if (inferedResponseStatus >= 500 && inferedResponseStatus < 600)
423
+ return {
424
+ color: "white",
425
+ backgroundColor: "red"
426
+ };
427
+ else
428
+ return {
429
+ color: "black",
430
+ backgroundColor: "gray"
431
+ };
432
+ })()
433
+ );
396
434
 
397
435
  allowLogsMethods.includes(
398
436
  request.method.toUpperCase() as (typeof allowLogsMethods)[number]
399
437
  ) &&
400
438
  console.info(
401
- `PID: ${convertedPID} - Method: ${convertedMethod} - IP: ${convertedReqIp} - ${pathname} - Time: ${convertedTime}`
439
+ [
440
+ `PID: ${convertedPID}`,
441
+ `Method: ${convertedMethod}`,
442
+ `IP: ${convertedReqIp}`,
443
+ pathname,
444
+ `Time: ${convertedTime}`,
445
+ typeof responseStatus !== "number" || !responseStatus
446
+ ? undefined
447
+ : convertedResponseStatus
448
+ ]
449
+ .filter((x) => !!x?.trim())
450
+ .join(" - ")
402
451
  );
403
452
  }
404
453
  }
@@ -6,51 +6,45 @@ type AnsiOptions = {
6
6
  };
7
7
 
8
8
  const ansiColors = Object.freeze({
9
- black: 30,
10
- red: 31,
11
- green: 32,
12
- yellow: 33,
13
- blue: 34,
14
- magenta: 35,
15
- cyan: 36,
16
- white: 37,
17
- gray: 90
9
+ black: "38;5;16",
10
+ red: "38;5;196",
11
+ green: "38;5;46",
12
+ yellow: "38;5;226",
13
+ blue: "38;5;21",
14
+ magenta: "38;5;201",
15
+ cyan: "38;5;51",
16
+ white: "38;5;231",
17
+ gray: "38;5;244"
18
18
  });
19
19
 
20
20
  const backgroundColors = Object.freeze({
21
- black: 40,
22
- red: 41,
23
- green: 42,
24
- yellow: 43,
25
- blue: 44,
26
- magenta: 45,
27
- cyan: 46,
28
- white: 47,
29
- gray: 100
21
+ black: "48;5;16",
22
+ red: "48;5;196",
23
+ green: "48;5;46",
24
+ yellow: "48;5;226",
25
+ blue: "48;5;21",
26
+ magenta: "48;5;201",
27
+ cyan: "48;5;51",
28
+ white: "48;5;231",
29
+ gray: "48;5;244"
30
30
  });
31
31
 
32
32
  export const ansiText = (text: string, options: AnsiOptions = {}) => {
33
33
  const { color, backgroundColor, bold, underline } = options;
34
+ const codes: string[] = [];
34
35
 
35
- let ansiCode = "";
36
-
37
- if (bold) {
38
- ansiCode += "\x1b[1m"; // Mã ANSI cho in đậm
39
- }
40
-
41
- if (underline) {
42
- ansiCode += "\x1b[4m"; // Mã ANSI cho gạch chân
43
- }
36
+ if (bold) codes.push("1");
37
+ if (underline) codes.push("4");
44
38
 
45
39
  if (color && ansiColors[color]) {
46
- ansiCode += `\x1b[${ansiColors[color]}m`;
40
+ codes.push(ansiColors[color]);
47
41
  }
48
42
 
49
- // Màu nền
50
43
  if (backgroundColor && backgroundColors[backgroundColor]) {
51
- ansiCode += `\x1b[${backgroundColors[backgroundColor]}m`;
44
+ codes.push(backgroundColors[backgroundColor]);
52
45
  }
53
46
 
54
- // Kết quả với reset
55
- return `${ansiCode}${text}\x1b[0m`;
47
+ if (codes.length === 0) return text;
48
+
49
+ return `\x1b[${codes.join(";")}m${text}\x1b[0m`;
56
50
  };