{"name":"hyperbee","version":"2.27.1","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"hyperbee@2.27.1","maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"andrewosh","email":"andrewosh@gmail.com"}],"homepage":"https://github.com/holepunchto/hyperbee","bugs":{"url":"https://github.com/holepunchto/hyperbee/issues"},"dist":{"shasum":"d927be7848790460a8f48919a87d2a7c90c05510","tarball":"http://registry-npm.ywwl.com/hyperbee/download/hyperbee-2.27.1.tgz","fileCount":10,"integrity":"sha512-NG4qKMWQqJayD7NEJ3u18QWszZNreo744L15z3DH2lC5PbP60pNYc7Z2t9fC9vuPy71FGkekvgOePsI+Gdi9LQ==","signatures":[{"sig":"MEUCIC34nA89yNXpyioBOCyleYN+GfTiW4EH8dx9nGgTp8zmAiEAgpNRJqrPvUbg6m03JXsA/hPuOJI3NnUMTui6pNyoM+o=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":105585,"size":22763,"noattachment":false,"key":"/hyperbee/-/hyperbee-2.27.1.tgz"},"main":"index.js","gitHead":"ca624a94651844a19f5510e1b895c51635a891cf","imports":{"process":{"bare":"bare-process","default":"process"}},"scripts":{"lint":"prettier --check . && lunte","test":"npm run test:node && npm run test:bare","format":"prettier --write .","protobuf":"protocol-buffers schema.proto -o ./lib/messages.js","test:bare":"brittle-bare test/*.js","test:node":"brittle-node test/*.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/holepunchto/hyperbee.git","type":"git"},"_npmVersion":"11.7.0","description":"An append-only B-tree running on a Hypercore.","directories":{},"_nodeVersion":"22.16.0","dependencies":{"b4a":"^1.6.0","rache":"^1.0.0","codecs":"^3.0.0","unslab":"^1.2.0","streamx":"^2.12.4","mutexify":"^1.4.0","debounceify":"^1.0.0","safety-catch":"^1.0.2","ready-resource":"^1.0.0","hypercore-errors":"^1.0.0","resolve-reject-promise":"^1.1.0","protocol-buffers-encodings":"^1.2.0"},"_hasShrinkwrap":false,"devDependencies":{"lunte":"^1.0.0","brittle":"^3.1.0","prettier":"^3.6.2","hypercore":"^11.0.0","sub-encoder":"^1.0.6","bare-process":"^4.2.2","tree-to-string":"^1.1.1","protocol-buffers":"^4.2.0","prettier-config-holepunch":"^2.0.0"},"_npmOperationalInternal":{"tmp":"tmp/hyperbee_2.27.1_1769511945015_0.8434965844840188","host":"s3://npm-registry-packages-npm-production"},"readme":"# Hyperbee 🐝\n\n[See API docs at docs.pears.com](https://docs.pears.com/building-blocks/hyperbee)\n\nAn append-only B-tree running on a Hypercore. Allows sorted iteration and more.\n\n```\nnpm install hyperbee\n```\n\n## Usage\n\n```js\nconst Hyperbee = require('hyperbee')\nconst Hypercore = require('hypercore')\n\nconst core = new Hypercore('./storage')\nconst db = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'binary' })\n\n// If you own the core\nawait db.put('key1', 'value1')\nawait db.put('key2', 'value2')\nawait db.del('some-key')\n\n// If you want to insert/delete batched values\nconst batch = db.batch()\n\nawait batch.put('key', 'value')\nawait batch.del('some-key')\nawait batch.flush() // Execute the batch\n\n// Query the core\nconst entry = await db.get('key') // => null or { key, value }\n\n// Read all entries\nfor await (const entry of db.createReadStream()) {\n  // ..\n}\n\n// Read a range\nfor await (const entry of db.createReadStream({ gte: 'a', lt: 'd' })) {\n  // Anything >=a and <d\n}\n\n// Get the last written entry\nfor await (const entry of db.createHistoryStream({ reverse: true, limit: 1 })) {\n  // ..\n}\n```\n\nIt works with sparse cores, only a small subset of the full core is downloaded to satisfy your queries.\n\n## API\n\n#### `const db = new Hyperbee(core, [options])`\n\nMake a new Hyperbee instance. `core` should be a [Hypercore](https://github.com/holepunchto/hypercore).\n\n`options` include:\n\n```js\n{\n  keyEncoding: 'binary', // \"binary\" (default), \"utf-8\", \"ascii\", \"json\", or an abstract-encoding\n  valueEncoding: 'binary' // Same options as keyEncoding like \"json\", etc\n}\n```\n\nNote that currently read/diff streams sort based on the encoded value of the keys.\n\n#### `await db.ready()`\n\nWaits until internal state is loaded.\n\nUse it once before reading synchronous properties like `db.version`, unless you called any of the other APIs.\n\n#### `await db.close()`\n\nFully close this bee, including its core.\n\n#### `db.core`\n\nThe underlying Hypercore backing this bee.\n\n#### `db.version`\n\nNumber that indicates how many modifications were made, useful as a version identifier.\n\n#### `db.id`\n\nString containing the id (z-base-32 of the public key) identifying this bee.\n\n#### `db.key`\n\nBuffer containing the public key identifying this bee.\n\n#### `db.discoveryKey`\n\nBuffer containing a key derived from `db.key`.\n\nThis discovery key does not allow you to verify the data, it's only to announce or look for peers that are sharing the same bee, without leaking the bee key.\n\n#### `db.writable`\n\nBoolean indicating if we can put or delete data in this bee.\n\n#### `db.readable`\n\nBoolean indicating if we can read from this bee. After closing the bee this will be `false`.\n\n#### `await db.put(key, [value], [options])`\n\nInsert a new key. Value can be optional.\n\nIf you're inserting a series of data atomically or want more performance then check the `db.batch` API.\n\n`options` includes:\n\n```js\n{\n  cas (prev, next) { return true }\n}\n```\n\n##### Compare And Swap (cas)\n\n`cas` option is a function comparator to control whether the `put` succeeds.\n\nBy returning `true` it will insert the value, otherwise it won't.\n\nIt receives two args: `prev` is the current node entry, and `next` is the potential new node.\n\n```js\nawait db.put('number', '123', { cas })\nconsole.log(await db.get('number')) // => { seq: 1, key: 'number', value: '123' }\n\nawait db.put('number', '123', { cas })\nconsole.log(await db.get('number')) // => { seq: 1, key: 'number', value: '123' }\n// Without cas this would have been { seq: 2, ... }, and the next { seq: 3 }\n\nawait db.put('number', '456', { cas })\nconsole.log(await db.get('number')) // => { seq: 2, key: 'number', value: '456' }\n\nfunction cas(prev, next) {\n  // You can use same-data or same-object lib, depending on the value complexity\n  return prev.value !== next.value\n}\n```\n\n#### `const { seq, key, value } = await db.get(key)`\n\nGet a key's value. Returns `null` if key doesn't exists.\n\n`seq` is the Hypercore index at which this key was inserted.\n\n#### `await db.del(key, [options])`\n\nDelete a key.\n\n`options` include:\n\n```js\n{\n  cas (prev, next) { return true }\n}\n```\n\n##### Compare And Swap (cas)\n\n`cas` option is a function comparator to control whether the `del` succeeds.\n\nBy returning `true` it will delete the value, otherwise it won't.\n\nIt only receives one arg: `prev` which is the current node entry.\n\n```js\n// This won't get deleted\nawait db.del('number', { cas })\nconsole.log(await db.get('number')) // => { seq: 1, key: 'number', value: 'value' }\n\n// Change the value so the next time we try to delete it then \"cas\" will return true\nawait db.put('number', 'can-be-deleted')\n\nawait db.del('number', { cas })\nconsole.log(await db.get('number')) // => null\n\nfunction cas(prev) {\n  return prev.value === 'can-be-deleted'\n}\n```\n\n#### `const { key, value } = await db.getBySeq(seq, [options])`\n\nGet the key and value from a block number.\n\n`seq` is the Hypercore index. Returns `null` if block doesn't exists.\n\n#### `const stream = db.replicate(isInitiatorOrStream)`\n\nSee more about how replicate works at [core.replicate][core-replicate-docs].\n\n#### `const batch = db.batch()`\n\nMake a new atomic batch that is either fully processed or not processed at all.\n\nIf you have several inserts and deletions then a batch can be much faster.\n\n#### `await batch.put(key, [value], [options])`\n\nInsert a key into a batch.\n\n`options` are the same as `db.put` method.\n\n#### `const { seq, key, value } = await batch.get(key)`\n\nGet a key, value out of a batch.\n\n#### `await batch.del(key, [options])`\n\nDelete a key into the batch.\n\n`options` are the same as `db.del` method.\n\n#### `await batch.flush()`\n\nCommit the batch to the database, and releases any locks it has acquired.\n\n#### `await batch.close()`\n\nDestroy a batch, and releases any locks it has acquired on the db.\n\nCall this if you want to abort a batch without flushing it.\n\n#### `const stream = db.createReadStream([range], [options])`\n\nMake a read stream. Sort order is based on the binary value of the keys.\n\nAll entries in the stream are similar to the ones returned from `db.get`.\n\n`range` should specify the range you want to read and looks like this:\n\n```js\n{\n  gt: 'only return keys > than this',\n  gte: 'only return keys >= than this',\n  lt: 'only return keys < than this',\n  lte: 'only return keys <= than this'\n}\n```\n\n`options` include:\n\n```js\n{\n  reverse: false // Set to true to get them in reverse order,\n  limit: -1 // Set to the max number of entries you want\n}\n```\n\n#### `const { seq, key, value } = await db.peek([range], [options])`\n\nSimilar to doing a read stream and returning the first value, but a bit faster than that.\n\n#### `const stream = db.createHistoryStream([options])`\n\nCreate a stream of all entries ever inserted or deleted from the db.\n\nEach entry has an additional `type` property indicating if it was a `put` or `del` operation.\n\n`options` include:\n\n```js\n{\n  live: false, // If true the stream will wait for new data and never end\n  reverse: false, // If true get from the newest to the oldest\n  gte: seq, // Start with this seq (inclusive)\n  gt: seq, // Start after this index\n  lte: seq, // Stop after this index\n  lt: seq, // Stop before this index\n  limit: -1 // Set to the max number of entries you want\n}\n```\n\nIf any of the `gte`, `gt`, `lte`, `lt` arguments are `< 0` then\nthey'll implicitly be added with the version before starting so\ndoing `{ gte: -1 }` makes a stream starting at the last index.\n\n#### `const stream = db.createDiffStream(otherVersion, [options])`\n\nEfficiently create a stream of the shallow changes between two versions of the db.\n\n`options` are the same as `db.createReadStream`, except for `reverse`.\n\nEach entry is sorted by key and looks like this:\n\n```js\n{\n  left: Object, // The entry in the `db`\n  right: Object // The entry in `otherVersion`\n}\n```\n\nIf an entry exists in db but not in the other version, then `left` is set\nand `right` will be null, and vice versa.\n\nIf the entries are causally equal (i.e. the have the same seq), they are not\nreturned, only the diff.\n\n#### `const entryWatcher = await db.getAndWatch(key, [options])`\n\nReturns a watcher which listens to changes on the given key.\n\n`entryWatcher.node` contains the current entry in the same format as the result of `bee.get(key)`, and will be updated as it changes.\n\nBy default, the node will have the bee's key- and value encoding, but you can overwrite it by setting the `keyEncoding` and `valueEncoding` options.\n\nYou can listen to `entryWatcher.on('update')` to be notified when the value of node has changed.\n\nCall `await watcher.close()` to stop the watcher.\n\n#### `const watcher = db.watch([range])`\n\nListens to changes that are on the optional `range`.\n\n`range` options are the same as `db.createReadStream` except for `reverse`.\n\nBy default, the yielded snapshots will have the bee's key- and value encoding, but you can overwrite them by setting the `keyEncoding` and `valueEncoding` options.\n\nUsage example:\n\n```js\nfor await (const [current, previous] of watcher) {\n  console.log(current.version)\n  console.log(previous.version)\n}\n```\n\nReturns a new value after a change, `current` and `previous` are snapshots that are auto-closed before next value.\n\nDon't close those snapshots yourself because they're used internally, let them be auto-closed.\n\nWatchers on subs and checkouts are not supported. Instead, use the range option to limit scope.\n\n`await watcher.ready()`\n\nWaits until the watcher is loaded and detecting changes.\n\n`await watcher.close()`\n\nStops the watcher. You could also stop it by using `break` in the loop.\n\n#### `const snapshot = db.checkout(version)`\n\nGet a read-only snapshot of a previous version.\n\n#### `const snapshot = db.snapshot()`\n\nShorthand for getting a checkout for the current version.\n\n#### `const sub = db.sub('sub-prefix', options = {})`\n\nCreate a sub-database where all entries will be prefixed by a given value.\n\nThis makes it easy to create namespaces within a single Hyperbee.\n\n`options` include:\n\n```\n{\n  sep: Buffer.alloc(1), // A namespace separator\n  valueEncoding, // Optional sub valueEncoding (defaults to the parents)\n  keyEncoding // Optional sub keyEncoding (defaults to the parents)\n}\n```\n\nFor example:\n\n```js\nconst root = new Hyperbee(core)\nconst sub = root.sub('a')\n\n// In root, this will have the key ('a' + separator + 'b')\nawait sub.put('b', 'hello')\n\n// Returns => { key: 'b', value: 'hello')\nawait sub.get('b')\n```\n\n#### `const header = await db.getHeader([options])`\n\nReturns the header contained in the first block. Throws if undecodable.\n\n`options` are the same as the `core.get` method.\n\n#### `const isHyperbee = await Hyperbee.isHyperbee(core, [options])`\n\nReturns `true` if the core contains a Hyperbee, `false` otherwise.\n\nThis requests the first block on the core, so it can throw depending on the options.\n\n`options` are the same as the `core.get` method.\n\n[core-replicate-docs]: https://github.com/holepunchto/hypercore#const-stream--corereplicateisinitiatororreplicationstream\n","publish_time":1769511945222,"_cnpm_publish_time":1769511945222,"dist-tags":{"latest":"2.27.3"}}