(UNOFFICIAL BRANCH) The Concise Binary Object Representation (CBOR) data format (RFC7049) implemented in pure JavaScript.
npm install cbor-js-unofficialcbor-js
=======
The Concise Binary Object Representation (CBOR) data format (RFC 7049) implemented in pure JavaScript.
This is an unofficial branch of a CBOR implementation by Patrick Gansterer (paroga) that supports Float 16 and 32 encoding by default, implements an exact accounting buffer allocation strategy, and adds an option for allowing extraneous bytes at the end of a chunk.
API
---
The CBOR-object provides the following two functions:
CBOR.decode(data)
> Take the ArrayBuffer object data and return it decoded as a JavaScript object.
CBOR.encode(data)
> Take the JavaScript object data and return it encoded as a ArrayBuffer object.
Usage
-----
Include cbor.js in your or HTML page:
``html
`
CBOR
Then you can use it via the -object in your code:
`javascript
`
var initial = { Hello: "World" };
var encoded = CBOR.encode(initial);
var decoded = CBOR.decode(encoded);
initial
After running this example and decoded represent the same value.
WebSocket
$3
The API was designed to play well with the object in the browser:
`javascript
``
var websocket = new WebSocket(url);
websocket.binaryType = "arraybuffer";
...
websocket.onmessage = function(event) {
var message = CBOR.decode(event.data);
};
...
websocket.send(CBOR.encode(message));