tinny-backend 1.0.7 → 1.0.9

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,17 +12,22 @@ export default class Server {
12
12
  private logs;
13
13
  private ServerName;
14
14
  private defaultHandler;
15
+ private golobalMidellWare;
16
+ private Wss;
15
17
  getMethodHandlers(): Handlers[];
16
18
  getHttpServer(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
17
19
  getDecorators(): Decorator[];
18
20
  getUpTime(): number;
19
21
  getReqCount(): number;
20
22
  getLogs(): Logs[];
23
+ createWsServer(): this;
24
+ getWsServer(): import("ws").WebSocketServer;
21
25
  private generateKey;
22
26
  private generateSession;
23
27
  private getSession;
24
28
  getHost(): string;
25
29
  getPort(): number;
30
+ use(fun: HandlerFun): void;
26
31
  add(opt: AddOption): void;
27
32
  decorate(name: string, data: any): void;
28
33
  useDecorator(name: string): Decorator | null | undefined;
@@ -8,6 +8,7 @@ import { fileURLToPath } from 'url';
8
8
  import { readdir } from 'node:fs/promises';
9
9
  import RequestParser from "./Request.js";
10
10
  import ResponseParser from "./Response.js";
11
+ import { TinnyWs } from "./Websocket.js";
11
12
  export default class Server {
12
13
  PORT;
13
14
  hostname;
@@ -20,6 +21,8 @@ export default class Server {
20
21
  logs;
21
22
  ServerName;
22
23
  defaultHandler;
24
+ golobalMidellWare;
25
+ Wss = null;
23
26
  getMethodHandlers() {
24
27
  return this.methodHandler;
25
28
  }
@@ -38,6 +41,17 @@ export default class Server {
38
41
  getLogs() {
39
42
  return this.logs;
40
43
  }
44
+ createWsServer() {
45
+ if (!this.Wss)
46
+ this.Wss = new TinnyWs(this);
47
+ return this;
48
+ }
49
+ getWsServer() {
50
+ if (this.Wss)
51
+ return this.Wss.getWss();
52
+ this.Wss = new TinnyWs(this);
53
+ return this.Wss.getWss();
54
+ }
41
55
  generateKey() {
42
56
  return randomBytes(16).toString('hex');
43
57
  }
@@ -61,6 +75,9 @@ export default class Server {
61
75
  getPort() {
62
76
  return this.PORT;
63
77
  }
78
+ use(fun) {
79
+ this.golobalMidellWare.push({ handler: fun });
80
+ }
64
81
  add(opt) {
65
82
  const newPath = opt.path.replace(/(?<=.)\/+$/, "");
66
83
  const paramNames = [];
@@ -207,6 +224,7 @@ export default class Server {
207
224
  });
208
225
  }
209
226
  constructor(args) {
227
+ this.golobalMidellWare = [];
210
228
  this.sessions = [];
211
229
  this.decorators = [];
212
230
  this.methodHandler = [];
@@ -238,6 +256,13 @@ export default class Server {
238
256
  Req.body = body;
239
257
  body = "";
240
258
  let handlersCount = 0;
259
+ for (const fun of this.golobalMidellWare) {
260
+ await fun.handler(req, res);
261
+ if (res.writableEnded) {
262
+ handlersCount++;
263
+ break;
264
+ }
265
+ }
241
266
  for (let i = 0; i < this.methodHandler.length; i++) {
242
267
  const match = Req.ReqUrl?.pathname.match(this.methodHandler[i]?.regex ?? "");
243
268
  if (this.methodHandler[i]?.method == req.method
@@ -0,0 +1,7 @@
1
+ import { WebSocketServer } from 'ws';
2
+ import type Server from './Server.js';
3
+ export declare class TinnyWs {
4
+ private wss;
5
+ constructor(server: Server);
6
+ getWss(): WebSocketServer;
7
+ }
@@ -0,0 +1,14 @@
1
+ import { WebSocketServer } from 'ws';
2
+ export class TinnyWs {
3
+ wss;
4
+ constructor(server) {
5
+ this.wss = new WebSocketServer({ server: server.getHttpServer() });
6
+ this.wss.on("error", (error) => {
7
+ server.log(`WS: ${error.message}`, "error");
8
+ });
9
+ return this;
10
+ }
11
+ getWss() {
12
+ return this.wss;
13
+ }
14
+ }
@@ -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.7";
1
+ export declare const VERSION = "1.0.9";
@@ -1 +1 @@
1
- export const VERSION = "1.0.7";
1
+ export const VERSION = "1.0.9";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinny-backend",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "A lightweight and flexible backend framework built on Node.js.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -45,6 +45,8 @@
45
45
  "scripts": {
46
46
  "build": "tsc",
47
47
  "dev": "tsx src/index.ts",
48
+ "test-ws": "tsx src/test/ws.test.ts",
49
+ "test-use": "tsx src/test/use.test.ts",
48
50
  "test": "vitest",
49
51
  "lint": "eslint . --ext .ts",
50
52
  "lint:fix": "eslint . --ext .ts --fix",
@@ -53,6 +55,7 @@
53
55
  },
54
56
  "devDependencies": {
55
57
  "@types/node": "^25.9.3",
58
+ "@types/ws": "^8.18.1",
56
59
  "@typescript-eslint/eslint-plugin": "^8.39.1",
57
60
  "@typescript-eslint/parser": "^8.39.1",
58
61
  "eslint": "^9.36.0",
@@ -62,10 +65,13 @@
62
65
  "vitest": "^4.1.9"
63
66
  },
64
67
  "engines": {
65
- "node": ">=18.0.0"
68
+ "node": ">=24.13.1"
66
69
  },
67
70
  "publishConfig": {
68
71
  "access": "public"
69
72
  },
70
- "private": false
71
- }
73
+ "private": false,
74
+ "dependencies": {
75
+ "ws": "^8.21.3"
76
+ }
77
+ }