From ff26e175a84d051136da6cc12704a3a5d40c0232 Mon Sep 17 00:00:00 2001 From: James Keane Date: Thu, 14 Dec 2023 02:59:28 -0500 Subject: [PATCH] Added usage example in README. Also made the DHT constructor a bit more user friendly. --- README.md | 12 ++++++++++++ index.js | 1 - package.json | 2 +- src/index.js | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) delete mode 100644 index.js diff --git a/README.md b/README.md index a1f6430..7160dfb 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,15 @@ An implementation of the Kademlia DHT. Supports: - BEP-42 with `sse4_crc32` optional dependency. - BEP-44 with `ed25519-supercop` optional dependency. + +## Usage +```javascript + import DHT from 'kadem'; + + // load DHT from stored state file; if it doesn't + // exist or is not provided it'll bootstrap itself. + const dht = DHT.load('.dht_state'); + dht.listen(/** listening port, can be anything */ 8468); + + const value = await dht.get({ k: key, salt: salt }); +``` diff --git a/index.js b/index.js deleted file mode 100644 index f472c54..0000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./src/index'); diff --git a/package.json b/package.json index 0693b29..040a52e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "kadem", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "src/index.js", "scripts": { "test": "mocha test/**/*.js" }, diff --git a/src/index.js b/src/index.js index f2ba24b..b376385 100644 --- a/src/index.js +++ b/src/index.js @@ -155,6 +155,8 @@ util.inherits(DHT, EventEmitter); * @param {string} filename The filename to load from. */ DHT.load = function(filename) { + if (filename == undefined || !fs.existsSync(filename)) return new DHT(); + let state = JSON.parse(fs.readFileSync(filename).toString('utf-8')); return new DHT({ K: state.K,