diff --git a/engine/sdks/rust/runner-protocol/build.rs b/engine/sdks/rust/runner-protocol/build.rs index 3d8188330a..d6d122ef2e 100644 --- a/engine/sdks/rust/runner-protocol/build.rs +++ b/engine/sdks/rust/runner-protocol/build.rs @@ -83,6 +83,28 @@ mod typescript { String::from_utf8_lossy(&output.stderr), ); } + + // Post-process the generated TypeScript file to replace Node.js assert + post_process_typescript(&src_dir.join("index.ts")); + } + + fn post_process_typescript(file_path: &Path) { + let content = + fs::read_to_string(file_path).expect("Failed to read generated TypeScript file"); + + // Remove Node.js assert import + let content = content.replace("import assert from \"node:assert\"\n", ""); + + // Add custom assert function at the end + let assert_function = r#" +function assert(condition: boolean, message?: string): asserts condition { + if (!condition) throw new Error(message ?? "Assertion failed") +} + +"#; + let content = format!("{}\n{}", content, assert_function); + + fs::write(file_path, content).expect("Failed to write post-processed TypeScript file"); } } diff --git a/engine/sdks/typescript/runner-protocol/src/index.ts b/engine/sdks/typescript/runner-protocol/src/index.ts index 941d1eea03..8c06280a4b 100644 --- a/engine/sdks/typescript/runner-protocol/src/index.ts +++ b/engine/sdks/typescript/runner-protocol/src/index.ts @@ -1,4 +1,3 @@ -import assert from "node:assert" import * as bare from "@bare-ts/lib" const DEFAULT_CONFIG = /* @__PURE__ */ bare.Config({}) @@ -2119,3 +2118,9 @@ export function decodeToServerlessServer(bytes: Uint8Array): ToServerlessServer } return result } + + +function assert(condition: boolean, message?: string): asserts condition { + if (!condition) throw new Error(message ?? "Assertion failed") +} +