Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from uniquelyparticular/feature/initialrelease
Browse files Browse the repository at this point in the history
feat: initial release
  • Loading branch information
agrohs authored May 2, 2019
2 parents 38e32e9 + 0baa9a7 commit d8c0486
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ const parseURL = url => {
}

const isAuthorized = (referer, whitelist = []) => {
console.log('whitelist', whitelist)
// console.log('whitelist', whitelist)
const { host, protocol } = parseURL(referer)
console.log('host', host)
console.log('protocol', protocol)
// console.log('host', host)
// console.log('protocol', protocol)
return (
isWhitelisted(host, whitelist) &&
(protocol === 'https:' || (protocol === 'http:' && host === 'localhost'))
Expand All @@ -74,8 +74,7 @@ const destinationWhiteList = toRegexArray(
process.env.PROXY_DESTINATION_WHITELIST
)
const proxyPrefix = process.env.PROXY_PREFIX || 'proxy'

console.log('process.env.PROXY_PREFIX', process.env.PROXY_PREFIX)
// console.log('process.env.PROXY_PREFIX', process.env.PROXY_PREFIX)

const requestHeaders = headers => {
console.log('requestHeaders, headers', headers)
Expand All @@ -92,19 +91,19 @@ const requestHeaders = headers => {
'x-forwarded-referer': referer
}
const modifiedHeaders = { ...filteredHeaders, ...defaultHeaders }
console.log('requestHeaders, modifiedHeaders', modifiedHeaders)
// console.log('requestHeaders, modifiedHeaders', modifiedHeaders)
return modifiedHeaders
}

const allowHeaders = headers => {
console.log('allowHeaders, headers', headers)
// console.log('allowHeaders, headers', headers)
const {
'access-control-request-headers': requestedHeaders,
...filteredHeaders
} = headers

console.log('allowHeaders, requestedHeaders', requestedHeaders)
console.log('allowHeaders, filteredHeaders', filteredHeaders)
// console.log('allowHeaders, requestedHeaders', requestedHeaders)
// console.log('allowHeaders, filteredHeaders', filteredHeaders)

const defaultAllowedHeaders = [
'accept',
Expand All @@ -119,38 +118,38 @@ const allowHeaders = headers => {
...Object.keys(filteredHeaders),
...defaultAllowedHeaders
].join(',')
console.log('allowHeaders, allowedHeaders', allowedHeaders)
// console.log('allowHeaders, allowedHeaders', allowedHeaders)
return allowedHeaders
}

const json = response => {
return response
.text()
.then(text => {
console.log('processRequest, text', text)
// console.log('processRequest, text', text)
return text
})
.then((response = {}) => {
const json = JSON.parse(response)
console.log('processRequest, json', json)
// console.log('processRequest, json', json)
return json
})
}

const processRequest = (res, origin, url, options) => {
console.log('url', url)
console.log('options', options)
// console.log('url', url)
// console.log('options', options)
return fetch(url, options)
.then(response => {
console.log('processRequest, response.status', response.status)
// console.log('processRequest, response.status', response.status)
if (response.status > 299) {
return send(res, response.status || 500, response)
} else {
return json(response)
.then(data => {
console.log('processRequest, data', data)
// console.log('processRequest, data', data)
if (origin) {
console.log('processRequest, origin', origin)
// console.log('processRequest, origin', origin)
res.setHeader('access-control-allow-origin', origin)
}
return send(res, 200, data)
Expand All @@ -168,25 +167,25 @@ const processRequest = (res, origin, url, options) => {
}

const handleOptions = async (req, res) => {
console.log('handleOptions, req.headers', req.headers)
console.log('handleOptions, allowHeaders', allowHeaders(req.headers))
// console.log('handleOptions, req.headers', req.headers)
// console.log('handleOptions, allowHeaders', allowHeaders(req.headers))
res.setHeader('access-control-allow-headers', allowHeaders(req.headers))
return send(res, 204)
}

const handleProxy = async (req, res) => {
console.log('called proxy')
console.log('req.method', req.method)
// console.log('called proxy')
// console.log('req.method', req.method)
if (req.method === 'OPTIONS') {
return handleOptions(req, res)
}

try {
const path = req.url
console.log('path', path)
// console.log('path', path)
// console.log('req.rawHeaders',req.rawHeaders)
console.log('req.headers.referer', req.headers.referer)
console.log('req.headers.origin', req.headers.origin)
// console.log('req.headers.referer', req.headers.referer)
// console.log('req.headers.origin', req.headers.origin)
// console.log('req.headers',req.headers)
if (!req.headers.referer) {
return noReferer(req, res)
Expand All @@ -195,15 +194,11 @@ const handleProxy = async (req, res) => {
return notAuthorized(req, res)
}

console.log('proxyPrefix', proxyPrefix)
console.log(
"path.replace(`/${proxyPrefix}/`,'')",
path.replace(`/${proxyPrefix}/`, '')
)
// console.log('proxyPrefix', proxyPrefix)
const destinationURL = decodeURIComponent(
path.replace(`/${proxyPrefix}/`, '')
)
console.log('destinationURL', destinationURL)
// console.log('destinationURL', destinationURL)

if (!isAuthorized(destinationURL, destinationWhiteList)) {
return notAuthorized(req, res)
Expand All @@ -216,14 +211,14 @@ const handleProxy = async (req, res) => {

if (req.method !== 'GET') {
const txt = await text(req)
console.log('txt', txt)
// console.log('txt', txt)
if (txt) {
const body = JSON.parse(txt)
console.log('body', body)
// console.log('body', body)
if (body) {
fetchOptions.body = JSON.stringify(body)
}
console.log('body fetchOptions', fetchOptions)
// console.log('body fetchOptions', fetchOptions)
}
}
return processRequest(res, req.headers.origin, destinationURL, fetchOptions)
Expand Down

0 comments on commit d8c0486

Please sign in to comment.