@stonyx/sockets 0.1.1-alpha.1 → 0.1.1-alpha.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.
package/pnpm-lock.yaml DELETED
@@ -1,387 +0,0 @@
1
- lockfileVersion: '9.0'
2
-
3
- settings:
4
- autoInstallPeers: true
5
- excludeLinksFromLockfile: false
6
-
7
- importers:
8
-
9
- .:
10
- dependencies:
11
- stonyx:
12
- specifier: 0.2.3-beta.6
13
- version: 0.2.3-beta.6
14
- ws:
15
- specifier: ^8.18.1
16
- version: 8.19.0
17
- devDependencies:
18
- '@stonyx/utils':
19
- specifier: 0.2.3-beta.5
20
- version: 0.2.3-beta.5
21
- qunit:
22
- specifier: ^2.24.1
23
- version: 2.25.0
24
- sinon:
25
- specifier: ^21.0.0
26
- version: 21.0.3
27
-
28
- packages:
29
-
30
- '@sinonjs/commons@3.0.1':
31
- resolution: {integrity: sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==}
32
-
33
- '@sinonjs/fake-timers@15.1.1':
34
- resolution: {integrity: sha512-cO5W33JgAPbOh07tvZjUOJ7oWhtaqGHiZw+11DPbyqh2kHTBc3eF/CjJDeQ4205RLQsX6rxCuYOroFQwl7JDRw==}
35
-
36
- '@sinonjs/samsam@9.0.3':
37
- resolution: {integrity: sha512-ZgYY7Dc2RW+OUdnZ1DEHg00lhRt+9BjymPKHog4PRFzr1U3MbK57+djmscWyKxzO1qfunHqs4N45WWyKIFKpiQ==}
38
-
39
- '@stonyx/utils@0.2.3-beta.5':
40
- resolution: {integrity: sha512-FRxrTg34yuXvDgxOWruZT2ybuyNIOOlLFI5SjFJeMd9oWvA5EbOKe7bi1yQ8jC/2cyp7tkEQLW6e9yQMjC0JYA==}
41
-
42
- call-bind-apply-helpers@1.0.2:
43
- resolution: {integrity: sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==}
44
- engines: {node: '>= 0.4'}
45
-
46
- call-bound@1.0.4:
47
- resolution: {integrity: sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==}
48
- engines: {node: '>= 0.4'}
49
-
50
- chalk@5.6.2:
51
- resolution: {integrity: sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==}
52
- engines: {node: ^12.17.0 || ^14.13 || >=16.0.0}
53
-
54
- commander@7.2.0:
55
- resolution: {integrity: sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==}
56
- engines: {node: '>= 10'}
57
-
58
- diff@8.0.3:
59
- resolution: {integrity: sha512-qejHi7bcSD4hQAZE0tNAawRK1ZtafHDmMTMkrrIGgSLl7hTnQHmKCeB45xAcbfTqK2zowkM3j3bHt/4b/ARbYQ==}
60
- engines: {node: '>=0.3.1'}
61
-
62
- dunder-proto@1.0.1:
63
- resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==}
64
- engines: {node: '>= 0.4'}
65
-
66
- es-define-property@1.0.1:
67
- resolution: {integrity: sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==}
68
- engines: {node: '>= 0.4'}
69
-
70
- es-errors@1.3.0:
71
- resolution: {integrity: sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==}
72
- engines: {node: '>= 0.4'}
73
-
74
- es-object-atoms@1.1.1:
75
- resolution: {integrity: sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==}
76
- engines: {node: '>= 0.4'}
77
-
78
- fs@0.0.1-security:
79
- resolution: {integrity: sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==}
80
-
81
- function-bind@1.1.2:
82
- resolution: {integrity: sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==}
83
-
84
- get-intrinsic@1.3.0:
85
- resolution: {integrity: sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==}
86
- engines: {node: '>= 0.4'}
87
-
88
- get-proto@1.0.1:
89
- resolution: {integrity: sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==}
90
- engines: {node: '>= 0.4'}
91
-
92
- globalyzer@0.1.0:
93
- resolution: {integrity: sha512-40oNTM9UfG6aBmuKxk/giHn5nQ8RVz/SS4Ir6zgzOv9/qC3kKZ9v4etGTcJbEl/NyVQH7FGU7d+X1egr57Md2Q==}
94
-
95
- globrex@0.1.2:
96
- resolution: {integrity: sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==}
97
-
98
- gopd@1.2.0:
99
- resolution: {integrity: sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==}
100
- engines: {node: '>= 0.4'}
101
-
102
- has-flag@4.0.0:
103
- resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==}
104
- engines: {node: '>=8'}
105
-
106
- has-symbols@1.1.0:
107
- resolution: {integrity: sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==}
108
- engines: {node: '>= 0.4'}
109
-
110
- hasown@2.0.2:
111
- resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==}
112
- engines: {node: '>= 0.4'}
113
-
114
- inherits@2.0.3:
115
- resolution: {integrity: sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==}
116
-
117
- math-intrinsics@1.1.0:
118
- resolution: {integrity: sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==}
119
- engines: {node: '>= 0.4'}
120
-
121
- node-chronicle@0.2.0:
122
- resolution: {integrity: sha512-se9NsW67UZqDFbK/+Rbml9KdCVTwzaadIgL4NDBcDpLMhOf1qerRxXicE+/dBsoyhDqWCwAFbBjR3iytF3gepA==}
123
-
124
- node-watch@0.7.3:
125
- resolution: {integrity: sha512-3l4E8uMPY1HdMMryPRUAl+oIHtXtyiTlIiESNSVSNxcPfzAFzeTbXFQkZfAwBbo0B1qMSG8nUABx+Gd+YrbKrQ==}
126
- engines: {node: '>=6'}
127
-
128
- object-inspect@1.13.4:
129
- resolution: {integrity: sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==}
130
- engines: {node: '>= 0.4'}
131
-
132
- path@0.12.7:
133
- resolution: {integrity: sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q==}
134
-
135
- process@0.11.10:
136
- resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==}
137
- engines: {node: '>= 0.6.0'}
138
-
139
- punycode@1.4.1:
140
- resolution: {integrity: sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==}
141
-
142
- qs@6.15.0:
143
- resolution: {integrity: sha512-mAZTtNCeetKMH+pSjrb76NAM8V9a05I9aBZOHztWy/UqcJdQYNsf59vrRKWnojAT9Y+GbIvoTBC++CPHqpDBhQ==}
144
- engines: {node: '>=0.6'}
145
-
146
- qunit@2.25.0:
147
- resolution: {integrity: sha512-MONPKgjavgTqArCwZOEz8nEMbA19zNXIp5ZOW9rPYj5cbgQp0fiI36c9dPTSzTRRzx+KcfB5eggYB/ENqxi0+w==}
148
- engines: {node: '>=10'}
149
- hasBin: true
150
-
151
- side-channel-list@1.0.0:
152
- resolution: {integrity: sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==}
153
- engines: {node: '>= 0.4'}
154
-
155
- side-channel-map@1.0.1:
156
- resolution: {integrity: sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==}
157
- engines: {node: '>= 0.4'}
158
-
159
- side-channel-weakmap@1.0.2:
160
- resolution: {integrity: sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==}
161
- engines: {node: '>= 0.4'}
162
-
163
- side-channel@1.1.0:
164
- resolution: {integrity: sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==}
165
- engines: {node: '>= 0.4'}
166
-
167
- sinon@21.0.3:
168
- resolution: {integrity: sha512-0x8TQFr8EjADhSME01u1ZK31yv2+bd6Z5NrBCHVM+n4qL1wFqbxftmeyi3bwlr49FbbzRfrqSFOpyHCOh/YmYA==}
169
-
170
- stonyx@0.2.3-beta.6:
171
- resolution: {integrity: sha512-gl6lYlr9vq02qvb3Gooy8AbBTYBrFBVBIp4Vwa60jnEJZ1PYTB0B4VPhYbRIvK6Nyz8YP8Q1W5a/ClU1QThoIA==}
172
- hasBin: true
173
-
174
- supports-color@7.2.0:
175
- resolution: {integrity: sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==}
176
- engines: {node: '>=8'}
177
-
178
- tiny-glob@0.2.9:
179
- resolution: {integrity: sha512-g/55ssRPUjShh+xkfx9UPDXqhckHEsHr4Vd9zX55oSdGZc/MD0m3sferOkwWtp98bv+kcVfEHtRJgBVJzelrzg==}
180
-
181
- type-detect@4.0.8:
182
- resolution: {integrity: sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==}
183
- engines: {node: '>=4'}
184
-
185
- type-detect@4.1.0:
186
- resolution: {integrity: sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==}
187
- engines: {node: '>=4'}
188
-
189
- url@0.11.4:
190
- resolution: {integrity: sha512-oCwdVC7mTuWiPyjLUz/COz5TLk6wgp0RCsN+wHZ2Ekneac9w8uuV0njcbbie2ME+Vs+d6duwmYuR3HgQXs1fOg==}
191
- engines: {node: '>= 0.4'}
192
-
193
- util@0.10.4:
194
- resolution: {integrity: sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==}
195
-
196
- ws@8.19.0:
197
- resolution: {integrity: sha512-blAT2mjOEIi0ZzruJfIhb3nps74PRWTCz1IjglWEEpQl5XS/UNama6u2/rjFkDDouqr4L67ry+1aGIALViWjDg==}
198
- engines: {node: '>=10.0.0'}
199
- peerDependencies:
200
- bufferutil: ^4.0.1
201
- utf-8-validate: '>=5.0.2'
202
- peerDependenciesMeta:
203
- bufferutil:
204
- optional: true
205
- utf-8-validate:
206
- optional: true
207
-
208
- snapshots:
209
-
210
- '@sinonjs/commons@3.0.1':
211
- dependencies:
212
- type-detect: 4.0.8
213
-
214
- '@sinonjs/fake-timers@15.1.1':
215
- dependencies:
216
- '@sinonjs/commons': 3.0.1
217
-
218
- '@sinonjs/samsam@9.0.3':
219
- dependencies:
220
- '@sinonjs/commons': 3.0.1
221
- type-detect: 4.1.0
222
-
223
- '@stonyx/utils@0.2.3-beta.5': {}
224
-
225
- call-bind-apply-helpers@1.0.2:
226
- dependencies:
227
- es-errors: 1.3.0
228
- function-bind: 1.1.2
229
-
230
- call-bound@1.0.4:
231
- dependencies:
232
- call-bind-apply-helpers: 1.0.2
233
- get-intrinsic: 1.3.0
234
-
235
- chalk@5.6.2: {}
236
-
237
- commander@7.2.0: {}
238
-
239
- diff@8.0.3: {}
240
-
241
- dunder-proto@1.0.1:
242
- dependencies:
243
- call-bind-apply-helpers: 1.0.2
244
- es-errors: 1.3.0
245
- gopd: 1.2.0
246
-
247
- es-define-property@1.0.1: {}
248
-
249
- es-errors@1.3.0: {}
250
-
251
- es-object-atoms@1.1.1:
252
- dependencies:
253
- es-errors: 1.3.0
254
-
255
- fs@0.0.1-security: {}
256
-
257
- function-bind@1.1.2: {}
258
-
259
- get-intrinsic@1.3.0:
260
- dependencies:
261
- call-bind-apply-helpers: 1.0.2
262
- es-define-property: 1.0.1
263
- es-errors: 1.3.0
264
- es-object-atoms: 1.1.1
265
- function-bind: 1.1.2
266
- get-proto: 1.0.1
267
- gopd: 1.2.0
268
- has-symbols: 1.1.0
269
- hasown: 2.0.2
270
- math-intrinsics: 1.1.0
271
-
272
- get-proto@1.0.1:
273
- dependencies:
274
- dunder-proto: 1.0.1
275
- es-object-atoms: 1.1.1
276
-
277
- globalyzer@0.1.0: {}
278
-
279
- globrex@0.1.2: {}
280
-
281
- gopd@1.2.0: {}
282
-
283
- has-flag@4.0.0: {}
284
-
285
- has-symbols@1.1.0: {}
286
-
287
- hasown@2.0.2:
288
- dependencies:
289
- function-bind: 1.1.2
290
-
291
- inherits@2.0.3: {}
292
-
293
- math-intrinsics@1.1.0: {}
294
-
295
- node-chronicle@0.2.0:
296
- dependencies:
297
- chalk: 5.6.2
298
- fs: 0.0.1-security
299
- path: 0.12.7
300
- url: 0.11.4
301
-
302
- node-watch@0.7.3: {}
303
-
304
- object-inspect@1.13.4: {}
305
-
306
- path@0.12.7:
307
- dependencies:
308
- process: 0.11.10
309
- util: 0.10.4
310
-
311
- process@0.11.10: {}
312
-
313
- punycode@1.4.1: {}
314
-
315
- qs@6.15.0:
316
- dependencies:
317
- side-channel: 1.1.0
318
-
319
- qunit@2.25.0:
320
- dependencies:
321
- commander: 7.2.0
322
- node-watch: 0.7.3
323
- tiny-glob: 0.2.9
324
-
325
- side-channel-list@1.0.0:
326
- dependencies:
327
- es-errors: 1.3.0
328
- object-inspect: 1.13.4
329
-
330
- side-channel-map@1.0.1:
331
- dependencies:
332
- call-bound: 1.0.4
333
- es-errors: 1.3.0
334
- get-intrinsic: 1.3.0
335
- object-inspect: 1.13.4
336
-
337
- side-channel-weakmap@1.0.2:
338
- dependencies:
339
- call-bound: 1.0.4
340
- es-errors: 1.3.0
341
- get-intrinsic: 1.3.0
342
- object-inspect: 1.13.4
343
- side-channel-map: 1.0.1
344
-
345
- side-channel@1.1.0:
346
- dependencies:
347
- es-errors: 1.3.0
348
- object-inspect: 1.13.4
349
- side-channel-list: 1.0.0
350
- side-channel-map: 1.0.1
351
- side-channel-weakmap: 1.0.2
352
-
353
- sinon@21.0.3:
354
- dependencies:
355
- '@sinonjs/commons': 3.0.1
356
- '@sinonjs/fake-timers': 15.1.1
357
- '@sinonjs/samsam': 9.0.3
358
- diff: 8.0.3
359
- supports-color: 7.2.0
360
-
361
- stonyx@0.2.3-beta.6:
362
- dependencies:
363
- node-chronicle: 0.2.0
364
-
365
- supports-color@7.2.0:
366
- dependencies:
367
- has-flag: 4.0.0
368
-
369
- tiny-glob@0.2.9:
370
- dependencies:
371
- globalyzer: 0.1.0
372
- globrex: 0.1.2
373
-
374
- type-detect@4.0.8: {}
375
-
376
- type-detect@4.1.0: {}
377
-
378
- url@0.11.4:
379
- dependencies:
380
- punycode: 1.4.1
381
- qs: 6.15.0
382
-
383
- util@0.10.4:
384
- dependencies:
385
- inherits: 2.0.3
386
-
387
- ws@8.19.0: {}
package/src/client.js DELETED
@@ -1,152 +0,0 @@
1
- import { WebSocket } from 'ws';
2
- import config from 'stonyx/config';
3
- import log from 'stonyx/log';
4
- import { forEachFileImport } from '@stonyx/utils/file';
5
- import { encrypt, decrypt, deriveKey } from './encryption.js';
6
-
7
- export default class SocketClient {
8
- handlers = {};
9
- reconnectCount = 0;
10
-
11
- constructor() {
12
- if (SocketClient.instance) return SocketClient.instance;
13
- SocketClient.instance = this;
14
- }
15
-
16
- async init() {
17
- await this.discoverHandlers();
18
-
19
- const { encryption, authKey } = config.sockets;
20
- this.encryptionEnabled = encryption === 'true' || encryption === true;
21
-
22
- if (this.encryptionEnabled) {
23
- this.globalKey = deriveKey(authKey);
24
- }
25
-
26
- return this.connect();
27
- }
28
-
29
- async discoverHandlers() {
30
- const { handlerDir } = config.sockets;
31
-
32
- await forEachFileImport(handlerDir, (HandlerClass, { name }) => {
33
- const instance = new HandlerClass();
34
-
35
- if (typeof instance.client === 'function') {
36
- instance._clientRef = this;
37
- this.handlers[name] = instance;
38
- }
39
- }, { ignoreAccessFailure: true });
40
- }
41
-
42
- async connect() {
43
- return new Promise((resolve, reject) => {
44
- const { address, authKey } = config.sockets;
45
- this.promise = { resolve, reject };
46
-
47
- log.socket(`Connecting to remote server: ${address}`);
48
- const socket = new WebSocket(address);
49
- this.socket = socket;
50
-
51
- socket.onmessage = this.onMessage.bind(this);
52
- socket.onclose = this.onClose.bind(this);
53
- socket.onerror = event => {
54
- console.error(event);
55
- reject('Error connecting to socket server');
56
- };
57
-
58
- socket.onopen = () => {
59
- this.reconnectCount = 0;
60
- this.send({ request: 'auth', data: { authKey } }, true);
61
- };
62
- });
63
- }
64
-
65
- onMessage({ data: payload }) {
66
- try {
67
- let parsed;
68
-
69
- if (this.encryptionEnabled) {
70
- const key = this.sessionKey || this.globalKey;
71
- const raw = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
72
- parsed = JSON.parse(decrypt(raw, key));
73
- } else {
74
- parsed = JSON.parse(payload);
75
- }
76
-
77
- const { request, response, sessionKey } = parsed;
78
-
79
- // Built-in auth session key handling + heartbeat kickoff
80
- if (request === 'auth') {
81
- if (sessionKey && this.encryptionEnabled) {
82
- this.sessionKey = Buffer.from(sessionKey, 'base64');
83
- }
84
- this.nextHeartBeat();
85
- }
86
-
87
- // Built-in heartbeat — schedule next beat on response
88
- if (request === 'heartBeat') {
89
- this.nextHeartBeat();
90
- return;
91
- }
92
-
93
- const handler = this.handlers[request];
94
-
95
- if (!handler) {
96
- console.error(`Call to invalid handler: ${request}`);
97
- return;
98
- }
99
-
100
- handler.client.call({ client: this, ...handler }, response);
101
- } catch (error) {
102
- console.error(error);
103
- log.socket(`Invalid payload received from remote server`);
104
- }
105
- }
106
-
107
- send(payload, useGlobalKey = false) {
108
- if (this.encryptionEnabled) {
109
- const key = useGlobalKey ? this.globalKey : this.sessionKey;
110
- const data = encrypt(JSON.stringify(payload), key);
111
- this.socket.send(data);
112
- } else {
113
- this.socket.send(JSON.stringify(payload));
114
- }
115
- }
116
-
117
- heartBeat() {
118
- this.send({ request: 'heartBeat' });
119
- }
120
-
121
- nextHeartBeat() {
122
- this._heartBeatTimer = setTimeout(() => this.heartBeat(), config.sockets.heartBeatInterval);
123
- }
124
-
125
- onClose() {
126
- log.socket('Disconnected from remote server');
127
- if (this._heartBeatTimer) clearTimeout(this._heartBeatTimer);
128
- }
129
-
130
- close() {
131
- if (this._heartBeatTimer) clearTimeout(this._heartBeatTimer);
132
- if (this.socket) this.socket.close();
133
- }
134
-
135
- reconnect() {
136
- if (this.reconnectCount > 5) {
137
- log.socket('Max reconnect attempts exceeded');
138
- return;
139
- }
140
-
141
- this.reconnectCount++;
142
- return this.connect();
143
- }
144
-
145
- reset() {
146
- this.close();
147
- this.handlers = {};
148
- this.sessionKey = null;
149
- this.reconnectCount = 0;
150
- SocketClient.instance = null;
151
- }
152
- }
package/src/encryption.js DELETED
@@ -1,31 +0,0 @@
1
- import crypto from 'node:crypto';
2
-
3
- const ALGORITHM = 'aes-256-gcm';
4
- const IV_LENGTH = 12;
5
- const TAG_LENGTH = 16;
6
-
7
- export function encrypt(data, key) {
8
- const iv = crypto.randomBytes(IV_LENGTH);
9
- const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
10
- const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]);
11
- const tag = cipher.getAuthTag();
12
- return Buffer.concat([iv, tag, encrypted]);
13
- }
14
-
15
- export function decrypt(buffer, key) {
16
- if (typeof buffer === 'string') buffer = Buffer.from(buffer, 'base64');
17
- const iv = buffer.subarray(0, IV_LENGTH);
18
- const tag = buffer.subarray(IV_LENGTH, IV_LENGTH + TAG_LENGTH);
19
- const encrypted = buffer.subarray(IV_LENGTH + TAG_LENGTH);
20
- const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
21
- decipher.setAuthTag(tag);
22
- return Buffer.concat([decipher.update(encrypted), decipher.final()]).toString('utf8');
23
- }
24
-
25
- export function generateSessionKey() {
26
- return crypto.randomBytes(32);
27
- }
28
-
29
- export function deriveKey(authKey) {
30
- return crypto.scryptSync(authKey, 'stonyx-sockets', 32);
31
- }
package/src/main.js DELETED
@@ -1,19 +0,0 @@
1
- export { default as SocketServer } from './server.js';
2
- export { default as SocketClient } from './client.js';
3
- export { default as Handler } from './handler.js';
4
-
5
- export default class Sockets {
6
- constructor() {
7
- if (Sockets.instance) return Sockets.instance;
8
- Sockets.instance = this;
9
- }
10
-
11
- async init() {
12
- // Handler discovery is deferred to SocketServer.init() / SocketClient.init()
13
- // This entry point satisfies Stonyx module auto-initialization
14
- }
15
-
16
- reset() {
17
- Sockets.instance = null;
18
- }
19
- }