MCPcopy Create free account
hub / github.com/promptfoo/promptfoo / getJavascriptTransformFunction

Function getJavascriptTransformFunction

src/util/transform.ts:71–92  ·  view source on GitHub ↗

* Retrieves a JavaScript transform function from a file. * @param filePath - The path to the JavaScript file. * @param functionName - Optional name of the function to retrieve. * @returns A Promise resolving to the requested function. * @throws Error if the file doesn't export a valid function.

(
  filePath: string,
  functionName?: string,
)

Source from the content-addressed store, hash-verified

69 * @throws Error if the file doesn't export a valid function.
70 */
71async function getJavascriptTransformFunction(
72 filePath: string,
73 functionName?: string,
74): Promise<Function> {
75 const requiredModule = await importModule(filePath);
76
77 // Validate that functionName is an own property to prevent prototype pollution attacks
78 if (
79 functionName &&
80 Object.prototype.hasOwnProperty.call(requiredModule, functionName) &&
81 typeof requiredModule[functionName] === 'function'
82 ) {
83 return requiredModule[functionName];
84 } else if (typeof requiredModule === 'function') {
85 return requiredModule;
86 } else if (requiredModule.default && typeof requiredModule.default === 'function') {
87 return requiredModule.default;
88 }
89 throw new Error(
90 `Transform ${filePath} must export a function, have a default export as a function, or export the specified function "${functionName}"`,
91 );
92}
93
94/**
95 * Creates a function that runs a Python transform function.

Callers 1

getFileTransformFunctionFunction · 0.85

Calls 2

importModuleFunction · 0.90
callMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…