@supabase/realtime-js 2.71.2-canary.28 → 2.71.2-canary.3

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 (89) hide show
  1. package/package.json +1 -1
  2. package/dist/main/RealtimeChannel.d.ts +0 -235
  3. package/dist/main/RealtimeChannel.d.ts.map +0 -1
  4. package/dist/main/RealtimeChannel.js +0 -576
  5. package/dist/main/RealtimeChannel.js.map +0 -1
  6. package/dist/main/RealtimeClient.d.ts +0 -196
  7. package/dist/main/RealtimeClient.d.ts.map +0 -1
  8. package/dist/main/RealtimeClient.js +0 -736
  9. package/dist/main/RealtimeClient.js.map +0 -1
  10. package/dist/main/RealtimePresence.d.ts +0 -68
  11. package/dist/main/RealtimePresence.d.ts.map +0 -1
  12. package/dist/main/RealtimePresence.js +0 -229
  13. package/dist/main/RealtimePresence.js.map +0 -1
  14. package/dist/main/index.d.ts +0 -6
  15. package/dist/main/index.d.ts.map +0 -1
  16. package/dist/main/index.js +0 -53
  17. package/dist/main/index.js.map +0 -1
  18. package/dist/main/lib/constants.d.ts +0 -37
  19. package/dist/main/lib/constants.d.ts.map +0 -1
  20. package/dist/main/lib/constants.js +0 -46
  21. package/dist/main/lib/constants.js.map +0 -1
  22. package/dist/main/lib/push.d.ts +0 -48
  23. package/dist/main/lib/push.d.ts.map +0 -1
  24. package/dist/main/lib/push.js +0 -102
  25. package/dist/main/lib/push.js.map +0 -1
  26. package/dist/main/lib/serializer.d.ts +0 -7
  27. package/dist/main/lib/serializer.d.ts.map +0 -1
  28. package/dist/main/lib/serializer.js +0 -36
  29. package/dist/main/lib/serializer.js.map +0 -1
  30. package/dist/main/lib/timer.d.ts +0 -22
  31. package/dist/main/lib/timer.d.ts.map +0 -1
  32. package/dist/main/lib/timer.js +0 -39
  33. package/dist/main/lib/timer.js.map +0 -1
  34. package/dist/main/lib/transformers.d.ts +0 -109
  35. package/dist/main/lib/transformers.d.ts.map +0 -1
  36. package/dist/main/lib/transformers.js +0 -229
  37. package/dist/main/lib/transformers.js.map +0 -1
  38. package/dist/main/lib/version.d.ts +0 -2
  39. package/dist/main/lib/version.d.ts.map +0 -1
  40. package/dist/main/lib/version.js +0 -5
  41. package/dist/main/lib/version.js.map +0 -1
  42. package/dist/main/lib/websocket-factory.d.ts +0 -35
  43. package/dist/main/lib/websocket-factory.d.ts.map +0 -1
  44. package/dist/main/lib/websocket-factory.js +0 -90
  45. package/dist/main/lib/websocket-factory.js.map +0 -1
  46. package/dist/module/RealtimeChannel.d.ts +0 -235
  47. package/dist/module/RealtimeChannel.d.ts.map +0 -1
  48. package/dist/module/RealtimeChannel.js +0 -536
  49. package/dist/module/RealtimeChannel.js.map +0 -1
  50. package/dist/module/RealtimeClient.d.ts +0 -196
  51. package/dist/module/RealtimeClient.d.ts.map +0 -1
  52. package/dist/module/RealtimeClient.js +0 -698
  53. package/dist/module/RealtimeClient.js.map +0 -1
  54. package/dist/module/RealtimePresence.d.ts +0 -68
  55. package/dist/module/RealtimePresence.d.ts.map +0 -1
  56. package/dist/module/RealtimePresence.js +0 -225
  57. package/dist/module/RealtimePresence.js.map +0 -1
  58. package/dist/module/index.d.ts +0 -6
  59. package/dist/module/index.d.ts.map +0 -1
  60. package/dist/module/index.js +0 -6
  61. package/dist/module/index.js.map +0 -1
  62. package/dist/module/lib/constants.d.ts +0 -37
  63. package/dist/module/lib/constants.d.ts.map +0 -1
  64. package/dist/module/lib/constants.js +0 -43
  65. package/dist/module/lib/constants.js.map +0 -1
  66. package/dist/module/lib/push.d.ts +0 -48
  67. package/dist/module/lib/push.d.ts.map +0 -1
  68. package/dist/module/lib/push.js +0 -99
  69. package/dist/module/lib/push.js.map +0 -1
  70. package/dist/module/lib/serializer.d.ts +0 -7
  71. package/dist/module/lib/serializer.d.ts.map +0 -1
  72. package/dist/module/lib/serializer.js +0 -33
  73. package/dist/module/lib/serializer.js.map +0 -1
  74. package/dist/module/lib/timer.d.ts +0 -22
  75. package/dist/module/lib/timer.d.ts.map +0 -1
  76. package/dist/module/lib/timer.js +0 -36
  77. package/dist/module/lib/timer.js.map +0 -1
  78. package/dist/module/lib/transformers.d.ts +0 -109
  79. package/dist/module/lib/transformers.d.ts.map +0 -1
  80. package/dist/module/lib/transformers.js +0 -217
  81. package/dist/module/lib/transformers.js.map +0 -1
  82. package/dist/module/lib/version.d.ts +0 -2
  83. package/dist/module/lib/version.d.ts.map +0 -1
  84. package/dist/module/lib/version.js +0 -2
  85. package/dist/module/lib/version.js.map +0 -1
  86. package/dist/module/lib/websocket-factory.d.ts +0 -35
  87. package/dist/module/lib/websocket-factory.d.ts.map +0 -1
  88. package/dist/module/lib/websocket-factory.js +0 -86
  89. package/dist/module/lib/websocket-factory.js.map +0 -1
@@ -1,698 +0,0 @@
1
- import WebSocketFactory from './lib/websocket-factory';
2
- import { CHANNEL_EVENTS, CONNECTION_STATE, DEFAULT_VERSION, DEFAULT_TIMEOUT, SOCKET_STATES, TRANSPORTS, VSN, WS_CLOSE_NORMAL, } from './lib/constants';
3
- import Serializer from './lib/serializer';
4
- import Timer from './lib/timer';
5
- import { httpEndpointURL } from './lib/transformers';
6
- import RealtimeChannel from './RealtimeChannel';
7
- const noop = () => { };
8
- // Connection-related constants
9
- const CONNECTION_TIMEOUTS = {
10
- HEARTBEAT_INTERVAL: 25000,
11
- RECONNECT_DELAY: 10,
12
- HEARTBEAT_TIMEOUT_FALLBACK: 100,
13
- };
14
- const RECONNECT_INTERVALS = [1000, 2000, 5000, 10000];
15
- const DEFAULT_RECONNECT_FALLBACK = 10000;
16
- const WORKER_SCRIPT = `
17
- addEventListener("message", (e) => {
18
- if (e.data.event === "start") {
19
- setInterval(() => postMessage({ event: "keepAlive" }), e.data.interval);
20
- }
21
- });`;
22
- export default class RealtimeClient {
23
- /**
24
- * Initializes the Socket.
25
- *
26
- * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
27
- * @param httpEndpoint The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol)
28
- * @param options.transport The Websocket Transport, for example WebSocket. This can be a custom implementation
29
- * @param options.timeout The default timeout in milliseconds to trigger push timeouts.
30
- * @param options.params The optional params to pass when connecting.
31
- * @param options.headers Deprecated: headers cannot be set on websocket connections and this option will be removed in the future.
32
- * @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
33
- * @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
34
- * @param options.logLevel Sets the log level for Realtime
35
- * @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
36
- * @param options.decode The function to decode incoming messages. Defaults to Serializer's decode.
37
- * @param options.reconnectAfterMs he optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
38
- * @param options.worker Use Web Worker to set a side flow. Defaults to false.
39
- * @param options.workerUrl The URL of the worker script. Defaults to https://realtime.supabase.com/worker.js that includes a heartbeat event call to keep the connection alive.
40
- */
41
- constructor(endPoint, options) {
42
- var _a;
43
- this.accessTokenValue = null;
44
- this.apiKey = null;
45
- this.channels = new Array();
46
- this.endPoint = '';
47
- this.httpEndpoint = '';
48
- /** @deprecated headers cannot be set on websocket connections */
49
- this.headers = {};
50
- this.params = {};
51
- this.timeout = DEFAULT_TIMEOUT;
52
- this.transport = null;
53
- this.heartbeatIntervalMs = CONNECTION_TIMEOUTS.HEARTBEAT_INTERVAL;
54
- this.heartbeatTimer = undefined;
55
- this.pendingHeartbeatRef = null;
56
- this.heartbeatCallback = noop;
57
- this.ref = 0;
58
- this.reconnectTimer = null;
59
- this.logger = noop;
60
- this.conn = null;
61
- this.sendBuffer = [];
62
- this.serializer = new Serializer();
63
- this.stateChangeCallbacks = {
64
- open: [],
65
- close: [],
66
- error: [],
67
- message: [],
68
- };
69
- this.accessToken = null;
70
- this._connectionState = 'disconnected';
71
- this._wasManualDisconnect = false;
72
- this._authPromise = null;
73
- /**
74
- * Use either custom fetch, if provided, or default fetch to make HTTP requests
75
- *
76
- * @internal
77
- */
78
- this._resolveFetch = (customFetch) => {
79
- let _fetch;
80
- if (customFetch) {
81
- _fetch = customFetch;
82
- }
83
- else if (typeof fetch === 'undefined') {
84
- // Node.js environment without native fetch
85
- _fetch = (...args) => import('@supabase/node-fetch')
86
- .then(({ default: fetch }) => fetch(...args))
87
- .catch((error) => {
88
- throw new Error(`Failed to load @supabase/node-fetch: ${error.message}. ` +
89
- `This is required for HTTP requests in Node.js environments without native fetch.`);
90
- });
91
- }
92
- else {
93
- _fetch = fetch;
94
- }
95
- return (...args) => _fetch(...args);
96
- };
97
- // Validate required parameters
98
- if (!((_a = options === null || options === void 0 ? void 0 : options.params) === null || _a === void 0 ? void 0 : _a.apikey)) {
99
- throw new Error('API key is required to connect to Realtime');
100
- }
101
- this.apiKey = options.params.apikey;
102
- // Initialize endpoint URLs
103
- this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`;
104
- this.httpEndpoint = httpEndpointURL(endPoint);
105
- this._initializeOptions(options);
106
- this._setupReconnectionTimer();
107
- this.fetch = this._resolveFetch(options === null || options === void 0 ? void 0 : options.fetch);
108
- }
109
- /**
110
- * Connects the socket, unless already connected.
111
- */
112
- connect() {
113
- // Skip if already connecting, disconnecting, or connected
114
- if (this.isConnecting() ||
115
- this.isDisconnecting() ||
116
- (this.conn !== null && this.isConnected())) {
117
- return;
118
- }
119
- this._setConnectionState('connecting');
120
- this._setAuthSafely('connect');
121
- // Establish WebSocket connection
122
- if (this.transport) {
123
- // Use custom transport if provided
124
- this.conn = new this.transport(this.endpointURL());
125
- }
126
- else {
127
- // Try to use native WebSocket
128
- try {
129
- this.conn = WebSocketFactory.createWebSocket(this.endpointURL());
130
- }
131
- catch (error) {
132
- this._setConnectionState('disconnected');
133
- const errorMessage = error.message;
134
- // Provide helpful error message based on environment
135
- if (errorMessage.includes('Node.js')) {
136
- throw new Error(`${errorMessage}\n\n` +
137
- 'To use Realtime in Node.js, you need to provide a WebSocket implementation:\n\n' +
138
- 'Option 1: Use Node.js 22+ which has native WebSocket support\n' +
139
- 'Option 2: Install and provide the "ws" package:\n\n' +
140
- ' npm install ws\n\n' +
141
- ' import ws from "ws"\n' +
142
- ' const client = new RealtimeClient(url, {\n' +
143
- ' ...options,\n' +
144
- ' transport: ws\n' +
145
- ' })');
146
- }
147
- throw new Error(`WebSocket not available: ${errorMessage}`);
148
- }
149
- }
150
- this._setupConnectionHandlers();
151
- }
152
- /**
153
- * Returns the URL of the websocket.
154
- * @returns string The URL of the websocket.
155
- */
156
- endpointURL() {
157
- return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: VSN }));
158
- }
159
- /**
160
- * Disconnects the socket.
161
- *
162
- * @param code A numeric status code to send on disconnect.
163
- * @param reason A custom reason for the disconnect.
164
- */
165
- disconnect(code, reason) {
166
- if (this.isDisconnecting()) {
167
- return;
168
- }
169
- this._setConnectionState('disconnecting', true);
170
- if (this.conn) {
171
- // Setup fallback timer to prevent hanging in disconnecting state
172
- const fallbackTimer = setTimeout(() => {
173
- this._setConnectionState('disconnected');
174
- }, 100);
175
- this.conn.onclose = () => {
176
- clearTimeout(fallbackTimer);
177
- this._setConnectionState('disconnected');
178
- };
179
- // Close the WebSocket connection
180
- if (code) {
181
- this.conn.close(code, reason !== null && reason !== void 0 ? reason : '');
182
- }
183
- else {
184
- this.conn.close();
185
- }
186
- this._teardownConnection();
187
- }
188
- else {
189
- this._setConnectionState('disconnected');
190
- }
191
- }
192
- /**
193
- * Returns all created channels
194
- */
195
- getChannels() {
196
- return this.channels;
197
- }
198
- /**
199
- * Unsubscribes and removes a single channel
200
- * @param channel A RealtimeChannel instance
201
- */
202
- async removeChannel(channel) {
203
- const status = await channel.unsubscribe();
204
- if (this.channels.length === 0) {
205
- this.disconnect();
206
- }
207
- return status;
208
- }
209
- /**
210
- * Unsubscribes and removes all channels
211
- */
212
- async removeAllChannels() {
213
- const values_1 = await Promise.all(this.channels.map((channel) => channel.unsubscribe()));
214
- this.channels = [];
215
- this.disconnect();
216
- return values_1;
217
- }
218
- /**
219
- * Logs the message.
220
- *
221
- * For customized logging, `this.logger` can be overridden.
222
- */
223
- log(kind, msg, data) {
224
- this.logger(kind, msg, data);
225
- }
226
- /**
227
- * Returns the current state of the socket.
228
- */
229
- connectionState() {
230
- switch (this.conn && this.conn.readyState) {
231
- case SOCKET_STATES.connecting:
232
- return CONNECTION_STATE.Connecting;
233
- case SOCKET_STATES.open:
234
- return CONNECTION_STATE.Open;
235
- case SOCKET_STATES.closing:
236
- return CONNECTION_STATE.Closing;
237
- default:
238
- return CONNECTION_STATE.Closed;
239
- }
240
- }
241
- /**
242
- * Returns `true` is the connection is open.
243
- */
244
- isConnected() {
245
- return this.connectionState() === CONNECTION_STATE.Open;
246
- }
247
- /**
248
- * Returns `true` if the connection is currently connecting.
249
- */
250
- isConnecting() {
251
- return this._connectionState === 'connecting';
252
- }
253
- /**
254
- * Returns `true` if the connection is currently disconnecting.
255
- */
256
- isDisconnecting() {
257
- return this._connectionState === 'disconnecting';
258
- }
259
- channel(topic, params = { config: {} }) {
260
- const realtimeTopic = `realtime:${topic}`;
261
- const exists = this.getChannels().find((c) => c.topic === realtimeTopic);
262
- if (!exists) {
263
- const chan = new RealtimeChannel(`realtime:${topic}`, params, this);
264
- this.channels.push(chan);
265
- return chan;
266
- }
267
- else {
268
- return exists;
269
- }
270
- }
271
- /**
272
- * Push out a message if the socket is connected.
273
- *
274
- * If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established.
275
- */
276
- push(data) {
277
- const { topic, event, payload, ref } = data;
278
- const callback = () => {
279
- this.encode(data, (result) => {
280
- var _a;
281
- (_a = this.conn) === null || _a === void 0 ? void 0 : _a.send(result);
282
- });
283
- };
284
- this.log('push', `${topic} ${event} (${ref})`, payload);
285
- if (this.isConnected()) {
286
- callback();
287
- }
288
- else {
289
- this.sendBuffer.push(callback);
290
- }
291
- }
292
- /**
293
- * Sets the JWT access token used for channel subscription authorization and Realtime RLS.
294
- *
295
- * If param is null it will use the `accessToken` callback function or the token set on the client.
296
- *
297
- * On callback used, it will set the value of the token internal to the client.
298
- *
299
- * @param token A JWT string to override the token set on the client.
300
- */
301
- async setAuth(token = null) {
302
- this._authPromise = this._performAuth(token);
303
- try {
304
- await this._authPromise;
305
- }
306
- finally {
307
- this._authPromise = null;
308
- }
309
- }
310
- /**
311
- * Sends a heartbeat message if the socket is connected.
312
- */
313
- async sendHeartbeat() {
314
- var _a;
315
- if (!this.isConnected()) {
316
- this.heartbeatCallback('disconnected');
317
- return;
318
- }
319
- // Handle heartbeat timeout and force reconnection if needed
320
- if (this.pendingHeartbeatRef) {
321
- this.pendingHeartbeatRef = null;
322
- this.log('transport', 'heartbeat timeout. Attempting to re-establish connection');
323
- this.heartbeatCallback('timeout');
324
- // Force reconnection after heartbeat timeout
325
- this._wasManualDisconnect = false;
326
- (_a = this.conn) === null || _a === void 0 ? void 0 : _a.close(WS_CLOSE_NORMAL, 'heartbeat timeout');
327
- setTimeout(() => {
328
- var _a;
329
- if (!this.isConnected()) {
330
- (_a = this.reconnectTimer) === null || _a === void 0 ? void 0 : _a.scheduleTimeout();
331
- }
332
- }, CONNECTION_TIMEOUTS.HEARTBEAT_TIMEOUT_FALLBACK);
333
- return;
334
- }
335
- // Send heartbeat message to server
336
- this.pendingHeartbeatRef = this._makeRef();
337
- this.push({
338
- topic: 'phoenix',
339
- event: 'heartbeat',
340
- payload: {},
341
- ref: this.pendingHeartbeatRef,
342
- });
343
- this.heartbeatCallback('sent');
344
- this._setAuthSafely('heartbeat');
345
- }
346
- onHeartbeat(callback) {
347
- this.heartbeatCallback = callback;
348
- }
349
- /**
350
- * Flushes send buffer
351
- */
352
- flushSendBuffer() {
353
- if (this.isConnected() && this.sendBuffer.length > 0) {
354
- this.sendBuffer.forEach((callback) => callback());
355
- this.sendBuffer = [];
356
- }
357
- }
358
- /**
359
- * Return the next message ref, accounting for overflows
360
- *
361
- * @internal
362
- */
363
- _makeRef() {
364
- let newRef = this.ref + 1;
365
- if (newRef === this.ref) {
366
- this.ref = 0;
367
- }
368
- else {
369
- this.ref = newRef;
370
- }
371
- return this.ref.toString();
372
- }
373
- /**
374
- * Unsubscribe from channels with the specified topic.
375
- *
376
- * @internal
377
- */
378
- _leaveOpenTopic(topic) {
379
- let dupChannel = this.channels.find((c) => c.topic === topic && (c._isJoined() || c._isJoining()));
380
- if (dupChannel) {
381
- this.log('transport', `leaving duplicate topic "${topic}"`);
382
- dupChannel.unsubscribe();
383
- }
384
- }
385
- /**
386
- * Removes a subscription from the socket.
387
- *
388
- * @param channel An open subscription.
389
- *
390
- * @internal
391
- */
392
- _remove(channel) {
393
- this.channels = this.channels.filter((c) => c.topic !== channel.topic);
394
- }
395
- /** @internal */
396
- _onConnMessage(rawMessage) {
397
- this.decode(rawMessage.data, (msg) => {
398
- // Handle heartbeat responses
399
- if (msg.topic === 'phoenix' && msg.event === 'phx_reply') {
400
- this.heartbeatCallback(msg.payload.status === 'ok' ? 'ok' : 'error');
401
- }
402
- // Handle pending heartbeat reference cleanup
403
- if (msg.ref && msg.ref === this.pendingHeartbeatRef) {
404
- this.pendingHeartbeatRef = null;
405
- }
406
- // Log incoming message
407
- const { topic, event, payload, ref } = msg;
408
- const refString = ref ? `(${ref})` : '';
409
- const status = payload.status || '';
410
- this.log('receive', `${status} ${topic} ${event} ${refString}`.trim(), payload);
411
- // Route message to appropriate channels
412
- this.channels
413
- .filter((channel) => channel._isMember(topic))
414
- .forEach((channel) => channel._trigger(event, payload, ref));
415
- this._triggerStateCallbacks('message', msg);
416
- });
417
- }
418
- /**
419
- * Clear specific timer
420
- * @internal
421
- */
422
- _clearTimer(timer) {
423
- var _a;
424
- if (timer === 'heartbeat' && this.heartbeatTimer) {
425
- clearInterval(this.heartbeatTimer);
426
- this.heartbeatTimer = undefined;
427
- }
428
- else if (timer === 'reconnect') {
429
- (_a = this.reconnectTimer) === null || _a === void 0 ? void 0 : _a.reset();
430
- }
431
- }
432
- /**
433
- * Clear all timers
434
- * @internal
435
- */
436
- _clearAllTimers() {
437
- this._clearTimer('heartbeat');
438
- this._clearTimer('reconnect');
439
- }
440
- /**
441
- * Setup connection handlers for WebSocket events
442
- * @internal
443
- */
444
- _setupConnectionHandlers() {
445
- if (!this.conn)
446
- return;
447
- // Set binary type if supported (browsers and most WebSocket implementations)
448
- if ('binaryType' in this.conn) {
449
- ;
450
- this.conn.binaryType = 'arraybuffer';
451
- }
452
- this.conn.onopen = () => this._onConnOpen();
453
- this.conn.onerror = (error) => this._onConnError(error);
454
- this.conn.onmessage = (event) => this._onConnMessage(event);
455
- this.conn.onclose = (event) => this._onConnClose(event);
456
- }
457
- /**
458
- * Teardown connection and cleanup resources
459
- * @internal
460
- */
461
- _teardownConnection() {
462
- if (this.conn) {
463
- this.conn.onopen = null;
464
- this.conn.onerror = null;
465
- this.conn.onmessage = null;
466
- this.conn.onclose = null;
467
- this.conn = null;
468
- }
469
- this._clearAllTimers();
470
- this.channels.forEach((channel) => channel.teardown());
471
- }
472
- /** @internal */
473
- _onConnOpen() {
474
- this._setConnectionState('connected');
475
- this.log('transport', `connected to ${this.endpointURL()}`);
476
- this.flushSendBuffer();
477
- this._clearTimer('reconnect');
478
- if (!this.worker) {
479
- this._startHeartbeat();
480
- }
481
- else {
482
- if (!this.workerRef) {
483
- this._startWorkerHeartbeat();
484
- }
485
- }
486
- this._triggerStateCallbacks('open');
487
- }
488
- /** @internal */
489
- _startHeartbeat() {
490
- this.heartbeatTimer && clearInterval(this.heartbeatTimer);
491
- this.heartbeatTimer = setInterval(() => this.sendHeartbeat(), this.heartbeatIntervalMs);
492
- }
493
- /** @internal */
494
- _startWorkerHeartbeat() {
495
- if (this.workerUrl) {
496
- this.log('worker', `starting worker for from ${this.workerUrl}`);
497
- }
498
- else {
499
- this.log('worker', `starting default worker`);
500
- }
501
- const objectUrl = this._workerObjectUrl(this.workerUrl);
502
- this.workerRef = new Worker(objectUrl);
503
- this.workerRef.onerror = (error) => {
504
- this.log('worker', 'worker error', error.message);
505
- this.workerRef.terminate();
506
- };
507
- this.workerRef.onmessage = (event) => {
508
- if (event.data.event === 'keepAlive') {
509
- this.sendHeartbeat();
510
- }
511
- };
512
- this.workerRef.postMessage({
513
- event: 'start',
514
- interval: this.heartbeatIntervalMs,
515
- });
516
- }
517
- /** @internal */
518
- _onConnClose(event) {
519
- var _a;
520
- this._setConnectionState('disconnected');
521
- this.log('transport', 'close', event);
522
- this._triggerChanError();
523
- this._clearTimer('heartbeat');
524
- // Only schedule reconnection if it wasn't a manual disconnect
525
- if (!this._wasManualDisconnect) {
526
- (_a = this.reconnectTimer) === null || _a === void 0 ? void 0 : _a.scheduleTimeout();
527
- }
528
- this._triggerStateCallbacks('close', event);
529
- }
530
- /** @internal */
531
- _onConnError(error) {
532
- this._setConnectionState('disconnected');
533
- this.log('transport', `${error}`);
534
- this._triggerChanError();
535
- this._triggerStateCallbacks('error', error);
536
- }
537
- /** @internal */
538
- _triggerChanError() {
539
- this.channels.forEach((channel) => channel._trigger(CHANNEL_EVENTS.error));
540
- }
541
- /** @internal */
542
- _appendParams(url, params) {
543
- if (Object.keys(params).length === 0) {
544
- return url;
545
- }
546
- const prefix = url.match(/\?/) ? '&' : '?';
547
- const query = new URLSearchParams(params);
548
- return `${url}${prefix}${query}`;
549
- }
550
- _workerObjectUrl(url) {
551
- let result_url;
552
- if (url) {
553
- result_url = url;
554
- }
555
- else {
556
- const blob = new Blob([WORKER_SCRIPT], { type: 'application/javascript' });
557
- result_url = URL.createObjectURL(blob);
558
- }
559
- return result_url;
560
- }
561
- /**
562
- * Set connection state with proper state management
563
- * @internal
564
- */
565
- _setConnectionState(state, manual = false) {
566
- this._connectionState = state;
567
- if (state === 'connecting') {
568
- this._wasManualDisconnect = false;
569
- }
570
- else if (state === 'disconnecting') {
571
- this._wasManualDisconnect = manual;
572
- }
573
- }
574
- /**
575
- * Perform the actual auth operation
576
- * @internal
577
- */
578
- async _performAuth(token = null) {
579
- let tokenToSend;
580
- if (token) {
581
- tokenToSend = token;
582
- }
583
- else if (this.accessToken) {
584
- // Always call the accessToken callback to get fresh token
585
- tokenToSend = await this.accessToken();
586
- }
587
- else {
588
- tokenToSend = this.accessTokenValue;
589
- }
590
- if (this.accessTokenValue != tokenToSend) {
591
- this.accessTokenValue = tokenToSend;
592
- this.channels.forEach((channel) => {
593
- const payload = {
594
- access_token: tokenToSend,
595
- version: DEFAULT_VERSION,
596
- };
597
- tokenToSend && channel.updateJoinPayload(payload);
598
- if (channel.joinedOnce && channel._isJoined()) {
599
- channel._push(CHANNEL_EVENTS.access_token, {
600
- access_token: tokenToSend,
601
- });
602
- }
603
- });
604
- }
605
- }
606
- /**
607
- * Wait for any in-flight auth operations to complete
608
- * @internal
609
- */
610
- async _waitForAuthIfNeeded() {
611
- if (this._authPromise) {
612
- await this._authPromise;
613
- }
614
- }
615
- /**
616
- * Safely call setAuth with standardized error handling
617
- * @internal
618
- */
619
- _setAuthSafely(context = 'general') {
620
- this.setAuth().catch((e) => {
621
- this.log('error', `error setting auth in ${context}`, e);
622
- });
623
- }
624
- /**
625
- * Trigger state change callbacks with proper error handling
626
- * @internal
627
- */
628
- _triggerStateCallbacks(event, data) {
629
- try {
630
- this.stateChangeCallbacks[event].forEach((callback) => {
631
- try {
632
- callback(data);
633
- }
634
- catch (e) {
635
- this.log('error', `error in ${event} callback`, e);
636
- }
637
- });
638
- }
639
- catch (e) {
640
- this.log('error', `error triggering ${event} callbacks`, e);
641
- }
642
- }
643
- /**
644
- * Setup reconnection timer with proper configuration
645
- * @internal
646
- */
647
- _setupReconnectionTimer() {
648
- this.reconnectTimer = new Timer(async () => {
649
- setTimeout(async () => {
650
- await this._waitForAuthIfNeeded();
651
- if (!this.isConnected()) {
652
- this.connect();
653
- }
654
- }, CONNECTION_TIMEOUTS.RECONNECT_DELAY);
655
- }, this.reconnectAfterMs);
656
- }
657
- /**
658
- * Initialize client options with defaults
659
- * @internal
660
- */
661
- _initializeOptions(options) {
662
- var _a, _b, _c, _d, _e, _f, _g, _h;
663
- // Set defaults
664
- this.transport = (_a = options === null || options === void 0 ? void 0 : options.transport) !== null && _a !== void 0 ? _a : null;
665
- this.timeout = (_b = options === null || options === void 0 ? void 0 : options.timeout) !== null && _b !== void 0 ? _b : DEFAULT_TIMEOUT;
666
- this.heartbeatIntervalMs =
667
- (_c = options === null || options === void 0 ? void 0 : options.heartbeatIntervalMs) !== null && _c !== void 0 ? _c : CONNECTION_TIMEOUTS.HEARTBEAT_INTERVAL;
668
- this.worker = (_d = options === null || options === void 0 ? void 0 : options.worker) !== null && _d !== void 0 ? _d : false;
669
- this.accessToken = (_e = options === null || options === void 0 ? void 0 : options.accessToken) !== null && _e !== void 0 ? _e : null;
670
- // Handle special cases
671
- if (options === null || options === void 0 ? void 0 : options.params)
672
- this.params = options.params;
673
- if (options === null || options === void 0 ? void 0 : options.logger)
674
- this.logger = options.logger;
675
- if ((options === null || options === void 0 ? void 0 : options.logLevel) || (options === null || options === void 0 ? void 0 : options.log_level)) {
676
- this.logLevel = options.logLevel || options.log_level;
677
- this.params = Object.assign(Object.assign({}, this.params), { log_level: this.logLevel });
678
- }
679
- // Set up functions with defaults
680
- this.reconnectAfterMs =
681
- (_f = options === null || options === void 0 ? void 0 : options.reconnectAfterMs) !== null && _f !== void 0 ? _f : ((tries) => {
682
- return RECONNECT_INTERVALS[tries - 1] || DEFAULT_RECONNECT_FALLBACK;
683
- });
684
- this.encode =
685
- (_g = options === null || options === void 0 ? void 0 : options.encode) !== null && _g !== void 0 ? _g : ((payload, callback) => {
686
- return callback(JSON.stringify(payload));
687
- });
688
- this.decode = (_h = options === null || options === void 0 ? void 0 : options.decode) !== null && _h !== void 0 ? _h : this.serializer.decode.bind(this.serializer);
689
- // Handle worker setup
690
- if (this.worker) {
691
- if (typeof window !== 'undefined' && !window.Worker) {
692
- throw new Error('Web Worker is not supported');
693
- }
694
- this.workerUrl = options === null || options === void 0 ? void 0 : options.workerUrl;
695
- }
696
- }
697
- }
698
- //# sourceMappingURL=RealtimeClient.js.map