Comparing version 3.0.1 to 4.0.0
"use strict"; | ||
var Aria2 = require(".."); | ||
const Aria2 = require(".."); | ||
module.exports = function(cli, options, method, params) { | ||
var debug = require("./debug")(cli); | ||
module.exports = async function(cli, options, method, params) { | ||
const debug = require("./debug")(cli); | ||
var client = new Aria2(options); | ||
const client = new Aria2(options); | ||
client.onsend = function(m) { | ||
@@ -16,15 +16,10 @@ debug("OUT", m); | ||
var cb = function(err, res) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
try { | ||
const res = await client.call(method, ...params); | ||
console.log(res); | ||
process.exit(0); | ||
}; | ||
var args = [method].concat(params, cb); | ||
client.send.apply(client, args); | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
}; |
@@ -5,11 +5,11 @@ #!/usr/bin/env node | ||
var url = require("url"); | ||
var cli = require("commander"); | ||
const url = require("url"); | ||
const cli = require("commander"); | ||
process.title = "aria2rpc"; | ||
var makeOptions = function() { | ||
var options = { secret: cli.secret }; | ||
const makeOptions = function() { | ||
const options = { secret: cli.secret }; | ||
if (cli.url) { | ||
var parsed = url.parse(cli.url); | ||
const parsed = url.parse(cli.url); | ||
options.secure = parsed.protocol === "wss:"; | ||
@@ -35,6 +35,9 @@ options.host = parsed.hostname; | ||
cli | ||
.command("call <method> [params...]") | ||
.description("call an aria2 RPC method and print result") | ||
.action(function(method, params) { | ||
var options = makeOptions(); | ||
.command("call <method> [params]") | ||
.description( | ||
"call an aria2 RPC method with params provided as a JSON array and print result" | ||
) | ||
.action((method, params) => { | ||
params = params ? JSON.parse(params) : []; | ||
const options = makeOptions(); | ||
require("./call")(cli, options, method, params); | ||
@@ -47,4 +50,4 @@ }); | ||
.description("start interactive console") | ||
.action(function() { | ||
var options = makeOptions(); | ||
.action(() => { | ||
const options = makeOptions(); | ||
require("./console")(cli, options); | ||
@@ -51,0 +54,0 @@ }); |
"use strict"; | ||
var readline = require("readline"); | ||
var Aria2 = require(".."); | ||
const readline = require("readline"); | ||
const Aria2 = require(".."); | ||
module.exports = function(cli, options) { | ||
var debug = require("./debug")(cli); | ||
module.exports = async function(cli, options) { | ||
const debug = require("./debug")(cli); | ||
var client = new Aria2(options); | ||
const client = new Aria2(options); | ||
client.onsend = function(m) { | ||
@@ -20,38 +20,40 @@ debug("OUT", m); | ||
debug("CONNECTING"); | ||
client.open(function(err) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
debug("CONNECTED"); | ||
try { | ||
await client.open(); | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
return; | ||
} | ||
var rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
rl.setPrompt("aria2rpc ≻ "); | ||
rl.prompt(); | ||
rl.on("line", function(line) { | ||
line = line.trim(); | ||
if (!line) return rl.prompt(); | ||
var params = line.split(" "); | ||
var cb = function(err, res) { | ||
if (err) console.error(err); | ||
else console.log(res); | ||
rl.prompt(); | ||
}; | ||
debug("CONNECTED"); | ||
var args = params.concat(cb); | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
rl.setPrompt("aria2rpc ≻ "); | ||
rl.prompt(); | ||
rl.on("line", async function(line) { | ||
line = line.trim(); | ||
if (!line) return rl.prompt(); | ||
const [method, args] = line.split(" "); | ||
const params = args ? JSON.parse(args) : []; | ||
client.send.apply(client, args); | ||
try { | ||
const res = await client.call(method, ...params); | ||
console.log(res); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
rl.prompt(); | ||
}); | ||
rl.on("close", function() { | ||
debug("CLOSING"); | ||
client.close(function() { | ||
debug("CLOSED"); | ||
process.exit(0); | ||
}); | ||
rl.on("close", function() { | ||
debug("CLOSING"); | ||
client.close(function() { | ||
debug("CLOSED"); | ||
process.exit(0); | ||
}); | ||
}); | ||
}); | ||
}; |
"use strict"; | ||
module.exports = function(cli) { | ||
return function() { | ||
return function(...args) { | ||
if (cli.debug) { | ||
console.log.apply(console, arguments); | ||
console.log(...args); | ||
} | ||
}; | ||
}; |
319
index.js
@@ -1,318 +0,5 @@ | ||
(function(global) { | ||
"use strict"; | ||
"use strict"; | ||
var WebSocket; | ||
var fetch; | ||
var pg; | ||
const Aria2 = require("./lib/Aria2"); | ||
var isNode = typeof module !== "undefined" && module.exports; | ||
if (isNode) { | ||
WebSocket = require("ws"); | ||
fetch = require("node-fetch"); | ||
pg = require("polygoat"); | ||
} else { | ||
WebSocket = global.WebSocket; | ||
fetch = global.fetch; | ||
pg = global.polygoat; | ||
} | ||
var Aria2 = function(opts) { | ||
this.callbacks = Object.create(null); | ||
this.lastId = 0; | ||
for (var i in Aria2.options) { | ||
this[i] = | ||
typeof opts === "object" && i in opts ? opts[i] : Aria2.options[i]; | ||
} | ||
}; | ||
Aria2.prototype.http = function(m, fn) { | ||
var that = this; | ||
var content = { | ||
method: m.method, | ||
id: m.id | ||
}; | ||
if (Array.isArray(m.params) && m.params.length > 0) { | ||
content.params = m.params; | ||
} | ||
var url = | ||
"http" + | ||
(this.secure ? "s" : "") + | ||
"://" + | ||
this.host + | ||
":" + | ||
this.port + | ||
this.path; | ||
fetch(url, { | ||
method: "POST", | ||
body: JSON.stringify(content), | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
.then(function(res) { | ||
return res.json(); | ||
}) | ||
.then(function(msg) { | ||
that._onmessage(msg); | ||
}) | ||
.catch(fn); | ||
}; | ||
Aria2.prototype.send = function( | ||
method /* [,param] [,param] [,...] [, fn] */ | ||
) { | ||
var params = Array.prototype.slice.call(arguments, 1); | ||
var cb = | ||
typeof params[params.length - 1] === "function" ? params.pop() : null; | ||
return this.exec(method, params, cb); | ||
}; | ||
Aria2.prototype.exec = function(method, parameters, cb) { | ||
if (typeof method !== "string") { | ||
throw new TypeError(method + " is not a string"); | ||
} | ||
if (method.indexOf("system.") !== 0 && method.indexOf("aria2.") !== 0) { | ||
method = "aria2." + method; | ||
} | ||
var m = { | ||
method: method, | ||
"json-rpc": "2.0", | ||
id: this.lastId++ | ||
}; | ||
var params = this.secret ? ["token:" + this.secret] : []; | ||
if (Array.isArray(parameters)) { | ||
params = params.concat(parameters); | ||
} | ||
if (params.length > 0) m.params = params; | ||
this.onsend(m); | ||
var that = this; | ||
// send via websocket | ||
if (this.socket && this.socket.readyState === 1) { | ||
this.socket.send(JSON.stringify(m)); | ||
// send via http | ||
} else { | ||
this.http(m, function(err) { | ||
that.callbacks[m.id](err); | ||
delete that.callbacks[m.id]; | ||
}); | ||
} | ||
return pg(function(done) { | ||
that.callbacks[m.id] = done; | ||
}, cb); | ||
}; | ||
Aria2.prototype._onmessage = function(m) { | ||
this.onmessage(m); | ||
if (m.id !== undefined) { | ||
var callback = this.callbacks[m.id]; | ||
if (callback) { | ||
if (m.error) { | ||
callback(m.error); | ||
} else { | ||
callback(null, m.result); | ||
} | ||
delete this.callbacks[m.id]; | ||
} | ||
} else if (m.method) { | ||
var n = m.method.split("aria2.")[1]; | ||
if ( | ||
n.indexOf("on") === 0 && | ||
typeof this[n] === "function" && | ||
Aria2.notifications.indexOf(n) > -1 | ||
) { | ||
this[n].apply(this, m.params); | ||
} | ||
} | ||
}; | ||
Aria2.prototype.open = function(fn) { | ||
var url = | ||
"ws" + | ||
(this.secure ? "s" : "") + | ||
"://" + | ||
this.host + | ||
":" + | ||
this.port + | ||
this.path; | ||
var socket = (this.socket = new WebSocket(url)); | ||
var that = this; | ||
var called = false; | ||
socket.onclose = function() { | ||
that.onclose(); | ||
}; | ||
socket.onmessage = function(event) { | ||
that._onmessage(JSON.parse(event.data)); | ||
}; | ||
return pg(function(done) { | ||
socket.onopen = function() { | ||
if (!called) { | ||
done(); | ||
called = true; | ||
} | ||
that.onopen(); | ||
}; | ||
socket.onerror = function(err) { | ||
if (!called) { | ||
done(err); | ||
called = true; | ||
} | ||
}; | ||
}, fn); | ||
}; | ||
Aria2.prototype.close = function(fn) { | ||
var socket = this.socket; | ||
return pg(function(done) { | ||
if (!socket) { | ||
done(); | ||
} else { | ||
socket.addEventListener("close", function() { | ||
done(); | ||
}); | ||
socket.close(); | ||
} | ||
}, fn); | ||
}; | ||
// https://aria2.github.io/manual/en/html/aria2c.html#methods | ||
Aria2.methods = [ | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.addUri | ||
"addUri", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.addTorrent | ||
"addTorrent", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.addMetalink | ||
"addMetalink", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.remove | ||
"remove", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.forceRemove | ||
"forceRemove", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.pause | ||
"pause", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.pauseAll | ||
"pauseAll", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.forcePause | ||
"forcePause", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.forcePauseAll | ||
"forcePauseAll", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.unpause | ||
"unpause", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.unpauseAll | ||
"unpauseAll", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellStatus | ||
"tellStatus", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getUris | ||
"getUris", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getFiles | ||
"getFiles", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getPeers | ||
"getPeers", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getServers | ||
"getServers", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellActive | ||
"tellActive", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellWaiting | ||
"tellWaiting", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellStopped | ||
"tellStopped", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.changePosition | ||
"changePosition", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.changeUri | ||
"changeUri", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getOption | ||
"getOption", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.changeOption | ||
"changeOption", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getGlobalOption | ||
"getGlobalOption", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.changeGlobalOption | ||
"changeGlobalOption", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getGlobalStat | ||
"getGlobalStat", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.purgeDownloadResult | ||
"purgeDownloadResult", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.removeDownloadResult | ||
"removeDownloadResult", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getVersion | ||
"getVersion", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.getSessionInfo | ||
"getSessionInfo", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.shutdown | ||
"shutdown", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.forceShutdown | ||
"forceShutdown", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.saveSession | ||
"saveSession", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#system.multicall | ||
"system.multicall", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#system.listMethods | ||
"system.listMethods", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#system.listNotifications | ||
"system.listNotifications" | ||
]; | ||
// https://aria2.github.io/manual/en/html/aria2c.html#notifications | ||
Aria2.notifications = [ | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadStart | ||
"onDownloadStart", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadPause | ||
"onDownloadPause", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadStop | ||
"onDownloadStop", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadComplete | ||
"onDownloadComplete", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadError | ||
"onDownloadError", | ||
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.onBtDownloadComplete | ||
"onBtDownloadComplete" | ||
]; | ||
Aria2.events = ["onopen", "onclose", "onsend", "onmessage"]; | ||
Aria2.options = { | ||
secure: false, | ||
host: "localhost", | ||
port: 6800, | ||
secret: "", | ||
path: "/jsonrpc" | ||
}; | ||
Aria2.methods.forEach(function(method) { | ||
var sufix = method.indexOf(".") > -1 ? method.split(".")[1] : method; | ||
Aria2.prototype[sufix] = function(/* [param] [,param] [,...] */) { | ||
return this.send.apply( | ||
this, | ||
[method].concat(Array.prototype.slice.call(arguments)) | ||
); | ||
}; | ||
}); | ||
Aria2.notifications.forEach(function(notification) { | ||
Aria2.prototype[notification] = function() {}; | ||
}); | ||
Aria2.events.forEach(function(event) { | ||
Aria2.prototype[event] = function() {}; | ||
}); | ||
if (isNode) { | ||
module.exports = Aria2; | ||
} else { | ||
global.Aria2 = Aria2; | ||
} | ||
})(this); | ||
module.exports = Aria2; |
{ | ||
"name": "aria2", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Library and cli for aria2, \"The next generation download utility.\"", | ||
@@ -27,5 +27,4 @@ "homepage": "https://github.com/sonnyp/aria2.js", | ||
"lint": "prettier -l **/*.js", | ||
"bundle": "cat node_modules/polygoat/index.js index.js > bundle.js", | ||
"unit": "mocha test/test.js", | ||
"test": "npm run bundle && npm run unit && npm run lint" | ||
"unit": "ava", | ||
"test": "npm run unit && npm run lint" | ||
}, | ||
@@ -36,12 +35,15 @@ "repository": "github:sonnyp/aria2.js", | ||
"node-fetch": "^2.1.2", | ||
"polygoat": "^1.1.4", | ||
"ws": "^5.1.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"mocha": "^5.1.1", | ||
"prettier": "1.12.1", | ||
"sinon": "^4.5.0", | ||
"sinon-chai": "^3.0.0" | ||
"ava": "^0.25.0", | ||
"prettier": "1.12.1" | ||
}, | ||
"engines": { | ||
"node": ">= 7.6.0" | ||
}, | ||
"browser": { | ||
"ws": false, | ||
"node-fetch": false | ||
} | ||
} |
230
README.md
@@ -5,3 +5,3 @@ # aria2.js | ||
[![license](https://img.shields.io/github/license/sonnyp/aria2.js.svg?maxAge=2592000&style=flat-square)](https://raw.githubusercontent.com/sonnyp/aria2.js/master/LICENSE.md) | ||
[![license](https://img.shields.io/github/license/sonnyp/aria2.js.svg?maxAge=2592000&style=flat-square)](https://raw.githubusercontent.com/sonnyp/aria2.js/master/LICENSE) | ||
@@ -19,49 +19,8 @@ [![Build Status](https://img.shields.io/travis/sonnyp/aria2.js/master.svg?style=flat-square)](https://travis-ci.org/sonnyp/aria2.js/branches) | ||
* [close](#close) | ||
* [onsend and onmessage](#onsend-and-onmessage) | ||
* [aria2 methods](#aria2-methods) | ||
* [addUri](https://aria2.github.io/manual/en/html/aria2c.html#aria2.addUri) | ||
* [addTorrent](https://aria2.github.io/manual/en/html/aria2c.html#aria2.addTorrent) | ||
* [addMetaLink](https://aria2.github.io/manual/en/html/aria2c.html#aria2.addMetalink) | ||
* [remove](https://aria2.github.io/manual/en/html/aria2c.html#aria2.remove) | ||
* [forceRemove](https://aria2.github.io/manual/en/html/aria2c.html#aria2.forceRemove) | ||
* [pause](https://aria2.github.io/manual/en/html/aria2c.html#aria2.pause) | ||
* [pauseAll](https://aria2.github.io/manual/en/html/aria2c.html#aria2.pauseAll) | ||
* [forcePause](https://aria2.github.io/manual/en/html/aria2c.html#aria2.forcePause) | ||
* [forcePauseAll](https://aria2.github.io/manual/en/html/aria2c.html#aria2.forcePauseAll) | ||
* [unpause](https://aria2.github.io/manual/en/html/aria2c.html#aria2.unpause) | ||
* [unpauseAll](https://aria2.github.io/manual/en/html/aria2c.html#aria2.unpauseAll) | ||
* [tellStatus](https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellStatus) | ||
* [getUris](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getUris) | ||
* [getFiles](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getFiles) | ||
* [getPeers](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getPeers) | ||
* [getServers](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getServers) | ||
* [tellActive](https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellActive) | ||
* [tellWaiting](https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellWaiting) | ||
* [tellStopped](https://aria2.github.io/manual/en/html/aria2c.html#aria2.tellStopped) | ||
* [changePosition](https://aria2.github.io/manual/en/html/aria2c.html#aria2.changePosition) | ||
* [changeUri](https://aria2.github.io/manual/en/html/aria2c.html#aria2.changeUri) | ||
* [getOption](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getOption) | ||
* [changeOption](https://aria2.github.io/manual/en/html/aria2c.html#aria2.changeOption) | ||
* [getGlobalOption](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getGlobalOption) | ||
* [changeGlobalOption](https://aria2.github.io/manual/en/html/aria2c.html#aria2.changeGlobalOption) | ||
* [getGlobalStat](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getGlobalStat) | ||
* [purgeDownloadResult](https://aria2.github.io/manual/en/html/aria2c.html#aria2.purgeDownloadResult) | ||
* [removeDownloadResult](https://aria2.github.io/manual/en/html/aria2c.html#aria2.removeDownloadResult) | ||
* [getVersion](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getVersion) | ||
* [getSessionInfo](https://aria2.github.io/manual/en/html/aria2c.html#aria2.getSessionInfo) | ||
* [shutdown](https://aria2.github.io/manual/en/html/aria2c.html#aria2.shutdown) | ||
* [forceShutdown](https://aria2.github.io/manual/en/html/aria2c.html#aria2.forceShutdown) | ||
* [saveSession](https://aria2.github.io/manual/en/html/aria2c.html#aria2.saveSession) | ||
* [system.multicall](https://aria2.github.io/manual/en/html/aria2c.html#system.multicall) | ||
* [system.listMethods](https://aria2.github.io/manual/en/html/aria2c.html#system.listMethods) | ||
* [system.listNotifications](https://aria2.github.io/manual/en/html/aria2c.html#system.listNotifications) | ||
* [aria2 events](#aria2-events) | ||
* [onDownloadStart](https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadStart) | ||
* [onDownloadPause](https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadPause) | ||
* [onDownloadStop](https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadStop) | ||
* [onDownloadComplete](https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadComplete) | ||
* [onDownloadError](https://aria2.github.io/manual/en/html/aria2c.html#aria2.onDownloadError) | ||
* [onBtDownloadComplete](https://aria2.github.io/manual/en/html/aria2c.html#aria2.onBtDownloadComplete) | ||
* [Example](#example) | ||
* [Contributing](#contributing) | ||
* [call](#call) | ||
* [multicall](#multicall) | ||
* [batch](#batch) | ||
* [listNotifications](#listNotifications) | ||
* [listMethods](#listMethods) | ||
* [events](#events) | ||
@@ -77,6 +36,6 @@ # Introduction | ||
* ~~[JSONP](https://aria2.github.io/manual/en/html/aria2c.html#json-rpc-using-http-get)~~ [#25](https://github.com/sonnyp/aria2.js/pull/25) | ||
* callback API | ||
* promise API | ||
* light (1.5KB minified and gzipped) | ||
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). | ||
[↑](#aria2js) | ||
@@ -86,26 +45,14 @@ | ||
`npm install aria2` | ||
Start aria2c in daemon mode with | ||
--- | ||
`aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all` | ||
```javascript | ||
var Aria2 = require("aria2"); | ||
``` | ||
Install aria2 | ||
or | ||
`npm install aria2` | ||
```xml | ||
<script src="node_modules/aria2/bundle.js"></script> | ||
``` | ||
```javascript | ||
var Aria2 = window.Aria2; | ||
const Aria2 = require("aria2"); | ||
``` | ||
--- | ||
Start aria2c in daemon mode with | ||
`aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all` | ||
[↑](#aria2js) | ||
@@ -116,3 +63,3 @@ | ||
```javascript | ||
var aria2 = new Aria2([options]); | ||
const aria2 = new Aria2([options]); | ||
``` | ||
@@ -132,7 +79,7 @@ | ||
`secret` is optional and refers to [--rpc-secret](https://aria2.github.io/manual/en/html/aria2c.html#cmdoption--rpc-secret). | ||
`secret` is optional and refers to [--rpc-secret](https://aria2.github.io/manual/en/html/aria2c.html#cmdoption--rpc-secret). If you define it, it will be added to every call for you. | ||
If the WebSocket is open (via the [open method](#open)) aria2.js will use the WebSocket transport, otherwise the HTTP transport. | ||
For HTTP, aria2.js makes use of the new fetch standard, you might need a [polyfill](https://github.com/github/fetch) if you want to support older browsers. | ||
The `"aria2."` prefix can be omitted from both methods and notifications. | ||
@@ -146,9 +93,6 @@ [↑](#aria2js) | ||
```javascript | ||
aria2.onopen = function() { | ||
console.log("aria2 open"); | ||
}; | ||
aria2.open([cb]); | ||
// or | ||
aria2.open().then(fn); | ||
aria2 | ||
.open() | ||
.then(() => console.log("open")) | ||
.catch(err => console.log("error", err)); | ||
``` | ||
@@ -163,9 +107,6 @@ | ||
```javascript | ||
aria2.onclose = function() { | ||
console.log("aria2 closed!"); | ||
}; | ||
aria2.close([cb]); // callback style | ||
// or | ||
aria2.close().then(fn); // promise style | ||
aria2 | ||
.close() | ||
.then(() => console.log("closed")) | ||
.catch(err => console.log("error", err)); | ||
``` | ||
@@ -175,14 +116,11 @@ | ||
## onsend and onmessage | ||
## call | ||
`aria2.onsend()` is called for every message sent. | ||
`aria2.onmessage()` is called for every message received. | ||
`aria2.call()` calls a method. Parameters are provided as arguments. | ||
```javascript | ||
aria2.onsend = function(m) { | ||
console.log("aria2 OUT", m); | ||
}; | ||
aria2.onmessage = function(m) { | ||
console.log("aria2 IN", m); | ||
}; | ||
// http://bbb3d.renderfarming.net/download.html | ||
const magnet = | ||
"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"; | ||
const [guid] = await aria2.call("addUri", [magnet], { dir: "/tmp" }); | ||
``` | ||
@@ -192,36 +130,53 @@ | ||
## aria2 methods | ||
## multicall | ||
For a complete listing see [aria2 methods](https://aria2.github.io/manual/en/html/aria2c.html#methods). | ||
`aria2.multicall()` is a helper for [system.multicall](https://aria2.github.io/manual/en/html/aria2c.html#system.multicall). It returns an array of results or throw if any of the call failed. | ||
If you passed the secret option to aria2.js, it will be automatically added to every request so there is no need to include it. | ||
```javascript | ||
const multicall = [ | ||
[methodA, param1, param2], | ||
[methodB, param1, param2] | ||
] | ||
For every aria2 methods you can use | ||
const results = await aria2.multicall(multicall); | ||
``` | ||
#### callback style | ||
## batch | ||
`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. | ||
```javascript | ||
aria2.getVersion([params,] function(err, res) { | ||
console.log(err || res); | ||
}); | ||
const batch = [ | ||
[methodA, param1, param2], | ||
[methodB, param1, param2] | ||
] | ||
const promises = await aria2.batch(batch); | ||
``` | ||
or | ||
[↑](#aria2js) | ||
```javascript | ||
aria2.send('getVersion', [params,] function(err, res) { | ||
console.log(err || res); | ||
}); | ||
``` | ||
## listNotifications | ||
#### promise style | ||
`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. | ||
```javascript | ||
aria2.getVersion([params]).then(fn); | ||
``` | ||
const notifications = await aria2.listNotifications(); | ||
/* | ||
[ | ||
'onDownloadStart', | ||
'onDownloadPause', | ||
'onDownloadStop', | ||
'onDownloadComplete', | ||
'onDownloadError', | ||
'onBtDownloadComplete' | ||
] | ||
*/ | ||
or | ||
```javascript | ||
aria2.send("getVersion", [params]).then(fn); | ||
// notifications logger example | ||
notifications.forEach((notification) => { | ||
aria2.on(notification, (params) => { | ||
console.log('aria2', notification, params) | ||
}) | ||
}) | ||
``` | ||
@@ -231,12 +186,14 @@ | ||
## aria2 events | ||
## listMethods | ||
For a complete listing see [aria2 notifications](https://aria2.github.io/manual/en/html/aria2c.html#json-rpc-over-websocket). | ||
`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. | ||
For every notifications you can bind a function. | ||
```javascript | ||
const methods = await aria2.listMethods(); | ||
/* | ||
[ 'addUri', | ||
[...] | ||
'system.listNotifications' ] | ||
```javascript | ||
aria2.onDownloadStart = function(gid) { | ||
console.log(gid); | ||
}; | ||
*/ | ||
``` | ||
@@ -246,15 +203,34 @@ | ||
# Example | ||
## events | ||
See [example](https://github.com/sonnyp/aria2.js/blob/master/example/). | ||
```javascript | ||
// emitted when the WebSocket is open. | ||
aria2.on('open' () => { | ||
console.log('aria2 OPEN'); | ||
}); | ||
[↑](#aria2js) | ||
// emitted when the WebSocket is closed. | ||
aria2.on('close' () => { | ||
console.log('aria2 CLOSE'); | ||
}); | ||
# Tests | ||
// emitted for every message sent. | ||
aria2.on("output", m => { | ||
console.log("aria2 OUT", m); | ||
}); | ||
// emitted for every message received. | ||
aria2.on("input", m => { | ||
console.log("aria2 IN", m); | ||
}); | ||
``` | ||
npm install | ||
npm test | ||
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). | ||
```javascript | ||
aria2.on("onDownloadStart", [guid] => { | ||
console.log("aria2 onDownloadStart", guid); | ||
}); | ||
``` | ||
[↑](#aria2js) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3
2
18
31497
391
225
4
- Removedpolygoat@^1.1.4
- Removedpolygoat@1.1.4(transitive)