UNPKG

database-proxy

Version:

Through a set of access control rules configuration database access to realize the client directly access the database via HTTP.

86 lines (85 loc) 2.96 kB
"use strict"; 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 __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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConditionHandler = void 0; const vm = __importStar(require("vm")); const ConditionHandler = async function (config, context) { // 缺省验证时,给予通过 if (config === undefined) { return null; } try { const { injections, params, ruler } = context; const global = Object.assign(Object.assign({}, injections), params); const queries = getQueries(config, global); const prepared = []; for (const query of queries) { const [target, value] = query; const result = await doQuery(target, value, ruler.accessor); prepared.push({ query, result }); } function GetFunc(target, value) { for (const el of prepared) { const [t, v] = el.query; if (t === target && v === value) return el.result; } return null; } global.get = GetFunc; const script = new vm.Script(config); const result = script.runInNewContext(global); if (result) return null; return 'the expression evaluated to a falsy value'; } catch (error) { return error; } }; exports.ConditionHandler = ConditionHandler; async function doQuery(target, value, accessor) { // target like '/users/_id' const [, collection, field] = target.split('/'); const query = { [field]: value, }; const result = await accessor.get(collection, query); return result; } function getQueries(code, injections) { const wrapper = ` let collection = [] function get(...params){ collection.push(params) return {} } ${code}; (collection); `; const script = new vm.Script(wrapper); const queries = script.runInNewContext(injections); return queries; }