@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
225 lines (224 loc) • 9.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("./common");
var manifest_1 = require("./manifest");
var stripSlashes = common_1.util.stripSlashes;
var DEFAULT = {
FILE: 'ssr.yml',
};
var ERROR = {
noFile: function (path) { return "An \"" + DEFAULT.FILE + "\" configuration file does not exist at: " + path; },
};
var Config = (function () {
function Config(args) {
this.def = args.def;
}
Object.defineProperty(Config.prototype, "secret", {
get: function () {
return toValue(this.def.secret);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Config.prototype, "builder", {
get: function () {
var builder = this.def.builder || {};
builder.bundles = builder.bundles || 'bundles';
builder.entries = builder.entries || '';
return builder;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Config.prototype, "s3", {
get: function () {
var s3 = this.def.s3 || {};
var path = s3.path || {};
var endpoint = common_1.util.stripHttp(s3.endpoint || '');
var cdn = common_1.util.stripHttp(s3.cdn || '');
var accessKey = toValue(s3.accessKey);
var secret = toValue(s3.secret);
var bucket = s3.bucket || '';
var api = {
endpoint: endpoint,
cdn: cdn,
accessKey: accessKey,
secret: secret,
bucket: bucket,
path: {
base: stripSlashes(path.base || ''),
manifest: stripSlashes(path.manifest || ''),
bundles: stripSlashes(path.bundles || ''),
},
get config() {
return { endpoint: endpoint, accessKey: accessKey, secret: secret };
},
get fs() {
return common_1.fs.s3(api.config);
},
versions: function (options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var s3, prefix, list, dirs, versions;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
s3 = api.fs;
prefix = api.path.base + "/" + api.path.bundles;
list = s3.list({
bucket: bucket,
prefix: prefix,
});
return [4, list.dirs];
case 1:
dirs = (_a.sent()).items.map(function (_a) {
var key = _a.key;
return ({ key: key, version: common_1.fs.basename(key) });
});
versions = common_1.semver.sort(dirs.map(function (item) { return item.version; }));
return [2, options.sort === 'DESC' ? versions.reverse() : versions];
}
});
});
},
};
return api;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Config.prototype, "baseUrl", {
get: function () {
var s3 = this.s3;
return "https://" + (s3.cdn || s3.endpoint) + "/" + s3.path.base;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Config.prototype, "manifest", {
get: function () {
var filePath = common_1.fs.resolve(this.def.manifest || 'manifest.yml');
var s3 = this.s3;
var manifestUrl = "https://" + s3.endpoint + "/" + s3.bucket + "/" + s3.path.base + "/" + s3.path.manifest;
var baseUrl = this.baseUrl;
var config = this;
var api = {
local: {
path: filePath,
get exists() {
return common_1.fs.pathExists(filePath);
},
load: function (args) {
if (args === void 0) { args = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var loadBundleManifest;
return tslib_1.__generator(this, function (_a) {
loadBundleManifest = args.loadBundleManifest;
return [2, manifest_1.Manifest.fromFile({ path: filePath, baseUrl: baseUrl, loadBundleManifest: loadBundleManifest })];
});
});
},
ensureLatest: function (args) {
if (args === void 0) { args = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var remote, minimal;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, api.local.exists];
case 1:
if (!!(_a.sent())) return [3, 3];
return [4, config.createFromTemplate()];
case 2:
_a.sent();
_a.label = 3;
case 3: return [4, api.s3.pull({ force: true })];
case 4:
remote = _a.sent();
if (!remote.ok) return [3, 6];
minimal = common_1.defaultValue(args.minimal, true);
return [4, remote.save(filePath, { minimal: minimal })];
case 5:
_a.sent();
_a.label = 6;
case 6: return [2, api.local.load()];
}
});
});
},
},
s3: {
url: manifestUrl,
pull: function (args) {
if (args === void 0) { args = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2, manifest_1.Manifest.get(tslib_1.__assign(tslib_1.__assign({}, args), { manifestUrl: manifestUrl, baseUrl: baseUrl }))];
});
});
},
},
};
return api;
},
enumerable: true,
configurable: true
});
Config.prototype.createFromTemplate = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var source, target;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
source = common_1.fs.join(__dirname, '../tmpl/manifest.yml');
target = this.manifest.local.path;
return [4, common_1.fs.ensureDir(common_1.fs.dirname(target))];
case 1:
_a.sent();
return [4, common_1.fs.copy(source, target)];
case 2:
_a.sent();
return [2, { source: source, target: target }];
}
});
});
};
Config.create = function (options) {
if (options === void 0) { options = {}; }
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var path, def;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
path = common_1.fs.resolve(options.path || DEFAULT.FILE);
return [4, common_1.fs.pathExists(path)];
case 1:
if (!!(_a.sent())) return [3, 2];
throw new Error(ERROR.noFile(path));
case 2: return [4, common_1.fs.file.loadAndParse(path)];
case 3:
def = _a.sent();
return [2, new Config({ def: def })];
}
});
});
};
Config.createSync = function (options) {
if (options === void 0) { options = {}; }
var path = common_1.fs.resolve(options.path || DEFAULT.FILE);
if (!common_1.fs.pathExistsSync(path)) {
throw new Error("An \"ssr.yml\" configuration file does not exist at: " + path);
}
else {
var def = common_1.fs.file.loadAndParseSync(path);
return new Config({ def: def });
}
};
return Config;
}());
exports.Config = Config;
var toValue = function (value) {
value = value || '';
value = process.env[value] ? process.env[value] : value;
return value || '';
};