From 082d25d6c8f125a716be6b0181086a54fd507b53 Mon Sep 17 00:00:00 2001 From: Soorria Saruva Date: Tue, 16 May 2023 13:34:27 +1000 Subject: [PATCH] fix code execution params name clobbering, and make variable access slightly safer --- src/chain.ts | 16 +++++++++++----- src/variable.ts | 4 ++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/chain.ts b/src/chain.ts index 6339419..f61d76b 100644 --- a/src/chain.ts +++ b/src/chain.ts @@ -1,4 +1,10 @@ -import { UnwrapVariable, Variable, createVariable, toPath } from "./variable"; +import { + UnwrapVariable, + Variable, + createVariable, + toOptionalPath, + toPath, +} from "./variable"; import { API, APIAuthDetails } from "./api"; import { AllowedTransformationId, @@ -133,7 +139,7 @@ export class Chain< public code< CodeParams extends Record>, - Fn extends (params: CodeParams) => any + Fn extends (params: UnwrapVariable) => any >( params: CodeParams, fn: Fn @@ -142,7 +148,7 @@ export class Chain< } { const paramsObjectContent = Object.entries(params) .map(([key, variable]) => { - return `${JSON.stringify(key)}: ${toPath(variable)}`; + return `${JSON.stringify(key)}: ${toOptionalPath(variable)}`; }) .join(","); @@ -150,8 +156,8 @@ export class Chain< const codeAsIIFE = ` return (() => { - const params = { ${paramsObjectContent} }; - return (${fnString})(params); + const _$$params = { ${paramsObjectContent} }; + return (${fnString})(_$$params); })();`.trim(); return this.step("js_code_transformation", { diff --git a/src/variable.ts b/src/variable.ts index 9178fb8..9b3c54a 100644 --- a/src/variable.ts +++ b/src/variable.ts @@ -60,3 +60,7 @@ export const isVariable = (value: any): value is Variable => { export const toPath = (variable: Variable): string => { return variable[VARIABLE_INTERNAL].path; }; + +export const toOptionalPath = (variable: Variable): string => { + return toPath(variable).split(".").join("?."); +};