Skip to content

Commit

Permalink
fix: [fix/custom-headers]: merging of custom headers with proxying re…
Browse files Browse the repository at this point in the history
…quest headers (#2)
  • Loading branch information
Oleg Levshin authored May 7, 2019
1 parent e9f3017 commit a6dc17b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bin/corsyusha.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ const main = async () => {

const normalizedServerLogging = [null, 'true', '1', 'yes', true, 1].includes(serverLogging)

const normalizedHeaders = JSON.parse(headers)
const parsedHeaders = JSON.parse(headers)

await corsyusha({
url,
port,
host,
urlSection,
serverLogging: normalizedServerLogging,
headers: normalizedHeaders,
headers: parsedHeaders,
})
} catch (error) {
console.error(error)
Expand Down
21 changes: 13 additions & 8 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ module.exports = ({

try {

const requestOptions = {
url: requestUrl,
...(
typeof headers === 'object' && Object.keys(headers).length ?
{ headers } :
{}
),
if (typeof headers === 'object') {
const normalizedHeaders = Object.keys(headers).reduce((result, h) => ({
...result,
[h.toLowerCase()]: headers[h],
}), {})

req.headers = {
...normalizedHeaders,
...req.headers,
}
}

req.pipe(request(requestOptions)).pipe(res)
logger.info(`${'Proxied request merged headers'.blue} ${JSON.stringify(req.headers).green}`)

req.pipe(request(requestUrl)).pipe(res)

} catch (error) {
logger.error(error)
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "corsyusha",
"version": "1.1.0",
"version": "1.1.1",
"description": "Simple and fast (built on top of fastify) proxy to bypass CORS issues during prototyping against existing APIs without having to worry about CORS",
"keywords": [
"cors",
Expand Down

0 comments on commit a6dc17b

Please sign in to comment.