vona-module-a-socket 5.0.0 → 5.1.1

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 (107) hide show
  1. package/LICENSE +21 -0
  2. package/cli/socketConnection/boilerplate/{{sceneName}}.{{beanName}}.ts_ +20 -0
  3. package/cli/socketNamespace/boilerplate/{{sceneName}}.{{beanName}}.ts_ +17 -0
  4. package/cli/socketPacket/boilerplate/{{sceneName}}.{{beanName}}.ts_ +15 -0
  5. package/dist/.metadata/index.d.ts +260 -0
  6. package/dist/.metadata/index.d.ts.map +1 -0
  7. package/dist/.metadata/this.d.ts +3 -0
  8. package/dist/.metadata/this.d.ts.map +1 -0
  9. package/dist/bean/bean.socket.d.ts +16 -0
  10. package/dist/bean/bean.socket.d.ts.map +1 -0
  11. package/dist/bean/broadcast.broadcast.d.ts +14 -0
  12. package/dist/bean/broadcast.broadcast.d.ts.map +1 -0
  13. package/dist/bean/broadcast.send.d.ts +13 -0
  14. package/dist/bean/broadcast.send.d.ts.map +1 -0
  15. package/dist/bean/hmr.socketConnection.d.ts +7 -0
  16. package/dist/bean/hmr.socketConnection.d.ts.map +1 -0
  17. package/dist/bean/hmr.socketPacket.d.ts +7 -0
  18. package/dist/bean/hmr.socketPacket.d.ts.map +1 -0
  19. package/dist/bean/socketConnection.alive.d.ts +14 -0
  20. package/dist/bean/socketConnection.alive.d.ts.map +1 -0
  21. package/dist/bean/socketConnection.app.d.ts +11 -0
  22. package/dist/bean/socketConnection.app.d.ts.map +1 -0
  23. package/dist/bean/socketConnection.cors.d.ts +11 -0
  24. package/dist/bean/socketConnection.cors.d.ts.map +1 -0
  25. package/dist/bean/socketConnection.event.d.ts +11 -0
  26. package/dist/bean/socketConnection.event.d.ts.map +1 -0
  27. package/dist/bean/socketConnection.instance.d.ts +11 -0
  28. package/dist/bean/socketConnection.instance.d.ts.map +1 -0
  29. package/dist/bean/socketConnection.passport.d.ts +12 -0
  30. package/dist/bean/socketConnection.passport.d.ts.map +1 -0
  31. package/dist/bean/socketConnection.ready.d.ts +11 -0
  32. package/dist/bean/socketConnection.ready.d.ts.map +1 -0
  33. package/dist/bean/socketPacket.event.d.ts +10 -0
  34. package/dist/bean/socketPacket.event.d.ts.map +1 -0
  35. package/dist/bean/socketPacket.performAction.d.ts +11 -0
  36. package/dist/bean/socketPacket.performAction.d.ts.map +1 -0
  37. package/dist/config/config.d.ts +15 -0
  38. package/dist/config/config.d.ts.map +1 -0
  39. package/dist/index.d.ts +4 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +632 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/lib/beanSocketNamespaceBase.d.ts +7 -0
  44. package/dist/lib/beanSocketNamespaceBase.d.ts.map +1 -0
  45. package/dist/lib/const.d.ts +8 -0
  46. package/dist/lib/const.d.ts.map +1 -0
  47. package/dist/lib/index.d.ts +6 -0
  48. package/dist/lib/index.d.ts.map +1 -0
  49. package/dist/lib/socketConnection.d.ts +3 -0
  50. package/dist/lib/socketConnection.d.ts.map +1 -0
  51. package/dist/lib/socketNamespace.d.ts +4 -0
  52. package/dist/lib/socketNamespace.d.ts.map +1 -0
  53. package/dist/lib/socketPacket.d.ts +3 -0
  54. package/dist/lib/socketPacket.d.ts.map +1 -0
  55. package/dist/monkey.d.ts +7 -0
  56. package/dist/monkey.d.ts.map +1 -0
  57. package/dist/service/socket.d.ts +13 -0
  58. package/dist/service/socket.d.ts.map +1 -0
  59. package/dist/service/socketEvent.d.ts +10 -0
  60. package/dist/service/socketEvent.d.ts.map +1 -0
  61. package/dist/types/index.d.ts +6 -0
  62. package/dist/types/index.d.ts.map +1 -0
  63. package/dist/types/socket.d.ts +18 -0
  64. package/dist/types/socket.d.ts.map +1 -0
  65. package/dist/types/socketConnection.d.ts +34 -0
  66. package/dist/types/socketConnection.d.ts.map +1 -0
  67. package/dist/types/socketEvent.d.ts +46 -0
  68. package/dist/types/socketEvent.d.ts.map +1 -0
  69. package/dist/types/socketNamespace.d.ts +23 -0
  70. package/dist/types/socketNamespace.d.ts.map +1 -0
  71. package/dist/types/socketPacket.d.ts +25 -0
  72. package/dist/types/socketPacket.d.ts.map +1 -0
  73. package/package.json +58 -22
  74. package/src/.metadata/index.ts +359 -0
  75. package/src/.metadata/this.ts +2 -0
  76. package/src/bean/bean.socket.ts +60 -0
  77. package/src/bean/broadcast.broadcast.ts +23 -0
  78. package/src/bean/broadcast.send.ts +22 -0
  79. package/src/bean/hmr.socketConnection.ts +14 -0
  80. package/src/bean/hmr.socketPacket.ts +14 -0
  81. package/src/bean/socketConnection.alive.ts +48 -0
  82. package/src/bean/socketConnection.app.ts +26 -0
  83. package/src/bean/socketConnection.cors.ts +27 -0
  84. package/src/bean/socketConnection.event.ts +34 -0
  85. package/src/bean/socketConnection.instance.ts +23 -0
  86. package/src/bean/socketConnection.passport.ts +37 -0
  87. package/src/bean/socketConnection.ready.ts +21 -0
  88. package/src/bean/socketPacket.event.ts +29 -0
  89. package/src/bean/socketPacket.performAction.ts +37 -0
  90. package/src/config/config.ts +19 -0
  91. package/src/index.ts +3 -0
  92. package/src/lib/beanSocketNamespaceBase.ts +15 -0
  93. package/src/lib/const.ts +22 -0
  94. package/src/lib/index.ts +5 -0
  95. package/src/lib/socketConnection.ts +7 -0
  96. package/src/lib/socketNamespace.ts +9 -0
  97. package/src/lib/socketPacket.ts +7 -0
  98. package/src/monkey.ts +15 -0
  99. package/src/service/socket.ts +180 -0
  100. package/src/service/socketEvent.ts +30 -0
  101. package/src/types/index.ts +5 -0
  102. package/src/types/socket.ts +21 -0
  103. package/src/types/socketConnection.ts +41 -0
  104. package/src/types/socketEvent.ts +55 -0
  105. package/src/types/socketNamespace.ts +27 -0
  106. package/src/types/socketPacket.ts +30 -0
  107. package/README.md +0 -1
@@ -0,0 +1,359 @@
1
+ // eslint-disable
2
+ /** bean: begin */
3
+ export * from '../bean/bean.socket.ts';
4
+
5
+ import 'vona';
6
+ declare module 'vona' {
7
+
8
+
9
+ }
10
+ declare module 'vona-module-a-socket' {
11
+
12
+ export interface BeanSocket {
13
+ /** @internal */
14
+ get scope(): ScopeModuleASocket;
15
+ }
16
+ }
17
+ /** bean: end */
18
+ /** bean: begin */
19
+ import type { BeanSocket } from '../bean/bean.socket.ts';
20
+ import 'vona';
21
+ declare module 'vona' {
22
+ export interface IBeanRecordGlobal {
23
+ 'socket': BeanSocket;
24
+ }
25
+ }
26
+ /** bean: end */
27
+ /** service: begin */
28
+ export * from '../service/socket.ts';
29
+ export * from '../service/socketEvent.ts';
30
+
31
+ import 'vona-module-a-bean';
32
+ declare module 'vona-module-a-bean' {
33
+
34
+ export interface IServiceRecord {
35
+ 'a-socket:socket': never;
36
+ 'a-socket:socketEvent': never;
37
+ }
38
+
39
+
40
+ }
41
+ declare module 'vona-module-a-socket' {
42
+
43
+ export interface ServiceSocket {
44
+ /** @internal */
45
+ get scope(): ScopeModuleASocket;
46
+ }
47
+
48
+ export interface ServiceSocket {
49
+ get $beanFullName(): 'a-socket.service.socket';
50
+ get $onionName(): 'a-socket:socket';
51
+
52
+ }
53
+
54
+ export interface ServiceSocketEvent {
55
+ /** @internal */
56
+ get scope(): ScopeModuleASocket;
57
+ }
58
+
59
+ export interface ServiceSocketEvent {
60
+ get $beanFullName(): 'a-socket.service.socketEvent';
61
+ get $onionName(): 'a-socket:socketEvent';
62
+
63
+ }
64
+ }
65
+ /** service: end */
66
+ /** service: begin */
67
+ import type { ServiceSocket } from '../service/socket.ts';
68
+ import type { ServiceSocketEvent } from '../service/socketEvent.ts';
69
+ export interface IModuleService {
70
+ 'socket': ServiceSocket;
71
+ 'socketEvent': ServiceSocketEvent;
72
+ }
73
+ /** service: end */
74
+ /** service: begin */
75
+
76
+ import 'vona';
77
+ declare module 'vona' {
78
+ export interface IBeanRecordGeneral {
79
+ 'a-socket.service.socket': ServiceSocket;
80
+ 'a-socket.service.socketEvent': ServiceSocketEvent;
81
+ }
82
+ }
83
+ /** service: end */
84
+ /** broadcast: begin */
85
+ export * from '../bean/broadcast.broadcast.ts';
86
+ export * from '../bean/broadcast.send.ts';
87
+
88
+ import { type IDecoratorBroadcastOptions } from 'vona-module-a-broadcast';
89
+ declare module 'vona-module-a-broadcast' {
90
+
91
+ export interface IBroadcastRecord {
92
+ 'a-socket:broadcast': IDecoratorBroadcastOptions;
93
+ 'a-socket:send': IDecoratorBroadcastOptions;
94
+ }
95
+
96
+
97
+ }
98
+ declare module 'vona-module-a-socket' {
99
+
100
+ export interface BroadcastBroadcast {
101
+ /** @internal */
102
+ get scope(): ScopeModuleASocket;
103
+ }
104
+
105
+ export interface BroadcastBroadcast {
106
+ get $beanFullName(): 'a-socket.broadcast.broadcast';
107
+ get $onionName(): 'a-socket:broadcast';
108
+ get $onionOptions(): IDecoratorBroadcastOptions;
109
+ }
110
+
111
+ export interface BroadcastSend {
112
+ /** @internal */
113
+ get scope(): ScopeModuleASocket;
114
+ }
115
+
116
+ export interface BroadcastSend {
117
+ get $beanFullName(): 'a-socket.broadcast.send';
118
+ get $onionName(): 'a-socket:send';
119
+ get $onionOptions(): IDecoratorBroadcastOptions;
120
+ }
121
+ }
122
+ /** broadcast: end */
123
+ /** broadcast: begin */
124
+ import type { BroadcastBroadcast } from '../bean/broadcast.broadcast.ts';
125
+ import type { BroadcastSend } from '../bean/broadcast.send.ts';
126
+ export interface IModuleBroadcast {
127
+ 'broadcast': BroadcastBroadcast;
128
+ 'send': BroadcastSend;
129
+ }
130
+ /** broadcast: end */
131
+ /** socketConnection: begin */
132
+ export * from '../bean/socketConnection.alive.ts';
133
+ export * from '../bean/socketConnection.app.ts';
134
+ export * from '../bean/socketConnection.cors.ts';
135
+ export * from '../bean/socketConnection.event.ts';
136
+ export * from '../bean/socketConnection.instance.ts';
137
+ export * from '../bean/socketConnection.passport.ts';
138
+ export * from '../bean/socketConnection.ready.ts';
139
+ import type { ISocketConnectionOptionsAlive } from '../bean/socketConnection.alive.ts';
140
+ import type { ISocketConnectionOptionsApp } from '../bean/socketConnection.app.ts';
141
+ import type { ISocketConnectionOptionsCors } from '../bean/socketConnection.cors.ts';
142
+ import type { ISocketConnectionOptionsEvent } from '../bean/socketConnection.event.ts';
143
+ import type { ISocketConnectionOptionsInstance } from '../bean/socketConnection.instance.ts';
144
+ import type { ISocketConnectionOptionsPassport } from '../bean/socketConnection.passport.ts';
145
+ import type { ISocketConnectionOptionsReady } from '../bean/socketConnection.ready.ts';
146
+ import 'vona-module-a-socket';
147
+ declare module 'vona-module-a-socket' {
148
+
149
+ export interface ISocketConnectionRecord {
150
+ 'a-socket:alive': ISocketConnectionOptionsAlive;
151
+ 'a-socket:app': ISocketConnectionOptionsApp;
152
+ 'a-socket:cors': ISocketConnectionOptionsCors;
153
+ 'a-socket:event': ISocketConnectionOptionsEvent;
154
+ 'a-socket:instance': ISocketConnectionOptionsInstance;
155
+ 'a-socket:passport': ISocketConnectionOptionsPassport;
156
+ 'a-socket:ready': ISocketConnectionOptionsReady;
157
+ }
158
+
159
+
160
+ }
161
+ declare module 'vona-module-a-socket' {
162
+
163
+ export interface SocketConnectionAlive {
164
+ /** @internal */
165
+ get scope(): ScopeModuleASocket;
166
+ }
167
+
168
+ export interface SocketConnectionAlive {
169
+ get $beanFullName(): 'a-socket.socketConnection.alive';
170
+ get $onionName(): 'a-socket:alive';
171
+ get $onionOptions(): ISocketConnectionOptionsAlive;
172
+ }
173
+
174
+ export interface SocketConnectionApp {
175
+ /** @internal */
176
+ get scope(): ScopeModuleASocket;
177
+ }
178
+
179
+ export interface SocketConnectionApp {
180
+ get $beanFullName(): 'a-socket.socketConnection.app';
181
+ get $onionName(): 'a-socket:app';
182
+ get $onionOptions(): ISocketConnectionOptionsApp;
183
+ }
184
+
185
+ export interface SocketConnectionCors {
186
+ /** @internal */
187
+ get scope(): ScopeModuleASocket;
188
+ }
189
+
190
+ export interface SocketConnectionCors {
191
+ get $beanFullName(): 'a-socket.socketConnection.cors';
192
+ get $onionName(): 'a-socket:cors';
193
+ get $onionOptions(): ISocketConnectionOptionsCors;
194
+ }
195
+
196
+ export interface SocketConnectionEvent {
197
+ /** @internal */
198
+ get scope(): ScopeModuleASocket;
199
+ }
200
+
201
+ export interface SocketConnectionEvent {
202
+ get $beanFullName(): 'a-socket.socketConnection.event';
203
+ get $onionName(): 'a-socket:event';
204
+ get $onionOptions(): ISocketConnectionOptionsEvent;
205
+ }
206
+
207
+ export interface SocketConnectionInstance {
208
+ /** @internal */
209
+ get scope(): ScopeModuleASocket;
210
+ }
211
+
212
+ export interface SocketConnectionInstance {
213
+ get $beanFullName(): 'a-socket.socketConnection.instance';
214
+ get $onionName(): 'a-socket:instance';
215
+ get $onionOptions(): ISocketConnectionOptionsInstance;
216
+ }
217
+
218
+ export interface SocketConnectionPassport {
219
+ /** @internal */
220
+ get scope(): ScopeModuleASocket;
221
+ }
222
+
223
+ export interface SocketConnectionPassport {
224
+ get $beanFullName(): 'a-socket.socketConnection.passport';
225
+ get $onionName(): 'a-socket:passport';
226
+ get $onionOptions(): ISocketConnectionOptionsPassport;
227
+ }
228
+
229
+ export interface SocketConnectionReady {
230
+ /** @internal */
231
+ get scope(): ScopeModuleASocket;
232
+ }
233
+
234
+ export interface SocketConnectionReady {
235
+ get $beanFullName(): 'a-socket.socketConnection.ready';
236
+ get $onionName(): 'a-socket:ready';
237
+ get $onionOptions(): ISocketConnectionOptionsReady;
238
+ }
239
+ }
240
+ /** socketConnection: end */
241
+ /** socketPacket: begin */
242
+ export * from '../bean/socketPacket.event.ts';
243
+ export * from '../bean/socketPacket.performAction.ts';
244
+ import type { ISocketPacketOptionsEvent } from '../bean/socketPacket.event.ts';
245
+ import type { ISocketPacketOptionsPerformAction } from '../bean/socketPacket.performAction.ts';
246
+ import 'vona-module-a-socket';
247
+ declare module 'vona-module-a-socket' {
248
+
249
+ export interface ISocketPacketRecord {
250
+ 'a-socket:event': ISocketPacketOptionsEvent;
251
+ 'a-socket:performAction': ISocketPacketOptionsPerformAction;
252
+ }
253
+
254
+
255
+ }
256
+ declare module 'vona-module-a-socket' {
257
+
258
+ export interface SocketPacketEvent {
259
+ /** @internal */
260
+ get scope(): ScopeModuleASocket;
261
+ }
262
+
263
+ export interface SocketPacketEvent {
264
+ get $beanFullName(): 'a-socket.socketPacket.event';
265
+ get $onionName(): 'a-socket:event';
266
+ get $onionOptions(): ISocketPacketOptionsEvent;
267
+ }
268
+
269
+ export interface SocketPacketPerformAction {
270
+ /** @internal */
271
+ get scope(): ScopeModuleASocket;
272
+ }
273
+
274
+ export interface SocketPacketPerformAction {
275
+ get $beanFullName(): 'a-socket.socketPacket.performAction';
276
+ get $onionName(): 'a-socket:performAction';
277
+ get $onionOptions(): ISocketPacketOptionsPerformAction;
278
+ }
279
+ }
280
+ /** socketPacket: end */
281
+ /** hmr: begin */
282
+ export * from '../bean/hmr.socketConnection.ts';
283
+ export * from '../bean/hmr.socketPacket.ts';
284
+
285
+ import 'vona';
286
+ declare module 'vona' {
287
+
288
+ export interface IHmrRecord {
289
+ 'a-socket:socketConnection': never;
290
+ 'a-socket:socketPacket': never;
291
+ }
292
+
293
+
294
+ }
295
+ declare module 'vona-module-a-socket' {
296
+
297
+ export interface HmrSocketConnection {
298
+ /** @internal */
299
+ get scope(): ScopeModuleASocket;
300
+ }
301
+
302
+ export interface HmrSocketConnection {
303
+ get $beanFullName(): 'a-socket.hmr.socketConnection';
304
+ get $onionName(): 'a-socket:socketConnection';
305
+
306
+ }
307
+
308
+ export interface HmrSocketPacket {
309
+ /** @internal */
310
+ get scope(): ScopeModuleASocket;
311
+ }
312
+
313
+ export interface HmrSocketPacket {
314
+ get $beanFullName(): 'a-socket.hmr.socketPacket';
315
+ get $onionName(): 'a-socket:socketPacket';
316
+
317
+ }
318
+ }
319
+ /** hmr: end */
320
+ /** config: begin */
321
+ export * from '../config/config.ts';
322
+ import type { config } from '../config/config.ts';
323
+ /** config: end */
324
+ /** monkey: begin */
325
+ export * from '../monkey.ts';
326
+ /** monkey: end */
327
+ /** scope: begin */
328
+ import { BeanScopeBase, type BeanScopeUtil, type TypeModuleConfig } from 'vona';
329
+ import { Scope } from 'vona-module-a-bean';
330
+
331
+ @Scope()
332
+ export class ScopeModuleASocket extends BeanScopeBase {}
333
+
334
+ export interface ScopeModuleASocket {
335
+ util: BeanScopeUtil;
336
+ config: TypeModuleConfig<typeof config>;
337
+ service: IModuleService;
338
+ broadcast: IModuleBroadcast;
339
+ }
340
+
341
+ import 'vona';
342
+ declare module 'vona' {
343
+ export interface IBeanScopeRecord {
344
+ 'a-socket': ScopeModuleASocket;
345
+ }
346
+
347
+ export interface IBeanScopeContainer {
348
+ socket: ScopeModuleASocket;
349
+ }
350
+
351
+ export interface IBeanScopeConfig {
352
+ 'a-socket': ReturnType<typeof config>;
353
+ }
354
+
355
+
356
+
357
+
358
+ }
359
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export const __ThisModule__ = 'a-socket';
2
+ export { ScopeModuleASocket as ScopeModule } from './index.ts';
@@ -0,0 +1,60 @@
1
+ import type { WebSocket } from 'ws';
2
+
3
+ import { BeanBase } from 'vona';
4
+ import { Bean } from 'vona-module-a-bean';
5
+
6
+ import type { ISocketNamespaceRecord } from '../types/socketNamespace.ts';
7
+
8
+ @Bean()
9
+ export class BeanSocket extends BeanBase {
10
+ private _clients: Record<string, WebSocket | undefined> = {};
11
+ private _clientsNamespace: Record<keyof ISocketNamespaceRecord, string[] | undefined> = {} as any;
12
+
13
+ protected async __dispose__() {
14
+ this.close();
15
+ }
16
+
17
+ public close() {
18
+ const clients = this._clients;
19
+ this._clients = {};
20
+ for (const id in clients) {
21
+ clients[id]?.terminate();
22
+ }
23
+ }
24
+
25
+ public get clients() {
26
+ return this._clients;
27
+ }
28
+
29
+ public get clientsNamespace() {
30
+ return this._clientsNamespace;
31
+ }
32
+
33
+ getClient(id: string) {
34
+ return this._clients[id];
35
+ }
36
+
37
+ addClient(ws: WebSocket) {
38
+ const id = ws.id;
39
+ const namespace = ws.namespace;
40
+ this._clients[id] = ws;
41
+ if (!this._clientsNamespace[namespace]) this._clientsNamespace[namespace] = [];
42
+ this._clientsNamespace[namespace].push(id);
43
+ }
44
+
45
+ removeClient(ws: WebSocket): void;
46
+ removeClient(id: string): void;
47
+ removeClient(ws: WebSocket | string): void {
48
+ const id = typeof ws === 'string' ? ws : ws.id;
49
+ const ws2 = this.getClient(id);
50
+ if (!ws2) return;
51
+ const namespace = ws2.namespace;
52
+ delete this._clients[id];
53
+ if (this._clientsNamespace[namespace]) {
54
+ const index = this._clientsNamespace[namespace].indexOf(id);
55
+ if (index !== -1) {
56
+ this._clientsNamespace[namespace].splice(index, 1);
57
+ }
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,23 @@
1
+ import type { IBroadcastExecute } from 'vona-module-a-broadcast';
2
+
3
+ import { BeanBroadcastBase, Broadcast } from 'vona-module-a-broadcast';
4
+
5
+ import type { ISendEventOptions } from '../types/socketEvent.ts';
6
+ import type { ISocketNamespaceRecord } from '../types/socketNamespace.ts';
7
+
8
+ export interface TypeBroadcastBroadcastJobData {
9
+ namespace: keyof ISocketNamespaceRecord;
10
+ eventName: PropertyKey;
11
+ data?: unknown;
12
+ options?: ISendEventOptions;
13
+ }
14
+
15
+ @Broadcast()
16
+ export class BroadcastBroadcast extends BeanBroadcastBase<TypeBroadcastBroadcastJobData> implements IBroadcastExecute<TypeBroadcastBroadcastJobData> {
17
+ async execute(data: TypeBroadcastBroadcastJobData, isEmitter?: boolean) {
18
+ const { namespace, eventName, data: eventData, options } = data;
19
+ if (!isEmitter) {
20
+ this.scope.service.socketEvent.broadcastWorker(namespace, eventName, eventData, options);
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,22 @@
1
+ import type { IBroadcastExecute } from 'vona-module-a-broadcast';
2
+
3
+ import { BeanBroadcastBase, Broadcast } from 'vona-module-a-broadcast';
4
+
5
+ import type { ISendEventOptions } from '../types/socketEvent.ts';
6
+
7
+ export interface TypeBroadcastSendJobData {
8
+ id: string;
9
+ eventName: PropertyKey;
10
+ data?: unknown;
11
+ options?: ISendEventOptions;
12
+ }
13
+
14
+ @Broadcast()
15
+ export class BroadcastSend extends BeanBroadcastBase<TypeBroadcastSendJobData> implements IBroadcastExecute<TypeBroadcastSendJobData> {
16
+ async execute(data: TypeBroadcastSendJobData, isEmitter?: boolean) {
17
+ const { id, eventName, data: eventData, options } = data;
18
+ if (!isEmitter) {
19
+ this.scope.service.socketEvent.sendWorker(id, eventName, eventData, options);
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,14 @@
1
+ import type { IDecoratorBeanOptionsBase } from 'vona';
2
+ import type { IHmrReload } from 'vona-module-a-hmr';
3
+
4
+ import { BeanBase } from 'vona';
5
+ import { Hmr } from 'vona-module-a-hmrbase';
6
+
7
+ import { clearAllCacheSocketConnections } from '../lib/const.ts';
8
+
9
+ @Hmr()
10
+ export class HmrSocketConnection extends BeanBase implements IHmrReload {
11
+ async reload(_beanOptions: IDecoratorBeanOptionsBase) {
12
+ clearAllCacheSocketConnections(this.app);
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ import type { IDecoratorBeanOptionsBase } from 'vona';
2
+ import type { IHmrReload } from 'vona-module-a-hmr';
3
+
4
+ import { BeanBase } from 'vona';
5
+ import { Hmr } from 'vona-module-a-hmrbase';
6
+
7
+ import { clearAllCacheSocketPackets } from '../lib/const.ts';
8
+
9
+ @Hmr()
10
+ export class HmrSocketPacket extends BeanBase implements IHmrReload {
11
+ async reload(_beanOptions: IDecoratorBeanOptionsBase) {
12
+ clearAllCacheSocketPackets(this.app);
13
+ }
14
+ }
@@ -0,0 +1,48 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorSocketConnectionOptions, ISocketConnectionExecute } from 'vona-module-a-socket';
3
+ import type { WebSocket } from 'ws';
4
+
5
+ import { BeanBase } from 'vona';
6
+ import { SocketConnection } from 'vona-module-a-socket';
7
+
8
+ export interface ISocketConnectionOptionsAlive extends IDecoratorSocketConnectionOptions {}
9
+
10
+ @SocketConnection<ISocketConnectionOptionsAlive>()
11
+ export class SocketConnectionAlive extends BeanBase implements ISocketConnectionExecute {
12
+ private _interval: any;
13
+
14
+ protected async __dispose__() {
15
+ if (this._interval) {
16
+ clearInterval(this._interval);
17
+ this._interval = null;
18
+ }
19
+ }
20
+
21
+ async enter(ws: WebSocket, _options: ISocketConnectionOptionsAlive, next: Next): Promise<void> {
22
+ // isAlive
23
+ ws.isAlive = true;
24
+ ws.on('pong', () => {
25
+ ws.isAlive = true;
26
+ });
27
+ // start interval
28
+ this._startInterval();
29
+ // next
30
+ return next();
31
+ }
32
+
33
+ async exit(_ws: WebSocket, _options: ISocketConnectionOptionsAlive, next: Next): Promise<void> {
34
+ // next
35
+ await next();
36
+ }
37
+
38
+ private _startInterval() {
39
+ if (this._interval) return;
40
+ this._interval = setInterval(() => {
41
+ this.app.wss.clients.forEach(ws => {
42
+ if (ws.isAlive === false) return ws.terminate();
43
+ ws.isAlive = false;
44
+ ws.ping();
45
+ });
46
+ }, this.scope.config.timeout.ping);
47
+ }
48
+ }
@@ -0,0 +1,26 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorSocketConnectionOptions, ISocketConnectionExecute } from 'vona-module-a-socket';
3
+ import type { WebSocket } from 'ws';
4
+
5
+ import { BeanBase } from 'vona';
6
+ import { SocketConnection } from 'vona-module-a-socket';
7
+
8
+ export interface ISocketConnectionOptionsApp extends IDecoratorSocketConnectionOptions {}
9
+
10
+ @SocketConnection<ISocketConnectionOptionsApp>({ dependencies: 'a-socket:alive' })
11
+ export class SocketConnectionApp extends BeanBase implements ISocketConnectionExecute {
12
+ async enter(_ws: WebSocket, _options: ISocketConnectionOptionsApp, next: Next): Promise<void> {
13
+ // check appReady
14
+ if (!this.ctx.innerAccess) {
15
+ if (this.app.meta.appClose) this.app.throw(423);
16
+ await this.$scope.instance.service.instance.checkAppReady();
17
+ }
18
+ // next
19
+ return next();
20
+ }
21
+
22
+ async exit(_ws: WebSocket, _options: ISocketConnectionOptionsApp, next: Next): Promise<void> {
23
+ // next
24
+ return next();
25
+ }
26
+ }
@@ -0,0 +1,27 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorSocketConnectionOptions, ISocketConnectionExecute } from 'vona-module-a-socket';
3
+ import type { WebSocket } from 'ws';
4
+
5
+ import { BeanBase } from 'vona';
6
+ import { SocketConnection } from 'vona-module-a-socket';
7
+
8
+ export interface ISocketConnectionOptionsCors extends IDecoratorSocketConnectionOptions {}
9
+
10
+ @SocketConnection<ISocketConnectionOptionsCors>({ dependencies: 'a-socket:instance' })
11
+ export class SocketConnectionCors extends BeanBase implements ISocketConnectionExecute {
12
+ async enter(ws: WebSocket, _options: ISocketConnectionOptionsCors, next: Next): Promise<void> {
13
+ // cors
14
+ const origin = this.bean.security.checkOrigin(this.ctx.get('origin'), this.ctx.host);
15
+ if (!origin) {
16
+ ws.terminate();
17
+ return;
18
+ }
19
+ // next
20
+ return next();
21
+ }
22
+
23
+ async exit(_ws: WebSocket, _options: ISocketConnectionOptionsCors, next: Next): Promise<void> {
24
+ // next
25
+ return next();
26
+ }
27
+ }
@@ -0,0 +1,34 @@
1
+ import type { Next } from 'vona';
2
+ import type { WebSocket } from 'ws';
3
+
4
+ import { BeanBase, cast } from 'vona';
5
+
6
+ import type { IDecoratorSocketConnectionOptions, ISocketConnectionExecute } from '../types/socketConnection.ts';
7
+ import type { ISocketEventRecord } from '../types/socketEvent.ts';
8
+
9
+ import { SocketConnection } from '../lib/socketConnection.ts';
10
+ import { socketEventRecord } from '../types/socketEvent.ts';
11
+
12
+ export interface ISocketConnectionOptionsEvent extends IDecoratorSocketConnectionOptions {}
13
+
14
+ @SocketConnection<ISocketConnectionOptionsEvent>({ dependencies: 'a-socket:cors' })
15
+ export class SocketConnectionEvent extends BeanBase implements ISocketConnectionExecute {
16
+ async enter(ws: WebSocket, _options: ISocketConnectionOptionsEvent, next: Next): Promise<void> {
17
+ // sendEvent
18
+ cast(ws).sendEvent = (eventName: keyof ISocketEventRecord, data, options, cb) => {
19
+ if (typeof options === 'function') {
20
+ cb = options;
21
+ options = {};
22
+ }
23
+ const eventNameInner = socketEventRecord[eventName] ?? eventName;
24
+ ws.send(`${this.scope.config.eventPrefix}${JSON.stringify([eventNameInner, data])}`, options, cb);
25
+ };
26
+ // next
27
+ return next();
28
+ }
29
+
30
+ async exit(_ws: WebSocket, _options: ISocketConnectionOptionsEvent, next: Next): Promise<void> {
31
+ // next
32
+ return next();
33
+ }
34
+ }
@@ -0,0 +1,23 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorSocketConnectionOptions, ISocketConnectionExecute } from 'vona-module-a-socket';
3
+ import type { WebSocket } from 'ws';
4
+
5
+ import { BeanBase } from 'vona';
6
+ import { SocketConnection } from 'vona-module-a-socket';
7
+
8
+ export interface ISocketConnectionOptionsInstance extends IDecoratorSocketConnectionOptions {}
9
+
10
+ @SocketConnection<ISocketConnectionOptionsInstance>({ dependencies: 'a-socket:app' })
11
+ export class SocketConnectionInstance extends BeanBase implements ISocketConnectionExecute {
12
+ async enter(_ws: WebSocket, _options: ISocketConnectionOptionsInstance, next: Next): Promise<void> {
13
+ // init instance
14
+ await this.$scope.instance.service.instance.initInstance();
15
+ // next
16
+ return next();
17
+ }
18
+
19
+ async exit(_ws: WebSocket, _options: ISocketConnectionOptionsInstance, next: Next): Promise<void> {
20
+ // next
21
+ return next();
22
+ }
23
+ }