@data-loom/realtime-js 0.0.2-alpha.1 → 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.
- package/dist/main/RealtimeChannel.d.ts +228 -0
- package/dist/main/RealtimeChannel.d.ts.map +1 -0
- package/dist/main/RealtimeChannel.js +528 -0
- package/dist/main/RealtimeChannel.js.map +1 -0
- package/dist/main/RealtimeClient.d.ts +197 -0
- package/dist/main/RealtimeClient.d.ts.map +1 -0
- package/dist/main/RealtimeClient.js +514 -0
- package/dist/main/RealtimeClient.js.map +1 -0
- package/dist/main/RealtimePresence.d.ts +67 -0
- package/dist/main/RealtimePresence.d.ts.map +1 -0
- package/dist/main/RealtimePresence.js +228 -0
- package/dist/main/RealtimePresence.js.map +1 -0
- package/dist/main/index.d.ts +5 -0
- package/dist/main/index.d.ts.map +1 -0
- package/dist/main/index.js +41 -0
- package/dist/main/index.js.map +1 -0
- package/dist/main/utils/constants.d.ts +43 -0
- package/dist/main/utils/constants.d.ts.map +1 -0
- package/dist/main/utils/constants.js +51 -0
- package/dist/main/utils/constants.js.map +1 -0
- package/dist/main/utils/push.d.ts +48 -0
- package/dist/main/utils/push.d.ts.map +1 -0
- package/dist/main/utils/push.js +104 -0
- package/dist/main/utils/push.js.map +1 -0
- package/dist/main/utils/serializer.d.ts +7 -0
- package/dist/main/utils/serializer.d.ts.map +1 -0
- package/dist/main/utils/serializer.js +36 -0
- package/dist/main/utils/serializer.js.map +1 -0
- package/dist/main/utils/timer.d.ts +22 -0
- package/dist/main/utils/timer.d.ts.map +1 -0
- package/dist/main/utils/timer.js +38 -0
- package/dist/main/utils/timer.js.map +1 -0
- package/dist/main/utils/transformers.d.ts +109 -0
- package/dist/main/utils/transformers.d.ts.map +1 -0
- package/dist/main/utils/transformers.js +229 -0
- package/dist/main/utils/transformers.js.map +1 -0
- package/dist/main/utils/version.d.ts +2 -0
- package/dist/main/utils/version.d.ts.map +1 -0
- package/dist/main/utils/version.js +5 -0
- package/dist/main/utils/version.js.map +1 -0
- package/dist/module/RealtimeChannel.d.ts +228 -0
- package/dist/module/RealtimeChannel.d.ts.map +1 -0
- package/dist/module/RealtimeChannel.js +498 -0
- package/dist/module/RealtimeChannel.js.map +1 -0
- package/dist/module/RealtimeClient.d.ts +197 -0
- package/dist/module/RealtimeClient.d.ts.map +1 -0
- package/dist/module/RealtimeClient.js +482 -0
- package/dist/module/RealtimeClient.js.map +1 -0
- package/dist/module/RealtimePresence.d.ts +67 -0
- package/dist/module/RealtimePresence.d.ts.map +1 -0
- package/dist/module/RealtimePresence.js +224 -0
- package/dist/module/RealtimePresence.js.map +1 -0
- package/dist/module/index.d.ts +5 -0
- package/dist/module/index.d.ts.map +1 -0
- package/dist/module/index.js +5 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/utils/constants.d.ts +43 -0
- package/dist/module/utils/constants.d.ts.map +1 -0
- package/dist/module/utils/constants.js +48 -0
- package/dist/module/utils/constants.js.map +1 -0
- package/dist/module/utils/push.d.ts +48 -0
- package/dist/module/utils/push.d.ts.map +1 -0
- package/dist/module/utils/push.js +101 -0
- package/dist/module/utils/push.js.map +1 -0
- package/dist/module/utils/serializer.d.ts +7 -0
- package/dist/module/utils/serializer.d.ts.map +1 -0
- package/dist/module/utils/serializer.js +33 -0
- package/dist/module/utils/serializer.js.map +1 -0
- package/dist/module/utils/timer.d.ts +22 -0
- package/dist/module/utils/timer.d.ts.map +1 -0
- package/dist/module/utils/timer.js +35 -0
- package/dist/module/utils/timer.js.map +1 -0
- package/dist/module/utils/transformers.d.ts +109 -0
- package/dist/module/utils/transformers.d.ts.map +1 -0
- package/dist/module/utils/transformers.js +217 -0
- package/dist/module/utils/transformers.js.map +1 -0
- package/dist/module/utils/version.d.ts +2 -0
- package/dist/module/utils/version.d.ts.map +1 -0
- package/dist/module/utils/version.js +2 -0
- package/dist/module/utils/version.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const constants_1 = require("./utils/constants");
|
|
30
|
+
const serializer_1 = __importDefault(require("./utils/serializer"));
|
|
31
|
+
const timer_1 = __importDefault(require("./utils/timer"));
|
|
32
|
+
const transformers_1 = require("./utils/transformers");
|
|
33
|
+
const RealtimeChannel_1 = __importDefault(require("./RealtimeChannel"));
|
|
34
|
+
const noop = () => { };
|
|
35
|
+
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined';
|
|
36
|
+
const WORKER_SCRIPT = `
|
|
37
|
+
addEventListener("message", (e) => {
|
|
38
|
+
if (e.data.event === "start") {
|
|
39
|
+
setInterval(() => postMessage({ event: "keepAlive" }), e.data.interval);
|
|
40
|
+
}
|
|
41
|
+
});`;
|
|
42
|
+
class RealtimeClient {
|
|
43
|
+
/**
|
|
44
|
+
* Initializes the Socket.
|
|
45
|
+
*
|
|
46
|
+
* @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
|
|
47
|
+
* @param httpEndpoint The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol)
|
|
48
|
+
* @param options.transport The Websocket Transport, for example WebSocket. This can be a custom implementation
|
|
49
|
+
* @param options.timeout The default timeout in milliseconds to trigger push timeouts.
|
|
50
|
+
* @param options.params The optional params to pass when connecting.
|
|
51
|
+
* @param options.headers The optional headers to pass when connecting.
|
|
52
|
+
* @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
|
|
53
|
+
* @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
|
|
54
|
+
* @param options.logLevel Sets the log level for Realtime
|
|
55
|
+
* @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
|
|
56
|
+
* @param options.decode The function to decode incoming messages. Defaults to Serializer's decode.
|
|
57
|
+
* @param options.reconnectAfterMs he optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
|
|
58
|
+
* @param options.worker Use Web Worker to set a side flow. Defaults to false.
|
|
59
|
+
* @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.
|
|
60
|
+
*/
|
|
61
|
+
constructor(endPoint, options) {
|
|
62
|
+
var _a;
|
|
63
|
+
this.accessTokenValue = null;
|
|
64
|
+
this.apiKey = null;
|
|
65
|
+
this.channels = [];
|
|
66
|
+
this.endPoint = '';
|
|
67
|
+
this.httpEndpoint = '';
|
|
68
|
+
this.headers = constants_1.DEFAULT_HEADERS;
|
|
69
|
+
this.params = {};
|
|
70
|
+
this.timeout = constants_1.DEFAULT_TIMEOUT;
|
|
71
|
+
this.heartbeatIntervalMs = 25000;
|
|
72
|
+
this.heartbeatTimer = undefined;
|
|
73
|
+
this.pendingHeartbeatRef = null;
|
|
74
|
+
this.ref = 0;
|
|
75
|
+
this.logger = noop;
|
|
76
|
+
this.conn = null;
|
|
77
|
+
this.sendBuffer = [];
|
|
78
|
+
this.serializer = new serializer_1.default();
|
|
79
|
+
this.stateChangeCallbacks = {
|
|
80
|
+
open: [],
|
|
81
|
+
close: [],
|
|
82
|
+
error: [],
|
|
83
|
+
message: [],
|
|
84
|
+
};
|
|
85
|
+
this.accessToken = null;
|
|
86
|
+
/**
|
|
87
|
+
* Use either custom fetch, if provided, or default fetch to make HTTP requests
|
|
88
|
+
*
|
|
89
|
+
* @internal
|
|
90
|
+
*/
|
|
91
|
+
this._resolveFetch = (customFetch) => {
|
|
92
|
+
let _fetch;
|
|
93
|
+
if (customFetch) {
|
|
94
|
+
_fetch = customFetch;
|
|
95
|
+
}
|
|
96
|
+
else if (typeof fetch === 'undefined') {
|
|
97
|
+
_fetch = (...args) => {
|
|
98
|
+
var _a;
|
|
99
|
+
return (_a = '@data-loom/node-fetch', Promise.resolve().then(() => __importStar(require(_a)))).then(({ default: fetch }) => fetch(...args));
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
_fetch = fetch;
|
|
104
|
+
}
|
|
105
|
+
return (...args) => _fetch(...args);
|
|
106
|
+
};
|
|
107
|
+
this.endPoint = `${endPoint}/${constants_1.TRANSPORTS.websocket}`;
|
|
108
|
+
this.httpEndpoint = (0, transformers_1.httpEndpointURL)(endPoint);
|
|
109
|
+
if (options === null || options === void 0 ? void 0 : options.transport) {
|
|
110
|
+
this.transport = options.transport;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
this.transport = null;
|
|
114
|
+
}
|
|
115
|
+
if (options === null || options === void 0 ? void 0 : options.params)
|
|
116
|
+
this.params = options.params;
|
|
117
|
+
if (options === null || options === void 0 ? void 0 : options.headers)
|
|
118
|
+
this.headers = Object.assign(Object.assign({}, this.headers), options.headers);
|
|
119
|
+
if (options === null || options === void 0 ? void 0 : options.timeout)
|
|
120
|
+
this.timeout = options.timeout;
|
|
121
|
+
if (options === null || options === void 0 ? void 0 : options.logger)
|
|
122
|
+
this.logger = options.logger;
|
|
123
|
+
if ((options === null || options === void 0 ? void 0 : options.logLevel) || (options === null || options === void 0 ? void 0 : options.log_level)) {
|
|
124
|
+
this.logLevel = options.logLevel || options.log_level;
|
|
125
|
+
this.params = Object.assign(Object.assign({}, this.params), { log_level: this.logLevel });
|
|
126
|
+
}
|
|
127
|
+
if (options === null || options === void 0 ? void 0 : options.heartbeatIntervalMs)
|
|
128
|
+
this.heartbeatIntervalMs = options.heartbeatIntervalMs;
|
|
129
|
+
const accessTokenValue = (_a = options === null || options === void 0 ? void 0 : options.params) === null || _a === void 0 ? void 0 : _a.apikey;
|
|
130
|
+
if (accessTokenValue) {
|
|
131
|
+
this.accessTokenValue = accessTokenValue;
|
|
132
|
+
this.apiKey = accessTokenValue;
|
|
133
|
+
}
|
|
134
|
+
this.reconnectAfterMs = (options === null || options === void 0 ? void 0 : options.reconnectAfterMs)
|
|
135
|
+
? options.reconnectAfterMs
|
|
136
|
+
: (tries) => {
|
|
137
|
+
return [1000, 2000, 5000, 10000][tries - 1] || 10000;
|
|
138
|
+
};
|
|
139
|
+
this.encode = (options === null || options === void 0 ? void 0 : options.encode)
|
|
140
|
+
? options.encode
|
|
141
|
+
: (payload, callback) => {
|
|
142
|
+
return callback(JSON.stringify(payload));
|
|
143
|
+
};
|
|
144
|
+
this.decode = (options === null || options === void 0 ? void 0 : options.decode)
|
|
145
|
+
? options.decode
|
|
146
|
+
: this.serializer.decode.bind(this.serializer);
|
|
147
|
+
this.reconnectTimer = new timer_1.default(async () => {
|
|
148
|
+
this.disconnect();
|
|
149
|
+
this.connect();
|
|
150
|
+
}, this.reconnectAfterMs);
|
|
151
|
+
this.fetch = this._resolveFetch(options === null || options === void 0 ? void 0 : options.fetch);
|
|
152
|
+
if (options === null || options === void 0 ? void 0 : options.worker) {
|
|
153
|
+
if (typeof window !== 'undefined' && !window.Worker) {
|
|
154
|
+
throw new Error('Web Worker is not supported');
|
|
155
|
+
}
|
|
156
|
+
this.worker = (options === null || options === void 0 ? void 0 : options.worker) || false;
|
|
157
|
+
this.workerUrl = options === null || options === void 0 ? void 0 : options.workerUrl;
|
|
158
|
+
}
|
|
159
|
+
this.accessToken = (options === null || options === void 0 ? void 0 : options.accessToken) || null;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Connects the socket, unless already connected.
|
|
163
|
+
*/
|
|
164
|
+
connect() {
|
|
165
|
+
if (this.conn) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (this.transport) {
|
|
169
|
+
this.conn = new this.transport(this.endpointURL(), undefined, {
|
|
170
|
+
headers: this.headers,
|
|
171
|
+
});
|
|
172
|
+
this.setupConnection();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (NATIVE_WEBSOCKET_AVAILABLE) {
|
|
176
|
+
this.conn = new WebSocket(this.endpointURL());
|
|
177
|
+
this.setupConnection();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
|
|
181
|
+
close: () => {
|
|
182
|
+
this.conn = null;
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
Promise.resolve().then(() => __importStar(require('ws'))).then(({ default: WS }) => {
|
|
186
|
+
this.conn = new WS(this.endpointURL(), undefined, {
|
|
187
|
+
headers: this.headers,
|
|
188
|
+
});
|
|
189
|
+
this.setupConnection();
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns the URL of the websocket.
|
|
194
|
+
* @returns string The URL of the websocket.
|
|
195
|
+
*/
|
|
196
|
+
endpointURL() {
|
|
197
|
+
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: constants_1.VSN }));
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Disconnects the socket.
|
|
201
|
+
*
|
|
202
|
+
* @param code A numeric status code to send on disconnect.
|
|
203
|
+
* @param reason A custom reason for the disconnect.
|
|
204
|
+
*/
|
|
205
|
+
disconnect(code, reason) {
|
|
206
|
+
if (this.conn) {
|
|
207
|
+
this.conn.onclose = function () { }; // noop
|
|
208
|
+
if (code) {
|
|
209
|
+
this.conn.close(code, reason !== null && reason !== void 0 ? reason : '');
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
this.conn.close();
|
|
213
|
+
}
|
|
214
|
+
this.conn = null;
|
|
215
|
+
// remove open handles
|
|
216
|
+
this.heartbeatTimer && clearInterval(this.heartbeatTimer);
|
|
217
|
+
this.reconnectTimer.reset();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Returns all created channels
|
|
222
|
+
*/
|
|
223
|
+
getChannels() {
|
|
224
|
+
return this.channels;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Unsubscribes and removes a single channel
|
|
228
|
+
* @param channel A RealtimeChannel instance
|
|
229
|
+
*/
|
|
230
|
+
async removeChannel(channel) {
|
|
231
|
+
const status = await channel.unsubscribe();
|
|
232
|
+
if (this.channels.length === 0) {
|
|
233
|
+
this.disconnect();
|
|
234
|
+
}
|
|
235
|
+
return status;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Unsubscribes and removes all channels
|
|
239
|
+
*/
|
|
240
|
+
async removeAllChannels() {
|
|
241
|
+
const values_1 = await Promise.all(this.channels.map((channel) => channel.unsubscribe()));
|
|
242
|
+
this.disconnect();
|
|
243
|
+
return values_1;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Logs the message.
|
|
247
|
+
*
|
|
248
|
+
* For customized logging, `this.logger` can be overridden.
|
|
249
|
+
*/
|
|
250
|
+
log(kind, msg, data) {
|
|
251
|
+
this.logger(kind, msg, data);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Returns the current state of the socket.
|
|
255
|
+
*/
|
|
256
|
+
connectionState() {
|
|
257
|
+
switch (this.conn && this.conn.readyState) {
|
|
258
|
+
case constants_1.SOCKET_STATES.connecting:
|
|
259
|
+
return constants_1.CONNECTION_STATE.Connecting;
|
|
260
|
+
case constants_1.SOCKET_STATES.open:
|
|
261
|
+
return constants_1.CONNECTION_STATE.Open;
|
|
262
|
+
case constants_1.SOCKET_STATES.closing:
|
|
263
|
+
return constants_1.CONNECTION_STATE.Closing;
|
|
264
|
+
default:
|
|
265
|
+
return constants_1.CONNECTION_STATE.Closed;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Returns `true` is the connection is open.
|
|
270
|
+
*/
|
|
271
|
+
isConnected() {
|
|
272
|
+
return this.connectionState() === constants_1.CONNECTION_STATE.Open;
|
|
273
|
+
}
|
|
274
|
+
channel(topic, params = { config: {} }) {
|
|
275
|
+
const chan = new RealtimeChannel_1.default(`realtime:${topic}`, params, this);
|
|
276
|
+
this.channels.push(chan);
|
|
277
|
+
return chan;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Push out a message if the socket is connected.
|
|
281
|
+
*
|
|
282
|
+
* If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established.
|
|
283
|
+
*/
|
|
284
|
+
push(data) {
|
|
285
|
+
const { topic, event, payload, ref } = data;
|
|
286
|
+
const callback = () => {
|
|
287
|
+
this.encode(data, (result) => {
|
|
288
|
+
var _a;
|
|
289
|
+
(_a = this.conn) === null || _a === void 0 ? void 0 : _a.send(result);
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
this.log('push', `${topic} ${event} (${ref})`, payload);
|
|
293
|
+
if (this.isConnected()) {
|
|
294
|
+
callback();
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
this.sendBuffer.push(callback);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Sets the JWT access token used for channel subscription authorization and Realtime RLS.
|
|
302
|
+
*
|
|
303
|
+
* If param is null it will use the `accessToken` callback function or the token set on the client.
|
|
304
|
+
*
|
|
305
|
+
* On callback used, it will set the value of the token internal to the client.
|
|
306
|
+
*
|
|
307
|
+
* @param token A JWT string to override the token set on the client.
|
|
308
|
+
*/
|
|
309
|
+
async setAuth(token = null) {
|
|
310
|
+
let tokenToSend = token ||
|
|
311
|
+
(this.accessToken && (await this.accessToken())) ||
|
|
312
|
+
this.accessTokenValue;
|
|
313
|
+
if (this.accessTokenValue != tokenToSend) {
|
|
314
|
+
this.accessTokenValue = tokenToSend;
|
|
315
|
+
this.channels.forEach((channel) => {
|
|
316
|
+
tokenToSend &&
|
|
317
|
+
channel.updateJoinPayload({
|
|
318
|
+
access_token: tokenToSend,
|
|
319
|
+
version: this.headers && this.headers['X-Client-Info'],
|
|
320
|
+
});
|
|
321
|
+
if (channel.joinedOnce && channel._isJoined()) {
|
|
322
|
+
channel._push(constants_1.CHANNEL_EVENTS.access_token, {
|
|
323
|
+
access_token: tokenToSend,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Sends a heartbeat message if the socket is connected.
|
|
331
|
+
*/
|
|
332
|
+
async sendHeartbeat() {
|
|
333
|
+
var _a;
|
|
334
|
+
if (!this.isConnected()) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (this.pendingHeartbeatRef) {
|
|
338
|
+
this.pendingHeartbeatRef = null;
|
|
339
|
+
this.log('transport', 'heartbeat timeout. Attempting to re-establish connection');
|
|
340
|
+
(_a = this.conn) === null || _a === void 0 ? void 0 : _a.close(constants_1.WS_CLOSE_NORMAL, 'hearbeat timeout');
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
this.pendingHeartbeatRef = this._makeRef();
|
|
344
|
+
this.push({
|
|
345
|
+
topic: 'phoenix',
|
|
346
|
+
event: 'heartbeat',
|
|
347
|
+
payload: {},
|
|
348
|
+
ref: this.pendingHeartbeatRef,
|
|
349
|
+
});
|
|
350
|
+
await this.setAuth();
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Flushes send buffer
|
|
354
|
+
*/
|
|
355
|
+
flushSendBuffer() {
|
|
356
|
+
if (this.isConnected() && this.sendBuffer.length > 0) {
|
|
357
|
+
this.sendBuffer.forEach((callback) => callback());
|
|
358
|
+
this.sendBuffer = [];
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Return the next message ref, accounting for overflows
|
|
363
|
+
*
|
|
364
|
+
* @internal
|
|
365
|
+
*/
|
|
366
|
+
_makeRef() {
|
|
367
|
+
let newRef = this.ref + 1;
|
|
368
|
+
if (newRef === this.ref) {
|
|
369
|
+
this.ref = 0;
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
this.ref = newRef;
|
|
373
|
+
}
|
|
374
|
+
return this.ref.toString();
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Unsubscribe from channels with the specified topic.
|
|
378
|
+
*
|
|
379
|
+
* @internal
|
|
380
|
+
*/
|
|
381
|
+
_leaveOpenTopic(topic) {
|
|
382
|
+
let dupChannel = this.channels.find((c) => c.topic === topic && (c._isJoined() || c._isJoining()));
|
|
383
|
+
if (dupChannel) {
|
|
384
|
+
this.log('transport', `leaving duplicate topic "${topic}"`);
|
|
385
|
+
dupChannel.unsubscribe();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Removes a subscription from the socket.
|
|
390
|
+
*
|
|
391
|
+
* @param channel An open subscription.
|
|
392
|
+
*
|
|
393
|
+
* @internal
|
|
394
|
+
*/
|
|
395
|
+
_remove(channel) {
|
|
396
|
+
this.channels = this.channels.filter((c) => c._joinRef() !== channel._joinRef());
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Sets up connection handlers.
|
|
400
|
+
*
|
|
401
|
+
* @internal
|
|
402
|
+
*/
|
|
403
|
+
setupConnection() {
|
|
404
|
+
if (this.conn) {
|
|
405
|
+
this.conn.binaryType = 'arraybuffer';
|
|
406
|
+
this.conn.onopen = () => this._onConnOpen();
|
|
407
|
+
this.conn.onerror = (error) => this._onConnError(error);
|
|
408
|
+
this.conn.onmessage = (event) => this._onConnMessage(event);
|
|
409
|
+
this.conn.onclose = (event) => this._onConnClose(event);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
/** @internal */
|
|
413
|
+
_onConnMessage(rawMessage) {
|
|
414
|
+
this.decode(rawMessage.data, (msg) => {
|
|
415
|
+
let { topic, event, payload, ref } = msg;
|
|
416
|
+
if (ref && ref === this.pendingHeartbeatRef) {
|
|
417
|
+
this.pendingHeartbeatRef = null;
|
|
418
|
+
}
|
|
419
|
+
this.log('receive', `${payload.status || ''} ${topic} ${event} ${(ref && '(' + ref + ')') || ''}`, payload);
|
|
420
|
+
this.channels
|
|
421
|
+
.filter((channel) => channel._isMember(topic))
|
|
422
|
+
.forEach((channel) => channel._trigger(event, payload, ref));
|
|
423
|
+
this.stateChangeCallbacks.message.forEach((callback) => callback(msg));
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
/** @internal */
|
|
427
|
+
async _onConnOpen() {
|
|
428
|
+
this.log('transport', `connected to ${this.endpointURL()}`);
|
|
429
|
+
this.flushSendBuffer();
|
|
430
|
+
this.reconnectTimer.reset();
|
|
431
|
+
if (!this.worker) {
|
|
432
|
+
this.heartbeatTimer && clearInterval(this.heartbeatTimer);
|
|
433
|
+
this.heartbeatTimer = setInterval(() => this.sendHeartbeat(), this.heartbeatIntervalMs);
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
if (this.workerUrl) {
|
|
437
|
+
this.log('worker', `starting worker for from ${this.workerUrl}`);
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
this.log('worker', `starting default worker`);
|
|
441
|
+
}
|
|
442
|
+
const objectUrl = this._workerObjectUrl(this.workerUrl);
|
|
443
|
+
this.workerRef = new Worker(objectUrl);
|
|
444
|
+
this.workerRef.onerror = (error) => {
|
|
445
|
+
this.log('worker', 'worker error', error.message);
|
|
446
|
+
this.workerRef.terminate();
|
|
447
|
+
};
|
|
448
|
+
this.workerRef.onmessage = (event) => {
|
|
449
|
+
if (event.data.event === 'keepAlive') {
|
|
450
|
+
this.sendHeartbeat();
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
this.workerRef.postMessage({
|
|
454
|
+
event: 'start',
|
|
455
|
+
interval: this.heartbeatIntervalMs,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
this.stateChangeCallbacks.open.forEach((callback) => callback());
|
|
459
|
+
}
|
|
460
|
+
/** @internal */
|
|
461
|
+
_onConnClose(event) {
|
|
462
|
+
this.log('transport', 'close', event);
|
|
463
|
+
this._triggerChanError();
|
|
464
|
+
this.heartbeatTimer && clearInterval(this.heartbeatTimer);
|
|
465
|
+
this.reconnectTimer.scheduleTimeout();
|
|
466
|
+
this.stateChangeCallbacks.close.forEach((callback) => callback(event));
|
|
467
|
+
}
|
|
468
|
+
/** @internal */
|
|
469
|
+
_onConnError(error) {
|
|
470
|
+
this.log('transport', error.message);
|
|
471
|
+
this._triggerChanError();
|
|
472
|
+
this.stateChangeCallbacks.error.forEach((callback) => callback(error));
|
|
473
|
+
}
|
|
474
|
+
/** @internal */
|
|
475
|
+
_triggerChanError() {
|
|
476
|
+
this.channels.forEach((channel) => channel._trigger(constants_1.CHANNEL_EVENTS.error));
|
|
477
|
+
}
|
|
478
|
+
/** @internal */
|
|
479
|
+
_appendParams(url, params) {
|
|
480
|
+
if (Object.keys(params).length === 0) {
|
|
481
|
+
return url;
|
|
482
|
+
}
|
|
483
|
+
const prefix = url.match(/\?/) ? '&' : '?';
|
|
484
|
+
const query = new URLSearchParams(params);
|
|
485
|
+
return `${url}${prefix}${query}`;
|
|
486
|
+
}
|
|
487
|
+
_workerObjectUrl(url) {
|
|
488
|
+
let result_url;
|
|
489
|
+
if (url) {
|
|
490
|
+
result_url = url;
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
const blob = new Blob([WORKER_SCRIPT], { type: 'application/javascript' });
|
|
494
|
+
result_url = URL.createObjectURL(blob);
|
|
495
|
+
}
|
|
496
|
+
return result_url;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
exports.default = RealtimeClient;
|
|
500
|
+
class WSWebSocketDummy {
|
|
501
|
+
constructor(address, _protocols, options) {
|
|
502
|
+
this.binaryType = 'arraybuffer';
|
|
503
|
+
this.onclose = () => { };
|
|
504
|
+
this.onerror = () => { };
|
|
505
|
+
this.onmessage = () => { };
|
|
506
|
+
this.onopen = () => { };
|
|
507
|
+
this.readyState = constants_1.SOCKET_STATES.connecting;
|
|
508
|
+
this.send = () => { };
|
|
509
|
+
this.url = null;
|
|
510
|
+
this.url = address;
|
|
511
|
+
this.close = options.close;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
//# sourceMappingURL=RealtimeClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RealtimeClient.js","sourceRoot":"","sources":["../../src/RealtimeClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,iDAU0B;AAE1B,oEAA2C;AAC3C,0DAAiC;AAEjC,uDAAsD;AACtD,wEAA+C;AAuB/C,MAAM,IAAI,GAAG,GAAG,EAAE,GAAE,CAAC,CAAA;AAqCrB,MAAM,0BAA0B,GAAG,OAAO,SAAS,KAAK,WAAW,CAAA;AACnE,MAAM,aAAa,GAAG;;;;;MAKhB,CAAA;AACN,MAAqB,cAAc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AC1C,MAAM,KAAK,GAAG,IAAI,eAAe,CAAC,MAAM,CAAC,CAAA;QAEzC,OAAO,GAAG,GAAG,GAAG,MAAM,GAAG,KAAK,EAAE,CAAA;IAClC,CAAC;IAEO,gBAAgB,CAAC,GAAuB;QAC9C,IAAI,UAAkB,CAAA;QACtB,IAAI,GAAG,EAAE;YACP,UAAU,GAAG,GAAG,CAAA;SACjB;aAAM;YACL,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,aAAa,CAAC,EAAE,EAAE,IAAI,EAAE,wBAAwB,EAAE,CAAC,CAAA;YAC1E,UAAU,GAAG,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAA;SACvC;QACD,OAAO,UAAU,CAAA;IACnB,CAAC;CACF;AArhBD,iCAqhBC;AAED,MAAM,gBAAgB;IAWpB,YACE,OAAe,EACf,UAAqB,EACrB,OAA4B;QAb9B,eAAU,GAAW,aAAa,CAAA;QAElC,YAAO,GAAa,GAAG,EAAE,GAAE,CAAC,CAAA;QAC5B,YAAO,GAAa,GAAG,EAAE,GAAE,CAAC,CAAA;QAC5B,cAAS,GAAa,GAAG,EAAE,GAAE,CAAC,CAAA;QAC9B,WAAM,GAAa,GAAG,EAAE,GAAE,CAAC,CAAA;QAC3B,eAAU,GAAW,yBAAa,CAAC,UAAU,CAAA;QAC7C,SAAI,GAAa,GAAG,EAAE,GAAE,CAAC,CAAA;QACzB,QAAG,GAAwB,IAAI,CAAA;QAO7B,IAAI,CAAC,GAAG,GAAG,OAAO,CAAA;QAClB,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;IAC5B,CAAC;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { PresenceOpts, PresenceOnJoinCallback, PresenceOnLeaveCallback } from 'phoenix';
|
|
2
|
+
import type RealtimeChannel from './RealtimeChannel';
|
|
3
|
+
type Presence<T extends {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
} = {}> = {
|
|
6
|
+
presence_ref: string;
|
|
7
|
+
} & T;
|
|
8
|
+
export type RealtimePresenceState<T extends {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
} = {}> = {
|
|
11
|
+
[key: string]: Presence<T>[];
|
|
12
|
+
};
|
|
13
|
+
export type RealtimePresenceJoinPayload<T extends {
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}> = {
|
|
16
|
+
event: `${REALTIME_PRESENCE_LISTEN_EVENTS.JOIN}`;
|
|
17
|
+
key: string;
|
|
18
|
+
currentPresences: Presence<T>[];
|
|
19
|
+
newPresences: Presence<T>[];
|
|
20
|
+
};
|
|
21
|
+
export type RealtimePresenceLeavePayload<T extends {
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}> = {
|
|
24
|
+
event: `${REALTIME_PRESENCE_LISTEN_EVENTS.LEAVE}`;
|
|
25
|
+
key: string;
|
|
26
|
+
currentPresences: Presence<T>[];
|
|
27
|
+
leftPresences: Presence<T>[];
|
|
28
|
+
};
|
|
29
|
+
export declare enum REALTIME_PRESENCE_LISTEN_EVENTS {
|
|
30
|
+
SYNC = "sync",
|
|
31
|
+
JOIN = "join",
|
|
32
|
+
LEAVE = "leave"
|
|
33
|
+
}
|
|
34
|
+
type RawPresenceState = {
|
|
35
|
+
[key: string]: {
|
|
36
|
+
metas: {
|
|
37
|
+
phx_ref?: string;
|
|
38
|
+
phx_ref_prev?: string;
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
}[];
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
type RawPresenceDiff = {
|
|
44
|
+
joins: RawPresenceState;
|
|
45
|
+
leaves: RawPresenceState;
|
|
46
|
+
};
|
|
47
|
+
export default class RealtimePresence {
|
|
48
|
+
channel: RealtimeChannel;
|
|
49
|
+
state: RealtimePresenceState;
|
|
50
|
+
pendingDiffs: RawPresenceDiff[];
|
|
51
|
+
joinRef: string | null;
|
|
52
|
+
caller: {
|
|
53
|
+
onJoin: PresenceOnJoinCallback;
|
|
54
|
+
onLeave: PresenceOnLeaveCallback;
|
|
55
|
+
onSync: () => void;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Initializes the Presence.
|
|
59
|
+
*
|
|
60
|
+
* @param channel - The RealtimeChannel
|
|
61
|
+
* @param opts - The options,
|
|
62
|
+
* for example `{events: {state: 'state', diff: 'diff'}}`
|
|
63
|
+
*/
|
|
64
|
+
constructor(channel: RealtimeChannel, opts?: PresenceOpts);
|
|
65
|
+
}
|
|
66
|
+
export {};
|
|
67
|
+
//# sourceMappingURL=RealtimePresence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RealtimePresence.d.ts","sourceRoot":"","sources":["../../src/RealtimePresence.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,YAAY,EACZ,sBAAsB,EACtB,uBAAuB,EACxB,MAAM,SAAS,CAAA;AAChB,OAAO,KAAK,eAAe,MAAM,mBAAmB,CAAA;AAEpD,KAAK,QAAQ,CAAC,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,GAAG,EAAE,IAAI;IACrD,YAAY,EAAE,MAAM,CAAA;CACrB,GAAG,CAAC,CAAA;AAEL,MAAM,MAAM,qBAAqB,CAAC,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,GAAG,EAAE,IAAI;IACzE,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,IAAI;IAC1E,KAAK,EAAE,GAAG,+BAA+B,CAAC,IAAI,EAAE,CAAA;IAChD,GAAG,EAAE,MAAM,CAAA;IACX,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAA;IAC/B,YAAY,EAAE,QAAQ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