2018-10-23 23:48:53 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-23 01:58:37 +08:00
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function ResourceLocator (environment) {
|
2017-03-23 01:58:37 +08:00
|
|
|
this.environment = environment;
|
|
|
|
|
|
|
|
this.resourcesUrlTemplates = null;
|
|
|
|
if (this.environment.resources_url_templates) {
|
|
|
|
var templates = environment.resources_url_templates;
|
|
|
|
|
|
|
|
if (templates.http) {
|
|
|
|
this.resourcesUrlTemplates = this.resourcesUrlTemplates || {};
|
|
|
|
this.resourcesUrlTemplates.http = dot.template(templates.http + '/{{=it.resource}}');
|
|
|
|
}
|
|
|
|
if (templates.https) {
|
|
|
|
this.resourcesUrlTemplates = this.resourcesUrlTemplates || {};
|
|
|
|
this.resourcesUrlTemplates.https = dot.template(templates.https + '/{{=it.resource}}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ResourceLocator;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ResourceLocator.prototype.getTileUrls = function (username, resourcePath) {
|
2017-03-23 01:58:37 +08:00
|
|
|
if (this.resourcesUrlTemplates) {
|
2018-01-03 23:05:19 +08:00
|
|
|
const urls = this.getUrlsFromTemplate(username, new TileResource(resourcePath));
|
|
|
|
return {
|
|
|
|
http: Array.isArray(urls.http) ? urls.http : [urls.http],
|
|
|
|
https: Array.isArray(urls.https) ? urls.https : [urls.https]
|
|
|
|
};
|
2017-03-23 01:58:37 +08:00
|
|
|
}
|
2018-01-03 23:05:19 +08:00
|
|
|
var cdnUrls = getCdnUrls(this.environment.serverMetadata, username, new TileResource(resourcePath));
|
|
|
|
if (cdnUrls) {
|
|
|
|
return cdnUrls;
|
|
|
|
} else {
|
|
|
|
var port = this.environment.port;
|
2017-03-23 01:58:37 +08:00
|
|
|
return {
|
2018-01-03 23:05:19 +08:00
|
|
|
http: [`http://${username}.localhost.lan:${port}/api/v1/map/${resourcePath}`]
|
2017-03-23 01:58:37 +08:00
|
|
|
};
|
2018-01-03 23:05:19 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ResourceLocator.prototype.getTemplateUrls = function (username, resourcePath) {
|
2018-01-04 04:53:03 +08:00
|
|
|
if (this.resourcesUrlTemplates) {
|
2018-01-04 07:33:59 +08:00
|
|
|
return this.getUrlsFromTemplate(username, new TemplateResource(resourcePath), true);
|
2018-01-04 04:53:03 +08:00
|
|
|
}
|
|
|
|
var cdnUrls = getCdnUrls(this.environment.serverMetadata, username, new TemplateResource(resourcePath));
|
|
|
|
if (cdnUrls) {
|
|
|
|
return cdnUrls;
|
|
|
|
} else {
|
|
|
|
var port = this.environment.port;
|
|
|
|
return {
|
|
|
|
http: {
|
|
|
|
urlTemplate: `http://${username}.localhost.lan:${port}/api/v1/map/${resourcePath}`,
|
|
|
|
subdomains: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ResourceLocator.prototype.getUrls = function (username, resourcePath) {
|
2018-01-03 23:05:19 +08:00
|
|
|
if (this.resourcesUrlTemplates) {
|
|
|
|
return this.getUrlsFromTemplate(username, new Resource(resourcePath));
|
|
|
|
}
|
|
|
|
var cdnUrls = getCdnUrls(this.environment.serverMetadata, username, new Resource(resourcePath));
|
|
|
|
if (cdnUrls) {
|
|
|
|
return cdnUrls;
|
2017-03-23 01:58:37 +08:00
|
|
|
} else {
|
|
|
|
var port = this.environment.port;
|
|
|
|
return {
|
2018-01-03 23:05:19 +08:00
|
|
|
http: `http://${username}.localhost.lan:${port}/api/v1/map/${resourcePath}`
|
2017-03-23 01:58:37 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-23 23:52:19 +08:00
|
|
|
ResourceLocator.prototype.urlForTemplate = function (tpl, username, cdnDomain, resource, templated) {
|
2018-01-04 07:33:59 +08:00
|
|
|
cdnDomain = cdnDomain || {};
|
|
|
|
if (templated) {
|
2018-01-04 04:53:03 +08:00
|
|
|
return {
|
|
|
|
urlTemplate: tpl({
|
2019-10-22 05:33:27 +08:00
|
|
|
cdn_url: (Object.prototype.hasOwnProperty.call(cdnDomain, 'urlTemplate') ? cdnDomain.urlTemplate : cdnDomain),
|
2018-01-04 04:53:03 +08:00
|
|
|
user: username,
|
|
|
|
port: this.environment.port,
|
|
|
|
resource: resource.getPath()
|
|
|
|
}),
|
2018-01-04 07:33:59 +08:00
|
|
|
subdomains: cdnDomain.subdomains || []
|
2018-01-04 04:53:03 +08:00
|
|
|
};
|
2018-01-04 07:33:59 +08:00
|
|
|
}
|
|
|
|
if (Array.isArray(cdnDomain)) {
|
2018-01-04 04:53:03 +08:00
|
|
|
return cdnDomain.map(d => tpl({
|
|
|
|
cdn_url: d,
|
|
|
|
user: username,
|
|
|
|
port: this.environment.port,
|
|
|
|
resource: resource.getPath()
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
return tpl({
|
|
|
|
cdn_url: cdnDomain,
|
|
|
|
user: username,
|
|
|
|
port: this.environment.port,
|
|
|
|
resource: resource.getPath()
|
|
|
|
});
|
|
|
|
}
|
2018-10-23 23:55:35 +08:00
|
|
|
};
|
2017-03-23 01:58:37 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
ResourceLocator.prototype.getUrlsFromTemplate = function (username, resource, templated) {
|
2017-03-23 01:58:37 +08:00
|
|
|
var urls = {};
|
|
|
|
var cdnDomain = getCdnDomain(this.environment.serverMetadata, resource) || {};
|
|
|
|
if (this.resourcesUrlTemplates.http) {
|
2018-10-23 23:54:11 +08:00
|
|
|
urls.http = this.urlForTemplate(
|
|
|
|
this.resourcesUrlTemplates.http,
|
|
|
|
username,
|
|
|
|
cdnDomain.http,
|
|
|
|
resource,
|
|
|
|
templated
|
|
|
|
);
|
2017-03-23 01:58:37 +08:00
|
|
|
}
|
|
|
|
if (this.resourcesUrlTemplates.https) {
|
2018-10-23 23:54:11 +08:00
|
|
|
urls.https = this.urlForTemplate(
|
|
|
|
this.resourcesUrlTemplates.https,
|
|
|
|
username,
|
|
|
|
cdnDomain.https,
|
|
|
|
resource,
|
|
|
|
templated
|
|
|
|
);
|
2017-03-23 01:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
};
|
|
|
|
|
2018-01-03 23:05:19 +08:00
|
|
|
class Resource {
|
|
|
|
constructor (resourcePath) {
|
|
|
|
this.resourcePath = resourcePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPath () {
|
|
|
|
return this.resourcePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDomain (domain, subdomains) {
|
2018-01-04 04:53:03 +08:00
|
|
|
if (!subdomains) {
|
|
|
|
return domain;
|
|
|
|
}
|
2018-01-03 23:05:19 +08:00
|
|
|
return domain.replace('{s}', subdomain(subdomains, this.resourcePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
getUrl (baseUrl, username, subdomains) {
|
|
|
|
let urls = getUrl(baseUrl, username, this.resourcePath);
|
|
|
|
if (subdomains) {
|
|
|
|
urls = urls.replace('{s}', subdomain(subdomains, this.resourcePath));
|
|
|
|
}
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TileResource extends Resource {
|
|
|
|
getDomain (domain, subdomains) {
|
2018-01-04 04:53:03 +08:00
|
|
|
if (!subdomains) {
|
|
|
|
return domain;
|
|
|
|
}
|
2018-01-03 23:05:19 +08:00
|
|
|
return subdomains.map(s => domain.replace('{s}', s));
|
|
|
|
}
|
|
|
|
|
|
|
|
getUrl (baseUrl, username, subdomains) {
|
|
|
|
if (!subdomains) {
|
|
|
|
return [super.getUrl(baseUrl, username)];
|
|
|
|
}
|
|
|
|
return subdomains.map(subdomain => {
|
|
|
|
return getUrl(baseUrl, username, this.resourcePath)
|
|
|
|
.replace('{s}', subdomain);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:53:03 +08:00
|
|
|
class TemplateResource extends Resource {
|
|
|
|
getDomain (domain, subdomains) {
|
|
|
|
return {
|
|
|
|
urlTemplate: domain,
|
|
|
|
subdomains: subdomains || []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getUrl (baseUrl, username, subdomains) {
|
|
|
|
return {
|
|
|
|
urlTemplate: getUrl(baseUrl, username, this.resourcePath),
|
|
|
|
subdomains: subdomains || []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getUrl (baseUrl, username, path) {
|
2018-01-03 23:05:19 +08:00
|
|
|
return `${baseUrl}/${username}/api/v1/map/${path}`;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getCdnUrls (serverMetadata, username, resource) {
|
2018-01-03 23:05:19 +08:00
|
|
|
if (serverMetadata && serverMetadata.cdn_url) {
|
|
|
|
var cdnUrl = serverMetadata.cdn_url;
|
|
|
|
var httpUrls = resource.getUrl(`http://${cdnUrl.http}`, username);
|
|
|
|
var httpsUrls = resource.getUrl(`https://${cdnUrl.https}`, username);
|
|
|
|
if (cdnUrl.templates) {
|
|
|
|
var templates = cdnUrl.templates;
|
|
|
|
httpUrls = resource.getUrl(templates.http.url, username, templates.http.subdomains);
|
|
|
|
httpsUrls = resource.getUrl(templates.https.url, username, templates.https.subdomains);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
http: httpUrls,
|
2019-10-22 01:07:24 +08:00
|
|
|
https: httpsUrls
|
2018-01-03 23:05:19 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getCdnDomain (serverMetadata, resource) {
|
2017-03-23 01:58:37 +08:00
|
|
|
if (serverMetadata && serverMetadata.cdn_url) {
|
|
|
|
var cdnUrl = serverMetadata.cdn_url;
|
2018-01-04 04:53:03 +08:00
|
|
|
var httpDomain = resource.getDomain(cdnUrl.http);
|
|
|
|
var httpsDomain = resource.getDomain(cdnUrl.https);
|
2017-03-23 01:58:37 +08:00
|
|
|
if (cdnUrl.templates) {
|
|
|
|
var templates = cdnUrl.templates;
|
|
|
|
var httpUrlTemplate = templates.http.url;
|
|
|
|
var httpsUrlTemplate = templates.https.url;
|
2018-01-03 23:05:19 +08:00
|
|
|
httpDomain = httpUrlTemplate.replace(/^(http[s]*:\/\/)/, '');
|
|
|
|
httpDomain = resource.getDomain(httpDomain, templates.http.subdomains);
|
|
|
|
httpsDomain = httpsUrlTemplate.replace(/^(http[s]*:\/\/)/, '');
|
|
|
|
httpsDomain = resource.getDomain(httpsDomain, templates.https.subdomains);
|
2017-03-23 01:58:37 +08:00
|
|
|
}
|
|
|
|
return {
|
2018-01-03 23:05:19 +08:00
|
|
|
http: httpDomain,
|
2019-10-22 01:07:24 +08:00
|
|
|
https: httpsDomain
|
2017-03-23 01:58:37 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-03-23 08:03:45 +08:00
|
|
|
// ref https://jsperf.com/js-crc32
|
2019-10-22 01:07:24 +08:00
|
|
|
function crcTable () {
|
2017-03-23 08:03:45 +08:00
|
|
|
var c;
|
|
|
|
var table = [];
|
|
|
|
for (var n = 0; n < 256; n++) {
|
2019-10-22 01:07:24 +08:00
|
|
|
c = n;
|
|
|
|
for (var k = 0; k < 8; k++) {
|
|
|
|
c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
|
|
|
|
}
|
|
|
|
table[n] = c;
|
2017-03-23 08:03:45 +08:00
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
var CRC_TABLE = crcTable();
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function crc32 (str) {
|
2017-03-23 08:03:45 +08:00
|
|
|
var crc = 0 ^ (-1);
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
2019-10-22 01:07:24 +08:00
|
|
|
crc = (crc >>> 8) ^ CRC_TABLE[(crc ^ str.charCodeAt(i)) & 0xFF];
|
2017-03-23 08:03:45 +08:00
|
|
|
}
|
|
|
|
return (crc ^ (-1)) >>> 0;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function subdomain (subdomains, resource) {
|
2017-03-23 08:03:45 +08:00
|
|
|
var index = crc32(resource) % subdomains.length;
|
2017-03-23 01:58:37 +08:00
|
|
|
return subdomains[index];
|
|
|
|
}
|
2017-03-23 08:03:45 +08:00
|
|
|
module.exports.subdomain = subdomain;
|