Socket
Socket
Sign inDemoInstall

smart-buffer

Package Overview
Dependencies
0
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.3 to 4.0.0-beta.1

build/utils.js

862

build/smartbuffer.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const buffer_1 = require("buffer");
const utils_1 = require("./utils");
// The default Buffer size if one is not provided.
const DEFAULT_SMARTBUFFER_SIZE = 4096;
// The default string encoding to use for reading/writing strings.
// The default string encoding to use for reading/writing strings.
const DEFAULT_SMARTBUFFER_ENCODING = 'utf8';

@@ -10,88 +13,55 @@ class SmartBuffer {

*
* @param arg1 { Number | BufferEncoding | Buffer | SmartBufferOptions }
* @param arg2 { BufferEncoding }
* @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
*/
constructor(arg1, arg2) {
constructor(options) {
this.length = 0;
this.encoding = DEFAULT_SMARTBUFFER_ENCODING;
this.writeOffset = 0;
this.readOffset = 0;
// Initial buffer size provided
if (typeof arg1 === 'number') {
if (Number.isFinite(arg1) && Number.isInteger(arg1) && arg1 > 0) {
this.buff = Buffer.allocUnsafe(arg1);
}
else {
throw new Error('Invalid size provided. Size must be a valid integer greater than zero.');
}
}
else if (typeof arg1 === 'string') {
if (Buffer.isEncoding(arg1)) {
this.buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
this.encoding = arg1;
}
else {
throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
}
}
else if (arg1 instanceof Buffer) {
this.buff = arg1;
this.length = arg1.length;
}
else if (SmartBuffer.isSmartBufferOptions(arg1)) {
this._encoding = DEFAULT_SMARTBUFFER_ENCODING;
this._writeOffset = 0;
this._readOffset = 0;
if (SmartBuffer.isSmartBufferOptions(options)) {
// Checks for encoding
if (arg1.encoding) {
if (Buffer.isEncoding(arg1.encoding)) {
this.encoding = arg1.encoding;
}
else {
throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
}
if (options.encoding) {
utils_1.checkEncoding(options.encoding);
this._encoding = options.encoding;
}
// Checks for initial size length
if (arg1.size) {
if (Number.isFinite(arg1.size) && Number.isInteger(arg1.size) && arg1.size > 0) {
this.buff = Buffer.allocUnsafe(arg1.size);
if (options.size) {
if (utils_1.isFiniteInteger(options.size) && options.size > 0) {
this._buff = buffer_1.Buffer.allocUnsafe(options.size);
}
else {
throw new Error('Invalid size provided. Size must be a valid integer greater than zero.');
throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_SIZE);
}
// Check for initial Buffer
}
else if (arg1.buff) {
if (arg1.buff instanceof Buffer) {
this.buff = arg1.buff;
this.length = arg1.buff.length;
else if (options.buff) {
if (options.buff instanceof buffer_1.Buffer) {
this._buff = options.buff;
this.length = options.buff.length;
}
else {
throw new Error('Invalid buffer provided in SmartBufferOptions.');
throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_BUFFER);
}
}
else {
this.buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
this._buff = buffer_1.Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
}
}
else if (typeof arg1 === 'object') {
throw new Error('Invalid object supplied to SmartBuffer constructor.');
}
else {
this.buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
}
// Check for encoding (Buffer, Encoding) constructor.
if (typeof arg2 === 'string') {
if (Buffer.isEncoding(arg2)) {
this.encoding = arg2;
// If something was passed but it's not a SmartBufferOptions object
if (typeof options !== 'undefined') {
throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_OBJECT);
}
else {
throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
}
// Otherwise default to sane options
this._buff = buffer_1.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) {

@@ -134,40 +104,45 @@ return new this({

/**
* Reads an Int8 value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readInt8, 1);
readInt8(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt8, 1, offset);
}
/**
* Reads an Int16BE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readInt16BE, 2);
readInt16BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt16BE, 2, offset);
}
/**
* Reads an Int16LE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readInt16LE, 2);
readInt16LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt16LE, 2, offset);
}
/**
* Reads an Int32BE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readInt32BE, 4);
readInt32BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt32BE, 4, offset);
}
/**
* Reads an Int32LE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readInt32LE, 4);
readInt32LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readInt32LE, 4, offset);
}

@@ -183,6 +158,18 @@ /**

writeInt8(value, offset) {
this.writeNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertInt8(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt8, 1, value, offset);
return this;
}
/**
* Writes an Int16BE value to the current write position (or at optional offset).

@@ -196,6 +183,18 @@ *

writeInt16BE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertInt16BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt16BE, 2, value, offset);
return this;
}
/**
* Writes an Int16LE value to the current write position (or at optional offset).

@@ -209,6 +208,18 @@ *

writeInt16LE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertInt16LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt16LE, 2, value, offset);
return this;
}
/**
* Writes an Int32BE value to the current write position (or at optional offset).

@@ -222,6 +233,18 @@ *

writeInt32BE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertInt32BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt32BE, 4, value, offset);
return this;
}
/**
* Writes an Int32LE value to the current write position (or at optional offset).

@@ -235,45 +258,62 @@ *

writeInt32LE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertInt32LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeInt32LE, 4, value, offset);
return this;
}
// Unsigned Integers
/**
* Reads an UInt8 value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readUInt8, 1);
readUInt8(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt8, 1, offset);
}
/**
* Reads an UInt16BE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readUInt16BE, 2);
readUInt16BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt16BE, 2, offset);
}
/**
* Reads an UInt16LE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readUInt16LE, 2);
readUInt16LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt16LE, 2, offset);
}
/**
* Reads an UInt32BE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readUInt32BE, 4);
readUInt32BE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt32BE, 4, offset);
}
/**
* Reads an UInt32LE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readUInt32LE, 4);
readUInt32LE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readUInt32LE, 4, offset);
}

@@ -289,6 +329,18 @@ /**

writeUInt8(value, offset) {
this.writeNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertUInt8(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt8, 1, value, offset);
return this;
}
/**
* Writes an UInt16BE value to the current write position (or at optional offset).

@@ -302,6 +354,18 @@ *

writeUInt16BE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertUInt16BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt16BE, 2, value, offset);
return this;
}
/**
* Writes an UInt16LE value to the current write position (or at optional offset).

@@ -315,6 +379,18 @@ *

writeUInt16LE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertUInt16LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt16LE, 2, value, offset);
return this;
}
/**
* Writes an UInt32BE value to the current write position (or at optional offset).

@@ -328,6 +404,18 @@ *

writeUInt32BE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertUInt32BE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt32BE, 4, value, offset);
return this;
}
/**
* Writes an UInt32LE value to the current write position (or at optional offset).

@@ -341,21 +429,35 @@ *

writeUInt32LE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertUInt32LE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeUInt32LE, 4, value, offset);
return this;
}
// Floating Point
/**
* Reads an FloatBE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readFloatBE, 4);
readFloatBE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readFloatBE, 4, offset);
}
/**
* Reads an FloatLE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readFloatLE, 4);
readFloatLE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readFloatLE, 4, offset);
}

@@ -371,6 +473,18 @@ /**

writeFloatBE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertFloatBE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeFloatBE, 4, value, offset);
return this;
}
/**
* Writes a FloatLE value to the current write position (or at optional offset).

@@ -384,21 +498,35 @@ *

writeFloatLE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertFloatLE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeFloatLE, 4, value, offset);
return this;
}
// Double Floating Point
/**
* Reads an DoublEBE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readDoubleBE, 8);
readDoubleBE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readDoubleBE, 8, offset);
}
/**
* Reads an DoubleLE value from the current read position.
* 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() {
return this.readNumberValue(Buffer.prototype.readDoubleLE, 8);
readDoubleLE(offset) {
return this.readNumberValue(buffer_1.Buffer.prototype.readDoubleLE, 8, offset);
}

@@ -414,6 +542,18 @@ /**

writeDoubleBE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertDoubleBE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeDoubleBE, 8, value, offset);
return this;
}
/**
* Writes a DoubleLE value to the current write position (or at optional offset).

@@ -427,5 +567,17 @@ *

writeDoubleLE(value, offset) {
this.writeNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
this.writeNumberValue(buffer_1.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
*/
insertDoubleLE(value, offset) {
this.insertNumberValue(buffer_1.Buffer.prototype.writeDoubleLE, 8, value, offset);
return this;
}
// Strings

@@ -435,3 +587,4 @@ /**

*
* @param length { Number } The number of bytes to read as a String.
* @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).

@@ -441,48 +594,41 @@ *

*/
readString(length, encoding) {
const lengthVal = (typeof length === 'number') ? Math.min(length, this.length - this.readOffset) : this.length - this.readOffset;
const value = this.buff.slice(this.readOffset, this.readOffset + lengthVal).toString(encoding || this.encoding);
this.readOffset += lengthVal;
readString(arg1, encoding) {
let lengthVal;
// Length provided
if (typeof arg1 === 'number') {
utils_1.checkLengthValue(arg1);
lengthVal = Math.min(arg1, this.length - this._readOffset);
}
else {
encoding = arg1;
lengthVal = this.length - this._readOffset;
}
// Check encoding
if (typeof encoding !== 'undefined') {
utils_1.checkEncoding(encoding);
}
const value = this._buff.slice(this._readOffset, this._readOffset + lengthVal).toString(encoding || this._encoding);
this._readOffset += lengthVal;
return value;
}
/**
* Writes a String to the current write position.
* 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, offset, encoding) {
utils_1.checkOffsetValue(offset);
return this._handleString(value, true, offset, encoding);
}
/**
* Writes a 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 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) {
let offsetVal = this.writeOffset;
let encodingVal = this.encoding;
// Check for offset
if (typeof arg2 === 'number') {
offsetVal = arg2;
}
else if (typeof arg2 === 'string') {
if (Buffer.isEncoding(arg2)) {
encodingVal = arg2;
}
else {
throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
}
}
// Check for encoding (third param)
if (typeof encoding === 'string') {
if (Buffer.isEncoding(encoding)) {
encodingVal = encoding;
}
else {
throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
}
}
// Calculate bytelength of string.
const byteLength = Buffer.byteLength(value, encodingVal);
// Ensure there is enough internal Buffer capacity.
this.ensureWriteable(byteLength, offsetVal);
// Write value
this.buff.write(value, offsetVal, byteLength, encodingVal);
// Increment internal Buffer write offset;
this.writeOffset += byteLength;
return this;
return this._handleString(value, false, arg2, encoding);
}

@@ -497,7 +643,10 @@ /**

readStringNT(encoding) {
if (typeof encoding !== 'undefined') {
utils_1.checkEncoding(encoding);
}
// Set null character position to the end SmartBuffer instance.
let nullPos = this.length;
// Find next null character (if one is not found, default from above is used)
for (let i = this.readOffset; i < this.length; i++) {
if (this.buff[i] === 0x00) {
for (let i = this._readOffset; i < this.length; i++) {
if (this._buff[i] === 0x00) {
nullPos = i;

@@ -508,9 +657,9 @@ break;

// Read string value
const value = this.buff.slice(this.readOffset, nullPos);
const value = this._buff.slice(this._readOffset, nullPos);
// Increment internal Buffer read offset
this.readOffset = nullPos + 1;
return value.toString(encoding || this.encoding);
this._readOffset = nullPos + 1;
return value.toString(encoding || this._encoding);
}
/**
* Writes a null-terminated String to the current write position.
* Inserts a null-terminated String.
*

@@ -521,7 +670,20 @@ * @param value { String } The String value to write.

*/
writeStringNT(value, offset, encoding) {
insertStringNT(value, offset, encoding) {
utils_1.checkOffsetValue(offset);
// Write Values
this.writeString(value, offset, encoding);
this.writeUInt8(0x00, (typeof offset === 'number' ? offset + value.length : this.writeOffset));
this.insertString(value, offset, encoding);
this.insertUInt8(0x00, offset + value.length);
}
/**
* 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));
}
// Buffers

@@ -536,8 +698,11 @@ /**

readBuffer(length) {
if (typeof length !== 'undefined') {
utils_1.checkLengthValue(length);
}
const lengthVal = typeof length === 'number' ? length : this.length;
const endPoint = Math.min(this.length, this.readOffset + lengthVal);
const endPoint = Math.min(this.length, this._readOffset + lengthVal);
// Read buffer value
const value = this.buff.slice(this.readOffset, endPoint);
const value = this._buff.slice(this._readOffset, endPoint);
// Increment internal Buffer read offset
this.readOffset = endPoint;
this._readOffset = endPoint;
return value;

@@ -551,11 +716,14 @@ }

*/
insertBuffer(value, offset) {
utils_1.checkOffsetValue(offset);
return this._handleBuffer(value, true, 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.
*/
writeBuffer(value, offset) {
const offsetVal = typeof offset === 'number' ? offset : this.writeOffset;
// Ensure there is enough internal Buffer capacity.
this.ensureWriteable(value.length, offsetVal);
// Write buffer value
value.copy(this.buff, offsetVal);
// Increment internal Buffer write offset
this.writeOffset += value.length;
return this;
return this._handleBuffer(value, false, offset);
}

@@ -571,4 +739,4 @@ /**

// Find next null character (if one is not found, default from above is used)
for (let i = this.readOffset; i < this.length; i++) {
if (this.buff[i] === 0x00) {
for (let i = this._readOffset; i < this.length; i++) {
if (this._buff[i] === 0x00) {
nullPos = i;

@@ -579,9 +747,9 @@ break;

// Read value
const value = this.buff.slice(this.readOffset, nullPos);
const value = this._buff.slice(this._readOffset, nullPos);
// Increment internal Buffer read offset
this.readOffset = nullPos + 1;
this._readOffset = nullPos + 1;
return value;
}
/**
* Writes a null-terminated Buffer to the current write position.
* Inserts a null-terminated Buffer.
*

@@ -591,6 +759,23 @@ * @param value { Buffer } The Buffer to write.

*/
insertBufferNT(value, offset) {
utils_1.checkOffsetValue(offset);
// Write Values
this.insertBuffer(value, offset);
this.insertUInt8(0x00, offset + value.length);
return 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, offset) {
// Checks for valid numberic value;
if (typeof offset !== 'undefined') {
utils_1.checkOffsetValue(offset);
}
// Write Values
this.writeBuffer(value, offset);
this.writeUInt8(0, (typeof offset === 'number' ? offset + value.length : this.writeOffset));
this.writeUInt8(0x00, (typeof offset === 'number' ? offset + value.length : this._writeOffset));
return this;

@@ -602,4 +787,4 @@ }

clear() {
this.writeOffset = 0;
this.readOffset = 0;
this._writeOffset = 0;
this._readOffset = 0;
this.length = 0;

@@ -613,52 +798,74 @@ }

remaining() {
return this.length - this.readOffset;
return this.length - this._readOffset;
}
/**
* Moves the read offset forward.
* Gets the current read offset value of the SmartBuffer instance.
*
* @param amount { Number } The amount to move the read offset forward by.
* @return { Number }
*/
skip(amount) {
if (this.readOffset + amount > this.length) {
throw new Error('Target position is beyond the bounds of the SmartBuffer size.');
}
this.readOffset += amount;
get readOffset() {
return this._readOffset;
}
/**
* Moves the read offset backwards.
* Sets the read offset value of the SmartBuffer instance.
*
* @param amount { Number } The amount to move the read offset backwards by.
* @param offset { Number } - The offset value to set.
*/
rewind(amount) {
if (this.readOffset - amount < 0) {
throw new Error('Target position is beyond the bounds of the SmartBuffer size.');
}
this.readOffset -= amount;
set readOffset(offset) {
utils_1.checkOffsetValue(offset);
// Check for bounds.
utils_1.checkTargetOffset(offset, this);
this._readOffset = offset;
}
/**
* Moves the read offset to a specific position.
* Gets the current write offset value of the SmartBuffer instance.
*
* @param position { Number } The position to move the read offset to.
* @return { Number }
*/
skipTo(position) {
this.moveTo(position);
get writeOffset() {
return this._writeOffset;
}
/**
* Moves the read offset to a specific position.
* Sets the write offset value of the SmartBuffer instance.
*
* @param position { Number } The position to move the read offset to.
* @param offset { Number } - The offset value to set.
*/
moveTo(position) {
if (position > this.length) {
throw new Error('Target position is beyond the bounds of the SmartBuffer size.');
}
this.readOffset = position;
set writeOffset(offset) {
utils_1.checkOffsetValue(offset);
// Check for bounds.
utils_1.checkTargetOffset(offset, this);
this._writeOffset = offset;
}
/**
* Gets the value of the internal managed Buffer
* Gets the currently set string encoding of the SmartBuffer instance.
*
* @return { BufferEncoding } The string Buffer encoding currently set.
*/
get encoding() {
return this._encoding;
}
/**
* Sets the string encoding of the SmartBuffer instance.
*
* @param encoding { BufferEncoding } The string Buffer encoding to set.
*/
set encoding(encoding) {
utils_1.checkEncoding(encoding);
this._encoding = encoding;
}
/**
* Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
*
* @return { Buffer } The Buffer value.
*/
get internalBuffer() {
return this._buff;
}
/**
* Gets the value of the internal managed Buffer (Includes managed data only)
*
* @param { Buffer }
*/
toBuffer() {
return this.buff.slice(0, this.length);
return this._buff.slice(0, this.length);
}

@@ -671,9 +878,6 @@ /**

toString(encoding) {
const encodingVal = typeof encoding === 'string' ? encoding : this.encoding;
if (Buffer.isEncoding(encodingVal)) {
return this.buff.toString(encodingVal, 0, this.length);
}
else {
throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
}
const encodingVal = typeof encoding === 'string' ? encoding : this._encoding;
// Check for invalid encoding.
utils_1.checkEncoding(encodingVal);
return this._buff.toString(encodingVal, 0, this.length);
}

@@ -687,29 +891,144 @@ /**

/**
* 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) {
let offsetVal = this._writeOffset;
let encodingVal = this._encoding;
// Check for offset
if (typeof arg3 === 'number') {
offsetVal = arg3;
// Check for encoding
}
else if (typeof arg3 === 'string') {
utils_1.checkEncoding(arg3);
encodingVal = arg3;
}
// Check for encoding (third param)
if (typeof encoding === 'string') {
utils_1.checkEncoding(encoding);
encodingVal = encoding;
}
// Calculate bytelength of string.
const byteLength = buffer_1.Buffer.byteLength(value, encodingVal);
// Ensure there is enough internal Buffer capacity.
if (isInsert) {
this.ensureInsertable(byteLength, offsetVal);
}
else {
this.ensureWriteable(byteLength, offsetVal);
}
// Write value
this._buff.write(value, offsetVal, byteLength, encodingVal);
// Increment internal Buffer write offset;
if (isInsert) {
this._writeOffset += byteLength;
}
else {
// If an offset was given, check to see if we wrote beyond the current writeOffset.
if (typeof arg3 === 'number') {
this._writeOffset = Math.max(this._writeOffset, offsetVal + byteLength);
}
else {
this._writeOffset += byteLength;
}
}
return this;
}
/**
* 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) {
const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;
// Ensure there is enough internal Buffer capacity.
if (isInsert) {
this.ensureInsertable(value.length, offsetVal);
}
else {
this.ensureWriteable(value.length, offsetVal);
}
// Write buffer value
value.copy(this._buff, offsetVal);
// Increment internal Buffer write offset;
if (isInsert) {
this._writeOffset += value.length;
}
else {
// If an offset was given, check to see if we wrote beyond the current writeOffset.
if (typeof offset === 'number') {
this._writeOffset = Math.max(this._writeOffset, offsetVal + value.length);
}
else {
this._writeOffset += value.length;
}
}
return this;
}
/**
* 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) {
if (this.remaining() < length) {
throw new Error('Reading beyond the bounds of the data.');
ensureReadable(length, offset) {
// Offset value defaults to managed read offset.
let offsetVal = this._readOffset;
// If an offset was provided, use it.
if (typeof offset !== 'undefined') {
// Checks for valid numberic value;
utils_1.checkOffsetValue(offset);
// Overide with custom offset.
offsetVal = offset;
}
// Checks if offset is below zero, or the offset+length offset is beyond the total length of the managed data.
if (offsetVal < 0 || offsetVal + length > this.length) {
throw new Error(utils_1.ERRORS.INVALID_READ_BEYOND_BOUNDS);
}
}
/**
* Ensures that the internal Buffer is large enough to write data.
* Ensures that the internal Buffer is large enough to insert data.
*
* @param minLength { Number } The minimum length of the data that needs to be written.
* @param dataLength { Number } The length of the data that needs to be written.
* @param offset { Number } The offset of the data to be written.
*/
ensureWriteable(minLength, offset) {
const offsetVal = typeof offset === 'number' ? offset : 0;
ensureInsertable(dataLength, offset) {
// Checks for valid numberic value;
utils_1.checkOffsetValue(offset);
// Ensure there is enough internal Buffer capacity.
this.ensureCapacity(this.length + minLength + offsetVal);
// If offset is provided, copy data into appropriate location in regards to the offset.
if (typeof offset === 'number') {
this.buff.copy(this.buff, offsetVal + minLength, offsetVal, this.buff.length);
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.
if (offset < this.length) {
this._buff.copy(this._buff, offset + dataLength, offset, this._buff.length);
}
// Adjust instance length.
this.length = Math.max(this.length + minLength, offsetVal + minLength);
// Adjust tracked smart buffer length
if (offset + dataLength > this.length) {
this.length = offset + dataLength;
}
else {
this.length += dataLength;
}
}
/**
* 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);
// Adjust SmartBuffer length (if offset + length is larger than managed length, adjust length)
if (offsetVal + dataLength > this.length) {
this.length = offsetVal + dataLength;
}
}
/**
* Ensures that the internal Buffer is large enough to write at least the given amount of data.

@@ -720,5 +1039,5 @@ *

ensureCapacity(minLength) {
const oldLength = this.buff.length;
const oldLength = this._buff.length;
if (minLength > oldLength) {
let data = this.buff;
let data = this._buff;
let newLength = (oldLength * 3) / 2 + 1;

@@ -728,4 +1047,4 @@ if (newLength < minLength) {

}
this.buff = Buffer.allocUnsafe(newLength);
data.copy(this.buff, 0, 0, oldLength);
this._buff = buffer_1.Buffer.allocUnsafe(newLength);
data.copy(this._buff, 0, 0, oldLength);
}

@@ -738,15 +1057,18 @@ }

* @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) {
this.ensureReadable(byteSize);
readNumberValue(func, byteSize, offset) {
this.ensureReadable(byteSize, offset);
// Call Buffer.readXXXX();
const value = func.call(this.buff, this.readOffset);
// Adjust internal read offset
this.readOffset += byteSize;
const value = func.call(this._buff, typeof offset === 'number' ? offset : this._readOffset);
// Adjust internal read offset if an optional read offset was not provided.
if (typeof offset === 'undefined') {
this._readOffset += byteSize;
}
return value;
}
/**
* Writes a numeric number value using the provided function.
* Inserts a numeric number value based on the given offset and value.
*

@@ -756,16 +1078,48 @@ * @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.

* @param value { Number } The number value to write.
* @param offset { Number } the offset to write the number at.
* @param offset { Number } the offset to write the number at (REQUIRED).
*
*/
writeNumberValue(func, byteSize, value, offset) {
const offsetVal = typeof offset === 'number' ? offset : this.writeOffset;
insertNumberValue(func, byteSize, value, offset) {
// Check for invalid offset values.
utils_1.checkOffsetValue(offset);
// Ensure there is enough internal Buffer capacity. (raw offset is passed)
this.ensureWriteable(byteSize, offset);
this.ensureInsertable(byteSize, offset);
// Call buffer.writeXXXX();
func.call(this.buff, value, offsetVal);
// Adjusts internal write offset
this.writeOffset += byteSize;
func.call(this._buff, value, offset);
// Adjusts internally managed write offset.
this._writeOffset += byteSize;
}
/**
* 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.
if (typeof offset === 'number') {
// Check if we're writing beyond the bounds of the managed data.
if (offset < 0) {
throw new Error(utils_1.ERRORS.INVALID_WRITE_BEYOND_BOUNDS);
}
utils_1.checkOffsetValue(offset);
}
// Default to writeOffset if no offset value was given.
const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;
// Ensure there is enough internal Buffer capacity. (raw offset is passed)
this.ensureWriteable(byteSize, offsetVal);
func.call(this._buff, value, offsetVal);
// If an offset was given, check to see if we wrote beyond the current writeOffset.
if (typeof offset === 'number') {
this._writeOffset = Math.max(this._writeOffset, offsetVal + byteSize);
}
else {
this._writeOffset += byteSize;
}
}
}
exports.SmartBuffer = SmartBuffer;
//# sourceMappingURL=smartbuffer.js.map
# Change Log
## 4.0
> Released xx/xx/2017
* Major breaking changes arriving in v4
### New Features
* Ability to read data from a specific offset. ex: readInt8(5);
* Ability to write over data when an offset is given (see breaking changes) ex: writeInt8(5, 0);
* Ability to set internal read and write offsets.
### Breaking Changes
* Old constructor patterns have been completely removed. It's now required to use the SmartBuffer.fromXXX() factory constructors. Read more on the v4 docs.
* rewind(), skip(), moveTo() has 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)
* insertXXX() methods have been added for when you want to insert data at a specific offset.
### Other Changes
* Standardizd error messaging
* Standardized offset/length bounds and sanity checking
*
## 3.0.3

@@ -3,0 +28,0 @@ > Released 02/19/2017

{
"name": "smart-buffer",
"version": "3.0.3",
"version": "4.0.0-beta.1",
"description": "A smarter Buffer that keeps track of its own read and write positions while growing endlessly.",

@@ -31,18 +31,49 @@ "main": "build/smartbuffer.js",

"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.15",
"istanbul": "^0.4.3",
"mocha": "^3.2.0",
"mocha-lcov-reporter": "^1.2.0"
"@types/chai": "^4.0.1",
"@types/mocha": "^2.2.41",
"@types/node": "^8.0.2",
"chai": "^4.0.2",
"coveralls": "^2.13.1",
"istanbul": "^0.4.5",
"mocha": "^3.4.2",
"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"
},
"typings": "typings/index",
"dependencies": {
"@types/node": "^7.0.4"
"typings": "typings",
"dependencies": {},
"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",
"lint": "tslint --type-check --project tsconfig.json 'src/**/*.ts'",
"build": "tsc -p ./"
},
"scripts": {
"test": "mocha test/smartbuffer.test.js",
"coverage": "istanbul cover node_modules/mocha/bin/_mocha recursive test",
"fullcoverage": "node_modules/.bin/istanbul -include-all-sources cover node_modules/mocha/bin/_mocha recursive test",
"prepublish": "npm install -g typescript && tsc -p ./"
"nyc": {
"extension": [
".ts",
".tsx"
],
"include": [
"src/*.ts",
"src/**/*.ts"
],
"exclude": [
"**.*.d.ts",
"node_modules",
"typings"
],
"require": [
"ts-node/register"
],
"reporter": [
"json",
"html"
],
"all": true
}
}

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc