Class CborTextEncoderStream

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.

import { assert, assertEquals } from "@std/assert";
import {
CborSequenceDecoderStream,
CborTextDecodedStream,
CborTextEncoderStream,
} from "@std/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);
}
Implements
  • TransformStream<string, 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 {
    CborSequenceDecoderStream,
    CborTextDecodedStream,
    CborTextEncoderStream,
    } from "@std/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);
    }
  • get writable(): WritableStream<string>
  • The WritableStream associated with the instance, which accepts string chunks to be encoded into CBOR format.

    Returns WritableStream<string>

    A WritableStream.

    import { assert, assertEquals } from "@std/assert";
    import {
    CborSequenceDecoderStream,
    CborTextDecodedStream,
    CborTextEncoderStream,
    } from "@std/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);
    }

Methods

  • Creates a CborTextEncoderStream instance from an iterable of string chunks.

    Parameters

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

      The value to encode of type AsyncIterable or Iterable

    Returns CborTextEncoderStream

    A CborTextEncoderStream instance of the encoded data.

    import { assert, assertEquals } from "@std/assert";
    import {
    CborSequenceDecoderStream,
    CborTextDecodedStream,
    CborTextEncoderStream,
    } from "@std/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);
    }