CborTextEncoderStream

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

class

class CborTextEncoderStream implements TransformStream<string, Uint8Array>

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

Notice: Each chunk of the ReadableStream is encoded as its own "Definite Length Text 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 {
  CborSequenceDecoderStream,
  CborTextDecodedStream,
  CborTextEncoderStream,
} from "@ende/cbor";

const rawMessage = "a".repeat(100);

for await (
  const value of ReadableStream.from([rawMessage])
    .pipeThrough(new CborTextEncoderStream())
    .pipeThrough(new CborSequenceDecoderStream())
) {
  assert(typeof value === "string" || value instanceof CborTextDecodedStream);
  if (value instanceof CborTextDecodedStream) {
    assertEquals((await Array.fromAsync(value)).join(""), rawMessage);
  } else assertEquals(value, rawMessage);
}

Constructors

constructor

new CborTextEncoderStream()

Constructs a new instance.

Static methods

from

static from(asyncIterable: AsyncIterable<string> | Iterable<string>): CborTextEncoderStream

Creates a CborTextEncoderStream instance from an iterable of string chunks.

asyncIterable: AsyncIterable<string> | Iterable<string>

The value to encode of type AsyncIterable or Iterable

Returns

A CborTextEncoderStream instance of the encoded data.

Example: Usage

import { assert, assertEquals } from "@ende/assert";
import {
  CborSequenceDecoderStream,
  CborTextDecodedStream,
  CborTextEncoderStream,
} from "@ende/cbor";

const rawMessage = "a".repeat(100);

for await (
  const value of CborTextEncoderStream.from([rawMessage])
    .readable
    .pipeThrough(new CborSequenceDecoderStream())
) {
  assert(typeof value === "string" || value instanceof CborTextDecodedStream);
  if (value instanceof CborTextDecodedStream) {
    assertEquals((await Array.fromAsync(value)).join(""), rawMessage);
  } else assertEquals(value, rawMessage);
}

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 {
  CborSequenceDecoderStream,
  CborTextDecodedStream,
  CborTextEncoderStream,
} from "@ende/cbor";

const rawMessage = "a".repeat(100);

for await (
  const value of ReadableStream.from([rawMessage])
    .pipeThrough(new CborTextEncoderStream())
    .pipeThrough(new CborSequenceDecoderStream())
) {
  assert(typeof value === "string" || value instanceof CborTextDecodedStream);
  if (value instanceof CborTextDecodedStream) {
    assertEquals((await Array.fromAsync(value)).join(""), rawMessage);
  } else assertEquals(value, rawMessage);
}

writable

get writable(): WritableStream<string>

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

Returns

A WritableStream.

Example: Usage

import { assert, assertEquals } from "@ende/assert";
import {
  CborSequenceDecoderStream,
  CborTextDecodedStream,
  CborTextEncoderStream,
} from "@ende/cbor";

const rawMessage = "a".repeat(100);

for await (
  const value of ReadableStream.from([rawMessage])
    .pipeThrough(new CborTextEncoderStream())
    .pipeThrough(new CborSequenceDecoderStream())
) {
  assert(typeof value === "string" || value instanceof CborTextDecodedStream);
  if (value instanceof CborTextDecodedStream) {
    assertEquals((await Array.fromAsync(value)).join(""), rawMessage);
  } else assertEquals(value, rawMessage);
}

Source