Skip to content

feat(bson-parsing)!: move parsing into web-worker COMPASS-11127 - #891

Open
mabaasit wants to merge 27 commits into
mainfrom
web-worker-parsing
Open

mabaasit wants to merge 27 commits into
mainfrom
web-worker-parsing

Conversation

@mabaasit

@mabaasit mabaasit commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

In draft because

  • I need to spend a bit more on (de)serialization (Neal is working on something very similar) and bundling.
  • I need to figure out the sync -> async api change for mongodb-query-parser package. This changes happened in this PR and for monorepo, it resolves to the local export and not the one pinned in package.json file.

Description

COMPASS-11127

Open Questions

Checklist


Stack created with GitHub Stacks CLI • Give Feedback 💬

@mabaasit
mabaasit added this pull request to stack #894 September 21, 2026 08:51
@mabaasit mabaasit changed the title feat(bson-parsin)!: move parsing into web-worker feat(bson-parsing)!: move parsing into web-worker Sep 21, 2026
@mabaasit mabaasit changed the title feat(bson-parsing)!: move parsing into web-worker feat(bson-parsing)!: move parsing into web-worker COMPASS-11127 Sep 22, 2026
Copilot AI lite review requested due to automatic review settings September 23, 2026 11:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Critical API, compatibility, browser-runtime, and BSON-cloning issues remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 4 High severity · 1 Medium severity · 1 Low severity

Open (6)
What changed in this PR

Moves BSON parsing into a web worker while adding structured-clone support, an explicit synchronous API, and ESM/Webpack tooling.

Changes:

  • Adds worker communication, lifecycle management, and BSON serialization.
  • Makes default parsing asynchronous while retaining synchronous parsing.
  • Updates tests, packaging, build configuration, and the MQL docs crawler.
File Review summary
packages/​shell-bson-parser/​webpack.worker.config.cjs Bundles the worker target.
packages/​shell-bson-parser/​tsconfig.json Enables NodeNext resolution.
packages/​shell-bson-parser/​test/​parse-test-cases.ts Updates ESM imports and test typing.
packages/​shell-bson-parser/​src/​worker.ts Handles worker parse requests.
packages/​shell-bson-parser/​src/​worker-types.ts Defines worker messages.
packages/​shell-bson-parser/​src/​worker-client.ts Critical: browser process access can throw. Moderate: concurrent initialization can leak workers, and termination can leave requests unresolved.
packages/​shell-bson-parser/​src/​structured-clone-bson.ts Critical: _bsontype duck typing can corrupt valid plain documents after cloning.
packages/​shell-bson-parser/​src/​structured-clone-bson.spec.ts Tests BSON round trips.
packages/​shell-bson-parser/​src/​stringify.spec.ts Updates ESM imports.
packages/​shell-bson-parser/​src/​parse.ts Updates internal ESM imports.
packages/​shell-bson-parser/​src/​parse.spec.ts Updates imports and Sinon usage.
packages/​shell-bson-parser/​src/​index.ts Critical: the async default API breaks synchronous query-parser usage. Nit: README examples remain synchronous.
packages/​shell-bson-parser/​src/​index.spec.ts Moderate: tests bypass the bundled worker path and do not cover the real worker artifact.
packages/​shell-bson-parser/​src/​eval.ts Updates an internal import.
packages/​shell-bson-parser/​src/​check.ts Updates internal imports.
packages/​shell-bson-parser/​package.json Critical: removing the CommonJS condition breaks CommonJS consumers. Moderate: type: module breaks the existing CommonJS benchmark.
packages/​shell-bson-parser/​.mocharc.cjs Adds shared Mocha configuration.
packages/​shell-bson-parser/​.eslintrc.cjs Ignores the Webpack configuration.
packages/​mql-typescript/​src/​driverSchema/​docs-crawler.ts Uses the synchronous parser API.
package-lock.json Locks updated Webpack dependencies.
configs/​mocha-config-devtools/​register/​tsnode-register.js Adds NodeNext test-loader support.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +25 to +29
"type": "module",
"main": "dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/.esm-wrapper.mjs"
}
"types": "./dist/index.d.ts",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this change in intentional. We deprecated mongodb-query-parser package and marked it as private in this PR (and also excluded it from tests/checks). As for mql-typescript, changed to dynamic imports


export const parse = (
...args: Parameters<typeof parseSync>
): Promise<ReturnType<typeof parseSync>> => callWorker(args);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is a breaking change and mongodb-query-parser is deprecated.

Comment on lines +25 to +26
function isBsonValue(value: object): value is { _bsontype: string } {
return typeof (value as { _bsontype?: unknown })._bsontype === 'string';

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 334507d

}

async function getWorkerBlobUrl(): Promise<string> {
if (process.env.TEST_SKIP_WORKER_SCRIPT_FETCH) {
Comment on lines +48 to +52
if (worker) {
return worker;
}

blobUrl = await getWorkerBlobUrl();
Comment on lines +6 to +8
export const parse = (
...args: Parameters<typeof parseSync>
): Promise<ReturnType<typeof parseSync>> => callWorker(args);
Comment thread packages/query-parser/package.json Outdated
"name": "mongodb-query-parser",
"description": "Parse MongoDB queries",
"deprecated": "This package is deprecated. Use @mongodb-js/shell-bson-parser instead.",
"private": true,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignores from being published.

@mabaasit
mabaasit marked this pull request as ready for review September 28, 2026 12:24
@mabaasit
mabaasit requested review from a team as code owners September 28, 2026 12:24
@mabaasit
mabaasit requested a review from esvm September 28, 2026 12:24
Base automatically changed from consolidate-query-parser-in-bson-parser to main September 28, 2026 15:28
@mabaasit
mabaasit force-pushed the web-worker-parsing branch 2 times, most recently from 89987eb to b87d456 Compare September 29, 2026 08:01

@esvm esvm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a small question but LGTM

Comment thread packages/shell-bson-parser/src/worker-client.ts
Comment thread packages/mql-typescript/src/driverSchema/docs-crawler.ts Outdated
@mabaasit
mabaasit force-pushed the web-worker-parsing branch from fd1ff4b to 2d47a92 Compare October 5, 2026 13:00
@mabaasit

mabaasit commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

3cad193 (compass-components bump) brought many changes to lock file.

@Anemy Anemy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are looking good, I left a few questions, not blockers.

Comment thread packages/oidc-http-server-pages/.depcheckrc
Comment thread packages/shell-bson-parser/src/check.ts
Comment thread packages/shell-bson-parser/src/worker-client.ts Outdated
Comment thread packages/shell-bson-parser/src/structured-clone-bson.ts Outdated
Comment thread packages/shell-bson-parser/src/structured-clone-bson.ts Outdated
Comment thread packages/shell-bson-parser/src/structured-clone-bson.ts Outdated
Comment thread packages/shell-bson-parser/src/structured-clone-bson.ts Outdated
Comment thread packages/shell-bson-parser/src/structured-clone-bson.ts Outdated
Comment thread packages/shell-bson-parser/src/worker-client.ts Outdated
Comment thread packages/shell-bson-parser/src/worker-client.ts Outdated
Comment thread packages/shell-bson-parser/src/worker-client.ts Outdated
Comment thread packages/shell-bson-parser/src/worker-client.ts Outdated
Comment thread packages/shell-bson-parser/src/worker.ts Outdated
const tag =
item._bsontype === 'Binary' &&
(item as { sub_type?: unknown }).sub_type === 4
Object.getPrototypeOf(item) === BSON.UUID.prototype

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not just the same as doing instanceof?

}
const proto = Reflect.getPrototypeOf(value);
return proto !== Object.prototype && proto !== null;
return KNOWN_BSON_PROTOTYPES.has(Object.getPrototypeOf(value));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just rely on the value of the string here? and could we use the [bsonType] symbol that is exported from the BSON library? then its indisputable. The ambiguous case we have to watch out for is plain object with a _bsontype property, but that goes away with the symbol.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to wide range of bson version we support, i'll keep using _bsonType as symbol is available from bson 7.0

@mabaasit
mabaasit force-pushed the web-worker-parsing branch from 1a74167 to ebf38c5 Compare October 9, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants