Skip to content

Commit

Permalink
Merge changes resulting in v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pabigot committed Nov 20, 2015
2 parents 7a2dd33 + 4332e9b commit a5d0ed8
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/coverage/
/node_modules/
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- "5.1.0"
- "5.0.0"
- "4.2"
- "4.1"
- "4.0"
- "0.12"
after_success:
- npm run lint.strict
- npm run coveralls
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# buffer-layout

[![NPM version](https://img.shields.io/npm/v/buffer-layout.svg)](https://www.npmjs.com/package/buffer-layout "View this project on NPM")
[![Build Status](https://travis-ci.org/pabigot/buffer-layout.svg?branch=master)](https://travis-ci.org/pabigot/buffer-layout "Check build status on TravisCI")
[![Coverage Status](https://coveralls.io/repos/pabigot/buffer-layout/badge.svg?branch=master&service=github)](https://coveralls.io/github/pabigot/buffer-layout?branch=master "Check coverage status on Coveralls")

buffer-layout is a utility module implemented in pure JavaScript that
supports translations between JavaScript values and Buffers. It is made
available through [github](https://github.com/pabigot/buffer-layout) and
Expand Down
22 changes: 21 additions & 1 deletion lib/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,18 @@
/*jslint
bitwise:true, this:true, white:true
*/
/*jshint -W034 */
/*jshint -W034 */ // don't whine about explicit "use strict"
"use strict";

/* istanbul ignore next */
/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger */
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};

/* istanbul ignore next */
/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign */
if (!Object.assign) {
Object.defineProperty(Object, 'assign', {
Expand Down Expand Up @@ -1488,3 +1490,21 @@ exports.f64 = function (property) { return new Double(property); };

/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
exports.f64be = function (property) { return new DoubleBE(property); };

/** Factory for {@link Structure|Structure} values. */
exports.struct = function (fields, property) { return new Structure(fields, property); };

/** Factory for {@link BitStructure|BitStructure} values. */
exports.bits = function (word, msb, property) { return new BitStructure(word, msb, property); };

/** Factory for {@link Sequence|Sequence} values. */
exports.seq = function (elt_layout, count, property) { return new Sequence(elt_layout, count, property); };

/** Factory for {@link Union|Union} values. */
exports.union = function (discr, default_layout, property) { return new Union(discr, default_layout, property); };

/** Factory for {@link UnionLayoutDiscriminator|UnionLayoutDiscriminator} values. */
exports.unionLayoutDiscriminator = function (layout, offset, property) { return new UnionLayoutDiscriminator(layout, offset, property); };

/** Factory for {@link Blob|Blob} values. */
exports.blob = function (length) { return new Blob(length); };
43 changes: 24 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
{
"name": "buffer-layout",
"version": "0.4.0",
"version": "0.5.0",
"description": "Translation between JavaScript values and Buffers",
"keywords": [
"Buffer",
"struct",
"endian",
"pack data"
"Buffer",
"struct",
"endian",
"pack data"
],
"homepage": "https://github.com/pabigot/buffer-layout",
"bugs": "https://github.com/pabigot/buffer-layout/issues",
"repository" : {
"type": "git",
"url": "https://github.com/pabigot/buffer-layout.git"
"type": "git",
"url": "https://github.com/pabigot/buffer-layout.git"
},
"license": "MIT",
"author": "Peter A. Bigot <pab@pabigot.com>",
"main": "./lib/Layout.js",
"devDependencies": {
"jsdoc": "~3.3.3",
"jshint": "~2.8.0",
"jslint": "~0.9.3",
"lodash": "~3.9.3",
"mocha": "~2.2.5"
"coveralls": "~2.11.4",
"istanbul": "~0.4.0",
"jsdoc": "~3.3.3",
"jshint": "^2.8.0",
"jslint": "~0.9.3",
"lodash": "~3.9.3",
"mocha": "~2.2.5"
},
"engines": {
"node": ">=0.12"
"node": ">=0.12"
},
"scripts": {
"mkdoc": "jsdoc -c jsdoc/conf.json",
"lint": "jshint lib/Layout.js || true",
"jslint": "jslint lib/Layout.js || true",
"test": "mocha -u tdd"
"coverage": "istanbul cover _mocha -- -u tdd",
"coveralls": "istanbul cover _mocha --report lcovonly -- -u tdd && cat ./coverage/lcov.info | coveralls",
"jslint": "jslint lib/Layout.js || true",
"lint": "jshint lib/Layout.js || true",
"lint.strict": "jshint lib/Layout.js",
"mkdoc": "jsdoc -c jsdoc/conf.json",
"test": "mocha -u tdd"
},
"jshintConfig": {
"node": true,
"laxbreak": true
"node": true,
"laxbreak": true
}
}
81 changes: 75 additions & 6 deletions test/LayoutTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ suite("Layout", function () {
assert.equal(lo.u48be().decode(b), 0x8fb779f22087);
assert.equal(lo.u48().decode(b), 0x8720f279b78f);
});
test("invalid ctor", function () {
assert.throws(function () { new lo.IntBE(8, 'u64'); }, TypeError);
});
});
test("Float", function () {
var be = lo.f32be('eff'),
Expand Down Expand Up @@ -277,8 +280,8 @@ suite("Layout", function () {
assert(_.isEqual(seq.decode(b), [1,5,6,4]));
});
test("in struct", function () {
var seq = new lo.Sequence(lo.u8(), 4, 'id'),
str = new lo.Structure([seq]),
var seq = lo.seq(lo.u8(), 4, 'id'),
str = lo.struct([seq]),
d = str.decode(Buffer('01020304', 'hex'));
assert(_.isEqual(d, {id:[1,2,3,4]}));
});
Expand Down Expand Up @@ -449,6 +452,21 @@ suite("Layout", function () {
assert.strictEqual(d.property, undefined);
});
});
suite("UnionDiscriminator", function () {
test("abstract", function () {
var ud = new lo.UnionDiscriminator('p');
assert.equal(ud.property, 'p');
assert.throws(function () { ud.decode(Buffer('00', 'hex')); }, Error);
assert.throws(function () { ud.encode(0, new Buffer(1)); }, Error);
});
});
suite("UnionLayoutDiscriminator", function () {
test("invalid ctor", function () {
assert.throws(function () { new lo.UnionLayoutDiscriminator('hi'); }, TypeError);
assert.throws(function () { new lo.UnionLayoutDiscriminator(lo.f32()); }, TypeError);
assert.throws(function () { new lo.UnionLayoutDiscriminator(lo.u8(), 'hi'); }, TypeError);
});
});
suite("Union", function () {
test("invalid ctor", function () {
assert.throws(function () { new lo.Union(); }, TypeError);
Expand Down Expand Up @@ -524,6 +542,13 @@ suite("Layout", function () {
rv = un.decode(b, 0, obj);
assert.strictEqual(rv, obj);
assert(_.isEqual(rv, {a:1, b:1, c:257, destination:true}));
un.discriminator.encode(v2.variant, b);
assert.equal(Buffer('0201010101', 'hex').compare(b), 0);
obj = {a:5, b:6, c:1540};
v3.encode(obj, b);
assert(_.isEqual(un.decode(b), obj));
assert.equal(Buffer('0305060406', 'hex').compare(b), 0);
assert.throws(function () { v2.decode(b); }, Error);
})
test("custom default", function () {
var dlo = lo.u8('number'),
Expand Down Expand Up @@ -680,28 +705,54 @@ suite("Layout", function () {
assert.throws(function () { new lo.BitStructure(lo.f32()); }, TypeError);
assert.throws(function () { new lo.BitStructure(lo.s32()); }, TypeError);
assert.throws(function () { new lo.BitStructure(lo.u40()); }, Error);

var bs = new lo.BitStructure(lo.u32());
assert.throws(function () { new lo.BitField(lo.u32(), 8); }, TypeError);
assert.throws(function () { new lo.BitField(bs, 'hi'); }, TypeError);
assert.throws(function () { new lo.BitField(bs, 0); }, TypeError);
assert.throws(function () { new lo.BitField(bs, 40); }, Error);
});
test("invalid add", function () {
assert.throws(function () {
var bs = new lo.BitStructure(lo.u32()),
var bs = lo.bits(lo.u32()),
bf1 = bs.addField(30),
bf2 = bs.addField(3);
}, Error);
assert.throws(function () {
var bs = new lo.BitStructure(lo.u8()),
var bs = lo.bits(lo.u8()),
bf1 = addField(2),
bf2 = addField(7);
}, Error);
assert.throws(function () {
var bs = new lo.BitStructure(lo.u8()),
var bs = lo.bits(lo.u8()),
bf1 = addField(0);
}, Error);
assert.throws(function () {
var bs = new lo.BitStructure(lo.u8()),
var bs = lo.bits(lo.u8()),
bf1 = addField(6),
bf2 = addField(-2);
}, Error);
});
test("size", function () {
var bs = new lo.BitStructure(lo.u16()),
bf10 = bs.addField(10, 'ten'),
bf6 = bs.addField(6, 'six'),
b = new Buffer(bs.span);
assert.equal((1 << 10) - 1, 1023);
assert.equal((1 << 6) - 1, 63);
var obj = bs.decode(Buffer('ffff', 'hex'));
assert.equal(obj.ten, (1 << 10) - 1);
assert.equal(obj.six, (1 << 6) - 1);
bs.encode(obj, b);
assert.equal(Buffer('ffff', 'hex').compare(b), 0);
b.fill(0);
assert.equal(Buffer('0000', 'hex').compare(b), 0);
bf10.encode((1 << 10) - 1);
bf6.encode((1 << 6) - 1);
assert.equal(bs._packedGetValue(), 0xFFFF);
assert.throws(function () { bf6.encode('hi', b); }, Error);
assert.throws(function () { bf6.encode(1 << 6, b); }, Error);
});
test("basic LSB", function () {
var pbl = lo.u32(),
bs = new lo.BitStructure(pbl);
Expand Down Expand Up @@ -867,6 +918,8 @@ suite("Layout", function () {
assert.equal(Buffer("030405", 'hex').compare(bv), 0);
bl.encode(Buffer("112233", 'hex'), b, 1);
assert.equal(Buffer("0111223305", 'hex').compare(b), 0);
assert.throws(function () { bl.encode('ABC', b); }, Error);
assert.throws(function () { bl.encode(Buffer('0102', 'hex'), b); }, Error);
});
});
suite("issue#8", function () {
Expand Down Expand Up @@ -921,4 +974,20 @@ suite("Layout", function () {
//assert(_.isEqual(dp, {id:1, ver:2, u32: [0x13121110, 0x17161514]}));
});
});
suite("factories", function () {
test("anon", function () {
var ver = lo.u8('ver'),
hdr = lo.struct([lo.u8('id'),
lo.u8('ver')]),
ud = lo.unionLayoutDiscriminator(ver, -1),
pld = lo.union(ud, lo.blob(8, 'blob')),
pkt = lo.struct([hdr, pld]),
exp_blob = Buffer('1011121314151617', 'hex'),
b = Buffer('01021011121314151617', 'hex');
assert(hdr instanceof lo.Structure);
assert(ud instanceof lo.UnionDiscriminator);
assert(pld instanceof lo.Union);
assert(pld.default_layout instanceof lo.Blob);
});
});
});

0 comments on commit a5d0ed8

Please sign in to comment.