Skip to content

spike(recipher-js): the value bridge declares its own TypeScript types - #1829

Draft
coderdan wants to merge 1 commit into
feat/recipher-js-valuesfrom
spike/recipher-bridge-types
Draft

coderdan wants to merge 1 commit into
feat/recipher-js-valuesfrom
spike/recipher-bridge-types

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

A prototype of step 1 from the review of #1775. The value bridge (value-napi) now has TypeScript declarations of its own, beside its source. The SDK imports them, and no longer writes its own copies of the wrapper classes or the type of each kind.

This is a draft to judge the shape. It is not meant to merge as it is. It is based on feat/recipher-js-values.

What moved

Two new files in value-napi/src/:

  • wrappers.d.ts declares the four classes that wrappers.js defines: VcDate, VcTimestamp, VcDecimal and VcUuid. Each has the constructor, value, toJSON() and toString(). It also declares the class constructor type (WrapperConstructor), the classes by name (WrapperClasses), and the object wrappers.js returns, including its isPlain check (Wrappers).
  • values.d.ts declares the bridge's value mapping:
    • Scalar: every value JsRef reads as one scalar, including undefined and VcUuid.
    • Opened<V>: the type JsBuilder builds back for each.
    • ScalarKinds: the type each of the 18 scalar kinds decrypts to, by the engine's name for the kind.

The SDK now uses them:

  • native-header.d.ts declares the four classes the addon exports, typed from WrapperClasses. So typeof Native includes them, and src/values.ts reads them as binding.value[name]. Before, it used Reflect.get and a cast to a type the caller chose.
  • src/value-types.ts keeps only what the SDK decides:
    • Scalar: the bridge's union minus undefined and VcUuid, which encrypt refuses.
    • Opened<V>: a JSON document, or else the bridge's Opened<V>.
    • The JSON types, which belong to the addon's JSON conversion until step 2.
  • src/kinds.ts keeps the names, because kinds.boolean and { name: 'bool' } are the SDK's API. It takes each kind's type from ScalarKinds, and adds json.
  • src/values.ts re-exports WrapperConstructor from the bridge. It still has one type alias per class, because TypeScript cannot re-export a type under a name the module also gives a value. Each alias is one line.
  • src/binding.ts: the list of names the loader checks is now type-checked against the declared exports (satisfies).

Neither new file loads anything. values.d.ts imports only from wrappers.d.ts. So the header still imports nothing that loads the addon, and build passes.

Line counts

File Before After
src/value-types.ts 127 73
src/values.ts 134 119
src/kinds.ts 52 61
native-header.d.ts 34 44
value-napi/src/wrappers.d.ts none 80
value-napi/src/values.d.ts none 90
SDK files above, together 347 297
SDK files and bridge files 347 467

kinds.ts grew by 9 lines. Each line no longer writes its type (kind<'int32', bigint>('int32') is now kind('int32')), but the lookup type and the reformatted kind signature add lines. The total grew because the bridge files declare more than the SDK's copies did:

  • undefined and VcUuid in the mapping;
  • toString();
  • the object wrappers.js returns.

They also have fuller documentation.

The tests add 115 lines (__tests__/unit/bridge-declarations.test.ts), 35 lines of Rust, and 17 lines of type tests.

How drift is caught

  • wrappers.d.ts against wrappers.js. __tests__/unit/bridge-declarations.test.ts evaluates wrappers.js with Node's vm, as the bridge evaluates it. It checks:

    • the script returns exactly the declared classes and isPlain;
    • each class has exactly the declared members and Node's inspect hook, and nothing else;
    • value is an own, enumerable, read-only string;
    • toJSON() and toString() return the text;
    • a text that is not a string throws a TypeError.

    Type-level check lines tie the member list to the declarations. It runs in test, so it needs no Rust. I added a valueOf() to wrappers.js to check: the test failed for all four classes.

  • values.d.ts against the engine. the_bridge_declares_a_type_for_every_scalar_kind, in crate/src/ciphertext.rs, reads ScalarKinds and compares its names with ValueKind::ALL. It leaves out array, object and json. It lives in the addon crate because the bridge does not depend on the engine. I removed int128 to check: the Rust test failed, and so did the typecheck, at kind('int128') in kinds.ts.

  • kinds against values.d.ts. A type test checks that kinds names every kind in ScalarKinds and json, and no other.

The kind names: can they come from one source?

No single source holds both halves. The names are the engine's (ValueKind::name). The type for each kind is the bridge's behaviour (JsBuilder), and no Rust table states it. vitaminc has no list of kind names either. Generating ScalarKinds from ValueKind::ALL at build time would need a new Rust table that maps each kind to TypeScript text. That table would be hand-written too, and would need a check for stale generated output, like the one native.d.ts has. A hand-written list with a drift test is the honest answer. The Rust test fails when the engine's kinds change, and the typecheck fails when kinds and the bridge disagree.

What no test checks: whether the TypeScript type declared for a kind is what JsBuilder really builds. The bridge's tests/kind_inventory.cjs checks what JsBuilder builds for each scalar, but it does not read values.d.ts.

What changed in the public types

I compared the bundled declarations against a build of the base branch:

  • Each wrapper instance now declares toString(): string. The class has had it all along.
  • Scalar now shows as Exclude<Scalar, undefined | VcUuid> over the bridge's union, rather than as a written-out union. It is the same set of types, but harder to read on hover.
  • The bundler renames the bridge's interfaces to VcDate$1 and so on, because the SDK has a value and a type named VcDate. The public names do not change.

No type test was changed. I added these:

  • type-tests/native.ts: the exported classes are the ones the header declares, and are WrapperConstructors.
  • type-tests/values.ts: kinds covers ScalarKinds and json.

No runtime behaviour or recipher_js::… error code changed.

What does not work, or is not done

  • The bridge files are not checked by Biome. biome.json excludes value-napi/. I ran them through Biome 2.6.4 on standard input as if they were in src/: they need no formatting and have no lint findings. CI will not check them.
  • The public declarations include the bridge files' header comments. These are notes for maintainers, such as the name of the test.
  • The stand-ins now differ from the declarations, and nothing tests them. The stand-in for a wrapper class has no toString(): it returns [object Object]. It has no inspect hook. It accepts a text that is not a string, through String(text). The drift test covers the real classes only.
  • OpenedJson and JsonValue stay in the SDK, because the JSON conversion is still in the addon (crate/src/json.rs). They would move with step 2.

The stand-ins

Stand-in code left in src/ (972 lines in all):

  • src/cipher.ts: about 39 lines. missing() is 30 lines, classesOf is 7 lines, and the Statics type is 2.
  • src/values.ts: about 21 lines. The stand-in class in wrapperClass is 13 lines, standInJson is 5, and choosing json is 3.

That is about 60 lines, or 6% of src/. The declarations make the wrapper stand-in a little stricter: its cast is now to the named class's type, not to any type the caller chose. They do not make it shorter. It still needs a cast, because only the real class has the type-only marker that keeps the four classes apart.

One option this opens up, not taken here: when the addon does not load, the SDK could evaluate wrappers.js itself, as the new test does, and export the real classes. The four wrapper stand-ins would go. But wrappers.js would then have to ship in dist (for example, as text bundled by tsup), and the classes would behave differently when the addon fails to load. That is the stand-in decision from the review.

What I would do next

  1. Decide whether a .d.ts in value-napi/ is the right home, given that Biome skips the folder. One choice is to stop excluding just the *.d.ts files.
  2. Step 2: move the JSON number rules into the bridge. OpenedJson and JsonValue would then move into values.d.ts too, and Opened<V> in the SDK would be the bridge's alone.
  3. Decide on the stand-ins. If they go, wrapperClass and json in values.ts become one-line re-exports from the binding.

Verification

All run locally on macOS (arm64) with Rust 1.94.1, Node LTS and pnpm 10.33.2:

  • cargo fmt --all --check: pass.
  • cargo clippy --locked --all-targets --all-features -- -D warnings: pass.
  • cargo test --locked --all-features: pass. That is 32 addon unit tests (1 new), 2 boundary tests, 33 bridge unit tests and the bridge's Node conformance test.
  • fmt and clippy on value-napi/tests/addon by its manifest, with --target-dir target/napi-test-addon: pass.
  • build (tsup, with the bundled declarations): pass.
  • build:debug: pass. Afterwards the tree changed only in native.d.ts, which follows the header.
  • test (vitest and tsc with the type tests): 48 tests, 22 of them new, all pass, and the typecheck is clean.
  • test:addon: 57 tests pass.
  • Biome 2.6.4 check on the changed files: clean. It checked 8 files. native.d.ts and the two value-napi/ files are excluded.
  • Script tests (scripts/vitest.config.mjs): 1616 pass. 2 files fail, bench-index-expressions and migration-fixture-client, which are known to fail in this sandbox.

Related

…the bridge

The SDK restated the value bridge's wrapper classes and value mapping by
hand: an interface per class in src/value-types.ts, a constructor type and
a Reflect.get per class in src/values.ts, and the type of each kind in
src/kinds.ts. Nothing kept those copies in step with wrappers.js,
JsBuilder or the engine's kinds.

The bridge now has two declaration files beside its source.
wrappers.d.ts declares the classes that wrappers.js defines. values.d.ts
declares the scalars JsRef reads, the type JsBuilder builds for each, and
the type of each scalar kind. The header declares the classes the addon
exports, so the loader reads them without a cast. The SDK imports the
rest, and adds only what it decides: which scalars encrypt takes, and
JSON documents.

A unit test evaluates wrappers.js and checks it against wrappers.d.ts. A
Rust test checks the kinds in values.d.ts against the engine's
ValueKind::ALL.

No runtime behaviour or error code changes. The public types change in
two ways: a wrapper instance now declares the toString() it has, and
Scalar is written as the bridge's union minus undefined and VcUuid.
@coderabbitai

coderabbitai Bot commented Nov 25, 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

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: fc7fdf0

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

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