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

Commit 549f2f6

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
fix: report correct size for raw dag nodes (#1591)
If you specify `cidVersion: 0`, for go compatibility you will have raw leaf nodes. If your data fits in one dag node, the root node will be a raw node for the same reason, and `ipfs.object.get` will return a buffer. This PR inspects the returned node to see if it's a buffer, if it is, use the `length` property, otherwise use the DAGNode `size` property. Fixes #1585
1 parent 542737b commit 549f2f6

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/core/components/files.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ function prepareFile (self, opts, file, callback) {
4040
(node, cb) => {
4141
const b58Hash = cid.toBaseEncodedString()
4242

43+
let size = node.size
44+
45+
if (Buffer.isBuffer(node)) {
46+
size = node.length
47+
}
48+
4349
cb(null, {
4450
path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash),
4551
hash: b58Hash,
46-
size: node.size
52+
size
4753
})
4854
}
4955
], callback)

test/core/files.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,30 @@ describe('files', () => {
8383
done()
8484
})
8585
})
86+
87+
it('should add a file with a v1 CID', (done) => {
88+
ipfs.files.add(Buffer.from([0, 1, 2]), {
89+
cidVersion: 1
90+
}, (err, files) => {
91+
expect(err).to.not.exist()
92+
expect(files.length).to.equal(1)
93+
expect(files[0].hash).to.equal('zb2rhiNedvrkpYhcrgtpmhKk5UPzcgizgSXaQLYXNY745BmYP')
94+
expect(files[0].size).to.equal(3)
95+
done()
96+
})
97+
})
98+
99+
it('should add a file with a v1 CID and not raw leaves', (done) => {
100+
ipfs.files.add(Buffer.from([0, 1, 2]), {
101+
cidVersion: 1,
102+
rawLeaves: false
103+
}, (err, files) => {
104+
expect(err).to.not.exist()
105+
expect(files.length).to.equal(1)
106+
expect(files[0].hash).to.equal('zdj7WcDSFNSsZkdkbpSDGeLsBtHbYKyvPQsaw6PpeeYdGqoAx')
107+
expect(files[0].size).to.equal(11)
108+
done()
109+
})
110+
})
86111
})
87112
})

test/http-api/files.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ describe('.files', () => {
2626
})
2727
})
2828

29-
after((done) => ipfsd.stop(done))
29+
after(function (done) {
30+
this.timeout(20 * 1000)
31+
if (ipfsd) {
32+
ipfsd.stop(done)
33+
} else {
34+
done()
35+
}
36+
})
3037

3138
describe('.add', function () {
3239
it('performs a speculative add, --only-hash', () => {

0 commit comments

Comments
 (0)