@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
50 lines (49 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManifestSource = void 0;
function ManifestSource(input) {
var throwError = function (detail) {
throw new Error("Invalid manifest source '".concat(input, "' - ").concat(detail));
};
if (typeof input !== 'string')
throwError('not a string');
var path = (input || '').trim();
if (!path)
throwError('empty');
var isHttp = path.startsWith('http://') || path.startsWith('https://');
var kind = (isHttp ? 'url' : 'filepath');
if (!path.endsWith('.json'))
throwError('not a path to a ".json" file');
if (kind === 'filepath') {
if (!path.startsWith('/'))
throwError('filepath must start with "/"');
}
if (kind === 'url') {
var pathname = new URL(path).pathname;
if (!pathname.startsWith('/cell:'))
throwError('not a [cell] uri');
if (pathname.indexOf('/fs/') < 0)
throwError('not a cell filesystem path');
}
return {
path: path,
kind: kind,
toString: function () { return path; },
get domain() {
return kind === 'filepath' ? 'runtime:electron:bundle' : new URL(path).host;
},
get dir() {
if (kind === 'filepath') {
return path.substring(0, path.lastIndexOf('/'));
}
if (kind === 'url') {
var pathname = new URL(path).pathname;
var start = pathname.indexOf('/fs/') + 3;
var end = pathname.lastIndexOf('/');
return start < 0 ? '' : pathname.substring(start, end);
}
throw new Error("Kind '".concat(kind, "' not supported"));
},
};
}
exports.ManifestSource = ManifestSource;