Skip to content

Commit

Permalink
feature: start ipfs-daemon but ipfs node can't start in main process
Browse files Browse the repository at this point in the history
  • Loading branch information
lindongwu committed Feb 2, 2018
1 parent 17ade0a commit 64d82b6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/electron-starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const url = require('url');
const createWindow = require('./createWindow');
const sha512 = require('hash.js/lib/hash/sha/512');

const initIPFS = require('./ipfs')
const { initIPFS, killIPFSD } = require('./ipfs');

/* Keep a global reference of the window object, if you don't, the window will be closed automatically when the JavaScript object is garbage collected. */
let appWindow;
Expand All @@ -25,6 +25,7 @@ const startUrl =
});

app.on('ready', () => {
initIPFS();
appWindow = createWindow('app', startUrl, signature);
apiWindow = createWindow('api', startUrl, signature, true);
if (isDev) {
Expand All @@ -37,6 +38,7 @@ app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
killIPFSD();
app.quit();
}
});
Expand Down
112 changes: 71 additions & 41 deletions src/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,74 @@ const IPFS = require('ipfs');
const DaemonFactory = require('ipfsd-ctl');

// Create
const repo = '/Users/dongwu/Desktop/ipfsRepo';

const ipfsDaemon = DaemonFactory.create()
ipfsDaemon.spawn(function (err, ipfsd) {
if (err) { throw err }

ipfsd.api.id(function (err, id) {
if (err) { throw err }

console.log(id)
ipfsd.stop()
})
})

// export const ipfsNode = new IPFS({
// repo,
// EXPERIMENTAL: { // enable experimental features
// pubsub: true,
// sharding: true, // enable dir sharding
// dht: true // enable KadDHT, currently not interopable with go-ipfs
// },
// config: { // overload the default IPFS node config, find defaults at https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime
// Addresses: {
// Swarm: [
// '/ip4/127.0.0.1/tcp/1337'
// ]
// }
// },
// libp2p: { // add custom modules to the libp2p stack of your node
// modules: {}
// }
// })
// ipfsNode.on('ready', () => {}) // Node is ready to use when you first create it
// ipfsNode.on('error', (err) => {}) // Node has hit some error while initing/starting

// ipfsNode.on('init', () => {}) // Node has successfully finished initing the repo
// ipfsNode.on('start', () => {}) // Node has started
// ipfsNode.on('stop', () => {}) // Node has stopped

function initIPFS() {}
module.exports = initIPFS;
const repoPath = '/Users/dongwu/Desktop/ipfsRepo';

let _ipfsd = null;
const ipfsDaemon = DaemonFactory.create();

function initIPFS() {
ipfsDaemon.spawn({ repoPath }, (err, ipfsd) => {
if (err) {
throw err;
}
_ipfsd = ipfsd;

ipfsd.api.id((err, id) => {
if (err) {
throw err;
}

console.log(id);

const ipfsNode = new IPFS({
repo: repoPath,
EXPERIMENTAL: {
// enable experimental features
pubsub: true,
sharding: true, // enable dir sharding
dht: true, // enable KadDHT, currently not interopable with go-ipfs
},
// config: {
// // overload the default IPFS node config, find defaults at https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime
// Addresses: {
// Swarm: ['/ip4/127.0.0.1/tcp/1337'],
// },
// },
libp2p: {
// add custom modules to the libp2p stack of your node
modules: {},
},
});

// Node is ready to use when you first create it
ipfsNode.on('ready', () => {
console.log('ready');
});
// Node has hit some error while initing/starting
ipfsNode.on('error', error => {
console.error(error);
});

// Node has successfully finished initing the repo
ipfsNode.on('init', () => {
console.log('init');
});
// Node has started
ipfsNode.on('start', () => {
console.log('start');
});
// Node has stopped
ipfsNode.on('stop', () => {
console.log('stop');
});
});
});
}

function killIPFSD() {
if (_ipfsd) {
_ipfsd.stop();
}
}

module.exports = { initIPFS, killIPFSD };

0 comments on commit 64d82b6

Please sign in to comment.