1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.Cluster = void 0;
|
7 | const analyticsexecutor_1 = require("./analyticsexecutor");
|
8 | const analyticsindexmanager_1 = require("./analyticsindexmanager");
|
9 | const binding_1 = __importDefault(require("./binding"));
|
10 | const bindingutilities_1 = require("./bindingutilities");
|
11 | const bucket_1 = require("./bucket");
|
12 | const bucketmanager_1 = require("./bucketmanager");
|
13 | const configProfile_1 = require("./configProfile");
|
14 | const connspec_1 = require("./connspec");
|
15 | const diagnosticsexecutor_1 = require("./diagnosticsexecutor");
|
16 | const eventingfunctionmanager_1 = require("./eventingfunctionmanager");
|
17 | const queryexecutor_1 = require("./queryexecutor");
|
18 | const queryindexmanager_1 = require("./queryindexmanager");
|
19 | const searchexecutor_1 = require("./searchexecutor");
|
20 | const searchindexmanager_1 = require("./searchindexmanager");
|
21 | const transactions_1 = require("./transactions");
|
22 | const transcoders_1 = require("./transcoders");
|
23 | const usermanager_1 = require("./usermanager");
|
24 | const utilities_1 = require("./utilities");
|
25 | const utilities_internal_1 = require("./utilities_internal");
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | class Cluster {
|
34 | |
35 |
|
36 |
|
37 | get conn() {
|
38 | return this._conn;
|
39 | }
|
40 | |
41 |
|
42 |
|
43 | get transcoder() {
|
44 | return this._transcoder;
|
45 | }
|
46 | |
47 |
|
48 |
|
49 | get kvTimeout() {
|
50 | return this._kvTimeout;
|
51 | }
|
52 | |
53 |
|
54 |
|
55 | get kvDurableTimeout() {
|
56 | return this._kvDurableTimeout;
|
57 | }
|
58 | |
59 |
|
60 |
|
61 | get viewTimeout() {
|
62 | return this._viewTimeout;
|
63 | }
|
64 | |
65 |
|
66 |
|
67 | get queryTimeout() {
|
68 | return this._queryTimeout;
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | get analyticsTimeout() {
|
74 | return this._analyticsTimeout;
|
75 | }
|
76 | |
77 |
|
78 |
|
79 | get searchTimeout() {
|
80 | return this._searchTimeout;
|
81 | }
|
82 | |
83 |
|
84 |
|
85 | get managementTimeout() {
|
86 | return this._managementTimeout;
|
87 | }
|
88 | |
89 |
|
90 |
|
91 | get bootstrapTimeout() {
|
92 | return this._bootstrapTimeout;
|
93 | }
|
94 | |
95 |
|
96 |
|
97 | get connectTimeout() {
|
98 | return this._connectTimeout;
|
99 | }
|
100 | |
101 |
|
102 |
|
103 | get resolveTimeout() {
|
104 | return this._resolveTimeout;
|
105 | }
|
106 | |
107 |
|
108 |
|
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 |
|
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 |
|
194 |
|
195 |
|
196 |
|
197 | bucket(bucketName) {
|
198 | if (!this._openBuckets.includes(bucketName)) {
|
199 | this._conn.openBucket(bucketName, (err) => {
|
200 | if (err) {
|
201 |
|
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 |
|
211 |
|
212 |
|
213 | users() {
|
214 | return new usermanager_1.UserManager(this);
|
215 | }
|
216 | |
217 |
|
218 |
|
219 |
|
220 | buckets() {
|
221 | return new bucketmanager_1.BucketManager(this);
|
222 | }
|
223 | |
224 |
|
225 |
|
226 |
|
227 | queryIndexes() {
|
228 | return new queryindexmanager_1.QueryIndexManager(this);
|
229 | }
|
230 | |
231 |
|
232 |
|
233 |
|
234 | analyticsIndexes() {
|
235 | return new analyticsindexmanager_1.AnalyticsIndexManager(this);
|
236 | }
|
237 | |
238 |
|
239 |
|
240 |
|
241 | searchIndexes() {
|
242 | return new searchindexmanager_1.SearchIndexManager(this);
|
243 | }
|
244 | |
245 |
|
246 |
|
247 |
|
248 |
|
249 | eventingFunctions() {
|
250 | return new eventingfunctionmanager_1.EventingFunctionManager(this);
|
251 | }
|
252 | |
253 |
|
254 |
|
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 |
|
264 |
|
265 |
|
266 |
|
267 |
|
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 |
|
283 |
|
284 |
|
285 |
|
286 |
|
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 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
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 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
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 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
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 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
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 |
|
382 |
|
383 |
|
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 |
|
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 |
|
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 |
|
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 | }
|
459 | exports.Cluster = Cluster;
|