Skip to main content
buffer - Node documentation

Usage in Deno

import * as mod from "node:buffer";

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');

Classes

c
I
v
Blob

A Blob encapsulates immutable, raw data that can be safely shared acrossmultiple worker threads.

c
I
v
File

A File provides information about files.

Functions

f
atob

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

f
btoa

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

f
isAscii

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

f
isUtf8

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

f
resolveObjectURL

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

f
transcode

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

Interfaces

Type Aliases

T
BufferEncoding
No documentation available
T
TranscodeEncoding
No documentation available
T
WithImplicitCoercion
No documentation available

Variables

v
constants
No documentation available
v
INSPECT_MAX_BYTES
No documentation available
v
kMaxLength
No documentation available
v
kStringMaxLength
No documentation available
v
SlowBuffer
No documentation available