Comparing version 1.2.6 to 1.2.8
@@ -7,4 +7,4 @@ /*! | ||
'use strict'; | ||
'use strict' | ||
module.exports = require('./lib/bfn-proxy'); | ||
module.exports = require('./lib/bfn-proxy') |
@@ -1,28 +0,28 @@ | ||
'use strict'; | ||
module.exports = function(other_uri_match, request, response){ | ||
var status_code = 200, | ||
status_msg; | ||
switch (other_uri_match){ | ||
case 'status': | ||
status_msg = 'OK'; | ||
break; | ||
case 'debug': | ||
status_msg = JSON.stringify({ | ||
'headers': request.headers, | ||
'url': request.url, | ||
'env': process.env | ||
}, null, 4); | ||
break; | ||
case 'myip': | ||
status_msg = request.headers['x-forwarded-for'] || request.connection.remoteAddress; | ||
break; | ||
case 'date': | ||
status_msg = (new Date()).toString(); | ||
break; | ||
default: | ||
status_code = 404; | ||
status_msg = 'Not Found'; | ||
} | ||
response.writeHead(status_code, {'content-length': status_msg.length, 'content-type': 'text/plain'}); | ||
return response.end(status_msg); | ||
}; | ||
'use strict' | ||
module.exports = function (otherUriMatch, request, response) { | ||
var statusCode = 200 | ||
var statusMsg | ||
switch (otherUriMatch) { | ||
case 'status': | ||
statusMsg = 'OK' | ||
break | ||
case 'debug': | ||
statusMsg = JSON.stringify({ | ||
'headers': request.headers, | ||
'url': request.url, | ||
'env': process.env | ||
}, null, 4) | ||
break | ||
case 'myip': | ||
statusMsg = request.headers['x-forwarded-for'] || request.connection.remoteAddress | ||
break | ||
case 'date': | ||
statusMsg = (new Date()).toString() | ||
break | ||
default: | ||
statusCode = 404 | ||
statusMsg = 'Not Found' | ||
} | ||
response.writeHead(statusCode, {'content-length': statusMsg.length, 'content-type': 'text/plain'}) | ||
return response.end(statusMsg) | ||
} |
@@ -1,72 +0,72 @@ | ||
'use strict'; | ||
var url = require('url'), | ||
http = require('http'), | ||
https = require('https'), | ||
omit = require('lodash.omit'), | ||
res404 = require('./not-found'), | ||
doAPI = require('./api-functions'); | ||
'use strict' | ||
var url = require('url') | ||
var http = require('http') | ||
var https = require('https') | ||
var omit = require('lodash.omit') | ||
var res404 = require('./not-found') | ||
var doAPI = require('./api-functions') | ||
module.exports = function(opts){ | ||
if (typeof opts !== 'object') opts = {}; | ||
var omit_headers = ['request', 'host', 'connection', 'x-forwarded-for', 'x-forwarded-proto', 'x-forwarded-port', 'x-request-start', 'x-request-id']; | ||
if (typeof opts['omit_headers'] === 'undefined') omit_headers = omit_headers.concat(['cookie', 'origin', 'referer', 'via']); | ||
else if (Array.isArray(opts['omit_headers'])) omit_headers = omit_headers.concat(opts['omit_headers']); | ||
return function(request, response){ | ||
var uri_match = request.url.match('^/?(?://(https?):)?/?(/[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/.*)$'), set_referer = null; | ||
if (!uri_match) { | ||
var root_html_uri_match = request.url.match('^/?([a-zA-Z0-9_\\.\\-]+\\.(?:html?)(?:\\?.*)?)$'); | ||
if (!root_html_uri_match) { | ||
if (!request.headers.referer || (request.url.replace(/^\/+/gm,'').length < 4)) { | ||
var other_uri_match = request.url.match('^/?api/1/([a-z_\-]+)/?$'); | ||
if (!other_uri_match) return res404(response); | ||
else return doAPI(other_uri_match[1], request, response); | ||
} else { | ||
var base_uri = request.baseUrl ? '(?:'+ request.baseUrl.replace(/^\/+|\/+$/g,'').replace(/[\-\[\]\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&") +')?' : ''; | ||
var ref_uri_match = request.headers.referer.match('^(?:https?\\://[a-zA-Z0-9_\\.\\-:]+)/?'+base_uri+'(?://(https?):)?/?(/[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/.*)$'); | ||
if (!ref_uri_match) return res404(response); | ||
uri_match = ref_uri_match; | ||
set_referer = uri_match[2]; | ||
uri_match[2] = ref_uri_match[2].replace(/^(\/+[^\/]+).*$/,'$1') + '/' + request.url.replace(/^\/+/gm,''); | ||
} | ||
} else if (opts.root_url) { | ||
uri_match = root_html_uri_match; | ||
uri_match[2] = '/'+ opts.root_url +'/'+request.url.replace(/^\/+/gm,''); | ||
uri_match[1] = opts.root_schema || 'https'; | ||
} else { | ||
return res404(response); | ||
} | ||
} | ||
var scheme = (uri_match[1]) ? uri_match[1] : ( request.headers['x-forwarded-proto'] || 'http' ); | ||
var options = url.parse('/'+uri_match[2], false, true); | ||
if (!options.host) return res404(response); | ||
var headers = omit(request.headers, omit_headers); | ||
if (set_referer) headers['referer'] = 'http'+ (scheme == 'http' ? '' : 's') +':'+ set_referer; | ||
options.headers = headers; | ||
options.method = request.method; | ||
options.agent = false; | ||
options.timeout = 8000; | ||
var req = (scheme == 'http' ? http : https).request(options, function(res) { | ||
if (res.statusCode && res.headers) { | ||
if ( res.statusCode >= 200 && res.statusCode <= 299 ) { | ||
response.writeHead(res.statusCode, res.headers); | ||
res.pipe(response, {end:true}); | ||
} else { | ||
if(~[301,302].indexOf(res.statusCode) && res.headers.location) { | ||
var status_msg = '<!DOCTYPE html><html><head><title>'+res.statusCode+'</title></head><body><pre>'+JSON.stringify(res.headers, null, 4)+'</pre></body></html>'; | ||
res.headers['content-length'] = status_msg.length; | ||
res.headers['content-type'] = 'text/html'; | ||
response.writeHead(404, res.headers); | ||
response.end(status_msg); | ||
} | ||
else { | ||
response.writeHead(res.statusCode, res.headers); | ||
res.pipe(response, {end:true}); | ||
} | ||
} | ||
} else { | ||
res404(response); | ||
} | ||
}).on('error', function(e){res404(response);}); | ||
request.pipe(req, {end:true}); | ||
}; | ||
}; | ||
module.exports = function (opts) { | ||
if (typeof opts !== 'object') opts = {} | ||
var omitHeaders = ['request', 'host', 'connection', 'x-forwarded-for', 'x-forwarded-proto', 'x-forwarded-port', 'x-request-start', 'x-request-id'] | ||
if (typeof opts['omitHeaders'] === 'undefined') omitHeaders = omitHeaders.concat(['cookie', 'origin', 'referer', 'via']) | ||
else if (Array.isArray(opts['omitHeaders'])) omitHeaders = omitHeaders.concat(opts['omitHeaders']) | ||
return function (request, response) { | ||
var uriMatch = request.url.match('^/?(?://(https?):)?/?(/[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/.*)$') | ||
var setReferer = null | ||
if (!uriMatch) { | ||
var rootHtmlUriMatch = request.url.match('^/?([a-zA-Z0-9_\\.\\-]+\\.(?:html?)(?:\\?.*)?)$') | ||
if (!rootHtmlUriMatch) { | ||
if (!request.headers.referer || (request.url.replace(/^\/+/gm, '').length < 4)) { | ||
var otherUriMatch = request.url.match('^/?api/1/([a-z_-]+)/?$') | ||
if (!otherUriMatch) return res404(response) | ||
else return doAPI(otherUriMatch[1], request, response) | ||
} else { | ||
var baseUri = request.baseUrl ? '(?:' + request.baseUrl.replace(/^\/+|\/+$/g, '').replace(/[-[\]{}()*+?.\\^$|]/g, '\\$&') + ')?' : '' | ||
var refUriMatch = request.headers.referer.match('^(?:https?\\://[a-zA-Z0-9_\\.\\-:]+)/?' + baseUri + '(?://(https?):)?/?(/[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}/.*)$') | ||
if (!refUriMatch) return res404(response) | ||
uriMatch = refUriMatch | ||
setReferer = uriMatch[2] | ||
uriMatch[2] = refUriMatch[2].replace(/^(\/+[^/]+).*$/, '$1') + '/' + request.url.replace(/^\/+/gm, '') | ||
} | ||
} else if (opts.root_url) { | ||
uriMatch = rootHtmlUriMatch | ||
uriMatch[2] = '/' + opts.root_url + '/' + request.url.replace(/^\/+/gm, '') | ||
uriMatch[1] = opts.root_schema || 'https' | ||
} else { | ||
return res404(response) | ||
} | ||
} | ||
var scheme = (uriMatch[1]) ? uriMatch[1] : (request.headers['x-forwarded-proto'] || 'http') | ||
var options = url.parse('/' + uriMatch[2], false, true) | ||
if (!options.host) return res404(response) | ||
var headers = omit(request.headers, omitHeaders) | ||
if (setReferer) headers['referer'] = 'http' + (scheme === 'http' ? '' : 's') + ':' + setReferer | ||
options.headers = headers | ||
options.method = request.method | ||
options.agent = false | ||
options.timeout = 8000 | ||
var req = (scheme === 'http' ? http : https).request(options, function (res) { | ||
if (res.statusCode && res.headers) { | ||
if (res.statusCode >= 200 && res.statusCode <= 299) { | ||
response.writeHead(res.statusCode, res.headers) | ||
res.pipe(response, {end: true}) | ||
} else { | ||
if (~[301, 302].indexOf(res.statusCode) && res.headers.location) { | ||
var statusMsg = '<!DOCTYPE html><html><head><title>' + res.statusCode + '</title></head><body><pre>' + JSON.stringify(res.headers, null, 4) + '</pre></body></html>' | ||
res.headers['content-length'] = statusMsg.length | ||
res.headers['content-type'] = 'text/html' | ||
response.writeHead(404, res.headers) | ||
response.end(statusMsg) | ||
} else { | ||
response.writeHead(res.statusCode, res.headers) | ||
res.pipe(response, {end: true}) | ||
} | ||
} | ||
} else { | ||
res404(response) | ||
} | ||
}).on('error', function (e) { res404(response) }) | ||
request.pipe(req, {end: true}) | ||
} | ||
} |
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
module.exports = function (res){ | ||
res.writeHead(404, 'Not Found', {'content-length': 9, 'content-type': 'text/plain'}); | ||
res.end('Not Found'); | ||
}; | ||
'use strict' | ||
module.exports = function (res) { | ||
res.writeHead(404, 'Not Found', {'content-length': 9, 'content-type': 'text/plain'}) | ||
res.end('Not Found') | ||
} |
{ | ||
"name": "bfn-proxy", | ||
"version": "1.2.6", | ||
"version": "1.2.8", | ||
"description": "HTTP request proxy middleware for node.js", | ||
@@ -31,8 +31,23 @@ "author": "Blue Fidelity Inc. (https://www.bluefidelity.com/)", | ||
"devDependencies": { | ||
"express": "^4.15.4", | ||
"mocha": "^3.5.3" | ||
"eslint": "3.19.0", | ||
"eslint-config-standard": "10.2.1", | ||
"eslint-plugin-import": "2.7.0", | ||
"eslint-plugin-markdown": "1.0.0-beta.6", | ||
"eslint-plugin-node": "5.1.1", | ||
"eslint-plugin-promise": "3.5.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "2.5.3", | ||
"split": "1.0.1", | ||
"supertest": "1.1.0" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
"lint": "eslint --plugin markdown --ext js,md .", | ||
"test": "mocha --check-leaks --reporter spec --bail", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec" | ||
} | ||
} |
@@ -6,2 +6,3 @@ # bfn-proxy | ||
[![Build Status][travis-image]][travis-url] | ||
[![Test Coverage][coveralls-image]][coveralls-url] | ||
@@ -32,4 +33,4 @@ Designed to be the simplest way possible to proxy http calls. | ||
require('http') | ||
.createServer(require('bfn-proxy')()) | ||
.listen(process.env.PORT||8080) | ||
.createServer(require('bfn-proxy')()) | ||
.listen(process.env.PORT || 8080) | ||
``` | ||
@@ -61,6 +62,6 @@ | ||
app.use('/pxy/', function (req, res, next) { | ||
if (req.connection.remoteAddress !== '127.0.0.1') { | ||
return next(new Error('Bad authentication data')) | ||
} | ||
pxy(req, res) | ||
if (req.connection.remoteAddress !== '127.0.0.1') { | ||
return next(new Error('Bad authentication data')) | ||
} | ||
pxy(req, res) | ||
}) | ||
@@ -81,2 +82,4 @@ ``` | ||
[travis-url]: https://travis-ci.org/BlueFidelity/bfn-proxy | ||
[coveralls-image]: https://img.shields.io/coveralls/BlueFidelity/bfn/master.svg | ||
[coveralls-url]: https://coveralls.io/r/BlueFidelity/bfn?branch=master | ||
@@ -1,10 +0,10 @@ | ||
var assert = require('assert'); | ||
var pxy = require('../index'); | ||
var assert = require('assert') | ||
var pxy = require('../index') | ||
describe('bfn-proxy', function() { | ||
describe('bfn-proxy()', function() { | ||
it('should return function', function () { | ||
assert.equal('function', typeof pxy()); | ||
}); | ||
}); | ||
}); | ||
describe('bfn-proxy', function () { | ||
describe('bfn-proxy()', function () { | ||
it('should return function', function () { | ||
assert.equal('function', typeof pxy()) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10435
13
82
11
3