1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.fallback = exports.protobuf = exports.createApiCall = exports.lro = exports.GrpcClient = exports.defaultToObjectOptions = exports.version = exports.routingHeader = void 0;
|
19 | const protobuf = require("protobufjs");
|
20 | exports.protobuf = protobuf;
|
21 | const gax = require("./gax");
|
22 | const routingHeader = require("./routingHeader");
|
23 | exports.routingHeader = routingHeader;
|
24 | const status_1 = require("./status");
|
25 | const google_auth_library_1 = require("google-auth-library");
|
26 | const objectHash = require("object-hash");
|
27 | const operationsClient_1 = require("./operationsClient");
|
28 | const createApiCall_1 = require("./createApiCall");
|
29 | const fallbackProto = require("./fallbackProto");
|
30 | const fallbackRest = require("./fallbackRest");
|
31 | const featureDetection_1 = require("./featureDetection");
|
32 | const fallbackServiceStub_1 = require("./fallbackServiceStub");
|
33 | var pathTemplate_1 = require("./pathTemplate");
|
34 | Object.defineProperty(exports, "PathTemplate", { enumerable: true, get: function () { return pathTemplate_1.PathTemplate; } });
|
35 | var gax_1 = require("./gax");
|
36 | Object.defineProperty(exports, "CallSettings", { enumerable: true, get: function () { return gax_1.CallSettings; } });
|
37 | Object.defineProperty(exports, "constructSettings", { enumerable: true, get: function () { return gax_1.constructSettings; } });
|
38 | Object.defineProperty(exports, "RetryOptions", { enumerable: true, get: function () { return gax_1.RetryOptions; } });
|
39 | exports.version = require('../../package.json').version + '-fallback';
|
40 | var descriptor_1 = require("./descriptor");
|
41 | Object.defineProperty(exports, "BundleDescriptor", { enumerable: true, get: function () { return descriptor_1.BundleDescriptor; } });
|
42 | Object.defineProperty(exports, "LongrunningDescriptor", { enumerable: true, get: function () { return descriptor_1.LongrunningDescriptor; } });
|
43 | Object.defineProperty(exports, "PageDescriptor", { enumerable: true, get: function () { return descriptor_1.PageDescriptor; } });
|
44 | Object.defineProperty(exports, "StreamDescriptor", { enumerable: true, get: function () { return descriptor_1.StreamDescriptor; } });
|
45 | var streaming_1 = require("./streamingCalls/streaming");
|
46 | Object.defineProperty(exports, "StreamType", { enumerable: true, get: function () { return streaming_1.StreamType; } });
|
47 | exports.defaultToObjectOptions = {
|
48 | keepCase: false,
|
49 | longs: String,
|
50 | enums: String,
|
51 | defaults: true,
|
52 | oneofs: true,
|
53 | };
|
54 | const CLIENT_VERSION_HEADER = 'x-goog-api-client';
|
55 | class GrpcClient {
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | constructor(options = {}) {
|
65 | if (!featureDetection_1.isNodeJS()) {
|
66 | if (!options.auth) {
|
67 | throw new Error(JSON.stringify(options) +
|
68 | 'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Use OAuth2Client from google-auth-library.');
|
69 | }
|
70 | this.auth = options.auth;
|
71 | }
|
72 | else {
|
73 | this.auth =
|
74 | options.auth ||
|
75 | new google_auth_library_1.GoogleAuth(options);
|
76 | }
|
77 | this.fallback = options.fallback !== 'rest' ? 'proto' : 'rest';
|
78 | this.grpcVersion = require('../../package.json').version;
|
79 | }
|
80 | |
81 |
|
82 |
|
83 |
|
84 | static clearProtoCache() {
|
85 | GrpcClient.protoCache.clear();
|
86 | }
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | loadProto(jsonObject) {
|
94 | const rootObject = protobuf.Root.fromJSON(jsonObject);
|
95 | return rootObject;
|
96 | }
|
97 | loadProtoJSON(json, ignoreCache = false) {
|
98 | const hash = objectHash(json).toString();
|
99 | const cached = GrpcClient.protoCache.get(hash);
|
100 | if (cached && !ignoreCache) {
|
101 | return cached;
|
102 | }
|
103 | const root = protobuf.Root.fromJSON(json);
|
104 | GrpcClient.protoCache.set(hash, root);
|
105 | return root;
|
106 | }
|
107 | static getServiceMethods(service) {
|
108 | const methods = {};
|
109 | for (const [methodName, methodObject] of Object.entries(service.methods)) {
|
110 | const methodNameLowerCamelCase = methodName[0].toLowerCase() + methodName.substring(1);
|
111 | methods[methodNameLowerCamelCase] = methodObject;
|
112 | }
|
113 | return methods;
|
114 | }
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | constructSettings(serviceName, clientConfig, configOverrides, headers) {
|
128 | function buildMetadata(abTests, moreHeaders) {
|
129 | const metadata = {};
|
130 | if (!headers) {
|
131 | headers = {};
|
132 | }
|
133 |
|
134 |
|
135 | for (const key in headers) {
|
136 | metadata[key] = Array.isArray(headers[key])
|
137 | ? headers[key]
|
138 | : [headers[key]];
|
139 | }
|
140 |
|
141 | const clientVersions = [];
|
142 | if (metadata[CLIENT_VERSION_HEADER] &&
|
143 | metadata[CLIENT_VERSION_HEADER][0]) {
|
144 | clientVersions.push(...metadata[CLIENT_VERSION_HEADER][0].split(' '));
|
145 | }
|
146 | clientVersions.push(`grpc-web/${exports.version}`);
|
147 | metadata[CLIENT_VERSION_HEADER] = [clientVersions.join(' ')];
|
148 | if (!moreHeaders) {
|
149 | return metadata;
|
150 | }
|
151 | for (const key in moreHeaders) {
|
152 | if (key.toLowerCase() !== CLIENT_VERSION_HEADER) {
|
153 | const value = moreHeaders[key];
|
154 | if (Array.isArray(value)) {
|
155 | if (metadata[key] === undefined) {
|
156 | metadata[key] = value;
|
157 | }
|
158 | else {
|
159 | if (Array.isArray(metadata[key])) {
|
160 | metadata[key].push(...value);
|
161 | }
|
162 | else {
|
163 | throw new Error(`Can not add value ${value} to the call metadata.`);
|
164 | }
|
165 | }
|
166 | }
|
167 | else {
|
168 | metadata[key] = [value];
|
169 | }
|
170 | }
|
171 | }
|
172 | return metadata;
|
173 | }
|
174 | return gax.constructSettings(serviceName, clientConfig, configOverrides, status_1.Status, { metadataBuilder: buildMetadata });
|
175 | }
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | async createStub(service, opts,
|
188 |
|
189 |
|
190 | customServicePath) {
|
191 | if (!this.authClient) {
|
192 | if (this.auth && 'getClient' in this.auth) {
|
193 | this.authClient = await this.auth.getClient();
|
194 | }
|
195 | else if (this.auth && 'getRequestHeaders' in this.auth) {
|
196 | this.authClient = this.auth;
|
197 | }
|
198 | }
|
199 | if (!this.authClient) {
|
200 | throw new Error('No authentication was provided');
|
201 | }
|
202 | service.resolveAll();
|
203 | const methods = GrpcClient.getServiceMethods(service);
|
204 | const protocol = opts.protocol || 'https';
|
205 | let servicePath = opts.servicePath;
|
206 | if (!servicePath &&
|
207 | service.options &&
|
208 | service.options['(google.api.default_host)']) {
|
209 | servicePath = service.options['(google.api.default_host)'];
|
210 | }
|
211 | if (!servicePath) {
|
212 | throw new Error(`Cannot determine service API path for service ${service.name}.`);
|
213 | }
|
214 | let servicePort;
|
215 | const match = servicePath.match(/^(.*):(\d+)$/);
|
216 | if (match) {
|
217 | servicePath = match[1];
|
218 | servicePort = parseInt(match[2]);
|
219 | }
|
220 | if (opts.port) {
|
221 | servicePort = opts.port;
|
222 | }
|
223 | else if (!servicePort) {
|
224 | servicePort = 443;
|
225 | }
|
226 | const encoder = this.fallback === 'rest'
|
227 | ? fallbackRest.encodeRequest
|
228 | : fallbackProto.encodeRequest;
|
229 | const decoder = this.fallback === 'rest'
|
230 | ? fallbackRest.decodeResponse
|
231 | : fallbackProto.decodeResponse;
|
232 | const serviceStub = fallbackServiceStub_1.generateServiceStub(methods, protocol, servicePath, servicePort, this.authClient, encoder, decoder);
|
233 | return serviceStub;
|
234 | }
|
235 | }
|
236 | exports.GrpcClient = GrpcClient;
|
237 | GrpcClient.protoCache = new Map();
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | function lro(options) {
|
245 | options = Object.assign({ scopes: [] }, options);
|
246 | const gaxGrpc = new GrpcClient(options);
|
247 | return new operationsClient_1.OperationsClientBuilder(gaxGrpc);
|
248 | }
|
249 | exports.lro = lro;
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | function createApiCall(func, settings, descriptor) {
|
274 | if (descriptor && 'streaming' in descriptor) {
|
275 | return () => {
|
276 | throw new Error('The gRPC-fallback client library (e.g. browser version of the library) currently does not support streaming calls.');
|
277 | };
|
278 | }
|
279 | return createApiCall_1.createApiCall(func, settings, descriptor);
|
280 | }
|
281 | exports.createApiCall = createApiCall;
|
282 | exports.protobufMinimal = require("protobufjs/minimal");
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | const fallback = module.exports;
|
291 | exports.fallback = fallback;
|
292 |
|
\ | No newline at end of file |