Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jprochazk/cbor

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jprochazk/cbor

Partial implementation of RFC 7049 (CBOR)

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
86
increased by145.71%
Maintainers
1
Weekly downloads
 
Created
Source

cbor

JavaScript implementation of the CBOR RFC 7049.

David GitHub

Usage

> npm install cbor@npm:jprochazk/cbor

The above command will install the library under the cbor alias.

The API is very simple:

import CBOR from "cbor";

const json = {
    "key": "value",
    "another key": [
        1, 2, 3
    ],
    "number": 3.141592653589793
    "nulls are also encoded": null
};

const encoded = CBOR.encode(json);
const decoded = CBOR.decode(encoded);

You can also encode into a pre-allocated buffer.

// Allocate a 4KiB buffer
const buffer = new ArrayBuffer(4096);
// Encode the data into this buffer
const encoded = CBOR.encodeInto(data, buffer);

Benchmarks

Speed was one of my main concerns when writing this library. The source code of the benchmark is available here. Here are the results:

CPUCBOR.encodeCBOR.encodeIntoCBOR.decode
i5-8600K0.6555 ms0.5743 ms1.1312 ms

The JSON object used is ~13KB decoded, ~6KB encoded. This means ~13MB/s decoding speed and ~10MB/s encoding speed.

You can squeeze out a bit more performance if you use CBOR.encodeInto with a sufficiently large buffer. the CBOR.encode default is 1024 bytes, which should be enough for the vast majority of uses, but if you ever find yourself using more than that, utilize CBOR.encodeInto.

Notes

This library is meant for use in the browser. Node support is in the works.

There are a few things from the specification which are currently unimplemented:

  • Byte strings
  • Tags, and the items they represent:
    • date/time, bignum/bigfloat as byte string, and others
  • 16-bit float (IEEE754 binary16)
  • Integers larger than 32 bit (BigInt)

These weren't required in my case, but they're trivial additions to the existing parser/writer logic. If you need one or more of these features, submit an issue.

Keywords

FAQs

Package last updated on 10 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc