Skip to content

Commit

Permalink
feat: rename to buffer-pack
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Shepheard committed Jun 19, 2014
1 parent ba41db9 commit 379a9f1
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 213 deletions.
10 changes: 4 additions & 6 deletions README.md
@@ -1,15 +1,13 @@
# @coinative/struct
# buffer-pack

[![Build Status](https://travis-ci.org/coinative/struct.svg?branch=master)](https://travis-ci.org/coinative/struct) [![Coverage Status](https://img.shields.io/coveralls/coinative/struct.svg)](https://coveralls.io/r/coinative/struct?branch=master)
[![Build Status](https://travis-ci.org/coinative/buffer-pack.svg?branch=master)](https://travis-ci.org/coinative/buffer-pack) [![Coverage Status](https://img.shields.io/coveralls/coinative/buffer-pack.svg)](https://coveralls.io/r/coinative/buffer-pack?branch=master)

Define structures to use for parsing and serialization.
Define a format to pack and unpack objects to/from buffers.

## Install

Not currently hosted on npmjs.org. Take this module as a git dependency via:

```
npm install coinative/struct
npm install buffer-pack
```

## License
Expand Down
20 changes: 10 additions & 10 deletions lib/codecs/array.js
@@ -1,28 +1,28 @@
'use strict';

module.exports = function (Struct) {
Struct.addCodec('array', function (options) {
module.exports = function (Packer) {
Packer.addCodec('array', function (options) {
var size = options.length;
var codec = options.type;
return {
serialize: function (value) {
pack: function (value) {
value = typeof value !== 'undefined' ? value : options.default;
for (var i = 0; i < size; i++) {
var item = value[i];
if (codec instanceof Struct) {
codec.serialize(item, this);
if (codec instanceof Packer) {
codec.pack(item, this);
} else {
codec.serialize.call(this, item);
codec.pack.call(this, item);
}
}
},
parse: function () {
unpack: function () {
var items = [];
for (var i = 0; i < size; i++) {
if (codec instanceof Struct) {
items.push(codec.parse(null, this));
if (codec instanceof Packer) {
items.push(codec.unpack(null, this));
} else {
items.push(codec.parse.call(this));
items.push(codec.unpack.call(this));
}
}
return items;
Expand Down
8 changes: 4 additions & 4 deletions lib/codecs/buffer.js
@@ -1,10 +1,10 @@
'use strict';

module.exports = function (Struct) {
Struct.addCodec('buffer', function (options) {
module.exports = function (Packer) {
Packer.addCodec('buffer', function (options) {
var size = options.length;
return {
serialize: function (value) {
pack: function (value) {
value = typeof value !== 'undefined' ? value : options.default;
var length = size;
if (typeof size === 'function') {
Expand All @@ -13,7 +13,7 @@ module.exports = function (Struct) {
value.copy(this.buffer, this.offset || 0, 0, length);
this.offset += length;
},
parse: function (obj) {
unpack: function (obj) {
var length = size;
if (typeof size === 'function') {
length = size.call(obj);
Expand Down
42 changes: 21 additions & 21 deletions lib/codecs/integer.js
Expand Up @@ -7,12 +7,12 @@ function intCodec(size, method) {
var writeMethod = 'write' + method;
return function (options) {
return {
serialize: function (value) {
pack: function (value) {
value = typeof value !== 'undefined' ? value : options.default;
this.buffer[writeMethod](value, this.offset);
this.offset += size;
},
parse: function () {
unpack: function () {
var value = this.buffer[readMethod](this.offset);
this.offset += size;
return value;
Expand All @@ -30,7 +30,7 @@ function int64Codec(signed, be) {
return function (options) {
if (signed) {
return {
serialize: function (value) {
pack: function (value) {
value = typeof value !== 'undefined' ? value : options.default;
if (Math.abs(value) > MAX_SAFE_INTEGER) {
throw new Error('Unsafe integer');
Expand All @@ -39,7 +39,7 @@ function int64Codec(signed, be) {
this.buffer['writeUInt32' + (be ? 'BE' : 'LE')](value >>> 0, this.offset + loOffset);
this.offset += 8;
},
parse: function () {
unpack: function () {
var high = this.buffer['readInt32' + (be ? 'BE' : 'LE')](this.offset + hiOffset) * Math.pow(2, 32);
var low = this.buffer['readUInt32' + (be ? 'BE' : 'LE')](this.offset + loOffset);
this.offset += 8;
Expand All @@ -51,7 +51,7 @@ function int64Codec(signed, be) {
};
} else {
return {
serialize: function (value) {
pack: function (value) {
value = typeof value !== 'undefined' ? value : options.default;
if (Math.abs(value) > MAX_SAFE_INTEGER) {
throw new Error('Unsafe integer');
Expand All @@ -60,7 +60,7 @@ function int64Codec(signed, be) {
this.buffer['writeUInt32' + (be ? 'BE' : 'LE')](value >>> 0, this.offset + loOffset);
this.offset += 8;
},
parse: function () {
unpack: function () {
var high = this.buffer['readUInt32' + (be ? 'BE' : 'LE')](this.offset + hiOffset) * Math.pow(2, 32);
var low = this.buffer['readUInt32' + (be ? 'BE' : 'LE')](this.offset + loOffset);
this.offset += 8;
Expand All @@ -74,22 +74,22 @@ function int64Codec(signed, be) {
}
}

module.exports = function (Struct) {
Struct.addCodec('uint8', intCodec(1, 'UInt8'));
Struct.addCodec('int8', intCodec(1, 'Int8'));
module.exports = function (Packer) {
Packer.addCodec('uint8', intCodec(1, 'UInt8'));
Packer.addCodec('int8', intCodec(1, 'Int8'));

Struct.addCodec('uint16be', intCodec(2, 'UInt16BE'));
Struct.addCodec('uint16le', intCodec(2, 'UInt16LE'));
Struct.addCodec('int16be', intCodec(2, 'Int16BE'));
Struct.addCodec('int16le', intCodec(2, 'Int16LE'));
Packer.addCodec('uint16be', intCodec(2, 'UInt16BE'));
Packer.addCodec('uint16le', intCodec(2, 'UInt16LE'));
Packer.addCodec('int16be', intCodec(2, 'Int16BE'));
Packer.addCodec('int16le', intCodec(2, 'Int16LE'));

Struct.addCodec('uint32be', intCodec(4, 'UInt32BE'));
Struct.addCodec('uint32le', intCodec(4, 'UInt32LE'));
Struct.addCodec('int32be', intCodec(4, 'Int32BE'));
Struct.addCodec('int32le', intCodec(4, 'Int32LE'));
Packer.addCodec('uint32be', intCodec(4, 'UInt32BE'));
Packer.addCodec('uint32le', intCodec(4, 'UInt32LE'));
Packer.addCodec('int32be', intCodec(4, 'Int32BE'));
Packer.addCodec('int32le', intCodec(4, 'Int32LE'));

Struct.addCodec('uint64be', int64Codec(false, true));
Struct.addCodec('uint64le', int64Codec(false, false));
Struct.addCodec('int64be', int64Codec(true, true));
Struct.addCodec('int64le', int64Codec(true, false));
Packer.addCodec('uint64be', int64Codec(false, true));
Packer.addCodec('uint64le', int64Codec(false, false));
Packer.addCodec('int64be', int64Codec(true, true));
Packer.addCodec('int64le', int64Codec(true, false));
};
8 changes: 4 additions & 4 deletions lib/codecs/string.js
@@ -1,11 +1,11 @@
'use strict';

module.exports = function (Struct) {
Struct.addCodec('str', function (options) {
module.exports = function (Packer) {
Packer.addCodec('str', function (options) {
var size = options.length;
var encoding = options.encoding || 'utf8';
return {
serialize: function (value) {
pack: function (value) {
value = typeof value !== 'undefined' ? value : options.default;
var length = Buffer.byteLength(value, encoding);
var written = this.buffer.write(value, this.offset || 0, size, encoding);
Expand All @@ -14,7 +14,7 @@ module.exports = function (Struct) {
}
this.offset += size;
},
parse: function () {
unpack: function () {
var value = this.buffer.toString(encoding, this.offset, this.offset + size);
if (typeof options.pad !== 'undefined') {
var pad = typeof options.pad === 'number' ? String.fromCharCode(options.pad) : options.pad;
Expand Down
42 changes: 21 additions & 21 deletions lib/index.js
Expand Up @@ -5,32 +5,32 @@ function Context(buffer) {
this.offset = 0;
}

function Struct() {
if (!(this instanceof Struct)) {
return new Struct();
function Packer() {
if (!(this instanceof Packer)) {
return new Packer();
}
this.fields = [];
return this;
}

Struct.prototype.serialize = function (obj, context) {
Packer.prototype.pack = function (obj, context) {
context = context || new Context(new Buffer(this.size(obj)));
this.fields.forEach(function (field) {
field.serialize.call(context, obj);
field.pack.call(context, obj);
});
return context.buffer;
};

Struct.prototype.parse = function (buffer, context) {
Packer.prototype.unpack = function (buffer, context) {
context = context || new Context(buffer);
var obj = {};
this.fields.forEach(function (field) {
field.parse.call(context, obj);
field.unpack.call(context, obj);
});
return obj;
};

Struct.prototype.size = function (obj) {
Packer.prototype.size = function (obj) {
var length = 0;
this.fields.forEach(function (field) {
length += field.size(obj);
Expand All @@ -45,11 +45,11 @@ function fieldify(_codec) {
codec = codec(options || {});
}
this.fields.push({
serialize: function(obj) {
codec.serialize.call(this, obj[name]);
pack: function(obj) {
codec.pack.call(this, obj[name]);
},
parse: function (obj) {
obj[name] = codec.parse.call(this, obj);
unpack: function (obj) {
obj[name] = codec.unpack.call(this, obj);
},
size: function (obj) {
return codec.size(obj[name]);
Expand All @@ -59,16 +59,16 @@ function fieldify(_codec) {
};
}

Struct.codecs = {};
Packer.codecs = {};

Struct.addCodec = function (type, codec) {
Struct.codecs[type] = codec;
Struct.prototype[type] = fieldify(codec);
Packer.addCodec = function (type, codec) {
Packer.codecs[type] = codec;
Packer.prototype[type] = fieldify(codec);
};

require('./codecs/integer')(Struct);
require('./codecs/buffer')(Struct);
require('./codecs/string')(Struct);
require('./codecs/array')(Struct);
require('./codecs/integer')(Packer);
require('./codecs/buffer')(Packer);
require('./codecs/string')(Packer);
require('./codecs/array')(Packer);

module.exports = Struct;
module.exports = Packer;
23 changes: 13 additions & 10 deletions package.json
@@ -1,26 +1,29 @@
{
"name": "struct",
"name": "buffer-pack",
"version": "0.0.1",
"description": "",
"main": "lib/index.js",
"description": "Define a format to pack and unpack objects to/from buffers",
"main": "./lib/index.js",
"contributors": [
"Ben Shepheard <ben@coinative.com>",
"Dan Howitt <dan@coinative.com>"
],
"repository": {
"type": "git",
"url": "https://github.com/coinative/struct.git"
"url": "https://github.com/coinative/buffer-pack.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/coinative/struct/issues"
"url": "https://github.com/coinative/buffer-pack/issues"
},
"homepage": "https://github.com/coinative/struct",
"homepage": "https://github.com/coinative/buffer-pack",
"license": "MIT",
"scripts": {
"test": "./node_modules/.bin/mocha -r ./test/support/env -R dot ./test",
"test-cov": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- -r ./test/support/env -R dot ./test",
"test-travis": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -r ./test/support/env -R spec ./test"
},
"devDependencies": {
"chai": "^1.9.1",
"istanbul": "^0.2.10",
"mocha": "^1.19.0"
"istanbul": "^0.2.11",
"mocha": "^1.20.1"
}
}

0 comments on commit 379a9f1

Please sign in to comment.