Provide backward support for Node.js Buffer API before v12
npm install xprezzo-buffer**Use the new Node.js Buffer APIs (Buffer.from, Buffer.alloc,Buffer.allocUnsafe, Buffer.allocUnsafeSlow) in all versions of Node.js.**
Uses the built-in implementation when available. (After Node.js v12)
Modified from safe-buffer
It also expose xprezzo-mixin
Problems faced:
* Too many requires which creates problem when project grow
* The dependencies update are slow
* Test cases of difficult to design
How Xprezzo try to tackle those problems:
* Useful internal libraries/packages are exposed
* Merge small libraries into a larger one.
* Provide easy to use test framework
```
npm install xprezzo-buffer
The goal of this package is to provide a safe replacement for the node.js Buffer.
It's a drop-in replacement for Buffer. You can use it by adding one require line to
the top of your node.js modules:
`js
var Buffer = require('xprezzo-buffer').Buffer
// Existing buffer code will continue to work without issues:
new Buffer('hey', 'utf8')
new Buffer([1, 2, 3], 'utf8')
new Buffer(obj)
new Buffer(16) // create an uninitialized buffer (potentially unsafe)
// But you can use these new explicit APIs to make clear what you want:
Buffer.from('hey', 'utf8') // convert from many types to a Buffer
Buffer.alloc(16) // create a zero-filled buffer (safe)
Buffer.allocUnsafe(16) // create an uninitialized buffer (potentially unsafe)
`
* array {Array}
Allocates a new Buffer using an array of octets.
`js`
const buf = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);
// creates a new Buffer containing ASCII bytes
// ['b','u','f','f','e','r']
A TypeError will be thrown if array is not an Array.
* arrayBuffer {ArrayBuffer} The .buffer property of a TypedArray ornew ArrayBuffer()
a byteOffset
* {Number} Default: 0length
* {Number} Default: arrayBuffer.length - byteOffset
When passed a reference to the .buffer property of a TypedArray instance,Buffer
the newly created will share the same allocated memory as the
TypedArray.
`js
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
const buf = Buffer.from(arr.buffer); // shares the memory with arr;
console.log(buf);
// Prints:
// changing the TypedArray changes the Buffer also
arr[1] = 6000;
console.log(buf);
// Prints:
`
The optional byteOffset and length arguments specify a memory range withinarrayBuffer
the that will be shared by the Buffer.
`js`
const ab = new ArrayBuffer(10);
const buf = Buffer.from(ab, 0, 2);
console.log(buf.length);
// Prints: 2
A TypeError will be thrown if arrayBuffer is not an ArrayBuffer.
* buffer {Buffer}
Copies the passed buffer data onto a new Buffer instance.
`js
const buf1 = Buffer.from('buffer');
const buf2 = Buffer.from(buf1);
buf1[0] = 0x61;
console.log(buf1.toString());
// 'auffer'
console.log(buf2.toString());
// 'buffer' (copy is not changed)
`
A TypeError will be thrown if buffer is not a Buffer.
* str {String} String to encode.encoding
* {String} Encoding to use, Default: 'utf8'
Creates a new Buffer containing the given JavaScript string str. Ifencoding
provided, the parameter identifies the character encoding.encoding
If not provided, defaults to 'utf8'.
`js
const buf1 = Buffer.from('this is a tést');
console.log(buf1.toString());
// prints: this is a tést
console.log(buf1.toString('ascii'));
// prints: this is a tC)st
const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
console.log(buf2.toString());
// prints: this is a tést
`
A TypeError will be thrown if str is not a string.
* size {Number}fill
* {Value} Default: undefinedencoding
* {String} Default: utf8
Allocates a new Buffer of size bytes. If fill is undefined, theBuffer will be zero-filled.
`js`
const buf = Buffer.alloc(5);
console.log(buf);
//
The size must be less than or equal to the value ofrequire('buffer').kMaxLength (on 64-bit architectures, kMaxLength is(2^31)-1). Otherwise, a [RangeError][] is thrown. A zero-length Buffer willsize
be created if a less than or equal to 0 is specified.
If fill is specified, the allocated Buffer will be initialized by callingbuf.fill(fill). See [buf.fill()][] for more information.
`js`
const buf = Buffer.alloc(5, 'a');
console.log(buf);
//
If both fill and encoding are specified, the allocated Buffer will bebuf.fill(fill, encoding)
initialized by calling . For example:
`js`
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
console.log(buf);
//
Calling Buffer.alloc(size) can be significantly slower than the alternativeBuffer.allocUnsafe(size) but ensures that the newly created Buffer instance
contents will never contain sensitive data.
A TypeError will be thrown if size is not a number.
* size {Number}
Allocates a new non-zero-filled Buffer of size bytes. The size mustrequire('buffer').kMaxLength
be less than or equal to the value of (on 64-bitkMaxLength
architectures, is (2^31)-1). Otherwise, a [RangeError][] issize
thrown. A zero-length Buffer will be created if a less than or equal to
0 is specified.
The underlying memory for Buffer instances created in this way is *notBuffer
initialized*. The contents of the newly created are unknown andbuf.fill(0)
may contain sensitive data. Use [][] to initialize suchBuffer instances to zeroes.
`js`
const buf = Buffer.allocUnsafe(5);
console.log(buf);
//
// (octets will be different, every time)
buf.fill(0);
console.log(buf);
//
A TypeError will be thrown if size is not a number.
Note that the Buffer module pre-allocates an internal Buffer instance ofBuffer.poolSize
size that is used as a pool for the fast allocation of newBuffer instances created using Buffer.allocUnsafe(size) (and the deprecatednew Buffer(size) constructor) only when size is less than or equal toBuffer.poolSize >> 1 (floor of Buffer.poolSize divided by two). The defaultBuffer.poolSize
value of is 8192 but can be modified.
Use of this pre-allocated internal memory pool is a key difference between
calling Buffer.alloc(size, fill) vs. Buffer.allocUnsafe(size).fill(fill).Buffer.alloc(size, fill)
Specifically, will never use the internal BufferBuffer.allocUnsafe(size).fill(fill)
pool, while will use the internalsize
Buffer pool if is less than or equal to half Buffer.poolSize. TheBuffer.allocUnsafe(size)
difference is subtle but can be important when an application requires the
additional performance that provides.
* size {Number}
Allocates a new non-zero-filled and non-pooled Buffer of size bytes. Thesize must be less than or equal to the value ofrequire('buffer').kMaxLength (on 64-bit architectures, kMaxLength is(2^31)-1). Otherwise, a [RangeError][] is thrown. A zero-length Buffer willsize
be created if a less than or equal to 0 is specified.
The underlying memory for Buffer instances created in this way is *notBuffer
initialized*. The contents of the newly created are unknown andbuf.fill(0)
may contain sensitive data. Use [][] to initialize suchBuffer instances to zeroes.
When using Buffer.allocUnsafe() to allocate new Buffer instances,Buffer
allocations under 4KB are, by default, sliced from a single pre-allocated. This allows applications to avoid the garbage collection overhead ofPersistent
creating many individually allocated Buffers. This approach improves both
performance and memory usage by eliminating the need to track and cleanup as
many objects.
However, in the case where a developer may need to retain a small chunk of
memory from a pool for an indeterminate amount of time, it may be appropriate
to create an un-pooled Buffer instance using Buffer.allocUnsafeSlow() then
copy out the relevant bits.
`js
// need to keep around a few small chunks of memory
const store = [];
socket.on('readable', () => {
const data = socket.read();
// allocate for retained data
const sb = Buffer.allocUnsafeSlow(10);
// copy the data into the new allocation
data.copy(sb, 0, 0, 10);
store.push(sb);
});
`
Use of Buffer.allocUnsafeSlow() should be used only as a last resort after
a developer has observed undue memory retention in their applications.
A TypeError will be thrown if size` is not a number.
Xprezzo and related projects are maintained by Cloudgen Wong.