@lvce-editor/process-explorer 3.2.0 → 3.4.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.
- package/dist/index.js +70 -12
- 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,53 @@ 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
|
|
94
|
-
|
|
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 = ({
|
|
131
|
+
childProcessId = process.ppid,
|
|
132
|
+
includeElectronData = true
|
|
133
|
+
} = {}) => {
|
|
134
|
+
if (includeElectronData) {
|
|
135
|
+
return Promise.resolve(process.ppid);
|
|
136
|
+
}
|
|
137
|
+
return getRemoteRootProcessId(childProcessId);
|
|
95
138
|
};
|
|
96
139
|
|
|
97
140
|
const commandMap = {
|
|
@@ -256,6 +299,9 @@ const getFallbackPatternName = cmd => {
|
|
|
256
299
|
return fallbackProcessNamePatterns.find(pattern => pattern.matches(cmd))?.name || '';
|
|
257
300
|
};
|
|
258
301
|
|
|
302
|
+
const includesPackagePath = (cmd, packageName) => {
|
|
303
|
+
return cmd.includes(`@lvce-editor/${packageName}/`) || cmd.includes(`@lvce-editor\\${packageName}\\`);
|
|
304
|
+
};
|
|
259
305
|
const processNamePatterns = [{
|
|
260
306
|
matches: cmd => cmd.includes('--type=zygote'),
|
|
261
307
|
name: 'zygote'
|
|
@@ -265,12 +311,24 @@ const processNamePatterns = [{
|
|
|
265
311
|
}, {
|
|
266
312
|
matches: cmd => cmd.includes('extensionHostMain.js'),
|
|
267
313
|
name: 'extension-host'
|
|
314
|
+
}, {
|
|
315
|
+
matches: cmd => includesPackagePath(cmd, 'file-system-process'),
|
|
316
|
+
name: 'file-system-process'
|
|
317
|
+
}, {
|
|
318
|
+
matches: cmd => includesPackagePath(cmd, 'file-watcher-process'),
|
|
319
|
+
name: 'file-watcher-process'
|
|
320
|
+
}, {
|
|
321
|
+
matches: cmd => includesPackagePath(cmd, 'typescript-compile-process'),
|
|
322
|
+
name: 'typescript-compile-process'
|
|
268
323
|
}, {
|
|
269
324
|
matches: cmd => cmd.includes('ptyHostMain.js'),
|
|
270
325
|
name: 'pty-host'
|
|
271
326
|
}, {
|
|
272
|
-
matches: cmd => cmd.includes('--lvce-window-kind=process-explorer'),
|
|
327
|
+
matches: cmd => cmd.includes('--lvce-window-kind=process-explorer') || cmd.includes('processExplorerMain.ts') || cmd.includes('processExplorerMain.js') || cmd.includes('processExplorer.js'),
|
|
273
328
|
name: 'process-explorer'
|
|
329
|
+
}, {
|
|
330
|
+
matches: cmd => cmd.includes('sharedProcessMain.js'),
|
|
331
|
+
name: 'shared-process'
|
|
274
332
|
}];
|
|
275
333
|
|
|
276
334
|
const getPatternName = cmd => {
|
|
@@ -319,13 +377,13 @@ const CommandLine = 2;
|
|
|
319
377
|
|
|
320
378
|
// listProcesses windows implementation based on https://github.com/microsoft/vscode/blob/c0769274fa136b45799edeccc0d0a2f645b75caf/src/vs/base/node/ps.ts (License MIT)
|
|
321
379
|
|
|
322
|
-
const listProcessesWithMemoryUsage$1 = async rootPid => {
|
|
380
|
+
const listProcessesWithMemoryUsage$1 = async (rootPid, includeElectronData = true) => {
|
|
323
381
|
try {
|
|
324
382
|
const processList = await getProcessList(rootPid, CommandLine | Memory);
|
|
325
383
|
if (!processList) {
|
|
326
384
|
throw new VError(`Root process ${rootPid} not found`);
|
|
327
385
|
}
|
|
328
|
-
const pidMap = await createPidMap();
|
|
386
|
+
const pidMap = includeElectronData ? await createPidMap() : {};
|
|
329
387
|
const completeProcessList = await addCpuUsage(processList);
|
|
330
388
|
const result = toResult(completeProcessList, rootPid, pidMap);
|
|
331
389
|
return result;
|
|
@@ -417,7 +475,7 @@ const addAccurateMemoryUsage = async process => {
|
|
|
417
475
|
|
|
418
476
|
const SIGINT = 'SIGINT';
|
|
419
477
|
|
|
420
|
-
const execFile = promisify(execFile$
|
|
478
|
+
const execFile = promisify(execFile$2);
|
|
421
479
|
const getPsOutput = async () => {
|
|
422
480
|
try {
|
|
423
481
|
const {
|
|
@@ -512,10 +570,10 @@ const parsePsOutput = (stdout, rootPid, pidMap) => {
|
|
|
512
570
|
return result;
|
|
513
571
|
};
|
|
514
572
|
|
|
515
|
-
const listProcessesWithMemoryUsage = async rootPid => {
|
|
573
|
+
const listProcessesWithMemoryUsage = async (rootPid, includeElectronData = true) => {
|
|
516
574
|
// console.time('getPsOutput')
|
|
517
575
|
const stdout = await getPsOutput();
|
|
518
|
-
const pidMap = await createPidMap();
|
|
576
|
+
const pidMap = includeElectronData ? await createPidMap() : {};
|
|
519
577
|
// console.log({ stdout })
|
|
520
578
|
// console.timeEnd('getPsOutput')
|
|
521
579
|
// console.time('parsePsOutput')
|