Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 253d8dd

Browse files
committed
merge for 1.1.0 release
2 parents ed1a85d + 6403ce9 commit 253d8dd

File tree

5 files changed

+584
-484
lines changed

5 files changed

+584
-484
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## [1.1.0] - 2018-01-06
4+
5+
* **API** Add a third parameter to Structure specifying it should decode
6+
short buffers as prefixes, resolving [issue #19][issue#19].
7+
* **API** Interpret string argument to BitStructure `msb` parameter as
8+
a `property` parameter, resolving [issue #17][issue#17].
9+
310
## [1.0.0] - 2017-12-17
411

512
* Minimum Node version increased to 4.5 to support dependency
@@ -115,6 +122,7 @@
115122

116123
* Initial release.
117124

125+
[1.1.0]: https://github.com/pabigot/buffer-layout/compare/v1.0.0...v1.1.0
118126
[1.0.0]: https://github.com/pabigot/buffer-layout/compare/v0.13.0...v1.0.0
119127
[0.13.0]: https://github.com/pabigot/buffer-layout/compare/v0.12.0...v0.13.0
120128
[0.12.1]: https://github.com/pabigot/buffer-layout/compare/v0.12.0...v0.12.1
@@ -169,6 +177,8 @@
169177
[issue#13]: https://github.com/pabigot/buffer-layout/issues/13
170178
[issue#14]: https://github.com/pabigot/buffer-layout/issues/14
171179
[issue#15]: https://github.com/pabigot/buffer-layout/issues/15
180+
[issue#17]: https://github.com/pabigot/buffer-layout/issues/17
181+
[issue#19]: https://github.com/pabigot/buffer-layout/issues/19
172182
[ci:travis]: https://travis-ci.org/pabigot/buffer-layout
173183
[ci:coveralls]: https://coveralls.io/github/pabigot/buffer-layout
174184
[node:issue#3992]: https://github.com/nodejs/node/issues/3992

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Peter A. Bigot
3+
Copyright (c) 2015-2018 Peter A. Bigot
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

lib/Layout.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* The MIT License (MIT)
22
*
3-
* Copyright 2015-2017 Peter A. Bigot
3+
* Copyright 2015-2018 Peter A. Bigot
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -1166,18 +1166,26 @@ class Sequence extends Layout {
11661166
* @param {string} [property] - initializer for {@link
11671167
* Layout#property|property}.
11681168
*
1169+
* @param {Boolean} [decodePrefixes] - initializer for {@link
1170+
* Structure#decodePrefixes|property}.
1171+
*
11691172
* @throws {Error} - if `fields` contains an unnamed variable-length
11701173
* layout.
11711174
*
11721175
* @constructor
11731176
* @augments {Layout}
11741177
*/
11751178
class Structure extends Layout {
1176-
constructor(fields, property) {
1179+
constructor(fields, property, decodePrefixes) {
11771180
if (!(Array.isArray(fields)
11781181
&& fields.reduce((acc, v) => acc && (v instanceof Layout), true))) {
11791182
throw new TypeError('fields must be array of Layout instances');
11801183
}
1184+
if (('boolean' === typeof property)
1185+
&& (undefined === decodePrefixes)) {
1186+
decodePrefixes = property;
1187+
property = undefined;
1188+
}
11811189

11821190
/* Verify absence of unnamed variable-length fields. */
11831191
for (const fd of fields) {
@@ -1205,6 +1213,17 @@ class Structure extends Layout {
12051213
*
12061214
* @type {Layout[]} */
12071215
this.fields = fields;
1216+
1217+
/** Control behavior of {@link Layout#decode|decode()} given short
1218+
* buffers.
1219+
*
1220+
* In some situations a structure many be extended with additional
1221+
* fields over time, with older installations providing only a
1222+
* prefix of the full structure. If this property is `true`
1223+
* decoding will accept those buffers and leave subsequent fields
1224+
* undefined, as long as the buffer ends at a field boundary.
1225+
* Defaults to `false`. */
1226+
this.decodePrefixes = !!decodePrefixes;
12081227
}
12091228

12101229
/** @override */
@@ -1239,6 +1258,10 @@ class Structure extends Layout {
12391258
dest[fd.property] = fd.decode(b, offset);
12401259
}
12411260
offset += fd.getSpan(b, offset);
1261+
if (this.decodePrefixes
1262+
&& (b.length === offset)) {
1263+
break;
1264+
}
12421265
}
12431266
return dest;
12441267
}
@@ -1920,9 +1943,12 @@ function fixBitwiseResult(v) {
19201943
* {@link UInt|UInt} (or {@link UIntBE|UIntBE}) that is no more than 4
19211944
* bytes wide.
19221945
*
1923-
* @param {bool} msb - `true` if the bit numbering starts at the most
1924-
* significant bit of the containing word; `false` (default) if it
1925-
* starts at the least significant bit of the containing word.
1946+
* @param {bool} [msb] - `true` if the bit numbering starts at the
1947+
* most significant bit of the containing word; `false` (default) if
1948+
* it starts at the least significant bit of the containing word. If
1949+
* the parameter at this position is a string and `property` is
1950+
* `undefined` the value of this argument will instead be used as the
1951+
* value of `property`.
19261952
*
19271953
* @param {string} [property] - initializer for {@link
19281954
* Layout#property|property}.
@@ -1936,6 +1962,11 @@ class BitStructure extends Layout {
19361962
|| (word instanceof UIntBE))) {
19371963
throw new TypeError('word must be a UInt or UIntBE layout');
19381964
}
1965+
if (('string' === typeof msb)
1966+
&& (undefined === property)) {
1967+
property = msb;
1968+
msb = undefined;
1969+
}
19391970
if (4 < word.span) {
19401971
throw new RangeError('word cannot exceed 32 bits');
19411972
}
@@ -2557,7 +2588,7 @@ exports.f64 = (property => new Double(property));
25572588
exports.f64be = (property => new DoubleBE(property));
25582589

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

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "buffer-layout",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Translation between JavaScript values and Buffers",
55
"keywords": [
66
"Buffer",
@@ -18,7 +18,7 @@
1818
"author": "Peter A. Bigot <[email protected]>",
1919
"main": "./lib/Layout.js",
2020
"devDependencies": {
21-
"coveralls": "~3.0.0",
21+
"coveralls": "^3.0.0",
2222
"eslint": "~4.13.1",
2323
"eslint-config-google": "~0.9.1",
2424
"eslint-plugin-pabigot": "~1.1.0",

0 commit comments

Comments
 (0)