Skip to content

Commit

Permalink
fix: order tree directory entries correctly (fixes ipld#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
sameer committed Mar 22, 2019
1 parent 4519644 commit ae6432b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/util/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ exports.serialize = (dagNode, callback) => {
Object.keys(dagNode).forEach((name) => {
entries.push([name, dagNode[name]])
})
entries.sort((a, b) => a[0] > b[0] ? 1 : -1)
entries.sort((a, b) => {
const aName = a[0] + (a[1].mode === '40000' ? '/' : '')
const bName = b[0] + (b[1].mode === '40000' ? '/' : '')
return aName > bName ? 1 : -1
})
let buf = new SmartBuffer()
entries.forEach((entry) => {
buf.writeStringNT(entry[1].mode + ' ' + entry[0])
Expand Down
16 changes: 15 additions & 1 deletion test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ describe('IPLD format resolver (local)', () => {
mode: '40000'
}
}
treeNode['somedir.notactuallyadir'] = {
hash: { '/': new CID('z8mWaJNVTadD7oum3m7f1dmarHvYhFV5b').buffer },
mode: '100644'
}
treeNode['somefile.notactuallyafile'] = {
hash: { '/': new CID('z8mWaFY1zpiZSXTBrz8i6A3o9vNvAs2CH').buffer },
mode: '40000'
}

const blobNode = Buffer.from('626c6f62203800736f6d6564617461', 'hex') // blob 8\0somedata

Expand Down Expand Up @@ -228,12 +236,18 @@ describe('IPLD format resolver (local)', () => {
expect(err).to.not.exist()

expect(paths).to.eql([
'somedir.notactuallyadir',
'somedir.notactuallyadir/hash',
'somedir.notactuallyadir/mode',
'somedir',
'somedir/hash',
'somedir/mode',
'somefile',
'somefile/hash',
'somefile/mode'
'somefile/mode',
'somefile.notactuallyafile',
'somefile.notactuallyafile/hash',
'somefile.notactuallyafile/mode'
])

done()
Expand Down

0 comments on commit ae6432b

Please sign in to comment.