{"id":11233,"library":"leb","title":"LEB128 Encoding and Decoding Utilities","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/danfuzz/leb","tags":["javascript","leb","leb128","uleb128","int","uint","encoding","decoding","encode"],"install":[{"cmd":"npm install leb","lang":"bash","label":"npm"},{"cmd":"yarn add leb","lang":"bash","label":"yarn"},{"cmd":"pnpm add leb","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For decoding signed 32-bit LEB128 values from a Buffer.","wrong":"const { decodeInt32 } = require('leb')","symbol":"decodeInt32","correct":"import { decodeInt32 } from 'leb'"},{"note":"For encoding signed 32-bit integers into a LEB128 Buffer.","wrong":"const { encodeInt32 } = require('leb')","symbol":"encodeInt32","correct":"import { encodeInt32 } from 'leb'"},{"note":"For decoding unsigned 32-bit LEB128 values from a Buffer.","wrong":"const { decodeUInt32 } = require('leb')","symbol":"decodeUInt32","correct":"import { decodeUInt32 } from 'leb'"},{"note":"For encoding unsigned 32-bit integers into a LEB128 Buffer.","wrong":"const { encodeUInt32 } = require('leb')","symbol":"encodeUInt32","correct":"import { encodeUInt32 } from 'leb'"}],"quickstart":{"code":"import { encodeInt32, decodeInt32, encodeUInt32, decodeUInt32 } from 'leb';\n\n// Example: Encoding and decoding a signed 32-bit integer\nconst signedNumber = -1234567;\nconst encodedSigned = encodeInt32(signedNumber);\nconsole.log(`Encoded signed ${signedNumber}: ${encodedSigned.toString('hex')}`);\nconst decodedSigned = decodeInt32(encodedSigned);\nconsole.log(`Decoded signed: ${decodedSigned.value} (nextIndex: ${decodedSigned.nextIndex})`);\n\n// Example: Encoding and decoding an unsigned 32-bit integer\nconst unsignedNumber = 4294967295; // Max UInt32\nconst encodedUnsigned = encodeUInt32(unsignedNumber);\nconsole.log(`Encoded unsigned ${unsignedNumber}: ${encodedUnsigned.toString('hex')}`);\nconst decodedUnsigned = decodeUInt32(encodedUnsigned);\nconsole.log(`Decoded unsigned: ${decodedUnsigned.value} (nextIndex: ${decodedUnsigned.nextIndex})`);\n\n// Example with an offset in a larger buffer\nconst multiValueBuffer = Buffer.concat([\n  encodeInt32(100),\n  encodeInt32(-500),\n  encodeUInt32(250)\n]);\n\nlet currentOffset = 0;\nlet result1 = decodeInt32(multiValueBuffer, currentOffset);\nconsole.log(`\nValue 1: ${result1.value}`);\ncurrentOffset = result1.nextIndex;\n\nlet result2 = decodeInt32(multiValueBuffer, currentOffset);\nconsole.log(`Value 2: ${result2.value}`);\ncurrentOffset = result2.nextIndex;\n\nlet result3 = decodeUInt32(multiValueBuffer, currentOffset);\nconsole.log(`Value 3: ${result3.value}`);\nconsole.log(`Final offset: ${result3.nextIndex}`);","lang":"javascript","description":"This quickstart demonstrates how to encode and decode both signed and unsigned 32-bit integers using `leb`, including handling offsets in a concatenated buffer."},"warnings":[{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Attempting 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').","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use `leb` in a browser environment without providing a global `Buffer` polyfill.","error":"TypeError: Buffer is not defined"},{"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.","cause":"An input number provided to `encodeInt32` or `encodeUInt32` exceeds the representable range for its target 32-bit type.","error":"Error: Value out of range for 32-bit signed integer"},{"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.","cause":"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.","error":"Error: Ran out of bytes while decoding LEB128 value"}],"ecosystem":"npm"}