- Assertion Testing
- Async Hooks
- Buffer
- C++ Addons
- C/C++ Addons - N-API
- Child Processes
- Cluster
- Command Line Options
- Console
- Crypto
- Debugger
- Deprecated APIs
- DNS
- Domain
- ECMAScript Modules
- Errors
- Events
- File System
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules
- Net
- OS
- Path
- Performance Hooks
- Process
- Punycode
- Query Strings
- Readline
- REPL
- Stream
- String Decoder
- Timers
- TLS/SSL
- Tracing
- TTY
- UDP/Datagram
- URL
- Utilities
- V8
- VM
- ZLIB
Node.js v8.17.0 Documentation
Table of Contents
- Buffer
Buffer.from(),Buffer.alloc(), andBuffer.allocUnsafe()- Buffers and Character Encodings
- Buffers and TypedArray
- Buffers and iteration
- Class: Buffer
- new Buffer(array)
- new Buffer(arrayBuffer[, byteOffset[, length]])
- new Buffer(buffer)
- new Buffer(size)
- new Buffer(string[, encoding])
- Class Method: Buffer.alloc(size[, fill[, encoding]])
- Class Method: Buffer.allocUnsafe(size)
- Class Method: Buffer.allocUnsafeSlow(size)
- Class Method: Buffer.byteLength(string[, encoding])
- Class Method: Buffer.compare(buf1, buf2)
- Class Method: Buffer.concat(list[, totalLength])
- Class Method: Buffer.from(array)
- Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]])
- Class Method: Buffer.from(buffer)
- Class Method: Buffer.from(string[, encoding])
- Class Method: Buffer.from(object[, offsetOrEncoding[, length]])
- Class Method: Buffer.isBuffer(obj)
- Class Method: Buffer.isEncoding(encoding)
- Class Property: Buffer.poolSize
- buf[index]
- buf.buffer
- buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
- buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])
- buf.entries()
- buf.equals(otherBuffer)
- buf.fill(value[, offset[, end]][, encoding])
- buf.includes(value[, byteOffset][, encoding])
- buf.indexOf(value[, byteOffset][, encoding])
- buf.keys()
- buf.lastIndexOf(value[, byteOffset][, encoding])
- buf.length
- buf.parent
- buf.readDoubleBE(offset[, noAssert])
- buf.readDoubleLE(offset[, noAssert])
- buf.readFloatBE(offset[, noAssert])
- buf.readFloatLE(offset[, noAssert])
- buf.readInt8(offset[, noAssert])
- buf.readInt16BE(offset[, noAssert])
- buf.readInt16LE(offset[, noAssert])
- buf.readInt32BE(offset[, noAssert])
- buf.readInt32LE(offset[, noAssert])
- buf.readIntBE(offset, byteLength[, noAssert])
- buf.readIntLE(offset, byteLength[, noAssert])
- buf.readUInt8(offset[, noAssert])
- buf.readUInt16BE(offset[, noAssert])
- buf.readUInt16LE(offset[, noAssert])
- buf.readUInt32BE(offset[, noAssert])
- buf.readUInt32LE(offset[, noAssert])
- buf.readUIntBE(offset, byteLength[, noAssert])
- buf.readUIntLE(offset, byteLength[, noAssert])
- buf.slice([start[, end]])
- buf.swap16()
- buf.swap32()
- buf.swap64()
- buf.toJSON()
- buf.toString([encoding[, start[, end]]])
- buf.values()
- buf.write(string[, offset[, length]][, encoding])
- buf.writeDoubleBE(value, offset[, noAssert])
- buf.writeDoubleLE(value, offset[, noAssert])
- buf.writeFloatBE(value, offset[, noAssert])
- buf.writeFloatLE(value, offset[, noAssert])
- buf.writeInt8(value, offset[, noAssert])
- buf.writeInt16BE(value, offset[, noAssert])
- buf.writeInt16LE(value, offset[, noAssert])
- buf.writeInt32BE(value, offset[, noAssert])
- buf.writeInt32LE(value, offset[, noAssert])
- buf.writeIntBE(value, offset, byteLength[, noAssert])
- buf.writeIntLE(value, offset, byteLength[, noAssert])
- buf.writeUInt8(value, offset[, noAssert])
- buf.writeUInt16BE(value, offset[, noAssert])
- buf.writeUInt16LE(value, offset[, noAssert])
- buf.writeUInt32BE(value, offset[, noAssert])
- buf.writeUInt32LE(value, offset[, noAssert])
- buf.writeUIntBE(value, offset, byteLength[, noAssert])
- buf.writeUIntLE(value, offset, byteLength[, noAssert])
- buffer.INSPECT_MAX_BYTES
- buffer.kMaxLength
- buffer.transcode(source, fromEnc, toEnc)
- Class: SlowBuffer
- Buffer Constants
Buffer#
Prior to the introduction of TypedArray, the JavaScript language had no
mechanism for reading or manipulating streams of binary data. The Buffer class
was introduced as part of the Node.js API to enable interaction with octet
streams in TCP streams, file system operations, and other contexts.
With TypedArray now available, the Buffer class implements the
Uint8Array API in a manner that is more optimized and suitable for Node.js.
Instances of the Buffer class are similar to arrays of integers but
correspond to fixed-sized, raw memory allocations outside the V8 heap.
The size of the Buffer is established when it is created and cannot be
changed.
The Buffer class is within the global scope, making it unlikely that one
would need to ever use require('buffer').Buffer.
Examples:
// Creates a zero-filled Buffer of length 10.
const buf1 = Buffer.alloc(10);
// Creates a Buffer of length 10, filled with 0x1.
const buf2 = Buffer.alloc(10, 1);
// Creates an uninitialized buffer of length 10.
// This is faster than calling Buffer.alloc() but the returned
// Buffer instance might contain old data that needs to be
// overwritten using either fill() or write().
const buf3 = Buffer.allocUnsafe(10);
// Creates a Buffer containing [0x1, 0x2, 0x3].
const buf4 = Buffer.from([1, 2, 3]);
// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].
const buf5 = Buffer.from('tést');
// Creates a Buffer containing Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].
const buf6 = Buffer.from('tést', 'latin1');
Buffer.from(), Buffer.alloc(), and Buffer.allocUnsafe()#
In versions of Node.js prior to v6, Buffer instances were created using the
Buffer constructor function, which allocates the returned Buffer
differently based on what arguments are provided:
- Passing a number as the first argument to
Buffer()(e.g.new Buffer(10)), allocates a newBufferobject of the specified size. Prior to Node.js 8.0.0, the memory allocated for suchBufferinstances is not initialized and can contain sensitive data. SuchBufferinstances must be subsequently initialized by using eitherbuf.fill(0)or by writing to theBuffercompletely. While this behavior is intentional to improve performance, development experience has demonstrated that a more explicit distinction is required between creating a fast-but-uninitializedBufferversus creating a slower-but-saferBuffer. Starting in Node.js 8.0.0,Buffer(num)andnew Buffer(num)will return aBufferwith initialized memory. - Passing a string, array, or
Bufferas the first argument copies the passed object's data into theBuffer. - Passing an
ArrayBufferor aSharedArrayBufferreturns aBufferthat shares allocated memory with the given array buffer.
Because the behavior of new Buffer() changes significantly based on the type
of value passed as the first argument, applications that do not properly
validate the input arguments passed to new Buffer(), or that fail to
appropriately initialize newly allocated Buffer content, can inadvertently
introduce security and reliability issues into their code.
To make the creation of Buffer instances more reliable and less error prone,
the various forms of the new Buffer() constructor have been deprecated
and replaced by separate Buffer.from(), Buffer.alloc(), and
Buffer.allocUnsafe() methods.
Developers should migrate all existing uses of the new Buffer() constructors
to one of these new APIs.
Buffer.from(array)returns a newBuffercontaining a copy of the provided octets.Buffer.from(arrayBuffer[, byteOffset [, length]])returns a newBufferthat shares the same allocated memory as the givenArrayBuffer.Buffer.from(buffer)returns a newBuffercontaining a copy of the contents of the givenBuffer.Buffer.from(string[, encoding])returns a newBuffercontaining a copy of the provided string.Buffer.alloc(size[, fill[, encoding]])returns a "filled"Bufferinstance of the specified size. This method can be significantly slower thanBuffer.allocUnsafe(size)but ensures that newly createdBufferinstances never contain old and potentially sensitive data.Buffer.allocUnsafe(size)andBuffer.allocUnsafeSlow(size)each return a newBufferof the specifiedsizewhose content must be initialized using eitherbuf.fill(0)or written to completely.
Buffer instances returned by Buffer.allocUnsafe() may be allocated off
a shared internal memory pool if size is less than or equal to half
Buffer.poolSize. Instances returned by Buffer.allocUnsafeSlow() never
use the shared internal memory pool.
The --zero-fill-buffers command line option#
Node.js can be started using the --zero-fill-buffers command line option to
force all newly allocated Buffer instances created using either
new Buffer(size), Buffer.allocUnsafe(), Buffer.allocUnsafeSlow() or
new SlowBuffer(size) to be automatically zero-filled upon creation. Use of
this flag changes the default behavior of these methods and can have a significant
impact on performance. Use of the --zero-fill-buffers option is recommended
only when necessary to enforce that newly allocated Buffer instances cannot
contain potentially sensitive data.
Example:
$ node --zero-fill-buffers
> Buffer.allocUnsafe(5);
<Buffer 00 00 00 00 00>
What makes Buffer.allocUnsafe() and Buffer.allocUnsafeSlow() "unsafe"?#
When calling Buffer.allocUnsafe() and Buffer.allocUnsafeSlow(), the
segment of allocated memory is uninitialized (it is not zeroed-out). While
this design makes the allocation of memory quite fast, the allocated segment of
memory might contain old data that is potentially sensitive. Using a Buffer
created by Buffer.allocUnsafe() without completely overwriting the memory
can allow this old data to be leaked when the Buffer memory is read.
While there are clear performance advantages to using Buffer.allocUnsafe(),
extra care must be taken in order to avoid introducing security
vulnerabilities into an application.
Buffers and Character Encodings#
Buffer instances are commonly used to represent sequences of encoded characters
such as UTF-8, UCS2, Base64, or even Hex-encoded data. It is possible to
convert back and forth between Buffer instances and ordinary JavaScript strings
by using an explicit character encoding.
Example:
const buf = Buffer.from('hello world', 'ascii');
// Prints: 68656c6c6f20776f726c64
console.log(buf.toString('hex'));
// Prints: aGVsbG8gd29ybGQ=
console.log(buf.toString('base64'));
The character encodings currently supported by Node.js include:
'ascii'- For 7-bit ASCII data only. This encoding is fast and will strip the high bit if set.'utf8'- Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.'utf16le'- 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.'ucs2'- Alias of'utf16le'.'base64'- Base64 encoding. When creating aBufferfrom a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC4648, Section 5.'latin1'- A way of encoding theBufferinto a one-byte encoded string (as defined by the IANA in RFC1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes).'binary'- Alias for'latin1'.'hex'- Encode each byte as two hexadecimal characters.
Note: Today's browsers follow the WHATWG Encoding Standard which aliases
both 'latin1' and 'ISO-8859-1' to 'win-1252'. This means that while doing
something like http.get(), if the returned charset is one of those listed in
the WHATWG specification it is possible that the server actually returned
'win-1252'-encoded data, and using 'latin1' encoding may incorrectly decode
the characters.
Buffers and TypedArray#
Buffer instances are also Uint8Array instances. However, there are subtle
incompatibilities with TypedArray. For example, while
ArrayBuffer#slice() creates a copy of the slice, the implementation of
Buffer#slice() creates a view over the existing Buffer
without copying, making Buffer#slice() far more efficient.
It is also possible to create new TypedArray instances from a Buffer with
the following caveats:
The
Bufferobject's memory is copied to theTypedArray, not shared.The
Bufferobject's memory is interpreted as an array of distinct elements, and not as a byte array of the target type. That is,new Uint32Array(Buffer.from([1, 2, 3, 4]))creates a 4-elementUint32Arraywith elements[1, 2, 3, 4], not aUint32Arraywith a single element[0x1020304]or[0x4030201].
It is possible to create a new Buffer that shares the same allocated memory as
a TypedArray instance by using the TypeArray object's .buffer property.
Example:
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
// Copies the contents of `arr`
const buf1 = Buffer.from(arr);
// Shares memory with `arr`
const buf2 = Buffer.from(arr.buffer);
// Prints: <Buffer 88 a0>
console.log(buf1);
// Prints: <Buffer 88 13 a0 0f>
console.log(buf2);
arr[1] = 6000;
// Prints: <Buffer 88 a0>
console.log(buf1);
// Prints: <Buffer 88 13 70 17>
console.log(buf2);
Note that when creating a Buffer using a TypedArray's .buffer, it is
possible to use only a portion of the underlying ArrayBuffer by passing in
byteOffset and length parameters.
Example:
const arr = new Uint16Array(20);
const buf = Buffer.from(arr.buffer, 0, 16);
// Prints: 16
console.log(buf.length);
The Buffer.from() and TypedArray.from() have different signatures and
implementations. Specifically, the TypedArray variants accept a second
argument that is a mapping function that is invoked on every element of the
typed array:
TypedArray.from(source[, mapFn[, thisArg]])
The Buffer.from() method, however, does not support the use of a mapping
function:
Buffer.from(array)Buffer.from(buffer)Buffer.from(arrayBuffer[, byteOffset [, length]])Buffer.from(string[, encoding])
Buffers and iteration#
Buffer instances can be iterated over using for..of syntax:
Example:
const buf = Buffer.from([1, 2, 3]);
// Prints:
// 1
// 2
// 3
for (const b of buf) {
console.log(b);
}
Additionally, the buf.values(), buf.keys(), and
buf.entries() methods can be used to create iterators.
Class: Buffer#
The Buffer class is a global type for dealing with binary data directly.
It can be constructed in a variety of ways.
new Buffer(array)#
Buffer.from(array) instead.array<integer[]> An array of bytes to copy from.
Allocates a new Buffer using an array of octets.
Example:
// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
new Buffer(arrayBuffer[, byteOffset[, length]])#
Buffer.from(arrayBuffer[, byteOffset [, length]]) instead.arrayBuffer<ArrayBuffer> | <SharedArrayBuffer> AnArrayBuffer,SharedArrayBufferor the.bufferproperty of aTypedArray.byteOffset<integer> Index of first byte to expose. Default:0.length<integer> Number of bytes to expose. Default:arrayBuffer.length - byteOffset.
This creates a view of the ArrayBuffer or SharedArrayBuffer without
copying the underlying memory. For example, when passed a reference to the
.buffer property of a TypedArray instance, the newly created Buffer will
share the same allocated memory as the TypedArray.
The optional byteOffset and length arguments specify a memory range within
the arrayBuffer that will be shared by the Buffer.
Example:
const arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
// Shares memory with `arr`
const buf = new Buffer(arr.buffer);
// Prints: <Buffer 88 13 a0 0f>
console.log(buf);
// Changing the original Uint16Array changes the Buffer also
arr[1] = 6000;
// Prints: <Buffer 88 13 70 17>
console.log(buf);
new Buffer(buffer)#
Buffer.from(buffer) instead.buffer<Buffer> | <Uint8Array> An existingBufferorUint8Arrayfrom which to copy data.
Copies the passed buffer data onto a new Buffer instance.
Example:
const buf1 = new Buffer('buffer');
const buf2 = new Buffer(buf1);
buf1[0] = 0x61;
// Prints: auffer
console.log(buf1.toString());
// Prints: buffer
console.log(buf2.toString());
new Buffer(size)#
size<integer> The desired length of the newBuffer.
Allocates a new Buffer of size bytes. If the size is larger than
buffer.constants.MAX_LENGTH or smaller than 0, a