{"name":"random-access-storage","version":"3.0.0","description":"Easily make random-access-storage instances","main":"index.js","dependencies":{"events":"^3.3.0","queue-tick":"^1.0.0"},"devDependencies":{"brittle":"^3.0.0","standard":"^17.0.0"},"scripts":{"test":"standard && brittle 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":"e273033b6a5fd80cbf2a6e95acecae2370b09efc","_id":"random-access-storage@3.0.0","_nodeVersion":"16.16.0","_npmVersion":"8.11.0","dist":{"integrity":"sha512-f9KeHUMhrqj0hE2d4HbUXk/Cd5vElaVzdA0PuqPTlnxsG3dDvwQaMg8POT5tWK6UxJ80zAONZpHgLPMnnff1RA==","shasum":"a4107a84313cf0e215b232d533ac286b77b99a8c","tarball":"http://registry-npm.ywwl.com/random-access-storage/download/random-access-storage-3.0.0.tgz","fileCount":4,"unpackedSize":16267,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD1+WuJ8seobnCNQ+63gZobF0Dlzg7lmfZ5bBbrPfQzcAIhAL2L8yJaHeuM7P5rK1CwU98sR2wN5y41tJEGCIHqIqkL"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjIaD4ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqmsBAAiENCxXPSu/onw0rq9lzjM09F1tTzJz3wDYqWbOsVRLX++BHE\r\n9q1g486l6n70p1QY/LCvHuZo0qC/iWXtczPKSyUnHoU0O8+GxIznl36UWwwx\r\noVYeHU6etzWf8cYASs7oqxXNHtCwBGFoO6Ix4logd1YXjwxMAfRYGjGlf1MG\r\nnqURKIMhJh/y2/ot1CSJHLSAZCz7HNyi4Ze9qrt8I2lEMqDUTYPLs8gMy7n6\r\nFxSz42Jopwf3qmxVfF/sr/AH0LZwCSBw+65E63pAHjkE96TJ+lvc2SWw152r\r\nqHtvkO7ARNxurKGY1Jqk7vAMAh0VcBU8cLfdAmnE3MWqDUZ9RvkwEGzJ5XVr\r\nzzQTdXhCc+R1jRg5SogAoNyvtyvsxIc8FyufHSr19B7xWMaeaal3owrtjY+y\r\nwi86KKnOZEHR56cinP7vPEgPY6r0C/QOJxNfuYIHDbGv66Y1df5kc13x37x/\r\nOOnoe5SS09ZNNYVgt12FE8AR+l4hjsP+V5ZBbzjjaSQ1HH7pOjNAjdgqC9mV\r\ngiToyI2e92PoxZyExfWfBBvl+AbyGMwxuxrkZNuOkGRpPaCkNmUye1GTvZ1S\r\nA+aki7i5Tpp/Z8zBb/G7J7remXj96GRPJWonnGc7YTKucSIdwNBoaZFuUJZr\r\nNK+pzl66FfTZ2EUmhpLBASGOcimmTzC7WVU=\r\n=suoX\r\n-----END PGP SIGNATURE-----\r\n","size":4643,"noattachment":false,"key":"/random-access-storage/-/random-access-storage-3.0.0.tgz"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"directories":{},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/random-access-storage_3.0.0_1663148280515_0.5201390054673463"},"_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":1663148280683,"_cnpm_publish_time":1663148280683,"dist-tags":{"latest":"3.0.2"}}