Skip to content

Commit 131fe6a

Browse files
committed
fix travis test
1 parent f4d9f5d commit 131fe6a

File tree

6 files changed

+202
-124
lines changed

6 files changed

+202
-124
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
language: node_js
22
node_js:
3-
- 0.10
4-
- 0.11
5-
6-
script: make test
3+
- "0.10"
4+
- "0.11"

Makefile

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ app.use('/proxy', proxy('www.google.com', {
4040

4141

4242

43-
You can also intercept the response get by proxy before send it back to the client:
43+
You can also intercept the response get by proxy before send it back to the client, or change the request options before it get sent to target:
4444

4545
```js
4646
app.use('/proxy', proxy('www.google.com', {
@@ -49,7 +49,13 @@ app.use('/proxy', proxy('www.google.com', {
4949
},
5050
intercept: function(data, req, res, callback) {
5151
data = JSON.parse(data.toString('utf8'));
52-
callback(null, JSON.stringify(data.obj));
52+
callback(null, JSON.stringify(data));
53+
},
54+
decorateRequest: function(req) {
55+
req.headers['Content-Type'] = '';
56+
req.method = 'GET";
57+
req.bodyContent = wrap(req.bodyContent);
58+
return req;
5359
}
5460
}));
5561
@@ -60,4 +66,4 @@ app.use('/proxy', proxy('www.google.com', {
6066
## Licence
6167
6268
MIT
63-
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. -->
69+
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. -->

index.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = function proxy(host, options) {
3232
* intercept(data, res, req, function(err, json));
3333
*/
3434
var intercept = options.intercept;
35-
var decorateProxyRequest = options.decorateProxyRequest || function () { return arguments; };
35+
var decorateRequest = options.decorateRequest;
3636
var forwardPath = options.forwardPath;
3737
var filter = options.filter;
3838

@@ -50,28 +50,33 @@ module.exports = function proxy(host, options) {
5050
// var hasRequestBody = 'content-type' in req.headers || 'transfer-encoding' in req.headers;
5151
getRawBody(req, {
5252
length: req.headers['content-length'],
53-
limit: '1mb', // let options do here?
53+
limit: '1mb' // TODO let options do here?
5454
}, function(err, bodyContent) {
55-
var decoratedRequest;
55+
if (err) return next(err);
5656

57-
if (err) return next(err)
58-
59-
decoratedRequest = decorateProxyRequest(hds, bodyContent);
60-
hds = decoratedRequest.headers;
61-
bodyContent = decoratedRequest.body;
62-
63-
if (bodyContent.length)
64-
hds['content-length'] = bodyContent.length
65-
66-
var chunks = [];
67-
var realRequest = http.request({
57+
var reqOpt = {
6858
hostname: (typeof host == 'function') ? host(req) : host.toString(),
6959
port: port,
7060
headers: hds,
7161
method: req.method,
72-
path: path
73-
}, function(rsp) {
62+
path: path,
63+
bodyContent: bodyContent
64+
};
65+
66+
67+
if (decorateRequest)
68+
reqOpt = decorateRequest(reqOpt) || reqOpt;
69+
70+
bodyContent = reqOpt.bodyContent;
71+
delete reqOpt.bodyContent;
7472

73+
if (typeof bodyContent == 'string')
74+
reqOpt.headers['content-length'] = Buffer.byteLength(bodyContent);
75+
else if (Buffer.isBuffer(bodyContent)) // Buffer
76+
reqOpt.headers['content-length'] = bodyContent.length;
77+
78+
var chunks = [];
79+
var realRequest = http.request(reqOpt, function(rsp) {
7580
var rspData = null;
7681
rsp.on('data', function(chunk) {
7782
chunks.push(chunk);
@@ -89,6 +94,20 @@ module.exports = function proxy(host, options) {
8994
if (err) {
9095
return next(err);
9196
}
97+
98+
if (typeof rsp == 'string')
99+
rsp = new Buffer(rsp, 'utf8');
100+
101+
if (!Buffer.isBuffer(rsp)) {
102+
next(new Error("intercept should return string or buffer as data"));
103+
}
104+
105+
if (!res.headersSent)
106+
res.set('content-length', rsp.length);
107+
else if (rsp.length != rspData.length) {
108+
next(new Error("'Content-Length' is already sent, the length of response data can not be changed"));
109+
}
110+
92111
if (!sent)
93112
res.send(rsp);
94113
});
@@ -107,6 +126,7 @@ module.exports = function proxy(host, options) {
107126
res.set(p, rsp.headers[p]);
108127
}
109128
}
129+
110130
});
111131

112132
realRequest.on('error', function(e) {
@@ -132,4 +152,4 @@ function extend(obj, source, skips) {
132152
}
133153

134154
return obj;
135-
};
155+
}

package.json

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "express-http-proxy",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "http proxy middleware for express",
55
"main": "index.js",
66
"scripts": {
7-
"test": "make test"
7+
"test": "npm run lint && ./node_modules/.bin/mocha -R spec test/test.js",
8+
"lint": "./node_modules/.bin/jshint index.js test/*.js"
89
},
910
"repository": {
1011
"type": "git",
@@ -25,12 +26,30 @@
2526
"url": "https://github.com/villadora/express-http-proxy/issues"
2627
},
2728
"devDependencies": {
28-
"supertest": "^0.13.0",
2929
"express": "^4.3.1",
30-
"mocha": "^1.19.0"
30+
"jshint": "^2.5.5",
31+
"mocha": "^1.19.0",
32+
"supertest": "^0.13.0"
3133
},
3234
"dependencies": {
3335
"type-is": "^1.2.0",
3436
"raw-body": "^1.1.6"
35-
}
37+
},
38+
"contributors": [
39+
{
40+
"name": "Wei Gao",
41+
"email": "jky239@gmail.com",
42+
"url": "https://github.com/villadora"
43+
},
44+
{
45+
"name": "Jérémy Lal",
46+
"email": "kapouer@melix.org",
47+
"url": "https://github.com/kapouer"
48+
},
49+
{
50+
"name": "Saulius Menkevičius",
51+
"email": null,
52+
"url": "https://github.com/razzmatazz"
53+
}
54+
]
3655
}

0 commit comments

Comments
 (0)