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
PaginationCallback ,
29
29
GaxCall ,
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 './tpu_client_config.json' ;
41
- import { operationsProtos } from 'google-gax' ;
42
40
const version = require ( '../../../package.json' ) . version ;
43
41
44
42
/**
@@ -101,8 +99,18 @@ export class TpuClient {
101
99
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
102
100
* For more information, please check the
103
101
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
102
+ * @param {gax } [gaxInstance]: loaded instance of `google-gax`. Useful if you
103
+ * need to avoid loading the default gRPC version and want to use the fallback
104
+ * HTTP implementation. Load only fallback version and pass it to the constructor:
105
+ * ```
106
+ * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
107
+ * const client = new TpuClient({fallback: 'rest'}, gax);
108
+ * ```
104
109
*/
105
- constructor ( opts ?: ClientOptions ) {
110
+ constructor (
111
+ opts ?: ClientOptions ,
112
+ gaxInstance ?: typeof gax | typeof gax . fallback
113
+ ) {
106
114
// Ensure that options include all the required fields.
107
115
const staticMembers = this . constructor as typeof TpuClient ;
108
116
const servicePath =
@@ -122,8 +130,13 @@ export class TpuClient {
122
130
opts [ 'scopes' ] = staticMembers . scopes ;
123
131
}
124
132
133
+ // Load google-gax module synchronously if needed
134
+ if ( ! gaxInstance ) {
135
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
136
+ }
137
+
125
138
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
126
- this . _gaxModule = opts . fallback ? gax . fallback : gax ;
139
+ this . _gaxModule = opts . fallback ? gaxInstance . fallback : gaxInstance ;
127
140
128
141
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
129
142
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -318,7 +331,7 @@ export class TpuClient {
318
331
this . innerApiCalls = { } ;
319
332
320
333
// Add a warn function to the client constructor so it can be easily tested.
321
- this . warn = gax . warn ;
334
+ this . warn = this . _gaxModule . warn ;
322
335
}
323
336
324
337
/**
@@ -528,7 +541,7 @@ export class TpuClient {
528
541
options . otherArgs = options . otherArgs || { } ;
529
542
options . otherArgs . headers = options . otherArgs . headers || { } ;
530
543
options . otherArgs . headers [ 'x-goog-request-params' ] =
531
- gax . routingHeader . fromParams ( {
544
+ this . _gaxModule . routingHeader . fromParams ( {
532
545
name : request . name || '' ,
533
546
} ) ;
534
547
this . initialize ( ) ;
@@ -619,7 +632,7 @@ export class TpuClient {
619
632
options . otherArgs = options . otherArgs || { } ;
620
633
options . otherArgs . headers = options . otherArgs . headers || { } ;
621
634
options . otherArgs . headers [ 'x-goog-request-params' ] =
622
- gax . routingHeader . fromParams ( {
635
+ this . _gaxModule . routingHeader . fromParams ( {
623
636
name : request . name || '' ,
624
637
} ) ;
625
638
this . initialize ( ) ;
@@ -704,7 +717,7 @@ export class TpuClient {
704
717
options . otherArgs = options . otherArgs || { } ;
705
718
options . otherArgs . headers = options . otherArgs . headers || { } ;
706
719
options . otherArgs . headers [ 'x-goog-request-params' ] =
707
- gax . routingHeader . fromParams ( {
720
+ this . _gaxModule . routingHeader . fromParams ( {
708
721
name : request . name || '' ,
709
722
} ) ;
710
723
this . initialize ( ) ;
@@ -812,7 +825,7 @@ export class TpuClient {
812
825
options . otherArgs = options . otherArgs || { } ;
813
826
options . otherArgs . headers = options . otherArgs . headers || { } ;
814
827
options . otherArgs . headers [ 'x-goog-request-params' ] =
815
- gax . routingHeader . fromParams ( {
828
+ this . _gaxModule . routingHeader . fromParams ( {
816
829
parent : request . parent || '' ,
817
830
} ) ;
818
831
this . initialize ( ) ;
@@ -838,11 +851,12 @@ export class TpuClient {
838
851
protos . google . cloud . tpu . v1 . OperationMetadata
839
852
>
840
853
> {
841
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
842
- { name}
843
- ) ;
854
+ const request =
855
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
856
+ { name}
857
+ ) ;
844
858
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
845
- const decodeOperation = new gax . Operation (
859
+ const decodeOperation = new this . _gaxModule . Operation (
846
860
operation ,
847
861
this . descriptors . longrunning . createNode ,
848
862
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -949,7 +963,7 @@ export class TpuClient {
949
963
options . otherArgs = options . otherArgs || { } ;
950
964
options . otherArgs . headers = options . otherArgs . headers || { } ;
951
965
options . otherArgs . headers [ 'x-goog-request-params' ] =
952
- gax . routingHeader . fromParams ( {
966
+ this . _gaxModule . routingHeader . fromParams ( {
953
967
name : request . name || '' ,
954
968
} ) ;
955
969
this . initialize ( ) ;
@@ -975,11 +989,12 @@ export class TpuClient {
975
989
protos . google . cloud . tpu . v1 . OperationMetadata
976
990
>
977
991
> {
978
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
979
- { name}
980
- ) ;
992
+ const request =
993
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
994
+ { name}
995
+ ) ;
981
996
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
982
- const decodeOperation = new gax . Operation (
997
+ const decodeOperation = new this . _gaxModule . Operation (
983
998
operation ,
984
999
this . descriptors . longrunning . deleteNode ,
985
1000
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1088,7 +1103,7 @@ export class TpuClient {
1088
1103
options . otherArgs = options . otherArgs || { } ;
1089
1104
options . otherArgs . headers = options . otherArgs . headers || { } ;
1090
1105
options . otherArgs . headers [ 'x-goog-request-params' ] =
1091
- gax . routingHeader . fromParams ( {
1106
+ this . _gaxModule . routingHeader . fromParams ( {
1092
1107
name : request . name || '' ,
1093
1108
} ) ;
1094
1109
this . initialize ( ) ;
@@ -1114,11 +1129,12 @@ export class TpuClient {
1114
1129
protos . google . cloud . tpu . v1 . OperationMetadata
1115
1130
>
1116
1131
> {
1117
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1118
- { name}
1119
- ) ;
1132
+ const request =
1133
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1134
+ { name}
1135
+ ) ;
1120
1136
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1121
- const decodeOperation = new gax . Operation (
1137
+ const decodeOperation = new this . _gaxModule . Operation (
1122
1138
operation ,
1123
1139
this . descriptors . longrunning . reimageNode ,
1124
1140
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1225,7 +1241,7 @@ export class TpuClient {
1225
1241
options . otherArgs = options . otherArgs || { } ;
1226
1242
options . otherArgs . headers = options . otherArgs . headers || { } ;
1227
1243
options . otherArgs . headers [ 'x-goog-request-params' ] =
1228
- gax . routingHeader . fromParams ( {
1244
+ this . _gaxModule . routingHeader . fromParams ( {
1229
1245
name : request . name || '' ,
1230
1246
} ) ;
1231
1247
this . initialize ( ) ;
@@ -1251,11 +1267,12 @@ export class TpuClient {
1251
1267
protos . google . cloud . tpu . v1 . OperationMetadata
1252
1268
>
1253
1269
> {
1254
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1255
- { name}
1256
- ) ;
1270
+ const request =
1271
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1272
+ { name}
1273
+ ) ;
1257
1274
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1258
- const decodeOperation = new gax . Operation (
1275
+ const decodeOperation = new this . _gaxModule . Operation (
1259
1276
operation ,
1260
1277
this . descriptors . longrunning . stopNode ,
1261
1278
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1362,7 +1379,7 @@ export class TpuClient {
1362
1379
options . otherArgs = options . otherArgs || { } ;
1363
1380
options . otherArgs . headers = options . otherArgs . headers || { } ;
1364
1381
options . otherArgs . headers [ 'x-goog-request-params' ] =
1365
- gax . routingHeader . fromParams ( {
1382
+ this . _gaxModule . routingHeader . fromParams ( {
1366
1383
name : request . name || '' ,
1367
1384
} ) ;
1368
1385
this . initialize ( ) ;
@@ -1388,11 +1405,12 @@ export class TpuClient {
1388
1405
protos . google . cloud . tpu . v1 . OperationMetadata
1389
1406
>
1390
1407
> {
1391
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1392
- { name}
1393
- ) ;
1408
+ const request =
1409
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1410
+ { name}
1411
+ ) ;
1394
1412
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1395
- const decodeOperation = new gax . Operation (
1413
+ const decodeOperation = new this . _gaxModule . Operation (
1396
1414
operation ,
1397
1415
this . descriptors . longrunning . startNode ,
1398
1416
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1486,7 +1504,7 @@ export class TpuClient {
1486
1504
options . otherArgs = options . otherArgs || { } ;
1487
1505
options . otherArgs . headers = options . otherArgs . headers || { } ;
1488
1506
options . otherArgs . headers [ 'x-goog-request-params' ] =
1489
- gax . routingHeader . fromParams ( {
1507
+ this . _gaxModule . routingHeader . fromParams ( {
1490
1508
parent : request . parent || '' ,
1491
1509
} ) ;
1492
1510
this . initialize ( ) ;
@@ -1524,7 +1542,7 @@ export class TpuClient {
1524
1542
options . otherArgs = options . otherArgs || { } ;
1525
1543
options . otherArgs . headers = options . otherArgs . headers || { } ;
1526
1544
options . otherArgs . headers [ 'x-goog-request-params' ] =
1527
- gax . routingHeader . fromParams ( {
1545
+ this . _gaxModule . routingHeader . fromParams ( {
1528
1546
parent : request . parent || '' ,
1529
1547
} ) ;
1530
1548
const defaultCallSettings = this . _defaults [ 'listNodes' ] ;
@@ -1571,7 +1589,7 @@ export class TpuClient {
1571
1589
options . otherArgs = options . otherArgs || { } ;
1572
1590
options . otherArgs . headers = options . otherArgs . headers || { } ;
1573
1591
options . otherArgs . headers [ 'x-goog-request-params' ] =
1574
- gax . routingHeader . fromParams ( {
1592
+ this . _gaxModule . routingHeader . fromParams ( {
1575
1593
parent : request . parent || '' ,
1576
1594
} ) ;
1577
1595
const defaultCallSettings = this . _defaults [ 'listNodes' ] ;
@@ -1679,7 +1697,7 @@ export class TpuClient {
1679
1697
options . otherArgs = options . otherArgs || { } ;
1680
1698
options . otherArgs . headers = options . otherArgs . headers || { } ;
1681
1699
options . otherArgs . headers [ 'x-goog-request-params' ] =
1682
- gax . routingHeader . fromParams ( {
1700
+ this . _gaxModule . routingHeader . fromParams ( {
1683
1701
parent : request . parent || '' ,
1684
1702
} ) ;
1685
1703
this . initialize ( ) ;
@@ -1725,7 +1743,7 @@ export class TpuClient {
1725
1743
options . otherArgs = options . otherArgs || { } ;
1726
1744
options . otherArgs . headers = options . otherArgs . headers || { } ;
1727
1745
options . otherArgs . headers [ 'x-goog-request-params' ] =
1728
- gax . routingHeader . fromParams ( {
1746
+ this . _gaxModule . routingHeader . fromParams ( {
1729
1747
parent : request . parent || '' ,
1730
1748
} ) ;
1731
1749
const defaultCallSettings = this . _defaults [ 'listTensorFlowVersions' ] ;
@@ -1776,7 +1794,7 @@ export class TpuClient {
1776
1794
options . otherArgs = options . otherArgs || { } ;
1777
1795
options . otherArgs . headers = options . otherArgs . headers || { } ;
1778
1796
options . otherArgs . headers [ 'x-goog-request-params' ] =
1779
- gax . routingHeader . fromParams ( {
1797
+ this . _gaxModule . routingHeader . fromParams ( {
1780
1798
parent : request . parent || '' ,
1781
1799
} ) ;
1782
1800
const defaultCallSettings = this . _defaults [ 'listTensorFlowVersions' ] ;
@@ -1884,7 +1902,7 @@ export class TpuClient {
1884
1902
options . otherArgs = options . otherArgs || { } ;
1885
1903
options . otherArgs . headers = options . otherArgs . headers || { } ;
1886
1904
options . otherArgs . headers [ 'x-goog-request-params' ] =
1887
- gax . routingHeader . fromParams ( {
1905
+ this . _gaxModule . routingHeader . fromParams ( {
1888
1906
parent : request . parent || '' ,
1889
1907
} ) ;
1890
1908
this . initialize ( ) ;
@@ -1926,7 +1944,7 @@ export class TpuClient {
1926
1944
options . otherArgs = options . otherArgs || { } ;
1927
1945
options . otherArgs . headers = options . otherArgs . headers || { } ;
1928
1946
options . otherArgs . headers [ 'x-goog-request-params' ] =
1929
- gax . routingHeader . fromParams ( {
1947
+ this . _gaxModule . routingHeader . fromParams ( {
1930
1948
parent : request . parent || '' ,
1931
1949
} ) ;
1932
1950
const defaultCallSettings = this . _defaults [ 'listAcceleratorTypes' ] ;
@@ -1977,7 +1995,7 @@ export class TpuClient {
1977
1995
options . otherArgs = options . otherArgs || { } ;
1978
1996
options . otherArgs . headers = options . otherArgs . headers || { } ;
1979
1997
options . otherArgs . headers [ 'x-goog-request-params' ] =
1980
- gax . routingHeader . fromParams ( {
1998
+ this . _gaxModule . routingHeader . fromParams ( {
1981
1999
parent : request . parent || '' ,
1982
2000
} ) ;
1983
2001
const defaultCallSettings = this . _defaults [ 'listAcceleratorTypes' ] ;
0 commit comments