This repository was archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
feat: update DAGLink and DAGNode to have an immutable API #6
Merged
+864
−674
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4bdb48b
feat: update DAGLink and DAGNode to have an immutable API
daviddias b56d797
feat(refactor): new API proposal
haadcode ea904a7
feat: refactor, structure code, make DAGNode funcs inside the same fo…
daviddias 6a3531d
feat: add a same create pattern api for DAGLink
daviddias 301d0b0
fix: that linting
daviddias 6e3ab23
docs: revamp README documentation to the new API
daviddias eec00da
fix: DAGLink tests
daviddias 710e53f
feat+fix: complete addLink and rmLink functions + fix their tests and…
daviddias f53e0c8
feat: IPLD Resolver updated, all tests passing
daviddias 0ea67c9
docs: add examples to readme
daviddias a6b9350
docs: document types of functions in the readme
daviddias 2b1a356
fix: apply code review
daviddias ae71f26
fix: serialization of empty node
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
|
||
[](https://saucelabs.com/u/ipld-js-dag-pb) | ||
|
||
> JavaScript Implementation of the IPLD Format MerkleDAG Node in Protobuf. | ||
> JavaScript Implementation of the IPLD Format MerkleDAG Node in Protobuf. In addition to the IPLD Format methods, this module also provides an API for creating the nodes and manipulating them (adding and removing links, etc). | ||
|
||
## Table of Contents | ||
|
||
|
@@ -28,89 +28,237 @@ | |
## Install | ||
|
||
```bash | ||
> npm i ipld-dag-pb | ||
> npm install ipld-dag-pb --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
```JavaScript | ||
const dagPB = require('ipld-dag-pb') | ||
|
||
// then, to access each of the components | ||
dagPB.DAGNode | ||
dagPB.DAGNode.create // create a DAGNode | ||
dagPB.DAGNode.clone // clone a DAGNode | ||
dagPB.DAGNode.addLink // add a Link to a DAGNode, creating a new one | ||
dagPB.DAGNode.rmLinkq // remove a Link to a DAGNode, creating a new one | ||
dagPB.DAGLink.create // create a DAGLink | ||
|
||
// IPLD Format specifics | ||
dagPB.resolver | ||
dagPB.util | ||
``` | ||
|
||
### Examples | ||
|
||
#### Create a DAGNode | ||
|
||
```JavaScript | ||
DAGNode.create(new Buffer('some data'), (err, node) => { | ||
if (err) { | ||
throw error | ||
} | ||
// node is your DAGNode instance | ||
}) | ||
``` | ||
|
||
#### Add and remove a Link | ||
|
||
```JavaScript | ||
const link = { | ||
name: 'I am a link', | ||
multihash: 'QmHash..' | ||
size: 42 | ||
} | ||
|
||
DAGNode.addLink(node, link, (err, nodeA) => { | ||
if (err) { | ||
throw err | ||
} | ||
// node - DAGNode instance with the link | ||
console.log('with link', nodeA.toJSON()) | ||
|
||
DAGNode.rmLink(nodeA, 'I am a link', (err, nodeB) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
// node - DAGNode instance without the link, equal to just node | ||
console.log('without link', nodeB.toJSON()) | ||
}) | ||
}) | ||
``` | ||
|
||
## API | ||
|
||
### DAGNode Class | ||
### DAGNode functions | ||
|
||
DAGNodes are immutable objects, in order to manipulate them you have to follow a function approach of applying function and getting new instances of the given DAGNode. | ||
|
||
You can incude it in your project with: | ||
|
||
```JavaScript | ||
const dagPB = require('ipld-dag-pb') | ||
const DAGNode = dagPB.DAGNode | ||
``` | ||
|
||
#### DAGNode.create(data, links, hashAlg, callback) | ||
|
||
- `data` - type: Buffer | ||
- `links`- type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON) | ||
- `hashAlg` - type: String | ||
- `callback` - type: function with signature `function (err, node) {}` | ||
|
||
Create a DAGNode. | ||
|
||
```JavaScript | ||
DAGNode.create('data', links, (err, dagNode) => { | ||
// ... | ||
}) | ||
``` | ||
|
||
links can be a single or an array of DAGLinks instances or objects with the following pattern | ||
|
||
```JavaScript | ||
{ | ||
name: '<some name>', | ||
hash: '<some multihash>', // can also be `multihash: <some multihash>` | ||
size: <sizeInBytes> | ||
} | ||
``` | ||
|
||
#### addLink(node, link, callback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the argument types |
||
|
||
Create a new DAGNode | ||
- `node` - type: DAGNode | ||
- `link` - type: DAGLink or DAGLink in its json format | ||
- `callback` - type: function with signature `function (err, node) {}` | ||
|
||
Creates a link on node A to node B by using node B to get its multihash. Returns a *new* instance of DAGNode without modifying the old one. | ||
|
||
Creates a new DAGNode instance with the union of node.links plus the new link. | ||
|
||
`link` can be: | ||
- DAGLink instance | ||
- DAGNode instance | ||
- Object with the following properties: | ||
|
||
```JavaScript | ||
var node = new dagPB.DAGNode([<data>, <[links]>]) | ||
{ | ||
name: '<some string>', // optional | ||
size: <size in bytes>, | ||
multihash: <multihash> // can be a String multihash or multihash buffer | ||
} | ||
``` | ||
|
||
#### `addNodeLink` | ||
|
||
> creates a link on node A to node B by using node B to get its multihash | ||
#### rmLink(node, nameOrMultihash, callback) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the argument types |
||
|
||
- `node` - type: DAGNode | ||
- `nameOrMultihash` - type: String or multihash buffer | ||
- `callback` - type: function with signature `function (err, node) {}` | ||
|
||
Removes a link from the node by name. Returns a *new* instance of DAGNode without modifying the old one. | ||
|
||
```JavaScript | ||
DAGNode.rmLink(node, 'Link1' (err, dagNode) => ...) | ||
``` | ||
|
||
#### `addRawLink` | ||
#### clone(node, callback) | ||
|
||
> creates a link on node A to node B by using directly node B multihash | ||
- `node` - type: DAGNode | ||
- `callback` - type: function with signature `function (err, node) {}` | ||
|
||
#### `updateNodeLink` | ||
Creates a clone of the DAGNode instance passed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the argument types |
||
|
||
> updates a link on the node. *caution* this method returns a copy of the MerkleDAG node | ||
```JavaScript | ||
DAGNode.clone(node, (err, nodeClone) => {}) | ||
``` | ||
|
||
#### `removeNodeLink` | ||
### DAGNode instance methods and properties | ||
|
||
> removes a link from the node by name | ||
You have the following methods and properties available in every DAGNode instance. | ||
|
||
#### `removeNodeLinkByHash` | ||
#### `node.data` | ||
|
||
> removes a link from the node by the hash of the linked node | ||
#### `node.links` | ||
|
||
An array of `DAGLinks` | ||
|
||
#### `clone` | ||
#### `node.size` | ||
|
||
> creates a clone of the MerkleDAG Node | ||
Size of the node, in bytes | ||
|
||
#### `size` | ||
#### `node.multihash` | ||
|
||
> (property) size of the node, in bytes | ||
#### `node.serialized` | ||
|
||
#### `links` | ||
#### `node.toJSON()` | ||
|
||
> (property) an array of `DAGLink`s belonging to the node | ||
#### `node.toString()` | ||
|
||
#### `multihash(callback)` | ||
|
||
> returns the multihash (default: sha2-256) | ||
### DAGLink functions | ||
|
||
#### `getPBNode` | ||
Following the same pattern as [`DAGNode functions`]() above, DAGLink also offers a function for its creation. | ||
|
||
> used internally | ||
You can incude it in your project with: | ||
|
||
#### `makeLink` | ||
```JavaScript | ||
const dagPB = require('ipld-dag-pb') | ||
const DAGLink = dagPB.DAGLink | ||
``` | ||
|
||
> used internally | ||
#### DAGLink.create(name, size, multihash, callback) | ||
|
||
### DAGLink Class | ||
```JavaScript | ||
DAGLink.create( | ||
'link-to-file', // name of the link (can be empty) | ||
10, // size in bytes | ||
'QmSomeHash...', // can be multihash buffer or string | ||
(err, link) => { | ||
if (err) { | ||
throw err | ||
} | ||
// link is a DAGLink instance | ||
}) | ||
``` | ||
|
||
Create a new DAGLink | ||
Note: DAGLinks are simpler objects and can be instantiated directly: | ||
|
||
```JavaScript | ||
var link = new dagPB.DAGLink(<name>, <size>, <hash>) | ||
const link = new DAGLink(name, size, multihash) | ||
``` | ||
|
||
### Local Resolver (to be used by the IPLD Resolver) | ||
### DAGLink instance methods and properties | ||
|
||
#### `link.name` | ||
|
||
#### `link.size` | ||
|
||
#### `link.multihash` | ||
|
||
#### `link.toJSON()` | ||
|
||
#### `link.toString()` | ||
|
||
### [IPLD Format Specifics](https://github.com/ipld/interface-ipld-format) - Local (node/block scope) resolver | ||
|
||
> See: https://github.com/ipld/interface-ipld-format#local-resolver-methods | ||
|
||
|
||
#### `dagPB.resolver.resolve` | ||
|
||
#### `dagPB.resolver.tree` | ||
|
||
#### `dagPB.resolver.patch` | ||
|
||
### [IPLD Format Specifics](https://github.com/ipld/interface-ipld-format) - util | ||
|
||
> See: https://github.com/ipld/interface-ipld-format#ipld-format-utils | ||
|
||
#### `resolver.resolve` | ||
### `dagPB.util.cid` | ||
|
||
#### `resolver.tree` | ||
### `dagPB.util.serialize` | ||
|
||
#### `resolver.patch` | ||
### `dagPB.util.deserialize` | ||
|
||
## License | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
const DAGLink = require('./index.js') | ||
|
||
function create (name, size, multihash, callback) { | ||
const link = new DAGLink(name, size, multihash) | ||
callback(null, link) | ||
} | ||
|
||
module.exports = create |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document the argument types