@qp-mongosh/shell-api 0.0.0-dev.5 → 0.0.0-dev.7

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.
Files changed (72) hide show
  1. package/.eslintignore +2 -2
  2. package/.eslintrc.js +1 -1
  3. package/AUTHORS +15 -15
  4. package/LICENSE +200 -200
  5. package/bin/report-missing-help.ts +24 -24
  6. package/bin/report-supported-api.ts +14 -14
  7. package/lib/abstract-cursor.d.ts +33 -33
  8. package/lib/abstract-cursor.js +191 -191
  9. package/lib/aggregation-cursor.d.ts +6 -6
  10. package/lib/aggregation-cursor.js +19 -19
  11. package/lib/bulk.d.ts +43 -43
  12. package/lib/bulk.js +223 -223
  13. package/lib/change-stream-cursor.d.ts +28 -28
  14. package/lib/change-stream-cursor.js +111 -111
  15. package/lib/collection.d.ts +95 -95
  16. package/lib/collection.js +964 -964
  17. package/lib/cursor.d.ts +32 -32
  18. package/lib/cursor.js +215 -215
  19. package/lib/database.d.ts +116 -116
  20. package/lib/database.js +1223 -1223
  21. package/lib/dbquery.d.ts +8 -8
  22. package/lib/dbquery.js +28 -28
  23. package/lib/decorators.d.ts +73 -73
  24. package/lib/decorators.js +395 -395
  25. package/lib/deprecation-warning.d.ts +2 -0
  26. package/lib/deprecation-warning.js +19 -0
  27. package/lib/deprecation-warning.js.map +1 -0
  28. package/lib/enums.d.ts +28 -28
  29. package/lib/enums.js +33 -33
  30. package/lib/error-codes.d.ts +12 -12
  31. package/lib/error-codes.js +19 -19
  32. package/lib/explainable-cursor.d.ts +11 -11
  33. package/lib/explainable-cursor.js +31 -31
  34. package/lib/explainable.d.ts +32 -32
  35. package/lib/explainable.js +166 -166
  36. package/lib/field-level-encryption.d.ts +50 -50
  37. package/lib/field-level-encryption.js +176 -176
  38. package/lib/help.d.ts +22 -22
  39. package/lib/help.js +26 -26
  40. package/lib/helpers.d.ts +71 -71
  41. package/lib/helpers.js +588 -588
  42. package/lib/index.d.ts +16 -16
  43. package/lib/index.js +45 -45
  44. package/lib/interruptor.d.ts +19 -19
  45. package/lib/interruptor.js +62 -62
  46. package/lib/mongo-errors.d.ts +5 -5
  47. package/lib/mongo-errors.js +37 -37
  48. package/lib/mongo.d.ts +75 -75
  49. package/lib/mongo.js +476 -476
  50. package/lib/no-db.d.ts +5 -5
  51. package/lib/no-db.js +28 -28
  52. package/lib/plan-cache.d.ts +16 -16
  53. package/lib/plan-cache.js +70 -70
  54. package/lib/replica-set.d.ts +45 -45
  55. package/lib/replica-set.js +314 -314
  56. package/lib/result.d.ts +61 -61
  57. package/lib/result.js +104 -104
  58. package/lib/session.d.ts +25 -25
  59. package/lib/session.js +88 -88
  60. package/lib/shard.d.ts +42 -42
  61. package/lib/shard.js +414 -414
  62. package/lib/shell-api.d.ts +52 -52
  63. package/lib/shell-api.js +298 -298
  64. package/lib/shell-bson.d.ts +40 -40
  65. package/lib/shell-bson.js +159 -159
  66. package/lib/shell-instance-state.d.ts +77 -77
  67. package/lib/shell-instance-state.js +392 -392
  68. package/lib/shell-internal-state.d.ts +74 -0
  69. package/lib/shell-internal-state.js +364 -0
  70. package/lib/shell-internal-state.js.map +1 -0
  71. package/package.json +47 -47
  72. package/tsconfig.lint.json +8 -8
@@ -1,393 +1,393 @@
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
- const errors_1 = require("@qp-mongosh/errors");
7
- const service_provider_core_1 = require("@qp-mongosh/service-provider-core");
8
- const events_1 = require("events");
9
- const mongodb_redact_1 = __importDefault(require("mongodb-redact"));
10
- const decorators_1 = require("./decorators");
11
- const enums_1 = require("./enums");
12
- const error_codes_1 = require("./error-codes");
13
- const index_1 = require("./index");
14
- const interruptor_1 = require("./interruptor");
15
- const mongo_errors_1 = require("./mongo-errors");
16
- const no_db_1 = __importDefault(require("./no-db"));
17
- const shell_bson_1 = __importDefault(require("./shell-bson"));
18
- class ShellInstanceState {
19
- constructor(initialServiceProvider, messageBus = new events_1.EventEmitter(), cliOptions = {}) {
20
- this.displayBatchSizeFromDBQuery = undefined;
21
- this.isInteractive = false;
22
- this.apiCallDepth = 0;
23
- this.warningsShown = new Set();
24
- this.interrupted = new interruptor_1.InterruptFlag();
25
- this.plugins = [new mongo_errors_1.TransformMongoErrorPlugin()];
26
- this.alreadyTransformedErrors = new WeakMap();
27
- this.initialServiceProvider = initialServiceProvider;
28
- this.messageBus = messageBus;
29
- this.shellApi = new index_1.ShellApi(this);
30
- this.shellBson = (0, shell_bson_1.default)(initialServiceProvider.bsonLibrary, (msg) => {
31
- void this.shellApi.print(`Warning: ${msg}`);
32
- });
33
- this.mongos = [];
34
- this.connectionInfo = { buildInfo: {} };
35
- if (!cliOptions.nodb) {
36
- const mongo = new index_1.Mongo(this, undefined, undefined, undefined, initialServiceProvider);
37
- this.mongos.push(mongo);
38
- this.currentDb = mongo.getDB(initialServiceProvider.initialDb || service_provider_core_1.DEFAULT_DB);
39
- }
40
- else {
41
- this.currentDb = new no_db_1.default();
42
- }
43
- this.currentCursor = null;
44
- this.context = {};
45
- this.cliOptions = cliOptions;
46
- this.evaluationListener = {};
47
- }
48
- async fetchConnectionInfo() {
49
- if (!this.cliOptions.nodb) {
50
- this.connectionInfo = await this.currentServiceProvider.getConnectionInfo();
51
- const apiVersionInfo = this.apiVersionInfo();
52
- this.messageBus.emit('mongosh:connect', {
53
- ...this.connectionInfo.extraInfo,
54
- api_version: apiVersionInfo === null || apiVersionInfo === void 0 ? void 0 : apiVersionInfo.version,
55
- api_strict: apiVersionInfo === null || apiVersionInfo === void 0 ? void 0 : apiVersionInfo.strict,
56
- api_deprecation_errors: apiVersionInfo === null || apiVersionInfo === void 0 ? void 0 : apiVersionInfo.deprecationErrors,
57
- uri: (0, mongodb_redact_1.default)(this.connectionInfo.extraInfo.uri)
58
- });
59
- }
60
- }
61
- async close(force) {
62
- for (const mongo of [...this.mongos]) {
63
- await mongo.close(force);
64
- }
65
- }
66
- setDbFunc(newDb) {
67
- this.currentDb = newDb;
68
- this.context.rs = new index_1.ReplicaSet(this.currentDb);
69
- this.context.sh = new index_1.Shard(this.currentDb);
70
- this.fetchConnectionInfo().catch(err => this.messageBus.emit('mongosh:error', err, 'shell-api'));
71
- this.currentDb._getCollectionNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err, 'shell-api'));
72
- this.currentDb._mongo._getDatabaseNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err, 'shell-api'));
73
- this.currentCursor = null;
74
- return newDb;
75
- }
76
- setCtx(contextObject) {
77
- this.context = contextObject;
78
- Object.assign(contextObject, this.shellApi);
79
- for (const name of Object.getOwnPropertyNames(index_1.ShellApi.prototype)) {
80
- const { shellApi } = this;
81
- if (decorators_1.toIgnore.concat(['help']).includes(name) ||
82
- typeof shellApi[name] !== 'function') {
83
- continue;
84
- }
85
- contextObject[name] = function (...args) {
86
- return shellApi[name](...args);
87
- };
88
- contextObject[name].help = shellApi[name].help;
89
- }
90
- contextObject.help = this.shellApi.help;
91
- Object.assign(contextObject, this.shellBson);
92
- if (contextObject.console === undefined) {
93
- contextObject.console = {};
94
- }
95
- for (const key of ['log', 'warn', 'info', 'error']) {
96
- contextObject.console[key] = (...args) => {
97
- return contextObject.print(...args);
98
- };
99
- }
100
- contextObject.console.clear = contextObject.cls;
101
- contextObject.rs = new index_1.ReplicaSet(this.currentDb);
102
- contextObject.sh = new index_1.Shard(this.currentDb);
103
- const setFunc = (newDb) => {
104
- if ((0, index_1.getShellApiType)(newDb) !== 'Database') {
105
- throw new errors_1.MongoshInvalidInputError('Cannot reassign \'db\' to non-Database type', errors_1.CommonErrors.InvalidOperation);
106
- }
107
- return this.setDbFunc(newDb);
108
- };
109
- if (this.initialServiceProvider.platform === service_provider_core_1.ReplPlatform.JavaShell) {
110
- contextObject.db = this.setDbFunc(this.currentDb);
111
- }
112
- else {
113
- Object.defineProperty(contextObject, 'db', {
114
- configurable: true,
115
- set: setFunc,
116
- get: () => (this.currentDb)
117
- });
118
- }
119
- this.messageBus.emit('mongosh:setCtx', { method: 'setCtx', arguments: {} });
120
- }
121
- get currentServiceProvider() {
122
- try {
123
- return this.currentDb._mongo._serviceProvider;
124
- }
125
- catch (err) {
126
- if ((err === null || err === void 0 ? void 0 : err.code) === error_codes_1.ShellApiErrors.NotConnected) {
127
- return this.initialServiceProvider;
128
- }
129
- throw err;
130
- }
131
- }
132
- emitApiCallWithArgs(event) {
133
- this.messageBus.emit('mongosh:api-call-with-arguments', event);
134
- }
135
- emitApiCall(event) {
136
- this.messageBus.emit('mongosh:api-call', {
137
- ...event,
138
- callDepth: this.apiCallDepth
139
- });
140
- }
141
- setEvaluationListener(listener) {
142
- this.evaluationListener = listener;
143
- }
144
- getAutocompleteParameters() {
145
- return {
146
- topology: () => {
147
- var _a;
148
- let topology;
149
- const topologyDescription = (_a = this.currentServiceProvider.getTopology()) === null || _a === void 0 ? void 0 : _a.description;
150
- const topologyType = topologyDescription === null || topologyDescription === void 0 ? void 0 : topologyDescription.type;
151
- switch (topologyType) {
152
- case 'ReplicaSetNoPrimary':
153
- case 'ReplicaSetWithPrimary':
154
- topology = enums_1.Topologies.ReplSet;
155
- break;
156
- case 'Sharded':
157
- topology = enums_1.Topologies.Sharded;
158
- break;
159
- case 'LoadBalanced':
160
- topology = enums_1.Topologies.LoadBalanced;
161
- break;
162
- default:
163
- topology = enums_1.Topologies.Standalone;
164
- if (topologyDescription.servers.size === 1) {
165
- const [server] = topologyDescription.servers.values();
166
- switch (server.type) {
167
- case 'Mongos':
168
- topology = enums_1.Topologies.Sharded;
169
- break;
170
- case 'PossiblePrimary':
171
- case 'RSPrimary':
172
- case 'RSSecondary':
173
- case 'RSArbiter':
174
- case 'RSOther':
175
- case 'RSGhost':
176
- topology = enums_1.Topologies.ReplSet;
177
- break;
178
- default:
179
- break;
180
- }
181
- }
182
- break;
183
- }
184
- return topology;
185
- },
186
- apiVersionInfo: () => {
187
- return this.apiVersionInfo();
188
- },
189
- connectionInfo: () => {
190
- return this.connectionInfo.extraInfo;
191
- },
192
- getCollectionCompletionsForCurrentDb: async (collName) => {
193
- try {
194
- const collectionNames = await this.currentDb._getCollectionNamesForCompletion();
195
- return collectionNames.filter((name) => name.toLowerCase().startsWith(collName.toLowerCase()));
196
- }
197
- catch (err) {
198
- if ((err === null || err === void 0 ? void 0 : err.code) === error_codes_1.ShellApiErrors.NotConnected ||
199
- (err === null || err === void 0 ? void 0 : err.codeName) === 'Unauthorized') {
200
- return [];
201
- }
202
- throw err;
203
- }
204
- },
205
- getDatabaseCompletions: async (dbName) => {
206
- try {
207
- const dbNames = await this.currentDb._mongo._getDatabaseNamesForCompletion();
208
- return dbNames.filter((name) => name.toLowerCase().startsWith(dbName.toLowerCase()));
209
- }
210
- catch (err) {
211
- if ((err === null || err === void 0 ? void 0 : err.code) === error_codes_1.ShellApiErrors.NotConnected ||
212
- (err === null || err === void 0 ? void 0 : err.codeName) === 'Unauthorized') {
213
- return [];
214
- }
215
- throw err;
216
- }
217
- }
218
- };
219
- }
220
- apiVersionInfo() {
221
- var _a, _b;
222
- const { serverApi } = (_b = (_a = this.currentServiceProvider.getRawClient()) === null || _a === void 0 ? void 0 : _a.options) !== null && _b !== void 0 ? _b : {};
223
- return (serverApi === null || serverApi === void 0 ? void 0 : serverApi.version) ? { strict: false, deprecationErrors: false, ...serverApi } : undefined;
224
- }
225
- async onInterruptExecution() {
226
- this.interrupted.set();
227
- this.currentCursor = null;
228
- this.resumeMongosAfterInterrupt = await Promise.all(this.mongos.map(async (m) => {
229
- try {
230
- return {
231
- mongo: m,
232
- resume: await m._suspend()
233
- };
234
- }
235
- catch (e) {
236
- return {
237
- mongo: m,
238
- resume: null
239
- };
240
- }
241
- }));
242
- return !this.resumeMongosAfterInterrupt.find(r => r.resume === null);
243
- }
244
- async onResumeExecution() {
245
- var _a, _b;
246
- const promises = (_b = (_a = this.resumeMongosAfterInterrupt) === null || _a === void 0 ? void 0 : _a.map(async (r) => {
247
- if (!this.mongos.find(m => m === r.mongo)) {
248
- return true;
249
- }
250
- if (r.resume === null) {
251
- return false;
252
- }
253
- try {
254
- await r.resume();
255
- return true;
256
- }
257
- catch (e) {
258
- return false;
259
- }
260
- })) !== null && _b !== void 0 ? _b : [];
261
- this.resumeMongosAfterInterrupt = undefined;
262
- const result = await Promise.all(promises);
263
- this.interrupted.reset();
264
- return !result.find(r => r === false);
265
- }
266
- async getDefaultPrompt() {
267
- const prefix = this.getDefaultPromptPrefix();
268
- const topologyInfo = this.getTopologySpecificPrompt();
269
- let dbname = '';
270
- try {
271
- dbname = this.currentDb.getName();
272
- }
273
- catch (_a) { }
274
- return `${[prefix, topologyInfo, dbname].filter(Boolean).join(' ')}> `;
275
- }
276
- getDefaultPromptPrefix() {
277
- var _a, _b, _c, _d;
278
- const extraConnectionInfo = (_a = this.connectionInfo) === null || _a === void 0 ? void 0 : _a.extraInfo;
279
- if (extraConnectionInfo === null || extraConnectionInfo === void 0 ? void 0 : extraConnectionInfo.is_data_lake) {
280
- return 'AtlasDataLake';
281
- }
282
- else if (extraConnectionInfo === null || extraConnectionInfo === void 0 ? void 0 : extraConnectionInfo.is_atlas) {
283
- return 'Atlas';
284
- }
285
- else if ((extraConnectionInfo === null || extraConnectionInfo === void 0 ? void 0 : extraConnectionInfo.is_enterprise) || ((_d = (_c = (_b = this.connectionInfo) === null || _b === void 0 ? void 0 : _b.buildInfo) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.indexOf('enterprise')) >= 0) {
286
- return 'Enterprise';
287
- }
288
- return '';
289
- }
290
- getTopologySpecificPrompt() {
291
- var _a, _b, _c, _d;
292
- const description = (_a = this.currentServiceProvider.getTopology()) === null || _a === void 0 ? void 0 : _a.description;
293
- if (!description) {
294
- return '';
295
- }
296
- let replicaSet = description.setName;
297
- let serverTypePrompt = '';
298
- switch (description.type) {
299
- case 'Single':
300
- const singleDetails = this.getTopologySinglePrompt(description);
301
- replicaSet = (_b = singleDetails === null || singleDetails === void 0 ? void 0 : singleDetails.replicaSet) !== null && _b !== void 0 ? _b : replicaSet;
302
- serverTypePrompt = (singleDetails === null || singleDetails === void 0 ? void 0 : singleDetails.serverType) ? `[direct: ${singleDetails.serverType}]` : '';
303
- break;
304
- case 'ReplicaSetNoPrimary':
305
- serverTypePrompt = '[secondary]';
306
- break;
307
- case 'ReplicaSetWithPrimary':
308
- serverTypePrompt = '[primary]';
309
- break;
310
- case 'Sharded':
311
- serverTypePrompt = ((_d = (_c = this.connectionInfo) === null || _c === void 0 ? void 0 : _c.extraInfo) === null || _d === void 0 ? void 0 : _d.atlas_version) ? '' : '[mongos]';
312
- break;
313
- case 'LoadBalanced':
314
- default:
315
- return '';
316
- }
317
- const setNamePrefix = replicaSet ? `${replicaSet} ` : '';
318
- return `${setNamePrefix}${serverTypePrompt}`;
319
- }
320
- getTopologySinglePrompt(description) {
321
- var _a;
322
- if (((_a = description.servers) === null || _a === void 0 ? void 0 : _a.size) !== 1) {
323
- return undefined;
324
- }
325
- const [server] = description.servers.values();
326
- let serverType;
327
- switch (server.type) {
328
- case 'Mongos':
329
- serverType = 'mongos';
330
- break;
331
- case 'RSPrimary':
332
- serverType = 'primary';
333
- break;
334
- case 'RSSecondary':
335
- serverType = 'secondary';
336
- break;
337
- case 'RSArbiter':
338
- serverType = 'arbiter';
339
- break;
340
- case 'RSOther':
341
- serverType = 'other';
342
- break;
343
- default:
344
- serverType = '';
345
- }
346
- return {
347
- replicaSet: server.setName,
348
- serverType
349
- };
350
- }
351
- registerPlugin(plugin) {
352
- this.plugins.push(plugin);
353
- }
354
- transformError(err) {
355
- if (Object.prototype.toString.call(err) === '[object Error]') {
356
- if (this.alreadyTransformedErrors.has(err)) {
357
- return this.alreadyTransformedErrors.get(err);
358
- }
359
- const before = err;
360
- for (const plugin of this.plugins) {
361
- if (plugin.transformError) {
362
- err = plugin.transformError(err);
363
- }
364
- }
365
- this.alreadyTransformedErrors.set(before, err);
366
- }
367
- return err;
368
- }
369
- async printDeprecationWarning(message) {
370
- if (!this.warningsShown.has(message)) {
371
- this.warningsShown.add(message);
372
- if (this.context.print) {
373
- await this.context.print.call(this.shellApi, `DeprecationWarning: ${message}`);
374
- }
375
- else {
376
- await this.shellApi.print(`DeprecationWarning: ${message}`);
377
- }
378
- }
379
- }
380
- async printWarning(message) {
381
- if (!this.warningsShown.has(message)) {
382
- this.warningsShown.add(message);
383
- if (this.context.print) {
384
- await this.context.print.call(this.shellApi, `Warning: ${message}`);
385
- }
386
- else {
387
- await this.shellApi.print(`Warning: ${message}`);
388
- }
389
- }
390
- }
391
- }
392
- exports.default = ShellInstanceState;
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
+ const errors_1 = require("@qp-mongosh/errors");
7
+ const service_provider_core_1 = require("@qp-mongosh/service-provider-core");
8
+ const events_1 = require("events");
9
+ const mongodb_redact_1 = __importDefault(require("mongodb-redact"));
10
+ const decorators_1 = require("./decorators");
11
+ const enums_1 = require("./enums");
12
+ const error_codes_1 = require("./error-codes");
13
+ const index_1 = require("./index");
14
+ const interruptor_1 = require("./interruptor");
15
+ const mongo_errors_1 = require("./mongo-errors");
16
+ const no_db_1 = __importDefault(require("./no-db"));
17
+ const shell_bson_1 = __importDefault(require("./shell-bson"));
18
+ class ShellInstanceState {
19
+ constructor(initialServiceProvider, messageBus = new events_1.EventEmitter(), cliOptions = {}) {
20
+ this.displayBatchSizeFromDBQuery = undefined;
21
+ this.isInteractive = false;
22
+ this.apiCallDepth = 0;
23
+ this.warningsShown = new Set();
24
+ this.interrupted = new interruptor_1.InterruptFlag();
25
+ this.plugins = [new mongo_errors_1.TransformMongoErrorPlugin()];
26
+ this.alreadyTransformedErrors = new WeakMap();
27
+ this.initialServiceProvider = initialServiceProvider;
28
+ this.messageBus = messageBus;
29
+ this.shellApi = new index_1.ShellApi(this);
30
+ this.shellBson = (0, shell_bson_1.default)(initialServiceProvider.bsonLibrary, (msg) => {
31
+ void this.shellApi.print(`Warning: ${msg}`);
32
+ });
33
+ this.mongos = [];
34
+ this.connectionInfo = { buildInfo: {} };
35
+ if (!cliOptions.nodb) {
36
+ const mongo = new index_1.Mongo(this, undefined, undefined, undefined, initialServiceProvider);
37
+ this.mongos.push(mongo);
38
+ this.currentDb = mongo.getDB(initialServiceProvider.initialDb || service_provider_core_1.DEFAULT_DB);
39
+ }
40
+ else {
41
+ this.currentDb = new no_db_1.default();
42
+ }
43
+ this.currentCursor = null;
44
+ this.context = {};
45
+ this.cliOptions = cliOptions;
46
+ this.evaluationListener = {};
47
+ }
48
+ async fetchConnectionInfo() {
49
+ if (!this.cliOptions.nodb) {
50
+ this.connectionInfo = await this.currentServiceProvider.getConnectionInfo();
51
+ const apiVersionInfo = this.apiVersionInfo();
52
+ this.messageBus.emit('mongosh:connect', {
53
+ ...this.connectionInfo.extraInfo,
54
+ api_version: apiVersionInfo === null || apiVersionInfo === void 0 ? void 0 : apiVersionInfo.version,
55
+ api_strict: apiVersionInfo === null || apiVersionInfo === void 0 ? void 0 : apiVersionInfo.strict,
56
+ api_deprecation_errors: apiVersionInfo === null || apiVersionInfo === void 0 ? void 0 : apiVersionInfo.deprecationErrors,
57
+ uri: (0, mongodb_redact_1.default)(this.connectionInfo.extraInfo.uri)
58
+ });
59
+ }
60
+ }
61
+ async close(force) {
62
+ for (const mongo of [...this.mongos]) {
63
+ await mongo.close(force);
64
+ }
65
+ }
66
+ setDbFunc(newDb) {
67
+ this.currentDb = newDb;
68
+ this.context.rs = new index_1.ReplicaSet(this.currentDb);
69
+ this.context.sh = new index_1.Shard(this.currentDb);
70
+ this.fetchConnectionInfo().catch(err => this.messageBus.emit('mongosh:error', err, 'shell-api'));
71
+ this.currentDb._getCollectionNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err, 'shell-api'));
72
+ this.currentDb._mongo._getDatabaseNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err, 'shell-api'));
73
+ this.currentCursor = null;
74
+ return newDb;
75
+ }
76
+ setCtx(contextObject) {
77
+ this.context = contextObject;
78
+ Object.assign(contextObject, this.shellApi);
79
+ for (const name of Object.getOwnPropertyNames(index_1.ShellApi.prototype)) {
80
+ const { shellApi } = this;
81
+ if (decorators_1.toIgnore.concat(['help']).includes(name) ||
82
+ typeof shellApi[name] !== 'function') {
83
+ continue;
84
+ }
85
+ contextObject[name] = function (...args) {
86
+ return shellApi[name](...args);
87
+ };
88
+ contextObject[name].help = shellApi[name].help;
89
+ }
90
+ contextObject.help = this.shellApi.help;
91
+ Object.assign(contextObject, this.shellBson);
92
+ if (contextObject.console === undefined) {
93
+ contextObject.console = {};
94
+ }
95
+ for (const key of ['log', 'warn', 'info', 'error']) {
96
+ contextObject.console[key] = (...args) => {
97
+ return contextObject.print(...args);
98
+ };
99
+ }
100
+ contextObject.console.clear = contextObject.cls;
101
+ contextObject.rs = new index_1.ReplicaSet(this.currentDb);
102
+ contextObject.sh = new index_1.Shard(this.currentDb);
103
+ const setFunc = (newDb) => {
104
+ if ((0, index_1.getShellApiType)(newDb) !== 'Database') {
105
+ throw new errors_1.MongoshInvalidInputError('Cannot reassign \'db\' to non-Database type', errors_1.CommonErrors.InvalidOperation);
106
+ }
107
+ return this.setDbFunc(newDb);
108
+ };
109
+ if (this.initialServiceProvider.platform === service_provider_core_1.ReplPlatform.JavaShell) {
110
+ contextObject.db = this.setDbFunc(this.currentDb);
111
+ }
112
+ else {
113
+ Object.defineProperty(contextObject, 'db', {
114
+ configurable: true,
115
+ set: setFunc,
116
+ get: () => (this.currentDb)
117
+ });
118
+ }
119
+ this.messageBus.emit('mongosh:setCtx', { method: 'setCtx', arguments: {} });
120
+ }
121
+ get currentServiceProvider() {
122
+ try {
123
+ return this.currentDb._mongo._serviceProvider;
124
+ }
125
+ catch (err) {
126
+ if ((err === null || err === void 0 ? void 0 : err.code) === error_codes_1.ShellApiErrors.NotConnected) {
127
+ return this.initialServiceProvider;
128
+ }
129
+ throw err;
130
+ }
131
+ }
132
+ emitApiCallWithArgs(event) {
133
+ this.messageBus.emit('mongosh:api-call-with-arguments', event);
134
+ }
135
+ emitApiCall(event) {
136
+ this.messageBus.emit('mongosh:api-call', {
137
+ ...event,
138
+ callDepth: this.apiCallDepth
139
+ });
140
+ }
141
+ setEvaluationListener(listener) {
142
+ this.evaluationListener = listener;
143
+ }
144
+ getAutocompleteParameters() {
145
+ return {
146
+ topology: () => {
147
+ var _a;
148
+ let topology;
149
+ const topologyDescription = (_a = this.currentServiceProvider.getTopology()) === null || _a === void 0 ? void 0 : _a.description;
150
+ const topologyType = topologyDescription === null || topologyDescription === void 0 ? void 0 : topologyDescription.type;
151
+ switch (topologyType) {
152
+ case 'ReplicaSetNoPrimary':
153
+ case 'ReplicaSetWithPrimary':
154
+ topology = enums_1.Topologies.ReplSet;
155
+ break;
156
+ case 'Sharded':
157
+ topology = enums_1.Topologies.Sharded;
158
+ break;
159
+ case 'LoadBalanced':
160
+ topology = enums_1.Topologies.LoadBalanced;
161
+ break;
162
+ default:
163
+ topology = enums_1.Topologies.Standalone;
164
+ if (topologyDescription.servers.size === 1) {
165
+ const [server] = topologyDescription.servers.values();
166
+ switch (server.type) {
167
+ case 'Mongos':
168
+ topology = enums_1.Topologies.Sharded;
169
+ break;
170
+ case 'PossiblePrimary':
171
+ case 'RSPrimary':
172
+ case 'RSSecondary':
173
+ case 'RSArbiter':
174
+ case 'RSOther':
175
+ case 'RSGhost':
176
+ topology = enums_1.Topologies.ReplSet;
177
+ break;
178
+ default:
179
+ break;
180
+ }
181
+ }
182
+ break;
183
+ }
184
+ return topology;
185
+ },
186
+ apiVersionInfo: () => {
187
+ return this.apiVersionInfo();
188
+ },
189
+ connectionInfo: () => {
190
+ return this.connectionInfo.extraInfo;
191
+ },
192
+ getCollectionCompletionsForCurrentDb: async (collName) => {
193
+ try {
194
+ const collectionNames = await this.currentDb._getCollectionNamesForCompletion();
195
+ return collectionNames.filter((name) => name.toLowerCase().startsWith(collName.toLowerCase()));
196
+ }
197
+ catch (err) {
198
+ if ((err === null || err === void 0 ? void 0 : err.code) === error_codes_1.ShellApiErrors.NotConnected ||
199
+ (err === null || err === void 0 ? void 0 : err.codeName) === 'Unauthorized') {
200
+ return [];
201
+ }
202
+ throw err;
203
+ }
204
+ },
205
+ getDatabaseCompletions: async (dbName) => {
206
+ try {
207
+ const dbNames = await this.currentDb._mongo._getDatabaseNamesForCompletion();
208
+ return dbNames.filter((name) => name.toLowerCase().startsWith(dbName.toLowerCase()));
209
+ }
210
+ catch (err) {
211
+ if ((err === null || err === void 0 ? void 0 : err.code) === error_codes_1.ShellApiErrors.NotConnected ||
212
+ (err === null || err === void 0 ? void 0 : err.codeName) === 'Unauthorized') {
213
+ return [];
214
+ }
215
+ throw err;
216
+ }
217
+ }
218
+ };
219
+ }
220
+ apiVersionInfo() {
221
+ var _a, _b;
222
+ const { serverApi } = (_b = (_a = this.currentServiceProvider.getRawClient()) === null || _a === void 0 ? void 0 : _a.options) !== null && _b !== void 0 ? _b : {};
223
+ return (serverApi === null || serverApi === void 0 ? void 0 : serverApi.version) ? { strict: false, deprecationErrors: false, ...serverApi } : undefined;
224
+ }
225
+ async onInterruptExecution() {
226
+ this.interrupted.set();
227
+ this.currentCursor = null;
228
+ this.resumeMongosAfterInterrupt = await Promise.all(this.mongos.map(async (m) => {
229
+ try {
230
+ return {
231
+ mongo: m,
232
+ resume: await m._suspend()
233
+ };
234
+ }
235
+ catch (e) {
236
+ return {
237
+ mongo: m,
238
+ resume: null
239
+ };
240
+ }
241
+ }));
242
+ return !this.resumeMongosAfterInterrupt.find(r => r.resume === null);
243
+ }
244
+ async onResumeExecution() {
245
+ var _a, _b;
246
+ const promises = (_b = (_a = this.resumeMongosAfterInterrupt) === null || _a === void 0 ? void 0 : _a.map(async (r) => {
247
+ if (!this.mongos.find(m => m === r.mongo)) {
248
+ return true;
249
+ }
250
+ if (r.resume === null) {
251
+ return false;
252
+ }
253
+ try {
254
+ await r.resume();
255
+ return true;
256
+ }
257
+ catch (e) {
258
+ return false;
259
+ }
260
+ })) !== null && _b !== void 0 ? _b : [];
261
+ this.resumeMongosAfterInterrupt = undefined;
262
+ const result = await Promise.all(promises);
263
+ this.interrupted.reset();
264
+ return !result.find(r => r === false);
265
+ }
266
+ async getDefaultPrompt() {
267
+ const prefix = this.getDefaultPromptPrefix();
268
+ const topologyInfo = this.getTopologySpecificPrompt();
269
+ let dbname = '';
270
+ try {
271
+ dbname = this.currentDb.getName();
272
+ }
273
+ catch (_a) { }
274
+ return `${[prefix, topologyInfo, dbname].filter(Boolean).join(' ')}> `;
275
+ }
276
+ getDefaultPromptPrefix() {
277
+ var _a, _b, _c, _d;
278
+ const extraConnectionInfo = (_a = this.connectionInfo) === null || _a === void 0 ? void 0 : _a.extraInfo;
279
+ if (extraConnectionInfo === null || extraConnectionInfo === void 0 ? void 0 : extraConnectionInfo.is_data_lake) {
280
+ return 'AtlasDataLake';
281
+ }
282
+ else if (extraConnectionInfo === null || extraConnectionInfo === void 0 ? void 0 : extraConnectionInfo.is_atlas) {
283
+ return 'Atlas';
284
+ }
285
+ else if ((extraConnectionInfo === null || extraConnectionInfo === void 0 ? void 0 : extraConnectionInfo.is_enterprise) || ((_d = (_c = (_b = this.connectionInfo) === null || _b === void 0 ? void 0 : _b.buildInfo) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.indexOf('enterprise')) >= 0) {
286
+ return 'Enterprise';
287
+ }
288
+ return '';
289
+ }
290
+ getTopologySpecificPrompt() {
291
+ var _a, _b, _c, _d;
292
+ const description = (_a = this.currentServiceProvider.getTopology()) === null || _a === void 0 ? void 0 : _a.description;
293
+ if (!description) {
294
+ return '';
295
+ }
296
+ let replicaSet = description.setName;
297
+ let serverTypePrompt = '';
298
+ switch (description.type) {
299
+ case 'Single':
300
+ const singleDetails = this.getTopologySinglePrompt(description);
301
+ replicaSet = (_b = singleDetails === null || singleDetails === void 0 ? void 0 : singleDetails.replicaSet) !== null && _b !== void 0 ? _b : replicaSet;
302
+ serverTypePrompt = (singleDetails === null || singleDetails === void 0 ? void 0 : singleDetails.serverType) ? `[direct: ${singleDetails.serverType}]` : '';
303
+ break;
304
+ case 'ReplicaSetNoPrimary':
305
+ serverTypePrompt = '[secondary]';
306
+ break;
307
+ case 'ReplicaSetWithPrimary':
308
+ serverTypePrompt = '[primary]';
309
+ break;
310
+ case 'Sharded':
311
+ serverTypePrompt = ((_d = (_c = this.connectionInfo) === null || _c === void 0 ? void 0 : _c.extraInfo) === null || _d === void 0 ? void 0 : _d.atlas_version) ? '' : '[mongos]';
312
+ break;
313
+ case 'LoadBalanced':
314
+ default:
315
+ return '';
316
+ }
317
+ const setNamePrefix = replicaSet ? `${replicaSet} ` : '';
318
+ return `${setNamePrefix}${serverTypePrompt}`;
319
+ }
320
+ getTopologySinglePrompt(description) {
321
+ var _a;
322
+ if (((_a = description.servers) === null || _a === void 0 ? void 0 : _a.size) !== 1) {
323
+ return undefined;
324
+ }
325
+ const [server] = description.servers.values();
326
+ let serverType;
327
+ switch (server.type) {
328
+ case 'Mongos':
329
+ serverType = 'mongos';
330
+ break;
331
+ case 'RSPrimary':
332
+ serverType = 'primary';
333
+ break;
334
+ case 'RSSecondary':
335
+ serverType = 'secondary';
336
+ break;
337
+ case 'RSArbiter':
338
+ serverType = 'arbiter';
339
+ break;
340
+ case 'RSOther':
341
+ serverType = 'other';
342
+ break;
343
+ default:
344
+ serverType = '';
345
+ }
346
+ return {
347
+ replicaSet: server.setName,
348
+ serverType
349
+ };
350
+ }
351
+ registerPlugin(plugin) {
352
+ this.plugins.push(plugin);
353
+ }
354
+ transformError(err) {
355
+ if (Object.prototype.toString.call(err) === '[object Error]') {
356
+ if (this.alreadyTransformedErrors.has(err)) {
357
+ return this.alreadyTransformedErrors.get(err);
358
+ }
359
+ const before = err;
360
+ for (const plugin of this.plugins) {
361
+ if (plugin.transformError) {
362
+ err = plugin.transformError(err);
363
+ }
364
+ }
365
+ this.alreadyTransformedErrors.set(before, err);
366
+ }
367
+ return err;
368
+ }
369
+ async printDeprecationWarning(message) {
370
+ if (!this.warningsShown.has(message)) {
371
+ this.warningsShown.add(message);
372
+ if (this.context.print) {
373
+ await this.context.print.call(this.shellApi, `DeprecationWarning: ${message}`);
374
+ }
375
+ else {
376
+ await this.shellApi.print(`DeprecationWarning: ${message}`);
377
+ }
378
+ }
379
+ }
380
+ async printWarning(message) {
381
+ if (!this.warningsShown.has(message)) {
382
+ this.warningsShown.add(message);
383
+ if (this.context.print) {
384
+ await this.context.print.call(this.shellApi, `Warning: ${message}`);
385
+ }
386
+ else {
387
+ await this.shellApi.print(`Warning: ${message}`);
388
+ }
389
+ }
390
+ }
391
+ }
392
+ exports.default = ShellInstanceState;
393
393
  //# sourceMappingURL=shell-instance-state.js.map