@lvce-editor/main-process 6.13.0 → 6.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ import { fileURLToPath } from 'node:url';
5
5
  import { inspect } from 'node:util';
6
6
  import { spawn } from 'node:child_process';
7
7
  import { Console } from 'node:console';
8
- import { mkdirSync, createWriteStream, readFileSync, openAsBlob, existsSync, openSync, writeSync, closeSync, rmSync } from 'node:fs';
8
+ import { mkdirSync, createWriteStream, readFileSync, openAsBlob, existsSync, writeFileSync, rmSync, openSync, writeSync, closeSync } from 'node:fs';
9
9
  import * as NodePath from 'node:path';
10
10
  import { dirname, join as join$1 } from 'node:path';
11
11
  import { homedir, tmpdir } from 'node:os';
@@ -4888,6 +4888,106 @@ const openContextMenu = async (menuItems, x, y) => {
4888
4888
  }
4889
4889
  };
4890
4890
 
4891
+ const profileDuration = 10_000;
4892
+ const getFileName$1 = targetName => {
4893
+ const safeTargetName = targetName.replaceAll(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-/, '').replace(/-$/, '') || 'extension-host';
4894
+ return `${safeTargetName}-${Date.now()}.cpuprofile`;
4895
+ };
4896
+ const wait = async () => {
4897
+ await new Promise(resolve => {
4898
+ setTimeout(resolve, profileDuration);
4899
+ });
4900
+ };
4901
+ const takeCpuProfile = async (windowId, workerName) => {
4902
+ const browserWindow = Electron.BrowserWindow.fromId(windowId);
4903
+ if (!browserWindow) {
4904
+ throw new Error(`Browser window not found: ${windowId}`);
4905
+ }
4906
+ const electronDebugger = browserWindow.webContents.debugger;
4907
+ const wasAttached = electronDebugger.isAttached();
4908
+ let filePath = '';
4909
+ let profileStarted = false;
4910
+ let profileStopped = false;
4911
+ let sessionId;
4912
+ let success = false;
4913
+ if (!wasAttached) {
4914
+ electronDebugger.attach();
4915
+ }
4916
+ try {
4917
+ if (workerName) {
4918
+ const {
4919
+ frameTree
4920
+ } = await electronDebugger.sendCommand('Page.getFrameTree');
4921
+ const {
4922
+ targetInfos
4923
+ } = await electronDebugger.sendCommand('Target.getTargets');
4924
+ const target = targetInfos.find(targetInfo => targetInfo.type === 'worker' && targetInfo.title === workerName && targetInfo.parentFrameId === frameTree.frame.id);
4925
+ if (!target) {
4926
+ throw new Error(`Worker not found: ${workerName}`);
4927
+ }
4928
+ const attachResult = await electronDebugger.sendCommand('Target.attachToTarget', {
4929
+ flatten: true,
4930
+ targetId: target.targetId
4931
+ });
4932
+ sessionId = attachResult.sessionId;
4933
+ }
4934
+ await electronDebugger.sendCommand('Profiler.enable', undefined, sessionId);
4935
+ await electronDebugger.sendCommand('Profiler.start', undefined, sessionId);
4936
+ profileStarted = true;
4937
+ await wait();
4938
+ const {
4939
+ profile
4940
+ } = await electronDebugger.sendCommand('Profiler.stop', undefined, sessionId);
4941
+ profileStopped = true;
4942
+ const downloadsPath = Electron.app.getPath('downloads');
4943
+ mkdirSync(downloadsPath, {
4944
+ recursive: true
4945
+ });
4946
+ const targetName = workerName || `Extension Host Window ${windowId}`;
4947
+ filePath = join$1(downloadsPath, getFileName$1(targetName));
4948
+ writeFileSync(filePath, JSON.stringify(profile), {
4949
+ flag: 'wx'
4950
+ });
4951
+ success = true;
4952
+ return filePath;
4953
+ } finally {
4954
+ try {
4955
+ if (profileStarted && !profileStopped && electronDebugger.isAttached()) {
4956
+ await electronDebugger.sendCommand('Profiler.stop', undefined, sessionId);
4957
+ }
4958
+ if (electronDebugger.isAttached()) {
4959
+ await electronDebugger.sendCommand('Profiler.disable', undefined, sessionId);
4960
+ }
4961
+ } finally {
4962
+ try {
4963
+ if (!success && filePath) {
4964
+ rmSync(filePath, {
4965
+ force: true
4966
+ });
4967
+ }
4968
+ if (sessionId && electronDebugger.isAttached()) {
4969
+ await electronDebugger.sendCommand('Target.detachFromTarget', {
4970
+ sessionId
4971
+ });
4972
+ }
4973
+ } finally {
4974
+ if (!wasAttached && electronDebugger.isAttached()) {
4975
+ electronDebugger.detach();
4976
+ }
4977
+ }
4978
+ }
4979
+ }
4980
+ };
4981
+ const takeWindowCpuProfile = async windowId => {
4982
+ number(windowId);
4983
+ return takeCpuProfile(windowId, '');
4984
+ };
4985
+ const takeWorkerCpuProfile = async (windowId, workerName) => {
4986
+ number(windowId);
4987
+ string(workerName);
4988
+ return takeCpuProfile(windowId, workerName);
4989
+ };
4990
+
4891
4991
  const getFileName = workerName => {
4892
4992
  const safeWorkerName = workerName.replaceAll(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-/, '').replace(/-$/, '') || 'worker';
4893
4993
  return `${safeWorkerName}-${Date.now()}.heapsnapshot`;
@@ -6601,6 +6701,8 @@ const commandMap = {
6601
6701
  'ElectronContextMenu.openContextMenu': openContextMenu,
6602
6702
  'ElectronDeveloper.crashMainProcess': crashMainProcess,
6603
6703
  'ElectronDeveloper.getPerformanceEntries': getPerformanceEntries,
6704
+ 'ElectronDeveloper.takeWindowCpuProfile': takeWindowCpuProfile,
6705
+ 'ElectronDeveloper.takeWorkerCpuProfile': takeWorkerCpuProfile,
6604
6706
  'ElectronDeveloper.takeWorkerHeapSnapshot': takeWorkerHeapSnapshot,
6605
6707
  'ElectronDialog.showMessageBox': showMessageBox,
6606
6708
  'ElectronDialog.showOpenDialog': showOpenDialog,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-process",
3
- "version": "6.13.0",
3
+ "version": "6.14.0",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "electron"