@google-cloud/spanner 7.18.0 → 7.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +1 -0
- package/build/protos/google/spanner/admin/database/v1/backup.proto +757 -0
- package/build/protos/google/spanner/admin/database/v1/backup_schedule.proto +230 -0
- package/build/protos/google/spanner/admin/database/v1/common.proto +132 -0
- package/build/protos/google/spanner/admin/database/v1/spanner_database_admin.proto +1209 -0
- package/build/protos/google/spanner/admin/instance/v1/common.proto +64 -0
- package/build/protos/google/spanner/admin/instance/v1/spanner_instance_admin.proto +2110 -0
- package/build/protos/google/spanner/executor/v1/cloud_executor.proto +1520 -0
- package/build/protos/google/spanner/v1/commit_response.proto +59 -0
- package/build/protos/google/spanner/v1/keys.proto +163 -0
- package/build/protos/google/spanner/v1/mutation.proto +107 -0
- package/build/protos/google/spanner/v1/query_plan.proto +128 -0
- package/build/protos/google/spanner/v1/result_set.proto +237 -0
- package/build/protos/google/spanner/v1/spanner.proto +1389 -0
- package/build/protos/google/spanner/v1/transaction.proto +575 -0
- package/build/protos/google/spanner/v1/type.proto +214 -0
- package/build/protos/protos.d.ts +39317 -0
- package/build/protos/protos.js +100360 -0
- package/build/protos/protos.json +10271 -0
- package/build/src/backup.d.ts +349 -0
- package/build/src/backup.js +246 -0
- package/build/src/batch-transaction.d.ts +247 -0
- package/build/src/batch-transaction.js +384 -0
- package/build/src/codec.d.ts +362 -0
- package/build/src/codec.js +791 -0
- package/build/src/common-grpc/service-object.d.ts +80 -0
- package/build/src/common-grpc/service-object.js +101 -0
- package/build/src/common-grpc/service.d.ts +445 -0
- package/build/src/common-grpc/service.js +826 -0
- package/build/src/common.d.ts +66 -0
- package/build/src/common.js +54 -0
- package/build/src/database.d.ts +2103 -0
- package/build/src/database.js +1655 -0
- package/build/src/extra_proto_list.json +1 -0
- package/build/src/helper.d.ts +45 -0
- package/build/src/helper.js +71 -0
- package/build/src/index.d.ts +1195 -0
- package/build/src/index.js +1059 -0
- package/build/src/instance-config.d.ts +294 -0
- package/build/src/instance-config.js +216 -0
- package/build/src/instance.d.ts +832 -0
- package/build/src/instance.js +781 -0
- package/build/src/instrument.d.ts +85 -0
- package/build/src/instrument.js +215 -0
- package/build/src/multiplexed-session.d.ts +136 -0
- package/build/src/multiplexed-session.js +197 -0
- package/build/src/partial-result-stream.d.ts +253 -0
- package/build/src/partial-result-stream.js +400 -0
- package/build/src/session-factory.d.ts +100 -0
- package/build/src/session-factory.js +93 -0
- package/build/src/session-pool.d.ts +471 -0
- package/build/src/session-pool.js +754 -0
- package/build/src/session.d.ts +258 -0
- package/build/src/session.js +348 -0
- package/build/src/spanner_grpc_config.json +92 -0
- package/build/src/table.d.ts +831 -0
- package/build/src/table.js +277 -0
- package/build/src/transaction-runner.d.ts +185 -0
- package/build/src/transaction-runner.js +282 -0
- package/build/src/transaction.d.ts +1691 -0
- package/build/src/transaction.js +2004 -0
- package/build/src/v1/database_admin_client.d.ts +2301 -0
- package/build/src/v1/database_admin_client.js +2286 -0
- package/build/src/v1/database_admin_client_config.json +160 -0
- package/build/src/v1/database_admin_proto_list.json +6 -0
- package/build/src/v1/gapic_metadata.json +27 -0
- package/build/src/v1/index.d.ts +4 -0
- package/build/src/v1/index.js +29 -0
- package/build/src/v1/instance_admin_client.d.ts +1985 -0
- package/build/src/v1/instance_admin_client.js +1768 -0
- package/build/src/v1/instance_admin_client_config.json +129 -0
- package/build/src/v1/instance_admin_proto_list.json +4 -0
- package/build/src/v1/spanner_client.d.ts +1250 -0
- package/build/src/v1/spanner_client.js +1086 -0
- package/build/src/v1/spanner_client_config.json +119 -0
- package/build/src/v1/spanner_executor_proxy_client.d.ts +418 -0
- package/build/src/v1/spanner_executor_proxy_client.js +706 -0
- package/build/src/v1/spanner_executor_proxy_client_config.json +30 -0
- package/build/src/v1/spanner_executor_proxy_proto_list.json +14 -0
- package/build/src/v1/spanner_proto_list.json +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2019 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.GrpcService = exports.ObjectToStructConverter = void 0;
|
|
19
|
+
/*!
|
|
20
|
+
* @module commonGrpc/service
|
|
21
|
+
*/
|
|
22
|
+
const path = require("path");
|
|
23
|
+
const common_1 = require("@google-cloud/common");
|
|
24
|
+
const projectify_1 = require("@google-cloud/projectify");
|
|
25
|
+
const proto_loader_1 = require("@grpc/proto-loader");
|
|
26
|
+
const duplexify = require("duplexify");
|
|
27
|
+
const extend = require("extend");
|
|
28
|
+
const google_gax_1 = require("google-gax");
|
|
29
|
+
const is = require("is");
|
|
30
|
+
const retryRequest = require("retry-request");
|
|
31
|
+
const stream_1 = require("stream");
|
|
32
|
+
const gaxProtoPath = path.join(path.dirname(require.resolve('google-gax')), '..', 'protos');
|
|
33
|
+
// TODO: convert this object to an array
|
|
34
|
+
/**
|
|
35
|
+
* @const {object} - A map of protobuf codes to HTTP status codes.
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
const GRPC_ERROR_CODE_TO_HTTP = {
|
|
39
|
+
0: {
|
|
40
|
+
code: 200,
|
|
41
|
+
message: 'OK',
|
|
42
|
+
},
|
|
43
|
+
1: {
|
|
44
|
+
code: 499,
|
|
45
|
+
message: 'Client Closed Request',
|
|
46
|
+
},
|
|
47
|
+
2: {
|
|
48
|
+
code: 500,
|
|
49
|
+
message: 'Internal Server Error',
|
|
50
|
+
},
|
|
51
|
+
3: {
|
|
52
|
+
code: 400,
|
|
53
|
+
message: 'Bad Request',
|
|
54
|
+
},
|
|
55
|
+
4: {
|
|
56
|
+
code: 504,
|
|
57
|
+
message: 'Gateway Timeout',
|
|
58
|
+
},
|
|
59
|
+
5: {
|
|
60
|
+
code: 404,
|
|
61
|
+
message: 'Not Found',
|
|
62
|
+
},
|
|
63
|
+
6: {
|
|
64
|
+
code: 409,
|
|
65
|
+
message: 'Conflict',
|
|
66
|
+
},
|
|
67
|
+
7: {
|
|
68
|
+
code: 403,
|
|
69
|
+
message: 'Forbidden',
|
|
70
|
+
},
|
|
71
|
+
8: {
|
|
72
|
+
code: 429,
|
|
73
|
+
message: 'Too Many Requests',
|
|
74
|
+
},
|
|
75
|
+
9: {
|
|
76
|
+
code: 412,
|
|
77
|
+
message: 'Precondition Failed',
|
|
78
|
+
},
|
|
79
|
+
10: {
|
|
80
|
+
code: 409,
|
|
81
|
+
message: 'Conflict',
|
|
82
|
+
},
|
|
83
|
+
11: {
|
|
84
|
+
code: 400,
|
|
85
|
+
message: 'Bad Request',
|
|
86
|
+
},
|
|
87
|
+
12: {
|
|
88
|
+
code: 501,
|
|
89
|
+
message: 'Not Implemented',
|
|
90
|
+
},
|
|
91
|
+
13: {
|
|
92
|
+
code: 500,
|
|
93
|
+
message: 'Internal Server Error',
|
|
94
|
+
},
|
|
95
|
+
14: {
|
|
96
|
+
code: 503,
|
|
97
|
+
message: 'Service Unavailable',
|
|
98
|
+
},
|
|
99
|
+
15: {
|
|
100
|
+
code: 500,
|
|
101
|
+
message: 'Internal Server Error',
|
|
102
|
+
},
|
|
103
|
+
16: {
|
|
104
|
+
code: 401,
|
|
105
|
+
message: 'Unauthorized',
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* The default configuration for all gRPC Service instantions.
|
|
110
|
+
*
|
|
111
|
+
* @resource [All options]{@link
|
|
112
|
+
* https://github.com/grpc/grpc/blob/13e185419cd177b7fb552601665e43820321a96b/include/grpc/impl/codegen/grpc_types.h#L148}
|
|
113
|
+
*
|
|
114
|
+
* @private
|
|
115
|
+
*
|
|
116
|
+
* @type {object}
|
|
117
|
+
*/
|
|
118
|
+
const GRPC_SERVICE_OPTIONS = {
|
|
119
|
+
// RE: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1991
|
|
120
|
+
'grpc.max_send_message_length': -1, // unlimited
|
|
121
|
+
'grpc.max_receive_message_length': -1, // unlimited
|
|
122
|
+
// RE: https://github.com/grpc/grpc/issues/8839
|
|
123
|
+
// RE: https://github.com/grpc/grpc/issues/8382
|
|
124
|
+
// RE: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1991
|
|
125
|
+
'grpc.initial_reconnect_backoff_ms': 5000,
|
|
126
|
+
};
|
|
127
|
+
class ObjectToStructConverter {
|
|
128
|
+
/**
|
|
129
|
+
* A class that can be used to convert an object to a struct. Optionally this
|
|
130
|
+
* class can be used to erase/throw on circular references during conversion.
|
|
131
|
+
*
|
|
132
|
+
* @private
|
|
133
|
+
*
|
|
134
|
+
* @param {object=} options - Configuration object.
|
|
135
|
+
* @param {boolean} options.removeCircular - Remove circular references in the
|
|
136
|
+
* object with a placeholder string. (Default: `false`)
|
|
137
|
+
* @param {boolean} options.stringify - Stringify un-recognized types. (Default:
|
|
138
|
+
* `false`)
|
|
139
|
+
*/
|
|
140
|
+
constructor(options) {
|
|
141
|
+
options = options || {};
|
|
142
|
+
this.seenObjects = new Set();
|
|
143
|
+
this.removeCircular = options.removeCircular === true;
|
|
144
|
+
this.stringify = options.stringify === true;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Begin the conversion process from a JS object to an encoded gRPC Value
|
|
148
|
+
* message.
|
|
149
|
+
*
|
|
150
|
+
* @param {*} value - The input value.
|
|
151
|
+
* @return {object} - The encoded value.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ObjectToStructConverter.convert({
|
|
155
|
+
* aString: 'Hi'
|
|
156
|
+
* });
|
|
157
|
+
* // {
|
|
158
|
+
* // fields: {
|
|
159
|
+
* // aString: {
|
|
160
|
+
* // stringValue: 'Hello!'
|
|
161
|
+
* // }
|
|
162
|
+
* // }
|
|
163
|
+
* // }
|
|
164
|
+
*/
|
|
165
|
+
convert(obj) {
|
|
166
|
+
const convertedObject = {
|
|
167
|
+
fields: {},
|
|
168
|
+
};
|
|
169
|
+
this.seenObjects.add(obj);
|
|
170
|
+
for (const prop in obj) {
|
|
171
|
+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
|
|
172
|
+
const value = obj[prop];
|
|
173
|
+
if (is.undefined(value)) {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
convertedObject.fields[prop] = this.encodeValue_(value);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
this.seenObjects.delete(obj);
|
|
180
|
+
return convertedObject;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Convert a raw value to a type-denoted protobuf message-friendly object.
|
|
184
|
+
*
|
|
185
|
+
* @private
|
|
186
|
+
*
|
|
187
|
+
* @param {*} value - The input value.
|
|
188
|
+
* @return {*} - The encoded value.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ObjectToStructConverter.encodeValue('Hi');
|
|
192
|
+
* // {
|
|
193
|
+
* // stringValue: 'Hello!'
|
|
194
|
+
* // }
|
|
195
|
+
*/
|
|
196
|
+
encodeValue_(value) {
|
|
197
|
+
let convertedValue;
|
|
198
|
+
if (is.null(value)) {
|
|
199
|
+
convertedValue = {
|
|
200
|
+
nullValue: 0,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
else if (is.number(value)) {
|
|
204
|
+
convertedValue = {
|
|
205
|
+
numberValue: value,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
else if (is.string(value)) {
|
|
209
|
+
convertedValue = {
|
|
210
|
+
stringValue: value,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
else if (is.boolean(value)) {
|
|
214
|
+
convertedValue = {
|
|
215
|
+
boolValue: value,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
else if (Buffer.isBuffer(value)) {
|
|
219
|
+
convertedValue = {
|
|
220
|
+
blobValue: value,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
else if (is.object(value)) {
|
|
224
|
+
if (this.seenObjects.has(value)) {
|
|
225
|
+
// Circular reference.
|
|
226
|
+
if (!this.removeCircular) {
|
|
227
|
+
throw new Error([
|
|
228
|
+
'This object contains a circular reference. To automatically',
|
|
229
|
+
'remove it, set the `removeCircular` option to true.',
|
|
230
|
+
].join(' '));
|
|
231
|
+
}
|
|
232
|
+
convertedValue = {
|
|
233
|
+
stringValue: '[Circular]',
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
convertedValue = {
|
|
238
|
+
structValue: this.convert(value),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
else if (is.array(value)) {
|
|
243
|
+
convertedValue = {
|
|
244
|
+
listValue: {
|
|
245
|
+
values: value.map(this.encodeValue_.bind(this)),
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
if (!this.stringify) {
|
|
251
|
+
throw new Error('Value of type ' + typeof value + ' not recognized.');
|
|
252
|
+
}
|
|
253
|
+
convertedValue = {
|
|
254
|
+
stringValue: String(value),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return convertedValue;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
exports.ObjectToStructConverter = ObjectToStructConverter;
|
|
261
|
+
class GrpcService extends common_1.Service {
|
|
262
|
+
/**
|
|
263
|
+
* Service is a base class, meant to be inherited from by a "service," like
|
|
264
|
+
* BigQuery or Storage.
|
|
265
|
+
*
|
|
266
|
+
* This handles making authenticated requests by exposing a `makeReq_`
|
|
267
|
+
* function.
|
|
268
|
+
*
|
|
269
|
+
* @constructor
|
|
270
|
+
* @alias module:common/grpc-service
|
|
271
|
+
*
|
|
272
|
+
* @param config - Configuration object.
|
|
273
|
+
* @param {object} options - [Configuration object](#/docs/?method=gcloud).
|
|
274
|
+
*/
|
|
275
|
+
constructor(config, options) {
|
|
276
|
+
super(config, options);
|
|
277
|
+
this.activeServiceMap_ = new Map();
|
|
278
|
+
this.protos = {};
|
|
279
|
+
if (global['GCLOUD_SANDBOX_ENV']) {
|
|
280
|
+
// gRPC has a tendency to cause our doc unit tests to fail, so we prevent
|
|
281
|
+
// any calls to that library from going through.
|
|
282
|
+
// Reference:
|
|
283
|
+
// https://github.com/GoogleCloudPlatform/google-cloud-node/pull/1137#issuecomment-193315047
|
|
284
|
+
return global['GCLOUD_SANDBOX_ENV'];
|
|
285
|
+
}
|
|
286
|
+
if (config.grpc) {
|
|
287
|
+
this.grpc = config.grpc;
|
|
288
|
+
this.grpcVersion = config.grpcVersion || 'grpc/unknown';
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
this.grpc = google_gax_1.grpc;
|
|
292
|
+
this.grpcVersion = 'grpc/' + new google_gax_1.GrpcClient().grpcVersion;
|
|
293
|
+
}
|
|
294
|
+
if (config.customEndpoint) {
|
|
295
|
+
this.grpcCredentials = this.grpc.credentials.createInsecure();
|
|
296
|
+
}
|
|
297
|
+
this.grpcMetadata = new this.grpc.Metadata();
|
|
298
|
+
this.grpcMetadata.add('x-goog-api-client', [
|
|
299
|
+
'gl-node/' + process.versions.node,
|
|
300
|
+
'gccl/' + config.packageJson.version,
|
|
301
|
+
this.grpcVersion,
|
|
302
|
+
].join(' '));
|
|
303
|
+
if (config.grpcMetadata) {
|
|
304
|
+
for (const prop in config.grpcMetadata) {
|
|
305
|
+
this.grpcMetadata.add(prop, config.grpcMetadata[prop]);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
this.maxRetries = options.maxRetries;
|
|
309
|
+
this.userAgent = common_1.util.getUserAgentFromPackageJson(config.packageJson);
|
|
310
|
+
if (this.providedUserAgent) {
|
|
311
|
+
this.userAgent = `${this.providedUserAgent} ${this.userAgent}`;
|
|
312
|
+
}
|
|
313
|
+
options['grpc.primary_user_agent'] = this.userAgent;
|
|
314
|
+
this.activeServiceMap_ = new Map();
|
|
315
|
+
this.protos = {};
|
|
316
|
+
const protoServices = config.protoServices;
|
|
317
|
+
Object.keys(protoServices).forEach(name => {
|
|
318
|
+
const protoConfig = protoServices[name];
|
|
319
|
+
const services = this.loadProtoFile(protoConfig.path, config);
|
|
320
|
+
const serviceKey = ['google', protoConfig.service, name]
|
|
321
|
+
.filter(x => x)
|
|
322
|
+
.join('.');
|
|
323
|
+
const service = services[serviceKey];
|
|
324
|
+
this.protos[name] = service;
|
|
325
|
+
if (protoConfig.baseUrl) {
|
|
326
|
+
service.baseUrl = protoConfig.baseUrl;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
request(pOpts, rOpts, callback) {
|
|
331
|
+
/**
|
|
332
|
+
* The function signature above is a little funky. This is due to the way
|
|
333
|
+
* method overloading in TypeScript operates. Since this class extends
|
|
334
|
+
* Service, the signatures for `request` need to have
|
|
335
|
+
* *something* in common. The only signature actually used here is:
|
|
336
|
+
*
|
|
337
|
+
* request(protoOpts: ProtoOpts, reqOpts: DecorateRequestOptions, callback:
|
|
338
|
+
* ServiceRequestCallback): Abortable|void;
|
|
339
|
+
*
|
|
340
|
+
* Hence the weird casting below.
|
|
341
|
+
*/
|
|
342
|
+
const protoOpts = pOpts;
|
|
343
|
+
let reqOpts = rOpts;
|
|
344
|
+
if (global['GCLOUD_SANDBOX_ENV']) {
|
|
345
|
+
return global['GCLOUD_SANDBOX_ENV'];
|
|
346
|
+
}
|
|
347
|
+
if (!this.grpcCredentials) {
|
|
348
|
+
// We must establish an authClient to give to grpc.
|
|
349
|
+
this.getGrpcCredentials_((err, credentials) => {
|
|
350
|
+
if (err) {
|
|
351
|
+
callback(err);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
this.grpcCredentials = credentials;
|
|
355
|
+
this.request(protoOpts, reqOpts, callback);
|
|
356
|
+
});
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const service = this.getService_(protoOpts);
|
|
360
|
+
const metadata = this.grpcMetadata;
|
|
361
|
+
const grpcOpts = {};
|
|
362
|
+
if (typeof protoOpts.timeout === 'number') {
|
|
363
|
+
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
reqOpts = this.decorateRequest_(reqOpts);
|
|
367
|
+
}
|
|
368
|
+
catch (e) {
|
|
369
|
+
callback(e);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
// Retains a reference to an error from the response. If the final callback
|
|
373
|
+
// is executed with this as the "response", we return it to the user as an
|
|
374
|
+
// error.
|
|
375
|
+
let respError;
|
|
376
|
+
const retryOpts = Object.assign({
|
|
377
|
+
retries: this.maxRetries,
|
|
378
|
+
currentRetryAttempt: 0,
|
|
379
|
+
shouldRetryFn: GrpcService.shouldRetryRequest_,
|
|
380
|
+
// retry-request determines if it should retry from the incoming HTTP
|
|
381
|
+
// response status. gRPC always returns an error proto message. We
|
|
382
|
+
// pass that "error" into retry-request to act as the HTTP response,
|
|
383
|
+
// so it can use the status code to determine if it should retry.
|
|
384
|
+
request(_, onResponse) {
|
|
385
|
+
respError = null;
|
|
386
|
+
return service[protoOpts.method](reqOpts, metadata, grpcOpts, (err, resp) => {
|
|
387
|
+
if (err) {
|
|
388
|
+
respError = GrpcService.decorateError_(err);
|
|
389
|
+
if (respError) {
|
|
390
|
+
onResponse(null, respError);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
onResponse(err, resp);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
onResponse(null, resp);
|
|
397
|
+
});
|
|
398
|
+
},
|
|
399
|
+
}, protoOpts.retryOpts);
|
|
400
|
+
return retryRequest(null, retryOpts, (err, resp) => {
|
|
401
|
+
if (!err && resp === respError) {
|
|
402
|
+
err = respError;
|
|
403
|
+
resp = null;
|
|
404
|
+
}
|
|
405
|
+
callback(err, resp);
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
requestStream(pOpts, rOpts) {
|
|
409
|
+
/**
|
|
410
|
+
* The function signature above is a little funky. This is due to the way
|
|
411
|
+
* method overloading in TypeScript operates. Since this class extends
|
|
412
|
+
* Service, the signatures for `requestStream` need to have
|
|
413
|
+
* *something* in common. The only signature actually used here is:
|
|
414
|
+
*
|
|
415
|
+
* requestStream(protoOpts: ProtoOpts, reqOpts: DecorateRequestOptions):
|
|
416
|
+
* Duplex;
|
|
417
|
+
*
|
|
418
|
+
* Hence the weird casting below.
|
|
419
|
+
*/
|
|
420
|
+
if (global['GCLOUD_SANDBOX_ENV']) {
|
|
421
|
+
return new stream_1.PassThrough({ objectMode: true });
|
|
422
|
+
}
|
|
423
|
+
const protoOpts = pOpts;
|
|
424
|
+
let reqOpts = rOpts;
|
|
425
|
+
if (!protoOpts.stream) {
|
|
426
|
+
protoOpts.stream = new stream_1.PassThrough({ objectMode: true });
|
|
427
|
+
}
|
|
428
|
+
const stream = protoOpts.stream;
|
|
429
|
+
if (!this.grpcCredentials) {
|
|
430
|
+
// We must establish an authClient to give to grpc.
|
|
431
|
+
this.getGrpcCredentials_((err, credentials) => {
|
|
432
|
+
if (err) {
|
|
433
|
+
stream.destroy(err);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
this.grpcCredentials = credentials;
|
|
437
|
+
this.requestStream(protoOpts, reqOpts);
|
|
438
|
+
});
|
|
439
|
+
return stream;
|
|
440
|
+
}
|
|
441
|
+
const objectMode = !!reqOpts.objectMode;
|
|
442
|
+
const service = this.getService_(protoOpts);
|
|
443
|
+
const grpcMetadata = this.grpcMetadata;
|
|
444
|
+
const grpcOpts = {};
|
|
445
|
+
if (typeof protoOpts.timeout === 'number') {
|
|
446
|
+
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
|
|
447
|
+
}
|
|
448
|
+
try {
|
|
449
|
+
reqOpts = this.decorateRequest_(reqOpts);
|
|
450
|
+
}
|
|
451
|
+
catch (e) {
|
|
452
|
+
setImmediate(() => {
|
|
453
|
+
stream.destroy(e);
|
|
454
|
+
});
|
|
455
|
+
return stream;
|
|
456
|
+
}
|
|
457
|
+
const retryOpts = Object.assign({
|
|
458
|
+
retries: this.maxRetries,
|
|
459
|
+
currentRetryAttempt: 0,
|
|
460
|
+
objectMode,
|
|
461
|
+
shouldRetryFn: GrpcService.shouldRetryRequest_,
|
|
462
|
+
request() {
|
|
463
|
+
const ee = service[protoOpts.method](reqOpts, grpcMetadata, grpcOpts).on('metadata', () => {
|
|
464
|
+
// retry-request requires a server response before it
|
|
465
|
+
// starts emitting data. The closest mechanism grpc
|
|
466
|
+
// provides is a metadata event, but this does not provide
|
|
467
|
+
// any kind of response status. So we're faking it here
|
|
468
|
+
// with code `0` which translates to HTTP 200.
|
|
469
|
+
//
|
|
470
|
+
// https://github.com/GoogleCloudPlatform/google-cloud-node/pull/1444#discussion_r71812636
|
|
471
|
+
const grcpStatus = GrpcService.decorateStatus_({ code: 0 });
|
|
472
|
+
ee.emit('response', grcpStatus);
|
|
473
|
+
});
|
|
474
|
+
return ee;
|
|
475
|
+
},
|
|
476
|
+
}, protoOpts.retryOpts);
|
|
477
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
478
|
+
return retryRequest(null, retryOpts)
|
|
479
|
+
.on('error', err => {
|
|
480
|
+
const grpcError = GrpcService.decorateError_(err);
|
|
481
|
+
stream.destroy(grpcError || err);
|
|
482
|
+
})
|
|
483
|
+
.on('request', stream.emit.bind(stream, 'request'))
|
|
484
|
+
.pipe(stream);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Make an authenticated writable streaming request with gRPC.
|
|
488
|
+
*
|
|
489
|
+
* @param {object} protoOpts - The proto options.
|
|
490
|
+
* @param {string} protoOpts.service - The service.
|
|
491
|
+
* @param {string} protoOpts.method - The method name.
|
|
492
|
+
* @param {number=} protoOpts.timeout - After how many milliseconds should the
|
|
493
|
+
* request cancel.
|
|
494
|
+
* @param {object} reqOpts - The request options.
|
|
495
|
+
*/
|
|
496
|
+
requestWritableStream(protoOpts, reqOpts) {
|
|
497
|
+
const stream =
|
|
498
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
499
|
+
(protoOpts.stream = protoOpts.stream || duplexify.obj());
|
|
500
|
+
if (global['GCLOUD_SANDBOX_ENV']) {
|
|
501
|
+
return stream;
|
|
502
|
+
}
|
|
503
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
504
|
+
const self = this;
|
|
505
|
+
if (!this.grpcCredentials) {
|
|
506
|
+
// We must establish an authClient to give to grpc.
|
|
507
|
+
this.getGrpcCredentials_((err, credentials) => {
|
|
508
|
+
if (err) {
|
|
509
|
+
stream.destroy(err);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
self.grpcCredentials = credentials;
|
|
513
|
+
self.requestWritableStream(protoOpts, reqOpts);
|
|
514
|
+
});
|
|
515
|
+
return stream;
|
|
516
|
+
}
|
|
517
|
+
const service = this.getService_(protoOpts);
|
|
518
|
+
const grpcMetadata = this.grpcMetadata;
|
|
519
|
+
const grpcOpts = {};
|
|
520
|
+
if (is.number(protoOpts.timeout)) {
|
|
521
|
+
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
reqOpts = this.decorateRequest_(reqOpts);
|
|
525
|
+
}
|
|
526
|
+
catch (e) {
|
|
527
|
+
setImmediate(() => {
|
|
528
|
+
stream.destroy(e);
|
|
529
|
+
});
|
|
530
|
+
return stream;
|
|
531
|
+
}
|
|
532
|
+
const grpcStream = service[protoOpts.method](reqOpts, grpcMetadata, grpcOpts)
|
|
533
|
+
.on('status', status => {
|
|
534
|
+
const grcpStatus = GrpcService.decorateStatus_(status);
|
|
535
|
+
stream.emit('response', grcpStatus || status);
|
|
536
|
+
})
|
|
537
|
+
.on('error', err => {
|
|
538
|
+
const grpcError = GrpcService.decorateError_(err);
|
|
539
|
+
stream.destroy(grpcError || err);
|
|
540
|
+
});
|
|
541
|
+
stream.setReadable(grpcStream);
|
|
542
|
+
stream.setWritable(grpcStream);
|
|
543
|
+
return stream;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Decode a protobuf Struct's value.
|
|
547
|
+
*
|
|
548
|
+
* @param {object} value - A Struct's Field message.
|
|
549
|
+
* @return {*} - The decoded value.
|
|
550
|
+
*/
|
|
551
|
+
static decodeValue_(value) {
|
|
552
|
+
switch (value.kind) {
|
|
553
|
+
case 'structValue': {
|
|
554
|
+
return GrpcService.structToObj_(value.structValue);
|
|
555
|
+
}
|
|
556
|
+
case 'nullValue': {
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
case 'listValue': {
|
|
560
|
+
return value.listValue.values.map(GrpcService.decodeValue_);
|
|
561
|
+
}
|
|
562
|
+
default: {
|
|
563
|
+
return value[value.kind];
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Convert a raw value to a type-denoted protobuf message-friendly object.
|
|
569
|
+
*
|
|
570
|
+
*
|
|
571
|
+
* @param {*} value - The input value.
|
|
572
|
+
* @return {*} - The encoded value.
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ObjectToStructConverter.encodeValue('Hi');
|
|
576
|
+
* // {
|
|
577
|
+
* // stringValue: 'Hello!'
|
|
578
|
+
* // }
|
|
579
|
+
*/
|
|
580
|
+
static encodeValue_(value) {
|
|
581
|
+
return new GrpcService.ObjectToStructConverter().encodeValue_(value);
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Creates a deadline.
|
|
585
|
+
*
|
|
586
|
+
* @private
|
|
587
|
+
*
|
|
588
|
+
* @param {number} timeout - Timeout in miliseconds.
|
|
589
|
+
* @return {date} deadline - The deadline in Date object form.
|
|
590
|
+
*/
|
|
591
|
+
static createDeadline_(timeout) {
|
|
592
|
+
return new Date(Date.now() + timeout);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Checks for a grpc status code and extends the error object with additional
|
|
596
|
+
* information.
|
|
597
|
+
*
|
|
598
|
+
* @private
|
|
599
|
+
*
|
|
600
|
+
* @param {error|object} err - The grpc error.
|
|
601
|
+
* @return {error|null}
|
|
602
|
+
*/
|
|
603
|
+
static decorateError_(err) {
|
|
604
|
+
const errorObj = is.error(err) ? err : {};
|
|
605
|
+
return GrpcService.decorateGrpcResponse_(errorObj, err);
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Checks for a grpc status code and extends the supplied object with
|
|
609
|
+
* additional information.
|
|
610
|
+
*
|
|
611
|
+
* @private
|
|
612
|
+
*
|
|
613
|
+
* @param {object} obj - The object to be extended.
|
|
614
|
+
* @param {object} response - The grpc response.
|
|
615
|
+
* @return {object|null}
|
|
616
|
+
*/
|
|
617
|
+
static decorateGrpcResponse_(obj, response) {
|
|
618
|
+
if (response && GRPC_ERROR_CODE_TO_HTTP[response.code]) {
|
|
619
|
+
const defaultResponseDetails = GRPC_ERROR_CODE_TO_HTTP[response.code];
|
|
620
|
+
let message = defaultResponseDetails.message;
|
|
621
|
+
if (response.message) {
|
|
622
|
+
// gRPC error messages can be either stringified JSON or strings.
|
|
623
|
+
try {
|
|
624
|
+
message = JSON.parse(response.message).description;
|
|
625
|
+
}
|
|
626
|
+
catch (e) {
|
|
627
|
+
message = response.message;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return extend(true, obj, response, {
|
|
631
|
+
code: defaultResponseDetails.code,
|
|
632
|
+
message,
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
return null;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Checks for grpc status code and extends the status object with additional
|
|
639
|
+
* information
|
|
640
|
+
*
|
|
641
|
+
* @private
|
|
642
|
+
* @param {object} status - The grpc status.
|
|
643
|
+
* @return {object|null}
|
|
644
|
+
*/
|
|
645
|
+
static decorateStatus_(status) {
|
|
646
|
+
return GrpcService.decorateGrpcResponse_({}, status);
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Function to decide whether or not a request retry could occur.
|
|
650
|
+
*
|
|
651
|
+
* @private
|
|
652
|
+
*
|
|
653
|
+
* @param {object} response - The request response.
|
|
654
|
+
* @return {boolean} shouldRetry
|
|
655
|
+
*/
|
|
656
|
+
static shouldRetryRequest_(response) {
|
|
657
|
+
return [429, 500, 502, 503].indexOf(response.code) > -1;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Convert an object to a struct.
|
|
661
|
+
*
|
|
662
|
+
* @private
|
|
663
|
+
*
|
|
664
|
+
* @param {object} obj - An object to convert.
|
|
665
|
+
* @param {object=} options - Configuration object.
|
|
666
|
+
* @param {boolean} options.removeCircular - Remove circular references in the
|
|
667
|
+
* object with a placeholder string.
|
|
668
|
+
* @param {boolean} options.stringify - Stringify un-recognized types.
|
|
669
|
+
* @return {array} - The converted object.
|
|
670
|
+
*
|
|
671
|
+
* @example
|
|
672
|
+
* GrpcService.objToStruct_({
|
|
673
|
+
* greeting: 'Hello!',
|
|
674
|
+
* favNumber: 7,
|
|
675
|
+
* friendIds: [
|
|
676
|
+
* 1004,
|
|
677
|
+
* 1006
|
|
678
|
+
* ],
|
|
679
|
+
* userDetails: {
|
|
680
|
+
* termsSigned: true
|
|
681
|
+
* }
|
|
682
|
+
* });
|
|
683
|
+
* // {
|
|
684
|
+
* // fields: {
|
|
685
|
+
* // greeting: {
|
|
686
|
+
* // stringValue: 'Hello!'
|
|
687
|
+
* // },
|
|
688
|
+
* // favNumber: {
|
|
689
|
+
* // numberValue: 7
|
|
690
|
+
* // },
|
|
691
|
+
* // friendIds: {
|
|
692
|
+
* // listValue: [
|
|
693
|
+
* // {
|
|
694
|
+
* // numberValue: 1004
|
|
695
|
+
* // },
|
|
696
|
+
* // {
|
|
697
|
+
* // numberValue: 1006
|
|
698
|
+
* // }
|
|
699
|
+
* // ]
|
|
700
|
+
* // },
|
|
701
|
+
* // userDetails: {
|
|
702
|
+
* // fields: {
|
|
703
|
+
* // termsSigned: {
|
|
704
|
+
* // booleanValue: true
|
|
705
|
+
* // }
|
|
706
|
+
* // }
|
|
707
|
+
* // }
|
|
708
|
+
* // }
|
|
709
|
+
* // }
|
|
710
|
+
*/
|
|
711
|
+
static objToStruct_(obj, options) {
|
|
712
|
+
return new GrpcService.ObjectToStructConverter(options).convert(obj);
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Condense a protobuf Struct into an object of only its values.
|
|
716
|
+
*
|
|
717
|
+
* @private
|
|
718
|
+
*
|
|
719
|
+
* @param {object} struct - A protobuf Struct message.
|
|
720
|
+
* @return {object} - The simplified object.
|
|
721
|
+
*
|
|
722
|
+
* @example
|
|
723
|
+
* GrpcService.structToObj_({
|
|
724
|
+
* fields: {
|
|
725
|
+
* name: {
|
|
726
|
+
* kind: 'stringValue',
|
|
727
|
+
* stringValue: 'Stephen'
|
|
728
|
+
* }
|
|
729
|
+
* }
|
|
730
|
+
* });
|
|
731
|
+
* // {
|
|
732
|
+
* // name: 'Stephen'
|
|
733
|
+
* // }
|
|
734
|
+
*/
|
|
735
|
+
static structToObj_(struct) {
|
|
736
|
+
const convertedObject = {};
|
|
737
|
+
for (const prop in struct.fields) {
|
|
738
|
+
const value = struct.fields[prop];
|
|
739
|
+
convertedObject[prop] = GrpcService.decodeValue_(value);
|
|
740
|
+
}
|
|
741
|
+
return convertedObject;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Assign a projectId if one is specified to all request options.
|
|
745
|
+
*
|
|
746
|
+
* @param {object} reqOpts - The request options.
|
|
747
|
+
* @return {object} - The decorated request object.
|
|
748
|
+
*/
|
|
749
|
+
decorateRequest_(reqOpts) {
|
|
750
|
+
reqOpts = Object.assign({}, reqOpts);
|
|
751
|
+
delete reqOpts.autoPaginate;
|
|
752
|
+
delete reqOpts.autoPaginateVal;
|
|
753
|
+
delete reqOpts.objectMode;
|
|
754
|
+
return (0, projectify_1.replaceProjectIdToken)(reqOpts, this.projectId);
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* To authorize requests through gRPC, we must get the raw google-auth-library
|
|
758
|
+
* auth client object.
|
|
759
|
+
*
|
|
760
|
+
* @private
|
|
761
|
+
*
|
|
762
|
+
* @param {function} callback - The callback function.
|
|
763
|
+
* @param {?error} callback.err - An error getting an auth client.
|
|
764
|
+
*/
|
|
765
|
+
getGrpcCredentials_(callback) {
|
|
766
|
+
this.authClient.getClient().then(client => {
|
|
767
|
+
const credentials = this.grpc.credentials.combineChannelCredentials(this.grpc.credentials.createSsl(), this.grpc.credentials.createFromGoogleCredential(client));
|
|
768
|
+
if (!this.projectId || this.projectId === '{{projectId}}') {
|
|
769
|
+
this.projectId = client.projectId;
|
|
770
|
+
}
|
|
771
|
+
callback(null, credentials);
|
|
772
|
+
}, callback);
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Loads a proto file, useful when handling multiple proto files/services
|
|
776
|
+
* within a single instance of GrpcService.
|
|
777
|
+
*
|
|
778
|
+
* @private
|
|
779
|
+
*
|
|
780
|
+
* @param protoConfig - The proto specific configs for this file.
|
|
781
|
+
* @param config - The base config for the GrpcService.
|
|
782
|
+
* @return protoObject - The loaded proto object.
|
|
783
|
+
*/
|
|
784
|
+
loadProtoFile(protoPath, config) {
|
|
785
|
+
const protoObjectCacheKey = [config.protosDir, protoPath].join('$');
|
|
786
|
+
if (!GrpcService.protoObjectCache[protoObjectCacheKey]) {
|
|
787
|
+
const services = (0, proto_loader_1.loadSync)(protoPath, {
|
|
788
|
+
keepCase: false,
|
|
789
|
+
defaults: true,
|
|
790
|
+
bytes: String,
|
|
791
|
+
longs: String,
|
|
792
|
+
enums: String,
|
|
793
|
+
oneofs: true,
|
|
794
|
+
includeDirs: [config.protosDir, gaxProtoPath],
|
|
795
|
+
});
|
|
796
|
+
GrpcService.protoObjectCache[protoObjectCacheKey] = services;
|
|
797
|
+
}
|
|
798
|
+
return GrpcService.protoObjectCache[protoObjectCacheKey];
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Retrieves the service object used to make the grpc requests.
|
|
802
|
+
*
|
|
803
|
+
* @private
|
|
804
|
+
*
|
|
805
|
+
* @param {object} protoOpts - The proto options.
|
|
806
|
+
* @return {object} service - The proto service.
|
|
807
|
+
*/
|
|
808
|
+
getService_(protoOpts) {
|
|
809
|
+
const proto = this.protos[protoOpts.service];
|
|
810
|
+
let service = this.activeServiceMap_.get(protoOpts.service);
|
|
811
|
+
if (!service) {
|
|
812
|
+
service = new proto[protoOpts.service](proto.baseUrl || this.baseUrl, this.grpcCredentials, Object.assign({
|
|
813
|
+
'grpc.primary_user_agent': this.userAgent,
|
|
814
|
+
}, GRPC_SERVICE_OPTIONS));
|
|
815
|
+
this.activeServiceMap_.set(protoOpts.service, service);
|
|
816
|
+
}
|
|
817
|
+
return service;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
exports.GrpcService = GrpcService;
|
|
821
|
+
/** A cache for proto objects. */
|
|
822
|
+
GrpcService.protoObjectCache = {};
|
|
823
|
+
GrpcService.GRPC_SERVICE_OPTIONS = GRPC_SERVICE_OPTIONS;
|
|
824
|
+
GrpcService.GRPC_ERROR_CODE_TO_HTTP = GRPC_ERROR_CODE_TO_HTTP;
|
|
825
|
+
GrpcService.ObjectToStructConverter = ObjectToStructConverter;
|
|
826
|
+
//# sourceMappingURL=service.js.map
|