diff --git a/package.json b/package.json index f95cfb847..b1822287a 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "readable-stream": "^2.3.3", "stream-http": "^2.7.2", "streamifier": "^0.1.1", - "tar-stream": "^1.5.4" + "tar-stream": "^1.5.5" }, "engines": { "node": ">=6.0.0", @@ -65,7 +65,7 @@ "dirty-chai": "^2.0.1", "eslint-plugin-react": "^7.4.0", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.34.0", + "interface-ipfs-core": "~0.34.3", "hapi": "^16.6.2", "ipfsd-ctl": "~0.24.1", "pre-commit": "^1.2.2", diff --git a/src/ls.js b/src/ls.js index 20082301f..78dc4d5bd 100644 --- a/src/ls.js +++ b/src/ls.js @@ -15,6 +15,48 @@ module.exports = (arg) => { path: 'ls', args: args, qs: opts - }, callback) + }, (err, results) => { + if (err) { + return callback(err) + } + + let result = results.Objects + if (!result) { + return callback(new Error('expected .Objects in results')) + } + + result = result[0] + if (!result) { + return callback(new Error('expected one array in results.Objects')) + } + + result = result.Links + if (!Array.isArray(result)) { + return callback(new Error('expected one array in results.Objects[0].Links')) + } + + result = result.map((link) => ({ + depth: 1, + name: link.Name, + path: args + '/' + link.Name, + size: link.Size, + hash: link.Hash, + type: typeOf(link) + })) + + callback(null, result) + }) }) } + +function typeOf (link) { + switch (link.Type) { + case 1: + case 5: + return 'dir' + case 2: + return 'file' + default: + return 'unknown' + } +} diff --git a/test/ls.spec.js b/test/ls.spec.js index 0b2bcc828..4cbf845f4 100644 --- a/test/ls.spec.js +++ b/test/ls.spec.js @@ -40,17 +40,6 @@ describe('.ls', function () { after((done) => fc.dismantle(done)) describe('Callback API', () => { - it('should correctly retrieve links', function (done) { - ipfs.ls(folder, (err, res) => { - expect(err).to.not.exist() - - expect(res).to.have.a.property('Objects') - expect(res.Objects[0]).to.have.a.property('Links') - expect(res.Objects[0]).to.have.property('Hash', 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') - done() - }) - }) - it('should correctly handle a nonexist()ing hash', function (done) { ipfs.ls('surelynotavalidhashheh?', (err, res) => { expect(err).to.exist() @@ -69,15 +58,6 @@ describe('.ls', function () { }) describe('Promises API', () => { - it('should correctly retrieve links', () => { - return ipfs.ls(folder) - .then((res) => { - expect(res).to.have.a.property('Objects') - expect(res.Objects[0]).to.have.a.property('Links') - expect(res.Objects[0]).to.have.property('Hash', 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') - }) - }) - it('should correctly handle a nonexist()ing hash', () => { return ipfs.ls('surelynotavalidhashheh?') .catch((err) => expect(err).to.exist())