Skip to content

Commit dfcba90

Browse files
committed
Initial version
0 parents  commit dfcba90

File tree

15 files changed

+7327
-0
lines changed

15 files changed

+7327
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

.eslintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"env": {
3+
"node": true
4+
},
5+
"extends": [
6+
"bloq",
7+
"bloq/experimental"
8+
]
9+
}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.eslintcache
2+
.nyc_output
3+
coverage
4+
node_modules
5+
npm-debug.log

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-exact = true

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "6"
4+
- "7"
5+
- "8"
6+

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Bloq, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# http-json-proxy
2+
3+
[![Build Status](https://travis-ci.org/bloq/http-json-proxy.svg?branch=master)](https://travis-ci.org/bloq/http-json-proxy)
4+
[![bitHound Overall Score](https://www.bithound.io/github/bloq/http-json-proxy/badges/score.svg)](https://www.bithound.io/github/bloq/http-json-proxy)
5+
[![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)
6+
[![bitHound Code](https://www.bithound.io/github/bloq/http-json-proxy/badges/code.svg)](https://www.bithound.io/github/bloq/http-json-proxy)
7+
8+
Simple HTTP JSON proxy.
9+
10+
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.
11+
12+
## Installation
13+
14+
```bash
15+
$ npm install --global http-json-proxy
16+
```
17+
18+
## Usage
19+
20+
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:
21+
22+
```
23+
$ http-json-proxy -p 18545 -t http://localhost:8545
24+
Proxy for http://localhost:8545 listening on port 18545
25+
```
26+
27+
Then, each call will be logged as follows:
28+
29+
```
30+
--> POST / {"jsonrpc":"2.0","id":3,"method":"eth_gasPrice","params":[]}
31+
<-- {"jsonrpc":"2.0","result":"0x2e90edd000","id":3}
32+
```
33+
34+
### Options
35+
36+
```
37+
$ http-json-proxy
38+
Start a HTTP JSON proxy server.
39+
40+
Options:
41+
--version Show version number [boolean]
42+
--port, -p the port the server should listen to [number]
43+
--target, -t the proxied API server URL [string] [required]
44+
--help Show help [boolean]
45+
```
46+
47+
## API
48+
49+
The module can also be used programmatically as follows:
50+
51+
```js
52+
const createProxy = require('http-json-proxy')
53+
54+
const options = {
55+
port: 18545,
56+
target: 'http://localhost:8545',
57+
onReq: function (req) {
58+
console.log('-->', req.method, req.url, JSON.stringify(req.body))
59+
return req
60+
},
61+
onRes: function (body) {
62+
console.log('<--', JSON.stringify(body))
63+
return body
64+
}
65+
}
66+
67+
const proxy = createProxy(options)
68+
```
69+
70+
### `createProxy(options)`
71+
72+
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.
73+
74+
#### `options.port`
75+
76+
Is the port the proxy will listen on. If not specified, the proxy will start listening to a random unused port.
77+
78+
#### `options.host`
79+
80+
Is the host the proxy will listen on. If not specified, the proxy will listen in all interfaces.
81+
82+
#### `options.target`
83+
84+
Is the proxied API server URL.
85+
86+
#### `options.onReq`
87+
88+
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.
89+
90+
#### `options.onRes`
91+
92+
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.
93+
94+
#### `options.onErr`
95+
96+
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.
97+
98+
## License
99+
100+
MIT

bin/.eslintrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
"max-len": "off",
4+
"no-console": "off"
5+
}
6+
}

bin/proxy.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
5+
const yargs = require('yargs')
6+
7+
const createProxy = require('..')
8+
9+
const argv = yargs
10+
.version()
11+
.usage('Start a HTTP JSON proxy server.')
12+
.options('port', {
13+
alias: 'p',
14+
describe: 'the port the server should listen to',
15+
type: 'number'
16+
})
17+
.options('target', {
18+
alias: 't',
19+
describe: 'the proxied API server URL',
20+
demandOption: true,
21+
type: 'string'
22+
})
23+
.help()
24+
.parse()
25+
26+
const { port, target } = argv
27+
28+
const proxy = createProxy({
29+
port,
30+
target,
31+
onReq (req) {
32+
console.log('-->', req.method, req.url, JSON.stringify(req.body))
33+
return req
34+
},
35+
onRes (body) {
36+
console.log('<--', JSON.stringify(body))
37+
return body
38+
},
39+
onErr (err) {
40+
console.warn('<-- ERROR', err.message)
41+
return err
42+
}
43+
})
44+
45+
proxy.on('listening', function () {
46+
console.log(`Proxy for ${target} listening on port ${proxy.address().port}`)
47+
})

lib/identity/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
/**
4+
* Returns the first parameter it receives.
5+
*
6+
* @param {*} x Any value.
7+
* @returns {*} the received value.
8+
*/
9+
const identity = x => x
10+
11+
module.exports = identity

0 commit comments

Comments
 (0)