-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
pusher.ts
433 lines (369 loc) · 14.1 KB
/
pusher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import isObject from 'lodash/isObject';
import type {Channel, ChannelAuthorizerGenerator, Options} from 'pusher-js/with-encryption';
import {InteractionManager} from 'react-native';
import Onyx from 'react-native-onyx';
import type {LiteralUnion, ValueOf} from 'type-fest';
import Log from '@libs/Log';
import type CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxUpdatesFromServer, ReportUserIsTyping} from '@src/types/onyx';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import TYPE from './EventType';
import Pusher from './library';
import type {SocketEventName} from './library/types';
type States = {
previous: string;
current: string;
};
type Args = {
appKey: string;
cluster: string;
authEndpoint: string;
};
type UserIsTypingEvent = ReportUserIsTyping & {
userLogin?: string;
};
type UserIsLeavingRoomEvent = Record<string, boolean> & {
userLogin?: string;
};
type PusherEventMap = {
[TYPE.USER_IS_TYPING]: UserIsTypingEvent;
[TYPE.USER_IS_LEAVING_ROOM]: UserIsLeavingRoomEvent;
};
type EventData<EventName extends string> = {chunk?: string; id?: string; index?: number; final?: boolean} & (EventName extends keyof PusherEventMap
? PusherEventMap[EventName]
: OnyxUpdatesFromServer);
type EventCallbackError = {type: ValueOf<typeof CONST.ERROR>; data: {code: number}};
type ChunkedDataEvents = {chunks: unknown[]; receivedFinal: boolean};
type SocketEventCallback = (eventName: SocketEventName, data?: States | EventCallbackError) => void;
type PusherWithAuthParams = InstanceType<typeof Pusher> & {
config: {
auth?: {
params?: unknown;
};
};
};
type PusherEventName = LiteralUnion<DeepValueOf<typeof TYPE>, string>;
type PusherSubscribtionErrorData = {type?: string; error?: string; status?: string};
let shouldForceOffline = false;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (network) => {
if (!network) {
return;
}
shouldForceOffline = !!network.shouldForceOffline;
},
});
let socket: PusherWithAuthParams | null;
let pusherSocketID = '';
const socketEventCallbacks: SocketEventCallback[] = [];
let customAuthorizer: ChannelAuthorizerGenerator;
const eventsBoundToChannels = new Map<Channel, Set<PusherEventName>>();
/**
* Trigger each of the socket event callbacks with the event information
*/
function callSocketEventCallbacks(eventName: SocketEventName, data?: EventCallbackError | States) {
socketEventCallbacks.forEach((cb) => cb(eventName, data));
}
/**
* Initialize our pusher lib
* @returns resolves when Pusher has connected
*/
function init(args: Args, params?: unknown): Promise<void> {
return new Promise((resolve) => {
if (socket) {
resolve();
return;
}
// Use this for debugging
// Pusher.log = (message) => {
// if (window.console && window.console.log) {
// window.console.log(message);
// }
// };
const options: Options = {
cluster: args.cluster,
authEndpoint: args.authEndpoint,
};
if (customAuthorizer) {
options.authorizer = customAuthorizer;
}
socket = new Pusher(args.appKey, options);
// If we want to pass params in our requests to api.php we'll need to add it to socket.config.auth.params
// as per the documentation
// (https://pusher.com/docs/channels/using_channels/connection#channels-options-parameter).
// Any param mentioned here will show up in $_REQUEST when we call "AuthenticatePusher". Params passed here need
// to pass our inputRules to show up in the request.
if (params) {
socket.config.auth = {};
socket.config.auth.params = params;
}
// Listen for connection errors and log them
socket?.connection.bind('error', (error: EventCallbackError) => {
callSocketEventCallbacks('error', error);
});
socket?.connection.bind('connected', () => {
pusherSocketID = socket?.connection.socket_id ?? '';
callSocketEventCallbacks('connected');
resolve();
});
socket?.connection.bind('disconnected', () => {
callSocketEventCallbacks('disconnected');
});
socket?.connection.bind('state_change', (states: States) => {
callSocketEventCallbacks('state_change', states);
});
});
}
/**
* Returns a Pusher channel for a channel name
*/
function getChannel(channelName: string): Channel | undefined {
if (!socket) {
return;
}
return socket.channel(channelName);
}
/**
* Binds an event callback to a channel + eventName
*/
function bindEventToChannel<EventName extends PusherEventName>(channel: Channel | undefined, eventName: EventName, eventCallback: (data: EventData<EventName>) => void = () => {}) {
if (!eventName || !channel) {
return;
}
const chunkedDataEvents: Record<string, ChunkedDataEvents> = {};
const callback = (eventData: EventData<EventName>) => {
if (shouldForceOffline) {
Log.info('[Pusher] Ignoring a Push event because shouldForceOffline = true');
return;
}
let data: EventData<EventName>;
try {
data = isObject(eventData) ? eventData : (JSON.parse(eventData) as EventData<EventName>);
} catch (err) {
Log.alert('[Pusher] Unable to parse single JSON event data from Pusher', {error: err, eventData});
return;
}
if (data.id === undefined || data.chunk === undefined || data.final === undefined) {
eventCallback(data);
return;
}
// If we are chunking the requests, we need to construct a rolling list of all packets that have come through
// Pusher. If we've completed one of these full packets, we'll combine the data and act on the event that it's
// assigned to.
// If we haven't seen this eventID yet, initialize it into our rolling list of packets.
if (!chunkedDataEvents[data.id]) {
chunkedDataEvents[data.id] = {chunks: [], receivedFinal: false};
}
// Add it to the rolling list.
const chunkedEvent = chunkedDataEvents[data.id];
if (data.index !== undefined) {
chunkedEvent.chunks[data.index] = data.chunk;
}
// If this is the last packet, mark that we've hit the end.
if (data.final) {
chunkedEvent.receivedFinal = true;
}
// Only call the event callback if we've received the last packet and we don't have any holes in the complete
// packet.
if (chunkedEvent.receivedFinal && chunkedEvent.chunks.length === Object.keys(chunkedEvent.chunks).length) {
try {
eventCallback(JSON.parse(chunkedEvent.chunks.join('')) as EventData<EventName>);
} catch (err) {
Log.alert('[Pusher] Unable to parse chunked JSON response from Pusher', {
error: err,
eventData: chunkedEvent.chunks.join(''),
});
// Using console.error is helpful here because it will print a usable stack trace to the console to debug where the error comes from
console.error(err);
}
delete chunkedDataEvents[data.id];
}
};
channel.bind(eventName, callback);
if (!eventsBoundToChannels.has(channel)) {
eventsBoundToChannels.set(channel, new Set());
}
eventsBoundToChannels.get(channel)?.add(eventName);
}
/**
* Subscribe to a channel and an event
* @param [onResubscribe] Callback to be called when reconnection happen
*/
function subscribe<EventName extends PusherEventName>(
channelName: string,
eventName: EventName,
eventCallback: (data: EventData<EventName>) => void = () => {},
onResubscribe = () => {},
): Promise<void> {
return new Promise((resolve, reject) => {
InteractionManager.runAfterInteractions(() => {
// We cannot call subscribe() before init(). Prevent any attempt to do this on dev.
if (!socket) {
throw new Error(`[Pusher] instance not found. Pusher.subscribe()
most likely has been called before Pusher.init()`);
}
Log.info('[Pusher] Attempting to subscribe to channel', false, {channelName, eventName});
let channel = getChannel(channelName);
if (!channel?.subscribed) {
channel = socket.subscribe(channelName);
let isBound = false;
channel.bind('pusher:subscription_succeeded', () => {
// Check so that we do not bind another event with each reconnect attempt
if (!isBound) {
bindEventToChannel(channel, eventName, eventCallback);
resolve();
isBound = true;
return;
}
// When subscribing for the first time we register a success callback that can be
// called multiple times when the subscription succeeds again in the future
// e.g. as a result of Pusher disconnecting and reconnecting. This callback does
// not fire on the first subscription_succeeded event.
onResubscribe();
});
channel.bind('pusher:subscription_error', (data: PusherSubscribtionErrorData = {}) => {
const {type, error, status} = data;
Log.hmmm('[Pusher] Issue authenticating with Pusher during subscribe attempt.', {
channelName,
status,
type,
error,
});
reject(error);
});
} else {
bindEventToChannel(channel, eventName, eventCallback);
resolve();
}
});
});
}
/**
* Unsubscribe from a channel and optionally a specific event
*/
function unsubscribe(channelName: string, eventName: PusherEventName = '') {
const channel = getChannel(channelName);
if (!channel) {
Log.hmmm('[Pusher] Attempted to unsubscribe or unbind from a channel, but Pusher-JS has no knowledge of it', {channelName, eventName});
return;
}
if (eventName) {
Log.info('[Pusher] Unbinding event', false, {eventName, channelName});
channel.unbind(eventName);
eventsBoundToChannels.get(channel)?.delete(eventName);
if (eventsBoundToChannels.get(channel)?.size === 0) {
Log.info(`[Pusher] After unbinding ${eventName} from channel ${channelName}, no other events were bound to that channel. Unsubscribing...`, false);
eventsBoundToChannels.delete(channel);
socket?.unsubscribe(channelName);
}
} else {
if (!channel.subscribed) {
Log.info('Pusher] Attempted to unsubscribe from channel, but we are not subscribed to begin with', false, {channelName});
return;
}
Log.info('[Pusher] Unsubscribing from channel', false, {channelName});
channel.unbind();
socket?.unsubscribe(channelName);
}
}
/**
* Are we already in the process of subscribing to this channel?
*/
function isAlreadySubscribing(channelName: string): boolean {
if (!socket) {
return false;
}
const channel = getChannel(channelName);
return channel ? channel.subscriptionPending : false;
}
/**
* Are we already subscribed to this channel?
*/
function isSubscribed(channelName: string): boolean {
if (!socket) {
return false;
}
const channel = getChannel(channelName);
return channel ? channel.subscribed : false;
}
/**
* Sends an event over a specific event/channel in pusher.
*/
function sendEvent<EventName extends PusherEventName>(channelName: string, eventName: EventName, payload: EventData<EventName>) {
// Check to see if we are subscribed to this channel before sending the event. Sending client events over channels
// we are not subscribed too will throw errors and cause reconnection attempts. Subscriptions are not instant and
// can happen later than we expect.
if (!isSubscribed(channelName)) {
return;
}
if (shouldForceOffline) {
Log.info('[Pusher] Ignoring a Send event because shouldForceOffline = true');
return;
}
socket?.send_event(eventName, payload, channelName);
}
/**
* Register a method that will be triggered when a socket event happens (like disconnecting)
*/
function registerSocketEventCallback(cb: SocketEventCallback) {
socketEventCallbacks.push(cb);
}
/**
* A custom authorizer allows us to take a more fine-grained approach to
* authenticating Pusher. e.g. we can handle failed attempts to authorize
* with an expired authToken and retry the attempt.
*/
function registerCustomAuthorizer(authorizer: ChannelAuthorizerGenerator) {
customAuthorizer = authorizer;
}
/**
* Disconnect from Pusher
*/
function disconnect() {
if (!socket) {
Log.info('[Pusher] Attempting to disconnect from Pusher before initialisation has occurred, ignoring.');
return;
}
socket.disconnect();
socket = null;
pusherSocketID = '';
}
/**
* Disconnect and Re-Connect Pusher
*/
function reconnect() {
if (!socket) {
Log.info('[Pusher] Unable to reconnect since Pusher instance does not yet exist.');
return;
}
Log.info('[Pusher] Reconnecting to Pusher');
socket.disconnect();
socket.connect();
}
function getPusherSocketID(): string {
return pusherSocketID;
}
if (window) {
/**
* Pusher socket for debugging purposes
*/
window.getPusherInstance = () => socket;
}
export {
init,
subscribe,
unsubscribe,
getChannel,
isSubscribed,
isAlreadySubscribing,
sendEvent,
disconnect,
reconnect,
registerSocketEventCallback,
registerCustomAuthorizer,
TYPE,
getPusherSocketID,
};
export type {EventCallbackError, States, UserIsTypingEvent, UserIsLeavingRoomEvent};