Skip to content

Commit 7367e59

Browse files
Merge pull request #3 from diasdavid/fix/support-empty-blobs
add a clause to support files that were stored without any data
2 parents 60e2ad0 + b29a227 commit 7367e59

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,24 @@ Blobs.prototype.createReadStream = function (opts) {
156156
return server.get(key)
157157
})
158158
.then(function (result) {
159-
if (!result) throw new Error('key not found: ' + key)
160-
159+
if (!result) {
160+
throw new Error('key not found: ' + key)
161+
}
161162
buf = result
162163
var nextPart = buf.pop()
163-
164+
if (isUndefined(nextPart)) {
165+
return next(null, new Buffer(0))
166+
}
164167
next(null, toBuffer(nextPart))
165168
})
166169
.catch(function (err) {
167170
next(err)
168171
})
169172
}
170173

171-
if (buf.length === 0) return next(null, null)
174+
if (buf.length === 0) {
175+
return next(null, null)
176+
}
172177

173178
next(null, toBuffer(buf.pop()))
174179
})

test/specific.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,25 @@ describe('idb-plus-blob-store', () => {
8989
ws.write('hello')
9090
ws.end()
9191
})
92+
it('read an empty blob', (done) => {
93+
const name = 'hello.txt'
94+
const ws = store.createWriteStream({name}, (err, blob) => {
95+
expect(err).to.not.exist
96+
expect(blob.key).to.be.eql(name)
97+
98+
const rs = store.createReadStream({name})
99+
100+
rs.pipe(bl((err, res) => {
101+
expect(err).to.not.exist
102+
expect(res.length).to.equal(0)
103+
expect(res).to.be.eql(new Buffer(0))
104+
105+
done()
106+
}))
107+
})
108+
109+
ws.write(new Buffer(0))
110+
ws.end()
111+
})
92112
})
93113
})

0 commit comments

Comments
 (0)