UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

239 lines (238 loc) 8.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Urls = void 0; var common_1 = require("../common"); var ROUTES_1 = require("../ROUTES"); var Uri_1 = require("../Uri"); var Url_1 = require("./Url"); var util = require("./util"); var Urls = (function () { function Urls(input) { var _this = this; this.toUrl = function (path, options) { if (options === void 0) { options = {}; } var query = options.query; var origin = _this.origin; return new Url_1.Url({ origin: origin, path: path, query: query }); }; var origin = Url_1.Url.parse(input).origin; this.hostname = origin.hostname; this.host = origin.host; this.protocol = origin.protocol; this.port = origin.port; this.origin = origin.toString(); } Urls.create = function (input) { return new Urls(input); }; Object.defineProperty(Urls.prototype, "sys", { get: function () { var toUrl = this.toUrl; return { get info() { return toUrl('.sys'); }, get uid() { return toUrl('.uid'); }, }; }, enumerable: false, configurable: true }); Object.defineProperty(Urls.prototype, "local", { get: function () { var toUrl = this.toUrl; return { get fs() { return toUrl("/local/fs"); }, }; }, enumerable: false, configurable: true }); Object.defineProperty(Urls.prototype, "fn", { get: function () { var self = this; var toUrl = this.toUrl; var throwOnHostMistmatch = function (bundle) { if (util.stripHttp(bundle.host) !== self.host) { throw new Error("Host mismatch ('".concat(bundle.host, "' should be '").concat(self.host, "')")); } }; var trimDir = function (dir) { return (dir || '').trim().replace(/^\/*/, '').replace(/\/*$/, ''); }; var bundle = { manifest: function (bundle) { throwOnHostMistmatch(bundle); var FILENAME = common_1.constants.BUNDLE.MANIFEST.FILENAME; var dir = trimDir(bundle.dir); var filename = dir ? "".concat(dir, "/").concat(FILENAME) : FILENAME; return self.cell(bundle.uri).file.byName(filename); }, files: function (bundle) { throwOnHostMistmatch(bundle); var dir = trimDir(bundle.dir); var url = self.cell(bundle.uri).files.list; url = dir ? url.query({ filter: "".concat(dir, "/**") }) : url; return url; }, }; return { bundle: bundle, get run() { return toUrl("/fn:run"); }, }; }, enumerable: false, configurable: true }); Urls.prototype.ns = function (input) { var toUrl = this.toUrl; var id = (typeof input === 'string' ? input : input.id) || ''; if (!id.includes(':')) { id = "ns:".concat(id); } var uri = Uri_1.Uri.parse(id); var type = uri.parts.type; if (uri.error) { throw new Error(uri.error.message); } if (type === 'NS') { id = uri.parts.id; } else if (type === 'CELL') { id = uri.parts.ns; } else { var err = "The id for the namespace is a URI, but not of type \"ns:\" or \"cell:\" (\"".concat(id, "\")"); throw new Error(err); } return { uri: uri.toString(), get info() { return toUrl("/ns:".concat(id)); }, }; }; Urls.prototype.cell = function (input) { var toUrl = this.toUrl; var uri = Uri_1.Uri.cell(input); var ns = uri.ns, key = uri.key, type = uri.type; if (type !== 'CELL') { var err = "The given URI is a ".concat(type, " not a CELL (\"").concat(input.toString(), "\")"); throw new Error(err); } var api = { uri: uri.toString(), get info() { return toUrl("/cell:".concat(ns, ":").concat(key)); }, files: { get list() { return toUrl("/cell:".concat(ns, ":").concat(key, "/fs")); }, get delete() { return toUrl("/cell:".concat(ns, ":").concat(key, "/fs")); }, get copy() { return toUrl("/cell:".concat(ns, ":").concat(key, "/fs:copy")); }, get upload() { return toUrl("/cell:".concat(ns, ":").concat(key, "/fs:upload")); }, get uploaded() { return toUrl("/cell:".concat(ns, ":").concat(key, "/fs:uploaded")); }, }, file: { toString: function () { return "/cell:".concat(ns, ":").concat(key, "/fs/"); }, byName: function (filename) { filename = (filename || '').trim(); if (!filename) { throw new Error("Filename not provided."); } return toUrl("/cell:".concat(ns, ":").concat(key, "/fs/").concat(filename)); }, byFileUri: function (fileUri, fileExtension) { fileExtension = (fileExtension || '').trim(); var uri = Uri_1.Uri.file(fileUri); if (uri.type !== 'FILE') { throw new Error("The given URI [".concat(fileUri, "] is not of type [file:]")); } var ext = (fileExtension || '').trim().replace(/^\.*/, ''); var filename = "".concat(uri.file).concat(ext ? ".".concat(ext) : '').trim(); if (!filename) { throw new Error("File uri/name could not be derived.."); } var file = "file:".concat(filename); return toUrl("/cell:".concat(ns, ":").concat(key, "/").concat(file)); }, }, }; return api; }; Urls.prototype.row = function (input) { var toUrl = this.toUrl; var uri = Uri_1.Uri.row(input); var ns = uri.ns, key = uri.key, type = uri.type; if (type !== 'ROW') { var err = "The given URI is a ".concat(type, " not a ROW (\"").concat(input.toString(), "\")"); throw new Error(err); } return { uri: uri.toString(), get info() { return toUrl("/cell:".concat(ns, ":").concat(key)); }, }; }; Urls.prototype.column = function (input) { var toUrl = this.toUrl; var uri = Uri_1.Uri.column(input); var ns = uri.ns, key = uri.key, type = uri.type; if (type !== 'COLUMN') { var err = "The given URI is a ".concat(type, " not a COLUMN (\"").concat(input.toString(), "\")"); throw new Error(err); } return { uri: uri.toString(), get info() { return toUrl("/cell:".concat(ns, ":").concat(key)); }, }; }; Urls.prototype.file = function (input) { var toUrl = this.toUrl; var uri = Uri_1.Uri.file(input); if (uri.type !== 'FILE') { var err = "The given URI is not of type \"file:\" (\"".concat(input.toString(), "\")"); throw new Error(err); } var id = uri.id; return { uri: uri.toString(), get info() { return toUrl("/file:".concat(id, "/info")); }, get download() { return toUrl("/file:".concat(id)); }, get delete() { return toUrl("/file:".concat(id)); }, get uploaded() { return toUrl("/file:".concat(id, "/uploaded")); }, }; }; Urls.routes = ROUTES_1.ROUTES; Urls.Uri = Uri_1.Uri; Urls.Url = Url_1.Url; Urls.parse = Url_1.Url.parse; return Urls; }()); exports.Urls = Urls;