CborByteEncoderStream

import { CborByteEncoderStream } from "jsr:@ende/cbor";

class

class CborByteEncoderStream implements TransformStream<Uint8Array, Uint8Array>

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.

Example: Usage

import { assert, assertEquals } from "@ende/assert";
import { concat } from "@ende/bytes";
import {
  CborByteDecodedStream,
  CborByteEncoderStream,
  CborSequenceDecoderStream,
} from "@ende/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));
}

Constructors

constructor

new CborByteEncoderStream()

Constructs a new instance.

Static methods

from

static from(asyncIterable: AsyncIterable<Uint8Array> | Iterable<Uint8Array>): CborByteEncoderStream

Creates a CborByteEncoderStream instance from an iterable of Uint8Array chunks.

asyncIterable: AsyncIterable<Uint8Array> | Iterable<Uint8Array>

The value to encode of type AsyncIterable or Iterable.

Returns

A CborByteEncoderStream instance of the encoded data.

Example: Usage

import { assert, assertEquals } from "@ende/assert";
import { concat } from "@ende/bytes";
import {
  CborByteDecodedStream,
  CborByteEncoderStream,
  CborSequenceDecoderStream,
} from "@ende/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));
}

Methods

readable

get readable(): ReadableStream<Uint8Array>

The ReadableStream associated with the instance, which provides the encoded CBOR data as Uint8Array chunks.

Returns

A ReadableStream.

Example: Usage

import { assert, assertEquals } from "@ende/assert";
import { concat } from "@ende/bytes";
import {
  CborByteDecodedStream,
  CborByteEncoderStream,
  CborSequenceDecoderStream,
} from "@ende/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));
}

writable

get writable(): WritableStream<Uint8Array>

The WritableStream associated with the instance, which accepts Uint8Array chunks to be encoded into CBOR format.

Returns

a WritableStream.

Example: Usage

import { assert, assertEquals } from "@ende/assert";
import { concat } from "@ende/bytes";
import {
  CborByteDecodedStream,
  CborByteEncoderStream,
  CborSequenceDecoderStream,
} from "@ende/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));
}

Source