module.exports = Storage

function Storage () {
  if (!(this instanceof Storage)) return new Storage()
  this.chunks = []
}

Storage.prototype.put = function (index, buf, cb) {
  this.chunks[index] = buf
  if (cb) process.nextTick(cb)
}

function nextTick (cb, err, val) {
  process.nextTick(function () {
    cb(err, val)
  })
}

Storage.prototype.get = function (index, opts, cb) {
  if (typeof opts === 'function') return this.get(index, null, opts)
  var buf = this.chunks[index]
  if (!buf) return nextTick(cb, new Error('Chunk not found'))
  if (!opts) return nextTick(cb, null, buf)
  var offset = opts.offset || 0
  var len = opts.length || (buf.length - offset)
  nextTick(cb, null, buf.slice(offset, len + offset))
}

module.exports = Storage

function Storage (chunkLength) {
  if (!(this instanceof Storage)) return new Storage()
  this.chunks = []
  this.chunkLength = Number(chunkLength)
  this.closed = false
  if (!this.chunkLength) throw new Error('First argument must be a chunk length')
}

Storage.prototype.put = function (index, buf, cb) {
  if (this.closed) return nextTick(cb, new Error('Storage is closed'))
  if (buf.length !== this.chunkLength) {
    return nextTick(cb, new Error('Chunk length must be ' + this.chunkLength))
  }
  this.chunks[index] = buf
  if (cb) process.nextTick(cb)
}

Storage.prototype.get = function (index, opts, cb) {
  if (typeof opts === 'function') return this.get(index, null, opts)
  if (this.closed) return nextTick(cb, new Error('Storage is closed'))
  var buf = this.chunks[index]
  if (!buf) return nextTick(cb, new Error('Chunk not found'))
  if (!opts) return nextTick(cb, null, buf)
  var offset = opts.offset || 0
  var len = opts.length || (buf.length - offset)
  nextTick(cb, null, buf.slice(offset, len + offset))
}

Storage.prototype.close = Storage.prototype.destroy = function (cb) {
  if (this.closed) return nextTick(cb, new Error('Storage is closed'))
  this.closed = true
  this.chunks = null
  nextTick(cb, null)
}

function nextTick (cb, err, val) {
  process.nextTick(function () {
    if (cb) cb(err, val)
  })
}

Add close and destroy; force fixed chunk length
Add close and destroy; force fixed chunk length
