-
Notifications
You must be signed in to change notification settings - Fork 196
/
index.ts
252 lines (213 loc) · 7.73 KB
/
index.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
/*!
* Reconnecting WebSocket
* by Pedro Ladaria <pedro.ladaria@gmail.com>
* https://github.com/pladaria/reconnecting-websocket
* License MIT
*/
type Options = {
[key: string]: any,
constructor?: new(url: string, protocols?: string | string[]) => WebSocket;
maxReconnectionDelay?: number;
minReconnectionDelay?: number;
reconnectionDelayGrowFactor?: number;
connectionTimeout?: number;
maxRetries?: number;
debug?: boolean;
};
interface EventListener {
(event?: any): any;
};
interface EventListeners {
[key: string]: [EventListener, any][];
};
interface ReconnectingWebsocket extends WebSocket {
[key: string]: any;
};
const isWebSocket = (constructor: any) =>
constructor && constructor.CLOSING === 2;
const isGlobalWebSocket = () =>
typeof WebSocket !== 'undefined' && isWebSocket(WebSocket);
const getDefaultOptions = () => <Options>({
constructor: isGlobalWebSocket() ? WebSocket : null,
maxReconnectionDelay: 10000,
minReconnectionDelay: 1500,
reconnectionDelayGrowFactor: 1.3,
connectionTimeout: 4000,
maxRetries: Infinity,
debug: false,
});
const bypassProperty = (src: any, dst: any, name: string) => {
Object.defineProperty(dst, name, {
get: () => src[name],
set: (value) => {src[name] = value},
enumerable: true,
configurable: true,
});
};
const initReconnectionDelay = (config: Options) =>
(config.minReconnectionDelay + Math.random() * config.minReconnectionDelay);
const updateReconnectionDelay = (config: Options, previousDelay: number) => {
const newDelay = previousDelay * config.reconnectionDelayGrowFactor;
return (newDelay > config.maxReconnectionDelay)
? config.maxReconnectionDelay
: newDelay;
};
const LEVEL_0_EVENTS = ['onopen', 'onclose', 'onmessage', 'onerror'];
const reassignEventListeners = (ws: ReconnectingWebsocket, oldWs: ReconnectingWebsocket, listeners: EventListeners) => {
Object.keys(listeners).forEach(type => {
listeners[type].forEach(([listener, options]) => {
ws.addEventListener(type, listener, options);
});
});
if (oldWs) {
LEVEL_0_EVENTS.forEach(name => {
ws[name] = oldWs[name];
});
}
};
const ReconnectingWebsocket = function(
url: string | (() => string),
protocols?: string | string[],
options = <Options>{}
) {
let ws: WebSocket;
let connectingTimeout: number;
let reconnectDelay = 0;
let retriesCount = 0;
let shouldRetry = true;
let savedOnClose: any = null;
const listeners: EventListeners = {};
// require new to construct
if (!(this instanceof ReconnectingWebsocket)) {
throw new TypeError("Failed to construct 'ReconnectingWebSocket': Please use the 'new' operator");
}
// Set config. Not using `Object.assign` because of IE11
const config = getDefaultOptions();
Object.keys(config)
.filter(key => options.hasOwnProperty(key))
.forEach(key => config[key] = options[key]);
if (!isWebSocket(config.constructor)) {
throw new TypeError('Invalid WebSocket constructor. Set `options.constructor`');
}
const log = config.debug ? (...params: any[]) => console.log('RWS:', ...params) : () => {};
/**
* Not using dispatchEvent, otherwise we must use a DOM Event object
* Deferred because we want to handle the close event before this
*/
const emitError = (code: string, msg: string) => setTimeout(() => {
const err = <any>new Error(msg);
err.code = code;
if (Array.isArray(listeners.error)) {
listeners.error.forEach(([fn]) => fn(err));
}
if (ws.onerror) {
ws.onerror(err);
}
}, 0);
const handleClose = () => {
log('handleClose', {shouldRetry});
retriesCount++;
log('retries count:', retriesCount);
if (retriesCount > config.maxRetries) {
emitError('EHOSTDOWN', 'Too many failed connection attempts');
return;
}
if (!reconnectDelay) {
reconnectDelay = initReconnectionDelay(config);
} else {
reconnectDelay = updateReconnectionDelay(config, reconnectDelay);
}
log('handleClose - reconnectDelay:', reconnectDelay);
if (shouldRetry) {
setTimeout(connect, reconnectDelay);
}
};
const connect = () => {
if (!shouldRetry) {
return;
}
log('connect');
const oldWs = ws;
const wsUrl = (typeof url === 'function') ? url() : url;
ws = new (<any>config.constructor)(wsUrl, protocols);
connectingTimeout = setTimeout(() => {
log('timeout');
ws.close();
emitError('ETIMEDOUT', 'Connection timeout');
}, config.connectionTimeout);
log('bypass properties');
for (let key in ws) {
// @todo move to constant
if (['addEventListener', 'removeEventListener', 'close', 'send'].indexOf(key) < 0) {
bypassProperty(ws, this, key);
}
}
ws.addEventListener('open', () => {
clearTimeout(connectingTimeout);
log('open');
reconnectDelay = initReconnectionDelay(config);
log('reconnectDelay:', reconnectDelay);
retriesCount = 0;
});
ws.addEventListener('close', handleClose);
reassignEventListeners(ws, oldWs, listeners);
// because when closing with fastClose=true, it is saved and set to null to avoid double calls
ws.onclose = ws.onclose || savedOnClose;
savedOnClose = null;
};
log('init');
connect();
this.close = (code = 1000, reason = '', {keepClosed = false, fastClose = true, delay = 0} = {}) => {
log('close - params:', {reason, keepClosed, fastClose, delay, retriesCount, maxRetries: config.maxRetries});
shouldRetry = !keepClosed && retriesCount <= config.maxRetries;
if (delay) {
reconnectDelay = delay;
}
ws.close(code, reason);
if (fastClose) {
const fakeCloseEvent = <CloseEvent>{
code,
reason,
wasClean: true,
};
// execute close listeners soon with a fake closeEvent
// and remove them from the WS instance so they
// don't get fired on the real close.
handleClose();
ws.removeEventListener('close', handleClose);
// run and remove level2
if (Array.isArray(listeners.close)) {
listeners.close.forEach(([listener, options]) => {
listener(fakeCloseEvent);
ws.removeEventListener('close', listener, options);
});
}
// run and remove level0
if (ws.onclose) {
savedOnClose = ws.onclose;
ws.onclose(fakeCloseEvent);
ws.onclose = null;
}
}
};
this.send = (data: any) => {
ws.send(data)
};
this.addEventListener = (type: string, listener: EventListener, options: any) => {
if (Array.isArray(listeners[type])) {
if (!listeners[type].some(([l]) => l === listener)) {
listeners[type].push([listener, options]);
}
} else {
listeners[type] = [[listener, options]];
}
ws.addEventListener(type, listener, options);
};
this.removeEventListener = (type: string, listener: EventListener, options: any) => {
if (Array.isArray(listeners[type])) {
listeners[type] = listeners[type].filter(([l]) => l !== listener);
}
ws.removeEventListener(type, listener, options);
};
};
export = ReconnectingWebsocket;