io / proto-codec-bin
src/io/proto/binCodec.ts is the binary protobuf entry for Apollo HD-map. The file itself is 23 lines, but its two functions sit on the critical path for every import / export.
Exported symbols
ts
export function decodeMapBin(bytes: Uint8Array): Promise<Record<string, unknown>>;
export function encodeMapBin(obj: Record<string, unknown>): Promise<Uint8Array>;1
2
2
Source:
src/io/proto/binCodec.ts:1-23.
decodeMapBin(bytes)
ts
const Map = await getMapType();
const msg = Map.decode(bytes);
return Map.toObject(msg, {
longs: Number,
enums: Number,
defaults: false,
arrays: true,
objects: true,
});1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
| Option | Setting | Reason |
|---|---|---|
longs: Number | int64 → JS number | Apollo fields never overflow 53-bit precision |
enums: Number | numeric enum values | aligned with entityBridge/enums.ts's *_INV maps |
defaults: false | unset fields stay absent | preserves round-trip wire fidelity |
arrays: true | repeated → empty array | removes null guards downstream |
objects: true | sub-message → empty object | same |
encodeMapBin(obj)
ts
const Map = await getMapType();
const err = Map.verify(obj);
if (err) throw new Error(`Map.verify failed: ${err}`);
const msg = Map.fromObject(obj);
return Map.encode(msg).finish();1
2
3
4
5
2
3
4
5
Map.verify is protobufjs' generated sanity check; a non-empty return becomes Error: Map.verify failed: <reason>.
Consumers
src/io/apolloIO.worker.ts— bothrunImportandrunExport.src/io/proto/textCodec.tsdoes not depend on binCodec but sharesgetMapType().
Tests
src/io/proto/__tests__/binRoundtrip.test.ts round-trips the upstream map_data/sunnyvale_loop fixture and asserts byte equality (modulo apollo.hdmap.Map.editor_meta).
See also
- Proto / Loader — schema source.
- io/proto-codec-text — text format counterpart.
- Import / Parse Base Map and Export / Base Map — full pipelines.