Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

smart-buffer

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smart-buffer - npm Package Compare versions

Comparing version 4.0.0-beta.1 to 4.0.0

docs/CHANGELOG.md

1042

build/smartbuffer.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const buffer_1 = require("buffer");
const utils_1 = require("./utils");

@@ -11,6 +10,6 @@ // The default Buffer size if one is not provided.

/**
* Creates a new SmartBuffer instance.
*
* @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
*/
* Creates a new SmartBuffer instance.
*
* @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
*/
constructor(options) {

@@ -30,3 +29,3 @@ this.length = 0;

if (utils_1.isFiniteInteger(options.size) && options.size > 0) {
this._buff = buffer_1.Buffer.allocUnsafe(options.size);
this._buff = Buffer.allocUnsafe(options.size);
}

@@ -39,3 +38,3 @@ else {

else if (options.buff) {
if (options.buff instanceof buffer_1.Buffer) {
if (options.buff instanceof Buffer) {
this._buff = options.buff;

@@ -49,3 +48,3 @@ this.length = options.buff.length;

else {
this._buff = buffer_1.Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
}

@@ -59,13 +58,13 @@ }

// Otherwise default to sane options
this._buff = buffer_1.Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
}
}
/**
* Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
*
* @param size { Number } The size of the internal Buffer.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
* Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
*
* @param size { Number } The size of the internal Buffer.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
static fromSize(size, encoding) {

@@ -78,9 +77,9 @@ return new this({

/**
* Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
*
* @param buffer { Buffer } The Buffer to use as the internal Buffer value.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
* Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
*
* @param buffer { Buffer } The Buffer to use as the internal Buffer value.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
static fromBuffer(buff, encoding) {

@@ -93,6 +92,6 @@ return new this({

/**
* Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
*
* @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
*/
* Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
*
* @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
*/
static fromOptions(options) {

@@ -102,4 +101,4 @@ return new this(options);

/**
* Type checking function that determines if an object is a SmartBufferOptions object.
*/
* Type checking function that determines if an object is a SmartBufferOptions object.
*/
static isSmartBufferOptions(options) {

@@ -111,164 +110,164 @@ const castOptions = options;

/**
* Reads an Int8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt8(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt8, 1, offset);
return this._readNumberValue(Buffer.prototype.readInt8, 1, offset);
}
/**
* Reads an Int16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt16BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt16BE, 2, offset);
return this._readNumberValue(Buffer.prototype.readInt16BE, 2, offset);
}
/**
* Reads an Int16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt16LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt16LE, 2, offset);
return this._readNumberValue(Buffer.prototype.readInt16LE, 2, offset);
}
/**
* Reads an Int32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt32BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt32BE, 4, offset);
return this._readNumberValue(Buffer.prototype.readInt32BE, 4, offset);
}
/**
* Reads an Int32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt32LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt32LE, 4, offset);
return this._readNumberValue(Buffer.prototype.readInt32LE, 4, offset);
}
/**
* Writes an Int8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt8(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeInt8, 1, value, offset);
this._writeNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
return this;
}
/**
* Inserts an Int8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt8(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt8, 1, value, offset);
this._insertNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
return this;
}
/**
* Writes an Int16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt16BE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeInt16BE, 2, value, offset);
this._writeNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
return this;
}
/**
* Inserts an Int16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt16BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt16BE, 2, value, offset);
this._insertNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
return this;
}
/**
* Writes an Int16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt16LE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeInt16LE, 2, value, offset);
this._writeNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
return this;
}
/**
* Inserts an Int16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt16LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt16LE, 2, value, offset);
this._insertNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
return this;
}
/**
* Writes an Int32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt32BE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeInt32BE, 4, value, offset);
this._writeNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
return this;
}
/**
* Inserts an Int32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt32BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt32BE, 4, value, offset);
this._insertNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
return this;
}
/**
* Writes an Int32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt32LE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeInt32LE, 4, value, offset);
this._writeNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
return this;
}
/**
* Inserts an Int32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt32LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt32LE, 4, value, offset);
this._insertNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
return this;

@@ -278,164 +277,164 @@ }

/**
* Reads an UInt8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt8(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt8, 1, offset);
return this._readNumberValue(Buffer.prototype.readUInt8, 1, offset);
}
/**
* Reads an UInt16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt16BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt16BE, 2, offset);
return this._readNumberValue(Buffer.prototype.readUInt16BE, 2, offset);
}
/**
* Reads an UInt16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt16LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt16LE, 2, offset);
return this._readNumberValue(Buffer.prototype.readUInt16LE, 2, offset);
}
/**
* Reads an UInt32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt32BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt32BE, 4, offset);
return this._readNumberValue(Buffer.prototype.readUInt32BE, 4, offset);
}
/**
* Reads an UInt32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt32LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt32LE, 4, offset);
return this._readNumberValue(Buffer.prototype.readUInt32LE, 4, offset);
}
/**
* Writes an UInt8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt8(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeUInt8, 1, value, offset);
this._writeNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
return this;
}
/**
* Inserts an UInt8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt8(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt8, 1, value, offset);
this._insertNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
return this;
}
/**
* Writes an UInt16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt16BE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeUInt16BE, 2, value, offset);
this._writeNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
return this;
}
/**
* Inserts an UInt16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt16BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt16BE, 2, value, offset);
this._insertNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
return this;
}
/**
* Writes an UInt16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt16LE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeUInt16LE, 2, value, offset);
this._writeNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
return this;
}
/**
* Inserts an UInt16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt16LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt16LE, 2, value, offset);
this._insertNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
return this;
}
/**
* Writes an UInt32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt32BE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeUInt32BE, 4, value, offset);
this._writeNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
return this;
}
/**
* Inserts an UInt32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt32BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt32BE, 4, value, offset);
this._insertNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
return this;
}
/**
* Writes an UInt32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt32LE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeUInt32LE, 4, value, offset);
this._writeNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
return this;
}
/**
* Inserts an UInt32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt32LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt32LE, 4, value, offset);
this._insertNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
return this;

@@ -445,65 +444,65 @@ }

/**
* Reads an FloatBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an FloatBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readFloatBE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readFloatBE, 4, offset);
return this._readNumberValue(Buffer.prototype.readFloatBE, 4, offset);
}
/**
* Reads an FloatLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an FloatLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readFloatLE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readFloatLE, 4, offset);
return this._readNumberValue(Buffer.prototype.readFloatLE, 4, offset);
}
/**
* Writes a FloatBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a FloatBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeFloatBE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeFloatBE, 4, value, offset);
this._writeNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
return this;
}
/**
* Inserts a FloatBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a FloatBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertFloatBE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeFloatBE, 4, value, offset);
this._insertNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
return this;
}
/**
* Writes a FloatLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a FloatLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeFloatLE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeFloatLE, 4, value, offset);
this._writeNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
return this;
}
/**
* Inserts a FloatLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a FloatLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertFloatLE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeFloatLE, 4, value, offset);
this._insertNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
return this;

@@ -513,65 +512,65 @@ }

/**
* Reads an DoublEBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an DoublEBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readDoubleBE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readDoubleBE, 8, offset);
return this._readNumberValue(Buffer.prototype.readDoubleBE, 8, offset);
}
/**
* Reads an DoubleLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an DoubleLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readDoubleLE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readDoubleLE, 8, offset);
return this._readNumberValue(Buffer.prototype.readDoubleLE, 8, offset);
}
/**
* Writes a DoubleBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a DoubleBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeDoubleBE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeDoubleBE, 8, value, offset);
this._writeNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
return this;
}
/**
* Inserts a DoubleBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a DoubleBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertDoubleBE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeDoubleBE, 8, value, offset);
this._insertNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
return this;
}
/**
* Writes a DoubleLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a DoubleLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeDoubleLE(value, offset) {
this.writeNumberValue(buffer_1.Buffer.prototype.writeDoubleLE, 8, value, offset);
this._writeNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
return this;
}
/**
* Inserts a DoubleLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a DoubleLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertDoubleLE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeDoubleLE, 8, value, offset);
this._insertNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
return this;

@@ -581,10 +580,10 @@ }

/**
* Reads a String from the current read position.
*
* @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for
* the string (Defaults to instance level encoding).
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
* Reads a String from the current read position.
*
* @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for
* the string (Defaults to instance level encoding).
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
readString(arg1, encoding) {

@@ -610,8 +609,8 @@ let lengthVal;

/**
* Inserts a String
*
* @param value { String } The String value to insert.
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Inserts a String
*
* @param value { String } The String value to insert.
* @param offset { Number } The offset to insert the string at.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
insertString(value, offset, encoding) {

@@ -622,8 +621,8 @@ utils_1.checkOffsetValue(offset);

/**
* Writes a String
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Writes a String
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
writeString(value, arg2, encoding) {

@@ -633,8 +632,8 @@ return this._handleString(value, false, arg2, encoding);

/**
* Reads a null-terminated String from the current read position.
*
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
* Reads a null-terminated String from the current read position.
*
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
readStringNT(encoding) {

@@ -660,8 +659,8 @@ if (typeof encoding !== 'undefined') {

/**
* Inserts a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Inserts a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
insertStringNT(value, offset, encoding) {

@@ -674,21 +673,21 @@ utils_1.checkOffsetValue(offset);

/**
* Writes a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Writes a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
writeStringNT(value, arg2, encoding) {
// Write Values
this.writeString(value, arg2, encoding);
this.writeUInt8(0x00, (typeof arg2 === 'number' ? arg2 + value.length : this.writeOffset));
this.writeUInt8(0x00, typeof arg2 === 'number' ? arg2 + value.length : this.writeOffset);
}
// Buffers
/**
* Reads a Buffer from the internal read position.
*
* @param length { Number } The length of data to read as a Buffer.
*
* @return { Buffer }
*/
* Reads a Buffer from the internal read position.
*
* @param length { Number } The length of data to read as a Buffer.
*
* @return { Buffer }
*/
readBuffer(length) {

@@ -707,7 +706,7 @@ if (typeof length !== 'undefined') {

/**
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
insertBuffer(value, offset) {

@@ -718,7 +717,7 @@ utils_1.checkOffsetValue(offset);

/**
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
writeBuffer(value, offset) {

@@ -728,6 +727,6 @@ return this._handleBuffer(value, false, offset);

/**
* Reads a null-terminated Buffer from the current read poisiton.
*
* @return { Buffer }
*/
* Reads a null-terminated Buffer from the current read poisiton.
*
* @return { Buffer }
*/
readBufferNT() {

@@ -750,7 +749,7 @@ // Set null character position to the end SmartBuffer instance.

/**
* Inserts a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
* Inserts a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
insertBufferNT(value, offset) {

@@ -764,7 +763,7 @@ utils_1.checkOffsetValue(offset);

/**
* Writes a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
* Writes a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
writeBufferNT(value, offset) {

@@ -777,8 +776,8 @@ // Checks for valid numberic value;

this.writeBuffer(value, offset);
this.writeUInt8(0x00, (typeof offset === 'number' ? offset + value.length : this._writeOffset));
this.writeUInt8(0x00, typeof offset === 'number' ? offset + value.length : this._writeOffset);
return this;
}
/**
* Clears the SmartBuffer instance to its original empty state.
*/
* Clears the SmartBuffer instance to its original empty state.
*/
clear() {

@@ -788,8 +787,9 @@ this._writeOffset = 0;

this.length = 0;
return this;
}
/**
* Gets the remaining data left to be read from the SmartBuffer instance.
*
* @return { Number }
*/
* Gets the remaining data left to be read from the SmartBuffer instance.
*
* @return { Number }
*/
remaining() {

@@ -799,6 +799,6 @@ return this.length - this._readOffset;

/**
* Gets the current read offset value of the SmartBuffer instance.
*
* @return { Number }
*/
* Gets the current read offset value of the SmartBuffer instance.
*
* @return { Number }
*/
get readOffset() {

@@ -808,6 +808,6 @@ return this._readOffset;

/**
* Sets the read offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
* Sets the read offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
set readOffset(offset) {

@@ -820,6 +820,6 @@ utils_1.checkOffsetValue(offset);

/**
* Gets the current write offset value of the SmartBuffer instance.
*
* @return { Number }
*/
* Gets the current write offset value of the SmartBuffer instance.
*
* @return { Number }
*/
get writeOffset() {

@@ -829,6 +829,6 @@ return this._writeOffset;

/**
* Sets the write offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
* Sets the write offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
set writeOffset(offset) {

@@ -841,6 +841,6 @@ utils_1.checkOffsetValue(offset);

/**
* Gets the currently set string encoding of the SmartBuffer instance.
*
* @return { BufferEncoding } The string Buffer encoding currently set.
*/
* Gets the currently set string encoding of the SmartBuffer instance.
*
* @return { BufferEncoding } The string Buffer encoding currently set.
*/
get encoding() {

@@ -850,6 +850,6 @@ return this._encoding;

/**
* Sets the string encoding of the SmartBuffer instance.
*
* @param encoding { BufferEncoding } The string Buffer encoding to set.
*/
* Sets the string encoding of the SmartBuffer instance.
*
* @param encoding { BufferEncoding } The string Buffer encoding to set.
*/
set encoding(encoding) {

@@ -860,6 +860,6 @@ utils_1.checkEncoding(encoding);

/**
* Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
*
* @return { Buffer } The Buffer value.
*/
* Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
*
* @return { Buffer } The Buffer value.
*/
get internalBuffer() {

@@ -869,6 +869,6 @@ return this._buff;

/**
* Gets the value of the internal managed Buffer (Includes managed data only)
*
* @param { Buffer }
*/
* Gets the value of the internal managed Buffer (Includes managed data only)
*
* @param { Buffer }
*/
toBuffer() {

@@ -878,6 +878,6 @@ return this._buff.slice(0, this.length);

/**
* Gets the String value of the internal managed Buffer
*
* @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
*/
* Gets the String value of the internal managed Buffer
*
* @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
*/
toString(encoding) {

@@ -890,15 +890,16 @@ const encodingVal = typeof encoding === 'string' ? encoding : this._encoding;

/**
* Destroys the SmartBuffer instance.
*/
* Destroys the SmartBuffer instance.
*/
destroy() {
this.clear();
return this;
}
/**
* Handles inserting and writing strings.
*
* @param value { String } The String value to insert.
* @param isInsert { Boolean } True if inserting a string, false if writing.
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Handles inserting and writing strings.
*
* @param value { String } The String value to insert.
* @param isInsert { Boolean } True if inserting a string, false if writing.
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
_handleString(value, isInsert, arg3, encoding) {

@@ -922,3 +923,3 @@ let offsetVal = this._writeOffset;

// Calculate bytelength of string.
const byteLength = buffer_1.Buffer.byteLength(value, encodingVal);
const byteLength = Buffer.byteLength(value, encodingVal);
// Ensure there is enough internal Buffer capacity.

@@ -929,3 +930,3 @@ if (isInsert) {

else {
this.ensureWriteable(byteLength, offsetVal);
this._ensureWriteable(byteLength, offsetVal);
}

@@ -944,2 +945,3 @@ // Write value

else {
// If no offset was given, we wrote to the end of the SmartBuffer so increment writeOffset.
this._writeOffset += byteLength;

@@ -951,7 +953,7 @@ }

/**
* Handles writing or insert of a Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
* Handles writing or insert of a Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
_handleBuffer(value, isInsert, offset) {

@@ -964,3 +966,3 @@ const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;

else {
this.ensureWriteable(value.length, offsetVal);
this._ensureWriteable(value.length, offsetVal);
}

@@ -979,2 +981,3 @@ // Write buffer value

else {
// If no offset was given, we wrote to the end of the SmartBuffer so increment writeOffset.
this._writeOffset += value.length;

@@ -986,7 +989,7 @@ }

/**
* Ensures that the internal Buffer is large enough to read data.
*
* @param length { Number } The length of the data that needs to be read.
* @param offset { Number } The offset of the data that needs to be read.
*/
* Ensures that the internal Buffer is large enough to read data.
*
* @param length { Number } The length of the data that needs to be read.
* @param offset { Number } The offset of the data that needs to be read.
*/
ensureReadable(length, offset) {

@@ -1008,7 +1011,7 @@ // Offset value defaults to managed read offset.

/**
* Ensures that the internal Buffer is large enough to insert data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written.
*/
* Ensures that the internal Buffer is large enough to insert data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written.
*/
ensureInsertable(dataLength, offset) {

@@ -1018,3 +1021,3 @@ // Checks for valid numberic value;

// Ensure there is enough internal Buffer capacity.
this.ensureCapacity(this.length + dataLength);
this._ensureCapacity(this.length + dataLength);
// If an offset was provided and its not the very end of the buffer, copy data into appropriate location in regards to the offset.

@@ -1033,11 +1036,11 @@ if (offset < this.length) {

/**
* Ensures that the internal Buffer is large enough to write data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written (defaults to writeOffset).
*/
ensureWriteable(dataLength, offset) {
* Ensures that the internal Buffer is large enough to write data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written (defaults to writeOffset).
*/
_ensureWriteable(dataLength, offset) {
const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;
// Ensure enough capacity to write data.
this.ensureCapacity(offsetVal + dataLength);
this._ensureCapacity(offsetVal + dataLength);
// Adjust SmartBuffer length (if offset + length is larger than managed length, adjust length)

@@ -1049,15 +1052,15 @@ if (offsetVal + dataLength > this.length) {

/**
* Ensures that the internal Buffer is large enough to write at least the given amount of data.
*
* @param minLength { Number } The minimum length of the data needs to be written.
*/
ensureCapacity(minLength) {
* Ensures that the internal Buffer is large enough to write at least the given amount of data.
*
* @param minLength { Number } The minimum length of the data needs to be written.
*/
_ensureCapacity(minLength) {
const oldLength = this._buff.length;
if (minLength > oldLength) {
let data = this._buff;
let newLength = (oldLength * 3) / 2 + 1;
let newLength = oldLength * 3 / 2 + 1;
if (newLength < minLength) {
newLength = minLength;
}
this._buff = buffer_1.Buffer.allocUnsafe(newLength);
this._buff = Buffer.allocUnsafe(newLength);
data.copy(this._buff, 0, 0, oldLength);

@@ -1067,11 +1070,11 @@ }

/**
* Reads a numeric number value using the provided function.
*
* @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
* @param byteSize { Number } The number of bytes read.
* @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead.
*
* @param { Number }
*/
readNumberValue(func, byteSize, offset) {
* Reads a numeric number value using the provided function.
*
* @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
* @param byteSize { Number } The number of bytes read.
* @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead.
*
* @param { Number }
*/
_readNumberValue(func, byteSize, offset) {
this.ensureReadable(byteSize, offset);

@@ -1087,11 +1090,11 @@ // Call Buffer.readXXXX();

/**
* Inserts a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
insertNumberValue(func, byteSize, value, offset) {
* Inserts a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
_insertNumberValue(func, byteSize, value, offset) {
// Check for invalid offset values.

@@ -1107,11 +1110,11 @@ utils_1.checkOffsetValue(offset);

/**
* Writes a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
writeNumberValue(func, byteSize, value, offset) {
* Writes a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
_writeNumberValue(func, byteSize, value, offset) {
// If an offset was provided, validate it.

@@ -1128,3 +1131,3 @@ if (typeof offset === 'number') {

// Ensure there is enough internal Buffer capacity. (raw offset is passed)
this.ensureWriteable(byteSize, offsetVal);
this._ensureWriteable(byteSize, offsetVal);
func.call(this._buff, value, offsetVal);

@@ -1136,2 +1139,3 @@ // If an offset was given, check to see if we wrote beyond the current writeOffset.

else {
// If no numeric offset was given, we wrote to the end of the SmartBuffer so increment writeOffset.
this._writeOffset += byteSize;

@@ -1138,0 +1142,0 @@ }

@@ -16,2 +16,3 @@ "use strict";

INVALID_TARGET_OFFSET: 'Target offset is beyond the bounds of the internal SmartBuffer data.',
INVALID_TARGET_LENGTH: 'Specified length value moves cursor beyong the bounds of the internal SmartBuffer data.',
INVALID_READ_BEYOND_BOUNDS: 'Attempted to read beyond the bounds of the managed data.',

@@ -93,6 +94,4 @@ INVALID_WRITE_BEYOND_BOUNDS: 'Attempted to write beyond the bounds of the managed data.'

function isInteger(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
}
//# sourceMappingURL=utils.js.map
{
"name": "smart-buffer",
"version": "4.0.0-beta.1",
"description": "A smarter Buffer that keeps track of its own read and write positions while growing endlessly.",
"version": "4.0.0",
"description": "smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.",
"main": "build/smartbuffer.js",

@@ -31,15 +31,15 @@ "homepage": "https://github.com/JoshGlazebrook/smart-buffer/",

"devDependencies": {
"@types/chai": "^4.0.1",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.2",
"chai": "^4.0.2",
"coveralls": "^2.13.1",
"@types/chai": "4.0.4",
"@types/mocha": "2.2.43",
"@types/node": "8.0.46",
"chai": "4.1.2",
"coveralls": "3.0.0",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"mocha": "4.0.1",
"mocha-lcov-reporter": "^1.3.0",
"nyc": "^11.0.2",
"source-map-support": "^0.4.15",
"ts-node": "^3.1.0",
"tslint": "^5.4.3",
"typescript": "^2.3.4"
"nyc": "11.2.1",
"source-map-support": "0.5.0",
"ts-node": "3.3.0",
"tslint": "5.8.0",
"typescript": "2.5.3"
},

@@ -49,6 +49,6 @@ "typings": "typings",

"scripts": {
"prepublish": "npm install -g typescript && tsc -p ./",
"test": "NODE_ENV=test mocha --recursive --compilers ts:ts-node/register",
"cover": "NODE_ENV=test nyc npm test",
"coveralls": "NODE_ENV=test nyc report --reporter=text-lcov | coveralls",
"prepublish": "npm install -g typescript && npm run build",
"test": "NODE_ENV=test mocha --recursive --compilers ts:ts-node/register test/**/*.ts",
"coverage": "NODE_ENV=test nyc npm test",
"coveralls": "NODE_ENV=test nyc npm test && nyc report --reporter=text-lcov | coveralls",
"lint": "tslint --type-check --project tsconfig.json 'src/**/*.ts'",

@@ -55,0 +55,0 @@ "build": "tsc -p ./"

smart-buffer [![Build Status](https://travis-ci.org/JoshGlazebrook/smart-buffer.svg?branch=master)](https://travis-ci.org/JoshGlazebrook/smart-buffer) [![Coverage Status](https://coveralls.io/repos/github/JoshGlazebrook/smart-buffer/badge.svg?branch=master)](https://coveralls.io/github/JoshGlazebrook/smart-buffer?branch=master)
=============
smart-buffer is a light Buffer wrapper that takes away the need to keep track of what position to read and write data to and from the underlying Buffer. It also adds null terminating string operations and **grows** as you add more data.
smart-buffer is a Buffer wrapper that adds automatic read & write offset tracking, string operations, data insertions, and more.
![stats](https://nodei.co/npm/smart-buffer.png?downloads=true&downloadRank=true&stars=true "stats")
### What it's useful for:
I created smart-buffer because I wanted to simplify the process of using Buffer for building and reading network packets to send over a socket. Rather than having to keep track of which position I need to write a UInt16 to after adding a string of variable length, I simply don't have to.
Key Features:
* Proxies all of the Buffer write and read functions.
* Keeps track of read and write positions for you.
* Grows the internal Buffer as you add data to it.
**Key Features**:
* Proxies all of the Buffer write and read functions
* Keeps track of read and write offsets automatically
* Grows the internal Buffer as needed
* Useful string operations. (Null terminating strings)
* Allows for inserting values at specific points in the internal Buffer.
* Allows for inserting values at specific points in the Buffer
* Built in TypeScript
* Type Definitions Provided
* Browser Support (using Webpack/Browserify)
* Full test coverage
Requirements:
**Requirements**:
* Node v4.0+ is supported at this time. (Versions prior to 2.0 will work on node 0.10)
#### Note:
smart-buffer can be used for writing to an underlying buffer as well as reading from it. It however does not function correctly if you're mixing both read and write operations with each other.
## Breaking Changes with 2.0
The latest version (2.0+) is written in TypeScript, and are compiled to ES6 Javascript. This means the earliest Node.js it supports will be 4.x (in strict mode.) If you're using version 6 and above it will work without any issues. From an API standpoint, 2.0 is backwards compatible. The only difference is SmartBuffer is not exported directly as the root module.
## Breaking Changes in v4.0
## Breaking Changes with 3.0
Starting with 3.0, if any of the readIntXXXX() methods are called and the requested data is larger than the bounds of the internally managed valid buffer data, an exception will now be thrown.
* Old constructor patterns have been completely removed. It's now required to use the SmartBuffer.fromXXX() factory constructors.
* rewind(), skip(), moveTo() have been removed. (see setting internal read and write offsets)
* Internal private properties are now prefixed with underscores (_)
* **All** writeXXX() methods that are given an offset will now **overwrite data** instead of insert. (see [write vs insert](#write-vs-insert))
* insertXXX() methods have been added for when you want to insert data at a specific offset (this replaces the old behavior of writeXXX() when an offset was provided)
## Looking for v3 docs?
Legacy documentation for version 3 and prior can be found [here](https://github.com/JoshGlazebrook/smart-buffer/docs/README_v3.md).
## Installing:
`npm install smart-buffer`
`yarn add smart-buffer`
or
`yarn add smart-buffer`
`npm install smart-buffer`
Note: The published NPM package includes the built javascript library.
Note: The published NPM package includes the built javascript library.
If you cloned this repo and wish to build the library manually use:
`tsc -p ./`
`npm run build`
## Using smart-buffer
### Example
```javascript
// Javascript
const SmartBuffer = require('smart-buffer').SmartBuffer;
Say you were building a packet that had to conform to the following protocol:
// Typescript
import { SmartBuffer, SmartBufferOptions} from 'smart-buffer';
```
### Simple Example
Building a packet that uses the following protocol specification:
`[PacketType:2][PacketLength:2][Data:XX]`

@@ -58,17 +69,5 @@

```javascript
// 1.x (javascript)
var SmartBuffer = require('smart-buffer');
// 1.x (typescript)
import SmartBuffer = require('smart-buffer');
// 2.x+ (javascript)
const SmartBuffer = require('smart-buffer').SmartBuffer;
// 2.x+ (typescript)
import { SmartBuffer, SmartBufferOptions} from 'smart-buffer';
function createLoginPacket(username, password, age, country) {
let packet = new SmartBuffer();
packet.writeUInt16LE(0x0060); // Login Packet Type/ID
const packet = new SmartBuffer();
packet.writeUInt16LE(0x0060); // Some packet type
packet.writeStringNT(username);

@@ -78,4 +77,4 @@ packet.writeStringNT(password);

packet.writeStringNT(country);
packet.writeUInt16LE(packet.length - 2, 2);
packet.insertUInt16LE(packet.length - 2, 2);
return packet.toBuffer();

@@ -86,7 +85,7 @@ }

```javascript
let login = createLoginPacket("Josh", "secret123", 22, "United States");
const login = createLoginPacket("Josh", "secret123", 22, "United States");
// <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]` part of the packet was inserted after we had added everything else, and as shown in the Buffer dump above, is in the correct location along with everything else.
Notice that the `[PacketLength:2]` value (1e 00) was inserted at position 2.

@@ -96,5 +95,5 @@ Reading back the packet we created above is just as easy:

let reader = SmartBuffer.fromBuffer(login);
const reader = SmartBuffer.fromBuffer(login);
let logininfo = {
const logininfo = {
packetType: reader.readUInt16LE(),

@@ -109,3 +108,3 @@ packetLength: reader.readUInt16LE(),

/*
{
{
packetType: 96, (0x0060)

@@ -116,26 +115,55 @@ packetLength: 30,

age: 22,
country: 'United States'
};
country: 'United States'
}
*/
```
# Api Reference:
### Constructing a smart-buffer
## Write vs Insert
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.
smart-buffer has a few different ways to construct an instance. Starting with version 2.0, the following factory methods are preffered.
**SmartBuffer v3**:
```javascript
const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6]));
buff.writeInt8(7, 2);
console.log(buff.toBuffer())
// <Buffer 01 02 07 03 04 05 06>
```
**SmartBuffer v4**:
```javascript
let SmartBuffer = require('smart-buffer');
const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6]));
buff.writeInt8(7, 2);
console.log(buff.toBuffer());
// <Buffer 01 02 07 04 05 06>
```
To insert you instead should use:
```javascript
const buff = SmartBuffer.fromBuffer(new Buffer([1,2,3,4,5,6]));
buff.insertInt8(7, 2);
console.log(buff.toBuffer());
// <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.
## Constructing a smart-buffer
There are a few different ways to construct a SmartBuffer instance.
```javascript
// Creating SmartBuffer from existing Buffer
let buff = SmartBuffer.fromBuffer(buffer); // Creates instance from buffer. (Uses default utf8 encoding)
let buff = SmartBuffer.fromBuffer(buffer, 'ascii'); // Creates instance from buffer with ascii encoding for Strings.
const buff = SmartBuffer.fromBuffer(buffer); // Creates instance from buffer. (Uses default utf8 encoding)
const buff = SmartBuffer.fromBuffer(buffer, 'ascii'); // Creates instance from buffer with ascii encoding for strings.
// Creating SmartBuffer with specified internal Buffer size.
let buff = SmartBuffer.fromSize(1024); // Creates instance with internal Buffer size of 1024.
let buff = SmartBuffer.fromSize(1024, 'utf8'); // Creates instance with intenral Buffer size of 1024, and utf8 encoding.
// Creating SmartBuffer with specified internal Buffer size. (Note: this is not a hard cap, the internal buffer will grow as needed).
const buff = SmartBuffer.fromSize(1024); // Creates instance with internal Buffer size of 1024.
const buff = SmartBuffer.fromSize(1024, 'utf8'); // Creates instance with internal Buffer size of 1024, and utf8 encoding for strings.
// Creating SmartBuffer with options object. This one specifies size and encoding.
let buff = SmartBuffer.fromOptions({
const buff = SmartBuffer.fromOptions({
size: 1024,

@@ -146,221 +174,466 @@ encoding: 'ascii'

// Creating SmartBuffer with options object. This one specified an existing Buffer.
let buff = SmartBuffer.fromOptions({
const buff = SmartBuffer.fromOptions({
buff: buffer
});
// Creating SmartBuffer from a string.
const buff = SmartBuffer.fromBuffer(Buffer.from('some string', 'utf8'));
// Just want a regular SmartBuffer with all default options?
let buff = new SmartBuffer();
const buff = new SmartBuffer();
```
## Backwards Compatibility:
# Api Reference:
All constructors used prior to 2.0 still are supported. However it's not recommended to use these.
**Note:** SmartBuffer is fully documented with Typescript definitions as well as jsdocs so your favorite editor/IDE will have intellisense.
**Table of Contents**
1. [Constructing](#constructing)
2. **Numbers**
1. [Integers](#integers)
2. [Floating Points](#floating-point-numbers)
3. **Strings**
1. [Strings](#strings)
2. [Null Terminated Strings](#null-terminated-strings)
4. [Buffers](#buffers)
5. [Offsets](#offsets)
6. [Other](#other)
## Constructing
### constructor()
### constructor([options])
- ```options``` *{SmartBufferOptions}* An optional options object to construct a SmartBuffer with.
Examples:
```javascript
let writer = new SmartBuffer(); // Defaults to utf8, 4096 length internal Buffer.
let writer = new SmartBuffer(1024); // Defaults to utf8, 1024 length internal Buffer.
let writer = new SmartBuffer('ascii'); // Sets to ascii encoding, 4096 length internal buffer.
let writer = new SmartBuffer(1024, 'ascii'); // Sets to ascii encoding, 1024 length internal buffer.
const buff = new SmartBuffer();
const buff = new SmartBuffer({
size: 1024,
encoding: 'ascii'
});
```
## Reading Data
### Class Method: fromBuffer(buffer[, encoding])
- ```buffer``` *{Buffer}* The Buffer instance to wrap.
- ```encoding``` *{string}* The string encoding to use. ```Default: 'utf8'```
smart-buffer supports all of the common read functions you will find in the vanilla Buffer class. The only difference is, you do not need to specify which location to start reading from. This is possible because as you read data out of a smart-buffer, it automatically progresses an internal read offset/position to know where to pick up from on the next read.
Examples:
```javascript
const someBuffer = Buffer.from('some string');
const buff = SmartBuffer.fromBuffer(someBuffer); // Defaults to utf8
const buff = SmartBuffer.fromBuffer(someBuffer, 'ascii');
```
## Reading Numeric Values
### Class Method: fromSize(size[, encoding])
- ```size``` *{number}* The size to initialize the internal Buffer.
- ```encoding``` *{string}* The string encoding to use. ```Default: 'utf8'```
When numeric values, you simply need to call the function you want, and the data is returned.
Examples:
```javascript
const buff = SmartBuffer.fromSize(1024); // Defaults to utf8
const buff = SmartBuffer.fromSize(1024, 'ascii');
```
Supported Operations:
* readInt8
* readInt16BE
* readInt16LE
* readInt32BE
* readInt32LE
* readUInt8
* readUInt16BE
* readUInt16LE
* readUInt32BE
* readUInt32LE
* readFloatBE
* readFloatLE
* readDoubleBE
* readDoubleLE
### Class Method: fromOptions(options)
- ```options``` *{SmartBufferOptions}* The Buffer instance to wrap.
```typescript
interface SmartBufferOptions {
encoding?: BufferEncoding; // Defaults to utf8
size?: number; // Defaults to 4096
buff?: Buffer;
}
```
Examples:
```javascript
let reader = new SmartBuffer(somebuffer);
let num = reader.readInt8();
const buff = SmartBuffer.fromOptions({
size: 1024
};
const buff = SmartBuffer.fromOptions({
size: 1024,
encoding: 'utf8'
});
const buff = SmartBuffer.fromOptions({
encoding: 'utf8'
});
const someBuff = Buffer.from('some string', 'utf8');
const buff = SmartBuffer.fromOptions({
buffer: someBuff,
encoding: 'utf8'
});
```
## Reading String Values
## Integers
When reading String values, you can either choose to read a null terminated string, or a string of a specified length.
### readInt8([offset])
- ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
- Returns *{number}*
### SmartBuffer.readStringNT( [encoding] )
> `String` **String encoding to use** - Defaults to the encoding set in the constructor.
Read a Int8 value.
returns `String`
### buff.readInt16BE([offset])
### buff.readInt16LE([offset])
### buff.readUInt16BE([offset])
### buff.readUInt16LE([offset])
- ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
- Returns *{number}*
> Note: When readStringNT is called and there is no null character found, smart-buffer will read to the end of the internal Buffer.
Read a 16 bit integer value.
### SmartBuffer.readString( [length] )
### SmartBuffer.readString( [encoding] )
### SmartBuffer.readString( [length], [encoding] )
> `Number` **Length of the string to read**
### buff.readInt32BE([offset])
### buff.readInt32LE([offset])
### buff.readUInt32BE([offset])
### buff.readUInt32LE([offset])
- ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
- Returns *{number}*
> `String` **String encoding to use** - Defaults to the encoding set in the constructor, or utf8.
Read a 32 bit integer value.
returns `String`
> Note: When readString is called without a specified length, smart-buffer will read to the end of the internal Buffer.
### buff.writeInt8(value[, offset])
### buff.writeUInt8(value[, offset])
- ```value``` *{number}* The value to write.
- ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
- Returns *{this}*
Write a Int8 value.
### buff.insertInt8(value, offset)
### buff.insertUInt8(value, offset)
- ```value``` *{number}* The value to insert.
- ```offset``` *{number}* The offset to insert this data at.
- Returns *{this}*
## Reading Buffer Values
Insert a Int8 value.
### SmartBuffer.readBuffer( length )
> `Number` **Length of data to read into a Buffer**
returns `Buffer`
### buff.writeInt16BE(value[, offset])
### buff.writeInt16LE(value[, offset])
### buff.writeUInt16BE(value[, offset])
### buff.writeUInt16LE(value[, offset])
- ```value``` *{number}* The value to write.
- ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
- Returns *{this}*
> Note: This function uses `slice` to retrieve the Buffer.
Write a 16 bit integer value.
### buff.insertInt16BE(value, offset)
### buff.insertInt16LE(value, offset)
### buff.insertUInt16BE(value, offset)
### buff.insertUInt16LE(value, offset)
- ```value``` *{number}* The value to insert.
- ```offset``` *{number}* The offset to insert this data at.
- Returns *{this}*
### SmartBuffer.readBufferNT()
Insert a 16 bit integer value.
returns `Buffer`
> Note: This reads the next sequence of bytes in the buffer until a null (0x00) value is found. (Null terminated buffer)
> Note: This function uses `slice` to retrieve the Buffer.
### buff.writeInt32BE(value[, offset])
### buff.writeInt32LE(value[, offset])
### buff.writeUInt32BE(value[, offset])
### buff.writeUInt32LE(value[, offset])
- ```value``` *{number}* The value to write.
- ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
- Returns *{this}*
Write a 32 bit integer value.
## Writing Data
### buff.insertInt32BE(value, offset)
### buff.insertInt32LE(value, offset)
### buff.insertUInt32BE(value, offset)
### buff.nsertUInt32LE(value, offset)
- ```value``` *{number}* The value to insert.
- ```offset``` *{number}* The offset to insert this data at.
- Returns *{this}*
smart-buffer supports all of the common write functions you will find in the vanilla Buffer class. The only difference is, you do not need to specify which location to write to in your Buffer by default. You do however have the option of **inserting** a piece of data into your smart-buffer at a given location.
Insert a 32 bit integer value.
## Writing Numeric Values
## Floating Point Numbers
### buff.readFloatBE([offset])
### buff.readFloatLE([offset])
- ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
- Returns *{number}*
For numeric values, you simply need to call the function you want, and the data is written at the end of the internal Buffer's current write position. You can specify a offset/position to **insert** the given value at, but keep in mind this does not override data at the given position. This feature also does not work properly when inserting a value beyond the current internal length of the smart-buffer (length being the .length property of the smart-buffer instance you're writing to)
Read a Float value.
Supported Operations:
* writeInt8
* writeInt16BE
* writeInt16LE
* writeInt32BE
* writeInt32LE
* writeUInt8
* writeUInt16BE
* writeUInt16LE
* writeUInt32BE
* writeUInt32LE
* writeFloatBE
* writeFloatLE
* writeDoubleBE
* writeDoubleLE
### buff.eadDoubleBE([offset])
### buff.readDoubleLE([offset])
- ```offset``` *{number}* Optional position to start reading data from. **Default**: ```Auto managed offset```
- Returns *{number}*
The following signature is the same for all the above functions:
Read a Double value.
### SmartBuffer.writeInt8( value, [offset] )
> `Number` **A valid Int8 number**
> `Number` **The position to insert this value at**
### buff.writeFloatBE(value[, offset])
### buff.writeFloatLE(value[, offset])
- ```value``` *{number}* The value to write.
- ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
- Returns *{this}*
returns this
Write a Float value.
> Note: All write operations return `this` to allow for chaining.
### buff.insertFloatBE(value, offset)
### buff.insertFloatLE(value, offset)
- ```value``` *{number}* The value to insert.
- ```offset``` *{number}* The offset to insert this data at.
- Returns *{this}*
## Writing String Values
Insert a Float value.
When reading String values, you can either choose to write a null terminated string, or a non null terminated string.
### SmartBuffer.writeStringNT( value, [offset], [encoding] )
### SmartBuffer.writeStringNT( value, [offset] )
### SmartBuffer.writeStringNT( value, [encoding] )
> `String` **String value to write**
### buff.writeDoubleBE(value[, offset])
### buff.writeDoubleLE(value[, offset])
- ```value``` *{number}* The value to write.
- ```offset``` *{number}* An optional offset to write this value to. **Default:** ```Auto managed offset```
- Returns *{this}*
> `Number` **The position to insert this String at**
Write a Double value.
> `String` **The String encoding to use.** - Defaults to the encoding set in the constructor, or utf8.
### buff.insertDoubleBE(value, offset)
### buff.insertDoubleLE(value, offset)
- ```value``` *{number}* The value to insert.
- ```offset``` *{number}* The offset to insert this data at.
- Returns *{this}*
returns this
Insert a Double value.
### SmartBuffer.writeString( value, [offset], [encoding] )
### SmartBuffer.writeString( value, [offset] )
### SmartBuffer.writeString( value, [encoding] )
> `String` **String value to write**
## Strings
> `Number` **The position to insert this String at**
### buff.readString()
### buff.readString(size[, encoding])
### buff.readString(encoding)
- ```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```.
> `String` **The String encoding to use** - Defaults to the encoding set in the constructor, or utf8.
Read a string value.
returns this
Examples:
```javascript
const buff = SmartBuffer.fromBuffer(Buffer.from('hello there', 'utf8'));
buff.readString(); // 'hello there'
buff.readString(2); // 'he'
buff.readString(2, 'utf8'); // 'he'
buff.readString('utf8'); // 'hello there'
```
### buff.writeString(value)
### buff.writeString(value[, offset])
### buff.writeString(value[, encoding])
### buff.writeString(value[, offset[, encoding]])
- ```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```
## Writing Buffer Values
Write a string value.
### SmartBuffer.writeBuffer( value, [offset] )
> `Buffer` **Buffer value to write**
Examples:
```javascript
buff.writeString('hello'); // Auto managed offset
buff.writeString('hello', 2);
buff.writeString('hello', 'utf8') // Auto managed offset
buff.writeString('hello', 2, 'utf8');
```
> `Number` **The position to insert this Buffer's content at**
### buff.insertString(value, offset[, encoding])
- ```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```
returns this
Insert a string value.
### SmartBuffer.writeBufferNT( value, [offset] )
> `Buffer` **Buffer value to write**
Examples:
```javascript
buff.insertString('hello', 2);
buff.insertString('hello', 2, 'utf8');
```
> `Number` **The position to insert this Buffer's content at**
## Null Terminated Strings
returns this
### buff.readStringNT()
### buff.readStringNT(encoding)
- ```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).
## Utility Functions
Examples:
```javascript
const buff = SmartBuffer.fromBuffer(Buffer.from('hello\0 there', 'utf8'));
buff.readStringNT(); // 'hello'
### SmartBuffer.clear()
Resets the SmartBuffer to its default state where it can be reused for reading or writing.
// If we called this again:
buff.readStringNT(); // ' there'
```
### SmartBuffer.remaining()
### buff.writeStringNT(value)
### buff.writeStringNT(value[, offset])
### buff.writeStringNT(value[, encoding])
### buff.writeStringNT(value[, offset[, encoding]])
- ```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```
returns `Number` The amount of data left to read based on the current read Position.
Write a null terminated string value.
### SmartBuffer.skip( value )
> `Number` **The amount of bytes to skip ahead**
Examples:
```javascript
buff.writeStringNT('hello'); // Auto managed offset <Buffer 68 65 6c 6c 6f 00>
buff.writeStringNT('hello', 2); // <Buffer 00 00 68 65 6c 6c 6f 00>
buff.writeStringNT('hello', 'utf8') // Auto managed offset
buff.writeStringNT('hello', 2, 'utf8');
```
Skips the read position ahead by the given value.
### buff.insertStringNT(value, offset[, encoding])
- ```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```
returns this
Insert a null terminated string value.
### SmartBuffer.rewind( value )
> `Number` **The amount of bytes to reward backwards**
Examples:
```javascript
buff.insertStringNT('hello', 2);
buff.insertStringNT('hello', 2, 'utf8');
```
Rewinds the read position backwards by the given value.
## Buffers
returns this
### buff.readBuffer([length])
- ```length``` *{number}* The number of bytes to read into a Buffer. **Default:** ```Reads to the end of the Buffer```
### SmartBuffer.moveTo( position )
> `Number` **The point to skip the read position to**
Read a Buffer of a specified size.
Moves the read position to the given point.
returns this
### buff.writeBuffer(value[, offset])
- ```value``` *{Buffer}* The buffer value to write.
- ```offset``` *{number}* An optional offset to write the value to. **Default:** ```Auto managed offset```
### SmartBuffer.toBuffer()
### buff.insertBuffer(value, offset)
- ```value``` *{Buffer}* The buffer value to write.
- ```offset``` *{number}* The offset to write the value to.
returns `Buffer` A Buffer containing the contents of the internal Buffer.
> Note: This uses the slice function.
### buff.readBufferNT()
### SmartBuffer.toString( [encoding] )
> `String` **The String encoding to use** - Defaults to the encoding set in the constructor, or utf8.
Read a null terminated Buffer.
returns `String` The internal Buffer in String representation.
### buff.writeBufferNT(value[, offset])
- ```value``` *{Buffer}* The buffer value to write.
- ```offset``` *{number}* An optional offset to write the value to. **Default:** ```Auto managed offset```
## Properties
Write a null terminated Buffer.
### SmartBuffer.length
returns `Number` **The length of the data that is being tracked in the internal Buffer** - Does NOT return the absolute length of the internal Buffer being written to.
### buff.insertBufferNT(value, offset)
- ```value``` *{Buffer}* The buffer value to write.
- ```offset``` *{number}* The offset to write the value to.
Insert a null terminated Buffer.
## Offsets
### buff.readOffset
### buff.readOffset(offset)
- ```offset``` *{number}* The new read offset value to set.
- Returns: ```The current read offset```
Gets or sets the current read offset.
Examples:
```javascript
const currentOffset = buff.readOffset; // 5
buff.readOffset = 10;
console.log(buff.readOffset) // 10
```
### buff.writeOffset
### buff.writeOffset(offset)
- ```offset``` *{number}* The new write offset value to set.
- Returns: ```The current write offset```
Gets or sets the current write offset.
Examples:
```javascript
const currentOffset = buff.writeOffset; // 5
buff.writeOffset = 10;
console.log(buff.writeOffset) // 10
```
### buff.encoding
### buff.encoding(encoding)
- ```encoding``` *{string}* The new string encoding to set.
- Returns: ```The current string encoding```
Gets or sets the current string encoding.
Examples:
```javascript
const currentEncoding = buff.encoding; // 'utf8'
buff.encoding = 'ascii';
console.log(buff.encoding) // 'ascii'
```
## Other
### buff.clear()
Clear and resets the SmartBuffer instance.
### buff.remaining()
- Returns ```Remaining data left to be read```
Gets the number of remaining bytes to be read.
### buff.internalBuffer
- Returns: *{Buffer}*
Gets the internally managed Buffer (Includes unmanaged data).
Examples:
```javascript
const buff = SmartBuffer.fromSize(16);
buff.writeString('hello');
console.log(buff.InternalBuffer); // <Buffer 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00>
```
### buff.toBuffer()
- Returns: *{Buffer}*
Gets a sliced Buffer instance of the internally managed Buffer. (Only includes managed data)
Examples:
```javascript
const buff = SmartBuffer.fromSize(16);
buff.writeString('hello');
console.log(buff.toBuffer()); // <Buffer 68 65 6c 6c 6f>
```
### buff.toString([encoding])
- ```encoding``` *{string}* The string encoding to use when converting to a string. **Default:** ```utf8```
- Returns *{string}*
Gets a string representation of all data in the SmartBuffer.
### buff.destroy()
Destroys the SmartBuffer instance.
## License
This work is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License).

@@ -17,616 +17,616 @@ /// <reference types="node" />

/**
* Creates a new SmartBuffer instance.
*
* @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
*/
* Creates a new SmartBuffer instance.
*
* @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
*/
constructor(options?: SmartBufferOptions);
/**
* Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
*
* @param size { Number } The size of the internal Buffer.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
* Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
*
* @param size { Number } The size of the internal Buffer.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
static fromSize(size: number, encoding?: BufferEncoding): SmartBuffer;
/**
* Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
*
* @param buffer { Buffer } The Buffer to use as the internal Buffer value.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
* Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
*
* @param buffer { Buffer } The Buffer to use as the internal Buffer value.
* @param encoding { String } The BufferEncoding to use for strings.
*
* @return { SmartBuffer }
*/
static fromBuffer(buff: Buffer, encoding?: BufferEncoding): SmartBuffer;
/**
* Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
*
* @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
*/
* Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
*
* @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
*/
static fromOptions(options: SmartBufferOptions): SmartBuffer;
/**
* Type checking function that determines if an object is a SmartBufferOptions object.
*/
* Type checking function that determines if an object is a SmartBufferOptions object.
*/
static isSmartBufferOptions(options: SmartBufferOptions): options is SmartBufferOptions;
/**
* Reads an Int8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt8(offset?: number): number;
/**
* Reads an Int16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt16BE(offset?: number): number;
/**
* Reads an Int16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt16LE(offset?: number): number;
/**
* Reads an Int32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt32BE(offset?: number): number;
/**
* Reads an Int32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an Int32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readInt32LE(offset?: number): number;
/**
* Writes an Int8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt8(value: number, offset?: number): SmartBuffer;
/**
* Inserts an Int8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt8(value: number, offset: number): SmartBuffer;
/**
* Writes an Int16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt16BE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an Int16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt16BE(value: number, offset: number): SmartBuffer;
/**
* Writes an Int16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt16LE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an Int16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt16LE(value: number, offset: number): SmartBuffer;
/**
* Writes an Int32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt32BE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an Int32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt32BE(value: number, offset: number): SmartBuffer;
/**
* Writes an Int32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an Int32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeInt32LE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an Int32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an Int32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertInt32LE(value: number, offset: number): SmartBuffer;
/**
* Reads an UInt8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt8 value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt8(offset?: number): number;
/**
* Reads an UInt16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt16BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt16BE(offset?: number): number;
/**
* Reads an UInt16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt16LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt16LE(offset?: number): number;
/**
* Reads an UInt32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt32BE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt32BE(offset?: number): number;
/**
* Reads an UInt32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an UInt32LE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readUInt32LE(offset?: number): number;
/**
* Writes an UInt8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt8 value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt8(value: number, offset?: number): SmartBuffer;
/**
* Inserts an UInt8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt8 value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt8(value: number, offset: number): SmartBuffer;
/**
* Writes an UInt16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt16BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt16BE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an UInt16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt16BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt16BE(value: number, offset: number): SmartBuffer;
/**
* Writes an UInt16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt16LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt16LE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an UInt16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt16LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt16LE(value: number, offset: number): SmartBuffer;
/**
* Writes an UInt32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt32BE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt32BE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an UInt32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt32BE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt32BE(value: number, offset: number): SmartBuffer;
/**
* Writes an UInt32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes an UInt32LE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeUInt32LE(value: number, offset?: number): SmartBuffer;
/**
* Inserts an UInt32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts an UInt32LE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertUInt32LE(value: number, offset: number): SmartBuffer;
/**
* Reads an FloatBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an FloatBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readFloatBE(offset?: number): number;
/**
* Reads an FloatLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an FloatLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readFloatLE(offset?: number): number;
/**
* Writes a FloatBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a FloatBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeFloatBE(value: number, offset?: number): SmartBuffer;
/**
* Inserts a FloatBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a FloatBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertFloatBE(value: number, offset: number): SmartBuffer;
/**
* Writes a FloatLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a FloatLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeFloatLE(value: number, offset?: number): SmartBuffer;
/**
* Inserts a FloatLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a FloatLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertFloatLE(value: number, offset: number): SmartBuffer;
/**
* Reads an DoublEBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an DoublEBE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readDoubleBE(offset?: number): number;
/**
* Reads an DoubleLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
* Reads an DoubleLE value from the current read position or an optionally provided offset.
*
* @param offset { Number } The offset to read data from (optional)
* @return { Number }
*/
readDoubleLE(offset?: number): number;
/**
* Writes a DoubleBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a DoubleBE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeDoubleBE(value: number, offset?: number): SmartBuffer;
/**
* Inserts a DoubleBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a DoubleBE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertDoubleBE(value: number, offset: number): SmartBuffer;
/**
* Writes a DoubleLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
* Writes a DoubleLE value to the current write position (or at optional offset).
*
* @param value { Number } The value to write.
* @param offset { Number } The offset to write the value at.
*
* @return this
*/
writeDoubleLE(value: number, offset?: number): SmartBuffer;
/**
* Inserts a DoubleLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
* Inserts a DoubleLE value at the given offset value.
*
* @param value { Number } The value to insert.
* @param offset { Number } The offset to insert the value at.
*
* @return this
*/
insertDoubleLE(value: number, offset: number): SmartBuffer;
/**
* Reads a String from the current read position.
*
* @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for
* the string (Defaults to instance level encoding).
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
* Reads a String from the current read position.
*
* @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for
* the string (Defaults to instance level encoding).
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
readString(arg1?: number | BufferEncoding, encoding?: BufferEncoding): string;
/**
* Inserts a String
*
* @param value { String } The String value to insert.
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
insertString(value: string, offset: number, encoding?: BufferEncoding): this;
* Inserts a String
*
* @param value { String } The String value to insert.
* @param offset { Number } The offset to insert the string at.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
insertString(value: string, offset: number, encoding?: BufferEncoding): SmartBuffer;
/**
* Writes a String
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
writeString(value: string, arg2?: number | BufferEncoding, encoding?: BufferEncoding): this;
* Writes a String
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
writeString(value: string, arg2?: number | BufferEncoding, encoding?: BufferEncoding): SmartBuffer;
/**
* Reads a null-terminated String from the current read position.
*
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
* Reads a null-terminated String from the current read position.
*
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
*
* @return { String }
*/
readStringNT(encoding?: BufferEncoding): string;
/**
* Inserts a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Inserts a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
insertStringNT(value: string, offset: number, encoding?: BufferEncoding): void;
/**
* Writes a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Writes a null-terminated String.
*
* @param value { String } The String value to write.
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
writeStringNT(value: string, arg2?: number | BufferEncoding, encoding?: BufferEncoding): void;
/**
* Reads a Buffer from the internal read position.
*
* @param length { Number } The length of data to read as a Buffer.
*
* @return { Buffer }
*/
* Reads a Buffer from the internal read position.
*
* @param length { Number } The length of data to read as a Buffer.
*
* @return { Buffer }
*/
readBuffer(length?: number): Buffer;
/**
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
insertBuffer(value: Buffer, offset: number): this;
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
insertBuffer(value: Buffer, offset: number): SmartBuffer;
/**
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
writeBuffer(value: Buffer, offset?: number): this;
* Writes a Buffer to the current write position.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
writeBuffer(value: Buffer, offset?: number): SmartBuffer;
/**
* Reads a null-terminated Buffer from the current read poisiton.
*
* @return { Buffer }
*/
* Reads a null-terminated Buffer from the current read poisiton.
*
* @return { Buffer }
*/
readBufferNT(): Buffer;
/**
* Inserts a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
insertBufferNT(value: Buffer, offset: number): this;
* Inserts a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
insertBufferNT(value: Buffer, offset: number): SmartBuffer;
/**
* Writes a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
writeBufferNT(value: Buffer, offset?: number): this;
* Writes a null-terminated Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
writeBufferNT(value: Buffer, offset?: number): SmartBuffer;
/**
* Clears the SmartBuffer instance to its original empty state.
*/
clear(): void;
* Clears the SmartBuffer instance to its original empty state.
*/
clear(): SmartBuffer;
/**
* Gets the remaining data left to be read from the SmartBuffer instance.
*
* @return { Number }
*/
* Gets the remaining data left to be read from the SmartBuffer instance.
*
* @return { Number }
*/
remaining(): number;
/**
* Gets the current read offset value of the SmartBuffer instance.
*
* @return { Number }
*/
* Gets the current read offset value of the SmartBuffer instance.
*
* @return { Number }
*/
/**
* Sets the read offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
* Sets the read offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
readOffset: number;
/**
* Gets the current write offset value of the SmartBuffer instance.
*
* @return { Number }
*/
* Gets the current write offset value of the SmartBuffer instance.
*
* @return { Number }
*/
/**
* Sets the write offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
* Sets the write offset value of the SmartBuffer instance.
*
* @param offset { Number } - The offset value to set.
*/
writeOffset: number;
/**
* Gets the currently set string encoding of the SmartBuffer instance.
*
* @return { BufferEncoding } The string Buffer encoding currently set.
*/
* Gets the currently set string encoding of the SmartBuffer instance.
*
* @return { BufferEncoding } The string Buffer encoding currently set.
*/
/**
* Sets the string encoding of the SmartBuffer instance.
*
* @param encoding { BufferEncoding } The string Buffer encoding to set.
*/
* Sets the string encoding of the SmartBuffer instance.
*
* @param encoding { BufferEncoding } The string Buffer encoding to set.
*/
encoding: BufferEncoding;
/**
* Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
*
* @return { Buffer } The Buffer value.
*/
* Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
*
* @return { Buffer } The Buffer value.
*/
readonly internalBuffer: Buffer;
/**
* Gets the value of the internal managed Buffer (Includes managed data only)
*
* @param { Buffer }
*/
* Gets the value of the internal managed Buffer (Includes managed data only)
*
* @param { Buffer }
*/
toBuffer(): Buffer;
/**
* Gets the String value of the internal managed Buffer
*
* @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
*/
* Gets the String value of the internal managed Buffer
*
* @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
*/
toString(encoding?: BufferEncoding): string;
/**
* Destroys the SmartBuffer instance.
*/
destroy(): void;
* Destroys the SmartBuffer instance.
*/
destroy(): SmartBuffer;
/**
* Handles inserting and writing strings.
*
* @param value { String } The String value to insert.
* @param isInsert { Boolean } True if inserting a string, false if writing.
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
* Handles inserting and writing strings.
*
* @param value { String } The String value to insert.
* @param isInsert { Boolean } True if inserting a string, false if writing.
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
*/
private _handleString(value, isInsert, arg3?, encoding?);
/**
* Handles writing or insert of a Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
* Handles writing or insert of a Buffer.
*
* @param value { Buffer } The Buffer to write.
* @param offset { Number } The offset to write the Buffer to.
*/
private _handleBuffer(value, isInsert, offset?);
/**
* Ensures that the internal Buffer is large enough to read data.
*
* @param length { Number } The length of the data that needs to be read.
* @param offset { Number } The offset of the data that needs to be read.
*/
* Ensures that the internal Buffer is large enough to read data.
*
* @param length { Number } The length of the data that needs to be read.
* @param offset { Number } The offset of the data that needs to be read.
*/
private ensureReadable(length, offset?);
/**
* Ensures that the internal Buffer is large enough to insert data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written.
*/
* Ensures that the internal Buffer is large enough to insert data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written.
*/
private ensureInsertable(dataLength, offset);
/**
* Ensures that the internal Buffer is large enough to write data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written (defaults to writeOffset).
*/
private ensureWriteable(dataLength, offset?);
* Ensures that the internal Buffer is large enough to write data.
*
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written (defaults to writeOffset).
*/
private _ensureWriteable(dataLength, offset?);
/**
* Ensures that the internal Buffer is large enough to write at least the given amount of data.
*
* @param minLength { Number } The minimum length of the data needs to be written.
*/
private ensureCapacity(minLength);
* Ensures that the internal Buffer is large enough to write at least the given amount of data.
*
* @param minLength { Number } The minimum length of the data needs to be written.
*/
private _ensureCapacity(minLength);
/**
* Reads a numeric number value using the provided function.
*
* @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
* @param byteSize { Number } The number of bytes read.
* @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead.
*
* @param { Number }
*/
private readNumberValue(func, byteSize, offset?);
* Reads a numeric number value using the provided function.
*
* @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
* @param byteSize { Number } The number of bytes read.
* @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead.
*
* @param { Number }
*/
private _readNumberValue(func, byteSize, offset?);
/**
* Inserts a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
private insertNumberValue(func, byteSize, value, offset);
* Inserts a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
private _insertNumberValue(func, byteSize, value, offset);
/**
* Writes a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
private writeNumberValue(func, byteSize, value, offset?);
* Writes a numeric number value based on the given offset and value.
*
* @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
* @param byteSize { Number } The number of bytes written.
* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
private _writeNumberValue(func, byteSize, value, offset?);
}
export { SmartBufferOptions, SmartBuffer };

@@ -16,2 +16,3 @@ /// <reference types="node" />

INVALID_TARGET_OFFSET: string;
INVALID_TARGET_LENGTH: string;
INVALID_READ_BEYOND_BOUNDS: string;

@@ -18,0 +19,0 @@ INVALID_WRITE_BEYOND_BOUNDS: string;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc