chunk-store-stream
Advanced tools
Comparing version 4.1.1 to 4.2.0
{ | ||
"name": "chunk-store-stream", | ||
"description": "Convert an abstract-chunk-store compliant store into a readable or writable stream", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh", |
21
read.js
@@ -16,17 +16,30 @@ const stream = require('readable-stream') | ||
const offset = opts.offset || 0 | ||
this._store = store | ||
this._chunkLength = chunkLength | ||
this._index = 0 | ||
this._index = Math.floor(offset / chunkLength) | ||
this._chunkOffset = offset % chunkLength | ||
this._consumedLength = 0 | ||
} | ||
_read () { | ||
if (this._index * this._chunkLength >= this._length) { | ||
const remainingLength = this._length - this._consumedLength | ||
if (remainingLength <= 0) { | ||
this.push(null) | ||
} else { | ||
this._store.get(this._index, (err, chunk) => { | ||
const offset = this._chunkOffset | ||
const length = Math.min(remainingLength, this._chunkLength - this._chunkOffset) | ||
this._chunkOffset = 0 | ||
this._consumedLength += length | ||
this._store.get(this._index, { | ||
offset, | ||
length | ||
}, (err, chunk) => { | ||
if (err) return this.destroy(err) | ||
this.push(chunk) | ||
}) | ||
this._index += 1 | ||
} | ||
this._index += 1 | ||
} | ||
@@ -33,0 +46,0 @@ |
@@ -14,3 +14,4 @@ const BlockStream = require('block-stream2') | ||
this._blockstream = new BlockStream(chunkLength, { zeroPadding: false }) | ||
const zeroPadding = opts.zeroPadding === undefined ? false : opts.zeroPadding | ||
this._blockstream = new BlockStream(chunkLength, { zeroPadding }) | ||
this._outstandingPuts = 0 | ||
@@ -23,3 +24,4 @@ | ||
this._outstandingPuts += 1 | ||
store.put(index, chunk, () => { | ||
store.put(index, chunk, (err) => { | ||
if (err) return this.destroy(err) | ||
this._outstandingPuts -= 1 | ||
@@ -26,0 +28,0 @@ if (this._outstandingPuts === 0 && typeof this._finalCb === 'function') { |
7897
98