Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@levischuck/tiny-cbor

levischuck881.1kMIT0.3.1TypeScript support: included

Tiny CBOR library

readme

Tiny CBOR

This minimal generic library decodes and encodes most useful CBOR structures into simple JavaScript structures:

  • Maps with keys as strings or numbers with CBORType values as a Map
  • Arrays of CBORType values
  • integers as numbers
  • float32 and float64 as numbers
  • float16 NaN, Infinity, -Infinity
  • strings
  • byte strings as Uint8Array
  • booleans
  • null and undefined
  • tags as CBORTag(tag, value)

Limitations

This implementation does not support:

  • indefinite length maps, arrays, text strings, or byte strings.
  • half precision floating point numbers
  • integers outside the range of [-9007199254740991, 9007199254740991], see Number.MAX_SAFE_INTEGER
  • native output to JSON
  • does not support generic objects, only Maps

This implementation has the following constraints:

  • Map keys may only be strings or numbers
  • Tags are not interpreted

Behavior

Maps that have duplicate keys will throw an error during decoding. Decoding data that is incomplete will throw an error during decoding.

Example

CBOR byte decoding example, this outputs a CBORType which is up to you to ensure matches the right underlying type (e.g. a string) at runtime.

// NPM
// import { decodeCBOR } from "@levischuck/tiny-cbor";
// or JSR
// import { decodeCBOR } from "jsr:@levischuck/tiny-cbor";
import { decodeCBOR } from "./index.ts";
// Get your bytes somehow, directly or with decodeBase64 / decodeHex (available through @levischuck/tiny-encodings)
// encoded ["hello", "world", 1]
const HELLO_WORLD_BYTES = new Uint8Array([
  0x83, // Array (3)
  0x65, // text (5),
  0x68, // h
  0x65, // e
  0x6C, // l
  0x6C, // l
  0x6F, // o
  0x65, // text(5),
  0x77, // w
  0x6F, // o
  0x72, // r
  0x6C, // l
  0x64, // d
  0x01, // 1
]);
const decoded = decodeCBOR(HELLO_WORLD_BYTES);
if (
  Array.isArray(decoded) && decoded.length == 3 && decoded[0] == "hello" &&
  decoded[1] == "world" && decoded[2] == 1
) {
  console.log("Success!");
}

If you're looking to make this more ergonomic in JS / TS, check out tiny-cbor-schema!

Where to get it

This library is available on NPM and JSR.

This library is no longer automatically published to Deno's Third Party Modules. Newer versions may appear on deno.land/x, but do not work.

changelog

Changelog

0.3.1 (2025-05-21)

Bug Fixes

0.2.12 (2025-05-21)

Bug Fixes

  • Some types no longer worked in deno 2.22.2 (5fedeb1)

0.2.11 (2025-02-08)

Bug Fixes

  • Deno check docs and badge style (40b56e1)

0.2.10 (2025-02-08)

Bug Fixes

  • Add where to find and move test imports to JSR (f1ab683)

0.2.9 (2025-02-08)

Bug Fixes

0.2.8 (2025-02-05)

Bug Fixes

0.2.7 (2025-02-05)

Bug Fixes

0.2.6 (2025-02-05)

Bug Fixes

0.2.5 (2025-02-05)

Bug Fixes

  • Node 24 doesn't exist yet (7767091)

0.2.4 (2025-02-05)

Bug Fixes

0.2.3 (2025-02-05)

Bug Fixes