@arduino/cbor-js
v0.1.5
Published
The Concise Binary Object Representation (CBOR) data format (RFC7049) implemented in pure JavaScript.
Keywords
Readme
cbor-js
The Concise Binary Object Representation (CBOR) data format (RFC 7049) implemented in pure JavaScript.
Install
npm install @arduino/cbor-jsAPI
The package exports two functions:
encode(value)
Take a JavaScript value and return it encoded as an
ArrayBuffer.
decode(data)
Take an
ArrayBuffer(orArrayBufferLike) and return the decoded JavaScript value.
Usage
ESM:
import { encode, decode } from "@arduino/cbor-js"
const initial = { Hello: "World" }
const encoded = encode(initial)
const decoded = decode(encoded)CommonJS:
const { encode, decode } = require("@arduino/cbor-js")After running this example initial and decoded represent the same value.
Combination with WebSocket
The API plays well with the WebSocket object in the browser:
import { encode, decode } from "@arduino/cbor-js"
const websocket = new WebSocket(url)
websocket.binaryType = "arraybuffer"
websocket.onmessage = event => {
const message = decode(event.data)
}
websocket.send(encode(message))