| 348 | } |
| 349 | |
| 350 | fn script_source_with_host_entrypoint(source: &str) -> Result<String> { |
| 351 | let rewritten = if source.contains("export default async function run") { |
| 352 | source.replacen("export default async function run", "async function run", 1) |
| 353 | } else if source.contains("export default function run") { |
| 354 | source.replacen("export default function run", "function run", 1) |
| 355 | } else if source.contains("async function run") || source.contains("function run") { |
| 356 | source.to_string() |
| 357 | } else { |
| 358 | return Err(anyhow!( |
| 359 | "PTC script must define async function run(ctx, inputs)" |
| 360 | )); |
| 361 | }; |
| 362 | |
| 363 | Ok(format!( |
| 364 | r#"{rewritten} |
| 365 | |
| 366 | globalThis.__a3sResultJson = (async () => JSON.stringify(await run(globalThis.__a3sCtx, globalThis.__a3sInputs)))(); |
| 367 | "# |
| 368 | )) |
| 369 | } |
| 370 | |
| 371 | async fn run_embedded_script( |
| 372 | source: String, |