Skip to content

Commit

Permalink
Merge pull request #5 from tyler-johnson/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tyler-johnson authored Sep 15, 2016
2 parents 292cbd5 + 8594f73 commit c9ae3ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
31 changes: 16 additions & 15 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"parser": "babel-eslint",
"extends": "eslint:recommended",
"rules": {
"semi": 1,
"no-console": 0
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true,
"browser": true
}
"parser": "babel-eslint",
"extends": "eslint:recommended",
"rules": {
"semi": 1,
"no-console": 0,
"no-shadow": 1
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"node": true,
"es6": true,
"browser": true
}
}
26 changes: 9 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export default function(fn, opts={}) {

const proxy = httpProxy.createProxyServer({ target });

proxy.on("proxyRes", function(proxyRes, req, res) {
const existing = res.getHeader("Via");
const viaheader = `${existing ? existing + ", " : ""}${req.httpVersion} ${via} (${name}/${version})`;
res.setHeader("Via", viaheader);
});

return async function(req, res, next) {
try {
// hijack the root response and inject proxy information
Expand All @@ -45,24 +51,10 @@ export default function(fn, opts={}) {
// inject couchdb proxy headers into request
const ctx = await confusedAsync(fn, null, [ req, res ]);
if (ctx != null) {
const name = typeof ctx.name === "string" ? ctx.name : "";
req.headers[headerFields.username] = name;
const n = typeof ctx.name === "string" ? ctx.name : "";
req.headers[headerFields.username] = n;
req.headers[headerFields.roles] = Array.isArray(ctx.roles) ? ctx.roles.join(",") : "";
if (secret) req.headers[headerFields.token] = signRequest(name, secret);
}

// attach Via header on response
// do this last in case there was an error
if (via) {
const writeHead = res.writeHead;
res.writeHead = function(code, headers) {
const existing = res.getHeader("Via");
const viaheader = `${existing ? existing + ", " : ""}${req.httpVersion} ${via} (${name}/${version})`;
res.setHeader("Via", viaheader);
if (headers) headers["Via"] = viaheader;

return writeHead.apply(res, arguments);
};
if (secret) req.headers[headerFields.token] = signRequest(n, secret);
}

proxy.web(req, res);
Expand Down

0 comments on commit c9ae3ad

Please sign in to comment.