@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
259 lines (258 loc) • 11.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Manifest = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var Site_1 = require("./Site");
var URL_CACHE = {};
var Manifest = (function () {
function Manifest(args) {
this.def = args.def;
this.baseUrl = common_1.util.stripSlashes(args.baseUrl);
this.status = args.status || 200;
}
Manifest.reset = function () {
URL_CACHE = {};
};
Manifest.fromFile = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var baseUrl, loadBundleManifest, path, yaml, def;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
baseUrl = args.baseUrl, loadBundleManifest = args.loadBundleManifest;
path = common_1.fs.resolve(args.path);
return [4, common_1.fs.pathExists(path)];
case 1:
if (!(_a.sent())) {
throw new Error("Manifest file does not exist: '" + args.path + "'");
}
return [4, common_1.fs.readFile(path, 'utf-8')];
case 2:
yaml = _a.sent();
return [4, Manifest.parse({ yaml: yaml, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
case 3:
def = _a.sent();
return [2, Manifest.create({ def: def, baseUrl: baseUrl })];
}
});
});
};
Manifest.fromUrl = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var manifestUrl, loadBundleManifest, errorResponse, res, error, baseUrl, manifest, error_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
manifestUrl = args.manifestUrl, loadBundleManifest = args.loadBundleManifest;
errorResponse = function (status, error) {
return { ok: false, status: status, error: new Error(error) };
};
_a.label = 1;
case 1:
_a.trys.push([1, 4, , 5]);
return [4, common_1.http.get(args.manifestUrl)];
case 2:
res = _a.sent();
if (!res.ok) {
error = res.status === 403
? "The manifest YAML has not been made \"public\" on the internet."
: "Failed while pulling manifest YAML from cloud.";
return [2, errorResponse(403, error)];
}
baseUrl = args.baseUrl || manifestUrl;
return [4, Manifest.fromYaml({ yaml: res.text, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
case 3:
manifest = _a.sent();
return [2, {
ok: true,
status: 200,
manifest: manifest,
}];
case 4:
error_1 = _a.sent();
return [2, errorResponse(500, error_1)];
case 5: return [2];
}
});
});
};
Manifest.fromYaml = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var yaml, baseUrl, loadBundleManifest, def;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
yaml = args.yaml, baseUrl = args.baseUrl, loadBundleManifest = args.loadBundleManifest;
return [4, Manifest.parse({ yaml: yaml, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
case 1:
def = _a.sent();
return [2, Manifest.create({ def: def, baseUrl: baseUrl })];
}
});
});
};
Manifest.parse = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var loadBundleManifest, baseUrl, yaml, error, sites, input, manifest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
loadBundleManifest = args.loadBundleManifest;
baseUrl = args.baseUrl.replace(/\/manifest.yml$/, '');
yaml = common_1.util.parseYaml(args.yaml);
if (!yaml.ok || !yaml.data) {
error = "Failed to parse manifest YAML. " + yaml.error.message;
throw new Error(error);
}
sites = [];
input = yaml.data.sites;
return [4, Site_1.Site.formatMany({ input: input, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
case 1:
sites = _a.sent();
manifest = { sites: sites };
return [2, manifest];
}
});
});
};
Manifest.get = function (args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var ttl, key, cached, res, manifest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
ttl = args.ttl;
key = args.manifestUrl + ":" + (args.loadBundleManifest || 'false');
cached = URL_CACHE[key];
if (!args.force && cached && !isCacheExpired({ key: key, ttl: ttl })) {
return [2, cached.manifest];
}
return [4, Manifest.fromUrl(args)];
case 1:
res = _a.sent();
if (res.manifest) {
manifest = res.manifest;
cached = { manifest: manifest, time: common_1.time.now.timestamp };
URL_CACHE[key] = cached;
}
return [2, URL_CACHE[key].manifest];
}
});
});
};
Object.defineProperty(Manifest.prototype, "ok", {
get: function () {
return this.status.toString().startsWith('2');
},
enumerable: false,
configurable: true
});
Object.defineProperty(Manifest.prototype, "sites", {
get: function () {
if (!this._sites) {
var manifest_1 = this.def;
this._sites = this.def.sites.map(function (def, index) { return Site_1.Site.create({ index: index, manifest: manifest_1 }); });
}
return this._sites;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Manifest.prototype, "site", {
get: function () {
var _this = this;
return {
byName: function (name) {
name = (name || '').trim();
return _this.sites.find(function (site) { return site.name.trim() === name; });
},
byHost: function (domain) {
domain = common_1.util.stripHttp(domain || '');
return _this.sites.find(function (site) { return site.isMatch(domain || ''); });
},
};
},
enumerable: false,
configurable: true
});
Object.defineProperty(Manifest.prototype, "change", {
get: function () {
var _this = this;
return {
site: function (id) {
var name = typeof id === 'string' ? id : id.name;
return {
bundle: function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var site, bundle, def, manifest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
site = this.site.byName(name);
if (!site) {
return [2, undefined];
}
bundle = args.value;
def = tslib_1.__assign({}, this.def);
def.sites[site.index].bundle = bundle;
manifest = Manifest.create({ def: def, baseUrl: this.baseUrl });
if (!args.saveTo) return [3, 2];
return [4, manifest.save(args.saveTo)];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2, manifest];
}
});
}); },
};
},
};
},
enumerable: false,
configurable: true
});
Manifest.prototype.toObject = function () {
return tslib_1.__assign({ status: this.status }, this.def);
};
Manifest.prototype.save = function (path, options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var def, fields;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
def = tslib_1.__assign({}, this.def);
fields = ['files', 'entries', 'baseUrl', 'size', 'bytes'];
if (common_1.defaultValue(options.minimal, true)) {
def.sites.forEach(function (site) {
fields.forEach(function (field) {
delete site[field];
});
});
}
path = common_1.fs.resolve(path);
return [4, common_1.fs.ensureDir(common_1.fs.dirname(path))];
case 1:
_a.sent();
return [4, common_1.fs.file.stringifyAndSave(path, def)];
case 2:
_a.sent();
return [2];
}
});
});
};
Manifest.create = function (args) { return new Manifest(args); };
return Manifest;
}());
exports.Manifest = Manifest;
function isCacheExpired(args) {
var key = args.key, ttl = args.ttl;
var cached = URL_CACHE[key];
if (!cached || typeof ttl !== 'number') {
return false;
}
var age = common_1.time.now.timestamp - cached.time;
return age > ttl;
}