# @digitalcredentials/cborld

> A CBOR-LD encoder/decoder for Javascript.

Latest version **4.3.4** (published 2022-11-02) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install @digitalcredentials/cborld
pnpm add @digitalcredentials/cborld
yarn add @digitalcredentials/cborld
bun add @digitalcredentials/cborld
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.3.4 |
| Published | 2022-11-02 |
| First published | 2021-09-23 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=12 |
| Dependencies | 4 |
| Unpacked size | 134.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Digital Bazaar, Inc. |
| Maintainers | codenamedmitri, kimhd, uligall, alexander.muehle, stuartf, jchartrand, kiliankae |
| Keywords | json-ld, cbor, linked data, compression |

## Links

- npm: https://www.npmjs.com/package/@digitalcredentials/cborld
- Repository: https://github.com/digitalcredentials/cborld
- Issues: https://github.com/digitalcredentials/cborld/issues
- npm.io page: https://npm.io/package/@digitalcredentials/cborld

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^8.3.1
- [cborg](https://npm.io/package/cborg.md) ^1.2.0
- [js-base64](https://npm.io/package/js-base64.md) ^3.6.0
- [@digitalcredentials/base58-universal](https://npm.io/package/@digitalcredentials/base58-universal.md) ^1.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 4.3.4 (latest) — 2022-11-02
- 4.3.3 — 2022-09-08
- 4.3.2 — 2022-08-10
- 4.3.1 — 2021-09-23

## README

# JavaScript CBOR-LD Processor _(@digitalcredentials/cborld)_

[![Node.js CI](https://github.com/digitalcredentials/cborld/workflows/Node.js%20CI/badge.svg)](https://github.com/digitalcredentials/cborld/actions?query=workflow%3A%22Node.js+CI%22)
[![NPM Version](https://img.shields.io/npm/v/@digitalcredentials/cborld.svg)](https://npm.im/@digitalcredentials/cborld)

> A JavaScript CBOR-LD Process for Web browsers and Node.js apps.

## Table of Contents

- [Background](#background)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Contribute](#contribute)
- [Commercial Support](#commercial-support)
- [License](#license)

## Background

(Forked from [`digitalbazaar/cborld` v4](https://github.com/digitalbazaar/cborld)
to provide TypeScript and ReactNative compatibility.)

This library provides a CBOR-LD Processor for Web browsers, React Native, 
and Node.js applications.

## Install

### NPM

```
npm install @digitalcredentials/cborld
```

### Git

To install locally (for development):

```
git clone https://github.com/digitalcredentials/cborld.git
cd cborld
npm install
```

## Usage

This library provides two primary functions for encoding and decoding
CBOR-LD data.

### Encode to CBOR-LD

To encode a JSON-LD document as CBOR-LD:

```js
import {encode, documentLoader} from '@digitalcredentials/cborld';

const jsonldDocument = {
  '@context': 'https://www.w3.org/ns/activitystreams',
  type: 'Note',
  summary: 'CBOR-LD',
  content: 'CBOR-LD is awesome!'
};

// encode a JSON-LD Javascript object into CBOR-LD bytes
const cborldBytes = await encode({jsonldDocument, documentLoader});
```

To decode a CBOR-LD document to JSON-LD:

```js
import {decode, documentLoader} from '@digitalcredentials/cborld';

// get the CBOR-LD bytes
const cborldBytes = await fs.promises.readFile('out.cborld');

// decode the CBOR-LD bytes into a Javascript object
const jsonldDocument = await cborld.decode({cborldBytes, documentLoader});
```

## API

### Functions

<dl>
<dt><a href="#encode">encode(options)</a> ⇒ <code>Uint8Array</code></dt>
<dd>
  <p>Encodes a given JSON-LD document into a CBOR-LD byte array.</p>
</dd>
<dt><a href="#decode">decode(options)</a> ⇒ <code>object</code></dt>
<dd>
  <p>Decodes a CBOR-LD byte array into a JSON-LD document.</p>
</dd>
</dl>

### Typedefs

<dl> <dt><a href="#diagnosticFunction">diagnosticFunction</a> :
    <code>function</code></dt>
  <dd>
    <p>A diagnostic function that is called with
      diagnostic information. Typically set to <code>console.log</code> when
      debugging.</p>
  </dd> <dt><a href="#documentLoaderFunction">documentLoaderFunction</a> ⇒
    <code>string</code></dt>
  <dd>
    <p>Fetches a resource given a URL and returns it
      as a string.</p>
  </dd>
</dl>

<a name="encode"></a>

### encode(options) ⇒ <code>Promise&lt;Uint8Array></code>
Encodes a given JSON-LD document into a CBOR-LD byte array.

**Kind**: global function
**Returns**: <code>Uint8Array</code> - - The encoded CBOR-LD bytes.
<table>
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>options</td>
      <td><code>object</code></td>
      <td>
        <p>The
          options to use when encoding to CBOR-LD.</p>
      </td>
    </tr>
    <tr>
      <td>options.jsonldDocument</td>
      <td><code>object</code></td>
      <td>
        <p>The JSON-LD
          Document to convert to CBOR-LD bytes.</p>
      </td>
    </tr>
    <tr>
      <td>options.documentLoader</td>
      <td><code><a href="#documentLoaderFunction">documentLoaderFunction</a></code></td>
      <td>
        <p>The document loader to use when resolving JSON-LD Context URLs.</p>
      </td>
    </tr>
    <tr>
      <td>[options.appContextMap]</td>
      <td><code>Map</code></td>
      <td>
        <p>A map of JSON-LD Context URLs and their encoded CBOR-LD values
          (must be values greater than 32767 (0x7FFF)).</p>
      </td>
    </tr>
    <tr>
      <td>[options.appTermMap]</td>
      <td><code>Map</code></td>
      <td>
        <p>A map of JSON-LD terms and their associated
          CBOR-LD term codecs.</p>
      </td>
    </tr>
    <tr>
      <td>[options.diagnose]</td>
      <td><code><a href="#diagnosticFunction">diagnosticFunction</a></code></td>
      <td>
        <p>A function that, if provided, is called with diagnostic
        information.</p>
      </td>
    </tr>
  </tbody>
</table>
<a name="decode"></a>

### decode(options) ⇒ <code>Promise&lt;object></code>
Decodes a CBOR-LD byte array into a JSON-LD document.

**Kind**: global function
**Returns**: <code>object</code> - - The decoded JSON-LD Document.
<table>
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>options</td>
      <td><code>object</code></td>
      <td>
        <p>The options to use when decoding CBOR-LD.</p>
      </td>
    </tr>
    <tr>
      <td>options.cborldBytes</td>
      <td><code>Uint8Array</code></td>
      <td>
        <p>The encoded CBOR-LD bytes to
          decode.</p>
      </td>
    </tr>
    <tr>
      <td>options.documentLoader</td>
      <td><code>function</code></td>
      <td>
        <p>The document loader to use when
          resolving JSON-LD Context URLs.</p>
      </td>
    </tr>
    <tr>
      <td>[options.appContextMap]</td>
      <td><code>Map</code></td>
      <td>
        <p>A map of JSON-LD Context URLs and
          their associated CBOR-LD values. The values must be greater than
          32767 (0x7FFF)).</p>
      </td>
    </tr>
    <tr>
      <td>[options.appTermMap]</td>
      <td><code>Map</code></td>
      <td>
        <p>A map of JSON-LD terms and
          their associated CBOR-LD term codecs.</p>
      </td>
    </tr>
    <tr>
      <td>[options.diagnose]</td>
      <td><code><a href="#diagnosticFunction">diagnosticFunction</a></code></td>
      <td>
        <p>A function that, if
          provided, is called with diagnostic information.</p>
      </td>
    </tr>
  </tbody>
</table>
<a name="diagnosticFunction"></a>

### diagnosticFunction : <code>function</code>
A diagnostic function that is called with diagnostic information. Typically
set to `console.log` when debugging.

**Kind**: global typedef
<table>
  <thead>
    <tr>
      <th>Param</th><th>Type</th><th>Description</th>
    </tr>
  </thead>
  <tbody>
<tr>
    <td>message</td><td><code>string</code></td><td><p>The diagnostic message.</p>
</td>
    </tr>  </tbody>
</table>

<a name="documentLoaderFunction"></a>

### documentLoaderFunction ⇒ <code>string</code>
Fetches a resource given a URL and returns it as a string.

**Kind**: global typedef
**Returns**: <code>string</code> - The resource associated with the URL as a string.
<table>
  <thead>
    <tr>
      <th>Param</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>url</td>
      <td><code>string</code></td>
      <td>
        <p>The URL to retrieve.</p>
      </td>
    </tr>
  </tbody>
</table>

Examples:

```
TBD
```

## Contribute

Please follow the existing code style.

PRs accepted.

If editing the README, please conform to the
[standard-readme](https://github.com/RichardLitt/standard-readme) specification.

## License

* MIT License - DCC - TypeScript compatibility.
* New BSD License (3-clause) © 2020-2021 Digital Bazaar - Initial implementation.

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