NodeOPCUA API Documentation
    Preparing search index...

    Class BinaryStream

    a BinaryStream can be use to perform sequential read or write inside a buffer. The BinaryStream maintains a cursor up to date as the caller operates on the stream using the various read/write methods. It uses the Little Endian It uses the Little Endian convention.

    data can either be:

    • a Buffer , in this case the BinaryStream operates on this Buffer
    • null , in this case a BinaryStream with 1024 bytes is created
    • any data , in this case the object is converted into a binary buffer.

    example:

    var stream = new BinaryStream(32)
    
    Index
    • Parameters

      • Optionaldata: number | Buffer<ArrayBufferLike>

      Returns BinaryStream

    length: number

    the current position inside the buffer

    maxArrayLength: number

    hard ceiling on the number of elements a single array length prefix may declare.

    The length is read straight off the wire as a UInt32, so without a ceiling a tiny message can declare billions of elements and drive the decoder to loop and allocate to match. Mirrors the cap the Variant value path has always enforced (Variant.maxArrayLength); the generic path that decodes every structured-type array field never had one. Adjustable so a deployment that legitimately exchanges larger arrays can raise it.

    maxByteStringLength: number
    maxNestingLevel: number

    how deep decoders may nest recursive types (ExtensionObject, Variant, DiagnosticInfo) while reading from this stream.

    These types can nest without ever making the message larger than the negotiated limit, so a message well under the size cap can still drive the decoder deep enough to exhaust the call stack. OPC UA Part 6 §5.1.8/§5.1.9 anticipates this and requires a decoder to support at least 100 levels and to report an error beyond what it supports; §5.2.2.12 says the same for the self-recursive DiagnosticInfo. One shared budget across the three types bounds what actually matters - total stack depth - regardless of how a message mixes them.

    Set to 128, comfortably above the 100 the spec requires us to support (so a message that legitimately nests 100 deep still decodes whichever way you count the outermost level) and far below the depth at which the call stack would actually overflow.

    maxStringLength: number
    • validate a wire array length before a decoder loops over it.

      Two independent bounds:

      • it may not exceed BinaryStream.maxArrayLength;
      • it may not exceed the bytes left in the stream, since every encoded element occupies at least one byte. This tight check alone rejects an implausible length (0x7FFFFFFE elements behind a 100-byte body) immediately, and also catches a length that sits under the ceiling yet is still far larger than the remaining payload can back.

      Parameters

      • length: number

        the element count just read from the stream

      Returns void

    • signal that the decoder is about to descend one level into a recursive type. Throws BinaryStreamMaxNestingLevelExceededError once the depth passes BinaryStream.maxNestingLevel, before the recursive call is made and before any further stack frame is consumed. Every successful call must be paired with exitNestingLevel, which is why callers use try/finally.

      Returns void

    • mark that the decoder has finished one level of a recursive type. Pair with a preceding successful enterNestingLevel via try/finally so the depth is restored even when the nested decode throws.

      Returns void

    • write a 32 bit unsigned integer at an absolute position previously returned by reserveUInt32, without moving the cursor.

      Parameters

      • position: number
      • value: number

      Returns void

    • Parameters

      • length: number

      Returns Uint8Array

    • Parameters

      • length: number

      Returns Buffer

    • read a single signed byte (8 bits) from the stream.

      Returns number

      the value read

    • read a byte stream to the stream. The method reads the length of the byte array from the stream as a 32 bits integer before reading the byte stream.

      Returns Buffer<ArrayBufferLike> | null

    • read a single 64-bit floating point number from the stream.

      Returns number

    • read a single 32-bit floating point number from the stream.

      Returns number

    • read a single signed 16-bit integer from the stream.

      Returns number

    • Returns number

    • read a single signed 32-bit integer from the stream.

      Returns number

    • Returns string | null

    • read a single unsigned 16-bit integer from the stream.

      Returns number

    • read a single unsigned 32-bit integer from the stream.

      Returns number

    • read a single unsigned byte (8 bits) from the stream.

      Returns number

    • reserve room for a 32 bit unsigned integer whose value is not known yet, and return the position at which it can later be written with patchUInt32.

      This lets a length-prefixed body be written in a single pass: reserve the slot, encode the body, then patch in the byte count. The alternative - computing the size up front - means encoding the body twice, which compounds for nested structures.

      Returns number

    • set the cursor to the begining of the stream

      Returns void

    • Parameters

      • arrayBuf: ArrayBuffer

        a buffer or byte array write

      • Optionaloffset: number

        the offset position (default =0)

      • Optionallength: number

        the number of byte to write

      Returns void

    • write a byte stream to the stream. The method writes the length of the byte array into the stream as a 32 bits integer before the byte stream.

      Parameters

      • buf: Buffer

        the buffer to write.

      Returns void

    • write a single 64 bit floating number to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • write a single 32 bit floating number to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • write a single 16 bit signed integer to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • write a single signed byte (8 bits) to the stream. value must be in the range of [-127,128]

      Parameters

      • value: number

        the value to write

      Returns void

    • write a single 32 bit signed integer to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • Parameters

      • value: string | null

      Returns void

    • write a single 16 bit unsigned integer to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • write a single 32 bit unsigned integer to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • write a single unsigned byte (8 bits) to the stream.

      Parameters

      • value: number

        the value to write

      Returns void

    • create a stream that reallocates its buffer as needed, up to maxLength bytes.

      Encoding a message whose size is not known up front otherwise means encoding it twice: once into a BinaryStreamSizeCalculator to learn the length, then again for real. Both passes walk the whole object graph, and measuring shows the sizing pass costs about as much as the real one.

      Parameters

      • initialSize: number

        starting capacity; a good guess avoids reallocation entirely

      • maxLength: number

        hard ceiling - exceeding it throws BinaryStreamMaxSizeExceededError

      Returns BinaryStream