Backlog
Labels
Milestone
Description
Background
@cipherstash/recipher is the TypeScript SDK on Stack Encrypt, in languages/typescript/packages/recipher-js. Every call returns, or resolves to, a Result, with the methods of Rust's Result in camelCase. Only unwrap and expect throw (README, "Results, not exceptions").
Problem
An async call needs parentheses before any Result method, because await applies to the whole expression after it:
const cipher = (await Cipher.create()).unwrap()
const stored = (await keyset.encrypt(user, { record: UserRecord })).unwrap()Rust chains .await like a method. TypeScript has no such form, so the Rust pattern needs the parentheses here. The parentheses make each call harder to read at a glance.
Proposal
An async call returns a pending result: an object with then, and the Result methods that do not need the value before it arrives. The neverthrow library calls this ResultAsync.
const cipher = await Cipher.create().unwrap()
const stored = await keyset.encrypt(user, { record: UserRecord }).unwrap()
const age = await keyset.encrypt(34n, { kind: kinds.uint64 }).map(toBytes).unwrapOr(empty)
const result = await keyset.encrypt(user, { record: UserRecord }) // still a Result- A thenable class, not a
Promisesubclass.awaitandPromise.allaccept any thenable, and a subclass ofPromisechanges whatthenreturns. - The methods:
map,mapErr,andThen,orElse,unwrap,unwrapOr,expect, andmatch. Each returns a pending result or a promise.isOkandisErrare left out, because they cannot be answered before the value arrives. - The rule does not change.
await xresolves to aResultand never rejects. Onlyunwrapandexpectreject, on anErr, as they throw today. - Synchronous calls keep returning a plain
Result:Ciphertext.fromBytes,RcDate.from, and the other factories. - The language binding returns it. No TypeScript code wraps a native method: each method returns its
Resultitself. The package's TypeScript hands the binding the pending-result class when it loads the binding, as it hands overOkandErrtoday. The methods that set up a call (.audit,.withCredential,.signal) are on the same object, and come before anyResultmethod. - README, examples, and type tests are updated, including the "Results, not exceptions" section.
Relationship to other work
- feat(recipher-js): records through the engine's Planned #1776: changes the record API to
keyset.encrypt(row, { record }). This change comes after it, so the two do not conflict.
Activity
docs(plans): every async call returns a pending result, so examples n…