@@ -28,9 +28,8 @@ Layout support is provided for these types of data:
28
28
29
29
## Installation
30
30
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 ` .
34
33
35
34
## Examples
36
35
@@ -58,9 +57,9 @@ The C definition:
58
57
The buffer-layout way:
59
58
60
59
const ds = lo.seq(lo.s16(), 4);
61
- const b = new Buffer(8);
60
+ const b = Buffer.alloc (8);
62
61
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);
64
63
assert.deepEqual(ds.decode(b), [1, -1, 3, -3]);
65
64
66
65
See [ Int] ( http://pabigot.github.io/buffer-layout/module-Layout-Int.html )
@@ -81,10 +80,10 @@ The buffer-layout way:
81
80
lo.seq(lo.u8(), 3), // alignment padding
82
81
lo.u32('u32')]);
83
82
assert.equal(ds.offsetOf('u32'), 4);
84
- const b = new Buffer(8);
83
+ const b = Buffer.alloc (8);
85
84
b.fill(0xbd);
86
85
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);
88
87
assert.deepEqual(ds.decode(b), {v: 1, u32: 0x12345678});
89
88
90
89
Note that the C language requires padding which must be explicitly added
@@ -108,10 +107,10 @@ The buffer-layout way:
108
107
const ds = lo.struct([lo.u8('v'),
109
108
lo.u32('u32')]);
110
109
assert.equal(ds.offsetOf('u32'), 1);
111
- const b = new Buffer(5);
110
+ const b = Buffer.alloc (5);
112
111
b.fill(0xbd);
113
112
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);
115
114
assert.deepEqual(ds.decode(b), {v: 1, u32: 0x12345678});
116
115
117
116
### A tagged union of 4-byte values
@@ -136,15 +135,15 @@ The buffer-layout way:
136
135
const u32 = un.addVariant('w'.charCodeAt(0), lo.u32(), 'u32');
137
136
const s16 = un.addVariant('h'.charCodeAt(0), lo.seq(lo.s16(), 2), 's16');
138
137
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')),
141
140
{u32: 0x12345678});
142
- assert.deepEqual(un.decode(Buffer('660000bd41', 'hex')),
141
+ assert.deepEqual(un.decode(Buffer.from ('660000bd41', 'hex')),
143
142
{f32: 23.625});
144
- assert.deepEqual(un.decode(Buffer('a5a5a5a5a5', 'hex')),
143
+ assert.deepEqual(un.decode(Buffer.from ('a5a5a5a5a5', 'hex')),
145
144
{t: 0xa5, u8: [0xa5, 0xa5, 0xa5, 0xa5]});
146
145
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);
148
147
149
148
See [ Union] ( http://pabigot.github.io/buffer-layout/module-Layout-Union.html ) .
150
149
@@ -172,20 +171,20 @@ representing the union and the variants:
172
171
lo.bindConstructorLayout(Vf32,
173
172
Union.layout_.addVariant('f'.charCodeAt(0), lo.f32(), 'f32'));
174
173
175
- let v = Union.decode(Buffer('7778563412', 'hex'));
174
+ let v = Union.decode(Buffer.from ('7778563412', 'hex'));
176
175
assert(v instanceof Vu32);
177
176
assert(v instanceof Union);
178
177
assert.equal(v.u32, 0x12345678);
179
178
180
- v = Union.decode(Buffer('a5a5a5a5a5', 'hex'));
179
+ v = Union.decode(Buffer.from ('a5a5a5a5a5', 'hex'));
181
180
assert(v instanceof Union);
182
181
assert.equal(v.t, 0xa5);
183
182
assert.deepEqual(v.u8, [0xa5, 0xa5, 0xa5, 0xa5]);
184
183
185
- const b = new Buffer(Union.layout_.span);
184
+ const b = Buffer.alloc (Union.layout_.span);
186
185
v = new Vf32(23.625);
187
186
v.encode(b);
188
- assert.equal(Buffer('660000bd41', 'hex').compare(b), 0);
187
+ assert.equal(Buffer.from ('660000bd41', 'hex').compare(b), 0);
189
188
190
189
See
191
190
[ Layout.makeDestinationObject()] ( http://pabigot.github.io/buffer-layout/module-Layout-Layout.html#makeDestinationObject )
@@ -206,14 +205,14 @@ The C definition:
206
205
The buffer-layout way:
207
206
208
207
const ds = lo.bits(lo.u32());
209
- const b = new Buffer(4);
208
+ const b = Buffer.alloc (4);
210
209
ds.addField(3, 'b00l03');
211
210
ds.addBoolean('flg03');
212
211
ds.addField(24, 'b04l18');
213
212
ds.addField(4, 'b1Cl04');
214
213
b.fill(0xff);
215
214
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);
217
216
assert.deepEqual(ds.decode(b),
218
217
{b00l03: 3, flg03: true, b04l18: 24, b1Cl04: 4});
219
218
@@ -228,7 +227,7 @@ The C definition:
228
227
The buffer-layout way:
229
228
230
229
const ds = lo.nu64be();
231
- const b = Buffer('0102030405060708', 'hex');
230
+ const b = Buffer.from ('0102030405060708', 'hex');
232
231
const v = 72623859790382856;
233
232
const nv = v - 6;
234
233
assert.equal(v, nv);
@@ -249,11 +248,11 @@ The C definition:
249
248
The buffer-layout way:
250
249
251
250
const ds = lo.cstr();
252
- const b = new Buffer(8);
251
+ const b = Buffer.alloc (8);
253
252
assert.equal(ds.encode('hi!', b), 3 + 1);
254
253
const slen = ds.getSpan(b);
255
254
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);
257
256
assert.equal(ds.decode(b), 'hi!');
258
257
259
258
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)
263
262
The buffer-layout way:
264
263
265
264
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);
268
267
269
268
See [ Blob] ( http://pabigot.github.io/buffer-layout/module-Layout-Blob.html ) .
270
269
@@ -276,13 +275,14 @@ The buffer-layout way:
276
275
const n = lo.u8('n');
277
276
const vla = lo.seq(pr, lo.offset(n, -1), 'a');
278
277
const st = lo.struct([n, vla], 'st');
279
- const b = new Buffer(32);
278
+ const b = Buffer.alloc (32);
280
279
const arr = [['k1', 'v1'], ['k2', 'v2'], ['k3', 'etc']];
281
280
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)));
283
283
const span = st.getSpan(b);
284
284
assert.equal(span, 20);
285
- assert.equal(Buffer('036b31007631006b32007632006b330065746300', 'hex')
285
+ assert.equal(Buffer.from ('036b31007631006b32007632006b330065746300', 'hex')
286
286
.compare(b.slice(0, span)), 0);
287
287
assert.deepEqual(st.decode(b), {n: 3, a: arr});
288
288
@@ -307,11 +307,11 @@ The buffer-layout way:
307
307
lo.greedy(lo.u16().span),
308
308
'data')],
309
309
'ds');
310
- const b = Buffer('21010002030405', 'hex');
310
+ const b = Buffer.from ('21010002030405', 'hex');
311
311
assert.deepEqual(st.decode(b), {prop: 33, data: [0x0001, 0x0302, 0x0504]});
312
312
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);
315
315
316
316
### Tagged values, or variable-length unions
317
317
@@ -333,7 +333,7 @@ recognition of `true` and `false` values for `b` as distinct variants:
333
333
const cstr = un.addVariant('s'.charCodeAt(0), lo.cstr(), 'str');
334
334
const tr = un.addVariant('T'.charCodeAt(0), lo.const(true), 'b');
335
335
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);
337
337
un.configGetSourceVariant(function(src) {
338
338
if (src.hasOwnProperty('b')) {
339
339
return src.b ? tr : fa;
@@ -347,36 +347,34 @@ decoding each of the alternatives:
347
347
b.fill(0xff);
348
348
assert.equal(un.encode({u8: 1}, b), 1 + 1);
349
349
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);
351
351
assert.equal(un.decode(b).u8, 1);
352
352
353
353
b.fill(0xff);
354
354
assert.equal(un.encode({s16: -32000}, b), 1 + 2);
355
355
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);
357
357
assert.equal(un.decode(b).s16, -32000);
358
358
359
359
b.fill(0xff);
360
360
const v48 = Math.pow(2, 47) - 1;
361
361
assert.equal(un.encode({s48: v48}, b), 1 + 6);
362
362
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);
364
364
assert.equal(un.decode(b).s48, v48);
365
365
366
366
b.fill(0xff);
367
367
assert.equal(un.encode({b: true}, b), 1);
368
368
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);
370
370
assert.strictEqual(un.decode(b).b, true);
371
371
372
372
b.fill(0xff);
373
373
assert.equal(un.encode({b: false}, b), 1);
374
374
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);
376
376
assert.strictEqual(un.decode(b).b, false);
377
377
378
378
** 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.
0 commit comments