solr-proxy
Advanced tools
Comparing version 2.1.0 to 2.1.1
#!/usr/bin/env node | ||
var cli = require('../lib/cli'); | ||
var argv = require('minimist')(process.argv.slice(2)); | ||
var SolrProxy = require('../'); | ||
var cli = require('../lib/cli') | ||
var argv = require('minimist')(process.argv.slice(2)) | ||
var SolrProxy = require('../') | ||
cli.argv(argv, console.log, SolrProxy); | ||
cli.argv(argv, console.log, SolrProxy) |
92
index.js
@@ -1,6 +0,6 @@ | ||
var httpProxy = require('http-proxy'), | ||
url = require('url'), | ||
extend = require('xtend'), | ||
debug = require('debug')('solr-proxy'), | ||
SolrProxy = {}; | ||
var httpProxy = require('http-proxy') | ||
var url = require('url') | ||
var extend = require('xtend') | ||
var debug = require('debug')('solr-proxy') | ||
var SolrProxy = {} | ||
@@ -13,14 +13,14 @@ /* | ||
*/ | ||
var validateRequest = function(request, options) { | ||
var parsedUrl = url.parse(request.url, true), | ||
path = parsedUrl.pathname, | ||
queryParams = Object.keys(parsedUrl.query); | ||
var validateRequest = function (request, options) { | ||
var parsedUrl = url.parse(request.url, true) | ||
var path = parsedUrl.pathname | ||
var queryParams = Object.keys(parsedUrl.query) | ||
return options.validHttpMethods.indexOf(request.method) !== -1 && | ||
options.validPaths.indexOf(path) !== -1 && | ||
queryParams.every(function(p) { | ||
var paramPrefix = p.split('.')[0]; // invalidate not just "stream", but "stream.*" | ||
return options.invalidParams.indexOf(paramPrefix) === -1; | ||
}); | ||
}; | ||
queryParams.every(function (p) { | ||
var paramPrefix = p.split('.')[0] // invalidate not just "stream", but "stream.*" | ||
return options.invalidParams.indexOf(paramPrefix) === -1 | ||
}) | ||
} | ||
@@ -36,48 +36,48 @@ var defaultOptions = { | ||
} | ||
}; | ||
} | ||
var createServer = function(options) { | ||
var proxy = httpProxy.createProxyServer({target: options.backend}); | ||
var createServer = function (options) { | ||
var proxy = httpProxy.createProxyServer({target: options.backend}) | ||
proxy.on('error', function(err, req, res) { | ||
res.writeHead(502, { 'Content-Type': 'text/plain' }); | ||
res.end('Proxy error: ' + err); | ||
}); | ||
proxy.on('error', function (err, req, res) { | ||
res.writeHead(502, { 'Content-Type': 'text/plain' }) | ||
res.end('Proxy error: ' + err) | ||
}) | ||
var createServer; | ||
var createServer | ||
if (options.ssl) { | ||
const https = require('https'); | ||
createServer = (callback) => https.createServer(options.ssl, callback); | ||
const https = require('https') | ||
createServer = (callback) => https.createServer(options.ssl, callback) | ||
} else { | ||
const http = require('http'); | ||
createServer = http.createServer; | ||
const http = require('http') | ||
createServer = http.createServer | ||
} | ||
// adapted from https://git.io/k5dCxQ | ||
var server = createServer(function(request, response) { | ||
var server = createServer(function (request, response) { | ||
if (validateRequest(request, options)) { | ||
debug('ALLOWED: ' + request.method + ' ' + request.url); | ||
proxy.web(request, response); | ||
debug('ALLOWED: ' + request.method + ' ' + request.url) | ||
proxy.web(request, response) | ||
} else { | ||
debug('DENIED: ' + request.method + ' ' + request.url); | ||
response.writeHead(403, 'Illegal request'); | ||
response.write('solrProxy: access denied\n'); | ||
response.end(); | ||
debug('DENIED: ' + request.method + ' ' + request.url) | ||
response.writeHead(403, 'Illegal request') | ||
response.write('solrProxy: access denied\n') | ||
response.end() | ||
} | ||
}); | ||
return server; | ||
}; | ||
}) | ||
return server | ||
} | ||
SolrProxy.start = function(port, options) { | ||
options = options || {}; | ||
options.backend = extend(defaultOptions.backend, options.backend); | ||
options = extend(defaultOptions, options); | ||
SolrProxy.start = function (port, options) { | ||
options = options || {} | ||
options.backend = extend(defaultOptions.backend, options.backend) | ||
options = extend(defaultOptions, options) | ||
port = port || options.listenPort; | ||
port = port || options.listenPort | ||
var server = createServer(options); | ||
server.listen(port); | ||
return server; | ||
}; | ||
var server = createServer(options) | ||
server.listen(port) | ||
return server | ||
} | ||
module.exports = SolrProxy; | ||
module.exports = SolrProxy |
var createProxyOptions = function (argv) { | ||
var proxyOptions = { | ||
backend: {} | ||
}; | ||
} | ||
if (argv.backendPort) { | ||
proxyOptions.backend.port = argv.backendPort; | ||
proxyOptions.backend.port = argv.backendPort | ||
} | ||
if (argv.backendHost) { | ||
proxyOptions.backend.host = argv.backendHost; | ||
proxyOptions.backend.host = argv.backendHost | ||
} | ||
if (argv.validMethods) { | ||
proxyOptions.validHttpMethods = argv.validMethods.split(','); | ||
proxyOptions.validHttpMethods = argv.validMethods.split(',') | ||
} | ||
if (argv.invalidParams) { | ||
proxyOptions.invalidParams = argv.invalidParams.split(','); | ||
proxyOptions.invalidParams = argv.invalidParams.split(',') | ||
} | ||
if (argv.validPaths) { | ||
proxyOptions.validPaths = argv.validPaths.split(','); | ||
proxyOptions.validPaths = argv.validPaths.split(',') | ||
} | ||
return proxyOptions; | ||
}; | ||
return proxyOptions | ||
} | ||
module.exports = function(argv, stdout, SolrProxy) { | ||
module.exports = function (argv, stdout, SolrProxy) { | ||
var usageMessage = 'Usage: solr-proxy [options]\n' + | ||
@@ -44,13 +43,13 @@ '\n' + | ||
' --version, -v Show version\n' + | ||
' --help, -h Show this message'; | ||
' --help, -h Show this message' | ||
if (argv.help || argv.h) { | ||
stdout(usageMessage); | ||
return; | ||
stdout(usageMessage) | ||
return | ||
} | ||
if (argv.version || argv.v) { | ||
var version = require('../../package.json').version; | ||
stdout(version); | ||
return; | ||
var version = require('../../package.json').version | ||
stdout(version) | ||
return | ||
} | ||
@@ -60,9 +59,9 @@ | ||
// quiet mode, change stdout to a no-op | ||
stdout = function () {}; | ||
stdout = function () {} | ||
} | ||
var proxyOptions = createProxyOptions(argv); | ||
var proxyOptions = createProxyOptions(argv) | ||
SolrProxy.start(argv.port, proxyOptions); | ||
stdout('solr-proxy is running...'); | ||
}; | ||
SolrProxy.start(argv.port, proxyOptions) | ||
stdout('solr-proxy is running...') | ||
} |
module.exports = { | ||
argv: require('./argv') | ||
}; | ||
} |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"author": "Rich Trott <rtrott@gmail.com>", | ||
@@ -18,3 +18,3 @@ "bugs": { | ||
"scripts": { | ||
"test": "lab -t 100 test" | ||
"test": "standard && lab -t 100 test" | ||
}, | ||
@@ -37,8 +37,10 @@ "bin": { | ||
"http-proxy": "^1.10.1", | ||
"minimist": "^1.2.0", | ||
"xtend": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"code": "^3.0.2", | ||
"lab": "^15.4.5", | ||
"request": "^2.86.0" | ||
"code": "^5.2.0", | ||
"lab": "^15.5.0", | ||
"request": "^2.86.0", | ||
"standard": "^11.0.1" | ||
}, | ||
@@ -45,0 +47,0 @@ "engines": { |
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
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
10024
131
4
4
+ Addedminimist@^1.2.0
+ Addedminimist@1.2.8(transitive)