@hautechai/sdk 0.2.2 → 0.2.4

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.
@@ -1,11 +1,12 @@
1
1
  import { OperationEntity, OperationsApi } from '../../autogenerated';
2
- import { WebSocket } from 'ws';
2
+ import { client as WebSocketClient, connection as Connection } from 'websocket';
3
3
  export declare class OperationsListener {
4
4
  useWebsocket: {
5
5
  endpoint: string;
6
6
  token: () => string | Promise<string>;
7
7
  } | null;
8
- ws: WebSocket | null;
8
+ ws: WebSocketClient | null;
9
+ connection: Connection | null;
9
10
  operations: () => Promise<OperationsApi>;
10
11
  constructor({ ws, operations, }: {
11
12
  ws: {
@@ -20,6 +21,6 @@ export declare class OperationsListener {
20
21
  subscribe(): Promise<void>;
21
22
  onOpen(): void;
22
23
  onMessage(msg: string): void;
23
- onClose(number: number, reason: Buffer): void;
24
+ onClose(number: number, reason: string): void;
24
25
  close(): void;
25
26
  }
@@ -1,10 +1,11 @@
1
- import { WebSocket } from 'ws';
1
+ import { client as WebSocketClient } from 'websocket';
2
2
  import { HautechException } from '../exceptions';
3
3
  // This is pretty much a dirty solution until we need to rework this part.
4
4
  export class OperationsListener {
5
5
  constructor({ ws, operations, }) {
6
6
  this.useWebsocket = null;
7
7
  this.ws = null;
8
+ this.connection = null;
8
9
  this.operationsStore = {};
9
10
  if (ws) {
10
11
  this.useWebsocket = {
@@ -15,7 +16,7 @@ export class OperationsListener {
15
16
  this.operations = operations;
16
17
  }
17
18
  async getOperation(id) {
18
- const isWsReady = this.ws?.readyState == WebSocket.OPEN;
19
+ const isWsReady = this.connection?.connected;
19
20
  if (!this.operationsStore[id] || !isWsReady) {
20
21
  const api = await this.operations();
21
22
  const operation = await api.operationsControllerGetOperationV1(id);
@@ -34,19 +35,28 @@ export class OperationsListener {
34
35
  if (!this.useWebsocket)
35
36
  return;
36
37
  try {
37
- this.ws = new WebSocket(this.useWebsocket.endpoint, ['1', await this.useWebsocket.token()]);
38
- this.ws.on('open', () => this.onOpen());
39
- this.ws.on('message', (msg) => this.onMessage(msg));
40
- this.ws.on('close', (number, reason) => this.onClose(number, reason));
38
+ this.ws = new WebSocketClient();
39
+ this.ws.connect(this.useWebsocket.endpoint, ['1', await this.useWebsocket.token()]);
40
+ this.ws.on('connect', (connection) => {
41
+ this.connection = connection;
42
+ this.onOpen();
43
+ connection.on('message', (msg) => {
44
+ if (msg.type === 'utf8')
45
+ this.onMessage(msg.utf8Data);
46
+ else if (msg.type === 'binary')
47
+ this.onMessage(msg.binaryData.toString('utf8'));
48
+ });
49
+ connection.on('close', (number, reason) => this.onClose(number, reason));
50
+ });
41
51
  }
42
52
  catch (err) {
43
53
  throw new HautechException(`SDK failed to open websocket: ${err}`);
44
54
  }
45
55
  }
46
56
  onOpen() {
47
- if (!this.ws)
57
+ if (!this.connection)
48
58
  throw new HautechException('Semantics error: this is a bug.');
49
- this.ws.send(JSON.stringify({
59
+ this.connection.send(JSON.stringify({
50
60
  event: 'subscribe',
51
61
  data: {
52
62
  channel: 'own_resources',
@@ -54,7 +64,7 @@ export class OperationsListener {
54
64
  }));
55
65
  }
56
66
  onMessage(msg) {
57
- if (!this.ws)
67
+ if (!this.connection)
58
68
  throw new HautechException('Semantics error: this is a bug.');
59
69
  const { event, data } = JSON.parse(msg);
60
70
  switch (event) {
@@ -67,15 +77,14 @@ export class OperationsListener {
67
77
  }
68
78
  }
69
79
  onClose(number, reason) {
70
- if (!this.ws)
80
+ if (!this.connection)
71
81
  throw new HautechException('Semantics error: this is a bug.');
72
82
  // Reset dirty state.
73
83
  this.operationsStore = {};
74
84
  this.ws = null;
75
85
  }
76
86
  close() {
77
- if (!this.ws)
78
- return;
79
- this.ws.close();
87
+ this.ws?.abort();
88
+ this.connection?.close();
80
89
  }
81
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hautechai/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "license": "MIT",
5
5
  "keywords": [],
6
6
  "repository": {
@@ -11,9 +11,9 @@
11
11
  "description": "Hautech SDK",
12
12
  "dependencies": {
13
13
  "@hautechai/pipelines": "0.3.2",
14
- "axios": "1.6.1",
14
+ "axios": "1.7.9",
15
15
  "jose": "5.9.6",
16
- "ws": "^8.18.0"
16
+ "websocket": "^1.0.35"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@jest/globals": "^29.7.0",
@@ -21,6 +21,7 @@
21
21
  "@types/jest": "29.5.12",
22
22
  "@types/jsonwebtoken": "9.0.7",
23
23
  "@types/node": "22.5.2",
24
+ "@types/websocket": "^1.0.10",
24
25
  "@types/ws": "^8.5.14",
25
26
  "dotenv": "16.4.7",
26
27
  "jest": "29.7.0",