Skip to content

Commit

Permalink
merge for 1.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
pabigot committed Jan 6, 2018
2 parents ed1a85d + 6403ce9 commit 253d8dd
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 484 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [1.1.0] - 2018-01-06

* **API** Add a third parameter to Structure specifying it should decode
short buffers as prefixes, resolving [issue #19][issue#19].
* **API** Interpret string argument to BitStructure `msb` parameter as
a `property` parameter, resolving [issue #17][issue#17].

## [1.0.0] - 2017-12-17

* Minimum Node version increased to 4.5 to support dependency
Expand Down Expand Up @@ -115,6 +122,7 @@

* Initial release.

[1.1.0]: https://github.com/pabigot/buffer-layout/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/pabigot/buffer-layout/compare/v0.13.0...v1.0.0
[0.13.0]: https://github.com/pabigot/buffer-layout/compare/v0.12.0...v0.13.0
[0.12.1]: https://github.com/pabigot/buffer-layout/compare/v0.12.0...v0.12.1
Expand Down Expand Up @@ -169,6 +177,8 @@
[issue#13]: https://github.com/pabigot/buffer-layout/issues/13
[issue#14]: https://github.com/pabigot/buffer-layout/issues/14
[issue#15]: https://github.com/pabigot/buffer-layout/issues/15
[issue#17]: https://github.com/pabigot/buffer-layout/issues/17
[issue#19]: https://github.com/pabigot/buffer-layout/issues/19
[ci:travis]: https://travis-ci.org/pabigot/buffer-layout
[ci:coveralls]: https://coveralls.io/github/pabigot/buffer-layout
[node:issue#3992]: https://github.com/nodejs/node/issues/3992
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Peter A. Bigot
Copyright (c) 2015-2018 Peter A. Bigot

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
43 changes: 37 additions & 6 deletions lib/Layout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* The MIT License (MIT)
*
* Copyright 2015-2017 Peter A. Bigot
* Copyright 2015-2018 Peter A. Bigot
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -1166,18 +1166,26 @@ class Sequence extends Layout {
* @param {string} [property] - initializer for {@link
* Layout#property|property}.
*
* @param {Boolean} [decodePrefixes] - initializer for {@link
* Structure#decodePrefixes|property}.
*
* @throws {Error} - if `fields` contains an unnamed variable-length
* layout.
*
* @constructor
* @augments {Layout}
*/
class Structure extends Layout {
constructor(fields, property) {
constructor(fields, property, decodePrefixes) {
if (!(Array.isArray(fields)
&& fields.reduce((acc, v) => acc && (v instanceof Layout), true))) {
throw new TypeError('fields must be array of Layout instances');
}
if (('boolean' === typeof property)
&& (undefined === decodePrefixes)) {
decodePrefixes = property;
property = undefined;
}

/* Verify absence of unnamed variable-length fields. */
for (const fd of fields) {
Expand Down Expand Up @@ -1205,6 +1213,17 @@ class Structure extends Layout {
*
* @type {Layout[]} */
this.fields = fields;

/** Control behavior of {@link Layout#decode|decode()} given short
* buffers.
*
* In some situations a structure many be extended with additional
* fields over time, with older installations providing only a
* prefix of the full structure. If this property is `true`
* decoding will accept those buffers and leave subsequent fields
* undefined, as long as the buffer ends at a field boundary.
* Defaults to `false`. */
this.decodePrefixes = !!decodePrefixes;
}

/** @override */
Expand Down Expand Up @@ -1239,6 +1258,10 @@ class Structure extends Layout {
dest[fd.property] = fd.decode(b, offset);
}
offset += fd.getSpan(b, offset);
if (this.decodePrefixes
&& (b.length === offset)) {
break;
}
}
return dest;
}
Expand Down Expand Up @@ -1920,9 +1943,12 @@ function fixBitwiseResult(v) {
* {@link UInt|UInt} (or {@link UIntBE|UIntBE}) that is no more than 4
* bytes wide.
*
* @param {bool} msb - `true` if the bit numbering starts at the most
* significant bit of the containing word; `false` (default) if it
* starts at the least significant bit of the containing word.
* @param {bool} [msb] - `true` if the bit numbering starts at the
* most significant bit of the containing word; `false` (default) if
* it starts at the least significant bit of the containing word. If
* the parameter at this position is a string and `property` is
* `undefined` the value of this argument will instead be used as the
* value of `property`.
*
* @param {string} [property] - initializer for {@link
* Layout#property|property}.
Expand All @@ -1936,6 +1962,11 @@ class BitStructure extends Layout {
|| (word instanceof UIntBE))) {
throw new TypeError('word must be a UInt or UIntBE layout');
}
if (('string' === typeof msb)
&& (undefined === property)) {
property = msb;
msb = undefined;
}
if (4 < word.span) {
throw new RangeError('word cannot exceed 32 bits');
}
Expand Down Expand Up @@ -2557,7 +2588,7 @@ exports.f64 = (property => new Double(property));
exports.f64be = (property => new DoubleBE(property));

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

/** Factory for {@link BitStructure|BitStructure} values. */
exports.bits = ((word, msb, property) => new BitStructure(word, msb, property));
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "buffer-layout",
"version": "1.0.0",
"version": "1.1.0",
"description": "Translation between JavaScript values and Buffers",
"keywords": [
"Buffer",
Expand All @@ -18,7 +18,7 @@
"author": "Peter A. Bigot <[email protected]>",
"main": "./lib/Layout.js",
"devDependencies": {
"coveralls": "~3.0.0",
"coveralls": "^3.0.0",
"eslint": "~4.13.1",
"eslint-config-google": "~0.9.1",
"eslint-plugin-pabigot": "~1.1.0",
Expand Down
Loading

0 comments on commit 253d8dd

Please sign in to comment.