Skip to content

Commit

Permalink
Merge pull request #3 from diasdavid/fix/support-empty-blobs
Browse files Browse the repository at this point in the history
add a clause to support files that were stored without any data
  • Loading branch information
dignifiedquire committed Apr 25, 2016
2 parents 60e2ad0 + b29a227 commit 7367e59
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,24 @@ Blobs.prototype.createReadStream = function (opts) {
return server.get(key)
})
.then(function (result) {
if (!result) throw new Error('key not found: ' + key)

if (!result) {
throw new Error('key not found: ' + key)
}
buf = result
var nextPart = buf.pop()

if (isUndefined(nextPart)) {
return next(null, new Buffer(0))
}
next(null, toBuffer(nextPart))
})
.catch(function (err) {
next(err)
})
}

if (buf.length === 0) return next(null, null)
if (buf.length === 0) {
return next(null, null)
}

next(null, toBuffer(buf.pop()))
})
Expand Down
20 changes: 20 additions & 0 deletions test/specific.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,25 @@ describe('idb-plus-blob-store', () => {
ws.write('hello')
ws.end()
})
it('read an empty blob', (done) => {
const name = 'hello.txt'
const ws = store.createWriteStream({name}, (err, blob) => {
expect(err).to.not.exist
expect(blob.key).to.be.eql(name)

const rs = store.createReadStream({name})

rs.pipe(bl((err, res) => {
expect(err).to.not.exist
expect(res.length).to.equal(0)
expect(res).to.be.eql(new Buffer(0))

done()
}))
})

ws.write(new Buffer(0))
ws.end()
})
})
})

0 comments on commit 7367e59

Please sign in to comment.