buffer-layout is a pure JavaScript utility library designed for translating between JavaScript values and Node.js Buffers. It enables developers to define and manipulate binary data structures that closely mimic C structs, offering explicit control over memory layout and endianness. The library provides layout constructors for various data types, including signed and unsigned integers (1 to 6 bytes, with 64-bit integers decoded as standard JavaScript Numbers), floats, doubles, sequences, complex structures, unions, bit fields, NUL-terminated C strings, and raw data blobs. The current version, 1.2.2, was last published in 2021. Given its age and lack of recent updates on its GitHub repository (last commit approximately four years ago), the project appears to be in an abandoned state, with no active development or maintenance. A key differentiator is its detailed control over C-style memory layouts, including the necessity of manually accounting for padding and bit fields.
npm install buffer-layoutVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining both packed and aligned C-style structs using `buffer-layout`, showing how to encode JavaScript objects into Buffers and decode them back, including explicit padding for alignment.
Consider migrating to a maintained alternative like `@solana/buffer-layout` which is a TypeScript-enabled fork with active maintenance and similar API.
Use `lo.seq(lo.u8(), N)` or `lo.blob(N)` with no property name to insert `N` bytes of padding where required by alignment rules of the target C structure.
For precise handling of 64-bit integers, use layouts that return `BigInt` (if available in a fork/alternative) or implement manual conversion using `BigInt` after decoding the raw bytes. The original `buffer-layout` library does not natively support `BigInt`.
Install `@types/buffer-layout` if available, or consider migrating to `@solana/buffer-layout`, which is a TypeScript-first fork and provides native types.
Ensure you are using CommonJS `const lo = require('buffer-layout');` in a CJS file, or configure your bundler to handle CJS modules in an ESM project. If using native ESM, you might need to use `import * as lo from 'buffer-layout';` or ensure your `package.json` specifies `"type": "commonjs"` for the file.Review your layout definition against the target binary format or C struct definition. Pay close attention to data types, endianness, and explicit padding needed for memory alignment. Use `Buffer.compare` or `Buffer.equals` for byte-by-byte comparison during debugging.
Verify that the `Buffer.alloc()` size is sufficient for the defined layout (`layout.span`). Double-check any manual offset parameters passed to `encode` or `decode` methods.
No dependency data recorded yet.