Skip to content

Commit

Permalink
Merge pull request #64 from Chia-Mine/v14.3.2
Browse files Browse the repository at this point in the history
V14.3.2
  • Loading branch information
ChiaMineJP authored Oct 11, 2024
2 parents 58856ac + 12b7dcf commit 03601a8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [14.3.2]
### Changed
- Error logs are generated when
- an error occurs during sending a message.
- it receives a websocket message with unexpected format.
- Debug logs are generated when ping/pong events are triggered.

## [14.3.1]
### Changed
- Added a `Host` header when sending https request to a remote host.
Expand Down Expand Up @@ -1708,6 +1715,7 @@ daemon.sendMessage(destination, get_block_record_by_height_command, data);
Initial release.
<!-- [Unreleased]: https://github.com/Chia-Mine/chia-agent/compare/v0.0.1...v0.0.2 -->
[14.3.2]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.1...v14.3.2
[14.3.1]: https://github.com/Chia-Mine/chia-agent/compare/v14.3.0...v14.3.1
[14.3.0]: https://github.com/Chia-Mine/chia-agent/compare/v14.2.2...v14.3.0
[14.2.2]: https://github.com/Chia-Mine/chia-agent/compare/v14.2.1...v14.2.2
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chia-agent",
"version": "14.3.1",
"version": "14.3.2",
"author": "ChiaMineJP <[email protected]>",
"description": "chia rpc/websocket client library",
"license": "MIT",
Expand Down
18 changes: 14 additions & 4 deletions src/daemon/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ function create(url: string) {
return new WS(url, options);
}

const defaultTimeoutInMs = 50000;
export const defaultTimeoutInMs = 50000;

export function open(url: string, timeoutMs?: number): Promise<{ ws: WS, openEvent: Event }> {
return new Promise((resolve, reject) => {
const ws = create(url);
let timer: ReturnType<typeof setTimeout> | null = null;
timeoutMs = typeof timeoutMs === "number" ? timeoutMs : defaultTimeoutInMs;
let opened = false;

timer = setTimeout(() => {
timer = null;
Expand All @@ -43,14 +44,23 @@ export function open(url: string, timeoutMs?: number): Promise<{ ws: WS, openEve
reject("Timeout");
}, timeoutMs);

ws.onopen = (openEvent) => {
const onOpenError = (err: WS.ErrorEvent) => {
if (!opened) {
reject(err);
}
};

ws.addEventListener("open", (openEvent: WS.Event)=> {
opened = true;
ws.removeEventListener("error", onOpenError);

if (timer !== null) {
clearTimeout(timer);
timer = null;
resolve({ws, openEvent});
}
};
});

ws.onerror = (err) => reject(err);
ws.addEventListener("error", onOpenError);
});
}
47 changes: 40 additions & 7 deletions src/daemon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Daemon {
this.onError = this.onError.bind(this);
this.onMessage = this.onMessage.bind(this);
this.onClose = this.onClose.bind(this);
this.onPing = this.onPing.bind(this);
this.onPong = this.onPong.bind(this);
}

/**
Expand Down Expand Up @@ -110,9 +112,11 @@ class Daemon {

const result = await open(daemonServerURL, timeoutMs);
this._socket = result.ws;
this._socket.onerror = this.onError;
this._socket.onmessage = this.onMessage;
this._socket.onclose = this.onClose;
this._socket.addEventListener("error", this.onError);
this._socket.addEventListener("message", this.onMessage);
this._socket.addEventListener("close", this.onClose);
this._socket.addListener("ping", this.onPing);
this._socket.addListener("pong", this.onPong);

await this.onOpen(result.openEvent, daemonServerURL);

Expand Down Expand Up @@ -145,7 +149,14 @@ class Daemon {
this._responseQueue[reqId] = resolve as (v: unknown) => void;

getLogger().debug(`Sending message. dest=${destination} command=${command} reqId=${reqId}`);
this._socket.send(JSON.stringify(message));
const messageStr = JSON.stringify(message);

this._socket.send(messageStr, (err: Error|undefined) => {
if(err){
getLogger().error(`Error while sending message: ${messageStr}`);
getLogger().error(JSON.stringify(err));
}
});
});
}

Expand Down Expand Up @@ -289,10 +300,22 @@ class Daemon {
}

protected onMessage(event: MessageEvent){
const payload = JSON.parse(event.data as string) as WsMessage;
const {request_id, origin, command} = payload;
let payload: WsMessage;
let request_id: string;
let origin: WsMessage["origin"];
let command: WsMessage["command"];

try {
payload = JSON.parse(event.data as string) as WsMessage;
({request_id, origin, command} = payload);
} catch(err: unknown){
getLogger().error(`Failed to parse message data: ${JSON.stringify(err)}`);
getLogger().error(`payload: ${event.data}`);
return;
}

getLogger().debug(`Arrived message. origin=${origin} command=${command} reqId=${request_id}`);

const resolver = this._responseQueue[request_id];
if(resolver){
delete this._responseQueue[request_id];
Expand All @@ -317,6 +340,8 @@ class Daemon {
this._socket.removeEventListener("error", this.onError);
this._socket.removeEventListener("message", this.onMessage);
this._socket.removeEventListener("close", this.onClose);
this._socket.removeListener("ping", this.onPing);
this._socket.removeListener("pong", this.onPong);
this._socket = null;
}

Expand All @@ -333,6 +358,14 @@ class Daemon {
this._onClosePromise = undefined;
}
}

protected onPing() {
getLogger().debug("Received ping");
}

protected onPong() {
getLogger().debug("Received pong");
}
}

export type TDaemon = InstanceType<typeof Daemon>;

0 comments on commit 03601a8

Please sign in to comment.