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

fix: handle progress for empty files #3260

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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