Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
refactor: use the new IPLD API
Browse files Browse the repository at this point in the history
This is part of the Awesome Endeavour: Async Iterators:
ipfs/js-ipfs#1670
  • Loading branch information
vmx committed Feb 7, 2019
1 parent bc222c9 commit 50bcb3d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 48 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
"devDependencies": {
"aegir": "^18.0.2",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cids": "~0.5.5",
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"ipfs-unixfs-exporter": "~0.35.4",
"ipld": "~0.20.2",
"ipfs-unixfs-exporter": "git+https://github.com/ipfs/js-ipfs-unixfs-exporter.git#new-ipld-api",
"ipld": "git+https://github.com/ipld/js-ipld.git#new-api-impl",
"ipld-in-memory": "^2.0.0",
"multihashes": "~0.4.14",
"pull-buffer-stream": "^1.0.1",
Expand All @@ -60,6 +61,7 @@
"ipfs-unixfs": "~0.1.16",
"ipld-dag-pb": "~0.15.2",
"left-pad": "^1.3.0",
"multicodec": "~0.5.0",
"multihashing-async": "~0.5.1",
"pull-batch": "^1.0.0",
"pull-pair": "^1.1.0",
Expand Down
34 changes: 22 additions & 12 deletions src/utils/persist.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'

const {
util: {
cid
}
util
} = require('ipld-dag-pb')
const multicodec = require('multicodec')

const defaultOptions = {
cidVersion: 0,
Expand All @@ -27,7 +26,7 @@ const persist = (node, ipld, options, callback) => {
}

if (options.onlyHash) {
return cid(node, {
return util.cid(node, {
version: cidVersion,
hashAlg: hashAlg
}, (err, cid) => {
Expand All @@ -38,16 +37,27 @@ const persist = (node, ipld, options, callback) => {
})
}

ipld.put(node, {
version: cidVersion,
hashAlg: hashAlg,
format: codec
}, (error, cid) => {
callback(error, {
// The IPLD expects the format and hashAlg as constants
if (typeof codec === 'string') {
const constantName = codec.toUpperCase().replace(/-/g, '_')
codec = multicodec[constantName]
}
if (typeof hashAlg === 'string') {
const constantName = hashAlg.toUpperCase().replace(/-/g, '_')
hashAlg = multicodec[constantName]
}

const result = ipld.put([node], codec, {
cidVersion,
hashAlg
})
result.first().then(
(cid) => callback(null, {
cid,
node
})
})
}),
(error) => callback(error)
)
}

module.exports = persist
9 changes: 4 additions & 5 deletions test/builder-only-hash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict'

const chai = require('chai')
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))
const expect = chai.expect
const pull = require('pull-stream/pull')
Expand All @@ -27,16 +28,14 @@ describe('builder: onlyHash', () => {
})

it('will only chunk and hash if passed an "onlyHash" option', (done) => {
const onCollected = (err, nodes) => {
const onCollected = async (err, nodes) => {
if (err) return done(err)

const node = nodes[0]
expect(node).to.exist()

ipld.get(new CID(node.multihash), (err, res) => {
expect(err).to.exist()
done()
})
const result = ipld.get([new CID(node.multihash)])
expect(result.first()).to.be.rejectedWith('Not Found').notify(done)
}

const content = String(Math.random() + Date.now())
Expand Down
30 changes: 18 additions & 12 deletions test/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ describe('builder', () => {
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)

// Fetch using hashAlg encoded multihash
ipld.get(cid, (err, res) => {
if (err) return cb(err)
const content = UnixFS.unmarshal(res.value.data).data
expect(content.equals(inputFile.content)).to.be.true()
cb()
})
const result = ipld.get([cid])
result.first().then(
(node) => {
const content = UnixFS.unmarshal(node.data).data
expect(content.equals(inputFile.content)).to.be.true()
cb()
},
(error) => cb(error)
)
}

pull(
Expand Down Expand Up @@ -124,12 +127,15 @@ describe('builder', () => {
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)

// Fetch using hashAlg encoded multihash
ipld.get(cid, (err, res) => {
if (err) return cb(err)
const meta = UnixFS.unmarshal(res.value.data)
expect(meta.type).to.equal('directory')
cb()
})
const result = ipld.get([cid])
result.first().then(
(node) => {
const meta = UnixFS.unmarshal(node.data)
expect(meta.type).to.equal('directory')
cb()
},
(error) => cb(error)
)
}

pull(
Expand Down
7 changes: 4 additions & 3 deletions test/helpers/collect-leaf-cids.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ module.exports = (ipld, multihash, callback) => {
return pull(
values([cid]),
asyncMap((cid, callback) => {
ipld.get(cid, (error, result) => {
callback(error, !error && result.value)
})
ipld.get([cid]).first().then(
(node) => callback(null, node),
(error) => callback(error)
)
}),
asyncMap((node, callback) => {
if (!node.links) {
Expand Down
36 changes: 22 additions & 14 deletions test/importer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ const checkLeafNodeTypes = (ipld, options, expected, done) => {
importer(ipld, options),
collect(cb)
),
(files, cb) => ipld.get(new CID(files[0].multihash), cb),
(result, cb) => {
const node = result.value
(files, cb) => ipld.get([new CID(files[0].multihash)]).first().then(
(nodes) => cb(null, nodes),
(error) => cb(error)
),
(node, cb) => {
const meta = UnixFs.unmarshal(node.data)

expect(meta.type).to.equal('file')
Expand All @@ -137,9 +139,11 @@ const checkLeafNodeTypes = (ipld, options, expected, done) => {
node.links.map(link => {
return (done) => {
waterfall([
(next) => ipld.get(link.cid, next),
(result, next) => {
const node = result.value
(cb) => ipld.get([link.cid]).first().then(
(node) => cb(null, node),
(error) => cb(error)
),
(node, next) => {
const meta = UnixFs.unmarshal(node.data)

expect(meta.type).to.equal(expected)
Expand All @@ -163,9 +167,11 @@ const checkNodeLinks = (ipld, options, expected, done) => {
importer(ipld, options),
collect(cb)
),
(files, cb) => ipld.get(new CID(files[0].multihash), cb),
(result, cb) => {
const node = result.value
(files, cb) => ipld.get([new CID(files[0].multihash)]).first().then(
(nodes) => cb(null, nodes),
(error) => cb(error)
),
(node, cb) => {
const meta = UnixFs.unmarshal(node.data)

expect(meta.type).to.equal('file')
Expand Down Expand Up @@ -587,10 +593,8 @@ strategies.forEach((strategy) => {
const file = files[0]
expect(file).to.exist()

ipld.get(new CID(file.multihash), (err) => {
expect(err).to.exist()
done()
})
const result = ipld.get([new CID(file.multihash)])
expect(result.first()).to.be.rejectedWith('Not Found').notify(done)
}

pull(
Expand Down Expand Up @@ -658,7 +662,11 @@ strategies.forEach((strategy) => {

// Just check the intermediate directory can be retrieved
if (!inputFile) {
return ipld.get(cid, cb)
const result = ipld.get([new CID(file.multihash)])
return result.first().then(
(_cid) => cb(),
(_error) => cb()
)
}

// Check the imported content is correct
Expand Down

0 comments on commit 50bcb3d

Please sign in to comment.