diff --git a/package-lock.json b/package-lock.json index b55fea5ffaa..0955e546a1a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46027,6 +46027,7 @@ "chai": "^4.3.6", "compass-preferences-model": "^2.15.1", "depcheck": "^1.4.1", + "ejson-shell-parser": "^1.2.4", "enzyme": "^3.11.0", "eslint": "^7.25.0", "hadron-app-registry": "^9.0.11", @@ -58958,6 +58959,7 @@ "chai": "^4.3.6", "compass-preferences-model": "^2.15.1", "depcheck": "^1.4.1", + "ejson-shell-parser": "^1.2.4", "enzyme": "^3.11.0", "eslint": "^7.25.0", "hadron-app-registry": "^9.0.11", diff --git a/packages/compass-export-to-language/package.json b/packages/compass-export-to-language/package.json index 9394e027330..d4d700c146d 100644 --- a/packages/compass-export-to-language/package.json +++ b/packages/compass-export-to-language/package.json @@ -80,6 +80,7 @@ "chai": "^4.3.6", "compass-preferences-model": "^2.15.1", "depcheck": "^1.4.1", + "ejson-shell-parser": "^1.2.4", "enzyme": "^3.11.0", "eslint": "^7.25.0", "hadron-app-registry": "^9.0.11", diff --git a/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.js b/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.js deleted file mode 100644 index 035ad3e7d72..00000000000 --- a/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.js +++ /dev/null @@ -1,45 +0,0 @@ -import vm from 'vm'; - -let dummySandbox; - -// We do want to report the number of stages in an aggregation -// for telemetry, but we only receive them as a string from -// the consumers of this package. Instead, we evaluate -// the code in a dummy sandbox environment and just count -// the number of stages in the result. This is a little inefficient, -// but ultimately a simpler solution than pulling in a query -// parser here. -export function countAggregationStagesInString(str) { - if (!dummySandbox) { - dummySandbox = vm.createContext(Object.create(null), { - codeGeneration: { strings: false, wasm: false }, - microtaskMode: 'afterEvaluate', - }); - vm.runInContext( - [ - 'BSONRegExp', - 'DBRef', - 'Decimal128', - 'Double', - 'Int32', - 'Long', - 'Int64', - 'MaxKey', - 'MinKey', - 'ObjectID', - 'ObjectId', - 'BSONSymbol', - 'Timestamp', - 'Code', - 'Buffer', - 'Binary', - ] - .map((name) => `function ${name}() {}`) - .join('\n'), - dummySandbox - ); - } - - return vm.runInContext('(' + str + ')', dummySandbox, { timeout: 100 }) - .length; -} diff --git a/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.ts b/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.ts new file mode 100644 index 00000000000..19608536ad3 --- /dev/null +++ b/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.ts @@ -0,0 +1,9 @@ +import parseShellBSON, { ParseMode } from 'ejson-shell-parser'; + +export function countAggregationStagesInString(source: string): number { + const parsed = parseShellBSON(source, { mode: ParseMode.Loose }); + if (!Array.isArray(parsed)) { + throw new Error('Source expression is not an aggregation stage array'); + } + return parsed.length; +}