Skip to content

@cipherstash/recipher wraps every native method by hand to return a Result — spike returning Result from Rust so native.d.ts is the public type #1801

Done

Description

@coderdan

Background

@cipherstash/recipher is the TypeScript SDK on the Stack Encrypt engine (the Rust crate stack-encrypt, which encrypts and decrypts values). Its Rust half is at languages/typescript/packages/recipher-js: the Node addon recipher-js (crate/) and the value bridge recipher-value-napi (value-napi/), which reads JavaScript values into the engine's value model and builds them back.

No call in the SDK throws. Each returns a Result, a small TypeScript class with Rust's Result methods (isOk, map, andThen, unwrap and the rest).

Problem

The classes Cipher, Keyset and Ciphertext are written in Rust, and napi-rs generates their TypeScript declarations (native.d.ts). But the public API still goes through a hand-written layer in src/cipher.ts and src/binding.ts:

  1. A native method returns a plain object, { ok: true, value } or { ok: false, error }.
  2. adapt replaces each method on the native class with one that wraps that object in a Result. It is told, by a table, which methods exist and whether each is synchronous.
  3. retyped casts the class to interfaces written by hand in cipher.ts, which repeat every method's signature.

So each method is described three times: in Rust, in the adapt table and in an interface. A method added in Rust and left out of the table returns a bare object where callers expect a Result. A type test catches some of this, not all.

Proposal

Find out whether the hand-written layer can go, with the generated native.d.ts as the public type.

  1. Spike: hand the Ok and Err constructors to the addon once, when it loads, so a native method returns a real Result. Check that napi-rs 3 can express the signatures the API needs through its type attributes: a generic Ciphertext<T>, Promise<Result<T, RecipherError>>, and the overloads for one value and an array.
  2. Report what works, what does not, and what it costs (per-call overhead, how readable the generated declarations are).
  3. If it works, remove adapt, retyped and the repeated interfaces. If it only partly works, say which part stays and why.

The Result class itself stays in TypeScript: its methods take functions, which napi-rs cannot generate.

Relationship to other work

Activity

  1. self-assigned this
    on Nov 24, 2026
  2. added
    enhancement
    on Nov 24, 2026
  3. coderdan commented on Nov 24, 2026

    @coderdan
    ContributorAuthor

    Spike result

    It works, with one behaviour lost. Rust can build every Result, and the generated native.d.ts can be the public type. The spike converted every method, records included, on a local branch (spike/recipher-result-from-rust, not pushed). Its tests pass apart from the two cases below.

    What works

    • The signatures. napi-rs 3's ts_type attribute replaces a method's whole signature, and a newline in it gives one overload per line. Generic methods and overloads all generate correctly, for example:
      decrypt<T>(ciphertext: Ciphertext<T>): Promise<Result<T, RecipherError>>
      decrypt<T>(ciphertexts: readonly Ciphertext<T>[]): Promise<Result<T[], RecipherError>>
    • The generic Ciphertext<T>. napi-rs cannot put a type parameter on a class. A small header file declares interface Ciphertext<T = …>, and TypeScript merges it into the generated class. A test confirms Ciphertext<number> is not assignable to Ciphertext<string>.
    • Holding the constructors. The addon keeps Ok, Err and RecipherError per JavaScript environment and calls them when an async method completes. A worker thread gets its own set. Tested.
    • Wrong argument types are still an Err, because every argument is checked in Rust.
    • Cost. A synchronous Ok is about 300 ns where it was about 540 ns. Async calls are unchanged; the round trip to the worker thread dominates.

    What is deleted

    adapt, retyped, settle on the TypeScript side, and the hand-written interfaces.

    File Before After
    src/cipher.ts 313 lines 89
    src/binding.ts 228 lines 116
    native-header.d.ts (new) none 38

    What stays in TypeScript: the Result class, the loader, and stand-in classes for when the native build did not load.

    What is lost

    A method called without its object throws, instead of returning an Err:

    stored.map(cipher.decrypt)   // TypeError: Illegal invocation

    The JavaScript engine makes this check before any Rust runs, and the spike found no napi-rs setting that turns it off. adapt used to catch it. Static methods are not affected.

    A fix was tried as a probe and works: a getter in front of each instance method that returns the method bound to its object. It costs one getter per method and one bind each time the property is read.

    Other costs

    • The signatures are long strings in Rust attributes. Nothing type-checks them until tsc reads the generated file.
    • Type errors a user sees name the class Ciphertext$1<string>, because the bundled declarations hold two things called Ciphertext. Shipping native.d.ts unbundled would probably fix it; not tried.
    • Loading the .node file without the package no longer works, so the binding interface number goes up.

    Decision needed

    Whether "no call throws" has to cover a method called without its object. If not, the layer can simply go. If so, it needs the bound getters, which bring back a smaller version of the per-method repetition this issue set out to remove.

  4. coderdan commented on Nov 25, 2026

    @coderdan
    ContributorAuthor

    Decided: a method called without its object, as in stored.map(cipher.decrypt), throws a TypeError. The JavaScript engine raises it before any Rust runs. Every other call, a wrong or missing argument included, returns or resolves to an Err.

    Implemented in #1774 at a32e13d: Rust returns each Result itself, the generated native.d.ts is the public type, and adapt, settle, retyped and the hand-written interfaces are gone. A test asserts the TypeError, and another that wrong arguments are an Err. #1774 closes this issue when it merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

SDKenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions