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

Implements

Methods

  • Returns total length of the struct. Struct length is always fixed and configured by calls to member() methods.

    Parameters

    Returns number

  • 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
    • 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.

        • (struct): T
        • Parameters

          • struct: R

          Returns T

    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