From 65ebf9ef9303a1f9892d7950aa88a781bf0140fc Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Fri, 13 Jun 2025 22:12:07 +0800 Subject: [PATCH] fix: sqlite wasm in electron production (#3920) --- .github/workflows/build-desktop.yml | 1 + apps/desktop/layer/main/src/index.ts | 15 +++++++++++ apps/desktop/layer/main/src/init.ts | 30 ++++++++++++++-------- package.json | 3 ++- patches/@sentry__electron.patch | 38 ++++++++++++++++++++++++++++ pnpm-lock.yaml | 7 +++-- 6 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 patches/@sentry__electron.patch diff --git a/.github/workflows/build-desktop.yml b/.github/workflows/build-desktop.yml index 091cbd31d..ad2987e8c 100644 --- a/.github/workflows/build-desktop.yml +++ b/.github/workflows/build-desktop.yml @@ -6,6 +6,7 @@ on: - "**" paths: - "apps/desktop/**" + - "packages/**" - "pnpm-lock.yaml" - ".github/workflows/build-desktop.yml" workflow_dispatch: diff --git a/apps/desktop/layer/main/src/index.ts b/apps/desktop/layer/main/src/index.ts index 2fbb25cff..2129ea5e2 100644 --- a/apps/desktop/layer/main/src/index.ts +++ b/apps/desktop/layer/main/src/index.ts @@ -112,6 +112,21 @@ function bootstrap() { callback({ cancel: false, requestHeaders: details.requestHeaders }) }) + // It's wired, I can not make this work. For request to worker.js, it will apply an incomplete header. + // not-set cross-origin-embedder-policy: + // Maybe this is related to https://github.com/electron/electron/issues/45168 + // So I have to use `app.commandLine.appendSwitch("enable-features", "SharedArrayBuffer")` + // + // session.defaultSession.webRequest.onHeadersReceived((details, callback) => { + // callback({ + // responseHeaders: { + // ...details.responseHeaders, + // "Cross-Origin-Opener-Policy": "same-origin", + // "Cross-Origin-Embedder-Policy": "require-corp", + // }, + // }) + // }) + mainWindow = createMainWindow() updateProxy() diff --git a/apps/desktop/layer/main/src/init.ts b/apps/desktop/layer/main/src/init.ts index cb3e74205..065e4b765 100644 --- a/apps/desktop/layer/main/src/init.ts +++ b/apps/desktop/layer/main/src/init.ts @@ -27,6 +27,25 @@ if (process.argv.length === 3 && process.argv[2]!.startsWith("follow-dev:")) { * Mandatory and fast initializers for the app */ export function initializeAppStage0() { + app.commandLine.appendSwitch("enable-features", "SharedArrayBuffer") + + // https://github.com/getsentry/sentry-electron/issues/661 + protocol.registerSchemesAsPrivileged([ + { + scheme: "sentry-ipc", + privileges: { bypassCSP: true, corsEnabled: true, supportFetchAPI: true, secure: true }, + }, + { + scheme: "app", + privileges: { + standard: true, + bypassCSP: true, + supportFetchAPI: true, + secure: true, + }, + }, + ]) + initializeSentry() initializeIpcServices() } @@ -59,17 +78,6 @@ export const initializeAppStage1 = () => { registerPushNotifications() clearCacheCronJob() checkAndCleanCodeCache() - - protocol.registerSchemesAsPrivileged([ - { - scheme: "app", - privileges: { - standard: true, - bypassCSP: true, - supportFetchAPI: true, - }, - }, - ]) } let contextMenuDisposer: () => void diff --git a/package.json b/package.json index 75b533d26..77409b8a5 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,8 @@ "react-native-sheet-transitions": "patches/react-native-sheet-transitions.patch", "react-native-screens": "patches/react-native-screens.patch", "expo-image": "patches/expo-image.patch", - "react-native-ios-utilities": "patches/react-native-ios-utilities.patch" + "react-native-ios-utilities": "patches/react-native-ios-utilities.patch", + "@sentry/electron": "patches/@sentry__electron.patch" }, "overrides": { "@electron/node-gyp": "10.2.0-electron.2", diff --git a/patches/@sentry__electron.patch b/patches/@sentry__electron.patch new file mode 100644 index 000000000..2aa4921df --- /dev/null +++ b/patches/@sentry__electron.patch @@ -0,0 +1,38 @@ +diff --git a/esm/main/ipc.js b/esm/main/ipc.js +index 8edfaf4660734f9120a6d5f5f806688c25a3a026..a692b1c16470b0dbed7c53e1c9aeff8a456f1c5a 100644 +--- a/esm/main/ipc.js ++++ b/esm/main/ipc.js +@@ -111,14 +111,6 @@ function configureProtocol(client, options) { + if (app.isReady()) { + throw new Error("Sentry SDK should be initialized before the Electron app 'ready' event is fired"); + } +- protocol.registerSchemesAsPrivileged([SENTRY_CUSTOM_SCHEME]); +- // We Proxy this function so that later user calls to registerSchemesAsPrivileged don't overwrite our custom scheme +- // eslint-disable-next-line @typescript-eslint/unbound-method +- protocol.registerSchemesAsPrivileged = new Proxy(protocol.registerSchemesAsPrivileged, { +- apply: (target, __, args) => { +- target([...args[0], SENTRY_CUSTOM_SCHEME]); +- }, +- }); + const rendererStatusChanged = createRendererAnrStatusHandler(client); + app + .whenReady() +diff --git a/main/ipc.js b/main/ipc.js +index 01fc75bdf031b62195504cc0bf7055ebbf15b641..0519242ba5f391f9f618229044948feefeae1de8 100644 +--- a/main/ipc.js ++++ b/main/ipc.js +@@ -111,14 +111,6 @@ function configureProtocol(client, options) { + if (electron.app.isReady()) { + throw new Error("Sentry SDK should be initialized before the Electron app 'ready' event is fired"); + } +- electron.protocol.registerSchemesAsPrivileged([SENTRY_CUSTOM_SCHEME]); +- // We Proxy this function so that later user calls to registerSchemesAsPrivileged don't overwrite our custom scheme +- // eslint-disable-next-line @typescript-eslint/unbound-method +- electron.protocol.registerSchemesAsPrivileged = new Proxy(electron.protocol.registerSchemesAsPrivileged, { +- apply: (target, __, args) => { +- target([...args[0], SENTRY_CUSTOM_SCHEME]); +- }, +- }); + const rendererStatusChanged = rendererAnr.createRendererAnrStatusHandler(client); + electron.app + .whenReady() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5b484c297..3dde79096 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,6 +30,9 @@ patchedDependencies: '@pengx17/electron-forge-maker-appimage': hash: 5b5ab1ba36e8c0d7ffee912ebf29c1a18bc101c9c661ceb1bb0bda3deaf4c667 path: patches/@pengx17__electron-forge-maker-appimage.patch + '@sentry/electron': + hash: b5efa039abfa14f7833b762e8a7c1c3ae147fda22954d860ffa3387db9acf8eb + path: patches/@sentry__electron.patch daisyui@4.12.24: hash: d393ab1cbfbfcff21dce0796a59c2d8a37e2c6dd634a8ab476cbc67e47b93d9c path: patches/daisyui@4.12.24.patch @@ -353,7 +356,7 @@ importers: version: 1.0.1 '@sentry/electron': specifier: 6.6.0 - version: 6.6.0 + version: 6.6.0(patch_hash=b5efa039abfa14f7833b762e8a7c1c3ae147fda22954d860ffa3387db9acf8eb) builder-util-runtime: specifier: 9.3.1 version: 9.3.1 @@ -21443,7 +21446,7 @@ snapshots: '@sentry/core@9.22.0': {} - '@sentry/electron@6.6.0': + '@sentry/electron@6.6.0(patch_hash=b5efa039abfa14f7833b762e8a7c1c3ae147fda22954d860ffa3387db9acf8eb)': dependencies: '@sentry/browser': 9.18.0 '@sentry/core': 9.18.0