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

Commit ecad2fc

Browse files
committed
passim: eslint new-cap, update minimum Node version to 4.5
We need the safe Buffer API which became availble in 4.5.0.
1 parent 9663c16 commit ecad2fc

File tree

7 files changed

+313
-436
lines changed

7 files changed

+313
-436
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module.exports = {
99
],
1010
rules: {
1111
'guard-for-in': 'off',
12-
'new-cap': 'off',
1312
'prefer-rest-params': 'off',
1413
'prefer-spread': 'off',
1514
__temporary: 'off',

README.md

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ Layout support is provided for these types of data:
2828

2929
## Installation
3030

31-
Development and testing is done using Node.js, supporting versions 4 and
32-
later (earlier versions may work). Install with `npm install
33-
buffer-layout`.
31+
Development and testing is done using Node.js, supporting versions 4.5
32+
and later. Install with `npm install buffer-layout`.
3433

3534
## Examples
3635

@@ -58,9 +57,9 @@ The C definition:
5857
The buffer-layout way:
5958

6059
const ds = lo.seq(lo.s16(), 4);
61-
const b = new Buffer(8);
60+
const b = Buffer.alloc(8);
6261
assert.equal(ds.encode([1, -1, 3, -3], b), 4 * 2);
63-
assert.equal(Buffer('0100ffff0300fdff', 'hex').compare(b), 0);
62+
assert.equal(Buffer.from('0100ffff0300fdff', 'hex').compare(b), 0);
6463
assert.deepEqual(ds.decode(b), [1, -1, 3, -3]);
6564

6665
See [Int](http://pabigot.github.io/buffer-layout/module-Layout-Int.html)
@@ -81,10 +80,10 @@ The buffer-layout way:
8180
lo.seq(lo.u8(), 3), // alignment padding
8281
lo.u32('u32')]);
8382
assert.equal(ds.offsetOf('u32'), 4);
84-
const b = new Buffer(8);
83+
const b = Buffer.alloc(8);
8584
b.fill(0xbd);
8685
assert.equal(ds.encode({v: 1, u32: 0x12345678}, b), 1 + 3 + 4);
87-
assert.equal(Buffer('01bdbdbd78563412', 'hex').compare(b), 0);
86+
assert.equal(Buffer.from('01bdbdbd78563412', 'hex').compare(b), 0);
8887
assert.deepEqual(ds.decode(b), {v: 1, u32: 0x12345678});
8988

9089
Note that the C language requires padding which must be explicitly added
@@ -108,10 +107,10 @@ The buffer-layout way:
108107
const ds = lo.struct([lo.u8('v'),
109108
lo.u32('u32')]);
110109
assert.equal(ds.offsetOf('u32'), 1);
111-
const b = new Buffer(5);
110+
const b = Buffer.alloc(5);
112111
b.fill(0xbd);
113112
assert.equal(ds.encode({v: 1, u32: 0x12345678}, b), 1 + 4);
114-
assert.equal(Buffer('0178563412', 'hex').compare(b), 0);
113+
assert.equal(Buffer.from('0178563412', 'hex').compare(b), 0);
115114
assert.deepEqual(ds.decode(b), {v: 1, u32: 0x12345678});
116115

117116
### A tagged union of 4-byte values
@@ -136,15 +135,15 @@ The buffer-layout way:
136135
const u32 = un.addVariant('w'.charCodeAt(0), lo.u32(), 'u32');
137136
const s16 = un.addVariant('h'.charCodeAt(0), lo.seq(lo.s16(), 2), 's16');
138137
const f32 = un.addVariant('f'.charCodeAt(0), lo.f32(), 'f32');
139-
const b = new Buffer(un.span);
140-
assert.deepEqual(un.decode(Buffer('7778563412', 'hex')),
138+
const b = Buffer.alloc(un.span);
139+
assert.deepEqual(un.decode(Buffer.from('7778563412', 'hex')),
141140
{u32: 0x12345678});
142-
assert.deepEqual(un.decode(Buffer('660000bd41', 'hex')),
141+
assert.deepEqual(un.decode(Buffer.from('660000bd41', 'hex')),
143142
{f32: 23.625});
144-
assert.deepEqual(un.decode(Buffer('a5a5a5a5a5', 'hex')),
143+
assert.deepEqual(un.decode(Buffer.from('a5a5a5a5a5', 'hex')),
145144
{t: 0xa5, u8: [0xa5, 0xa5, 0xa5, 0xa5]});
146145
assert.equal(s16.encode({s16: [123, -123]}, b), 1 + 2 * 2);
147-
assert.equal(Buffer('687b0085ff', 'hex').compare(b), 0);
146+
assert.equal(Buffer.from('687b0085ff', 'hex').compare(b), 0);
148147

149148
See [Union](http://pabigot.github.io/buffer-layout/module-Layout-Union.html).
150149

@@ -172,20 +171,20 @@ representing the union and the variants:
172171
lo.bindConstructorLayout(Vf32,
173172
Union.layout_.addVariant('f'.charCodeAt(0), lo.f32(), 'f32'));
174173

175-
let v = Union.decode(Buffer('7778563412', 'hex'));
174+
let v = Union.decode(Buffer.from('7778563412', 'hex'));
176175
assert(v instanceof Vu32);
177176
assert(v instanceof Union);
178177
assert.equal(v.u32, 0x12345678);
179178

180-
v = Union.decode(Buffer('a5a5a5a5a5', 'hex'));
179+
v = Union.decode(Buffer.from('a5a5a5a5a5', 'hex'));
181180
assert(v instanceof Union);
182181
assert.equal(v.t, 0xa5);
183182
assert.deepEqual(v.u8, [0xa5, 0xa5, 0xa5, 0xa5]);
184183

185-
const b = new Buffer(Union.layout_.span);
184+
const b = Buffer.alloc(Union.layout_.span);
186185
v = new Vf32(23.625);
187186
v.encode(b);
188-
assert.equal(Buffer('660000bd41', 'hex').compare(b), 0);
187+
assert.equal(Buffer.from('660000bd41', 'hex').compare(b), 0);
189188

190189
See
191190
[Layout.makeDestinationObject()](http://pabigot.github.io/buffer-layout/module-Layout-Layout.html#makeDestinationObject)
@@ -206,14 +205,14 @@ The C definition:
206205
The buffer-layout way:
207206

208207
const ds = lo.bits(lo.u32());
209-
const b = new Buffer(4);
208+
const b = Buffer.alloc(4);
210209
ds.addField(3, 'b00l03');
211210
ds.addBoolean('flg03');
212211
ds.addField(24, 'b04l18');
213212
ds.addField(4, 'b1Cl04');
214213
b.fill(0xff);
215214
assert.equal(ds.encode({b00l03: 3, b04l18: 24, b1Cl04: 4}, b), 4);
216-
assert.equal(Buffer('8b010040', 'hex').compare(b), 0);
215+
assert.equal(Buffer.from('8b010040', 'hex').compare(b), 0);
217216
assert.deepEqual(ds.decode(b),
218217
{b00l03: 3, flg03: true, b04l18: 24, b1Cl04: 4});
219218

@@ -228,7 +227,7 @@ The C definition:
228227
The buffer-layout way:
229228

230229
const ds = lo.nu64be();
231-
const b = Buffer('0102030405060708', 'hex');
230+
const b = Buffer.from('0102030405060708', 'hex');
232231
const v = 72623859790382856;
233232
const nv = v - 6;
234233
assert.equal(v, nv);
@@ -249,11 +248,11 @@ The C definition:
249248
The buffer-layout way:
250249

251250
const ds = lo.cstr();
252-
const b = new Buffer(8);
251+
const b = Buffer.alloc(8);
253252
assert.equal(ds.encode('hi!', b), 3 + 1);
254253
const slen = ds.getSpan(b);
255254
assert.equal(slen, 4);
256-
assert.equal(Buffer('68692100', 'hex').compare(b.slice(0, slen)), 0);
255+
assert.equal(Buffer.from('68692100', 'hex').compare(b.slice(0, slen)), 0);
257256
assert.equal(ds.decode(b), 'hi!');
258257

259258
See [CString](http://pabigot.github.io/buffer-layout/module-Layout-CString.html).
@@ -263,8 +262,8 @@ See [CString](http://pabigot.github.io/buffer-layout/module-Layout-CString.html)
263262
The buffer-layout way:
264263

265264
const ds = lo.blob(4);
266-
const b = Buffer('0102030405060708', 'hex');
267-
assert.equal(Buffer('03040506', 'hex').compare(ds.decode(b, 2)), 0);
265+
const b = Buffer.from('0102030405060708', 'hex');
266+
assert.equal(Buffer.from('03040506', 'hex').compare(ds.decode(b, 2)), 0);
268267

269268
See [Blob](http://pabigot.github.io/buffer-layout/module-Layout-Blob.html).
270269

@@ -276,13 +275,14 @@ The buffer-layout way:
276275
const n = lo.u8('n');
277276
const vla = lo.seq(pr, lo.offset(n, -1), 'a');
278277
const st = lo.struct([n, vla], 'st');
279-
const b = new Buffer(32);
278+
const b = Buffer.alloc(32);
280279
const arr = [['k1', 'v1'], ['k2', 'v2'], ['k3', 'etc']];
281280
b.fill(0);
282-
assert.equal(st.encode({a: arr}, b), 1 + (2 * ((2 + 1) + (2 + 1)) + (2 + 1) + (3 + 1)));
281+
assert.equal(st.encode({a: arr}, b),
282+
1 + (2 * ((2 + 1) + (2 + 1)) + (2 + 1) + (3 + 1)));
283283
const span = st.getSpan(b);
284284
assert.equal(span, 20);
285-
assert.equal(Buffer('036b31007631006b32007632006b330065746300', 'hex')
285+
assert.equal(Buffer.from('036b31007631006b32007632006b330065746300', 'hex')
286286
.compare(b.slice(0, span)), 0);
287287
assert.deepEqual(st.decode(b), {n: 3, a: arr});
288288

@@ -307,11 +307,11 @@ The buffer-layout way:
307307
lo.greedy(lo.u16().span),
308308
'data')],
309309
'ds');
310-
const b = Buffer('21010002030405', 'hex');
310+
const b = Buffer.from('21010002030405', 'hex');
311311
assert.deepEqual(st.decode(b), {prop: 33, data: [0x0001, 0x0302, 0x0504]});
312312
b.fill(0xFF);
313-
assert.equal(st.encode({prop: 9, data: [5,6]}, b), 1 + 2 * 2);
314-
assert.equal(Buffer('0905000600FFFF', 'hex').compare(b), 0);
313+
assert.equal(st.encode({prop: 9, data: [5, 6]}, b), 1 + 2 * 2);
314+
assert.equal(Buffer.from('0905000600FFFF', 'hex').compare(b), 0);
315315

316316
### Tagged values, or variable-length unions
317317

@@ -333,7 +333,7 @@ recognition of `true` and `false` values for `b` as distinct variants:
333333
const cstr = un.addVariant('s'.charCodeAt(0), lo.cstr(), 'str');
334334
const tr = un.addVariant('T'.charCodeAt(0), lo.const(true), 'b');
335335
const fa = un.addVariant('F'.charCodeAt(0), lo.const(false), 'b');
336-
const b = new Buffer(1 + 6);
336+
const b = Buffer.alloc(1 + 6);
337337
un.configGetSourceVariant(function(src) {
338338
if (src.hasOwnProperty('b')) {
339339
return src.b ? tr : fa;
@@ -347,36 +347,34 @@ decoding each of the alternatives:
347347
b.fill(0xff);
348348
assert.equal(un.encode({u8: 1}, b), 1 + 1);
349349
assert.equal(un.getSpan(b), 2);
350-
assert.equal(Buffer('4201ffffffffff', 'hex').compare(b), 0);
350+
assert.equal(Buffer.from('4201ffffffffff', 'hex').compare(b), 0);
351351
assert.equal(un.decode(b).u8, 1);
352352

353353
b.fill(0xff);
354354
assert.equal(un.encode({s16: -32000}, b), 1 + 2);
355355
assert.equal(un.getSpan(b), 3);
356-
assert.equal(Buffer('680083ffffffff', 'hex').compare(b), 0);
356+
assert.equal(Buffer.from('680083ffffffff', 'hex').compare(b), 0);
357357
assert.equal(un.decode(b).s16, -32000);
358358

359359
b.fill(0xff);
360360
const v48 = Math.pow(2, 47) - 1;
361361
assert.equal(un.encode({s48: v48}, b), 1 + 6);
362362
assert.equal(un.getSpan(b), 7);
363-
assert.equal(Buffer('51ffffffffff7f', 'hex').compare(b), 0);
363+
assert.equal(Buffer.from('51ffffffffff7f', 'hex').compare(b), 0);
364364
assert.equal(un.decode(b).s48, v48);
365365

366366
b.fill(0xff);
367367
assert.equal(un.encode({b: true}, b), 1);
368368
assert.equal(un.getSpan(b), 1);
369-
assert.equal(Buffer('54ffffffffffff', 'hex').compare(b), 0);
369+
assert.equal(Buffer.from('54ffffffffffff', 'hex').compare(b), 0);
370370
assert.strictEqual(un.decode(b).b, true);
371371

372372
b.fill(0xff);
373373
assert.equal(un.encode({b: false}, b), 1);
374374
assert.equal(un.getSpan(b), 1);
375-
assert.equal(Buffer('46ffffffffffff', 'hex').compare(b), 0);
375+
assert.equal(Buffer.from('46ffffffffffff', 'hex').compare(b), 0);
376376
assert.strictEqual(un.decode(b).b, false);
377377

378378
**NOTE** This code tickles a long-standing [bug in
379-
Buffer.writeInt{L,B}E](https://github.com/nodejs/node/pull/3994). `buffer-layout`
380-
provides a [module that patches
381-
`Buffer`](http://pabigot.github.io/buffer-layout/module-patchIssue3992.html)
382-
to fix the bug if it detects that the running Node has the error.
379+
Buffer.writeInt{L,B}E](https://github.com/nodejs/node/pull/3994); if you
380+
are using Node prior to 4.2.4 or 5.2.0 you should update.

lib/Layout.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@
133133

134134
const assert = require('assert');
135135

136-
require('./patchIssue3992');
137-
138136
/**
139137
* Base class for layout objects.
140138
*
@@ -328,58 +326,59 @@ function nameWithProperty(name, lo) {
328326
exports.nameWithProperty = nameWithProperty;
329327

330328
/**
331-
* Augment a constructor so that instances can be encoded/decoded
332-
* using a given layout.
329+
* Augment a class so that instances can be encoded/decoded using a
330+
* given layout.
333331
*
334-
* Calling this function couples `ctor` with `layout` in several ways:
332+
* Calling this function couples `Class` with `layout` in several ways:
335333
*
336-
* * `ctor.layout_` becomes a static member property equal to `layout`;
334+
* * `Class.layout_` becomes a static member property equal to `layout`;
337335
* * `layout.boundConstructor_` becomes a static member property equal
338-
* to `ctor`;
336+
* to `Class`;
339337
* * The {@link Layout#makeDestinationObject|makeDestinationObject()}
340338
* property of `layout` is set to a function that returns a `new
341-
* ctor()`;
342-
* * `ctor.decode(b, offset)` becomes a static member function that
339+
* Class()`;
340+
* * `Class.decode(b, offset)` becomes a static member function that
343341
* delegates to {@link Layout#decode|layout.decode}. The
344342
* synthesized function may be captured and extended.
345-
* * `ctor.prototype.encode(b, offset)` provides an instance member
343+
* * `Class.prototype.encode(b, offset)` provides an instance member
346344
* function that delegates to {@link Layout#encode|layout.encode}
347345
* with `src` set to `this`. The synthesized function may be
348346
* captured and extended, but when the extension is invoked `this`
349347
* must be explicitly bound to the instance.
350348
*
351-
* @param {constructor} ctor - the constructor for a JavaScript class.
349+
* @param {class} Class - a JavaScript class with a nullary
350+
* constructor.
352351
*
353352
* @param {Layout} layout - the {@link Layout|Layout} instance used to
354-
* encode instances of `ctor`.
353+
* encode instances of `Class`.
355354
*/
356-
exports.bindConstructorLayout = function(ctor, layout) {
357-
if ('function' !== typeof ctor) {
358-
throw new TypeError('ctor must be constructor');
355+
exports.bindConstructorLayout = function(Class, layout) {
356+
if ('function' !== typeof Class) {
357+
throw new TypeError('Class must be constructor');
359358
}
360-
if (ctor.hasOwnProperty('layout_')) {
361-
throw new Error('ctor is already bound to a layout');
359+
if (Class.hasOwnProperty('layout_')) {
360+
throw new Error('Class is already bound to a layout');
362361
}
363362
if (!(layout && (layout instanceof Layout))) {
364363
throw new TypeError('layout must be a Layout');
365364
}
366365
if (layout.hasOwnProperty('boundConstructor_')) {
367366
throw new Error('layout is already bound to a constructor');
368367
}
369-
ctor.layout_ = layout;
370-
layout.boundConstructor_ = ctor;
368+
Class.layout_ = layout;
369+
layout.boundConstructor_ = Class;
371370
layout.makeDestinationObject = function() {
372-
return new ctor();
371+
return new Class();
373372
};
374-
Object.defineProperty(ctor.prototype, 'encode', {
373+
Object.defineProperty(Class.prototype, 'encode', {
375374
configurable: false,
376375
enumerable: false,
377376
value: function(b, offset) {
378377
return layout.encode(this, b, offset);
379378
},
380379
writable: true,
381380
});
382-
Object.defineProperty(ctor, 'decode', {
381+
Object.defineProperty(Class, 'decode', {
383382
configurable: false,
384383
enumerable: false,
385384
value: function(b, offset) {

0 commit comments

Comments
 (0)