kopia lustrzana https://github.com/c9/core
97 wiersze
3.4 KiB
JavaScript
97 wiersze
3.4 KiB
JavaScript
/**
|
|
* Base URL utilities for the Ajax.org Cloud IDE
|
|
*
|
|
* Access via require("c9/urls").
|
|
*
|
|
* @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 main(options, imports, register) {
|
|
errorLogger = imports["error.logger"];
|
|
|
|
register(null, {
|
|
urls: plugin
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get a desired base URL, given some context.
|
|
*
|
|
* @param req The current request object or URL
|
|
* @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;
|
|
if (typeof sourceHost !== "string")
|
|
throw new Error("Not a valid request object: " + req);
|
|
if (!sourceBaseUrlPattern)
|
|
throw new Error("getBaseUrls() requires at least two arguments");
|
|
|
|
var sourceHostMatcher = sourceBaseUrlPattern
|
|
.replace(/^https?:\/\//, "")
|
|
.replace(/\/.*/, "")
|
|
.replace(/\./, "\\.")
|
|
.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));
|
|
targetHost = "c9.io";
|
|
}
|
|
|
|
if (targetHost.match(/^(ide|vfs)./) && !targetBaseUrlPattern)
|
|
errorLogger.log(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);
|
|
return replaceDomain(targetBaseUrlPattern || sourceBaseUrlPattern, targetHost)
|
|
.replace(/\/$/, "");
|
|
};
|
|
|
|
plugin.replaceDomains = function(settings, domains) {
|
|
domains = Array.isArray(domains) ? domains : domains.split(",");
|
|
var primaryDomain = domains[0];
|
|
settings.domains = domains;
|
|
settings.primaryDomain = replaceDomain(settings.primaryDomain || "$DOMAIN", primaryDomain);
|
|
settings.primaryBaseUrl = replaceDomain(settings.primaryBaseUrl || "https://$DOMAIN", primaryDomain);
|
|
for (var s in settings) {
|
|
if (!settings[s])
|
|
continue;
|
|
if (settings[s].baseUrl)
|
|
settings[s].baseUrl = replaceDomain(settings[s].baseUrl, primaryDomain);
|
|
if (settings[s].primaryBaseUrl)
|
|
settings[s].primaryBaseUrl = replaceDomain(settings[s].primaryBaseUrl, primaryDomain);
|
|
if (settings[s].baseUrls) {
|
|
assert(settings[s].baseUrls.length === 1);
|
|
settings[s].baseUrls = domains.map(function(d) {
|
|
return replaceDomain(settings[s].baseUrls[0], d);
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
function replaceDomain(url, domain) {
|
|
return url.replace("$DOMAIN", domain);
|
|
}
|
|
|
|
function mockErrorLogger() {}
|
|
mockErrorLogger.log = function(err) {
|
|
console.error(err.stack);
|
|
};
|
|
plugin.mockErrorLogger = mockErrorLogger;
|