UNPKG

@noggin/elastic-noggin-sdk

Version:
331 lines (330 loc) 10.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnoFactory = void 0; const Eno_1 = require("./models/Eno"); const sidStringify_1 = require("./sidStringify"); const lodash_1 = require("lodash"); const constants_1 = __importDefault(require("./constants")); const sha_js_1 = __importDefault(require("sha.js")); const EMPTY_NONCE_TYPES = [ "var", "query/dimension", "op/query", "op/query/dimension", "op/pull", "op/merge", "op/formula", "op/session", "op/watch/register", "op/watch/unregister", "op/auth/gen-token", "op/auth/reset-token", "op/auth/register", ]; class EnoFactory { constructor(typeOrEnoProto = null, security = null) { this._proto = null; this._patchTargetTip = null; this._useEmptyNonce = false; this._useTipNonce = false; this._useKnownNonce = null; if (typeOrEnoProto && typeof typeOrEnoProto !== "string") { this.setProto(typeOrEnoProto); return; } this.reset(typeOrEnoProto, security); } makeEno() { const isValid = this._isSettingValid(); if (!isValid) { throw new Error("Eno Factory setting has not enough information to make an eno."); } this._cleanField(); if (this._useEmptyNonce || this._emptyNonceRequired()) { this._proto.source.nonce = ''; } else if (this._useTipNonce && this._patchTargetTip) { this._proto.source.nonce = this._patchTargetTip; } else if (this._useKnownNonce !== null) { this._proto.source.nonce = this._useKnownNonce; } else { this._proto.source.nonce = this._getRandomNonce(); } this._generateSid(); this._cleanTransaction(); this._proto.tip = this._patchTargetTip || this._proto.sid; return new Eno_1.Eno(this._proto); } _getRandomNonce() { return Math.random() + ""; } _cleanTransaction() { if (this._proto.clientT === null) { this._resetClientT(); } const clientT = this._proto.clientT; clientT.sequence = clientT.sequence || 1; clientT.createdDate = clientT.modifiedDate = new Date().valueOf(); this._proto.serverT = null; } _emptyNonceRequired() { return EMPTY_NONCE_TYPES.indexOf(this._proto.source.type) > -1; } _generateSid() { let sha256 = (0, sha_js_1.default)("sha256"); (0, sidStringify_1.hashSidStringify)(sha256, this._proto.source); this._proto.sid = sha256.digest("hex"); sha256 = null; } _cleanField() { this._proto.source.field = this._proto.source.field.filter((field) => { return ((field.value && field.value.length !== 0) || (field.i18n && _containsNonEmptyValue()) || field.formula); function _containsNonEmptyValue() { let nonEmptyValueFound = false; for (let i = 0; i < field.i18n.length; i++) { if (field.i18n[i].value && field.i18n[i].value.length > 0) { nonEmptyValueFound = true; break; } } return nonEmptyValueFound; } }); } _isSettingValid() { return !(this._proto.source === null || this._proto.source.type === null || this._proto.source.security === null); } reset(type = null, security = null) { this._patchTargetTip = null; this._useEmptyNonce = false; this._useTipNonce = false; this._useKnownNonce = null; this._proto = { source: { deleted: false, type, security, parent: [], field: [], nonce: this._getRandomNonce(), }, tip: null, sid: null, serverT: null, clientT: null, }; return this; } setProto(eno) { this.reset(); if (eno.clientT) { this._proto.clientT = (0, lodash_1.cloneDeep)(eno.clientT); } if (eno.source) { this._proto.source = (0, lodash_1.cloneDeep)(eno.source); } return this; } setProtoToPatch(eno) { if (!eno.source) { throw new Error("You can't patch acknowledgement"); } this.reset(); this._patchTargetTip = eno.tip; if (eno.clientT) { this._proto.clientT = (0, lodash_1.cloneDeep)(eno.clientT); this._proto.clientT.sequence++; } else { this._resetClientT(); this._proto.clientT.sequence = 1; } this._proto.source = (0, lodash_1.cloneDeep)(eno.source); this._proto.source.parent = [eno.sid]; return this; } resetFields() { this._proto.source.field = []; return this; } setWellKnownTip(tip) { this._patchTargetTip = tip; return this; } setType(type) { this._proto.source.field = this._proto.source.type !== type ? [] : this._proto.source.field; this._proto.source.type = type; return this; } useEmptyNonce() { this._useEmptyNonce = true; return this; } useTipNonce() { this._useTipNonce = true; return this; } useRandomNonce() { this._useTipNonce = false; this._useEmptyNonce = false; this._useKnownNonce = null; return this; } useKnownNonce(nonce) { this._useKnownNonce = nonce; return this; } setI18nValue(fieldTip, lang, value) { value = value.filter(this._normalizeValuesFilter); let fieldFound = false; for (let i = 0; i < this._proto.source.field.length; i++) { const field = this._proto.source.field[i]; if (field.tip === fieldTip) { fieldFound = true; this._updateExistingI18n(this._proto.source.field[i], lang, value); break; } } if (fieldFound) { return this; } this._proto.source.field.push({ tip: fieldTip, i18n: [{ lang, value }] }); return this; } _updateExistingI18n(field, lang, value) { field.i18n = field.i18n || []; if (field.value) { field.i18n = [{ lang: constants_1.default.LANG_DEFAULT, value: field.value }]; delete field.value; } if (!(0, lodash_1.isArray)(value) || value.length === 0) { (0, lodash_1.pullAllBy)(field.i18n, [{ lang }], "lang"); return; } const i18nValue = field.i18n.find((i18n) => i18n.lang === lang); if (i18nValue && (0, lodash_1.isEqual)(i18nValue.value, value)) { return; } if (lang === constants_1.default.LANG_DEFAULT) { field.i18n = [{ lang, value }]; return; } let langFound = false; for (const i18n of field.i18n) { if (i18n.lang === lang) { langFound = true; i18n.value = value; break; } } if (!langFound) { field.i18n.push({ lang, value }); } } setField(newFieldOrTip, value) { let newField = typeof newFieldOrTip === "string" ? { tip: newFieldOrTip, value } : newFieldOrTip; newField = this._normalizeIField(newField); let fieldFound = false; for (let i = 0; i < this._proto.source.field.length; i++) { const field = this._proto.source.field[i]; if (field.tip === newField.tip) { fieldFound = true; this._proto.source.field[i] = newField; break; } } if (fieldFound) { return this; } this._proto.source.field.push(newField); return this; } setFieldFormula(fieldTip, formula) { let newField = { tip: fieldTip, formula: [formula] }; newField = this._normalizeIField(newField); let fieldFound = false; for (let i = 0; i < this._proto.source.field.length; i++) { const field = this._proto.source.field[i]; if (field.tip === newField.tip) { fieldFound = true; this._proto.source.field[i] = newField; break; } } if (fieldFound) { return this; } this._proto.source.field.push(newField); return this; } _normalizeIField(field) { if (!field.i18n && !field.formula) { field.value = field.value === null || field.value === undefined ? [] : field.value; field.value = field.value.filter(this._normalizeValuesFilter); return field; } if (field.i18n) { field.i18n.forEach((i18n) => { i18n.value = i18n.value === null || i18n.value === undefined ? [] : i18n.value; i18n.value = i18n.value.filter(this._normalizeValuesFilter); }); } return field; } _normalizeValuesFilter(val) { return val !== null && val !== undefined; } setFields(newFields) { newFields.forEach((newField) => { this.setField(newField); }); return this; } setSecurity(security) { this._proto.source.security = security; return this; } setDeleted(deleted) { this._proto.source.deleted = deleted; this.resetFields(); return this; } setBranch(branch = constants_1.default.BRANCH_MASTER) { if (this._proto.clientT === null) { this._resetClientT(branch); return this; } this._proto.clientT.branch = branch; return this; } _resetClientT(branch = constants_1.default.BRANCH_MASTER) { this._proto.clientT = { branch, sequence: null, createdDate: null, modifiedDate: null, }; } setSequence(sequence) { if (this._proto.clientT === null) { this._resetClientT(); } this._proto.clientT.sequence = sequence; return this; } } exports.EnoFactory = EnoFactory;