Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fixup! Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
vmx committed Apr 29, 2019
1 parent 4c75b5d commit 938ff19
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,34 @@ const file = {
size: 11
}

dagCBOR.util.serialize(file, (err, serialized) => {
if (err) {
throw err
}
const serialized = dagCBOR.util.serialize(file)
console.log(`Encoded as a ${serialized.length} byte Buffer`)

console.log(`Encoded as a ${serialized.length} byte Buffer`)

dagCBOR.util.deserialize(serialized, (err, node) => {
if (err) {
throw err
}

console.log('Decoded as:', node)
require('assert').deepEqual(node, file) // should match
})
})
const node = dagCBOR.util.deserialize(serialized)
console.log('Decoded as:', node)
require('assert').deepEqual(node, file) // should match

// → Encoded as a 22 byte Buffer
// → Decoded as: { name: 'hello.txt', size: 11 }
```

## API

### `dagCBOR.util.serialize(obj, callback)`
### `dagCBOR.util.serialize(obj)`

Encodes an object into IPLD CBOR form, replacing any CIDs found within the object to CBOR tags (with an id of `42`).

- `obj` (any): any object able to be serialized as CBOR
- `callback` (`Function`): function to be called when serialization is complete, arguments are: `error` and the serialized node if no error was encountered.

### `dagCBOR.util.deserialize(serialized, callback)`
Returns the serialized node.

### `dagCBOR.util.deserialize(serialized)`

Decodes an IPLD CBOR encoded representation, restoring any CBOR tags (id `42`) to CIDs.

- `serialized` (`Buffer` or `String`): a binary blob representing an IPLD CBOR encoded object.
- `callback` (`Function`): function to be called when deserialization is complete, arguments are: `error` and the deserialized object if no error was encountered.

Returns the deserialized object.

### `dagCBOR.util.configureDecoder([options])`

Expand All @@ -123,7 +115,7 @@ Possible values in the `options` argument are:

Calling `dagCBOR.util.configureDecoder()` with no arguments will reset to the default decoder `size`, `maxSize` and `tags`.

### `dagCBOR.util.cid(obj[, options,] callback)`
### `dagCBOR.util.cid(obj[, options,])`

Create a [CID](https://github.com/multiformats/js-cid) for the given unserialized object.

Expand All @@ -132,7 +124,8 @@ Create a [CID](https://github.com/multiformats/js-cid) for the given unserialize
* `hashAlg` (`String`): a [registered multicodec](https://github.com/multiformats/multicodec/blob/master/table.csv) hash algorithm.
* `hashLen` (`String`): an optional hash length
* `version` (`Number`): CID version number, defaults to `1`
- `callback` (`Function`): function to be called when the object is serialized and a CID is created from that serialized form, arguments are: `error` and the created CID if no error was encountered.

Returns a Promise with the created CID.

## Contribute

Expand Down

0 comments on commit 938ff19

Please sign in to comment.