0.0.1 • Published 5 years ago

cbor-wrapper v0.0.1

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

cbor-wrapper

A wrapper for cbor lib

Peer dependencies

  1. cbor (required)
  2. buffer (optional) .Please install buffer if you get error message 'Cannot find variable buffer') https://stackoverflow.com/questions/48432524/cant-find-variable-buffer
import buffer from 'buffer';
global.Buffer = buffer.Buffer;

Encode

import cborWrapper from 'cbor-wrapper';

async function encode(data) {
  const encodedData = await cborWrapper.encode(data); // Buffer <01, 02, 05, ...>
  return encodedData;
}

Serialize data to string

import cborWrapper from 'cbor-wrapper';

async function serialize(data) {
  const encodedData = await cborWrapper.encode(data, true); // "binary string ..."
  return encodedData;
}

Decode

import cborWrapper from 'cbor-wrapper';

async function decode() {
  await cborWrapper.decode('{ "text": "Hello World" }'); // support JSON serialized string
  await cborWrapper.decode('binary string'); // support binary string
  await cborWrapper.decode('01EFC3....'); // support hex string
  await cborWrapper.decode(Buffer.alloc(4000)); // support buffer object
}