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

Commit 1b17688

Browse files
feat(pr): addressing PR comments
1 parent b5c7628 commit 1b17688

18 files changed

+70
-49
lines changed

src/http/index.js

+26-32
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ function hapiInfoToMultiaddr (info) {
2929
return toMultiaddr(uri)
3030
}
3131

32-
async function serverCreator (serverAddrsArr, createServerFunc, hapiInfoToMultiaddr, ipfs) {
33-
if (!serverAddrsArr.length) {
34-
debug(Error('There are no addresses'))
32+
async function serverCreator (serverAddrs, createServer, hapiInfoToMultiaddr, ipfs) {
33+
if (!serverAddrs.length) {
34+
return []
3535
}
3636
// just in case the address is just string
37-
let serversAddrs = [].concat(serverAddrsArr)
38-
const processServer = async (serverInstance, createServerFunc, hapiInfoToMultiaddr, ipfs) => {
39-
let addr = serverInstance.split('/')
40-
let _Server = await createServerFunc(addr[2], addr[4], ipfs)
41-
await _Server.start()
42-
_Server.info.ma = hapiInfoToMultiaddr(_Server.info)
43-
return _Server
37+
serverAddrs = Array.isArray(serverAddrs) ? serverAddrs : [serverAddrs]
38+
const processServer = async address => {
39+
const addrParts = address.split('/')
40+
const server = await createServer(addrParts[2], addrParts[4], ipfs)
41+
await server.start()
42+
server.info.ma = hapiInfoToMultiaddr(server.info)
43+
return server
4444
}
45-
return Promise.all(
46-
serversAddrs.map(server => processServer(server, createServerFunc, hapiInfoToMultiaddr, ipfs))
47-
).catch(err => debug(err))
45+
return Promise.all(serverAddrs.map(processServer))
4846
}
4947

5048
class HttpApi {
@@ -109,24 +107,22 @@ class HttpApi {
109107

110108
const apiAddrs = config.Addresses.API
111109

112-
this._apiServer = await Promise.resolve(
113-
serverCreator.apply(this, [apiAddrs, this._createApiServer, hapiInfoToMultiaddr, ipfs])
114-
)
110+
this._apiServers = await serverCreator(apiAddrs, this._createApiServer, hapiInfoToMultiaddr, ipfs)
111+
115112
// for the CLI to know the where abouts of the API
116-
await promisify(ipfs._repo.apiAddr.set)(this._apiServer[0].info.ma)
113+
await promisify(ipfs._repo.apiAddr.set)(this._apiServers[0].info.ma)
117114

118-
const gatewayAddr = config.Addresses.Gateway
115+
const gatewayAddrs = config.Addresses.Gateway
119116

120-
this._gatewayServer = await Promise.resolve(
121-
serverCreator.apply(this, [gatewayAddr, this._createGatewayServer, hapiInfoToMultiaddr, ipfs])
122-
)
123-
this._apiServer.forEach(apiServer => {
117+
this._gatewayServers = await serverCreator(gatewayAddrs, this._createGatewayServer, hapiInfoToMultiaddr, ipfs)
118+
119+
this._apiServers.forEach(apiServer => {
124120
ipfs._print('API listening on %s', apiServer.info.ma)
125121
})
126-
this._gatewayServer.forEach(gatewayServer => {
122+
this._gatewayServers.forEach(gatewayServer => {
127123
ipfs._print('Gateway (read only) listening on %s', gatewayServer.info.ma)
128124
})
129-
this._apiServer.forEach(apiServer => {
125+
this._apiServers.forEach(apiServer => {
130126
ipfs._print('Web UI available at %s', toUri(apiServer.info.ma) + '/webui')
131127
})
132128
this._log('started')
@@ -198,20 +194,18 @@ class HttpApi {
198194
}
199195

200196
get apiAddr () {
201-
if (!this._apiServer) throw new Error('API address unavailable - server is not started')
202-
return multiaddr('/ip4/127.0.0.1/tcp/' + this._apiServer[0].info.port)
197+
if (!this._apiServers) throw new Error('API address unavailable - server is not started')
198+
return multiaddr('/ip4/127.0.0.1/tcp/' + this._apiServers[0].info.port)
203199
}
204200

205201
async stop () {
206-
function stopServer (serverArr) {
207-
for (let i = 0; i < serverArr.length; i++) {
208-
serverArr[i].stop()
209-
}
202+
function stopServers (servers) {
203+
return Promise.all(servers.map(server => server.stop()))
210204
}
211205
this._log('stopping')
212206
await Promise.all([
213-
this._apiServer && stopServer(this._apiServer),
214-
this._gatewayServer && stopServer(this._gatewayServer),
207+
stopServers(this._apiServers),
208+
stopServers(this._gatewayServers),
215209
this._ipfs && this._ipfs.stop()
216210
])
217211
this._log('stopped')

test/cli/daemon.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const checkLock = (repo, cb) => {
2626
cb()
2727
}
2828

29-
function testSignal (ipfs, sig) {
29+
function testSignal (ipfs, sig, config) {
3030
return ipfs('init').then(() => {
3131
return ipfs('config', 'Addresses', JSON.stringify({
3232
API: '/ip4/127.0.0.1/tcp/0',
@@ -91,6 +91,33 @@ describe('daemon', () => {
9191
}).catch(err => done(err))
9292
})
9393

94+
skipOnWindows('should handle API Array and Gateway Array', function (done) {
95+
this.timeout(100 * 1000)
96+
// These tests are flaky, but retrying 3 times seems to make it work 99% of the time
97+
this.retries(3)
98+
99+
ipfs('init').then(() => {
100+
return ipfs('config', 'Addresses', JSON.stringify({
101+
Swarm: ['/ip4/0.0.0.0/tcp/4002', '/ip4/127.0.0.1/tcp/4003/ws'],
102+
API: ['/ip4/127.0.0.1/tcp/5002', '/ip6/::1/tcp/5002'],
103+
Gateway: ['/ip4/127.0.0.1/tcp/9090', '/ip6/::1/tcp/9090']
104+
}), '--json')
105+
}).then(() => {
106+
const res = ipfs('daemon')
107+
const timeout = setTimeout(() => {
108+
done(new Error('Daemon did not get ready in time'))
109+
}, 1000 * 120)
110+
res.stdout.on('data', (data) => {
111+
const line = data.toString()
112+
if (line.includes('Daemon is ready')) {
113+
clearTimeout(timeout)
114+
res.kill()
115+
done()
116+
}
117+
})
118+
}).catch(err => done(err))
119+
})
120+
94121
skipOnWindows('should handle SIGINT gracefully', function (done) {
95122
this.timeout(100 * 1000)
96123

test/gateway/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('HTTP Gateway', function () {
6060

6161
await http.api.start()
6262

63-
gateway = http.api._gatewayServer[0]
63+
gateway = http.api._gatewayServers[0]
6464

6565
// QmbQD7EMEL1zeebwBsWEfA3ndgSS6F7S6iTuwuqasPgVRi
6666
await http.api._ipfs.add([

test/http-api/inject/bitswap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (http) => {
1212
let api
1313

1414
before(() => {
15-
api = http.api._apiServer[0]
15+
api = http.api._apiServers[0]
1616
})
1717

1818
before(async function () {

test/http-api/inject/block.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (http) => {
1313
let api
1414

1515
before(() => {
16-
api = http.api._apiServer[0]
16+
api = http.api._apiServers[0]
1717
})
1818

1919
describe('/block/put', () => {

test/http-api/inject/bootstrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = (http) => {
1111
let api
1212

1313
before(() => {
14-
api = http.api._apiServer[0]
14+
api = http.api._apiServers[0]
1515
return api.inject({
1616
method: 'GET',
1717
url: '/api/v0/bootstrap/add/default'

test/http-api/inject/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = (http) => {
1717

1818
before(() => {
1919
updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8'))
20-
api = http.api._apiServer[0]
20+
api = http.api._apiServers[0]
2121
})
2222

2323
after(() => {

test/http-api/inject/dht.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (http) => {
1212
let api
1313

1414
before(() => {
15-
api = http.api._apiServer[0]
15+
api = http.api._apiServers[0]
1616
})
1717

1818
describe('/findpeer', () => {

test/http-api/inject/dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = (http) => {
88
let api
99

1010
before(() => {
11-
api = http.api._apiServer[0]
11+
api = http.api._apiServers[0]
1212
})
1313

1414
it('resolve ipfs.io dns', async () => {

test/http-api/inject/files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (http) => {
1313
let api
1414

1515
before(() => {
16-
api = http.api._apiServer[0]
16+
api = http.api._apiServers[0]
1717
})
1818

1919
describe('/add', () => {

test/http-api/inject/id.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = (http) => {
88
let api
99

1010
before(() => {
11-
api = http.api._apiServer[0]
11+
api = http.api._apiServers[0]
1212
})
1313

1414
it('get the id', async () => {

test/http-api/inject/name.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = (http) => {
1616
let api
1717

1818
before(() => {
19-
api = http.api._apiServer[0]
19+
api = http.api._apiServers[0]
2020
})
2121

2222
it('should publish a record', async function () {

test/http-api/inject/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = (http) => {
1717
let api
1818

1919
before('api', () => {
20-
api = http.api._apiServer[0]
20+
api = http.api._apiServers[0]
2121
})
2222

2323
describe('/new', () => {

test/http-api/inject/pin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = (http) => {
3737
let api
3838

3939
before(() => {
40-
api = http.api._apiServer[0]
40+
api = http.api._apiServers[0]
4141
})
4242

4343
describe('rm', () => {

test/http-api/inject/ping.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (http) => {
1313
let api
1414

1515
before(() => {
16-
api = http.api._apiServer[0]
16+
api = http.api._apiServers[0]
1717
})
1818

1919
it('returns 400 if both n and count are provided', async () => {

test/http-api/inject/pubsub.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = (http) => {
1616
const topicNotSubscribed = 'somethingRandom'
1717

1818
before(() => {
19-
api = http.api._apiServer[0]
19+
api = http.api._apiServers[0]
2020
})
2121

2222
describe('/sub', () => {

test/http-api/inject/resolve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (http) => {
1212
let api
1313

1414
before(() => {
15-
api = http.api._apiServer[0]
15+
api = http.api._apiServers[0]
1616
})
1717

1818
it('should resolve a path and return a base2 encoded CID', async () => {

test/http-api/inject/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = (http) => {
99
let api
1010

1111
before(() => {
12-
api = http.api._apiServer[0]
12+
api = http.api._apiServers[0]
1313
})
1414

1515
it('get the version', async () => {

0 commit comments

Comments
 (0)