Skip to content

Commit

Permalink
fix unhandled exception on json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Nov 14, 2020
1 parent 570c536 commit 7ef09b7
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 551 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js

node_js:
- '10'
- '12'
- '13'
- "10"
- "12"
- "14"
- "15"
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ aria2.js controls aria2 via its [JSON-RPC interface](https://aria2.github.io/man
* multiple transports
* [HTTP](https://aria2.github.io/manual/en/html/aria2c.html#rpc-interface)
* [WebSocket](https://aria2.github.io/manual/en/html/aria2c.html#json-rpc-over-websocket)
* ~~[JSONP](https://aria2.github.io/manual/en/html/aria2c.html#json-rpc-using-http-get)~~ [#25](https://github.com/sonnyp/aria2.js/pull/25)
* promise API

See [aria2 methods](https://aria2.github.io/manual/en/html/aria2c.html#methods) and [aria2 notifications](https://aria2.github.io/manual/en/html/aria2c.html#notifications).
Expand Down
10 changes: 5 additions & 5 deletions lib/Aria2.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Aria2 extends JSONRPCClient {
const multi = [
calls.map(([method, ...params]) => {
return { methodName: prefix(method), params: this.addSecret(params) };
})
}),
];
return super.call("system.multicall", multi);
}
Expand All @@ -47,19 +47,19 @@ class Aria2 extends JSONRPCClient {
return super.batch(
calls.map(([method, ...params]) => [
prefix(method),
this.addSecret(params)
this.addSecret(params),
])
);
}

async listNotifications() {
const events = await this.call("system.listNotifications");
return events.map(event => unprefix(event));
return events.map((event) => unprefix(event));
}

async listMethods() {
const methods = await this.call("system.listMethods");
return methods.map(method => unprefix(method));
return methods.map((method) => unprefix(method));
}
}

Expand All @@ -70,7 +70,7 @@ Aria2.defaultOptions = Object.assign({}, JSONRPCClient.defaultOptions, {
host: "localhost",
port: 6800,
secret: "",
path: "/jsonrpc"
path: "/jsonrpc",
});

module.exports = Aria2;
35 changes: 24 additions & 11 deletions lib/JSONRPCClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class JSONRPCClient extends EventEmitter {

websocket(message) {
return new Promise((resolve, reject) => {
const cb = err => {
const cb = (err) => {
if (err) reject(err);
else resolve();
};
Expand All @@ -48,18 +48,21 @@ class JSONRPCClient extends EventEmitter {
}

async http(message) {
const response = this.fetch(this.url("http"), {
const response = await this.fetch(this.url("http"), {
method: "POST",
body: JSON.stringify(message),
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
"Content-Type": "application/json",
},
});

response.then(async res => {
this._onmessage(await res.json());
});
response
.json()
.then(this._onmessage)
.catch((err) => {
this.emit("error", err);
});

return response;
}
Expand All @@ -72,7 +75,7 @@ class JSONRPCClient extends EventEmitter {
const message = {
method,
"json-rpc": "2.0",
id: this.id()
id: this.id(),
};

if (params) Object.assign(message, { params });
Expand Down Expand Up @@ -152,12 +155,22 @@ class JSONRPCClient extends EventEmitter {
socket.onclose = (...args) => {
this.emit("close", ...args);
};
socket.onmessage = event => {
this._onmessage(JSON.parse(event.data));
socket.onmessage = (event) => {
let message;
try {
message = JSON.parse(event.data);
} catch (err) {
this.emit("error", err);
return;
}
this._onmessage(message);
};
socket.onopen = (...args) => {
this.emit("open", ...args);
};
socket.onerror = (...args) => {
this.emit("error", ...args);
};

return promiseEvent(this, "open");
}
Expand All @@ -176,7 +189,7 @@ JSONRPCClient.defaultOptions = {
secret: "",
path: "/jsonrpc",
fetch,
WebSocket
WebSocket,
};

module.exports = JSONRPCClient;
6 changes: 3 additions & 3 deletions lib/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { inspect } = require("util");

module.exports = aria2 => {
module.exports = (aria2) => {
aria2.on("open", () => {
console.log("aria2", "OPEN");
});
Expand All @@ -11,12 +11,12 @@ module.exports = aria2 => {
console.log("aria2", "CLOSE");
});

aria2.on("input", m => {
aria2.on("input", (m) => {
console.log("aria2", "IN");
console.log(inspect(m, { depth: null, colors: true }));
});

aria2.on("output", m => {
aria2.on("output", (m) => {
console.log("aria2", "OUT");
console.log(inspect(m, { depth: null, colors: true }));
});
Expand Down
Loading

0 comments on commit 7ef09b7

Please sign in to comment.