@lvce-editor/process-explorer 3.2.0 → 3.3.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.
Files changed (2) hide show
  1. package/dist/index.js +51 -11
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import { ElectronMessagePortRpcClient, WebSocketRpcParent, NodeWebSocketRpcClient, NodeWorkerRpcClient, NodeForkedProcessRpcClient, ElectronUtilityProcessRpcClient } from '@lvce-editor/rpc';
2
2
  import { object, number, string } from '@lvce-editor/assert';
3
3
  import { RpcId, MainProcess, set as set$2 } from '@lvce-editor/rpc-registry';
4
+ import { execFile as execFile$2 } from 'node:child_process';
5
+ import { promisify } from 'node:util';
4
6
  import { VError } from '@lvce-editor/verror';
5
7
  import { readFile } from 'node:fs/promises';
6
8
  import { join } from 'node:path';
7
- import { execFile as execFile$1 } from 'node:child_process';
8
- import { promisify } from 'node:util';
9
9
 
10
10
  const state = {
11
11
  commandMap: {}
@@ -70,6 +70,9 @@ const createWebSocketRpc = async (create, handle, request) => {
70
70
  });
71
71
  };
72
72
  const handleWebSocket = async (handle, request, rpcId) => {
73
+ if (!handle || !request) {
74
+ return;
75
+ }
73
76
  object(handle);
74
77
  object(request);
75
78
  const rpc = await createWebSocketRpc(NodeWebSocketRpcClient.create, handle, request);
@@ -85,13 +88,50 @@ const getModule$1 = async () => {
85
88
  return Promise.resolve().then(function () { return ListProcessesWithMemoryUsageUnix; });
86
89
  };
87
90
 
88
- const listProcessesWithMemoryUsage$2 = async rootPid => {
91
+ const listProcessesWithMemoryUsage$2 = async (rootPid, includeElectronData = true) => {
89
92
  const module = await getModule$1();
90
- return module.listProcessesWithMemoryUsage(rootPid);
93
+ return module.listProcessesWithMemoryUsage(rootPid, includeElectronData);
91
94
  };
92
95
 
93
- const getMainProcessId = () => {
94
- return process.ppid;
96
+ const execFile$1 = promisify(execFile$2);
97
+ const parseProcessId = stdout => {
98
+ const parentProcessId = Number(stdout.trim());
99
+ if (!Number.isFinite(parentProcessId) || parentProcessId <= 0) {
100
+ return 0;
101
+ }
102
+ return parentProcessId;
103
+ };
104
+ const getParentProcessIdUnix = async pid => {
105
+ const {
106
+ stdout
107
+ } = await execFile$1('ps', ['-o', 'ppid=', '-p', String(pid)]);
108
+ return parseProcessId(stdout);
109
+ };
110
+ const getParentProcessIdWindows = async pid => {
111
+ const {
112
+ stdout
113
+ } = await execFile$1('powershell.exe', ['-NoProfile', '-Command', `(Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}").ParentProcessId`]);
114
+ return parseProcessId(stdout);
115
+ };
116
+ const getParentProcessId = async pid => {
117
+ if (process.platform === 'win32') {
118
+ return getParentProcessIdWindows(pid);
119
+ }
120
+ return getParentProcessIdUnix(pid);
121
+ };
122
+ const getRemoteRootProcessId = async (childProcessId = process.ppid) => {
123
+ try {
124
+ const parentProcessId = await getParentProcessId(childProcessId);
125
+ return parentProcessId || process.ppid;
126
+ } catch {
127
+ return process.ppid;
128
+ }
129
+ };
130
+ const getMainProcessId = (includeElectronData = true, childProcessId = process.ppid) => {
131
+ if (includeElectronData) {
132
+ return Promise.resolve(process.ppid);
133
+ }
134
+ return getRemoteRootProcessId(childProcessId);
95
135
  };
96
136
 
97
137
  const commandMap = {
@@ -319,13 +359,13 @@ const CommandLine = 2;
319
359
 
320
360
  // listProcesses windows implementation based on https://github.com/microsoft/vscode/blob/c0769274fa136b45799edeccc0d0a2f645b75caf/src/vs/base/node/ps.ts (License MIT)
321
361
 
322
- const listProcessesWithMemoryUsage$1 = async rootPid => {
362
+ const listProcessesWithMemoryUsage$1 = async (rootPid, includeElectronData = true) => {
323
363
  try {
324
364
  const processList = await getProcessList(rootPid, CommandLine | Memory);
325
365
  if (!processList) {
326
366
  throw new VError(`Root process ${rootPid} not found`);
327
367
  }
328
- const pidMap = await createPidMap();
368
+ const pidMap = includeElectronData ? await createPidMap() : {};
329
369
  const completeProcessList = await addCpuUsage(processList);
330
370
  const result = toResult(completeProcessList, rootPid, pidMap);
331
371
  return result;
@@ -417,7 +457,7 @@ const addAccurateMemoryUsage = async process => {
417
457
 
418
458
  const SIGINT = 'SIGINT';
419
459
 
420
- const execFile = promisify(execFile$1);
460
+ const execFile = promisify(execFile$2);
421
461
  const getPsOutput = async () => {
422
462
  try {
423
463
  const {
@@ -512,10 +552,10 @@ const parsePsOutput = (stdout, rootPid, pidMap) => {
512
552
  return result;
513
553
  };
514
554
 
515
- const listProcessesWithMemoryUsage = async rootPid => {
555
+ const listProcessesWithMemoryUsage = async (rootPid, includeElectronData = true) => {
516
556
  // console.time('getPsOutput')
517
557
  const stdout = await getPsOutput();
518
- const pidMap = await createPidMap();
558
+ const pidMap = includeElectronData ? await createPidMap() : {};
519
559
  // console.log({ stdout })
520
560
  // console.timeEnd('getPsOutput')
521
561
  // console.time('parsePsOutput')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/process-explorer",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "Process Explorer",
5
5
  "main": "dist/index.js",
6
6
  "bin": "bin/processExplorer.js",