feat(core): single MessagePack wire contract with refAsPath option #82

Merged
crapougnax merged 3 commits from claude-bot/feat/msgpack-lowest-level-normalizer into develop 2026-11-23 14:56:09 +00:00 AGit
Member

Summary

MessagePack payloads only got path instead of ref when they still held live objects exposing toMsgPack(). References already serialized by a handler (toJSON(), toReference(), plain literals) kept their ref key, so clients received a mix of both.

This moves the MessagePack wire contract to a single function in @quatrain/core and reduces the HTTP layer to negotiation and encoding.

Changes

  • @quatrain/core: normalizeForMsgPack(value, options) enforces the contract on any value, whatever produced it:
    • toMsgPack() / toJSON() are applied and their output is normalized again;
    • every ref key is emitted as path, at any depth (arrays, Map, Set, nested objects);
    • null, undefined, functions, symbols and circular references are omitted;
    • binary views are preserved, bigint is converted.
  • refAsPath option (default true): { refAsPath: false } keeps ref. It is handed down to toMsgPack() on ObjectUri, AbstractObject and DataObject, so live objects and serialized references give the same result.
  • @quatrain/http: msgPackContentNegotiation(options) creates a framework-agnostic middleware (structural request/response types, no Express dependency) that decodes MessagePack request bodies and encodes res.json() / res.send() payloads when the client asks for MessagePack.
  • @quatrain/types: MsgPackOptions type; ObjectUri.toMsgPackReference() no longer overwrites an existing path key and ignores arrays.
  • packages/http/tsconfig.json now resets paths and excludes tests from the build, like its sibling packages.

Behaviour to review

  • The ref to path rule applies to any dictionary carrying a ref key, not only to dictionaries shaped like a reference.
  • A dictionary carrying both ref and path is left untouched.
  • null items are removed from arrays, which shifts indexes.

Validation

  • jest packages/http packages/core packages/types packages/backend/src: 28 suites, 194 tests passing.
  • turbo run build --filter=...@quatrain/types: 76 packages built.
  • The middleware type-checks as an Express RequestHandler.
  • Full jest run: the only failing suites are the Firestore and Postgres integration tests, which need a live database and fail the same way on develop.

Supersedes

Branch feat/msgpack-content-negotiation (Express-specific middleware in @quatrain/api-server-express), which can be deleted.

## Summary MessagePack payloads only got `path` instead of `ref` when they still held live objects exposing `toMsgPack()`. References already serialized by a handler (`toJSON()`, `toReference()`, plain literals) kept their `ref` key, so clients received a mix of both. This moves the MessagePack wire contract to a single function in `@quatrain/core` and reduces the HTTP layer to negotiation and encoding. ## Changes - **`@quatrain/core`**: `normalizeForMsgPack(value, options)` enforces the contract on any value, whatever produced it: - `toMsgPack()` / `toJSON()` are applied and their output is normalized again; - every `ref` key is emitted as `path`, at any depth (arrays, `Map`, `Set`, nested objects); - `null`, `undefined`, functions, symbols and circular references are omitted; - binary views are preserved, `bigint` is converted. - **`refAsPath` option** (default `true`): `{ refAsPath: false }` keeps `ref`. It is handed down to `toMsgPack()` on `ObjectUri`, `AbstractObject` and `DataObject`, so live objects and serialized references give the same result. - **`@quatrain/http`**: `msgPackContentNegotiation(options)` creates a framework-agnostic middleware (structural request/response types, no Express dependency) that decodes MessagePack request bodies and encodes `res.json()` / `res.send()` payloads when the client asks for MessagePack. - **`@quatrain/types`**: `MsgPackOptions` type; `ObjectUri.toMsgPackReference()` no longer overwrites an existing `path` key and ignores arrays. - `packages/http/tsconfig.json` now resets `paths` and excludes tests from the build, like its sibling packages. ## Behaviour to review - The `ref` to `path` rule applies to **any** dictionary carrying a `ref` key, not only to dictionaries shaped like a reference. - A dictionary carrying both `ref` and `path` is left untouched. - `null` items are removed from arrays, which shifts indexes. ## Validation - `jest packages/http packages/core packages/types packages/backend/src`: 28 suites, 194 tests passing. - `turbo run build --filter=...@quatrain/types`: 76 packages built. - The middleware type-checks as an Express `RequestHandler`. - Full `jest` run: the only failing suites are the Firestore and Postgres integration tests, which need a live database and fail the same way on `develop`. ## Supersedes Branch `feat/msgpack-content-negotiation` (Express-specific middleware in `@quatrain/api-server-express`), which can be deleted.
Payloads sent as MessagePack were only converted when they still held live
objects exposing toMsgPack(). References already serialized with toJSON()
or toReference() kept their `ref` key.

normalizeForMsgPack() now enforces the contract on any value, whatever
produced it: toMsgPack() / toJSON() are applied and their output is
normalized again, every `ref` key is emitted as `path` at any depth,
null values are omitted, binary views are preserved, Map and Set are
converted, circular references are dropped.

ObjectUri.toMsgPackReference() no longer overwrites an existing `path`
key and ignores arrays.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
msgPackContentNegotiation decodes MessagePack request bodies and encodes
res.json() / res.send() payloads when the client asks for MessagePack
(Accept header or ?format=msgpack). It only negotiates and encodes: the
wire contract is delegated to normalizeForMsgPack() from @quatrain/core.

The middleware relies on structural request/response types, so it plugs
into Express or Connect without depending on them.

The package tsconfig now resets `paths` and excludes tests from the
build, like its sibling packages, so workspace dependencies resolve to
their built output.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
feat(core): add a refAsPath option to the MessagePack wire contract
All checks were successful
Quatrain Core CI / Monorepo Build, Tests & Quality Audit (push) Successful in 4m51s
Quatrain Core CI / Monorepo Build, Tests & Quality Audit (pull_request) Successful in 5m0s
Quatrain Core CI / Publish Changed Packages to npm (push) Has been skipped
Quatrain Core CI / Publish Changed Packages to npm (pull_request) Has been skipped
f028cd6579
The `ref` to `path` rule stays on by default and can now be turned off
per application: normalizeForMsgPack(value, { refAsPath: false }).

The option is handed down to toMsgPack() on ObjectUri, AbstractObject
and DataObject, so live objects and already serialized references give
the same result. msgPackContentNegotiation() in @quatrain/http becomes a
factory taking the same options.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
crapougnax merged commit c154930bc9 into develop 2026-11-23 14:56:09 +00:00
Sign in to join this conversation.
No description provided.