Comparing version 0.0.5 to 0.0.6
172
lib/index.js
@@ -21,8 +21,2 @@ (function(global) { | ||
var defaultOpts = { | ||
secure: false, | ||
host: 'localhost', | ||
port: 6800 | ||
}; | ||
var Aria2 = function(opts) { | ||
@@ -32,4 +26,4 @@ this.callbacks = {}; | ||
for (var i in defaultOpts) | ||
this[i] = typeof opts === 'object' && i in opts ? opts[i] : defaultOpts[i]; | ||
for (var i in Aria2.options) | ||
this[i] = typeof opts === 'object' && i in opts ? opts[i] : Aria2.options[i]; | ||
}; | ||
@@ -60,6 +54,3 @@ Aria2.prototype.http = function(m, fn) { | ||
if (opts.jsonp) | ||
return this._onmessage(res.body); | ||
var m = JSON.parse(res.body.toString()); | ||
var m = opts.jsonp ? res.body : JSON.parse(res.body.toString()); | ||
this._onmessage(m); | ||
@@ -69,3 +60,3 @@ | ||
}; | ||
Aria2.prototype.send = function(method, params, fn) { | ||
Aria2.prototype.send = function(method, params) { | ||
var m = { | ||
@@ -78,3 +69,3 @@ 'method': 'aria2.' + method, | ||
if (arguments.length > 1) { | ||
params = []; | ||
params = this.secret ? ['token:' + this.secret] : []; | ||
for (var i = 1; i < arguments.length; i++) { | ||
@@ -118,80 +109,107 @@ if (typeof arguments[i] === 'function') { | ||
var n = m.method.split('aria2.')[1]; | ||
if (n in Aria2.notifications) { | ||
this[n](m.params); | ||
} | ||
if (n.indexOf('on') === 0 && typeof this[n] === 'function' && Aria2.notifications.indexOf(n) > -1) | ||
this[n].apply(this, m.params); | ||
} | ||
this.onmessage(m); | ||
}; | ||
Aria2.prototype.open = function(url) { | ||
url = url || (this.secure ? 'wss' : 'ws') + '://' + this.host + ':' + this.port + '/jsonrpc'; | ||
Aria2.prototype.open = function(fn) { | ||
var url = (this.secure ? 'wss' : 'ws') + '://' + this.host + ':' + this.port + '/jsonrpc'; | ||
this.socket = new WebSocket(url); | ||
this.socket.onopen = this.onopen.bind(this); | ||
this.socket.onclose = this.onclose.bind(this); | ||
this.socket.onmessage = (function(e) { | ||
this._onmessage(JSON.parse(e.data)); | ||
}).bind(this); | ||
var self = this; | ||
var callbacked = false; | ||
this.socket.onopen = function() { | ||
if (!callbacked) { | ||
fn(); | ||
callbacked = true; | ||
} | ||
self.onopen(); | ||
}; | ||
this.socket.onerror = function(err) { | ||
if (!callbacked) { | ||
fn(err); | ||
callbacked = true; | ||
} | ||
}; | ||
this.socket.onclose = function() { | ||
self.onclose(); | ||
}; | ||
this.socket.onmessage = function(e) { | ||
self._onmessage(JSON.parse(e.data)); | ||
}; | ||
}; | ||
Aria2.prototype.close = function() { | ||
this.socket.close(); | ||
delete this.socket; | ||
}; | ||
Aria2.methods = { | ||
addUri: {}, | ||
addTorrent: {}, | ||
addMetalink: {}, | ||
remove: {}, | ||
forceRemove: {}, | ||
pause: {}, | ||
pauseAll: {}, | ||
forcePause: {}, | ||
forcePauseAll: {}, | ||
unpause: {}, | ||
unpauseAll: {}, | ||
tellStatus: {}, | ||
getUris: {}, | ||
getFiles: {}, | ||
getPeers: {}, | ||
getServers: {}, | ||
tellActive: {}, | ||
tellWaiting: {}, | ||
tellStopped: {}, | ||
changePosition: {}, | ||
changeUri: {}, | ||
getOption: {}, | ||
changeOption: {}, | ||
getGlobalOption: {}, | ||
changeGlobalOption: {}, | ||
getGlobalStat: {}, | ||
purgeDownloadResult: {}, | ||
removeDownloadResult: {}, | ||
getVersion: {}, | ||
getSessionInfo: {}, | ||
shutdown: {}, | ||
forceShutdown: {}, | ||
Aria2.methods = [ | ||
'addUri', | ||
'addTorrent', | ||
'addMetalink', | ||
'remove', | ||
'forceRemove', | ||
'pause', | ||
'pauseAll', | ||
'forcePause', | ||
'forcePauseAll', | ||
'unpause', | ||
'unpauseAll', | ||
'tellStatus', | ||
'getUris', | ||
'getFiles', | ||
'getPeers', | ||
'getServers', | ||
'tellActive', | ||
'tellWaiting', | ||
'tellStopped', | ||
'changePosition', | ||
'changeUri', | ||
'getOption', | ||
'changeOption', | ||
'getGlobalOption', | ||
'changeGlobalOption', | ||
'getGlobalStat', | ||
'purgeDownloadResult', | ||
'removeDownloadResult', | ||
'getVersion', | ||
'getSessionInfo', | ||
'shutdown', | ||
'forceShutdown' | ||
// multicall: {}, | ||
]; | ||
Aria2.notifications = [ | ||
'onDownloadStart', | ||
'onDownloadPause', | ||
'onDownloadStop', | ||
'onDownloadComplete', | ||
'onDownloadError', | ||
'onBtDownloadComplete' | ||
]; | ||
Aria2.events = [ | ||
'onopen', | ||
'onclose', | ||
'onsend', | ||
'onmessage' | ||
]; | ||
Aria2.options = { | ||
secure: false, | ||
host: 'localhost', | ||
port: 6800, | ||
secret: '' | ||
}; | ||
Aria2.notifications = { | ||
onDownloadStart: {}, | ||
onDownloadPause: {}, | ||
onDownloadStop: {}, | ||
onDownloadComplete: {}, | ||
onDownloadError: {}, | ||
onBtDownloadComplete: {} | ||
}; | ||
for (var i in Aria2.methods) { | ||
(function(m) { | ||
Aria2.prototype[m] = function() { | ||
this.send(m, arguments[0], arguments[1]); | ||
}; | ||
}(i)); | ||
} | ||
for (var y in Aria2.notifications) { | ||
Aria2.prototype[y] = function() {}; | ||
} | ||
Aria2.methods.forEach(function(method) { | ||
Aria2.prototype[method] = function() { | ||
this.send.apply(this, [method].concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
}); | ||
['open', 'close', 'send', 'message'].forEach(function(e) { | ||
Aria2.prototype['on' + e] = function() {}; | ||
Aria2.notifications.forEach(function(notification) { | ||
Aria2.prototype[notification] = function() {}; | ||
}); | ||
Aria2.events.forEach(function(event) { | ||
Aria2.prototype[event] = function() {}; | ||
}); | ||
if (typeof module !== 'undefined' && module.exports) | ||
@@ -198,0 +216,0 @@ module.exports = Aria2; |
{ | ||
"name": "aria2", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Library for aria2, \"The next generation download utility.\"", | ||
@@ -26,13 +26,13 @@ "homepage": "https://github.com/sonnyp/aria2.js", | ||
"dependencies": { | ||
"ws": "~0.4.31", | ||
"httpclient": "~0.0.3" | ||
"ws": "^0.4.31", | ||
"httpclient": "0.0.3" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.17.1", | ||
"grunt": "~0.4.2", | ||
"grunt-jsvalidate": "~0.2.2", | ||
"grunt-contrib-jshint": "~0.8.0", | ||
"grunt-contrib-concat": "~0.3.0", | ||
"grunt-contrib-uglify": "~0.4.0" | ||
"mocha": "^1.18.2", | ||
"grunt": "^0.4.4", | ||
"grunt-contrib-jshint": "^0.10.0", | ||
"grunt-contrib-concat": "^0.4.0", | ||
"grunt-contrib-uglify": "^0.4.0", | ||
"grunt-mocha-test": "^0.10.2" | ||
} | ||
} |
@@ -13,3 +13,3 @@ aria2.js | ||
aria2.js supports the WebSocket and HTTP transports. | ||
aria2.js supports the WebSocket, HTTP and JSONP transports. | ||
@@ -21,3 +21,3 @@ ### Browser | ||
```xml | ||
<script src="bower_components/aria2/aria2.min.js"></script> | ||
<script src="bower_components/aria2/dist/aria2.min.js"></script> | ||
``` | ||
@@ -32,4 +32,4 @@ | ||
``` | ||
## Init | ||
```javascript | ||
@@ -43,6 +43,9 @@ var aria2 = new Aria2([options]); | ||
port: 6800, | ||
secure: false | ||
secure: false, | ||
secret: '' | ||
} | ||
``` | ||
Secret is optional and refers to [--rpc-secret](http://aria2.sourceforge.net/manual/en/html/aria2c.html#cmdoption--rpc-secret). | ||
## Open | ||
@@ -83,13 +86,15 @@ | ||
Note that if you have passed the secret option to aria2.js, it will be automatically added it to every request so there is no need to include it. | ||
When sending a request to aria2, if the WebSocket isn't available or closed, aria2.js will use the HTTP transport. | ||
For every method you can use | ||
For every aria2 methods you can use | ||
```javascript | ||
aria2.send('getVersion', [params,] function(err, res) { | ||
aria2.getVersion([params,] function(err, res) { | ||
console.log(err || res); | ||
}); | ||
``` | ||
or directly | ||
or | ||
```javascript | ||
aria2.getVersion([params,] function(err, res) { | ||
aria2.send('getVersion', [params,] function(err, res) { | ||
console.log(err || res); | ||
@@ -102,6 +107,6 @@ }); | ||
For every notifications you can attach a function to call. | ||
For every notifications you can bind a function. | ||
```javascript | ||
aria2.onDownloadStart = function(event) { | ||
console.log(event); | ||
aria2.onDownloadStart = function(gid) { | ||
console.log(gid); | ||
}; | ||
@@ -108,0 +113,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
9558
192
111
+ Addedhttpclient@0.0.3(transitive)
- Removedes6-promise@1.0.0(transitive)
- Removedhttpclient@0.0.4(transitive)
Updatedhttpclient@0.0.3
Updatedws@^0.4.31