CborSequenceDecoderStream
import { CborSequenceDecoderStream } from "jsr:@ende/cbor";class
class CborSequenceDecoderStream implements TransformStream<Uint8Array, CborStreamOutput>A TransformStream that decodes a CBOR-sequence-encoded
ReadableStream
Limitations:
- While CBOR does support map keys of any type, this implementation only supports map keys being of type string, and will throw if detected decoding otherwise.
- This decoder does not validate that the encoded data is free of duplicate map keys, and will serve them all. This behaviour differentiates from decodeCbor and decodeCborSequence.
- Arrays and Maps will always be decoded as a CborArrayDecodedStream and CborMapDecodedStream, respectively.
- "Byte Strings" and "Text Strings" will be decoded as a CborByteDecodedStream and CborTextDecodedStream, respectively, if they are encoded as an "Indefinite Length String" or their "Definite Length" is 2 ** 32 and 2 ** 16, respectively, or greater.
Notice:
- This decoder handles the tag numbers 0, and 1 automatically, all
others returned are wrapped in a CborTag
instance. - If a parent stream yields CborByteDecodedStream, CborTextDecodedStream, CborArrayDecodedStream, CborMapDecodedStream, or CborTag (with any of these types as content), it will not resolve the next chunk until the yielded stream is fully consumed or canceled.
Example: Usage
import { encodeBase64Url } from "@ende/encoding";
import {
CborArrayDecodedStream,
CborArrayEncoderStream,
CborByteDecodedStream,
CborByteEncoderStream,
CborMapDecodedStream,
CborMapEncoderStream,
type CborStreamOutput,
CborSequenceDecoderStream,
CborSequenceEncoderStream,
CborTag,
CborTextDecodedStream,
CborTextEncoderStream,
} from "@ende/cbor";
const rawMessage = [
undefined,
null,
true,
false,
3.14,
5,
2n ** 32n,
"Hello World",
new Uint8Array(25),
new Date(),
new CborTag(33, encodeBase64Url(new Uint8Array(7))),
["cake", "carrot"],
{ a: 3, b: "d" },
CborByteEncoderStream.from([new Uint8Array(7)]),
CborTextEncoderStream.from(["Bye!"]),
CborArrayEncoderStream.from([
"Hey!",
CborByteEncoderStream.from([new Uint8Array(18)]),
]),
CborMapEncoderStream.from([
["a", 0],
["b", "potato"],
]),
];
async function logValue(value: CborStreamOutput) {
if (
value instanceof CborByteDecodedStream ||
value instanceof CborTextDecodedStream
) {
for await (const x of value) console.log(x);
} else if (value instanceof CborArrayDecodedStream) {
for await (const x of value) logValue(x);
} else if (value instanceof CborMapDecodedStream) {
for await (const [k, v] of value) {
console.log(k);
logValue(v);
}
} else if (value instanceof CborTag) {
console.log(value);
logValue(value.tagContent);
} else console.log(value);
}
for await (
const value of ReadableStream.from(rawMessage)
.pipeThrough(new CborSequenceEncoderStream())
.pipeThrough(new CborSequenceDecoderStream())
) {
logValue(value);
}
Constructors
constructor
new CborSequenceDecoderStream()Constructs a new instance.
Methods
readable
get readable(): ReadableStream<CborStreamOutput>The ReadableStream
Returns
A ReadableStream
Example: Usage
import { encodeBase64Url } from "@ende/encoding";
import {
CborArrayDecodedStream,
CborArrayEncoderStream,
CborByteDecodedStream,
CborByteEncoderStream,
CborMapDecodedStream,
CborMapEncoderStream,
type CborStreamOutput,
CborSequenceDecoderStream,
CborSequenceEncoderStream,
CborTag,
CborTextDecodedStream,
CborTextEncoderStream,
} from "@ende/cbor";
const rawMessage = [
undefined,
null,
true,
false,
3.14,
5,
2n ** 32n,
"Hello World",
new Uint8Array(25),
new Date(),
new CborTag(33, encodeBase64Url(new Uint8Array(7))),
["cake", "carrot"],
{ a: 3, b: "d" },
CborByteEncoderStream.from([new Uint8Array(7)]),
CborTextEncoderStream.from(["Bye!"]),
CborArrayEncoderStream.from([
"Hey!",
CborByteEncoderStream.from([new Uint8Array(18)]),
]),
CborMapEncoderStream.from([
["a", 0],
["b", "potato"],
]),
];
async function logValue(value: CborStreamOutput) {
if (
value instanceof CborByteDecodedStream ||
value instanceof CborTextDecodedStream
) {
for await (const x of value) console.log(x);
} else if (value instanceof CborArrayDecodedStream) {
for await (const x of value) logValue(x);
} else if (value instanceof CborMapDecodedStream) {
for await (const [k, v] of value) {
console.log(k);
logValue(v);
}
} else if (value instanceof CborTag) {
console.log(value);
logValue(value.tagContent);
} else console.log(value);
}
for await (
const value of ReadableStream.from(rawMessage)
.pipeThrough(new CborSequenceEncoderStream())
.pipeThrough(new CborSequenceDecoderStream())
) {
logValue(value);
}
writable
get writable(): WritableStream<Uint8Array>The WritableStream
Returns
A WritableStream
Example: Usage
import { encodeBase64Url } from "@ende/encoding";
import {
CborArrayDecodedStream,
CborArrayEncoderStream,
CborByteDecodedStream,
CborByteEncoderStream,
CborMapDecodedStream,
CborMapEncoderStream,
type CborStreamOutput,
CborSequenceDecoderStream,
CborSequenceEncoderStream,
CborTag,
CborTextDecodedStream,
CborTextEncoderStream,
} from "@ende/cbor";
const rawMessage = [
undefined,
null,
true,
false,
3.14,
5,
2n ** 32n,
"Hello World",
new Uint8Array(25),
new Date(),
new CborTag(33, encodeBase64Url(new Uint8Array(7))),
["cake", "carrot"],
{ a: 3, b: "d" },
CborByteEncoderStream.from([new Uint8Array(7)]),
CborTextEncoderStream.from(["Bye!"]),
CborArrayEncoderStream.from([
"Hey!",
CborByteEncoderStream.from([new Uint8Array(18)]),
]),
CborMapEncoderStream.from([
["a", 0],
["b", "potato"],
]),
];
async function logValue(value: CborStreamOutput) {
if (
value instanceof CborByteDecodedStream ||
value instanceof CborTextDecodedStream
) {
for await (const x of value) console.log(x);
} else if (value instanceof CborArrayDecodedStream) {
for await (const x of value) logValue(x);
} else if (value instanceof CborMapDecodedStream) {
for await (const [k, v] of value) {
console.log(k);
logValue(v);
}
} else if (value instanceof CborTag) {
console.log(value);
logValue(value.tagContent);
} else console.log(value);
}
for await (
const value of ReadableStream.from(rawMessage)
.pipeThrough(new CborSequenceEncoderStream())
.pipeThrough(new CborSequenceDecoderStream())
) {
logValue(value);
}