Add experimental WebAssembly support for BMP/BGP/MRT parsing#267
Merged
Add experimental WebAssembly support for BMP/BGP/MRT parsing#267
Conversation
Add a `wasm` feature flag that compiles the parsing core to WebAssembly, published as `@bgpkit/parser` on npm. Supports three targets: Node.js (CommonJS), bundlers (ES modules), and browsers/workers (ES modules with manual init). Exported functions: - parseOpenBmpMessage: OpenBMP-wrapped BMP frames (RouteViews Kafka) - parseBmpMessage: raw BMP frames - parseMrtFile: decompressed MRT files - parseBgpUpdate: single BGP UPDATE messages Includes JS wrapper with JSON deserialization, TypeScript types, multi-target build script, and two Node.js examples (Kafka OpenBMP stream consumer and MRT file parser).
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #267 +/- ##
==========================================
- Coverage 91.15% 90.68% -0.47%
==========================================
Files 83 84 +1
Lines 15138 15462 +324
==========================================
+ Hits 13799 14022 +223
- Misses 1339 1440 +101 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Extract core parsing logic into testable functions that return Result<String, String> instead of JsError, with thin wasm_bindgen wrappers on top. Tests cover BMP initiation/peer-up parsing, BGP UPDATE parsing, MRT empty file, invalid input handling, and peer header construction.
Add parseMrtRecord and parseMrtRecords functions that parse one MRT record at a time, enabling processing of multi-GB RIB dump files without exceeding WASM's 4GB memory limit. JS-side MRT header framing ensures only one record's bytes are passed to WASM per call. Use thread-local Elementor to persist PeerIndexTable across calls, required for TABLE_DUMP_V2 (RIB dump) files. Add resetMrtParser to clear state between files. Tested: 3.9GB RIB dump (55.9M elements) parses in ~190s via streaming.
- Remove parseMrtFile (batch) in favor of parseMrtRecords (streaming generator) and streamMrtFrom (Node.js async generator with I/O) - Remove detectCompression from public exports (internal utility) - Add openMrt, streamMrtFrom Node.js I/O helpers with gz/bz2 support - Add parseMrtRecord/resetMrtParser low-level API for custom iteration - Add web.mjs/web.d.ts entry point for browser and Cloudflare Workers - Rewrite README with use-case-driven sections, code snippets, memory considerations, and platform support matrix - Simplify parse-mrt-file example to use streamMrtFrom - Add browser MRT explorer examples (HTML and JSX) - Bump npm package version to 0.15.2 - Update CHANGELOG with streaming API details
Switch from esm.sh to jsdelivr CDN for loading @bgpkit/parser WASM. esm.sh rewrites JS modules, breaking import.meta.url and WASM path resolution. jsdelivr serves raw files with correct Content-Type and CORS headers. Also use the /web entry point (with async init()) instead of the bundler target which requires bundler-specific WASM handling.
- Link to mrt-explorer.labs.bgpkit.com in README and CHANGELOG - Remove unused mrt-explorer.jsx (standalone HTML file is the demo) - Add .wrangler/ to .gitignore
The deployed version lives in the bgpkit/labs repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add experimental WebAssembly support for BMP/BGP/MRT parsing, published as
@bgpkit/parseron npm.Live demo: mrt-explorer.labs.bgpkit.com
Use cases
Public API
Core parsing functions (all platforms):
parseOpenBmpMessage(data)— parse OpenBMP-wrapped BMP framesparseBmpMessage(data, timestamp)— parse raw BMP framesparseBgpUpdate(data)— parse a single BGP UPDATE messageparseMrtRecords(data)— streaming generator that yields MRT records one at a timeparseMrtRecord(data)/resetMrtParser()— low-level MRT record parsingNode.js I/O helpers:
streamMrtFrom(pathOrUrl)— fetch, decompress (gz/bz2), and stream-parse MRT recordsopenMrt(pathOrUrl)— fetch and decompress MRT data into a BufferPlatform support
init())init())Memory model
The entire decompressed MRT file must fit in memory.
parseMrtRecordsthen iterates record-by-record, avoiding accumulation of all parsed BgpElem objects. Suitable for MRT updates files (20–200 MB decompressed). Full RIB dumps (500 MB+) should use the native Rust crate.Versioning
npm package version tracks the Rust crate minor version: for crate
0.X.Y, npm is0.X.Zwhere Z increments independently.Changes
wasmfeature flag withwasm-bindgenbindings insrc/wasm.rssrc/wasm/js/) with Node.js CJS, bundler ESM, and web ESM entry pointssrc/wasm/build.sh) producing multi-target npm packageseek-bzipdependencyTest plan
src/wasm/test/)cargo testpassescargo clippy --all-featurespasses