Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit a029c7e

Browse files
committed
perf: do not load a node when we only want the hash or size
License: MIT Signed-off-by: achingbrain <alex@achingbrain.net>
1 parent 930a9fa commit a029c7e

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"ipfs-multipart": "~0.1.0",
5757
"ipfs-unixfs": "~0.1.16",
5858
"ipfs-unixfs-importer": "~0.37.0",
59-
"ipfs-unixfs-exporter": "~0.35.0",
59+
"ipfs-unixfs-exporter": "~0.35.1",
6060
"ipld-dag-pb": "~0.15.0",
6161
"is-pull-stream": "~0.0.0",
6262
"is-stream": "^1.1.0",

src/core/stat.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const pull = require('pull-stream/pull')
1111
const collect = require('pull-stream/sinks/collect')
1212
const asyncMap = require('pull-stream/throughs/async-map')
1313
const exporter = require('ipfs-unixfs-exporter')
14+
const CID = require('cids')
1415
const log = require('debug')('ipfs:mfs:stat')
1516

1617
const defaultOptions = {
@@ -40,6 +41,18 @@ module.exports = (context) => {
4041
}),
4142

4243
asyncMap((file, cb) => {
44+
if (options.hash) {
45+
return cb(null, {
46+
hash: formatCid(new CID(file.hash), options.cidBase)
47+
})
48+
}
49+
50+
if (options.size) {
51+
return cb(null, {
52+
size: file.size
53+
})
54+
}
55+
4356
loadNode(context, {
4457
cid: file.hash
4558
}, (err, result) => {
@@ -51,18 +64,7 @@ module.exports = (context) => {
5164
node, cid
5265
} = result
5366

54-
if (options.hash) {
55-
return cb(null, {
56-
hash: formatCid(cid, options.cidBase)
57-
})
58-
} else if (options.size) {
59-
return cb(null, {
60-
size: node.size
61-
})
62-
}
63-
6467
const meta = unmarshal(node.data)
65-
6668
let blocks = node.links.length
6769

6870
if (meta.type === 'file') {

0 commit comments

Comments
 (0)