make Prettier

This commit is contained in:
AJ ONeal 2020-04-26 17:31:48 -06:00
parent 16251a99b9
commit 0e5fff5c4a
1 changed files with 91 additions and 87 deletions

View File

@ -1,7 +1,7 @@
'use strict'; "use strict";
module.exports = function (opts) { module.exports = function (opts) {
var escapeHtml = require('escape-html'); var escapeHtml = require("escape-html");
if (!opts) { if (!opts) {
opts = {}; opts = {};
@ -13,70 +13,74 @@ module.exports = function (opts) {
opts.browsers = 301; opts.browsers = 301;
} }
if (!opts.apis) { if (!opts.apis) {
opts.apis = 'meta'; opts.apis = "meta";
} }
if (!Array.isArray(opts.paths)) { if (!Array.isArray(opts.paths)) {
opts.paths = [ { match: '/' } ]; opts.paths = [{ match: "/" }];
} }
if (!('body' in opts)) { if (!("body" in opts)) {
opts.body = "<!-- Hello Developer Person! We don't serve insecure resources around here." opts.body =
+ "\n Please use HTTPS instead. -->"; "<!-- Hello Developer Person! We don't serve insecure resources around here." +
"\n Please use HTTPS instead. -->";
} }
opts.body = opts.body.replace(/{{\s+PORT\s+}}/ig, opts.port); opts.body = opts.body.replace(/{{\s+PORT\s+}}/gi, opts.port);
return function (req, res, next) { return function (req, res, next) {
if (req.connection.encrypted if (
|| 'https' === req.protocol req.connection.encrypted ||
|| (opts.trustProxy && 'https' === req.headers['x-forwarded-proto']) "https" === req.protocol ||
(opts.trustProxy && "https" === req.headers["x-forwarded-proto"])
) { ) {
next(); next();
return; return;
} }
var url = (req.originalUrl || req.url); var url = req.originalUrl || req.url;
// We don't want chrome showing the "Not Secure" badge during the redirect. // We don't want chrome showing the "Not Secure" badge during the redirect.
var probablyBrowser = (0 === (req.headers['user-agent']||'').indexOf('Mozilla/')); var probablyBrowser =
0 === (req.headers["user-agent"] || "").indexOf("Mozilla/");
// But we don't want devs, APIs, or Bots to accidentally browse insecure. // But we don't want devs, APIs, or Bots to accidentally browse insecure.
var redirect = probablyBrowser ? opts.browsers : opts.apis; var redirect = probablyBrowser ? opts.browsers : opts.apis;
var host = req.headers.host || ''; var host = req.headers.host || "";
if (!/:\d+/.test(host) && 443 !== opts.port) { if (!/:\d+/.test(host) && 443 !== opts.port) {
// we are using standard port 80, but we aren't using standard port 443 // we are using standard port 80, but we aren't using standard port 443
host += ':80'; host += ":80";
} }
var newLocation = 'https://' var newLocation =
+ host.replace(/:\d+/, ':' + opts.port) + url "https://" + host.replace(/:\d+/, ":" + opts.port) + url;
;
//var encodedLocation = encodeURI(newLocation); //var encodedLocation = encodeURI(newLocation);
var escapedLocation = escapeHtml(newLocation); var escapedLocation = escapeHtml(newLocation);
var decodedLocation; var decodedLocation;
try { try {
decodedLocation = decodeURIComponent(newLocation); decodedLocation = decodeURIComponent(newLocation);
} catch(e) { } catch (e) {
decodedLocation = newLocation; // "#/error/?error_message=" + e.toString(); decodedLocation = newLocation; // "#/error/?error_message=" + e.toString();
} }
var body = opts.body var body = opts.body
.replace(/{{\s*HTML_URL\s*}}/ig, escapeHtml(decodedLocation)) .replace(/{{\s*HTML_URL\s*}}/gi, escapeHtml(decodedLocation))
.replace(/{{\s*URL\s*}}/ig, escapedLocation) .replace(/{{\s*URL\s*}}/gi, escapedLocation)
.replace(/{{\s*UNSAFE_URL\s*}}/ig, newLocation) .replace(/{{\s*UNSAFE_URL\s*}}/gi, newLocation);
; var metaRedirect =
"" +
var metaRedirect = '' "<html>" +
+ '<html>' '\n<head><META http-equiv="refresh" content="0;URL=\'' +
+ '\n<head><META http-equiv="refresh" content="0;URL=\'' + escapedLocation + '\'"></head>' escapedLocation +
+ '\n<body>' + body + '</body>' "'\"></head>" +
+ '\n</html>\n' "\n<body>" +
; body +
"</body>" +
"\n</html>\n";
var pathMatch; var pathMatch;
opts.paths.some(function (p) { opts.paths.some(function (p) {
if (!p.match) { if (!p.match) {
// ignore // ignore
} else if ('string' === typeof p.match) { } else if ("string" === typeof p.match) {
pathMatch = (url === p.match) && (p.redirect || 301); pathMatch = url === p.match && (p.redirect || 301);
} else { } else {
pathMatch = p.match.test && p.match.test(url) && (p.redirect || 301); pathMatch =
p.match.test && p.match.test(url) && (p.redirect || 301);
} }
if (pathMatch) { if (pathMatch) {
redirect = pathMatch; redirect = pathMatch;
@ -86,9 +90,9 @@ module.exports = function (opts) {
// If it's not a non-0 number (because null is 0) then 'meta' is assumed. // If it's not a non-0 number (because null is 0) then 'meta' is assumed.
if (redirect && isFinite(redirect)) { if (redirect && isFinite(redirect)) {
res.statusCode = redirect; res.statusCode = redirect;
res.setHeader('Location', newLocation); res.setHeader("Location", newLocation);
} }
res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(metaRedirect); res.end(metaRedirect);
}; };
}; };