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

Commit

Permalink
fix: revert stable module removal
Browse files Browse the repository at this point in the history
Sorting is not stable on Node 10, adds a test that exposes the bug
and reinstates the `stable` module to fix it.

Reverts 3048e3e
  • Loading branch information
achingbrain authored and vmx committed Apr 24, 2020
1 parent 3fc4118 commit 8640b22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"class-is": "^1.1.0",
"multicodec": "^1.0.1",
"multihashing-async": "~0.8.1",
"protons": "^1.0.2"
"protons": "^1.0.2",
"stable": "^0.1.8"
},
"devDependencies": {
"aegir": "^21.9.0",
Expand Down
3 changes: 2 additions & 1 deletion src/dag-node/sortLinks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { Buffer } = require('buffer')
const sort = require('stable')

const linkSort = (a, b) => {
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer)
Expand All @@ -12,7 +13,7 @@ const linkSort = (a, b) => {
* @returns {Array}
*/
const sortLinks = (links) => {
return links.sort(linkSort)
return sort(links, linkSort)
}

module.exports = sortLinks
55 changes: 55 additions & 0 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,61 @@ module.exports = (repo) => {
])
})

it('create a node with sorted links', () => {
const links = [{
Name: '',
Hash: new CID('QmUGhP2X8xo9dsj45vqx1H6i5WqPqLqmLQsHTTxd3ke8mp'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmP7SrR76KHK9A916RbHG1ufy2TzNABZgiE23PjZDMzZXy'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmQg1v4o9xdT3Q14wh4S7dxZkDjyZ9ssFzFzyep1YrVJBY'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmdP6fartWRrydZCUjHgrJ4XpxSE4SAoRsWJZ1zJ4MWiuf'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmNNjUStxtMC1WaSZYiDW6CmAUrvd5Q2e17qnxPgVdwrwW'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmWJwqZBJWerHsN1b7g4pRDYmzGNnaMYuD3KSbnpaxsB2h'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmRXPSdysBS3dbUXe6w8oXevZWHdPQWaR2d3fggNsjvieL'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmTUZAXfws6zrhEksnMqLxsbhXZBQs4FNiarjXSYQqVrjC'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmNNk7dTdh8UofwgqLNauq6N78DPc6LKK2yBs1MFdx7Mbg'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmW5mrJfyqh7B4ywSvraZgnWjS3q9CLiYURiJpCX3aro5i'),
Tsize: 262158
}, {
Name: '',
Hash: new CID('QmTFHZL5CkgNz19MdPnSuyLAi6AVq9fFp81zmPpaL2amED'),
Tsize: 262158
}]

const node = new DAGNode(Buffer.from('some data'), links)
const serialized = node.serialize()
const deserialized = dagPB.util.deserialize(serialized)

// check sorting
expect(deserialized.Links.map((l) => l.Hash)).to.be.eql(links.map(l => l.Hash))
})

it('create with empty link name', () => {
const node = new DAGNode(Buffer.from('hello'), [
new DAGLink('', 10, 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')
Expand Down

0 comments on commit 8640b22

Please sign in to comment.