Rust WebAssembly package
The Rust WebAssembly package turns the MLT decoder into a browser-ready npm package. Rust reads the tile bytes and performs the format work. TypeScript presents the result as typed arrays, vector-tile-like features, GeoJSON, or tools for inspecting the encoded tile.
Package boundary
The package is published as @maplibre/mlt-wasm at version 0.2.5. Its Rust crate compiles mlt-core to WebAssembly through wasm-bindgen, while its TypeScript layer defines the public API that browser and bundler code uses. This keeps format work on the Rust side and gives JavaScript callers data shapes that fit common map and rendering flows. See the Rust tile workspace, core codecs, native bindings, and conversion CLI pages for the neighboring package roles.
The main boundary has one direction: a caller supplies an MLT byte buffer, Rust decodes it, and JavaScript receives a complete result or a handle for later inspection.
flowchart LR
A[MLT tile bytes] --> B[TypeScript public API]
B --> C[wasm-bindgen boundary]
C --> D[Rust mlt-core decoder]
D --> E[Typed arrays]
D --> F[Vector-tile-like features]
D --> G[GeoJSON]
D --> H[Annotated tile handle]
Rust binding role
The Rust binding exposes three kinds of work: column decoding for efficient data access, GeoJSON conversion for a simple interchange result, and tile annotation for wire-format tools. It converts Rust failures into JavaScript errors for decoding paths, while annotation reports a partial walk and its error as data.
References: rust/…/lib.rs• L26-63 rust/…/columns.rs• L24-79 rust/…/annotate.rs• L22-37
JavaScript package role
The TypeScript package is the stable browser-facing surface. It re-exports the tile decoders, geometry helpers, column types, GeoJSON function, and annotation types from one package entry point. The surface supports both a vector-tile-like feature view and direct column access for renderers that manage their own buffers. Geometry helpers can expose feature views or layer-wide start positions for batch and GPU work. A single internal module imports the generated WebAssembly module so browser bundlers do not initialize that top-level-await module through several paths. Optional annotation and GeoJSON exports fail with a clear feature message when a smaller WebAssembly build leaves them out.
References: rust/…/index.ts• L1-59 rust/…/wasm.ts• L1-36 rust/…/featureGeometry.ts• L188-274
Packaging flow
The package build creates two linked artifacts. wasm-pack builds the Rust crate for a bundler and writes the generated WebAssembly package to pkg; TypeScript then emits JavaScript and declarations to dist. The npm package publishes those two output areas rather than the source tree.
flowchart LR
A[Rust mlt-wasm crate] --> B[wasm-pack]
B --> C[pkg WebAssembly module]
D[TypeScript API] --> E[tsc]
E --> F[dist JavaScript and declarations]
C --> G[npm package]
F --> G
Build configuration
The Rust crate defaults to MLT v2 support, GeoJSON conversion, and tile annotation. It also has a coverage build. Release WebAssembly uses browser-focused optimization and SIMD settings. The npm scripts join the WebAssembly build, TypeScript build, and compressed artifact step into one package build. A decoder-only build can disable the optional annotation and GeoJSON features to reduce the WebAssembly file.
References: rust/…/Cargo.toml• L14-39 rust/…/package.json• L29-38 rust/…/README.md• L25-43
Browser module loading
The package targets bundlers rather than a direct browser script tag. The test bundler enables WebAssembly and top-level await, which matches the generated module loading model used by the package. The package entry points expose compiled JavaScript and declarations while the generated WebAssembly stays in the published pkg area.
References: rust/…/package.json• L4-10 rust/…/package.json• L29-35 rust/…/vitest.config.ts• L1-8 rust/…/tsconfig.json• L1-16
Tile decoding APIs
The package offers several result shapes for different consumers. Column data is the broad base: feature wrappers, geometry helpers, and the 3D API build their higher-level views from it.
| API | Main result | Best fit |
|---|---|---|
decodeTileColumns | Layers of typed arrays and offset arrays | Renderers and code that owns its data buffers |
decodeTile | Layers with vector-tile-like features | Existing vector-tile consumers |
decodeTile3D | Layers with [x, y, z] feature vertices | Callers that require 3D coordinates in every layer |
tileGeoJson | A GeoJSON feature collection | Simple feature interchange |
Column data
decodeTileColumns returns layers in wire order. Each layer contains geometry buffers, feature metadata, optional ids, property columns, and v2 m-value columns. A property column has one slot per feature; its optional presence bitmap distinguishes a missing value from the slot's default value. The JavaScript column surface does not pass through nested columns. Layer selection can skip unwanted layers before they are decoded, and duplicate layer names remain separate in this low-level result.
Geometry stays in tile coordinates. Two-dimensional layers use (x, y) vertices, while three-dimensional layers use interleaved (x, y, z) vertices and a zStep value that toElevation converts to metres. Layers with outlines expose offset levels for features, geometries, and rings. Tessellated polygon and mesh layers expose triangle offsets and an index buffer instead of requiring outline runs.
The boundary copies decoded arrays out of WebAssembly before it returns them. JavaScript can therefore keep the column result while later WebAssembly work changes its memory.
References: rust/…/columns.ts• L15-187 rust/…/columns.ts• L191-264 rust/…/columns.rs• L24-79
Mesh and m-value data
The column API represents v2 m-values at the level where the tile stores them. Vertex m-values belong to the stored vertices. Mesh m-values belong to triangles. A feature presence bit says whether a feature has a value, and offsets select that feature's run. Numeric values can be read as views; string values use dictionary codes and resolve to strings through mValueSlice.
Mesh layers carry 3D vertices and triangle indices. Their features expose mesh geometry rather than polygon rings. A mesh with relative height uses the terrain at the centre of its vertex bounds as its height base. decodeTile and decodeTile3D expose the same triangle and m-value access through feature methods.
References: rust/…/columns.ts• L121-165 rust/…/columns.ts• L221-264 rust/…/vectorTile.ts• L166-201 rust/…/vectorTile.ts• L327-351 rust/…/featureGeometry.ts• L45-60
Vector-tile views
decodeTile turns column layers into a familiar layer-and-feature view. A feature exposes its id, properties, geometry type, and geometry loaders. The layer record uses names as keys, so this view requires unique layer names. featureGeometry provides views for one feature, while geometryStarts provides layer-wide boundaries for batch work without changing the shared vertex buffer.
The 2D view supports the usual point, line, and polygon forms. It can also expose the stored z values of a 3D layer separately. Mesh features expose one vertex run and triangle indices instead of polygon rings. decodeTile3D provides the same feature shape with three raw values per vertex, but it requires every layer to have z coordinates and rejects the TessPolygons and TessPolygonsWithOutlines layouts. decodeTileColumns reads every supported layout, including Meshes. Use toElevation with the layer's zStep when a caller needs metres. The layer's heightMode says whether zero is tied to mean sea level or the ground below each vertex.
References: rust/…/vectorTile.ts• L131-285 rust/…/vectorTile.ts• L288-345 rust/…/vectorTile.ts• L397-498 rust/…/featureGeometry.ts• L254-419
GeoJSON conversion
tileGeoJson provides a compact path from raw MLT bytes to a GeoJSON feature collection. The result keeps layer and extent context on each feature. v2 m-value columns appear as m:-prefixed arrays, and mesh features carry triangle indices. This path favors a ready-to-use object model over direct access to the decoded column buffers.
References: rust/…/lib.rs• L39-59 rust/…/geojson.ts• L1-12
Tile annotation API
annotateTile serves tools that need to explain the encoded tile rather than only consume its decoded features. It returns a handle with a region tree, optional layer views, on-demand blob decoding, and the error state of the walk.
Region inspection
The region tree describes the byte ranges and field meanings that the walker reached. Callers can ask for the whole tile or one top-level layer. Blob values are decoded only when requested and can be capped, so inspection tools can show large tiles without creating every decoded value at once.
Annotation is deliberately partial for malformed input. The handle keeps enough state to report the regions that were read, the error that stopped the walk, and the remaining unannotated bytes. The caller releases the handle when inspection ends.
References: rust/…/annotate.rs• L7-10 rust/…/annotate.rs• L39-95 rust/…/annotate.ts• L3-15 rust/…/annotate.ts• L18-81
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Rust export names and the imports in js/wasm.ts aligned. A renamed WebAssembly export breaks all higher-level APIs at module load.
- Keep the column object contract aligned between src/columns.rs and js/columns.ts. Changes to typed arrays, presence bits, offsets, m-value scopes, or geometry layouts affect both direct consumers and vector-tile wrappers.
- Keep the
layersfilter semantics aligned across the Rust decoder and the TypeScript result. It must retain every matching layer in wire order, including duplicate names. - Keep
decodeTile,decodeTile3D,featureGeometry, andgeometryStartsaligned with the geometry layouts returned bydecodeTileColumns. Tessellated layers expose triangles rather than outline runs. Meshes expose per-feature vertex runs and triangle indices. Missing z data, raw z values, andzStepconversion have different supported paths. - Keep vertex and triangle m-value scopes aligned between the Rust column export and
mValueSlice, feature loaders, and GeoJSON. A scope or offset change can silently attach data to the wrong geometry unit. - Keep the
pkganddistoutput paths aligned with the npmfiles,main, andtypesfields. A successful local build can still publish an unusable package if these paths diverge.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A 64-bit id or property exceeds JavaScript's safe integer range | The column API loses integer precision because the value crosses as Float64Array | Use the annotation API when exact 64-bit values are required |
A caller treats a default slot as a stored property without checking present | A missing value is reported as 0, "", or another type default | Read values through columnValue or apply the presence bitmap |
A caller uses decodeTile3D on a layer without z data | The layer wrapper rejects the input | Use decodeTile for mixed or 2D tiles |
A caller uses feature geometry or geometry starts on a TessPolygons layer | There are triangles but no outline runs, so the helper throws | Read indexBuffer and triangleOffsets from the column result |
| A caller treats a mesh as a polygon | The feature has no polygon rings and polygon loading throws | Read its vertex run with loadGeometry() and its indices with loadTriangles() |
| A caller reads a vertex m-value from a triangle column, or the reverse | The feature returns no value for the requested scope | Use loadVertexValues for vertex data and loadTriangleValues for mesh triangle data |
| A tile has duplicate layer names | The record-shaped result from decodeTile cannot represent both layers | Use decodeTileColumns, which preserves wire order and duplicates |
| A tile walk stops on malformed bytes | Annotation returns a partial tree and an error instead of a complete annotation | Check error and treat the unannotated leaf as unknown data |
Key files
| File | Why to read it |
|---|---|
| rust/…/README.md | Gives the package purpose, build flow, feature choices, and browser-facing usage examples |
| rust/…/lib.rs | Defines the main Rust-to-JavaScript exports and the GeoJSON boundary |
| rust/…/columns.rs | Defines the column data shape and copy boundary sent to JavaScript |
| rust/…/annotate.rs | Defines the stateful annotation handle and partial-walk behavior |
| rust/…/index.ts | Shows the public TypeScript package surface |
| rust/…/wasm.ts | Shows the single WebAssembly import and optional feature checks |
| rust/…/columns.ts | Documents typed arrays, presence bitmaps, layer filtering, geometry columns, m-value scopes, and z conversion |
| rust/…/vectorTile.ts | Shows how column data becomes vector-tile-like layers and 2D, 3D, or mesh features |
| rust/…/featureGeometry.ts | Defines feature geometry and renderer-oriented offset views |
| rust/…/geojson.ts | Defines the browser-facing GeoJSON conversion |
| rust/…/annotate.ts | Defines the browser-facing annotation types and handle methods |
| rust/…/meshes.spec.ts | Shows the expected mesh, triangle m-value, and height-mode behavior |
| rust/…/package.json | Defines package entry points and the WebAssembly, TypeScript, test, and compression commands |
| rust/…/Cargo.toml | Defines the Rust package, WebAssembly crate type, and feature defaults |