Comparing version 8.1.0 to 8.1.1
@@ -44,5 +44,9 @@ /** | ||
fn = path; | ||
path = "*"; | ||
path = ""; | ||
} | ||
if (path === "*") { | ||
path = ""; | ||
} | ||
app.stack.push({route: path, handle: fn, id: opts.id}); | ||
@@ -49,0 +53,0 @@ } |
@@ -1,3 +0,4 @@ | ||
var respMod = require("resp-modifier"); | ||
var utils = require("./utils"); | ||
var respMod = require("resp-modifier"); | ||
var utils = require("./utils"); | ||
var parseUrl = require("parseurl"); | ||
@@ -7,17 +8,25 @@ /** | ||
* @param proxyServer | ||
* @param userConfig | ||
* @param getOpts | ||
* @returns {Function} | ||
*/ | ||
module.exports = function (proxyServer, userConfig) { | ||
module.exports = function (proxyServer, getOpts) { | ||
return function (req, res) { | ||
var userConf = userConfig(); | ||
var config = userConf.config; | ||
var stack = userConf.stack; | ||
var opts = getOpts(); | ||
var config = opts.config; | ||
var stack = opts.stack; | ||
var foxyNext = function () { | ||
/** | ||
* Create middleware on the fly to match the host | ||
*/ | ||
var middleware = respMod({ | ||
rules: utils.getRules(config, req.headers.host), | ||
ignorePaths: config.get("ignorePaths") | ||
}); | ||
var finalHandler = function () { | ||
proxyServer.web(req, res, { | ||
target: config.get("target"), | ||
headers: { | ||
target: config.get("target"), | ||
headers: { | ||
"host": config.get("hostHeader"), | ||
@@ -30,62 +39,121 @@ "accept-encoding": "identity", | ||
if (!stack.some(function (item) { | ||
return item.id === "resp-mod"; | ||
})) { | ||
stack.push({ | ||
route: "*", | ||
handle: getMw(config, req), | ||
id: "resp-mod" | ||
}); | ||
} else { | ||
stack = stack.map(function (item) { | ||
if (item.id === "resp-mod") { | ||
item.handle = getMw(config, req); | ||
} | ||
return item; | ||
}); | ||
handle(stack, req, res, function () { | ||
middleware(req, res, finalHandler); | ||
}); | ||
}; | ||
}; | ||
/** | ||
* @type {setImmediate} | ||
*/ | ||
var defer = typeof setImmediate === "function" ? setImmediate : function (fn) { | ||
process.nextTick(fn.bind.apply(fn, arguments)); | ||
}; | ||
/** | ||
* Handle server requests, punting them down | ||
* the middleware stack. | ||
* | ||
* @api private | ||
*/ | ||
function handle(stack, req, res, out) { | ||
var searchIndex = req.url.indexOf("?"); | ||
var pathlength = searchIndex !== -1 ? searchIndex : req.url.length; | ||
var fqdn = req.url[0] !== "/" && 1 + req.url.substr(0, pathlength).indexOf("://"); | ||
var protohost = fqdn ? req.url.substr(0, req.url.indexOf("/", 2 + fqdn)) : ""; | ||
var removed = ""; | ||
var slashAdded = false; | ||
var index = 0; | ||
// final function handler | ||
var done = out; | ||
// store the original URL | ||
req.originalUrl = req.originalUrl || req.url; | ||
function next(err) { | ||
if (slashAdded) { | ||
req.url = req.url.substr(1); | ||
slashAdded = false; | ||
} | ||
if (!stack.some(function (item) { | ||
return item.id === "foxy-links"; | ||
})) { | ||
stack.push({ | ||
route: "*", | ||
handle: foxyNext, | ||
id: "foxy-links" | ||
}); | ||
} else { | ||
stack = stack.map(function (item) { | ||
if (item.id === "foxy-links") { | ||
item.handle = foxyNext; | ||
} | ||
return item; | ||
}); | ||
if (removed.length !== 0) { | ||
req.url = protohost + removed + req.url.substr(protohost.length); | ||
removed = ""; | ||
} | ||
if (stack.length) { | ||
var canContinue = true; | ||
stack.forEach(function (item) { | ||
if (canContinue) { | ||
canContinue = false; | ||
if (item.route === "*" || item.route === "" || req.url === item.route) { | ||
item.handle(req, res, function () { | ||
canContinue = true; | ||
}); | ||
} | ||
} | ||
}); | ||
// next callback | ||
var layer = stack[index++]; // jshint ignore:line | ||
// all done | ||
if (!layer) { | ||
defer(done, err); | ||
return; | ||
} | ||
}; | ||
}; | ||
// route data | ||
var path = parseUrl(req).pathname || "/"; | ||
var route = layer.route; | ||
// skip this layer if the route doesn't match | ||
if (path.toLowerCase().substr(0, route.length) !== route.toLowerCase()) { | ||
return next(err); | ||
} | ||
// skip if route match does not border "/", ".", or end | ||
var c = path[route.length]; | ||
if (c !== undefined && "/" !== c && "." !== c) { | ||
return next(err); | ||
} | ||
// trim off the part of the url that matches the route | ||
if (route.length !== 0 && route !== "/") { | ||
removed = route; | ||
req.url = protohost + req.url.substr(protohost.length + removed.length); | ||
// ensure leading slash | ||
if (!fqdn && req.url[0] !== "/") { | ||
req.url = "/" + req.url; | ||
slashAdded = true; | ||
} | ||
} | ||
// call the layer handle | ||
call(layer.handle, route, err, req, res, next); | ||
} | ||
next(); | ||
} | ||
/** | ||
* @param config | ||
* @param req | ||
* @returns {*} | ||
* Invoke a route handle. | ||
* | ||
* @api private | ||
*/ | ||
function getMw(config, req) { | ||
return respMod({ | ||
rules: utils.getRules(config, req.headers.host), | ||
ignorePaths: config.get("ignorePaths") | ||
}); | ||
function call(handle, route, err, req, res, next) { | ||
var arity = handle.length; | ||
var hasError = Boolean(err); | ||
//console.log("%s %s : %s", handle.name || "<anonymous>", route, req.originalUrl); | ||
try { | ||
if (hasError && arity === 4) { | ||
// error-handling middleware | ||
handle(err, req, res, next); | ||
return; | ||
} else if (!hasError && arity < 4) { | ||
// request-handling middleware | ||
handle(req, res, next); | ||
return; | ||
} | ||
} catch (e) { | ||
// reset the error | ||
err = e; | ||
} | ||
// continue | ||
next(err); | ||
} |
{ | ||
"name": "foxy", | ||
"version": "8.1.0", | ||
"version": "8.1.1", | ||
"description": "Proxy with response modding", | ||
@@ -41,2 +41,3 @@ "main": "index.js", | ||
"meow": "^2.0.0", | ||
"parseurl": "^1.3.0", | ||
"resp-modifier": "^1.0.0" | ||
@@ -43,0 +44,0 @@ }, |
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
19481
590
8
+ Addedparseurl@^1.3.0
+ Addedparseurl@1.3.3(transitive)