Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Type<VALUE, READ_VALUE>

An interface representing an object that can serialize values of a certain type. The object must also be able to serialize itself.

Type parameters

  • VALUE

    The type of values this object can serialize

  • READ_VALUE: VALUE = VALUE

    The type of values this object deserializes. Must be a subset of VALUE. Defaults to VALUE.

Hierarchy

  • Type

Index

Methods

addToBuffer

  • Appends the type information to an AppendableBuffer

    Parameters

    Returns boolean

    false if it wrote a pointer to a previous instance, true if it wrote the type byte. For internal use.

consumeValue

  • consumeValue(buffer: ArrayBuffer, offset: number, baseValue?: unknown): ReadResult<READ_VALUE>
  • Reads a value from the specified bytes at the specified offset. Returns the value that was read and the number of bytes consumed.

    Parameters

    • buffer: ArrayBuffer

      The buffer containing the value bytes

    • offset: number

      The position in the buffer to read from

    • Optional baseValue: unknown

      A value to mutate into the read value. Used by RecursiveType.

    Returns ReadResult<READ_VALUE>

    The read value and the number of bytes read

equals

  • equals(otherType: unknown): boolean
  • Returns whether this type object represents the same type as another object

    Parameters

    • otherType: unknown

      Another object to compare with

    Returns boolean

    true iff the types would be serialized to the same bytes

getHash

  • getHash(): string
  • Gets a base-64 SHA256 hash of the type, using a cached value if present

    Returns string

    A hash of the buffer given by toBuffer

getSignature

  • getSignature(): string
  • Gets a signature string for the type, using a cached value if present. This string encodes the specification version and the type hash. Represented as a base-64 string.

    Returns string

    A signature for the type

readValue

  • readValue(buffer: ArrayBuffer | Uint8Array, offset?: number): READ_VALUE
  • Deserializes a value, i.e. takes a buffer containing its binary form and returns the value. The inverse of valueBuffer. Requires the type (this) to be known.

    Example:

    let type = new sb.ArrayType(
      new sb.FlexUnsignedIntType
    )
    let value = [0, 10, 100, 1000, 10000]
    let buffer = type.valueBuffer(value)
    let readValue = type.readValue(buffer)
    console.log(readValue) // [ 0, 10, 100, 1000, 10000 ]
    
    throws

    If the value does not occupy all of the buffer from index offset to the end

    Parameters

    • buffer: ArrayBuffer | Uint8Array

      The buffer containing the value bytes

    • Optional offset: number

      The position in the buffer to read from (defaults to 0)

    Returns READ_VALUE

    A value equivalent to the one that was written

toBuffer

  • toBuffer(): ArrayBuffer
  • Gets the type in buffer form, using a cached value if present. Since types are immutable, the result should never change from the cached value.

    Returns ArrayBuffer

    A buffer containing the type bytes

valueBuffer

  • valueBuffer(value: VALUE): ArrayBuffer
  • Gets an ArrayBuffer containing the value in binary format. See this type's writeValue documentation for examples of values.

    Parameters

    • value: VALUE

      The value to write

    Returns ArrayBuffer

    An ArrayBuffer storing the value

writeValue

  • Appends value bytes to an AppendableBuffer according to the type

    throws

    If the value doesn't match the type, e.g. new sb.StringType().writeValue(buffer, 23)

    Parameters

    • buffer: AppendableBuffer

      The buffer to which to append

    • value: VALUE

      The value to write

    Returns void

Generated using TypeDoc