On this page

node:buffer module APIs

History

While, the Buffer object is available as a global, there are additional Buffer-related APIs that are available only via the node:buffer module accessed using require('node:buffer').

M

buffer.atob

History
buffer.atob(data): string
Stability: 3Legacy. Use Buffer.from(data, 'base64') instead.
Attributes
data:any
The Base64-encoded input string.
Returns:string

Decodes a string of Base64-encoded data into bytes, and encodes those bytes into a string using Latin-1 (ISO-8859-1).

The data may be any JavaScript-value that can be coerced into a string.

This function is only provided for compatibility with legacy web platform APIs and should never be used in new code, because they use strings to represent binary data and predate the introduction of typed arrays in JavaScript. For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using Buffer.from(str, 'base64') and buf.toString('base64').

An automated migration is available (source:

M

buffer.btoa

History
buffer.btoa(data): string
Stability: 3Legacy. Use buf.toString('base64') instead.
Attributes
data:any
An ASCII (Latin1) string.
Returns:string

Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes into a string using Base64.

The data may be any JavaScript-value that can be coerced into a string.

This function is only provided for compatibility with legacy web platform APIs and should never be used in new code, because they use strings to represent binary data and predate the introduction of typed arrays in JavaScript. For code running using Node.js APIs, converting between base64-encoded strings and binary data should be performed using Buffer.from(str, 'base64') and buf.toString('base64').

An automated migration is available (source:

buffer.isAscii(input): boolean
Attributes
The input to validate.
Returns:boolean

This function returns true if input contains only valid ASCII-encoded data, including the case in which input is empty.

A detached ArrayBuffer, or a TypedArray backed by one, is treated as empty.

buffer.isUtf8(input): boolean
Attributes
The input to validate.
Returns:boolean

This function returns true if input contains only valid UTF-8-encoded data, including the case in which input is empty.

A detached ArrayBuffer, or a TypedArray backed by one, is treated as empty.

P

buffer.INSPECT_MAX_BYTES

History
Type?:integer
Default: 50

Returns the maximum number of bytes that will be returned when buf.inspect() is called. This can be overridden by user modules. See util.inspect() for more details on buf.inspect() behavior.

P

buffer.kMaxLength

History
Type:integer
The largest size allowed for a single Buffer instance.

An alias for buffer.constants.MAX_LENGTH.

P

buffer.kStringMaxLength

History
Type:integer
The largest length allowed for a single string instance.

An alias for buffer.constants.MAX_STRING_LENGTH.

buffer.resolveObjectURL(id): Blob
Attributes
A 'blob:nodedata:... URL string returned by a prior call to URL.createObjectURL().
Returns:Blob

Resolves a 'blob:nodedata:...' an associated Blob object registered using a prior call to URL.createObjectURL().

buffer.transcode(source, fromEnc, toEnc): Buffer
Attributes
A Buffer or Uint8Array instance.
fromEnc:string
The current encoding.
toEnc:string
To target encoding.
Returns:Buffer

Re-encodes the given Buffer or Uint8Array instance from one character encoding to another. Returns a new Buffer instance.

Throws if the fromEnc or toEnc specify invalid character encodings or if conversion from fromEnc to toEnc is not permitted.

Encodings supported by buffer.transcode() are: 'ascii', 'utf8', 'utf16le', 'ucs2', 'latin1', and 'binary'.

The transcoding process will use substitution characters if a given byte sequence cannot be adequately represented in the target encoding. For instance:

import { Buffer, transcode } from 'node:buffer';

const newBuf = transcode(Buffer.from(''), 'utf8', 'ascii');
console.log(newBuf.toString('ascii'));
// Prints: '?'
const { Buffer, transcode } = require('node:buffer');

const newBuf = transcode(Buffer.from(''), 'utf8', 'ascii');
console.log(newBuf.toString('ascii'));
// Prints: '?'

Because the Euro () sign is not representable in US-ASCII, it is replaced with ? in the transcoded Buffer.

Buffer constants

History
Type:integer
The largest size allowed for a single Buffer instance.

On 32-bit architectures, this value is equal to 231 - 1 (about 2 GiB).

On 64-bit architectures, this value is equal to Number.MAX_SAFE_INTEGER (253 - 1, about 8 PiB).

It reflects v8::Uint8Array::kMaxLength under the hood.

This value is also available as buffer.kMaxLength.

P

buffer.constants.MAX_STRING_LENGTH

History
Type:integer
The largest length allowed for a single string instance.

Represents the largest length that a string primitive can have, counted in UTF-16 code units.

This value may depend on the JS engine that is being used.