Repository navigation
Description
Background
@cipherstash/recipher is the TypeScript SDK on the Stack Encrypt engine. Its classes (Cipher, Keyset, Ciphertext) are written in Rust, in languages/typescript/packages/recipher-js/crate/, with napi-rs. napi-rs generates their TypeScript declarations into the committed native.d.ts. Once #1774 lands, that file is the package's public type (#1801).
A Rust doc comment (///) on a #[napi] item is copied word for word into native.d.ts as a JSDoc block, code fences included. TypeDoc then reads it. So the Rust doc comment is the SDK's published documentation, and a ts code fence in it is an example users read.
In Rust, a doc example is a test: cargo test --doc compiles and runs it, so an example that stops matching the API fails CI. TypeScript has nothing like that here. Rustdoc ignores a ts or typescript fence completely. This was checked with a small crate: such a block is neither compiled nor counted as an ignored test, cargo doc -D warnings passes, and the HTML shows it as <pre class="language-ts"> without highlighting.
Problem
Nothing checks the TypeScript examples in the SDK's documentation. An example can name a method that was renamed, pass an argument of the wrong type, or show a result that is no longer returned, and every check stays green. Users copy examples, so a stale one is a bug report waiting to happen, or worse, code that compiles against an older version and misleads.
The same applies to the TypeScript examples in the package README and in src/ module docs, such as the example at the top of src/index.ts.
Proposal
-
Pick one convention for examples. Tag every TypeScript example
ts. Give it an info-string marker for an example that must not run, for examplets no-run, the way Rust usesno_run: an example that needs real credentials or a network is still type-checked, but not run. -
Extract the examples. A script pulls every
tsfence out of:native.d.ts, which is exactly the text users read in TypeDoc;- the TypeScript under
src/; - the package README.
Each example records its source and line, so a failure points at the doc comment to fix. That is the Rust source, for an example that came from
native.d.ts. -
Type-check them. Write each example to a file that imports
@cipherstash/recipher(and@cipherstash/recipher/testingwhere needed), with the setup it assumes (acipher, akeyset) supplied by a small preamble. Compile them withtscagainst the package's types, astype-tests/does today. -
Run them. Run each example not marked
no-runwith vitest, against the debug addon and the deterministic test cipher. An example that writesunwrap()on anErrthen fails, as a Rust doc test would. -
Wire it into CI in
tests-recipher-js.yml, which runs every check for the package, and into the package'stest:addon. Running the examples needs the addon. Type-checking them alone needs only the types, so it can go intest. -
Interim step: steps 2 and 3 (type-checking alone) are cheap and catch most drift. They can ship first. Running the examples follows.
Done when
- A
tsexample in a doc comment that does not compile, or that fails when run, fails CI, naming the file and line it came from. no-runexamples are type-checked but not run.- The convention is written down where the package's contributors will find it, in its README or
AGENTS.mdentry.
Relationship to other work
- feat(recipher-js): walking skeleton of the TypeScript SDK #1774 makes
native.d.tsthe public type, which is what makes the Rust doc comments the published docs. @cipherstash/recipherwraps every native method by hand to return aResult— spike returningResultfrom Rust sonative.d.tsis the public type #1801 is the decision behind that.- TypeScript SDK: add fuzzing, mutation testing and the CRAP gate once the API settles, not with the first pull requests #1824 schedules the slower quality gates. This check is cheap enough to add earlier.
Activity