@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
104 lines (103 loc) • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
var tslib_1 = require("tslib");
var common_1 = require("./common");
var CACHE = { 'Cache-Control': "s-maxage=5, stale-while-revalidate" };
function init(args) {
var _this = this;
var router = args.router, getManifest = args.getManifest;
router.get('/.manifest/summary', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var manifest, sites;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, getManifest()];
case 1:
manifest = _a.sent();
sites = manifest.sites.reduce(function (acc, site) {
var _a;
var info = toSiteInfo({ site: site });
return tslib_1.__assign(tslib_1.__assign({}, acc), (_a = {}, _a[site.name] = info, _a));
}, {});
return [2, {
status: 200,
headers: CACHE,
data: { sites: sites },
}];
}
});
}); });
router.get('/.manifest/summary/:site', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var manifest, name, site, status_1, message;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, getManifest()];
case 1:
manifest = _a.sent();
name = (req.params.site || '').toString();
site = manifest.site.byName(name);
if (site) {
return [2, {
status: 200,
headers: CACHE,
data: toSiteInfo({ site: site, name: true, files: true }),
}];
}
else {
status_1 = 404;
message = "Site named '" + (req.params.site || 'UNNAMED') + "' not found in manifest.";
return [2, {
status: status_1,
data: { status: status_1, message: message },
}];
}
return [2];
}
});
}); });
router.get('/.manifest', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var manifest;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, getManifest({ force: true })];
case 1:
manifest = _a.sent();
return [2, {
status: 200,
headers: CACHE,
data: manifest.toObject(),
}];
}
});
}); });
}
exports.init = init;
function toSiteInfo(args) {
var site = args.site;
var name = site.name, version = site.version, size = site.size;
var domain = site.domain.join(', ');
var info = { version: version, size: size, domain: domain };
if (args.name) {
info = tslib_1.__assign({ name: name }, info);
}
if (args.files) {
var files = site.files.map(function (file) {
var path = file.path, bytes = file.bytes;
var size = common_1.fs.size.toString(bytes);
return { path: path, size: size };
});
var groups_1 = [
{ ext: ['js'], group: 'javascript' },
{ ext: ['html', 'htm'], group: 'html' },
{ ext: ['css'], group: 'css' },
{ ext: ['png', 'jpeg', 'jpg', 'gif', 'ico'], group: 'images' },
];
var findGroup_1 = function (path) {
var ext = common_1.fs.extname(path).replace(/^\./, '');
var item = groups_1.find(function (item) { return item.ext.includes(ext); });
return item ? item.group : 'asset';
};
info = tslib_1.__assign(tslib_1.__assign({}, info), { files: common_1.R.groupBy(function (file) { return findGroup_1(file.path); }, files) });
}
return info;
}