js-ipld-dag-pb
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).
Lead Maintainer
Volker Mische
Table of Contents
Install
> npm install ipld-dag-pb --save
Usage
const dagPB = require('ipld-dag-pb')
dagPB.DAGNode.create
dagPB.DAGNode.clone
dagPB.DAGNode.addLink
dagPB.DAGNode.rmLink
dagPB.resolver
dagPB.util
Examples
Create a DAGNode
const node1 = DAGNode.create(Buffer.from('some data'))
const node2 = DAGNode.create('some data')
Add and remove a Link
const link = {
Name: 'I am a link',
Hash: 'QmHash..',
Tsize: 42
}
const nodeA = await DAGNode.addLink(node, link)
console.log('with link', nodeA.toJSON())
const nodeB = await DAGNode.rmLink(nodeA, 'I am a link')
console.log('without link', nodeB.toJSON())
API
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:
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
DAGNode.create(data, links)
data
- type: Bufferlinks
- type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
Create a DAGNode.
const dagNode = DAGNode.create('data', links)
links can be a single or an array of DAGLinks instances or objects with the following pattern
{
Name: '<some name>',
Hash: '<some cid>',
TSize: <sizeInBytes>
}
addLink(node, link)
node
- type: DAGNodelink
- type: DAGLink or DAGLink in its json format
Creates a link on node A to node B by using node B to get its CID. 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:
const link = {
Name: '<some string>',
Tsize: <size in bytes>,
Hash: <cid> // can be a String CID, CID buffer or CID object
}
const dagNode = await DAGNode.addLink(node, link)
rmLink(node, nameOrCid)
node
- type: DAGNodenameOrCid
- type: String, CID object or CID buffer
Removes a link from the node by name. Returns a new instance of DAGNode without modifying the old one.
const dagNode = await DAGNode.rmLink(node, 'Link1')
clone(node)
Creates a clone of the DAGNode instance passed
const nodeClone = DAGNode.clone(node)
DAGNode instance methods and properties
You have the following methods and properties available in every DAGNode instance.
node.Data
node.Links
An array of JSON Objects with fields named Hash
, Name
, and Tsize
.
node.size
Size of the node, in bytes
node.toJSON()
node.toString()
DAGLink functions
Following the same pattern as DAGNode functions
above, DAGLink also offers a function for its creation.
You can incude it in your project with:
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
DAGLink constructor
const link = new DAGLink(
'link-to-file',
10,
'QmSomeHash...',
)
DAGLink instance methods and properties
link.Name
link.Tsize
link.Hash
link.toJSON()
link.toString()
See: https://github.com/ipld/interface-ipld-format#local-resolver-methods
dagPB.resolver.resolve
dagPB.resolver.tree
See: https://github.com/ipld/interface-ipld-format#ipld-format-utils
dagPB.util.cid
dagPB.util.serialize
dagPB.util.deserialize
Contribute
Please contribute! Look at the issues!
Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to IPLD are subject to the IPFS Code of Conduct.
Small note: If editing the README, please conform to the standard-readme specification.
License
ISC © 2016 Protocol Labs Inc.