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

Converting string input data to Buffer. #11

Merged
merged 2 commits into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const dagPB = require('ipld-dag-pb')
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.DAGNode.rmLink // remove a Link to a DAGNode, creating a new one
dagPB.DAGLink.create // create a DAGLink

// IPLD Format specifics
Expand All @@ -84,11 +84,15 @@ dagPB.util
#### Create a DAGNode

```JavaScript
DAGNode.create(new Buffer('some data'), (err, node) => {
DAGNode.create(new Buffer('some data'), (err, node1) => {
if (err) {
throw error
}
// node is your DAGNode instance
// node1 is your DAGNode instance.
})

DAGNode.create('string data', (err, node2) => {
// node2 will have the same data as node1.
})
```

Expand Down
2 changes: 2 additions & 0 deletions src/dag-node/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function create (data, dagLinks, hashAlg, callback) {
if (typeof data === 'function') {
callback = data
data = undefined
} else if (typeof data === 'string') {
data = new Buffer(data)
}
if (typeof dagLinks === 'function') {
callback = dagLinks
Expand Down
23 changes: 23 additions & 0 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ module.exports = (repo) => {
expect(node.data.length).to.be.above(0).mark()
expect(Buffer.isBuffer(node.data)).to.be.true.mark()
expect(node.size).to.be.above(0).mark()
expect(node.toJSON().multihash).to.be.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')

dagPB.util.serialize(node, (err, serialized) => {
expect(err).to.not.exist.mark()

dagPB.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist.mark()
expect(node.data).to.eql(deserialized.data).mark()
})
})
})
})

it('create a node with string data', (done) => {
expect(7).checks(done)
const data = 'some data'

DAGNode.create(data, (err, node) => {
expect(err).to.not.exist.mark()
expect(node.data.length).to.be.above(0).mark()
expect(Buffer.isBuffer(node.data)).to.be.true.mark()
expect(node.size).to.be.above(0).mark()
expect(node.toJSON().multihash).to.be.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')

dagPB.util.serialize(node, (err, serialized) => {
expect(err).to.not.exist.mark()
Expand Down