From 2d351c3654f638662cbe097c96803c43f3ae06d4 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Thu, 6 Mar 2025 18:22:42 +0800 Subject: [PATCH] perf: http body check --- .../core/workflow/dispatch/tools/http468.ts | 145 ++++++++++++++++-- 1 file changed, 136 insertions(+), 9 deletions(-) diff --git a/packages/service/core/workflow/dispatch/tools/http468.ts b/packages/service/core/workflow/dispatch/tools/http468.ts index cc36e0c0f..71ea700b7 100644 --- a/packages/service/core/workflow/dispatch/tools/http468.ts +++ b/packages/service/core/workflow/dispatch/tools/http468.ts @@ -120,27 +120,144 @@ export const dispatchHttp468Request = async (props: HttpRequestProps): Promise { - const valToStr = (val: any) => { + // Check if the variable is in quotes + const isVariableInQuotes = (text: string, variable: string) => { + const index = text.indexOf(variable); + if (index === -1) return false; + + // 计算变量前面的引号数量 + const textBeforeVar = text.substring(0, index); + const matches = textBeforeVar.match(/"/g) || []; + + // 如果引号数量为奇数,则变量在引号内 + return matches.length % 2 === 1; + }; + const valToStr = (val: any, isQuoted = false) => { if (val === undefined) return 'null'; if (val === null) return 'null'; if (typeof val === 'object') return JSON.stringify(val); if (typeof val === 'string') { + if (isQuoted) { + return val.replace(/(? { + // const testData = [ + // // 基本字符串替换 + // { + // body: `{"name":"{{name}}","age":"18"}`, + // variables: [{ key: '{{name}}', value: '测试' }], + // result: `{"name":"测试","age":"18"}` + // }, + // // 特殊字符处理 + // { + // body: `{"text":"{{text}}"}`, + // variables: [{ key: '{{text}}', value: '包含"引号"和\\反斜杠' }], + // result: `{"text":"包含\\"引号\\"和\\反斜杠"}` + // }, + // // 数字类型处理 + // { + // body: `{"count":{{count}},"price":{{price}}}`, + // variables: [ + // { key: '{{count}}', value: '42' }, + // { key: '{{price}}', value: '99.99' } + // ], + // result: `{"count":42,"price":99.99}` + // }, + // // 布尔值处理 + // { + // body: `{"isActive":{{isActive}},"hasData":{{hasData}}}`, + // variables: [ + // { key: '{{isActive}}', value: 'true' }, + // { key: '{{hasData}}', value: 'false' } + // ], + // result: `{"isActive":true,"hasData":false}` + // }, + // // 对象类型处理 + // { + // body: `{"user":{{user}},"user2":"{{user2}}"}`, + // variables: [ + // { key: '{{user}}', value: `{"id":1,"name":"张三"}` }, + // { key: '{{user2}}', value: `{"id":1,"name":"张三"}` } + // ], + // result: `{"user":{"id":1,"name":"张三"},"user2":"{\\"id\\":1,\\"name\\":\\"张三\\"}"}` + // }, + // // 数组类型处理 + // { + // body: `{"items":{{items}}}`, + // variables: [{ key: '{{items}}', value: '[1, 2, 3]' }], + // result: `{"items":[1,2,3]}` + // }, + // // null 和 undefined 处理 + // { + // body: `{"nullValue":{{nullValue}},"undefinedValue":{{undefinedValue}}}`, + // variables: [ + // { key: '{{nullValue}}', value: 'null' }, + // { key: '{{undefinedValue}}', value: 'undefined' } + // ], + // result: `{"nullValue":null,"undefinedValue":null}` + // }, + // // 嵌套JSON结构 + // { + // body: `{"data":{"nested":{"value":"{{nestedValue}}"}}}`, + // variables: [{ key: '{{nestedValue}}', value: '嵌套值' }], + // result: `{"data":{"nested":{"value":"嵌套值"}}}` + // }, + // // 多变量替换 + // { + // body: `{"first":"{{first}}","second":"{{second}}","third":{{third}}}`, + // variables: [ + // { key: '{{first}}', value: '第一' }, + // { key: '{{second}}', value: '第二' }, + // { key: '{{third}}', value: '3' } + // ], + // result: `{"first":"第一","second":"第二","third":3}` + // }, + // // JSON字符串作为变量值 + // { + // body: `{"config":{{config}}}`, + // variables: [{ key: '{{config}}', value: '{"setting":"enabled","mode":"advanced"}' }], + // result: `{"config":{"setting":"enabled","mode":"advanced"}}` + // } + // ]; + + // for (let i = 0; i < testData.length; i++) { + // const item = testData[i]; + // let bodyStr = item.body; + // for (const variable of item.variables) { + // const isQuote = isVariableInQuotes(bodyStr, variable.key); + // bodyStr = bodyStr.replace(variable.key, valToStr(variable.value, isQuote)); + // } + // bodyStr = bodyStr.replace(/(".*?")\s*:\s*undefined\b/g, '$1:null'); + + // console.log(bodyStr === item.result, i); + // if (bodyStr !== item.result) { + // console.log(bodyStr); + // console.log(item.result); + // } else { + // try { + // JSON.parse(item.result); + // } catch (error) { + // console.log('反序列化异常', i, item.result); + // } + // } + // } + // }; + // bodyTest(); // 1. Replace {{key.key}} variables const regex1 = /\{\{\$([^.]+)\.([^$]+)\$\}\}/g; @@ -148,6 +265,10 @@ export const dispatchHttp468Request = async (props: HttpRequestProps): Promise { const nodeId = match[1]; const id = match[2]; + const fullMatch = match[0]; + + // 检查变量是否在引号内 + const isInQuotes = isVariableInQuotes(text, fullMatch); const variableVal = (() => { if (nodeId === VARIABLE_NODE_ID) { @@ -165,9 +286,9 @@ export const dispatchHttp468Request = async (props: HttpRequestProps): Promise formatVal); }); @@ -176,10 +297,16 @@ export const dispatchHttp468Request = async (props: HttpRequestProps): Promise match.slice(2, -2)))]; for (const key of uniqueKeys2) { - text = text.replace(new RegExp(`{{(${key})}}`, 'g'), () => valToStr(allVariables[key])); + const fullMatch = `{{${key}}}`; + // 检查变量是否在引号内 + const isInQuotes = isVariableInQuotes(text, fullMatch); + + text = text.replace(new RegExp(`{{(${key})}}`, ''), () => + valToStr(allVariables[key], isInQuotes) + ); } - return text.replace(/(".*?")\s*:\s*undefined\b/g, '$1: null'); + return text.replace(/(".*?")\s*:\s*undefined\b/g, '$1:null'); }; httpReqUrl = replaceStringVariables(httpReqUrl);