Skip to content

Commit 88f4f43

Browse files
authored
Add default read offsets for Node Buffer compatibility (#99)
* Add default read offsets for Node Buffer compatibility This PR adds an optional offset to the `readXX` methods to create compatibility with the Node Buffer API https://nodejs.org/api/buffer.html#buffer_buf_readbigint64be_offset * Added tests * test udpate
1 parent bd6fea1 commit 88f4f43

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

BufferList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ BufferList.prototype._match = function (offset, search) {
373373
return this.slice(offset, offset + byteLength)[m](0, byteLength)
374374
}
375375
} else {
376-
BufferList.prototype[m] = function (offset) {
376+
BufferList.prototype[m] = function (offset = 0) {
377377
return this.slice(offset, offset + methods[m])[m](0)
378378
}
379379
}

test/test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ tape('test readUInt8 / readInt8', function (t) {
265265
const buf3 = Buffer.alloc(3)
266266
const bl = new BufferList()
267267

268+
buf1[0] = 0x1
268269
buf2[1] = 0x3
269270
buf2[2] = 0x4
270271
buf3[0] = 0x23
@@ -274,6 +275,7 @@ tape('test readUInt8 / readInt8', function (t) {
274275
bl.append(buf2)
275276
bl.append(buf3)
276277

278+
t.equal(bl.readUInt8(), 0x1)
277279
t.equal(bl.readUInt8(2), 0x3)
278280
t.equal(bl.readInt8(2), 0x3)
279281
t.equal(bl.readUInt8(3), 0x4)
@@ -292,6 +294,7 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
292294
const buf3 = Buffer.alloc(3)
293295
const bl = new BufferList()
294296

297+
buf1[0] = 0x1
295298
buf2[1] = 0x3
296299
buf2[2] = 0x4
297300
buf3[0] = 0x23
@@ -301,6 +304,8 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
301304
bl.append(buf2)
302305
bl.append(buf3)
303306

307+
t.equal(bl.readUInt16BE(), 0x0100)
308+
t.equal(bl.readUInt16LE(), 0x0001)
304309
t.equal(bl.readUInt16BE(2), 0x0304)
305310
t.equal(bl.readUInt16LE(2), 0x0403)
306311
t.equal(bl.readInt16BE(2), 0x0304)
@@ -323,6 +328,7 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
323328
const buf3 = Buffer.alloc(3)
324329
const bl = new BufferList()
325330

331+
buf1[0] = 0x1
326332
buf2[1] = 0x3
327333
buf2[2] = 0x4
328334
buf3[0] = 0x23
@@ -332,6 +338,8 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
332338
bl.append(buf2)
333339
bl.append(buf3)
334340

341+
t.equal(bl.readUInt32BE(), 0x01000304)
342+
t.equal(bl.readUInt32LE(), 0x04030001)
335343
t.equal(bl.readUInt32BE(2), 0x03042342)
336344
t.equal(bl.readUInt32LE(2), 0x42230403)
337345
t.equal(bl.readInt32BE(2), 0x03042342)
@@ -391,6 +399,7 @@ tape('test readFloatLE / readFloatBE', function (t) {
391399
const buf3 = Buffer.alloc(3)
392400
const bl = new BufferList()
393401

402+
buf1[0] = 0x01
394403
buf2[1] = 0x00
395404
buf2[2] = 0x00
396405
buf3[0] = 0x80
@@ -400,7 +409,11 @@ tape('test readFloatLE / readFloatBE', function (t) {
400409
bl.append(buf2)
401410
bl.append(buf3)
402411

403-
t.equal(bl.readFloatLE(2), 0x01)
412+
const canonical = Buffer.concat([buf1, buf2, buf3])
413+
t.equal(bl.readFloatLE(), canonical.readFloatLE())
414+
t.equal(bl.readFloatBE(), canonical.readFloatBE())
415+
t.equal(bl.readFloatLE(2), canonical.readFloatLE(2))
416+
t.equal(bl.readFloatBE(2), canonical.readFloatBE(2))
404417

405418
t.end()
406419
})
@@ -411,6 +424,7 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
411424
const buf3 = Buffer.alloc(10)
412425
const bl = new BufferList()
413426

427+
buf1[0] = 0x01
414428
buf2[1] = 0x55
415429
buf2[2] = 0x55
416430
buf3[0] = 0x55
@@ -424,7 +438,11 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
424438
bl.append(buf2)
425439
bl.append(buf3)
426440

427-
t.equal(bl.readDoubleLE(2), 0.3333333333333333)
441+
const canonical = Buffer.concat([buf1, buf2, buf3])
442+
t.equal(bl.readDoubleBE(), canonical.readDoubleBE())
443+
t.equal(bl.readDoubleLE(), canonical.readDoubleLE())
444+
t.equal(bl.readDoubleBE(2), canonical.readDoubleBE(2))
445+
t.equal(bl.readDoubleLE(2), canonical.readDoubleLE(2))
428446

429447
t.end()
430448
})

0 commit comments

Comments
 (0)