Skip to content

Commit

Permalink
refactor: export CID instances not strings or buffers (#19)
Browse files Browse the repository at this point in the history
This PR updates the exporter to remove the `hash` property and replace it with a `cid` property in exported objects.

refs ipfs-inactive/interface-js-ipfs-core#394

* refactor: export CID instances not strings or buffers
* docs: update the docs
* fix: yield cid not hash for raw
* test: add test to ensure an exported raw leaf has a cid

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw authored and achingbrain committed Mar 8, 2019
1 parent c37090e commit 6d65368
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 29 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,23 @@ pull(
// name: 'QmFoo...',
// path: 'QmFoo...',
// size: ...
// hash: Buffer
// cid: CID
// content: undefined
// type: 'dir'
// }, {
// depth: 1,
// name: 'bar',
// path: 'QmFoo.../bar',
// size: ...
// hash: Buffer
// cid: CID
// content: undefined
// type: 'dir'
// }, {
// depth: 2,
// name: 'baz.txt',
// path: 'QmFoo.../bar/baz.txt',
// size: ...
// hash: Buffer
// cid: CID
// content: <Pull stream>
// type: 'file'
// }]
Expand Down Expand Up @@ -172,15 +172,15 @@ pull(
// name: 'QmFoo...',
// path: 'QmFoo...',
// size: ...
// hash: Buffer
// cid: CID
// content: undefined
// type: 'dir'
// }, {
// depth: 1,
// name: 'bar',
// path: 'QmFoo.../bar',
// size: ...
// hash: Buffer
// cid: CID
// content: undefined
// type: 'dir'
// }]
Expand Down
4 changes: 2 additions & 2 deletions src/dir-flat.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
name: name,
depth: depth,
path: path,
multihash: cid.buffer,
cid,
size: node.size,
type: 'dir'
}
Expand All @@ -35,7 +35,7 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
size: link.size,
name: link.name,
path: path + '/' + link.name,
multihash: link.cid.buffer,
cid: link.cid,
linkName: link.name,
pathRest: pathRest.slice(1),
type: 'dir'
Expand Down
6 changes: 3 additions & 3 deletions src/dir-hamt-sharded.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
name: name,
depth: depth,
path: path,
multihash: cid.buffer,
cid,
size: node.size,
type: 'dir'
}
Expand All @@ -47,7 +47,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
depth: entryName ? depth + 1 : depth,
name: entryName,
path: entryPath,
multihash: link.cid.buffer,
cid: link.cid,
pathRest: entryName ? pathRest.slice(1) : pathRest,
parent: dir || parent
}
Expand Down Expand Up @@ -123,7 +123,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
depth: entryName ? depth + 1 : depth,
name: entryName,
path: entryPath,
multihash: link.cid.buffer,
cid: link.cid,
pathRest: entryName ? pathRest.slice(1) : pathRest,
parent: dir || parent
}
Expand Down
4 changes: 2 additions & 2 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
content: once(Buffer.alloc(0)),
name: name,
path: path,
multihash: cid.buffer,
cid,
size: fileSize,
type: 'file'
})
Expand All @@ -73,7 +73,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
content: content,
name: name,
path: path,
multihash: cid.buffer,
cid,
size: fileSize,
type: 'file'
}])
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = (path, dag, options) => {

return pull(
values([{
multihash: cid.buffer,
cid,
name: dPath.base,
path: dPath.base,
pathRest: dPath.rest,
Expand All @@ -77,7 +77,7 @@ module.exports = (path, dag, options) => {
name: node.name,
path: options.fullPath ? node.path : finalPathFor(node),
size: node.size,
hash: node.multihash,
cid: node.cid,
content: node.content,
type: node.type
}
Expand Down
2 changes: 1 addition & 1 deletion src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
name: pathElem,
path: newName,
pathRest: pathRest.slice(1),
multihash: isCID && newNode,
cid: isCID && newNode,
object: !isCID && newNode,
parent: parent
}]),
Expand Down
4 changes: 2 additions & 2 deletions src/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
return once({
depth,
content: once(Buffer.alloc(0)),
hash: cid,
cid,
name,
path,
size,
Expand All @@ -53,7 +53,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
return once({
depth,
content: once(extractDataFromBlock(node, 0, offset, offset + length)),
hash: cid,
cid,
name,
path,
size,
Expand Down
7 changes: 2 additions & 5 deletions src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const filter = require('pull-stream/throughs/filter')
const flatten = require('pull-stream/throughs/flatten')
const map = require('pull-stream/throughs/map')
const paramap = require('pull-paramap')
const CID = require('cids')
const waterfall = require('async/waterfall')

const resolvers = {
Expand Down Expand Up @@ -42,11 +41,9 @@ function createResolver (dag, options, depth, parent) {
return cb(null, resolveItem(null, item.object, item, options))
}

const cid = new CID(item.multihash)

waterfall([
(done) => dag.get(cid, done),
(node, done) => done(null, resolveItem(cid, node.value, item, options))
(done) => dag.get(item.cid, done),
(node, done) => done(null, resolveItem(item.cid, node.value, item, options))
], cb)
}),
flatten(),
Expand Down
6 changes: 3 additions & 3 deletions test/exporter-sharded.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('exporter sharded', function () {
(exported, cb) => {
const dir = exported.shift()

expect(dir.hash).to.deep.equal(directory.buffer)
expect(dir.cid.equals(directory)).to.be.true()
expect(exported.length).to.equal(Object.keys(files).length)

parallel(
Expand All @@ -124,7 +124,7 @@ describe('exporter sharded', function () {
}

// validate the CID
expect(files[exported.name].cid.buffer).to.deep.equal(exported.hash)
expect(files[exported.name].cid.equals(exported.cid)).to.be.true()

// validate the exported file content
expect(files[exported.name].content).to.deep.equal(bufs[0])
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('exporter sharded', function () {
(exported, cb) => {
const dir = exported.shift()

expect(dir.hash).to.deep.equal(dirCid.buffer)
expect(dir.cid.equals(dirCid)).to.be.true()
expect(exported.length).to.equal(Object.keys(files).length)

cb()
Expand Down
2 changes: 1 addition & 1 deletion test/exporter-subtree.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('exporter subtree', () => {
pull.collect((err, files) => cb(err, { cid, files }))
),
({ cid, files }, cb) => {
files.forEach(file => expect(file).to.have.property('hash'))
files.forEach(file => expect(file).to.have.property('cid'))

expect(files.length).to.equal(1)
expect(files[0].path).to.equal('200Bytes.txt')
Expand Down
34 changes: 31 additions & 3 deletions test/exporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('exporter', () => {
function onFiles (err, files) {
expect(err).to.equal(null)
expect(files).to.have.length(1)
expect(files[0]).to.have.property('hash')
expect(files[0]).to.have.property('cid')
expect(files[0]).to.have.property('path', result.cid.toBaseEncodedString())
fileEql(files[0], unmarsh.data, done)
}
Expand Down Expand Up @@ -487,7 +487,7 @@ describe('exporter', () => {
pull.collect((err, files) => cb(err, { cid, files }))
),
({ cid, files }, cb) => {
files.forEach(file => expect(file).to.have.property('hash'))
files.forEach(file => expect(file).to.have.property('cid'))

expect(
files.map((file) => file.path)
Expand Down Expand Up @@ -541,7 +541,7 @@ describe('exporter', () => {
pull.collect((err, files) => cb(err, { cid, files }))
),
({ cid, files }, cb) => {
files.forEach(file => expect(file).to.have.property('hash'))
files.forEach(file => expect(file).to.have.property('cid'))

expect(
files.map((file) => file.path)
Expand Down Expand Up @@ -1066,6 +1066,34 @@ describe('exporter', () => {
)
}
})

it('exports a raw leaf', (done) => {
pull(
pull.values([{
path: '200Bytes.txt',
content: pull.values([smallFile])
}]),
importer(ipld, {
rawLeaves: true
}),
pull.collect(collected)
)

function collected (err, files) {
expect(err).to.not.exist()
expect(files.length).to.equal(1)

pull(
exporter(files[0].multihash, ipld),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(files.length).to.equal(1)
expect(CID.isCID(files[0].cid)).to.be.true()
fileEql(files[0], smallFile, done)
})
)
}
})
})

function fileEql (actual, expected, done) {
Expand Down

0 comments on commit 6d65368

Please sign in to comment.