Skip to content

Commit

Permalink
Merge pull request #25 from sonnyp/fetch
Browse files Browse the repository at this point in the history
fetch
  • Loading branch information
sonnyp authored Sep 28, 2016
2 parents 80ea2b6 + f89e77e commit fd16c42
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 85 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -92,8 +92,6 @@ var Aria2 = require('aria2');
or

```xml
<!-- optional for HTTP/JSONP support -->
<script src="node_modules/httpclient/bundle.js"></script>
<script src="node_modules/aria2/bundle.js"></script>
```
```javascript
Expand Down Expand Up @@ -122,16 +120,15 @@ default options match aria2c defaults and are
port: 6800,
secure: false,
secret: '',
path: '/jsonrpc',
jsonp: false
path: '/jsonrpc'
}
```

`secret` is optional and refers to [--rpc-secret](https://aria2.github.io/manual/en/html/aria2c.html#cmdoption--rpc-secret).

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)

Expand Down
4 changes: 4 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 7 additions & 20 deletions bin/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion example/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<meta charset="utf-8"/>
<title>aria2.js example</title>
<link rel="stylesheet" href="style.css"/>
<!-- optional for HTTP/jsonp support -->
<script src="../node_modules/httpclient/bundle.js"></script>
<script src="../bundle.js"></script>
<script src="promise.js"></script>
<script src="callback.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion example/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion example/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 27 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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)))
}
})
Expand All @@ -308,7 +289,7 @@
Aria2.prototype[event] = function () {}
})

if (isNode()) {
if (isNode) {
module.exports = Aria2
} else {
global.Aria2 = Aria2
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions test/unit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function (global) {
;(function (global) {
'use strict'

/* global describe, it, beforeEach */
Expand Down Expand Up @@ -211,4 +211,4 @@
})
})
})
}(this))
}(typeof global !== 'undefined' ? global : this))

0 comments on commit fd16c42

Please sign in to comment.