smart-buffer
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@types/node | 7.10.14 | 32.47 kB | MIT | prod |
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.
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.
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.