* 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, )
| 69 | * @throws Error if the file doesn't export a valid function. |
| 70 | */ |
| 71 | async 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. |
no test coverage detected
searching dependent graphs…