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

Commit 829d0fb

Browse files
committed
feat: read config from repo
Lets us do things like `jsipfs config --bool EXPERIMENTAL.pubsub true` and have IPFS respect the flags in daemon and non-daemon mode License: MIT Signed-off-by: Alex Potsides <[email protected]>
1 parent d394a12 commit 829d0fb

File tree

4 files changed

+135
-15
lines changed

4 files changed

+135
-15
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ You can check the development status at the [Waffle Board](https://waffle.io/ipf
6161
- [IPFS CLI](#ipfs-cli)
6262
- [IPFS Daemon](#ipfs-daemon)
6363
- [IPFS Module (use IPFS as a module in Node.js or in the Browser)](#ipfs-module)
64+
- [Experimental Features](#experimental-features)
6465
- [Tutorials and Examples](#tutorials-and-examples)
6566
- [API Docs](#api)
6667
- [Constructor](#ipfs-constructor)
@@ -187,6 +188,19 @@ node.on('ready', () => {
187188
})
188189
```
189190

191+
### Experimental Features
192+
193+
To enable or disable experimental features, please use the `jsipfs config` command:
194+
195+
```sh
196+
$ jsipfs config --bool EXPERIMENTAL.pubsub true
197+
$ jsipfs config --bool EXPERIMENTAL.pubsub false
198+
```
199+
200+
If running the daemon you will need to restart it for this to take effect.
201+
202+
See the `EXPERIMENTAL` options object passed to the IPFS constructor for which keys map to which experimental features.
203+
190204
### [Tutorials and Examples](/examples)
191205

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"libp2p-webrtc-star": "~0.15.0",
136136
"libp2p-websocket-star": "~0.8.0",
137137
"libp2p-websockets": "~0.12.0",
138+
"lodash.defaultsdeep": "^4.6.0",
138139
"lodash.flatmap": "^4.5.0",
139140
"lodash.get": "^4.4.2",
140141
"lodash.set": "^4.3.2",

src/core/index.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const CID = require('cids')
1616
const debug = require('debug')
1717
const extend = require('deep-extend')
1818
const EventEmitter = require('events')
19+
const waterfall = require('async/waterfall')
20+
const series = require('async/series')
21+
const defaults = require('lodash.defaultsdeep')
1922

2023
const config = require('./config')
2124
const boot = require('./boot')
@@ -109,20 +112,6 @@ class IPFS extends EventEmitter {
109112
this.dns = components.dns(this)
110113
this.key = components.key(this)
111114
this.stats = components.stats(this)
112-
113-
if (this._options.EXPERIMENTAL.pubsub) {
114-
this.log('EXPERIMENTAL pubsub is enabled')
115-
}
116-
if (this._options.EXPERIMENTAL.sharding) {
117-
this.log('EXPERIMENTAL sharding is enabled')
118-
}
119-
if (this._options.EXPERIMENTAL.dht) {
120-
this.log('EXPERIMENTAL Kademlia DHT is enabled')
121-
}
122-
if (this._options.EXPERIMENTAL.relay) {
123-
this.log('EXPERIMENTAL Relay is enabled')
124-
}
125-
126115
this.state = require('./state')(this)
127116

128117
// ipfs.ls
@@ -136,7 +125,49 @@ class IPFS extends EventEmitter {
136125
isIPFS: isIPFS
137126
}
138127

139-
boot(this)
128+
series([
129+
(cb) => {
130+
waterfall([
131+
(done) => this._repo.config.get((error, config) => {
132+
if (error) {
133+
this.log('Could not load config', error)
134+
}
135+
136+
done(null, config || {})
137+
}),
138+
(config, done) => {
139+
this._options = defaults({}, config, this._options)
140+
141+
done()
142+
}
143+
], cb)
144+
},
145+
(cb) => {
146+
if (this._options.EXPERIMENTAL.pubsub) {
147+
this.log('EXPERIMENTAL pubsub is enabled')
148+
}
149+
150+
if (this._options.EXPERIMENTAL.sharding) {
151+
this.log('EXPERIMENTAL sharding is enabled')
152+
}
153+
154+
if (this._options.EXPERIMENTAL.dht) {
155+
this.log('EXPERIMENTAL Kademlia DHT is enabled')
156+
}
157+
158+
if (this._options.EXPERIMENTAL.relay) {
159+
this.log('EXPERIMENTAL Relay is enabled')
160+
}
161+
162+
cb()
163+
}
164+
], (error) => {
165+
if (error) {
166+
return this.emit('error', error)
167+
}
168+
169+
boot(this)
170+
})
140171
}
141172
}
142173

test/core/options.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
9+
const IPFS = require('../../src/core')
10+
const parallel = require('async/parallel')
11+
12+
// This gets replaced by `create-repo-browser.js` in the browser
13+
const createTempRepo = require('../utils/create-repo-nodejs.js')
14+
15+
describe.only('options', () => {
16+
let repos = []
17+
let repo
18+
19+
beforeEach(() => {
20+
repo = createTempRepo()
21+
repos.push(repo)
22+
})
23+
24+
afterEach((done) => {
25+
parallel(
26+
repos.map(repo => (cb) => repo.teardown(cb)),
27+
done
28+
)
29+
})
30+
31+
it('should merge options with repo config', (done) => {
32+
let ipfs = new IPFS({
33+
repo: repo,
34+
init: true,
35+
start: false
36+
})
37+
38+
expect(ipfs._options.EXPERIMENTAL).to.deep.equal({})
39+
40+
ipfs.once('ready', () => {
41+
// no experimental options have been set
42+
expect(ipfs._options.EXPERIMENTAL).to.deep.equal({})
43+
44+
// set an experimental option
45+
repo.config.set('EXPERIMENTAL.pubsub', true, (error) => {
46+
if (error) {
47+
return done(error)
48+
}
49+
50+
ipfs = new IPFS({
51+
repo: repo,
52+
init: true,
53+
start: false,
54+
EXPERIMENTAL: {
55+
sharding: true
56+
}
57+
})
58+
59+
// should only have the experimental option we passed to the constructor
60+
expect(ipfs._options.EXPERIMENTAL).to.deep.equal({sharding: true})
61+
62+
ipfs.once('ready', () => {
63+
// should have read experimental options from repo config and merged with constructor args
64+
expect(ipfs._options.EXPERIMENTAL).to.deep.equal({
65+
sharding: true,
66+
pubsub: true
67+
})
68+
69+
done()
70+
})
71+
})
72+
})
73+
})
74+
})

0 commit comments

Comments
 (0)