Node.js v26.9.0 documentation
- Node.js v26.9.0
- Table of contents
- Buffer
- Buffers and character encodings
- Buffers and TypedArrays
- Buffers and iteration
- Class:
Blob - Class:
Buffer- Static method:
Buffer.alloc(size[, fill[, encoding]]) - Static method:
Buffer.allocUnsafe(size[, alignment]) - Static method:
Buffer.allocUnsafeSlow(size[, alignment]) - Static method:
Buffer.byteLength(string[, encoding]) - Static method:
Buffer.compare(buf1, buf2) - Static method:
Buffer.concat(list[, totalLength]) - Static method:
Buffer.copyBytesFrom(view[, offset[, length]]) - Static method:
Buffer.from(array) - Static method:
Buffer.from(arrayBuffer[, byteOffset[, length]]) - Static method:
Buffer.from(buffer) - Static method:
Buffer.from(object[, offsetOrEncoding[, length]]) - Static method:
Buffer.from(string[, encoding]) - Static method:
Buffer.isBuffer(obj) - Static method:
Buffer.isEncoding(encoding) Buffer.poolSizebuf[index]buf.bufferbuf.byteOffsetbuf.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[, start[, end]][, encoding])buf.indexOf(value[, start[, end]][, encoding])buf.keys()buf.lastIndexOf(value[, start[, end]][, encoding])buf.lengthbuf.parentbuf.readBigInt64BE([offset])buf.readBigInt64LE([offset])buf.readBigUInt64BE([offset])buf.readBigUInt64LE([offset])buf.readDoubleBE([offset])buf.readDoubleLE([offset])buf.readFloatBE([offset])buf.readFloatLE([offset])buf.readInt8([offset])buf.readInt16BE([offset])buf.readInt16LE([offset])buf.readInt32BE([offset])buf.readInt32LE([offset])buf.readIntBE(offset, byteLength)buf.readIntLE(offset, byteLength)buf.readUInt8([offset])buf.readUInt16BE([offset])buf.readUInt16LE([offset])buf.readUInt32BE([offset])buf.readUInt32LE([offset])buf.readUIntBE(offset, byteLength)buf.readUIntLE(offset, byteLength)buf.subarray([start[, end]])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.writeBigInt64BE(value[, offset])buf.writeBigInt64LE(value[, offset])buf.writeBigUInt64BE(value[, offset])buf.writeBigUInt64LE(value[, offset])buf.writeDoubleBE(value[, offset])buf.writeDoubleLE(value[, offset])buf.writeFloatBE(value[, offset])buf.writeFloatLE(value[, offset])buf.writeInt8(value[, offset])buf.writeInt16BE(value[, offset])buf.writeInt16LE(value[, offset])buf.writeInt32BE(value[, offset])buf.writeInt32LE(value[, offset])buf.writeIntBE(value, offset, byteLength)buf.writeIntLE(value, offset, byteLength)buf.writeUInt8(value[, offset])buf.writeUInt16BE(value[, offset])buf.writeUInt16LE(value[, offset])buf.writeUInt32BE(value[, offset])buf.writeUInt32LE(value[, offset])buf.writeUIntBE(value, offset, byteLength)buf.writeUIntLE(value, offset, byteLength)new Buffer(array)new Buffer(arrayBuffer[, byteOffset[, length]])new Buffer(buffer)new Buffer(size)new Buffer(string[, encoding])
- Static method:
- Class:
File node:buffermodule APIsBuffer.from(),Buffer.alloc(), andBuffer.allocUnsafe()
- Buffer
- Index
- About this documentation
- Usage and example
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Benchmark runner
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Environment Variables
- Errors
- Events
- File system
- FFI
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Iterable Streams API
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:moduleAPI - Modules: Packages
- Modules: TypeScript
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- SQLite
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- Virtual File System
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- Other versions
- Options
Buffer#
Stability: 2 - Stable
Buffer objects are used to represent a fixed-length sequence of bytes. Many
Node.js APIs support Buffers.
The Buffer class is a subclass of JavaScript's <Uint8Array> class and
extends it with methods that cover additional use cases. Node.js APIs accept
plain <Uint8Array>s wherever Buffers are supported as well.
While the Buffer class is available within the global scope, it is still
recommended to explicitly reference it via an import or require statement.
import { Buffer } from 'node:buffer'; // Creates a zero-filled Buffer of length 10. const buf1 = Buffer.alloc(10); // Creates a Buffer of length 10, // filled with bytes which all have the value `1`. 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 fill(), write(), or other functions that fill the Buffer's // contents. const buf3 = Buffer.allocUnsafe(10); // Creates a Buffer containing the bytes [1, 2, 3]. const buf4 = Buffer.from([1, 2, 3]); // Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries // are all truncated using `(value & 255)` to fit into the range 0–255. const buf5 = Buffer.from([257, 257.5, -255, '1']); // Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést': // [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation) // [116, 195, 169, 115, 116] (in decimal notation) const buf6 = Buffer.from('tést'); // Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74]. const buf7 = Buffer.from('tést', 'latin1');const { Buffer } = require('node:buffer'); // Creates a zero-filled Buffer of length 10. const buf1 = Buffer.alloc(10); // Creates a Buffer of length 10, // filled with bytes which all have the value `1`. 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 fill(), write(), or other functions that fill the Buffer's // contents. const buf3 = Buffer.allocUnsafe(10); // Creates a Buffer containing the bytes [1, 2, 3]. const buf4 = Buffer.from([1, 2, 3]); // Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries // are all truncated using `(value & 255)` to fit into the range 0–255. const buf5 = Buffer.from([257, 257.5, -255, '1']); // Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést': // [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation) // [116, 195, 169, 115, 116] (in decimal notation) const buf6 = Buffer.from('tést'); // Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74]. const buf7 = Buffer.from('tést', 'latin1');
Buffers and character encodings#
When converting between Buffers and strings, a character encoding may be
specified. If no character encoding is specified, UTF-8 will be used as the
default.
import { Buffer } from 'node:buffer'; const buf = Buffer.from('hello world', 'utf8'); console.log(buf.toString('hex')); // Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); // Prints: aGVsbG8gd29ybGQ= console.log(Buffer.from('fhqwhgads', 'utf8')); // Prints: <Buffer 66 68 71 77 68 67 61 64 73> console.log(Buffer.from('fhqwhgads', 'utf16le')); // Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>const { Buffer } = require('node:buffer'); const buf = Buffer.from('hello world', 'utf8'); console.log(buf.toString('hex')); // Prints: 68656c6c6f20776f726c64 console.log(buf.toString('base64')); // Prints: aGVsbG8gd29ybGQ= console.log(Buffer.from('fhqwhgads', 'utf8')); // Prints: <Buffer 66 68 71 77 68 67 61 64 73> console.log(Buffer.from('fhqwhgads', 'utf16le')); // Prints: <Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00>
Node.js buffers accept all case variations of encoding strings that they
receive. For example, UTF-8 can be specified as 'utf8', 'UTF8', or 'uTf8'.
The character encodings currently supported by Node.js are the following:
-
'utf8'(alias:'utf-8'): Multi-byte encoded Unicode characters. Many web pages and other document formats use UTF-8. This is the default character encoding. When decoding aBufferinto a string that does not exclusively contain valid UTF-8 data, the Unicode replacement characterU+FFFD� will be used to represent those errors. -
'utf16le'(alias:'utf-16le'): Multi-byte encoded Unicode characters. Unlike'utf8', each character in the string will be encoded using either 2 or 4 bytes. Node.js only supports the little-endian variant of UTF-16. -
'latin1': Latin-1 stands for ISO-8859-1. This character encoding only supports the Unicode characters fromU+0000toU+00FF. Each character is encoded using a single byte. Characters that do not fit into that range are truncated and will be mapped to characters in that range.
Converting a Buffer into a string using one of the above is referred to as
decoding, and converting a string into a Buffer is referred to as encoding.
Node.js also supports the following binary-to-text encodings. For
binary-to-text encodings, the naming convention is reversed: Converting a
Buffer into a string is typically referred to as encoding, and converting a
string into a Buffer as decoding.
-
'base64': Base64 encoding. When creating aBufferfrom a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC 4648, Section 5. Whitespace characters such as spaces, tabs, and new lines contained within the base64-encoded string are ignored. -
'base64url': base64url encoding as specified in RFC 4648, Section 5. When creating aBufferfrom a string, this encoding will also correctly accept regular base64-encoded strings. When encoding aBufferto a string, this encoding will omit padding. -
'hex': Encode each byte as two hexadecimal characters. Data truncation may occur when decoding strings that do not exclusively consist of an even number of hexadecimal characters. See below for an example.
The following legacy character encodings are also supported:
-
'ascii': For 7-bit ASCII data only. When encoding a string into aBuffer, this is equivalent to using'latin1'. When decoding aBufferinto a string, using this encoding will additionally unset the highest bit of each byte before decoding as'latin1'. Generally, there should be no reason to use this encoding, as'utf8'(or, if the data is known to always be ASCII-only,'latin1') will be a better choice when encoding or decoding ASCII-only text. It is only provided for legacy compatibility. -
'binary': Alias for'latin1'. The name of this encoding can be very misleading, as all of the encodings listed here convert between strings and binary data. For converting between strings andBuffers, typically'utf8'is the right choice. -
'ucs2','ucs-2': Aliases of'utf16le'. UCS-2 used to refer to a variant of UTF-16 that did not support characters that had code points larger than U+FFFF. In Node.js, these code points are always supported.
import { Buffer } from 'node:buffer'; Buffer.from('1ag123', 'hex'); // Prints <Buffer 1a>, data truncated when first non-hexadecimal value // ('g') encountered. Buffer.from('1a7', 'hex'); // Prints <Buffer 1a>, data truncated when data ends in single digit ('7'). Buffer.from('1634', 'hex'); // Prints <Buffer 16 34>, all data represented.const { Buffer } = require('node:buffer'); Buffer.from('1ag123', 'hex'); // Prints <Buffer 1a>, data truncated when first non-hexadecimal value // ('g') encountered. Buffer.from('1a7', 'hex'); // Prints <Buffer 1a>, data truncated when data ends in single digit ('7'). Buffer.from('1634', 'hex'); // Prints <Buffer 16 34>, all data represented.
Modern Web 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 TypedArrays#
Buffer instances are also JavaScript <Uint8Array> and <TypedArray>
instances. All <TypedArray> methods and properties are available on Buffers. There are,
however, subtle incompatibilities between the Buffer API and the
<TypedArray> API.
In particular:
- While
TypedArray.prototype.slice()creates a copy of part of theTypedArray,Buffer.prototype.slice()creates a view over the existingBufferwithout copying. This behavior can be surprising, and only exists for legacy compatibility.TypedArray.prototype.subarray()can be used to achieve the behavior ofBuffer.prototype.slice()on bothBuffers and otherTypedArrays and should be preferred. buf.toString()is incompatible with itsTypedArrayequivalent.- A number of methods, e.g.
buf.indexOf(), support additional arguments.
There are two ways to create new <TypedArray> instances from a Buffer:
- Passing a
Bufferto a<TypedArray>constructor will copy theBuffer's contents, interpreted as an array of integers, and not as a byte sequence of the target type.
import { Buffer } from 'node:buffer'; const buf = Buffer.from([1, 2, 3, 4]); const uint32array = new Uint32Array(buf); console.log(uint32array); // Prints: Uint32Array(4) [ 1, 2, 3, 4 ]const { Buffer } = require('node:buffer'); const buf = Buffer.from([1, 2, 3, 4]); const uint32array = new Uint32Array(buf); console.log(uint32array); // Prints: Uint32Array(4) [ 1, 2, 3, 4 ]
- Passing the
Buffer's underlying<ArrayBuffer>will create a<TypedArray>that shares its memory with theBuffer.
import { Buffer } from 'node:buffer'; const buf = Buffer.from('hello', 'utf16le'); const uint16array = new Uint16Array( buf.buffer, buf.byteOffset, buf.length / Uint16Array.BYTES_PER_ELEMENT); console.log(uint16array); // Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]const { Buffer } = require('node:buffer'); const buf = Buffer.from('hello', 'utf16le'); const uint16array = new Uint16Array( buf.buffer, buf.byteOffset, buf.length / Uint16Array.BYTES_PER_ELEMENT); console.log(uint16array); // Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]
It is possible to create a new Buffer that shares the same allocated
memory as a <TypedArray> instance by using the TypedArray object's
.buffer property in the same way. Buffer.from()
behaves like new Uint8Array() in this context.
import { Buffer } from 'node:buffer'; 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); console.log(buf1); // Prints: <Buffer 88 a0> console.log(buf2); // Prints: <Buffer 88 13 a0 0f> arr[1] = 6000; console.log(buf1); // Prints: <Buffer 88 a0> console.log(buf2); // Prints: <Buffer 88 13 70 17>const { Buffer } = require('node:buffer'); 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); console.log(buf1); // Prints: <Buffer 88 a0> console.log(buf2); // Prints: <Buffer 88 13 a0 0f> arr[1] = 6000; console.log(buf1); // Prints: <Buffer 88 a0> console.log(buf2); // Prints: <Buffer 88 13 70 17>
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.
import { Buffer } from 'node:buffer'; const arr = new Uint16Array(20); const buf = Buffer.from(arr.buffer, 0, 16); console.log(buf.length); // Prints: 16const { Buffer } = require('node:buffer'); const arr = new Uint16Array(20); const buf = Buffer.from(arr.buffer, 0, 16); console.log(buf.length); // Prints: 16
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:
The Buffer.from() method, however, does not support the use of a mapping
function:
Buffer methods are callable with Uint8Array instances#
All methods on the Buffer prototype are callable with a Uint8Array instance.
const { toString, write } = Buffer.prototype;
const uint8array = new Uint8Array(5);
write.call(uint8array, 'hello', 0, 5, 'utf8'); // 5
// <Uint8Array 68 65 6c 6c 6f>
toString.call(uint8array, 'utf8'); // 'hello'
Buffers and iteration#
Buffer instances can be iterated over using for..of syntax:
import { Buffer } from 'node:buffer'; const buf = Buffer.from([1, 2, 3]); for (const b of buf) { console.log(b); } // Prints: // 1 // 2 // 3const { Buffer } = require('node:buffer'); const buf = Buffer.from([1, 2, 3]); for (const b of buf) { console.log(b); } // Prints: // 1 // 2 // 3
Additionally, the buf.values(), buf.keys(), and
buf.entries() methods can be used to create iterators.
Class: Blob#
A <Blob> encapsulates immutable, raw data that can be safely shared across
multiple worker threads.
new buffer.Blob([sources[, options]])#
sources<string>[] |<ArrayBuffer>[] |<TypedArray>[] |<DataView>[] |<Blob>[] An array of string,<ArrayBuffer>,<TypedArray>,<DataView>, or<Blob>objects, or any mix of such objects, that will be stored within theBlob.options<Object>endings<string>One of either'transparent'or'native'. When set to'native', line endings in string source parts will be converted to the platform native line-ending as specified byrequire('node:os').EOL.type<string>The Blob content-type. The intent is fortypeto convey the MIME media type of the data, however no validation of the type format is performed.
Creates a new Blob object containing a concatenation of the given sources.
<ArrayBuffer>, <TypedArray>, <DataView>, and <Buffer> sources are copied into
the 'Blob' and can therefore be safely modified after the 'Blob' is created.
String sources are encoded as UTF-8 byte sequences and copied into the Blob. Unmatched surrogate pairs within each string part will be replaced by Unicode U+FFFD replacement characters.
blob.arrayBuffer()#
- Returns:
<Promise>
Returns a promise that fulfills with an <ArrayBuffer> containing a copy of
the Blob data.
blob.bytes()#
- Returns:
<Promise>
The blob.bytes() method returns the byte of the Blob object as a Promise<Uint8Array>.
const blob = new Blob(['hello']);
blob.bytes().then((bytes) => {
console.log(bytes); // Outputs: Uint8Array(5) [ 104, 101, 108, 108, 111 ]
});
blob.size#
The total size of the Blob in bytes.
blob.slice([start[, end[, type]]])#
start<number>The starting index.end<number>The ending index.type<string>The content-type for the newBlob- Returns:
<Blob>
Creates and returns a new Blob containing a subset of this Blob objects
data. The original Blob is not altered.
blob.stream()#
- Returns:
<ReadableStream>
Returns a new ReadableStream that allows the content of the Blob to be read.
blob.text()#
- Returns:
<Promise>
Returns a promise that fulfills with the contents of the Blob decoded as a
UTF-8 string.
blob.textStream()#
- Returns:
<ReadableStream>
Returns a new ReadableStream that allows the content of the Blob to be read
as a stream of UTF-8 decoded strings. It is equivalent to piping
blob.stream() through a TextDecoderStream set up with UTF-8.
blob.type#
- Type:
<string>
The content-type of the Blob.
Blob objects and MessageChannel#
Once a <Blob> object is created, it can be sent via MessagePort to multiple
destinations without transferring or immediately copying the data. The data
contained by the Blob is copied only when the arrayBuffer() or text()
methods are called.
import { Blob } from 'node:buffer'; import { setTimeout as delay } from 'node:timers/promises'; const blob = new Blob(['hello there']); const mc1 = new MessageChannel(); const mc2 = new MessageChannel(); mc1.port1.onmessage = async ({ data }) => { console.log(await data.arrayBuffer()); mc1.port1.close(); }; mc2.port1.onmessage = async ({ data }) => { await delay(1000); console.log(await data.arrayBuffer()); mc2.port1.close(); }; mc1.port2.postMessage(blob); mc2.port2.postMessage(blob); // The Blob is still usable after posting. blob.text().then(console.log);const { Blob } = require('node:buffer'); const { setTimeout: delay } = require('node:timers/promises'); const blob = new Blob(['hello there']); const mc1 = new MessageChannel(); const mc2 = new MessageChannel(); mc1.port1.onmessage = async ({ data }) => { console.log(await data.arrayBuffer()); mc1.port1.close(); }; mc2.port1.onmessage = async ({ data }) => { await delay(1000); console.log(await data.arrayBuffer()); mc2.port1.close(); }; mc1.port2.postMessage(blob); mc2.port2.postMessage(blob); // The Blob is still usable after posting. blob.text().then(console.log);
Class: Buffer#
The Buffer class is a global type for dealing with binary data directly.
It can be constructed in a variety of ways.
Static method: Buffer.alloc(size[, fill[, encoding]])#
size<integer>The desired length of the newBuffer.fill<string>|<Buffer>|<Uint8Array>|<integer>A value to pre-fill the newBufferwith. Default:0.encoding<string>Iffillis a string, this is its encoding. Default:'utf8'.- Returns:
<Buffer>
Allocates a new Buffer of size bytes. If fill is undefined, the
Buffer will be zero-filled.
import { Buffer } from 'node:buffer'; const buf = Buffer.alloc(5); console.log(buf); // Prints: <Buffer 00 00 00 00 00>const { Buffer } = require('node:buffer'); const buf = Buffer.alloc(5); console.log(buf); // Prints: <Buffer 00 00 00 00 00>
If size is larger than
buffer.constants.MAX_LENGTH or smaller than 0, ERR_OUT_OF_RANGE
is thrown.
If fill is specified, the allocated Buffer will be initialized by calling
buf.fill(fill).
import { Buffer } from 'node:buffer'; const buf = Buffer.alloc(5, 'a'); console.log(buf); // Prints: <Buffer 61 61 61 61 61>const { Buffer } = require('node:buffer'); const buf = Buffer.