/
index.js
450 lines (408 loc) · 11.9 KB
/
index.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* WebSocket with promise api
*/
/**
* @external Channel
*/
const Channel = require('chnl');
// todo: maybe remove PromiseController and just use promised-map with 2 items?
const PromiseController = require('promise-controller');
const { PromisedMap } = require('promised-map');
// todo: maybe remove Requests and just use promised-map?
const Requests = require('./requests');
const defaultOptions = require('./options');
const {throwIf, isPromise} = require('./utils');
// see: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants
const STATE = {
CONNECTING: 0,
OPEN: 1,
CLOSING: 2,
CLOSED: 3,
};
/**
* @typicalname wsp
*/
class WebSocketAsPromised {
/**
* Constructor. Unlike original WebSocket it does not immediately open connection.
* Please call `open()` method to connect.
*
* @param {String} url WebSocket URL
* @param {Options} [options]
*/
constructor(url, options) {
this._assertOptions(options);
this._url = url;
this._options = Object.assign({}, defaultOptions, options);
this._requests = new Requests();
this._promisedMap = new PromisedMap();
this._ws = null;
this._wsSubscription = null;
this._createOpeningController();
this._createClosingController();
this._createChannels();
}
/**
* Returns original WebSocket instance created by `options.createWebSocket`.
*
* @returns {WebSocket}
*/
get ws() {
return this._ws;
}
/**
* Returns WebSocket url.
*
* @returns {String}
*/
get url() {
return this._url;
}
/**
* Is WebSocket connection in opening state.
*
* @returns {Boolean}
*/
get isOpening() {
return Boolean(this._ws && this._ws.readyState === STATE.CONNECTING);
}
/**
* Is WebSocket connection opened.
*
* @returns {Boolean}
*/
get isOpened() {
return Boolean(this._ws && this._ws.readyState === STATE.OPEN);
}
/**
* Is WebSocket connection in closing state.
*
* @returns {Boolean}
*/
get isClosing() {
return Boolean(this._ws && this._ws.readyState === STATE.CLOSING);
}
/**
* Is WebSocket connection closed.
*
* @returns {Boolean}
*/
get isClosed() {
return Boolean(!this._ws || this._ws.readyState === STATE.CLOSED);
}
/**
* Event channel triggered when connection is opened.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onOpen.addListener(() => console.log('Connection opened'));
*
* @returns {Channel}
*/
get onOpen() {
return this._onOpen;
}
/**
* Event channel triggered every time when message is sent to server.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onSend.addListener(data => console.log('Message sent', data));
*
* @returns {Channel}
*/
get onSend() {
return this._onSend;
}
/**
* Event channel triggered every time when message received from server.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onMessage.addListener(message => console.log(message));
*
* @returns {Channel}
*/
get onMessage() {
return this._onMessage;
}
/**
* Event channel triggered every time when received message is successfully unpacked.
* For example, if you are using JSON transport, the listener will receive already JSON parsed data.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onUnpackedMessage.addListener(data => console.log(data.foo));
*
* @returns {Channel}
*/
get onUnpackedMessage() {
return this._onUnpackedMessage;
}
/**
* Event channel triggered every time when response to some request comes.
* Received message considered a response if requestId is found in it.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onResponse.addListener(data => console.log(data));
*
* @returns {Channel}
*/
get onResponse() {
return this._onResponse;
}
/**
* Event channel triggered when connection closed.
* Listener accepts single argument `{code, reason}`.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onClose.addListener(event => console.log(`Connections closed: ${event.reason}`));
*
* @returns {Channel}
*/
get onClose() {
return this._onClose;
}
/**
* Event channel triggered when by Websocket 'error' event.
*
* @see https://vitalets.github.io/chnl/#channel
* @example
* wsp.onError.addListener(event => console.error(event));
*
* @returns {Channel}
*/
get onError() {
return this._onError;
}
/**
* Opens WebSocket connection. If connection already opened, promise will be resolved with "open event".
*
* @returns {Promise<Event>}
*/
open() {
if (this.isClosing) {
return Promise.reject(new Error(`Can't open WebSocket while closing.`));
}
if (this.isOpened) {
return this._opening.promise;
}
return this._opening.call(() => {
this._opening.promise.catch(e => this._cleanup(e));
this._createWS();
});
}
/**
* Performs request and waits for response.
*
* @param {*} data
* @param {Object} [options]
* @param {String|Number} [options.requestId=<auto-generated>]
* @param {Number} [options.timeout=0]
* @returns {Promise}
*/
sendRequest(data, options = {}) {
const requestId = options.requestId || `${Math.random()}`;
const timeout = options.timeout !== undefined ? options.timeout : this._options.timeout;
return this._requests.create(requestId, () => {
this._assertRequestIdHandlers();
const finalData = this._options.attachRequestId(data, requestId);
this.sendPacked(finalData);
}, timeout);
}
/**
* Packs data with `options.packMessage` and sends to the server.
*
* @param {*} data
*/
sendPacked(data) {
this._assertPackingHandlers();
const message = this._options.packMessage(data);
this.send(message);
}
/**
* Sends data without packing.
*
* @param {String|Blob|ArrayBuffer} data
*/
send(data) {
throwIf(!this.isOpened, `Can't send data because WebSocket is not opened.`);
this._ws.send(data);
this._onSend.dispatchAsync(data);
}
/**
* Waits for particular message to come.
*
* @param {Function} predicate function to check incoming message.
* @param {Object} [options]
* @param {Number} [options.timeout=0]
* @param {Error} [options.timeoutError]
* @returns {Promise}
*
* @example
* const response = await wsp.waitUnpackedMessage(data => data && data.foo === 'bar');
*/
waitUnpackedMessage(predicate, options = {}) {
throwIf(typeof predicate !== 'function', `Predicate must be a function, got ${typeof predicate}`);
if (options.timeout) {
setTimeout(() => {
if (this._promisedMap.has(predicate)) {
const error = options.timeoutError || new Error('Timeout');
this._promisedMap.reject(predicate, error);
}
}, options.timeout);
}
return this._promisedMap.set(predicate);
}
/**
* Closes WebSocket connection. If connection already closed, promise will be resolved with "close event".
*
* @param {number=} [code=1000] A numeric value indicating the status code.
* @param {string=} [reason] A human-readable reason for closing connection.
* @returns {Promise<Event>}
*/
close(code, reason) { // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
return this.isClosed
? Promise.resolve(this._closing.value)
: this._closing.call(() => this._ws.close(code, reason));
}
/**
* Removes all listeners from WSP instance. Useful for cleanup.
*/
removeAllListeners() {
this._onOpen.removeAllListeners();
this._onMessage.removeAllListeners();
this._onUnpackedMessage.removeAllListeners();
this._onResponse.removeAllListeners();
this._onSend.removeAllListeners();
this._onClose.removeAllListeners();
this._onError.removeAllListeners();
}
_createOpeningController() {
const connectionTimeout = this._options.connectionTimeout || this._options.timeout;
this._opening = new PromiseController({
timeout: connectionTimeout,
timeoutReason: `Can't open WebSocket within allowed timeout: ${connectionTimeout} ms.`
});
}
_createClosingController() {
const closingTimeout = this._options.timeout;
this._closing = new PromiseController({
timeout: closingTimeout,
timeoutReason: `Can't close WebSocket within allowed timeout: ${closingTimeout} ms.`
});
}
_createChannels() {
this._onOpen = new Channel();
this._onMessage = new Channel();
this._onUnpackedMessage = new Channel();
this._onResponse = new Channel();
this._onSend = new Channel();
this._onClose = new Channel();
this._onError = new Channel();
}
_createWS() {
this._ws = this._options.createWebSocket(this._url);
this._wsSubscription = new Channel.Subscription([
{ channel: this._ws, event: 'open', listener: e => this._handleOpen(e) },
{ channel: this._ws, event: 'message', listener: e => this._handleMessage(e) },
{ channel: this._ws, event: 'error', listener: e => this._handleError(e) },
{ channel: this._ws, event: 'close', listener: e => this._handleClose(e) },
]).on();
}
_handleOpen(event) {
this._onOpen.dispatchAsync(event);
this._opening.resolve(event);
}
_handleMessage(event) {
const data = this._options.extractMessageData(event);
this._onMessage.dispatchAsync(data);
this._tryUnpack(data);
}
_tryUnpack(data) {
if (this._options.unpackMessage) {
data = this._options.unpackMessage(data);
if (isPromise(data)) {
data.then(data => this._handleUnpackedData(data));
} else {
this._handleUnpackedData(data);
}
}
}
_handleUnpackedData(data) {
if (data !== undefined) {
// todo: maybe trigger onUnpackedMessage always?
this._onUnpackedMessage.dispatchAsync(data);
this._tryHandleResponse(data);
}
this._tryHandleWaitingMessage(data);
}
_tryHandleResponse(data) {
if (this._options.extractRequestId) {
const requestId = this._options.extractRequestId(data);
if (requestId) {
this._onResponse.dispatchAsync(data, requestId);
this._requests.resolve(requestId, data);
}
}
}
_tryHandleWaitingMessage(data) {
this._promisedMap.forEach((_, predicate) => {
let isMatched = false;
try {
isMatched = predicate(data);
} catch (e) {
this._promisedMap.reject(predicate, e);
return;
}
if (isMatched) {
this._promisedMap.resolve(predicate, data);
}
});
}
_handleError(event) {
this._onError.dispatchAsync(event);
}
_handleClose(event) {
this._onClose.dispatchAsync(event);
this._closing.resolve(event);
const error = new Error(`WebSocket closed with reason: ${event.reason} (${event.code}).`);
if (this._opening.isPending) {
this._opening.reject(error);
}
this._cleanup(error);
}
_cleanupWS() {
if (this._wsSubscription) {
this._wsSubscription.off();
this._wsSubscription = null;
}
this._ws = null;
}
_cleanup(error) {
this._cleanupWS();
this._requests.rejectAll(error);
}
_assertOptions(options) {
Object.keys(options || {}).forEach(key => {
if (!defaultOptions.hasOwnProperty(key)) {
throw new Error(`Unknown option: ${key}`);
}
});
}
_assertPackingHandlers() {
const { packMessage, unpackMessage } = this._options;
throwIf(!packMessage || !unpackMessage,
`Please define 'options.packMessage / options.unpackMessage' for sending packed messages.`
);
}
_assertRequestIdHandlers() {
const { attachRequestId, extractRequestId } = this._options;
throwIf(!attachRequestId || !extractRequestId,
`Please define 'options.attachRequestId / options.extractRequestId' for sending requests.`
);
}
}
module.exports = WebSocketAsPromised;