From f2f884a3171b8236c48bba87fd6b8422a1688bab Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Tue, 13 Aug 2019 10:07:25 +0200 Subject: [PATCH] Connect to ipfs.kosmos.org node on start This ensures that the local IPFS node is always connected to a known Kosmos node before attempting to fetch documents. Vastly improves performance. closes #4 --- index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d2d6918..d66e956 100755 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const Kredits = require('kredits-contracts'); const IpfsPinner = require('./lib/ipfs-pinner'); +const debug = require('debug')('ipfs-pinner'); const argv = require('yargs') .default({ network: 'rinkeby', apm: 'open.aragonpm.eth', host: 'localhost', port: '5001', protocol: 'http', monitor: true }) @@ -26,6 +27,15 @@ const ipfsConfig = { console.log(`Using IPFS:`, ipfsConfig); +async function swarmConnect (ipfsApi, address) { + return new Promise((resolve, reject) => { + debug(`Connecting to known IPFS node: ${address}`); + ipfsApi.swarm.connect(address, err => { + return err ? reject(err) : resolve(); + }) + }); +} + (async () => { try { const kredits = await Kredits.for( @@ -35,7 +45,12 @@ console.log(`Using IPFS:`, ipfsConfig); // check the connection to the IPFS client // TODO redesign IPFS wrapper API and do not use an internal attribute - await kredits.ipfs._ipfsAPI.id(); + const ipfsApi = kredits.ipfs._ipfsAPI; + + await ipfsApi.id(); + + // TODO discover address dynamically from somewhere + await swarmConnect(ipfsApi, '/ip4/46.4.18.160/tcp/4001/ipfs/QmZ4Lpzhz8bafbTYvEMMCmrbrMM4JfyHDR23WbCSAd9bo7'); const ipfsPinner = new IpfsPinner(kredits);