function createBaichuanDecodeStream
createBaichuanDecodeStream(): TransformStream<Uint8Array, BaichuanMessage>

Creates a TransformStream that frames a Baichuan byte stream into messages.

Chunks may split or join messages at any byte; the stream buffers until a whole message is in. The body is split from the payload at header.payloadOffset, where 0 or absent means the whole rest is body. Bodies stay encrypted, see decryptBody in @hertzg/reolink-api/protocol/message.

The stream errors when a message does not start with the magic, and when the input ends partway through a message.

Examples

Decode two messages from one chunk

import { assertEquals } from "@std/assert";
import { decodeHex } from "@std/encoding/hex";
import { createBaichuanDecodeStream } from "@hertzg/reolink-api/streams/decode";

const reply = decodeHex("f0debc0a1f00000000000000fb030000c800146400000000");

const messages = await Array.fromAsync(
  ReadableStream.from([new Uint8Array([...reply, ...reply])])
    .pipeThrough(createBaichuanDecodeStream()),
);

assertEquals(messages.length, 2);

Return Type

A TransformStream from bytes to messages.

Usage

import { createBaichuanDecodeStream } from "streams/decode.ts";