-
Notifications
You must be signed in to change notification settings - Fork 400
/
config.js
352 lines (272 loc) · 8.9 KB
/
config.js
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
/* */
/* global location */
import uuidGenerator from './uuid';
const PRESENCE_TIMEOUT_MINIMUM = 20;
const PRESENCE_TIMEOUT_DEFAULT = 300;
const makeDefaultOrigins = () => Array.from({ length: 20 }, (_, i) => `ps${i + 1}.pndsn.com`);
export default class {
subscribeKey;
publishKey;
secretKey;
cipherKey;
authKey;
UUID;
proxy;
/*
if _useInstanceId is true, this instanceId will be added to all requests
*/
instanceId;
/*
If the SDK is running as part of another SDK built atop of it, allow a custom pnsdk with name and version.
*/
sdkName;
/*
keep track of the SDK family for identifier generator
*/
sdkFamily;
/*
If the SDK is operated by a partner, allow a custom pnsdk item for them.
*/
partnerId;
/*
filter expression to pass along when subscribing.
*/
filterExpression;
/*
configuration to supress leave events; when a presence leave is performed
this configuration will disallow the leave event from happening
*/
suppressLeaveEvents;
/*
use SSL for http requests?
*/
secure;
// Custom optional origin.
origin;
// log verbosity: true to output lots of info
logVerbosity;
// if instanceId config is true, the SDK will pass the unique instance identifier to the server as instanceId=<UUID>
useInstanceId;
// if requestId config is true, the SDK will pass a unique request identifier with each request as request_id=<UUID>
useRequestId;
// use connection keep-alive for http requests
keepAlive;
keepAliveSettings;
// if autoNetworkDetection config is true, the SDK will emit NetworkUp and NetworkDown
// when there changes in the networking
autoNetworkDetection;
// alert when a heartbeat works out.
announceSuccessfulHeartbeats;
announceFailedHeartbeats;
/*
how long the server will wait before declaring that the client is gone.
*/
_presenceTimeout;
/*
how often (in seconds) the client should announce its presence to server
*/
_heartbeatInterval;
/*
how long to wait for the server when running the subscribe loop
*/
_subscribeRequestTimeout;
/*
how long to wait for the server when making transactional requests
*/
_transactionalRequestTimeout;
/*
use send beacon API when unsubscribing.
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
*/
_useSendBeacon;
/*
allow frameworks to append to the PNSDK parameter
the key should be an identifier for the specific framework to prevent duplicates
*/
_PNSDKSuffix;
/*
if set, the SDK will alert if more messages arrive in one subscribe than the theshold
*/
requestMessageCountThreshold;
/*
Restore subscription list on disconnection.
*/
restore;
/*
support for client deduping
*/
dedupeOnSubscribe;
maximumCacheSize;
/*
support customp encryption and decryption functions.
*/
customEncrypt; // function to support custome encryption of messages
customDecrypt; // function used to decrypt old version messages
// File Upload
// How many times the publish-file should be retried before giving up
fileUploadPublishRetryLimit;
useRandomIVs;
enableSubscribeBeta;
constructor({ setup }) {
this._PNSDKSuffix = {};
this.instanceId = `pn-${uuidGenerator.createUUID()}`;
this.secretKey = setup.secretKey || setup.secret_key;
this.subscribeKey = setup.subscribeKey || setup.subscribe_key;
this.publishKey = setup.publishKey || setup.publish_key;
this.sdkName = setup.sdkName;
this.sdkFamily = setup.sdkFamily;
this.partnerId = setup.partnerId;
this.setAuthKey(setup.authKey);
this.setCipherKey(setup.cipherKey);
this.setFilterExpression(setup.filterExpression);
if (typeof setup.origin !== 'string' && !Array.isArray(setup.origin) && setup.origin !== undefined) {
throw new Error('Origin must be either undefined, a string or a list of strings.');
}
this.origin = setup.origin || makeDefaultOrigins();
this.secure = setup.ssl || false;
this.restore = setup.restore || false;
this.proxy = setup.proxy;
this.keepAlive = setup.keepAlive;
this.keepAliveSettings = setup.keepAliveSettings;
this.autoNetworkDetection = setup.autoNetworkDetection || false;
this.dedupeOnSubscribe = setup.dedupeOnSubscribe || false;
this.maximumCacheSize = setup.maximumCacheSize || 100;
this.customEncrypt = setup.customEncrypt;
this.customDecrypt = setup.customDecrypt;
this.fileUploadPublishRetryLimit = setup.fileUploadPublishRetryLimit ?? 5;
this.useRandomIVs = setup.useRandomIVs ?? true;
// flag for beta subscribe feature enablement
this.enableSubscribeBeta = setup.enableSubscribeBeta ?? false;
// if location config exist and we are in https, force secure to true.
if (typeof location !== 'undefined' && location.protocol === 'https:') {
this.secure = true;
}
this.logVerbosity = setup.logVerbosity || false;
this.suppressLeaveEvents = setup.suppressLeaveEvents || false;
this.announceFailedHeartbeats = setup.announceFailedHeartbeats || true;
this.announceSuccessfulHeartbeats = setup.announceSuccessfulHeartbeats || false;
this.useInstanceId = setup.useInstanceId || false;
this.useRequestId = setup.useRequestId || false;
this.requestMessageCountThreshold = setup.requestMessageCountThreshold;
// set timeout to how long a transaction request will wait for the server (default 15 seconds)
this.setTransactionTimeout(setup.transactionalRequestTimeout || 15 * 1000);
// set timeout to how long a subscribe event loop will run (default 310 seconds)
this.setSubscribeTimeout(setup.subscribeRequestTimeout || 310 * 1000);
// set config on beacon (https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon) usage
this.setSendBeaconConfig(setup.useSendBeacon || true);
// how long the SDK will report the client to be alive before issuing a timeout
if (setup.presenceTimeout) {
this.setPresenceTimeout(setup.presenceTimeout);
} else {
this._presenceTimeout = PRESENCE_TIMEOUT_DEFAULT;
}
if (setup.heartbeatInterval != null) {
this.setHeartbeatInterval(setup.heartbeatInterval);
}
if (typeof setup.userId === 'string') {
if (typeof setup.uuid === 'string') {
throw new Error('Only one of the following configuration options has to be provided: `uuid` or `userId`');
}
this.setUserId(setup.userId);
} else {
if (typeof setup.uuid !== 'string') {
throw new Error('One of the following configuration options has to be provided: `uuid` or `userId`');
}
this.setUUID(setup.uuid);
}
}
// exposed setters
getAuthKey() {
return this.authKey;
}
setAuthKey(val) {
this.authKey = val;
return this;
}
setCipherKey(val) {
this.cipherKey = val;
return this;
}
getUUID() {
return this.UUID;
}
setUUID(val) {
if (!val || typeof val !== 'string' || val.trim().length === 0) {
throw new Error('Missing uuid parameter. Provide a valid string uuid');
}
this.UUID = val;
return this;
}
getUserId() {
return this.UUID;
}
setUserId(value) {
if (!value || typeof value !== 'string' || value.trim().length === 0) {
throw new Error('Missing or invalid userId parameter. Provide a valid string userId');
}
this.UUID = value;
return this;
}
getFilterExpression() {
return this.filterExpression;
}
setFilterExpression(val) {
this.filterExpression = val;
return this;
}
getPresenceTimeout() {
return this._presenceTimeout;
}
setPresenceTimeout(val) {
if (val >= PRESENCE_TIMEOUT_MINIMUM) {
this._presenceTimeout = val;
} else {
this._presenceTimeout = PRESENCE_TIMEOUT_MINIMUM;
// eslint-disable-next-line no-console
console.log('WARNING: Presence timeout is less than the minimum. Using minimum value: ', this._presenceTimeout);
}
this.setHeartbeatInterval(this._presenceTimeout / 2 - 1);
return this;
}
setProxy(proxy) {
this.proxy = proxy;
}
getHeartbeatInterval() {
return this._heartbeatInterval;
}
setHeartbeatInterval(val) {
this._heartbeatInterval = val;
return this;
}
// deprecated setters.
getSubscribeTimeout() {
return this._subscribeRequestTimeout;
}
setSubscribeTimeout(val) {
this._subscribeRequestTimeout = val;
return this;
}
getTransactionTimeout() {
return this._transactionalRequestTimeout;
}
setTransactionTimeout(val) {
this._transactionalRequestTimeout = val;
return this;
}
isSendBeaconEnabled() {
return this._useSendBeacon;
}
setSendBeaconConfig(val) {
this._useSendBeacon = val;
return this;
}
getVersion() {
return '7.2.1';
}
_addPnsdkSuffix(name, suffix) {
this._PNSDKSuffix[name] = suffix;
}
_getPnsdkSuffix(separator) {
return Object.keys(this._PNSDKSuffix).reduce((result, key) => result + separator + this._PNSDKSuffix[key], '');
}
}