From 5e4e8517548694505cbdb2df172440a03db7df2a Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Tue, 12 Dec 2017 09:44:19 +0000 Subject: [PATCH] fix: files.cat: detect and handle rrors when unknown path and cat dir --- package.json | 1 + src/core/components/files.js | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 8e2c180104..3651e2e1ee 100644 --- a/package.json +++ b/package.json @@ -148,6 +148,7 @@ "progress": "^2.0.0", "promisify-es6": "^1.0.3", "pull-abortable": "^4.1.1", + "pull-defer": "^0.2.2", "pull-file": "^1.1.0", "pull-ndjson": "^0.1.1", "pull-paramap": "^1.2.2", diff --git a/src/core/components/files.js b/src/core/components/files.js index 8c2f91610d..4a4f60c797 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -137,10 +137,19 @@ module.exports = function files (self) { pull.collect((err, files) => { if (err) { d.end(err) } if (!files || !files.length) { - return d.end(new Error('No such file')) + return d.abort(new Error('No such file')) } - const content = files[files.length - 1].content + if (files.length > 1) { + files = files.filter((file) => file.path === ipfsPath) + } + + const file = files[0] + + const content = file.content + if (!content && file.type === 'dir') { + return d.abort(new Error('this dag node is a directory')) + } d.resolve(content) }) ) @@ -206,9 +215,8 @@ module.exports = function files (self) { addPullStream: _addPullStream, cat: promisify((ipfsPath, callback) => { - const p = _catPullStream(ipfsPath) pull( - p, + _catPullStream(ipfsPath), pull.collect((err, buffers) => { if (err) { return callback(err) } callback(null, Buffer.concat(buffers)) @@ -216,11 +224,7 @@ module.exports = function files (self) { ) }), - catReadableStream: (ipfsPath) => { - const p = _catPullStream(ipfsPath) - - return toStream.source(p) - }, + catReadableStream: (ipfsPath) => toStream.source(_catPullStream(ipfsPath)), catPullStream: _catPullStream,