Skip to content

Commit

Permalink
Allow configuring of pubsub topic via cli
Browse files Browse the repository at this point in the history
#25

Fixes #59
  • Loading branch information
andrew committed Jun 2, 2021
1 parent aca5463 commit 39b262c
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 32 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ yargs(hideBin(process.argv))
require('./lib/commands/trusted')(argv)
})
.default('port', 8005)
.default('topic', 'forage')
.argv
2 changes: 1 addition & 1 deletion lib/commands/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(':')
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/republish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 10 additions & 13 deletions lib/forage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -401,7 +399,6 @@ module.exports = {
watchKnown,
validate,
importPackage,
packageAnnoucementsTopic,
downloadPackageFromIPFS,
connectIPFS,
connectDB,
Expand Down
8 changes: 5 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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'))
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Options:
--help Show help [boolean]
--version Show version number [boolean]
--port [default: 8005]
--topic [default: "forage"]
```

## Development
Expand Down
2 changes: 1 addition & 1 deletion test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 39b262c

Please sign in to comment.