@lvce-editor/process-explorer 3.0.0 → 3.1.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 +79 -98
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -1,55 +1,90 @@
1
+ import { ElectronMessagePortRpcClient, WebSocketRpcParent, NodeWebSocketRpcClient, NodeWorkerRpcClient, NodeForkedProcessRpcClient, ElectronUtilityProcessRpcClient } from '@lvce-editor/rpc';
1
2
  import { object, number, string } from '@lvce-editor/assert';
2
- import { handleJsonRpcMessage, resolve, invoke as invoke$1 } from '@lvce-editor/json-rpc';
3
- import { IpcChildWithWebSocket, IpcChildWithNodeWorker, IpcChildWithNodeForkedProcess, IpcChildWithElectronUtilityProcess, IpcChildWithElectronMessagePort } from '@lvce-editor/ipc';
3
+ import { RpcId, MainProcess, set as set$1 } from '@lvce-editor/rpc-registry';
4
4
  import { VError } from '@lvce-editor/verror';
5
5
  import { readFile } from 'node:fs/promises';
6
6
  import { join } from 'node:path';
7
7
  import { execFile as execFile$1 } from 'node:child_process';
8
8
  import { promisify } from 'node:util';
9
9
 
10
- const state$1 = {
11
- commands: Object.create(null)
10
+ const state = {
11
+ commandMap: {}
12
12
  };
13
- const registerCommand = (key, fn) => {
14
- state$1.commands[key] = fn;
13
+ const set = commandMap => {
14
+ state.commandMap = commandMap;
15
15
  };
16
- const registerCommands = commandMap => {
17
- for (const [key, value] of Object.entries(commandMap)) {
18
- registerCommand(key, value);
19
- }
16
+ const get = () => {
17
+ return state.commandMap;
20
18
  };
21
- const getCommand = key => {
22
- return state$1.commands[key];
19
+
20
+ const requiresSocket = () => {
21
+ return false;
23
22
  };
24
23
 
25
- const execute = (command, ...args) => {
26
- const fn = getCommand(command);
27
- if (!fn) {
28
- throw new Error(`Command not found ${command}`);
24
+ const setRpc = (rpc, rpcId) => {
25
+ if (typeof rpcId !== 'number') {
26
+ return;
29
27
  }
30
- return fn(...args);
28
+ if (rpcId === RpcId.MainProcess) {
29
+ MainProcess.set(rpc);
30
+ return;
31
+ }
32
+ set$1(rpcId, rpc);
31
33
  };
32
34
 
33
- const preparePrettyError = error => {
34
- return error;
35
+ const createMessagePortRpc = async (create, messagePort) => {
36
+ return create({
37
+ commandMap: get(),
38
+ messagePort,
39
+ requiresSocket: requiresSocket
40
+ });
41
+ };
42
+ const handleMessagePort = async (messagePort, rpcId) => {
43
+ object(messagePort);
44
+ const rpc = await createMessagePortRpc(ElectronMessagePortRpcClient.create, messagePort);
45
+ setRpc(rpc, rpcId);
35
46
  };
36
- const logError = error => {
37
- console.error(error);
47
+
48
+ const handleElectronMessagePort = async (messagePort, rpcId) => {
49
+ await handleMessagePort(messagePort, rpcId);
38
50
  };
39
- const requiresSocket = () => {
40
- return false;
51
+
52
+ const createSocketRpc = async (create, webSocket) => {
53
+ return create({
54
+ commandMap: get(),
55
+ webSocket
56
+ });
41
57
  };
42
- const handleMessage = event => {
43
- return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
58
+ const handleSocket = async (webSocket, rpcId) => {
59
+ object(webSocket);
60
+ const rpc = await createSocketRpc(WebSocketRpcParent.create, webSocket);
61
+ setRpc(rpc, rpcId);
44
62
  };
45
63
 
46
- const handleIpc = ipc => {
47
- if ('addEventListener' in ipc) {
48
- ipc.addEventListener('message', handleMessage);
49
- } else {
50
- // deprecated
51
- ipc.on('message', handleMessage);
64
+ const isWindows = process.platform === 'win32';
65
+
66
+ const getModule$1 = async () => {
67
+ if (isWindows) {
68
+ return Promise.resolve().then(function () { return ListProcessesWithMemoryUsageWindows; });
52
69
  }
70
+ return Promise.resolve().then(function () { return ListProcessesWithMemoryUsageUnix; });
71
+ };
72
+ const listProcessesWithMemoryUsage$2 = async rootPid => {
73
+ const module = await getModule$1();
74
+ return module.listProcessesWithMemoryUsage(rootPid);
75
+ };
76
+
77
+ const getMainProcessId = () => {
78
+ return process.ppid;
79
+ };
80
+
81
+ const commandMap = {
82
+ 'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
83
+ 'HandleMessagePort.handleMessagePort': handleMessagePort,
84
+ 'HandleSocket.handleSocket': handleSocket,
85
+ 'ListProcessesWithMemoryUsage.listProcessesWithMemoryUsage': listProcessesWithMemoryUsage$2,
86
+ 'ProcessId.getMainProcessId': getMainProcessId
87
+ // 'ElectronContextMenu.openContextMenu': ElectronWebContentsView.handleContextMenu,
53
88
  };
54
89
 
55
90
  const NodeWorker = 1;
@@ -73,18 +108,18 @@ const Auto = () => {
73
108
  throw new Error(`[shared-process] unknown ipc type`);
74
109
  };
75
110
 
76
- const getModule$1 = method => {
111
+ const getModule = method => {
77
112
  switch (method) {
78
113
  case ElectronMessagePort:
79
- return IpcChildWithElectronMessagePort;
114
+ return ElectronMessagePortRpcClient.create;
80
115
  case ElectronUtilityProcess:
81
- return IpcChildWithElectronUtilityProcess;
116
+ return ElectronUtilityProcessRpcClient.create;
82
117
  case NodeForkedProcess:
83
- return IpcChildWithNodeForkedProcess;
118
+ return NodeForkedProcessRpcClient.create;
84
119
  case NodeWorker:
85
- return IpcChildWithNodeWorker;
120
+ return NodeWorkerRpcClient.create;
86
121
  case WebSocket:
87
- return IpcChildWithWebSocket;
122
+ return NodeWebSocketRpcClient.create;
88
123
  default:
89
124
  throw new Error('unexpected ipc type');
90
125
  }
@@ -94,81 +129,27 @@ const listen$1 = async ({
94
129
  method,
95
130
  ...params
96
131
  }) => {
97
- const module = getModule$1(method);
98
- // @ts-ignore
99
- const rawIpc = module.listen(params);
100
- // @ts-ignore
101
- if (module.signal) {
102
- // @ts-ignore
103
- module.signal(rawIpc);
104
- }
105
- // @ts-ignore
106
- const ipc = module.wrap(rawIpc);
107
- return ipc;
108
- };
109
-
110
- const MainProcess = -5;
111
-
112
- const state = {
113
- ipc: undefined
114
- };
115
- const invoke = (method, ...params) => {
116
- return invoke$1(state.ipc, method, ...params);
117
- };
118
-
119
- const handleElectronMessagePort = async (messagePort, ipcId) => {
120
- object(messagePort);
121
- // Assert.number(ipcId)
122
- const ipc = await listen$1({
123
- messagePort,
124
- method: ElectronMessagePort
125
- });
126
- handleIpc(ipc);
127
- if (ipcId === MainProcess) {
128
- state.ipc = ipc;
129
- }
130
- };
131
-
132
- const isWindows = process.platform === 'win32';
133
-
134
- const getModule = async () => {
135
- if (isWindows) {
136
- return Promise.resolve().then(function () { return ListProcessesWithMemoryUsageWindows; });
137
- }
138
- return Promise.resolve().then(function () { return ListProcessesWithMemoryUsageUnix; });
139
- };
140
- const listProcessesWithMemoryUsage$2 = async rootPid => {
141
- const module = await getModule();
142
- return module.listProcessesWithMemoryUsage(rootPid);
143
- };
144
-
145
- const getMainProcessId = () => {
146
- return process.ppid;
147
- };
148
-
149
- const commandMap = {
150
- 'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
151
- 'ListProcessesWithMemoryUsage.listProcessesWithMemoryUsage': listProcessesWithMemoryUsage$2,
152
- 'ProcessId.getMainProcessId': getMainProcessId
153
- // 'ElectronContextMenu.openContextMenu': ElectronWebContentsView.handleContextMenu,
132
+ const create = getModule(method);
133
+ const rpc = await create(params);
134
+ return rpc;
154
135
  };
155
136
 
156
137
  const listen = async () => {
157
- const ipc = await listen$1({
138
+ set(commandMap);
139
+ await listen$1({
140
+ commandMap: commandMap,
158
141
  method: Auto()
159
142
  });
160
- handleIpc(ipc);
161
143
  };
162
144
 
163
145
  const main = async () => {
164
- registerCommands(commandMap);
165
146
  await listen();
166
147
  };
167
148
 
168
149
  main();
169
150
 
170
151
  const createPidMap = async () => {
171
- return invoke('CreatePidMap.createPidMap');
152
+ return MainProcess.invoke('CreatePidMap.createPidMap');
172
153
  };
173
154
 
174
155
  const processNamePatterns = [{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/process-explorer",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Process Explorer",
5
5
  "main": "dist/index.js",
6
6
  "bin": "bin/processExplorer.js",
@@ -19,8 +19,8 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@lvce-editor/assert": "^1.5.1",
22
- "@lvce-editor/ipc": "^16.0.0",
23
- "@lvce-editor/json-rpc": "^8.0.0",
22
+ "@lvce-editor/rpc": "^6.4.0",
23
+ "@lvce-editor/rpc-registry": "^9.27.0",
24
24
  "@lvce-editor/verror": "^1.7.0"
25
25
  },
26
26
  "optionalDependencies": {