Thanks to visit codestin.com
Credit goes to lib.rs

#source-map #base64 #vlq

srcmap-codec

High-performance VLQ source map codec

13 releases

Uses new Rust 2024

0.3.6 Apr 17, 2026
0.3.5 Apr 13, 2026
0.3.0 Mar 15, 2026
0.2.3 Mar 14, 2026
0.1.3 Mar 10, 2026

#913 in Encoding

Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App Codestin Search App

2,369 downloads per month
Used in 9 crates (5 directly)

MIT license

52KB
949 lines

srcmap-codec

crates.io docs.rs CI Coverage

High-performance VLQ source map codec for Rust.

Encodes and decodes source map mappings strings using the Base64 VLQ format specified in ECMA-426 (Source Map v3). Drop-in Rust equivalent of @jridgewell/sourcemap-codec.

Install

[dependencies]
srcmap-codec = "0.3"

Usage

Decode and encode mappings

use srcmap_codec::{decode, encode};

let mappings = decode("AAAA;AACA,EAAE").unwrap();
assert_eq!(mappings.len(), 2); // 2 lines
assert_eq!(mappings[0][0], vec![0, 0, 0, 0]); // first segment

let encoded = encode(&mappings);
assert_eq!(encoded, "AAAA;AACA,EAAE");

Low-level VLQ primitives

use srcmap_codec::{vlq_decode, vlq_encode};

let mut buf = Vec::new();
vlq_encode(&mut buf, 42);

let (value, bytes_read) = vlq_decode(&buf, 0).unwrap();
assert_eq!(value, 42);

Parallel encoding

Enable the parallel feature for multi-threaded encoding via rayon. ~1.5x faster for large maps (5K+ lines).

[dependencies]
srcmap-codec = { version = "0.3", features = ["parallel"] }
use srcmap_codec::encode_parallel;

let encoded = encode_parallel(&mappings);

API

Function Description
decode(mappings) -> Result<SourceMapMappings> Decode a VLQ mappings string into lines of segments
encode(mappings) -> String Encode decoded mappings back to a VLQ string
encode_parallel(mappings) -> String Parallel encoding via rayon (requires parallel feature)
vlq_decode(bytes, offset) -> Result<(i64, usize)> Decode a single signed VLQ value at the given byte offset
vlq_encode(buf, value) Encode a single signed VLQ value and append to buffer
vlq_decode_unsigned(bytes, offset) -> Result<(u64, usize)> Decode a single unsigned VLQ value at the given byte offset
vlq_encode_unsigned(buf, value) Encode a single unsigned VLQ value and append to buffer

Types

type Segment = Vec<i64>;          // 1, 4, or 5 fields
type Line = Vec<Segment>;         // segments on one generated line
type SourceMapMappings = Vec<Line>; // all lines

Segments have 1, 4, or 5 fields:

  • 1 field: [generated_column]
  • 4 fields: [generated_column, source_index, original_line, original_column]
  • 5 fields: [generated_column, source_index, original_line, original_column, name_index]

Performance

Standard VLQ loop with a pre-computed base64 lookup table and continuation-bit processing. The encoder includes a single-char fast path for small values.

Part of srcmap

This crate is the foundation of the srcmap source map toolkit. See the main repository for the full suite including parser, generator, and remapping.

License

MIT

Dependencies

~0–250KB