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.2
  • 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 self-explanatory:

import CBOR from "cbor";

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

const encoded = CBOR.encode(json); // ArrayBuffer
const decoded = CBOR.decode(encoded); // { ... }

// You can also encode into a pre-allocated buffer
const buffer = new ArrayBuffer(4096);
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.6535 ms0.5743 ms1.0312 ms

The JSON data used in the test is 13 KB decoded and 6 KB encoded. This means the library can decode at 13 MB/s and encode at 10 MB/s.

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 supported for use in both Node & browsers.

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)

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