@rc-ex/ws 0.16.3 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/lib/exceptions/ClosedException.d.ts +4 -0
  2. package/lib/exceptions/ClosedException.js +9 -0
  3. package/lib/exceptions/ClosedException.js.map +1 -0
  4. package/lib/exceptions/ConnectionException.d.ts +7 -0
  5. package/lib/exceptions/ConnectionException.js +16 -0
  6. package/lib/exceptions/ConnectionException.js.map +1 -0
  7. package/lib/exceptions/TimeoutException.d.ts +4 -0
  8. package/lib/exceptions/TimeoutException.js +9 -0
  9. package/lib/exceptions/TimeoutException.js.map +1 -0
  10. package/lib/index.d.ts +2 -1
  11. package/lib/index.js +20 -21
  12. package/lib/index.js.map +1 -1
  13. package/lib/rest.d.ts +3 -3
  14. package/lib/rest.js +18 -20
  15. package/lib/rest.js.map +1 -1
  16. package/lib/subscription.d.ts +5 -4
  17. package/lib/subscription.js +4 -4
  18. package/lib/subscription.js.map +1 -1
  19. package/lib/types.d.ts +14 -0
  20. package/lib/types.js.map +1 -1
  21. package/lib/utils.d.ts +1 -1
  22. package/lib/utils.js +14 -12
  23. package/lib/utils.js.map +1 -1
  24. package/package.json +10 -4
  25. package/src/exceptions/ClosedException.ts +7 -0
  26. package/src/exceptions/ConnectionException.ts +17 -0
  27. package/src/exceptions/TimeoutException.ts +7 -0
  28. package/{index.ts → src/index.ts} +50 -36
  29. package/{rest.ts → src/rest.ts} +26 -26
  30. package/{subscription.ts → src/subscription.ts} +29 -27
  31. package/{types.ts → src/types.ts} +27 -5
  32. package/{utils.ts → src/utils.ts} +15 -14
  33. package/tsconfig.json +3 -2
  34. package/exceptions.ts +0 -25
  35. package/lib/exceptions.d.ts +0 -12
  36. package/lib/exceptions.js +0 -29
  37. package/lib/exceptions.js.map +0 -1
  38. package/lib/package.json +0 -31
@@ -1,28 +1,30 @@
1
+ /* eslint-disable no-console */
1
2
  import RingCentral from '@rc-ex/core';
2
3
  import {
4
+ RestMethod,
3
5
  RestRequestConfig,
4
6
  RestResponse,
5
- RestMethod,
6
- } from '@rc-ex/core/lib/Rest';
7
+ } from '@rc-ex/core/lib/types';
7
8
  import SdkExtension from '@rc-ex/core/lib/SdkExtension';
8
- import WS, {OPEN, CONNECTING, MessageEvent} from 'isomorphic-ws';
9
+ import WS, { OPEN, CONNECTING, MessageEvent } from 'isomorphic-ws';
9
10
  import hyperid from 'hyperid';
10
- import {EventEmitter} from 'events';
11
+ import { EventEmitter } from 'events';
11
12
  import waitFor from 'wait-for-async';
12
13
  import RestException from '@rc-ex/core/lib/RestException';
13
- import {SubscriptionInfo} from '@rc-ex/core/lib/definitions';
14
+ import SubscriptionInfo from '@rc-ex/core/lib/definitions/SubscriptionInfo';
14
15
  import debounce from 'lodash/debounce';
15
16
 
16
- import {request} from './rest';
17
+ import { request } from './rest';
17
18
  import {
18
19
  WsToken,
19
20
  ConnectionDetails,
20
21
  WebSocketOptions,
21
22
  WsgEvent,
22
23
  Wsc,
24
+ WebSocketExtensionInterface,
23
25
  } from './types';
24
26
  import Subscription from './subscription';
25
- import {ConnectionException} from './exceptions';
27
+ import ConnectionException from './exceptions/ConnectionException';
26
28
  import Utils from './utils';
27
29
 
28
30
  const uuid = hyperid();
@@ -39,19 +41,30 @@ class WebSocketExtension extends SdkExtension {
39
41
  eventEmitter = new EventEmitter();
40
42
 
41
43
  options: WebSocketOptions;
44
+
42
45
  rc!: RingCentral;
46
+
43
47
  wsToken?: WsToken;
48
+
44
49
  wsTokenExpiresAt = 0;
50
+
45
51
  ws!: WS;
52
+
46
53
  connectionDetails!: ConnectionDetails;
54
+
47
55
  wsc?: Wsc;
56
+
48
57
  subscriptions: Subscription[] = [];
58
+
49
59
  recover: Function;
60
+
50
61
  connect: Function;
51
62
 
52
63
  // for auto recover
53
64
  intervalHandle?: NodeJS.Timeout;
65
+
54
66
  recoverTimestamp?: number;
67
+
55
68
  pingServerHandle?: NodeJS.Timeout;
56
69
 
57
70
  request = request; // request method was moved to another file to keep this file short
@@ -64,7 +77,7 @@ class WebSocketExtension extends SdkExtension {
64
77
  this.options.autoRecover ??= {
65
78
  enabled: true,
66
79
  };
67
- this.options.autoRecover.checkInterval ??= retriesAttempted => {
80
+ this.options.autoRecover.checkInterval ??= (retriesAttempted) => {
68
81
  const interval = 2000 + 2000 * retriesAttempted;
69
82
  return Math.min(8000, interval);
70
83
  };
@@ -81,9 +94,9 @@ class WebSocketExtension extends SdkExtension {
81
94
 
82
95
  disable() {
83
96
  super.disable();
84
- for (const subscription of this.subscriptions ?? []) {
97
+ (this.subscriptions ?? []).forEach((subscription) => {
85
98
  subscription.enabled = false;
86
- }
99
+ });
87
100
  }
88
101
 
89
102
  async install(rc: RingCentral) {
@@ -95,7 +108,7 @@ class WebSocketExtension extends SdkExtension {
95
108
  endpoint: string,
96
109
  content?: {},
97
110
  queryParams?: {},
98
- config?: RestRequestConfig
111
+ config?: RestRequestConfig,
99
112
  ): Promise<RestResponse<T>> => {
100
113
  if (!this.enabled || !this.options.restOverWebSocket) {
101
114
  return request<T>(method, endpoint, content, queryParams, config);
@@ -103,10 +116,10 @@ class WebSocketExtension extends SdkExtension {
103
116
  if (
104
117
  // the following cannot be done with WebSocket
105
118
  (config?.headers?.['Content-Type'] as string | undefined)?.includes(
106
- 'multipart/form-data'
107
- ) ||
108
- config?.responseType === 'arraybuffer' ||
109
- endpoint.startsWith('/restapi/oauth/') // token, revoke, wstoken
119
+ 'multipart/form-data',
120
+ )
121
+ || config?.responseType === 'arraybuffer'
122
+ || endpoint.startsWith('/restapi/oauth/') // token, revoke, wstoken
110
123
  ) {
111
124
  return request<T>(method, endpoint, content, queryParams, config);
112
125
  }
@@ -155,14 +168,14 @@ class WebSocketExtension extends SdkExtension {
155
168
  retriesAttempted = 0;
156
169
  if (this.options.debugMode) {
157
170
  console.debug(
158
- `Auto recover done, recoveryState: ${this.connectionDetails.recoveryState}`
171
+ `Auto recover done, recoveryState: ${this.connectionDetails.recoveryState}`,
159
172
  );
160
173
  }
161
174
  this.eventEmitter.emit(
162
175
  this.connectionDetails.recoveryState === 'Successful'
163
176
  ? Events.autoRecoverSuccess
164
177
  : Events.autoRecoverFailed,
165
- this.ws
178
+ this.ws,
166
179
  );
167
180
  } catch (e) {
168
181
  if (e instanceof RestException) {
@@ -176,13 +189,13 @@ class WebSocketExtension extends SdkExtension {
176
189
  }
177
190
  this.intervalHandle = setInterval(
178
191
  check,
179
- this.options.autoRecover!.checkInterval!(retriesAttempted)
192
+ this.options.autoRecover!.checkInterval!(retriesAttempted),
180
193
  );
181
194
  }
182
195
  };
183
196
  this.intervalHandle = setInterval(
184
197
  check,
185
- this.options.autoRecover!.checkInterval!(retriesAttempted)
198
+ this.options.autoRecover!.checkInterval!(retriesAttempted),
186
199
  );
187
200
 
188
201
  // browser only code start
@@ -212,9 +225,9 @@ class WebSocketExtension extends SdkExtension {
212
225
  this.recoverTimestamp = Date.now();
213
226
  }
214
227
  if (
215
- this.connectionDetails !== undefined &&
216
- Date.now() - this.recoverTimestamp >
217
- this.connectionDetails.recoveryTimeout * 1000
228
+ this.connectionDetails !== undefined
229
+ && Date.now() - this.recoverTimestamp
230
+ > this.connectionDetails.recoveryTimeout * 1000
218
231
  ) {
219
232
  if (this.options.debugMode) {
220
233
  console.debug('connect to WSG but do not recover');
@@ -246,7 +259,7 @@ class WebSocketExtension extends SdkExtension {
246
259
  type: 'Heartbeat',
247
260
  messageId: uuid(),
248
261
  },
249
- ])
262
+ ]),
250
263
  );
251
264
  }
252
265
  } catch (e) {
@@ -258,8 +271,7 @@ class WebSocketExtension extends SdkExtension {
258
271
  if (Date.now() > this.wsTokenExpiresAt) {
259
272
  const r = await this.rc.post('/restapi/oauth/wstoken');
260
273
  this.wsToken = r.data as WsToken;
261
- this.wsTokenExpiresAt =
262
- Date.now() + (this.wsToken.expires_in - 10) * 1000;
274
+ this.wsTokenExpiresAt = Date.now() + (this.wsToken.expires_in - 10) * 1000;
263
275
  }
264
276
  let wsUri = '';
265
277
  if (this.wsToken) {
@@ -290,7 +302,7 @@ class WebSocketExtension extends SdkExtension {
290
302
  }
291
303
  this.pingServerHandle = setTimeout(
292
304
  () => this.pingServer(),
293
- this.options.autoRecover!.pingServerInterval
305
+ this.options.autoRecover!.pingServerInterval,
294
306
  );
295
307
  });
296
308
  }
@@ -305,10 +317,10 @@ class WebSocketExtension extends SdkExtension {
305
317
  const event = mEvent as WsgEvent;
306
318
  const [meta, body] = Utils.splitWsgData(event.data);
307
319
  if (
308
- meta.wsc &&
309
- (!this.wsc ||
310
- (meta.type === 'ConnectionDetails' && body.recoveryState) ||
311
- this.wsc.sequence < meta.wsc.sequence)
320
+ meta.wsc
321
+ && (!this.wsc
322
+ || (meta.type === 'ConnectionDetails' && body.recoveryState)
323
+ || this.wsc.sequence < meta.wsc.sequence)
312
324
  ) {
313
325
  this.wsc = meta.wsc;
314
326
  this.eventEmitter.emit(Events.newWsc, this.wsc);
@@ -318,7 +330,7 @@ class WebSocketExtension extends SdkExtension {
318
330
  // get initial ConnectionDetails data
319
331
  const [meta, body, event] = await Utils.waitForWebSocketMessage(
320
332
  this.ws,
321
- meta => meta.type === 'ConnectionDetails' || meta.type === 'Error'
333
+ (meta) => meta.type === 'ConnectionDetails' || meta.type === 'Error',
322
334
  );
323
335
  if (meta.type === 'Error') {
324
336
  throw new ConnectionException(event);
@@ -326,12 +338,14 @@ class WebSocketExtension extends SdkExtension {
326
338
  this.connectionDetails = body;
327
339
 
328
340
  // recover all subscriptions, if there are any
329
- for (const subscription of this.subscriptions.filter(sub => sub.enabled)) {
341
+ for (const subscription of this.subscriptions.filter(
342
+ (sub) => sub.enabled,
343
+ )) {
330
344
  // because we have a new ws object
331
345
  subscription.setupWsEventListener();
332
346
  if (
333
- !recoverSession ||
334
- this.connectionDetails.recoveryState === 'Failed'
347
+ !recoverSession
348
+ || this.connectionDetails.recoveryState === 'Failed'
335
349
  ) {
336
350
  // create new subscription if don't recover existing one
337
351
  await subscription.subscribe();
@@ -361,9 +375,9 @@ class WebSocketExtension extends SdkExtension {
361
375
  async subscribe(
362
376
  eventFilters: string[],
363
377
  callback: (event: {}) => void,
364
- cache: SubscriptionInfo | undefined | null = undefined
378
+ cache: SubscriptionInfo | undefined | null = undefined,
365
379
  ) {
366
- const subscription = new Subscription(this, eventFilters, callback);
380
+ const subscription = new Subscription(this as WebSocketExtensionInterface, eventFilters, callback);
367
381
  if (cache === undefined || cache === null) {
368
382
  await subscription.subscribe();
369
383
  } else {
@@ -2,72 +2,72 @@ import {
2
2
  RestMethod,
3
3
  RestRequestConfig,
4
4
  RestResponse,
5
- } from '@rc-ex/core/lib/Rest';
5
+ } from '@rc-ex/core/lib/types';
6
6
  import RestException from '@rc-ex/core/lib/RestException';
7
7
  import hyperid from 'hyperid';
8
- import {getReasonPhrase} from 'http-status-codes';
8
+ import { getReasonPhrase } from 'http-status-codes';
9
9
 
10
- import WebSocketExtension from './index';
11
- import {version} from './package.json';
12
10
  import Utils from './utils';
11
+ import {
12
+ WebSocketExtensionInterface,
13
+ } from './types';
14
+
15
+ const version = '0.16';
13
16
 
14
17
  const uuid = hyperid();
15
18
 
16
19
  export async function request<T>(
17
- this: WebSocketExtension,
20
+ this: WebSocketExtensionInterface,
18
21
  method: RestMethod,
19
22
  endpoint: string,
20
23
  content?: {},
21
24
  queryParams?: {},
22
- config?: RestRequestConfig
25
+ config?: RestRequestConfig,
23
26
  ): Promise<RestResponse<T>> {
24
- const _config: RestRequestConfig = {
25
- method: method,
27
+ const newConfig: RestRequestConfig = {
28
+ method,
26
29
  baseURL: this.wsToken?.uri,
27
30
  url: endpoint,
28
31
  data: content,
29
32
  params: queryParams,
30
33
  ...config,
31
34
  };
32
- _config.headers = {
33
- ..._config.headers,
34
- 'X-User-Agent': `${this.rc.rest!.appName}/${
35
- this.rc.rest!.appVersion
36
- } ringcentral-extensible/ws/${version}`,
35
+ newConfig.headers = {
36
+ ...newConfig.headers,
37
+ 'X-User-Agent': `${this.rc.rest!.appName}/${this.rc.rest!.appVersion} ringcentral-extensible/ws/${version}`,
37
38
  };
38
39
  const messageId = uuid();
39
40
  const requestBody = [
40
41
  {
41
42
  type: 'ClientRequest',
42
43
  messageId,
43
- method: _config.method,
44
- path: _config.url,
45
- headers: _config.headers,
46
- query: _config.params,
44
+ method: newConfig.method,
45
+ path: newConfig.url,
46
+ headers: newConfig.headers,
47
+ query: newConfig.params,
47
48
  },
48
49
  ];
49
- if (_config.data) {
50
- requestBody.push(_config.data);
50
+ if (newConfig.data) {
51
+ requestBody.push(newConfig.data);
51
52
  }
52
53
  await this.ws.send(JSON.stringify(requestBody));
53
54
  const [meta, body] = await Utils.waitForWebSocketMessage(
54
55
  this.ws,
55
- meta => meta.messageId === messageId
56
+ (_meta) => _meta.messageId === messageId,
56
57
  );
57
58
  const response: RestResponse = {
58
59
  data: body as T,
59
60
  status: meta.status,
60
61
  statusText: getReasonPhrase(meta.status),
61
62
  headers: meta.headers,
62
- config: _config,
63
+ config: newConfig,
63
64
  };
64
65
  if (
65
- meta.type === 'ClientRequest' &&
66
- meta.status >= 200 &&
67
- meta.status < 300
66
+ meta.type === 'ClientRequest'
67
+ && meta.status >= 200
68
+ && meta.status < 300
68
69
  ) {
69
70
  return response;
70
- } else {
71
- throw new RestException(response);
72
71
  }
72
+ throw new RestException(response);
73
73
  }
@@ -1,36 +1,37 @@
1
- import {
2
- CreateSubscriptionRequest,
3
- SubscriptionInfo,
4
- } from '@rc-ex/core/lib/definitions';
5
- import {RestResponse} from '@rc-ex/core/lib/Rest';
6
- import {MessageEvent} from 'ws';
7
-
8
- import WebSocketExtension from './index';
9
- import {WsgEvent, WsgMeta} from './types';
1
+ /* eslint-disable no-console */
2
+ import CreateSubscriptionRequest from '@rc-ex/core/lib/definitions/CreateSubscriptionRequest';
3
+ import SubscriptionInfo from '@rc-ex/core/lib/definitions/SubscriptionInfo';
4
+ import { RestResponse } from '@rc-ex/core/lib/types';
5
+ import { MessageEvent } from 'ws';
6
+
7
+ import { WsgEvent, WsgMeta, WebSocketExtensionInterface } from './types';
10
8
  import Utils from './utils';
11
9
 
12
10
  class Subscription {
13
- wse: WebSocketExtension;
11
+ wse: WebSocketExtensionInterface;
12
+
14
13
  eventFilters: string[];
14
+
15
15
  eventListener: (event: MessageEvent) => void;
16
+
16
17
  timeout?: NodeJS.Timeout;
18
+
17
19
  enabled = true;
18
20
 
19
21
  constructor(
20
- wse: WebSocketExtension,
22
+ wse: WebSocketExtensionInterface,
21
23
  eventFilters: string[],
22
- callback: (event: {}) => void
24
+ callback: (event: {}) => void,
23
25
  ) {
24
26
  this.wse = wse;
25
27
  this.eventFilters = eventFilters;
26
28
  this.eventListener = (mEvent: MessageEvent) => {
27
29
  const event = mEvent as WsgEvent;
28
- const [meta, body]: [WsgMeta, {subscriptionId: string}] =
29
- Utils.splitWsgData(event.data);
30
+ const [meta, body]: [WsgMeta, { subscriptionId: string }] = Utils.splitWsgData(event.data);
30
31
  if (
31
- this.enabled &&
32
- meta.type === 'ServerNotification' &&
33
- body.subscriptionId === this.subscriptionInfo!.id
32
+ this.enabled
33
+ && meta.type === 'ServerNotification'
34
+ && body.subscriptionId === this.subscriptionInfo!.id
34
35
  ) {
35
36
  callback(body);
36
37
  }
@@ -44,15 +45,17 @@ class Subscription {
44
45
 
45
46
  get requestBody(): CreateSubscriptionRequest {
46
47
  return {
47
- deliveryMode: {transportType: 'WebSocket'},
48
+ deliveryMode: { transportType: 'WebSocket' },
48
49
  eventFilters: this.eventFilters,
49
50
  };
50
51
  }
51
52
 
52
53
  _subscriptionInfo?: SubscriptionInfo;
54
+
53
55
  get subscriptionInfo(): SubscriptionInfo | undefined {
54
56
  return this._subscriptionInfo;
55
57
  }
58
+
56
59
  set subscriptionInfo(_subscription) {
57
60
  this._subscriptionInfo = _subscription;
58
61
  if (this.timeout) {
@@ -71,7 +74,7 @@ class Subscription {
71
74
  await this.wse.request<SubscriptionInfo>(
72
75
  'POST',
73
76
  '/restapi/v1.0/subscription',
74
- this.requestBody
77
+ this.requestBody,
75
78
  )
76
79
  ).data;
77
80
  }
@@ -85,11 +88,11 @@ class Subscription {
85
88
  await this.wse.request<SubscriptionInfo>(
86
89
  'PUT',
87
90
  `/restapi/v1.0/subscription/${this.subscriptionInfo!.id}`,
88
- this.requestBody
91
+ this.requestBody,
89
92
  )
90
93
  ).data;
91
94
  } catch (e) {
92
- const re = e as {response: RestResponse};
95
+ const re = e as { response: RestResponse };
93
96
  if (re.response && re.response.status === 404) {
94
97
  // subscription expired
95
98
  await this.subscribe();
@@ -104,17 +107,16 @@ class Subscription {
104
107
  try {
105
108
  await this.wse.request<SubscriptionInfo>(
106
109
  'DELETE',
107
- `/restapi/v1.0/subscription/${this.subscriptionInfo!.id}`
110
+ `/restapi/v1.0/subscription/${this.subscriptionInfo!.id}`,
108
111
  );
109
112
  } catch (e) {
110
- const re = e as {response: RestResponse};
113
+ const re = e as { response: RestResponse };
111
114
  if (re.response && re.response.status === 404) {
112
115
  // ignore
113
116
  if (this.wse.options.debugMode) {
114
117
  console.debug(
115
- `Subscription ${
116
- this.subscriptionInfo!.id
117
- } doesn't exist on server side`
118
+ `Subscription ${this.subscriptionInfo!.id
119
+ } doesn't exist on server side`,
118
120
  );
119
121
  }
120
122
  } else if (re.response && re.response.status === 401) {
@@ -139,7 +141,7 @@ class Subscription {
139
141
  if (this.wse.ws) {
140
142
  this.wse.ws.removeEventListener('message', this.eventListener);
141
143
  }
142
- this.wse.subscriptions = this.wse.subscriptions.filter(x => x !== this);
144
+ this.wse.subscriptions = this.wse.subscriptions.filter((x) => x !== this);
143
145
  }
144
146
  }
145
147
 
@@ -1,3 +1,7 @@
1
+ import RingCentral from '@rc-ex/core';
2
+ import { RestMethod, RestRequestConfig, RestResponse } from '@rc-ex/core/lib/types';
3
+ import WS from 'isomorphic-ws';
4
+
1
5
  export type WsToken = {
2
6
  uri: string;
3
7
  ws_access_token: string;
@@ -27,11 +31,11 @@ export type Wsc = {
27
31
 
28
32
  export type WsgMeta = {
29
33
  type:
30
- | 'ClientRequest'
31
- | 'ServerNotification'
32
- | 'Error'
33
- | 'ConnectionDetails'
34
- | 'Heartbeat';
34
+ | 'ClientRequest'
35
+ | 'ServerNotification'
36
+ | 'Error'
37
+ | 'ConnectionDetails'
38
+ | 'Heartbeat';
35
39
  messageId: string;
36
40
  status: number;
37
41
  headers: {
@@ -56,3 +60,21 @@ export type ConnectionDetails = {
56
60
  recoveryState?: 'Successful' | 'Failed';
57
61
  recoveryErrorCode?: string;
58
62
  };
63
+
64
+ export interface WebSocketExtensionInterface {
65
+ options: WebSocketOptions;
66
+ subscriptions: SubscriptionInterface[];
67
+ ws: WS;
68
+ wsToken?: WsToken;
69
+ rc: RingCentral;
70
+ request<T>(
71
+ method: RestMethod,
72
+ endpoint: string,
73
+ content?: {},
74
+ queryParams?: {},
75
+ config?: RestRequestConfig,
76
+ ): Promise<RestResponse<T>>;
77
+ }
78
+
79
+ export interface SubscriptionInterface {
80
+ }
@@ -1,7 +1,9 @@
1
- import WS, {MessageEvent} from 'isomorphic-ws';
1
+ /* eslint-disable no-console */
2
+ import WS, { MessageEvent } from 'isomorphic-ws';
2
3
 
3
- import {WsgMeta, WsgEvent} from './types';
4
- import {ClosedException, TimeoutException} from './exceptions';
4
+ import { WsgMeta, WsgEvent } from './types';
5
+ import ClosedException from './exceptions/ClosedException';
6
+ import TimeoutException from './exceptions/TimeoutException';
5
7
 
6
8
  class Utils {
7
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -12,19 +14,19 @@ class Utils {
12
14
  JSON.parse(wsgData.substring(1, index)),
13
15
  wsgData.substring(index + 1, wsgData.length - 1),
14
16
  ];
15
- } else {
16
- return JSON.parse(wsgData);
17
17
  }
18
+ return JSON.parse(wsgData);
18
19
  }
19
20
 
20
- static debugWebSocket(ws: WS) {
21
+ static debugWebSocket(_ws: WS) {
22
+ const ws = _ws;
21
23
  const send = ws.send.bind(ws);
22
24
  ws.send = async (str: string) => {
23
25
  await send(str);
24
26
  console.debug(
25
27
  `*** WebSocket outgoing message: ***
26
28
  ${JSON.stringify(JSON.parse(str), null, 2)}
27
- ******`
29
+ ******`,
28
30
  );
29
31
  };
30
32
  ws.addEventListener('message', (mEvent: MessageEvent) => {
@@ -32,16 +34,16 @@ ${JSON.stringify(JSON.parse(str), null, 2)}
32
34
  console.debug(
33
35
  `*** WebSocket incoming message: ***
34
36
  ${JSON.stringify(JSON.parse(event.data), null, 2)}
35
- ******`
37
+ ******`,
36
38
  );
37
39
  });
38
- ws.addEventListener('open', event => {
40
+ ws.addEventListener('open', (event) => {
39
41
  console.debug('WebSocket open event:', event);
40
42
  });
41
- ws.addEventListener('error', event => {
43
+ ws.addEventListener('error', (event) => {
42
44
  console.debug('WebSocket error event:', event);
43
45
  });
44
- ws.addEventListener('close', event => {
46
+ ws.addEventListener('close', (event) => {
45
47
  console.debug('WebSocket close event:', event);
46
48
  });
47
49
  }
@@ -49,7 +51,7 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
49
51
  static waitForWebSocketMessage(
50
52
  ws: WS,
51
53
  matchCondition: (meta: WsgMeta) => boolean,
52
- timeout = 60000
54
+ timeout = 60000,
53
55
  ) {
54
56
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
57
  return new Promise<[WsgMeta, any, WsgEvent]>((resolve, reject) => {
@@ -60,10 +62,10 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
60
62
  }
61
63
  }, 1000);
62
64
  const timeoutHandle = setTimeout(() => {
65
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
63
66
  ws.removeEventListener('message', handler);
64
67
  clearInterval(checkHandle);
65
68
  reject(new TimeoutException());
66
- return;
67
69
  }, timeout);
68
70
  const handler = (mEvent: MessageEvent) => {
69
71
  const event = mEvent as WsgEvent;
@@ -73,7 +75,6 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
73
75
  clearInterval(checkHandle);
74
76
  clearTimeout(timeoutHandle);
75
77
  resolve([meta, body, event]);
76
- return;
77
78
  }
78
79
  };
79
80
  ws.addEventListener('message', handler);
package/tsconfig.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "extends": "../../../tsconfig.json",
3
3
  "compilerOptions": {
4
- "outDir": "./lib"
5
- }
4
+ "outDir": "lib"
5
+ },
6
+ "include": ["src"]
6
7
  }
package/exceptions.ts DELETED
@@ -1,25 +0,0 @@
1
- import {WsgEvent, WsgError, WsgMeta} from './types';
2
- import Utils from './utils';
3
-
4
- export class ConnectionException extends Error {
5
- wsgEvent: WsgEvent;
6
- wsgError: WsgError;
7
- constructor(wsgEvent: WsgEvent) {
8
- const [, wsgError]: [WsgMeta, WsgError] = Utils.splitWsgData(wsgEvent.data);
9
- super(JSON.stringify(wsgError, null, 2));
10
- this.wsgEvent = wsgEvent;
11
- this.wsgError = wsgError;
12
- }
13
- }
14
-
15
- export class TimeoutException extends Error {
16
- constructor(message?: string) {
17
- super(message ?? 'Failed to receive expected WebSocket message in time.');
18
- }
19
- }
20
-
21
- export class ClosedException extends Error {
22
- constructor(message?: string) {
23
- super(message ?? 'WebSocket has been closed');
24
- }
25
- }
@@ -1,12 +0,0 @@
1
- import { WsgEvent, WsgError } from './types';
2
- export declare class ConnectionException extends Error {
3
- wsgEvent: WsgEvent;
4
- wsgError: WsgError;
5
- constructor(wsgEvent: WsgEvent);
6
- }
7
- export declare class TimeoutException extends Error {
8
- constructor(message?: string);
9
- }
10
- export declare class ClosedException extends Error {
11
- constructor(message?: string);
12
- }
package/lib/exceptions.js DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ClosedException = exports.TimeoutException = exports.ConnectionException = void 0;
7
- const utils_1 = __importDefault(require("./utils"));
8
- class ConnectionException extends Error {
9
- constructor(wsgEvent) {
10
- const [, wsgError] = utils_1.default.splitWsgData(wsgEvent.data);
11
- super(JSON.stringify(wsgError, null, 2));
12
- this.wsgEvent = wsgEvent;
13
- this.wsgError = wsgError;
14
- }
15
- }
16
- exports.ConnectionException = ConnectionException;
17
- class TimeoutException extends Error {
18
- constructor(message) {
19
- super(message !== null && message !== void 0 ? message : 'Failed to receive expected WebSocket message in time.');
20
- }
21
- }
22
- exports.TimeoutException = TimeoutException;
23
- class ClosedException extends Error {
24
- constructor(message) {
25
- super(message !== null && message !== void 0 ? message : 'WebSocket has been closed');
26
- }
27
- }
28
- exports.ClosedException = ClosedException;
29
- //# sourceMappingURL=exceptions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"exceptions.js","sourceRoot":"","sources":["../exceptions.ts"],"names":[],"mappings":";;;;;;AACA,oDAA4B;AAE5B,MAAa,mBAAoB,SAAQ,KAAK;IAG5C,YAAY,QAAkB;QAC5B,MAAM,CAAC,EAAE,QAAQ,CAAC,GAAwB,eAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC5E,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACzC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AATD,kDASC;AAED,MAAa,gBAAiB,SAAQ,KAAK;IACzC,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,uDAAuD,CAAC,CAAC;IAC5E,CAAC;CACF;AAJD,4CAIC;AAED,MAAa,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,2BAA2B,CAAC,CAAC;IAChD,CAAC;CACF;AAJD,0CAIC"}