Files
wekan/client/lib/bsonBrowserShim.js
T
Lauri OjansivuandClaude Opus 4.8 1d4cfd332d Fix client bundle crash (bson getBuiltinModule) that blanked all boards
After the mongodb/bson 7.3.0 bump, opening any board rendered nothing. bson 7.x
evaluates, at module-load time:

    const { startupSnapshot } = globalThis?.process?.getBuiltinModule('v8') ?? {};

The optional chaining stops before the call, so in the browser (partial
`process` polyfill, no getBuiltinModule) it throws
`TypeError: getBuiltinModule is not a function` while evaluating
client/components/cards/attachments.js (`import { ObjectId } from 'bson'`). That
aborts the client bundle bootstrap partway through client/imports.js, so every
feature imported after it (notifications, swimlanes, rules, ...) is never
registered and the board view dies with `No such template: notifications`.

- Add client/lib/bsonBrowserShim.js (imported first in client/main.js) that
  installs a no-op process.getBuiltinModule in the browser, so bson takes its
  intended `?? {}` fallback (there is no V8 startup snapshot in a browser).
- Add unit tests (client/lib/tests/bsonBrowserShim.tests.js), 6 cases.
- Harden the #5686 Playwright spec (specs/34) to actually exercise the gesture
  now that boards render: select real checklist text and fire the real outside
  click; DOM-aware "outside the card" point for the control case.
- Update CHANGELOG upcoming.

Verified against a running instance: boards render again, Template.notifications
is registered, specs 34 and 35 pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 12:41:07 +03:00

50 lines
1.9 KiB
JavaScript

// Browser shim for `process.getBuiltinModule`, imported FIRST in the client
// entry so it runs before any module that pulls in bson.
//
// Regression (after the mongodb/bson 7.3.0 bump): bson 7.x evaluates, at module
// load time,
//
// const { startupSnapshot } = globalThis?.process?.getBuiltinModule('v8') ?? {};
//
// The optional chaining stops BEFORE the call, so when a partial `process`
// polyfill is present (Meteor/rspack provide one in the browser) but has no
// `getBuiltinModule`, the call throws `TypeError: getBuiltinModule is not a
// function`. That throw happens while `client/components/cards/attachments.js`
// (`import { ObjectId } from 'bson'`) is being evaluated, which aborts the
// client bundle bootstrap partway through `client/imports.js` — every feature
// imported after it (notifications, rules, swimlanes, …) is never registered,
// so the board view throws `No such template: notifications` and renders
// nothing.
//
// Installing a no-op `getBuiltinModule` makes bson take its intended browser
// fallback (`?? {}`): there is no V8 startup snapshot in a browser, so
// `undefined` is the correct value.
/**
* Ensure `glob.process.getBuiltinModule` is callable. Only adds the method when
* a `process` object exists but lacks it (the exact browser-polyfill case);
* never overwrites a real Node implementation. Returns true when it installed
* the shim.
*
* @param {*} glob a global-like object (e.g. globalThis)
* @returns {boolean}
*/
export function installGetBuiltinModuleShim(glob) {
if (
glob &&
glob.process &&
typeof glob.process.getBuiltinModule !== 'function'
) {
try {
glob.process.getBuiltinModule = () => undefined;
return true;
} catch (e) {
// `process` may be frozen in some environments — nothing more we can do.
return false;
}
}
return false;
}
installGetBuiltinModuleShim(typeof globalThis !== 'undefined' ? globalThis : undefined);