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').
buffer.atob(data): string
Buffer.from(data, 'base64') instead.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:
npx codemod@latest @nodejs/buffer-atob-btoa
buffer.btoa(data): string
buf.toString('base64') instead.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:
npx codemod@latest @nodejs/buffer-atob-btoa
buffer.isAscii
History
ArrayBuffers and views backed by them are treated as empty.buffer.isAscii(input): boolean
Buffer | ArrayBuffer | TypedArraybooleanThis 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
History
ArrayBuffers and views backed by them are treated as empty.buffer.isUtf8(input): boolean
Buffer | ArrayBuffer | TypedArraybooleanThis 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.
integer50Returns 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.
integerBuffer instance.An alias for buffer.constants.MAX_LENGTH.
integerstring instance.An alias for buffer.constants.MAX_STRING_LENGTH.
buffer.resolveObjectURL(id): Blob
Resolves a 'blob:nodedata:...' an associated Blob object registered using
a prior call to URL.createObjectURL().
buffer.transcode(source, fromEnc, toEnc): Buffer
Buffer | Uint8ArrayBuffer or Uint8Array instance.stringstringBufferRe-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
integerBuffer 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.
integerstring 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.