forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
201 lines (158 loc) · 6.29 KB
/
Copy pathconfig.js
File metadata and controls
201 lines (158 loc) · 6.29 KB
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
/* @flow */
/* global location */
import uuidGenerator from 'uuid';
import { InternalSetupStruct, DatabaseInterface } from '../flow_interfaces';
import packageJSON from '../../../package.json';
type ConfigConstructArgs = {
setup: InternalSetupStruct,
db: DatabaseInterface
}
export default class {
_db: DatabaseInterface;
subscribeKey: string;
publishKey: string;
secretKey: string;
cipherKey: string;
authKey: string;
UUID: string;
/*
if _useInstanceId is true, this instanceId will be added to all requests
*/
instanceId: string;
/*
keep track of the SDK family for identifier generator
*/
sdkFamily: string;
/*
If the SDK is operated by a partner, allow a custom pnsdk item for them.
*/
partnerId: string;
/*
filter expression to pass along when subscribing.
*/
filterExpression: string;
/*
configuration to supress leave events; when a presence leave is performed
this configuration will disallow the leave event from happening
*/
suppressLeaveEvents: boolean;
/*
use SSL for http requests?
*/
secure: boolean;
// Custom optional origin.
origin: string;
// log verbosity: true to output lots of info
logVerbosity: boolean;
// if instanceId config is true, the SDK will pass the unique instance identifier to the server as instanceId=<UUID>
useInstanceId: boolean;
// if requestId config is true, the SDK will pass a unique request identifier with each request as request_id=<UUID>
useRequestId: boolean;
// alert when a heartbeat works out.
announceSuccessfulHeartbeats: boolean;
announceFailedHeartbeats: boolean;
/*
how long the server will wait before declaring that the client is gone.
*/
_presenceTimeout: number;
/*
how often (in seconds) the client should announce its presence to server
*/
_heartbeatInterval: number;
/*
how long to wait for the server when running the subscribe loop
*/
_subscribeRequestTimeout: number;
/*
how long to wait for the server when making transactional requests
*/
_transactionalRequestTimeout: number;
/*
use send beacon API when unsubscribing.
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
*/
_useSendBeacon: boolean;
/*
if set, the SDK will alert if more messages arrive in one subscribe than the theshold
*/
requestMessageCountThreshold: number;
constructor({ setup, db } : ConfigConstructArgs) {
this._db = db;
this.instanceId = uuidGenerator.v4();
this.secretKey = setup.secretKey;
this.subscribeKey = setup.subscribeKey;
this.publishKey = setup.publishKey;
this.sdkFamily = setup.sdkFamily;
this.partnerId = setup.partnerId;
this.setAuthKey(setup.authKey);
this.setCipherKey(setup.cipherKey);
this.setFilterExpression(setup.filterExpression);
this.origin = setup.origin || 'pubsub.pubnub.com';
this.secure = setup.ssl || 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
this.setPresenceTimeout(setup.presenceTimeout || 300);
if (setup.heartbeatInterval) {
this.setHeartbeatInterval(setup.heartbeatInterval);
}
this.setUUID(this._decideUUID(setup.uuid)); // UUID decision depends on subKey.
}
// exposed setters
getAuthKey(): string { return this.authKey; }
setAuthKey(val: string): this { this.authKey = val; return this; }
setCipherKey(val: string): this { this.cipherKey = val; return this; }
getUUID(): string { return this.UUID; }
setUUID(val: string): this {
if (this._db && this._db.set) this._db.set(this.subscribeKey + 'uuid', val);
this.UUID = val;
return this;
}
getFilterExpression(): string { return this.filterExpression; }
setFilterExpression(val: string): this { this.filterExpression = val; return this; }
getPresenceTimeout(): number { return this._presenceTimeout; }
setPresenceTimeout(val: number): this {
this._presenceTimeout = val;
this.setHeartbeatInterval((this._presenceTimeout / 2) - 1);
return this;
}
getHeartbeatInterval(): number { return this._heartbeatInterval; }
setHeartbeatInterval(val: number): this { this._heartbeatInterval = val; return this; }
// deprecated setters.
getSubscribeTimeout(): number { return this._subscribeRequestTimeout; }
setSubscribeTimeout(val: number): this { this._subscribeRequestTimeout = val; return this; }
getTransactionTimeout(): number { return this._transactionalRequestTimeout; }
setTransactionTimeout(val: number): this { this._transactionalRequestTimeout = val; return this; }
isSendBeaconEnabled(): boolean { return this._useSendBeacon; }
setSendBeaconConfig(val: boolean): this { this._useSendBeacon = val; return this; }
getVersion(): string {
return packageJSON.version;
}
_decideUUID(providedUUID: string): string {
// if the uuid was provided by setup, use this UUID.
if (providedUUID) {
return providedUUID;
}
// if the database module is enabled and we have something saved, use this.
if (this._db && this._db.get && this._db.get(this.subscribeKey + 'uuid')) {
return this._db.get(this.subscribeKey + 'uuid');
}
// randomize the UUID and push to storage
return uuidGenerator.v4();
}
}