From 39b262cde46b6e0cfe887e8761672e9b02057477 Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Wed, 2 Jun 2021 16:10:16 +0100 Subject: [PATCH] Allow configuring of pubsub topic via cli #25 Fixes #59 --- cli.js | 1 + lib/commands/add.js | 2 +- lib/commands/export.js | 2 +- lib/commands/id.js | 2 +- lib/commands/import.js | 2 +- lib/commands/peers.js | 2 +- lib/commands/preload.js | 2 +- lib/commands/republish.js | 2 +- lib/commands/seed.js | 2 +- lib/commands/server.js | 2 +- lib/commands/update.js | 2 +- lib/commands/verify.js | 2 +- lib/commands/watch.js | 2 +- lib/core.js | 6 +++--- lib/forage.js | 23 ++++++++++------------- main.js | 8 +++++--- readme.md | 1 + test/hooks.js | 2 +- 18 files changed, 33 insertions(+), 32 deletions(-) diff --git a/cli.js b/cli.js index bccd9b4..ea91af9 100755 --- a/cli.js +++ b/cli.js @@ -73,4 +73,5 @@ yargs(hideBin(process.argv)) require('./lib/commands/trusted')(argv) }) .default('port', 8005) + .default('topic', 'forage') .argv diff --git a/lib/commands/add.js b/lib/commands/add.js index 96502b1..50ebe20 100644 --- a/lib/commands/add.js +++ b/lib/commands/add.js @@ -7,7 +7,7 @@ async function add(argv) { var manager = argv.manager var name = argv.name var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); var {version, cid} = await forage.importLatest(manager, name) diff --git a/lib/commands/export.js b/lib/commands/export.js index 7f15de1..55af06b 100644 --- a/lib/commands/export.js +++ b/lib/commands/export.js @@ -5,7 +5,7 @@ const forage = require('../forage'); async function export(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); var stats = await forage.core.exportPackages(db) console.log(stats) diff --git a/lib/commands/id.js b/lib/commands/id.js index 3351cbd..ca54af3 100644 --- a/lib/commands/id.js +++ b/lib/commands/id.js @@ -5,7 +5,7 @@ const forage = require('../forage'); async function id(argv) { var db = forage.connectDB() - var ipfsID = await forage.connectIPFS(db); + var ipfsID = await forage.connectIPFS(db, argv.topic); var key = await forage.signing.fetchPrivateKey(db) var jose = require('node-jose'); var string = forage.signing.encode(key) diff --git a/lib/commands/import.js b/lib/commands/import.js index 256150e..3ae9b43 100644 --- a/lib/commands/import.js +++ b/lib/commands/import.js @@ -8,7 +8,7 @@ const async = require('async'); async function import(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); var q = async.queue(async function(task) { var parts = task.pkg.split(':') diff --git a/lib/commands/peers.js b/lib/commands/peers.js index 56f84cf..c819ba8 100644 --- a/lib/commands/peers.js +++ b/lib/commands/peers.js @@ -5,7 +5,7 @@ const forage = require('../forage'); async function peers(argv) { var db = forage.connectDB() - var ipfsID = await forage.connectIPFS(db) + var ipfsID = await forage.connectIPFS(db, argv.topic) var peerIds = await forage.core.activePeers() diff --git a/lib/commands/preload.js b/lib/commands/preload.js index 7493a83..8f01588 100644 --- a/lib/commands/preload.js +++ b/lib/commands/preload.js @@ -8,7 +8,7 @@ const async = require('async'); async function preload(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); var packages = [] diff --git a/lib/commands/republish.js b/lib/commands/republish.js index cf73812..0f314fe 100644 --- a/lib/commands/republish.js +++ b/lib/commands/republish.js @@ -7,7 +7,7 @@ const async = require('async'); async function republish(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); var cids = {} diff --git a/lib/commands/seed.js b/lib/commands/seed.js index caf936a..ee79688 100644 --- a/lib/commands/seed.js +++ b/lib/commands/seed.js @@ -5,7 +5,7 @@ const forage = require('../forage'); async function seed(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); forage.seed() } diff --git a/lib/commands/server.js b/lib/commands/server.js index 97d9480..ae8006d 100644 --- a/lib/commands/server.js +++ b/lib/commands/server.js @@ -10,7 +10,7 @@ const forage = require('../forage'); async function server(argv) { var db = forage.connectDB() - var ipfsID = await forage.connectIPFS(db); + var ipfsID = await forage.connectIPFS(db, argv.topic); if(ipfsID){ var server = createServer(db) server.listen(argv.port) diff --git a/lib/commands/update.js b/lib/commands/update.js index e37a516..59b2905 100644 --- a/lib/commands/update.js +++ b/lib/commands/update.js @@ -5,7 +5,7 @@ const forage = require('../forage'); async function update(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); await forage.updateAll(function(pkg, res) { if(res){ diff --git a/lib/commands/verify.js b/lib/commands/verify.js index 04f223a..6d3543a 100644 --- a/lib/commands/verify.js +++ b/lib/commands/verify.js @@ -6,7 +6,7 @@ const async = require('async'); async function verify(argv) { var db = forage.connectDB() - await forage.connectIPFS(db); + await forage.connectIPFS(db, argv.topic); var q = async.queue(async function(pkg, callback) { var res = await forage.validate(pkg.manager, pkg.name, pkg.version) diff --git a/lib/commands/watch.js b/lib/commands/watch.js index 3ec80fe..1c38270 100644 --- a/lib/commands/watch.js +++ b/lib/commands/watch.js @@ -5,7 +5,7 @@ const forage = require('../forage'); async function watch(argv) { var db = forage.connectDB() - var ipfsID = await forage.connectIPFS(db); + var ipfsID = await forage.connectIPFS(db, argv.topic); if(ipfsID){ forage.watchAll(); } diff --git a/lib/core.js b/lib/core.js index 9880875..a37dd64 100644 --- a/lib/core.js +++ b/lib/core.js @@ -213,9 +213,9 @@ function forageVersion() { return packageJson.version } -async function unsubscribePackageAnnoucements(packageAnnoucementsTopic) { - await ipfs.pubsub.unsubscribe(packageAnnoucementsTopic) - log.info(`Unsubscribed from '${packageAnnoucementsTopic}' pubsub topic`) +async function unsubscribePackageAnnoucements(topic) { + await ipfs.pubsub.unsubscribe(topic) + log.info(`Unsubscribed from '${topic}' pubsub topic`) } async function exportPackages(db) { diff --git a/lib/forage.js b/lib/forage.js index b31b1cf..e7c1cef 100644 --- a/lib/forage.js +++ b/lib/forage.js @@ -26,8 +26,6 @@ const envPaths = require('env-paths'); const level = require('level-party') var db -const packageAnnoucementsTopic = 'forage' - var ipfsID = undefined; function connectDB(name = 'forage') { @@ -45,20 +43,20 @@ function concurrency() { return core.concurrency } -async function connectIPFS(db){ +async function connectIPFS(db, topic = 'forage'){ try{ ipfsID = await ipfs.id() log.info('Connected to IPFS') - subscribePackageAnnoucements(packageAnnoucementsTopic) - await core.dialPeers(db, ipfsID, packageAnnoucementsTopic) + subscribePackageAnnoucements(topic) + await core.dialPeers(db, ipfsID, topic) return ipfsID; } catch { log.error("Couldn't connect to IPFS, attempting to start go-ipfs") try{ var ipfsd = await core.startIPFS() - subscribePackageAnnoucements(packageAnnoucementsTopic) + subscribePackageAnnoucements(topic) ipfsID = await ipfsd.api.id() - await core.dialPeers(db, ipfsID, packageAnnoucementsTopic) + await core.dialPeers(db, ipfsID, topic) return ipfsID; } catch(e){ log.info('ERROR', e) @@ -129,13 +127,13 @@ async function defaultAnnounceCb(msg) { return action } -async function subscribePackageAnnoucements(packageAnnoucementsTopic) { +async function subscribePackageAnnoucements(topic) { try { - await ipfs.pubsub.subscribe(packageAnnoucementsTopic, defaultAnnounceCb) - log.info(`Subscribed to '${packageAnnoucementsTopic}' pubsub topic`) - await core.savePeers(db, packageAnnoucementsTopic) + await ipfs.pubsub.subscribe(topic, defaultAnnounceCb) + log.info(`Subscribed to '${topic}' pubsub topic`) + await core.savePeers(db, topic) } catch(e) { - log.error(`Failed to subscribe to '${packageAnnoucementsTopic}' pubsub topic`) + log.error(`Failed to subscribe to '${topic}' pubsub topic`) log.info("IPFS experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment") log.info(e) } @@ -401,7 +399,6 @@ module.exports = { watchKnown, validate, importPackage, - packageAnnoucementsTopic, downloadPackageFromIPFS, connectIPFS, connectDB, diff --git a/main.js b/main.js index 65c4079..370cc0f 100644 --- a/main.js +++ b/main.js @@ -23,6 +23,8 @@ var tray = undefined var win = undefined var db var started = false +var topic = 'forage' +var port = 8005 if(os.platform() === 'darwin'){ // Don't show the app in the doc @@ -37,10 +39,10 @@ app.on('ready', () => { }) async function startServer() { - var ipfsID = await forage.connectIPFS(db); + var ipfsID = await forage.connectIPFS(db, topic); if (ipfsID) { server = createServer(db) - server.listen(8005) + server.listen(port) // TODO decide on which packages to download via IPFS when announced (all or only versions of existing ones) forage.watchKnown(); forage.periodicUpdate(); @@ -53,7 +55,7 @@ async function startServer() { function stopServer() { console.log('stopping') server.close(); - forage.core.unsubscribePackageAnnoucements(forage.packageAnnoucementsTopic) + forage.core.unsubscribePackageAnnoucements(topic) started = false updateStatusMenu() tray.setImage(path.join(assetsDirectory, 'forageoffTemplate.png')) diff --git a/readme.md b/readme.md index 5c4e0a3..84522fd 100644 --- a/readme.md +++ b/readme.md @@ -115,6 +115,7 @@ Options: --help Show help [boolean] --version Show version number [boolean] --port [default: 8005] + --topic [default: "forage"] ``` ## Development diff --git a/test/hooks.js b/test/hooks.js index 2d204bd..976b7f6 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -4,7 +4,7 @@ const createServer = require('../lib/server'); exports.mochaHooks = { beforeAll: async function(){ db = forage.connectDB('forage-test') - ipfsID = await forage.connectIPFS(db) + ipfsID = await forage.connectIPFS(db, 'forage-test') server = createServer(db) server.listen(8006)