Skip to content

Document how to build a lossless transport - #26

Merged
kubukoz merged 1 commit into
mainfrom
lossless-transport-docs
Sep 2, 2026
Merged

Document how to build a lossless transport#26
kubukoz merged 1 commit into
mainfrom
lossless-transport-docs

Conversation

@kubukoz

@kubukoz kubukoz commented Sep 2, 2026

Copy link
Copy Markdown
Member

The @lossless section already said a lossless transport is required, named the runtime as one, and pointed at parseLossless / stringifyLossless. But that was one sentence with no code and no list of where the substitution has to happen — and since nothing in the generated code can detect a lossy transport, this is the one place where being terse costs a silent bug: the schema validates, the field is a number, and it is the wrong number.

So this adds a Building a lossless transport subsection, with a first line that lets anyone using the runtime's fetchTransport stop reading immediately.

What it adds beyond what was there

All four JSON boundaries, not the obvious two. A transport has to substitute on the request body, the response body, and per-line ndjson in both directions. All four exist in the reference implementation (fetch.ts:90,150,241, ndjson.ts:21,53), so a transport that fixes only the unary pair still rounds a @lossless member inside a stream.

res.json() gets named as a trap. It is the natural way to read a body and it is unfixable from outside — the rounding happens inside it, before the caller ever holds the value. Read text() and parse that.

A doc-comment fix. StreamTransportResponse.stream is specified as already-parsed ndjson elements, so parsing them losslessly is the transport's job and no later schema can undo it. Its comment said "already JSON.parsed" — naming the exact function a transport author must not use there. Now corrected, with the lossless requirement stated where an implementor will actually read it.

Two body-reading cases that aren't about precision but that the generated client depends on: an empty body (204, or an operation with no output members) is not valid JSON, and a non-2xx body may not be JSON at all. Both are shown via readBody from fetch.ts, since throwing on either turns a useful status code into a parse error.

On "provide one as a separate module"

Not doing that, and I think the current shape is right: fetchTransport already is a provided lossless transport, and parseLossless / stringifyLossless are already exported from the package root (index.ts) specifically so a hand-rolled transport can use them. A separate module would split two small functions away from their only in-repo consumer without making anything more reachable. The gap was documentation, and that is what this fixes.

Docs plus one doc comment; no behaviour change. Runtime typecheck and all 46 tests pass.

The @lossless section said a lossless transport is required, named the runtime
as one, and pointed at parseLossless / stringifyLossless — but in one sentence,
with no code and no list of where the substitution has to happen. Since nothing
in the generated code can detect a lossy transport, that is the one place where
being terse costs a silent bug: the schema validates, the field is a number, and
it is the wrong number.

So: a "Building a lossless transport" subsection, and a first line that lets
anyone using the runtime's fetchTransport stop reading.

It enumerates all four JSON boundaries rather than the two obvious ones —
request body, response body, *and* per-line ndjson in both directions. All four
exist in runtime/src (fetch.ts:90,150,241 and ndjson.ts:21,53), so a transport
that fixes only the unary pair still rounds a @lossless member inside a stream.

Two specific traps, both from reading the reference implementation:

  - res.json() cannot be fixed from outside — the rounding happens inside it,
    before the caller holds the value. Read text() and parse it.
  - StreamTransportResponse.stream is specified as already-parsed elements, so
    parsing them losslessly is the transport's job; no later schema can undo it.
    The doc comment on that field said "already JSON.parse'd", which named the
    exact function a transport author must not use there. Fixed, and the
    lossless requirement stated where an implementor will actually read it.

Also documents the two body-reading cases that are not about precision but that
the generated client depends on — an empty body (204, or no output members) is
not valid JSON, and a non-2xx body may not be JSON at all — with the readBody
from fetch.ts, since throwing on either turns a useful status into a parse error.

Docs and one doc comment; no behaviour change. Runtime typecheck + 46 tests pass.
@kubukoz
kubukoz merged commit 59e44a9 into main Sep 2, 2026
10 checks passed
@kubukoz
kubukoz deleted the lossless-transport-docs branch September 2, 2026 18:13
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