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

Commit 69fa802

Browse files
authored
feat(breaking change): experimental config options (#749)
* feat(breaking change): experimental config options
1 parent c205736 commit 69fa802

File tree

18 files changed

+173
-85
lines changed

18 files changed

+173
-85
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/
200200
const repo = <IPFS Repo instance or repo path>
201201

202202
// Create the IPFS node instance
203-
const node = new IPFS(repo)
203+
const node = new IPFS({
204+
repo: repo,
205+
EXPERIMENTAL: {
206+
pubsub: false
207+
}
208+
})
204209

205210
// We need to init our repo, in this case the repo was empty
206211
// We are picking 2048 bits for the RSA key that will be our PeerId

examples/basics/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ const IPFS = require('../../src/core')
1212
/*
1313
* Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs)
1414
*/
15-
const node = new IPFS(path.join(os.tmpDir() + '/' + new Date().toString()))
15+
const node = new IPFS({
16+
repo: path.join(os.tmpDir() + '/' + new Date().toString()),
17+
EXPERIMENTAL: {
18+
pubsub: false
19+
}
20+
})
1621

1722
const fileToAdd = {
1823
path: 'hello.txt',

examples/bundle-browserify/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ var IPFS = require('../../../src/core') // replace this by line below
77
// for simplicity, we create a new repo everytime the node
88
// is created, because you can't init already existing repos
99
const repoPath = String(Math.random())
10-
const node = new IPFS(repoPath)
10+
const node = new IPFS({
11+
repo: repoPath,
12+
EXPERIMENTAL: {
13+
pubsub: false
14+
}
15+
})
1116
const concat = require('concat-stream')
1217

1318
node.init({ emptyRepo: true, bits: 2048 }, function (err) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ 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-
node = new IPFS(repoPath)
32+
node = new IPFS({
33+
repo: repoPath,
34+
EXPERIMENTAL: {
35+
pubsub: false
36+
}
37+
})
3338

3439
node.init({ emptyRepo: true, bits: 2048 }, function (err) {
3540
if (err) {

src/cli/commands/init.js

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

3-
const IpfsRepo = require('ipfs-repo')
4-
const Ipfs = require('../../core')
3+
const Repo = require('ipfs-repo')
4+
const IPFS = require('../../core')
55
const Store = require('fs-pull-blob-store')
66
const utils = require('../utils')
77

@@ -35,18 +35,21 @@ module.exports = {
3535
const log = utils.createLogger(true)
3636
log(`initializing ipfs node at ${path}`)
3737

38-
const repo = new IpfsRepo(path, {
38+
const repo = new Repo(path, {
3939
stores: Store
4040
})
4141

42-
const ipfs = new Ipfs(repo)
42+
const ipfs = new IPFS({
43+
repo: repo,
44+
EXPERIMENTAL: {}
45+
})
4346

4447
ipfs.init({
4548
bits: argv.bits,
4649
force: argv.force,
4750
emptyRepo: argv.emptyRepo,
4851
log
49-
}, function (err) {
52+
}, (err) => {
5053
if (err) {
5154
console.error(err.toString())
5255
process.exit(1)

src/cli/utils.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ function getAPICtl () {
3636

3737
exports.getIPFS = (callback) => {
3838
if (!isDaemonOn()) {
39-
const ipfs = new IPFS(exports.getRepoPath())
40-
ipfs.load(() => {
41-
callback(null, ipfs)
39+
const ipfs = new IPFS({
40+
repo: exports.getRepoPath(),
41+
EXPERIMENTAL: {
42+
pubsub: true
43+
}
4244
})
45+
ipfs.load(() => callback(null, ipfs))
4346
return
4447
}
4548

src/core/components/go-offline.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ module.exports = (self) => {
44
return (callback) => {
55
self._blockService.goOffline()
66
self._bitswap.stop()
7-
self._pubsub.stop((err) => {
7+
8+
if (self._configOpts.EXPERIMENTAL.pubsub) {
9+
self._pubsub.stop(next)
10+
} else {
11+
next()
12+
}
13+
14+
function next (err) {
815
if (err) {
916
return callback(err)
1017
}
1118
self.libp2p.stop(callback)
12-
})
19+
}
1320
}
1421
}

src/core/components/go-online.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ module.exports = (self) => {
3131
self._blockService.goOnline(self._bitswap)
3232
cb()
3333
},
34-
(cb) => self._pubsub.start(cb) // ,
35-
// For all of the protocols to handshake with each other
36-
// (cb) => setTimeout(cb, 1000) // Still not decided if we want this
34+
(cb) => {
35+
if (self._configOpts.EXPERIMENTAL.pubsub) {
36+
self._pubsub.start(cb)
37+
} else {
38+
cb()
39+
}
40+
}
3741
], callback)
3842
})
3943
}

src/core/components/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
exports.goOnline = require('./go-online')
4+
exports.goOffline = require('./go-offline')
5+
exports.isOnline = require('./is-online')
6+
exports.load = require('./load')
7+
exports.version = require('./version')
8+
exports.id = require('./id')
9+
exports.repo = require('./repo')
10+
exports.init = require('./init')
11+
exports.bootstrap = require('./bootstrap')
12+
exports.config = require('./config')
13+
exports.block = require('./block')
14+
exports.object = require('./object')
15+
exports.libp2p = require('./libp2p')
16+
exports.swarm = require('./swarm')
17+
exports.ping = require('./ping')
18+
exports.files = require('./files')
19+
exports.bitswap = require('./bitswap')
20+
exports.pubsub = require('./pubsub')

src/core/index.js

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,66 @@
33
const BlockService = require('ipfs-block-service')
44
const IPLDResolver = require('ipld-resolver')
55
const PeerBook = require('peer-book')
6+
const debug = require('debug')
67

78
const defaultRepo = require('./default-repo')
89

9-
const goOnline = require('./components/go-online')
10-
const goOffline = require('./components/go-offline')
11-
const isOnline = require('./components/is-online')
12-
const load = require('./components/load')
13-
const version = require('./components/version')
14-
const id = require('./components/id')
15-
const repo = require('./components/repo')
16-
const init = require('./components/init')
17-
const bootstrap = require('./components/bootstrap')
18-
const config = require('./components/config')
19-
const block = require('./components/block')
20-
const object = require('./components/object')
21-
const libp2p = require('./components/libp2p')
22-
const swarm = require('./components/swarm')
23-
const ping = require('./components/ping')
24-
const files = require('./components/files')
25-
const bitswap = require('./components/bitswap')
26-
const pubsub = require('./components/pubsub')
10+
const components = require('./components')
2711

28-
exports = module.exports = IPFS
12+
class IPFS {
13+
constructor (configOpts) {
14+
let repoInstance
15+
if (typeof configOpts.repo === 'string' || configOpts.repo === undefined) {
16+
repoInstance = defaultRepo(configOpts.repo)
17+
} else {
18+
repoInstance = configOpts.repo
19+
}
20+
delete configOpts.repo
2921

30-
function IPFS (repoInstance) {
31-
if (!(this instanceof IPFS)) {
32-
throw new Error('Must be instantiated with new')
33-
}
34-
35-
if (typeof repoInstance === 'string' ||
36-
repoInstance === undefined) {
37-
repoInstance = defaultRepo(repoInstance)
38-
}
22+
configOpts.EXPERIMENTAL = configOpts.EXPERIMENTAL || {}
3923

40-
// IPFS Core Internals
41-
this._repo = repoInstance
42-
this._peerInfoBook = new PeerBook()
43-
this._peerInfo = null
44-
this._libp2pNode = null
45-
this._bitswap = null
46-
this._blockService = new BlockService(this._repo)
47-
this._ipldResolver = new IPLDResolver(this._blockService)
48-
this._pubsub = null
24+
// IPFS utils
25+
this.types = {}
26+
this.log = debug('jsipfs')
27+
this.log.err = debug('jsipfs:err')
4928

50-
// IPFS Core exposed components
29+
// IPFS Core Internals
30+
this._configOpts = configOpts
31+
this._repo = repoInstance
32+
this._peerInfoBook = new PeerBook()
33+
this._peerInfo = undefined
34+
this._libp2pNode = undefined
35+
this._bitswap = undefined
36+
this._blockService = new BlockService(this._repo)
37+
this._ipldResolver = new IPLDResolver(this._blockService)
38+
this._pubsub = undefined
5139

52-
// for booting up a node
53-
this.goOnline = goOnline(this)
54-
this.goOffline = goOffline(this)
55-
this.isOnline = isOnline(this)
56-
this.load = load(this)
57-
this.init = init(this)
40+
// IPFS Core exposed components
41+
// - for booting up a node
42+
this.goOnline = components.goOnline(this)
43+
this.goOffline = components.goOffline(this)
44+
this.isOnline = components.isOnline(this)
45+
this.load = components.load(this)
46+
this.init = components.init(this)
47+
// - interface-ipfs-core defined API
48+
this.version = components.version(this)
49+
this.id = components.id(this)
50+
this.repo = components.repo(this)
51+
this.bootstrap = components.bootstrap(this)
52+
this.config = components.config(this)
53+
this.block = components.block(this)
54+
this.object = components.object(this)
55+
this.libp2p = components.libp2p(this)
56+
this.swarm = components.swarm(this)
57+
this.files = components.files(this)
58+
this.bitswap = components.bitswap(this)
59+
this.ping = components.ping(this)
60+
this.pubsub = components.pubsub(this)
5861

59-
// interface-ipfs-core defined API
60-
this.version = version(this)
61-
this.id = id(this)
62-
this.repo = repo(this)
63-
this.bootstrap = bootstrap(this)
64-
this.config = config(this)
65-
this.block = block(this)
66-
this.object = object(this)
67-
this.libp2p = libp2p(this)
68-
this.swarm = swarm(this)
69-
this.files = files(this)
70-
this.bitswap = bitswap(this)
71-
this.ping = ping(this)
72-
this.pubsub = pubsub(this)
62+
if (configOpts.EXPERIMENTAL.pubsub) {
63+
this.log('EXPERIMENTAL pubsub is enabled')
64+
}
65+
}
7366
}
67+
68+
module.exports = IPFS

src/http-api/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ exports = module.exports = function HttpApi (repo) {
2525
repo = new IPFSRepo(repo, {stores: Store})
2626
}
2727

28-
this.ipfs = new IPFS(repo)
28+
this.ipfs = new IPFS({
29+
repo: repo,
30+
EXPERIMENTAL: {
31+
pubsub: true
32+
}
33+
})
34+
2935
const repoPath = this.ipfs.repo.path()
3036

3137
try {

test/cli/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ describe('cli', () => {
2020
})
2121
})
2222

23-
after(() => {
24-
clean(repoTests)
25-
})
23+
after(() => clean(repoTests))
2624

2725
describe('--all', () => {
2826
const tests = fs.readdirSync(__dirname)

test/core/bitswap.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ describe('bitswap', () => {
3232

3333
beforeEach((done) => {
3434
const repo = createTempRepo()
35-
inProcNode = new IPFS(repo)
35+
inProcNode = new IPFS({
36+
repo: repo,
37+
EXPERIMENTAL: {
38+
pubsub: true
39+
}
40+
})
3641
series([
3742
(cb) => inProcNode.init({ bits: 2048 }, cb),
3843
(cb) => {

test/core/bootstrap.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ describe('bootstrap', () => {
1818

1919
before((done) => {
2020
const repo = createTempRepo()
21-
ipfs = new IPFS(repo)
21+
ipfs = new IPFS({
22+
repo: repo,
23+
EXPERIMENTAL: {
24+
pubsub: true
25+
}
26+
})
2227
series([
2328
(cb) => ipfs.init({ bits: 1024 }, cb),
2429
(cb) => ipfs.load(cb)

test/core/init.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ describe('init', () => {
1818

1919
beforeEach(() => {
2020
repo = createTempRepo()
21-
ipfs = new IPFS(repo)
21+
ipfs = new IPFS({
22+
repo: repo,
23+
EXPERIMENTAL: {
24+
pubsub: true
25+
}
26+
})
2227
})
2328

2429
afterEach((done) => repo.teardown(done))

test/utils/factory-core/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ function Factory () {
4343
const repo = createTempRepo(repoPath)
4444

4545
// create the IPFS node
46-
const ipfs = new IPFS(repo)
46+
const ipfs = new IPFS({
47+
repo: repo,
48+
EXPERIMENTAL: {
49+
pubsub: true
50+
}
51+
})
52+
4753
ipfs.init({ emptyRepo: true, bits: 1024 }, (err) => {
4854
if (err) {
4955
return callback(err)

test/utils/factory-http/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ function Factory () {
5151
}
5252

5353
// create the IPFS node
54-
const ipfs = new IPFS(repo)
54+
const ipfs = new IPFS({
55+
repo: repo,
56+
EXPERIMENTAL: {
57+
pubsub: true
58+
}
59+
})
60+
5561
ipfs.init({
5662
emptyRepo: true,
5763
bits: 1024

0 commit comments

Comments
 (0)