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

fix: report correct size for raw dag nodes #1591

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ function prepareFile (self, opts, file, callback) {
(node, cb) => {
const b58Hash = cid.toBaseEncodedString()

let size = node.size

if (Buffer.isBuffer(node)) {
size = node.length
}

cb(null, {
path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash),
hash: b58Hash,
size: node.size
size
})
}
], callback)
Expand Down
25 changes: 25 additions & 0 deletions test/core/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,30 @@ describe('files', () => {
done()
})
})

it('should add a file with a v1 CID', (done) => {
ipfs.files.add(Buffer.from([0, 1, 2]), {
cidVersion: 1
}, (err, files) => {
expect(err).to.not.exist()
expect(files.length).to.equal(1)
expect(files[0].hash).to.equal('zb2rhiNedvrkpYhcrgtpmhKk5UPzcgizgSXaQLYXNY745BmYP')
expect(files[0].size).to.equal(3)
done()
})
})

it('should add a file with a v1 CID and not raw leaves', (done) => {
ipfs.files.add(Buffer.from([0, 1, 2]), {
cidVersion: 1,
rawLeaves: false
}, (err, files) => {
expect(err).to.not.exist()
expect(files.length).to.equal(1)
expect(files[0].hash).to.equal('zdj7WcDSFNSsZkdkbpSDGeLsBtHbYKyvPQsaw6PpeeYdGqoAx')
expect(files[0].size).to.equal(11)
Copy link
Member Author

@achingbrain achingbrain Sep 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N.b. this is bigger than the input buffer due to the protobuf overhead. The other test uses raw nodes, so the data hasn't been wrapped in a protobuf.

done()
})
})
})
})