{"name":"random-access-storage","version":"1.4.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":"55650e6e6f6b06e0a45382fbc11086c15ef67879","_id":"random-access-storage@1.4.0","_npmVersion":"6.4.1","_nodeVersion":"10.15.1","_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"dist":{"integrity":"sha512-7oszloM/+PdqWp/oFGyL6SeI14liqo8AAisHAZQGkWdHISyAnngKjNPL0JYIazeLxbHPY6oed2yUffowdq/o6A==","shasum":"cbe5b5ccfb38680aac7b78a050d9f0a5ef36841f","tarball":"http://registry-npm.ywwl.com/random-access-storage/download/random-access-storage-1.4.0.tgz","fileCount":7,"unpackedSize":24099,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdAOZaCRA9TVsSAnZWagAAfncP/0PlKn27AiqOK5Yrf67q\nHvC6J7nnJVDG6kFH7bjOQbuQMFbdO0RMh7HDYaJEExL/QEPgM+MeXe9V7oGX\nmlctpBzQZwKDOl1PEZHueKQWm15s+kFQODbVkjbz7RTi4HR6yxnEb458OTp7\n+6IcSwBnZoPvvFK5hNQdPyat2yx0XSPxHoyWqSGoGUXBaQro0qdfkQe5LO54\nRcxebj2HgY55A8WzcuQDT14h7R/2WsuXWTTQVw2J3Rd28h29C60GjM38ckW4\ngU5LCKxghL1kv7BPzbkav/9uFMlnnG6/uzQNnkqLV4PAck8f4HOEc+R8zxH+\nO+VeZqfV18kRQg95RTvFTYjWCma98ZOsVieGF4tyf1x1QSCfIW1d3anAuDnk\nmEn4/AlZwqiAYF7SWEFRf6GpU8sr/0yYAqncjlzpGH9MGpyLqgsKGZBR6GyQ\nt6e60kwhzebwHC0R8b9KzFrKkTpe+SMpbIA9Ip/5q44ry/9B/nvArUxFLAKw\njOMOgbfrK76hTpbYgqw+d3SFGpzpl4MPFol9GGCYV4v2vmI4Je+nmRgMWSRx\nmI314imBDRjv/4d9wsnzsHkPA6Vow6PJWX/wL+ETGoHYJeXY8Sw687Nbwoer\nf0VKjqD17scg16/tYW/wfo51ho/UJfrbgEXV9YxlqyDHUqBGZ+usW+F26fSu\n1TY6\r\n=IIMf\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEi4/o4Hi61sW3HhXQXsdzvY1dW07CtpzFjiIMO7T9eiAiAmOYH1SMFisYoowvtdufZ08bMqisgz6fSq14LbSexXAA=="}],"size":5774,"noattachment":false,"key":"/random-access-storage/-/random-access-storage-1.4.0.tgz"},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/random-access-storage_1.4.0_1560340058284_0.35045027794822414"},"_hasShrinkwrap":false,"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":1560340058460,"_cnpm_publish_time":1560340058460,"dist-tags":{"latest":"3.0.2"}}