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

Commit

Permalink
fix: handle progress for empty files (#3260)
Browse files Browse the repository at this point in the history
When a file is empty the progress event will have a `Bytes` property
that is `0` so test if the property is not undefined rather than if
it is truthy.

Fixes #3255
  • Loading branch information
achingbrain authored Sep 2, 2020
1 parent 1b6cf60 commit 9c36cb8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
23 changes: 23 additions & 0 deletions packages/interface-ipfs-core/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ module.exports = (common, options) => {
expect(accumProgress).to.equal(fixtures.bigFile.data.length)
})

it('should add an empty file with progress enabled', async () => {
let progCalled = false
let accumProgress = 0
function handler (p) {
progCalled = true
accumProgress = p
}

const file = await ipfs.add(fixtures.emptyFile.data, { progress: handler })

expect(file.cid.toString()).to.equal(fixtures.emptyFile.cid)
expect(file.path).to.equal(fixtures.emptyFile.cid)
expect(progCalled).to.be.true()
expect(accumProgress).to.equal(fixtures.emptyFile.data.length)
})

it('should add an empty file without progress enabled', async () => {
const file = await ipfs.add(fixtures.emptyFile.data)

expect(file.cid.toString()).to.equal(fixtures.emptyFile.cid)
expect(file.path).to.equal(fixtures.emptyFile.cid)
})

it('should add a Buffer as tuple', async () => {
const tuple = { path: 'testfile.txt', content: fixtures.smallFile.data }

Expand Down
4 changes: 4 additions & 0 deletions packages/interface-ipfs-core/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ exports.fixtures = Object.freeze({
bigFile: Object.freeze({
cid: 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq',
data: loadFixture('test/fixtures/15mb.random', 'interface-ipfs-core')
}),
emptyFile: Object.freeze({
cid: 'QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH',
data: new Uint8Array(0)
})
})
6 changes: 3 additions & 3 deletions packages/ipfs-http-client/src/add-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ module.exports = configure((api) => {
for await (let file of res.ndjson()) {
file = toCamel(file)

if (progressFn && file.bytes) {
progressFn(file.bytes)
} else {
if (file.hash !== undefined) {
yield toCoreInterface(file)
} else if (progressFn) {
progressFn(file.bytes || 0)
}
}
}
Expand Down

0 comments on commit 9c36cb8

Please sign in to comment.