Skip to content

Commit 4eda97d

Browse files
committed
disableSentry: fix for asar with bundle
1 parent f8c5c7b commit 4eda97d

2 files changed

Lines changed: 90 additions & 19 deletions

File tree

packages/core-extensions/src/disableSentry/host.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,57 @@
1+
import { existsSync } from "node:fs";
12
import { Module } from "node:module";
23
import { join } from "node:path";
34

45
const logger = moonlightHost.getLogger("disableSentry");
56

67
if (moonlightHost.asarPath !== "moonlightDesktop") {
78
try {
8-
const hostSentryPath = require.resolve(join(moonlightHost.asarPath, "node_modules", "@sentry", "electron"));
9-
require.cache[hostSentryPath] = new Module(hostSentryPath, require.cache[require.resolve(moonlightHost.asarPath)]);
10-
require.cache[hostSentryPath]!.exports = {
11-
init: () => {},
12-
captureException: () => {},
13-
setTag: () => {},
14-
setUser: () => {},
15-
captureMessage: () => {}
16-
};
17-
logger.debug("Stubbed Sentry host side!");
9+
if (existsSync(join(moonlightHost.asarPath, "node_modules", "@sentry", "electron"))) {
10+
const hostSentryPath = require.resolve(join(moonlightHost.asarPath, "node_modules", "@sentry", "electron"));
11+
require.cache[hostSentryPath] = new Module(
12+
hostSentryPath,
13+
require.cache[require.resolve(moonlightHost.asarPath)]
14+
);
15+
require.cache[hostSentryPath]!.exports = {
16+
init: () => {},
17+
captureException: () => {},
18+
setTag: () => {},
19+
setUser: () => {},
20+
captureMessage: () => {}
21+
};
22+
logger.debug("Stubbed Sentry host side!");
23+
} else if (existsSync(join(moonlightHost.asarPath, "bundle.js"))) {
24+
let realGetGlobalSentry: any;
25+
Object.defineProperty(Object.prototype, "getGlobalSentry", {
26+
configurable: true,
27+
set(getGlobalSentry) {
28+
realGetGlobalSentry = getGlobalSentry;
29+
if (this.init) {
30+
const oldInit = this.init;
31+
this.init = (buildInfo: any, _sentry: any) => {
32+
oldInit(buildInfo);
33+
};
34+
35+
Object.defineProperty(this, "getGlobalSentry", {
36+
configurable: true,
37+
writable: true,
38+
enumerable: true,
39+
value: getGlobalSentry
40+
});
41+
42+
logger.debug("Patched crash reporter to not have Sentry");
43+
44+
// @ts-expect-error yes it in fact doesn't exist
45+
delete Object.prototype.getGlobalSentry;
46+
}
47+
},
48+
get() {
49+
return realGetGlobalSentry;
50+
}
51+
});
52+
} else {
53+
logger.error("Cannot find Sentry, something in asar changed");
54+
}
1855
} catch (err) {
1956
logger.error("Failed to stub Sentry host side:", err);
2057
}
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { existsSync } from "node:fs";
12
import Module from "node:module";
23
import { resolve } from "node:path";
34
import { constants } from "@moonlight-mod/types";
@@ -7,15 +8,48 @@ const logger = moonlightNode.getLogger("disableSentry");
78

89
const preloadPath = ipcRenderer.sendSync(constants.ipcGetOldPreloadPath);
910
try {
10-
const sentryPath = require.resolve(resolve(preloadPath, "..", "node_modules", "@sentry", "electron"));
11-
require.cache[sentryPath] = new Module(sentryPath, require.cache[require.resolve(preloadPath)]);
12-
require.cache[sentryPath]!.exports = {
13-
init: () => {},
14-
setTag: () => {},
15-
setUser: () => {},
16-
captureMessage: () => {}
17-
};
18-
logger.debug("Stubbed Sentry node side!");
11+
if (existsSync(resolve(preloadPath, "..", "node_modules", "@sentry", "electron"))) {
12+
const sentryPath = require.resolve(resolve(preloadPath, "..", "node_modules", "@sentry", "electron"));
13+
require.cache[sentryPath] = new Module(sentryPath, require.cache[require.resolve(preloadPath)]);
14+
require.cache[sentryPath]!.exports = {
15+
init: () => {},
16+
setTag: () => {},
17+
setUser: () => {},
18+
captureMessage: () => {}
19+
};
20+
logger.debug("Stubbed Sentry node side!");
21+
} else if (existsSync(resolve(preloadPath, "..", "bundle.js"))) {
22+
let realGetGlobalSentry: any;
23+
Object.defineProperty(Object.prototype, "getGlobalSentry", {
24+
configurable: true,
25+
set(getGlobalSentry) {
26+
realGetGlobalSentry = getGlobalSentry;
27+
if (this.init) {
28+
const oldInit = this.init;
29+
this.init = (buildInfo: any, _sentry: any) => {
30+
oldInit(buildInfo);
31+
};
32+
33+
Object.defineProperty(this, "getGlobalSentry", {
34+
configurable: true,
35+
writable: true,
36+
enumerable: true,
37+
value: getGlobalSentry
38+
});
39+
40+
logger.debug("Patched crash reporter to not have Sentry");
41+
42+
// @ts-expect-error yes it in fact doesn't exist
43+
delete Object.prototype.getGlobalSentry;
44+
}
45+
},
46+
get() {
47+
return realGetGlobalSentry;
48+
}
49+
});
50+
} else {
51+
logger.error("Cannot find Sentry, something in asar changed");
52+
}
1953
} catch (err) {
2054
logger.error("Failed to stub Sentry:", err);
2155
}

0 commit comments

Comments
 (0)