Repository navigation
Description
Background
@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.
When the value bridge refuses a value (a Map, an array with a hole, a bigint too large to store), it has to tell the addon why, so the addon can give the caller an error with a stable code.
Problem
The bridge reports a refusal as a Node-API status and a sentence of free text (JsFault, value-napi/src/error.rs). The addon then works out which refusal it was by comparing the start of that sentence against a table (WALK_REFUSALS in crate/src/js.rs):
("only plain objects", "recipher::not_plain", "a Map, a Set, a class instance ..."),
("sparse arrays", "recipher::sparse_array", "an array has a hole ..."),A second table (VITAMINC_CODES) repeats the six ERR_… codes the bridge already defines in ConversionError, to attach the SDK's own message to each.
So one fact, which refusal happened, is written three times: in the bridge's text, in the addon's table of text prefixes, and in the addon's table of codes. If someone rewords a message in the bridge, the addon stops recognising it and reports the generic recipher::value. No test fails unless it covers that exact refusal.
This was unavoidable while the bridge was a crate from another repository. It lives here now (#1794), so the two sides can share a type.
Proposal
- Give the bridge one error enum for every refusal of a value, deriving
miette::Diagnosticso each variant has a code and a message, as the engine's errors do. It covers the walk's refusals (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 the six scalar refusals inConversionError. - The addon reads the variant. Delete
WALK_REFUSALS,VITAMINC_CODESand the matching on message text incrate/src/js.rs. - Keep the rule that a thrown JavaScript error's own message is never passed on: it can quote the value.
- A test builds one of every variant and checks each has a code of the right form, as
packages/stack-encrypt/src/codes.rsdoes.
The codes a caller sees should stay the same unless there is a reason to change one. If the six ERR_… codes become recipher::… names, say so in the pull request.
Relationship to other work
- From the review of feat(recipher-js): walking skeleton of the TypeScript SDK #1774.
- chore(recipher-js): the JavaScript value bridge lives beside the addon #1794 moved the bridge here.
- Engine errors have codes but no documentation links — decide the URL format and give every code a page #1790 is about documentation pages for error codes, and asks what the
ERR_…codes should be called.
Activity
refactor(recipher-js): the value bridge says which rule a refused val…
refactor(recipher-js): the value bridge says which rule a refused val…
refactor(recipher-js): the value bridge says which rule a refused val…
refactor(recipher-js): the value bridge says which rule a refused val…