Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/mongodb-constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"reformat": "npm run prettier -- --write ."
},
"peerDependencies": {
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0"
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2"
},
"devDependencies": {
"@mongodb-js/eslint-config-devtools": "^0.11.13",
Expand All @@ -63,7 +63,7 @@
"@types/mocha": "^9.1.1",
"@types/semver": "^7.8.0",
"acorn": "^8.18.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2",
"chai": "^4.5.0",
"depcheck": "^1.4.7",
"eslint": "^7.25.0 || ^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/mongodb-log-writer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@
"typescript": "^5.9.3"
},
"optionalDependencies": {
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0"
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2"
}
}
4 changes: 2 additions & 2 deletions packages/mongodb-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"reservoir": "^0.1.2"
},
"optionalDependencies": {
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2",
"cli-table": "^0.3.4",
"js-yaml": "^5.2.2",
"mongodb": "^6.9.0 || ^7.0.0",
Expand All @@ -93,7 +93,7 @@
"@types/node": "^25.5.2",
"@types/reservoir": "^0.1.0",
"ajv": "^8.20.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2",
"chai": "^4.5.0",
"depcheck": "^1.4.7",
"eslint": "^7.25.0 || ^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/mongodb-ts-autocomplete/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@types/chai": "^4.2.21",
"@types/mocha": "^9.1.1",
"@types/node": "^25.5.2",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2",
"chai": "^4.5.0",
"depcheck": "^1.4.7",
"eslint": "^7.25.0 || ^8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/mql-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"zod": "^3.24.2"
},
"dependencies": {
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2",
"mongodb": "^7.5.0"
}
}
4 changes: 2 additions & 2 deletions packages/shell-bson-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"web-worker": "^1.5.0"
},
"peerDependencies": {
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0"
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2"
},
"devDependencies": {
"@mongodb-js/eslint-config-devtools": "^0.11.13",
Expand All @@ -68,7 +68,7 @@
"@types/estree": "^0.0.41",
"@types/mocha": "^9.1.1",
"benchmark": "^2.1.4",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.0.0",
"bson": "^4.6.3 || ^5 || ^6.10.3 || ^7.3.2",
"chai": "^4.5.0",
"depcheck": "^1.4.7",
"eslint": "^7.25.0 || ^8.0.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/shell-bson-parser/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('shell-bson-parser with webworker processing', function () {
expect(res).to.deep.equal(expected);
});
}

it('keeps the error type of a failed parse', async function () {
try {
await api.parse('{ a: }');
expect.fail('Expected parse to throw');
} catch (err) {
expect(err).to.be.instanceOf(SyntaxError);
expect((err as Error).name).to.equal('SyntaxError');
}
});
});

describe('restrictGlobalScope', function () {
Expand Down
53 changes: 39 additions & 14 deletions packages/shell-bson-parser/src/worker-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as WebWorkerModule from 'web-worker';
const WebWorker = (WebWorkerModule as unknown as { default: typeof Worker })
.default;
import { trackBSON, untrackBSON } from './structured-clone-bson.js';
import type { WorkerResponse } from './worker-types.js';

Expand Down Expand Up @@ -33,26 +30,53 @@ const pending = new Map<
}
>();

// jsdom defines `window` but no `Worker`, so `typeof window` alone cannot tell
// a real browser apart from a Node test environment.
const isNodeEnv =
typeof window === 'undefined' &&
typeof process !== 'undefined' &&
!!process.versions?.node;
!!process.versions?.node &&
typeof (globalThis as { Worker?: unknown }).Worker === 'undefined';

async function loadWebWorker(): Promise<typeof Worker> {
// web-worker's CJS build mistakes jsdom for a browser and throws when
// constructing a worker; its ESM entry resolves import.meta.url correctly.
// The CJS bundle keeps this a native import() (see webpack.cjs.config.cjs).
const WebWorkerModule = (await import('web-worker')) as unknown as {
default: typeof Worker;
};
return WebWorkerModule.default;
}

async function getWorkerScriptUrl(): Promise<string> {
if (process.env.TEST_WORKER_SCRIPT_URL) {
return new URL(
process.env.TEST_WORKER_SCRIPT_URL,
import.meta.url,
).toString();
const testWorkerScriptUrl =
typeof process !== 'undefined'
? process.env?.TEST_WORKER_SCRIPT_URL
: undefined;

// eslint-disable-next-line no-console
console.log(`
testWorkerScriptUrl: ${String(testWorkerScriptUrl)}
isNodeEnv: ${String(isNodeEnv)}
window: ${String(typeof window !== 'undefined')}
process: ${String(typeof process !== 'undefined')}
cjs or esm: ${String(typeof require !== 'undefined')}
`);

// Bundlers rewrite `new URL(<dynamic>, import.meta.url)` into an unresolvable
// context module and inline a bare `import.meta.url` as a build-time path, so
// resolve the override against the emitted worker URL instead.
const workerScriptUrl = new URL('./worker.js', import.meta.url);

if (testWorkerScriptUrl) {
return new URL(testWorkerScriptUrl, workerScriptUrl).toString();
}
if (isNodeEnv) {
return new URL('./worker.js', import.meta.url).toString();
return workerScriptUrl.toString();
}

// On browser env we want to fetch and blob so that the worker
// script can run on the web running locally.
const scriptUrl = new URL('./worker.js', import.meta.url);
const response = await fetch(scriptUrl);
// script can run on atlas-cloud running locally.
const response = await fetch(workerScriptUrl);
if (!response.ok) {
throw new Error(
`Failed to fetch shell-bson-parser worker script: ${response.status} ${response.statusText}`,
Expand All @@ -73,6 +97,7 @@ function createWorker(): Promise<Worker> {

workerPromise = (async () => {
const scriptUrl = await getWorkerScriptUrl();
const WebWorker = await loadWebWorker();
const newWorker = new WebWorker(scriptUrl, { type: 'module' });
const onMessageHandler = (event: MessageEvent<WorkerResponse>) => {
const response = event.data;
Expand Down
16 changes: 15 additions & 1 deletion packages/shell-bson-parser/webpack.cjs.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@ module.exports = {
output: {
path: path.resolve(__dirname, 'dist', 'cjs'),
filename: 'index.js',
// Keep the emitted worker at a stable `worker.js` so runtime resolution
// next to this bundle finds it instead of a content-hashed name.
assetModuleFilename: '[name][ext]',
// Allow native `import()` for the `web-worker` external below.
environment: {
dynamicImport: true,
},
library: {
type: 'commonjs2',
},
},
externals: [/^acorn/, /^javascript-stringify/, /^web-worker/, /^bson/],
// `web-worker` must resolve through its ESM entry: its CJS build mis-detects
// jsdom as a browser and throws when constructing a worker.
externals: [
{ 'web-worker': 'import web-worker' },
/^acorn/,
/^javascript-stringify/,
/^bson/,
],
module: {
parser: {
javascript: {
Expand Down
Loading