3 Commits

Author SHA1 Message Date
b11dbb9fde 1.1.0 2019-08-13 11:10:09 +02:00
gregkare
bfb51d48bc Merge branch 'feature/connect_to_kosmos_ipfs_node' of kosmos/kredits-ipfs-pinner into master 2019-08-13 09:02:34 +00:00
f2f884a317 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
2019-08-13 10:07:25 +02:00
3 changed files with 18 additions and 3 deletions

View File

@@ -2,6 +2,7 @@
const Kredits = require('kredits-contracts'); const Kredits = require('kredits-contracts');
const IpfsPinner = require('./lib/ipfs-pinner'); const IpfsPinner = require('./lib/ipfs-pinner');
const debug = require('debug')('ipfs-pinner');
const argv = require('yargs') const argv = require('yargs')
.default({ network: 'rinkeby', apm: 'open.aragonpm.eth', host: 'localhost', port: '5001', protocol: 'http', monitor: true }) .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); 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 () => { (async () => {
try { try {
const kredits = await Kredits.for( const kredits = await Kredits.for(
@@ -35,7 +45,12 @@ console.log(`Using IPFS:`, ipfsConfig);
// check the connection to the IPFS client // check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute // 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); const ipfsPinner = new IpfsPinner(kredits);

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "@67p/kredits-ipfs-pinner", "name": "@67p/kredits-ipfs-pinner",
"version": "1.0.3", "version": "1.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kosmos/kredits-ipfs-pinner", "name": "@kosmos/kredits-ipfs-pinner",
"version": "1.0.3", "version": "1.1.0",
"description": "Pins IPFS data of a Kredits organization on an IPFS node", "description": "Pins IPFS data of a Kredits organization on an IPFS node",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {