0.5.0 • Published 1 year ago

@jprochazk/cbor v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Build Status David GitHub

TypeScript implementation of the Concise Binary Object Representation RFC 7049. From the official website:

CBOR is a data format whose design goals include the possibility of extremely small code size, fairly small message size, and extensibility without the need for version negotiation.

The API is self-explanatory:

// 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); // Object

// You can also encode into a pre-allocated buffer
const encoded = CBOR.encodeInto(new ArrayBuffer(4096), data);

Usage

The library is targeted for both Node and Browsers using NPM:

> npm install cbor@npm:jprochazk/cbor

This installs the library under the cbor alias.

// as CommonJS
const CBOR = require("cbor");

// as an ES module
import CBOR from "cbor";

CBOR.encode(...);
CBOR.decode(...);

Or included in the page as a static script from the unkpg CDN:

<script src="https://unpkg.com/@jprochazk/cbor@0.4.6"></script>
<script>
    // after including the script in the page, the CBOR object is available globally
    CBOR.encode(...);
    CBOR.decode(...);
</script>

Benchmarks

Benchmark is available here.

BrowserCBOR.decodeCBOR.encodeCBOR.encodeInto
Chrome5225 ops/s8998 ops/s9268 ops/s
Firefox20454 ops/s22323 ops/s22900 ops/s

Results are on a i5-8600k intel processor. Your mileage may vary. The JSON data used in the test is 2 KB decoded and 1.8 KB encoded. I specifically chose this data, because it encodes badly (lots of nested objects with only a single property). The benchmark suggests the library can encode/decode around 10 MB/s in Chrome and around 50 MB/s in Firefox. Hopefully, these are meaningful results.

Notes

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

  • 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.

0.5.0

1 year ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.3.0

4 years ago

0.4.1

4 years ago

0.3.2

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago

1.0.0

4 years ago