@data-loom/realtime-js 0.0.2-alpha.2 → 0.0.2

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 (81) hide show
  1. package/dist/main/RealtimeChannel.d.ts +228 -0
  2. package/dist/main/RealtimeChannel.d.ts.map +1 -0
  3. package/dist/main/RealtimeChannel.js +528 -0
  4. package/dist/main/RealtimeChannel.js.map +1 -0
  5. package/dist/main/RealtimeClient.d.ts +197 -0
  6. package/dist/main/RealtimeClient.d.ts.map +1 -0
  7. package/dist/main/RealtimeClient.js +514 -0
  8. package/dist/main/RealtimeClient.js.map +1 -0
  9. package/dist/main/RealtimePresence.d.ts +67 -0
  10. package/dist/main/RealtimePresence.d.ts.map +1 -0
  11. package/dist/main/RealtimePresence.js +228 -0
  12. package/dist/main/RealtimePresence.js.map +1 -0
  13. package/dist/main/index.d.ts +5 -0
  14. package/dist/main/index.d.ts.map +1 -0
  15. package/dist/main/index.js +41 -0
  16. package/dist/main/index.js.map +1 -0
  17. package/dist/main/utils/constants.d.ts +43 -0
  18. package/dist/main/utils/constants.d.ts.map +1 -0
  19. package/dist/main/utils/constants.js +51 -0
  20. package/dist/main/utils/constants.js.map +1 -0
  21. package/dist/main/utils/push.d.ts +48 -0
  22. package/dist/main/utils/push.d.ts.map +1 -0
  23. package/dist/main/utils/push.js +104 -0
  24. package/dist/main/utils/push.js.map +1 -0
  25. package/dist/main/utils/serializer.d.ts +7 -0
  26. package/dist/main/utils/serializer.d.ts.map +1 -0
  27. package/dist/main/utils/serializer.js +36 -0
  28. package/dist/main/utils/serializer.js.map +1 -0
  29. package/dist/main/utils/timer.d.ts +22 -0
  30. package/dist/main/utils/timer.d.ts.map +1 -0
  31. package/dist/main/utils/timer.js +38 -0
  32. package/dist/main/utils/timer.js.map +1 -0
  33. package/dist/main/utils/transformers.d.ts +109 -0
  34. package/dist/main/utils/transformers.d.ts.map +1 -0
  35. package/dist/main/utils/transformers.js +229 -0
  36. package/dist/main/utils/transformers.js.map +1 -0
  37. package/dist/main/utils/version.d.ts +2 -0
  38. package/dist/main/utils/version.d.ts.map +1 -0
  39. package/dist/main/utils/version.js +5 -0
  40. package/dist/main/utils/version.js.map +1 -0
  41. package/dist/module/RealtimeChannel.d.ts +228 -0
  42. package/dist/module/RealtimeChannel.d.ts.map +1 -0
  43. package/dist/module/RealtimeChannel.js +498 -0
  44. package/dist/module/RealtimeChannel.js.map +1 -0
  45. package/dist/module/RealtimeClient.d.ts +197 -0
  46. package/dist/module/RealtimeClient.d.ts.map +1 -0
  47. package/dist/module/RealtimeClient.js +482 -0
  48. package/dist/module/RealtimeClient.js.map +1 -0
  49. package/dist/module/RealtimePresence.d.ts +67 -0
  50. package/dist/module/RealtimePresence.d.ts.map +1 -0
  51. package/dist/module/RealtimePresence.js +224 -0
  52. package/dist/module/RealtimePresence.js.map +1 -0
  53. package/dist/module/index.d.ts +5 -0
  54. package/dist/module/index.d.ts.map +1 -0
  55. package/dist/module/index.js +5 -0
  56. package/dist/module/index.js.map +1 -0
  57. package/dist/module/utils/constants.d.ts +43 -0
  58. package/dist/module/utils/constants.d.ts.map +1 -0
  59. package/dist/module/utils/constants.js +48 -0
  60. package/dist/module/utils/constants.js.map +1 -0
  61. package/dist/module/utils/push.d.ts +48 -0
  62. package/dist/module/utils/push.d.ts.map +1 -0
  63. package/dist/module/utils/push.js +101 -0
  64. package/dist/module/utils/push.js.map +1 -0
  65. package/dist/module/utils/serializer.d.ts +7 -0
  66. package/dist/module/utils/serializer.d.ts.map +1 -0
  67. package/dist/module/utils/serializer.js +33 -0
  68. package/dist/module/utils/serializer.js.map +1 -0
  69. package/dist/module/utils/timer.d.ts +22 -0
  70. package/dist/module/utils/timer.d.ts.map +1 -0
  71. package/dist/module/utils/timer.js +35 -0
  72. package/dist/module/utils/timer.js.map +1 -0
  73. package/dist/module/utils/transformers.d.ts +109 -0
  74. package/dist/module/utils/transformers.d.ts.map +1 -0
  75. package/dist/module/utils/transformers.js +217 -0
  76. package/dist/module/utils/transformers.js.map +1 -0
  77. package/dist/module/utils/version.d.ts +2 -0
  78. package/dist/module/utils/version.d.ts.map +1 -0
  79. package/dist/module/utils/version.js +2 -0
  80. package/dist/module/utils/version.js.map +1 -0
  81. package/package.json +2 -2
@@ -0,0 +1,197 @@
1
+ import type { WebSocket as WSWebSocket } from 'ws';
2
+ import { CONNECTION_STATE, LOG_LEVEL } from './utils/constants';
3
+ import Serializer from './utils/serializer';
4
+ import Timer from './utils/timer';
5
+ import RealtimeChannel from './RealtimeChannel';
6
+ import type { RealtimeChannelOptions } from './RealtimeChannel';
7
+ type Fetch = typeof fetch;
8
+ export type Channel = {
9
+ name: string;
10
+ inserted_at: string;
11
+ updated_at: string;
12
+ id: number;
13
+ };
14
+ export type LogLevel = LOG_LEVEL;
15
+ export type RealtimeMessage = {
16
+ topic: string;
17
+ event: string;
18
+ payload: any;
19
+ ref: string;
20
+ join_ref?: string;
21
+ };
22
+ export type RealtimeRemoveChannelResponse = 'ok' | 'timed out' | 'error';
23
+ export interface WebSocketLikeConstructor {
24
+ new (address: string | URL, _ignored?: any, options?: {
25
+ headers: Object | undefined;
26
+ }): WebSocketLike;
27
+ }
28
+ export type WebSocketLike = WebSocket | WSWebSocket | WSWebSocketDummy;
29
+ export interface WebSocketLikeError {
30
+ error: any;
31
+ message: string;
32
+ type: string;
33
+ }
34
+ export type RealtimeClientOptions = {
35
+ transport?: WebSocketLikeConstructor;
36
+ timeout?: number;
37
+ heartbeatIntervalMs?: number;
38
+ logger?: Function;
39
+ encode?: Function;
40
+ decode?: Function;
41
+ reconnectAfterMs?: Function;
42
+ headers?: {
43
+ [key: string]: string;
44
+ };
45
+ params?: {
46
+ [key: string]: any;
47
+ };
48
+ log_level?: LogLevel;
49
+ logLevel?: LogLevel;
50
+ fetch?: Fetch;
51
+ worker?: boolean;
52
+ workerUrl?: string;
53
+ accessToken?: () => Promise<string | null>;
54
+ };
55
+ export default class RealtimeClient {
56
+ accessTokenValue: string | null;
57
+ apiKey: string | null;
58
+ channels: RealtimeChannel[];
59
+ endPoint: string;
60
+ httpEndpoint: string;
61
+ headers?: {
62
+ [key: string]: string;
63
+ };
64
+ params?: {
65
+ [key: string]: string;
66
+ };
67
+ timeout: number;
68
+ transport: WebSocketLikeConstructor | null;
69
+ heartbeatIntervalMs: number;
70
+ heartbeatTimer: ReturnType<typeof setInterval> | undefined;
71
+ pendingHeartbeatRef: string | null;
72
+ ref: number;
73
+ reconnectTimer: Timer;
74
+ logger: Function;
75
+ logLevel?: LogLevel;
76
+ encode: Function;
77
+ decode: Function;
78
+ reconnectAfterMs: Function;
79
+ conn: WebSocketLike | null;
80
+ sendBuffer: Function[];
81
+ serializer: Serializer;
82
+ stateChangeCallbacks: {
83
+ open: Function[];
84
+ close: Function[];
85
+ error: Function[];
86
+ message: Function[];
87
+ };
88
+ fetch: Fetch;
89
+ accessToken: (() => Promise<string | null>) | null;
90
+ worker?: boolean;
91
+ workerUrl?: string;
92
+ workerRef?: Worker;
93
+ /**
94
+ * Initializes the Socket.
95
+ *
96
+ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
97
+ * @param httpEndpoint The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol)
98
+ * @param options.transport The Websocket Transport, for example WebSocket. This can be a custom implementation
99
+ * @param options.timeout The default timeout in milliseconds to trigger push timeouts.
100
+ * @param options.params The optional params to pass when connecting.
101
+ * @param options.headers The optional headers to pass when connecting.
102
+ * @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
103
+ * @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
104
+ * @param options.logLevel Sets the log level for Realtime
105
+ * @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
106
+ * @param options.decode The function to decode incoming messages. Defaults to Serializer's decode.
107
+ * @param options.reconnectAfterMs he optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
108
+ * @param options.worker Use Web Worker to set a side flow. Defaults to false.
109
+ * @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.
110
+ */
111
+ constructor(endPoint: string, options?: RealtimeClientOptions);
112
+ /**
113
+ * Connects the socket, unless already connected.
114
+ */
115
+ connect(): void;
116
+ /**
117
+ * Returns the URL of the websocket.
118
+ * @returns string The URL of the websocket.
119
+ */
120
+ endpointURL(): string;
121
+ /**
122
+ * Disconnects the socket.
123
+ *
124
+ * @param code A numeric status code to send on disconnect.
125
+ * @param reason A custom reason for the disconnect.
126
+ */
127
+ disconnect(code?: number, reason?: string): void;
128
+ /**
129
+ * Returns all created channels
130
+ */
131
+ getChannels(): RealtimeChannel[];
132
+ /**
133
+ * Unsubscribes and removes a single channel
134
+ * @param channel A RealtimeChannel instance
135
+ */
136
+ removeChannel(channel: RealtimeChannel): Promise<RealtimeRemoveChannelResponse>;
137
+ /**
138
+ * Unsubscribes and removes all channels
139
+ */
140
+ removeAllChannels(): Promise<RealtimeRemoveChannelResponse[]>;
141
+ /**
142
+ * Logs the message.
143
+ *
144
+ * For customized logging, `this.logger` can be overridden.
145
+ */
146
+ log(kind: string, msg: string, data?: any): void;
147
+ /**
148
+ * Returns the current state of the socket.
149
+ */
150
+ connectionState(): CONNECTION_STATE;
151
+ /**
152
+ * Returns `true` is the connection is open.
153
+ */
154
+ isConnected(): boolean;
155
+ channel(topic: string, params?: RealtimeChannelOptions): RealtimeChannel;
156
+ /**
157
+ * Push out a message if the socket is connected.
158
+ *
159
+ * If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established.
160
+ */
161
+ push(data: RealtimeMessage): void;
162
+ /**
163
+ * Sets the JWT access token used for channel subscription authorization and Realtime RLS.
164
+ *
165
+ * If param is null it will use the `accessToken` callback function or the token set on the client.
166
+ *
167
+ * On callback used, it will set the value of the token internal to the client.
168
+ *
169
+ * @param token A JWT string to override the token set on the client.
170
+ */
171
+ setAuth(token?: string | null): Promise<void>;
172
+ /**
173
+ * Sends a heartbeat message if the socket is connected.
174
+ */
175
+ sendHeartbeat(): Promise<void>;
176
+ /**
177
+ * Flushes send buffer
178
+ */
179
+ flushSendBuffer(): void;
180
+ private _workerObjectUrl;
181
+ }
182
+ declare class WSWebSocketDummy {
183
+ binaryType: string;
184
+ close: Function;
185
+ onclose: Function;
186
+ onerror: Function;
187
+ onmessage: Function;
188
+ onopen: Function;
189
+ readyState: number;
190
+ send: Function;
191
+ url: string | URL | null;
192
+ constructor(address: string, _protocols: undefined, options: {
193
+ close: Function;
194
+ });
195
+ }
196
+ export {};
197
+ //# sourceMappingURL=RealtimeClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RealtimeClient.d.ts","sourceRoot":"","sources":["../../src/RealtimeClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,IAAI,WAAW,EAAE,MAAM,IAAI,CAAA;AAElD,OAAO,EAEL,gBAAgB,EAOhB,SAAS,EACV,MAAM,mBAAmB,CAAA;AAE1B,OAAO,UAAU,MAAM,oBAAoB,CAAA;AAC3C,OAAO,KAAK,MAAM,eAAe,CAAA;AAGjC,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAE/D,KAAK,KAAK,GAAG,OAAO,KAAK,CAAA;AAEzB,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,EAAE,EAAE,MAAM,CAAA;CACX,CAAA;AACD,MAAM,MAAM,QAAQ,GAAG,SAAS,CAAA;AAEhC,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,GAAG,CAAA;IACZ,GAAG,EAAE,MA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AE,QAAQ,CAAW;IAC5B,SAAS,EAAE,QAAQ,CAAW;IAC9B,MAAM,EAAE,QAAQ,CAAW;IAC3B,UAAU,EAAE,MAAM,CAA2B;IAC7C,IAAI,EAAE,QAAQ,CAAW;IACzB,GAAG,EAAE,MAAM,GAAG,GAAG,GAAG,IAAI,CAAO;gBAG7B,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,SAAS,EACrB,OAAO,EAAE;QAAE,KAAK,EAAE,QAAQ,CAAA;KAAE;CAK/B"}
@@ -0,0 +1,482 @@
1
+ import { CHANNEL_EVENTS, CONNECTION_STATE, DEFAULT_HEADERS, DEFAULT_TIMEOUT, SOCKET_STATES, TRANSPORTS, VSN, WS_CLOSE_NORMAL, } from './utils/constants';
2
+ import Serializer from './utils/serializer';
3
+ import Timer from './utils/timer';
4
+ import { httpEndpointURL } from './utils/transformers';
5
+ import RealtimeChannel from './RealtimeChannel';
6
+ const noop = () => { };
7
+ const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined';
8
+ const WORKER_SCRIPT = `
9
+ addEventListener("message", (e) => {
10
+ if (e.data.event === "start") {
11
+ setInterval(() => postMessage({ event: "keepAlive" }), e.data.interval);
12
+ }
13
+ });`;
14
+ export default class RealtimeClient {
15
+ /**
16
+ * Initializes the Socket.
17
+ *
18
+ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
19
+ * @param httpEndpoint The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol)
20
+ * @param options.transport The Websocket Transport, for example WebSocket. This can be a custom implementation
21
+ * @param options.timeout The default timeout in milliseconds to trigger push timeouts.
22
+ * @param options.params The optional params to pass when connecting.
23
+ * @param options.headers The optional headers to pass when connecting.
24
+ * @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
25
+ * @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
26
+ * @param options.logLevel Sets the log level for Realtime
27
+ * @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
28
+ * @param options.decode The function to decode incoming messages. Defaults to Serializer's decode.
29
+ * @param options.reconnectAfterMs he optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
30
+ * @param options.worker Use Web Worker to set a side flow. Defaults to false.
31
+ * @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.
32
+ */
33
+ constructor(endPoint, options) {
34
+ var _a;
35
+ this.accessTokenValue = null;
36
+ this.apiKey = null;
37
+ this.channels = [];
38
+ this.endPoint = '';
39
+ this.httpEndpoint = '';
40
+ this.headers = DEFAULT_HEADERS;
41
+ this.params = {};
42
+ this.timeout = DEFAULT_TIMEOUT;
43
+ this.heartbeatIntervalMs = 25000;
44
+ this.heartbeatTimer = undefined;
45
+ this.pendingHeartbeatRef = null;
46
+ this.ref = 0;
47
+ this.logger = noop;
48
+ this.conn = null;
49
+ this.sendBuffer = [];
50
+ this.serializer = new Serializer();
51
+ this.stateChangeCallbacks = {
52
+ open: [],
53
+ close: [],
54
+ error: [],
55
+ message: [],
56
+ };
57
+ this.accessToken = null;
58
+ /**
59
+ * Use either custom fetch, if provided, or default fetch to make HTTP requests
60
+ *
61
+ * @internal
62
+ */
63
+ this._resolveFetch = (customFetch) => {
64
+ let _fetch;
65
+ if (customFetch) {
66
+ _fetch = customFetch;
67
+ }
68
+ else if (typeof fetch === 'undefined') {
69
+ _fetch = (...args) => import('@data-loom/node-fetch').then(({ default: fetch }) => fetch(...args));
70
+ }
71
+ else {
72
+ _fetch = fetch;
73
+ }
74
+ return (...args) => _fetch(...args);
75
+ };
76
+ this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`;
77
+ this.httpEndpoint = httpEndpointURL(endPoint);
78
+ if (options === null || options === void 0 ? void 0 : options.transport) {
79
+ this.transport = options.transport;
80
+ }
81
+ else {
82
+ this.transport = null;
83
+ }
84
+ if (options === null || options === void 0 ? void 0 : options.params)
85
+ this.params = options.params;
86
+ if (options === null || options === void 0 ? void 0 : options.headers)
87
+ this.headers = Object.assign(Object.assign({}, this.headers), options.headers);
88
+ if (options === null || options === void 0 ? void 0 : options.timeout)
89
+ this.timeout = options.timeout;
90
+ if (options === null || options === void 0 ? void 0 : options.logger)
91
+ this.logger = options.logger;
92
+ if ((options === null || options === void 0 ? void 0 : options.logLevel) || (options === null || options === void 0 ? void 0 : options.log_level)) {
93
+ this.logLevel = options.logLevel || options.log_level;
94
+ this.params = Object.assign(Object.assign({}, this.params), { log_level: this.logLevel });
95
+ }
96
+ if (options === null || options === void 0 ? void 0 : options.heartbeatIntervalMs)
97
+ this.heartbeatIntervalMs = options.heartbeatIntervalMs;
98
+ const accessTokenValue = (_a = options === null || options === void 0 ? void 0 : options.params) === null || _a === void 0 ? void 0 : _a.apikey;
99
+ if (accessTokenValue) {
100
+ this.accessTokenValue = accessTokenValue;
101
+ this.apiKey = accessTokenValue;
102
+ }
103
+ this.reconnectAfterMs = (options === null || options === void 0 ? void 0 : options.reconnectAfterMs)
104
+ ? options.reconnectAfterMs
105
+ : (tries) => {
106
+ return [1000, 2000, 5000, 10000][tries - 1] || 10000;
107
+ };
108
+ this.encode = (options === null || options === void 0 ? void 0 : options.encode)
109
+ ? options.encode
110
+ : (payload, callback) => {
111
+ return callback(JSON.stringify(payload));
112
+ };
113
+ this.decode = (options === null || options === void 0 ? void 0 : options.decode)
114
+ ? options.decode
115
+ : this.serializer.decode.bind(this.serializer);
116
+ this.reconnectTimer = new Timer(async () => {
117
+ this.disconnect();
118
+ this.connect();
119
+ }, this.reconnectAfterMs);
120
+ this.fetch = this._resolveFetch(options === null || options === void 0 ? void 0 : options.fetch);
121
+ if (options === null || options === void 0 ? void 0 : options.worker) {
122
+ if (typeof window !== 'undefined' && !window.Worker) {
123
+ throw new Error('Web Worker is not supported');
124
+ }
125
+ this.worker = (options === null || options === void 0 ? void 0 : options.worker) || false;
126
+ this.workerUrl = options === null || options === void 0 ? void 0 : options.workerUrl;
127
+ }
128
+ this.accessToken = (options === null || options === void 0 ? void 0 : options.accessToken) || null;
129
+ }
130
+ /**
131
+ * Connects the socket, unless already connected.
132
+ */
133
+ connect() {
134
+ if (this.conn) {
135
+ return;
136
+ }
137
+ if (this.transport) {
138
+ this.conn = new this.transport(this.endpointURL(), undefined, {
139
+ headers: this.headers,
140
+ });
141
+ this.setupConnection();
142
+ return;
143
+ }
144
+ if (NATIVE_WEBSOCKET_AVAILABLE) {
145
+ this.conn = new WebSocket(this.endpointURL());
146
+ this.setupConnection();
147
+ return;
148
+ }
149
+ this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
150
+ close: () => {
151
+ this.conn = null;
152
+ },
153
+ });
154
+ import('ws').then(({ default: WS }) => {
155
+ this.conn = new WS(this.endpointURL(), undefined, {
156
+ headers: this.headers,
157
+ });
158
+ this.setupConnection();
159
+ });
160
+ }
161
+ /**
162
+ * Returns the URL of the websocket.
163
+ * @returns string The URL of the websocket.
164
+ */
165
+ endpointURL() {
166
+ return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: VSN }));
167
+ }
168
+ /**
169
+ * Disconnects the socket.
170
+ *
171
+ * @param code A numeric status code to send on disconnect.
172
+ * @param reason A custom reason for the disconnect.
173
+ */
174
+ disconnect(code, reason) {
175
+ if (this.conn) {
176
+ this.conn.onclose = function () { }; // noop
177
+ if (code) {
178
+ this.conn.close(code, reason !== null && reason !== void 0 ? reason : '');
179
+ }
180
+ else {
181
+ this.conn.close();
182
+ }
183
+ this.conn = null;
184
+ // remove open handles
185
+ this.heartbeatTimer && clearInterval(this.heartbeatTimer);
186
+ this.reconnectTimer.reset();
187
+ }
188
+ }
189
+ /**
190
+ * Returns all created channels
191
+ */
192
+ getChannels() {
193
+ return this.channels;
194
+ }
195
+ /**
196
+ * Unsubscribes and removes a single channel
197
+ * @param channel A RealtimeChannel instance
198
+ */
199
+ async removeChannel(channel) {
200
+ const status = await channel.unsubscribe();
201
+ if (this.channels.length === 0) {
202
+ this.disconnect();
203
+ }
204
+ return status;
205
+ }
206
+ /**
207
+ * Unsubscribes and removes all channels
208
+ */
209
+ async removeAllChannels() {
210
+ const values_1 = await Promise.all(this.channels.map((channel) => channel.unsubscribe()));
211
+ this.disconnect();
212
+ return values_1;
213
+ }
214
+ /**
215
+ * Logs the message.
216
+ *
217
+ * For customized logging, `this.logger` can be overridden.
218
+ */
219
+ log(kind, msg, data) {
220
+ this.logger(kind, msg, data);
221
+ }
222
+ /**
223
+ * Returns the current state of the socket.
224
+ */
225
+ connectionState() {
226
+ switch (this.conn && this.conn.readyState) {
227
+ case SOCKET_STATES.connecting:
228
+ return CONNECTION_STATE.Connecting;
229
+ case SOCKET_STATES.open:
230
+ return CONNECTION_STATE.Open;
231
+ case SOCKET_STATES.closing:
232
+ return CONNECTION_STATE.Closing;
233
+ default:
234
+ return CONNECTION_STATE.Closed;
235
+ }
236
+ }
237
+ /**
238
+ * Returns `true` is the connection is open.
239
+ */
240
+ isConnected() {
241
+ return this.connectionState() === CONNECTION_STATE.Open;
242
+ }
243
+ channel(topic, params = { config: {} }) {
244
+ const chan = new RealtimeChannel(`realtime:${topic}`, params, this);
245
+ this.channels.push(chan);
246
+ return chan;
247
+ }
248
+ /**
249
+ * Push out a message if the socket is connected.
250
+ *
251
+ * If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established.
252
+ */
253
+ push(data) {
254
+ const { topic, event, payload, ref } = data;
255
+ const callback = () => {
256
+ this.encode(data, (result) => {
257
+ var _a;
258
+ (_a = this.conn) === null || _a === void 0 ? void 0 : _a.send(result);
259
+ });
260
+ };
261
+ this.log('push', `${topic} ${event} (${ref})`, payload);
262
+ if (this.isConnected()) {
263
+ callback();
264
+ }
265
+ else {
266
+ this.sendBuffer.push(callback);
267
+ }
268
+ }
269
+ /**
270
+ * Sets the JWT access token used for channel subscription authorization and Realtime RLS.
271
+ *
272
+ * If param is null it will use the `accessToken` callback function or the token set on the client.
273
+ *
274
+ * On callback used, it will set the value of the token internal to the client.
275
+ *
276
+ * @param token A JWT string to override the token set on the client.
277
+ */
278
+ async setAuth(token = null) {
279
+ let tokenToSend = token ||
280
+ (this.accessToken && (await this.accessToken())) ||
281
+ this.accessTokenValue;
282
+ if (this.accessTokenValue != tokenToSend) {
283
+ this.accessTokenValue = tokenToSend;
284
+ this.channels.forEach((channel) => {
285
+ tokenToSend &&
286
+ channel.updateJoinPayload({
287
+ access_token: tokenToSend,
288
+ version: this.headers && this.headers['X-Client-Info'],
289
+ });
290
+ if (channel.joinedOnce && channel._isJoined()) {
291
+ channel._push(CHANNEL_EVENTS.access_token, {
292
+ access_token: tokenToSend,
293
+ });
294
+ }
295
+ });
296
+ }
297
+ }
298
+ /**
299
+ * Sends a heartbeat message if the socket is connected.
300
+ */
301
+ async sendHeartbeat() {
302
+ var _a;
303
+ if (!this.isConnected()) {
304
+ return;
305
+ }
306
+ if (this.pendingHeartbeatRef) {
307
+ this.pendingHeartbeatRef = null;
308
+ this.log('transport', 'heartbeat timeout. Attempting to re-establish connection');
309
+ (_a = this.conn) === null || _a === void 0 ? void 0 : _a.close(WS_CLOSE_NORMAL, 'hearbeat timeout');
310
+ return;
311
+ }
312
+ this.pendingHeartbeatRef = this._makeRef();
313
+ this.push({
314
+ topic: 'phoenix',
315
+ event: 'heartbeat',
316
+ payload: {},
317
+ ref: this.pendingHeartbeatRef,
318
+ });
319
+ await this.setAuth();
320
+ }
321
+ /**
322
+ * Flushes send buffer
323
+ */
324
+ flushSendBuffer() {
325
+ if (this.isConnected() && this.sendBuffer.length > 0) {
326
+ this.sendBuffer.forEach((callback) => callback());
327
+ this.sendBuffer = [];
328
+ }
329
+ }
330
+ /**
331
+ * Return the next message ref, accounting for overflows
332
+ *
333
+ * @internal
334
+ */
335
+ _makeRef() {
336
+ let newRef = this.ref + 1;
337
+ if (newRef === this.ref) {
338
+ this.ref = 0;
339
+ }
340
+ else {
341
+ this.ref = newRef;
342
+ }
343
+ return this.ref.toString();
344
+ }
345
+ /**
346
+ * Unsubscribe from channels with the specified topic.
347
+ *
348
+ * @internal
349
+ */
350
+ _leaveOpenTopic(topic) {
351
+ let dupChannel = this.channels.find((c) => c.topic === topic && (c._isJoined() || c._isJoining()));
352
+ if (dupChannel) {
353
+ this.log('transport', `leaving duplicate topic "${topic}"`);
354
+ dupChannel.unsubscribe();
355
+ }
356
+ }
357
+ /**
358
+ * Removes a subscription from the socket.
359
+ *
360
+ * @param channel An open subscription.
361
+ *
362
+ * @internal
363
+ */
364
+ _remove(channel) {
365
+ this.channels = this.channels.filter((c) => c._joinRef() !== channel._joinRef());
366
+ }
367
+ /**
368
+ * Sets up connection handlers.
369
+ *
370
+ * @internal
371
+ */
372
+ setupConnection() {
373
+ if (this.conn) {
374
+ this.conn.binaryType = 'arraybuffer';
375
+ this.conn.onopen = () => this._onConnOpen();
376
+ this.conn.onerror = (error) => this._onConnError(error);
377
+ this.conn.onmessage = (event) => this._onConnMessage(event);
378
+ this.conn.onclose = (event) => this._onConnClose(event);
379
+ }
380
+ }
381
+ /** @internal */
382
+ _onConnMessage(rawMessage) {
383
+ this.decode(rawMessage.data, (msg) => {
384
+ let { topic, event, payload, ref } = msg;
385
+ if (ref && ref === this.pendingHeartbeatRef) {
386
+ this.pendingHeartbeatRef = null;
387
+ }
388
+ this.log('receive', `${payload.status || ''} ${topic} ${event} ${(ref && '(' + ref + ')') || ''}`, payload);
389
+ this.channels
390
+ .filter((channel) => channel._isMember(topic))
391
+ .forEach((channel) => channel._trigger(event, payload, ref));
392
+ this.stateChangeCallbacks.message.forEach((callback) => callback(msg));
393
+ });
394
+ }
395
+ /** @internal */
396
+ async _onConnOpen() {
397
+ this.log('transport', `connected to ${this.endpointURL()}`);
398
+ this.flushSendBuffer();
399
+ this.reconnectTimer.reset();
400
+ if (!this.worker) {
401
+ this.heartbeatTimer && clearInterval(this.heartbeatTimer);
402
+ this.heartbeatTimer = setInterval(() => this.sendHeartbeat(), this.heartbeatIntervalMs);
403
+ }
404
+ else {
405
+ if (this.workerUrl) {
406
+ this.log('worker', `starting worker for from ${this.workerUrl}`);
407
+ }
408
+ else {
409
+ this.log('worker', `starting default worker`);
410
+ }
411
+ const objectUrl = this._workerObjectUrl(this.workerUrl);
412
+ this.workerRef = new Worker(objectUrl);
413
+ this.workerRef.onerror = (error) => {
414
+ this.log('worker', 'worker error', error.message);
415
+ this.workerRef.terminate();
416
+ };
417
+ this.workerRef.onmessage = (event) => {
418
+ if (event.data.event === 'keepAlive') {
419
+ this.sendHeartbeat();
420
+ }
421
+ };
422
+ this.workerRef.postMessage({
423
+ event: 'start',
424
+ interval: this.heartbeatIntervalMs,
425
+ });
426
+ }
427
+ this.stateChangeCallbacks.open.forEach((callback) => callback());
428
+ }
429
+ /** @internal */
430
+ _onConnClose(event) {
431
+ this.log('transport', 'close', event);
432
+ this._triggerChanError();
433
+ this.heartbeatTimer && clearInterval(this.heartbeatTimer);
434
+ this.reconnectTimer.scheduleTimeout();
435
+ this.stateChangeCallbacks.close.forEach((callback) => callback(event));
436
+ }
437
+ /** @internal */
438
+ _onConnError(error) {
439
+ this.log('transport', error.message);
440
+ this._triggerChanError();
441
+ this.stateChangeCallbacks.error.forEach((callback) => callback(error));
442
+ }
443
+ /** @internal */
444
+ _triggerChanError() {
445
+ this.channels.forEach((channel) => channel._trigger(CHANNEL_EVENTS.error));
446
+ }
447
+ /** @internal */
448
+ _appendParams(url, params) {
449
+ if (Object.keys(params).length === 0) {
450
+ return url;
451
+ }
452
+ const prefix = url.match(/\?/) ? '&' : '?';
453
+ const query = new URLSearchParams(params);
454
+ return `${url}${prefix}${query}`;
455
+ }
456
+ _workerObjectUrl(url) {
457
+ let result_url;
458
+ if (url) {
459
+ result_url = url;
460
+ }
461
+ else {
462
+ const blob = new Blob([WORKER_SCRIPT], { type: 'application/javascript' });
463
+ result_url = URL.createObjectURL(blob);
464
+ }
465
+ return result_url;
466
+ }
467
+ }
468
+ class WSWebSocketDummy {
469
+ constructor(address, _protocols, options) {
470
+ this.binaryType = 'arraybuffer';
471
+ this.onclose = () => { };
472
+ this.onerror = () => { };
473
+ this.onmessage = () => { };
474
+ this.onopen = () => { };
475
+ this.readyState = SOCKET_STATES.connecting;
476
+ this.send = () => { };
477
+ this.url = null;
478
+ this.url = address;
479
+ this.close = options.close;
480
+ }
481
+ }
482
+ //# sourceMappingURL=RealtimeClient.js.map