Gathering detailed insights and metrics for smart-buffer
Gathering detailed insights and metrics for smart-buffer
Gathering detailed insights and metrics for smart-buffer
Gathering detailed insights and metrics for smart-buffer
safe-buffer
Safer Node.js Buffer API
safer-buffer
Modern Buffer API polyfill without footguns
@smithy/util-buffer-from
[![NPM version](https://img.shields.io/npm/v/@smithy/util-buffer-from/latest.svg)](https://www.npmjs.com/package/@smithy/util-buffer-from) [![NPM downloads](https://img.shields.io/npm/dm/@smithy/util-buffer-from.svg)](https://www.npmjs.com/package/@smithy
buffer
Node.js Buffer API, for the browser
smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
npm install smart-buffer
99.7
Supply Chain
100
Quality
76.2
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
102 Stars
92 Commits
19 Forks
4 Watching
14 Branches
5 Contributors
Updated on 03 Aug 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.2%
3,617,270
Compared to previous day
Last week
1.8%
21,157,925
Compared to previous week
Last month
8%
88,582,741
Compared to previous month
Last year
23.7%
901,427,599
Compared to previous year
smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
Key Features:
Requirements:
Legacy documentation for version 3 and prior can be found here.
yarn add smart-buffer
or
npm install smart-buffer
Note: The published NPM package includes the built javascript library. If you cloned this repo and wish to build the library manually use:
npm run build
1// Javascript 2const SmartBuffer = require('smart-buffer').SmartBuffer; 3 4// Typescript 5import { SmartBuffer, SmartBufferOptions} from 'smart-buffer';
Building a packet that uses the following protocol specification:
[PacketType:2][PacketLength:2][Data:XX]
To build this packet using the vanilla Buffer class, you would have to count up the length of the data payload beforehand. You would also need to keep track of the current "cursor" position in your Buffer so you write everything in the right places. With smart-buffer you don't have to do either of those things.
1function createLoginPacket(username, password, age, country) { 2 const packet = new SmartBuffer(); 3 packet.writeUInt16LE(0x0060); // Some packet type 4 packet.writeStringNT(username); 5 packet.writeStringNT(password); 6 packet.writeUInt8(age); 7 packet.writeStringNT(country); 8 packet.insertUInt16LE(packet.length - 2, 2); 9 10 return packet.toBuffer(); 11}
With the above function, you now can do this:
1const login = createLoginPacket("Josh", "secret123", 22, "United States"); 2 3// <Buffer 60 00 1e 00 4a 6f 73 68 00 73 65 63 72 65 74 31 32 33 00 16 55 6e 69 74 65 64 20 53 74 61 74 65 73 00>
Notice that the [PacketLength:2]
value (1e 00) was inserted at position 2.
Reading back the packet we created above is just as easy:
1 2const reader = SmartBuffer.fromBuffer(login); 3 4const logininfo = { 5 packetType: reader.readUInt16LE(), 6 packetLength: reader.readUInt16LE(), 7 username: reader.readStringNT(), 8 password: reader.readStringNT(), 9 age: reader.readUInt8(), 10 country: reader.readStringNT() 11}; 12 13/* 14{ 15 packetType: 96, (0x0060) 16 packetLength: 30, 17 username: 'Josh', 18 password: 'secret123', 19 age: 22, 20 country: 'United States' 21} 22*/
In prior versions of SmartBuffer, .writeXXX(value, offset) calls would insert data when an offset was provided. In version 4, this will now overwrite the data at the offset position. To insert data there are now corresponding .insertXXX(value, offset) methods.
SmartBuffer v3:
1const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6])); 2buff.writeInt8(7, 2); 3console.log(buff.toBuffer()) 4 5// <Buffer 01 02 07 03 04 05 06>
SmartBuffer v4:
1const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6])); 2buff.writeInt8(7, 2); 3console.log(buff.toBuffer()); 4 5// <Buffer 01 02 07 04 05 06>
To insert you instead should use:
1const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6])); 2buff.insertInt8(7, 2); 3console.log(buff.toBuffer()); 4 5// <Buffer 01 02 07 03 04 05 06>
Note: Insert/Writing to a position beyond the currently tracked internal Buffer will zero pad to your offset.
There are a few different ways to construct a SmartBuffer instance.
1// Creating SmartBuffer from existing Buffer 2const buff = SmartBuffer.fromBuffer(buffer); // Creates instance from buffer. (Uses default utf8 encoding) 3const buff = SmartBuffer.fromBuffer(buffer, 'ascii'); // Creates instance from buffer with ascii encoding for strings. 4 5// Creating SmartBuffer with specified internal Buffer size. (Note: this is not a hard cap, the internal buffer will grow as needed). 6const buff = SmartBuffer.fromSize(1024); // Creates instance with internal Buffer size of 1024. 7const buff = SmartBuffer.fromSize(1024, 'utf8'); // Creates instance with internal Buffer size of 1024, and utf8 encoding for strings. 8 9// Creating SmartBuffer with options object. This one specifies size and encoding. 10const buff = SmartBuffer.fromOptions({ 11 size: 1024, 12 encoding: 'ascii' 13}); 14 15// Creating SmartBuffer with options object. This one specified an existing Buffer. 16const buff = SmartBuffer.fromOptions({ 17 buff: buffer 18}); 19 20// Creating SmartBuffer from a string. 21const buff = SmartBuffer.fromBuffer(Buffer.from('some string', 'utf8')); 22 23// Just want a regular SmartBuffer with all default options? 24const buff = new SmartBuffer();
Note: SmartBuffer is fully documented with Typescript definitions as well as jsdocs so your favorite editor/IDE will have intellisense.
Table of Contents
options
{SmartBufferOptions} An optional options object to construct a SmartBuffer with.Examples:
1const buff = new SmartBuffer(); 2const buff = new SmartBuffer({ 3 size: 1024, 4 encoding: 'ascii' 5});
buffer
{Buffer} The Buffer instance to wrap.encoding
{string} The string encoding to use. Default: 'utf8'
Examples:
1const someBuffer = Buffer.from('some string'); 2const buff = SmartBuffer.fromBuffer(someBuffer); // Defaults to utf8 3const buff = SmartBuffer.fromBuffer(someBuffer, 'ascii');
size
{number} The size to initialize the internal Buffer.encoding
{string} The string encoding to use. Default: 'utf8'
Examples:
1const buff = SmartBuffer.fromSize(1024); // Defaults to utf8 2const buff = SmartBuffer.fromSize(1024, 'ascii');
options
{SmartBufferOptions} The Buffer instance to wrap.1interface SmartBufferOptions { 2 encoding?: BufferEncoding; // Defaults to utf8 3 size?: number; // Defaults to 4096 4 buff?: Buffer; 5}
Examples:
1const buff = SmartBuffer.fromOptions({ 2 size: 1024 3}; 4const buff = SmartBuffer.fromOptions({ 5 size: 1024, 6 encoding: 'utf8' 7}); 8const buff = SmartBuffer.fromOptions({ 9 encoding: 'utf8' 10}); 11 12const someBuff = Buffer.from('some string', 'utf8'); 13const buff = SmartBuffer.fromOptions({ 14 buffer: someBuff, 15 encoding: 'utf8' 16});
offset
{number} Optional position to start reading data from. Default: Auto managed offset
Read a Int8 value.
offset
{number} Optional position to start reading data from. Default: Auto managed offset
Read a 16 bit integer value.
offset
{number} Optional position to start reading data from. Default: Auto managed offset
Read a 32 bit integer value.
value
{number} The value to write.offset
{number} An optional offset to write this value to. Default: Auto managed offset
Write a Int8 value.
value
{number} The value to insert.offset
{number} The offset to insert this data at.Insert a Int8 value.
value
{number} The value to write.offset
{number} An optional offset to write this value to. Default: Auto managed offset
Write a 16 bit integer value.
value
{number} The value to insert.offset
{number} The offset to insert this data at.Insert a 16 bit integer value.
value
{number} The value to write.offset
{number} An optional offset to write this value to. Default: Auto managed offset
Write a 32 bit integer value.
value
{number} The value to insert.offset
{number} The offset to insert this data at.Insert a 32 bit integer value.
offset
{number} Optional position to start reading data from. Default: Auto managed offset
Read a Float value.
offset
{number} Optional position to start reading data from. Default: Auto managed offset
Read a Double value.
value
{number} The value to write.offset
{number} An optional offset to write this value to. Default: Auto managed offset
Write a Float value.
value
{number} The value to insert.offset
{number} The offset to insert this data at.Insert a Float value.
value
{number} The value to write.offset
{number} An optional offset to write this value to. Default: Auto managed offset
Write a Double value.
value
{number} The value to insert.offset
{number} The offset to insert this data at.Insert a Double value.
size
{number} The number of bytes to read. Default: Reads to the end of the Buffer.
encoding
{string} The string encoding to use. Default: utf8
.Read a string value.
Examples:
1const buff = SmartBuffer.fromBuffer(Buffer.from('hello there', 'utf8')); 2buff.readString(); // 'hello there' 3buff.readString(2); // 'he' 4buff.readString(2, 'utf8'); // 'he' 5buff.readString('utf8'); // 'hello there'
value
{string} The string value to write.offset
{number} The offset to write this value to. Default: Auto managed offset
encoding
{string} An optional string encoding to use. Default: utf8
Write a string value.
Examples:
1buff.writeString('hello'); // Auto managed offset 2buff.writeString('hello', 2); 3buff.writeString('hello', 'utf8') // Auto managed offset 4buff.writeString('hello', 2, 'utf8');
value
{string} The string value to write.offset
{number} The offset to write this value to.encoding
{string} An optional string encoding to use. Default: utf8
Insert a string value.
Examples:
1buff.insertString('hello', 2); 2buff.insertString('hello', 2, 'utf8');
encoding
{string} The string encoding to use. Default: utf8
.Read a null terminated string value. (If a null is not found, it will read to the end of the Buffer).
Examples:
1const buff = SmartBuffer.fromBuffer(Buffer.from('hello\0 there', 'utf8')); 2buff.readStringNT(); // 'hello' 3 4// If we called this again: 5buff.readStringNT(); // ' there'
value
{string} The string value to write.offset
{number} The offset to write this value to. Default: Auto managed offset
encoding
{string} An optional string encoding to use. Default: utf8
Write a null terminated string value.
Examples:
1buff.writeStringNT('hello'); // Auto managed offset <Buffer 68 65 6c 6c 6f 00> 2buff.writeStringNT('hello', 2); // <Buffer 00 00 68 65 6c 6c 6f 00> 3buff.writeStringNT('hello', 'utf8') // Auto managed offset 4buff.writeStringNT('hello', 2, 'utf8');
value
{string} The string value to write.offset
{number} The offset to write this value to.encoding
{string} An optional string encoding to use. Default: utf8
Insert a null terminated string value.
Examples:
1buff.insertStringNT('hello', 2); 2buff.insertStringNT('hello', 2, 'utf8');
length
{number} The number of bytes to read into a Buffer. Default: Reads to the end of the Buffer
Read a Buffer of a specified size.
value
{Buffer} The buffer value to write.offset
{number} An optional offset to write the value to. Default: Auto managed offset
value
{Buffer} The buffer value to write.offset
{number} The offset to write the value to.Read a null terminated Buffer.
value
{Buffer} The buffer value to write.offset
{number} An optional offset to write the value to. Default: Auto managed offset
Write a null terminated Buffer.
value
{Buffer} The buffer value to write.offset
{number} The offset to write the value to.Insert a null terminated Buffer.
offset
{number} The new read offset value to set.The current read offset
Gets or sets the current read offset.
Examples:
1const currentOffset = buff.readOffset; // 5 2 3buff.readOffset = 10; 4 5console.log(buff.readOffset) // 10
offset
{number} The new write offset value to set.The current write offset
Gets or sets the current write offset.
Examples:
1const currentOffset = buff.writeOffset; // 5 2 3buff.writeOffset = 10; 4 5console.log(buff.writeOffset) // 10
encoding
{string} The new string encoding to set.The current string encoding
Gets or sets the current string encoding.
Examples:
1const currentEncoding = buff.encoding; // 'utf8' 2 3buff.encoding = 'ascii'; 4 5console.log(buff.encoding) // 'ascii'
Clear and resets the SmartBuffer instance.
Remaining data left to be read
Gets the number of remaining bytes to be read.
Gets the internally managed Buffer (Includes unmanaged data).
Examples:
1const buff = SmartBuffer.fromSize(16); 2buff.writeString('hello'); 3console.log(buff.InternalBuffer); // <Buffer 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00>
Gets a sliced Buffer instance of the internally managed Buffer. (Only includes managed data)
Examples:
1const buff = SmartBuffer.fromSize(16); 2buff.writeString('hello'); 3console.log(buff.toBuffer()); // <Buffer 68 65 6c 6c 6f>
encoding
{string} The string encoding to use when converting to a string. Default: utf8
Gets a string representation of all data in the SmartBuffer.
Destroys the SmartBuffer instance.
This work is licensed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
36 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More