Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 85 additions & 31 deletions docs/plans/2026-11-22-recipher-typescript-sdk.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# `@cipherstash/recipher`: the TypeScript SDK on Stack Encrypt

Date: 2026-11-22. Revised 2026-11-23: the SDK is a general-purpose core
with EQL as an extension, and the work is in three phases. Status:
proposed. Nothing in this plan is built.
with EQL as an extension, and the work is in three phases. Revised
2026-11-29: the value rules now match the code of #1775 and #1996.

Status: in progress. Step 1 of the build order is built, in #1774. Steps
2 and 3 and the UUID are in open pull requests: #1775, #1776, and #1996.

This plan says how to build the TypeScript SDK on the Stack Encrypt engine
(`packages/stack-encrypt`). It is one new npm package and an addition to
Expand Down Expand Up @@ -379,7 +382,7 @@ as bytes. Rust's `OpaqueBlob` writes the blob from a `Serialize`, and Go
writes it with `vcffi.MarshalStorage`. The SDK needs the same from a
JavaScript value, which reaches Rust as a frame, and nothing converts a
frame to a blob yet (#1822). The engine needs no change. Version 1 of the
profile holds plain scalars only, so a `Date` or a `VcDecimal` inside an
profile holds plain scalars only, so a `Date` or an `RcDecimal` inside an
opaque object is refused.

### A record
Expand Down Expand Up @@ -587,31 +590,50 @@ So no JSON text of a row crosses the boundary.
| `bigint` | an integer, at the narrowest width that holds it | `bigint`, for every width |
| `Uint8Array`, `Buffer` | bytes | `Uint8Array` |
| `Date` | a timestamp, in milliseconds | `Date` |
| `VcTimestamp("2026-11-23T01:02:03.000000001Z")` | a timestamp, in nanoseconds | `Date` when it is whole milliseconds, else `VcTimestamp` |
| `VcDate("2026-11-23")` | a calendar date | `VcDate` |
| `VcDecimal("1.50")` | a decimal, with its scale | `VcDecimal` |
| `VcUuid("..")` | refused for now, with `recipher_js::uuid` | |
| `RcTimestamp("2026-11-23T01:02:03.000000001Z")` | a timestamp, in nanoseconds | `Date` when it is whole milliseconds, else `RcTimestamp` |
| `RcDate("2026-11-23")` | a calendar date | `RcDate` |
| `RcDecimal("1.50")` | a decimal, with its scale | `RcDecimal` |
| `RcUuid("..")` | a universally unique identifier (UUID), from #1996, as a value or a record field | `RcUuid` |
| `null` | null | `null`. In a record, only where the field declares `null` |

- **Integers are a `bigint`.** A `number` is a float, and `5` does not
match the integer `5n` in a query. Integer data uses `bigint`.
- **An integer's width is not chosen.** The bridge stores a `bigint` at the
narrowest width that holds it. Equality terms are the same across
widths, so `5n` here matches an `int64` 5 from Go.
- **A UUID waits on the engine.** vitaminc's value model has a UUID
type, and the bridge reads a `VcUuid` into it. The engine's dynamic
value model, the types a frame may hold in `dynamic::ValueFrame`, has
no UUID, so the SDK refuses one until the engine adds it (#1821).
- **A UUID is an `RcUuid`.** The engine's dynamic value model has a UUID
kind, `ValueKind::Uuid` (#1929). A UUID field takes an equality index
only. The SDK encrypts an `RcUuid` as that kind from #1996, as a value
on its own or in a record field declared `kinds.uuid`. A decrypted
`RcUuid` contains lowercase hyphenated hex. A string that contains a
UUID is encrypted as text. Until #1996 merges, the SDK refuses an
`RcUuid` with `recipher_js::uuid`.
- **No precision is lost.** A timestamp finer than a millisecond opens as
a `VcTimestamp`, never as a rounded `Date`.
an `RcTimestamp`, never as a rounded `Date`.
- **The wrapper classes are the bridge's.** Each holds its text, such as
`new VcDecimal("1.50")`, and is frozen. The core package re-exports the
four the addon defines.
`new RcDecimal("1.50")`, and is frozen. The core package re-exports the
four the addon defines. The classes were renamed in #1775 to
`RcDate`, `RcTimestamp`, `RcDecimal`, and `RcUuid`. vitaminc's own
bridge names them `VcDate`, `VcTimestamp`, `VcDecimal`, and `VcUuid`.
- **The bridge's safety rules apply.** Only a plain object is read, by its
own enumerable string keys. A `Map`, a `Set`, a class instance, a sparse
array and a string with an unpaired surrogate are refused. Nesting is
limited to 128 levels. A polluted prototype cannot add a key or see a
decrypted value.
array and a string with an unpaired surrogate are refused. A polluted
prototype cannot add a key or see a decrypted value.
- **Nesting has two limits.** Both count levels the same way. The top
value is level 0, and each array or object puts its items one level
further down. So in `[[1]]`, the `1` is two levels down.
- The bridge reads no value more than 128 levels down. That is the
frame's limit.
- A JSON document has no array or object more than 126 levels down,
counted from the document's own top. So at most 127 arrays and
objects nest inside one another, and a scalar can be 127 levels
down. The engine's JSON parser reads no deeper. #1775 checks this
limit when `json(..)` reads the document, and refuses a deeper one
with `recipher_js::too_deep`.
- A `Json` never sits inside a value tree. In #1775, `encrypt` takes a
`Json` as one value, or as one item of an array of values. Each item
is read from level 0. A `Json` anywhere deeper is refused. So only
the document's own limit applies to a document.
- **A failed conversion is an `Err`.** The bridge reports which rule a
value broke as a typed refusal, a diagnostic with a `recipher_js::` code,
such as `recipher_js::not_plain` or `recipher_js::too_deep`. A scalar out of
Expand All @@ -625,9 +647,16 @@ each kind is the one in the table, so an integer field is a `bigint`, at
every width. A `number` in an integer field does not compile.

**Integers in an adapter.** An ORM such as Drizzle gives an integer column
as a `number`. The adapter converts, because it knows the column's type: a
`number` that is a safe integer becomes a `bigint` on the way in, and a
`bigint` becomes a `number` on the way out. An unsafe integer is refused.
as a `number`. The adapter converts, because the adapter has the column's
type. The column's TypeScript type is `number | bigint`.

- On the way in, a `number` that is a safe integer becomes a `bigint`. A
`bigint` is accepted as it is. A whole `number` outside the safe range
is refused, because it may have lost digits.
- On the way out, an integer within 2^53 - 1 of zero becomes a `number`.
A larger integer comes back as a `bigint`. So the adapter can read every
stored row, such as an `int64` above 2^53 that Go wrote.

The core has one mapping, and it is the bridge's.

**The wasm build.** The value bridge is for Node addons only. The wasm
Expand All @@ -641,11 +670,31 @@ value conformance cases (`value-napi/tests/value-conformance.cjs`).
**JSON documents.** `json(value)` encrypts a JSON document, for a `json`
field or an EQL `Json` type. A JavaScript `number` cannot hold every
integer a JSON document can. So an integer outside the safe range is a
`bigint` in the tree. On encrypt, a `number` that is an unsafe integer is
refused, and a `bigint` is written as its digits. On decrypt, an integer
outside the safe range comes back as a `bigint`. The conversion is in Rust,
and the engine's one parser reads the result. The `eql-json-v1` case that
holds `18446744073709551615` round-trips on both builds.
`bigint` in the tree.

On encrypt, #1775 stores a whole number in a document by these rules.
Each range includes both ends.

| The whole number | As a `number` | As a `bigint` |
|---|---|---|
| -(2^53 - 1) to 2^53 - 1 | an integer | an integer |
| 2^53 to 2^64 - 1 | refused, and the refusal asks for a `bigint` | an integer |
| -2^63 to -2^53 | refused, and the refusal asks for a `bigint` | an integer |
| -(2^64 - 1) to -(2^63 + 1) | refused: no exact form in a document | refused |
| 2^64 or more, or -2^64 or less | a float, such as `1e20` or `-1e20` | refused |

A whole `number` outside the safe range may have lost digits, so the
refusal asks for a `bigint` where one fits. Below -2^63 no `bigint`
fits, so the refusal says the integer has no exact form. A `number` with
a fraction is a float. `NaN` and the infinities are refused.

On decrypt, an integer outside the safe range comes back as a `bigint`.
The conversion is in Rust, and the engine's one parser reads the result.
On the native build, a test in #1775 (`__tests__/addon/values.test.ts`,
"keeps an integer outside the safe range as a bigint") round-trips
2^64 - 1. The wasm build is not built yet. The plan is for the wasm build
to pass the same test, and the `eql-json-v1` case that contains
`18446744073709551615`.

The value profile of the conformance suite (`record-values-v1`) is the
contract for the record kinds. Its cases include every kind at its edges
Expand Down Expand Up @@ -975,11 +1024,14 @@ The core, with no EQL:
`string` value on Node, with no context. CI builds it on Linux. Proves that the engine's futures run on
napi-rs's runtime. Adds the test that the crate links no
`eql-bindings`.
2. **Values.** The value bridge for every type in the table above,
the wrapper classes, an array of values,
`bytes` for a file, the value profile and the fuzz target. `opaque`
for a whole object follows the storage-blob converter (#1822), and
the UUID type the engine's (#1821).
2. **Values.** The value bridge for every type in the table above
except `RcUuid`. The wrapper classes, an array of values, `bytes` for
a file, the value profile, and the fuzz target. `opaque` for a whole
object follows the storage-blob converter (#1822). #1996 adds
`RcUuid` after step 3, over the engine's UUID kind (#1929). A
`RcUuid` then encrypts as a value on its own, with no record. The
pull request comes after step 3 so that its tests can also store a
UUID record field.
3. **Records.** `record`, `encrypt`, `passthrough`, nulls, the stored row
type, `encryptField` and `decryptField`, the type tests, and the
declaration, runtime and read profiles. This step is the first to call
Expand Down Expand Up @@ -1180,7 +1232,9 @@ Found by steps 1 to 3, in #1774 at `872ff2ea2`, #1775 at `30f1d26fd` and
decrypts as a `Vec<u8>` holding the blob, and the blob encrypted as
bytes decrypts as the `OpaqueBlob`. So a binding reaches the format by
writing the blob itself. See [A file, and a whole object](#a-file-and-a-whole-object).
- The engine's dynamic value model has no UUID. See [Values](#values).
- The engine's dynamic value model had no UUID at those commits. The
model has one now (#1929). #1996 makes the addon use it. See
[Values](#values).
- The engine opens one field of a stored row without the row's other
fields: `decryptField` runs the plan of that field alone, and a field
encrypted alone has the same bytes as in a whole row.
Expand Down
Loading