Git Product home page Git Product logo

cbor-utils's Introduction

CBOR Utils

This library provides you with low level tools to use CBOR format. It is inspired by minicbor crate in Rust and rxjs library in Javascript.

API Docs

To see the full API Documentation, please visit Docs.

Installation

Using npm:

npm install cbor-utils

Using jsr:

npx jsr add @whiteand/cbor

Example:

If you want to encode something to Uint8Array you can use encode function.

import { encode, u8, bytes, bool } from "cbor-utils";
const bytes = encode((e) => {
  e.encode(u8, 42);
  e.encode(bytes, new Uint8Array([1, 2, 3]));
  e.encode(bool, true);
});

If you want to decode something you can use decode function.

import { ok, decode, Result, u8, bytes, bool } from "cbor-utils";

/**
 * @type {Result<{id: number, hash: Uint8Array, submitted: boolean }, DecodingError>
 */
const res = decode(cborBytes, (d) => {
  const id = d.decode(u8);
  if (!id.ok()) return id;
  const hash = d.decode(bytes);
  if (!hash.ok()) return hash;
  const submitted = d.decode(bool);
  if (!submitted.ok()) return submitted;
  return ok({
    id: id.value,
    hash: hash.value,
    submitted: submitted.value,
  });
});

//or less type safe version, but without necessity to check each decoded item result:
import { tryDecode, u8, bytes, bool } from "cbor-utils";

/**
 * @type {Result<{id: number, hash: Uint8Array, submitted: boolean }, unknown>
 */
const result2 = tryDecode(cborBytes, (d) => {
  const id = d.decode(u8);
  const hash = d.decode(bytes);
  const submitted = d.decode(bool);

  return {
    id,
    hash,
    submitted,
  };
});

Terminology

CBOR Type

A CBOR Type is an object that allows you to encode and decode values of the specified type. Example: CBOR Type of u8 means that this object has encode capability that converts u8 into a CBOR data item, also it is able to read u8 data item from an Uint8Array.

Encoder

Is an object that allows you to encode values into a growing Uint8Array.

Decoder

Is an object that consists of two fields buf: Uint8Array and ptr: number. CBOR Types modify the decoder type to read cbor item.

Operator

Is a function that takes one CBOR Type (called source type) and returns another CBOR Type (called target type). Example: array operator takes CBOR Type of u8 and returns CBOR type of array<u8>.

cbor-utils's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.