/
ws-async.ts
466 lines (426 loc) · 15.4 KB
/
ws-async.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* Implement promise-based WebSocket API, in order to keep dependencies
* of this module minimal.
*/
import { decode, encode } from '@msgpack/msgpack';
import * as WebSocket from 'isomorphic-ws';
import { ErrorCode, FrontPanelError } from './error';
import { AsyncOperation } from './operation';
function isBrowser(): boolean {
return typeof window !== `undefined`;
}
// see: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants
enum ReadyState {
CONNECTING = 0,
OPEN,
CLOSING,
CLOSED
};
// As a special case, server may send unsolicited replies with the session ID
// and sequence number of 0. They indicate notifications about server events
// not corresponding to any client requests, e.g. a device [dis]connection,
// and for them the result code is replaced with the notification opcode and
// the return value is used to transport the notification payload.
export enum Notification {
DeviceConnected = 1,
DeviceDisconnected
}
export interface IReply {
readonly session: number;
readonly replyId: number;
readonly data: any;
readonly notification?: Notification;
}
interface IReplyOrError {
readonly reply?: IReply;
readonly error?: FrontPanelError;
}
/**
* The WebSockedAsPromised class.
*/
export class AsyncWebSocket {
private readonly url: string;
private readonly requests: Map<number, AsyncOperation<IReply>>;
private readonly replies: IReplyOrError[];
private openOperation: AsyncOperation<void> | null;
private closeOperation: AsyncOperation<void> | null;
private replyOperation: AsyncOperation<IReply> | null;
private ws: WebSocket | null;
private disconnecting: boolean;
private allowSelfSigned: boolean;
private lastError?: Error;
private readonly session: number;
private sequenceNumber: number;
/**
* Constructor. Unlike original WebSocket it does not immediately open connection.
* Please call [[open]] method to connect.
*
* @param {String} url WebSocket URL.
* @param {boolean} allowSelfSigned Whether the server certificate is not
* verified against the list of supplied CAs. Allow connection
* to servers with self signed certificates.
*/
constructor(url: string, allowSelfSigned: boolean) {
this.url = url;
this.requests = new Map<number, AsyncOperation<IReply>>();
this.replies = [];
this.openOperation = null;
this.closeOperation = null;
this.replyOperation = null;
this.ws = null;
this.disconnecting = false;
this.allowSelfSigned = allowSelfSigned;
this.lastError = undefined;
this.session = 1;
this.sequenceNumber = 0;
}
/**
* Is WebSocket connection in opening state.
*
* @returns {boolean}
*/
public get isOpening(): boolean {
return this.ws !== null && this.ws.readyState === ReadyState.CONNECTING;
}
/**
* Is WebSocket connection opened.
*
* @returns {boolean}
*/
public get isOpened(): boolean {
return this.ws !== null && this.ws.readyState === ReadyState.OPEN;
}
/**
* Is WebSocket connection in closing state.
*
* @returns {Boolean}
*/
public get isClosing(): boolean {
return this.ws !== null && this.ws.readyState === ReadyState.CLOSING;
}
/**
* Is WebSocket connection closed.
*
* @returns {boolean}
*/
public get isClosed(): boolean {
return this.ws === null || this.ws.readyState === ReadyState.CLOSED;
}
/**
* Opens WebSocket connection.
*
* @returns {Promise<void>}
*/
public open(): Promise<void> {
if (this.ws !== null) {
// TODO: Replace with switch/case. Currently for TS 3.6.4 returning from
// a case statement will rise the error: TS7029 Fallthrough case in switch.
if (this.ws.readyState === ReadyState.OPEN) {
return Promise.resolve();
} else if (this.ws.readyState === ReadyState.CONNECTING) {
this._rejectWithError(
new FrontPanelError(
ErrorCode.Failed,
'WebSocket opening already in progress'
)
);
} else if (this.ws.readyState === ReadyState.CLOSING) {
this._rejectWithError(
new FrontPanelError(
ErrorCode.Failed,
'WebSocket is closing right now'
)
);
}
}
if (this.lastError) {
return Promise.reject(this.lastError);
}
this.openOperation = new AsyncOperation<void>(() => {
this.disconnecting = false;
// Allow connection to servers with self-signed certificates for the
// Node.js implementation. We can't do the same for the browser.
// User should open https://server/ in a browser by himself and
// accept the certificate manually (when connecting to wss://server/).
this.ws =
isBrowser() || !this.allowSelfSigned
? new WebSocket(this.url)
: new WebSocket(this.url, undefined, {
rejectUnauthorized: false
});
// msgpack-lite doesn't seem to work directly with blobs, so
// we'd have to convert them to arrays on reception otherwise
// using FileReader and avoiding this makes things simpler
// (but maybe also slower? to be checked...)
this.ws.binaryType = 'arraybuffer';
this.ws.onopen = () => this._handleOpen();
this.ws.onerror = () => this._handleError();
this.ws.onclose = (event: WebSocket.CloseEvent) =>
this._handleClose(event);
this.ws.onmessage = (event: WebSocket.MessageEvent) =>
this._handleMessage(event);
});
return this.openOperation.promise;
}
/**
* Closes WebSocket connection.
*
* @returns {Promise<>}
*/
public close(code?: number): Promise<void> {
if (this.isClosed) {
Promise.resolve();
}
if (!this.isOpened) {
this._rejectWithError(
new FrontPanelError(
ErrorCode.Failed,
'Failed to close not opened WebSocket'
)
);
}
// In case of an error WebSocket.close already was called in
// _RejectWithError so we can simply reject the promise.
if (this.lastError) {
return Promise.reject(this.lastError);
}
this.closeOperation = new AsyncOperation<void>(() => {
// The client is started disconnection.
this.disconnecting = true;
this._getWS().close(code);
});
return this.closeOperation.promise;
}
/**
* Returns promised which will be satisfied when the reply is received.
*
* @param {*} data The data to send.
* @returns {Promise}
*/
public send(...data: any[]): Promise<IReply> {
if (!this.isOpened) {
this._rejectWithError(
new FrontPanelError(
ErrorCode.Failed,
'Failed to send: WebSocket is not opened'
)
);
}
if (this.lastError) {
return Promise.reject(this.lastError);
}
this.sequenceNumber++;
const requestId = this.sequenceNumber;
const request = new AsyncOperation<IReply>(() => {
const dataToSend = [this.session, requestId].concat(data);
const buf = encode(dataToSend);
this._getWS().send(buf);
});
this.requests.set(requestId, request);
return request.promise;
}
/**
* Waits for reply which is not requested by Send.
* The replies are filtered by the replyId.
*
* @returns {Promise}
*/
public waitForServer(): Promise<IReply> {
if (!this.isOpened) {
this._rejectWithError(
new FrontPanelError(
ErrorCode.Failed,
'Failed to wait for reply: WebSocket is not opened'
)
);
}
if (this.lastError) {
return Promise.reject(this.lastError);
}
this.replyOperation = new AsyncOperation<IReply>(() => {
if (this.replyOperation !== null) {
// Check for replies.
const replyOrError = this.replies.shift();
if (replyOrError) {
if (replyOrError.reply) {
this.replyOperation.resolve(replyOrError.reply);
} else {
this.replyOperation.reject(replyOrError.error);
}
this.replyOperation = null;
}
}
});
return this.replyOperation.promise;
}
private _handleOpen(): void {
if (this.openOperation) {
this.openOperation.resolve();
this.openOperation = null;
}
}
private _handleError(): void {
this._rejectWithError(
new FrontPanelError(
ErrorCode.CommunicationError,
'Communication error'
)
);
}
private _handleClose(event: WebSocket.CloseEvent): void {
// Reject all pending operations if any.
if (
this.openOperation !== null ||
this.replyOperation !== null ||
this.requests.size !== 0
) {
this._rejectWithError(
new FrontPanelError(
this.disconnecting
? ErrorCode.DisconnectByClient
: ErrorCode.DisconnectByServer,
`WebSocket closed with reason: ${event.reason} (${event.code})`
),
true /* Do not reject close */
);
}
if (this.closeOperation) {
this.closeOperation.resolve();
this.closeOperation = null;
this.ws = null;
}
}
private _handleMessage(event: WebSocket.MessageEvent): void {
// Do nothing if we already are in error state.
if (this.lastError) {
return;
}
try {
const elements = decode(new Uint8Array(event.data as ArrayBuffer));
if (!Array.isArray(elements)) {
throw new FrontPanelError(
ErrorCode.CommunicationError,
'The reply is unexpectedly not an array'
);
}
if (elements.length < 3) {
throw new FrontPanelError(
ErrorCode.CommunicationError,
`Unexpectedly few elements in the reply ${elements}`
);
}
const session: number = elements[0];
elements.shift();
if (session !== this.session && session !== 0) {
throw new FrontPanelError(
ErrorCode.CommunicationError,
`Unexpected session ID ${session}`
);
}
const replyId: number = elements[0];
elements.shift();
// The error for the current reply. We correctly parse
// the reply data with the session number and the reply number.
// So we can reject the single reply instead of failing the whole state.
let replyError: FrontPanelError | null = null;
// Whether the reply is solicited.
let notification: Notification | undefined;
if (session === 0 && replyId === 0) {
notification = elements[0];
} else {
const errorCode: ErrorCode = elements[0];
if (errorCode !== ErrorCode.NoError) {
let errorMessage = `Reply #${replyId} failed`;
// Append the error description, if it isn't an empty string.
const errorDescription: string = elements[1];
if (errorDescription) {
errorMessage += `: '${errorDescription}'`;
}
replyError = new FrontPanelError(errorCode, errorMessage);
}
}
elements.shift();
if (replyError === null && elements.length > 1) {
replyError = new FrontPanelError(
ErrorCode.Failed,
'Unexpected extra elements in the reply: ' +
elements.slice(1)
);
}
// Extract the return value, if any: some requests don't return
// anything.
// We could return "undefined" from here, but it compares equal to
// null, which means that an error has occurred, when using "==",
// so to avoid any misunderstandings, return an empty array instead.
const data: any[] = elements.length === 0 ? [] : elements[0];
const reply: IReply = { replyId, session, data, notification };
const request = this.requests.get(replyId);
if (request) {
if (replyError) {
request.reject(replyError);
} else {
request.resolve(reply);
}
this.requests.delete(replyId);
} else {
if (this.replyOperation) {
if (replyError) {
this.replyOperation.reject(replyError);
} else {
this.replyOperation.resolve(reply);
}
this.replyOperation = null;
} else {
if (replyError) {
this.replies.push({ error: replyError });
} else {
this.replies.push({ reply });
}
}
}
} catch (e) {
if (e instanceof FrontPanelError) {
this._rejectWithError(e);
} else {
this._rejectWithError(new FrontPanelError(ErrorCode.Failed, (<Error>e).message));
}
}
}
private _rejectWithError(
error: FrontPanelError,
doNotRejectClose?: boolean
) {
this.lastError = error;
if (this.openOperation !== null) {
this.openOperation.reject(error);
this.openOperation = null;
}
if (this.closeOperation !== null && !doNotRejectClose) {
this.closeOperation.reject(error);
this.closeOperation = null;
}
if (this.replyOperation !== null) {
this.replyOperation.reject(error);
this.replyOperation = null;
}
if (this.requests.size !== 0) {
this.requests.forEach((request: AsyncOperation<IReply>) => {
request.reject(error);
});
this.requests.clear();
}
// Close WebSocket on an error.
if (this.ws !== null) {
this.ws.close();
}
}
private _getWS(): WebSocket {
if (this.ws === null) {
throw new FrontPanelError(
ErrorCode.Failed,
'WebSocket is unexpectedly null'
);
}
return this.ws;
}
}