Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit cd906cd

Browse files
authored
WIP - All new way of initing a node. (#790)
feat: new API to create node - no more config set madness
1 parent 669fc28 commit cd906cd

File tree

23 files changed

+533
-327
lines changed

23 files changed

+533
-327
lines changed

README.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -196,46 +196,54 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/
196196

197197
#### Create a IPFS node instance
198198

199-
The basic startup flow involves (optionally) creating a Repo, creating an IPFS node, `init`-ing it so it can generate its keys, `load`-ing its configuration, and putting it online with `goOnline`. Here is a structural example:
199+
Creating an IPFS instance couldn't be easier, all you have to do is:
200+
201+
```JavaScript
202+
// Create the IPFS node instance
203+
const node = new IPFS()
204+
205+
node.on('start', () => {
206+
// Your now is ready to use \o/
207+
208+
// stopping a node
209+
node.stop(() => {
210+
// node is now 'offline'
211+
})
212+
})
213+
```
214+
215+
#### Advanced options when creating an IPFS node.
216+
217+
When starting a node, you can:
200218

201219
```JavaScript
202220
// IPFS will need a repo, it can create one for you or you can pass
203221
// it a repo instance of the type IPFS Repo
204222
// https://github.com/ipfs/js-ipfs-repo
205223
const repo = <IPFS Repo instance or repo path>
206224

207-
// Create the IPFS node instance
208225
const node = new IPFS({
209226
repo: repo,
210-
EXPERIMENTAL: {
211-
pubsub: false
227+
init: true, // default
228+
// init: false,
229+
// init: {
230+
// bits: 1024 // size of the RSA key generated
231+
// },
232+
start: true,
233+
// start: false,
234+
EXPERIMENTAL: { // enable experimental features
235+
pubsub: true
236+
},
237+
config: { // overload the default config
238+
Addresses: {
239+
Swarm: [
240+
'/ip4/127.0.0.1/tcp/1337'
241+
]
242+
}
212243
}
213244
})
214-
215-
// We need to init our repo, in this case the repo was empty
216-
// We are picking 2048 bits for the RSA key that will be our PeerId
217-
node.init({ emptyRepo: true, bits: 2048 }, (err) => {
218-
if (err) { throw err }
219-
220-
// Once the repo is initiated, we have to load it so that the IPFS
221-
// instance has its config values. This is useful when you have
222-
// previous created repos and you don't need to generate a new one
223-
node.load((err) => {
224-
if (err) { throw err }
225-
226-
// Last but not the least, we want our IPFS node to use its peer
227-
// connections to fetch and serve blocks from.
228-
node.goOnline((err) => {
229-
if (err) { throw err }
230-
// Here you should be good to go and call any IPFS function
231-
})
232-
})
233245
```
234246

235-
> We are working on making this init process better, see https://github.com/ipfs/js-ipfs/issues/556 for the discussion.
236-
237-
More examples can be found in the [examples folder](./examples)
238-
239247
### [Tutorials and Examples](/examples)
240248

241249
You can find some examples and tutorials in the [examples](/examples) folder, these exist to help you get started using `js-ipfs`.

examples/basics/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const IPFS = require('../../src/core')
1414
*/
1515
const node = new IPFS({
1616
repo: path.join(os.tmpdir() + '/' + new Date().toString()),
17+
init: false,
18+
start: false,
1719
EXPERIMENTAL: {
1820
pubsub: false
1921
}
@@ -42,14 +44,10 @@ series([
4244
* Initialize the repo for this node
4345
*/
4446
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
45-
/*
46-
* Load the repo config into the IPFS node
47-
*/
48-
(cb) => node.load(cb),
4947
/*
5048
* Take the node online (bitswap, network and so on)
5149
*/
52-
(cb) => node.goOnline(cb),
50+
(cb) => node.start(cb),
5351
/*
5452
* Add a file to IPFS - Complete Files API on:
5553
* https://github.com/ipfs/interface-ipfs-core/tree/master/API/files

examples/browser-script-tag/index.html

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,28 @@
44
<title>IPFS in the Browser</title>
55
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
66
<script type="text/javascript">
7-
// We provide a hosted signalling endpoint that you can use to discover
8-
// and dial to other nodes. It is hosted at `star-signal.cloud.ipfs.team`
9-
// If you run your own signalling, you can change this multiaddr.
10-
const SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/'
11-
127
const repoPath = 'ipfs-' + Math.random()
138

149
// Create an IPFS node
1510
const node = new Ipfs({
11+
init: false,
12+
start: false
1613
repo: repoPath
1714
})
1815

1916
// Init the node
2017
node.init(handleInit)
2118

2219
function handleInit (err) {
23-
if (!err) { // The repo was initialized for the first time, we need to configure it
24-
addWebRTCMultiaddr()
25-
} else if (err && err.message !== 'repo already exists') { // The repo already existed, let's just load it
26-
loadRepo()
27-
} else {
20+
if (err) {
2821
throw err
2922
}
30-
}
31-
32-
function addWebRTCMultiaddr() {
33-
// Addj the WebrTCStar Multiaddr to your node
34-
node.config.get(function (err, config) {
35-
if (err) {
36-
throw err
37-
}
38-
39-
const starAddr = (SIGNALING_SERVER + config.Identity.PeerID)
40-
41-
node.config.set('Addresses.Swarm[1]', starAddr, loadRepo)
42-
})
43-
}
4423

45-
function loadRepo() {
46-
node.load(() => node.goOnline(() => {
24+
node.start(() => {
4725
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')
4826

4927
document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')
5028

51-
// \o/ Now you have an IPFS node using WebRTC to find other nodes!
5229
// You can write more code here to use it. Use methods like
5330
// node.files.add, node.files.get. See the API docs here:
5431
// https://github.com/ipfs/interface-ipfs-core/tree/master/API

examples/bundle-browserify/src/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const repoPath = String(Math.random())
1111

1212
const node = new IPFS({
1313
repo: repoPath,
14+
init: false,
15+
start: false,
1416
EXPERIMENTAL: {
1517
pubsub: false
1618
}
@@ -23,17 +25,12 @@ node.init({ emptyRepo: true, bits: 2048 }, function (err) {
2325
if (err) {
2426
throw err
2527
}
26-
node.load(function (err) {
28+
29+
node.start(function (err) {
2730
if (err) {
2831
throw err
2932
}
30-
31-
node.goOnline(function (err) {
32-
if (err) {
33-
throw err
34-
}
35-
console.log('IPFS node is ready')
36-
})
33+
console.log('IPFS node is ready')
3734
})
3835
})
3936

examples/bundle-webpack/src/components/app.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ class App extends React.Component {
2929
// for simplicity, we create a new repo everytime the node
3030
// is created, because you can't init already existing repos
3131
const repoPath = String(Math.random())
32+
3233
node = new IPFS({
3334
repo: repoPath,
35+
init: false,
36+
start: false,
3437
EXPERIMENTAL: {
3538
pubsub: false
3639
}
@@ -40,18 +43,13 @@ class App extends React.Component {
4043
if (err) {
4144
throw err
4245
}
43-
node.load(function (err) {
46+
47+
node.start(function (err) {
4448
if (err) {
4549
throw err
4650
}
47-
48-
node.goOnline(function (err) {
49-
if (err) {
50-
throw err
51-
}
52-
console.log('IPFS node is ready')
53-
ops()
54-
})
51+
console.log('IPFS node is ready')
52+
ops()
5553
})
5654
})
5755
}

examples/dag/create-node.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22

3-
const series = require('async/series')
4-
53
const IPFS = require('../../src/core')
64
// In your project, replace by the following line and install IPFS as a dep
75
// const IPFS = require('ipfs')
@@ -18,11 +16,7 @@ function createNode (options, callback) {
1816
repo: options.path
1917
})
2018

21-
series([
22-
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
23-
(cb) => node.load(cb),
24-
(cb) => node.goOnline(cb)
25-
], (err) => callback(err, node))
19+
node.on('start', () => callback(null, node))
2620
}
2721

2822
module.exports = createNode

gulpfile.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,37 @@ let nodes = []
1616
function spawnDaemon (num, callback) {
1717
num = leftPad(num, 3, 0)
1818

19-
const repo = createTempRepo()
20-
2119
const node = new IPFS({
22-
repo: repo,
20+
repo: createTempRepo(),
21+
init: {
22+
bits: 1024
23+
},
24+
start: false,
2325
EXPERIMENTAL: {
2426
pubsub: true
27+
},
28+
config: {
29+
Addresses: {
30+
Swarm: [
31+
`/ip4/127.0.0.1/tcp/10${num}`,
32+
`/ip4/127.0.0.1/tcp/20${num}/ws`
33+
],
34+
API: `/ip4/127.0.0.1/tcp/31${num}`,
35+
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
36+
},
37+
Discovery: {
38+
MDNS: {
39+
Enabled: false
40+
}
41+
}
2542
}
2643
})
2744

28-
series([
29-
(cb) => node.init({ emptyRepo: true, bits: 1024 }, cb),
30-
(cb) => {
31-
repo.config.get((err, config) => {
32-
if (err) { return callback(err) }
33-
34-
config.Addresses = {
35-
Swarm: [
36-
`/ip4/127.0.0.1/tcp/10${num}`,
37-
`/ip4/127.0.0.1/tcp/20${num}/ws`
38-
],
39-
API: `/ip4/127.0.0.1/tcp/31${num}`,
40-
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
41-
}
42-
43-
config.Discovery.MDNS.Enabled = false
44-
45-
repo.config.set(config, cb)
46-
})
47-
},
48-
(cb) => node.load(cb),
49-
(cb) => {
50-
const daemon = new HTTPAPI(node.repo.path())
51-
nodes.push(daemon)
52-
daemon.start(cb)
53-
}
54-
], callback)
45+
setTimeout(() => {
46+
const daemon = new HTTPAPI(node.repo.path())
47+
nodes.push(daemon)
48+
daemon.start(callback)
49+
}, 400)
5550
}
5651

5752
gulp.task('libnode:start', (done) => {
@@ -66,7 +61,7 @@ gulp.task('libnode:start', (done) => {
6661

6762
gulp.task('libnode:stop', (done) => {
6863
series(nodes.map((node) => (cb) => {
69-
setTimeout(() => node.stop(cb), 200)
64+
setTimeout(() => node.stop(cb), 100)
7065
}), done)
7166
})
7267

src/cli/commands/init.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ module.exports = {
1717
default: '2048',
1818
describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)'
1919
},
20-
force: {
21-
alias: 'f',
22-
type: 'boolean',
23-
describe: 'Overwrite existing config (if it exists)'
24-
},
2520
emptyRepo: {
2621
alias: 'e',
2722
type: 'boolean',
@@ -39,16 +34,16 @@ module.exports = {
3934
stores: Store
4035
})
4136

42-
const ipfs = new IPFS({
37+
const node = new IPFS({
4338
repo: repo,
44-
EXPERIMENTAL: {}
39+
init: false,
40+
start: false
4541
})
4642

47-
ipfs.init({
43+
node.init({
4844
bits: argv.bits,
49-
force: argv.force,
5045
emptyRepo: argv.emptyRepo,
51-
log
46+
log: log
5247
}, (err) => {
5348
if (err) {
5449
console.error(err.toString())

src/cli/utils.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ function getAPICtl () {
3535
}
3636

3737
exports.getIPFS = (callback) => {
38-
if (!isDaemonOn()) {
39-
const ipfs = new IPFS({
40-
repo: exports.getRepoPath(),
41-
EXPERIMENTAL: {
42-
pubsub: true
43-
}
44-
})
45-
ipfs.load(() => callback(null, ipfs))
46-
return
38+
if (isDaemonOn()) {
39+
return callback(null, getAPICtl())
4740
}
4841

49-
callback(null, getAPICtl())
42+
const node = new IPFS({
43+
repo: exports.getRepoPath(),
44+
init: false,
45+
start: false,
46+
EXPERIMENTAL: {
47+
pubsub: true
48+
}
49+
})
50+
51+
node.preStart(() => callback(null, node))
5052
}
5153

5254
exports.getRepoPath = () => {

0 commit comments

Comments
 (0)