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