Repository navigation
Conversation
…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.
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true |
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.tsdeclares the four classes thatwrappers.jsdefines:VcDate,VcTimestamp,VcDecimalandVcUuid. Each has the constructor,value,toJSON()andtoString(). It also declares the class constructor type (WrapperConstructor), the classes by name (WrapperClasses), and the objectwrappers.jsreturns, including itsisPlaincheck (Wrappers).values.d.tsdeclares the bridge's value mapping:Scalar: every valueJsRefreads as one scalar, includingundefinedandVcUuid.Opened<V>: the typeJsBuilderbuilds 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.tsdeclares the four classes the addon exports, typed fromWrapperClasses. Sotypeof Nativeincludes them, andsrc/values.tsreads them asbinding.value[name]. Before, it usedReflect.getand a cast to a type the caller chose.src/value-types.tskeeps only what the SDK decides:Scalar: the bridge's union minusundefinedandVcUuid, whichencryptrefuses.Opened<V>: a JSON document, or else the bridge'sOpened<V>.src/kinds.tskeeps the names, becausekinds.booleanand{ name: 'bool' }are the SDK's API. It takes each kind's type fromScalarKinds, and addsjson.src/values.tsre-exportsWrapperConstructorfrom 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.tsimports only fromwrappers.d.ts. So the header still imports nothing that loads the addon, andbuildpasses.Line counts
src/value-types.tssrc/values.tssrc/kinds.tsnative-header.d.tsvalue-napi/src/wrappers.d.tsvalue-napi/src/values.d.tskinds.tsgrew by 9 lines. Each line no longer writes its type (kind<'int32', bigint>('int32')is nowkind('int32')), but the lookup type and the reformattedkindsignature add lines. The total grew because the bridge files declare more than the SDK's copies did:undefinedandVcUuidin the mapping;toString();wrappers.jsreturns.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.tsagainstwrappers.js.__tests__/unit/bridge-declarations.test.tsevaluateswrappers.jswith Node'svm, as the bridge evaluates it. It checks:isPlain;inspecthook, and nothing else;valueis an own, enumerable, read-only string;toJSON()andtoString()return the text;TypeError.Type-level
checklines tie the member list to the declarations. It runs intest, so it needs no Rust. I added avalueOf()towrappers.jsto check: the test failed for all four classes.values.d.tsagainst the engine.the_bridge_declares_a_type_for_every_scalar_kind, incrate/src/ciphertext.rs, readsScalarKindsand compares its names withValueKind::ALL. It leaves outarray,objectandjson. It lives in the addon crate because the bridge does not depend on the engine. I removedint128to check: the Rust test failed, and so did the typecheck, atkind('int128')inkinds.ts.kindsagainstvalues.d.ts. A type test checks thatkindsnames every kind inScalarKindsandjson, 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. GeneratingScalarKindsfromValueKind::ALLat 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 onenative.d.tshas. 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 whenkindsand the bridge disagree.What no test checks: whether the TypeScript type declared for a kind is what
JsBuilderreally builds. The bridge'stests/kind_inventory.cjschecks whatJsBuilderbuilds for each scalar, but it does not readvalues.d.ts.What changed in the public types
I compared the bundled declarations against a build of the base branch:
toString(): string. The class has had it all along.Scalarnow shows asExclude<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.VcDate$1and so on, because the SDK has a value and a type namedVcDate. 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 areWrapperConstructors.type-tests/values.ts:kindscoversScalarKindsandjson.No runtime behaviour or
recipher_js::…error code changed.What does not work, or is not done
biome.jsonexcludesvalue-napi/. I ran them through Biome 2.6.4 on standard input as if they were insrc/: they need no formatting and have no lint findings. CI will not check them.toString(): it returns[object Object]. It has noinspecthook. It accepts a text that is not a string, throughString(text). The drift test covers the real classes only.OpenedJsonandJsonValuestay 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,classesOfis 7 lines, and theStaticstype is 2.src/values.ts: about 21 lines. The stand-in class inwrapperClassis 13 lines,standInJsonis 5, and choosingjsonis 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.jsitself, as the new test does, and export the real classes. The four wrapper stand-ins would go. Butwrappers.jswould then have to ship indist(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
.d.tsinvalue-napi/is the right home, given that Biome skips the folder. One choice is to stop excluding just the*.d.tsfiles.OpenedJsonandJsonValuewould then move intovalues.d.tstoo, andOpened<V>in the SDK would be the bridge's alone.wrapperClassandjsoninvalues.tsbecome 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.value-napi/tests/addonby 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 innative.d.ts, which follows the header.test(vitest andtscwith the type tests): 48 tests, 22 of them new, all pass, and the typecheck is clean.test:addon: 57 tests pass.checkon the changed files: clean. It checked 8 files.native.d.tsand the twovalue-napi/files are excluded.scripts/vitest.config.mjs): 1616 pass. 2 files fail,bench-index-expressionsandmigration-fixture-client, which are known to fail in this sandbox.Related