@sogni-ai/sogni-client 4.0.0-alpha.19 → 4.0.0-alpha.20

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 (24) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.d.ts +66 -0
  3. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js +332 -0
  4. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js.map +1 -0
  5. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.d.ts +3 -9
  6. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js +104 -100
  7. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js.map +1 -1
  8. package/dist/ApiClient/WebSocketClient/index.js +2 -2
  9. package/dist/ApiClient/WebSocketClient/index.js.map +1 -1
  10. package/dist/ApiClient/index.js +1 -1
  11. package/dist/ApiClient/index.js.map +1 -1
  12. package/package.json +1 -1
  13. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.ts +426 -0
  14. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/index.ts +127 -98
  15. package/src/ApiClient/WebSocketClient/index.ts +2 -2
  16. package/src/ApiClient/index.ts +1 -1
  17. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/WSCoordinator.d.ts +0 -102
  18. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/WSCoordinator.js +0 -378
  19. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/WSCoordinator.js.map +0 -1
  20. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/types.d.ts +0 -102
  21. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/types.js +0 -3
  22. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/types.js.map +0 -1
  23. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/WSCoordinator.ts +0 -443
  24. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/types.ts +0 -107
@@ -1,443 +0,0 @@
1
- import { Logger } from '../../../lib/DefaultLogger';
2
- import getUUID from '../../../lib/getUUID';
3
- import { AuthenticatedData, SocketEventMap, SocketEventName } from '../events';
4
- import { MessageType, SocketMessageMap } from '../messages';
5
- import { Balances } from '../../../Account/types';
6
- import {
7
- ChannelMessage,
8
- Heartbeat,
9
- MessageEnvelope,
10
- SocketEventReceived,
11
- SendSocketMessage
12
- } from './types';
13
-
14
- interface StartingEvents {
15
- authenticated?: AuthenticatedData;
16
- balanceUpdate?: Balances;
17
- swarmModels?: Record<string, number>;
18
- }
19
-
20
- interface WSCoordinatorCallbacks {
21
- /**
22
- * Invoked when authentication state changes (authenticated/unauthenticated).
23
- * @param isAuthenticated - true if client is authenticated, false if not.
24
- */
25
- onAuthChanged: (isAuthenticated: boolean) => void;
26
- /**
27
- * Invoked when role changes (primary/secondary).
28
- * @param isPrimary - true if client is primary, false if secondary.
29
- */
30
- onRoleChange: (isPrimary: boolean) => void;
31
- /**
32
- * Invoked when connection state must change (connected/disconnected).
33
- * @param isConnected
34
- */
35
- onConnectionToggle: (isConnected: boolean) => void;
36
- /**
37
- * Invoked when secondary client receives a socket event from primary.
38
- * @param message
39
- */
40
- onMessageFromPrimary: (message: SocketEventReceived | Heartbeat) => void;
41
- /**
42
- * Invoked when primary client receives a socket send request from secondary.
43
- * @param message
44
- */
45
- onSendRequest: (message: SendSocketMessage) => Promise<void>;
46
- }
47
-
48
- class WSCoordinator {
49
- private static readonly HEARTBEAT_INTERVAL = 2000;
50
- private static readonly PRIMARY_TIMEOUT = 5000;
51
- private static readonly CHANNEL_NAME = 'sogni-websocket-clients';
52
- private static readonly ACK_TIMEOUT = 5000;
53
-
54
- private id: string;
55
- private callbacks: WSCoordinatorCallbacks;
56
- private channel: BroadcastChannel;
57
- private _isPrimary: boolean;
58
- private _isConnected = false;
59
- private lastPrimaryHeartbeat: number = 0;
60
- private logger: Logger;
61
- private heartbeatInterval: NodeJS.Timeout | null = null;
62
- private primaryCheckInterval: NodeJS.Timeout | null = null;
63
- private startingEvents: StartingEvents = {};
64
- private ackCallbacks: Record<string, (error?: any) => void> = {};
65
- private initialized = false;
66
-
67
- constructor(callbacks: WSCoordinatorCallbacks, logger: Logger) {
68
- this.id = getUUID();
69
- this.logger = logger;
70
- this.callbacks = callbacks;
71
- this.channel = new BroadcastChannel(WSCoordinator.CHANNEL_NAME);
72
- this.channel.onmessage = this.handleMessage.bind(this);
73
- this._isPrimary = false;
74
-
75
- // Listen for tab closing to gracefully release primary role
76
- if (typeof window !== 'undefined') {
77
- window.addEventListener('beforeunload', this.handleBeforeUnload.bind(this));
78
- }
79
- }
80
-
81
- /**
82
- * Initialize tab coordination and determine role
83
- */
84
- async initialize(): Promise<boolean> {
85
- if (this.initialized) {
86
- return this._isPrimary;
87
- }
88
- this.initialized = true;
89
- this.logger.info(`WSCoordinator ${this.id} initializing...`);
90
- // Announce our presence
91
- this.broadcast({
92
- type: 'announce'
93
- });
94
-
95
- // Wait to see if there's an existing primary
96
- await this.waitForPrimaryResponse();
97
- if (!this._isPrimary) {
98
- this.logger.info(`Client ${this.id} is secondary, primary exists`);
99
- this.startPrimaryCheck();
100
- } else {
101
- this.logger.info(`Client ${this.id} becoming primary`);
102
- this.stopPrimaryCheck();
103
- this.becomePrimary();
104
- }
105
-
106
- return this._isPrimary;
107
- }
108
-
109
- connect() {
110
- if (this._isPrimary) {
111
- throw new Error('Primary should connect the socket directly.');
112
- }
113
- this.broadcast({
114
- type: 'connection-toggle',
115
- payload: { connected: true }
116
- });
117
- }
118
-
119
- disconnect() {
120
- if (this._isPrimary) {
121
- throw new Error('Primary should disconnect socket directly.');
122
- }
123
- this.broadcast({
124
- type: 'connection-toggle',
125
- payload: { connected: false }
126
- });
127
- }
128
-
129
- async changeAuthState(isAuthenticated: boolean) {
130
- this.broadcast({
131
- type: 'authentication',
132
- payload: { authenticated: isAuthenticated }
133
- });
134
- }
135
-
136
- /**
137
- * Wait briefly to see if a primary tab responds
138
- */
139
- private waitForPrimaryResponse(): Promise<void> {
140
- return new Promise((resolve) => {
141
- const timeout = setTimeout(() => {
142
- // No primary responded, we become primary
143
- this._isPrimary = true;
144
- resolve();
145
- }, 500);
146
-
147
- const messageHandler = (e: MessageEvent<MessageEnvelope>) => {
148
- const envelope = e.data;
149
- const message = envelope.payload;
150
- if (message.type === 'primary-present' || message.type === 'primary-claim') {
151
- // Primary exists
152
- this._isPrimary = false;
153
- this.lastPrimaryHeartbeat = envelope.timestamp;
154
- clearTimeout(timeout);
155
- this.channel.removeEventListener('message', messageHandler);
156
- resolve();
157
- }
158
- };
159
-
160
- this.channel.addEventListener('message', messageHandler);
161
- });
162
- }
163
-
164
- /**
165
- * Become the primary tab
166
- */
167
- private becomePrimary() {
168
- this._isPrimary = true;
169
- this.callbacks.onRoleChange(true);
170
-
171
- // Broadcast that we're claiming primary role
172
- this.broadcast({
173
- type: 'primary-claim'
174
- });
175
-
176
- // Start sending heartbeats
177
- this.startHeartbeat();
178
- }
179
-
180
- /**
181
- * Release primary role (when closing)
182
- */
183
- private releasePrimary() {
184
- if (this._isPrimary) {
185
- this.broadcast({
186
- type: 'primary-release'
187
- });
188
- this.stopHeartbeat();
189
- }
190
- }
191
-
192
- /**
193
- * Start sending heartbeat messages as primary
194
- */
195
- private startHeartbeat() {
196
- if (this.heartbeatInterval) {
197
- throw new Error('Heartbeat interval already started. This should never happen.');
198
- }
199
- this.heartbeatInterval = setInterval(() => {
200
- this.broadcast({
201
- type: 'primary-present',
202
- payload: { connected: this._isConnected }
203
- });
204
- }, WSCoordinator.HEARTBEAT_INTERVAL);
205
- }
206
-
207
- /**
208
- * Stop sending heartbeat messages
209
- */
210
- private stopHeartbeat() {
211
- if (this.heartbeatInterval) {
212
- clearInterval(this.heartbeatInterval);
213
- this.heartbeatInterval = null;
214
- }
215
- }
216
-
217
- /**
218
- * Start checking for primary heartbeat (as secondary)
219
- */
220
- private startPrimaryCheck() {
221
- this.primaryCheckInterval = setInterval(() => {
222
- const timeSinceLastHeartbeat = Date.now() - this.lastPrimaryHeartbeat;
223
- if (timeSinceLastHeartbeat > WSCoordinator.PRIMARY_TIMEOUT) {
224
- this.logger.warn(`Primary tab timeout, becoming primary`);
225
- this.stopPrimaryCheck();
226
- this.becomePrimary();
227
- }
228
- }, 1000);
229
- }
230
-
231
- /**
232
- * Stop checking for primary heartbeat
233
- */
234
- private stopPrimaryCheck() {
235
- if (this.primaryCheckInterval) {
236
- clearInterval(this.primaryCheckInterval);
237
- this.primaryCheckInterval = null;
238
- }
239
- }
240
-
241
- private handleMessage(e: MessageEvent<MessageEnvelope>) {
242
- const envelope = e.data;
243
- const message = envelope.payload;
244
- // If a message sent to specific recipient and not to us, ignore it
245
- if (!!envelope.recipientId && envelope.recipientId !== this.id) {
246
- return;
247
- }
248
- switch (message.type) {
249
- case 'announce':
250
- if (this._isPrimary) {
251
- this.broadcast({
252
- type: 'primary-present',
253
- payload: { connected: this._isConnected }
254
- });
255
- // Re-broadcast starting events for the new tab
256
- Object.entries(this.startingEvents).forEach(([eventType, payload]) => {
257
- this.broadcast(
258
- {
259
- type: 'socket-event',
260
- payload: { eventType: eventType as SocketEventName, payload }
261
- },
262
- envelope.senderId
263
- );
264
- });
265
- }
266
- return;
267
- case 'primary-claim':
268
- case 'primary-present':
269
- this.lastPrimaryHeartbeat = envelope.timestamp;
270
- if (this._isPrimary) {
271
- this.logger.info(`Stepping down from primary, ${envelope.senderId} claimed it`);
272
- this.stopHeartbeat();
273
- this._isPrimary = false;
274
- this.callbacks.onRoleChange(false);
275
- this.startPrimaryCheck();
276
- }
277
- return;
278
- case 'primary-release':
279
- if (!this._isPrimary) {
280
- // Wait random time (0 - 300ms) before claiming "primary" role, so not all tabs try to claim at the same time
281
- setTimeout(
282
- () => {
283
- const timeSinceRelease = Date.now() - envelope.timestamp;
284
- const timeSinceLastHeartbeat = Date.now() - this.lastPrimaryHeartbeat;
285
- if (timeSinceLastHeartbeat > timeSinceRelease) {
286
- this.logger.info(`Primary released, becoming primary`);
287
- this.stopPrimaryCheck();
288
- this.becomePrimary();
289
- } else {
290
- this.logger.info(`Another primary exists, do nothing`);
291
- }
292
- },
293
- Math.round(Math.random() * 300)
294
- );
295
- }
296
- return;
297
- case 'authentication':
298
- this.callbacks.onAuthChanged(message.payload.authenticated);
299
- return;
300
- case 'connection-toggle':
301
- if (this._isPrimary) {
302
- this.logger.info(
303
- `Should ${message.payload.connected ? 'connect' : 'disconnect'} socket.`
304
- );
305
- this.callbacks.onConnectionToggle(message.payload.connected);
306
- }
307
- return;
308
- case 'socket-event': {
309
- if (!this._isPrimary) {
310
- this.callbacks.onMessageFromPrimary(message);
311
- }
312
- return;
313
- }
314
- case 'socket-send': {
315
- if (this._isPrimary) {
316
- this.callbacks
317
- .onSendRequest(message)
318
- .then(() => {
319
- //Acknowledge the request
320
- this.broadcast({
321
- type: 'socket-ack',
322
- payload: {
323
- envelopeId: envelope.id
324
- }
325
- });
326
- })
327
- .catch((e) => {
328
- this.logger.error(`Error sending socket message: ${e}`);
329
- this.broadcast({
330
- type: 'socket-ack',
331
- payload: {
332
- envelopeId: envelope.id,
333
- error: e.message
334
- }
335
- });
336
- });
337
- }
338
- return;
339
- }
340
- case 'socket-ack': {
341
- if (!this._isPrimary) {
342
- if (this.ackCallbacks[message.payload.envelopeId]) {
343
- this.ackCallbacks[message.payload.envelopeId](message.payload.error);
344
- }
345
- }
346
- }
347
- }
348
- }
349
-
350
- private broadcast(message: ChannelMessage, recipientId?: string): string {
351
- const envelope: MessageEnvelope = {
352
- id: getUUID(),
353
- senderId: this.id,
354
- timestamp: Date.now(),
355
- payload: message
356
- };
357
- if (recipientId) {
358
- envelope.recipientId = recipientId;
359
- }
360
- this.channel.postMessage(envelope);
361
- return envelope.id;
362
- }
363
-
364
- /**
365
- * Send a message to be transmitted over the socket (from secondary to primary)
366
- */
367
- sendToSocket<T extends MessageType = MessageType>(messageType: T, data: SocketMessageMap[T]) {
368
- if (this._isPrimary) {
369
- throw new Error('Primary tab should send directly');
370
- }
371
- this.logger.debug(`Sending socket message ${messageType}`, data);
372
- const messageId = this.broadcast({
373
- type: 'socket-send',
374
- payload: { messageType, data }
375
- });
376
- return new Promise<void>((resolve, reject) => {
377
- const ackTimeout = setTimeout(() => {
378
- //If callback is not called within 5 seconds, call it with an error
379
- if (this.ackCallbacks[messageId]) {
380
- this.ackCallbacks[messageId](new Error('Message delivery timeout'));
381
- }
382
- }, WSCoordinator.ACK_TIMEOUT);
383
- this.ackCallbacks[messageId] = (error?: any) => {
384
- delete this.ackCallbacks[messageId];
385
- clearTimeout(ackTimeout);
386
- if (error) {
387
- reject(error);
388
- } else {
389
- resolve();
390
- }
391
- };
392
- });
393
- }
394
-
395
- /**
396
- * Broadcast a socket event from primary to all secondaries
397
- */
398
- broadcastSocketEvent<E extends SocketEventName = SocketEventName>(
399
- eventType: E,
400
- payload: SocketEventMap[E]
401
- ) {
402
- if (!this._isPrimary) {
403
- throw new Error('Only primary tab can broadcast socket events');
404
- }
405
- if (eventType === 'connected') {
406
- this._isConnected = true;
407
- } else if (eventType === 'disconnected') {
408
- this._isConnected = false;
409
- }
410
- this.updateStartingState(eventType, payload);
411
- this.logger.debug(`Broadcasting socket event ${eventType}`, payload);
412
- this.broadcast({
413
- type: 'socket-event',
414
- payload: { eventType, payload }
415
- });
416
- }
417
-
418
- private updateStartingState<E extends SocketEventName>(eventType: E, payload: SocketEventMap[E]) {
419
- if (eventType === 'authenticated') {
420
- this.startingEvents.authenticated = payload as AuthenticatedData;
421
- } else if (eventType === 'balanceUpdate') {
422
- this.startingEvents.balanceUpdate = payload as Balances;
423
- } else if (eventType === 'swarmModels') {
424
- this.startingEvents.swarmModels = payload as Record<string, number>;
425
- }
426
- }
427
-
428
- isPrimary() {
429
- return this._isPrimary;
430
- }
431
-
432
- /**
433
- * Handle tab closing event
434
- */
435
- private handleBeforeUnload = () => {
436
- if (this._isPrimary) {
437
- this.logger.info(`Client ${this.id} closing, releasing primary role`);
438
- this.releasePrimary();
439
- }
440
- };
441
- }
442
-
443
- export default WSCoordinator;
@@ -1,107 +0,0 @@
1
- import { SocketEventMap, SocketEventName } from '../events';
2
- import { MessageType, SocketMessageMap } from '../messages';
3
-
4
- /**
5
- * Primary tab to broadcast socket events to secondary tabs.
6
- * @param eventType - The event type.
7
- * @param payload - The event payload. See {@link SocketEventMap} for the list of available events.
8
- */
9
- export interface SocketEventReceived<T extends SocketEventName = SocketEventName> {
10
- type: 'socket-event';
11
- payload: { eventType: T; payload: SocketEventMap[T] };
12
- }
13
-
14
- /**
15
- * Sent by secondary tabs to the primary tab to send a message to WebSocket.
16
- * @param messageType - The message type.
17
- * @param data - The message payload. See {@link SocketMessageMap} for the list of available messages.
18
- */
19
- export interface SendSocketMessage<T extends MessageType = MessageType> {
20
- type: 'socket-send';
21
- payload: { messageType: T; data: SocketMessageMap[T] };
22
- }
23
-
24
- /**
25
- * Sent by the primary tab to acknowledge that a message was sent to WebSocket.
26
- */
27
- export interface SocketMessageAck {
28
- type: 'socket-ack';
29
- payload: { envelopeId: string; error?: any };
30
- }
31
-
32
- /**
33
- * Sent by the primary tab to notify the secondary tabs that the primary tab is still alive.
34
- * @param connected - true if the primary tab is connected to the server, false otherwise.
35
- */
36
- export interface Heartbeat {
37
- type: 'primary-present';
38
- payload: { connected: boolean };
39
- }
40
-
41
- /**
42
- * Used to tell the primary tab to connect/disconnect socket.
43
- * @param connected - true to connect, false to disconnect.
44
- */
45
- export interface ConnectionToggle {
46
- type: 'connection-toggle';
47
- payload: { connected: boolean };
48
- }
49
-
50
- /**
51
- * Sent by tab when it is opened to notify other tabs. If another tab is present,
52
- * it will respond with {@link Heartbeat}.
53
- */
54
- export interface ClientAnnounce {
55
- type: 'announce';
56
- }
57
-
58
- /**
59
- * Sent by the client to let other know that it is claiming the primary role.
60
- */
61
- export interface PrimaryClaim {
62
- type: 'primary-claim';
63
- }
64
-
65
- /**
66
- * Sent by the client to let other know that it is releasing the primary role.
67
- * This usually happens when the tab is closed.
68
- */
69
- export interface PrimaryRelease {
70
- type: 'primary-release';
71
- }
72
-
73
- /**
74
- * Sent by the tab where user has auth state changed.
75
- * @param authenticated - true if the user is authenticated, false otherwise.
76
- */
77
- export interface AuthenticationChange {
78
- type: 'authentication';
79
- payload: { authenticated: boolean };
80
- }
81
-
82
- export type ChannelMessage =
83
- | AuthenticationChange
84
- | SocketEventReceived
85
- | SendSocketMessage
86
- | SocketMessageAck
87
- | ConnectionToggle
88
- | Heartbeat
89
- | ClientAnnounce
90
- | PrimaryClaim
91
- | PrimaryRelease;
92
-
93
- /**
94
- * Envelope for messages sent between tabs.
95
- * @param id - Unique message ID.
96
- * @param senderId - ID of the tab that sent the message.
97
- * @param recipientId - ID of the tab that should receive the message. If not specified, the message will be broadcasted to all tabs.
98
- * @param timestamp - Timestamp of the message.
99
- * @param payload - Message payload.
100
- */
101
- export interface MessageEnvelope {
102
- id: string;
103
- senderId: string;
104
- recipientId?: string;
105
- timestamp: number;
106
- payload: ChannelMessage;
107
- }