{"name":"random-access-storage","version":"0.0.0","description":"Easily make random-access-storage instances","main":"index.js","dependencies":{"inherits":"^2.0.3"},"devDependencies":{"standard":"^10.0.3","tape":"^4.8.0"},"scripts":{"test":"standard && tape test.js"},"repository":{"type":"git","url":"git+https://github.com/random-access-storage/random-access-storage.git"},"author":{"name":"Mathias Buus","url":"@mafintosh"},"license":"MIT","bugs":{"url":"https://github.com/random-access-storage/random-access-storage/issues"},"homepage":"https://github.com/random-access-storage/random-access-storage","gitHead":"35efd846be2cc1499230b92728ace5cfad20e677","_id":"random-access-storage@0.0.0","_npmVersion":"5.6.0","_nodeVersion":"9.5.0","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"integrity":"sha512-RHzSCPS660SRn01H7daeamXfRls9xOKVo9nH2iMkXvjSHLt225zSqmogjo5MnU+RbOFn0XYzPDd1AjyYZROHNA==","shasum":"df1f6227d130254ca7bbaf182312fbffdbd22eeb","tarball":"http://registry-npm.ywwl.com/random-access-storage/download/random-access-storage-0.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDvWtXNWSLywrEyI2uhEegzn1C7pu3r8+Q09GUhIQz92AiEAlebDKJG14DMl990+8Et5X9LMeOIFkqdspkfam0avBzg="}],"size":4096,"noattachment":false,"key":"/random-access-storage/-/random-access-storage-0.0.0.tgz"},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/random-access-storage-0.0.0.tgz_1517765292899_0.3913068105466664"},"directories":{},"readme":"# random-access-storage\n\nEasily make random-access-storage instances\n\n```\nnpm install random-access-storage\n```\n\nA random-access-storage instance is a common interface for a storage abstraction, that provides the following core api.\n\n* `read(offset, size)` - Read a buffer at a custom offset.\n* `write(offset, data)` - Write a buffer to a custom offset.\n* `del(offset, size)` - Delete data at a custom offset.\n\nThis module exposes a base class that implements most of the plumbing and flow you'd usually want to implement when making one.\n\n## Usage\n\n``` js\nconst RandomAccessStorage = require('random-access-storage')\nconst fs = require('fs')\n\nconst file = fileReader('index.js')\n\nfile.read(0, 10, (err, buf) => console.log('0-10: ' + buf.toString()))\nfile.read(40, 15, (err, buf) => console.log('40-55: ' + buf.toString()))\nfile.close()\n\nfunction fileReader (name) {\n  let fd = 0\n  return new RandomAccessStorage({\n    open: function (req) {\n      // called once automatically before the first read call\n      fs.open(name, 'r', function (err, res) {\n        if (err) return req.callback(err)\n        fd = res\n        req.callback(null)\n      })\n    },\n    read: function (req) {\n      const buf = Buffer.allocUnsafe(req.size)\n      fs.read(fd, buf, 0, buf.length, req.offset, function (err, read) {\n        if (err) return req.callback(err)\n        if (read < buf.length) return req.callback(new Error('Could not read'))\n        req.callback(null, buf)\n      })\n    },\n    close: function (req) {\n      if (!fd) return req.callback(null)\n      fs.close(fd, err => req.callback(err))\n    }\n  })\n}\n```\n\n## API\n\n#### `const storage = new RandomAccessStorage([options])`\n\nMake a new instance. Options include:\n\n``` js\n{\n  createAlways: false, // always create storage on first open\n  open: fn, // sets ._open\n  read: fn, // sets ._read\n  write: fn, // sets ._write\n  del: fn, // sets ._del\n  truncate: fn, // sets ._truncate\n  stat: fn, // sets ._stat\n  suspend: fn, // sets ._suspend\n  close: fn, // sets ._close\n  unlink: fn // sets ._unlink\n}\n```\n\n#### `storage.readable`\n\nTrue if the storage implements `._read`.\n\n#### `storage.writable`\n\nTrue if the storage implements `._write`.\n\n#### `storage.deletable`\n\nTrue if the storage implements `._del`.\n\n#### `storage.truncatable`\n\nTrue if the storage implements `._truncate`.\n\n#### `storage.statable`\n\nTrue if the storage implements `._stat`.\n\n#### `storage.opened`\n\nTrue if the storage has been fully opened.\n\n#### `storage.closed`\n\nTrue if the storage has been fully closed.\n\n#### `storage.unlinked`\n\nTrue if the storage has been fully unlinked.\n\n#### `storage.writing`\n\nTrue if the storage is currently being written to.\n\n#### `storage.on('open')`\n\nEmitted when the storage is fully open.\n\n#### `storage.on('close')`\n\nEmitted when the storage is fully closed.\n\n#### `storage.on('unlink')`\n\nEmitted when the storage is fully unlinked.\n\n#### `storage.on('suspend')`\n\nEmitted when the storage is fully suspended\n\n#### `storage.on('unsuspend')`\n\nEmitted when the storage comes out of suspension.\n\n#### `storage.open(cb)`\n\nExplicitly open the storage. If you do not call this yourself,\nit will automatically called before any read/write/del/stat operation.\n\nIt is safe to call this more than once.\n\nTriggers *one* call to `_open` if you implement that.\n\n#### `storage._open(req)`\n\nImplement storage open.\n\n* `req.create` is `true` if the storage should be created.\n\nCall `req.callback` when it is fully opened.\n\n#### `storage.read(offset, size, callback)`\n\nRead the specified bytes from the storage at the specified byte offset.\nCalls the callback with a `(err, buffer)`.\n\n#### `storage._read(req)`\n\nImplement storage read.\n\n* `req.offset` contains the byte offset you should read at.\n* `req.size` contains the amount of bytes you should read.\n\nCall `req.callback(err, buffer)` when the read is completed.\n\nNote that this is guaranteed to run after the storage has been opened and not after it has been closed.\n\n#### `storage.write(offset, buffer, [callback])`\n\nWrite the specified buffer to the specified byte offset. Optionally pass a callback that is called with `(err)` when the write has completed.\n\n#### `storage._write(req)`\n\nImplement storage write.\n\n* `req.offset` contains the byte offset you should write at.\n* `req.data` contains the buffer you should write.\n\nCall `req.callback(err)` when the write is completed.\n\nNote that this is guaranteed to run after the storage has been opened and not after it has been closed.\n\n#### `storage.del(offset, size, [callback])`\n\nDelete the specified amount of bytes at the specified offset. Optionally pass a callback that is called with `(err)` when the delete has completed.\n\n#### `storage._del(req)`\n\nImplement storage delete.\n\n* `req.offset` contains the byte offset to delete at.\n* `req.size` contains the amount of bytes to delete.\n\nCall `req.callback(err)` when the delete has completed.\n\nNote that this is guaranteed to run after the storage has been opened and not after it has been closed.\n\n#### `storage.truncate(offset, [callback])`\n\nTruncate the storage at the specified offset. Optionally pass a callback that is called with `(err)` when the truncate has completed.\n\n#### `storage._truncate(req)`\n\nImplement storage truncate. Defaults to `storage._del(req)`.\n\n* `req.offset` contains the byte offset to truncate at.\n\nCall `req.callback(err)` when the truncate has completed.\n\nNote that this is guaranteed to run after the storage has been opened and not after it has been closed.\n\n#### `storage.stat(callback)`\n\nStat the storage. Should return an object with useful information about the underlying storage, including:\n\n```\n{\n  size: number // how many bytes of data is stored?\n}\n```\n\n#### `storage._stat(req)`\n\nImplement storage stat.\n\nCall `req.callback(err, statObject)` when the stat has completed.\n\nNote that this is guaranteed to run after the storage has been opened and not after it has been closed.\n\n#### `storage.suspend([callback])`\n\nSuspend (temporarily close) the storage instance.\n\n#### `storage._suspend(req)`\n\nImplement storage suspend. Defaults to calling `_close`.\n\nOptionally implement this to add a way for your storage instance to temporarily free resources.\n\nCall `req.callback(err)` when the storage has been fully suspended.\n\n#### `storage.close([callback])`\n\nClose the storage instance.\n\n#### `storage._close(req)`\n\nImplement storage close.\n\nCall `req.callback(err)` when the storage is fully closed.\n\nNote this is guaranteed to run after all pending read/write/stat/del operations has finished and no methods will run after.\n\n#### `storage.unlink([callback])`\n\nUnlink the storage instance, removing all underlying data.\n\n#### `storage._unlink(req)`\n\nImplement storage unlink.\n\nCall `req.callback(err)` when the storage has been fully unlinked.\n\nNote this is guaranteed to run after `.close()` has been called and no methods will be run after.\n\n## License\n\nMIT\n","publish_time":1517765293857,"_cnpm_publish_time":1517765293857,"dist-tags":{"latest":"3.0.2"}}