4.0.2 • Published 3 years ago
mutable-buffer v4.0.2
mutable-buffer
A mutable buffer library for both node.js and browser
Install
$ npm install mutable-buffer
Usage
Basic usage
const {MutableBuffer} = require('mutable-buffer');
const buffer = new MutableBuffer(/* initialSize, blockSize */);
// use it like node Buffer
buffer.writeUInt8(8);
buffer.writeUInt16LE(0x1234);
buffer.write('hello');
buffer.write(otherBuffer);
// write a string to the buffer utf8 encoded and adds a null character (\0) at the end.
buffer.writeCString('hello');
// write a char
buffer.writeChar('a');
// get size of mutable buffer
buffer.size();
// get current capacity of mutable buffer
buffer.capacity();
// return a sliced Buffer instance
result = buffer.render();
// return a fresh Buffer instance
result = buffer.render(true);
// or return a sliced Buffer instance and clear buffer
result = buffer.flush();
// or return a fresh Buffer instance and clear buffer
result = buffer.flush(true);
// clear manual
buffer.clear();
Trim null characters
const {MutableBuffer} = require('mutable-buffer');
const buffer = new MutableBuffer(/* initialSize, blockSize */);
// trimLeft
buffer.write([0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00]);
buffer.trimLeft().flush(); // => [0x01, 0x02, 0x00, 0x00, 0x00]
// trimRight
buffer.write([0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00]);
buffer.trimRight().flush(); // => [0x00, 0x00, 0x01, 0x02]
// trim
buffer.write([0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00]);
buffer.trim().flush(); // => [0x01, 0x02]
Use in browser
mutable-buffer
introduced feross/buffer to support for the browser out of box in
v3.0
.
A front-end packaging tool like webpack
will recognize the browser
entry defined in pacakge.json
and use the
browser version of mutable-buffer
by default.
You can print MutableBuffer.target
to confirm that.
// in node
console.log(MutableBuffer.target); // => 'node'
// in browser
console.log(MutableBuffer.target); // => 'web'
License
MIT © taoyuan
4.0.1
3 years ago
4.0.2
3 years ago
3.0.0
4 years ago
3.0.0-0
4 years ago
2.2.3
4 years ago
2.2.5
4 years ago
2.2.4
4 years ago
2.2.2
4 years ago
2.2.1
4 years ago
2.2.0
4 years ago
2.1.1
6 years ago
2.1.0
6 years ago
2.0.3
8 years ago
2.0.2
8 years ago
2.0.1
8 years ago
2.0.0
8 years ago
1.0.2
9 years ago
1.0.1
9 years ago
0.0.0
9 years ago