@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
206 lines (205 loc) • 8.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var Route_1 = require("./Route");
var Site = (function () {
function Site(args) {
var index = args.index, manifest = args.manifest;
this.index = index;
this.manifest = manifest;
this._regexes = toDomainRegexes(this.def.domain);
if (!this.name) {
throw new Error("A site definition must have a name.");
}
}
Site.formatMany = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var baseUrl, loadBundleManifest, error, sites, _i, _a, input, site;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
baseUrl = args.baseUrl, loadBundleManifest = args.loadBundleManifest;
if (!Array.isArray(args.input)) {
error = "The manifest YAML \"sites\" field is not an array.";
throw new Error(error);
}
sites = [];
_i = 0, _a = args.input;
_b.label = 1;
case 1:
if (!(_i < _a.length)) return [3, 4];
input = _a[_i];
return [4, Site.formatOne({ input: input, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
case 2:
site = _b.sent();
sites = site ? tslib_1.__spreadArrays(sites, [site]) : sites;
_b.label = 3;
case 3:
_i++;
return [3, 1];
case 4: return [2, sites];
}
});
});
};
Site.formatOne = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var input, baseUrl, name, domain, bundle, routes, files, entries, size, bytes, bundleUrl, res, bundleManifest, site;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
input = args.input, baseUrl = args.baseUrl;
if (typeof input !== 'object') {
return [2];
}
name = (input.name || '').trim();
domain = input.domain || '';
domain = Array.isArray(domain) ? domain : [domain];
domain = domain.map(function (hostname) { return common_1.util.stripHttp(hostname); });
bundle = common_1.util.asString(input.bundle).replace(/\/*$/, '');
routes = typeof input.routes === 'object' ? input.routes : {};
routes = Object.keys(routes).reduce(function (acc, next) {
var input = routes[next];
if (input) {
var route = Route_1.Route.format({ input: input });
if (route) {
acc[next] = route;
}
}
return acc;
}, {});
files = [];
entries = [];
size = '-';
bytes = -1;
if (!args.loadBundleManifest) return [3, 2];
bundleUrl = baseUrl + "/" + bundle + "/" + common_1.constants.PATH.BUNDLE_MANIFEST;
return [4, common_1.http.get(bundleUrl)];
case 1:
res = _a.sent();
bundleManifest = res.ok ? common_1.jsYaml.safeLoad(res.text) : undefined;
if (bundleManifest) {
size = bundleManifest.size;
bytes = bundleManifest.bytes;
files = bundleManifest.files || [];
entries = bundleManifest.entries || [];
}
_a.label = 2;
case 2:
site = {
name: name,
domain: domain,
baseUrl: baseUrl,
bundle: bundle,
routes: routes,
size: size,
bytes: bytes,
files: files,
entries: entries,
};
return [2, site];
}
});
});
};
Object.defineProperty(Site.prototype, "def", {
get: function () {
return this.manifest.sites[this.index];
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "name", {
get: function () {
return (this.def.name || '').trim();
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "domain", {
get: function () {
return this.def.domain;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "bundle", {
get: function () {
return this.def.bundle;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "bundleUrl", {
get: function () {
var base = common_1.util.stripSlashes(this.def.baseUrl);
var path = common_1.util.stripSlashes(this.bundle);
return base + "/" + path;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "version", {
get: function () {
return common_1.util.firstSemver(this.bundle) || '0.0.0';
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "size", {
get: function () {
return this.def.size;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "files", {
get: function () {
return this.def.files || [];
},
enumerable: true,
configurable: true
});
Object.defineProperty(Site.prototype, "routes", {
get: function () {
if (!this._routes) {
var site_1 = this.def;
var routes = Object.keys(this.def.routes).map(function (key) { return site_1.routes[key]; });
this._routes = routes.map(function (route) { return Route_1.Route.create({ site: site_1, route: route }); });
}
return this._routes;
},
enumerable: true,
configurable: true
});
Site.prototype.isMatch = function (domain) {
var _this = this;
var isMatch = function (domain, regex) {
var res = regex.exec(domain);
return Array.isArray(res) && res[0] === domain;
};
var domains = Array.isArray(domain) ? domain : [domain];
var regexes = this._regexes;
return domains.some(function (d) { return _this.domain.includes(d) || regexes.some(function (r) { return isMatch(d, r); }); });
};
Site.prototype.route = function (pathname) {
return pathname ? this.routes.find(function (route) { return route.isMatch(pathname); }) : undefined;
};
Site.prototype.redirectUrl = function (pathname) {
var path = common_1.util.stripSlashes(pathname);
var file = this.files.find(function (file) { return path.endsWith(file.path); });
return file ? this.bundleUrl + "/" + file.path : '';
};
Site.prototype.toObject = function () {
return tslib_1.__assign({}, this.def);
};
Site.create = function (args) { return new Site(args); };
return Site;
}());
exports.Site = Site;
function toDomainRegexes(domains) {
var toRegex = function (domain) { return new RegExp(common_1.util.stripSlashes(domain)); };
return domains.filter(function (domain) { return common_1.util.isDomainRegex(domain); }).map(function (domain) { return toRegex(domain); });
}
exports.toDomainRegexes = toDomainRegexes;