Skip to content

Commit 2b1ed1b

Browse files
committed
feat: Support Electron
1 parent 6f82262 commit 2b1ed1b

File tree

6 files changed

+661
-23
lines changed

6 files changed

+661
-23
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
"vitest": "^3.0.4",
5959
"webpack": "^5.97.1",
6060
"node-loader": "^2.1.0",
61-
"esbuild": "^0.24.2"
61+
"esbuild": "^0.24.2",
62+
"electron": "^34.1.0",
63+
"@electron/rebuild": "^3.7.1"
6264
},
6365
"sideEffects": false,
6466
"volta": {

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
3737
return require(binaryPath);
3838
}
3939

40+
if (process.versions.electron) {
41+
try {
42+
return require('../build/Release/sentry_cpu_profiler.node');
43+
} catch (e) {
44+
console.warn(`The '@sentry/profiling-node' binary could not be found. Use '@electron/rebuild' to ensure the native module is built for Electron.`);
45+
throw e;
46+
}
47+
}
48+
4049
// We need the fallthrough so that in the end, we can fallback to the dynamic require.
4150
// This is for cases where precompiled binaries were not provided, but may have been compiled from source.
4251
if (platform === 'darwin') {

test/bundle.mjs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import { inspect } from 'node:util';
2-
31
import { CpuProfilerBindings, ProfileFormat } from '@sentry-internal/node-cpu-profiler';
42

3+
process.on('uncaughtException', (err) => {
4+
console.error(err);
5+
process.exit(1);
6+
});
7+
process.on('unhandledRejection', (err) => {
8+
console.error(err);
9+
process.exit(1);
10+
});
11+
512
CpuProfilerBindings.startProfiling('test');
613

714
setTimeout(() => {
815
const report = CpuProfilerBindings.stopProfiling('test', ProfileFormat.THREAD);
9-
console.log(inspect(report, false, null, true));
16+
console.assert(report);
17+
process.exit(0);
1018
}, 5000);

test/electron.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { execSync, spawnSync, } from 'child_process';
2+
import { default as getElectronPath } from 'electron';
3+
import { describe, expect, test } from "vitest";
4+
5+
describe('Electron', () => {
6+
test('should work', () => {
7+
execSync('yarn rebuild', { cwd: __dirname });
8+
9+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
10+
// @ts-ignore - The Electron types don't detail the default export outside
11+
// of an Electron process.
12+
const result = spawnSync(getElectronPath, [__dirname]);
13+
expect(result.status).toBe(0);
14+
});
15+
});

test/prepare.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ console.log('Writing package.json...');
3333
writeFileSync(join(__dirname, 'package.json'), `{
3434
"name": "node-cpu-profiler-test",
3535
"license": "MIT",
36+
"main": "bundle.mjs",
37+
"scripts": {
38+
"rebuild": "electron-rebuild"
39+
},
3640
"dependencies": {
3741
"@sentry-internal/node-cpu-profiler": "file:../${normalizedName}-${pkgJson.version}.tgz"
3842
}

0 commit comments

Comments
 (0)