fix: js-parse (#1298)

Reviewed-on: daoyoucloud/tachybase#1298
This commit is contained in:
sealday 2024-07-09 20:24:33 +08:00
parent 66736adafa
commit 3e0a949997
5 changed files with 1383 additions and 825 deletions

View File

@ -88,9 +88,6 @@
"pnpm": "8.15.5" "pnpm": "8.15.5"
}, },
"pnpm": { "pnpm": {
"neverBuiltDependencies": [
"canvas"
],
"overrides": { "overrides": {
"@types/node": "20.14.2", "@types/node": "20.14.2",
"antd": "5.19.1", "antd": "5.19.1",

View File

@ -18,12 +18,15 @@
"@types/ejs": "^3.1.1", "@types/ejs": "^3.1.1",
"@types/file-saver": "^2.0.7", "@types/file-saver": "^2.0.7",
"@types/lodash": "^4.17.5", "@types/lodash": "^4.17.5",
"@types/qrcode": "^1.5.5",
"antd": "5.19.1", "antd": "5.19.1",
"axios": "^1.6.2", "axios": "^1.6.2",
"canvas": "^2.11.2",
"cron-parser": "4.4.0", "cron-parser": "4.4.0",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"lodash": "4.17.21", "lodash": "4.17.21",
"lru-cache": "8.0.5", "lru-cache": "8.0.5",
"qrcode": "^1.5.3",
"react": "~18.3.1", "react": "~18.3.1",
"react-i18next": "^14.1.2", "react-i18next": "^14.1.2",
"react-js-cron": "^3.1.0", "react-js-cron": "^3.1.0",

View File

@ -27,7 +27,7 @@ export class JSParseInstruction extends Instruction {
type: 'string', type: 'string',
title: tval('JSCode expression'), title: tval('JSCode expression'),
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Input.TextArea', 'x-component': 'CodeMirror',
required: true, required: true,
}, },
model: { model: {

View File

@ -1,4 +1,6 @@
import * as canvas from 'canvas';
import _ from 'lodash'; import _ from 'lodash';
import qrcode from 'qrcode';
import { FlowNodeModel, Instruction, JOB_STATUS, Processor } from '../..'; import { FlowNodeModel, Instruction, JOB_STATUS, Processor } from '../..';
@ -6,9 +8,21 @@ export class JSParseInstruction extends Instruction {
async run(node: FlowNodeModel, input: any, processor: Processor) { async run(node: FlowNodeModel, input: any, processor: Processor) {
const { source = '', JSCode = '', model } = node.config; const { source = '', JSCode = '', model } = node.config;
const data = processor.getParsedValue(source, node.id); const data = processor.getParsedValue(source, node.id);
const query = evalSimulate;
try { try {
let result = query ? await query(JSCode, { scopes: data, handlers: {}, modules: {} }) : data; const ctx = {
data,
body: {},
};
await evalSimulate(JSCode, {
ctx,
lib: {
JSON,
canvas,
qrcode,
log: console.log,
},
});
let result = ctx.body;
if (typeof result === 'object' && result && model?.length) { if (typeof result === 'object' && result && model?.length) {
if (Array.isArray(result)) { if (Array.isArray(result)) {
@ -47,10 +61,12 @@ function mapModel(data, model) {
return result; return result;
} }
async function evalSimulate(jsCode, { scopes, handlers, modules }) { async function evalSimulate(jsCode, { ctx, lib }) {
try { const AsyncFunction = async function () {}.constructor;
return new Function('$root', `with($root) { ${jsCode}; }`)({ scopes, handlers, modules }); return await new AsyncFunction('$root', `with($root) { ${jsCode}; }`)({
} catch (err) { ctx,
console.log('err', err); // 允许用户覆盖,这个时候可以使用 _ctx
} __ctx: ctx,
lib,
});
} }

File diff suppressed because it is too large Load Diff