Options
All
  • Public
  • Public/Protected
  • All
Menu

@solana/buffer-layout

Support for translating between Uint8Array instances and JavaScript native types.

{@link module:Layout~Layout|Layout} is the basis of a class hierarchy that associates property names with sequences of encoded bytes.

Layouts are supported for these scalar (numeric) types:

  • {@link module:Layout~UInt|Unsigned integers in little-endian format} with {@link module:Layout.u8|8-bit}, {@link module:Layout.u16|16-bit}, {@link module:Layout.u24|24-bit}, {@link module:Layout.u32|32-bit}, {@link module:Layout.u40|40-bit}, and {@link module:Layout.u48|48-bit} representation ranges;
  • {@link module:Layout~UIntBE|Unsigned integers in big-endian format} with {@link module:Layout.u16be|16-bit}, {@link module:Layout.u24be|24-bit}, {@link module:Layout.u32be|32-bit}, {@link module:Layout.u40be|40-bit}, and {@link module:Layout.u48be|48-bit} representation ranges;
  • {@link module:Layout~Int|Signed integers in little-endian format} with {@link module:Layout.s8|8-bit}, {@link module:Layout.s16|16-bit}, {@link module:Layout.s24|24-bit}, {@link module:Layout.s32|32-bit}, {@link module:Layout.s40|40-bit}, and {@link module:Layout.s48|48-bit} representation ranges;
  • {@link module:Layout~IntBE|Signed integers in big-endian format} with {@link module:Layout.s16be|16-bit}, {@link module:Layout.s24be|24-bit}, {@link module:Layout.s32be|32-bit}, {@link module:Layout.s40be|40-bit}, and {@link module:Layout.s48be|48-bit} representation ranges;
  • 64-bit integral values that decode to an exact (if magnitude is less than 2^53) or nearby integral Number in {@link module:Layout.nu64|unsigned little-endian}, {@link module:Layout.nu64be|unsigned big-endian}, {@link module:Layout.ns64|signed little-endian}, and {@link module:Layout.ns64be|unsigned big-endian} encodings;
  • 32-bit floating point values with {@link module:Layout.f32|little-endian} and {@link module:Layout.f32be|big-endian} representations;
  • 64-bit floating point values with {@link module:Layout.f64|little-endian} and {@link module:Layout.f64be|big-endian} representations;
  • {@link module:Layout.const|Constants} that take no space in the encoded expression.

and for these aggregate types:

  • {@link module:Layout.seq|Sequence}s of instances of a {@link module:LayoutLayout|Layout}, with JavaScript representation as an Array and constant or data-dependent {@link module:LayoutSequence#count|length};
  • {@link module:Layout.struct|Structure}s that aggregate a heterogeneous sequence of {@link module:Layout~Layout|Layout} instances, with JavaScript representation as an Object;
  • {@link module:Layout.union|Union}s that support multiple {@link module:Layout~VariantLayout|variant layouts} over a fixed (padded) or variable (not padded) span of bytes, using an unsigned integer at the start of the data or a separate {@link module:Layout.unionLayoutDiscriminator|layout element} to determine which layout to use when interpreting the buffer contents;
  • {@link module:Layout.bits|BitStructure}s that contain a sequence of individual {@link module:Layout~BitStructure#addField|BitField}s packed into an 8, 16, 24, or 32-bit unsigned integer starting at the least- or most-significant bit;
  • {@link module:Layout.cstr|C strings} of varying length;
  • {@link module:Layout.blob|Blobs} of fixed- or variable-{@link module:Layout~Blob#length|length} raw data.

All {@link module:Layout~Layout|Layout} instances are immutable after construction, to prevent internal state from becoming inconsistent.

local

Layout

local

ExternalLayout

local

GreedyCount

local

OffsetLayout

local

UInt

local

UIntBE

local

Int

local

IntBE

local

NearUInt64

local

NearUInt64BE

local

NearInt64

local

NearInt64BE

local

Float

local

FloatBE

local

Double

local

DoubleBE

local

Sequence

local

Structure

local

UnionDiscriminator

local

UnionLayoutDiscriminator

local

Union

local

VariantLayout

local

BitStructure

local

BitField

local

Boolean

local

Blob

local

CString

local

Constant

local

bindConstructorLayout

license

MIT

author

Peter A. Bigot

see

buffer-layout on GitHub

Index

Functions

bindConstructorLayout

  • bindConstructorLayout<T>(Class: any, layout: Layout<T>): void
  • Augment a class so that instances can be encoded/decoded using a given layout.

    Calling this function couples Class with layout in several ways:

    • Class.layout_ becomes a static member property equal to layout;
    • layout.boundConstructor_ becomes a static member property equal to Class;
    • The {@link Layout#makeDestinationObject|makeDestinationObject()} property of layout is set to a function that returns a new Class();
    • Class.decode(b, offset) becomes a static member function that delegates to {@link Layout#decode|layout.decode}. The synthesized function may be captured and extended.
    • Class.prototype.encode(b, offset) provides an instance member function that delegates to {@link Layout#encode|layout.encode} with src set to this. The synthesized function may be captured and extended, but when the extension is invoked this must be explicitly bound to the instance.

    Type parameters

    • T

    Parameters

    • Class: any

      a JavaScript class with a nullary constructor.

    • layout: Layout<T>

      the Layout instance used to encode instances of Class.

    Returns void

Const bits

Const blob

checkUint8Array

  • checkUint8Array(b: Uint8Array): void

Const constant

  • constant<T>(value: T, property?: string): Constant<T>

Const cstr

Const f32

  • f32(property?: string): Float

Const f32be

Const f64

  • f64(property?: string): Double

Const f64be

Const greedy

  • greedy(elementSpan: number, property?: string): GreedyCount

nameWithProperty

  • nameWithProperty(name: string, lo: { property?: string }): string
  • Parameters

    • name: string
    • lo: { property?: string }
      • Optional property?: string

    Returns string

Const ns64

Const ns64be

Const nu64

Const nu64be

Const offset

Const s16

  • s16(property?: string): Int

Const s16be

  • s16be(property?: string): IntBE

Const s24

  • s24(property?: string): Int

Const s24be

  • s24be(property?: string): IntBE

Const s32

  • s32(property?: string): Int

Const s32be

  • s32be(property?: string): IntBE

Const s40

  • s40(property?: string): Int

Const s40be

  • s40be(property?: string): IntBE

Const s48

  • s48(property?: string): Int

Const s48be

  • s48be(property?: string): IntBE

Const s8

  • s8(property?: string): Int

Const seq

Const struct

  • struct<T>(fields: Layout<T[keyof T]>[], property?: string, decodePrefixes?: boolean): Structure<T>

Const u16

  • u16(property?: string): UInt

Const u16be

  • u16be(property?: string): UIntBE

Const u24

  • u24(property?: string): UInt

Const u24be

  • u24be(property?: string): UIntBE

Const u32

  • u32(property?: string): UInt

Const u32be

  • u32be(property?: string): UIntBE

Const u40

  • u40(property?: string): UInt

Const u40be

  • u40be(property?: string): UIntBE

Const u48

  • u48(property?: string): UInt

Const u48be

  • u48be(property?: string): UIntBE

Const u8

  • u8(property?: string): UInt

uint8ArrayToBuffer

  • uint8ArrayToBuffer(b: Uint8Array): Buffer

Const union

Const unionLayoutDiscriminator

Const utf8

  • utf8(maxSpan: number, property?: string): UTF8
  • Factory for UTF8 values.

    Parameters

    • maxSpan: number
    • Optional property: string

    Returns UTF8

Generated using TypeDoc