You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@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
@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:
A native method returns a plain object, { ok: true, value } or { ok: false, error }.
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.
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.
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.
Report what works, what does not, and what it costs (per-call overhead, how readable the generated declarations are).
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.
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:
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:
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.
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.
Activity
coderdan commentedon Nov 24, 2026
Spike result
It works, with one behaviour lost. Rust can build every
Result, and the generatednative.d.tscan 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
ts_typeattribute 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:Ciphertext<T>. napi-rs cannot put a type parameter on a class. A small header file declaresinterface Ciphertext<T = …>, and TypeScript merges it into the generated class. A test confirmsCiphertext<number>is not assignable toCiphertext<string>.Ok,ErrandRecipherErrorper JavaScript environment and calls them when an async method completes. A worker thread gets its own set. Tested.Err, because every argument is checked in Rust.Okis 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,settleon the TypeScript side, and the hand-written interfaces.src/cipher.tssrc/binding.tsnative-header.d.ts(new)What stays in TypeScript: the
Resultclass, 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:The JavaScript engine makes this check before any Rust runs, and the spike found no napi-rs setting that turns it off.
adaptused 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
bindeach time the property is read.Other costs
tscreads the generated file.Ciphertext$1<string>, because the bundled declarations hold two things calledCiphertext. Shippingnative.d.tsunbundled would probably fix it; not tried..nodefile 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.
docs(plans): record this week's error and bridge decisions, and #1801 …
docs(plans): record this week's error and bridge decisions, and #1801 …
refactor(recipher-js): native methods return the Result themselves
docs(plans): record #1801 as decided in the TypeScript SDK plan
coderdan commentedon Nov 25, 2026
Decided: a method called without its object, as in
stored.map(cipher.decrypt), throws aTypeError. The JavaScript engine raises it before any Rust runs. Every other call, a wrong or missing argument included, returns or resolves to anErr.Implemented in #1774 at a32e13d: Rust returns each
Resultitself, the generatednative.d.tsis the public type, andadapt,settle,retypedand the hand-written interfaces are gone. A test asserts theTypeError, and another that wrong arguments are anErr. #1774 closes this issue when it merges.