cbornamespacenamespace cborCBOR (RFC 8949), read and written with no outside package (commons 0014-webauthn FR-001): what WebAuthn's attestation objects and COSE keys are made of. The reader takes the definite-length forms of every major type (unsigned and negative integers, byte and text strings, arrays, maps, tags, the simple values false, true, null and undefined, and half, single and double floats) and returns a Result whose error says what and at which byte: an indefinite length, a reserved additional value, text that is not UTF-8, a map key that is neither an integer nor a text string, a key given twice, nesting deeper than 64 levels, or bytes left over. An integer beyond Number.MAX_SAFE_INTEGER is a bigint. The writer gives the core deterministic encoding (RFC 8949 §4.2.1): the shortest heads, definite lengths and map keys sorted by their encoded bytes, so the same value is the same bytes every time.
import { type CborValue, decodeCbor, encodeCbor } from "./cbor.ts";
const bytes = encodeCbor(new Map<number | string, CborValue>([[1, 2], ["a", [true, null]]]));
decodeCbor(bytes); // { kind: "ok", value: Map { 1 => 2, "a" => [true, null] } }cbornamespacenamespace cborcbor.CborKeytypeno test imports ittype CborKey = number | bigint | string;A map key this module reads and writes: an integer or a text string.
cbor.CborTaggedtypeno test imports ittype CborTagged = { readonly tag: number | bigint; readonly value: CborValue };A tagged value (major type 6): its tag number and the value it tags.
cbor.CborValuetypeno test imports ittype CborValue =
number | bigint | string | Uint8Array | boolean | null | undefined | readonly CborValue[] | ReadonlyMap<CborKey, CborValue> | CborTagged;A CBOR data item as a value: integers (a bigint beyond the safe range), floats, strings, bytes, arrays, maps, tags and simple values.
cbor.CborErrortypeno test imports ittype CborError = { readonly kind: "cbor"; readonly at: number; readonly message: string };Why bytes are not CBOR this reader takes: what, and at which byte.
cbor.CborItemtypeno test imports ittype CborItem = { readonly value: CborValue; readonly end: number };One data item read from a longer input: its value and the offset just after it.
cbor.MAX_DEPTHconstno test imports itconst MAX_DEPTH: 64The deepest nesting of arrays, maps and tags read.
cbor.formatCborErrorfunctionno test imports itfunction formatCborError(e: CborError): stringA CBOR error as one line.
cbor.isTaggedfunctionno test imports itfunction isTagged(value: CborValue): value is CborTaggedWhether a value is a tagged value (rather than a map, an array or bytes).
cbor.isMapfunctionno test imports itfunction isMap(value: CborValue): value is ReadonlyMap<CborKey, CborValue>Whether a value is a map.
cbor.decodeCborItemfunctionno test imports itfunction decodeCborItem(bytes: Uint8Array<ArrayBufferLike>, offset?: number): Result<CborItem, CborError>The data item that starts at offset, and the offset just after it: for CBOR followed by more bytes (WebAuthn's authenticator data).
cbor.decodeCborfunctionno test imports itfunction decodeCbor(bytes: Uint8Array<ArrayBufferLike>): Result<CborValue, CborError>The one data item the bytes hold; bytes left after it are an error.
cbor.encodeCborfunctionno test imports itfunction encodeCbor(value: CborValue): Uint8Array<ArrayBufferLike>A value as CBOR, in the core deterministic encoding: integers (safe numbers or bigints) in their shortest head, other numbers as doubles, strings as UTF-8, Uint8Array as a byte string, arrays, maps with their keys sorted by their encoding, tags and the four simple values. A value no CBOR item can hold (an integer beyond 64 bits) is a programming error and throws a RangeError.
CborKeytypetype CborKey = number | bigint | string;A map key this module reads and writes: an integer or a text string.
CborTaggedtypeno test imports ittype CborTagged = { readonly tag: number | bigint; readonly value: CborValue };A tagged value (major type 6): its tag number and the value it tags.
CborValuetypetype CborValue =
number | bigint | string | Uint8Array | boolean | null | undefined | readonly CborValue[] | ReadonlyMap<CborKey, CborValue> | CborTagged;A CBOR data item as a value: integers (a bigint beyond the safe range), floats, strings, bytes, arrays, maps, tags and simple values.
CborErrortypeno test imports ittype CborError = { readonly kind: "cbor"; readonly at: number; readonly message: string };Why bytes are not CBOR this reader takes: what, and at which byte.
CborItemtypeno test imports ittype CborItem = { readonly value: CborValue; readonly end: number };One data item read from a longer input: its value and the offset just after it.
MAX_DEPTHconstno test imports itconst MAX_DEPTH: 64The deepest nesting of arrays, maps and tags read.
formatCborErrorfunctionfunction formatCborError(e: CborError): stringA CBOR error as one line.
isTaggedfunctionno test imports itfunction isTagged(value: CborValue): value is CborTaggedWhether a value is a tagged value (rather than a map, an array or bytes).
isMapfunctionno test imports itfunction isMap(value: CborValue): value is ReadonlyMap<CborKey, CborValue>Whether a value is a map.
decodeCborItemfunctionfunction decodeCborItem(bytes: Uint8Array<ArrayBufferLike>, offset?: number): Result<CborItem, CborError>The data item that starts at offset, and the offset just after it: for CBOR followed by more bytes (WebAuthn's authenticator data).
decodeCborfunctionfunction decodeCbor(bytes: Uint8Array<ArrayBufferLike>): Result<CborValue, CborError>The one data item the bytes hold; bytes left after it are an error.
encodeCborfunctionfunction encodeCbor(value: CborValue): Uint8Array<ArrayBufferLike>A value as CBOR, in the core deterministic encoding: integers (safe numbers or bigints) in their shortest head, other numbers as doubles, strings as UTF-8, Uint8Array as a byte string, arrays, maps with their keys sorted by their encoding, tags and the four simple values. A value no CBOR item can hold (an integer beyond 64 bits) is a programming error and throws a RangeError.