Skip to content

fix(shell-bson-parser): drop web-worker package - #934

Closed
mabaasit wants to merge 1 commit into
mainfrom
drop-web-worker-package
Closed

mabaasit wants to merge 1 commit into
mainfrom
drop-web-worker-package

Conversation

@mabaasit

Copy link
Copy Markdown
Collaborator

Description

parse breaks once a consumer bundles this package, because it finds its worker by file path. The CJS build turns import.meta.url into require("node:url"), require("node:path") and __dirname:

  • Web targets fail to build, even when only toJSString is imported. We hit this in the VS Code extension's webview.
  • Bundled Node targets fail at runtime. __dirname points into the consumer's bundle, so worker.js isn't found. web-worker also starts its thread with new Worker(__filename), which is the consumer's bundle.

This PR makes the worker self-contained, so it no longer depends on where files end up after bundling:

The worker is inlined as a string. A build step writes the bundled dist/worker.js into dist/worker-source.js. The client loads it with a lazy import(), so consumers that only use toJSString don't load it.

Browsers start the worker from a blob: URL. The client no longer needs the fetch path or a served worker.js.
Node starts it with worker_threads. worker_threads is loaded via process.getBuiltinModule, which bundlers don't rewrite or polyfill. The worker answers on parentPort itself, so no shim is needed.

The web-worker dependency is removed, along with the CJS build's DefinePlugin and web-worker workarounds.

Open Questions

Checklist

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.

🟡 Changes recommended

The production lazy-loading and consumer-bundling paths central to this fix lack integration regression coverage.

1 open issue
What changed in this PR

This PR replaces web-worker with native worker adapters and inlined worker source so shell-bson-parser can work in bundled consumers without resolving worker scripts by file path.

Changes:

  • Generate a lazily loaded worker-source module and simplify the CommonJS build.
  • Use blob-backed browser workers and native Node.js worker threads.
  • Update lifecycle tests, documentation, and dependencies.
File Description
packages/​shell-bson-parser/​webpack.worker.config.cjs Generate the inlined worker-source module.
packages/​shell-bson-parser/​webpack.cjs.config.cjs Remove shim workarounds and externalize worker-source loading.
packages/​shell-bson-parser/​test/​fixtures/​slow-worker.js Use native Node.js worker messaging.
packages/​shell-bson-parser/​src/​worker.ts Handle requests through Node.js parentPort.
packages/​shell-bson-parser/​src/​worker-types.ts Define shared worker handles and callbacks.
packages/​shell-bson-parser/​src/​worker-source.ts Add a placeholder for generated worker source.
packages/​shell-bson-parser/​src/​worker-client.ts Load source lazily and select native worker adapters.
packages/​shell-bson-parser/​src/​node-worker.ts Implement the Node.js worker adapter.
packages/​shell-bson-parser/​src/​index.spec.ts Update source injection and worker lifecycle coverage.
packages/​shell-bson-parser/​src/​browser-worker.ts Implement blob-backed browser workers and cleanup.
packages/​shell-bson-parser/​README.md Document native workers and bundled source loading.
packages/​shell-bson-parser/​package.json Remove the web-worker dependency.
package-lock.json Remove corresponding dependency lock entries.

Review effort: Balanced

new Blob([code], { type: 'text/javascript' }),
);
return blobUrl;
const source = await import('./worker-source.js');

@nbbeeken nbbeeken 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.

Why doesn't the web-worker package still work if we give it the blob input? I'm thinking the logic that abstracts node vs web is still useful, just how we load the src needs fixing, no?

): WorkerHandle {
// Compass Web injects whatwg-url for bare URL references; it lacks blob APIs.
// Use the native constructor for blob creation and revocation.
const blobUrl = globalThis.URL.createObjectURL(

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.

Suggested change
const blobUrl = globalThis.URL.createObjectURL(
const blobUrl = URL.createObjectURL(

Is there a reason to be so explicit about the globalThis? same for Worker?

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.

Okay, although i closed this PR, I still have it in the code already. This is for compass-web. We resolve url to whatwg-url and that does not have blob-url methods. This breaks for compass-web and i had to go this route for that.

What would be the better alternate here?

// Compass Web injects whatwg-url for bare URL references; it lacks blob APIs.
// Use the native constructor for blob creation and revocation.
const blobUrl = globalThis.URL.createObjectURL(
new Blob([source], { type: 'text/javascript' }),

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.

I recently learned that this is allowed even when you're running node with eval disabled... neat! but concerning? I don't know... nothing to action here lol

Comment on lines +14 to +16
throw new Error(
'@mongodb-js/shell-bson-parser needs a global Worker or process.getBuiltinModule',
);

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.

Well in this case the global wouldn't help this code work right?

@mabaasit

Copy link
Copy Markdown
Collaborator Author

@nbbeeken I am closing this and created #935 to split the exports and avoid vscode webview from pulling in worker code.

@mabaasit mabaasit closed this Oct 27, 2026
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.

3 participants