@lvce-editor/process-explorer 2.2.0 → 2.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 +76 -102
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IpcChildWithWebSocket, IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithNodeWorker, IpcChildWithNodeForkedProcess } from '@lvce-editor/ipc';
2
- import { object, number as number$1, string } from '@lvce-editor/assert';
2
+ import { object, number, string } from '@lvce-editor/assert';
3
3
  import { VError } from '@lvce-editor/verror';
4
4
  import { readFile } from 'node:fs/promises';
5
5
  import { join } from 'node:path';
@@ -7,95 +7,45 @@ import { execFile as execFile$1 } from 'node:child_process';
7
7
  import { promisify } from 'node:util';
8
8
 
9
9
  const Two = '2.0';
10
- class AssertionError extends Error {
11
- constructor(message) {
12
- super(message);
13
- this.name = 'AssertionError';
14
- }
15
- }
16
- const getType = value => {
17
- switch (typeof value) {
18
- case 'number':
19
- return 'number';
20
- case 'function':
21
- return 'function';
22
- case 'string':
23
- return 'string';
24
- case 'object':
25
- if (value === null) {
26
- return 'null';
27
- }
28
- if (Array.isArray(value)) {
29
- return 'array';
30
- }
31
- return 'object';
32
- case 'boolean':
33
- return 'boolean';
34
- default:
35
- return 'unknown';
36
- }
37
- };
38
- const number = value => {
39
- const type = getType(value);
40
- if (type !== 'number') {
41
- throw new AssertionError('expected value to be of type number');
42
- }
43
- };
44
- const state$1$1 = {
10
+ const state$2 = {
45
11
  callbacks: Object.create(null)
46
12
  };
47
13
  const set = (id, fn) => {
48
- state$1$1.callbacks[id] = fn;
14
+ state$2.callbacks[id] = fn;
49
15
  };
50
16
  const get = id => {
51
- return state$1$1.callbacks[id];
17
+ return state$2.callbacks[id];
52
18
  };
53
19
  const remove = id => {
54
- delete state$1$1.callbacks[id];
55
- };
56
- const state$2 = {
57
- id: 0
20
+ delete state$2.callbacks[id];
58
21
  };
22
+ let id = 0;
59
23
  const create$3 = () => {
60
- return ++state$2.id;
24
+ return ++id;
61
25
  };
62
26
  const warn = (...args) => {
63
27
  console.warn(...args);
64
28
  };
65
- const withResolvers$1 = () => {
66
- /**
67
- * @type {any}
68
- */
69
- let _resolve;
70
- const promise = new Promise(resolve => {
71
- _resolve = resolve;
72
- });
73
- return {
74
- resolve: _resolve,
75
- promise
76
- };
77
- };
78
29
  const registerPromise = () => {
79
30
  const id = create$3();
80
31
  const {
81
32
  resolve,
82
33
  promise
83
- } = withResolvers$1();
34
+ } = Promise.withResolvers();
84
35
  set(id, resolve);
85
36
  return {
86
37
  id,
87
38
  promise
88
39
  };
89
40
  };
90
- const resolve = (id, args) => {
91
- number(id);
41
+ const resolve = (id, response) => {
92
42
  const fn = get(id);
93
43
  if (!fn) {
94
- console.log(args);
44
+ console.log(response);
95
45
  warn(`callback ${id} may already be disposed`);
96
46
  return;
97
47
  }
98
- fn(args);
48
+ fn(response);
99
49
  remove(id);
100
50
  };
101
51
  const create$2 = (method, params) => {
@@ -245,14 +195,16 @@ const unwrapJsonRpcResult = responseMessage => {
245
195
  }
246
196
  throw new JsonRpcError('unexpected response message');
247
197
  };
248
- const create$1 = (message, error) => {
249
- return {
250
- jsonrpc: Two,
251
- id: message.id,
252
- error
253
- };
254
- };
255
198
  const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
199
+ const getErrorType = prettyError => {
200
+ if (prettyError && prettyError.type) {
201
+ return prettyError.type;
202
+ }
203
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
204
+ return prettyError.constructor.name;
205
+ }
206
+ return undefined;
207
+ };
256
208
  const getErrorProperty = (error, prettyError) => {
257
209
  if (error && error.code === E_COMMAND_NOT_FOUND) {
258
210
  return {
@@ -267,11 +219,19 @@ const getErrorProperty = (error, prettyError) => {
267
219
  data: {
268
220
  stack: prettyError.stack,
269
221
  codeFrame: prettyError.codeFrame,
270
- type: prettyError.type,
271
- code: prettyError.code
222
+ type: getErrorType(prettyError),
223
+ code: prettyError.code,
224
+ name: prettyError.name
272
225
  }
273
226
  };
274
227
  };
228
+ const create$1 = (message, error) => {
229
+ return {
230
+ jsonrpc: Two,
231
+ id: message.id,
232
+ error
233
+ };
234
+ };
275
235
  const getErrorResponse = (message, error, preparePrettyError, logError) => {
276
236
  const prettyError = preparePrettyError(error);
277
237
  logError(error, prettyError);
@@ -307,32 +267,42 @@ const defaultRequiresSocket = () => {
307
267
  return false;
308
268
  };
309
269
  const defaultResolve = resolve;
310
- const handleJsonRpcMessage = async (...args) => {
311
- let message;
312
- let ipc;
313
- let execute;
314
- let preparePrettyError;
315
- let logError;
316
- let resolve;
317
- let requiresSocket;
270
+
271
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
272
+ const normalizeParams = args => {
318
273
  if (args.length === 1) {
319
- const arg = args[0];
320
- message = arg.message;
321
- ipc = arg.ipc;
322
- execute = arg.execute;
323
- preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
324
- logError = arg.logError || defaultLogError;
325
- requiresSocket = arg.requiresSocket || defaultRequiresSocket;
326
- resolve = arg.resolve || defaultResolve;
327
- } else {
328
- ipc = args[0];
329
- message = args[1];
330
- execute = args[2];
331
- resolve = args[3];
332
- preparePrettyError = args[4];
333
- logError = args[5];
334
- requiresSocket = args[6];
274
+ const options = args[0];
275
+ return {
276
+ ipc: options.ipc,
277
+ message: options.message,
278
+ execute: options.execute,
279
+ resolve: options.resolve || defaultResolve,
280
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
281
+ logError: options.logError || defaultLogError,
282
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
283
+ };
335
284
  }
285
+ return {
286
+ ipc: args[0],
287
+ message: args[1],
288
+ execute: args[2],
289
+ resolve: args[3],
290
+ preparePrettyError: args[4],
291
+ logError: args[5],
292
+ requiresSocket: args[6]
293
+ };
294
+ };
295
+ const handleJsonRpcMessage = async (...args) => {
296
+ const options = normalizeParams(args);
297
+ const {
298
+ message,
299
+ ipc,
300
+ execute,
301
+ resolve,
302
+ preparePrettyError,
303
+ logError,
304
+ requiresSocket
305
+ } = options;
336
306
  if ('id' in message) {
337
307
  if ('method' in message) {
338
308
  const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
@@ -353,15 +323,19 @@ const handleJsonRpcMessage = async (...args) => {
353
323
  }
354
324
  throw new JsonRpcError('unexpected message');
355
325
  };
356
- const invoke$1 = async (ipc, method, ...params) => {
326
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
357
327
  const {
358
328
  message,
359
329
  promise
360
330
  } = create$2(method, params);
361
- ipc.send(message);
331
+ {
332
+ ipc.send(message);
333
+ }
362
334
  const responseMessage = await promise;
363
- const result = unwrapJsonRpcResult(responseMessage);
364
- return result;
335
+ return unwrapJsonRpcResult(responseMessage);
336
+ };
337
+ const invoke$1 = (ipc, method, ...params) => {
338
+ return invokeHelper(ipc, method, params);
365
339
  };
366
340
 
367
341
  const state$1 = {
@@ -525,9 +499,9 @@ const main = async () => {
525
499
  main();
526
500
 
527
501
  const getName = (pid, cmd, rootPid, pidMap) => {
528
- number$1(pid);
502
+ number(pid);
529
503
  string(cmd);
530
- number$1(rootPid);
504
+ number(rootPid);
531
505
  object(pidMap);
532
506
  if (pid === rootPid) {
533
507
  return 'main';
@@ -739,7 +713,7 @@ const getContent = async pid => {
739
713
  };
740
714
  const getAccurateMemoryUsage = async pid => {
741
715
  try {
742
- number$1(pid);
716
+ number(pid);
743
717
  if (isMacOs) {
744
718
  return 0;
745
719
  }
@@ -807,7 +781,7 @@ const parsePsOutputLine = line => {
807
781
  };
808
782
  const parsePsOutput = (stdout, rootPid, pidMap) => {
809
783
  string(stdout);
810
- number$1(rootPid);
784
+ number(rootPid);
811
785
  object(pidMap);
812
786
  if (stdout === EmptyString) {
813
787
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/process-explorer",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Process Explorer",
5
5
  "main": "dist/index.js",
6
6
  "bin": "bin/processExplorer.js",
@@ -19,9 +19,9 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@lvce-editor/assert": "^1.3.0",
22
- "@lvce-editor/ipc": "^10.0.4",
23
- "@lvce-editor/json-rpc": "^3.0.0",
24
- "@lvce-editor/verror": "^1.4.0"
22
+ "@lvce-editor/ipc": "^11.4.0",
23
+ "@lvce-editor/json-rpc": "^5.2.0",
24
+ "@lvce-editor/verror": "^1.6.0"
25
25
  },
26
26
  "optionalDependencies": {
27
27
  "@vscode/windows-process-tree": "^0.6.0"