Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report
Generated on Dec 20, 2023 via pnpm

smart-buffer 1.0.3

A smarter Buffer that keeps track of its own read and write positions while growing endlessly.
Package summary
Share
0
issues
0
licenses
Package created
3 Nov 2013
Version published
7 Apr 2015
Maintainers
1
Total deps
0
Direct deps
0
License
MIT

Issues

0
This package has no issues

Frequently Asked Questions

What does smart-buffer do?

Smart-buffer is a package that serves as a wrapper for Buffer in Node.js, offering automated read & write offset tracking, data insertions, string operations, and more. It simplifies buffer management by growing the internal Buffer as needed, tracking read and write offsets automatically, and providing useful string operations such as null-terminating strings. The tool is built in TypeScript but also works in JavaScript environments given its provided type definitions.

How do you use smart-buffer?

To use smart-buffer in your project, you first need to install it using npm or yarn:

npm install smart-buffer
# or
yarn add smart-buffer

You can then require it in your JavaScript files as follows:

const SmartBuffer = require('smart-buffer').SmartBuffer;

If you are using TypeScript, import it like below:

import { SmartBuffer, SmartBufferOptions} from 'smart-buffer';

Smart-buffer allows you to read and write data in various ways. Here is an example of creating a login packet with a protocol specification and reading it back:

// Creating packet
function createLoginPacket(username, password, age, country) {
    const packet = new SmartBuffer();
    packet.writeUInt16LE(0x0060); // Some packet type
    packet.writeStringNT(username);
    packet.writeStringNT(password);
    packet.writeUInt8(age);
    packet.writeStringNT(country);
    packet.insertUInt16LE(packet.length - 2, 2);

    return packet.toBuffer();
}

// Using packet
const login = createLoginPacket("Josh", "secret123", 22, "United States");

// Reading packet
const reader = SmartBuffer.fromBuffer(login);
const logininfo = {
    packetType: reader.readUInt16LE(),
    packetLength: reader.readUInt16LE(),
    username: reader.readStringNT(),
    password: reader.readStringNT(),
    age: reader.readUInt8(),
    country: reader.readStringNT()
};

This package allows you to create packets without having to keep track of the current "cursor" position in your Buffer manually. It similarly reads back packets conveniently.

Where are the smart-buffer docs?

The complete documentation for smart-buffer, detailing all its methods and use-cases, is hosted on GitHub at https://github.com/JoshGlazebrook/smart-buffer/blob/master/README.md. Here, you can find the description and usage of various methods smart-buffer provides, including methods for numbers, strings, buffers, and offset operations among others.