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

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 (66) 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/enums.d.ts +28 -28
  26. package/lib/enums.js +33 -33
  27. package/lib/error-codes.d.ts +12 -12
  28. package/lib/error-codes.js +19 -19
  29. package/lib/explainable-cursor.d.ts +11 -11
  30. package/lib/explainable-cursor.js +31 -31
  31. package/lib/explainable.d.ts +32 -32
  32. package/lib/explainable.js +166 -166
  33. package/lib/field-level-encryption.d.ts +50 -50
  34. package/lib/field-level-encryption.js +176 -176
  35. package/lib/help.d.ts +22 -22
  36. package/lib/help.js +26 -26
  37. package/lib/helpers.d.ts +71 -71
  38. package/lib/helpers.js +588 -588
  39. package/lib/index.d.ts +16 -16
  40. package/lib/index.js +45 -45
  41. package/lib/interruptor.d.ts +19 -19
  42. package/lib/interruptor.js +62 -62
  43. package/lib/mongo-errors.d.ts +5 -5
  44. package/lib/mongo-errors.js +37 -37
  45. package/lib/mongo.d.ts +75 -75
  46. package/lib/mongo.js +476 -476
  47. package/lib/no-db.d.ts +5 -5
  48. package/lib/no-db.js +28 -28
  49. package/lib/plan-cache.d.ts +16 -16
  50. package/lib/plan-cache.js +70 -70
  51. package/lib/replica-set.d.ts +45 -45
  52. package/lib/replica-set.js +314 -314
  53. package/lib/result.d.ts +61 -61
  54. package/lib/result.js +104 -104
  55. package/lib/session.d.ts +25 -25
  56. package/lib/session.js +88 -88
  57. package/lib/shard.d.ts +42 -42
  58. package/lib/shard.js +414 -414
  59. package/lib/shell-api.d.ts +52 -52
  60. package/lib/shell-api.js +298 -298
  61. package/lib/shell-bson.d.ts +40 -40
  62. package/lib/shell-bson.js +159 -159
  63. package/lib/shell-instance-state.d.ts +77 -77
  64. package/lib/shell-instance-state.js +392 -392
  65. package/package.json +47 -47
  66. package/tsconfig.lint.json +8 -8
package/lib/decorators.js CHANGED
@@ -1,396 +1,396 @@
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.addSourceToResults = exports.classPlatforms = exports.platforms = exports.classDeprecated = exports.returnType = exports.shellCommandCompleter = exports.directShellCommand = exports.returnsPromise = exports.nonAsyncFunctionsReturningPromises = exports.topologies = exports.deprecated = exports.apiVersions = exports.serverVersions = exports.shellApiClassNoHelp = exports.shellApiClassDefault = exports.toIgnore = exports.signatures = exports.toShellResult = exports.getShellApiType = exports.ShellApiValueClass = exports.ShellApiWithMongoClass = exports.ShellApiClass = void 0;
7
- const errors_1 = require("@qp-mongosh/errors");
8
- const enums_1 = require("./enums");
9
- const help_1 = __importDefault(require("./help"));
10
- const helpers_1 = require("./helpers");
11
- const addSourceToResultsSymbol = Symbol.for('@@mongosh.addSourceToResults');
12
- const resultSource = Symbol.for('@@mongosh.resultSource');
13
- class ShellApiClass {
14
- get [enums_1.shellApiType]() {
15
- throw new errors_1.MongoshInternalError('Shell API Type did not use decorators');
16
- }
17
- set [enums_1.shellApiType](value) {
18
- (0, helpers_1.addHiddenDataProperty)(this, enums_1.shellApiType, value);
19
- }
20
- [enums_1.asPrintable]() {
21
- if (Array.isArray(this)) {
22
- return [...this];
23
- }
24
- return { ...this };
25
- }
26
- }
27
- exports.ShellApiClass = ShellApiClass;
28
- class ShellApiWithMongoClass extends ShellApiClass {
29
- get _instanceState() {
30
- var _a;
31
- return (_a = this._mongo) === null || _a === void 0 ? void 0 : _a._instanceState;
32
- }
33
- }
34
- exports.ShellApiWithMongoClass = ShellApiWithMongoClass;
35
- class ShellApiValueClass extends ShellApiClass {
36
- get _mongo() {
37
- throw new errors_1.MongoshInternalError('Not supported on this value class');
38
- }
39
- get _instanceState() {
40
- throw new errors_1.MongoshInternalError('Not supported on this value class');
41
- }
42
- }
43
- exports.ShellApiValueClass = ShellApiValueClass;
44
- function getShellApiType(rawValue) {
45
- var _a;
46
- return (_a = rawValue === null || rawValue === void 0 ? void 0 : rawValue[enums_1.shellApiType]) !== null && _a !== void 0 ? _a : null;
47
- }
48
- exports.getShellApiType = getShellApiType;
49
- async function toShellResult(rawValue) {
50
- var _a;
51
- if ((typeof rawValue !== 'object' && typeof rawValue !== 'function') || rawValue === null) {
52
- return {
53
- type: null,
54
- rawValue: rawValue,
55
- printable: rawValue
56
- };
57
- }
58
- if ('then' in rawValue && typeof rawValue.then === 'function') {
59
- return toShellResult(await rawValue);
60
- }
61
- const printable = typeof rawValue[enums_1.asPrintable] === 'function' ? await rawValue[enums_1.asPrintable]() : rawValue;
62
- const source = (_a = rawValue[resultSource]) !== null && _a !== void 0 ? _a : undefined;
63
- return {
64
- type: getShellApiType(rawValue),
65
- rawValue: rawValue,
66
- printable: printable,
67
- source: source
68
- };
69
- }
70
- exports.toShellResult = toShellResult;
71
- function wrapWithAddSourceToResult(fn) {
72
- function addSource(result, obj) {
73
- if (typeof result === 'object' && result !== null) {
74
- const resultSourceInformation = {
75
- namespace: obj[enums_1.namespaceInfo](),
76
- };
77
- (0, helpers_1.addHiddenDataProperty)(result, resultSource, resultSourceInformation);
78
- if (result[enums_1.shellApiType] === undefined && fn.returnType) {
79
- (0, helpers_1.addHiddenDataProperty)(result, enums_1.shellApiType, fn.returnType);
80
- }
81
- }
82
- return result;
83
- }
84
- const wrapper = fn.returnsPromise ?
85
- markImplicitlyAwaited(async function (...args) {
86
- return addSource(await fn.call(this, ...args), this);
87
- }) : function (...args) {
88
- return addSource(fn.call(this, ...args), this);
89
- };
90
- Object.setPrototypeOf(wrapper, Object.getPrototypeOf(fn));
91
- Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(fn));
92
- return wrapper;
93
- }
94
- function wrapWithApiChecks(fn, className) {
95
- const wrapper = fn.returnsPromise ?
96
- markImplicitlyAwaited(async function (...args) {
97
- var _a, _b;
98
- const instanceState = getShellInstanceState(this);
99
- emitApiCallTelemetry(instanceState, className, fn, true);
100
- const interruptFlag = instanceState === null || instanceState === void 0 ? void 0 : instanceState.interrupted;
101
- interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
102
- const interrupt = interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.asPromise();
103
- let result;
104
- try {
105
- if (instanceState) {
106
- instanceState.apiCallDepth++;
107
- }
108
- result = await Promise.race([
109
- (_a = interrupt === null || interrupt === void 0 ? void 0 : interrupt.promise) !== null && _a !== void 0 ? _a : new Promise(() => { }),
110
- fn.call(this, ...args)
111
- ]);
112
- }
113
- catch (e) {
114
- throw (_b = instanceState === null || instanceState === void 0 ? void 0 : instanceState.transformError(e)) !== null && _b !== void 0 ? _b : e;
115
- }
116
- finally {
117
- if (instanceState) {
118
- instanceState.apiCallDepth--;
119
- }
120
- if (interrupt) {
121
- interrupt.destroy();
122
- }
123
- }
124
- interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
125
- return result;
126
- }) : function (...args) {
127
- var _a;
128
- const instanceState = getShellInstanceState(this);
129
- emitApiCallTelemetry(instanceState, className, fn, false);
130
- const interruptFlag = instanceState === null || instanceState === void 0 ? void 0 : instanceState.interrupted;
131
- interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
132
- let result;
133
- try {
134
- if (instanceState) {
135
- instanceState.apiCallDepth++;
136
- }
137
- result = fn.call(this, ...args);
138
- }
139
- catch (e) {
140
- throw (_a = instanceState === null || instanceState === void 0 ? void 0 : instanceState.transformError(e)) !== null && _a !== void 0 ? _a : e;
141
- }
142
- finally {
143
- if (instanceState) {
144
- instanceState.apiCallDepth--;
145
- }
146
- }
147
- interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
148
- return result;
149
- };
150
- Object.setPrototypeOf(wrapper, Object.getPrototypeOf(fn));
151
- Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(fn));
152
- return wrapper;
153
- }
154
- function emitApiCallTelemetry(instanceState, className, fn, isAsync) {
155
- var _a;
156
- (_a = instanceState === null || instanceState === void 0 ? void 0 : instanceState.emitApiCall) === null || _a === void 0 ? void 0 : _a.call(instanceState, {
157
- method: fn.name,
158
- class: className,
159
- deprecated: !!fn.deprecated,
160
- isAsync
161
- });
162
- }
163
- function getShellInstanceState(apiObject) {
164
- if (!apiObject[enums_1.shellApiType]) {
165
- throw new errors_1.MongoshInternalError('getShellInstanceState can only be called for functions from shell API classes');
166
- }
167
- return apiObject._instanceState;
168
- }
169
- const signaturesGlobalIdentifier = '@@@mdb.signatures@@@';
170
- if (!global[signaturesGlobalIdentifier]) {
171
- global[signaturesGlobalIdentifier] = {};
172
- }
173
- const signatures = global[signaturesGlobalIdentifier];
174
- exports.signatures = signatures;
175
- signatures.Document = { type: 'Document', attributes: {} };
176
- exports.toIgnore = ['constructor', 'help', 'toJSON'];
177
- function shellApiClassGeneric(constructor, hasHelp) {
178
- const className = constructor.name;
179
- const classHelpKeyPrefix = `shell-api.classes.${className}.help`;
180
- const classHelp = {
181
- help: `${classHelpKeyPrefix}.description`,
182
- docs: `${classHelpKeyPrefix}.link`,
183
- attr: []
184
- };
185
- const classSignature = {
186
- type: className,
187
- returnsPromise: constructor.prototype.returnsPromise || false,
188
- deprecated: constructor.prototype.deprecated || false,
189
- attributes: {}
190
- };
191
- const classAttributes = Object.getOwnPropertyNames(constructor.prototype);
192
- for (const propertyName of classAttributes) {
193
- const descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, propertyName);
194
- const isMethod = (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) && typeof descriptor.value === 'function';
195
- if (!isMethod ||
196
- exports.toIgnore.includes(propertyName) ||
197
- propertyName.startsWith('_'))
198
- continue;
199
- let method = descriptor.value;
200
- if (constructor[addSourceToResultsSymbol]) {
201
- method = wrapWithAddSourceToResult(method);
202
- }
203
- method = wrapWithApiChecks(method, className);
204
- method.serverVersions = method.serverVersions || enums_1.ALL_SERVER_VERSIONS;
205
- method.apiVersions = method.apiVersions || enums_1.ALL_API_VERSIONS;
206
- method.topologies = method.topologies || enums_1.ALL_TOPOLOGIES;
207
- method.returnType = method.returnType || { type: 'unknown', attributes: {} };
208
- method.returnsPromise = method.returnsPromise || false;
209
- method.deprecated = method.deprecated || false;
210
- method.platforms = method.platforms || enums_1.ALL_PLATFORMS;
211
- method.isDirectShellCommand = method.isDirectShellCommand || false;
212
- method.acceptsRawInput = method.acceptsRawInput || false;
213
- method.shellCommandCompleter = method.shellCommandCompleter || undefined;
214
- classSignature.attributes[propertyName] = {
215
- type: 'function',
216
- serverVersions: method.serverVersions,
217
- apiVersions: method.apiVersions,
218
- topologies: method.topologies,
219
- returnType: method.returnType === 'this' ? className : method.returnType,
220
- returnsPromise: method.returnsPromise,
221
- deprecated: method.deprecated,
222
- platforms: method.platforms,
223
- isDirectShellCommand: method.isDirectShellCommand,
224
- acceptsRawInput: method.acceptsRawInput,
225
- shellCommandCompleter: method.shellCommandCompleter
226
- };
227
- const attributeHelpKeyPrefix = `${classHelpKeyPrefix}.attributes.${propertyName}`;
228
- const attrHelp = {
229
- help: `${attributeHelpKeyPrefix}.example`,
230
- docs: `${attributeHelpKeyPrefix}.link`,
231
- attr: [
232
- { description: `${attributeHelpKeyPrefix}.description` }
233
- ]
234
- };
235
- const aHelp = new help_1.default(attrHelp);
236
- method.help = () => (aHelp);
237
- Object.setPrototypeOf(method.help, aHelp);
238
- classHelp.attr.push({
239
- name: propertyName,
240
- description: `${attributeHelpKeyPrefix}.description`
241
- });
242
- Object.defineProperty(constructor.prototype, propertyName, {
243
- ...descriptor,
244
- value: method
245
- });
246
- }
247
- let superClass = constructor.prototype;
248
- while ((superClass = Object.getPrototypeOf(superClass)) !== null) {
249
- if (superClass.constructor.name === 'ShellApiClass' || superClass.constructor === Array) {
250
- break;
251
- }
252
- const superClassHelpKeyPrefix = `shell-api.classes.${superClass.constructor.name}.help`;
253
- for (const propertyName of Object.getOwnPropertyNames(superClass)) {
254
- const descriptor = Object.getOwnPropertyDescriptor(superClass, propertyName);
255
- const isMethod = (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) && typeof descriptor.value === 'function';
256
- if (classAttributes.includes(propertyName) ||
257
- !isMethod ||
258
- exports.toIgnore.includes(propertyName) ||
259
- propertyName.startsWith('_'))
260
- continue;
261
- const method = descriptor.value;
262
- classSignature.attributes[propertyName] = {
263
- type: 'function',
264
- serverVersions: method.serverVersions,
265
- apiVersions: method.apiVersions,
266
- topologies: method.topologies,
267
- returnType: method.returnType === 'this' ? className : method.returnType,
268
- returnsPromise: method.returnsPromise,
269
- deprecated: method.deprecated,
270
- platforms: method.platforms,
271
- isDirectShellCommand: method.isDirectShellCommand,
272
- acceptsRawInput: method.acceptsRawInput,
273
- shellCommandCompleter: method.shellCommandCompleter
274
- };
275
- const attributeHelpKeyPrefix = `${superClassHelpKeyPrefix}.attributes.${propertyName}`;
276
- classHelp.attr.push({
277
- name: propertyName,
278
- description: `${attributeHelpKeyPrefix}.description`
279
- });
280
- }
281
- }
282
- const help = new help_1.default(classHelp);
283
- constructor.prototype.help = () => (help);
284
- Object.setPrototypeOf(constructor.prototype.help, help);
285
- constructor.prototype[enums_1.asPrintable] =
286
- constructor.prototype[enums_1.asPrintable] ||
287
- ShellApiClass.prototype[enums_1.asPrintable];
288
- (0, helpers_1.addHiddenDataProperty)(constructor.prototype, enums_1.shellApiType, className);
289
- if (hasHelp) {
290
- signatures[className] = classSignature;
291
- }
292
- }
293
- function shellApiClassDefault(constructor) {
294
- shellApiClassGeneric(constructor, true);
295
- }
296
- exports.shellApiClassDefault = shellApiClassDefault;
297
- function shellApiClassNoHelp(constructor) {
298
- shellApiClassGeneric(constructor, false);
299
- }
300
- exports.shellApiClassNoHelp = shellApiClassNoHelp;
301
- function markImplicitlyAwaited(orig) {
302
- function wrapper(...args) {
303
- const origResult = orig.call(this, ...args);
304
- return (0, helpers_1.addHiddenDataProperty)(origResult, Symbol.for('@@mongosh.syntheticPromise'), true);
305
- }
306
- Object.setPrototypeOf(wrapper, Object.getPrototypeOf(orig));
307
- Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(orig));
308
- return wrapper;
309
- }
310
- function serverVersions(versionArray) {
311
- return function (_target, _propertyKey, descriptor) {
312
- descriptor.value.serverVersions = versionArray;
313
- };
314
- }
315
- exports.serverVersions = serverVersions;
316
- function apiVersions(versionArray) {
317
- return function (_target, _propertyKey, descriptor) {
318
- if (versionArray.length === 0) {
319
- versionArray = [0, 0];
320
- }
321
- else if (versionArray.length === 1) {
322
- versionArray = [versionArray[0], Infinity];
323
- }
324
- descriptor.value.apiVersions = versionArray;
325
- };
326
- }
327
- exports.apiVersions = apiVersions;
328
- function deprecated(_target, _propertyKey, descriptor) {
329
- descriptor.value.deprecated = true;
330
- }
331
- exports.deprecated = deprecated;
332
- function topologies(topologiesArray) {
333
- return function (_target, _propertyKey, descriptor) {
334
- descriptor.value.topologies = topologiesArray;
335
- };
336
- }
337
- exports.topologies = topologies;
338
- exports.nonAsyncFunctionsReturningPromises = [];
339
- function returnsPromise(_target, _propertyKey, descriptor) {
340
- const originalFunction = descriptor.value;
341
- originalFunction.returnsPromise = true;
342
- async function wrapper(...args) {
343
- try {
344
- return await originalFunction.call(this, ...args);
345
- }
346
- finally {
347
- if (typeof setTimeout === 'function' && typeof setImmediate === 'function') {
348
- await new Promise(setImmediate);
349
- }
350
- }
351
- }
352
- Object.setPrototypeOf(wrapper, Object.getPrototypeOf(originalFunction));
353
- Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(originalFunction));
354
- descriptor.value = markImplicitlyAwaited(wrapper);
355
- if (originalFunction.constructor.name !== 'AsyncFunction') {
356
- exports.nonAsyncFunctionsReturningPromises.push(originalFunction.name);
357
- }
358
- }
359
- exports.returnsPromise = returnsPromise;
360
- function directShellCommand(_target, _propertyKey, descriptor) {
361
- descriptor.value.isDirectShellCommand = true;
362
- }
363
- exports.directShellCommand = directShellCommand;
364
- function shellCommandCompleter(completer) {
365
- return function (_target, _propertyKey, descriptor) {
366
- descriptor.value.shellCommandCompleter = completer;
367
- };
368
- }
369
- exports.shellCommandCompleter = shellCommandCompleter;
370
- function returnType(type) {
371
- return function (_target, _propertyKey, descriptor) {
372
- descriptor.value.returnType = type;
373
- };
374
- }
375
- exports.returnType = returnType;
376
- function classDeprecated(constructor) {
377
- constructor.prototype.deprecated = true;
378
- }
379
- exports.classDeprecated = classDeprecated;
380
- function platforms(platformsArray) {
381
- return function (_target, _propertyKey, descriptor) {
382
- descriptor.value.platforms = platformsArray;
383
- };
384
- }
385
- exports.platforms = platforms;
386
- function classPlatforms(platformsArray) {
387
- return function (constructor) {
388
- constructor.prototype.platforms = platformsArray;
389
- };
390
- }
391
- exports.classPlatforms = classPlatforms;
392
- function addSourceToResults(constructor) {
393
- constructor[addSourceToResultsSymbol] = true;
394
- }
395
- exports.addSourceToResults = addSourceToResults;
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.addSourceToResults = exports.classPlatforms = exports.platforms = exports.classDeprecated = exports.returnType = exports.shellCommandCompleter = exports.directShellCommand = exports.returnsPromise = exports.nonAsyncFunctionsReturningPromises = exports.topologies = exports.deprecated = exports.apiVersions = exports.serverVersions = exports.shellApiClassNoHelp = exports.shellApiClassDefault = exports.toIgnore = exports.signatures = exports.toShellResult = exports.getShellApiType = exports.ShellApiValueClass = exports.ShellApiWithMongoClass = exports.ShellApiClass = void 0;
7
+ const errors_1 = require("@qp-mongosh/errors");
8
+ const enums_1 = require("./enums");
9
+ const help_1 = __importDefault(require("./help"));
10
+ const helpers_1 = require("./helpers");
11
+ const addSourceToResultsSymbol = Symbol.for('@@mongosh.addSourceToResults');
12
+ const resultSource = Symbol.for('@@mongosh.resultSource');
13
+ class ShellApiClass {
14
+ get [enums_1.shellApiType]() {
15
+ throw new errors_1.MongoshInternalError('Shell API Type did not use decorators');
16
+ }
17
+ set [enums_1.shellApiType](value) {
18
+ (0, helpers_1.addHiddenDataProperty)(this, enums_1.shellApiType, value);
19
+ }
20
+ [enums_1.asPrintable]() {
21
+ if (Array.isArray(this)) {
22
+ return [...this];
23
+ }
24
+ return { ...this };
25
+ }
26
+ }
27
+ exports.ShellApiClass = ShellApiClass;
28
+ class ShellApiWithMongoClass extends ShellApiClass {
29
+ get _instanceState() {
30
+ var _a;
31
+ return (_a = this._mongo) === null || _a === void 0 ? void 0 : _a._instanceState;
32
+ }
33
+ }
34
+ exports.ShellApiWithMongoClass = ShellApiWithMongoClass;
35
+ class ShellApiValueClass extends ShellApiClass {
36
+ get _mongo() {
37
+ throw new errors_1.MongoshInternalError('Not supported on this value class');
38
+ }
39
+ get _instanceState() {
40
+ throw new errors_1.MongoshInternalError('Not supported on this value class');
41
+ }
42
+ }
43
+ exports.ShellApiValueClass = ShellApiValueClass;
44
+ function getShellApiType(rawValue) {
45
+ var _a;
46
+ return (_a = rawValue === null || rawValue === void 0 ? void 0 : rawValue[enums_1.shellApiType]) !== null && _a !== void 0 ? _a : null;
47
+ }
48
+ exports.getShellApiType = getShellApiType;
49
+ async function toShellResult(rawValue) {
50
+ var _a;
51
+ if ((typeof rawValue !== 'object' && typeof rawValue !== 'function') || rawValue === null) {
52
+ return {
53
+ type: null,
54
+ rawValue: rawValue,
55
+ printable: rawValue
56
+ };
57
+ }
58
+ if ('then' in rawValue && typeof rawValue.then === 'function') {
59
+ return toShellResult(await rawValue);
60
+ }
61
+ const printable = typeof rawValue[enums_1.asPrintable] === 'function' ? await rawValue[enums_1.asPrintable]() : rawValue;
62
+ const source = (_a = rawValue[resultSource]) !== null && _a !== void 0 ? _a : undefined;
63
+ return {
64
+ type: getShellApiType(rawValue),
65
+ rawValue: rawValue,
66
+ printable: printable,
67
+ source: source
68
+ };
69
+ }
70
+ exports.toShellResult = toShellResult;
71
+ function wrapWithAddSourceToResult(fn) {
72
+ function addSource(result, obj) {
73
+ if (typeof result === 'object' && result !== null) {
74
+ const resultSourceInformation = {
75
+ namespace: obj[enums_1.namespaceInfo](),
76
+ };
77
+ (0, helpers_1.addHiddenDataProperty)(result, resultSource, resultSourceInformation);
78
+ if (result[enums_1.shellApiType] === undefined && fn.returnType) {
79
+ (0, helpers_1.addHiddenDataProperty)(result, enums_1.shellApiType, fn.returnType);
80
+ }
81
+ }
82
+ return result;
83
+ }
84
+ const wrapper = fn.returnsPromise ?
85
+ markImplicitlyAwaited(async function (...args) {
86
+ return addSource(await fn.call(this, ...args), this);
87
+ }) : function (...args) {
88
+ return addSource(fn.call(this, ...args), this);
89
+ };
90
+ Object.setPrototypeOf(wrapper, Object.getPrototypeOf(fn));
91
+ Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(fn));
92
+ return wrapper;
93
+ }
94
+ function wrapWithApiChecks(fn, className) {
95
+ const wrapper = fn.returnsPromise ?
96
+ markImplicitlyAwaited(async function (...args) {
97
+ var _a, _b;
98
+ const instanceState = getShellInstanceState(this);
99
+ emitApiCallTelemetry(instanceState, className, fn, true);
100
+ const interruptFlag = instanceState === null || instanceState === void 0 ? void 0 : instanceState.interrupted;
101
+ interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
102
+ const interrupt = interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.asPromise();
103
+ let result;
104
+ try {
105
+ if (instanceState) {
106
+ instanceState.apiCallDepth++;
107
+ }
108
+ result = await Promise.race([
109
+ (_a = interrupt === null || interrupt === void 0 ? void 0 : interrupt.promise) !== null && _a !== void 0 ? _a : new Promise(() => { }),
110
+ fn.call(this, ...args)
111
+ ]);
112
+ }
113
+ catch (e) {
114
+ throw (_b = instanceState === null || instanceState === void 0 ? void 0 : instanceState.transformError(e)) !== null && _b !== void 0 ? _b : e;
115
+ }
116
+ finally {
117
+ if (instanceState) {
118
+ instanceState.apiCallDepth--;
119
+ }
120
+ if (interrupt) {
121
+ interrupt.destroy();
122
+ }
123
+ }
124
+ interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
125
+ return result;
126
+ }) : function (...args) {
127
+ var _a;
128
+ const instanceState = getShellInstanceState(this);
129
+ emitApiCallTelemetry(instanceState, className, fn, false);
130
+ const interruptFlag = instanceState === null || instanceState === void 0 ? void 0 : instanceState.interrupted;
131
+ interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
132
+ let result;
133
+ try {
134
+ if (instanceState) {
135
+ instanceState.apiCallDepth++;
136
+ }
137
+ result = fn.call(this, ...args);
138
+ }
139
+ catch (e) {
140
+ throw (_a = instanceState === null || instanceState === void 0 ? void 0 : instanceState.transformError(e)) !== null && _a !== void 0 ? _a : e;
141
+ }
142
+ finally {
143
+ if (instanceState) {
144
+ instanceState.apiCallDepth--;
145
+ }
146
+ }
147
+ interruptFlag === null || interruptFlag === void 0 ? void 0 : interruptFlag.checkpoint();
148
+ return result;
149
+ };
150
+ Object.setPrototypeOf(wrapper, Object.getPrototypeOf(fn));
151
+ Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(fn));
152
+ return wrapper;
153
+ }
154
+ function emitApiCallTelemetry(instanceState, className, fn, isAsync) {
155
+ var _a;
156
+ (_a = instanceState === null || instanceState === void 0 ? void 0 : instanceState.emitApiCall) === null || _a === void 0 ? void 0 : _a.call(instanceState, {
157
+ method: fn.name,
158
+ class: className,
159
+ deprecated: !!fn.deprecated,
160
+ isAsync
161
+ });
162
+ }
163
+ function getShellInstanceState(apiObject) {
164
+ if (!apiObject[enums_1.shellApiType]) {
165
+ throw new errors_1.MongoshInternalError('getShellInstanceState can only be called for functions from shell API classes');
166
+ }
167
+ return apiObject._instanceState;
168
+ }
169
+ const signaturesGlobalIdentifier = '@@@mdb.signatures@@@';
170
+ if (!global[signaturesGlobalIdentifier]) {
171
+ global[signaturesGlobalIdentifier] = {};
172
+ }
173
+ const signatures = global[signaturesGlobalIdentifier];
174
+ exports.signatures = signatures;
175
+ signatures.Document = { type: 'Document', attributes: {} };
176
+ exports.toIgnore = ['constructor', 'help', 'toJSON'];
177
+ function shellApiClassGeneric(constructor, hasHelp) {
178
+ const className = constructor.name;
179
+ const classHelpKeyPrefix = `shell-api.classes.${className}.help`;
180
+ const classHelp = {
181
+ help: `${classHelpKeyPrefix}.description`,
182
+ docs: `${classHelpKeyPrefix}.link`,
183
+ attr: []
184
+ };
185
+ const classSignature = {
186
+ type: className,
187
+ returnsPromise: constructor.prototype.returnsPromise || false,
188
+ deprecated: constructor.prototype.deprecated || false,
189
+ attributes: {}
190
+ };
191
+ const classAttributes = Object.getOwnPropertyNames(constructor.prototype);
192
+ for (const propertyName of classAttributes) {
193
+ const descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, propertyName);
194
+ const isMethod = (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) && typeof descriptor.value === 'function';
195
+ if (!isMethod ||
196
+ exports.toIgnore.includes(propertyName) ||
197
+ propertyName.startsWith('_'))
198
+ continue;
199
+ let method = descriptor.value;
200
+ if (constructor[addSourceToResultsSymbol]) {
201
+ method = wrapWithAddSourceToResult(method);
202
+ }
203
+ method = wrapWithApiChecks(method, className);
204
+ method.serverVersions = method.serverVersions || enums_1.ALL_SERVER_VERSIONS;
205
+ method.apiVersions = method.apiVersions || enums_1.ALL_API_VERSIONS;
206
+ method.topologies = method.topologies || enums_1.ALL_TOPOLOGIES;
207
+ method.returnType = method.returnType || { type: 'unknown', attributes: {} };
208
+ method.returnsPromise = method.returnsPromise || false;
209
+ method.deprecated = method.deprecated || false;
210
+ method.platforms = method.platforms || enums_1.ALL_PLATFORMS;
211
+ method.isDirectShellCommand = method.isDirectShellCommand || false;
212
+ method.acceptsRawInput = method.acceptsRawInput || false;
213
+ method.shellCommandCompleter = method.shellCommandCompleter || undefined;
214
+ classSignature.attributes[propertyName] = {
215
+ type: 'function',
216
+ serverVersions: method.serverVersions,
217
+ apiVersions: method.apiVersions,
218
+ topologies: method.topologies,
219
+ returnType: method.returnType === 'this' ? className : method.returnType,
220
+ returnsPromise: method.returnsPromise,
221
+ deprecated: method.deprecated,
222
+ platforms: method.platforms,
223
+ isDirectShellCommand: method.isDirectShellCommand,
224
+ acceptsRawInput: method.acceptsRawInput,
225
+ shellCommandCompleter: method.shellCommandCompleter
226
+ };
227
+ const attributeHelpKeyPrefix = `${classHelpKeyPrefix}.attributes.${propertyName}`;
228
+ const attrHelp = {
229
+ help: `${attributeHelpKeyPrefix}.example`,
230
+ docs: `${attributeHelpKeyPrefix}.link`,
231
+ attr: [
232
+ { description: `${attributeHelpKeyPrefix}.description` }
233
+ ]
234
+ };
235
+ const aHelp = new help_1.default(attrHelp);
236
+ method.help = () => (aHelp);
237
+ Object.setPrototypeOf(method.help, aHelp);
238
+ classHelp.attr.push({
239
+ name: propertyName,
240
+ description: `${attributeHelpKeyPrefix}.description`
241
+ });
242
+ Object.defineProperty(constructor.prototype, propertyName, {
243
+ ...descriptor,
244
+ value: method
245
+ });
246
+ }
247
+ let superClass = constructor.prototype;
248
+ while ((superClass = Object.getPrototypeOf(superClass)) !== null) {
249
+ if (superClass.constructor.name === 'ShellApiClass' || superClass.constructor === Array) {
250
+ break;
251
+ }
252
+ const superClassHelpKeyPrefix = `shell-api.classes.${superClass.constructor.name}.help`;
253
+ for (const propertyName of Object.getOwnPropertyNames(superClass)) {
254
+ const descriptor = Object.getOwnPropertyDescriptor(superClass, propertyName);
255
+ const isMethod = (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) && typeof descriptor.value === 'function';
256
+ if (classAttributes.includes(propertyName) ||
257
+ !isMethod ||
258
+ exports.toIgnore.includes(propertyName) ||
259
+ propertyName.startsWith('_'))
260
+ continue;
261
+ const method = descriptor.value;
262
+ classSignature.attributes[propertyName] = {
263
+ type: 'function',
264
+ serverVersions: method.serverVersions,
265
+ apiVersions: method.apiVersions,
266
+ topologies: method.topologies,
267
+ returnType: method.returnType === 'this' ? className : method.returnType,
268
+ returnsPromise: method.returnsPromise,
269
+ deprecated: method.deprecated,
270
+ platforms: method.platforms,
271
+ isDirectShellCommand: method.isDirectShellCommand,
272
+ acceptsRawInput: method.acceptsRawInput,
273
+ shellCommandCompleter: method.shellCommandCompleter
274
+ };
275
+ const attributeHelpKeyPrefix = `${superClassHelpKeyPrefix}.attributes.${propertyName}`;
276
+ classHelp.attr.push({
277
+ name: propertyName,
278
+ description: `${attributeHelpKeyPrefix}.description`
279
+ });
280
+ }
281
+ }
282
+ const help = new help_1.default(classHelp);
283
+ constructor.prototype.help = () => (help);
284
+ Object.setPrototypeOf(constructor.prototype.help, help);
285
+ constructor.prototype[enums_1.asPrintable] =
286
+ constructor.prototype[enums_1.asPrintable] ||
287
+ ShellApiClass.prototype[enums_1.asPrintable];
288
+ (0, helpers_1.addHiddenDataProperty)(constructor.prototype, enums_1.shellApiType, className);
289
+ if (hasHelp) {
290
+ signatures[className] = classSignature;
291
+ }
292
+ }
293
+ function shellApiClassDefault(constructor) {
294
+ shellApiClassGeneric(constructor, true);
295
+ }
296
+ exports.shellApiClassDefault = shellApiClassDefault;
297
+ function shellApiClassNoHelp(constructor) {
298
+ shellApiClassGeneric(constructor, false);
299
+ }
300
+ exports.shellApiClassNoHelp = shellApiClassNoHelp;
301
+ function markImplicitlyAwaited(orig) {
302
+ function wrapper(...args) {
303
+ const origResult = orig.call(this, ...args);
304
+ return (0, helpers_1.addHiddenDataProperty)(origResult, Symbol.for('@@mongosh.syntheticPromise'), true);
305
+ }
306
+ Object.setPrototypeOf(wrapper, Object.getPrototypeOf(orig));
307
+ Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(orig));
308
+ return wrapper;
309
+ }
310
+ function serverVersions(versionArray) {
311
+ return function (_target, _propertyKey, descriptor) {
312
+ descriptor.value.serverVersions = versionArray;
313
+ };
314
+ }
315
+ exports.serverVersions = serverVersions;
316
+ function apiVersions(versionArray) {
317
+ return function (_target, _propertyKey, descriptor) {
318
+ if (versionArray.length === 0) {
319
+ versionArray = [0, 0];
320
+ }
321
+ else if (versionArray.length === 1) {
322
+ versionArray = [versionArray[0], Infinity];
323
+ }
324
+ descriptor.value.apiVersions = versionArray;
325
+ };
326
+ }
327
+ exports.apiVersions = apiVersions;
328
+ function deprecated(_target, _propertyKey, descriptor) {
329
+ descriptor.value.deprecated = true;
330
+ }
331
+ exports.deprecated = deprecated;
332
+ function topologies(topologiesArray) {
333
+ return function (_target, _propertyKey, descriptor) {
334
+ descriptor.value.topologies = topologiesArray;
335
+ };
336
+ }
337
+ exports.topologies = topologies;
338
+ exports.nonAsyncFunctionsReturningPromises = [];
339
+ function returnsPromise(_target, _propertyKey, descriptor) {
340
+ const originalFunction = descriptor.value;
341
+ originalFunction.returnsPromise = true;
342
+ async function wrapper(...args) {
343
+ try {
344
+ return await originalFunction.call(this, ...args);
345
+ }
346
+ finally {
347
+ if (typeof setTimeout === 'function' && typeof setImmediate === 'function') {
348
+ await new Promise(setImmediate);
349
+ }
350
+ }
351
+ }
352
+ Object.setPrototypeOf(wrapper, Object.getPrototypeOf(originalFunction));
353
+ Object.defineProperties(wrapper, Object.getOwnPropertyDescriptors(originalFunction));
354
+ descriptor.value = markImplicitlyAwaited(wrapper);
355
+ if (originalFunction.constructor.name !== 'AsyncFunction') {
356
+ exports.nonAsyncFunctionsReturningPromises.push(originalFunction.name);
357
+ }
358
+ }
359
+ exports.returnsPromise = returnsPromise;
360
+ function directShellCommand(_target, _propertyKey, descriptor) {
361
+ descriptor.value.isDirectShellCommand = true;
362
+ }
363
+ exports.directShellCommand = directShellCommand;
364
+ function shellCommandCompleter(completer) {
365
+ return function (_target, _propertyKey, descriptor) {
366
+ descriptor.value.shellCommandCompleter = completer;
367
+ };
368
+ }
369
+ exports.shellCommandCompleter = shellCommandCompleter;
370
+ function returnType(type) {
371
+ return function (_target, _propertyKey, descriptor) {
372
+ descriptor.value.returnType = type;
373
+ };
374
+ }
375
+ exports.returnType = returnType;
376
+ function classDeprecated(constructor) {
377
+ constructor.prototype.deprecated = true;
378
+ }
379
+ exports.classDeprecated = classDeprecated;
380
+ function platforms(platformsArray) {
381
+ return function (_target, _propertyKey, descriptor) {
382
+ descriptor.value.platforms = platformsArray;
383
+ };
384
+ }
385
+ exports.platforms = platforms;
386
+ function classPlatforms(platformsArray) {
387
+ return function (constructor) {
388
+ constructor.prototype.platforms = platformsArray;
389
+ };
390
+ }
391
+ exports.classPlatforms = classPlatforms;
392
+ function addSourceToResults(constructor) {
393
+ constructor[addSourceToResultsSymbol] = true;
394
+ }
395
+ exports.addSourceToResults = addSourceToResults;
396
396
  //# sourceMappingURL=decorators.js.map