Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit f4d9f0d

Browse files
authored
Merge pull request #9 from ipld/fix/unixfs
[WIP] Fix various issues found when testing unixfs-engine
2 parents 72c7223 + 405dd01 commit f4d9f0d

9 files changed

+151
-19
lines changed

.aegir.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
karma: {
5+
files: [{
6+
pattern: 'test/data/**/*',
7+
watched: false,
8+
served: true,
9+
included: false
10+
}]
11+
}
12+
}

src/dag-node/create.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const multihashing = require('multihashing-async')
4-
const sortInplace = require('stable')
4+
const sort = require('stable')
55
const dagPBUtil = require('../util.js')
66
const serialize = dagPBUtil.serialize
77
const dagNodeUtil = require('./util.js')
@@ -28,21 +28,21 @@ function create (data, dagLinks, hashAlg, callback) {
2828
}
2929

3030
const links = dagLinks.map((l) => {
31-
if (!l.constructor && l.constructor.name !== 'DAGLink') {
31+
if (l.constructor && l.constructor.name === 'DAGLink') {
3232
return l
3333
}
3434

3535
return new DAGLink(
36-
l.name || l.Name,
37-
l.size || l.Size,
38-
l.hash || l.Hash || l.multihash)
36+
l.name ? l.name : l.Name,
37+
l.size ? l.size : l.Size,
38+
l.hash || l.Hash || l.multihash
39+
)
3940
})
40-
41-
sortInplace(links, linkSort)
41+
const sortedLinks = sort(links, linkSort)
4242

4343
serialize({
4444
data: data,
45-
links: links
45+
links: sortedLinks
4646
}, (err, serialized) => {
4747
if (err) {
4848
return callback(err)
@@ -51,7 +51,7 @@ function create (data, dagLinks, hashAlg, callback) {
5151
if (err) {
5252
return callback(err)
5353
}
54-
const dagNode = new DAGNode(data, links, serialized, multihash)
54+
const dagNode = new DAGNode(data, sortedLinks, serialized, multihash)
5555
callback(null, dagNode)
5656
})
5757
})

src/dag-node/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DAGNode {
2121

2222
this._json = {
2323
data: this.data,
24-
links: this.links.map((l) => l.json),
24+
links: this.links.map((l) => l.toJSON()),
2525
multihash: mh.toB58String(this.multihash),
2626
size: this.size
2727
}

src/dag-node/util.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function cloneLinks (dagNode) {
2222
}
2323

2424
function linkSort (a, b) {
25-
return (new Buffer(a.name || '', 'ascii').compare(new Buffer(b.name || '', 'ascii')))
25+
const aBuf = new Buffer(a.name || '', 'ascii')
26+
const bBuf = new Buffer(b.name || '', 'ascii')
27+
28+
return aBuf.compare(bBuf)
2629
}
2730

2831
/*

test/dag-link-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ module.exports = (repo) => {
1515
.to.equal('12208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f43')
1616
})
1717

18+
it('empty string', () => {
19+
const link = new DAGLink('', 4, 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')
20+
expect(link.name).to.be.eql('')
21+
})
22+
1823
it('create with multihash as a multihash Buffer', () => {
1924
const link = new DAGLink('hello', 3, new Buffer('12208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f43', 'hex'))
2025

test/dag-node-test.js

+112-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const BlockService = require('ipfs-block-service')
1818
const Block = require('ipfs-block')
1919
const CID = require('cids')
2020
const bs58 = require('bs58')
21+
const loadFixture = require('aegir/fixtures')
22+
23+
const testBlockNamedLinks = loadFixture(__dirname, 'data/test-block-named-links')
24+
const testBlockUnnamedLinks = loadFixture(__dirname, 'data/test-block-unnamed-links')
2125

2226
module.exports = (repo) => {
2327
describe('DAGNode', () => {
@@ -44,11 +48,11 @@ module.exports = (repo) => {
4448

4549
it('create a node with links', (done) => {
4650
const l1 = [{
47-
Name: 'some link',
51+
Name: 'some other link',
4852
Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V',
4953
Size: 8
5054
}, {
51-
Name: 'some other link',
55+
Name: 'some link',
5256
Hash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U',
5357
Size: 10
5458
}]
@@ -73,14 +77,30 @@ module.exports = (repo) => {
7377
DAGNode.create(someData, l2, (err, node) => {
7478
expect(err).to.not.exist
7579
node2 = node
76-
expect(node2.links).to.eql(l2)
80+
expect(node2.links).to.eql([l2[1], l2[0]])
7781
cb()
7882
})
7983
}
8084
], (err) => {
8185
expect(err).to.not.exist
8286
expect(node1.toJSON()).to.eql(node2.toJSON())
8387
expect(node1.serialized).to.eql(node2.serialized)
88+
89+
// check sorting
90+
expect(node1.links.map((l) => l.name)).to.be.eql([
91+
'some link',
92+
'some other link'
93+
])
94+
done()
95+
})
96+
})
97+
98+
it('create with empty link name', (done) => {
99+
DAGNode.create(new Buffer('hello'), [
100+
new DAGLink('', 10, 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')
101+
], (err, node) => {
102+
expect(err).to.not.exist
103+
expect(node.links[0].name).to.be.eql('')
84104
done()
85105
})
86106
})
@@ -134,7 +154,7 @@ module.exports = (repo) => {
134154
.to.eql(node2.multihash)
135155
expect(node1b.links[0].size)
136156
.to.eql(node2.size)
137-
expect(node1b.links[0].name).to.not.exist
157+
expect(node1b.links[0].name).to.be.eql('')
138158
cb()
139159
})
140160
}
@@ -170,7 +190,7 @@ module.exports = (repo) => {
170190
.to.eql(node2.multihash)
171191
expect(node1b.links[0].size)
172192
.to.eql(node2.size)
173-
expect(node1b.links[0].name).to.not.exist
193+
expect(node1b.links[0].name).to.be.eql('')
174194
cb()
175195
})
176196
}
@@ -206,7 +226,7 @@ module.exports = (repo) => {
206226
.to.eql(node2.multihash)
207227
expect(node1b.links[0].size)
208228
.to.eql(node2.size)
209-
expect(node1b.links[0].name).to.not.exist
229+
expect(node1b.links[0].name).to.be.eql('')
210230
cb()
211231
})
212232
}
@@ -391,7 +411,7 @@ module.exports = (repo) => {
391411
})
392412
})
393413

394-
it('read a go-ipfs marshalled node and assert it gets read correctly', (done) => {
414+
it('deserialize go-ipfs block from ipldResolver', (done) => {
395415
const bs = new BlockService(repo)
396416

397417
const cidStr = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
@@ -408,6 +428,91 @@ module.exports = (repo) => {
408428
})
409429
})
410430

431+
it('deserialize go-ipfs block with unnamed links', (done) => {
432+
const buf = testBlockUnnamedLinks
433+
434+
const expectedLinks = [
435+
{
436+
'name': '',
437+
'multihash': 'QmSbCgdsX12C4KDw3PDmpBN9iCzS87a5DjgSCoW9esqzXk',
438+
'size': 45623854
439+
},
440+
{
441+
'name': '',
442+
'multihash': 'Qma4GxWNhywSvWFzPKtEswPGqeZ9mLs2Kt76JuBq9g3fi2',
443+
'size': 45623854
444+
},
445+
{
446+
'name': '',
447+
'multihash': 'QmQfyxyys7a1e3mpz9XsntSsTGc8VgpjPj5BF1a1CGdGNc',
448+
'size': 45623854
449+
},
450+
{
451+
'name': '',
452+
'multihash': 'QmSh2wTTZT4N8fuSeCFw7wterzdqbE93j1XDhfN3vQHzDV',
453+
'size': 45623854
454+
},
455+
{
456+
'name': '',
457+
'multihash': 'QmVXsSVjwxMsCwKRCUxEkGb4f4B98gXVy3ih3v4otvcURK',
458+
'size': 45623854
459+
},
460+
{
461+
'name': '',
462+
'multihash': 'QmZjhH97MEYwQXzCqSQbdjGDhXWuwW4RyikR24pNqytWLj',
463+
'size': 45623854
464+
},
465+
{
466+
'name': '',
467+
'multihash': 'QmRs6U5YirCqC7taTynz3x2GNaHJZ3jDvMVAzaiXppwmNJ',
468+
'size': 32538395
469+
}
470+
]
471+
472+
dagPB.util.deserialize(buf, (err, node) => {
473+
expect(err).to.not.exist
474+
const nodeJSON = node.toJSON()
475+
expect(nodeJSON.links).to.eql(expectedLinks)
476+
expect(nodeJSON.multihash).to.eql('QmQqy2SiEkKgr2cw5UbQ93TtLKEMsD8TdcWggR8q9JabjX')
477+
done()
478+
})
479+
})
480+
481+
it('deserialize go-ipfs block with named links', (done) => {
482+
const buf = testBlockNamedLinks
483+
484+
const expectedLinks = [
485+
{
486+
'name': 'audio_only.m4a',
487+
'multihash': 'QmaUAwAQJNtvUdJB42qNbTTgDpzPYD1qdsKNtctM5i7DGB',
488+
'size': 23319629
489+
},
490+
{
491+
'name': 'chat.txt',
492+
'multihash': 'QmNVrxbB25cKTRuKg2DuhUmBVEK9NmCwWEHtsHPV6YutHw',
493+
'size': 996
494+
},
495+
{
496+
'name': 'playback.m3u',
497+
'multihash': 'QmUcjKzDLXBPmB6BKHeKSh6ZoFZjss4XDhMRdLYRVuvVfu',
498+
'size': 116
499+
},
500+
{
501+
'name': 'zoom_0.mp4',
502+
'multihash': 'QmQqy2SiEkKgr2cw5UbQ93TtLKEMsD8TdcWggR8q9JabjX',
503+
'size': 306281879
504+
}
505+
]
506+
507+
dagPB.util.deserialize(buf, (err, node) => {
508+
expect(err).to.not.exist
509+
const nodeJSON = node.toJSON()
510+
expect(nodeJSON.links).to.eql(expectedLinks)
511+
expect(nodeJSON.multihash).to.eql('QmbSAC58x1tsuPBAoarwGuTQAgghKvdbKSBC8yp5gKCj5M')
512+
done()
513+
})
514+
})
515+
411516
it('dagNode.toJSON with empty Node', (done) => {
412517
DAGNode.create(new Buffer(0), (err, node) => {
413518
expect(err).to.not.exist

test/data/test-block-named-links

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
9
2+
" �9|�UV=3����h�����J�iV�=r�5�audio_only.m4aͨ� 1
3+
" \�Ѩ��DOd��*&��g��h�g�X��I2chat.txt�4
4+
" ]D���(��E�r�*{�v<_��2u���H playback.m3ut6
5+
" %9�n��� }��l�ԛ�.����J<�Bԝ�"
6+
zoom_0.mp4����
7+


test/data/test-block-unnamed-links

360 Bytes
Binary file not shown.

test/resolver.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('IPLD Format resolver (local)', () => {
1717
let dataLinksNodeBlock
1818

1919
const links = [{
20-
name: undefined,
20+
name: '',
2121
multihash: 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U',
2222
size: 10
2323
}, {

0 commit comments

Comments
 (0)