New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

buffer-array

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

buffer-array - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

159

index.js
'use strict'
class BufferArray {
constructor(size) {
/**
* @param size {Buffer|Number|Array}
*/
constructor(size) {
this._buf = new Buffer(size)
this._pos = 0
if (Buffer.isBuffer(size)) {

@@ -12,6 +15,7 @@ this._pos = this._buf.length

}
/**
* Set pointer position
* @param {number} pos
* @param pos {Number} optional
* @returns {Number|void}
*/

@@ -22,3 +26,3 @@ seek(pos) {

}
this._pos = pos

@@ -29,16 +33,17 @@ }

* Write raw buffer to the end
* @param {buffer} buf
* @param buf {Buffer}
* @returns {Boolean}
*/
push(buf) {
if (!Buffer.isBuffer(buf)) {
throw new TypeError('Expected buffer')
throw new TypeError('Expected buffer')
}
if (out_of_bounds_in(this._buf, this._pos, buf.length)) {
return false
}
buf.copy(this._buf, this._pos)
this._pos = this._pos + buf.length
return true

@@ -49,11 +54,13 @@ }

* Read `size` bytes from the end
* @param size {Number}
* @returns {Buffer|Boolean}
*/
pop(size) {
if (out_of_bounds_out(this._pos, size)) {
return
return false
}
var bout = new Buffer(size)
this._buf.copy(bout, 0, this._pos - size, this._pos)
this._pos = this._pos - size

@@ -64,50 +71,59 @@ this._buf.fill(0, this._pos)

}
/**
* Write raw buffer to the beginning
* @param {buffer} buf
* @param {Buffer} buf
* @returns {Boolean}
*/
unshift(buf) {
if (!Buffer.isBuffer(buf)) {
throw new TypeError('Expected buffer')
throw new TypeError('Expected buffer')
}
if (out_of_bounds_in(this._buf, this._pos, buf.length)) {
return false
}
if (this._pos > 0) {
let buf = this._buf.slice(0, this._pos)
buf.copy(this._buf, buf.length)
let b = this._buf.slice(0, this._pos)
b.copy(this._buf, buf.length)
}
buf.copy(this._buf, 0)
this._pos = this._pos + buf.length
return true
}
/**
* Read `size` bytes from the beginning
* @param size {Number}
* @returns {Buffer|Boolean}
*/
shift(size) {
if (out_of_bounds_out(this._pos, size)) {
return
return false
}
var bout = new Buffer(size)
this._buf.copy(bout)
shift_buffer(this._buf, this._pos, size)
this._pos = this._pos - size
this._buf.fill(0, this._pos)
return bout
}
/**
* @returns {Number}
*/
get length() {
return this._buf.length
}
/**
* clear internal buffer
*/
clear() {

@@ -117,3 +133,7 @@ this._buf.fill(0)

}
/**
* convert buffer-array to Buffer
* @returns {Buffer}
*/
toBuffer() {

@@ -125,10 +145,17 @@ return this._buf

/**
* true if out of bounds
* return true if out of bounds
* @param buf {Buffer}
* @param pos {Number}
* @param size {Number}
* @returns {Boolean}
*/
function out_of_bounds_in(buf, pos, size) {
return pos + size >= buf.length
return pos + size > buf.length
}
/**
* true if out of bounds
* return true if out of bounds
* @param pos {Number}
* @param size {Number}
* @returns {Boolean}
*/

@@ -156,2 +183,9 @@ function out_of_bounds_out(pos, size) {

/**
* factory of `push*` methods
* @param method {String}
* @param size {Number}
* @returns {Function}
* @private
*/
function _push(method, size) {

@@ -162,6 +196,6 @@ return function(value) {

}
this._buf[method](value, this._pos)
this._pos = this._pos + size
return true

@@ -171,12 +205,19 @@ }

/**
* factory of `pop*` methods
* @param method {String}
* @param size {Number}
* @returns {Function}
* @private
*/
function _pop(method, size) {
return function() {
if (out_of_bounds_out(this._pos, size)) {
return
return
}
var value = this._buf[method](this._pos - size)
this._pos = this._pos - size
this._buf.fill(0, this._pos)
return value

@@ -186,2 +227,9 @@ }

/**
* factory of `unshift*` methods
* @param method {String}
* @param size {Number}
* @returns {Function}
* @private
*/
function _unshift(method, size) {

@@ -192,3 +240,3 @@ return function (value) {

}
if (this._pos > 0) {

@@ -198,6 +246,6 @@ let buf = this._buf.slice(0, this._pos)

}
this._buf[method](value, 0)
this._pos = this._pos + size
return true

@@ -207,15 +255,22 @@ }

/**
* factory of `shift*` methods
* @param method {String}
* @param size {Number}
* @returns {Function}
* @private
*/
function _shift(method, size) {
return function () {
if (out_of_bounds_out(this._pos, size)) {
return
return
}
var value = this._buf[method](0)
shift_buffer(this._buf, this._pos, size)
this._pos = this._pos - size
this._buf.fill(0, this._pos)
return value

@@ -225,2 +280,8 @@ }

/**
* move data from `source` buffer to the beginning
* @param source {Buffer}
* @param pos {Number}
* @param size {Number}
*/
function shift_buffer(source, pos, size) {

@@ -240,4 +301,8 @@ if (pos > 0) {

/**
* @param size {Buffer|Number|Array}
* @returns {BufferArray}
*/
module.exports = function ba(size) {
return new BufferArray(size)
}
}
{
"name": "buffer-array",
"version": "1.0.0",
"version": "1.0.1",
"description": "The Buffer with Array API",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -17,4 +17,4 @@ # buffer-array

ba.push(new Buffer([0, 0x0a]))
var x = ba.popInt16BE() // x == 10
buf.push(new Buffer([0, 0x0a]))
var x = buf.popInt16BE() // x == 10
```

@@ -24,2 +24,28 @@

* **`constructor(data: Buffer): BufferArray`**
* **`constructor(size: Number): BufferArray`**
Create new instance of `buffer-array` with fixed-size buffer
* **`seek(pos: Number): void`**
* **`seek(): Number`**
Set / get current pointer position.
```js
var packet = ba(38)
packet.pushInt32BE(640)
packet.seek() // == 4
packet.length // == 38
```
#### `length: Number`
Get length of the internal buffer
#### `clear(): void`
Remove all written data and set pointer position to 0.
#### `toBuffer(): Buffer`
Get internal buffer (_not a copy_)
#### `push(data: Buffer): bool`

@@ -106,2 +132,2 @@ Write buffer `data` to the end

## License
MIT, 2016 (c) Dmitry Tsvettsikh
MIT, 2016 (c) Dmitry Tsvettsikh

@@ -6,3 +6,3 @@ 'use strict'

test('constructor', function(t) {
test('constructor', function(t) {
var ba1 = new BufferArray(5)

@@ -12,3 +12,3 @@ var ba2 = new BufferArray(new Array(5))

var ba4 = BufferArray(5)
t.equal(ba1._buf.length, 5)

@@ -19,9 +19,9 @@ t.equal(ba2._buf.length, 5)

t.equal(ba4._buf.length, 5)
t.end()
})
test('method `length`', function(t) {
test('method `length`', function(t) {
var ba = new BufferArray(5)
t.equal(ba.length, 5)

@@ -31,13 +31,13 @@ t.end()

test('method `seek`', function(t) {
var ba1 = new BufferArray(5)
test('method `seek`', function(t) {
var ba1 = new BufferArray(5)
t.equal(ba1.seek(), 0)
ba1.seek(2)
t.equal(ba1.seek(), 2)
t.end()
})
test('method `clear`', function(t) {
test('method `clear`', function(t) {
var b = new Buffer(3);

@@ -47,10 +47,10 @@ b[0] = 13

b[2] = 19
var ba = new BufferArray(b)
ba.seek(1)
ba.clear()
t.ok(ba.toBuffer().equals(new Buffer([0,0,0])), 'buffers should be equals')
t.equal(ba.seek(), 0)
t.end()

@@ -60,18 +60,18 @@ })

test('method `push`', function(t) {
var ba = new BufferArray(5)
var ba = new BufferArray(4)
t.ok(ba.push(new Buffer([13, 17])))
t.ok(ba.push(new Buffer([23, 12])))
t.ok(ba.push(new Buffer([23, 12])))
t.equal(ba.seek(), 4)
var ba_b = ba.toBuffer().slice(0, 4)
t.ok(ba_b.equals(new Buffer([13, 17, 23, 12])), 'buffers should be equals')
t.notOk(ba.push(new Buffer([33, 7])), 'out of bounds')
t.equal(ba.seek(), 4)
t.throws(function () {
ba.push(3)
}, 'Expected buffer')
t.end()

@@ -82,5 +82,5 @@ })

var ba = new BufferArray(new Buffer([13, 17, 23, 12]))
var out = ba.pop(3)
t.equal(ba.seek(), 1)

@@ -90,3 +90,3 @@ t.ok(out.equals(new Buffer([17, 23, 12])), 'buffers should be equals')

t.notOk(ba.pop(2))
t.end()

@@ -97,9 +97,9 @@ })

var ba = new BufferArray(3)
t.ok(ba.pushInt16BE(10))
var int16 = ba.toBuffer().slice(0, 2)
t.ok(int16.equals(new Buffer([0, 0x0a])), 'buffers should be equals')
t.equal(ba.seek(), 2)
t.notOk(ba.pushInt16BE(10), 'out of bounds')

@@ -111,14 +111,14 @@ t.equal(ba.seek(), 2)

test('method `pop*`', function(t) {
var ba = new BufferArray(3)
var ba = new BufferArray(3)
ba.pushInt16BE(10)
var out = ba.popInt16BE()
t.equal(out, 10)
t.equal(ba.seek(), 0)
var out2 = ba.popInt16BE()
t.equal(out2, void 0)
t.equal(ba.seek(), 0)
t.end()

@@ -130,3 +130,3 @@ })

ba.pushInt16BE(10)
t.ok(ba.unshiftInt16BE(125))

@@ -138,3 +138,3 @@ t.equal(ba.seek(), 4)

t.equal(ba.popInt16BE(), 125)
t.end()

@@ -147,12 +147,12 @@ })

ba.pushInt16BE(114)
var out = ba.shiftInt16BE()
t.equal(out, 10)
t.equal(ba.seek(), 2)
var out2 = ba.shiftInt32BE()
t.equal(out2, void 0)
t.equal(ba.seek(), 2)
t.end()

@@ -163,5 +163,5 @@ })

var ba = new BufferArray(new Buffer([13, 17, 23, 12]))
var out = ba.shift(3)
t.equal(ba.seek(), 1)

@@ -171,3 +171,3 @@ t.ok(out.equals(new Buffer([13, 17, 23])), 'buffers should be equals')

t.notOk(ba.shift(2))
t.end()

@@ -178,18 +178,18 @@ })

var ba = new BufferArray(5)
t.ok(ba.unshift(new Buffer([13, 17])))
t.ok(ba.unshift(new Buffer([23, 12])))
t.equal(ba.seek(), 4)
var ba_b = ba.toBuffer().slice(0, 4)
t.ok(ba_b.equals(new Buffer([23, 12, 13, 17])), 'buffers should be equals')
t.ok(ba.unshift(new Buffer([23, 12, 58])))
t.equal(ba.seek(), 5)
var ba_b = ba.toBuffer().slice(0)
t.ok(ba_b.equals(new Buffer([23, 12, 58, 13, 17])), 'buffers should be equals')
t.notOk(ba.unshift(new Buffer([33, 7])), 'out of bounds')
t.equal(ba.seek(), 4)
t.equal(ba.seek(), 5)
t.throws(function () {
ba.unshift(3)
}, 'Expected buffer')
t.end()
})
})

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