@heliosjs/middlewares 4.0.2 → 4.0.4

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.
@@ -1,4 +1,4 @@
1
- import { InterceptorCB } from '@heliosjs/core';
1
+ import { InterceptorCB } from './types/core';
2
2
  /**
3
3
  * Decorator to register an interceptor at the controller or method level.
4
4
  *
@@ -42,3 +42,4 @@ export type Meta = {
42
42
  userAgent: string;
43
43
  startTime: number;
44
44
  };
45
+ export type NonEmptyArray<T> = readonly [T, ...T[]] | [T, ...T[]];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heliosjs/middlewares",
3
- "version": "4.0.2",
3
+ "version": "4.0.4",
4
4
  "description": "Core functionality for Quantum Flow",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "reflect-metadata": "^0.2.2"
31
31
  },
32
32
  "peerDependencies": {
33
- "@heliosjs/core": "^2.1.2",
33
+ "@heliosjs/core": "^2.1.3",
34
34
  "typescript": ">=4.0.0"
35
35
  }
36
36
  }
@@ -1,50 +0,0 @@
1
- import WebSocket from 'ws';
2
- export type WebSocketHandlerType = 'connection' | 'message' | 'close' | 'error';
3
- export interface WebSocketEvent {
4
- type: WebSocketHandlerType;
5
- client: WebSocketClient;
6
- message?: WebSocketMessage;
7
- data?: unknown;
8
- }
9
- export interface WebSocketClient {
10
- id: string;
11
- socket: WebSocket;
12
- topics: Set<string>;
13
- data: Record<string, unknown>;
14
- connectedAt: Date;
15
- }
16
- export interface WebSocketMessage {
17
- type: string;
18
- topic?: string;
19
- data: any;
20
- clientId?: string;
21
- }
22
- export interface WebSocketStats {
23
- clients: number;
24
- topics: Array<{
25
- topic: string;
26
- subscribers: number;
27
- }>;
28
- }
29
- export interface IWebSocketService {
30
- sendToClient(clientId: string, message: unknown): boolean;
31
- publishToTopic(topic: string, data: any, exclude?: string[]): void;
32
- broadcast(message: unknown, excludeClientId?: string): void;
33
- getStats(): {
34
- clients: number;
35
- topics: Array<{
36
- topic: string;
37
- subscribers: number;
38
- }>;
39
- };
40
- isAvailable(): boolean;
41
- }
42
- export interface IWebSocketServer {
43
- sendToClient(clientId: string, message: unknown): boolean;
44
- publishToTopic(topic: string, data: any, exclude: string[]): void;
45
- broadcast(message: unknown, excludeClientId?: string): void;
46
- getStats(): WebSocketStats;
47
- subscribeToTopic(client: WebSocketClient, topic: string): void;
48
- unsubscribeFromTopic(client: WebSocketClient, topic: string): void;
49
- registerControllers(controllers: (new (...args: any[]) => any)[]): void;
50
- }
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/ws/index.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export * from './server';
2
- export * from './service';
@@ -1,19 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./server"), exports);
18
- __exportStar(require("./service"), exports);
19
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/sse/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,4CAA0B"}
@@ -1,14 +0,0 @@
1
- import { ServerResponse } from 'node:http';
2
- import { ControllerType, ISSEServer, SSEClient, SSEMessage } from '../../types/core';
3
- export declare class SSEServer implements ISSEServer {
4
- private readonly clients;
5
- controllers: any[];
6
- createConnection(res: ServerResponse): SSEClient;
7
- sendToClient(clientId: string, message: SSEMessage): boolean;
8
- broadcast(message: SSEMessage, excludeClientId?: string): void;
9
- getStats(): {
10
- clients: number;
11
- };
12
- registerControllers(controllers: ControllerType[]): void;
13
- private triggerHandlers;
14
- }
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SSEServer = void 0;
4
- const shared_1 = require("../shared");
5
- class SSEServer {
6
- clients = new Map();
7
- controllers = [];
8
- createConnection(res) {
9
- const clientId = (0, shared_1.generateUniqueId)();
10
- res.writeHead(200, {
11
- 'Content-Type': 'text/event-stream',
12
- 'Cache-Control': 'no-cache',
13
- Connection: 'keep-alive',
14
- 'Access-Control-Allow-Origin': '*',
15
- });
16
- res.write(': connected\n\n');
17
- const client = {
18
- id: clientId,
19
- response: res,
20
- topics: new Set(),
21
- data: {},
22
- connectedAt: new Date(),
23
- };
24
- this.clients.set(clientId, client);
25
- this.triggerHandlers('connection', {
26
- type: 'connection',
27
- client,
28
- data: res,
29
- });
30
- res.on('close', () => {
31
- this.clients.delete(clientId);
32
- this.triggerHandlers('close', { type: 'close', client });
33
- });
34
- return client;
35
- }
36
- sendToClient(clientId, message) {
37
- const client = this.clients.get(clientId);
38
- if (!client)
39
- return false;
40
- try {
41
- let sseMessage = '';
42
- if (message.event)
43
- sseMessage += `event: ${message.event}\n`;
44
- if (message.id)
45
- sseMessage += `id: ${message.id}\n`;
46
- if (message.retry)
47
- sseMessage += `retry: ${message.retry}\n`;
48
- const dataStr = typeof message.data === 'string' ? message.data : JSON.stringify(message.data);
49
- dataStr.split('\n').forEach((line) => {
50
- sseMessage += `data: ${line}\n`;
51
- });
52
- sseMessage += '\n';
53
- client.response.write(sseMessage);
54
- return true;
55
- }
56
- catch (error) {
57
- console.error(`SSE send error to ${clientId}:`, error);
58
- return false;
59
- }
60
- }
61
- broadcast(message, excludeClientId) {
62
- this.clients.forEach((_, clientId) => {
63
- if (clientId !== excludeClientId) {
64
- this.sendToClient(clientId, message);
65
- }
66
- });
67
- }
68
- getStats() {
69
- return { clients: this.clients.size };
70
- }
71
- registerControllers(controllers) {
72
- this.controllers = controllers.filter((c) => c.sse);
73
- }
74
- async triggerHandlers(eventType, event) {
75
- for (const controller of this.controllers) {
76
- if (controller.sse.handlers && controller.sse.handlers[eventType]) {
77
- for (const handler of controller.sse.handlers[eventType]) {
78
- await handler.fn(event).catch(console.error);
79
- }
80
- }
81
- }
82
- }
83
- }
84
- exports.SSEServer = SSEServer;
85
- //# sourceMappingURL=server.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/utils/sse/server.ts"],"names":[],"mappings":";;;AAGA,sCAA6C;AAC7C,MAAa,SAAS;IACH,OAAO,GAA2B,IAAI,GAAG,EAAE,CAAC;IAC7D,WAAW,GAAU,EAAE,CAAC;IAEjB,gBAAgB,CAAC,GAAmB;QACzC,MAAM,QAAQ,GAAG,IAAA,yBAAgB,GAAE,CAAC;QAEpC,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE;YACjB,cAAc,EAAE,mBAAmB;YACnC,eAAe,EAAE,UAAU;YAC3B,UAAU,EAAE,YAAY;YACxB,6BAA6B,EAAE,GAAG;SACnC,CAAC,CAAC;QAEH,GAAG,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;QAE7B,MAAM,MAAM,GAAc;YACxB,EAAE,EAAE,QAAQ;YACZ,QAAQ,EAAE,GAAG;YACb,MAAM,EAAE,IAAI,GAAG,EAAE;YACjB,IAAI,EAAE,EAAE;YACR,WAAW,EAAE,IAAI,IAAI,EAAE;SACxB,CAAC;QAEF,IAAI,CAAC,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C/C,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA3FD,8BA2FC"}
@@ -1,17 +0,0 @@
1
- import { ServerResponse } from 'http';
2
- import { ISSEService, SSEClient, SSEMessage } from '../../types/core';
3
- import { SSEServer } from './server';
4
- export declare class SSEService implements ISSEService {
5
- private static instance;
6
- private sse;
7
- private constructor();
8
- static getInstance(): SSEService;
9
- initialize(sse: SSEServer): void;
10
- sendToClient(clientId: string, message: SSEMessage): boolean;
11
- broadcast(message: SSEMessage, excludeClientId?: string): void;
12
- getStats(): {
13
- clients: number;
14
- };
15
- isAvailable(): boolean;
16
- createConnection(res: ServerResponse): SSEClient;
17
- }
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SSEService = void 0;
4
- class SSEService {
5
- static instance;
6
- sse = null;
7
- constructor() { }
8
- static getInstance() {
9
- if (!SSEService.instance) {
10
- SSEService.instance = new SSEService();
11
- }
12
- return SSEService.instance;
13
- }
14
- initialize(sse) {
15
- this.sse = sse;
16
- }
17
- sendToClient(clientId, message) {
18
- if (!this.sse)
19
- return false;
20
- return this.sse.sendToClient(clientId, message);
21
- }
22
- broadcast(message, excludeClientId) {
23
- if (!this.sse)
24
- return;
25
- this.sse.broadcast(message, excludeClientId);
26
- }
27
- getStats() {
28
- if (!this.sse)
29
- return { clients: 0 };
30
- return this.sse.getStats();
31
- }
32
- isAvailable() {
33
- return this.sse !== null;
34
- }
35
- createConnection(res) {
36
- return this.sse.createConnection(res);
37
- }
38
- }
39
- exports.SSEService = SSEService;
40
- //# sourceMappingURL=service.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/utils/sse/service.ts"],"names":[],"mappings":";;;AAIA,MAAa,UAAU;IACb,MAAM,CAAC,QAAQ,CAAa;IAC5B,GAAG,GAAqB,IAAI,CAAC;IAErC,gBAAuB,CAAC;IAExB,MAAM,CAAC,WAAW;QAChB,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;YACzB,UAAU,CAAC,QAAQ,GAAG,IAAI,UAAU,EAAE,CAAC;QACzC,CAAC;QACD,OAAO,UAAU,CAAC,QAAQ,CAAC;IAC7B,CAAC;IAED,UAAU,CAAC,GAAc;QACvB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED,YAAY,CAAC,QAAgB,EAAE,OAAmB;QAChD,IAAI,CAAC,IAAI,CAAC,GAAG;YAAE,OAAO,KAAK,CAAC;QAC5B,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,SAAS,CAAC,OAAmB,EAAE,eAAwB;QACrD,IAAI,CAAC,IAAI,CAAC,GAAG;YAAE,OAAO;QACtB,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IAC/C,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,IAAI,CAAC,GAAG;YAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;QACrC,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;IAED,WAAW;QACT,OAAO,IAAI,CAAC,GAAG,KAAK,IAAI,CAAC;IAC3B,CAAC;IAED,gBAAgB,CAAC,GAAmB;QAClC,OAAO,IAAI,CAAC,GAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;IACzC,CAAC;CACF;AAvCD,gCAuCC"}