Struct provides a builder-like interface to create Buffer-based memory structures for read/write interfacing with data structures from adapters.

Implements

Methods

  • Creates the struct and optionally fills it with data. If data is provided, the length of the provided buffer needs to match the structure length.

    This method is stripped from type on struct build().

    Parameters

    • Optionaldata: Buffer<ArrayBufferLike>

    Returns BuiltStruct<Struct>

  • Sets default data to initialize empty struct with.

    Parameters

    • data: Buffer<ArrayBufferLike>

      Data to initialize empty struct with.

    Returns this

  • Adds a numeric member of uint8, uint16 or uint32 type. Internal representation is always little endian.

    This method is stripped from type on struct build().

    Type Parameters

    • T extends number
    • N extends string
    • R extends Struct & Record<N, T>

    Parameters

    • type: "uint8" | "uint16" | "uint32"

      Underlying data type (uint8, uint16 or uint32).

    • name: N

      Name of the struct member.

    Returns R

  • Adds an uint8 array (byte array) as a struct member.

    This method is stripped from type on struct build().

    Type Parameters

    • T extends Buffer<ArrayBufferLike>
    • N extends string
    • R extends Struct & Record<N, T>

    Parameters

    • type: "uint8array" | "uint8array-reversed"

      Underlying data type. Must be uint8array.

    • name: N

      Name of the struct member.

    • length: number

      Length of the byte array.

    Returns R

  • Adds another struct type as a struct member. Struct factory is provided as a child struct definition source.

    This method is stripped from type on struct build().

    Type Parameters

    Parameters

    • type: "struct"

      Underlying data type. Must be struct.

    • name: N

      Name of the struct member.

    • structFactory: StructFactorySignature<T>

      Factory providing the wanted child struct.

    Returns R

  • Adds a custom method to the struct.

    This method is stripped from type on struct build().

    Type Parameters

    • T
    • N extends string
    • R extends Struct & Record<N, () => T>

    Parameters

    • name: N

      Name of the method to be appended.

    • returnType: T

      Return type (eg. Buffer.prototype).

    • body: (struct: R) => T

      Function implementation. Takes struct as a first and single input parameter.

    Returns R

  • Creates an empty struct. Further calls to member() and method() functions will form the structure. Finally call to build() will type the resulting structure appropriately without internal functions.

    Returns Struct