tezx 1.0.10 → 1.0.12

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.
@@ -2,10 +2,11 @@ import { CommonHandler } from "./common";
2
2
  import { GlobalConfig } from "./config/config";
3
3
  import { sanitizePathSplit } from "./utils/url";
4
4
  export class TriMiddleware {
5
+ children = new Map();
6
+ middlewares = new Set();
7
+ isOptional = false;
8
+ pathname;
5
9
  constructor(pathname = "/") {
6
- this.children = new Map();
7
- this.middlewares = new Set();
8
- this.isOptional = false;
9
10
  this.pathname = pathname;
10
11
  if (GlobalConfig.allowDuplicateMw) {
11
12
  this.middlewares = []; // Array (DuplicateMiddlewares)
@@ -16,9 +17,10 @@ export class TriMiddleware {
16
17
  }
17
18
  }
18
19
  export default class MiddlewareConfigure extends CommonHandler {
20
+ triMiddlewares = new TriMiddleware();
21
+ basePath;
19
22
  constructor(basePath = "/") {
20
23
  super();
21
- this.triMiddlewares = new TriMiddleware();
22
24
  this.basePath = basePath;
23
25
  }
24
26
  addMiddleware(pathname, middlewares) {
@@ -5,41 +5,30 @@ const common_1 = require("./common");
5
5
  const config_1 = require("./config/config");
6
6
  const url_1 = require("./utils/url");
7
7
  class TriMiddleware {
8
+ children = new Map();
9
+ middlewares = new Set();
10
+ isOptional = false;
11
+ pathname;
8
12
  constructor(pathname = "/") {
9
- this.children = new Map();
10
- this.middlewares = new Set();
11
- this.isOptional = false;
12
13
  this.pathname = pathname;
13
14
  if (config_1.GlobalConfig.allowDuplicateMw) {
14
- this.middlewares = []; // Array (DuplicateMiddlewares)
15
+ this.middlewares = [];
15
16
  }
16
17
  else {
17
- this.middlewares = new Set(); // Set (UniqueMiddlewares)
18
+ this.middlewares = new Set();
18
19
  }
19
20
  }
20
21
  }
21
22
  exports.TriMiddleware = TriMiddleware;
22
23
  class MiddlewareConfigure extends common_1.CommonHandler {
24
+ triMiddlewares = new TriMiddleware();
25
+ basePath;
23
26
  constructor(basePath = "/") {
24
27
  super();
25
- this.triMiddlewares = new TriMiddleware();
26
28
  this.basePath = basePath;
27
29
  }
28
30
  addMiddleware(pathname, middlewares) {
29
- // console.log(this.basePath)
30
- // console.log(pathname);
31
31
  const parts = (0, url_1.sanitizePathSplit)(this.basePath, pathname);
32
- // console.log(parts, this.basePath)
33
- // if (/(\/\*|\?)/.test(pathname)) {
34
- // let path = parts.join("/");
35
- // let handler = this.routeMiddlewares.get(path);
36
- // if (!handler) {
37
- // // handler.set(method, { callback: callback, middlewares });
38
- // return this.routeMiddlewares.set(path, middlewares);
39
- // }
40
- // // console.log(this.routeMiddlewares, 35345)
41
- // return handler.push(...middlewares);
42
- // }
43
32
  let node = this.triMiddlewares;
44
33
  for (const part of parts) {
45
34
  if (part.startsWith("*")) {
@@ -49,7 +38,6 @@ class MiddlewareConfigure extends common_1.CommonHandler {
49
38
  node = node.children.get("*");
50
39
  }
51
40
  else if (part.startsWith(":")) {
52
- // Dynamic parameter (e.g., :id)
53
41
  const isOptional = part?.endsWith("?");
54
42
  if (isOptional) {
55
43
  node.isOptional = isOptional;
@@ -61,13 +49,11 @@ class MiddlewareConfigure extends common_1.CommonHandler {
61
49
  node = node.children.get(":");
62
50
  }
63
51
  else {
64
- // Static path segment
65
52
  if (!node.children.has(part)) {
66
53
  node.children.set(part, new TriMiddleware());
67
54
  }
68
55
  node = node.children.get(part);
69
56
  }
70
- // Optionally, you could store the parameter name in the node if needed
71
57
  }
72
58
  if (config_1.GlobalConfig.allowDuplicateMw) {
73
59
  node.middlewares.push(...middlewares);
@@ -77,8 +63,6 @@ class MiddlewareConfigure extends common_1.CommonHandler {
77
63
  node.middlewares.add(middleware);
78
64
  }
79
65
  }
80
- // this.triMiddleware = new TriMiddleware(pathname);
81
- // this.triMiddleware.middlewares = middlewares;
82
66
  }
83
67
  }
84
68
  exports.default = MiddlewareConfigure;
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.denoAdapter = denoAdapter;
4
4
  exports.bunAdapter = bunAdapter;
5
5
  exports.nodeAdapter = nodeAdapter;
6
- //src/adapter.ts
7
6
  const config_1 = require("./config/config");
8
7
  function denoAdapter(TezX) {
9
8
  function listen(port, callback) {
@@ -43,11 +42,8 @@ function denoAdapter(TezX) {
43
42
  if (!server) {
44
43
  throw new Error("Deno is not find");
45
44
  }
46
- // Determine protocol based on SSL configuration
47
45
  const protocol = "\x1b[1;34mhttp\x1b[0m";
48
- // Constructing the colorful message with emojis
49
46
  const message = `\x1b[1m🚀 Deno TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
50
- // Logging the message to the console
51
47
  if (typeof callback === "function") {
52
48
  callback(message);
53
49
  }
@@ -91,12 +87,8 @@ function bunAdapter(TezX) {
91
87
  },
92
88
  });
93
89
  config_1.GlobalConfig.serverInfo = server;
94
- // Determine protocol based on SSL configuration
95
90
  const protocol = "\x1b[1;34mhttp\x1b[0m";
96
- // const protocol = "\x1b[1;35mhttps\x1b[0m" : "\x1b[1;34mhttp\x1b[0m";
97
- // Constructing the colorful message with emojis
98
91
  const message = `\x1b[1m Bun TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
99
- // Logging the message to the console
100
92
  if (typeof callback == "function") {
101
93
  callback(message);
102
94
  }
@@ -122,46 +114,38 @@ function nodeAdapter(TezX) {
122
114
  address = {
123
115
  remoteAddr: {
124
116
  family: req.socket.remoteFamily,
125
- address: req.socket.remoteAddress, // Client IP
126
- port: req.socket.remotePort, // Client Port
117
+ address: req.socket.remoteAddress,
118
+ port: req.socket.remotePort,
127
119
  },
128
120
  localAddr: {
129
- address: req.socket.localAddress, // Server IP
130
- port: req.socket.localPort, // Server Port
121
+ address: req.socket.localAddress,
122
+ port: req.socket.localPort,
131
123
  family: req.socket.localFamily,
132
124
  },
133
125
  };
134
126
  }
135
127
  const response = await TezX.serve(req, address);
136
- // console.log((req as any).socket.remoteAddress)
137
128
  const statusText = response?.statusText;
138
129
  if (!(response instanceof Response)) {
139
130
  throw new Error("Invalid response from TezX.serve");
140
131
  }
141
132
  const headers = Object.fromEntries(await response.headers.entries());
142
- // const body = await response.text(); // Ensure it's a string
143
133
  if (statusText) {
144
134
  res.statusMessage = statusText;
145
135
  }
146
136
  res.writeHead(response.status, headers);
147
137
  const { Readable } = await Promise.resolve().then(() => require("stream"));
148
138
  if (response.body instanceof Readable) {
149
- // Stream the body (e.g., for large files or binary data)
150
139
  response.body.pipe(res);
151
140
  }
152
141
  else {
153
- // Convert body to text or buffer
154
142
  const body = await response.arrayBuffer();
155
143
  res.end(Buffer.from(body));
156
144
  }
157
145
  });
158
146
  server.listen(port, () => {
159
- // Determine protocol based on SSL configuration
160
147
  const protocol = "\x1b[1;34mhttp\x1b[0m";
161
- // const protocol = "\x1b[1;35mhttps\x1b[0m" : "\x1b[1;34mhttp\x1b[0m";
162
- // Constructing the colorful message with emojis
163
148
  const message = `\x1b[1m NodeJS TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
164
- // Logging the message to the console
165
149
  config_1.GlobalConfig.serverInfo = server;
166
150
  if (typeof callback == "function") {
167
151
  callback(message);
@@ -3,17 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonHandler = void 0;
4
4
  const config_1 = require("./config/config");
5
5
  class CommonHandler {
6
- /**
7
- * Register a custom 404 handler for missing routes
8
- * @param {Callback} callback - Handler function to execute when no route matches
9
- * @returns {this} - Returns current instance for chaining
10
- *
11
- * @example
12
- * // Register a custom not-found handler
13
- * app.notFound((ctx) => {
14
- * ctx.status(404).text('Custom not found message');
15
- * });
16
- */
17
6
  notFound(callback) {
18
7
  config_1.GlobalConfig.notFound = callback;
19
8
  return this;
@@ -24,75 +13,3 @@ class CommonHandler {
24
13
  }
25
14
  }
26
15
  exports.CommonHandler = CommonHandler;
27
- // class T<T> {
28
- // private data: T;
29
- // constructor() {
30
- // this.data = {} as T; // Initialize with an empty object of type T
31
- // }
32
- // // Method to set a value
33
- // set<K extends keyof T>(key: K, value: T[K]): void {
34
- // this.data[key] = value;
35
- // }
36
- // // Method to get a value
37
- // get<K extends keyof T>(key: K): T[K] | undefined {
38
- // return this.data[key];
39
- // }
40
- // }
41
- // // Create an instance of T with a specific type
42
- // const x = new T<{ user: string }>();
43
- // // Set the 'user' property
44
- // x.set('user', 'rakib');
45
- // // Get the 'user' property
46
- // console.log(x.get('user')); // Output: "rakib"
47
- // class T<T extends Record<string,any>> {
48
- // [key: any extends keyof T]: T; // Allow dynamic properties
49
- // constructor() { }
50
- // }
51
- // // Create an instance of T with a specific type
52
- // const x = new T<{ user: string }>();
53
- // // Assign the 'user' property dynamically
54
- // x.user = 'rakib';
55
- // // // Access the 'user' property
56
- // console.log(x.user); // Output: "rakib"
57
- // class T {
58
- // constructor(data: T) {
59
- // Object.assign(this, data);
60
- // }
61
- // }
62
- // type x = T & {
63
- // user: 5345
64
- // }
65
- // const xx: x = {
66
- // user: 5345
67
- // }
68
- // class T<T> {
69
- // constructor(public data: T) { }
70
- // }
71
- // const x = new T<{ user: string }>({ user: 'rakib' });
72
- // x.data.user = 'rakib';
73
- // console.log(x.data.user); // Outputs: rakib
74
- // class T<T extends object> {
75
- // constructor(data: T) {
76
- // Object.assign(this, data);
77
- // }
78
- // }
79
- // const x = new T({ user: 'rakib' }) as T<{ user: string }> & { user: string };
80
- // x.user = 'rakib';
81
- // console.log(x.user); // Outputs: rakib
82
- // const x = new T({ user: 'rakib' }) as T<{ user: string }> & { user: string };
83
- // x.user = 'rakib';
84
- // console.log(x.user); // Outputs:
85
- // class T<T extends Record<string, any>> {
86
- // private data: T = {} as T;
87
- // [key: string]: any; // Allows dynamic property assignment
88
- // set<K extends keyof T>(key: K, value: T[K]): void {
89
- // this.data[key] = value;
90
- // }
91
- // get<K extends keyof T>(key: K): T[K] | undefined {
92
- // return this.data[key];
93
- // }
94
- // }
95
- // // ✅ Usage
96
- // const x = new T<{ user: string }>();
97
- // x.user = "rakib"; // ✅ Works
98
- // console.log(x.user); // Output: "rakib"
@@ -1,40 +1,35 @@
1
1
  "use strict";
2
- var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
3
- if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
4
- return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
5
- };
6
- var _a;
7
2
  Object.defineProperty(exports, "__esModule", { value: true });
8
3
  exports.GlobalConfig = void 0;
9
4
  const debugging_1 = require("../utils/debugging");
10
- exports.GlobalConfig = (_a = class {
11
- static get debugging() {
12
- return this.debugMode
13
- ? {
14
- info: (msg, ...args) => (0, debugging_1.loggerOutput)("info", msg, ...args),
15
- warn: (msg, ...args) => (0, debugging_1.loggerOutput)("warn", msg, ...args),
16
- error: (msg, ...args) => (0, debugging_1.loggerOutput)("error", msg, ...args),
17
- debug: (msg, ...args) => (0, debugging_1.loggerOutput)("debug", msg, ...args),
18
- success: (msg, ...args) => (0, debugging_1.loggerOutput)("success", msg, ...args),
19
- }
20
- : {
21
- info: (msg, ...args) => { },
22
- warn: (msg, ...args) => { },
23
- error: (msg, ...args) => { },
24
- debug: (msg, ...args) => { },
25
- success: (msg, ...args) => { },
26
- };
27
- }
28
- },
29
- __setFunctionName(_a, "GlobalConfig"),
30
- _a.notFound = (ctx) => {
5
+ let GlobalConfig = class {
6
+ static notFound = (ctx) => {
31
7
  const { method, urlRef: { pathname }, } = ctx.req;
32
8
  return ctx.text(`${method}: '${pathname}' could not find\n`, 404);
33
- },
34
- _a.onError = (err, ctx) => {
9
+ };
10
+ static onError = (err, ctx) => {
35
11
  return ctx.text(err, 500);
36
- },
37
- _a.allowDuplicateMw = false,
38
- _a.overwriteMethod = true,
39
- _a.debugMode = false,
40
- _a);
12
+ };
13
+ static allowDuplicateMw = false;
14
+ static overwriteMethod = true;
15
+ static debugMode = false;
16
+ static serverInfo;
17
+ static get debugging() {
18
+ return this.debugMode
19
+ ? {
20
+ info: (msg, ...args) => (0, debugging_1.loggerOutput)("info", msg, ...args),
21
+ warn: (msg, ...args) => (0, debugging_1.loggerOutput)("warn", msg, ...args),
22
+ error: (msg, ...args) => (0, debugging_1.loggerOutput)("error", msg, ...args),
23
+ debug: (msg, ...args) => (0, debugging_1.loggerOutput)("debug", msg, ...args),
24
+ success: (msg, ...args) => (0, debugging_1.loggerOutput)("success", msg, ...args),
25
+ }
26
+ : {
27
+ info: (msg, ...args) => { },
28
+ warn: (msg, ...args) => { },
29
+ error: (msg, ...args) => { },
30
+ debug: (msg, ...args) => { },
31
+ success: (msg, ...args) => { },
32
+ };
33
+ }
34
+ };
35
+ exports.GlobalConfig = GlobalConfig;