gizlang:cbor
CBOR (RFC 8949) as a value, for the binary structures of WebAuthn and COSE keys. It reads any definite length and writes the shortest one: integers within i64, byte and text strings, arrays, maps, false, true and null. Tags, floats, other simple values, indefinite lengths and integers past i64 are errors: no protocol this module serves needs them, and a reader that refuses them cannot misread them.
import * as Cbor from "gizlang:cbor";Types
Value
type Value = | { kind: "int"; value: i64; } | { kind: "bytes"; value: bytes; } | { kind: "text"; value: string; } | { kind: "array"; items: Vec<Value>; } | { kind: "map"; keys: Vec<Value>; values: Vec<Value>; } | { kind: "bool"; value: bool; } | { kind: "null"; }A CBOR item. A map keeps its keys and values in order, as two Vecs of equal length; a repeated key keeps both.
intbytestextarraymapboolnull
CborError
type CborError = { offset: i64; message: string; }Where the bytes stop being CBOR, in bytes, and why.
offset:i64message:string
Read
type Read = { value: Value; next: i64; }An item read, and the position after it.
value:gizlang_cbor.Valuenext:i64
Functions
read
function read(data: bytes, at: i64): Result<Read, CborError>The item at AT in DATA and the position after it: for items that sit back to back, as the key in authenticator data.
Effects: fail.
decode
function decode(data: bytes): Value throws CborErrorThe one item that DATA holds, and nothing after it.
Effects: fail.
encode
function encode(value: Value): bytesThe CBOR bytes of VALUE: definite lengths, each in its shortest form.
Effects: fail.
lookup
function lookup(container: Value, key: Value): Option<Value>The item of MAP at KEY (an int, bytes or text item), or undefined. A repeated key gives its first value.
Effects: fail.
bytesOf
function bytesOf(v: Value): Option<bytes>The bytes of a byte string item, or undefined.
textOf
function textOf(v: Value): Option<string>The text of a text string item, or undefined.
intOf
function intOf(v: Value): Option<i64>The integer of an integer item, or undefined.