English | 中文
A test double that plays the whole show.
Stunt Double is a Rust-based mock server for integration testing against real external dependencies. It reads upstream APIs, transforms data with built-in JavaScript, returns files and binary responses, and runs without host runtime dependencies.
Slices T1–T13 are implemented: T1–T7 implement the runtime, T8 freezes the v1 configuration, CLI, and ctx API contracts and defines the public apiVersion 1 type definitions, and T9 configures the release pipeline whose contract requires three-platform archives, checksums, attestations, an SBOM, the .d.ts asset, and a GHCR image covering linux/amd64 and linux/arm64 for each release; T10–T12 add graceful shutdown, rooted file reads and streamed local file responses, and request-scoped multipart uploads; T13 runs the document demo fixture fully offline with local manifest, local download, and upload routes. stuntdouble serve and stuntdouble validate run from a TOML configuration; matched routes execute JavaScript in the embedded Boa runtime with a host-injected ctx (apiVersion, request, http.get, http.pipe, file.readText, file.readBytes, file.stream, file.writeText, file.writeBytes, respond, log, env); ctx.http.get performs allowlisted upstream HTTP calls, and ctx.http.pipe streams an allowlisted upstream body to the client with Range passthrough. The post-v1 v2 slice adds process-local ctx.state, shared by all Route scripts in one running serve instance, with atomic one-time consumption and replay prevention with per-key TTL; the host independently enforces key, depth, value, and capacity limits using its own compact JSON encoding, and restart clears the state. Unmatched paths return 404 not_found, and a configured [fallback] can instead answer unknown paths and method mismatches with a scripted 404/405 (the host injects Allow for 405); script failures return 500 script_error or script_no_response; uncaught upstream transport failures return 502 upstream_unreachable. Script-supplied response headers are validated fail-closed; malformed names or values answer script_error, and ctx.http.pipe rejects them before contacting the upstream. Each matched Route runs in a fresh script-worker process, so a crash, protocol failure, or deadline cannot take down another route. Scripts also run under a host-owned resource envelope (reply deadline, request read deadline, bounded fail-fast script-worker pool, loop-iteration backstop, recursion/VM-stack limits, and a sandbox.script_memory_limit_mb (default 256 MiB, minimum 64 MiB) memory bound applied as RLIMIT_AS on Linux and as a Job Object commit cap on Windows), and an engine panic answers 500 script_error; Boa 0.22 exposes no heap metric or interrupt hook, so macOS keeps process isolation and deadline kill without a hard memory bound in this slice (SECURITY.md). The document manifest and PDF download demo scenarios run from the in-repo fixture in demo/, which also serves the offline file routes. Every request also writes one structured log line with the upstream call chain, body sizes, and allowlisted headers, and serve --verbose adds a stable detail class to 500/502 responses without exposing stack traces, upstream bodies, or internal addresses. T10 adds signal-driven shutdown: the first Ctrl-C/SIGINT or Unix SIGTERM drains in-flight requests and exits 0; a second signal observed after the shutdown starts forces exit 130/143 (standard signals are not queued, so back-to-back signals may coalesce). Rooted ctx.file reads and streamed local file responses are implemented (T11), T12 parses multipart uploads before the script into request-scoped temporary storage exposed as ctx.request.files, and opt-in ctx.file.writeText / ctx.file.writeBytes atomically persist artifact files when [files] writable = true (ADR 0022).
ctx API, and CLI.ctx API type definitions — apiVersion 1 source types.Dual-licensed under MIT OR Apache-2.0, at your option.