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

Commit f1eb595

Browse files
committed
fix: options to the HTTP API
1 parent 20ff4ab commit f1eb595

File tree

6 files changed

+126
-5
lines changed

6 files changed

+126
-5
lines changed

src/cli/commands/daemon.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@ module.exports = {
1010

1111
describe: 'Start a long-running daemon process',
1212

13-
handler () {
13+
builder: {
14+
'enable-sharding-experiment': {
15+
type: 'boolean',
16+
defaultt: false
17+
},
18+
'enable-pubsub-experiment': {
19+
type: 'booleam',
20+
default: false
21+
}
22+
},
23+
24+
handler (argv) {
1425
console.log('Initializing daemon...')
1526

1627
const repoPath = utils.getRepoPath()
17-
httpAPI = new HttpAPI(repoPath)
28+
httpAPI = new HttpAPI(process.env.IPFS_PATH, argv)
1829

1930
httpAPI.start((err) => {
2031
if (err && err.code === 'ENOENT') {

src/cli/commands/files/add.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,27 @@ module.exports = {
5656
alias: 'w',
5757
type: 'boolean',
5858
default: false
59+
},
60+
'enable-sharding-experiment': {
61+
type: 'boolean',
62+
defaultt: false
63+
},
64+
'shard-split-threshold': {
65+
type: 'integer',
66+
default: 1000
5967
}
6068
},
6169

6270
handler (argv) {
6371
const inPath = checkPath(argv.file, argv.recursive)
6472
const index = inPath.lastIndexOf('/') + 1
6573
const options = {
66-
strategy: argv.trickle ? 'trickle' : 'balanced'
74+
strategy: argv.trickle ? 'trickle' : 'balanced',
75+
shardSplitThreshold: argv.enableShardingExperiment ? argv.shardSplitThreshold : Infinity
76+
}
77+
78+
if (argv.enableShardingExperiment && utils.isDaemonOn()) {
79+
throw new Error('Error: Enabling the sharding experiment should be done on the daemon')
6780
}
6881
const ipfs = argv.ipfs
6982

src/core/components/files.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ const Duplex = require('stream').Duplex
1818

1919
module.exports = function files (self) {
2020
const createAddPullStream = (options) => {
21+
const opts = Object.assign({}, {
22+
shardSplitThreshold: self._options.EXPERIMENTAL.sharding ? 1000 : Infinity
23+
}, options)
24+
2125
return pull(
2226
pull.map(normalizeContent),
2327
pull.flatten(),
24-
importer(self._ipldResolver, options),
28+
importer(self._ipldResolver, opts),
2529
pull.asyncMap(prepareFile.bind(null, self))
2630
)
2731
}

src/core/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class IPFS extends EventEmitter {
9797
if (this._options.EXPERIMENTAL.pubsub) {
9898
this.log('EXPERIMENTAL pubsub is enabled')
9999
}
100+
if (this._options.EXPERIMENTAL.sharding) {
101+
this.log('EXPERIMENTAL sharding is enabled')
102+
}
100103
this.state = require('./state')(this)
101104

102105
boot(this)

src/http-api/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function HttpApi (repo, config) {
4040
start: true,
4141
config: config,
4242
EXPERIMENTAL: {
43-
pubsub: true
43+
pubsub: true,
44+
sharding: config && config.enableShardingExperiment
4445
}
4546
})
4647
} catch (err) {

test/core/files-sharding.spec.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'use strict'
2+
3+
const expect = require('chai').expect
4+
const pull = require('pull-stream')
5+
6+
const IPFS = require('../../src/core')
7+
const createTempRepo = require('../utils/create-repo-node.js')
8+
9+
describe('files dir', () => {
10+
11+
const files = []
12+
for(let i = 0; i < 1005; i++) {
13+
files.push({
14+
path: 'test-folder/' + i,
15+
content: new Buffer('some content ' + i)
16+
})
17+
}
18+
19+
describe('without sharding', () => {
20+
let ipfs
21+
22+
before((done) => {
23+
ipfs = new IPFS({
24+
repo: createTempRepo(),
25+
config: {
26+
Bootstrap: []
27+
}
28+
})
29+
ipfs.once('start', done)
30+
})
31+
32+
after((done) => {
33+
ipfs.stop(done)
34+
})
35+
36+
it('should be able to add dir without sharding', (done) => {
37+
pull(
38+
pull.values(files),
39+
ipfs.files.createAddPullStream(),
40+
pull.collect((err, results) => {
41+
const last = results[results.length - 1]
42+
expect(last.path).to.be.eql('test-folder')
43+
expect(last.hash).to.be.eql('QmWWM8ZV6GPhqJ46WtKcUaBPNHN5yQaFsKDSQ1RE73w94Q')
44+
done()
45+
})
46+
)
47+
48+
after((done) => {
49+
ipfs.stop(() => done()) // ignore stop errors
50+
})
51+
52+
}).timeout(4000)
53+
})
54+
55+
describe('with sharding', () => {
56+
let ipfs
57+
58+
before((done) => {
59+
ipfs = new IPFS({
60+
repo: createTempRepo(),
61+
config: {
62+
Bootstrap: []
63+
},
64+
EXPERIMENTAL: {
65+
sharding: true
66+
}
67+
})
68+
ipfs.once('start', done)
69+
})
70+
71+
after((done) => {
72+
ipfs.stop(() => done()) // ignore stop errors
73+
})
74+
75+
it('should be able to add dir with sharding', (done) => {
76+
pull(
77+
pull.values(files),
78+
ipfs.files.createAddPullStream(),
79+
pull.collect((err, results) => {
80+
const last = results[results.length - 1]
81+
expect(last.path).to.be.eql('test-folder')
82+
expect(last.hash).to.be.eql('QmZjYC1kWrLmiRYbEmGSo2PEpMixzT2k2xoCKSBzt8KDcy')
83+
done()
84+
})
85+
)
86+
87+
}).timeout(4000)
88+
})
89+
})

0 commit comments

Comments
 (0)