Skip to content

refactor(recipher-js): the value bridge says which rule a refused value broke - #1805

Closed
coderdan wants to merge 8 commits into
chore/recipher-value-napifrom
refactor/recipher-typed-refusals
Closed

coderdan wants to merge 8 commits into
chore/recipher-value-napifrom
refactor/recipher-typed-refusals

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

Closes #1799. On top of #1794.

When the value bridge (recipher-value-napi) refuses a JavaScript value, it now says which rule the value broke as a typed value, so the addon no longer works it out from the wording of a message.

Before, a refusal was a Node-API status and a sentence. The addon compared the start of the sentence against a table to pick a code, and a second table repeated the bridge's six scalar codes. A reworded message in the bridge would have silently turned a specific code into the generic recipher::value.

The codes a caller sees are unchanged. The package is private, so there is no changeset.

Changes

In the bridge (value-napi/):

  • A new Refusal enum, one variant for each rule: not a plain object, a reserved property name, nested too deeply, a sparse array, bytes of the wrong kind, a type with no value, an object that changed while it was read, an unpaired surrogate, and a refused scalar. It derives miette::Diagnostic, so each variant has a code and a message, and two have help.
  • ConversionError (the six scalar refusals) derives Diagnostic too, with the ERR_… codes it already had.
  • JsFault carries an optional Refusal. JsFault::refusal() returns it. It is None for a failure of Node-API itself and for an exception the application threw.
  • JsRef::to_frame and JsBuilder::build return Result<_, JsFault> instead of napi::Result, so the variant reaches the caller. A JsFault still converts to a napi error with ?, and the text JavaScript sees when the bridge is used directly is unchanged, so the bridge's four Node suites pass untouched.
  • The helpers that raise a refusal return JsFault instead of a napi error.

In the addon (crate/src/js.rs):

  • refused reads fault.refusal() and converts the diagnostic. WALK_REFUSALS, VITAMINC_CODES and vitaminc_code (which read code off a thrown error) are deleted: about 110 lines.
  • not_built matches Refusal::ReservedKey instead of the start of a message.
  • An exception pending in JavaScript is still cleared and never copied.

What changes for a caller

  • The codes are the same.
  • The messages for the six scalar refusals are now the bridge's (invalid UUID) where they were the addon's own (a VcUuid's text is not a UUID). The other messages are the addon's old ones, moved into the enum.
  • recipher::not_plain and recipher::sparse_array now have help.

Verification

Run locally:

  • cargo test --locked --all-features: recipher-js 39 and 4; recipher-value-napi 33 (two new: every refusal has a code of its own, and a fault says which refusal it is); its Node suite against the test addon passed.
  • cargo fmt --all --check and clippy with -D warnings: clean.
  • The addon suite: 76 passed with no test changed, which is the evidence that the codes held. Unit suite: 29. tsc --noEmit: clean. native.d.ts unchanged.

Review notes

Related

One napi-rs 3 crate, recipher-js, defines the Cipher, Keyset and
Ciphertext classes and runs encrypt and decrypt of one string through the
engine. @napi-rs/cli v3 builds it and writes its TypeScript declarations
to the committed native.d.ts. The TypeScript holds Result, RecipherError,
the typed signatures over the native methods, and the adapter that turns
each native outcome into a Result, so no call throws.
keyset.encrypt takes every value in vitaminc's JavaScript mapping, an
array of them in one key request, and a JSON document made with json(..),
a native class that reads the document when it is made. Ciphertext
carries the kind it opens as, and Ciphertext.fromBytes(bytes, kind) reads
stored bytes as a kind, kinds.json among them. vitaminc's refusals each
get a code of their own. The binding interface is 2.
record(..), encrypt(..) and passthrough() declare a record as plain data.
The crate builds the engine's plan from it with the engine's own types,
once for each declaration, and encrypts and decrypts rows, and one field
or an array of one field, through dynamic::Planned. A refusal names the
record and the field. A decrypted row's inspect hides its values. The
binding interface is 3.
…causes

A RecipherError held the engine's code and a message with the help
appended to it, and two of the structured fields. It now holds the whole
diagnostic, as the Go SDK's does: help and url as properties of their own,
every structured field, and the cause chain. A cause from another library
is named as one and its message is never copied. A test lists every code
the SDK gives an error of its own and checks each one's form.
miette's Diagnostic has a severity, and the Go SDK's Diagnostic carries
it. A RecipherError now does too: error unless the engine's diagnostic
states warning or advice.
fields holds every structured field of the engine's error, the reason
token among them, so the typed reason property was the same string a
second way. It is removed. field and record stay: the SDK names them
where the engine does not.
recipher-value-napi is vitaminc-aead-napi 0.10.0, copied from
cipherstash/vitaminc at b3f69b12 and renamed, as an unpublished member of
the recipher-js workspace at value-napi/. The addon depends on it by path
in place of the crates.io crate, so the value mapping can change in the
pull request that needs the change.

The source files are unchanged. The manifest, the README, the docs.rs
links, the test addon's dependencies and two test paths are edited for
the new home. vitaminc's value conformance corpus comes with it, at the
same commit.
…ue broke

The bridge reported a refusal as a Node-API status and a sentence, and
the addon found out which refusal it was by matching the start of the
sentence against a table, with a second table repeating the six scalar
codes. A reworded message would have turned a specific code into the
generic one with no test failing.

The bridge now has a Refusal enum, a miette diagnostic with a code and a
message for each rule, and JsFault carries it. JsRef::to_frame and
JsBuilder::build return the JsFault. The addon reads the variant; both
tables and the code that read `code` off a thrown error are gone. The
codes a caller sees are unchanged.

Closes #1799
@coderdan coderdan self-assigned this Nov 24, 2026
@coderabbitai

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

@coderdan

Copy link
Copy Markdown
Contributor Author

Folded into the rewritten stack: the bridge half is the second commit of #1794, and the addon half is in #1774, #1775 and #1776. #1799 is now closed by #1794. Previous head: 04652cb. The branch is kept.

@coderdan coderdan closed this Nov 24, 2026
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