diff --git a/.travis.yml b/.travis.yml
index 0d2ec32..e60bac7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,11 +3,7 @@ sudo: false
language: node_js
node_js:
- - '0.10'
- '0.12'
- '4'
- '5'
- '6'
-
-before_script:
- - npm install -g mocha standard
diff --git a/README.md b/README.md
index c0a5996..e055600 100644
--- a/README.md
+++ b/README.md
@@ -71,7 +71,7 @@ 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)
+ - ~~[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)
@@ -92,8 +92,6 @@ var Aria2 = require('aria2');
or
```xml
-
-
```
```javascript
@@ -122,8 +120,7 @@ default options match aria2c defaults and are
port: 6800,
secure: false,
secret: '',
- path: '/jsonrpc',
- jsonp: false
+ path: '/jsonrpc'
}
```
@@ -131,7 +128,7 @@ default options match aria2c defaults and are
If the WebSocket is open (via the [open method](#open)) aria2.js will use the WebSocket transport, otherwise the HTTP transport.
-`jsonp: true` will make aria2.js uses [JSONP](https://en.wikipedia.org/wiki/JSONP) for non WebSocket requests, useful if you cannot make aria2c allow your origin. It has no effect on Node.js.
+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.
[↑](#aria2js)
diff --git a/bin/README.md b/bin/README.md
index 0c66421..298b36e 100644
--- a/bin/README.md
+++ b/bin/README.md
@@ -15,8 +15,12 @@ Check `aria2rpc -h` and https://aria2.github.io/manual/en/html/aria2c.html#metho
## call
+Uses HTTP transport.
+
![](./call.gif)
## console
+Uses Websocket transport.
+
![](./console.gif)
diff --git a/bin/call.js b/bin/call.js
index 437b91e..7d4da8c 100644
--- a/bin/call.js
+++ b/bin/call.js
@@ -12,31 +12,18 @@ module.exports = function (cli, options, method, params) {
client.onmessage = function (m) {
debug('IN', m)
}
- debug('CONNECTING')
- client.open(function (err) {
+
+ var cb = function (err, res) {
if (err) {
console.error(err)
process.exit(1)
}
- debug('CONNECTED')
-
- var cb = function (err, res) {
- debug('CLOSING')
- client.close(function () {
- debug('CLOSED')
- if (err) {
- console.error(err)
- process.exit(1)
- }
-
- console.log(res)
- process.exit(0)
- })
- }
+ console.log(res)
+ process.exit(0)
+ }
- var args = [method].concat(params, cb)
+ var args = [method].concat(params, cb)
- client.send.apply(client, args)
- })
+ client.send.apply(client, args)
}
diff --git a/example/callback.js b/example/callback.js
index 82f4e44..65ff62e 100644
--- a/example/callback.js
+++ b/example/callback.js
@@ -12,7 +12,7 @@
}
// those are default options
- var options = {'host': 'localhost', 'port': 6800, 'secure': false, jsonp: false}
+ var options = {'host': 'localhost', 'port': 6800, 'secure': false}
var aria2 = new Aria2(options)
aria2.getVersion(function (err, res) {
diff --git a/example/index.html b/example/index.html
index 641a87b..5b2b954 100644
--- a/example/index.html
+++ b/example/index.html
@@ -4,8 +4,6 @@
aria2.js example
-
-
diff --git a/example/promise.js b/example/promise.js
index ce8d99c..49537f2 100644
--- a/example/promise.js
+++ b/example/promise.js
@@ -12,7 +12,7 @@
}
// those are default options
- var options = {'host': 'localhost', 'port': 6800, 'secure': false, jsonp: false}
+ var options = {'host': 'localhost', 'port': 6800, 'secure': false}
var aria2 = new Aria2(options)
aria2.getVersion().then(
diff --git a/example/websocket.js b/example/websocket.js
index f4529a7..1af0ae0 100644
--- a/example/websocket.js
+++ b/example/websocket.js
@@ -12,7 +12,7 @@
}
// those are default options
- var options = {'host': 'localhost', 'port': 6800, 'secure': false, jsonp: false}
+ var options = {'host': 'localhost', 'port': 6800, 'secure': false}
var aria2 = new Aria2(options)
// triggered when a message is being sent
diff --git a/index.js b/index.js
index 8f37e83..8538c73 100644
--- a/index.js
+++ b/index.js
@@ -2,25 +2,18 @@
'use strict'
var WebSocket
- var b64
- var httpclient
+ var fetch
var pg
- function isNode () {
- return typeof module !== 'undefined' && module.exports
- }
+ var isNode = typeof module !== 'undefined' && module.exports
- if (isNode()) {
+ if (isNode) {
WebSocket = require('ws')
- b64 = function (str) {
- return new Buffer(str).toString('base64')
- }
- httpclient = require('httpclient')
+ fetch = require('node-fetch')
pg = require('polygoat')
} else {
WebSocket = global.WebSocket
- b64 = global.atob
- httpclient = global.HTTPClient
+ fetch = global.fetch
pg = global.polygoat
}
@@ -34,45 +27,34 @@
}
Aria2.prototype.http = function (m, fn) {
- var opts = {
- 'host': this.host,
- 'port': this.port,
- 'path': this.path,
- 'secure': this.secure
- }
-
+ var that = this
var content = {
method: m.method,
id: m.id
}
- // use POST (default)
- if (isNode() || !this.jsonp) {
- opts.body = content
- opts.method = 'POST'
- if (Array.isArray(m.params) && m.params.length > 0) {
- opts.body.params = m.params
- }
- // use JSONP
- } else {
- opts.query = content
- opts.jsonp = 'jsoncallback'
- if (Array.isArray(m.params) && m.params.length > 0) {
- opts.query.params = b64(JSON.stringify(m.params))
- }
+ if (Array.isArray(m.params) && m.params.length > 0) {
+ content.params = m.params
}
- var that = this
-
- httpclient.request(opts, function (err, res, body) {
- if (err) return fn(err)
-
- var msg = opts.jsonp ? body : JSON.parse(body.toString())
- that._onmessage(msg)
- })
+ 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]*/) {
+ 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)
@@ -289,13 +271,12 @@
'host': 'localhost',
'port': 6800,
'secret': '',
- 'path': '/jsonrpc',
- 'jsonp': false
+ 'path': '/jsonrpc'
}
Aria2.methods.forEach(function (method) {
var sufix = method.indexOf('.') > -1 ? method.split('.')[1] : method
- Aria2.prototype[sufix] = function (/* [param] [,param] [,...]*/) {
+ Aria2.prototype[sufix] = function (/* [param] [,param] [,...] */) {
return this.send.apply(this, [method].concat(Array.prototype.slice.call(arguments)))
}
})
@@ -308,7 +289,7 @@
Aria2.prototype[event] = function () {}
})
- if (isNode()) {
+ if (isNode) {
module.exports = Aria2
} else {
global.Aria2 = Aria2
diff --git a/package.json b/package.json
index 7f26be8..4098fd9 100644
--- a/package.json
+++ b/package.json
@@ -32,13 +32,15 @@
"repository": "github:sonnyp/aria2.js",
"dependencies": {
"commander": "^2.9.0",
- "httpclient": "0.1.0",
+ "node-fetch": "^1.6.3",
"polygoat": "^1.1.2",
"ws": "^1.0.1"
},
"devDependencies": {
"chai": "^3.4.1",
+ "mocha": "^3.1.0",
"sinon": "^1.17.2",
- "sinon-chai": "^2.8.0"
+ "sinon-chai": "^2.8.0",
+ "standard": "^8.2.0"
}
}
diff --git a/test/unit.js b/test/unit.js
index 217c934..f3f72bd 100644
--- a/test/unit.js
+++ b/test/unit.js
@@ -1,4 +1,4 @@
-(function (global) {
+;(function (global) {
'use strict'
/* global describe, it, beforeEach */
@@ -211,4 +211,4 @@
})
})
})
-}(this))
+}(typeof global !== 'undefined' ? global : this))