chia-agent 14.3.1 → 14.3.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [14.3.2]
4
+ ### Changed
5
+ - Error logs are generated when
6
+ - an error occurs during sending a message.
7
+ - it receives a websocket message with unexpected format.
8
+ - Debug logs are generated when ping/pong events are triggered.
9
+
3
10
  ## [14.3.1]
4
11
  ### Changed
5
12
  - Added a `Host` header when sending https request to a remote host.
@@ -1708,6 +1715,7 @@ daemon.sendMessage(destination, get_block_record_by_height_command, data);
1708
1715
  Initial release.
1709
1716
 
1710
1717
  <!-- [Unreleased]: https://github.com/Chia-Mine/chia-agent/compare/v0.0.1...v0.0.2 -->
1718
+ [14.3.2]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.1...v14.3.2
1711
1719
  [14.3.1]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.0...v14.3.1
1712
1720
  [14.3.0]: https://github.com/Chia-Mine/chia-agent/compare/v14.2.2...v14.3.0
1713
1721
  [14.2.2]: https://github.com/Chia-Mine/chia-agent/compare/v14.2.1...v14.2.2
@@ -1,5 +1,6 @@
1
1
  import * as WS from "ws";
2
2
  import { Event } from "ws";
3
+ export declare const defaultTimeoutInMs = 50000;
3
4
  export declare function open(url: string, timeoutMs?: number): Promise<{
4
5
  ws: WS;
5
6
  openEvent: Event;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.open = void 0;
3
+ exports.open = exports.defaultTimeoutInMs = void 0;
4
4
  const fs_1 = require("fs");
5
5
  const config_1 = require("../config");
6
6
  const WS = require("ws");
@@ -25,25 +25,33 @@ function create(url) {
25
25
  };
26
26
  return new WS(url, options);
27
27
  }
28
- const defaultTimeoutInMs = 50000;
28
+ exports.defaultTimeoutInMs = 50000;
29
29
  function open(url, timeoutMs) {
30
30
  return new Promise((resolve, reject) => {
31
31
  const ws = create(url);
32
32
  let timer = null;
33
- timeoutMs = typeof timeoutMs === "number" ? timeoutMs : defaultTimeoutInMs;
33
+ timeoutMs = typeof timeoutMs === "number" ? timeoutMs : exports.defaultTimeoutInMs;
34
+ let opened = false;
34
35
  timer = setTimeout(() => {
35
36
  timer = null;
36
37
  (0, logger_1.getLogger)().error("Request to open connection timed out");
37
38
  reject("Timeout");
38
39
  }, timeoutMs);
39
- ws.onopen = (openEvent) => {
40
+ const onOpenError = (err) => {
41
+ if (!opened) {
42
+ reject(err);
43
+ }
44
+ };
45
+ ws.addEventListener("open", (openEvent) => {
46
+ opened = true;
47
+ ws.removeEventListener("error", onOpenError);
40
48
  if (timer !== null) {
41
49
  clearTimeout(timer);
42
50
  timer = null;
43
51
  resolve({ ws, openEvent });
44
52
  }
45
- };
46
- ws.onerror = (err) => reject(err);
53
+ });
54
+ ws.addEventListener("error", onOpenError);
47
55
  });
48
56
  }
49
57
  exports.open = open;
package/daemon/index.d.ts CHANGED
@@ -57,6 +57,8 @@ declare class Daemon {
57
57
  protected onError(error: ErrorEvent): void;
58
58
  protected onMessage(event: MessageEvent): void;
59
59
  protected onClose(event: CloseEvent): void;
60
+ protected onPing(): void;
61
+ protected onPong(): void;
60
62
  }
61
63
  export type TDaemon = InstanceType<typeof Daemon>;
62
64
  export {};
package/daemon/index.js CHANGED
@@ -68,6 +68,8 @@ class Daemon {
68
68
  this.onError = this.onError.bind(this);
69
69
  this.onMessage = this.onMessage.bind(this);
70
70
  this.onClose = this.onClose.bind(this);
71
+ this.onPing = this.onPing.bind(this);
72
+ this.onPong = this.onPong.bind(this);
71
73
  }
72
74
  /**
73
75
  * Connect to local daemon via websocket.
@@ -92,9 +94,11 @@ class Daemon {
92
94
  (0, logger_1.getLogger)().debug(`Opening websocket connection to ${daemonServerURL}`);
93
95
  const result = yield (0, connection_1.open)(daemonServerURL, timeoutMs);
94
96
  this._socket = result.ws;
95
- this._socket.onerror = this.onError;
96
- this._socket.onmessage = this.onMessage;
97
- this._socket.onclose = this.onClose;
97
+ this._socket.addEventListener("error", this.onError);
98
+ this._socket.addEventListener("message", this.onMessage);
99
+ this._socket.addEventListener("close", this.onClose);
100
+ this._socket.addListener("ping", this.onPing);
101
+ this._socket.addListener("pong", this.onPong);
98
102
  yield this.onOpen(result.openEvent, daemonServerURL);
99
103
  return true;
100
104
  });
@@ -124,7 +128,13 @@ class Daemon {
124
128
  const reqId = message.request_id;
125
129
  this._responseQueue[reqId] = resolve;
126
130
  (0, logger_1.getLogger)().debug(`Sending message. dest=${destination} command=${command} reqId=${reqId}`);
127
- this._socket.send(JSON.stringify(message));
131
+ const messageStr = JSON.stringify(message);
132
+ this._socket.send(messageStr, (err) => {
133
+ if (err) {
134
+ (0, logger_1.getLogger)().error(`Error while sending message: ${messageStr}`);
135
+ (0, logger_1.getLogger)().error(JSON.stringify(err));
136
+ }
137
+ });
128
138
  });
129
139
  });
130
140
  }
@@ -252,8 +262,19 @@ class Daemon {
252
262
  this._errorEventListeners.forEach(l => l(error));
253
263
  }
254
264
  onMessage(event) {
255
- const payload = JSON.parse(event.data);
256
- const { request_id, origin, command } = payload;
265
+ let payload;
266
+ let request_id;
267
+ let origin;
268
+ let command;
269
+ try {
270
+ payload = JSON.parse(event.data);
271
+ ({ request_id, origin, command } = payload);
272
+ }
273
+ catch (err) {
274
+ (0, logger_1.getLogger)().error(`Failed to parse message data: ${JSON.stringify(err)}`);
275
+ (0, logger_1.getLogger)().error(`payload: ${event.data}`);
276
+ return;
277
+ }
257
278
  (0, logger_1.getLogger)().debug(`Arrived message. origin=${origin} command=${command} reqId=${request_id}`);
258
279
  const resolver = this._responseQueue[request_id];
259
280
  if (resolver) {
@@ -276,6 +297,8 @@ class Daemon {
276
297
  this._socket.removeEventListener("error", this.onError);
277
298
  this._socket.removeEventListener("message", this.onMessage);
278
299
  this._socket.removeEventListener("close", this.onClose);
300
+ this._socket.removeListener("ping", this.onPing);
301
+ this._socket.removeListener("pong", this.onPong);
279
302
  this._socket = null;
280
303
  }
281
304
  this._closing = false;
@@ -289,4 +312,10 @@ class Daemon {
289
312
  this._onClosePromise = undefined;
290
313
  }
291
314
  }
315
+ onPing() {
316
+ (0, logger_1.getLogger)().debug("Received ping");
317
+ }
318
+ onPong() {
319
+ (0, logger_1.getLogger)().debug("Received pong");
320
+ }
292
321
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chia-agent",
3
- "version": "14.3.1",
3
+ "version": "14.3.2",
4
4
  "author": "ChiaMineJP <admin@chiamine.jp>",
5
5
  "description": "chia rpc/websocket client library",
6
6
  "license": "MIT",