smart-buffer
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -125,68 +125,8 @@ "use strict"; | ||
/** | ||
* Ensures that the internal Buffer is large enough to write data. | ||
* | ||
* @param minLength { Number } The minimum length of the data that needs to be written. | ||
* @param offset { Number } The offset of the data to be written. | ||
* Type checking function that determines if an object is a SmartBufferOptions object. | ||
*/ | ||
ensureWriteable(minLength, offset) { | ||
const offsetVal = typeof offset === 'number' ? offset : 0; | ||
// 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); | ||
} | ||
// Adjust instance length. | ||
this.length = Math.max(this.length + minLength, offsetVal + minLength); | ||
static isSmartBufferOptions(options) { | ||
const castOptions = options; | ||
return castOptions && (castOptions.encoding !== undefined || castOptions.size !== undefined || castOptions.buff !== undefined); | ||
} | ||
/** | ||
* 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; | ||
if (newLength < minLength) { | ||
newLength = minLength; | ||
} | ||
this.buff = Buffer.allocUnsafe(newLength); | ||
data.copy(this.buff, 0, 0, oldLength); | ||
} | ||
} | ||
/** | ||
* 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 { Number } | ||
*/ | ||
readNumberValue(func, byteSize) { | ||
// Call Buffer.readXXXX(); | ||
const value = func.call(this.buff, this.readOffset); | ||
// Adjust internal read offset | ||
this.readOffset += byteSize; | ||
return value; | ||
} | ||
/** | ||
* Writes a numeric number value using the provided function. | ||
* | ||
* @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. | ||
* | ||
*/ | ||
writeNumberValue(func, byteSize, value, offset) { | ||
const offsetVal = typeof offset === 'number' ? offset : this.writeOffset; | ||
// Ensure there is enough internal Buffer capacity. (raw offset is passed) | ||
this.ensureWriteable(byteSize, offset); | ||
// Call buffer.writeXXXX(); | ||
func.call(this.buff, value, offsetVal); | ||
// Adjusts internal write offset | ||
this.writeOffset += byteSize; | ||
} | ||
// Signed integers | ||
@@ -719,10 +659,70 @@ /** | ||
/** | ||
* Type checking function that determines if an object is a SmartBufferOptions object. | ||
* Ensures that the internal Buffer is large enough to write data. | ||
* | ||
* @param minLength { Number } The minimum 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; | ||
// 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); | ||
} | ||
// Adjust instance length. | ||
this.length = Math.max(this.length + minLength, offsetVal + 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. | ||
*/ | ||
static isSmartBufferOptions(options) { | ||
const castOptions = options; | ||
return castOptions && (castOptions.encoding !== undefined || castOptions.size !== undefined || castOptions.buff !== undefined); | ||
ensureCapacity(minLength) { | ||
const oldLength = this.buff.length; | ||
if (minLength > oldLength) { | ||
let data = this.buff; | ||
let newLength = (oldLength * 3) / 2 + 1; | ||
if (newLength < minLength) { | ||
newLength = minLength; | ||
} | ||
this.buff = Buffer.allocUnsafe(newLength); | ||
data.copy(this.buff, 0, 0, oldLength); | ||
} | ||
} | ||
/** | ||
* 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 { Number } | ||
*/ | ||
readNumberValue(func, byteSize) { | ||
// Call Buffer.readXXXX(); | ||
const value = func.call(this.buff, this.readOffset); | ||
// Adjust internal read offset | ||
this.readOffset += byteSize; | ||
return value; | ||
} | ||
/** | ||
* Writes a numeric number value using the provided function. | ||
* | ||
* @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. | ||
* | ||
*/ | ||
writeNumberValue(func, byteSize, value, offset) { | ||
const offsetVal = typeof offset === 'number' ? offset : this.writeOffset; | ||
// Ensure there is enough internal Buffer capacity. (raw offset is passed) | ||
this.ensureWriteable(byteSize, offset); | ||
// Call buffer.writeXXXX(); | ||
func.call(this.buff, value, offsetVal); | ||
// Adjusts internal write offset | ||
this.writeOffset += byteSize; | ||
} | ||
} | ||
exports.SmartBuffer = SmartBuffer; | ||
//# sourceMappingURL=smartbuffer.js.map |
{ | ||
"name": "smart-buffer", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A smarter Buffer that keeps track of its own read and write positions while growing endlessly.", | ||
@@ -5,0 +5,0 @@ "main": "build/smartbuffer.js", |
@@ -37,3 +37,3 @@ 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) | ||
`yarn install smart-buffer` | ||
`yarn add smart-buffer` | ||
@@ -40,0 +40,0 @@ Note: The published NPM package includes the built javascript library. |
@@ -1,2 +0,2 @@ | ||
var SmartBuffer = require('../build/smartbuffer'); | ||
var SmartBuffer = require('../build/smartbuffer').SmartBuffer; | ||
var assert = require('chai').assert; | ||
@@ -3,0 +3,0 @@ |
@@ -12,5 +12,5 @@ /// <reference types="node" /> | ||
declare class SmartBuffer { | ||
private buff; | ||
length: number; | ||
encoding: BufferEncoding; | ||
private buff; | ||
private writeOffset; | ||
@@ -20,2 +20,31 @@ private readOffset; | ||
/** | ||
* 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 } | ||
*/ | ||
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. | ||
*/ | ||
static fromOptions(options: SmartBufferOptions): SmartBuffer | ||
/** | ||
* Type checking function that determines if an object is a SmartBufferOptions object. | ||
*/ | ||
static isSmartBufferOptions(options: SmartBufferOptions): options is SmartBufferOptions; | ||
/** | ||
* Creates a new SmartBuffer instance (defaults to utf8 encoding, 4096 internal Buffer size) | ||
@@ -63,48 +92,2 @@ */ | ||
/** | ||
* 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 } | ||
*/ | ||
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. | ||
*/ | ||
static fromOptions(options: SmartBufferOptions): SmartBuffer | ||
/** | ||
* Ensures that the internal Buffer is large enough to write data. | ||
* | ||
* @param minLength { Number } The minimum length of the data that needs to be written. | ||
* @param offset { Number } The offset of the data to be written. | ||
*/ | ||
private ensureWriteable(minLength, 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); | ||
/** | ||
* 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 { Number } | ||
*/ | ||
private readNumberValue(func, byteSize); | ||
/** | ||
* Writes a numeric number value using the provided function. | ||
@@ -441,5 +424,23 @@ * | ||
/** | ||
* Type checking function that determines if an object is a SmartBufferOptions object. | ||
* Ensures that the internal Buffer is large enough to write data. | ||
* | ||
* @param minLength { Number } The minimum length of the data that needs to be written. | ||
* @param offset { Number } The offset of the data to be written. | ||
*/ | ||
static isSmartBufferOptions(options: SmartBufferOptions): options is SmartBufferOptions; | ||
private ensureWriteable(minLength, 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); | ||
/** | ||
* 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 { Number } | ||
*/ | ||
private readNumberValue(func, byteSize); | ||
} | ||
@@ -446,0 +447,0 @@ |
Sorry, the diff of this file is not supported yet
91202
13
1680