Class CborByteEncoderStream

A TransformStream that encodes a ReadableStream into CBOR "Indefinite Length Byte String". RFC 8949 - Concise Binary Object Representation (CBOR)

Notice: Each chunk of the ReadableStream is encoded as its own "Definite Length Byte String" meaning space can be saved if large chunks are pipped through instead of small chunks.

import { assert, assertEquals } from "@std/assert";
import { concat } from "@std/bytes";
import {
CborByteDecodedStream,
CborByteEncoderStream,
CborSequenceDecoderStream,
} from "@std/cbor";

const rawMessage = new Uint8Array(100);

for await (
const value of ReadableStream.from([rawMessage])
.pipeThrough(new CborByteEncoderStream())
.pipeThrough(new CborSequenceDecoderStream())
) {
assert(value instanceof Uint8Array || value instanceof CborByteDecodedStream);
if (value instanceof CborByteDecodedStream) {
assertEquals(concat(await Array.fromAsync(value)), new Uint8Array(100));
} else assertEquals(value, new Uint8Array(100));
}
Implements
  • TransformStream<Uint8Array, Uint8Array>

Constructors

Accessors

Methods

Constructors

Accessors

  • get readable(): ReadableStream<Uint8Array>
  • The ReadableStream associated with the instance, which provides the encoded CBOR data as Uint8Array chunks.

    Returns ReadableStream<Uint8Array>

    A ReadableStream.

    import { assert, assertEquals } from "@std/assert";
    import { concat } from "@std/bytes";
    import {
    CborByteDecodedStream,
    CborByteEncoderStream,
    CborSequenceDecoderStream,
    } from "@std/cbor";

    const rawMessage = new Uint8Array(100);

    for await (
    const value of ReadableStream.from([rawMessage])
    .pipeThrough(new CborByteEncoderStream())
    .pipeThrough(new CborSequenceDecoderStream())
    ) {
    assert(value instanceof Uint8Array || value instanceof CborByteDecodedStream);
    if (value instanceof CborByteDecodedStream) {
    assertEquals(concat(await Array.fromAsync(value)), new Uint8Array(100));
    } else assertEquals(value, new Uint8Array(100));
    }
  • get writable(): WritableStream<Uint8Array>
  • The WritableStream associated with the instance, which accepts Uint8Array chunks to be encoded into CBOR format.

    Returns WritableStream<Uint8Array>

    a WritableStream.

    import { assert, assertEquals } from "@std/assert";
    import { concat } from "@std/bytes";
    import {
    CborByteDecodedStream,
    CborByteEncoderStream,
    CborSequenceDecoderStream,
    } from "@std/cbor";

    const rawMessage = new Uint8Array(100);

    for await (
    const value of ReadableStream.from([rawMessage])
    .pipeThrough(new CborByteEncoderStream())
    .pipeThrough(new CborSequenceDecoderStream())
    ) {
    assert(value instanceof Uint8Array || value instanceof CborByteDecodedStream);
    if (value instanceof CborByteDecodedStream) {
    assertEquals(concat(await Array.fromAsync(value)), new Uint8Array(100));
    } else assertEquals(value, new Uint8Array(100));
    }

Methods

  • Creates a CborByteEncoderStream instance from an iterable of Uint8Array chunks.

    Parameters

    • asyncIterable: AsyncIterable<Uint8Array, any, any> | Iterable<Uint8Array, any, any>

      The value to encode of type AsyncIterable or Iterable.

    Returns CborByteEncoderStream

    A CborByteEncoderStream instance of the encoded data.

    import { assert, assertEquals } from "@std/assert";
    import { concat } from "@std/bytes";
    import {
    CborByteDecodedStream,
    CborByteEncoderStream,
    CborSequenceDecoderStream,
    } from "@std/cbor";

    const rawMessage = new Uint8Array(100);

    for await (
    const value of CborByteEncoderStream.from([rawMessage])
    .readable
    .pipeThrough(new CborSequenceDecoderStream())
    ) {
    assert(value instanceof Uint8Array || value instanceof CborByteDecodedStream);
    if (value instanceof CborByteDecodedStream) {
    assertEquals(concat(await Array.fromAsync(value)), new Uint8Array(100));
    } else assertEquals(value, new Uint8Array(100));
    }