Registry / serialization / leb
library1.0.0jsnpmunverified

The `leb` Node.js module provides a suite of utility functions for encoding and decoding integers using the LEB128 (Little-Endian Base 128) variable-length representation format. It supports both signed and unsigned values, with options for 32-bit integers, 64-bit integers (which return a `lossy` flag due to JavaScript's number precision limitations), and arbitrary-length buffer representations. Currently at `v1.0.0`, the package was recently resurrected and aims to provide a reliable, dependency-free solution for LEB128, a format notably used in the DWARF 3 debugging format and Android's DEX file format. The package maintains a stable API with no breaking changes in its recent major release and focuses on direct encoding/decoding operations for binary data within Node.js environments.

npm install leb
INSTALL
IMPORT
SIG · LEB
L
leb
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

decodeInt32
import { decodeInt32 } from 'leb'
const { decodeInt32 } = require('leb')
For decoding signed 32-bit LEB128 values from a Buffer.
encodeInt32
import { encodeInt32 } from 'leb'
const { encodeInt32 } = require('leb')
For encoding signed 32-bit integers into a LEB128 Buffer.
decodeUInt32
import { decodeUInt32 } from 'leb'
const { decodeUInt32 } = require('leb')
For decoding unsigned 32-bit LEB128 values from a Buffer.
encodeUInt32
import { encodeUInt32 } from 'leb'
const { encodeUInt32 } = require('leb')
For encoding unsigned 32-bit integers into a LEB128 Buffer.

This quickstart demonstrates how to encode and decode both signed and unsigned 32-bit integers using `leb`, including handling offsets in a concatenated buffer.

import { encodeInt32, decodeInt32, encodeUInt32, decodeUInt32 } from 'leb'; // Example: Encoding and decoding a signed 32-bit integer const signedNumber = -1234567; const encodedSigned = encodeInt32(signedNumber); console.log(`Encoded signed ${signedNumber}: ${encodedSigned.toString('hex')}`); const decodedSigned = decodeInt32(encodedSigned); console.log(`Decoded signed: ${decodedSigned.value} (nextIndex: ${decodedSigned.nextIndex})`); // Example: Encoding and decoding an unsigned 32-bit integer const unsignedNumber = 4294967295; // Max UInt32 const encodedUnsigned = encodeUInt32(unsignedNumber); console.log(`Encoded unsigned ${unsignedNumber}: ${encodedUnsigned.toString('hex')}`); const decodedUnsigned = decodeUInt32(encodedUnsigned); console.log(`Decoded unsigned: ${decodedUnsigned.value} (nextIndex: ${decodedUnsigned.nextIndex})`); // Example with an offset in a larger buffer const multiValueBuffer = Buffer.concat([ encodeInt32(100), encodeInt32(-500), encodeUInt32(250) ]); let currentOffset = 0; let result1 = decodeInt32(multiValueBuffer, currentOffset); console.log(` Value 1: ${result1.value}`); currentOffset = result1.nextIndex; let result2 = decodeInt32(multiValueBuffer, currentOffset); console.log(`Value 2: ${result2.value}`); currentOffset = result2.nextIndex; let result3 = decodeUInt32(multiValueBuffer, currentOffset); console.log(`Value 3: ${result3.value}`); console.log(`Final offset: ${result3.nextIndex}`);
Debug
Known issues
gotchaWhen decoding 64-bit integers, JavaScript's native number type cannot represent all possible 64-bit integer values precisely. The `decodeInt64` and `decodeUInt64` methods will return a `lossy` flag, which developers must check to determine if the decoded number exactly matches the encoded form.
fix
Always check the `lossy` flag when working with 64-bit LEB128 values, and consider using `BigInt` for full precision if the library supports buffer output for 64-bit values or if you're handling large numbers manually.
affects: >=1.0.0
gotchaThe `leb` package is designed for Node.js environments and relies on the Node.js `Buffer` API. It is not directly usable in a browser environment without a `Buffer` polyfill or a bundling process that handles Node.js-specific APIs.
fix
For browser usage, ensure you have a `Buffer` polyfill (e.g., `buffer` npm package) or configure your bundler (like Webpack or Rollup) to correctly resolve Node.js-specific modules for browser compatibility.
affects: >=1.0.0
gotchaAttempting to decode an LEB128 sequence from a `Buffer` that is too short, or contains an incomplete/malformed sequence at the specified index, will result in an exception (e.g., 'Ran out of bytes').
fix
Always validate the input `Buffer` and `index` to ensure enough bytes are available for a complete LEB128 value. Implement robust error handling around decoding calls to gracefully manage malformed input.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Buffer is not defined
Attempting to use `leb` in a browser environment without providing a global `Buffer` polyfill.
fix
In a browser environment, you need to install and configure a `Buffer` polyfill (e.g., `npm install buffer` and import it) or ensure your bundler (Webpack, Rollup) includes one.
Error: Value out of range for 32-bit signed integer
An input number provided to `encodeInt32` or `encodeUInt32` exceeds the representable range for its target 32-bit type.
fix
Ensure the input integer falls within the valid range for a 32-bit signed integer (-2,147,483,648 to 2,147,483,647) or a 32-bit unsigned integer (0 to 4,294,967,295). Use the appropriate 64-bit encoding functions (`encodeInt64`, `encodeUInt64`) for larger numbers.
Error: Ran out of bytes while decoding LEB128 value
The `Buffer` provided to a decode function (e.g., `decodeInt32`) does not contain enough bytes to complete the LEB128 sequence, or the last byte has its high bit set, indicating an incomplete sequence.
fix
Verify that the input `Buffer` contains a complete LEB128 encoded value starting from the given index. Ensure the buffer is not truncated and includes the final byte with its high bit unset.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
leb — npm install leb · libregistry