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

Commit

Permalink
perf: do not load a node when we only want the hash or size
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
achingbrain committed Nov 29, 2018
1 parent 930a9fa commit a029c7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"ipfs-multipart": "~0.1.0",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-importer": "~0.37.0",
"ipfs-unixfs-exporter": "~0.35.0",
"ipfs-unixfs-exporter": "~0.35.1",
"ipld-dag-pb": "~0.15.0",
"is-pull-stream": "~0.0.0",
"is-stream": "^1.1.0",
Expand Down
24 changes: 13 additions & 11 deletions src/core/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')
const asyncMap = require('pull-stream/throughs/async-map')
const exporter = require('ipfs-unixfs-exporter')
const CID = require('cids')
const log = require('debug')('ipfs:mfs:stat')

const defaultOptions = {
Expand Down Expand Up @@ -40,6 +41,18 @@ module.exports = (context) => {
}),

asyncMap((file, cb) => {
if (options.hash) {
return cb(null, {
hash: formatCid(new CID(file.hash), options.cidBase)
})
}

if (options.size) {
return cb(null, {
size: file.size
})
}

loadNode(context, {
cid: file.hash
}, (err, result) => {
Expand All @@ -51,18 +64,7 @@ module.exports = (context) => {
node, cid
} = result

if (options.hash) {
return cb(null, {
hash: formatCid(cid, options.cidBase)
})
} else if (options.size) {
return cb(null, {
size: node.size
})
}

const meta = unmarshal(node.data)

let blocks = node.links.length

if (meta.type === 'file') {
Expand Down

0 comments on commit a029c7e

Please sign in to comment.