tezx 2.0.8 → 2.0.11

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.
Files changed (45) hide show
  1. package/cjs/core/router.js +2 -2
  2. package/cjs/helper/common.js +7 -1
  3. package/cjs/helper/index.js +8 -9
  4. package/cjs/index.js +7 -8
  5. package/cjs/types/index.js +2 -0
  6. package/cjs/utils/debugging.js +0 -1
  7. package/cjs/utils/regexRouter.js +1 -1
  8. package/cjs/ws/node.js +1 -1
  9. package/core/MiddlewareConfigure.d.ts +1 -3
  10. package/core/common.d.ts +1 -1
  11. package/core/config.d.ts +1 -2
  12. package/core/context.d.ts +4 -13
  13. package/core/request.d.ts +3 -21
  14. package/core/router.d.ts +43 -54
  15. package/core/router.js +2 -2
  16. package/core/server.d.ts +2 -2
  17. package/helper/common.js +7 -1
  18. package/helper/index.d.ts +8 -8
  19. package/helper/index.js +3 -4
  20. package/index.d.ts +5 -8
  21. package/index.js +5 -6
  22. package/middleware/basicAuth.d.ts +1 -1
  23. package/middleware/cacheControl.d.ts +2 -1
  24. package/middleware/cors.d.ts +1 -1
  25. package/middleware/detectBot.d.ts +1 -1
  26. package/middleware/index.d.ts +1 -1
  27. package/middleware/logger.d.ts +1 -1
  28. package/middleware/pagination.d.ts +1 -2
  29. package/middleware/powered-by.d.ts +1 -1
  30. package/middleware/rateLimiter.d.ts +1 -1
  31. package/middleware/request-id.d.ts +1 -1
  32. package/middleware/requestTimeout.d.ts +1 -1
  33. package/middleware/sanitizeHeader.d.ts +1 -1
  34. package/middleware/secureHeaders.d.ts +1 -1
  35. package/middleware/xssProtection.d.ts +1 -1
  36. package/package.json +4 -3
  37. package/types/index.d.ts +59 -0
  38. package/types/index.js +1 -0
  39. package/utils/debugging.d.ts +0 -5
  40. package/utils/debugging.js +0 -1
  41. package/utils/regexRouter.d.ts +1 -1
  42. package/utils/regexRouter.js +1 -1
  43. package/utils/staticFile.d.ts +2 -1
  44. package/ws/index.d.ts +1 -1
  45. package/ws/node.js +1 -1
@@ -250,7 +250,7 @@ class Router extends MiddlewareConfigure_js_1.default {
250
250
  if (!config_js_1.GlobalConfig.overwriteMethod && handler.has(method))
251
251
  return;
252
252
  return handler.set(method, {
253
- callback,
253
+ callback: callback,
254
254
  paramNames: [],
255
255
  regex: regex,
256
256
  middlewares: finalMiddleware
@@ -275,7 +275,7 @@ class Router extends MiddlewareConfigure_js_1.default {
275
275
  if (!config_js_1.GlobalConfig.overwriteMethod && handler.has(method))
276
276
  return;
277
277
  return handler.set(method, {
278
- callback,
278
+ callback: callback,
279
279
  regex: regex,
280
280
  paramNames: paramNames,
281
281
  middlewares: finalMiddleware
@@ -6,6 +6,12 @@ function generateID() {
6
6
  const random = Math.floor(Math.random() * 0xffffffffffff)
7
7
  .toString(16)
8
8
  .padStart(12, "0");
9
- const pid = (process.pid % 0x10000).toString(16).padStart(4, "0");
9
+ let pid = Math.floor(Math.random() * 0xffff).toString(16).padStart(4, "0");
10
+ if (typeof Deno !== "undefined" && typeof Deno.pid === "number") {
11
+ pid = (Deno.pid % 0xffff).toString(16).padStart(4, "0");
12
+ }
13
+ else if (typeof process !== "undefined" && typeof process.pid === "number") {
14
+ pid = (process.pid % 0xffff).toString(16).padStart(4, "0");
15
+ }
10
16
  return `${timestamp}-${random}-${pid}`;
11
17
  }
@@ -1,20 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateID = exports.sanitizePathSplit = exports.Environment = exports.GlobalConfig = void 0;
3
+ exports.sanitizePathSplit = exports.GlobalConfig = exports.generateID = exports.Environment = exports.colorText = void 0;
4
4
  const config_js_1 = require("../core/config.js");
5
+ Object.defineProperty(exports, "GlobalConfig", { enumerable: true, get: function () { return config_js_1.GlobalConfig; } });
5
6
  const environment_js_1 = require("../core/environment.js");
7
+ Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return environment_js_1.Environment; } });
8
+ const colors_js_1 = require("../utils/colors.js");
9
+ Object.defineProperty(exports, "colorText", { enumerable: true, get: function () { return colors_js_1.colorText; } });
6
10
  const url_js_1 = require("../utils/url.js");
11
+ Object.defineProperty(exports, "sanitizePathSplit", { enumerable: true, get: function () { return url_js_1.sanitizePathSplit; } });
7
12
  const common_js_1 = require("./common.js");
8
- var config_js_2 = require("../core/config.js");
9
- Object.defineProperty(exports, "GlobalConfig", { enumerable: true, get: function () { return config_js_2.GlobalConfig; } });
10
- var environment_js_2 = require("../core/environment.js");
11
- Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return environment_js_2.Environment; } });
12
- var url_js_2 = require("../utils/url.js");
13
- Object.defineProperty(exports, "sanitizePathSplit", { enumerable: true, get: function () { return url_js_2.sanitizePathSplit; } });
14
- var common_js_2 = require("./common.js");
15
- Object.defineProperty(exports, "generateID", { enumerable: true, get: function () { return common_js_2.generateID; } });
13
+ Object.defineProperty(exports, "generateID", { enumerable: true, get: function () { return common_js_1.generateID; } });
16
14
  exports.default = {
17
15
  Environment: environment_js_1.Environment,
16
+ colorText: colors_js_1.colorText,
18
17
  GlobalConfig: config_js_1.GlobalConfig,
19
18
  sanitizePathSplit: url_js_1.sanitizePathSplit,
20
19
  generateID: common_js_1.generateID,
package/cjs/index.js CHANGED
@@ -1,19 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.version = exports.compileRegexRoute = exports.regexMatchRoute = exports.TezX = exports.Router = void 0;
3
+ exports.version = exports.TezX = exports.Router = exports.regexMatchRoute = exports.compileRegexRoute = void 0;
4
4
  const router_js_1 = require("./core/router.js");
5
+ Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return router_js_1.Router; } });
5
6
  const server_js_1 = require("./core/server.js");
7
+ Object.defineProperty(exports, "TezX", { enumerable: true, get: function () { return server_js_1.TezX; } });
6
8
  const regexRouter_js_1 = require("./utils/regexRouter.js");
7
- Object.defineProperty(exports, "regexMatchRoute", { enumerable: true, get: function () { return regexRouter_js_1.regexMatchRoute; } });
8
9
  Object.defineProperty(exports, "compileRegexRoute", { enumerable: true, get: function () { return regexRouter_js_1.compileRegexRoute; } });
9
- var router_js_2 = require("./core/router.js");
10
- Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return router_js_2.Router; } });
11
- var server_js_2 = require("./core/server.js");
12
- Object.defineProperty(exports, "TezX", { enumerable: true, get: function () { return server_js_2.TezX; } });
13
- exports.version = "2.0.8";
10
+ Object.defineProperty(exports, "regexMatchRoute", { enumerable: true, get: function () { return regexRouter_js_1.regexMatchRoute; } });
11
+ exports.version = "2.0.11";
14
12
  exports.default = {
15
13
  Router: router_js_1.Router,
16
- regexMatchRoute: regexRouter_js_1.regexMatchRoute, compileRegexRoute: regexRouter_js_1.compileRegexRoute,
14
+ regexMatchRoute: regexRouter_js_1.regexMatchRoute,
15
+ compileRegexRoute: regexRouter_js_1.compileRegexRoute,
17
16
  TezX: server_js_1.TezX,
18
17
  version: exports.version,
19
18
  };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loggerOutput = void 0;
4
4
  const colors_js_1 = require("./colors.js");
5
5
  const loggerOutput = (level, message, ...args) => {
6
- const timestamp = new Date().toISOString();
7
6
  const LEVEL_COLORS = {
8
7
  info: colors_js_1.COLORS.blue,
9
8
  warn: colors_js_1.COLORS.yellow,
@@ -46,7 +46,7 @@ function addBaseToRegex(basePath, routeRegex) {
46
46
  const finalRegex = new RegExp(`^${combined}$`);
47
47
  return finalRegex;
48
48
  }
49
- function regexMatchRoute(regex, url, paramNames) {
49
+ function regexMatchRoute(regex, url, paramNames = []) {
50
50
  const match = url.match(regex);
51
51
  if (!match)
52
52
  return { success: false, params: {} };
package/cjs/ws/node.js CHANGED
@@ -44,7 +44,7 @@ class NodeTransport {
44
44
  maxPayload: options.maxPayload ?? 1048576,
45
45
  perMessageDeflate: options.perMessageDeflate ?? false,
46
46
  });
47
- server.on("upgrade", (request, socket, head) => {
47
+ server?.on("upgrade", (request, socket, head) => {
48
48
  this.wss?.handleUpgrade(request, socket, head, (ws) => {
49
49
  this.wss?.emit("connection", ws, request);
50
50
  this.setupHandlers(ws, event, options);
@@ -1,7 +1,5 @@
1
+ import { DuplicateMiddlewares, Middleware, UniqueMiddlewares } from "../types/index.js";
1
2
  import { CommonHandler } from "./common.js";
2
- import { Middleware } from "./router.js";
3
- export type DuplicateMiddlewares = Middleware<any>[];
4
- export type UniqueMiddlewares = Set<Middleware<any>>;
5
3
  export declare class TriMiddleware {
6
4
  children: Map<string, TriMiddleware>;
7
5
  middlewares: DuplicateMiddlewares | UniqueMiddlewares;
package/core/common.d.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import { Callback, ctx } from "../types/index.js";
1
2
  import { Context } from "./context.js";
2
- import { Callback, ctx } from "./router.js";
3
3
  export interface option {
4
4
  status?: number;
5
5
  }
package/core/config.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { Callback, ctx } from "./router.js";
2
- export type AdapterType = "bun" | "deno" | "node";
1
+ import { AdapterType, Callback, ctx } from "../types/index.js";
3
2
  export declare let GlobalConfig: {
4
3
  new (): {};
5
4
  notFound: Callback;
package/core/context.d.ts CHANGED
@@ -1,17 +1,8 @@
1
+ import { CookieOptions, HTTPMethod, PathType, ResponseHeaders } from "../types/index.js";
1
2
  import { State } from "../utils/state.js";
2
- import { HTTPMethod, Request as RequestParser } from "./request.js";
3
+ import { Request as RequestParser } from "./request.js";
3
4
  import { TezXServeOptions } from "./server.js";
4
- export interface CookieOptions {
5
- expires?: Date;
6
- path?: string;
7
- maxAge?: number;
8
- domain?: string;
9
- secure?: boolean;
10
- httpOnly?: boolean;
11
- sameSite?: "Strict" | "Lax" | "None";
12
- }
13
- export type ResponseHeaders = Record<string, string>;
14
- export declare class Context<T extends Record<string, any> = {}> {
5
+ export declare class Context<T extends Record<string, any> = {}, Path extends PathType = any> {
15
6
  #private;
16
7
  [key: string]: any;
17
8
  rawRequest: Request;
@@ -192,7 +183,7 @@ export declare class Context<T extends Record<string, any> = {}> {
192
183
  * // Access route params
193
184
  * const id = request.params.get('id');
194
185
  */
195
- get req(): RequestParser;
186
+ get req(): RequestParser<Path>;
196
187
  protected set params(params: Record<string, any>);
197
188
  /**
198
189
  * Set response body to be passed between middlewares or returned as final output.
package/core/request.d.ts CHANGED
@@ -1,24 +1,7 @@
1
+ import { ExtractParamsFromPath, FormDataOptions, HTTPMethod, NetAddr, PathType } from "../types/index.js";
1
2
  import type { UrlRef } from "../utils/url.js";
2
3
  import { TezXServeOptions } from "./server.js";
3
- export type FormDataOptions = {
4
- maxSize?: number;
5
- allowedTypes?: string[];
6
- sanitized?: boolean;
7
- maxFiles?: number;
8
- };
9
- type TransportType = "tcp" | "udp" | "unix" | "pipe" | "unixpacket";
10
- export type NetAddr = {
11
- transport?: TransportType;
12
- family?: "IPv4" | "IPv6" | "Unix";
13
- address?: string;
14
- port?: number;
15
- };
16
- export type ConnAddress = {
17
- remoteAddr: NetAddr;
18
- localAddr: NetAddr;
19
- };
20
- export type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "PATCH" | "HEAD" | "ALL" | "TRACE" | "CONNECT" | string;
21
- export declare class Request {
4
+ export declare class Request<Path extends PathType = any> {
22
5
  /**
23
6
  * Full request URL including protocol and query string
24
7
  * @type {string}
@@ -44,7 +27,7 @@ export declare class Request {
44
27
  * @param name - The parameter name.
45
28
  * @returns The parameter value if found, or undefined.
46
29
  */
47
- readonly params: Record<string, any>;
30
+ readonly params: ExtractParamsFromPath<Path>;
48
31
  /**
49
32
  * Represents the remote address details of a connected client.
50
33
  *
@@ -151,4 +134,3 @@ export declare class Request {
151
134
  */
152
135
  formData(options?: FormDataOptions): Promise<Record<string, any>>;
153
136
  }
154
- export {};
package/core/router.d.ts CHANGED
@@ -1,12 +1,6 @@
1
- import { Context, ResponseHeaders } from "./context.js";
2
- import MiddlewareConfigure, { DuplicateMiddlewares, UniqueMiddlewares } from "./MiddlewareConfigure.js";
3
- import { HTTPMethod } from "./request.js";
4
- export type ctx<T extends Record<string, any> = {}> = Context<T> & T;
5
- export type NextCallback = () => Promise<any>;
6
- export type CallbackReturn = Promise<Response> | Response;
7
- export type Callback<T extends Record<string, any> = {}> = (ctx: ctx<T>) => CallbackReturn;
8
- export type Middleware<T extends Record<string, any> = {}> = (ctx: ctx<T>, next: NextCallback) => Promise<Response | void> | Response | NextCallback;
9
- export type PathType = string | RegExp;
1
+ import { Callback, DuplicateMiddlewares, HTTPMethod, Middleware, PathType, StaticServeOption, UniqueMiddlewares } from "../types/index.js";
2
+ import { Context } from "./context.js";
3
+ import MiddlewareConfigure from "./MiddlewareConfigure.js";
10
4
  export type RouterConfig = {
11
5
  /**
12
6
  * `env` allows you to define environment variables for the router.
@@ -31,10 +25,6 @@ export declare class TrieRouter {
31
25
  isParam: boolean;
32
26
  constructor(pathname?: string);
33
27
  }
34
- export type StaticServeOption = {
35
- cacheControl?: string;
36
- headers?: ResponseHeaders;
37
- };
38
28
  export type RouterHandler<T extends Record<string, any>> = {
39
29
  callback: Callback<T>;
40
30
  paramNames: string[];
@@ -80,9 +70,9 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
80
70
  * // With multiple middlewares
81
71
  * app.get('/admin', [authMiddleware, adminMiddleware], (ctx) => { ... });
82
72
  */
83
- get(path: PathType, callback: Callback<T>): this;
84
- get(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
85
- get(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
73
+ get<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
74
+ get<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
75
+ get<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
86
76
  /**
87
77
  * Registers a Server-Sent Events (SSE) route handler for the given path.
88
78
  *
@@ -115,63 +105,63 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
115
105
  * @param {PathType} path - The route path for SSE (e.g. `/events`).
116
106
  * @param {(ctx: Context) => any} handler - A handler function that returns a streamed response.
117
107
  */
118
- sse(path: PathType, handler: (ctx: Context) => any): void;
108
+ sse<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, handler: (ctx: Context<T & U, Path>) => any): void;
119
109
  /**
120
110
  * Registers a POST route with optional middleware(s)
121
111
  * @param path - URL path pattern
122
112
  * @param args - Handler callback or middleware(s) + handler
123
113
  */
124
- post(path: PathType, callback: Callback<T>): this;
125
- post(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
126
- post(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
114
+ post<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
115
+ post<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
116
+ post<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
127
117
  /**
128
118
  * Registers a PUT route with optional middleware(s)
129
119
  * @param path - URL path pattern
130
120
  * @param args - Handler callback or middleware(s) + handler
131
121
  */
132
- put(path: PathType, callback: Callback<T>): this;
133
- put(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
134
- put(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
122
+ put<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
123
+ put<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
124
+ put<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
135
125
  /**
136
126
  * Registers a PATCH route with optional middleware(s)
137
127
  * @param path - URL path pattern
138
128
  * @param args - Handler callback or middleware(s) + handler
139
129
  */
140
- patch(path: PathType, callback: Callback<T>): this;
141
- patch(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
142
- patch(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
130
+ patch<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
131
+ patch<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
132
+ patch<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
143
133
  /**
144
134
  * Registers a DELETE route with optional middleware(s)
145
135
  * @param path - URL path pattern
146
136
  * @param args - Handler callback or middleware(s) + handler
147
137
  */
148
- delete(path: PathType, callback: Callback<T>): this;
149
- delete(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
150
- delete(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
138
+ delete<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
139
+ delete<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
140
+ delete<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
151
141
  /**
152
142
  * Registers an OPTIONS route (primarily for CORS preflight requests)
153
143
  * @param path - URL path pattern
154
144
  * @param args - Handler callback or middleware(s) + handler
155
145
  */
156
- options(path: PathType, callback: Callback<T>): this;
157
- options(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
158
- options(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
146
+ options<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
147
+ options<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
148
+ options<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
159
149
  /**
160
150
  * Registers a HEAD route (returns headers only)
161
151
  * @param path - URL path pattern
162
152
  * @param args - Handler callback or middleware(s) + handler
163
153
  */
164
- head(path: PathType, callback: Callback<T>): this;
165
- head(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
166
- head(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
154
+ head<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
155
+ head<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
156
+ head<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
167
157
  /**
168
158
  * Registers a route that responds to all HTTP methods
169
159
  * @param path - URL path pattern
170
160
  * @param args - Handler callback or middleware(s) + handler
171
161
  */
172
- all(path: PathType, callback: Callback<T>): this;
173
- all(path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
174
- all(path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
162
+ all<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
163
+ all<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
164
+ all<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
175
165
  /**
176
166
  * Generic method registration for custom HTTP methods
177
167
  * @param method - HTTP method name (e.g., 'PURGE')
@@ -182,10 +172,9 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
182
172
  * // Register custom method
183
173
  * server.addRoute('PURGE', '/cache', purgeHandler);
184
174
  */
185
- addRoute(method: HTTPMethod, path: PathType, callback: Callback<T>): this;
186
- addRoute(method: HTTPMethod, path: PathType, middleware: Middleware<T>): this;
187
- addRoute(method: HTTPMethod, path: PathType, middleware: Middleware<T>, callback: Callback<T>): this;
188
- addRoute(method: HTTPMethod, path: PathType, middlewares: Middleware<T>[], callback: Callback<T>): this;
175
+ addRoute<U extends Record<string, any> = {}, Path extends PathType = any>(method: HTTPMethod, path: Path, callback: Callback<T & U, Path>): this;
176
+ addRoute<U extends Record<string, any> = {}, Path extends PathType = any>(method: HTTPMethod, path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
177
+ addRoute<U extends Record<string, any> = {}, Path extends PathType = any>(method: HTTPMethod, path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
189
178
  /**
190
179
  * Mount a sub-router at specific path prefix
191
180
  * @param path - Base path for the sub-router
@@ -197,7 +186,7 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
197
186
  * apiRouter.get('/users', () => { ... });
198
187
  * server.addRouter('/api', apiRouter);
199
188
  */
200
- addRouter(path: string, router: Router<T | any>): void;
189
+ addRouter<U extends Record<string, any> = {}, Path extends string = any>(path: Path, router: Router<T | U | any>): void;
201
190
  /**
202
191
  * Create route group with shared path prefix
203
192
  * @param prefix - Path prefix for the group
@@ -209,7 +198,7 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
209
198
  * group.get('/users', v1UserHandler);
210
199
  * });
211
200
  */
212
- group(prefix: string, callback: (group: Router<T>) => void): this;
201
+ group<U extends Record<string, any> = {}, Prefix extends string = any>(prefix: Prefix, callback: (group: Router<T & U>) => void): this;
213
202
  /**
214
203
  * Register middleware with flexible signature
215
204
  * @overload
@@ -217,14 +206,14 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
217
206
  * @param middlewares - Middleware(s) to register
218
207
  * @param [callback] - Optional sub-router or handler
219
208
  */
220
- use(path: string, middlewares: Middleware<T>[], callback: Callback<T> | Router<T | any>): this;
221
- use(path: string, middleware: Middleware<T>, callback: Callback<T> | Router<T | any>): this;
222
- use(path: string, middlewares: Middleware<T>[]): this;
223
- use(path: string, middlewares: Middleware<T>): this;
224
- use(path: string, callback: Callback<T> | Router<T | any>): this;
225
- use(middlewares: Middleware<T>[], callback: Callback<T> | Router<T | any>): this;
226
- use(middleware: Middleware<T>, callback: Callback<T> | Router<T | any>): this;
227
- use(middlewares: Middleware<T>[]): this;
228
- use(middleware: Middleware<T>): this;
229
- use(callback: Callback<T> | Router<T | any>): this;
209
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path> | Router<T & U | any>): this;
210
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path> | Router<T & U | any>): this;
211
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[]): this;
212
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>): this;
213
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path> | Router<T & U | any>): this;
214
+ use<U extends Record<string, any> = {}, Path extends string = any>(middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path> | Router<T & U | any>): this;
215
+ use<U extends Record<string, any> = {}, Path extends string = any>(middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path> | Router<T & U | any>): this;
216
+ use<U extends Record<string, any> = {}, Path extends string = any>(middlewares: Middleware<T & U, Path>[]): this;
217
+ use<U extends Record<string, any> = {}, Path extends string = any>(middleware: Middleware<T & U, Path>): this;
218
+ use<U extends Record<string, any> = {}, Path extends string = any>(callback: Callback<T & U, Path> | Router<T & U | any>): this;
230
219
  }
package/core/router.js CHANGED
@@ -213,7 +213,7 @@ export class Router extends MiddlewareConfigure {
213
213
  if (!GlobalConfig.overwriteMethod && handler.has(method))
214
214
  return;
215
215
  return handler.set(method, {
216
- callback,
216
+ callback: callback,
217
217
  paramNames: [],
218
218
  regex: regex,
219
219
  middlewares: finalMiddleware
@@ -238,7 +238,7 @@ export class Router extends MiddlewareConfigure {
238
238
  if (!GlobalConfig.overwriteMethod && handler.has(method))
239
239
  return;
240
240
  return handler.set(method, {
241
- callback,
241
+ callback: callback,
242
242
  regex: regex,
243
243
  paramNames: paramNames,
244
244
  middlewares: finalMiddleware
package/core/server.d.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import { ConnAddress, HTTPMethod, Middleware } from "../types/index.js";
1
2
  import { Context } from "./context.js";
2
- import { ConnAddress, HTTPMethod } from "./request.js";
3
- import { Middleware, Router, RouterConfig } from "./router.js";
3
+ import { Router, RouterConfig } from "./router.js";
4
4
  export type TezXServeOptions = {
5
5
  connInfo: ConnAddress;
6
6
  };
package/helper/common.js CHANGED
@@ -3,6 +3,12 @@ export function generateID() {
3
3
  const random = Math.floor(Math.random() * 0xffffffffffff)
4
4
  .toString(16)
5
5
  .padStart(12, "0");
6
- const pid = (process.pid % 0x10000).toString(16).padStart(4, "0");
6
+ let pid = Math.floor(Math.random() * 0xffff).toString(16).padStart(4, "0");
7
+ if (typeof Deno !== "undefined" && typeof Deno.pid === "number") {
8
+ pid = (Deno.pid % 0xffff).toString(16).padStart(4, "0");
9
+ }
10
+ else if (typeof process !== "undefined" && typeof process.pid === "number") {
11
+ pid = (process.pid % 0xffff).toString(16).padStart(4, "0");
12
+ }
7
13
  return `${timestamp}-${random}-${pid}`;
8
14
  }
package/helper/index.d.ts CHANGED
@@ -1,23 +1,23 @@
1
+ import { GlobalConfig } from "../core/config.js";
1
2
  import { Environment } from "../core/environment.js";
3
+ import { colorText } from "../utils/colors.js";
2
4
  import { sanitizePathSplit } from "../utils/url.js";
3
5
  import { generateID } from "./common.js";
4
- export { GlobalConfig } from "../core/config.js";
5
- export type { AdapterType } from "../core/config.js";
6
- export { Environment } from "../core/environment.js";
7
- export { sanitizePathSplit } from "../utils/url.js";
6
+ export type { AdapterType } from "../types/index.js";
8
7
  export type { UrlRef } from "../utils/url.js";
9
- export { generateID } from "./common.js";
8
+ export { colorText, Environment, generateID, GlobalConfig, sanitizePathSplit };
10
9
  declare const _default: {
11
10
  Environment: typeof Environment;
11
+ colorText: typeof colorText;
12
12
  GlobalConfig: {
13
13
  new (): {};
14
- notFound: import("../index.js").Callback;
15
- onError: <T extends Record<string, any> = {}>(err: string, ctx: import("../index.js").Context<T>) => Response;
14
+ notFound: import("../types/index.js").Callback;
15
+ onError: <T extends Record<string, any> = {}>(err: string, ctx: import("../types/index.js").ctx<T>) => Response;
16
16
  allowDuplicateMw?: boolean;
17
17
  overwriteMethod?: boolean;
18
18
  debugMode?: boolean;
19
19
  server: any;
20
- adapter: import("../core/config.js").AdapterType;
20
+ adapter: import("../types/index.js").AdapterType;
21
21
  readonly debugging: {
22
22
  info: (msg: string, ...args: unknown[]) => void;
23
23
  warn: (msg: string, ...args: unknown[]) => void;
package/helper/index.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import { GlobalConfig } from "../core/config.js";
2
2
  import { Environment } from "../core/environment.js";
3
+ import { colorText } from "../utils/colors.js";
3
4
  import { sanitizePathSplit } from "../utils/url.js";
4
5
  import { generateID } from "./common.js";
5
- export { GlobalConfig } from "../core/config.js";
6
- export { Environment } from "../core/environment.js";
7
- export { sanitizePathSplit } from "../utils/url.js";
8
- export { generateID } from "./common.js";
6
+ export { colorText, Environment, generateID, GlobalConfig, sanitizePathSplit };
9
7
  export default {
10
8
  Environment,
9
+ colorText,
11
10
  GlobalConfig,
12
11
  sanitizePathSplit,
13
12
  generateID,
package/index.d.ts CHANGED
@@ -1,15 +1,12 @@
1
1
  import { Router } from "./core/router.js";
2
2
  import { TezX } from "./core/server.js";
3
- import { regexMatchRoute, compileRegexRoute } from "./utils/regexRouter.js";
4
- export { Router } from "./core/router.js";
5
- export type { Callback, ctx as Context, Middleware, NextCallback, RouterConfig, PathType, StaticServeOption } from "./core/router.js";
6
- export type { AdapterType } from "./core/config.js";
7
- export type { CookieOptions, ResponseHeaders } from "./core/context.js";
8
- export type { NetAddr as AddressType, ConnAddress, FormDataOptions, HTTPMethod } from "./core/request.js";
9
- export { TezX } from "./core/server.js";
3
+ import { compileRegexRoute, regexMatchRoute } from "./utils/regexRouter.js";
4
+ export type { Context as BaseContext } from "./core/context.js";
5
+ export type { RouterConfig } from "./core/router.js";
6
+ export type { AdapterType, NetAddr as AddressType, Callback, CallbackReturn, ConnAddress, ctx as Context, CookieOptions, FormDataOptions, HTTPMethod, Middleware, NextCallback, PathType, ResponseHeaders, StaticServeOption } from "./types/index.js";
10
7
  export type { TezXConfig, TezXServeOptions } from "./core/server.js";
11
8
  export type { UrlRef } from "./utils/url.js";
12
- export { regexMatchRoute, compileRegexRoute };
9
+ export { compileRegexRoute, regexMatchRoute, Router, TezX };
13
10
  export declare let version: string;
14
11
  declare const _default: {
15
12
  Router: typeof Router;
package/index.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import { Router } from "./core/router.js";
2
2
  import { TezX } from "./core/server.js";
3
- import { regexMatchRoute, compileRegexRoute } from "./utils/regexRouter.js";
4
- export { Router } from "./core/router.js";
5
- export { TezX } from "./core/server.js";
6
- export { regexMatchRoute, compileRegexRoute };
7
- export let version = "2.0.8";
3
+ import { compileRegexRoute, regexMatchRoute } from "./utils/regexRouter.js";
4
+ export { compileRegexRoute, regexMatchRoute, Router, TezX };
5
+ export let version = "2.0.11";
8
6
  export default {
9
7
  Router,
10
- regexMatchRoute, compileRegexRoute,
8
+ regexMatchRoute,
9
+ compileRegexRoute,
11
10
  TezX,
12
11
  version,
13
12
  };
@@ -1,5 +1,5 @@
1
1
  import { Context } from "../core/context.js";
2
- import { CallbackReturn, Middleware } from "../core/router.js";
2
+ import { CallbackReturn, Middleware } from "../types/index.js";
3
3
  /**
4
4
  * Supported authentication method types.
5
5
  */
@@ -1,4 +1,5 @@
1
- import { Context, Middleware } from "../index.js";
1
+ import { Context } from "../core/context.js";
2
+ import { Middleware } from "../types/index.js";
2
3
  export type CacheRule = {
3
4
  /**
4
5
  * 🎯 Condition to determine if this rule applies.
@@ -1,4 +1,4 @@
1
- import { ctx as Context } from "../core/router.js";
1
+ import { ctx as Context } from "../types/index.js";
2
2
  export type CorsOptions = {
3
3
  origin?: string | RegExp | (string | RegExp)[] | ((reqOrigin: string) => boolean);
4
4
  methods?: string[];
@@ -1,5 +1,5 @@
1
- import { CallbackReturn } from "../core/router.js";
2
1
  import { Context, Middleware } from "../index.js";
2
+ import { CallbackReturn } from "../types/index.js";
3
3
  export type DetectBotReason = "User-Agent" | "Blacklisted IP" | "Query Parameter" | "Rate Limiting" | "Custom Detector" | "Multiple Indicators";
4
4
  export type BotDetectionResult = {
5
5
  isBot: boolean;
@@ -33,6 +33,6 @@ declare const _default: {
33
33
  requestTimeout: (options: import("./requestTimeout.js").TimeoutOptions) => import("../index.js").Middleware;
34
34
  sanitizeHeaders: (options?: import("./sanitizeHeader.js").SanitizeHeadersOptions) => import("../index.js").Middleware;
35
35
  secureHeaders: (options?: import("./secureHeaders.js").SecurityHeaderOptions) => import("../index.js").Middleware;
36
- xssProtection: (options?: import("./xssProtection.js").XSSProtectionOptions) => (ctx: import("../core/context.js").Context, next: import("../index.js").NextCallback) => Promise<any>;
36
+ xssProtection: (options?: import("./xssProtection.js").XSSProtectionOptions) => (ctx: import("../index.js").BaseContext, next: import("../index.js").NextCallback) => Promise<any>;
37
37
  };
38
38
  export default _default;
@@ -1,4 +1,4 @@
1
- import { Middleware } from "../core/router.js";
1
+ import { Middleware } from "../types/index.js";
2
2
  /**
3
3
  * Logger Middleware
4
4
  * Logs incoming requests with method, pathname, status, and execution time.
@@ -1,5 +1,4 @@
1
- import { Context } from "../index.js";
2
- import { Middleware } from "../core/router.js";
1
+ import { Context, Middleware } from "../index.js";
3
2
  export type PaginationOptions = {
4
3
  /**
5
4
  * 🔢 Default page number when not specified
@@ -1,4 +1,4 @@
1
- import { Middleware } from "../core/router.js";
1
+ import { Middleware } from "../types/index.js";
2
2
  /**
3
3
  * PoweredBy Middleware
4
4
  * Adds an "X-Powered-By" header to responses.
@@ -1,5 +1,5 @@
1
1
  import { Context } from "../core/context.js";
2
- import { CallbackReturn, Middleware } from "../core/router.js";
2
+ import { CallbackReturn, Middleware } from "../types/index.js";
3
3
  export type RateLimiterOptions = {
4
4
  /**
5
5
  * 🔴 Maximum allowed requests in the time window
@@ -1,4 +1,4 @@
1
- import { Middleware } from "../core/router.js";
1
+ import { Middleware } from "../types/index.js";
2
2
  /**
3
3
  * Request ID Middleware
4
4
  * Assigns a unique request ID to each incoming request.
@@ -1,5 +1,5 @@
1
1
  import { Context } from "../core/context.js";
2
- import { CallbackReturn, Middleware } from "../core/router.js";
2
+ import { CallbackReturn, Middleware } from "../types/index.js";
3
3
  export type TimeoutOptions = {
4
4
  /**
5
5
  * ⏳ Function to dynamically determine the timeout duration (in milliseconds).
@@ -1,4 +1,4 @@
1
- import { Middleware } from "../core/router.js";
1
+ import { Middleware } from "../types/index.js";
2
2
  export type SanitizeHeadersOptions = {
3
3
  /**
4
4
  * 🟢 Whitelist of allowed headers (case-insensitive)
@@ -1,5 +1,5 @@
1
1
  import { Context } from "../core/context.js";
2
- import { Middleware } from "../core/router.js";
2
+ import { Middleware } from "../types/index.js";
3
3
  export type DynamicHeaderValue = string | ((ctx: Context) => string | undefined);
4
4
  export type SecurityHeaderOptions = {
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import { Context } from "../core/context.js";
2
- import { NextCallback } from "../core/router.js";
2
+ import { NextCallback } from "../types/index.js";
3
3
  export type XSSProtectionOptions = {
4
4
  /**
5
5
  * 🟢 Whether to enable XSS protection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tezx",
3
- "version": "2.0.8",
3
+ "version": "2.0.11",
4
4
  "description": "TezX is a high-performance, lightweight JavaScript framework designed for speed, scalability, and flexibility. It enables efficient routing, middleware management, and static file serving with minimal configuration. Fully compatible with Node.js, Deno, and Bun.",
5
5
  "main": "cjs/index.js",
6
6
  "module": "index.js",
@@ -61,13 +61,14 @@
61
61
  },
62
62
  "files": [
63
63
  "adapter/",
64
+ "bun/",
64
65
  "cjs/",
65
66
  "core/",
66
- "helper/",
67
- "bun/",
68
67
  "deno/",
68
+ "helper/",
69
69
  "node/",
70
70
  "middleware/",
71
+ "types/",
71
72
  "utils/",
72
73
  "ws/",
73
74
  "index.js",
@@ -0,0 +1,59 @@
1
+ export type DuplicateMiddlewares = Middleware<any>[];
2
+ export type UniqueMiddlewares = Set<Middleware<any>>;
3
+ export interface CookieOptions {
4
+ expires?: Date;
5
+ path?: string;
6
+ maxAge?: number;
7
+ domain?: string;
8
+ secure?: boolean;
9
+ httpOnly?: boolean;
10
+ sameSite?: "Strict" | "Lax" | "None";
11
+ }
12
+ export type AdapterType = "bun" | "deno" | "node";
13
+ export type ResponseHeaders = Record<string, string>;
14
+ import { Context } from "../core/context.js";
15
+ export type StaticServeOption = {
16
+ cacheControl?: string;
17
+ headers?: ResponseHeaders;
18
+ };
19
+ type ExtractParam<Path extends string> = Path extends `${infer _Start}:${infer Param}/${infer Rest}` ? Param extends `${infer Name}?` ? {
20
+ [K in Name]?: string;
21
+ } & ExtractParam<`/${Rest}`> : {
22
+ [K in Param]: string;
23
+ } & ExtractParam<`/${Rest}`> : Path extends `${infer _Start}:${infer Param}` ? Param extends `${infer Name}?` ? {
24
+ [K in Name]?: string;
25
+ } : {
26
+ [K in Param]: string;
27
+ } : {};
28
+ type ExtractWildcard<Path extends string> = Path extends `${string}*${infer Wildcard}` ? Wildcard extends "" ? {
29
+ "*": string;
30
+ } : {
31
+ [K in Wildcard]: string;
32
+ } : {};
33
+ export type ExtractRouteParams<Path extends string> = ExtractParam<Path> & ExtractWildcard<Path> & Record<string, string>;
34
+ export type ExtractParamsFromPath<Path extends PathType> = (Path extends string ? ExtractRouteParams<Path> : {}) & Record<string, string>;
35
+ export type NextCallback = () => Promise<any>;
36
+ export type CallbackReturn = Promise<Response> | Response;
37
+ export type ctx<T extends Record<string, any> = {}, Path extends PathType = any> = Context<T, Path> & T;
38
+ export type Callback<T extends Record<string, any> = {}, Path extends PathType = any> = (ctx: ctx<T, Path>) => CallbackReturn;
39
+ export type Middleware<T extends Record<string, any> = {}, Path extends PathType = any> = (ctx: ctx<T, Path>, next: NextCallback) => Promise<Response | void> | Response | NextCallback;
40
+ export type PathType = string | RegExp;
41
+ export type FormDataOptions = {
42
+ maxSize?: number;
43
+ allowedTypes?: string[];
44
+ sanitized?: boolean;
45
+ maxFiles?: number;
46
+ };
47
+ type TransportType = "tcp" | "udp" | "unix" | "pipe" | "unixpacket";
48
+ export type NetAddr = {
49
+ transport?: TransportType;
50
+ family?: "IPv4" | "IPv6" | "Unix";
51
+ address?: string;
52
+ port?: number;
53
+ };
54
+ export type ConnAddress = {
55
+ remoteAddr: NetAddr;
56
+ localAddr: NetAddr;
57
+ };
58
+ export type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "PATCH" | "HEAD" | "ALL" | "TRACE" | "CONNECT" | string;
59
+ export {};
package/types/index.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,7 +1,2 @@
1
1
  export type LogLevel = "info" | "warn" | "error" | "debug" | "success";
2
2
  export declare const loggerOutput: (level: LogLevel, message: string, ...args: unknown[]) => void;
3
- /**
4
- * A universal logger function that measures and logs the processing time of an operation.
5
- * @param label - A label to identify the operation being logged.
6
- * @param callback - The operation to measure and execute.
7
- */
@@ -1,6 +1,5 @@
1
1
  import { COLORS } from "./colors.js";
2
2
  export const loggerOutput = (level, message, ...args) => {
3
- const timestamp = new Date().toISOString();
4
3
  const LEVEL_COLORS = {
5
4
  info: COLORS.blue,
6
5
  warn: COLORS.yellow,
@@ -60,7 +60,7 @@ export declare function addBaseToRegex(basePath: string, routeRegex: RegExp): Re
60
60
  * const result = regexMatchRoute(regex, url, paramNames);
61
61
  * // result = { success: true, params: { userId: "123", postId: "abc" } }
62
62
  */
63
- export declare function regexMatchRoute(regex: RegExp, url: string, paramNames: string[]): {
63
+ export declare function regexMatchRoute(regex: RegExp, url: string, paramNames?: string[]): {
64
64
  params: Record<string, string | null>;
65
65
  success: boolean;
66
66
  };
@@ -41,7 +41,7 @@ export function addBaseToRegex(basePath, routeRegex) {
41
41
  const finalRegex = new RegExp(`^${combined}$`);
42
42
  return finalRegex;
43
43
  }
44
- export function regexMatchRoute(regex, url, paramNames) {
44
+ export function regexMatchRoute(regex, url, paramNames = []) {
45
45
  const match = url.match(regex);
46
46
  if (!match)
47
47
  return { success: false, params: {} };
@@ -1,4 +1,5 @@
1
- import { Router, StaticServeOption } from "../core/router.js";
1
+ import { Router } from "../core/router.js";
2
+ import { StaticServeOption } from "../types/index.js";
2
3
  export declare const mimeTypes: {
3
4
  [key: string]: string;
4
5
  };
package/ws/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { CallbackReturn, Middleware } from "../core/router.js";
2
1
  import { Context } from "../index.js";
2
+ import { CallbackReturn, Middleware } from "../types/index.js";
3
3
  /**
4
4
  * Type definition for WebSocket event handlers.
5
5
  * @template T - The type of data expected by the handler
package/ws/node.js CHANGED
@@ -8,7 +8,7 @@ export class NodeTransport {
8
8
  maxPayload: options.maxPayload ?? 1048576,
9
9
  perMessageDeflate: options.perMessageDeflate ?? false,
10
10
  });
11
- server.on("upgrade", (request, socket, head) => {
11
+ server?.on("upgrade", (request, socket, head) => {
12
12
  this.wss?.handleUpgrade(request, socket, head, (ws) => {
13
13
  this.wss?.emit("connection", ws, request);
14
14
  this.setupHandlers(ws, event, options);