@@ -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 ( / A P I .* l i s t e n i n g o n : ? ( .* ) / )
170205 const gwMatch = output . trim ( ) . match ( / G a t e w a y .* l i s t e n i n g o n : ? ( .* ) / )
206+ const grpcMatch = output . trim ( ) . match ( / g R P C .* l i s t e n i n g o n : ? ( .* ) / )
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 ( / (?: d a e m o n i s r u n n i n g | D a e m o n i s r e a d y ) / ) ) {
181221 // we're good
222+ this . _createApi ( )
182223 this . started = true
183224 this . subprocess . stdout . off ( 'data' , readyHandler )
184225 resolve ( this . api )
0 commit comments