tinny-backend 1.0.8 → 1.1.0

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.
@@ -12,6 +12,7 @@ export default class Server {
12
12
  private logs;
13
13
  private ServerName;
14
14
  private defaultHandler;
15
+ private golobalMidellWare;
15
16
  private Wss;
16
17
  getMethodHandlers(): Handlers[];
17
18
  getHttpServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
@@ -26,6 +27,7 @@ export default class Server {
26
27
  private getSession;
27
28
  getHost(): string;
28
29
  getPort(): number;
30
+ use(fun: HandlerFun): void;
29
31
  add(opt: AddOption): void;
30
32
  decorate(name: string, data: any): void;
31
33
  useDecorator(name: string): Decorator | null | undefined;
@@ -21,6 +21,7 @@ export default class Server {
21
21
  logs;
22
22
  ServerName;
23
23
  defaultHandler;
24
+ golobalMidellWare;
24
25
  Wss = null;
25
26
  getMethodHandlers() {
26
27
  return this.methodHandler;
@@ -74,6 +75,9 @@ export default class Server {
74
75
  getPort() {
75
76
  return this.PORT;
76
77
  }
78
+ use(fun) {
79
+ this.golobalMidellWare.push({ handler: fun });
80
+ }
77
81
  add(opt) {
78
82
  const newPath = opt.path.replace(/(?<=.)\/+$/, "");
79
83
  const paramNames = [];
@@ -220,6 +224,7 @@ export default class Server {
220
224
  });
221
225
  }
222
226
  constructor(args) {
227
+ this.golobalMidellWare = [];
223
228
  this.sessions = [];
224
229
  this.decorators = [];
225
230
  this.methodHandler = [];
@@ -250,7 +255,6 @@ export default class Server {
250
255
  }
251
256
  Req.body = body;
252
257
  body = "";
253
- let handlersCount = 0;
254
258
  for (let i = 0; i < this.methodHandler.length; i++) {
255
259
  const match = Req.ReqUrl?.pathname.match(this.methodHandler[i]?.regex ?? "");
256
260
  if (this.methodHandler[i]?.method == req.method
@@ -259,24 +263,28 @@ export default class Server {
259
263
  this.methodHandler[i]?.paramNames.forEach((name, index) => {
260
264
  Req.params[name] = match[index + 1] || "";
261
265
  });
262
- if (!Res.isClosed) {
266
+ if (!res.writableEnded) {
263
267
  if (this.methodHandler[i]?.middelWares && typeof this.methodHandler[i]?.middelWares != "undefined") {
264
268
  for (const middelware of this.methodHandler[i]?.middelWares ?? []) {
265
269
  await middelware(Req, Res);
266
- if (Res.isClosed)
270
+ if (res.writableEnded)
267
271
  break;
268
272
  }
269
273
  }
270
- if (!Res.isClosed)
274
+ if (!res.writableEnded)
271
275
  await this.methodHandler[i]?.handler(req, res);
272
276
  const nextFn = this.methodHandler[i]?.next;
273
- if (typeof nextFn === "function")
277
+ if (typeof nextFn === "function" && !res.writableEnded)
274
278
  await nextFn(Req, Res);
275
- handlersCount++;
276
279
  }
277
280
  }
278
281
  }
279
- if (!handlersCount) {
282
+ for (const fun of this.golobalMidellWare) {
283
+ if (res.writableEnded)
284
+ break;
285
+ await fun.handler(req, res);
286
+ }
287
+ if (!res.writableEnded) {
280
288
  this.defaultHandler(Req, Res);
281
289
  }
282
290
  });
@@ -21,6 +21,9 @@ export type Cookie = {
21
21
  key: string;
22
22
  value: string;
23
23
  };
24
+ export type GolobalMidellWare = {
25
+ handler: HandlerFun;
26
+ };
24
27
  export type ServerOptions = {
25
28
  port?: number;
26
29
  hostname?: string;
@@ -1 +1 @@
1
- export declare const VERSION = "1.0.8";
1
+ export declare const VERSION = "1.1.0";
@@ -1 +1 @@
1
- export const VERSION = "1.0.8";
1
+ export const VERSION = "1.1.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinny-backend",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "A lightweight and flexible backend framework built on Node.js.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -46,6 +46,7 @@
46
46
  "build": "tsc",
47
47
  "dev": "tsx src/index.ts",
48
48
  "test-ws": "tsx src/test/ws.test.ts",
49
+ "test-use": "tsx src/test/use.test.ts",
49
50
  "test": "vitest",
50
51
  "lint": "eslint . --ext .ts",
51
52
  "lint:fix": "eslint . --ext .ts --fix",