@rc-ex/ws 1.2.0 → 1.2.1

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.
package/src/utils.ts CHANGED
@@ -1,17 +1,18 @@
1
- /* eslint-disable no-console */
2
- import type { MessageEvent } from 'isomorphic-ws';
3
- import type WS from 'isomorphic-ws';
1
+ import type { MessageEvent } from "isomorphic-ws";
2
+ import type WS from "isomorphic-ws";
4
3
 
5
- import type { WsgMeta, WsgEvent } from './types';
6
- import ClosedException from './exceptions/ClosedException';
7
- import TimeoutException from './exceptions/TimeoutException';
4
+ import type { WsgEvent, WsgMeta } from "./types";
5
+ import ClosedException from "./exceptions/ClosedException";
6
+ import TimeoutException from "./exceptions/TimeoutException";
8
7
 
9
8
  class Utils {
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
9
  public static splitWsgData(wsgData: string): [WsgMeta, any] {
12
- if (wsgData.includes(',--Boundary')) {
13
- const index = wsgData.indexOf(',--Boundary');
14
- return [JSON.parse(wsgData.substring(1, index)), wsgData.substring(index + 1, wsgData.length - 1)];
10
+ if (wsgData.includes(",--Boundary")) {
11
+ const index = wsgData.indexOf(",--Boundary");
12
+ return [
13
+ JSON.parse(wsgData.substring(1, index)),
14
+ wsgData.substring(index + 1, wsgData.length - 1),
15
+ ];
15
16
  }
16
17
  return JSON.parse(wsgData);
17
18
  }
@@ -27,7 +28,7 @@ ${JSON.stringify(JSON.parse(str), null, 2)}
27
28
  ******`,
28
29
  );
29
30
  };
30
- ws.addEventListener('message', (mEvent: MessageEvent) => {
31
+ ws.addEventListener("message", (mEvent: MessageEvent) => {
31
32
  const event = mEvent as WsgEvent;
32
33
  console.debug(
33
34
  `*** WebSocket incoming message: ***
@@ -35,19 +36,22 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
35
36
  ******`,
36
37
  );
37
38
  });
38
- ws.addEventListener('open', (event) => {
39
- console.debug('WebSocket open event:', event);
39
+ ws.addEventListener("open", (event) => {
40
+ console.debug("WebSocket open event:", event);
40
41
  });
41
- ws.addEventListener('error', (event) => {
42
- console.debug('WebSocket error event:', event);
42
+ ws.addEventListener("error", (event) => {
43
+ console.debug("WebSocket error event:", event);
43
44
  });
44
- ws.addEventListener('close', (event) => {
45
- console.debug('WebSocket close event:', event);
45
+ ws.addEventListener("close", (event) => {
46
+ console.debug("WebSocket close event:", event);
46
47
  });
47
48
  }
48
49
 
49
- public static waitForWebSocketMessage(ws: WS, matchCondition: (meta: WsgMeta) => boolean, timeout = 60000) {
50
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ public static waitForWebSocketMessage(
51
+ ws: WS,
52
+ matchCondition: (meta: WsgMeta) => boolean,
53
+ timeout = 60000,
54
+ ) {
51
55
  return new Promise<[WsgMeta, any, WsgEvent]>((resolve, reject) => {
52
56
  const checkHandle = setInterval(() => {
53
57
  if (ws.readyState === ws.CLOSED) {
@@ -56,8 +60,7 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
56
60
  }
57
61
  }, 1000);
58
62
  const timeoutHandle = setTimeout(() => {
59
- // eslint-disable-next-line @typescript-eslint/no-use-before-define
60
- ws.removeEventListener('message', handler);
63
+ ws.removeEventListener("message", handler);
61
64
  clearInterval(checkHandle);
62
65
  reject(new TimeoutException());
63
66
  }, timeout);
@@ -65,13 +68,13 @@ ${JSON.stringify(JSON.parse(event.data), null, 2)}
65
68
  const event = mEvent as WsgEvent;
66
69
  const [meta, body] = Utils.splitWsgData(event.data);
67
70
  if (matchCondition(meta)) {
68
- ws.removeEventListener('message', handler);
71
+ ws.removeEventListener("message", handler);
69
72
  clearInterval(checkHandle);
70
73
  clearTimeout(timeoutHandle);
71
74
  resolve([meta, body, event]);
72
75
  }
73
76
  };
74
- ws.addEventListener('message', handler);
77
+ ws.addEventListener("message", handler);
75
78
  });
76
79
  }
77
80
  }