Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
gabmontes committed Mar 27, 2018
0 parents commit dfcba90
Show file tree
Hide file tree
Showing 15 changed files with 7,327 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"node": true
},
"extends": [
"bloq",
"bloq/experimental"
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.eslintcache
.nyc_output
coverage
node_modules
npm-debug.log
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-exact = true
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "6"
- "7"
- "8"

20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) Bloq, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# http-json-proxy

[![Build Status](https://travis-ci.org/bloq/http-json-proxy.svg?branch=master)](https://travis-ci.org/bloq/http-json-proxy)
[![bitHound Overall Score](https://www.bithound.io/github/bloq/http-json-proxy/badges/score.svg)](https://www.bithound.io/github/bloq/http-json-proxy)
[![bitHound Dependencies](https://www.bithound.io/github/bloq/http-json-proxy/badges/dependencies.svg)](https://www.bithound.io/github/bloq/http-json-proxy/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/bloq/http-json-proxy/badges/code.svg)](https://www.bithound.io/github/bloq/http-json-proxy)

Simple HTTP JSON proxy.

This proxy can be used as a middleman in between a HTTP JSON API server and a client to monitor the requests, responses and even modify on the fly any of those.

## Installation

```bash
$ npm install --global http-json-proxy
```

## Usage

The following command will spin up a proxy server that will forward all requests to a locally installed Ethereum node and will log to console each JSON RPC call with the corresponding response:

```
$ http-json-proxy -p 18545 -t http://localhost:8545
Proxy for http://localhost:8545 listening on port 18545
```

Then, each call will be logged as follows:

```
--> POST / {"jsonrpc":"2.0","id":3,"method":"eth_gasPrice","params":[]}
<-- {"jsonrpc":"2.0","result":"0x2e90edd000","id":3}
```

### Options

```
$ http-json-proxy
Start a HTTP JSON proxy server.
Options:
--version Show version number [boolean]
--port, -p the port the server should listen to [number]
--target, -t the proxied API server URL [string] [required]
--help Show help [boolean]
```

## API

The module can also be used programmatically as follows:

```js
const createProxy = require('http-json-proxy')

const options = {
port: 18545,
target: 'http://localhost:8545',
onReq: function (req) {
console.log('-->', req.method, req.url, JSON.stringify(req.body))
return req
},
onRes: function (body) {
console.log('<--', JSON.stringify(body))
return body
}
}

const proxy = createProxy(options)
```

### `createProxy(options)`

Creates a new proxy that starts listening on the specified port, forwarding all requests to the target server. It returns an [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) instance.

#### `options.port`

Is the port the proxy will listen on. If not specified, the proxy will start listening to a random unused port.

#### `options.host`

Is the host the proxy will listen on. If not specified, the proxy will listen in all interfaces.

#### `options.target`

Is the proxied API server URL.

#### `options.onReq`

Will be called on each request with the `req` object that will be forwarded to the target server and shall return that `req`. Any of the properties of the `req` object can be altered to modify the actual request that is sent to the target server. Defaults to the identity function.

#### `options.onRes`

Will be called on each response with the `body` of the response and shall return the actual `body` to be provided to the client. It can be altered to provide a different response too. Defaults to the identity function.

#### `options.onErr`

Will be called on each request error with the corresponding `err` object and shall return the same, altered or different `err` object that will be returned to the client along with a 500 status code. Defaults to the identity function.

## License

MIT
6 changes: 6 additions & 0 deletions bin/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"max-len": "off",
"no-console": "off"
}
}
47 changes: 47 additions & 0 deletions bin/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node

'use strict'

const yargs = require('yargs')

const createProxy = require('..')

const argv = yargs
.version()
.usage('Start a HTTP JSON proxy server.')
.options('port', {
alias: 'p',
describe: 'the port the server should listen to',
type: 'number'
})
.options('target', {
alias: 't',
describe: 'the proxied API server URL',
demandOption: true,
type: 'string'
})
.help()
.parse()

const { port, target } = argv

const proxy = createProxy({
port,
target,
onReq (req) {
console.log('-->', req.method, req.url, JSON.stringify(req.body))
return req
},
onRes (body) {
console.log('<--', JSON.stringify(body))
return body
},
onErr (err) {
console.warn('<-- ERROR', err.message)
return err
}
})

proxy.on('listening', function () {
console.log(`Proxy for ${target} listening on port ${proxy.address().port}`)
})
11 changes: 11 additions & 0 deletions lib/identity/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

/**
* Returns the first parameter it receives.
*
* @param {*} x Any value.
* @returns {*} the received value.
*/
const identity = x => x

module.exports = identity
Loading

0 comments on commit dfcba90

Please sign in to comment.