@platform/react.ssr
Version:
An SSR (server-side-render) system for react apps bundled with ParcelJS and hosted on S3.
248 lines (247 loc) • 10.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 = 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, path, text, def;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
baseUrl = args.baseUrl;
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:
text = _a.sent();
return [4, Manifest.parse({ yaml: text, baseUrl: baseUrl })];
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.body, 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 text, baseUrl, loadBundleManifest, def;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
text = args.yaml, baseUrl = args.baseUrl, loadBundleManifest = args.loadBundleManifest;
return [4, Manifest.parse({ yaml: text, 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 key, manifest, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
key = args.manifestUrl + "::" + (args.loadBundleManifest || 'false');
manifest = URL_CACHE[key];
if (manifest && !args.force) {
return [2, manifest];
}
return [4, Manifest.fromUrl(args)];
case 1:
res = _a.sent();
if (res.manifest) {
manifest = res.manifest;
URL_CACHE[key] = manifest;
}
return [2, manifest];
}
});
});
};
Object.defineProperty(Manifest.prototype, "ok", {
get: function () {
return this.status.toString().startsWith('2');
},
enumerable: true,
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: true,
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: true,
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: true,
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;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
def = tslib_1.__assign({}, this.def);
if (options.minimal) {
def.sites.forEach(function (site) {
delete site.files;
delete site.entries;
delete site.baseUrl;
delete site.size;
delete site.bytes;
});
}
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;