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 all commits
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ node_modules
dist

test/repo-just-for-test*
/test/blocks
/test/blocks

# Vim .swp files
**.swp
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
6 changes: 6 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 All @@ -23,6 +25,10 @@ function create (data, dagLinks, hashAlg, callback) {
hashAlg = undefined
}

if (!Buffer.isBuffer(data)) {
return callback('Passed \'data\' is not a buffer or a string!')
}

if (!hashAlg) {
hashAlg = 'sha2-256'
}
Expand Down
35 changes: 35 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 Expand Up @@ -127,6 +150,18 @@ module.exports = (repo) => {
})
})

it('fail to create a node with other data types', (done) => {
DAGNode.create({}, (err, node) => {
expect(err).to.exist
expect(node).to.not.exist
DAGNode.create([], (err, node) => {
expect(err).to.exist
expect(node).to.not.exist
done()
})
})
})

it('addLink by DAGNode', (done) => {
let node1
let node2
Expand Down