Skip to content

feat(recipher-js): walking skeleton of the TypeScript SDK - #1774

Draft
coderdan wants to merge 1 commit into
docs/typescript-sdk-planfrom
feat/recipher-js-skeleton
Draft

coderdan wants to merge 1 commit into
docs/typescript-sdk-planfrom
feat/recipher-js-skeleton

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

This is step 1 of phase 1 of the TypeScript SDK plan (#1747): a walking skeleton of @cipherstash/recipher. A TypeScript program can build a cipher, encrypt one string and decrypt it again on Node, through the Stack Encrypt engine (the Rust crate packages/stack-encrypt). Every call resolves to a Result; none throws.

The package is private and is not published. It takes no changeset.

Changes

Rust: a detached Cargo workspace at languages/typescript/packages/recipher-js, with its own Cargo.lock. The root Cargo.toml excludes it.

  • crates/core (crate recipher-js): the logic. It is the only crate that names an engine type, and it has no JavaScript types, so its tests run without Node. It has Cipher::detect (ZeroKMS, with the credentials the engine finds), Cipher::testing (the engine's deterministic test key source, behind a testing feature), encrypt and decrypt, and Refusal: every error with a kind, code, message, reason and field.
  • crates/node (crate recipher-js-node): the napi-rs 3 addon. It reads a JavaScript value into a frame with vitaminc's JsRef::to_frame, and builds the opened value with JsBuilder::build. It holds no value conversion of its own. It also exports vitaminc's four wrapper classes, ready for step 2.

TypeScript: @cipherstash/recipher.

  • Cipher.create(), cipher.defaultKeyset(), keyset.encrypt(string), cipher.decrypt(ct), keyset.decrypt(ct), Ciphertext.toBytes() and Ciphertext.fromBytes(bytes).
  • Result: the package's own type, with Rust's methods in camelCase. Only unwrap() and expect() throw.
  • RecipherError and the closed ErrorKind set.
  • @cipherstash/recipher/testing: testCipher(), a cipher with no credentials and no network.
  • The loader checks the addon's binding interface number, and returns an Err (not an exception) when the addon is missing or has another number.

Tests and CI.

  • test (run by root pnpm test): the Result and error suites, a test that test and build never reach cargo, and tsc over the type tests. It loads no addon.
  • test:addon: the suites that load the addon. test:cargo: cargo test --locked, cargo fmt --check and clippy.
  • .github/workflows/tests-recipher-js.yml: one Linux job that runs test:cargo, build:debug and test:addon, then checks the build left the tree clean.
  • AGENTS.md (repository layout, Cargo workspaces) and SECURITY.md (package list) name the package.

What the tests prove

  • The bytes are the engine's. A Rust test seals a string through the core crate and opens it with the engine's own typed keyset.decrypt::<String>. Another seals with the engine's keyset.encrypt(String) and opens it through the core crate. So Rust (and Go, which runs the same engine) read what this SDK writes.
  • The engine's futures are Send and 'static. The plan could not check this. The core crate's futures say so in their types, a test asserts it, and napi's spawn_future_with_callback (which requires it) compiles over them.
  • The core crate names no Label or IntoLabel (a test reads its source), and neither crate links eql-bindings (a test runs cargo tree --locked on each).
  • Failures are values. An altered ciphertext, bytes that are not a ciphertext, a value that is not a string, a wrong seed, and Cipher.create() with no credentials each resolve to an Err of the right kind. None quotes the plaintext or the bytes.

Verification

Run locally on macOS (arm64), Rust 1.94.1, Node 24, pnpm 10.33.2:

  • cargo fmt --all --check: passed.
  • cargo clippy --locked --all-targets --all-features -- -D warnings: passed.
  • cargo test --locked --all-features: 16 passed (6 unit, 3 boundary, 7 round-trip).
  • pnpm --filter @cipherstash/recipher build (tsup, ESM and CJS): passed. Both dist/index.js and dist/index.cjs load the addon and round-trip a value, and a cipher from ./testing is an instanceof the root entry's Cipher.
  • pnpm --filter @cipherstash/recipher build:debug: built recipher.node.
  • pnpm --filter @cipherstash/recipher test:addon: 13 passed.
  • pnpm --filter @cipherstash/recipher test (unit suites and tsc with the type tests): 16 passed, typecheck clean.
  • pnpm run code:check: no errors (the warnings are the repository's existing ones; none is in this package).
  • pnpm run test:scripts: 96 files passed.
  • lint:typecheck-scope, lint:package-paths, lint:workflow-cache, lint:eql-pins: passed.

Not run: anything against a live ZeroKMS (no credentials were used). The new workflow has not run on Linux yet; this PR's checks are its first run.

Deviations from the plan

  • No napi CLI. The addon is built by a small script (scripts/build-native.mjs) that runs cargo build and copies the cdylib to recipher.node. @napi-rs/cli 3 brings a large dependency tree (including native cross-toolchain packages) into the lockfile, and step 1 needs none of what it does. So there is no generated native.d.ts; the addon's interface is typed by hand in src/native.ts, and the loader checks that each function it calls exists. Step 10 (the platform packages) is the place to decide on the CLI.
  • A fifth error field and a ninth kind. The plan names kind, code, reason and field. The error also has message, because the engine's message is what a person reads. The plan's eight kinds have no home for "the native build did not load" or "the engine returned something the SDK does not read", so there is a ninth kind, internal.
  • Calls return a Promise, not yet a builder. The plan's builder exists to carry .audit, .withCredential and .signal, which are step 5. A builder that is awaited the same way can replace the Promise then without changing any call site.
  • The test cipher is in the debug build only. build:debug turns on the testing cargo feature; build:native (release) does not. How a published package offers @cipherstash/recipher/testing (a second build, as Go's encrypttest has, or a feature in the one build) is left to step 5 or step 10.
  • The step-1 crates name wasm nowhere. The plan lists three crates; the wasm crate is step 6, and no placeholder is created.

Needs from the engine

Nothing for this step.

Related

Refs #1747 (the plan). The next step's PR is based on this branch.

Add @cipherstash/recipher, the TypeScript SDK on the Stack Encrypt
engine, with the first call through it: one string encrypted and
decrypted on Node, with no context.

The Rust is a detached Cargo workspace. `recipher-js` holds the logic,
names the engine's types and has no JavaScript types, so its tests run
without Node. `recipher-js-node` is the napi-rs 3 addon: it reads and
builds JavaScript values through vitaminc-aead-napi 0.10 and calls the
core crate. The engine's futures are Send and 'static, and run on
napi's tokio runtime.

Every call resolves to the package's own Result, with Rust's methods;
none throws and no promise rejects. A test cipher over the engine's
deterministic key source (@cipherstash/recipher/testing) runs every
test offline.

The ciphertext is the engine's: a Rust test opens what the core seals
with the engine's typed keyset.decrypt::<String>, and the core opens
what the engine sealed. Tests fail if the core crate names Label or
IntoLabel, or if either crate links eql-bindings.

The package is private and takes no changeset. Its build and test
scripts never run cargo; tests-recipher-js.yml runs the cargo checks
and the suites that load the addon, on Linux.
@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2232e04

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

@github-actions

Copy link
Copy Markdown
Contributor

Mutation testing (cargo-mutants, --in-diff, stack-auth + stack-encrypt + stack-encrypt-jsonpath)

No mutants were generated for the changed lines.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant