Comparing version 1.1.0 to 1.2.0
113
Aria2.js
@@ -1,2 +0,2 @@ | ||
(function(global) { | ||
;(function (global) { | ||
'use strict' | ||
@@ -7,12 +7,11 @@ | ||
var httpclient | ||
var noop = function() {} | ||
var noop = function () {} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
WebSocket = require('ws') | ||
b64 = function(str) { | ||
b64 = function (str) { | ||
return new Buffer(str).toString('base64') | ||
} | ||
httpclient = require('httpclient') | ||
} | ||
else { | ||
} else { | ||
WebSocket = global.WebSocket | ||
@@ -23,3 +22,3 @@ b64 = global.atob | ||
var Aria2 = function(opts) { | ||
var Aria2 = function (opts) { | ||
this.callbacks = Object.create(null) | ||
@@ -32,4 +31,4 @@ this.lastId = 0 | ||
Aria2.prototype.http = function(m, fn) { | ||
//FIXME json-rpc post wouldn't work | ||
Aria2.prototype.http = function (m, fn) { | ||
// FIXME json-rpc post wouldn't work | ||
var opts = { | ||
@@ -42,16 +41,18 @@ 'host': this.host, | ||
'method': m.method, | ||
'id': m.id, | ||
}, | ||
'id': m.id | ||
} | ||
} | ||
if (Array.isArray(m.params) && m.params.length > 0) | ||
if (Array.isArray(m.params) && m.params.length > 0) { | ||
opts.query.params = b64(JSON.stringify(m.params)) | ||
} | ||
//browser, use jsonp | ||
if (typeof module !== 'undefined' && module.exports) | ||
// browser, use jsonp | ||
if (typeof module !== 'undefined' && module.exports) { | ||
opts.jsonp = 'jsoncallback' | ||
} | ||
var that = this | ||
httpclient.request(opts, function(err, res) { | ||
httpclient.request(opts, function (err, res) { | ||
if (err) return fn(err) | ||
@@ -64,10 +65,15 @@ | ||
Aria2.prototype.send = function(method /* [,param] [,param] [,...] [, fn]*/) { | ||
if (typeof method !== 'string') | ||
Aria2.prototype.send = function (method /* [,param] [,param] [,...] [, fn]*/) { | ||
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': 'aria2.' + method, | ||
'method': method, | ||
'json-rpc': '2.0', | ||
'id': this.lastId++, | ||
'id': this.lastId++ | ||
} | ||
@@ -79,6 +85,7 @@ | ||
var arg = arguments[i] | ||
if (i === arguments.length - 1 && typeof arg === 'function') | ||
if (i === arguments.length - 1 && typeof arg === 'function') { | ||
this.callbacks[m.id] = arg | ||
else | ||
} else { | ||
params.push(arg) | ||
} | ||
} | ||
@@ -90,10 +97,11 @@ | ||
//send via websocket | ||
if (this.socket && this.socket.readyState === 1) | ||
// send via websocket | ||
if (this.socket && this.socket.readyState === 1) { | ||
return this.socket.send(JSON.stringify(m)) | ||
} | ||
var that = this | ||
//send via http | ||
this.http(m, function(err) { | ||
// send via http | ||
this.http(m, function (err) { | ||
that.callbacks[m.id](err) | ||
@@ -104,3 +112,3 @@ delete that.callbacks[m.id] | ||
Aria2.prototype._onmessage = function(m) { | ||
Aria2.prototype._onmessage = function (m) { | ||
this.onmessage(m) | ||
@@ -111,18 +119,18 @@ | ||
if (callback) { | ||
if (m.error) | ||
if (m.error) { | ||
callback(m.error) | ||
else | ||
} else { | ||
callback(null, m.result) | ||
} | ||
delete this.callbacks[m.id] | ||
} | ||
} | ||
else if (m.method) { | ||
} 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) | ||
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) { | ||
Aria2.prototype.open = function (fn) { | ||
var url = 'ws' + (this.secure ? 's' : '') + '://' + this.host + ':' + this.port + this.path | ||
@@ -133,3 +141,3 @@ var socket = this.socket = new WebSocket(url) | ||
socket.onopen = function() { | ||
socket.onopen = function () { | ||
if (fn && !called) { | ||
@@ -141,3 +149,3 @@ fn() | ||
} | ||
socket.onerror = function(err) { | ||
socket.onerror = function (err) { | ||
if (fn && !called) { | ||
@@ -148,6 +156,6 @@ fn(err) | ||
} | ||
socket.onclose = function() { | ||
socket.onclose = function () { | ||
that.onclose() | ||
} | ||
socket.onmessage = function(event) { | ||
socket.onmessage = function (event) { | ||
that._onmessage(JSON.parse(event.data)) | ||
@@ -157,3 +165,3 @@ } | ||
Aria2.prototype.close = function(fn) { | ||
Aria2.prototype.close = function (fn) { | ||
fn = fn || noop | ||
@@ -165,3 +173,3 @@ if (!this.socket) { | ||
this.socket.addEventListener('close', function() { | ||
this.socket.addEventListener('close', function () { | ||
fn() | ||
@@ -239,5 +247,7 @@ }) | ||
// http://aria2.sourceforge.net/manual/en/html/aria2c.html#aria2.saveSession | ||
'saveSession', | ||
// http://aria2.sourceforge.net/manual/en/html/aria2c.html#system.multicall | ||
// multicall: {}, | ||
'saveSession' | ||
// http://aria2.sourceforge.net/manual/en/html/aria2c.html#system.multicall | ||
// 'system.multicall', | ||
// http://aria2.sourceforge.net/manual/en/html/aria2c.html#system.listMethods | ||
// 'system.listMethods', | ||
] | ||
@@ -258,3 +268,3 @@ | ||
// http://aria2.sourceforge.net/manual/en/html/aria2c.html#aria2.onBtDownloadComplete | ||
'onBtDownloadComplete', | ||
'onBtDownloadComplete' | ||
] | ||
@@ -266,3 +276,3 @@ | ||
'onsend', | ||
'onmessage', | ||
'onmessage' | ||
] | ||
@@ -275,7 +285,7 @@ | ||
'secret': '', | ||
'path': '/jsonrpc', | ||
'path': '/jsonrpc' | ||
} | ||
Aria2.methods.forEach(function(method) { | ||
Aria2.prototype[method] = function(/* [param] [,param] [,...]*/) { | ||
Aria2.methods.forEach(function (method) { | ||
Aria2.prototype[method] = function (/* [param] [,param] [,...]*/) { | ||
this.send.apply(this, [method].concat(Array.prototype.slice.call(arguments))) | ||
@@ -285,8 +295,8 @@ } | ||
Aria2.notifications.forEach(function(notification) { | ||
Aria2.prototype[notification] = function() {} | ||
Aria2.notifications.forEach(function (notification) { | ||
Aria2.prototype[notification] = function () {} | ||
}) | ||
Aria2.events.forEach(function(event) { | ||
Aria2.prototype[event] = function() {} | ||
Aria2.events.forEach(function (event) { | ||
Aria2.prototype[event] = function () {} | ||
}) | ||
@@ -296,6 +306,5 @@ | ||
module.exports = Aria2 | ||
} | ||
else { | ||
} else { | ||
global.Aria2 = Aria2 | ||
} | ||
}(this)) |
@@ -5,14 +5,14 @@ 'use strict' | ||
module.exports = function(cli, options, method, params) { | ||
module.exports = function (cli, options, method, params) { | ||
var debug = require('./debug')(cli) | ||
var client = new Aria2(options) | ||
client.onsend = function(m) { | ||
client.onsend = function (m) { | ||
debug('OUT', m) | ||
} | ||
client.onmessage = function(m) { | ||
client.onmessage = function (m) { | ||
debug('IN', m) | ||
} | ||
debug('CONNECTING') | ||
client.open(function(err) { | ||
client.open(function (err) { | ||
if (err) { | ||
@@ -25,5 +25,5 @@ console.error(err) | ||
var cb = function(err, res) { | ||
var cb = function (err, res) { | ||
debug('CLOSING') | ||
client.close(function() { | ||
client.close(function () { | ||
debug('CLOSED') | ||
@@ -30,0 +30,0 @@ if (err) { |
@@ -10,3 +10,3 @@ #!/usr/bin/env node | ||
var makeOptions = function() { | ||
var makeOptions = function () { | ||
var options = {'secret': cli.secret} | ||
@@ -33,3 +33,3 @@ if (cli.url) { | ||
.description('call an aria2 RPC method and print result') | ||
.action(function(method, params) { | ||
.action(function (method, params) { | ||
var options = makeOptions() | ||
@@ -43,3 +43,3 @@ require('./call')(cli, options, method, params) | ||
.description('start interactive console') | ||
.action(function() { | ||
.action(function () { | ||
var options = makeOptions() | ||
@@ -46,0 +46,0 @@ require('./console')(cli, options) |
@@ -6,10 +6,10 @@ 'use strict' | ||
module.exports = function(cli, options) { | ||
module.exports = function (cli, options) { | ||
var debug = require('./debug')(cli) | ||
var client = new Aria2(options) | ||
client.onsend = function(m) { | ||
client.onsend = function (m) { | ||
debug('OUT', m) | ||
} | ||
client.onmessage = function(m) { | ||
client.onmessage = function (m) { | ||
debug('IN', m) | ||
@@ -21,3 +21,3 @@ if (m.id === undefined) { | ||
debug('CONNECTING') | ||
client.open(function(err) { | ||
client.open(function (err) { | ||
if (err) { | ||
@@ -32,11 +32,11 @@ console.error(err) | ||
'input': process.stdin, | ||
'output': process.stdout, | ||
'output': process.stdout | ||
}) | ||
rl.setPrompt('aria2rpc ≻ ') | ||
rl.prompt() | ||
rl.on('line', function(line) { | ||
rl.on('line', function (line) { | ||
line = line.trim() | ||
if (!line) return rl.prompt() | ||
var params = line.split(' ') | ||
var cb = function(err, res) { | ||
var cb = function (err, res) { | ||
if (err) console.error(err) | ||
@@ -51,5 +51,5 @@ else console.log(res) | ||
}) | ||
rl.on('close', function() { | ||
rl.on('close', function () { | ||
debug('CLOSING') | ||
client.close(function() { | ||
client.close(function () { | ||
debug('CLOSED') | ||
@@ -56,0 +56,0 @@ process.exit(0) |
'use strict' | ||
module.exports = function(cli) { | ||
return function() { | ||
module.exports = function (cli) { | ||
return function () { | ||
if (cli.debug) { | ||
@@ -6,0 +6,0 @@ console.log.apply(console, arguments) |
@@ -18,33 +18,6 @@ aria2rpc | ||
Try aria2rpc with `aria2rpc call getVersion` it should print something like | ||
![](./call.gif) | ||
``` | ||
{ enabledFeatures: | ||
[ 'BitTorrent', | ||
'Firefox3 Cookie', | ||
'GZip', | ||
'HTTPS', | ||
'Message Digest', | ||
'Metalink', | ||
'XML-RPC' ], | ||
version: '1.19.3' } | ||
``` | ||
## console | ||
Run `aria2rpc console` and type `getVersion` followed by enter. | ||
it should print something like | ||
``` | ||
{ enabledFeatures: | ||
[ 'BitTorrent', | ||
'Firefox3 Cookie', | ||
'GZip', | ||
'HTTPS', | ||
'Message Digest', | ||
'Metalink', | ||
'XML-RPC' ], | ||
version: '1.19.3' } | ||
``` | ||
you can call as much methods as you want. | ||
![](./console.gif) |
{ | ||
"name": "aria2", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Library and cli for aria2, \"The next generation download utility.\"", | ||
@@ -26,3 +26,3 @@ "homepage": "https://github.com/sonnyp/aria2.js", | ||
"preversion": "npm test", | ||
"lint": "eslint .", | ||
"lint": "standard", | ||
"unit": "mocha test/unit.js", | ||
@@ -37,10 +37,9 @@ "integration": "mocha test/integration.js", | ||
"httpclient": "0.0.4", | ||
"ws": "^0.8.0" | ||
"ws": "^0.8.1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.2.0", | ||
"eslint": "^0.24.1", | ||
"sinon": "^1.15.4", | ||
"chai": "^3.4.1", | ||
"sinon": "^1.17.2", | ||
"sinon-chai": "^2.8.0" | ||
} | ||
} |
aria2.js | ||
======== | ||
JavaScript (Node.js and browsers) library and cli for [aria2, "The next generation download utility."](http://aria2.sourceforge.net/) | ||
JavaScript (Node.js and browsers) library and [cli](https://github.com/sonnyp/aria2.js/blob/master/bin/README.md) for [aria2, "The next generation download utility."](http://aria2.sourceforge.net/) | ||
[![Build Status](https://img.shields.io/travis/sonnyp/aria2.js/master.svg?style=flat-square)](https://travis-ci.org/sonnyp/aria2.js/branches) | ||
[![Code quality](https://img.shields.io/codeclimate/github/kabisaict/flow.svg?style=flat-square)](https://codeclimate.com/github/sonnyp/aria2.js) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/) | ||
@@ -53,2 +53,4 @@ [![Dependency Status](https://img.shields.io/david/sonnyp/aria2.js.svg?style=flat-square)](https://david-dm.org/sonnyp/aria2.js) | ||
- [saveSession](http://aria2.sourceforge.net/manual/en/html/aria2c.html#aria2.saveSession) | ||
- [system.multicall](http://aria2.sourceforge.net/manual/en/html/aria2c.html#system.multicall) | ||
- [system.listMethods](http://aria2.sourceforge.net/manual/en/html/aria2c.html#system.listMethods) | ||
- [aria2 events](#aria2-events) | ||
@@ -205,3 +207,3 @@ - [onDownloadStart](http://aria2.sourceforge.net/manual/en/html/aria2c.html#aria2.onDownloadStart) | ||
``` | ||
npm install -g eslint mocha babel | ||
npm install (-g) mocha standard | ||
npm test | ||
@@ -208,0 +210,0 @@ ``` |
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
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
34863
3
11
385
217
Updatedws@^0.8.1