| 220 | ); |
| 221 | |
| 222 | const resolveScriptSource = async ( |
| 223 | inlineSource: string | undefined, |
| 224 | sourcePath: string | undefined, |
| 225 | context: Context, |
| 226 | ): Promise<string> => { |
| 227 | if (inlineSource !== undefined) { |
| 228 | if (sourcePath !== undefined) { |
| 229 | throw new Error('Specify exactly one of function or sourcePath.'); |
| 230 | } |
| 231 | return inlineSource; |
| 232 | } |
| 233 | if (sourcePath === undefined) { |
| 234 | throw new Error('Specify exactly one of function or sourcePath.'); |
| 235 | } |
| 236 | |
| 237 | const resourceUrl = sourcePath.startsWith('file:') |
| 238 | ? sourcePath |
| 239 | : pathToFileURL(sourcePath).href; |
| 240 | try { |
| 241 | return await context.loadResource(resourceUrl); |
| 242 | } catch (error) { |
| 243 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 244 | throw new Error( |
| 245 | `Unable to read script source from ${sourcePath}: ${errorMessage}`, |
| 246 | {cause: error}, |
| 247 | ); |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | const performEvaluation = async ( |
| 252 | evaluatable: Evaluatable, |