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

Commit 4a02ee7

Browse files
authored
feat: support exporting nodes encoded with the identity hash (#27)
* feat: support exporting nodes encoded with the identity hash We can encode small amounts of data inside CIDs so we should be able to export them too. * chore: update require
1 parent d12178a commit 4a02ee7

File tree

5 files changed

+110
-10
lines changed

5 files changed

+110
-10
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"ipld-in-memory": "^3.0.0",
5050
"ipfs-unixfs-importer": "^0.42.0",
5151
"multicodec": "^1.0.0",
52-
"multihashes": "^0.4.14",
5352
"nyc": "^15.0.0",
5453
"sinon": "^8.0.4"
5554
},

src/resolvers/identity.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
3+
const errCode = require('err-code')
4+
const extractDataFromBlock = require('../utils/extract-data-from-block')
5+
const validateOffsetAndLength = require('../utils/validate-offset-and-length')
6+
const mh = require('multihashing-async').multihash
7+
8+
const rawContent = (node) => {
9+
return function * (options = {}) {
10+
const {
11+
offset,
12+
length
13+
} = validateOffsetAndLength(node.length, options.offset, options.length)
14+
15+
yield extractDataFromBlock(node, 0, offset, offset + length)
16+
}
17+
}
18+
19+
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld) => {
20+
if (toResolve.length) {
21+
throw errCode(new Error(`No link named ${path} found in raw node ${cid.toBaseEncodedString()}`), 'ERR_NOT_FOUND')
22+
}
23+
24+
const buf = await mh.decode(cid.multihash)
25+
26+
return {
27+
entry: {
28+
name,
29+
path,
30+
cid,
31+
node: buf,
32+
content: rawContent(buf.digest),
33+
depth
34+
}
35+
}
36+
}
37+
38+
module.exports = resolve

src/resolvers/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const errCode = require('err-code')
55
const resolvers = {
66
'dag-pb': require('./unixfs-v1'),
77
raw: require('./raw'),
8-
'dag-cbor': require('./dag-cbor')
8+
'dag-cbor': require('./dag-cbor'),
9+
identity: require('./identity')
910
}
1011

1112
const resolve = (cid, name, path, toResolve, depth, ipld) => {

test/exporter-sharded.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const expect = chai.expect
77
const IPLD = require('ipld')
88
const inMemory = require('ipld-in-memory')
99
const UnixFS = require('ipfs-unixfs')
10-
const mh = require('multihashes')
10+
const mh = require('multihashing-async').multihash
1111
const mc = require('multicodec')
1212
const all = require('async-iterator-all')
1313
const last = require('async-iterator-last')
@@ -184,15 +184,15 @@ describe('exporter sharded', function () {
184184
it('exports a file from a sharded directory inside a regular directory inside a sharded directory', async () => {
185185
const dirCid = await createShard(15)
186186

187-
const node = new DAGNode(new UnixFS('directory').marshal(), [
187+
const node = new DAGNode(new UnixFS({ type: 'directory' }).marshal(), [
188188
new DAGLink('shard', 5, dirCid)
189189
])
190190
const nodeCid = await ipld.put(node, mc.DAG_PB, {
191191
cidVersion: 0,
192192
hashAlg: mh.names['sha2-256']
193193
})
194194

195-
const shardNode = new DAGNode(new UnixFS('hamt-sharded-directory').marshal(), [
195+
const shardNode = new DAGNode(new UnixFS({ type: 'hamt-sharded-directory' }).marshal(), [
196196
new DAGLink('75normal-dir', 5, nodeCid)
197197
])
198198
const shardNodeCid = await ipld.put(shardNode, mc.DAG_PB, {

test/exporter.spec.js

+67-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
DAGNode,
1313
DAGLink
1414
} = require('ipld-dag-pb')
15-
const mh = require('multihashes')
15+
const mh = require('multihashing-async').multihash
1616
const mc = require('multicodec')
1717
const exporter = require('../src')
1818
const importer = require('ipfs-unixfs-importer')
@@ -38,7 +38,10 @@ describe('exporter', () => {
3838
options.content = options.content || Buffer.from([0x01, 0x02, 0x03])
3939
options.links = options.links || []
4040

41-
const file = new UnixFS(options.type, options.content)
41+
const file = new UnixFS({
42+
type: options.type,
43+
data: options.content
44+
})
4245

4346
const node = new DAGNode(file.marshal(), options.links)
4447
const cid = await ipld.put(node, mc.DAG_PB, {
@@ -190,7 +193,10 @@ describe('exporter', () => {
190193

191194
it('exports a small file with links', async () => {
192195
const content = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
193-
const chunk1 = new UnixFS('raw', content.slice(0, 5))
196+
const chunk1 = new UnixFS({
197+
type: 'raw',
198+
data: content.slice(0, 5)
199+
})
194200
const chunkNode1 = new DAGNode(chunk1.marshal())
195201
const chunkCid1 = await ipld.put(chunkNode1, mc.DAG_PB, {
196202
cidVersion: 0,
@@ -204,7 +210,9 @@ describe('exporter', () => {
204210
hashAlg: mh.names['sha2-256']
205211
})
206212

207-
const file = new UnixFS('file')
213+
const file = new UnixFS({
214+
type: 'file'
215+
})
208216
file.addBlockSize(5)
209217
file.addBlockSize(5)
210218

@@ -830,7 +838,9 @@ describe('exporter', () => {
830838
foo: 'bar'
831839
}, mc.DAG_CBOR)
832840

833-
const file = new UnixFS('file')
841+
const file = new UnixFS({
842+
type: 'file'
843+
})
834844
file.addBlockSize(100)
835845

836846
const cid = await ipld.put(new DAGNode(file.marshal(), [
@@ -891,4 +901,56 @@ describe('exporter', () => {
891901
expect(exported[4].name).to.equal('qux.txt')
892902
expect(exported[4].path).to.equal(`${dirCid}/qux.txt`)
893903
})
904+
905+
it('exports a CID encoded with the identity hash', async () => {
906+
const data = Buffer.from('hello world')
907+
const hash = mh.encode(data, 'identity')
908+
const cid = new CID(1, 'identity', hash)
909+
910+
const exported = await exporter(cid, ipld)
911+
const result = Buffer.concat(await all(exported.content()))
912+
913+
expect(result).to.deep.equal(data)
914+
expect(result.toString('utf8')).to.equal('hello world')
915+
})
916+
917+
it('exports a CID encoded with the identity hash with an offset', async () => {
918+
const data = Buffer.from('hello world')
919+
const hash = mh.encode(data, 'identity')
920+
const cid = new CID(1, 'identity', hash)
921+
922+
const exported = await exporter(cid, ipld)
923+
const result = Buffer.concat(await all(exported.content({
924+
offset: 1
925+
})))
926+
927+
expect(result.toString('utf8')).to.equal('ello world')
928+
})
929+
930+
it('exports a CID encoded with the identity hash with a length', async () => {
931+
const data = Buffer.from('hello world')
932+
const hash = mh.encode(data, 'identity')
933+
const cid = new CID(1, 'identity', hash)
934+
935+
const exported = await exporter(cid, ipld)
936+
const result = Buffer.concat(await all(exported.content({
937+
length: 1
938+
})))
939+
940+
expect(result.toString('utf8')).to.equal('h')
941+
})
942+
943+
it('exports a CID encoded with the identity hash with an offset and a length', async () => {
944+
const data = Buffer.from('hello world')
945+
const hash = mh.encode(data, 'identity')
946+
const cid = new CID(1, 'identity', hash)
947+
948+
const exported = await exporter(cid, ipld)
949+
const result = Buffer.concat(await all(exported.content({
950+
offset: 3,
951+
length: 1
952+
})))
953+
954+
expect(result.toString('utf8')).to.equal('l')
955+
})
894956
})

0 commit comments

Comments
 (0)