0.22.3 • Published 3 years ago

ipld-dag-pb v0.22.3

Weekly downloads
28,794
License
MIT
Repository
github
Last release
3 years ago

⛔️ DEPRECATED: This module has been superseded by @ipld/dag-pb and multiformats

js-ipld-dag-pb

npm.io npm.io npm.io Travis CI Coverage Status Dependency Status js-standard-style standard-readme compliant Greenkeeper badge npm.io npm.io

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')

// IPLD Format specifics
dagPB.resolver
dagPB.util

Examples

Create a DAGNode

const node1 = new DAGNode(new TextEncoder('utf8').encode('some data'))

// node2 will have the same data as node1
const node2 = new DAGNode('some data')

Add and remove a Link

const link = {
  Name: 'I am a link',
  Hash: 'QmHash..',
  Tsize: 42
}

node.addLink(link)
console.log('with link', node.toJSON())

nodeA.rmLink('I am a link')
console.log('now without link', node.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 constructor

  • data - type: Uint8Array or String
  • links- (optional) type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
  • serializedSize- (optional) type: Number of bytes the serialized node has. If none is given, it will automatically be calculated.

Create a DAGNode.

const dagNode = new DAGNode('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>
}

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()

node.toDAGLink()

  • options - (optional) type: Object. Currently the only option is name to specify a named link.

Converts a DAGNode into a DAGLink.

const node = new DAGNode('some data')
const link = node.toDAGLink()
// Named link
const link = node.toDAGLink({ name: 'name-of-the-link' })

node.addLink(link)

  • link - type: DAGLink or DAGLink in its json format

Creates a link on node A. Modifies the node.

link can be:

  • DAGLink instance
  • DAGNode instance
  • Object with the following properties:
const link = {
  Name: '<some string>', // optional
  Tsize: <size in bytes>,
  Hash: <cid> // can be a String CID, CID buffer or CID object
}

node.addLink(link)

node.rmLink(nameOrCid)

  • nameOrCid - type: String, CID object or CID buffer

Removes a link from the node by name. Modifies the node.

node.rmLink('Link1')

node.serialize()

Serialize the DAGNode instance to its portable binary format. Yields the same result as dagPB.util.serialize(node). Returns a Uint8Array.

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

// link is a DAGLink instance
const link = new DAGLink(
  'link-to-file',  // name of the link (can be empty)
  10,              // size in bytes
  'QmSomeHash...', // can be CID object, CID buffer or string
)

DAGLink instance methods and properties

link.Name

link.Tsize

link.Hash

link.toJSON()

link.toString()

IPLD Format Specifics - Local (node/block scope) resolver

See: https://github.com/ipld/interface-ipld-format#local-resolver-methods

dagPB.resolver.resolve

dagPB.resolver.tree

IPLD Format Specifics - util

See: https://github.com/ipld/interface-ipld-format#ipld-format-utils

dagPB.util.cid

dagPB.util.serialize

Serialize the DAGNode instance to its portable binary format. Yields the same result as node.serialize(). Returns a Uint8Array.

dagPB.util.deserialize

Deserialize a DAGNode instance from its portable binary format. Returns a DAGNode.

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.

0.22.3

3 years ago

0.22.2

3 years ago

0.22.1

3 years ago

0.22.0

3 years ago

0.21.1

3 years ago

0.21.0

3 years ago

0.20.0

4 years ago

0.19.0

4 years ago

0.18.5

4 years ago

0.18.4

4 years ago

0.18.3

4 years ago

0.18.2

4 years ago

0.18.1

5 years ago

0.18.0

5 years ago

0.17.4

5 years ago

0.17.3

5 years ago

0.17.2

5 years ago

0.17.1

5 years ago

0.17.0

5 years ago

0.16.0

5 years ago

0.15.3

5 years ago

0.15.2

5 years ago

0.15.1

5 years ago

0.15.0

5 years ago

0.14.11

6 years ago

0.14.10

6 years ago

0.14.9

6 years ago

0.14.8

6 years ago

0.14.7

6 years ago

0.14.6

6 years ago

0.14.5

6 years ago

0.14.4

6 years ago

0.14.3

6 years ago

0.14.2

6 years ago

0.14.1

6 years ago

0.14.0

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.11.4

6 years ago

0.11.3

6 years ago

0.11.2

7 years ago

0.11.1

7 years ago

0.11.0

7 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.5

7 years ago

0.9.4

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago