UNPKG

@tachybase/module-workflow

Version:

A powerful BPM tool that provides foundational support for business automation, with the capability to extend unlimited triggers and nodes.

213 lines (212 loc) 6.64 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var script_instruction_exports = {}; __export(script_instruction_exports, { ScriptInstruction: () => ScriptInstruction }); module.exports = __toCommonJS(script_instruction_exports); var import_node_crypto = __toESM(require("node:crypto")); var import_node_vm = require("node:vm"); var import_core = require("@babel/core"); var import_dayjs = __toESM(require("dayjs")); var import_jsonata = __toESM(require("jsonata")); var import_lodash = __toESM(require("lodash")); var import_qrcode = __toESM(require("qrcode")); var import__ = require("../.."); class ScriptInstruction extends import__.Instruction { async run(node, input, processor) { const { sourceArray, type, code = "", model } = node.config; let data = {}; switch (sourceArray.length) { case 0: { data = {}; break; } case 1: { const keyName = sourceArray[0]["keyName"]; const sourcePath = sourceArray[0]["sourcePath"]; const rawData = processor.getParsedValue(sourcePath, node.id); if (keyName) { data = { [keyName]: rawData }; } else { data = rawData; } break; } default: { data = sourceArray.reduce( (cookedData, { keyName, sourcePath }) => ({ ...cookedData, [keyName]: processor.getParsedValue(sourcePath, node.id) }), {} ); } } try { let result = {}; switch (type) { case "jsonata": result = await convertByJSONata(code, data); break; case "js": result = await convertByJsCode(code, data); break; case "ts": result = await convertByTsCode(code, data, processor); break; default: } if (typeof result === "object" && result && (model == null ? void 0 : model.length)) { if (Array.isArray(result)) { result = result.map((item) => mapModel(item, model)); } else { result = mapModel(result, model); } } return { result, status: import__.JOB_STATUS.RESOLVED }; } catch (err) { return { result: err.toString(), status: import__.JOB_STATUS.ERROR }; } } } async function convertByJSONata(code, data) { const engine = (expression, data2) => (0, import_jsonata.default)(expression).evaluate(data2); const result = await engine(code, data); return result; } async function convertByJsCode(code, data) { const ctx = { data, body: {} }; await evalSimulate(code, { ctx, lib: { log: console.log, JSON, qrcode: import_qrcode.default, crypto: import_node_crypto.default, jsonata: import_jsonata.default, dayjs: import_dayjs.default } }); return ctx.body; } async function convertByTsCode(code, data, processor) { const options = processor.options; const app = processor.options.plugin.app; const compiledCode = (0, import_core.transform)(code, { sourceType: "module", filename: "a.tsx", presets: [ [ require("@babel/preset-env"), { modules: "commonjs", targets: { node: "current" } } ], require("@babel/preset-react"), require("@babel/preset-typescript") ] }).code; const script = new import_node_vm.Script(compiledCode); const defaultFunction = async (event, context) => { return {}; }; const contextRequire = function(moduleName) { if (moduleName === "@tachybase/utils/client") { return require.call(this, "@tachybase/utils"); } if (moduleName === "@tachybase/module-pdf/client") { return require.call(this, "@tachybase/module-pdf"); } if (moduleName === "@react-pdf/renderer") { return require.call(this, "@tachybase/module-pdf"); } if (app.modules[moduleName]) { return app.modules[moduleName]; } return require.call(this, moduleName); }; Object.assign(contextRequire, require); const sandbox = { module: {}, exports: { default: defaultFunction }, require: contextRequire, console }; (0, import_node_vm.createContext)(sandbox); try { script.runInContext(sandbox); } catch (error) { app.logger.error("Cloud Component ", { error }); } const func = sandbox.exports.default; const result = await func(data, { ...options }); return result; } function mapModel(data, model) { if (typeof data !== "object" || data === null) { throw new Error("Invalid data: data should be a non-null object"); } const result = model.reduce((acc, { path, alias }) => { const key = alias ?? path.replace(/\./g, "_"); const value = import_lodash.default.get(data, path); acc[key] = value; return acc; }, {}); return result; } async function evalSimulate(jsCode, { ctx, lib }) { const AsyncFunction = async function() { }.constructor; return await new AsyncFunction("$root", `with($root) { ${jsCode}; }`)({ ctx, // 允许用户覆盖,这个时候可以使用 _ctx __ctx: ctx, lib }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ScriptInstruction });