module buffer

Supported only in the v8 engine. Introduced in Max v9.2.0.

The buffer module exposed by require("buffer"), providing the buffer.IOBuffer class for handling binary data.

IOBuffer is the binary type used throughout the v8 networking APIs. It is a Uint8Array subclass in the spirit of Node's Buffer, but is not full Node Buffer parity. There is no global Buffer alias (which would conflict with the Max buffer~ wrapper). See buffer.IOBuffer for details.

Example

const { IOBuffer } = require("buffer");
const buf = IOBuffer.from("hello");
post(buf.toString(), "\n"); // hello

Index

Classes

Type Aliases

class IOBuffer

A Uint8Array subclass for binary data, used throughout the v8 networking APIs.

IOBuffer is the binary type used throughout the networking APIs. It is a subclass of Uint8Array. There is no global Buffer alias (which would conflict with the Max buffer~ wrapper), and IOBuffer is not full Node Buffer parity.

Obtain the class with const { IOBuffer } = require("buffer").

Example

const { IOBuffer } = require("buffer");
const a = IOBuffer.from("hello");
const b = IOBuffer.from("68656c6c6f", "hex");
post(a.equals(b), "\n"); // true
post(a.toString("base64"), "\n");

Constructors

Methods

new IOBuffer(value, encoding)

new IOBuffer(
  value?:
    number | ArrayBuffer | ArrayBufferView | string | ArrayLike<number>,
  encoding?: IOBufferEncoding,
);

Constructs a new instance of the IOBuffer class

ParameterTypeDescription
optional valuenumber | ArrayBuffer | ArrayBufferView | string | ArrayLike<number>a byte length, an ArrayBuffer, a typed array / view, a string (decoded with encoding), or an array-like of byte values.
optional encodingIOBufferEncodingthe encoding used when value is a string (default "utf8").

IOBuffer.alloc(size, fill) static

Allocates a zero- or fill-initialized IOBuffer of the given size.

static alloc(size: number, fill?: number | string): IOBuffer;
NameTypeDescription
sizenumber
optional fillnumber | string
Return ValueIOBuffer

IOBuffer.byteLength(value, encoding) static

Returns the byte length of a string in the given encoding.

static byteLength(value: string, encoding?: IOBufferEncoding): number;
NameTypeDescription
valuestring
optional encodingIOBufferEncoding
Return Valuenumber

IOBuffer.concat(items) static

Concatenates several buffers into one.

static concat(items: IOBuffer[]): IOBuffer;
NameTypeDescription
itemsIOBuffer[]
Return ValueIOBuffer

equals(other)

Returns whether this buffer has the same bytes as another.

equals(other: IOBuffer): boolean;
NameTypeDescription
otherIOBuffer
Return Valueboolean

IOBuffer.from(value, encoding) static

Creates an IOBuffer from a value.

static from(
  value: ArrayBuffer | ArrayBufferView | string | ArrayLike<number>,
  encoding?: IOBufferEncoding,
): IOBuffer;
NameTypeDescription
valueArrayBuffer | ArrayBufferView | string | ArrayLike<number>
optional encodingIOBufferEncoding
Return ValueIOBuffer

IOBuffer.from(arrayLike, mapfn, thisArg) static

Inherited Uint8Array.from form with a map function.

static from(
  arrayLike: Iterable<number> | ArrayLike<number>,
  mapfn?: (v: number, k: number) => number,
  thisArg?: any,
): IOBuffer;
NameTypeDescription
arrayLikeIterable<number> | ArrayLike<number>
optional mapfn(v: number, k: number) => number
optional thisArgany
Return ValueIOBuffer

IOBuffer.from(arrayLike, mapfn, thisArg) static

Inherited Uint8Array.from form with a map function.

static from<T>(
  arrayLike: Iterable<T> | ArrayLike<T>,
  mapfn: (v: T, k: number) => number,
  thisArg?: any,
): IOBuffer;
NameTypeDescription
arrayLikeIterable<T> | ArrayLike<T>
mapfn(v: T, k: number) => number
optional thisArgany
Return ValueIOBuffer

IOBuffer.isBuffer(value) static

Returns whether a value is an IOBuffer.

static isBuffer(value: any): boolean;
NameTypeDescription
valueany
Return Valueboolean

slice(start, end)

Returns a new IOBuffer over a byte range (a view, like Uint8Array.slice).

slice(start?: number, end?: number): IOBuffer;
NameTypeDescription
optional startnumber
optional endnumber
Return ValueIOBuffer

toString(encoding, start, end)

Decodes (a range of) the buffer to a string.

toString(encoding?: IOBufferEncoding, start?: number, end?: number): string;
NameTypeDescription
optional encodingIOBufferEncodingthe output encoding (default "utf8").
optional startnumberstart byte offset.
optional endnumberend byte offset (exclusive).
Return Valuestring

toUint8Array()

Returns a plain Uint8Array view of the same bytes.

toUint8Array(): Uint8Array;
NameTypeDescription
Return ValueUint8Array

type IOBufferEncoding

Encodings accepted by buffer.IOBuffer construction and conversion.

type IOBufferEncoding = "utf8" | "ascii" | "base64" | "hex";