Skip to content

Commit

Permalink
add a clause to support files that were stored without any data
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Apr 25, 2016
1 parent 60e2ad0 commit b29a227
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 b29a227

Please sign in to comment.