From d8f975bd96c0d4117cf7368105a58c1ae1d26c93 Mon Sep 17 00:00:00 2001 From: Sean Zellmer Date: Fri, 24 Feb 2023 22:03:04 -0600 Subject: [PATCH] chore: Add readme & basic example --- examples/basic.js | 44 +++++++ package-lock.json | 287 +++++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + readme.md | 79 +++++++++++++ 4 files changed, 408 insertions(+), 3 deletions(-) create mode 100644 examples/basic.js create mode 100644 readme.md diff --git a/examples/basic.js b/examples/basic.js new file mode 100644 index 0000000..c98b61f --- /dev/null +++ b/examples/basic.js @@ -0,0 +1,44 @@ +import { AutobaseManager } from '../index.js' +import Corestore from 'corestore' +import Hyperswarm from 'hyperswarm' +import Autobase from 'autobase' +import crypto from 'crypto' + +const corestore = new Corestore('./basic-example') +await corestore.ready() + +const mine = corestore.namespace('inputs').get({ name: 'input' }) +const output = corestore.namespace('outputs').get({ name: 'output' }) +await mine.ready() +await output.ready() +const base = new Autobase({ + inputs: [mine], + localInput: mine, + localOutput: output +}) + +// Create manager & ensure its ready +const manager = new AutobaseManager( + // Autobase to manage + base, + // allow() to filter core keys + () => true, + // Get function for a hypercore given a key + corestore.get.bind(corestore), + // Storage for managing autobase keys + corestore.storage) +await manager.ready() + +// Create connection to attach to +const swarm = new Hyperswarm() +swarm.on('connection', (conn, info) => { + console.log('found peer', info.publicKey.toString('hex').slice(-6)) + const stream = corestore.replicate(conn) + + // Attach manager + manager.attachStream(stream) +}) +const topic = crypto.createHash('sha256') + .update('autobase-manager-test') + .digest() +swarm.join(topic) diff --git a/package-lock.json b/package-lock.json index 9c7b345..faaa83b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "eslint-plugin-import": "^2.27.5", "eslint-plugin-n": "^15.6.1", "eslint-plugin-promise": "^6.1.1", + "hyperswarm": "^4.3.7", "random-access-memory": "^6.1.0", "tape": "^5.6.3" } @@ -80,6 +81,32 @@ "integrity": "sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==", "dev": true }, + "node_modules/@hyperswarm/dht": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@hyperswarm/dht/-/dht-6.5.0.tgz", + "integrity": "sha512-GehqKOueSJY0Jbv9UNdCw2PhQBnRYmlafINk8O7Xif6JEtHzA66mg09G5d6L7duoy+I57CNLUSyv5JTQ+NOkXQ==", + "dev": true, + "dependencies": { + "@hyperswarm/secret-stream": "^6.0.0", + "b4a": "^1.3.1", + "bogon": "^1.0.0", + "compact-encoding": "^2.4.1", + "compact-encoding-net": "^1.0.1", + "debugging-stream": "^2.0.0", + "dht-rpc": "^6.6.1", + "events": "^3.3.0", + "hypercore-crypto": "^3.3.0", + "noise-curve-ed": "^2.0.0", + "noise-handshake": "^3.0.0", + "record-cache": "^1.1.1", + "safety-catch": "^1.0.1", + "sodium-universal": "^4.0.0", + "xache": "^1.1.0" + }, + "bin": { + "hyperswarm-dht": "bin.js" + } + }, "node_modules/@hyperswarm/secret-stream": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/@hyperswarm/secret-stream/-/secret-stream-6.1.2.tgz", @@ -337,6 +364,16 @@ "nanoassert": "^2.0.0" } }, + "node_modules/bogon": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/bogon/-/bogon-1.1.0.tgz", + "integrity": "sha512-a6SnToksXHuUlgeMvI/txWmTcKz7c7iBa8f0HbXL4toN1Uza/CTQ4F7n9jSDX49TCpxv3KUP100q4sZfwLyLiw==", + "dev": true, + "dependencies": { + "compact-encoding": "^2.11.0", + "compact-encoding-net": "^1.2.0" + } + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -452,6 +489,15 @@ "b4a": "^1.3.0" } }, + "node_modules/compact-encoding-net": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/compact-encoding-net/-/compact-encoding-net-1.2.0.tgz", + "integrity": "sha512-LVXpNpF7PGQeHRVVLGgYWzuVoYAaDZvKUsUxRioGfkotzvOh4AzoQF1HBH3zMNaSnx7gJXuUr3hkjnijaH/Eng==", + "dev": true, + "dependencies": { + "compact-encoding": "^2.4.1" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -516,6 +562,15 @@ } } }, + "node_modules/debugging-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/debugging-stream/-/debugging-stream-2.0.0.tgz", + "integrity": "sha512-xwfl6wB/3xc553uwtGnSa94jFxnGOc02C0WU2Nmzwr80gzeqn1FX4VcbvoKIhe8L/lPq4BTQttAbrTN94uN8rA==", + "dev": true, + "dependencies": { + "streamx": "^2.12.4" + } + }, "node_modules/deep-equal": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-2.2.0.tgz", @@ -575,6 +630,25 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/dht-rpc": { + "version": "6.6.2", + "resolved": "https://registry.npmjs.org/dht-rpc/-/dht-rpc-6.6.2.tgz", + "integrity": "sha512-tOG4cEwere/Vi5y8zJb1D55ApPFRMyHwgVLmjPeROQhYc0/L2ZYw/h4u3XqGYI0i+vweBN2ZEJ99vk9iBsxd3A==", + "dev": true, + "dependencies": { + "b4a": "^1.3.1", + "compact-encoding": "^2.1.0", + "compact-encoding-net": "^1.0.1", + "events": "^3.3.0", + "fast-fifo": "^1.0.0", + "kademlia-routing-table": "^1.0.0", + "nat-sampler": "^1.0.1", + "sodium-universal": "^4.0.0", + "streamx": "^2.10.3", + "time-ordered-set": "^1.0.2", + "udx-native": "^1.2.0" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -1499,6 +1573,19 @@ "sodium-universal": "^4.0.0" } }, + "node_modules/hyperswarm": { + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/hyperswarm/-/hyperswarm-4.3.7.tgz", + "integrity": "sha512-COL8cMmBUa0ElZ85oFT1KQnZh1UkfJ75VavyypXZplGErlajqbnI2CDNF9K7cGiIk7xXR0RI6otx3495WSOB2Q==", + "dev": true, + "dependencies": { + "@hyperswarm/dht": "^6.0.1", + "b4a": "^1.3.1", + "events": "^3.3.0", + "safety-catch": "^1.0.2", + "shuffled-priority-queue": "^2.1.0" + } + }, "node_modules/ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", @@ -1912,6 +1999,12 @@ "json5": "lib/cli.js" } }, + "node_modules/kademlia-routing-table": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/kademlia-routing-table/-/kademlia-routing-table-1.0.1.tgz", + "integrity": "sha512-dKk19sC3/+kWhBIvOKCthxVV+JH0NrswSBq4sA4eOkkPMqQM1rRuOWte1WSKXeP8r9Nx4NuiH2gny3lMddJTpw==", + "dev": true + }, "node_modules/levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -2003,7 +2096,13 @@ "version": "2.2.2", "resolved": "https://registry.npmjs.org/napi-macros/-/napi-macros-2.2.2.tgz", "integrity": "sha512-hmEVtAGYzVQpCKdbQea4skABsdXW4RUh5t5mJ2zzqowJS2OyXZTU1KhDVFhx+NlWZ4ap9mqR9TcDO3LTTttd+g==", - "optional": true + "devOptional": true + }, + "node_modules/nat-sampler": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/nat-sampler/-/nat-sampler-1.0.1.tgz", + "integrity": "sha512-yQvyNN7xbqR8crTKk3U8gRgpcV1Az+vfCEijiHu9oHHsnIl8n3x+yXNHl42M6L3czGynAVoOT9TqBfS87gDdcw==", + "dev": true }, "node_modules/natural-compare": { "version": "1.4.0", @@ -2311,6 +2410,15 @@ "resolved": "https://registry.npmjs.org/random-array-iterator/-/random-array-iterator-1.0.0.tgz", "integrity": "sha512-u7xCM93XqKEvPTP6xZp2ehttcAemKnh73oKNf1FvzuVCfpt6dILDt1Kxl1LeBjm2iNIeR49VGFhy4Iz3yOun+Q==" }, + "node_modules/record-cache": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/record-cache/-/record-cache-1.2.0.tgz", + "integrity": "sha512-kyy3HWCez2WrotaL3O4fTn0rsIdfRKOdQQcEJ9KpvmKmbffKVvwsloX063EgRUlpJIXHiDQFhJcTbZequ2uTZw==", + "dev": true, + "dependencies": { + "b4a": "^1.3.1" + } + }, "node_modules/regexp.prototype.flags": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz", @@ -2508,6 +2616,15 @@ "node": ">=8" } }, + "node_modules/shuffled-priority-queue": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/shuffled-priority-queue/-/shuffled-priority-queue-2.1.0.tgz", + "integrity": "sha512-xhdh7fHyMsr0m/w2kDfRJuBFRS96b9l8ZPNWGaQ+PMvnUnZ/Eh+gJJ9NsHBd7P9k0399WYlCLzsy18EaMfyadA==", + "dev": true, + "dependencies": { + "unordered-set": "^2.0.1" + } + }, "node_modules/side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", @@ -2774,6 +2891,12 @@ "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", "dev": true }, + "node_modules/time-ordered-set": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/time-ordered-set/-/time-ordered-set-1.0.2.tgz", + "integrity": "sha512-vGO99JkxvgX+u+LtOKQEpYf31Kj3i/GNwVstfnh4dyINakMgeZCpew1e3Aj+06hEslhtHEd52g7m5IV+o1K8Mw==", + "dev": true + }, "node_modules/timeout-refresh": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/timeout-refresh/-/timeout-refresh-2.0.1.tgz", @@ -2829,6 +2952,20 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/udx-native": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/udx-native/-/udx-native-1.5.5.tgz", + "integrity": "sha512-jBDQvkX5DkgX0vxRzhe8COv6P9lIZnqTUSfdD0j578fLZINBAtab7DfUXg8bUodOm140h5s94FK+/zCXNLWJvw==", + "dev": true, + "hasInstallScript": true, + "dependencies": { + "b4a": "^1.5.0", + "events": "^3.3.0", + "napi-macros": "^2.0.0", + "node-gyp-build": "^4.4.0", + "streamx": "^2.12.0" + } + }, "node_modules/unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", @@ -2844,6 +2981,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/unordered-set": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/unordered-set/-/unordered-set-2.0.1.tgz", + "integrity": "sha512-eUmNTPzdx+q/WvOHW0bgGYLWvWHNT3PTKEQLg0MAQhc0AHASHVHoP/9YytYd4RBVariqno/mEUhVZN98CmD7bg==", + "dev": true + }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", @@ -3018,6 +3161,29 @@ "integrity": "sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==", "dev": true }, + "@hyperswarm/dht": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@hyperswarm/dht/-/dht-6.5.0.tgz", + "integrity": "sha512-GehqKOueSJY0Jbv9UNdCw2PhQBnRYmlafINk8O7Xif6JEtHzA66mg09G5d6L7duoy+I57CNLUSyv5JTQ+NOkXQ==", + "dev": true, + "requires": { + "@hyperswarm/secret-stream": "^6.0.0", + "b4a": "^1.3.1", + "bogon": "^1.0.0", + "compact-encoding": "^2.4.1", + "compact-encoding-net": "^1.0.1", + "debugging-stream": "^2.0.0", + "dht-rpc": "^6.6.1", + "events": "^3.3.0", + "hypercore-crypto": "^3.3.0", + "noise-curve-ed": "^2.0.0", + "noise-handshake": "^3.0.0", + "record-cache": "^1.1.1", + "safety-catch": "^1.0.1", + "sodium-universal": "^4.0.0", + "xache": "^1.1.0" + } + }, "@hyperswarm/secret-stream": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/@hyperswarm/secret-stream/-/secret-stream-6.1.2.tgz", @@ -3163,7 +3329,7 @@ "autobase": { "version": "git+ssh://git@github.com/holepunchto/autobase.git#7719d9a87fd191563044cb14468340ce42b6d4c7", "dev": true, - "from": "autobase@holepunchto/autobase", + "from": "autobase@github:holepunchto/autobase", "requires": { "b4a": "^1.3.1", "codecs": "^3.0.0", @@ -3214,6 +3380,16 @@ "nanoassert": "^2.0.0" } }, + "bogon": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/bogon/-/bogon-1.1.0.tgz", + "integrity": "sha512-a6SnToksXHuUlgeMvI/txWmTcKz7c7iBa8f0HbXL4toN1Uza/CTQ4F7n9jSDX49TCpxv3KUP100q4sZfwLyLiw==", + "dev": true, + "requires": { + "compact-encoding": "^2.11.0", + "compact-encoding-net": "^1.2.0" + } + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -3310,6 +3486,15 @@ "b4a": "^1.3.0" } }, + "compact-encoding-net": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/compact-encoding-net/-/compact-encoding-net-1.2.0.tgz", + "integrity": "sha512-LVXpNpF7PGQeHRVVLGgYWzuVoYAaDZvKUsUxRioGfkotzvOh4AzoQF1HBH3zMNaSnx7gJXuUr3hkjnijaH/Eng==", + "dev": true, + "requires": { + "compact-encoding": "^2.4.1" + } + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -3363,6 +3548,15 @@ "ms": "2.1.2" } }, + "debugging-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/debugging-stream/-/debugging-stream-2.0.0.tgz", + "integrity": "sha512-xwfl6wB/3xc553uwtGnSa94jFxnGOc02C0WU2Nmzwr80gzeqn1FX4VcbvoKIhe8L/lPq4BTQttAbrTN94uN8rA==", + "dev": true, + "requires": { + "streamx": "^2.12.4" + } + }, "deep-equal": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-2.2.0.tgz", @@ -3410,6 +3604,25 @@ "integrity": "sha512-hsBd2qSVCRE+5PmNdHt1uzyrFu5d3RwmFDKzyNZMFq/EwDNJF7Ee5+D5oEKF0hU6LhtoUF1macFvOe4AskQC1Q==", "dev": true }, + "dht-rpc": { + "version": "6.6.2", + "resolved": "https://registry.npmjs.org/dht-rpc/-/dht-rpc-6.6.2.tgz", + "integrity": "sha512-tOG4cEwere/Vi5y8zJb1D55ApPFRMyHwgVLmjPeROQhYc0/L2ZYw/h4u3XqGYI0i+vweBN2ZEJ99vk9iBsxd3A==", + "dev": true, + "requires": { + "b4a": "^1.3.1", + "compact-encoding": "^2.1.0", + "compact-encoding-net": "^1.0.1", + "events": "^3.3.0", + "fast-fifo": "^1.0.0", + "kademlia-routing-table": "^1.0.0", + "nat-sampler": "^1.0.1", + "sodium-universal": "^4.0.0", + "streamx": "^2.10.3", + "time-ordered-set": "^1.0.2", + "udx-native": "^1.2.0" + } + }, "doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -4103,6 +4316,19 @@ "sodium-universal": "^4.0.0" } }, + "hyperswarm": { + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/hyperswarm/-/hyperswarm-4.3.7.tgz", + "integrity": "sha512-COL8cMmBUa0ElZ85oFT1KQnZh1UkfJ75VavyypXZplGErlajqbnI2CDNF9K7cGiIk7xXR0RI6otx3495WSOB2Q==", + "dev": true, + "requires": { + "@hyperswarm/dht": "^6.0.1", + "b4a": "^1.3.1", + "events": "^3.3.0", + "safety-catch": "^1.0.2", + "shuffled-priority-queue": "^2.1.0" + } + }, "ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", @@ -4395,6 +4621,12 @@ "minimist": "^1.2.0" } }, + "kademlia-routing-table": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/kademlia-routing-table/-/kademlia-routing-table-1.0.1.tgz", + "integrity": "sha512-dKk19sC3/+kWhBIvOKCthxVV+JH0NrswSBq4sA4eOkkPMqQM1rRuOWte1WSKXeP8r9Nx4NuiH2gny3lMddJTpw==", + "dev": true + }, "levn": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", @@ -4468,7 +4700,13 @@ "version": "2.2.2", "resolved": "https://registry.npmjs.org/napi-macros/-/napi-macros-2.2.2.tgz", "integrity": "sha512-hmEVtAGYzVQpCKdbQea4skABsdXW4RUh5t5mJ2zzqowJS2OyXZTU1KhDVFhx+NlWZ4ap9mqR9TcDO3LTTttd+g==", - "optional": true + "devOptional": true + }, + "nat-sampler": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/nat-sampler/-/nat-sampler-1.0.1.tgz", + "integrity": "sha512-yQvyNN7xbqR8crTKk3U8gRgpcV1Az+vfCEijiHu9oHHsnIl8n3x+yXNHl42M6L3czGynAVoOT9TqBfS87gDdcw==", + "dev": true }, "natural-compare": { "version": "1.4.0", @@ -4698,6 +4936,15 @@ "resolved": "https://registry.npmjs.org/random-array-iterator/-/random-array-iterator-1.0.0.tgz", "integrity": "sha512-u7xCM93XqKEvPTP6xZp2ehttcAemKnh73oKNf1FvzuVCfpt6dILDt1Kxl1LeBjm2iNIeR49VGFhy4Iz3yOun+Q==" }, + "record-cache": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/record-cache/-/record-cache-1.2.0.tgz", + "integrity": "sha512-kyy3HWCez2WrotaL3O4fTn0rsIdfRKOdQQcEJ9KpvmKmbffKVvwsloX063EgRUlpJIXHiDQFhJcTbZequ2uTZw==", + "dev": true, + "requires": { + "b4a": "^1.3.1" + } + }, "regexp.prototype.flags": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz", @@ -4838,6 +5085,15 @@ "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", "dev": true }, + "shuffled-priority-queue": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/shuffled-priority-queue/-/shuffled-priority-queue-2.1.0.tgz", + "integrity": "sha512-xhdh7fHyMsr0m/w2kDfRJuBFRS96b9l8ZPNWGaQ+PMvnUnZ/Eh+gJJ9NsHBd7P9k0399WYlCLzsy18EaMfyadA==", + "dev": true, + "requires": { + "unordered-set": "^2.0.1" + } + }, "side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", @@ -5054,6 +5310,12 @@ "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", "dev": true }, + "time-ordered-set": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/time-ordered-set/-/time-ordered-set-1.0.2.tgz", + "integrity": "sha512-vGO99JkxvgX+u+LtOKQEpYf31Kj3i/GNwVstfnh4dyINakMgeZCpew1e3Aj+06hEslhtHEd52g7m5IV+o1K8Mw==", + "dev": true + }, "timeout-refresh": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/timeout-refresh/-/timeout-refresh-2.0.1.tgz", @@ -5097,6 +5359,19 @@ "is-typed-array": "^1.1.9" } }, + "udx-native": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/udx-native/-/udx-native-1.5.5.tgz", + "integrity": "sha512-jBDQvkX5DkgX0vxRzhe8COv6P9lIZnqTUSfdD0j578fLZINBAtab7DfUXg8bUodOm140h5s94FK+/zCXNLWJvw==", + "dev": true, + "requires": { + "b4a": "^1.5.0", + "events": "^3.3.0", + "napi-macros": "^2.0.0", + "node-gyp-build": "^4.4.0", + "streamx": "^2.12.0" + } + }, "unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", @@ -5109,6 +5384,12 @@ "which-boxed-primitive": "^1.0.2" } }, + "unordered-set": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/unordered-set/-/unordered-set-2.0.1.tgz", + "integrity": "sha512-eUmNTPzdx+q/WvOHW0bgGYLWvWHNT3PTKEQLg0MAQhc0AHASHVHoP/9YytYd4RBVariqno/mEUhVZN98CmD7bg==", + "dev": true + }, "uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/package.json b/package.json index 51ae44f..afa8d76 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "eslint-plugin-import": "^2.27.5", "eslint-plugin-n": "^15.6.1", "eslint-plugin-promise": "^6.1.1", + "hyperswarm": "^4.3.7", "random-access-memory": "^6.1.0", "tape": "^5.6.3" }, diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d93139b --- /dev/null +++ b/readme.md @@ -0,0 +1,79 @@ +# Autobase Manager + +A simple "replicate everything" approach to synchronizing `autobase`s' `inputs` +and `outputs` via a frame stream (most likely `hyperswarm`). + +## Usage + +```js +const AutobaseManager = require('@lejeunerenard/autobase-manager') +// ... require other modules + +// Create an Autobase +const base = new Autobase({ ... }) + +const manager = new AutobaseManager( + base, + // function to filter core keys + (key, coreType, channel) => true, + // get(key) function to get a hypercore given a key + corestore.get.bind(corestore), + // Storage for managing autobase keys + corestore.storage) + +// Wait until everything is loaded +await manager.ready() + +const swarm = new Hyperswarm() +swarm.on('connection', (conn) => { + const stream = corestore.replicate(conn) + + // Attach manager + manager.attachStream(stream) +}) +``` + +## API + +`const manager = new Autobase(base, allow, get, storage)` + +Create a new manager given an autobase, allow function, a means of getting a +core and a storage for persisting keys distributed to load on start. + +- `base` an Autobase to be managed +- `allow` a function which returns a boolean for whether to add a core or not. + The function is passed the following arguments: `allow(key, coreType, + channel)`: + - `key` is the key as a hexadecimal string of a core announced by a peer to be + replicated + - `coreType` is either `input` or `output` representing whether the core is to + be added as an input or output on the Autobase. + - `channel` is the underlying `Protomux` channel in case more advanced logic + is needed. +- `get(key)` is a function for retrieving a Hypercore when given a key. +- `storage` is a directory where you want to store managed keys or alternatively + you own [abstract-random-access](https://github.com/random-access-storage/abstract-random-access) + instance. For example, if using a Corestore, `corestore.storage`. + +`await manager.ready()` + +Returns a promise that resolves when the manager has loaded all known cores from +storage. + +`manager.attachStream(stream)` + +Attach manager onto a framed stream to be extended and used for coordinating +Hypercore keys. Most likely you will attach to every connection with a peer. + +All known and `allow`ed keys will be announced to the attached stream upon +attaching and as cores are added via peers. + +`await manager.announceAll()` + +Manually announce to all streams the manager has been attached to. Helpful for +sharing cores that were added to the Autobase not by the manager. + +`await manager.updateStorageKeys()` + +Update storage keys to be persisted and announce all known keys to all attached +streams.