{"name":"random-access-storage","version":"2.0.0","description":"Easily make random-access-storage instances","main":"index.js","dependencies":{"events":"^3.3.0","queue-tick":"^1.0.0"},"devDependencies":{"brittle":"^2.3.1","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":"c62ab680ad181ddfcda7f063c9a979cdfb49d193","_id":"random-access-storage@2.0.0","_nodeVersion":"16.15.0","_npmVersion":"8.5.5","dist":{"integrity":"sha512-b750PUpZ3sPoTUyob3eF6V+6UcuhdqPXgm0NzxjP6ovay3UBTsum0b57MrW2GApyOph1otg0ty7duk0TrBrrCw==","shasum":"e0b80142cfa57670c1f12fb6605f4b7a06216acc","tarball":"http://registry-npm.ywwl.com/random-access-storage/download/random-access-storage-2.0.0.tgz","fileCount":7,"unpackedSize":24077,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCv0PMv5zfunhpFmt0yD3yfq4G74qdC7tFp02xNytAC/AIhAOiMjWQayPA6OoUrIx3LBLTGZMnaaG0nsXxcCNDchVDv"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiuWvrACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmozug/9HFo9CikAuQe9rTNDDkmL1QTg0+j7wbuLloJ/II65RZzVymAA\r\nldwbLIpJtKQhWRlLQRr21Q3jRRS8QZpy/Wdi89W0XSmq3GAUhlmgUz/y1Y5K\r\nHdfZ7zDvB3XZJPdEHRjdfm8sThf5PdJLyaoDwxmdaivY8XiUrbEV1C5wVpSH\r\njq2ipaSbIF/6xdIfCVgBEPX82Dy0BtOwqq/ALxFE4dL/xp5WbWnKxFVj980N\r\nj61hz6voznT6qQA2GGOPcHO+XwjeLvhdS5rJkqoVJ7UCBDxei4jQlN/+Xk9h\r\nmNxMZqKJqAozjROEmUfbJJoDqxyWFOe0QP/ESIbwz36YC22D55yJ6KlS7K27\r\nFJdenm0r6xjJ5mW7iMY0mc7RSO7vmcv9VhcfQ7jgKeLezJB0ZWq7EkZrsF50\r\nqCOeCgb0KvFkihdUQZCUUH+pE21dCp0QMlZ35aFvS8SrAx27/5mfpTalg4gv\r\nuad+ELKzFmcrqWxfkGDgHfOns5mceLyutkYtQmXGZmWJpAeHxhHuK3+GC/tG\r\n8phbj0XbUb6nRsxM4hkaTom5erw1gWuyXF2sZsiMinRgdKHWU3MMqJ7EhjXP\r\nSzFgQwk2aa+vfqsUgS/o0Tlkh0rBzDAkvgu1R4NSnyQBdK0nJTOq+ppIWPWp\r\nMmKzEpYhMWyTZov00DsLG4GfowQOzl2qOtU=\r\n=BF1D\r\n-----END PGP SIGNATURE-----\r\n","size":5852,"noattachment":false,"key":"/random-access-storage/-/random-access-storage-2.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_2.0.0_1656318955256_0.09795835741998293"},"_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":1656318955417,"_cnpm_publish_time":1656318955417,"dist-tags":{"latest":"3.0.2"}}