UNPKG

ts-comply

Version:

TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.

318 lines 19.5 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.push(_); } else if (_ = accept(result)) { if (kind === "field") initializers.push(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var _this = this; Object.defineProperty(exports, "__esModule", { value: true }); exports.DestorServer = exports.DestorCode = void 0; var globals_ix_1 = require("../../../src/common/globals.ix"); var http_shim_1 = require("../http.shim"); var fs = __importStar(require("fs")); var env_profile_1 = require("../../../src/common/env/env.profile"); var type_transform_1 = require("../../../src/type-transform"); var properties_parse_1 = require("../../../src/common/util/convenience/properties.parse"); var node_util_1 = require("../../util/node-util"); var security_common_1 = require("../../../src/common/security/security.common"); var scopeName = "destor;pid=".concat(process.pid); var DestorCodeEnum; (function (DestorCodeEnum) { DestorCodeEnum[DestorCodeEnum["DESTOR_RESOLVE_CONFIG_APP_PROFILE_NOT_FOUND"] = 0] = "DESTOR_RESOLVE_CONFIG_APP_PROFILE_NOT_FOUND"; DestorCodeEnum[DestorCodeEnum["DESTOR_AUTH_SERVER_APP_PROFILE_NOT_FOUND"] = 1] = "DESTOR_AUTH_SERVER_APP_PROFILE_NOT_FOUND"; DestorCodeEnum[DestorCodeEnum["DESTOR_SECRET_NOT_FOUND"] = 2] = "DESTOR_SECRET_NOT_FOUND"; })(DestorCodeEnum || (DestorCodeEnum = {})); exports.DestorCode = (0, globals_ix_1.ReturnCodeFamily)('DestorCode', DestorCodeEnum); var localData; var destorData; function getConf() { localData = (0, properties_parse_1.parseProperties)(fs.readFileSync(process.env.DESTOR_SERVER_CONFIG_FILE, 'utf8')); destorData = localData.destor; return localData; } var DestorServer = exports.DestorServer = function () { var _a; var _instanceExtraInitializers = []; var _rootProofOfAuthenticity_decorators; var _getResolveConfig_decorators; var _getAuthServer_decorators; return _a = (function (_super) { __extends(DestorServer, _super); function DestorServer() { var _this = _super.call(this, { name: 'destor', type: http_shim_1.HttpBaseLib.EXPRESS, scopeName: scopeName, security: { accessor: { required: true, baseToken: process.env.DESTOR_SERVER_BASE_TOKEN }, secureChannel: { enabled: true, required: true, encryption: security_common_1.SecureChannelTypes.ECC_4Q, signingKey: process.env.DESTOR_SERVER_SIGNING_KEY }, }, startOptions: { port: process.env.DESTOR_SERVER_PORT ? parseInt(process.env.DESTOR_SERVER_PORT, 10) : 17070 }, skipConfigSecretResolution: true, }) || this; _this.dataResolution = (__runInitializers(_this, _instanceExtraInitializers), (0, env_profile_1.envVar)('DESTOR_SERVER_DATA_RESOLUTION', 'local-file')); _this.getResolvConfig_iface = { path: "/resolve-config/:env", description: "", params: { env: { required: type_transform_1.required, type: "string", default: (0, type_transform_1.as)(), description: "" }, targets: { required: type_transform_1.required, type: "string", default: (0, type_transform_1.as)(), description: "" }, }, returns: { type: "{[key: string]: {value: any, error?: string}}", default: (0, type_transform_1.as)() }, }; _this.getAuthServer_iface = { path: "/auth-servers/:env", description: "", params: { env: { required: type_transform_1.required, type: "string", default: (0, type_transform_1.as)(), description: "" }, }, returns: { type: "{[name: string]: { url: string, type: string, publicKey: string }}", default: (0, type_transform_1.as)() }, }; _this.apiVersion = 'v1'; _this.apiPath = _this.configGlobal.destor.basePath; _this.addDefaultProcessor(http_shim_1.ReqProcessor.BASIC); getConf(); if (_this.dataResolution === 'local-file') { setInterval(function () { try { getConf(); } catch (e) { console.error(e); } }, 30000); } return _this; } DestorServer.prototype.rootProofOfAuthenticity = function (op) { return __awaiter(this, void 0, void 0, function () { var accessorResult, stamp; return __generator(this, function (_a) { switch (_a.label) { case 0: accessorResult = this.checkAccessor(op); if (accessorResult.bad) { return [2, (0, globals_ix_1.passthruError)(accessorResult)]; } return [4, this.stamp()]; case 1: stamp = _a.sent(); return [2, op.res.returnJson(__assign({}, stamp))]; } }); }); }; DestorServer.prototype.getResolveConfig = function (op) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var targets, result, _i, targets_1, targetStr, lit, targetPathStr, defaultValueDef, defaultValue, dfLit, dfType, dfValueStr, targetPath, resolved, resObj; return __generator(this, function (_e) { targets = op.params.targets.split('|').map(function (target) { target = target.trim(); while (target.startsWith('<') && target.endsWith('>')) { target = target.slice(1, -1); } return target; }); if (this.dataResolution === 'local-file') { if (!((_a = localData === null || localData === void 0 ? void 0 : localData.profiles) === null || _a === void 0 ? void 0 : _a[op.params.env])) { return [2, op.raise(exports.DestorCode.error('DESTOR_RESOLVE_CONFIG_APP_PROFILE_NOT_FOUND', "application profile env '".concat(op.params.env, "' not found on destor config")))]; } result = {}; for (_i = 0, targets_1 = targets; _i < targets_1.length; _i++) { targetStr = targets_1[_i]; lit = targetStr.split('?:'); targetPathStr = (_b = lit[0]) === null || _b === void 0 ? void 0 : _b.trim(); defaultValueDef = (_c = lit[1]) === null || _c === void 0 ? void 0 : _c.trimStart(); defaultValue = void 0; if (defaultValueDef) { if (defaultValueDef.indexOf(':') >= 0) { dfLit = defaultValueDef.split(':'); dfType = dfLit[0]; dfValueStr = dfLit.slice(1).join(':'); if (dfType === 'number') { defaultValue = +dfValueStr; } else if (dfType === 'boolean') { defaultValue = dfValueStr.toLowerCase() === 'true' ? true : false; } else if (dfType === 'string') { defaultValue = dfValueStr; } } else { defaultValue = defaultValueDef; } } targetPath = targetPathStr.split('.'); if (targetPath[0] !== 'config' && targetPath[0] !== 'secret') { result[targetStr] = { value: null, error: "resolvable path must start with 'config' or 'secret', got '".concat(targetPath[0], "'") }; continue; } resolved = (0, node_util_1.pathNavigate)(targetPath, (_d = localData === null || localData === void 0 ? void 0 : localData.profiles) === null || _d === void 0 ? void 0 : _d[op.params.env]); if (resolved === null) { resolved = (0, node_util_1.pathNavigate)(targetPath, localData === null || localData === void 0 ? void 0 : localData.global); } resObj = { value: resolved }; if (resolved === null) { if (defaultValueDef) { resObj.value = defaultValue; } else { resObj.error = "not_found"; } } result[targetStr] = resObj; } op.returnJson(result); } return [2]; }); }); }; DestorServer.prototype.getAuthServer = function (op) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var data; return __generator(this, function (_c) { if (this.dataResolution === 'local-file') { if (!((_a = localData === null || localData === void 0 ? void 0 : localData.profiles) === null || _a === void 0 ? void 0 : _a[op.params.env])) { return [2, op.raise(exports.DestorCode.error('DESTOR_AUTH_SERVER_APP_PROFILE_NOT_FOUND', "application profile env '".concat(op.params.env, "' not found on destor config")))]; } data = (0, node_util_1.pathNavigate)(['config', 'authServers'], (_b = localData === null || localData === void 0 ? void 0 : localData.profiles) === null || _b === void 0 ? void 0 : _b[op.params.env]); if (data === null) { data = (0, node_util_1.pathNavigate)(['config', 'authServers'], localData === null || localData === void 0 ? void 0 : localData.global); } return [2, op.returnJson(data)]; } return [2]; }); }); }; return DestorServer; }(http_shim_1.HttpServerShim)), (function () { _rootProofOfAuthenticity_decorators = [http_shim_1.HTTP.GET("/", { rootMount: true })]; _getResolveConfig_decorators = [http_shim_1.HTTP.GET("/resolve-config/:env", { rootMount: true })]; _getAuthServer_decorators = [http_shim_1.HTTP.GET("/auth-servers/:env", { rootMount: true })]; __esDecorate(_a, null, _rootProofOfAuthenticity_decorators, { kind: "method", name: "rootProofOfAuthenticity", static: false, private: false, access: { has: function (obj) { return "rootProofOfAuthenticity" in obj; }, get: function (obj) { return obj.rootProofOfAuthenticity; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getResolveConfig_decorators, { kind: "method", name: "getResolveConfig", static: false, private: false, access: { has: function (obj) { return "getResolveConfig" in obj; }, get: function (obj) { return obj.getResolveConfig; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _getAuthServer_decorators, { kind: "method", name: "getAuthServer", static: false, private: false, access: { has: function (obj) { return "getAuthServer" in obj; }, get: function (obj) { return obj.getAuthServer; } } }, null, _instanceExtraInitializers); })(), _a; }(); //# sourceMappingURL=destor.server.js.map