MCPcopy Index your code
hub / github.com/simstudioai/sim / resolveEnvironmentVariables

Function resolveEnvironmentVariables

apps/sim/app/api/function/execute/route.ts:539–589  ·  view source on GitHub ↗
(
  code: string,
  params: Record<string, any>,
  envVars: Record<string, string>,
  contextVariables: Record<string, any>
)

Source from the content-addressed store, hash-verified

537}
538
539function resolveEnvironmentVariables(
540 code: string,
541 params: Record<string, any>,
542 envVars: Record<string, string>,
543 contextVariables: Record<string, any>
544): string {
545 let resolvedCode = code
546
547 const regex = createEnvVarPattern()
548 let match: RegExpExecArray | null
549 const replacements: Array<{ match: string; index: number; varName: string; varValue: string }> =
550 []
551
552 const resolverVars: Record<string, string> = {}
553 Object.entries(params).forEach(([key, value]) => {
554 if (value !== undefined && value !== null) {
555 resolverVars[key] = String(value)
556 }
557 })
558 Object.entries(envVars).forEach(([key, value]) => {
559 if (value !== undefined && value !== null) {
560 resolverVars[key] = value
561 }
562 })
563
564 while ((match = regex.exec(code)) !== null) {
565 const varName = match[1].trim()
566
567 if (!(varName in resolverVars)) {
568 continue
569 }
570
571 replacements.push({
572 match: match[0],
573 index: match.index,
574 varName,
575 varValue: resolverVars[varName],
576 })
577 }
578
579 for (let i = replacements.length - 1; i >= 0; i--) {
580 const { match: matchStr, index, varName, varValue } = replacements[i]
581
582 const safeVarName = `__var_${varName.replace(/[^a-zA-Z0-9_]/g, '_')}`
583 contextVariables[safeVarName] = varValue
584 resolvedCode =
585 resolvedCode.slice(0, index) + safeVarName + resolvedCode.slice(index + matchStr.length)
586 }
587
588 return resolvedCode
589}
590
591function resolveTagVariables(
592 code: string,

Callers 1

resolveCodeVariablesFunction · 0.85

Calls 3

createEnvVarPatternFunction · 0.90
replaceMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected