diff --git a/src/index.js b/src/index.js index 8be232b..decf17c 100644 --- a/src/index.js +++ b/src/index.js @@ -156,11 +156,14 @@ 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) { @@ -168,7 +171,9 @@ Blobs.prototype.createReadStream = function (opts) { }) } - if (buf.length === 0) return next(null, null) + if (buf.length === 0) { + return next(null, null) + } next(null, toBuffer(buf.pop())) }) diff --git a/test/specific.spec.js b/test/specific.spec.js index 0b15ceb..e535e0a 100644 --- a/test/specific.spec.js +++ b/test/specific.spec.js @@ -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() + }) }) })