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

Commit 6d65368

Browse files
Alan Shawachingbrain
Alan Shaw
authored andcommitted
refactor: export CID instances not strings or buffers (#19)
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]>
1 parent c37090e commit 6d65368

File tree

11 files changed

+54
-29
lines changed

11 files changed

+54
-29
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,23 @@ pull(
125125
// name: 'QmFoo...',
126126
// path: 'QmFoo...',
127127
// size: ...
128-
// hash: Buffer
128+
// cid: CID
129129
// content: undefined
130130
// type: 'dir'
131131
// }, {
132132
// depth: 1,
133133
// name: 'bar',
134134
// path: 'QmFoo.../bar',
135135
// size: ...
136-
// hash: Buffer
136+
// cid: CID
137137
// content: undefined
138138
// type: 'dir'
139139
// }, {
140140
// depth: 2,
141141
// name: 'baz.txt',
142142
// path: 'QmFoo.../bar/baz.txt',
143143
// size: ...
144-
// hash: Buffer
144+
// cid: CID
145145
// content: <Pull stream>
146146
// type: 'file'
147147
// }]
@@ -172,15 +172,15 @@ pull(
172172
// name: 'QmFoo...',
173173
// path: 'QmFoo...',
174174
// size: ...
175-
// hash: Buffer
175+
// cid: CID
176176
// content: undefined
177177
// type: 'dir'
178178
// }, {
179179
// depth: 1,
180180
// name: 'bar',
181181
// path: 'QmFoo.../bar',
182182
// size: ...
183-
// hash: Buffer
183+
// cid: CID
184184
// content: undefined
185185
// type: 'dir'
186186
// }]

src/dir-flat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
1616
name: name,
1717
depth: depth,
1818
path: path,
19-
multihash: cid.buffer,
19+
cid,
2020
size: node.size,
2121
type: 'dir'
2222
}
@@ -35,7 +35,7 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
3535
size: link.size,
3636
name: link.name,
3737
path: path + '/' + link.name,
38-
multihash: link.cid.buffer,
38+
cid: link.cid,
3939
linkName: link.name,
4040
pathRest: pathRest.slice(1),
4141
type: 'dir'

src/dir-hamt-sharded.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
2121
name: name,
2222
depth: depth,
2323
path: path,
24-
multihash: cid.buffer,
24+
cid,
2525
size: node.size,
2626
type: 'dir'
2727
}
@@ -47,7 +47,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
4747
depth: entryName ? depth + 1 : depth,
4848
name: entryName,
4949
path: entryPath,
50-
multihash: link.cid.buffer,
50+
cid: link.cid,
5151
pathRest: entryName ? pathRest.slice(1) : pathRest,
5252
parent: dir || parent
5353
}
@@ -123,7 +123,7 @@ function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag
123123
depth: entryName ? depth + 1 : depth,
124124
name: entryName,
125125
path: entryPath,
126-
multihash: link.cid.buffer,
126+
cid: link.cid,
127127
pathRest: entryName ? pathRest.slice(1) : pathRest,
128128
parent: dir || parent
129129
}

src/file.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
5252
content: once(Buffer.alloc(0)),
5353
name: name,
5454
path: path,
55-
multihash: cid.buffer,
55+
cid,
5656
size: fileSize,
5757
type: 'file'
5858
})
@@ -73,7 +73,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
7373
content: content,
7474
name: name,
7575
path: path,
76-
multihash: cid.buffer,
76+
cid,
7777
size: fileSize,
7878
type: 'file'
7979
}])

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = (path, dag, options) => {
6363

6464
return pull(
6565
values([{
66-
multihash: cid.buffer,
66+
cid,
6767
name: dPath.base,
6868
path: dPath.base,
6969
pathRest: dPath.rest,
@@ -77,7 +77,7 @@ module.exports = (path, dag, options) => {
7777
name: node.name,
7878
path: options.fullPath ? node.path : finalPathFor(node),
7979
size: node.size,
80-
hash: node.multihash,
80+
cid: node.cid,
8181
content: node.content,
8282
type: node.type
8383
}

src/object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
2323
name: pathElem,
2424
path: newName,
2525
pathRest: pathRest.slice(1),
26-
multihash: isCID && newNode,
26+
cid: isCID && newNode,
2727
object: !isCID && newNode,
2828
parent: parent
2929
}]),

src/raw.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
3434
return once({
3535
depth,
3636
content: once(Buffer.alloc(0)),
37-
hash: cid,
37+
cid,
3838
name,
3939
path,
4040
size,
@@ -53,7 +53,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
5353
return once({
5454
depth,
5555
content: once(extractDataFromBlock(node, 0, offset, offset + length)),
56-
hash: cid,
56+
cid,
5757
name,
5858
path,
5959
size,

src/resolve.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const filter = require('pull-stream/throughs/filter')
77
const flatten = require('pull-stream/throughs/flatten')
88
const map = require('pull-stream/throughs/map')
99
const paramap = require('pull-paramap')
10-
const CID = require('cids')
1110
const waterfall = require('async/waterfall')
1211

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

45-
const cid = new CID(item.multihash)
46-
4744
waterfall([
48-
(done) => dag.get(cid, done),
49-
(node, done) => done(null, resolveItem(cid, node.value, item, options))
45+
(done) => dag.get(item.cid, done),
46+
(node, done) => done(null, resolveItem(item.cid, node.value, item, options))
5047
], cb)
5148
}),
5249
flatten(),

test/exporter-sharded.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('exporter sharded', function () {
111111
(exported, cb) => {
112112
const dir = exported.shift()
113113

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

117117
parallel(
@@ -124,7 +124,7 @@ describe('exporter sharded', function () {
124124
}
125125

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

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

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

182182
cb()

test/exporter-subtree.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('exporter subtree', () => {
5151
pull.collect((err, files) => cb(err, { cid, files }))
5252
),
5353
({ cid, files }, cb) => {
54-
files.forEach(file => expect(file).to.have.property('hash'))
54+
files.forEach(file => expect(file).to.have.property('cid'))
5555

5656
expect(files.length).to.equal(1)
5757
expect(files[0].path).to.equal('200Bytes.txt')

test/exporter.spec.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('exporter', () => {
199199
function onFiles (err, files) {
200200
expect(err).to.equal(null)
201201
expect(files).to.have.length(1)
202-
expect(files[0]).to.have.property('hash')
202+
expect(files[0]).to.have.property('cid')
203203
expect(files[0]).to.have.property('path', result.cid.toBaseEncodedString())
204204
fileEql(files[0], unmarsh.data, done)
205205
}
@@ -487,7 +487,7 @@ describe('exporter', () => {
487487
pull.collect((err, files) => cb(err, { cid, files }))
488488
),
489489
({ cid, files }, cb) => {
490-
files.forEach(file => expect(file).to.have.property('hash'))
490+
files.forEach(file => expect(file).to.have.property('cid'))
491491

492492
expect(
493493
files.map((file) => file.path)
@@ -541,7 +541,7 @@ describe('exporter', () => {
541541
pull.collect((err, files) => cb(err, { cid, files }))
542542
),
543543
({ cid, files }, cb) => {
544-
files.forEach(file => expect(file).to.have.property('hash'))
544+
files.forEach(file => expect(file).to.have.property('cid'))
545545

546546
expect(
547547
files.map((file) => file.path)
@@ -1066,6 +1066,34 @@ describe('exporter', () => {
10661066
)
10671067
}
10681068
})
1069+
1070+
it('exports a raw leaf', (done) => {
1071+
pull(
1072+
pull.values([{
1073+
path: '200Bytes.txt',
1074+
content: pull.values([smallFile])
1075+
}]),
1076+
importer(ipld, {
1077+
rawLeaves: true
1078+
}),
1079+
pull.collect(collected)
1080+
)
1081+
1082+
function collected (err, files) {
1083+
expect(err).to.not.exist()
1084+
expect(files.length).to.equal(1)
1085+
1086+
pull(
1087+
exporter(files[0].multihash, ipld),
1088+
pull.collect((err, files) => {
1089+
expect(err).to.not.exist()
1090+
expect(files.length).to.equal(1)
1091+
expect(CID.isCID(files[0].cid)).to.be.true()
1092+
fileEql(files[0], smallFile, done)
1093+
})
1094+
)
1095+
}
1096+
})
10691097
})
10701098

10711099
function fileEql (actual, expected, done) {

0 commit comments

Comments
 (0)