Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 120d291

Browse files
pgtedaviddias
authored andcommitted
fix: files.cat: detect and handle rrors when unknown path and cat dir (#1143)
1 parent 6a7027d commit 120d291

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"progress": "^2.0.0",
149149
"promisify-es6": "^1.0.3",
150150
"pull-abortable": "^4.1.1",
151+
"pull-defer": "^0.2.2",
151152
"pull-file": "^1.1.0",
152153
"pull-ndjson": "^0.1.1",
153154
"pull-paramap": "^1.2.2",

src/core/components/files.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,19 @@ module.exports = function files (self) {
137137
pull.collect((err, files) => {
138138
if (err) { d.end(err) }
139139
if (!files || !files.length) {
140-
return d.end(new Error('No such file'))
140+
return d.abort(new Error('No such file'))
141141
}
142142

143-
const content = files[files.length - 1].content
143+
if (files.length > 1) {
144+
files = files.filter((file) => file.path === ipfsPath)
145+
}
146+
147+
const file = files[0]
148+
149+
const content = file.content
150+
if (!content && file.type === 'dir') {
151+
return d.abort(new Error('this dag node is a directory'))
152+
}
144153
d.resolve(content)
145154
})
146155
)
@@ -206,21 +215,16 @@ module.exports = function files (self) {
206215
addPullStream: _addPullStream,
207216

208217
cat: promisify((ipfsPath, callback) => {
209-
const p = _catPullStream(ipfsPath)
210218
pull(
211-
p,
219+
_catPullStream(ipfsPath),
212220
pull.collect((err, buffers) => {
213221
if (err) { return callback(err) }
214222
callback(null, Buffer.concat(buffers))
215223
})
216224
)
217225
}),
218226

219-
catReadableStream: (ipfsPath) => {
220-
const p = _catPullStream(ipfsPath)
221-
222-
return toStream.source(p)
223-
},
227+
catReadableStream: (ipfsPath) => toStream.source(_catPullStream(ipfsPath)),
224228

225229
catPullStream: _catPullStream,
226230

0 commit comments

Comments
 (0)