npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@thi.ng/leb128

v3.1.71

Published

WASM based LEB128 encoder / decoder (signed & unsigned)

Readme

@thi.ng/leb128

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 210 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

WASM based Little Endian Base 128 varint encoding / decoding, supporting the full (u)int64 range.

The WASM binary (~860 bytes) is embedded as base64 string in the TypeScript source to make it easier to use in both browser & node environments. The source code of the actual implementation (written in Zig) is included in /zig/leb128.zig

All public functions throw an error if the WASM module could not be initialized.

The encodeSLEB128Into() and encodeULEB128Into() functions will check the bounds of the target array to ensure all bytes can be written and will throw an error if the result would go out of bounds.

References:

  • https://en.wikipedia.org/wiki/LEB128
  • http://webassembly.github.io/spec/core/binary/values.html#integers

Breaking changes

v3.0.0 introduces JS bigint support and both decode functions return a tuple of [bigint, number] with the bigint being the decoded value and the 2nd item the number of bytes consumed. Simarly, the encode functions now accept a JS number or bigint arg.

Furthermore, all values to be encoded/decoded are cast to i64/u64 range now.

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/leb128

ESM import:

import * as leb from "@thi.ng/leb128";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/leb128"></script>

JSDelivr documentation

For Node.js REPL:

const leb = await import("@thi.ng/leb128");

Package sizes (brotli'd, pre-treeshake): ESM: 1019 bytes

Dependencies

API

Generated API docs

import * as leb from "@thi.ng/leb128";

// if WASM is unavailable, the encode/decode functions will throw an error
let encoded = leb.encodeULEB128(Number.MAX_SAFE_INTEGER);

console.log(encoded);
// Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15 ]

// decoding returns tuple of [value (bigint), bytes consumed]
console.log(leb.decodeULEB128(encoded));
// [ 9007199254740991n, 8 ]

// encode signed int
encoded = leb.encodeSLEB128(Number.MIN_SAFE_INTEGER);

console.log(encoded)
// Uint8Array [ 129, 128, 128, 128, 128, 128, 128, 112 ]

console.log(leb.decodeSLEB128(encoded));
// [ -9007199254740991n, 8 ]

// when writing into an existing buffer, there needs to be enough bytes to write the value
const target = new Uint8Array(10);
const count = leb.encodeULEB128Into(target, Number.MAX_SAFE_INTEGER);

console.log(target);
// Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15, 0, 0 ]

console.log(count);
// 8

Building the binary

Requirements:

# install required tools
brew install zig binaryen

# first run native tests
zig test zig/leb128.zig
# Test 1/2 min safe integer...OK
# Test 2/2 max safe integer...OK
# All tests passed.

# build binary and regenerate src/binary.ts
yarn build:binary

# test TS/JS version
yarn test

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-leb128,
  title = "@thi.ng/leb128",
  author = "Karsten Schmidt and others",
  note = "https://thi.ng/leb128",
  year = 2019
}

License

© 2019 - 2025 Karsten Schmidt // Apache License 2.0