chia-agent 14.3.1 → 14.3.3

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,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [14.3.3]
4
+ ### Changed
5
+ - The default service name which [`Daemon`](./src/daemon/index.ts) client tries to register is now `wallet_ui`.
6
+ Previously `chia_agent` service and optionally `wallet_ui` service were registered to `chia-blockchain`'s `Daemon`.
7
+
8
+ ## [14.3.2]
9
+ ### Changed
10
+ - Error logs are generated when
11
+ - an error occurs during sending a message.
12
+ - it receives a websocket message with unexpected format.
13
+ - Debug logs are generated when ping/pong events are triggered.
14
+
3
15
  ## [14.3.1]
4
16
  ### Changed
5
17
  - Added a `Host` header when sending https request to a remote host.
@@ -1708,6 +1720,8 @@ daemon.sendMessage(destination, get_block_record_by_height_command, data);
1708
1720
  Initial release.
1709
1721
 
1710
1722
  <!-- [Unreleased]: https://github.com/Chia-Mine/chia-agent/compare/v0.0.1...v0.0.2 -->
1723
+ [14.3.3]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.2...v14.3.3
1724
+ [14.3.2]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.1...v14.3.2
1711
1725
  [14.3.1]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.0...v14.3.1
1712
1726
  [14.3.0]: https://github.com/Chia-Mine/chia-agent/compare/v14.2.2...v14.3.0
1713
1727
  [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.on("open", (openEvent) => {
46
+ opened = true;
47
+ ws.off("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.on("error", onOpenError);
47
55
  });
48
56
  }
49
57
  exports.open = open;
package/daemon/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export type WsEvent = Event | MessageEvent | ErrorEvent | CloseEvent;
7
7
  export type EventListener<T = WsEvent> = (ev: T) => unknown;
8
8
  type EventListenerOf<T> = T extends "open" ? EventListener<Event> : T extends "message" ? EventListener<MessageEvent> : T extends "error" ? EventListener<ErrorEvent> : T extends "close" ? EventListener<CloseEvent> : never;
9
9
  export type MessageListener<D extends WsMessage> = (msg: D) => unknown;
10
- export declare function getDaemon(): Daemon;
10
+ export declare function getDaemon(serviceName?: string): Daemon;
11
11
  declare class Daemon {
12
12
  protected _socket: WS | null;
13
13
  protected _connectedUrl: string;
@@ -22,9 +22,10 @@ declare class Daemon {
22
22
  protected _closing: boolean;
23
23
  protected _onClosePromise: (() => unknown) | undefined;
24
24
  protected _subscriptions: string[];
25
+ protected _serviceName: string;
25
26
  get connected(): boolean;
26
27
  get closing(): boolean;
27
- constructor();
28
+ constructor(serviceName?: string);
28
29
  /**
29
30
  * Connect to local daemon via websocket.
30
31
  * @param daemonServerURL
@@ -57,6 +58,8 @@ declare class Daemon {
57
58
  protected onError(error: ErrorEvent): void;
58
59
  protected onMessage(event: MessageEvent): void;
59
60
  protected onClose(event: CloseEvent): void;
61
+ protected onPing(): void;
62
+ protected onPong(): void;
60
63
  }
61
64
  export type TDaemon = InstanceType<typeof Daemon>;
62
65
  export {};
package/daemon/index.js CHANGED
@@ -14,13 +14,13 @@ const crypto_1 = require("crypto");
14
14
  const logger_1 = require("../logger");
15
15
  const connection_1 = require("./connection");
16
16
  const index_1 = require("../config/index");
17
- const chia_agent_service = "chia_agent";
17
+ const DEFAULT_SERVICE_NAME = "wallet_ui";
18
18
  let daemon = null;
19
- function getDaemon() {
19
+ function getDaemon(serviceName) {
20
20
  if (daemon) {
21
21
  return daemon;
22
22
  }
23
- return daemon = new Daemon();
23
+ return daemon = new Daemon(serviceName);
24
24
  }
25
25
  exports.getDaemon = getDaemon;
26
26
  // Gracefully disconnect from remote daemon server on Ctrl+C.
@@ -53,7 +53,7 @@ class Daemon {
53
53
  get closing() {
54
54
  return this._closing;
55
55
  }
56
- constructor() {
56
+ constructor(serviceName) {
57
57
  this._socket = null;
58
58
  this._connectedUrl = "";
59
59
  this._responseQueue = {};
@@ -64,10 +64,16 @@ class Daemon {
64
64
  this._messageListeners = {};
65
65
  this._closing = false;
66
66
  this._subscriptions = [];
67
+ this._serviceName = DEFAULT_SERVICE_NAME;
67
68
  this.onOpen = this.onOpen.bind(this);
68
69
  this.onError = this.onError.bind(this);
69
70
  this.onMessage = this.onMessage.bind(this);
70
71
  this.onClose = this.onClose.bind(this);
72
+ this.onPing = this.onPing.bind(this);
73
+ this.onPong = this.onPong.bind(this);
74
+ if (serviceName) {
75
+ this._serviceName = serviceName;
76
+ }
71
77
  }
72
78
  /**
73
79
  * Connect to local daemon via websocket.
@@ -92,9 +98,11 @@ class Daemon {
92
98
  (0, logger_1.getLogger)().debug(`Opening websocket connection to ${daemonServerURL}`);
93
99
  const result = yield (0, connection_1.open)(daemonServerURL, timeoutMs);
94
100
  this._socket = result.ws;
95
- this._socket.onerror = this.onError;
96
- this._socket.onmessage = this.onMessage;
97
- this._socket.onclose = this.onClose;
101
+ this._socket.on("error", this.onError);
102
+ this._socket.addEventListener("message", this.onMessage);
103
+ this._socket.on("close", this.onClose);
104
+ this._socket.on("ping", this.onPing);
105
+ this._socket.on("pong", this.onPong);
98
106
  yield this.onOpen(result.openEvent, daemonServerURL);
99
107
  return true;
100
108
  });
@@ -124,7 +132,13 @@ class Daemon {
124
132
  const reqId = message.request_id;
125
133
  this._responseQueue[reqId] = resolve;
126
134
  (0, logger_1.getLogger)().debug(`Sending message. dest=${destination} command=${command} reqId=${reqId}`);
127
- this._socket.send(JSON.stringify(message));
135
+ const messageStr = JSON.stringify(message);
136
+ this._socket.send(messageStr, (err) => {
137
+ if (err) {
138
+ (0, logger_1.getLogger)().error(`Error while sending message: ${messageStr}`);
139
+ (0, logger_1.getLogger)().error(JSON.stringify(err));
140
+ }
141
+ });
128
142
  });
129
143
  });
130
144
  }
@@ -133,7 +147,7 @@ class Daemon {
133
147
  command,
134
148
  data,
135
149
  ack: false,
136
- origin: chia_agent_service,
150
+ origin: this._serviceName,
137
151
  destination,
138
152
  request_id: (0, crypto_1.randomBytes)(32).toString("hex"),
139
153
  };
@@ -150,7 +164,7 @@ class Daemon {
150
164
  data: { success: true },
151
165
  ack: true,
152
166
  origin: "daemon",
153
- destination: chia_agent_service,
167
+ destination: service,
154
168
  request_id: "",
155
169
  };
156
170
  }
@@ -244,7 +258,7 @@ class Daemon {
244
258
  (0, logger_1.getLogger)().info("ws connection opened");
245
259
  this._connectedUrl = url;
246
260
  this._openEventListeners.forEach(l => l(event));
247
- return this.subscribe(chia_agent_service);
261
+ return this.subscribe(this._serviceName);
248
262
  });
249
263
  }
250
264
  onError(error) {
@@ -252,8 +266,19 @@ class Daemon {
252
266
  this._errorEventListeners.forEach(l => l(error));
253
267
  }
254
268
  onMessage(event) {
255
- const payload = JSON.parse(event.data);
256
- const { request_id, origin, command } = payload;
269
+ let payload;
270
+ let request_id;
271
+ let origin;
272
+ let command;
273
+ try {
274
+ payload = JSON.parse(event.data);
275
+ ({ request_id, origin, command } = payload);
276
+ }
277
+ catch (err) {
278
+ (0, logger_1.getLogger)().error(`Failed to parse message data: ${JSON.stringify(err)}`);
279
+ (0, logger_1.getLogger)().error(`payload: ${event.data}`);
280
+ return;
281
+ }
257
282
  (0, logger_1.getLogger)().debug(`Arrived message. origin=${origin} command=${command} reqId=${request_id}`);
258
283
  const resolver = this._responseQueue[request_id];
259
284
  if (resolver) {
@@ -273,9 +298,11 @@ class Daemon {
273
298
  }
274
299
  onClose(event) {
275
300
  if (this._socket) {
276
- this._socket.removeEventListener("error", this.onError);
301
+ this._socket.off("error", this.onError);
277
302
  this._socket.removeEventListener("message", this.onMessage);
278
- this._socket.removeEventListener("close", this.onClose);
303
+ this._socket.off("close", this.onClose);
304
+ this._socket.off("ping", this.onPing);
305
+ this._socket.off("pong", this.onPong);
279
306
  this._socket = null;
280
307
  }
281
308
  this._closing = false;
@@ -289,4 +316,10 @@ class Daemon {
289
316
  this._onClosePromise = undefined;
290
317
  }
291
318
  }
319
+ onPing() {
320
+ (0, logger_1.getLogger)().debug("Received ping");
321
+ }
322
+ onPong() {
323
+ (0, logger_1.getLogger)().debug("Received pong");
324
+ }
292
325
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chia-agent",
3
- "version": "14.3.1",
3
+ "version": "14.3.3",
4
4
  "author": "ChiaMineJP <admin@chiamine.jp>",
5
5
  "description": "chia rpc/websocket client library",
6
6
  "license": "MIT",