Skip to content

Commit 1bed9f0

Browse files
authored
feat: expose gRPC addr on daemon (#561)
Adds a `grpcAddr` property to IPFS daemon instances that contains the multiaddr of the gPRC websocket server exposed if available.
1 parent e8853dd commit 1bed9f0

File tree

6 files changed

+148
-12
lines changed

6 files changed

+148
-12
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"peerDependencies": {
7777
"go-ipfs": "*",
7878
"ipfs": "*",
79+
"ipfs-client": "*",
7980
"ipfs-http-client": "*"
8081
},
8182
"repository": {

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = ({ type }) => {
2929
Addresses: {
3030
Swarm: swarm,
3131
API: '/ip4/127.0.0.1/tcp/0',
32-
Gateway: '/ip4/127.0.0.1/tcp/0'
32+
Gateway: '/ip4/127.0.0.1/tcp/0',
33+
RPC: '/ip4/127.0.0.1/tcp/0'
3334
}
3435
}
3536
}

src/endpoint/routes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module.exports = (server, createFactory) => {
7575
id: id,
7676
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '',
7777
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '',
78+
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : '',
7879
initialized: nodes[id].initialized,
7980
started: nodes[id].started,
8081
disposable: nodes[id].disposable,
@@ -125,7 +126,8 @@ module.exports = (server, createFactory) => {
125126

126127
return {
127128
apiAddr: nodes[id].apiAddr ? nodes[id].apiAddr.toString() : '',
128-
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : ''
129+
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : '',
130+
grpcAddr: nodes[id].grpcAddr ? nodes[id].grpcAddr.toString() : ''
129131
}
130132
} catch (err) {
131133
badRequest(err)

src/ipfsd-client.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Client {
3737

3838
this._setApi(remoteState.apiAddr)
3939
this._setGateway(remoteState.gatewayAddr)
40+
this._setGrpc(remoteState.grpcAddr)
41+
this._createApi()
4042
}
4143

4244
/**
@@ -46,9 +48,6 @@ class Client {
4648
_setApi (addr) {
4749
if (addr) {
4850
this.apiAddr = multiaddr(addr)
49-
this.api = this.opts.ipfsHttpModule(addr)
50-
this.api.apiHost = this.apiAddr.nodeAddress().address
51-
this.api.apiPort = this.apiAddr.nodeAddress().port
5251
}
5352
}
5453

@@ -59,8 +58,47 @@ class Client {
5958
_setGateway (addr) {
6059
if (addr) {
6160
this.gatewayAddr = multiaddr(addr)
62-
this.api.gatewayHost = this.gatewayAddr.nodeAddress().address
63-
this.api.gatewayPort = this.gatewayAddr.nodeAddress().port
61+
}
62+
}
63+
64+
/**
65+
* @private
66+
* @param {string} addr
67+
*/
68+
_setGrpc (addr) {
69+
if (addr) {
70+
this.grpcAddr = multiaddr(addr)
71+
}
72+
}
73+
74+
/**
75+
* @private
76+
*/
77+
_createApi () {
78+
if (this.opts.ipfsClientModule && this.grpcAddr && this.apiAddr) {
79+
this.api = this.opts.ipfsClientModule({
80+
grpc: this.grpcAddr,
81+
http: this.apiAddr
82+
})
83+
} else if (this.apiAddr) {
84+
this.api = this.opts.ipfsHttpModule(this.apiAddr)
85+
}
86+
87+
if (this.api) {
88+
if (this.apiAddr) {
89+
this.api.apiHost = this.apiAddr.nodeAddress().address
90+
this.api.apiPort = this.apiAddr.nodeAddress().port
91+
}
92+
93+
if (this.gatewayAddr) {
94+
this.api.gatewayHost = this.gatewayAddr.nodeAddress().address
95+
this.api.gatewayPort = this.gatewayAddr.nodeAddress().port
96+
}
97+
98+
if (this.grpcAddr) {
99+
this.api.grpcHost = this.grpcAddr.nodeAddress().address
100+
this.api.grpcPort = this.grpcAddr.nodeAddress().port
101+
}
64102
}
65103
}
66104

@@ -133,6 +171,8 @@ class Client {
133171

134172
this._setApi(res.apiAddr)
135173
this._setGateway(res.gatewayAddr)
174+
this._setGrpc(res.grpcAddr)
175+
this._createApi()
136176

137177
this.started = true
138178
}

src/ipfsd-daemon.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Daemon {
4848
this.started = false
4949
this.clean = true
5050
this.apiAddr = null
51+
this.grpcAddr = null
5152
this.gatewayAddr = null
5253
this.api = null
5354
}
@@ -58,9 +59,14 @@ class Daemon {
5859
*/
5960
_setApi (addr) {
6061
this.apiAddr = multiaddr(addr)
61-
this.api = this.opts.ipfsHttpModule(addr)
62-
this.api.apiHost = this.apiAddr.nodeAddress().address
63-
this.api.apiPort = this.apiAddr.nodeAddress().port
62+
}
63+
64+
/**
65+
* @private
66+
* @param {string} addr
67+
*/
68+
_setGrpc (addr) {
69+
this.grpcAddr = multiaddr(addr)
6470
}
6571

6672
/**
@@ -69,8 +75,36 @@ class Daemon {
6975
*/
7076
_setGateway (addr) {
7177
this.gatewayAddr = multiaddr(addr)
72-
this.api.gatewayHost = this.gatewayAddr.nodeAddress().address
73-
this.api.gatewayPort = this.gatewayAddr.nodeAddress().port
78+
}
79+
80+
_createApi () {
81+
if (this.opts.ipfsClientModule && this.grpcAddr) {
82+
this.api = this.opts.ipfsClientModule({
83+
grpc: this.grpcAddr,
84+
http: this.apiAddr
85+
})
86+
} else if (this.apiAddr) {
87+
this.api = this.opts.ipfsHttpModule(this.apiAddr)
88+
}
89+
90+
if (!this.api) {
91+
throw new Error(`Could not create API from http '${this.apiAddr}' and/or gRPC '${this.grpcAddr}'`)
92+
}
93+
94+
if (this.apiAddr) {
95+
this.api.apiHost = this.apiAddr.nodeAddress().address
96+
this.api.apiPort = this.apiAddr.nodeAddress().port
97+
}
98+
99+
if (this.gatewayAddr) {
100+
this.api.gatewayHost = this.gatewayAddr.nodeAddress().address
101+
this.api.gatewayPort = this.gatewayAddr.nodeAddress().port
102+
}
103+
104+
if (this.grpcAddr) {
105+
this.api.grpcHost = this.grpcAddr.nodeAddress().address
106+
this.api.grpcPort = this.grpcAddr.nodeAddress().port
107+
}
74108
}
75109

76110
/**
@@ -150,6 +184,7 @@ class Daemon {
150184

151185
if (api) {
152186
this._setApi(api)
187+
this._createApi()
153188
} else if (!this.exec) {
154189
throw new Error('No executable specified')
155190
} else {
@@ -168,6 +203,7 @@ class Daemon {
168203
output += data.toString()
169204
const apiMatch = output.trim().match(/API .*listening on:? (.*)/)
170205
const gwMatch = output.trim().match(/Gateway .*listening on:? (.*)/)
206+
const grpcMatch = output.trim().match(/gRPC .*listening on:? (.*)/)
171207

172208
if (apiMatch && apiMatch.length > 0) {
173209
this._setApi(apiMatch[1])
@@ -177,8 +213,13 @@ class Daemon {
177213
this._setGateway(gwMatch[1])
178214
}
179215

216+
if (grpcMatch && grpcMatch.length > 0) {
217+
this._setGrpc(grpcMatch[1])
218+
}
219+
180220
if (output.match(/(?:daemon is running|Daemon is ready)/)) {
181221
// we're good
222+
this._createApi()
182223
this.started = true
183224
this.subprocess.stdout.off('data', readyHandler)
184225
resolve(this.api)

test/create.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,57 @@ describe('`createController` should return the correct class', () => {
5656

5757
expect(f).to.be.instanceOf(Client)
5858
})
59+
60+
it.skip('should use ipfs-client if passed', async () => {
61+
let clientCreated = false
62+
let httpCreated = false
63+
64+
await createController({
65+
type: 'js',
66+
disposable: false,
67+
ipfsModule: require('ipfs'),
68+
ipfsClientModule: (opts) => {
69+
clientCreated = true
70+
71+
return require('ipfs-client')(opts)
72+
},
73+
ipfsHttpModule: (opts) => {
74+
httpCreated = true
75+
76+
return require('ipfs-http-client')(opts)
77+
},
78+
ipfsBin: pathJoin(__dirname, '../node_modules/ipfs/src/cli/bin.js')
79+
})
80+
81+
expect(clientCreated).to.be.true()
82+
expect(httpCreated).to.be.false()
83+
})
84+
85+
it.skip('should use ipfs-client for remote if passed', async () => {
86+
let clientCreated = false
87+
let httpCreated = false
88+
89+
const f = await createController({
90+
remote: true,
91+
disposable: false,
92+
ipfsModule: require('ipfs'),
93+
ipfsClientModule: (opts) => {
94+
clientCreated = true
95+
96+
return require('ipfs-client')(opts)
97+
},
98+
ipfsHttpModule: (opts) => {
99+
httpCreated = true
100+
101+
return require('ipfs-http-client')(opts)
102+
},
103+
ipfsBin: pathJoin(__dirname, '../node_modules/ipfs/src/cli/bin.js')
104+
})
105+
106+
expect(f).to.be.instanceOf(Client)
107+
expect(clientCreated).to.be.true()
108+
expect(httpCreated).to.be.false()
109+
})
59110
})
60111

61112
const defaultOps = {

0 commit comments

Comments
 (0)