# cbor-sync

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

Latest version **1.0.4** (published 2019-05-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install cbor-sync
pnpm add cbor-sync
yarn add cbor-sync
bun add cbor-sync
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2019-05-09 |
| First published | 2014-02-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 34 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Maintainers | geraintluff, thegecko |
| Keywords | cbor |

## Links

- npm: https://www.npmjs.com/package/cbor-sync
- Repository: https://github.com/ARMmbed/cbor-sync
- Issues: https://github.com/ARMmbed/cbor-sync/issues
- npm.io page: https://npm.io/package/cbor-sync

## Recent versions

- 1.0.4 (latest) — 2019-05-09
- 1.0.3 — 2018-09-13
- 1.0.2 — 2014-06-19
- 0.0.4 — 2014-06-16
- 0.0.1 — 2014-02-27
- 0.0.0 — 2014-02-27

## README

# Encode/decode CBOR

[![Build Status](https://travis-ci.org/ARMmbed/cbor-sync.svg?branch=master)](https://travis-ci.org/ARMmbed/cbor-sync) [![NPM version](https://badge.fury.io/js/cbor-sync.png)](http://badge.fury.io/js/cbor-sync)

This package provides an extensible CBOR encoder/decoder.

## Usage

```javascript
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:

```javascript
// 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

---
_Source: https://npm.io/package/cbor-sync · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
