-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebsocketOptions.js
More file actions
284 lines (241 loc) · 5.29 KB
/
Copy pathwebsocketOptions.js
File metadata and controls
284 lines (241 loc) · 5.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
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
export const HTTPSPort = 443,
HTTPPort = 80
export const DefaultTimeout = 7000,
LongTimeout = DefaultTimeout * 2
export class Options {
_host
_port
_secure
_timeout
_longpoll = false
_protocols = []
_customWS
_endpoints = ['ws', 'longpoll']
_version = `v2.7.2`
_maxConnectionDelay = 10000
_minReconnectionDelay = 1000 + Math.random() * 4000
_reconnectionDelayGrowFactor = 1.3
_minUptime = 5000
_maxRetries = 10
_maxEnqueuedMessages = Infinity
_startClosed = false
_debug = !!process.env.DEBUG
/**
* @param {string} host
* @param {?number} timeout
* @param {?boolean} secure
* @param {?number} port
* @param {?boolean} longpoll
* @constructor
*/
constructor(
host,
timeout = DefaultTimeout,
secure = true,
port = HTTPSPort,
longpoll = false,
) {
this._host = host
this._port = port
this._secure = secure
this._timeout = timeout
this._longpoll = longpoll
}
/**
* @return {boolean}
* @throws {Error}
*/
check() {
if (this._timeout > LongTimeout)
throw new Error(`timeout less than LongTimeout`)
// eslint-disable-next-line
try {
new URL(this.url)
return true
} catch (e) {
throw e
}
}
/**
* @return {boolean}
*/
get isLongPool() {
// return this._longpoll
return false
}
/**
* @param {boolean} debug
* @return {Options}
*/
setDebug(debug) {
this._debug = debug
return this
}
/**
* @return {boolean}
*/
get debug() {
return this._debug
}
/**
* @param {?string} wsEndpoint
* @param {?string} longpollEndpoint
* @throws {Error}
*/
setEndpoints(wsEndpoint = 'ws', longpollEndpoint = 'longpool') {
if (typeof wsEndpoint !== 'string' || typeof longpollEndpoint !== 'string')
throw new TypeError('invalid endpoints')
this._endpoints = [wsEndpoint, longpollEndpoint]
return this
}
/**
* @return {Array<string>}
*/
get endpoints() {
return this._endpoints
}
/**
* @param {number} delay
* @return {Options}
*/
setMaxConnectionDelay(delay) {
this._maxConnectionDelay = delay
return this
}
/**
* @return {number}
*/
get maxConnectionDelay() {
return this._maxConnectionDelay
}
/**
* @param {number} delay
* @return {Options}
*/
setMinReconnectionDelay(delay) {
this._minReconnectionDelay = delay
return this
}
/**
* @return {number}
*/
get minReconnectionDelay() {
return this._minReconnectionDelay
}
/**
* @param {number} gw
* @return {Options}
*/
setReconnectionDelayGrowFactor(gw) {
this._reconnectionDelayGrowFactor = gw
return this
}
/**
* @return {number}
*/
get reconnectionDelayGrowFactor() {
return this._reconnectionDelayGrowFactor
}
/**
* @param {number} ut
* @return {Options}
*/
setMinUptime(ut) {
this._minUptime = ut
return this
}
/**
* @return {number}
*/
get minUptime() {
return this._minUptime
}
/**
* @param {number} retries
* @return {Options}
*/
setMaxRetries(retries) {
this._maxRetries = retries
return this
}
/**
* @return {number}
*/
get maxRetries() {
return this._maxRetries
}
/**
* @param {number} em
* @return {Options}
*/
setMaxEnqueuedMessages(em) {
this._maxEnqueuedMessages = em
return this
}
/**
* @return {number}
*/
get maxEnqueuedMessages() {
return this._maxEnqueuedMessages
}
/**
* @param {Array<string>} protocols
* @return {Options}
* @throws {TypeError}
*/
setProtocols(protocols) {
if (!Array.isArray(protocols))
throw new TypeError('protocols must be a Array')
this._protocols = protocols
return this
}
/**
* @return {Array<string>}
*/
get protocols() {
return this._protocols
}
/**
* @param {any} customWS
* @return {Options}
*/
setCustomWS(customWS) {
this._customWS = customWS
return this
}
/**
* @return {any}
*/
get customWS() {
return this._customWS
}
/**
* @return {string}
*/
get url() {
if (this._host.length > 2 && this._host.slice(0, 2) === 'ws') {
return `${this._host}${![HTTPPort, HTTPSPort].includes(this._port) ? `:${this._port}` : ''}`
}
return `ws${this._secure ? 's' : ''}://${this._host}${![HTTPPort, HTTPSPort].includes(this._port) ? `:${this._port}` : ''}/${!this._longpoll ? this._endpoints[0] : this._endpoints[1]}`
}
/**
* @return {{WebSocket: ?any, maxReconnectionDelay: number, minReconnectionDelay: number, reconnectionDelayGrowFactor: number, minUptime: number, connectionTimeout: number, maxRetries: number, maxEnqueuedMessages: number, startClosed: boolean, debug: boolean }}
*/
make() {
return {
...(this._customWS ? { WebSocket: this._customWS } : {}),
maxReconnectionDelay: this._maxConnectionDelay,
minReconnectionDelay: this._minReconnectionDelay,
reconnectionDelayGrowFactor: this._reconnectionDelayGrowFactor,
minUptime: this._minUptime,
connectionTimeout: this._timeout,
maxRetries: this._maxRetries,
maxEnqueuedMessages: this._maxEnqueuedMessages,
startClosed: this._startClosed,
debug: this._debug,
headers: {
Client: `tcabaci-read-js-client/${this._version}`,
},
}
}
}