@lvce-editor/main-process 6.8.1 → 6.8.2

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 { createWriteStream, readFileSync, openAsBlob, existsSync } from 'node:fs';
8
+ import { createWriteStream, readFileSync, openAsBlob, existsSync, mkdirSync, openSync, writeSync, closeSync, rmSync } from 'node:fs';
9
9
  import { tmpdir, homedir } from 'node:os';
10
10
  import * as NodePath from 'node:path';
11
11
  import { dirname, join as join$1 } from 'node:path';
@@ -4268,6 +4268,100 @@ const openContextMenu = async (menuItems, x, y) => {
4268
4268
  }
4269
4269
  };
4270
4270
 
4271
+ const getFileName = workerName => {
4272
+ const safeWorkerName = workerName.replaceAll(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-/, '').replace(/-$/, '') || 'worker';
4273
+ return `${safeWorkerName}-${Date.now()}.heapsnapshot`;
4274
+ };
4275
+ const takeWorkerHeapSnapshot = async (windowId, workerName) => {
4276
+ number(windowId);
4277
+ string(workerName);
4278
+ const browserWindow = Electron.BrowserWindow.fromId(windowId);
4279
+ if (!browserWindow) {
4280
+ throw new Error(`Browser window not found: ${windowId}`);
4281
+ }
4282
+ const electronDebugger = browserWindow.webContents.debugger;
4283
+ const wasAttached = electronDebugger.isAttached();
4284
+ let fileDescriptor;
4285
+ let filePath = '';
4286
+ let sessionId = '';
4287
+ let success = false;
4288
+ if (!wasAttached) {
4289
+ electronDebugger.attach();
4290
+ }
4291
+ try {
4292
+ const {
4293
+ frameTree
4294
+ } = await electronDebugger.sendCommand('Page.getFrameTree');
4295
+ const {
4296
+ targetInfos
4297
+ } = await electronDebugger.sendCommand('Target.getTargets');
4298
+ const target = targetInfos.find(targetInfo => targetInfo.type === 'worker' && targetInfo.title === workerName && targetInfo.parentFrameId === frameTree.frame.id);
4299
+ if (!target) {
4300
+ throw new Error(`Worker not found: ${workerName}`);
4301
+ }
4302
+ const attachResult = await electronDebugger.sendCommand('Target.attachToTarget', {
4303
+ flatten: true,
4304
+ targetId: target.targetId
4305
+ });
4306
+ sessionId = attachResult.sessionId;
4307
+ const downloadsPath = Electron.app.getPath('downloads');
4308
+ mkdirSync(downloadsPath, {
4309
+ recursive: true
4310
+ });
4311
+ filePath = join$1(downloadsPath, getFileName(workerName));
4312
+ fileDescriptor = openSync(filePath, 'wx');
4313
+ const currentFileDescriptor = fileDescriptor;
4314
+ let writeError;
4315
+ const handleMessage = (_event, method, parameters, messageSessionId) => {
4316
+ if (method !== 'HeapProfiler.addHeapSnapshotChunk' || messageSessionId !== sessionId || writeError) {
4317
+ return;
4318
+ }
4319
+ try {
4320
+ writeSync(currentFileDescriptor, parameters.chunk);
4321
+ } catch (error) {
4322
+ writeError = error;
4323
+ }
4324
+ };
4325
+ electronDebugger.on('message', handleMessage);
4326
+ try {
4327
+ await electronDebugger.sendCommand('HeapProfiler.enable', undefined, sessionId);
4328
+ await electronDebugger.sendCommand('HeapProfiler.takeHeapSnapshot', {
4329
+ reportProgress: false
4330
+ }, sessionId);
4331
+ if (writeError) {
4332
+ throw writeError;
4333
+ }
4334
+ } finally {
4335
+ electronDebugger.off('message', handleMessage);
4336
+ }
4337
+ success = true;
4338
+ return filePath;
4339
+ } finally {
4340
+ try {
4341
+ if (fileDescriptor !== undefined) {
4342
+ closeSync(fileDescriptor);
4343
+ }
4344
+ if (!success && filePath) {
4345
+ rmSync(filePath, {
4346
+ force: true
4347
+ });
4348
+ }
4349
+ } finally {
4350
+ try {
4351
+ if (sessionId && electronDebugger.isAttached()) {
4352
+ await electronDebugger.sendCommand('Target.detachFromTarget', {
4353
+ sessionId
4354
+ });
4355
+ }
4356
+ } finally {
4357
+ if (!wasAttached && electronDebugger.isAttached()) {
4358
+ electronDebugger.detach();
4359
+ }
4360
+ }
4361
+ }
4362
+ }
4363
+ };
4364
+
4271
4365
  const getPerformanceEntries = () => {
4272
4366
  const entries = getEntries();
4273
4367
  const {
@@ -5428,6 +5522,10 @@ const getDomTree = async view => {
5428
5522
  const result = await getBodyOuterHtml(view);
5429
5523
  return getSlimCode(result);
5430
5524
  };
5525
+ const capturePage = async view => {
5526
+ const image = await view.webContents.capturePage();
5527
+ return image.toDataURL();
5528
+ };
5431
5529
  const insertCss = async (view, code) => {
5432
5530
  const {
5433
5531
  webContents
@@ -6857,6 +6955,7 @@ const commandMap = {
6857
6955
  'ElectronContextMenu.openContextMenu': openContextMenu,
6858
6956
  'ElectronDeveloper.crashMainProcess': crashMainProcess,
6859
6957
  'ElectronDeveloper.getPerformanceEntries': getPerformanceEntries,
6958
+ 'ElectronDeveloper.takeWorkerHeapSnapshot': takeWorkerHeapSnapshot,
6860
6959
  'ElectronDialog.showMessageBox': showMessageBox,
6861
6960
  'ElectronDialog.showOpenDialog': showOpenDialog,
6862
6961
  'ElectronDialog.showSaveDialog': showSaveDialog,
@@ -6885,6 +6984,7 @@ const commandMap = {
6885
6984
  'ElectronWebContentsViewFunctions.addToWindow': addToWindow,
6886
6985
  'ElectronWebContentsViewFunctions.backward': wrapBrowserViewCommand(backward),
6887
6986
  'ElectronWebContentsViewFunctions.cancelNavigation': wrapBrowserViewCommand(cancelNavigation),
6987
+ 'ElectronWebContentsViewFunctions.capturePage': wrapBrowserViewCommand(capturePage),
6888
6988
  'ElectronWebContentsViewFunctions.copyImageAt': wrapBrowserViewCommand(copyImageAt),
6889
6989
  'ElectronWebContentsViewFunctions.executeJavaScript': wrapBrowserViewCommand(executeJavaScript),
6890
6990
  'ElectronWebContentsViewFunctions.focus': wrapBrowserViewCommand(focus),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/main-process",
3
- "version": "6.8.1",
3
+ "version": "6.8.2",
4
4
  "keywords": [
5
5
  "lvce-editor",
6
6
  "electron"