• @Size decorator reads data until the specified size in bytes is met.

    This decorator is useful when you need to process a fixed-size amount of data, either in bytes or based on another property that dynamically defines the size in bytes to read.

    Binary format definitions often specify the size of sections in bytes. These sections can have components of dynamic size, making it difficult to deduce the exact number of components.

    Type Parameters

    • This extends object

      The type of the class the decorator is applied to.

    • Value

      The type of the decorated property.

    • Args extends string

    Parameters

    • size: NumberOrRecursiveKey<This, Args>

      The fixed size or a property path that defines the size dynamically.

    • Optionalopt: Partial<ControllerOptions>

      Optional configuration.

    Returns DecoratorType<This, Value>

    The property decorator function.

    In the following example, the @Size decorator is used to read a specific number of bytes from a binary data stream into the decorated property.

    class Protocol {
    @Size(16)
    @Uint16
    data: number[] // Will contain 8 numbers
    }

    You can also use a string representing a property path to define the size dynamically.

    class Protocol {
    _size: number = 16

    @Size('_size')
    @Uint16
    data: number[]
    }

    A simple literal arithmetic expression can also be used by the @Size decorator.

    In the following example the length property include the terminator to count the string length. The @Size decorator substract that number from the size.

    class Protocol {
    @Uint32
    length: number

    @Size('length - 1')
    @Ascii
    data: string

    @Match(0)
    @Uint8
    terminator: number
    }