Skip to content

@cipherstash/recipher: TypeScript examples in the docs are never compiled or run — add doc tests #1826

Todo

Description

@coderdan

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

  1. Pick one convention for examples. Tag every TypeScript example ts. Give it an info-string marker for an example that must not run, for example ts no-run, the way Rust uses no_run: an example that needs real credentials or a network is still type-checked, but not run.

  2. Extract the examples. A script pulls every ts fence 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.

  3. Type-check them. Write each example to a file that imports @cipherstash/recipher (and @cipherstash/recipher/testing where needed), with the setup it assumes (a cipher, a keyset) supplied by a small preamble. Compile them with tsc against the package's types, as type-tests/ does today.

  4. Run them. Run each example not marked no-run with vitest, against the debug addon and the deterministic test cipher. An example that writes unwrap() on an Err then fails, as a Rust doc test would.

  5. Wire it into CI in tests-recipher-js.yml, which runs every check for the package, and into the package's test:addon. Running the examples needs the addon. Type-checking them alone needs only the types, so it can go in test.

  6. 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 ts example in a doc comment that does not compile, or that fails when run, fails CI, naming the file and line it came from.
  • no-run examples are type-checked but not run.
  • The convention is written down where the package's contributors will find it, in its README or AGENTS.md entry.

Relationship to other work

Activity

  1. added
    enhancement
    JavaScript
    on Nov 25, 2026
  2. self-assigned this
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

JavaScriptSDKenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions