Crypto: vendored CBOR and gzip codecs
What src/crypto/vendor/codecs.js contains, the three encoders it exports, how tools/crypto-vectors/vendor.ts generates it, and the test that requires byte-identical regeneration.

The app's CBOR and gzip code is a single generated file, src/crypto/vendor/codecs.js (2,924 lines). It is a browser-target bundle of @ipld/dag-cbor, cborg and fflate with Node fast paths and implicit clocks removed, so the same bytes come out under Bun, in a browser and in the Hermes data module runtime.

Exports

Typed by codecs.d.ts:

Export

Meaning

encode(value)

"Canonical client CBOR": cborg with the dag-cbor options, except that undefined encodes as null instead of throwing

strictEncode(value)

@ipld/dag-cbor's own encode: canonical DAG-CBOR that rejects undefined and non-finite numbers

decode(bytes)

Strict DAG-CBOR decode, including CID tag 42

gzipSync(bytes, {mtime})

Pure JavaScript gzip; the mtime option is required by the type

gunzipSync(bytes)

Pure JavaScript gunzip

src/crypto/cbor.ts re-exports encode and decode as the module's default codec.

Which encoder is used where:

    strictEncode: signing and encoding Changes, Refs and Comments, and the notify envelope (Blobs, Envelopes).

    encode: encodeBlob and verifyBlob, the agents envelope, and the vault state codec (Vault codec).

For values without undefined the two produce the same bytes.

What is inside the bundle

The file keeps a comment for each source module, so its structure is easy to read:

Lines

Source

2 to 1262

cborg as resolved under @ipld/dag-cbor (5.1.11 in bun.lock)

1263 to 1334

@ipld/dag-cbor/src/index.js (9.2.7), importing CID from multiformats/cid

1335 to 2408

top-level cborg (4.5.8), used by encode

2409 to 2915

fflate/esm/browser.js (0.8.3)

2916 to 2924

the entry: the three codec bindings and the export list

multiformats/cid stays external, so the bundle and the rest of the app share one CID class. Versions are pinned in package.json (@ipld/dag-cbor and cborg exactly); see Dependencies.

How it is generated

tools/crypto-vectors/vendor.ts runs Bun.build on codec-entry.ts:

    target browser, format esm, syntax minification only (whitespace and identifiers are kept readable),

    an onLoad plugin rewrites every file under node_modules: globalThis.process and globalThis.Buffer become undefined, and Date.now() becomes 0.

The header comment states the limit of those edits: "Only optional Node fast paths and implicit clocks are removed, not codec algorithms."

The script then prepends // Generated by tools/crypto-vectors/vendor.ts. See LICENSE files. and copies the licence texts next to the bundle:

File

Content

cborg.LICENSE

Apache 2.0, Rod Vagg

fflate.LICENSE

MIT, Arjun Barrett

dag-cbor.NOTICE.md

the package README, which carries the Apache 2.0 or MIT dual-licence notice because "dag-cbor's published tarball omits LICENSE files"

Run it from the app directory with the pinned Bun. --out <dir> writes elsewhere.

Why vendor at all

Inferred from the generator and the runtime constraints: the upstream packages probe process and Buffer and read the clock (gzip headers), and the data module must be deterministic and free of Node. Committing the bundle also makes any codec change visible in review. The reason given in the test file is about dependency drift (below).

Tests

    vendor.test.ts (1 test, 30 s timeout) runs the generator with --out into a temporary directory and requires the result to equal the committed codecs.js exactly. Its comment: a dependency change "would otherwise make the next regeneration silently swap codecs".

    "browser-only codec keeps strict DAG-CBOR semantics and client null encoding" in crypto.test.ts compares strictEncode with the real @ipld/dag-cbor on maps, CIDs, floats, negative numbers, bigints and arrays, checks the null encoding of undefined against the original client's bytes, and requires errors for undefined, Infinity and a duplicate map key.

Both run in the Bun unit tests.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Comments