Socket
Socket
Sign inDemoInstall

@ellipticoin/cbor

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ellipticoin/cbor

CBOR encode/decode (synchronous, semantic, browser-compatible)


Version published
Maintainers
1
Created
Source

Encode/decode CBOR

Build Status NPM version

This package provides an extensible CBOR encoder/decoder.

Usage

var CBOR = require('cbor-sync');

var encodedBuffer = CBOR.encode({hello: 'world'});
var decodedObject = CBOR.decode(encodedBuffer);

toCBOR()

Much like the toJSON() method, which allows objects to provide a replacement representation for encoding, this package checks for a toCBOR() method.

Note that this step happens after any semantic-tagging/-replacement step, so a custom semantic encoder will always override an objects built-in toCBOR() method.

Semantic extensions

CBOR provides a limited set of basic types (similar to JSON), but provides semantic tagging (optional for both encoder/decoder) that lets you annotate parts of the data so they can be decoded appropriately.

Here is an example (from this module) for encoding Date objects as ISO strings:

// 0 is the CBOR semantic tag number for date/time strings: https://tools.ietf.org/html/rfc7049#section-2.4
CBOR.addSemanticEncode(0, function (data) {
	if (data instanceof Date) {
		return data.toISOString();
	}
});
CBOR.addSemanticDecode(0, function (dateString) {
	return new Date(dateString);
});

Known issues

  • All floats encoded as 64-bit, regardless of whether they strictly need to be

Keywords

FAQs

Package last updated on 29 Dec 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