Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MD033: false
51 changes: 38 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ applications.

### NPM

```
```sh
npm install @digitalbazaar/cborld
```

### Git

To install locally (for development):

```
```sh
git clone https://github.com/digitalbazaar/cborld.git
cd cborld
npm install
Expand All @@ -50,31 +50,52 @@ CBOR-LD data.
To encode a JSON-LD document as CBOR-LD:

```js
import {encode} from '@digitalbazaar/cborld';
import { encode } from '@digitalbazaar/cborld';
import { JsonLdDocumentLoader } from 'jsonld-document-loader';

// Establish JSON-LD context
const CONTEXT_URL = 'https://example.com/my-context/v1';
const CONTEXT = {
'@context': {
ex: 'https://example.com/my-context/v1#',
type: '@type',
Example: 'ex:Example',
label: 'ex:label',
content: 'ex:content'
}
};
const loader = new JsonLdDocumentLoader();
loader.addStatic(CONTEXT_URL, CONTEXT)
const myLoader = loader.build();

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

// encode a JSON-LD Javascript object into CBOR-LD bytes
// Note: user must provide their own JSON-LD `documentLoader`
const cborldBytes = await encode({jsonldDocument, documentLoader});
const outputCborldBytes = await encode({
jsonldDocument: myDocument,
documentLoader: myLoader
});
```

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

```js
import {decode} from '@digitalbazaar/cborld';
import { decode } from '@digitalbazaar/cborld';

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

// decode the CBOR-LD bytes into a Javascript object
// Note: user must provide their own JSON-LD `documentLoader`
const jsonldDocument = await cborld.decode({cborldBytes, documentLoader});
// Note: reuses JSON-LD `myLoader` from encode example above
const outputJsonldDocument = await cborld.decode({
cborldBytes: inputCcborldBytes,
documentLoader: myLoader
});
```

## API
Expand Down Expand Up @@ -113,6 +134,7 @@ const jsonldDocument = await cborld.decode({cborldBytes, documentLoader});
<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
Expand Down Expand Up @@ -178,6 +200,7 @@ Encodes a given JSON-LD document into a CBOR-LD byte array.
<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
Expand Down Expand Up @@ -244,6 +267,7 @@ Decodes a CBOR-LD byte array into a JSON-LD document.
<a name="diagnosticFunction"></a>

### diagnosticFunction : <code>function</code>

A diagnostic function that is called with diagnostic information. Typically
set to `console.log` when debugging.

Expand All @@ -264,6 +288,7 @@ set to `console.log` when debugging.
<a name="documentLoaderFunction"></a>

### documentLoaderFunction ⇒ <code>string</code>

Fetches a resource given a URL and returns it as a string.

**Kind**: global typedef
Expand Down Expand Up @@ -305,7 +330,7 @@ If editing the README, please conform to the
## Commercial Support

Commercial support for this library is available upon request from
Digital Bazaar: [email protected]
Digital Bazaar: <mailto:[email protected]>

## License

Expand Down