node-http-proxy-json
Advanced tools
Comparing version 0.1.1 to 0.1.2
66
index.js
var zlib = require('zlib'); | ||
var concatStream = require('concat-stream'); | ||
var BufferHelper = require('bufferhelper'); | ||
@@ -12,9 +13,12 @@ /** | ||
var unzip, zip; | ||
// Now only deal with the gzip and deflate content-encoding. | ||
if (contentEncoding === 'gzip') { | ||
unzip = zlib.Gunzip(); | ||
zip = zlib.Gzip(); | ||
} else if (contentEncoding === 'deflate') { | ||
unzip = zlib.Inflate(); | ||
zip = zlib.Deflate(); | ||
// Now only deal with the gzip/deflate/undefined content-encoding. | ||
switch (contentEncoding) { | ||
case 'gzip': | ||
unzip = zlib.Gunzip(); | ||
zip = zlib.Gzip(); | ||
break; | ||
case 'deflate': | ||
unzip = zlib.Inflate(); | ||
zip = zlib.Deflate(); | ||
break; | ||
} | ||
@@ -31,7 +35,14 @@ | ||
}); | ||
handleCompressed(res, _write, _end, unzip, zip, callback); | ||
} else if (!contentEncoding) { | ||
handleUncompressed(res, _write, _end, callback); | ||
} else { | ||
console.log('Not supported content-encoding: ' + contentEncoding); | ||
return; | ||
} | ||
}; | ||
/** | ||
* handle compressed | ||
*/ | ||
function handleCompressed(res, _write, _end, unzip, zip, callback) { | ||
// The rewrite response method is replaced by unzip stream. | ||
@@ -42,4 +53,4 @@ res.write = function (data) { | ||
res.end = function (data) { | ||
unzip.end(data); | ||
res.end = function () { | ||
unzip.end(); | ||
}; | ||
@@ -76,3 +87,36 @@ | ||
}); | ||
unzip.pipe(concatWrite); | ||
}; | ||
} | ||
/** | ||
* handle Uncompressed | ||
*/ | ||
function handleUncompressed(res, _write, _end, callback) { | ||
var buffer = new BufferHelper(); | ||
// Rewrite response method and get the content. | ||
res.write = function (data) { | ||
buffer.concat(data); | ||
}; | ||
res.end = function () { | ||
var body; | ||
try { | ||
body = JSON.parse(buffer.toBuffer().toString()); | ||
} catch (e) { | ||
console.log('JSON.parse error:', e); | ||
} | ||
// Custom modified logic | ||
if (typeof callback === 'function') { | ||
body = callback(body); | ||
} | ||
// Converts the JSON to buffer. | ||
body = new Buffer(JSON.stringify(body)); | ||
// Call the response method | ||
_write.call(res, body); | ||
_end.call(res); | ||
}; | ||
} |
{ | ||
"name": "node-http-proxy-json", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "for node-http-proxy transform the response json from the proxied server.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha -R spec" | ||
"test": "./node_modules/.bin/mocha -R spec ./test/*.js" | ||
}, | ||
@@ -28,2 +28,3 @@ "repository": { | ||
"devDependencies": { | ||
"bufferhelper": "^0.2.1", | ||
"chai": "^3.5.0", | ||
@@ -30,0 +31,0 @@ "http-proxy": "^1.13.3", |
122
README.md
# node-http-proxy-json [![Build Status](https://travis-ci.org/langjt/node-http-proxy-json.svg?branch=master)](https://travis-ci.org/langjt/node-http-proxy-json) | ||
for [node-http-proxy](https://github.com/nodejitsu/node-http-proxy) transform the response json from the proxied server. | ||
## Install it via NPM | ||
## Installation | ||
@@ -10,4 +10,12 @@ ``` | ||
## Usage | ||
## Motivation | ||
When using [node-http-proxy](https://github.com/nodejitsu/node-http-proxy) need to modify the response. If your proxy server returns HTML/XML document, you can try [Harmon](https://github.com/No9/harmon). | ||
But sometimes the proxy server only returns the JSON. For example, call API from the server. Usually the server will compress the data. | ||
So before using this repository, confirm your server compression format, currently only supports **gzip**、**deflate** and **uncompressed**. | ||
If you need other compression formats, please create a new Issue, and I will try to achieve it as much as possible. | ||
## Use Cases | ||
#### Simulation server using gzip compression | ||
``` | ||
@@ -67,2 +75,110 @@ var zlib = require('zlib'); | ||
}).listen(5001); | ||
``` | ||
``` | ||
#### Simulation server using deflate compression | ||
``` | ||
var zlib = require('zlib'); | ||
var http = require('http'); | ||
var httpProxy = require('http-proxy'); | ||
var modifyResponse = require('../'); | ||
// Create a proxy server | ||
var proxy = httpProxy.createProxyServer({ | ||
target: 'http://localhost:5001' | ||
}); | ||
// Listen for the `proxyRes` event on `proxy`. | ||
proxy.on('proxyRes', function (proxyRes, req, res) { | ||
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) { | ||
if (body) { | ||
// modify some information | ||
body.age = 2; | ||
delete body.version; | ||
} | ||
return body; | ||
}); | ||
}); | ||
// Create your server and then proxies the request | ||
var server = http.createServer(function (req, res) { | ||
proxy.web(req, res); | ||
}).listen(5000); | ||
// Create your target server | ||
var targetServer = http.createServer(function (req, res) { | ||
// Create deflated content | ||
var deflate = zlib.Deflate(); | ||
var _write = res.write; | ||
var _end = res.end; | ||
deflate.on('data', function (buf) { | ||
_write.call(res, buf); | ||
}); | ||
deflate.on('end', function () { | ||
_end.call(res); | ||
}); | ||
res.write = function (data) { | ||
deflate.write(data); | ||
}; | ||
res.end = function () { | ||
deflate.end(); | ||
}; | ||
res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'deflate'}); | ||
res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'})); | ||
res.end(); | ||
}).listen(5001); | ||
``` | ||
#### Server does not enable compression | ||
``` | ||
var http = require('http'); | ||
var httpProxy = require('http-proxy'); | ||
var modifyResponse = require('../'); | ||
// Create a proxy server | ||
var proxy = httpProxy.createProxyServer({ | ||
target: 'http://localhost:5001' | ||
}); | ||
// Listen for the `proxyRes` event on `proxy`. | ||
proxy.on('proxyRes', function (proxyRes, req, res) { | ||
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) { | ||
if (body) { | ||
// modify some information | ||
body.age = 2; | ||
delete body.version; | ||
} | ||
return body; | ||
}); | ||
}); | ||
// Create your server and then proxies the request | ||
var server = http.createServer(function (req, res) { | ||
proxy.web(req, res); | ||
}).listen(5000); | ||
// Create your target server | ||
var targetServer = http.createServer(function (req, res) { | ||
res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'deflate'}); | ||
res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'})); | ||
res.end(); | ||
}).listen(5001); | ||
``` | ||
## Tests | ||
To run the test suite, first install the dependencies, then run `npm test`: | ||
```bash | ||
$ npm install | ||
$ npm test | ||
``` | ||
## License | ||
[MIT](http://opensource.org/licenses/MIT) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16687
9
293
182
4
7