Skip to content

Commit c3cc58c

Browse files
committed
Stop using EventEmitter
1 parent 057c098 commit c3cc58c

File tree

11 files changed

+262
-214
lines changed

11 files changed

+262
-214
lines changed

README.md

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ JavaScript (Node.js and browsers) library for [aria2, "The next generation downl
66
- [Introduction](#introduction)
77
- [Getting started](#getting-started)
88
- [Usage](#usage)
9-
- [open](#open)
10-
- [close](#close)
119
- [call](#call)
1210
- [multicall](#multicall)
1311
- [batch](#batch)
12+
- [open](#open)
13+
- [close](#close)
1414
- [listNotifications](#listnotifications)
1515
- [listMethods](#listmethods)
1616
- [events](#events)
@@ -19,19 +19,17 @@ JavaScript (Node.js and browsers) library for [aria2, "The next generation downl
1919

2020
aria2.js controls aria2 via its [JSON-RPC interface](https://aria2.github.io/manual/en/html/aria2c.html#rpc-interface) and features
2121

22-
- Node.js and browsers support
23-
- multiple transports
24-
- [HTTP](https://aria2.github.io/manual/en/html/aria2c.html#rpc-interface)
25-
- [WebSocket](https://aria2.github.io/manual/en/html/aria2c.html#json-rpc-over-websocket)
26-
- promise API
22+
- support for browsers, Node.js, React Native, deno and gjs environments
23+
- support for [HTTP](https://aria2.github.io/manual/en/html/aria2c.html#rpc-interface) and [WebSocket](https://aria2.github.io/manual/en/html/aria2c.html#json-rpc-over-websocket) transports
24+
- modern ECMAScript API
2725

28-
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).
26+
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) to get an idea of what aria2 and aria2.js are capable of.
2927

3028
[](#aria2js)
3129

3230
## Getting started
3331

34-
Start aria2 with rpc, example:
32+
Start aria2 in daemon mode with rpc, example:
3533

3634
`aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all`
3735

@@ -46,6 +44,8 @@ Install aria2.js
4644
```javascript
4745
const Aria2 = require("aria2");
4846
const aria2 = new Aria2([options]);
47+
aria2.onerror = console.error;
48+
aria2.onnotification = console.log;
4949
```
5050

5151
default options match aria2c defaults and are
@@ -64,34 +64,6 @@ default options match aria2c defaults and are
6464

6565
If the WebSocket is open (via the [open method](#open)) aria2.js will use the WebSocket transport, otherwise the HTTP transport.
6666

67-
The `"aria2."` prefix can be omitted from both methods and notifications.
68-
69-
[](#aria2js)
70-
71-
### open
72-
73-
`aria2.open()` opens the WebSocket connection. All subsequent requests will use the WebSocket transport instead of HTTP.
74-
75-
```javascript
76-
aria2
77-
.open()
78-
.then(() => console.log("open"))
79-
.catch((err) => console.log("error", err));
80-
```
81-
82-
[](#aria2js)
83-
84-
### close
85-
86-
`aria2.close()` closes the WebSocket connection. All subsequent requests will use the HTTP transport instead of WebSocket.
87-
88-
```javascript
89-
aria2
90-
.close()
91-
.then(() => console.log("closed"))
92-
.catch((err) => console.log("error", err));
93-
```
94-
9567
[](#aria2js)
9668

9769
### call
@@ -103,7 +75,7 @@ Example using [`addUri`](https://aria2.github.io/manual/en/html/aria2c.html#aria
10375
```javascript
10476
const magnet =
10577
"magnet:?xt=urn:btih:88594AAACBDE40EF3E2510C47374EC0AA396C08E&dn=bbb_sunflower_1080p_30fps_normal.mp4&tr=udp%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2ftracker.publicbt.com%3a80%2fannounce&ws=http%3a%2f%2fdistribution.bbb3d.renderfarming.net%2fvideo%2fmp4%2fbbb_sunflower_1080p_30fps_normal.mp4";
106-
const [guid] = await aria2.call("addUri", [magnet], { dir: "/tmp" });
78+
const guid = await aria2.call("addUri", [magnet], { dir: "/tmp" });
10779
```
10880

10981
[](#aria2js)
@@ -121,6 +93,8 @@ const multicall = [
12193
const results = await aria2.multicall(multicall);
12294
```
12395

96+
[](#aria2js)
97+
12498
### batch
12599

126100
`aria2.batch()` is a helper for [batch](https://aria2.github.io/manual/en/html/aria2c.html#system.multicall). It behaves the same as [multicall](#multicall) except it returns an array of promises which gives more flexibility in handling errors.
@@ -136,6 +110,32 @@ const promises = await aria2.batch(batch);
136110

137111
[](#aria2js)
138112

113+
### open
114+
115+
`aria2.open()` opens the WebSocket connection. All subsequent requests will use the WebSocket transport instead of HTTP.
116+
117+
```javascript
118+
aria2
119+
.open()
120+
.then(() => console.log("open"))
121+
.catch((err) => console.log("error", err));
122+
```
123+
124+
[](#aria2js)
125+
126+
### close
127+
128+
`aria2.close()` closes the WebSocket connection. All subsequent requests will use the HTTP transport instead of WebSocket.
129+
130+
```javascript
131+
aria2
132+
.close()
133+
.then(() => console.log("closed"))
134+
.catch((err) => console.log("error", err));
135+
```
136+
137+
[](#aria2js)
138+
139139
### listNotifications
140140

141141
`aria2.listNotifications()` is a helper for [system.listNotifications](https://aria2.github.io/manual/en/html/aria2c.html#system.listNotifications). The difference with `aria2.call('listNotifications')` is that it removes the `"aria2."` prefix from the results.
@@ -152,20 +152,13 @@ const notifications = await aria2.listNotifications();
152152
'onBtDownloadComplete'
153153
]
154154
*/
155-
156-
// notifications logger example
157-
notifications.forEach((notification) => {
158-
aria2.on(notification, (params) => {
159-
console.log("aria2", notification, params);
160-
});
161-
});
162155
```
163156

164157
[](#aria2js)
165158

166159
### listMethods
167160

168-
`aria2.listMethods()` is a helper for [system.listMethods](https://aria2.github.io/manual/en/html/aria2c.html#system.listMethods). The difference with `aria2.call('listMethods')` is that it removes the `"aria2."` prefix for the results.
161+
`aria2.listMethods()` is a helper for [system.listMethods](https://aria2.github.io/manual/en/html/aria2c.html#system.listMethods). The difference with `aria2.call('listMethods')` is that it removes the `"aria2."` prefix from the results.
169162

170163
```javascript
171164
const methods = await aria2.listMethods();
@@ -182,33 +175,39 @@ const methods = await aria2.listMethods();
182175
### events
183176

184177
```javascript
185-
// emitted when the WebSocket is open.
186-
aria2.on("open", () => {
187-
console.log("aria2 OPEN");
188-
});
189-
190-
// emitted when the WebSocket is closed.
191-
aria2.on("close", () => {
192-
console.log("aria2 CLOSE");
193-
});
194-
195-
// emitted for every message sent.
196-
aria2.on("output", (m) => {
197-
console.log("aria2 OUT", m);
198-
});
199-
200-
// emitted for every message received.
201-
aria2.on("input", (m) => {
202-
console.log("aria2 IN", m);
203-
});
204-
```
205-
206-
Additionally every [aria2 notifications](https://aria2.github.io/manual/en/html/aria2c.html#notifications) received will be emitted as an event (with and without the `"aria2."` prefix). Only available when using WebSocket, see [open](#open).
207-
208-
```javascript
209-
aria2.on("onDownloadStart", ([guid]) => {
210-
console.log("aria2 onDownloadStart", guid);
211-
});
178+
// called when an error occurs
179+
aria2.onerror = (error) => {
180+
console.log("aria2", "ERROR");
181+
console.log(error);
182+
};
183+
184+
// called when a notification is received on the WebSocket
185+
aria2.onnotification = (name, params) => {
186+
console.log("aria2", "notification", name);
187+
console.log(params);
188+
};
189+
190+
// called when the WebSocket is open.
191+
aria2.onopen = () => {
192+
console.log("aria2", "OPEN");
193+
};
194+
195+
// called when the WebSocket is closed.
196+
aria2.onclose = () => {
197+
console.log("aria2", "CLOSE");
198+
};
199+
200+
// called for every message received.
201+
aria2.oninput = (message) => {
202+
console.log("aria2", "IN");
203+
console.log(message);
204+
};
205+
206+
// called for every message sent.
207+
aria2.onoutput = (message) => {
208+
console.log("aria2", "OUT");
209+
console.log(message);
210+
};
212211
```
213212

214213
[](#aria2js)

lib/Aria2.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
11
import JSONRPCClient from "./JSONRPCClient.js";
22

3-
function prefix(str) {
4-
if (!str.startsWith("system.") && !str.startsWith("aria2.")) {
5-
str = "aria2." + str;
6-
}
7-
return str;
8-
}
9-
10-
function unprefix(str) {
11-
const suffix = str.split("aria2.")[1];
12-
return suffix || str;
13-
}
14-
153
class Aria2 extends JSONRPCClient {
16-
addSecret(parameters) {
4+
_withSecret(parameters) {
175
let params = this.secret ? ["token:" + this.secret] : [];
186
if (Array.isArray(parameters)) {
197
params = params.concat(parameters);
208
}
219
return params;
2210
}
2311

24-
_onnotification(notification) {
12+
_handleNotification(notification) {
2513
const { method, params } = notification;
2614
const event = unprefix(method);
27-
if (event !== method) this.emit(event, params);
28-
return super._onnotification(notification);
15+
this.onnotification(event, params);
2916
}
3017

3118
async call(method, ...params) {
32-
return super.call(prefix(method), this.addSecret(params));
19+
return super.call(prefix(method), this._withSecret(params));
3320
}
3421

3522
async multicall(calls) {
3623
const multi = [
3724
calls.map(([method, ...params]) => {
38-
return { methodName: prefix(method), params: this.addSecret(params) };
25+
return { methodName: prefix(method), params: this._withSecret(params) };
3926
}),
4027
];
4128
return super.call("system.multicall", multi);
@@ -45,7 +32,7 @@ class Aria2 extends JSONRPCClient {
4532
return super.batch(
4633
calls.map(([method, ...params]) => [
4734
prefix(method),
48-
this.addSecret(params),
35+
this._withSecret(params),
4936
])
5037
);
5138
}
@@ -61,14 +48,29 @@ class Aria2 extends JSONRPCClient {
6148
}
6249
}
6350

64-
Object.assign(Aria2, { prefix, unprefix });
65-
66-
Aria2.defaultOptions = Object.assign({}, JSONRPCClient.defaultOptions, {
67-
secure: false,
68-
host: "localhost",
69-
port: 6800,
70-
secret: "",
71-
path: "/jsonrpc",
72-
});
51+
Aria2.defaultOptions = {
52+
...JSONRPCClient.defaultOptions,
53+
...{
54+
secure: false,
55+
host: "localhost",
56+
port: 6800,
57+
secret: "",
58+
path: "/jsonrpc",
59+
},
60+
};
7361

7462
export default Aria2;
63+
64+
function prefix(str) {
65+
if (!str.startsWith("system.") && !str.startsWith("aria2.")) {
66+
str = "aria2." + str;
67+
}
68+
return str;
69+
}
70+
71+
function unprefix(str) {
72+
const suffix = str.split("aria2.")[1];
73+
return suffix || str;
74+
}
75+
76+
Object.assign(Aria2, { prefix, unprefix });

0 commit comments

Comments
 (0)