UNPKG

16.4 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Cluster = void 0;
7const analyticsexecutor_1 = require("./analyticsexecutor");
8const analyticsindexmanager_1 = require("./analyticsindexmanager");
9const binding_1 = __importDefault(require("./binding"));
10const bindingutilities_1 = require("./bindingutilities");
11const bucket_1 = require("./bucket");
12const bucketmanager_1 = require("./bucketmanager");
13const configProfile_1 = require("./configProfile");
14const connspec_1 = require("./connspec");
15const diagnosticsexecutor_1 = require("./diagnosticsexecutor");
16const eventingfunctionmanager_1 = require("./eventingfunctionmanager");
17const queryexecutor_1 = require("./queryexecutor");
18const queryindexmanager_1 = require("./queryindexmanager");
19const searchexecutor_1 = require("./searchexecutor");
20const searchindexmanager_1 = require("./searchindexmanager");
21const transactions_1 = require("./transactions");
22const transcoders_1 = require("./transcoders");
23const usermanager_1 = require("./usermanager");
24const utilities_1 = require("./utilities");
25const utilities_internal_1 = require("./utilities_internal");
26/**
27 * Exposes the operations which are available to be performed against a cluster.
28 * Namely the ability to access to Buckets as well as performing management
29 * operations against the cluster.
30 *
31 * @category Core
32 */
33class Cluster {
34 /**
35 * @internal
36 */
37 get conn() {
38 return this._conn;
39 }
40 /**
41 @internal
42 */
43 get transcoder() {
44 return this._transcoder;
45 }
46 /**
47 @internal
48 */
49 get kvTimeout() {
50 return this._kvTimeout;
51 }
52 /**
53 @internal
54 */
55 get kvDurableTimeout() {
56 return this._kvDurableTimeout;
57 }
58 /**
59 @internal
60 */
61 get viewTimeout() {
62 return this._viewTimeout;
63 }
64 /**
65 @internal
66 */
67 get queryTimeout() {
68 return this._queryTimeout;
69 }
70 /**
71 @internal
72 */
73 get analyticsTimeout() {
74 return this._analyticsTimeout;
75 }
76 /**
77 @internal
78 */
79 get searchTimeout() {
80 return this._searchTimeout;
81 }
82 /**
83 @internal
84 */
85 get managementTimeout() {
86 return this._managementTimeout;
87 }
88 /**
89 @internal
90 */
91 get bootstrapTimeout() {
92 return this._bootstrapTimeout;
93 }
94 /**
95 @internal
96 */
97 get connectTimeout() {
98 return this._connectTimeout;
99 }
100 /**
101 @internal
102 */
103 get resolveTimeout() {
104 return this._resolveTimeout;
105 }
106 /**
107 @internal
108 @deprecated Use the static sdk-level {@link connect} method instead.
109 */
110 constructor(connStr, options) {
111 var _a, _b, _c;
112 if (!options) {
113 options = {};
114 }
115 if (!options.security) {
116 options.security = {};
117 }
118 if (!options.timeouts) {
119 options.timeouts = {};
120 }
121 this._connStr = connStr;
122 this._trustStorePath = options.security.trustStorePath || '';
123 if (options.configProfile) {
124 configProfile_1.knownProfiles.applyProfile(options.configProfile, options);
125 }
126 this._kvTimeout = options.timeouts.kvTimeout || 2500;
127 this._kvDurableTimeout = options.timeouts.kvDurableTimeout || 10000;
128 this._viewTimeout = options.timeouts.viewTimeout || 75000;
129 this._queryTimeout = options.timeouts.queryTimeout || 75000;
130 this._analyticsTimeout = options.timeouts.analyticsTimeout || 75000;
131 this._searchTimeout = options.timeouts.searchTimeout || 75000;
132 this._managementTimeout = options.timeouts.managementTimeout || 75000;
133 this._bootstrapTimeout = (_a = options.timeouts) === null || _a === void 0 ? void 0 : _a.bootstrapTimeout;
134 this._connectTimeout = (_b = options.timeouts) === null || _b === void 0 ? void 0 : _b.connectTimeout;
135 this._resolveTimeout = (_c = options.timeouts) === null || _c === void 0 ? void 0 : _c.resolveTimeout;
136 if (options.transcoder) {
137 this._transcoder = options.transcoder;
138 }
139 else {
140 this._transcoder = new transcoders_1.DefaultTranscoder();
141 }
142 if (options.transactions) {
143 this._txnConfig = options.transactions;
144 }
145 else {
146 this._txnConfig = {};
147 }
148 if (options.username || options.password) {
149 if (options.authenticator) {
150 throw new Error('Cannot specify authenticator along with username/password.');
151 }
152 this._auth = {
153 username: options.username || '',
154 password: options.password || '',
155 };
156 }
157 else if (options.authenticator) {
158 this._auth = options.authenticator;
159 }
160 else {
161 this._auth = {
162 username: '',
163 password: '',
164 };
165 }
166 if (options.dnsConfig &&
167 (options.dnsConfig.nameserver ||
168 options.dnsConfig.port ||
169 options.dnsConfig.dnsSrvTimeout)) {
170 this._dnsConfig = {
171 nameserver: options.dnsConfig.nameserver,
172 port: options.dnsConfig.port,
173 dnsSrvTimeout: options.dnsConfig.dnsSrvTimeout || 500,
174 };
175 }
176 else {
177 this._dnsConfig = null;
178 }
179 this._openBuckets = [];
180 this._conn = new binding_1.default.Connection();
181 }
182 /**
183 @internal
184 */
185 static async connect(connStr, options, callback) {
186 return utilities_1.PromiseHelper.wrapAsync(async () => {
187 const cluster = new Cluster(connStr, options);
188 await cluster._connect();
189 return cluster;
190 }, callback);
191 }
192 /**
193 * Creates a Bucket object reference to a specific bucket.
194 *
195 * @param bucketName The name of the bucket to reference.
196 */
197 bucket(bucketName) {
198 if (!this._openBuckets.includes(bucketName)) {
199 this._conn.openBucket(bucketName, (err) => {
200 if (err) {
201 // BUG(JSCBC-1011): Move this to log framework once it is implemented.
202 console.error('failed to open bucket: %O', err);
203 }
204 });
205 this._openBuckets.push(bucketName);
206 }
207 return new bucket_1.Bucket(this, bucketName);
208 }
209 /**
210 * Returns a UserManager which can be used to manage the users
211 * of this cluster.
212 */
213 users() {
214 return new usermanager_1.UserManager(this);
215 }
216 /**
217 * Returns a BucketManager which can be used to manage the buckets
218 * of this cluster.
219 */
220 buckets() {
221 return new bucketmanager_1.BucketManager(this);
222 }
223 /**
224 * Returns a QueryIndexManager which can be used to manage the query indexes
225 * of this cluster.
226 */
227 queryIndexes() {
228 return new queryindexmanager_1.QueryIndexManager(this);
229 }
230 /**
231 * Returns a AnalyticsIndexManager which can be used to manage the analytics
232 * indexes of this cluster.
233 */
234 analyticsIndexes() {
235 return new analyticsindexmanager_1.AnalyticsIndexManager(this);
236 }
237 /**
238 * Returns a SearchIndexManager which can be used to manage the search
239 * indexes of this cluster.
240 */
241 searchIndexes() {
242 return new searchindexmanager_1.SearchIndexManager(this);
243 }
244 /**
245 * Returns a EventingFunctionManager which can be used to manage the eventing
246 * functions of this cluster.
247 * Uncommitted: This API is subject to change in the future.
248 */
249 eventingFunctions() {
250 return new eventingfunctionmanager_1.EventingFunctionManager(this);
251 }
252 /**
253 * Returns a Transactions object which can be used to perform transactions
254 * on this cluster.
255 */
256 transactions() {
257 if (!this._transactions) {
258 this._transactions = new transactions_1.Transactions(this, this._txnConfig);
259 }
260 return this._transactions;
261 }
262 /**
263 * Executes a N1QL query against the cluster.
264 *
265 * @param statement The N1QL statement to execute.
266 * @param options Optional parameters for this operation.
267 * @param callback A node-style callback to be invoked after execution.
268 */
269 query(statement, options, callback) {
270 if (options instanceof Function) {
271 callback = arguments[1];
272 options = undefined;
273 }
274 if (!options) {
275 options = {};
276 }
277 const exec = new queryexecutor_1.QueryExecutor(this);
278 const options_ = options;
279 return utilities_1.PromiseHelper.wrapAsync(() => exec.query(statement, options_), callback);
280 }
281 /**
282 * Executes an analytics query against the cluster.
283 *
284 * @param statement The analytics statement to execute.
285 * @param options Optional parameters for this operation.
286 * @param callback A node-style callback to be invoked after execution.
287 */
288 analyticsQuery(statement, options, callback) {
289 if (options instanceof Function) {
290 callback = arguments[1];
291 options = undefined;
292 }
293 if (!options) {
294 options = {};
295 }
296 const exec = new analyticsexecutor_1.AnalyticsExecutor(this);
297 const options_ = options;
298 return utilities_1.PromiseHelper.wrapAsync(() => exec.query(statement, options_), callback);
299 }
300 /**
301 * Executes a search query against the cluster.
302 *
303 * @param indexName The name of the index to query.
304 * @param query The SearchQuery describing the query to execute.
305 * @param options Optional parameters for this operation.
306 * @param callback A node-style callback to be invoked after execution.
307 */
308 searchQuery(indexName, query, options, callback) {
309 if (options instanceof Function) {
310 callback = arguments[2];
311 options = undefined;
312 }
313 if (!options) {
314 options = {};
315 }
316 const exec = new searchexecutor_1.SearchExecutor(this);
317 const options_ = options;
318 return utilities_1.PromiseHelper.wrapAsync(() => exec.query(indexName, query, options_), callback);
319 }
320 /**
321 * Executes a search query against the cluster.
322 *
323 * @param indexName The name of the index to query.
324 * @param request The SearchRequest describing the search to execute.
325 * @param options Optional parameters for this operation.
326 * @param callback A node-style callback to be invoked after execution.
327 */
328 search(indexName, request, options, callback) {
329 if (options instanceof Function) {
330 callback = arguments[2];
331 options = undefined;
332 }
333 if (!options) {
334 options = {};
335 }
336 const exec = new searchexecutor_1.SearchExecutor(this);
337 const options_ = options;
338 return utilities_1.PromiseHelper.wrapAsync(() => exec.query(indexName, request, options_), callback);
339 }
340 /**
341 * Returns a diagnostics report about the currently active connections with the
342 * cluster. Includes information about remote and local addresses, last activity,
343 * and other diagnostics information.
344 *
345 * @param options Optional parameters for this operation.
346 * @param callback A node-style callback to be invoked after execution.
347 */
348 diagnostics(options, callback) {
349 if (options instanceof Function) {
350 callback = arguments[0];
351 options = undefined;
352 }
353 if (!options) {
354 options = {};
355 }
356 const exec = new diagnosticsexecutor_1.DiagnoticsExecutor(this);
357 const options_ = options;
358 return utilities_1.PromiseHelper.wrapAsync(() => exec.diagnostics(options_), callback);
359 }
360 /**
361 * Performs a ping operation against the cluster. Pinging the services which
362 * are specified (or all services if none are specified). Returns a report
363 * which describes the outcome of the ping operations which were performed.
364 *
365 * @param options Optional parameters for this operation.
366 * @param callback A node-style callback to be invoked after execution.
367 */
368 ping(options, callback) {
369 if (options instanceof Function) {
370 callback = arguments[0];
371 options = undefined;
372 }
373 if (!options) {
374 options = {};
375 }
376 const exec = new diagnosticsexecutor_1.PingExecutor(this);
377 const options_ = options;
378 return utilities_1.PromiseHelper.wrapAsync(() => exec.ping(options_), callback);
379 }
380 /**
381 * Shuts down this cluster object. Cleaning up all resources associated with it.
382 *
383 * @param callback A node-style callback to be invoked after execution.
384 */
385 async close(callback) {
386 if (this._transactions) {
387 await this._transactions._close();
388 this._transactions = undefined;
389 }
390 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
391 this._conn.shutdown((cppErr) => {
392 wrapCallback((0, bindingutilities_1.errorFromCpp)(cppErr));
393 });
394 }, callback);
395 }
396 async _connect() {
397 return new Promise((resolve, reject) => {
398 const dsnObj = connspec_1.ConnSpec.parse(this._connStr);
399 dsnObj.options.user_agent_extra = (0, utilities_internal_1.generateClientString)();
400 //trust_store_path is legacy, C++ SDK expects trust_certificate
401 if ('trust_store_path' in dsnObj.options &&
402 !('trust_certificate' in dsnObj.options)) {
403 dsnObj.options.trust_certificate = dsnObj.options.trust_store_path;
404 delete dsnObj.options['trust_store_path'];
405 }
406 //if trust store was passed in via `SecurityConfig` override connstr
407 if (this._trustStorePath) {
408 dsnObj.options.trust_certificate = this._trustStorePath;
409 }
410 if (this.bootstrapTimeout) {
411 dsnObj.options['bootstrap_timeout'] = this.bootstrapTimeout.toString();
412 }
413 if (this.connectTimeout) {
414 dsnObj.options['kv_connect_timeout'] = this.connectTimeout.toString();
415 }
416 if (this.resolveTimeout) {
417 dsnObj.options['resolve_timeout'] = this.resolveTimeout.toString();
418 }
419 const connStr = dsnObj.toString();
420 const authOpts = {};
421 // lets allow `allowed_sasl_mechanisms` to override legacy connstr option
422 for (const saslKey of ['sasl_mech_force', 'allowed_sasl_mechanisms']) {
423 if (!(saslKey in dsnObj.options)) {
424 continue;
425 }
426 if (typeof dsnObj.options[saslKey] === 'string') {
427 authOpts.allowed_sasl_mechanisms = [dsnObj.options[saslKey]];
428 }
429 else {
430 authOpts.allowed_sasl_mechanisms = dsnObj.options[saslKey];
431 }
432 delete dsnObj.options[saslKey];
433 }
434 if (this._auth) {
435 const passAuth = this._auth;
436 if (passAuth.username || passAuth.password) {
437 authOpts.username = passAuth.username;
438 authOpts.password = passAuth.password;
439 if (passAuth.allowed_sasl_mechanisms) {
440 authOpts.allowed_sasl_mechanisms = passAuth.allowed_sasl_mechanisms;
441 }
442 }
443 const certAuth = this._auth;
444 if (certAuth.certificatePath || certAuth.keyPath) {
445 authOpts.certificate_path = certAuth.certificatePath;
446 authOpts.key_path = certAuth.keyPath;
447 }
448 }
449 this._conn.connect(connStr, authOpts, this._dnsConfig, (cppErr) => {
450 if (cppErr) {
451 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
452 return reject(err);
453 }
454 resolve(null);
455 });
456 });
457 }
458}
459exports.Cluster = Cluster;