UNPKG

@enonic/mock-xp

Version:

Mock Enonic XP API JavaScript Library

245 lines (244 loc) 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RepoConnection = void 0; var constants_1 = require("../constants"); var parentFromPath_1 = require("../util/parentFromPath"); var castBufferToByteSource_1 = require("../util/castBufferToByteSource"); var castTDataOutToByteSource_1 = require("../util/castTDataOutToByteSource"); var NodeAlreadyExistAtPathException_1 = require("./node/NodeAlreadyExistAtPathException"); var NodeNotFoundException_1 = require("./node/NodeNotFoundException"); var OperationNotPermittedException_1 = require("./node/OperationNotPermittedException"); var RepoConnection = (function () { function RepoConnection(_a) { var branch = _a.branch; this.branch = branch; this.log = this.branch.log; } RepoConnection.prototype.create = function (param) { return this.branch.createNode(param); }; RepoConnection.prototype.delete = function (keys) { return this.branch.deleteNode(keys); }; RepoConnection.prototype.duplicate = function (_a) { var nodeId = _a.nodeId, name = _a.name, parent = _a.parent, _b = _a.dataProcessor, dataProcessor = _b === void 0 ? function (node) { return node; } : _b, refresh = _a.refresh; if (nodeId === constants_1.UUID_NIL) { throw new OperationNotPermittedException_1.OperationNotPermittedException('Not allowed to duplicate root-node'); } var nodeToDuplicate = this._getSingle(nodeId); var newName = name || nodeToDuplicate._name; var parentPath = parent ? parent : (0, parentFromPath_1.parentFromPath)(nodeToDuplicate._path); if (name || parent) { var path = "".concat(parentPath, "/").concat(newName).replace(/\/+/g, '/'); if (this.exists(path)) { throw new NodeAlreadyExistAtPathException_1.NodeAlreadyExistAtPathException(path, this.branch.repo.id, this.branch); } } if (this.exists("".concat(parentPath, "/").concat(newName).replace(/\/+/g, '/'))) { var exists = false; do { newName = /-copy(-\d+)?$/.test(newName) ? "".concat(newName.replace(/-copy(-\d+)?$/, ''), "-copy-").concat(((parseInt(newName.replace(/.*-copy(-(\d+))$/, '$2')) || 1) + 1)) : "".concat(newName, "-copy"); var path = "".concat(parentPath, "/").concat(newName).replace(/\/+/g, '/'); exists = this.exists(path); } while (exists); } var duplicatedNodeData = dataProcessor(nodeToDuplicate); delete duplicatedNodeData._id; delete duplicatedNodeData._ts; delete duplicatedNodeData._versionKey; duplicatedNodeData._name = newName; duplicatedNodeData._parentPath = parentPath; var duplicatedNode = this.create(duplicatedNodeData); if (refresh) { this.refresh(refresh); } return duplicatedNode; }; RepoConnection.prototype.exists = function (key) { return this.branch.existsNode(key); }; RepoConnection.prototype.findChildren = function (_a) { var _b = _a.count, count = _b === void 0 ? 10 : _b, parentKey = _a.parentKey, _c = _a.start, start = _c === void 0 ? 0 : _c; var parentNode = this._getSingle(parentKey); var _path = parentNode._path; var childrenRes = this.query({ count: count, query: { boolean: { must: { term: { field: '_parentPath', value: _path, } } } }, start: start, }); return { count: childrenRes.count, hits: childrenRes.hits.map(function (_a) { var id = _a.id; return ({ id: id }); }), total: childrenRes.total, }; }; RepoConnection.prototype.get = function () { var keys = []; for (var _i = 0; _i < arguments.length; _i++) { keys[_i] = arguments[_i]; } return this.branch.getNodes({ keys: keys }); }; RepoConnection.prototype.getActiveVersion = function (_a) { var key = _a.key; return this.branch.getNodeActiveVersion({ key: key }); }; RepoConnection.prototype._getSingle = function (key) { return this.branch.getNode({ key: key }); }; RepoConnection.prototype.getBinary = function (_a) { var _b, _c, _d; var binaryReference = _a.binaryReference, key = _a.key; var node = this._getSingle(key); if (!node) { throw new NodeNotFoundException_1.NodeNotFoundException("Cannot get binary reference, node with id: ".concat(key, " not found")); } var sha512 = (_c = (_b = this.branch.binaryReferences) === null || _b === void 0 ? void 0 : _b[node._id]) === null || _c === void 0 ? void 0 : _c[binaryReference]; if (!sha512) { var emptyBuffer = Buffer.alloc(0); return (0, castBufferToByteSource_1.castBufferToByteSource)(emptyBuffer); } var tDataOut = this.branch.repo.server.vol.readFileSync("/".concat(sha512)); if (!tDataOut) { this.log.error("File with sha512: ".concat(sha512, " not found in vol! Repo: ").concat(this.branch.repo.id, " Branch: ").concat(this.branch.id, " NodeId: ").concat(node._id, " NodeBinaryReferences: ").concat(JSON.stringify((_d = this.branch.binaryReferences) === null || _d === void 0 ? void 0 : _d[node._id]))); } return (0, castTDataOutToByteSource_1.castTDataOutToByteSource)(tDataOut); }; RepoConnection.prototype.modify = function (_a) { var key = _a.key, editor = _a.editor; return this.branch.modifyNode({ key: key, editor: editor }); }; RepoConnection.prototype.move = function (_a) { var source = _a.source, target = _a.target; var isMoved = false; try { isMoved = !!this.branch.moveNode({ source: source, target: target }); } catch (e) { this.branch.log.error("Error in moveNode message:".concat(e.message), e); } return isMoved; }; RepoConnection.prototype.push = function (_a) { var key = _a.key, _b = _a.keys, keys = _b === void 0 ? [] : _b, target = _a.target; if (target === this.branch.id) { throw new Error('Target branch cannot be the same as source branch!'); } var targetBranch = this.branch.repo.getBranch(target); var targetBranchRepoConnection = new RepoConnection({ branch: targetBranch, }); if (key) { keys.push(key); } if (!keys.length) { throw new Error('No keys to push'); } var pushNodesResult = { success: [], failed: [], deleted: [], }; nodeKeysLoop: for (var i = 0; i < keys.length; i++) { var k = keys[i]; var existsOnSource = this.exists(k); var existsOnTarget = targetBranch.existsNode(k); if (existsOnSource) { var nodeOnSource = this._getSingle(k); if (existsOnTarget) { targetBranch._overwriteNode({ node: nodeOnSource }); pushNodesResult.success.push(k); continue nodeKeysLoop; } var createdNode = targetBranch._createNodeInternal(nodeOnSource); if (createdNode) { pushNodesResult.success.push(k); } else { pushNodesResult.failed.push({ id: k, reason: 'PARENT_NOT_FOUND' }); } continue nodeKeysLoop; } if (existsOnTarget) { if (targetBranchRepoConnection.delete(k)) { pushNodesResult.deleted.push(k); } else { pushNodesResult.failed.push({ id: k, reason: 'ACCESS_DENIED' }); } continue nodeKeysLoop; } pushNodesResult.failed.push({ id: k, reason: 'NOT_FOUND_ON_SOURCE_NOR_TARGET' }); } return pushNodesResult; }; RepoConnection.prototype.query = function (_a) { var aggregations = _a.aggregations, count = _a.count, explain = _a.explain, filters = _a.filters, highlight = _a.highlight, query = _a.query, sort = _a.sort, start = _a.start; return this.branch.query({ aggregations: aggregations, count: count, explain: explain, filters: filters, highlight: highlight, query: query, sort: sort, start: start }); }; RepoConnection.prototype.refresh = function (mode) { return this.branch.refresh({ mode: mode }); }; RepoConnection.prototype.setChildOrder = function (_a) { var childOrder = _a.childOrder, key = _a.key; return this.modify({ key: key, editor: function (node) { node._childOrder = childOrder; return node; } }); }; RepoConnection.prototype.setRootPermission = function (_a) { var _permissions = _a._permissions, _inheritsPermissions = _a._inheritsPermissions; return this.modify({ key: '/', editor: function (node) { node._inheritsPermissions = _inheritsPermissions; node._permissions = _permissions; return node; } }); }; return RepoConnection; }()); exports.RepoConnection = RepoConnection;