In Node.js, when we want to read continuous number or string from a Buffer instance, we have to maintain a position where we are reading at. To make it easy, here is a library to help to read Buffer instance easier.

View project in Github

buffer in node.js

Install

1
npm i ginkgoch-buffer-reader --save

Test

1
npm test

Example

Prepare for data

1
2
3
4
5
const buffer = Buffer.alloc(4);
buffer.writeInt8(8, 0);
buffer.writeInt16LE(16, 1);
buffer.writeUInt32LE(32, 3);
buffer.writeDoubleBE(54.8765, 7);

Without Ginkgoch Buffer Reader

1
2
3
4
let i1 = buffer.readInt8(0);
let i2 = buffer.readInt16LE(1);
let i3 = buffer.readUInt32LE(3);
let i4 = buffer.readDoubleBE(7);

It is terrible to remember the length of the value type and manually accumulate the position; it is very easy to make mistake and hard to find out why. Even though we could create UT for our logic, but why not to make it easy and handle the headache to us?

With Ginkgoch Buffer Reader
It automatically manages the read position. You don’t need to manually calculate the position and type length anymore.

1
2
3
4
5
6
const BufferReader = require('ginkgoch-buffer-reader');
let br = new BufferReader(buffer);
let i1 = br.nextInt8();
let i2 = br.nextInt16LE();
let i3 = br.nextUInt32LE();
let i4 = br.nextDoubleBE();

API List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
constructor(buffer: Buffer);
seek(offset: number, fromBeginning = true);
nextBuffer(length: number);
nextString(length: number, encoding = 'utf-8');
nextIn8();
nextUInt8();
nextUInt16LE();
nextUInt16BE();
nextInt16LE();
nextInt16BE();
nextUInt32LE();
nextUInt32BE();
nextInt32LE();
nextInt32BE();
nextFloatLE();
nextFloatBE();
nextDoubleLE();
nextDoubleBE();

Reference