17
17
// ** All changes to this file may be overwritten. **
18
18
19
19
/* global window */
20
- import * as gax from 'google-gax' ;
21
- import {
20
+ import type * as gax from 'google-gax' ;
21
+ import type {
22
22
Callback ,
23
23
CallOptions ,
24
24
Descriptors ,
@@ -28,7 +28,6 @@ import {
28
28
IamClient ,
29
29
IamProtos ,
30
30
} from 'google-gax' ;
31
-
32
31
import { Transform } from 'stream' ;
33
32
import * as protos from '../../protos/protos' ;
34
33
import jsonProtos = require( '../../protos/protos.json' ) ;
@@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json');
38
37
* This file defines retry strategy and timeouts for all API methods in this library.
39
38
*/
40
39
import * as gapicConfig from './publisher_client_config.json' ;
41
-
42
40
const version = require ( '../../../package.json' ) . version ;
43
41
44
42
/**
@@ -100,8 +98,18 @@ export class PublisherClient {
100
98
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
101
99
* For more information, please check the
102
100
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
101
+ * @param {gax } [gaxInstance]: loaded instance of `google-gax`. Useful if you
102
+ * need to avoid loading the default gRPC version and want to use the fallback
103
+ * HTTP implementation. Load only fallback version and pass it to the constructor:
104
+ * ```
105
+ * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
106
+ * const client = new PublisherClient({fallback: 'rest'}, gax);
107
+ * ```
103
108
*/
104
- constructor ( opts ?: ClientOptions ) {
109
+ constructor (
110
+ opts ?: ClientOptions ,
111
+ gaxInstance ?: typeof gax | typeof gax . fallback
112
+ ) {
105
113
// Ensure that options include all the required fields.
106
114
const staticMembers = this . constructor as typeof PublisherClient ;
107
115
const servicePath =
@@ -121,8 +129,13 @@ export class PublisherClient {
121
129
opts [ 'scopes' ] = staticMembers . scopes ;
122
130
}
123
131
132
+ // Load google-gax module synchronously if needed
133
+ if ( ! gaxInstance ) {
134
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
135
+ }
136
+
124
137
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
125
- this . _gaxModule = opts . fallback ? gax . fallback : gax ;
138
+ this . _gaxModule = opts . fallback ? gaxInstance . fallback : gaxInstance ;
126
139
127
140
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
128
141
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -143,7 +156,7 @@ export class PublisherClient {
143
156
if ( servicePath === staticMembers . servicePath ) {
144
157
this . auth . defaultScopes = staticMembers . scopes ;
145
158
}
146
- this . iamClient = new IamClient ( this . _gaxGrpc , opts ) ;
159
+ this . iamClient = new this . _gaxModule . IamClient ( this . _gaxGrpc , opts ) ;
147
160
148
161
// Determine the client header string.
149
162
const clientHeader = [ `gax/${ this . _gaxModule . version } ` , `gapic/${ version } ` ] ;
@@ -214,7 +227,7 @@ export class PublisherClient {
214
227
'messages' ,
215
228
[ 'topic' ] ,
216
229
'message_ids' ,
217
- gax . createByteLengthFunction (
230
+ this . _gaxModule . GrpcClient . createByteLengthFunction (
218
231
// eslint-disable-next-line @typescript-eslint/no-explicit-any
219
232
protoFilesRoot . lookupType ( 'google.pubsub.v1.PubsubMessage' ) as any
220
233
)
@@ -235,7 +248,7 @@ export class PublisherClient {
235
248
this . innerApiCalls = { } ;
236
249
237
250
// Add a warn function to the client constructor so it can be easily tested.
238
- this . warn = gax . warn ;
251
+ this . warn = this . _gaxModule . warn ;
239
252
}
240
253
241
254
/**
@@ -476,7 +489,7 @@ export class PublisherClient {
476
489
options . otherArgs = options . otherArgs || { } ;
477
490
options . otherArgs . headers = options . otherArgs . headers || { } ;
478
491
options . otherArgs . headers [ 'x-goog-request-params' ] =
479
- gax . routingHeader . fromParams ( {
492
+ this . _gaxModule . routingHeader . fromParams ( {
480
493
name : request . name || '' ,
481
494
} ) ;
482
495
this . initialize ( ) ;
@@ -564,7 +577,7 @@ export class PublisherClient {
564
577
options . otherArgs = options . otherArgs || { } ;
565
578
options . otherArgs . headers = options . otherArgs . headers || { } ;
566
579
options . otherArgs . headers [ 'x-goog-request-params' ] =
567
- gax . routingHeader . fromParams ( {
580
+ this . _gaxModule . routingHeader . fromParams ( {
568
581
'topic.name' : request . topic ! . name || '' ,
569
582
} ) ;
570
583
this . initialize ( ) ;
@@ -649,7 +662,7 @@ export class PublisherClient {
649
662
options . otherArgs = options . otherArgs || { } ;
650
663
options . otherArgs . headers = options . otherArgs . headers || { } ;
651
664
options . otherArgs . headers [ 'x-goog-request-params' ] =
652
- gax . routingHeader . fromParams ( {
665
+ this . _gaxModule . routingHeader . fromParams ( {
653
666
topic : request . topic || '' ,
654
667
} ) ;
655
668
this . initialize ( ) ;
@@ -731,7 +744,7 @@ export class PublisherClient {
731
744
options . otherArgs = options . otherArgs || { } ;
732
745
options . otherArgs . headers = options . otherArgs . headers || { } ;
733
746
options . otherArgs . headers [ 'x-goog-request-params' ] =
734
- gax . routingHeader . fromParams ( {
747
+ this . _gaxModule . routingHeader . fromParams ( {
735
748
topic : request . topic || '' ,
736
749
} ) ;
737
750
this . initialize ( ) ;
@@ -817,7 +830,7 @@ export class PublisherClient {
817
830
options . otherArgs = options . otherArgs || { } ;
818
831
options . otherArgs . headers = options . otherArgs . headers || { } ;
819
832
options . otherArgs . headers [ 'x-goog-request-params' ] =
820
- gax . routingHeader . fromParams ( {
833
+ this . _gaxModule . routingHeader . fromParams ( {
821
834
topic : request . topic || '' ,
822
835
} ) ;
823
836
this . initialize ( ) ;
@@ -902,7 +915,7 @@ export class PublisherClient {
902
915
options . otherArgs = options . otherArgs || { } ;
903
916
options . otherArgs . headers = options . otherArgs . headers || { } ;
904
917
options . otherArgs . headers [ 'x-goog-request-params' ] =
905
- gax . routingHeader . fromParams ( {
918
+ this . _gaxModule . routingHeader . fromParams ( {
906
919
subscription : request . subscription || '' ,
907
920
} ) ;
908
921
this . initialize ( ) ;
@@ -996,7 +1009,7 @@ export class PublisherClient {
996
1009
options . otherArgs = options . otherArgs || { } ;
997
1010
options . otherArgs . headers = options . otherArgs . headers || { } ;
998
1011
options . otherArgs . headers [ 'x-goog-request-params' ] =
999
- gax . routingHeader . fromParams ( {
1012
+ this . _gaxModule . routingHeader . fromParams ( {
1000
1013
project : request . project || '' ,
1001
1014
} ) ;
1002
1015
this . initialize ( ) ;
@@ -1037,7 +1050,7 @@ export class PublisherClient {
1037
1050
options . otherArgs = options . otherArgs || { } ;
1038
1051
options . otherArgs . headers = options . otherArgs . headers || { } ;
1039
1052
options . otherArgs . headers [ 'x-goog-request-params' ] =
1040
- gax . routingHeader . fromParams ( {
1053
+ this . _gaxModule . routingHeader . fromParams ( {
1041
1054
project : request . project || '' ,
1042
1055
} ) ;
1043
1056
const defaultCallSettings = this . _defaults [ 'listTopics' ] ;
@@ -1085,7 +1098,7 @@ export class PublisherClient {
1085
1098
options . otherArgs = options . otherArgs || { } ;
1086
1099
options . otherArgs . headers = options . otherArgs . headers || { } ;
1087
1100
options . otherArgs . headers [ 'x-goog-request-params' ] =
1088
- gax . routingHeader . fromParams ( {
1101
+ this . _gaxModule . routingHeader . fromParams ( {
1089
1102
project : request . project || '' ,
1090
1103
} ) ;
1091
1104
const defaultCallSettings = this . _defaults [ 'listTopics' ] ;
@@ -1192,7 +1205,7 @@ export class PublisherClient {
1192
1205
options . otherArgs = options . otherArgs || { } ;
1193
1206
options . otherArgs . headers = options . otherArgs . headers || { } ;
1194
1207
options . otherArgs . headers [ 'x-goog-request-params' ] =
1195
- gax . routingHeader . fromParams ( {
1208
+ this . _gaxModule . routingHeader . fromParams ( {
1196
1209
topic : request . topic || '' ,
1197
1210
} ) ;
1198
1211
this . initialize ( ) ;
@@ -1237,7 +1250,7 @@ export class PublisherClient {
1237
1250
options . otherArgs = options . otherArgs || { } ;
1238
1251
options . otherArgs . headers = options . otherArgs . headers || { } ;
1239
1252
options . otherArgs . headers [ 'x-goog-request-params' ] =
1240
- gax . routingHeader . fromParams ( {
1253
+ this . _gaxModule . routingHeader . fromParams ( {
1241
1254
topic : request . topic || '' ,
1242
1255
} ) ;
1243
1256
const defaultCallSettings = this . _defaults [ 'listTopicSubscriptions' ] ;
@@ -1285,7 +1298,7 @@ export class PublisherClient {
1285
1298
options . otherArgs = options . otherArgs || { } ;
1286
1299
options . otherArgs . headers = options . otherArgs . headers || { } ;
1287
1300
options . otherArgs . headers [ 'x-goog-request-params' ] =
1288
- gax . routingHeader . fromParams ( {
1301
+ this . _gaxModule . routingHeader . fromParams ( {
1289
1302
topic : request . topic || '' ,
1290
1303
} ) ;
1291
1304
const defaultCallSettings = this . _defaults [ 'listTopicSubscriptions' ] ;
@@ -1390,7 +1403,7 @@ export class PublisherClient {
1390
1403
options . otherArgs = options . otherArgs || { } ;
1391
1404
options . otherArgs . headers = options . otherArgs . headers || { } ;
1392
1405
options . otherArgs . headers [ 'x-goog-request-params' ] =
1393
- gax . routingHeader . fromParams ( {
1406
+ this . _gaxModule . routingHeader . fromParams ( {
1394
1407
topic : request . topic || '' ,
1395
1408
} ) ;
1396
1409
this . initialize ( ) ;
@@ -1431,7 +1444,7 @@ export class PublisherClient {
1431
1444
options . otherArgs = options . otherArgs || { } ;
1432
1445
options . otherArgs . headers = options . otherArgs . headers || { } ;
1433
1446
options . otherArgs . headers [ 'x-goog-request-params' ] =
1434
- gax . routingHeader . fromParams ( {
1447
+ this . _gaxModule . routingHeader . fromParams ( {
1435
1448
topic : request . topic || '' ,
1436
1449
} ) ;
1437
1450
const defaultCallSettings = this . _defaults [ 'listTopicSnapshots' ] ;
@@ -1479,7 +1492,7 @@ export class PublisherClient {
1479
1492
options . otherArgs = options . otherArgs || { } ;
1480
1493
options . otherArgs . headers = options . otherArgs . headers || { } ;
1481
1494
options . otherArgs . headers [ 'x-goog-request-params' ] =
1482
- gax . routingHeader . fromParams ( {
1495
+ this . _gaxModule . routingHeader . fromParams ( {
1483
1496
topic : request . topic || '' ,
1484
1497
} ) ;
1485
1498
const defaultCallSettings = this . _defaults [ 'listTopicSnapshots' ] ;
0 commit comments