Proto / Loader
src/io/proto/loader.ts loads the Apollo HD-map proto schema using protobufjs. All .proto files are bundled with Vite's ?raw glob, so runtime is fully offline and Vitest reuses the same mechanism on Node.
Exported symbols
ts
import * as protobuf from 'protobufjs';
export function loadApolloProtoRoot(): Promise<protobuf.Root>;
export function getMapType(): Promise<protobuf.Type>;1
2
3
4
2
3
4
Source:
src/io/proto/loader.ts:1-58.
loadApolloProtoRoot()
Returns the cached protobuf.Root once load('map_msgs/map.proto') has completed. The first call pays the parse cost (~5–15 ms); subsequent calls return the cached promise.
Implementation notes:
- Glob injection —
import.meta.glob('/src/proto/**/*.proto', { query: '?raw', eager: true })embeds every proto file into the bundle. Works in Vite and Vitest. resolvePathoverride — Apollo proto imports look likemap_msgs/map_lane.proto, root-relative undersrc/proto/. The default protobufjs resolver would join with the importing file's directory and yieldmap_msgs/map_msgs/…. We override to(_, target) => target.- Deferred fetch callback —
done(...)runs insidePromise.resolve().then(...)so protobufjs' internalqueuedcounter never dips to 0 mid-traversal. A synchronous callback triggers a prematureresolveAll()and surfaces as "no such Type" errors.
ts
const root = await loadApolloProtoRoot();
const Map = root.lookupType('apollo.hdmap.Map');1
2
2
getMapType()
Convenience: (await loadApolloProtoRoot()).lookupType('apollo.hdmap.Map'). Used by binCodec, textCodec, and adapter.
ts
const Map = await getMapType();
const obj = Map.toObject(Map.decode(bytes));1
2
2
Why ?raw instead of pbjs codegen
- No extra build step. Editing a proto only requires
vite, nopbjsinvocation. - Reflection at runtime.
Map.fields,field.resolvedTyperemain available, which is whatapolloMapToLonLatneeds to walk every PointENU recursively. - Bundle size.
?rawstrings plus a singleprotobuf.Rootis ~150 KB gzipped; pre-generated JSON is no smaller and loses reflection.
Flow
Errors
| Cause | Surface |
|---|---|
Missing file in PROTO_SOURCES | Error: Proto file not found in bundle: <name> |
| Synchronous fetch callback (regression) | Error: no such Type ... from protobufjs.resolveAll() |
| Invalid PROJ string upstream | Surfaced in adapter.ts, not here |
Tests
src/io/proto/__tests__/loader.test.ts covers cold load and cache reuse, plus a regression test for the deferred fetch callback.
See also
- Proto / Codec —
decodeMapBin/TextandencodeMapBin/TextusegetMapType(). - io/proto-adapter — walks the loaded type tree to apply projection transforms recursively.
- Proto / Schema — index of the
.protofiles this loader pulls.