buffercursor
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -21,4 +21,4 @@ // Copyright 2012 Timothy J Fontaine <tjfontaine@gmail.com> | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
var VError = require('verror'); | ||
@@ -41,8 +41,8 @@ var BufferCursor = module.exports = function(buff, noAssert) { | ||
var BCO = BufferCursor.BufferCursorOverflow = function() { | ||
Error.apply(this, Array.prototype.slice(arguments)); | ||
VError.apply(this, Array.prototype.slice.call(arguments, 0)); | ||
}; | ||
util.inherits(BCO, Error); | ||
util.inherits(BCO, VError); | ||
BufferCursor.prototype._move = function(step) { | ||
assert(this._pos + step <= this.buffer.length, 'Cannot read beyond buffer'); | ||
this._checkWrite(step); | ||
this._pos += step; | ||
@@ -52,9 +52,36 @@ }; | ||
BufferCursor.prototype._checkWrite = function(size) { | ||
if (this._pos + size > this.length) | ||
throw new BCO(); | ||
var shouldThrow = false; | ||
var length = this.length; | ||
var pos = this._pos; | ||
if (size > length) | ||
shouldThrow = true; | ||
if (length - pos < size) | ||
shouldThrow = true; | ||
if (shouldThrow) { | ||
var bco = new BCO('BufferCursorOverflow: length %d, position %d, size %d', | ||
length, | ||
pos, | ||
size); | ||
bco.kind = 'BufferCursorOverflow'; | ||
bco.length = length; | ||
bco.position = pos; | ||
bco.size = size; | ||
throw bco; | ||
} | ||
} | ||
BufferCursor.prototype.seek = function(pos) { | ||
assert(pos >= 0, 'Cannot seek behind 0'); | ||
assert(pos <= this.buffer.length, 'Cannot seek beyond buffer length'); | ||
if (pos < 0) | ||
throw new VError(new RangeError('Cannot seek before start of buffer'), | ||
'Negative seek values not allowed: %d', pos); | ||
if (pos > this.length) | ||
throw new VError(new RangeError('Trying to seek beyond buffer'), | ||
'Requested %d position is beyond length %d', | ||
pos, this.length); | ||
this._pos = pos; | ||
@@ -126,2 +153,5 @@ return this; | ||
// This method doesn't need to _checkWrite because Buffer implicitly truncates | ||
// to the length of the buffer, it's the only method in Node core that behaves | ||
// this way by default | ||
BufferCursor.prototype.write = function(value, length, encoding) { | ||
@@ -144,2 +174,4 @@ var end, ret; | ||
this._checkWrite(end - this._pos); | ||
this.buffer.fill(value, this._pos, end); | ||
@@ -146,0 +178,0 @@ this.seek(end); |
{ | ||
"name": "buffercursor", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"author": "Timothy J Fontaine <tjfontaine@gmail.com> (http://atxconsulting.com)", | ||
@@ -22,3 +22,6 @@ "description": "A simple way to traverse a Buffer like a cursor, updating position along the way", | ||
"node": ">= 0.5.0" | ||
}, | ||
"dependencies": { | ||
"verror": "^1.4.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11312
292
1
+ Addedverror@^1.4.0
+ Addedassert-plus@1.0.0(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedextsprintf@1.4.1(transitive)
+ Addedverror@1.10.1(transitive)