From 01e56ab0afe1f66606a08308bdd73d051782fa0a Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Mon, 19 Oct 2015 22:30:12 +0000 Subject: [PATCH] restructure not to be architect plugin --- node_modules/c9/urls.js | 60 +++++++++++++++++++++-------------------- package.json | 2 ++ 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/node_modules/c9/urls.js b/node_modules/c9/urls.js index a1b000af..a4ea6e7d 100644 --- a/node_modules/c9/urls.js +++ b/node_modules/c9/urls.js @@ -6,20 +6,19 @@ * @copyright 2015, Ajax.org B.V. */ -var plugin = module.exports = main; var assert = require("assert"); - -plugin.consumes = ["error.logger"]; -plugin.provides = ["urls"]; -var errorLogger = mockErrorLogger; +function getHost(req) { + if (req.headers) + return req.headers.host; -function main(options, imports, register) { - errorLogger = imports["error.logger"]; - - register(null, { - urls: plugin - }); + if (req.host) + return req.host; + + if (req.url) + return req.url.replace(/^https?:\/\/([^/]*).*/, "$1"); + + return req; } /** @@ -29,13 +28,12 @@ function main(options, imports, register) { * @param {String} [sourceUrlPattern] The source URL pattern, e.g. https://ide.$DOMAIN * @param {String} targetBaseUrlPattern The target URL pattern, e.g. https://$DOMAIN */ -plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) { - var sourceHost = req.headers && req.headers.host - || req.host - || req.url && req.url.replace(/^https?:\/\/([^/]*).*/, "$1") - || req; +function getBaseUrl(req, sourceBaseUrlPattern, targetBaseUrlPattern) { + var sourceHost = getHost(req); + if (typeof sourceHost !== "string") throw new Error("Not a valid request object: " + req); + if (!sourceBaseUrlPattern) throw new Error("getBaseUrl() requires at least two arguments"); @@ -46,24 +44,29 @@ plugin.getBaseUrl = function(req, sourceBaseUrlPattern, targetBaseUrlPattern) { .replace("$DOMAIN", "([^/]+)"); var hostMatch = sourceHost.match(sourceHostMatcher); var targetHost; + if (hostMatch) { targetHost = hostMatch[1]; } else { - errorLogger.log(new Error("Could not construct URL: request host " - + sourceHost + " should match " + sourceBaseUrlPattern)); + console.error(new Error("Could not construct URL: request host " + sourceHost + " should match " + sourceBaseUrlPattern)); + targetHost = "c9.io"; } - + if (targetHost.match(/^(ide|vfs)./) && !targetBaseUrlPattern) - errorLogger.log(new Error("Warning: no targetBaseUrlPattern specified, will stay at " + targetHost), { sourceBaseUrlPattern: sourceBaseUrlPattern }); + console.error(new Error("Warning: no targetBaseUrlPattern specified, will stay at " + targetHost), { + sourceBaseUrlPattern: sourceBaseUrlPattern + }); + if (targetHost.match(/^(ide|vfs)./)) - console.trace("Warning: possibly incorrect baseUrl constructed, with 'ide.' in the hostname: " + targetHost); + console.trace("Warning: possibly incorrect baseUrl constructed, with 'ide.' in the hostname: " + targetHost); + return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, targetHost) .replace(/\/$/, ""); -}; +} -plugin.replaceDomains = function(settings, domains) { +function replaceDomains(settings, domains) { domains = Array.isArray(domains) ? domains : domains.split(","); var primaryDomain = domains[0]; settings.domains = domains; @@ -85,14 +88,13 @@ plugin.replaceDomains = function(settings, domains) { }); } } -}; +} function replaceDomain(url, domain) { return url.replace("$DOMAIN", domain); } -function mockErrorLogger() {} -mockErrorLogger.log = function(err) { - console.error(err.stack); -}; -plugin.mockErrorLogger = mockErrorLogger; +module.exports = { + replaceDomains: replaceDomains, + getBaseUrl: getBaseUrl, +}; \ No newline at end of file diff --git a/package.json b/package.json index 1b933791..ce62d692 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,8 @@ "c9.ide.save": "#e00549cb0f", "c9.ide.scm": "#38d2b48b31", "c9.ide.terminal.monitor": "#b76f1c9f24", + "c9.ide.test": "#d312a01ac6", + "c9.ide.test.mocha": "#79ff886c04", "c9.ide.theme.flat": "#2de8414db7", "c9.ide.threewaymerge": "#229382aa0b", "c9.ide.undo": "#b028bcb4d5",