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

refactor: use the new IPLD API #20

Closed
wants to merge 1 commit into from
Closed
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
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.36.1",
"ipld": "~0.21.1",
"ipfs-unixfs-exporter": "git+https://github.com/ipfs/js-ipfs-unixfs-exporter.git#new-ipld-api",
"ipld": "~0.22.0",
"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
33 changes: 21 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,26 @@ 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]
}

ipld.put(node, codec, {
cidVersion,
hashAlg
}).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 cid = new CID(node.multihash)
expect(ipld.get(cid)).to.be.rejectedWith('Not Found').notify(done)
}

const content = String(Math.random() + Date.now())
Expand Down
28 changes: 16 additions & 12 deletions test/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ 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()
})
ipld.get(cid).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 +126,14 @@ 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()
})
ipld.get(cid).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).then(
(node) => callback(null, node),
(error) => callback(error)
)
}),
asyncMap((node, callback) => {
if (!node.links) {
Expand Down
35 changes: 21 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)).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).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)).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 cid = new CID(file.multihash)
expect(ipld.get(cid)).to.be.rejectedWith('Not Found').notify(done)
}

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

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

// Check the imported content is correct
Expand Down