@lvce-editor/process-explorer-worker 3.0.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/LICENSE +21 -0
- package/README.md +3 -0
- package/index.js +1974 -0
- package/package.json +13 -0
package/index.js
ADDED
|
@@ -0,0 +1,1974 @@
|
|
|
1
|
+
const toCommandId = key => {
|
|
2
|
+
const dotIndex = key.indexOf('.');
|
|
3
|
+
return key.slice(dotIndex + 1);
|
|
4
|
+
};
|
|
5
|
+
const create$a = () => {
|
|
6
|
+
const states = Object.create(null);
|
|
7
|
+
const commandMapRef = {};
|
|
8
|
+
return {
|
|
9
|
+
clear() {
|
|
10
|
+
for (const key of Object.keys(states)) {
|
|
11
|
+
delete states[key];
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
diff(uid, modules, numbers) {
|
|
15
|
+
const {
|
|
16
|
+
oldState,
|
|
17
|
+
scheduledState
|
|
18
|
+
} = states[uid];
|
|
19
|
+
const diffResult = [];
|
|
20
|
+
for (let i = 0; i < modules.length; i++) {
|
|
21
|
+
const fn = modules[i];
|
|
22
|
+
if (!fn(oldState, scheduledState)) {
|
|
23
|
+
diffResult.push(numbers[i]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return diffResult;
|
|
27
|
+
},
|
|
28
|
+
dispose(uid) {
|
|
29
|
+
delete states[uid];
|
|
30
|
+
},
|
|
31
|
+
get(uid) {
|
|
32
|
+
return states[uid];
|
|
33
|
+
},
|
|
34
|
+
getCommandIds() {
|
|
35
|
+
const keys = Object.keys(commandMapRef);
|
|
36
|
+
const ids = keys.map(toCommandId);
|
|
37
|
+
return ids;
|
|
38
|
+
},
|
|
39
|
+
getKeys() {
|
|
40
|
+
return Object.keys(states).map(Number);
|
|
41
|
+
},
|
|
42
|
+
registerCommands(commandMap) {
|
|
43
|
+
Object.assign(commandMapRef, commandMap);
|
|
44
|
+
},
|
|
45
|
+
set(uid, oldState, newState, scheduledState) {
|
|
46
|
+
states[uid] = {
|
|
47
|
+
newState,
|
|
48
|
+
oldState,
|
|
49
|
+
scheduledState: scheduledState ?? newState
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
wrapCommand(fn) {
|
|
53
|
+
const wrapped = async (uid, ...args) => {
|
|
54
|
+
const {
|
|
55
|
+
newState,
|
|
56
|
+
oldState
|
|
57
|
+
} = states[uid];
|
|
58
|
+
const newerState = await fn(newState, ...args);
|
|
59
|
+
if (oldState === newerState || newState === newerState) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const latestOld = states[uid];
|
|
63
|
+
const latestNew = {
|
|
64
|
+
...latestOld.newState,
|
|
65
|
+
...newerState
|
|
66
|
+
};
|
|
67
|
+
states[uid] = {
|
|
68
|
+
newState: latestNew,
|
|
69
|
+
oldState: latestOld.oldState,
|
|
70
|
+
scheduledState: latestNew
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
return wrapped;
|
|
74
|
+
},
|
|
75
|
+
wrapGetter(fn) {
|
|
76
|
+
const wrapped = (uid, ...args) => {
|
|
77
|
+
const {
|
|
78
|
+
newState
|
|
79
|
+
} = states[uid];
|
|
80
|
+
return fn(newState, ...args);
|
|
81
|
+
};
|
|
82
|
+
return wrapped;
|
|
83
|
+
},
|
|
84
|
+
wrapLoadContent(fn) {
|
|
85
|
+
const wrapped = async (uid, ...args) => {
|
|
86
|
+
const {
|
|
87
|
+
newState,
|
|
88
|
+
oldState
|
|
89
|
+
} = states[uid];
|
|
90
|
+
const result = await fn(newState, ...args);
|
|
91
|
+
const {
|
|
92
|
+
error,
|
|
93
|
+
state
|
|
94
|
+
} = result;
|
|
95
|
+
if (oldState === state || newState === state) {
|
|
96
|
+
return {
|
|
97
|
+
error
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const latestOld = states[uid];
|
|
101
|
+
const latestNew = {
|
|
102
|
+
...latestOld.newState,
|
|
103
|
+
...state
|
|
104
|
+
};
|
|
105
|
+
states[uid] = {
|
|
106
|
+
newState: latestNew,
|
|
107
|
+
oldState: latestOld.oldState,
|
|
108
|
+
scheduledState: latestNew
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
error
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
return wrapped;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
const terminate = () => {
|
|
119
|
+
globalThis.close();
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const None$1 = 0;
|
|
123
|
+
const Collapsed = 1;
|
|
124
|
+
const Expanded = 2;
|
|
125
|
+
|
|
126
|
+
const getRootProcess = (processes, rootPid) => {
|
|
127
|
+
if (rootPid) {
|
|
128
|
+
return processes.find(process => process.pid === rootPid);
|
|
129
|
+
}
|
|
130
|
+
return processes[0];
|
|
131
|
+
};
|
|
132
|
+
const hasChildren = (processes, pid) => {
|
|
133
|
+
return processes.some(process => process.ppid === pid);
|
|
134
|
+
};
|
|
135
|
+
const getChildren = (processes, collapsedPids, process, depth) => {
|
|
136
|
+
const children = processes.filter(otherProcess => otherProcess.ppid === process.pid);
|
|
137
|
+
if (children.length === 0) {
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
if (collapsedPids.includes(process.pid)) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
return children.flatMap(child => withChildren(processes, collapsedPids, child, depth + 1));
|
|
144
|
+
};
|
|
145
|
+
const withChildren = (processes, collapsedPids, process, depth) => {
|
|
146
|
+
const processHasChildren = hasChildren(processes, process.pid);
|
|
147
|
+
let flags = None$1;
|
|
148
|
+
if (processHasChildren && collapsedPids.includes(process.pid)) {
|
|
149
|
+
flags = Collapsed;
|
|
150
|
+
} else if (processHasChildren) {
|
|
151
|
+
flags = Expanded;
|
|
152
|
+
}
|
|
153
|
+
const visibleProcess = {
|
|
154
|
+
...process,
|
|
155
|
+
depth,
|
|
156
|
+
flags
|
|
157
|
+
};
|
|
158
|
+
return [visibleProcess, ...getChildren(processes, collapsedPids, process, depth)];
|
|
159
|
+
};
|
|
160
|
+
const getVisibleProcesses = (processes, collapsedPids, rootPid) => {
|
|
161
|
+
const rootProcess = getRootProcess(processes, rootPid);
|
|
162
|
+
if (!rootProcess) {
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
return withChildren(processes, collapsedPids, rootProcess, 1);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const collapseAll = state => {
|
|
169
|
+
const parentPids = new Set();
|
|
170
|
+
for (const process of state.processes) {
|
|
171
|
+
parentPids.add(process.ppid);
|
|
172
|
+
}
|
|
173
|
+
const collapsedPids = state.processes.filter(process => parentPids.has(process.pid)).map(process => process.pid);
|
|
174
|
+
const visibleProcesses = getVisibleProcesses(state.processes, collapsedPids, state.rootPid);
|
|
175
|
+
return {
|
|
176
|
+
...state,
|
|
177
|
+
collapsedPids,
|
|
178
|
+
focusedIndex: visibleProcesses.length === 0 ? -1 : 0,
|
|
179
|
+
visibleProcesses
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const {
|
|
184
|
+
get: get$2,
|
|
185
|
+
getCommandIds,
|
|
186
|
+
registerCommands,
|
|
187
|
+
set: set$4,
|
|
188
|
+
wrapCommand,
|
|
189
|
+
wrapLoadContent
|
|
190
|
+
} = create$a();
|
|
191
|
+
|
|
192
|
+
const create$9 = (id, _uri, x, y, width, height, _args, parentUid, _platform = 0, assetDir = '') => {
|
|
193
|
+
const state = {
|
|
194
|
+
assetDir,
|
|
195
|
+
collapsedPids: [],
|
|
196
|
+
errorMessage: '',
|
|
197
|
+
focus: 0,
|
|
198
|
+
focused: false,
|
|
199
|
+
focusedIndex: -1,
|
|
200
|
+
height,
|
|
201
|
+
initial: true,
|
|
202
|
+
parentUid,
|
|
203
|
+
processes: [],
|
|
204
|
+
rootPid: 0,
|
|
205
|
+
uid: id,
|
|
206
|
+
visibleProcesses: [],
|
|
207
|
+
width,
|
|
208
|
+
x,
|
|
209
|
+
y
|
|
210
|
+
};
|
|
211
|
+
set$4(state.uid, state, state);
|
|
212
|
+
return state;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const isEqual$1 = (oldState, newState) => {
|
|
216
|
+
return oldState.focused === newState.focused && oldState.focusedIndex === newState.focusedIndex;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const isEqual = (oldState, newState) => {
|
|
220
|
+
return oldState.initial === newState.initial && oldState.errorMessage === newState.errorMessage && oldState.visibleProcesses === newState.visibleProcesses;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const RenderItems = 1;
|
|
224
|
+
const RenderFocus = 2;
|
|
225
|
+
const RenderFocusContext = 3;
|
|
226
|
+
|
|
227
|
+
const modules = [isEqual, isEqual$1, isEqual$1];
|
|
228
|
+
const numbers = [RenderItems, RenderFocus, RenderFocusContext];
|
|
229
|
+
|
|
230
|
+
const diff = (oldState, newState) => {
|
|
231
|
+
const diffResult = [];
|
|
232
|
+
for (let i = 0; i < modules.length; i++) {
|
|
233
|
+
const fn = modules[i];
|
|
234
|
+
if (!fn(oldState, newState)) {
|
|
235
|
+
diffResult.push(numbers[i]);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return diffResult;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const diff2 = uid => {
|
|
242
|
+
const {
|
|
243
|
+
oldState,
|
|
244
|
+
scheduledState
|
|
245
|
+
} = get$2(uid);
|
|
246
|
+
return diff(oldState, scheduledState);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const expandAll = state => {
|
|
250
|
+
const collapsedPids = [];
|
|
251
|
+
const visibleProcesses = getVisibleProcesses(state.processes, collapsedPids, state.rootPid);
|
|
252
|
+
return {
|
|
253
|
+
...state,
|
|
254
|
+
collapsedPids,
|
|
255
|
+
visibleProcesses
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const focusIndex = (state, index) => {
|
|
260
|
+
if (index < -1 || index >= state.visibleProcesses.length) {
|
|
261
|
+
return state;
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
...state,
|
|
265
|
+
focusedIndex: index
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const focusFirst = state => {
|
|
270
|
+
if (state.visibleProcesses.length === 0) {
|
|
271
|
+
return state;
|
|
272
|
+
}
|
|
273
|
+
return focusIndex(state, 0);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const focusLast = state => {
|
|
277
|
+
if (state.visibleProcesses.length === 0) {
|
|
278
|
+
return state;
|
|
279
|
+
}
|
|
280
|
+
return focusIndex(state, state.visibleProcesses.length - 1);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const focusNext = state => {
|
|
284
|
+
if (state.focusedIndex >= state.visibleProcesses.length - 1) {
|
|
285
|
+
return state;
|
|
286
|
+
}
|
|
287
|
+
return focusIndex(state, state.focusedIndex + 1);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const focusPrevious = state => {
|
|
291
|
+
if (state.focusedIndex === -1 && state.visibleProcesses.length > 0) {
|
|
292
|
+
return focusIndex(state, state.visibleProcesses.length - 1);
|
|
293
|
+
}
|
|
294
|
+
if (state.focusedIndex <= 0) {
|
|
295
|
+
return state;
|
|
296
|
+
}
|
|
297
|
+
return focusIndex(state, state.focusedIndex - 1);
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const Div = 4;
|
|
301
|
+
const Table$1 = 9;
|
|
302
|
+
const TBody = 10;
|
|
303
|
+
const Td = 11;
|
|
304
|
+
const Text = 12;
|
|
305
|
+
const Th = 13;
|
|
306
|
+
const THead = 14;
|
|
307
|
+
const Tr = 15;
|
|
308
|
+
|
|
309
|
+
const ClientX = 'event.clientX';
|
|
310
|
+
const ClientY = 'event.clientY';
|
|
311
|
+
const TargetName = 'event.target.name';
|
|
312
|
+
|
|
313
|
+
const Enter = 3;
|
|
314
|
+
const Space = 9;
|
|
315
|
+
const End = 255;
|
|
316
|
+
const Home = 12;
|
|
317
|
+
const LeftArrow = 13;
|
|
318
|
+
const UpArrow = 14;
|
|
319
|
+
const RightArrow = 15;
|
|
320
|
+
const DownArrow = 16;
|
|
321
|
+
const ContextMenu = 56;
|
|
322
|
+
const Star = 131;
|
|
323
|
+
|
|
324
|
+
const CtrlCmd = 1 << 11 >>> 0;
|
|
325
|
+
|
|
326
|
+
const FileSystemWorker$1 = 209;
|
|
327
|
+
const RendererWorker = 1;
|
|
328
|
+
|
|
329
|
+
const FocusSelector = 'Viewlet.focusSelector';
|
|
330
|
+
const SetDom2 = 'Viewlet.setDom2';
|
|
331
|
+
const SetFocusContext = 'Viewlet.setFocusContext';
|
|
332
|
+
|
|
333
|
+
const Empty = 0;
|
|
334
|
+
const FocusExplorer = 13;
|
|
335
|
+
|
|
336
|
+
const getKeyBindings = () => {
|
|
337
|
+
return [{
|
|
338
|
+
command: 'ProcessExplorer.handleArrowRight',
|
|
339
|
+
key: RightArrow,
|
|
340
|
+
when: FocusExplorer
|
|
341
|
+
}, {
|
|
342
|
+
command: 'ProcessExplorer.handleArrowLeft',
|
|
343
|
+
key: LeftArrow,
|
|
344
|
+
when: FocusExplorer
|
|
345
|
+
}, {
|
|
346
|
+
command: 'ProcessExplorer.focusFirst',
|
|
347
|
+
key: Home,
|
|
348
|
+
when: FocusExplorer
|
|
349
|
+
}, {
|
|
350
|
+
command: 'ProcessExplorer.focusLast',
|
|
351
|
+
key: End,
|
|
352
|
+
when: FocusExplorer
|
|
353
|
+
}, {
|
|
354
|
+
command: 'ProcessExplorer.focusPrevious',
|
|
355
|
+
key: UpArrow,
|
|
356
|
+
when: FocusExplorer
|
|
357
|
+
}, {
|
|
358
|
+
command: 'ProcessExplorer.focusNext',
|
|
359
|
+
key: DownArrow,
|
|
360
|
+
when: FocusExplorer
|
|
361
|
+
}, {
|
|
362
|
+
command: 'ProcessExplorer.collapseAll',
|
|
363
|
+
key: CtrlCmd | LeftArrow,
|
|
364
|
+
when: FocusExplorer
|
|
365
|
+
}, {
|
|
366
|
+
command: 'ProcessExplorer.expandAll',
|
|
367
|
+
key: CtrlCmd | RightArrow,
|
|
368
|
+
when: FocusExplorer
|
|
369
|
+
}, {
|
|
370
|
+
command: 'ProcessExplorer.expandAll',
|
|
371
|
+
key: CtrlCmd | Star,
|
|
372
|
+
when: FocusExplorer
|
|
373
|
+
}, {
|
|
374
|
+
command: 'ProcessExplorer.handleDoubleClick',
|
|
375
|
+
key: Enter,
|
|
376
|
+
when: FocusExplorer
|
|
377
|
+
}, {
|
|
378
|
+
command: 'ProcessExplorer.handleDoubleClick',
|
|
379
|
+
key: Space,
|
|
380
|
+
when: FocusExplorer
|
|
381
|
+
}, {
|
|
382
|
+
command: 'ProcessExplorer.handleContextMenu',
|
|
383
|
+
key: ContextMenu,
|
|
384
|
+
when: FocusExplorer
|
|
385
|
+
}];
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const toggleIndex = (state, index) => {
|
|
389
|
+
const process = state.visibleProcesses[index];
|
|
390
|
+
if (!process || process.flags === None$1) {
|
|
391
|
+
return state;
|
|
392
|
+
}
|
|
393
|
+
const collapsedPids = state.collapsedPids.includes(process.pid) ? state.collapsedPids.filter(pid => pid !== process.pid) : [...state.collapsedPids, process.pid];
|
|
394
|
+
const visibleProcesses = getVisibleProcesses(state.processes, collapsedPids, state.rootPid);
|
|
395
|
+
return {
|
|
396
|
+
...state,
|
|
397
|
+
collapsedPids,
|
|
398
|
+
focusedIndex: Math.min(index, visibleProcesses.length - 1),
|
|
399
|
+
visibleProcesses
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const getParentIndex = state => {
|
|
404
|
+
const process = state.visibleProcesses[state.focusedIndex];
|
|
405
|
+
if (!process) {
|
|
406
|
+
return -1;
|
|
407
|
+
}
|
|
408
|
+
for (let i = state.focusedIndex - 1; i >= 0; i--) {
|
|
409
|
+
const otherProcess = state.visibleProcesses[i];
|
|
410
|
+
if (otherProcess.depth === process.depth - 1) {
|
|
411
|
+
return i;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return -1;
|
|
415
|
+
};
|
|
416
|
+
const handleArrowLeft = state => {
|
|
417
|
+
const process = state.visibleProcesses[state.focusedIndex];
|
|
418
|
+
if (!process) {
|
|
419
|
+
return state;
|
|
420
|
+
}
|
|
421
|
+
if (process.flags === Expanded) {
|
|
422
|
+
return toggleIndex(state, state.focusedIndex);
|
|
423
|
+
}
|
|
424
|
+
const parentIndex = getParentIndex(state);
|
|
425
|
+
if (parentIndex === -1) {
|
|
426
|
+
return state;
|
|
427
|
+
}
|
|
428
|
+
return focusIndex(state, parentIndex);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const handleArrowRight = state => {
|
|
432
|
+
const {
|
|
433
|
+
focusedIndex,
|
|
434
|
+
visibleProcesses
|
|
435
|
+
} = state;
|
|
436
|
+
const process = visibleProcesses[focusedIndex];
|
|
437
|
+
if (!process) {
|
|
438
|
+
return state;
|
|
439
|
+
}
|
|
440
|
+
if (process.flags === Collapsed) {
|
|
441
|
+
return toggleIndex(state, focusedIndex);
|
|
442
|
+
}
|
|
443
|
+
const nextProcess = visibleProcesses[focusedIndex + 1];
|
|
444
|
+
if (process.flags === Expanded && nextProcess && nextProcess.depth > process.depth) {
|
|
445
|
+
return focusIndex(state, focusedIndex + 1);
|
|
446
|
+
}
|
|
447
|
+
return state;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const handleBlur = state => {
|
|
451
|
+
return {
|
|
452
|
+
...state,
|
|
453
|
+
focused: false
|
|
454
|
+
};
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const handleClickAt = (state, index) => {
|
|
458
|
+
return focusIndex(state, index);
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const normalizeLine = line => {
|
|
462
|
+
if (line.startsWith('Error: ')) {
|
|
463
|
+
return line.slice('Error: '.length);
|
|
464
|
+
}
|
|
465
|
+
if (line.startsWith('VError: ')) {
|
|
466
|
+
return line.slice('VError: '.length);
|
|
467
|
+
}
|
|
468
|
+
return line;
|
|
469
|
+
};
|
|
470
|
+
const getCombinedMessage = (error, message) => {
|
|
471
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
472
|
+
if (message) {
|
|
473
|
+
return `${message}: ${stringifiedError}`;
|
|
474
|
+
}
|
|
475
|
+
return stringifiedError;
|
|
476
|
+
};
|
|
477
|
+
const NewLine$2 = '\n';
|
|
478
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
479
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
480
|
+
};
|
|
481
|
+
const mergeStacks = (parent, child) => {
|
|
482
|
+
if (!child) {
|
|
483
|
+
return parent;
|
|
484
|
+
}
|
|
485
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
486
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
487
|
+
if (childNewLineIndex === -1) {
|
|
488
|
+
return parent;
|
|
489
|
+
}
|
|
490
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
491
|
+
const childRest = child.slice(childNewLineIndex);
|
|
492
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
493
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
494
|
+
return parentFirstLine + childRest;
|
|
495
|
+
}
|
|
496
|
+
return child;
|
|
497
|
+
};
|
|
498
|
+
class VError extends Error {
|
|
499
|
+
constructor(error, message) {
|
|
500
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
501
|
+
super(combinedMessage);
|
|
502
|
+
this.name = 'VError';
|
|
503
|
+
if (error instanceof Error) {
|
|
504
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
505
|
+
}
|
|
506
|
+
if (error.codeFrame) {
|
|
507
|
+
// @ts-ignore
|
|
508
|
+
this.codeFrame = error.codeFrame;
|
|
509
|
+
}
|
|
510
|
+
if (error.code) {
|
|
511
|
+
// @ts-ignore
|
|
512
|
+
this.code = error.code;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const isMessagePort = value => {
|
|
518
|
+
return value && value instanceof MessagePort;
|
|
519
|
+
};
|
|
520
|
+
const isMessagePortMain = value => {
|
|
521
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
522
|
+
};
|
|
523
|
+
const isOffscreenCanvas = value => {
|
|
524
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
525
|
+
};
|
|
526
|
+
const isInstanceOf = (value, constructorName) => {
|
|
527
|
+
return value?.constructor?.name === constructorName;
|
|
528
|
+
};
|
|
529
|
+
const isSocket = value => {
|
|
530
|
+
return isInstanceOf(value, 'Socket');
|
|
531
|
+
};
|
|
532
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
533
|
+
const isTransferrable = value => {
|
|
534
|
+
for (const fn of transferrables) {
|
|
535
|
+
if (fn(value)) {
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return false;
|
|
540
|
+
};
|
|
541
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
542
|
+
if (!value) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
if (isTransferrable(value)) {
|
|
546
|
+
transferrables.push(value);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
if (Array.isArray(value)) {
|
|
550
|
+
for (const item of value) {
|
|
551
|
+
walkValue(item, transferrables, isTransferrable);
|
|
552
|
+
}
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
if (typeof value === 'object') {
|
|
556
|
+
for (const property of Object.values(value)) {
|
|
557
|
+
walkValue(property, transferrables, isTransferrable);
|
|
558
|
+
}
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
const getTransferrables = value => {
|
|
563
|
+
const transferrables = [];
|
|
564
|
+
walkValue(value, transferrables, isTransferrable);
|
|
565
|
+
return transferrables;
|
|
566
|
+
};
|
|
567
|
+
const attachEvents = that => {
|
|
568
|
+
const handleMessage = (...args) => {
|
|
569
|
+
const data = that.getData(...args);
|
|
570
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
571
|
+
data
|
|
572
|
+
}));
|
|
573
|
+
};
|
|
574
|
+
that.onMessage(handleMessage);
|
|
575
|
+
const handleClose = event => {
|
|
576
|
+
that.dispatchEvent(new Event('close'));
|
|
577
|
+
};
|
|
578
|
+
that.onClose(handleClose);
|
|
579
|
+
};
|
|
580
|
+
class Ipc extends EventTarget {
|
|
581
|
+
constructor(rawIpc) {
|
|
582
|
+
super();
|
|
583
|
+
this._rawIpc = rawIpc;
|
|
584
|
+
attachEvents(this);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
588
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
589
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
590
|
+
const NewLine$1 = '\n';
|
|
591
|
+
const joinLines$1 = lines => {
|
|
592
|
+
return lines.join(NewLine$1);
|
|
593
|
+
};
|
|
594
|
+
const RE_AT = /^\s+at/;
|
|
595
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
596
|
+
const isNormalStackLine = line => {
|
|
597
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
598
|
+
};
|
|
599
|
+
const getDetails = lines => {
|
|
600
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
601
|
+
if (index === -1) {
|
|
602
|
+
return {
|
|
603
|
+
actualMessage: joinLines$1(lines),
|
|
604
|
+
rest: []
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
let lastIndex = index - 1;
|
|
608
|
+
while (++lastIndex < lines.length) {
|
|
609
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return {
|
|
614
|
+
actualMessage: lines[index - 1],
|
|
615
|
+
rest: lines.slice(index, lastIndex)
|
|
616
|
+
};
|
|
617
|
+
};
|
|
618
|
+
const splitLines$1 = lines => {
|
|
619
|
+
return lines.split(NewLine$1);
|
|
620
|
+
};
|
|
621
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
622
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
623
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
624
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
625
|
+
};
|
|
626
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
627
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
628
|
+
};
|
|
629
|
+
const getMessageCodeBlock = stderr => {
|
|
630
|
+
const lines = splitLines$1(stderr);
|
|
631
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
632
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
633
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
634
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
635
|
+
return relevantMessage;
|
|
636
|
+
};
|
|
637
|
+
const isModuleNotFoundMessage = line => {
|
|
638
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
639
|
+
};
|
|
640
|
+
const getModuleNotFoundError = stderr => {
|
|
641
|
+
const lines = splitLines$1(stderr);
|
|
642
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
643
|
+
const message = lines[messageIndex];
|
|
644
|
+
return {
|
|
645
|
+
code: ERR_MODULE_NOT_FOUND,
|
|
646
|
+
message
|
|
647
|
+
};
|
|
648
|
+
};
|
|
649
|
+
const isModuleNotFoundError = stderr => {
|
|
650
|
+
if (!stderr) {
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
654
|
+
};
|
|
655
|
+
const isModulesSyntaxError = stderr => {
|
|
656
|
+
if (!stderr) {
|
|
657
|
+
return false;
|
|
658
|
+
}
|
|
659
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
660
|
+
};
|
|
661
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
662
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
663
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
664
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
665
|
+
};
|
|
666
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
667
|
+
const message = getMessageCodeBlock(stderr);
|
|
668
|
+
return {
|
|
669
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE,
|
|
670
|
+
message: `Incompatible native node module: ${message}`
|
|
671
|
+
};
|
|
672
|
+
};
|
|
673
|
+
const getModuleSyntaxError = () => {
|
|
674
|
+
return {
|
|
675
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
|
|
676
|
+
message: `ES Modules are not supported in electron`
|
|
677
|
+
};
|
|
678
|
+
};
|
|
679
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
680
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
681
|
+
return getNativeModuleErrorMessage(stderr);
|
|
682
|
+
}
|
|
683
|
+
if (isModulesSyntaxError(stderr)) {
|
|
684
|
+
return getModuleSyntaxError();
|
|
685
|
+
}
|
|
686
|
+
if (isModuleNotFoundError(stderr)) {
|
|
687
|
+
return getModuleNotFoundError(stderr);
|
|
688
|
+
}
|
|
689
|
+
const lines = splitLines$1(stderr);
|
|
690
|
+
const {
|
|
691
|
+
actualMessage,
|
|
692
|
+
rest
|
|
693
|
+
} = getDetails(lines);
|
|
694
|
+
return {
|
|
695
|
+
code: '',
|
|
696
|
+
message: actualMessage,
|
|
697
|
+
stack: rest
|
|
698
|
+
};
|
|
699
|
+
};
|
|
700
|
+
class IpcError extends VError {
|
|
701
|
+
// @ts-ignore
|
|
702
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
703
|
+
if (stdout || stderr) {
|
|
704
|
+
// @ts-ignore
|
|
705
|
+
const {
|
|
706
|
+
code,
|
|
707
|
+
message,
|
|
708
|
+
stack
|
|
709
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
710
|
+
const cause = new Error(message);
|
|
711
|
+
// @ts-ignore
|
|
712
|
+
cause.code = code;
|
|
713
|
+
cause.stack = stack;
|
|
714
|
+
super(cause, betterMessage);
|
|
715
|
+
} else {
|
|
716
|
+
super(betterMessage);
|
|
717
|
+
}
|
|
718
|
+
// @ts-ignore
|
|
719
|
+
this.name = 'IpcError';
|
|
720
|
+
// @ts-ignore
|
|
721
|
+
this.stdout = stdout;
|
|
722
|
+
// @ts-ignore
|
|
723
|
+
this.stderr = stderr;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
const readyMessage = 'ready';
|
|
727
|
+
const getData$2 = event => {
|
|
728
|
+
return event.data;
|
|
729
|
+
};
|
|
730
|
+
const listen$7 = () => {
|
|
731
|
+
// @ts-ignore
|
|
732
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
733
|
+
throw new TypeError('module is not in web worker scope');
|
|
734
|
+
}
|
|
735
|
+
return globalThis;
|
|
736
|
+
};
|
|
737
|
+
const signal$8 = global => {
|
|
738
|
+
global.postMessage(readyMessage);
|
|
739
|
+
};
|
|
740
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
741
|
+
getData(event) {
|
|
742
|
+
return getData$2(event);
|
|
743
|
+
}
|
|
744
|
+
send(message) {
|
|
745
|
+
// @ts-ignore
|
|
746
|
+
this._rawIpc.postMessage(message);
|
|
747
|
+
}
|
|
748
|
+
sendAndTransfer(message) {
|
|
749
|
+
const transfer = getTransferrables(message);
|
|
750
|
+
// @ts-ignore
|
|
751
|
+
this._rawIpc.postMessage(message, transfer);
|
|
752
|
+
}
|
|
753
|
+
dispose() {
|
|
754
|
+
// ignore
|
|
755
|
+
}
|
|
756
|
+
onClose(callback) {
|
|
757
|
+
// ignore
|
|
758
|
+
}
|
|
759
|
+
onMessage(callback) {
|
|
760
|
+
this._rawIpc.addEventListener('message', callback);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
const wrap$f = global => {
|
|
764
|
+
return new IpcChildWithModuleWorker(global);
|
|
765
|
+
};
|
|
766
|
+
const waitForFirstMessage = async port => {
|
|
767
|
+
const {
|
|
768
|
+
promise,
|
|
769
|
+
resolve
|
|
770
|
+
} = Promise.withResolvers();
|
|
771
|
+
port.addEventListener('message', resolve, {
|
|
772
|
+
once: true
|
|
773
|
+
});
|
|
774
|
+
const event = await promise;
|
|
775
|
+
// @ts-ignore
|
|
776
|
+
return event.data;
|
|
777
|
+
};
|
|
778
|
+
const listen$6 = async () => {
|
|
779
|
+
const parentIpcRaw = listen$7();
|
|
780
|
+
signal$8(parentIpcRaw);
|
|
781
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
782
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
783
|
+
if (firstMessage.method !== 'initialize') {
|
|
784
|
+
throw new IpcError('unexpected first message');
|
|
785
|
+
}
|
|
786
|
+
const type = firstMessage.params[0];
|
|
787
|
+
if (type === 'message-port') {
|
|
788
|
+
parentIpc.send({
|
|
789
|
+
id: firstMessage.id,
|
|
790
|
+
jsonrpc: '2.0',
|
|
791
|
+
result: null
|
|
792
|
+
});
|
|
793
|
+
parentIpc.dispose();
|
|
794
|
+
const port = firstMessage.params[1];
|
|
795
|
+
return port;
|
|
796
|
+
}
|
|
797
|
+
return globalThis;
|
|
798
|
+
};
|
|
799
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
800
|
+
getData(event) {
|
|
801
|
+
return getData$2(event);
|
|
802
|
+
}
|
|
803
|
+
send(message) {
|
|
804
|
+
this._rawIpc.postMessage(message);
|
|
805
|
+
}
|
|
806
|
+
sendAndTransfer(message) {
|
|
807
|
+
const transfer = getTransferrables(message);
|
|
808
|
+
this._rawIpc.postMessage(message, transfer);
|
|
809
|
+
}
|
|
810
|
+
dispose() {
|
|
811
|
+
if (this._rawIpc.close) {
|
|
812
|
+
this._rawIpc.close();
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
onClose(callback) {
|
|
816
|
+
// ignore
|
|
817
|
+
}
|
|
818
|
+
onMessage(callback) {
|
|
819
|
+
this._rawIpc.addEventListener('message', callback);
|
|
820
|
+
this._rawIpc.start();
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const wrap$e = port => {
|
|
824
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
825
|
+
};
|
|
826
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
827
|
+
__proto__: null,
|
|
828
|
+
listen: listen$6,
|
|
829
|
+
wrap: wrap$e
|
|
830
|
+
};
|
|
831
|
+
const addListener = (emitter, type, callback) => {
|
|
832
|
+
if ('addEventListener' in emitter) {
|
|
833
|
+
emitter.addEventListener(type, callback);
|
|
834
|
+
} else {
|
|
835
|
+
emitter.on(type, callback);
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
const removeListener = (emitter, type, callback) => {
|
|
839
|
+
if ('removeEventListener' in emitter) {
|
|
840
|
+
emitter.removeEventListener(type, callback);
|
|
841
|
+
} else {
|
|
842
|
+
emitter.off(type, callback);
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
846
|
+
const {
|
|
847
|
+
promise,
|
|
848
|
+
resolve
|
|
849
|
+
} = Promise.withResolvers();
|
|
850
|
+
const listenerMap = Object.create(null);
|
|
851
|
+
const cleanup = value => {
|
|
852
|
+
for (const event of Object.keys(eventMap)) {
|
|
853
|
+
removeListener(eventEmitter, event, listenerMap[event]);
|
|
854
|
+
}
|
|
855
|
+
resolve(value);
|
|
856
|
+
};
|
|
857
|
+
for (const [event, type] of Object.entries(eventMap)) {
|
|
858
|
+
const listener = event => {
|
|
859
|
+
cleanup({
|
|
860
|
+
event,
|
|
861
|
+
type
|
|
862
|
+
});
|
|
863
|
+
};
|
|
864
|
+
addListener(eventEmitter, event, listener);
|
|
865
|
+
listenerMap[event] = listener;
|
|
866
|
+
}
|
|
867
|
+
return promise;
|
|
868
|
+
};
|
|
869
|
+
const Message$1 = 3;
|
|
870
|
+
const create$5$1 = async ({
|
|
871
|
+
isMessagePortOpen,
|
|
872
|
+
messagePort
|
|
873
|
+
}) => {
|
|
874
|
+
if (!isMessagePort(messagePort)) {
|
|
875
|
+
throw new IpcError('port must be of type MessagePort');
|
|
876
|
+
}
|
|
877
|
+
if (isMessagePortOpen) {
|
|
878
|
+
return messagePort;
|
|
879
|
+
}
|
|
880
|
+
const eventPromise = getFirstEvent(messagePort, {
|
|
881
|
+
message: Message$1
|
|
882
|
+
});
|
|
883
|
+
messagePort.start();
|
|
884
|
+
const {
|
|
885
|
+
event,
|
|
886
|
+
type
|
|
887
|
+
} = await eventPromise;
|
|
888
|
+
if (type !== Message$1) {
|
|
889
|
+
throw new IpcError('Failed to wait for ipc message');
|
|
890
|
+
}
|
|
891
|
+
if (event.data !== readyMessage) {
|
|
892
|
+
throw new IpcError('unexpected first message');
|
|
893
|
+
}
|
|
894
|
+
return messagePort;
|
|
895
|
+
};
|
|
896
|
+
const signal$1 = messagePort => {
|
|
897
|
+
messagePort.start();
|
|
898
|
+
};
|
|
899
|
+
class IpcParentWithMessagePort extends Ipc {
|
|
900
|
+
getData = getData$2;
|
|
901
|
+
send(message) {
|
|
902
|
+
this._rawIpc.postMessage(message);
|
|
903
|
+
}
|
|
904
|
+
sendAndTransfer(message) {
|
|
905
|
+
const transfer = getTransferrables(message);
|
|
906
|
+
this._rawIpc.postMessage(message, transfer);
|
|
907
|
+
}
|
|
908
|
+
dispose() {
|
|
909
|
+
this._rawIpc.close();
|
|
910
|
+
}
|
|
911
|
+
onMessage(callback) {
|
|
912
|
+
this._rawIpc.addEventListener('message', callback);
|
|
913
|
+
}
|
|
914
|
+
onClose(callback) {}
|
|
915
|
+
}
|
|
916
|
+
const wrap$5 = messagePort => {
|
|
917
|
+
return new IpcParentWithMessagePort(messagePort);
|
|
918
|
+
};
|
|
919
|
+
const IpcParentWithMessagePort$1 = {
|
|
920
|
+
__proto__: null,
|
|
921
|
+
create: create$5$1,
|
|
922
|
+
signal: signal$1,
|
|
923
|
+
wrap: wrap$5
|
|
924
|
+
};
|
|
925
|
+
|
|
926
|
+
class CommandNotFoundError extends Error {
|
|
927
|
+
constructor(command) {
|
|
928
|
+
super(`Command not found ${command}`);
|
|
929
|
+
this.name = 'CommandNotFoundError';
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
const commands = Object.create(null);
|
|
933
|
+
const register = commandMap => {
|
|
934
|
+
Object.assign(commands, commandMap);
|
|
935
|
+
};
|
|
936
|
+
const getCommand = key => {
|
|
937
|
+
return commands[key];
|
|
938
|
+
};
|
|
939
|
+
const execute = (command, ...args) => {
|
|
940
|
+
const fn = getCommand(command);
|
|
941
|
+
if (!fn) {
|
|
942
|
+
throw new CommandNotFoundError(command);
|
|
943
|
+
}
|
|
944
|
+
return fn(...args);
|
|
945
|
+
};
|
|
946
|
+
|
|
947
|
+
const Two$1 = '2.0';
|
|
948
|
+
const callbacks = Object.create(null);
|
|
949
|
+
const get$1 = id => {
|
|
950
|
+
return callbacks[id];
|
|
951
|
+
};
|
|
952
|
+
const remove$1 = id => {
|
|
953
|
+
delete callbacks[id];
|
|
954
|
+
};
|
|
955
|
+
class JsonRpcError extends Error {
|
|
956
|
+
constructor(message) {
|
|
957
|
+
super(message);
|
|
958
|
+
this.name = 'JsonRpcError';
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
const NewLine = '\n';
|
|
962
|
+
const DomException = 'DOMException';
|
|
963
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
964
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
965
|
+
const TypeError$1 = 'TypeError';
|
|
966
|
+
const getErrorConstructor = (message, type) => {
|
|
967
|
+
if (type) {
|
|
968
|
+
switch (type) {
|
|
969
|
+
case DomException:
|
|
970
|
+
return DOMException;
|
|
971
|
+
case ReferenceError$1:
|
|
972
|
+
return ReferenceError;
|
|
973
|
+
case SyntaxError$1:
|
|
974
|
+
return SyntaxError;
|
|
975
|
+
case TypeError$1:
|
|
976
|
+
return TypeError;
|
|
977
|
+
default:
|
|
978
|
+
return Error;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
if (message.startsWith('TypeError: ')) {
|
|
982
|
+
return TypeError;
|
|
983
|
+
}
|
|
984
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
985
|
+
return SyntaxError;
|
|
986
|
+
}
|
|
987
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
988
|
+
return ReferenceError;
|
|
989
|
+
}
|
|
990
|
+
return Error;
|
|
991
|
+
};
|
|
992
|
+
const constructError = (message, type, name) => {
|
|
993
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
994
|
+
if (ErrorConstructor === DOMException && name) {
|
|
995
|
+
return new ErrorConstructor(message, name);
|
|
996
|
+
}
|
|
997
|
+
if (ErrorConstructor === Error) {
|
|
998
|
+
const error = new Error(message);
|
|
999
|
+
if (name && name !== 'VError') {
|
|
1000
|
+
error.name = name;
|
|
1001
|
+
}
|
|
1002
|
+
return error;
|
|
1003
|
+
}
|
|
1004
|
+
return new ErrorConstructor(message);
|
|
1005
|
+
};
|
|
1006
|
+
const joinLines = lines => {
|
|
1007
|
+
return lines.join(NewLine);
|
|
1008
|
+
};
|
|
1009
|
+
const splitLines = lines => {
|
|
1010
|
+
return lines.split(NewLine);
|
|
1011
|
+
};
|
|
1012
|
+
const getCurrentStack = () => {
|
|
1013
|
+
const stackLinesToSkip = 3;
|
|
1014
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
1015
|
+
return currentStack;
|
|
1016
|
+
};
|
|
1017
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
1018
|
+
return string.indexOf(NewLine, startIndex);
|
|
1019
|
+
};
|
|
1020
|
+
const getParentStack = error => {
|
|
1021
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
1022
|
+
if (parentStack.startsWith(' at')) {
|
|
1023
|
+
parentStack = error.message + NewLine + parentStack;
|
|
1024
|
+
}
|
|
1025
|
+
return parentStack;
|
|
1026
|
+
};
|
|
1027
|
+
const MethodNotFound = -32601;
|
|
1028
|
+
const Custom = -32001;
|
|
1029
|
+
const restoreJsonRpcError = error => {
|
|
1030
|
+
const currentStack = getCurrentStack();
|
|
1031
|
+
if (error && error instanceof Error) {
|
|
1032
|
+
if (typeof error.stack === 'string') {
|
|
1033
|
+
error.stack = error.stack + NewLine + currentStack;
|
|
1034
|
+
}
|
|
1035
|
+
return error;
|
|
1036
|
+
}
|
|
1037
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
1038
|
+
const restoredError = new JsonRpcError(error.message);
|
|
1039
|
+
const parentStack = getParentStack(error);
|
|
1040
|
+
restoredError.stack = parentStack + NewLine + currentStack;
|
|
1041
|
+
return restoredError;
|
|
1042
|
+
}
|
|
1043
|
+
if (error && error.message) {
|
|
1044
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
1045
|
+
if (error.data) {
|
|
1046
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
1047
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
1048
|
+
} else if (error.data.stack) {
|
|
1049
|
+
restoredError.stack = error.data.stack;
|
|
1050
|
+
}
|
|
1051
|
+
if (error.data.codeFrame) {
|
|
1052
|
+
// @ts-ignore
|
|
1053
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
1054
|
+
}
|
|
1055
|
+
if (error.data.code) {
|
|
1056
|
+
// @ts-ignore
|
|
1057
|
+
restoredError.code = error.data.code;
|
|
1058
|
+
}
|
|
1059
|
+
if (error.data.type) {
|
|
1060
|
+
// @ts-ignore
|
|
1061
|
+
restoredError.name = error.data.type;
|
|
1062
|
+
}
|
|
1063
|
+
} else {
|
|
1064
|
+
if (error.stack) {
|
|
1065
|
+
const lowerStack = restoredError.stack || '';
|
|
1066
|
+
// @ts-ignore
|
|
1067
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
1068
|
+
const parentStack = getParentStack(error);
|
|
1069
|
+
// @ts-ignore
|
|
1070
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
1071
|
+
}
|
|
1072
|
+
if (error.codeFrame) {
|
|
1073
|
+
// @ts-ignore
|
|
1074
|
+
restoredError.codeFrame = error.codeFrame;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
return restoredError;
|
|
1078
|
+
}
|
|
1079
|
+
if (typeof error === 'string') {
|
|
1080
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
1081
|
+
}
|
|
1082
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
1083
|
+
};
|
|
1084
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
1085
|
+
if ('error' in responseMessage) {
|
|
1086
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
1087
|
+
throw restoredError;
|
|
1088
|
+
}
|
|
1089
|
+
if ('result' in responseMessage) {
|
|
1090
|
+
return responseMessage.result;
|
|
1091
|
+
}
|
|
1092
|
+
throw new JsonRpcError('unexpected response message');
|
|
1093
|
+
};
|
|
1094
|
+
const warn = (...args) => {
|
|
1095
|
+
console.warn(...args);
|
|
1096
|
+
};
|
|
1097
|
+
const resolve = (id, response) => {
|
|
1098
|
+
const fn = get$1(id);
|
|
1099
|
+
if (!fn) {
|
|
1100
|
+
console.log(response);
|
|
1101
|
+
warn(`callback ${id} may already be disposed`);
|
|
1102
|
+
return;
|
|
1103
|
+
}
|
|
1104
|
+
fn(response);
|
|
1105
|
+
remove$1(id);
|
|
1106
|
+
};
|
|
1107
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
1108
|
+
const getErrorType = prettyError => {
|
|
1109
|
+
if (prettyError && prettyError.type) {
|
|
1110
|
+
return prettyError.type;
|
|
1111
|
+
}
|
|
1112
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
1113
|
+
return prettyError.constructor.name;
|
|
1114
|
+
}
|
|
1115
|
+
return undefined;
|
|
1116
|
+
};
|
|
1117
|
+
const isAlreadyStack = line => {
|
|
1118
|
+
return line.trim().startsWith('at ');
|
|
1119
|
+
};
|
|
1120
|
+
const getStack = prettyError => {
|
|
1121
|
+
const stackString = prettyError.stack || '';
|
|
1122
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
1123
|
+
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
1124
|
+
return stackString.slice(newLineIndex + 1);
|
|
1125
|
+
}
|
|
1126
|
+
return stackString;
|
|
1127
|
+
};
|
|
1128
|
+
const getErrorProperty = (error, prettyError) => {
|
|
1129
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
1130
|
+
return {
|
|
1131
|
+
code: MethodNotFound,
|
|
1132
|
+
data: error.stack,
|
|
1133
|
+
message: error.message
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
return {
|
|
1137
|
+
code: Custom,
|
|
1138
|
+
data: {
|
|
1139
|
+
code: prettyError.code,
|
|
1140
|
+
codeFrame: prettyError.codeFrame,
|
|
1141
|
+
name: prettyError.name,
|
|
1142
|
+
stack: getStack(prettyError),
|
|
1143
|
+
type: getErrorType(prettyError)
|
|
1144
|
+
},
|
|
1145
|
+
message: prettyError.message
|
|
1146
|
+
};
|
|
1147
|
+
};
|
|
1148
|
+
const create$1$1 = (id, error) => {
|
|
1149
|
+
return {
|
|
1150
|
+
error,
|
|
1151
|
+
id,
|
|
1152
|
+
jsonrpc: Two$1
|
|
1153
|
+
};
|
|
1154
|
+
};
|
|
1155
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
1156
|
+
const prettyError = preparePrettyError(error);
|
|
1157
|
+
logError(error, prettyError);
|
|
1158
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
1159
|
+
return create$1$1(id, errorProperty);
|
|
1160
|
+
};
|
|
1161
|
+
const create$8 = (message, result) => {
|
|
1162
|
+
return {
|
|
1163
|
+
id: message.id,
|
|
1164
|
+
jsonrpc: Two$1,
|
|
1165
|
+
result: result ?? null
|
|
1166
|
+
};
|
|
1167
|
+
};
|
|
1168
|
+
const getSuccessResponse = (message, result) => {
|
|
1169
|
+
const resultProperty = result ?? null;
|
|
1170
|
+
return create$8(message, resultProperty);
|
|
1171
|
+
};
|
|
1172
|
+
const getErrorResponseSimple = (id, error) => {
|
|
1173
|
+
return {
|
|
1174
|
+
error: {
|
|
1175
|
+
code: Custom,
|
|
1176
|
+
data: error,
|
|
1177
|
+
// @ts-ignore
|
|
1178
|
+
message: error.message
|
|
1179
|
+
},
|
|
1180
|
+
id,
|
|
1181
|
+
jsonrpc: Two$1
|
|
1182
|
+
};
|
|
1183
|
+
};
|
|
1184
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
1185
|
+
try {
|
|
1186
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
1187
|
+
return getSuccessResponse(message, result);
|
|
1188
|
+
} catch (error) {
|
|
1189
|
+
if (ipc.canUseSimpleErrorResponse) {
|
|
1190
|
+
return getErrorResponseSimple(message.id, error);
|
|
1191
|
+
}
|
|
1192
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
1193
|
+
}
|
|
1194
|
+
};
|
|
1195
|
+
const defaultPreparePrettyError = error => {
|
|
1196
|
+
return error;
|
|
1197
|
+
};
|
|
1198
|
+
const defaultLogError = () => {
|
|
1199
|
+
// ignore
|
|
1200
|
+
};
|
|
1201
|
+
const defaultRequiresSocket = () => {
|
|
1202
|
+
return false;
|
|
1203
|
+
};
|
|
1204
|
+
const defaultResolve = resolve;
|
|
1205
|
+
|
|
1206
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
1207
|
+
const normalizeParams = args => {
|
|
1208
|
+
if (args.length === 1) {
|
|
1209
|
+
const options = args[0];
|
|
1210
|
+
return {
|
|
1211
|
+
execute: options.execute,
|
|
1212
|
+
ipc: options.ipc,
|
|
1213
|
+
logError: options.logError || defaultLogError,
|
|
1214
|
+
message: options.message,
|
|
1215
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
1216
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket,
|
|
1217
|
+
resolve: options.resolve || defaultResolve
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
return {
|
|
1221
|
+
execute: args[2],
|
|
1222
|
+
ipc: args[0],
|
|
1223
|
+
logError: args[5],
|
|
1224
|
+
message: args[1],
|
|
1225
|
+
preparePrettyError: args[4],
|
|
1226
|
+
requiresSocket: args[6],
|
|
1227
|
+
resolve: args[3]
|
|
1228
|
+
};
|
|
1229
|
+
};
|
|
1230
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
1231
|
+
const options = normalizeParams(args);
|
|
1232
|
+
const {
|
|
1233
|
+
execute,
|
|
1234
|
+
ipc,
|
|
1235
|
+
logError,
|
|
1236
|
+
message,
|
|
1237
|
+
preparePrettyError,
|
|
1238
|
+
requiresSocket,
|
|
1239
|
+
resolve
|
|
1240
|
+
} = options;
|
|
1241
|
+
if ('id' in message) {
|
|
1242
|
+
if ('method' in message) {
|
|
1243
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
1244
|
+
try {
|
|
1245
|
+
ipc.send(response);
|
|
1246
|
+
} catch (error) {
|
|
1247
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
1248
|
+
ipc.send(errorResponse);
|
|
1249
|
+
}
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
resolve(message.id, message);
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
if ('method' in message) {
|
|
1256
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
throw new JsonRpcError('unexpected message');
|
|
1260
|
+
};
|
|
1261
|
+
|
|
1262
|
+
const Two = '2.0';
|
|
1263
|
+
|
|
1264
|
+
const create$7 = (method, params) => {
|
|
1265
|
+
return {
|
|
1266
|
+
jsonrpc: Two,
|
|
1267
|
+
method,
|
|
1268
|
+
params
|
|
1269
|
+
};
|
|
1270
|
+
};
|
|
1271
|
+
|
|
1272
|
+
const create$6 = (id, method, params) => {
|
|
1273
|
+
const message = {
|
|
1274
|
+
id,
|
|
1275
|
+
jsonrpc: Two,
|
|
1276
|
+
method,
|
|
1277
|
+
params
|
|
1278
|
+
};
|
|
1279
|
+
return message;
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
let id = 0;
|
|
1283
|
+
const create$5 = () => {
|
|
1284
|
+
return ++id;
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
const registerPromise = map => {
|
|
1288
|
+
const id = create$5();
|
|
1289
|
+
const {
|
|
1290
|
+
promise,
|
|
1291
|
+
resolve
|
|
1292
|
+
} = Promise.withResolvers();
|
|
1293
|
+
map[id] = resolve;
|
|
1294
|
+
return {
|
|
1295
|
+
id,
|
|
1296
|
+
promise
|
|
1297
|
+
};
|
|
1298
|
+
};
|
|
1299
|
+
|
|
1300
|
+
const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
|
|
1301
|
+
const {
|
|
1302
|
+
id,
|
|
1303
|
+
promise
|
|
1304
|
+
} = registerPromise(callbacks);
|
|
1305
|
+
const message = create$6(id, method, params);
|
|
1306
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
1307
|
+
ipc.sendAndTransfer(message);
|
|
1308
|
+
} else {
|
|
1309
|
+
ipc.send(message);
|
|
1310
|
+
}
|
|
1311
|
+
const responseMessage = await promise;
|
|
1312
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
1313
|
+
};
|
|
1314
|
+
const createRpc = ipc => {
|
|
1315
|
+
const callbacks = Object.create(null);
|
|
1316
|
+
ipc._resolve = (id, response) => {
|
|
1317
|
+
const fn = callbacks[id];
|
|
1318
|
+
if (!fn) {
|
|
1319
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
fn(response);
|
|
1323
|
+
delete callbacks[id];
|
|
1324
|
+
};
|
|
1325
|
+
const rpc = {
|
|
1326
|
+
async dispose() {
|
|
1327
|
+
await ipc?.dispose();
|
|
1328
|
+
},
|
|
1329
|
+
invoke(method, ...params) {
|
|
1330
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
1331
|
+
},
|
|
1332
|
+
invokeAndTransfer(method, ...params) {
|
|
1333
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
1334
|
+
},
|
|
1335
|
+
// @ts-ignore
|
|
1336
|
+
ipc,
|
|
1337
|
+
/**
|
|
1338
|
+
* @deprecated
|
|
1339
|
+
*/
|
|
1340
|
+
send(method, ...params) {
|
|
1341
|
+
const message = create$7(method, params);
|
|
1342
|
+
ipc.send(message);
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
return rpc;
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
const requiresSocket = () => {
|
|
1349
|
+
return false;
|
|
1350
|
+
};
|
|
1351
|
+
const preparePrettyError = error => {
|
|
1352
|
+
return error;
|
|
1353
|
+
};
|
|
1354
|
+
const logError = () => {
|
|
1355
|
+
// handled by renderer worker
|
|
1356
|
+
};
|
|
1357
|
+
const handleMessage = event => {
|
|
1358
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
1359
|
+
const actualExecute = event?.target?.execute || execute;
|
|
1360
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
1361
|
+
};
|
|
1362
|
+
|
|
1363
|
+
const handleIpc = ipc => {
|
|
1364
|
+
if ('addEventListener' in ipc) {
|
|
1365
|
+
ipc.addEventListener('message', handleMessage);
|
|
1366
|
+
} else if ('on' in ipc) {
|
|
1367
|
+
// deprecated
|
|
1368
|
+
ipc.on('message', handleMessage);
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
|
|
1372
|
+
const listen$1 = async (module, options) => {
|
|
1373
|
+
const rawIpc = await module.listen(options);
|
|
1374
|
+
if (module.signal) {
|
|
1375
|
+
module.signal(rawIpc);
|
|
1376
|
+
}
|
|
1377
|
+
const ipc = module.wrap(rawIpc);
|
|
1378
|
+
return ipc;
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
const create$4 = async ({
|
|
1382
|
+
commandMap,
|
|
1383
|
+
isMessagePortOpen = true,
|
|
1384
|
+
messagePort
|
|
1385
|
+
}) => {
|
|
1386
|
+
// TODO create a commandMap per rpc instance
|
|
1387
|
+
register(commandMap);
|
|
1388
|
+
const rawIpc = await IpcParentWithMessagePort$1.create({
|
|
1389
|
+
isMessagePortOpen,
|
|
1390
|
+
messagePort
|
|
1391
|
+
});
|
|
1392
|
+
const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
|
|
1393
|
+
handleIpc(ipc);
|
|
1394
|
+
const rpc = createRpc(ipc);
|
|
1395
|
+
messagePort.start();
|
|
1396
|
+
return rpc;
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1399
|
+
const create$3 = async ({
|
|
1400
|
+
commandMap,
|
|
1401
|
+
isMessagePortOpen,
|
|
1402
|
+
send
|
|
1403
|
+
}) => {
|
|
1404
|
+
const {
|
|
1405
|
+
port1,
|
|
1406
|
+
port2
|
|
1407
|
+
} = new MessageChannel();
|
|
1408
|
+
await send(port1);
|
|
1409
|
+
return create$4({
|
|
1410
|
+
commandMap,
|
|
1411
|
+
isMessagePortOpen,
|
|
1412
|
+
messagePort: port2
|
|
1413
|
+
});
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
const createSharedLazyRpc = factory => {
|
|
1417
|
+
let rpcPromise;
|
|
1418
|
+
const getOrCreate = () => {
|
|
1419
|
+
if (!rpcPromise) {
|
|
1420
|
+
rpcPromise = factory();
|
|
1421
|
+
}
|
|
1422
|
+
return rpcPromise;
|
|
1423
|
+
};
|
|
1424
|
+
return {
|
|
1425
|
+
async dispose() {
|
|
1426
|
+
const rpc = await getOrCreate();
|
|
1427
|
+
await rpc.dispose();
|
|
1428
|
+
},
|
|
1429
|
+
async invoke(method, ...params) {
|
|
1430
|
+
const rpc = await getOrCreate();
|
|
1431
|
+
return rpc.invoke(method, ...params);
|
|
1432
|
+
},
|
|
1433
|
+
async invokeAndTransfer(method, ...params) {
|
|
1434
|
+
const rpc = await getOrCreate();
|
|
1435
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
1436
|
+
},
|
|
1437
|
+
async send(method, ...params) {
|
|
1438
|
+
const rpc = await getOrCreate();
|
|
1439
|
+
rpc.send(method, ...params);
|
|
1440
|
+
}
|
|
1441
|
+
};
|
|
1442
|
+
};
|
|
1443
|
+
|
|
1444
|
+
const create$2 = async ({
|
|
1445
|
+
commandMap,
|
|
1446
|
+
isMessagePortOpen,
|
|
1447
|
+
send
|
|
1448
|
+
}) => {
|
|
1449
|
+
return createSharedLazyRpc(() => {
|
|
1450
|
+
return create$3({
|
|
1451
|
+
commandMap,
|
|
1452
|
+
isMessagePortOpen,
|
|
1453
|
+
send
|
|
1454
|
+
});
|
|
1455
|
+
});
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
const create$1 = async ({
|
|
1459
|
+
commandMap
|
|
1460
|
+
}) => {
|
|
1461
|
+
// TODO create a commandMap per rpc instance
|
|
1462
|
+
register(commandMap);
|
|
1463
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
1464
|
+
handleIpc(ipc);
|
|
1465
|
+
const rpc = createRpc(ipc);
|
|
1466
|
+
return rpc;
|
|
1467
|
+
};
|
|
1468
|
+
|
|
1469
|
+
const createMockRpc = ({
|
|
1470
|
+
commandMap
|
|
1471
|
+
}) => {
|
|
1472
|
+
const invocations = [];
|
|
1473
|
+
const invoke = (method, ...params) => {
|
|
1474
|
+
invocations.push([method, ...params]);
|
|
1475
|
+
const command = commandMap[method];
|
|
1476
|
+
if (!command) {
|
|
1477
|
+
throw new Error(`command ${method} not found`);
|
|
1478
|
+
}
|
|
1479
|
+
return command(...params);
|
|
1480
|
+
};
|
|
1481
|
+
const mockRpc = {
|
|
1482
|
+
invocations,
|
|
1483
|
+
invoke,
|
|
1484
|
+
invokeAndTransfer: invoke
|
|
1485
|
+
};
|
|
1486
|
+
return mockRpc;
|
|
1487
|
+
};
|
|
1488
|
+
|
|
1489
|
+
const rpcs = Object.create(null);
|
|
1490
|
+
const set$3 = (id, rpc) => {
|
|
1491
|
+
rpcs[id] = rpc;
|
|
1492
|
+
};
|
|
1493
|
+
const get = id => {
|
|
1494
|
+
return rpcs[id];
|
|
1495
|
+
};
|
|
1496
|
+
const remove = id => {
|
|
1497
|
+
delete rpcs[id];
|
|
1498
|
+
};
|
|
1499
|
+
|
|
1500
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
1501
|
+
const create = rpcId => {
|
|
1502
|
+
return {
|
|
1503
|
+
async dispose() {
|
|
1504
|
+
const rpc = get(rpcId);
|
|
1505
|
+
await rpc.dispose();
|
|
1506
|
+
},
|
|
1507
|
+
// @ts-ignore
|
|
1508
|
+
invoke(method, ...params) {
|
|
1509
|
+
const rpc = get(rpcId);
|
|
1510
|
+
// @ts-ignore
|
|
1511
|
+
return rpc.invoke(method, ...params);
|
|
1512
|
+
},
|
|
1513
|
+
// @ts-ignore
|
|
1514
|
+
invokeAndTransfer(method, ...params) {
|
|
1515
|
+
const rpc = get(rpcId);
|
|
1516
|
+
// @ts-ignore
|
|
1517
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
1518
|
+
},
|
|
1519
|
+
registerMockRpc(commandMap) {
|
|
1520
|
+
const mockRpc = createMockRpc({
|
|
1521
|
+
commandMap
|
|
1522
|
+
});
|
|
1523
|
+
set$3(rpcId, mockRpc);
|
|
1524
|
+
// @ts-ignore
|
|
1525
|
+
mockRpc[Symbol.dispose] = () => {
|
|
1526
|
+
remove(rpcId);
|
|
1527
|
+
};
|
|
1528
|
+
// @ts-ignore
|
|
1529
|
+
return mockRpc;
|
|
1530
|
+
},
|
|
1531
|
+
set(rpc) {
|
|
1532
|
+
set$3(rpcId, rpc);
|
|
1533
|
+
}
|
|
1534
|
+
};
|
|
1535
|
+
};
|
|
1536
|
+
|
|
1537
|
+
const {
|
|
1538
|
+
set: set$2
|
|
1539
|
+
} = create(FileSystemWorker$1);
|
|
1540
|
+
|
|
1541
|
+
const FileSystemWorker = {
|
|
1542
|
+
__proto__: null,
|
|
1543
|
+
set: set$2
|
|
1544
|
+
};
|
|
1545
|
+
|
|
1546
|
+
const {
|
|
1547
|
+
invoke: invoke$1,
|
|
1548
|
+
invokeAndTransfer,
|
|
1549
|
+
set: set$1
|
|
1550
|
+
} = create(RendererWorker);
|
|
1551
|
+
const sendMessagePortToFileSystemWorker$1 = async (port, rpcId) => {
|
|
1552
|
+
const command = 'FileSystem.handleMessagePort';
|
|
1553
|
+
await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToFileSystemWorker', port, command, rpcId);
|
|
1554
|
+
};
|
|
1555
|
+
|
|
1556
|
+
const isDebuggable = command => {
|
|
1557
|
+
return command.includes('node ') || command.includes('node.exe') || command.includes('node.mojom.NodeService');
|
|
1558
|
+
};
|
|
1559
|
+
|
|
1560
|
+
const KillProcess = 'Kill Process';
|
|
1561
|
+
const DebugProcess = 'Debug Process';
|
|
1562
|
+
const getMenuItems = process => {
|
|
1563
|
+
const menuItems = [{
|
|
1564
|
+
label: KillProcess
|
|
1565
|
+
}];
|
|
1566
|
+
if (isDebuggable(process.cmd)) {
|
|
1567
|
+
menuItems.push({
|
|
1568
|
+
label: DebugProcess
|
|
1569
|
+
});
|
|
1570
|
+
}
|
|
1571
|
+
return menuItems;
|
|
1572
|
+
};
|
|
1573
|
+
|
|
1574
|
+
const SigTerm = 'SIGTERM';
|
|
1575
|
+
|
|
1576
|
+
const handleContextMenuSelect = async (label, process) => {
|
|
1577
|
+
switch (label) {
|
|
1578
|
+
case DebugProcess:
|
|
1579
|
+
await invoke$1('AttachDebugger.attachDebugger', process.pid);
|
|
1580
|
+
return;
|
|
1581
|
+
case KillProcess:
|
|
1582
|
+
await invoke$1('Process.kill', process.pid, SigTerm);
|
|
1583
|
+
return;
|
|
1584
|
+
default:
|
|
1585
|
+
return;
|
|
1586
|
+
}
|
|
1587
|
+
};
|
|
1588
|
+
const handleContextMenu = async (state, index = state.focusedIndex, x = 0, y = 0) => {
|
|
1589
|
+
const process = state.visibleProcesses[index];
|
|
1590
|
+
if (!process) {
|
|
1591
|
+
return state;
|
|
1592
|
+
}
|
|
1593
|
+
const menuItems = getMenuItems(process);
|
|
1594
|
+
const event = await invoke$1('ElectronContextMenu.openContextMenu', menuItems, x, y, process);
|
|
1595
|
+
if (event.type === 'close' || !event.data) {
|
|
1596
|
+
return state;
|
|
1597
|
+
}
|
|
1598
|
+
await handleContextMenuSelect(event.data, process);
|
|
1599
|
+
return state;
|
|
1600
|
+
};
|
|
1601
|
+
|
|
1602
|
+
const handleDoubleClick = (state, index = state.focusedIndex) => {
|
|
1603
|
+
return toggleIndex(state, index);
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
const handleFocus = state => {
|
|
1607
|
+
return {
|
|
1608
|
+
...state,
|
|
1609
|
+
focused: true
|
|
1610
|
+
};
|
|
1611
|
+
};
|
|
1612
|
+
|
|
1613
|
+
const invoke = async (method, ...params) => {
|
|
1614
|
+
return invoke$1(method, ...params);
|
|
1615
|
+
};
|
|
1616
|
+
const {
|
|
1617
|
+
set
|
|
1618
|
+
} = FileSystemWorker;
|
|
1619
|
+
|
|
1620
|
+
const getErrorMessage = error => {
|
|
1621
|
+
if (error instanceof Error) {
|
|
1622
|
+
return error.message;
|
|
1623
|
+
}
|
|
1624
|
+
return String(error);
|
|
1625
|
+
};
|
|
1626
|
+
const getFocusedIndex = (oldFocusedIndex, visibleProcesses) => {
|
|
1627
|
+
if (visibleProcesses.length === 0) {
|
|
1628
|
+
return -1;
|
|
1629
|
+
}
|
|
1630
|
+
if (oldFocusedIndex < 0) {
|
|
1631
|
+
return 0;
|
|
1632
|
+
}
|
|
1633
|
+
return Math.min(oldFocusedIndex, visibleProcesses.length - 1);
|
|
1634
|
+
};
|
|
1635
|
+
const refresh = async state => {
|
|
1636
|
+
try {
|
|
1637
|
+
const rootPid = state.rootPid || (await invoke('ProcessId.getMainProcessId'));
|
|
1638
|
+
const processes = await invoke('ListProcessesWithMemoryUsage.listProcessesWithMemoryUsage', rootPid);
|
|
1639
|
+
const visibleProcesses = getVisibleProcesses(processes, state.collapsedPids, rootPid);
|
|
1640
|
+
return {
|
|
1641
|
+
...state,
|
|
1642
|
+
errorMessage: '',
|
|
1643
|
+
focusedIndex: getFocusedIndex(state.focusedIndex, visibleProcesses),
|
|
1644
|
+
initial: false,
|
|
1645
|
+
processes,
|
|
1646
|
+
rootPid,
|
|
1647
|
+
visibleProcesses
|
|
1648
|
+
};
|
|
1649
|
+
} catch (error) {
|
|
1650
|
+
return {
|
|
1651
|
+
...state,
|
|
1652
|
+
errorMessage: getErrorMessage(error),
|
|
1653
|
+
initial: false
|
|
1654
|
+
};
|
|
1655
|
+
}
|
|
1656
|
+
};
|
|
1657
|
+
|
|
1658
|
+
const loadContent = async state => {
|
|
1659
|
+
const newState = await refresh(state);
|
|
1660
|
+
return {
|
|
1661
|
+
error: undefined,
|
|
1662
|
+
state: newState
|
|
1663
|
+
};
|
|
1664
|
+
};
|
|
1665
|
+
|
|
1666
|
+
const renderFocus = (oldState, newState) => {
|
|
1667
|
+
if (!newState.focused || oldState.focusedIndex === newState.focusedIndex) {
|
|
1668
|
+
return [];
|
|
1669
|
+
}
|
|
1670
|
+
if (newState.focusedIndex < 0) {
|
|
1671
|
+
return [FocusSelector, '.ProcessExplorerTable'];
|
|
1672
|
+
}
|
|
1673
|
+
return [FocusSelector, `[data-index="${newState.focusedIndex}"]`];
|
|
1674
|
+
};
|
|
1675
|
+
|
|
1676
|
+
const renderFocusContext = (oldState, newState) => {
|
|
1677
|
+
if (oldState.focused === newState.focused) {
|
|
1678
|
+
return [];
|
|
1679
|
+
}
|
|
1680
|
+
return [SetFocusContext, newState.uid, newState.focused ? FocusExplorer : Empty];
|
|
1681
|
+
};
|
|
1682
|
+
|
|
1683
|
+
const text = data => {
|
|
1684
|
+
return {
|
|
1685
|
+
childCount: 0,
|
|
1686
|
+
text: data,
|
|
1687
|
+
type: Text
|
|
1688
|
+
};
|
|
1689
|
+
};
|
|
1690
|
+
|
|
1691
|
+
const Grid = 'grid';
|
|
1692
|
+
const GridCell = 'gridcell';
|
|
1693
|
+
const None = 'none';
|
|
1694
|
+
const Row$1 = 'row';
|
|
1695
|
+
const RowGroup = 'rowgroup';
|
|
1696
|
+
|
|
1697
|
+
const Cell = 'ProcessExplorerCell';
|
|
1698
|
+
const Error$1 = 'ProcessExplorerError';
|
|
1699
|
+
const HeaderCell = 'ProcessExplorerHeaderCell';
|
|
1700
|
+
const NameCell = 'ProcessExplorerNameCell';
|
|
1701
|
+
const Row = 'ProcessExplorerRow';
|
|
1702
|
+
const RowFocused = 'ProcessExplorerRowFocused';
|
|
1703
|
+
const Table = 'ProcessExplorerTable';
|
|
1704
|
+
const TableBody = 'ProcessExplorerTableBody';
|
|
1705
|
+
const TableHead = 'ProcessExplorerTableHead';
|
|
1706
|
+
const Viewlet = 'Viewlet';
|
|
1707
|
+
const ProcessExplorer = 'ProcessExplorer';
|
|
1708
|
+
|
|
1709
|
+
const HandleBlur = 1;
|
|
1710
|
+
const HandleClick = 2;
|
|
1711
|
+
const HandleContextMenu = 3;
|
|
1712
|
+
const HandleDoubleClick = 4;
|
|
1713
|
+
const HandleFocus = 5;
|
|
1714
|
+
const HandlePointerDown = 6;
|
|
1715
|
+
|
|
1716
|
+
const formatMemory = memory => {
|
|
1717
|
+
if (memory < 1000) {
|
|
1718
|
+
return `${memory} B`;
|
|
1719
|
+
}
|
|
1720
|
+
if (memory < 1000 ** 2) {
|
|
1721
|
+
return `${(memory / 1000).toFixed(1)} kB`;
|
|
1722
|
+
}
|
|
1723
|
+
if (memory < 1000 ** 3) {
|
|
1724
|
+
return `${(memory / 1000 ** 2).toFixed(1)} MB`;
|
|
1725
|
+
}
|
|
1726
|
+
if (memory < 1000 ** 4) {
|
|
1727
|
+
return `${(memory / 1000 ** 3).toFixed(1)} GB`;
|
|
1728
|
+
}
|
|
1729
|
+
return `${(memory / 1000 ** 4).toFixed(1)} TB`;
|
|
1730
|
+
};
|
|
1731
|
+
|
|
1732
|
+
const getRowClassName = focused => {
|
|
1733
|
+
if (focused) {
|
|
1734
|
+
return `${Row} ${RowFocused}`;
|
|
1735
|
+
}
|
|
1736
|
+
return Row;
|
|
1737
|
+
};
|
|
1738
|
+
const getPaddingLeft = process => {
|
|
1739
|
+
if (process.depth <= 1) {
|
|
1740
|
+
return '0';
|
|
1741
|
+
}
|
|
1742
|
+
const depthCh = (process.depth - 1) * 1.5;
|
|
1743
|
+
if (process.flags === None$1) {
|
|
1744
|
+
return `calc(${depthCh}ch + 17px)`;
|
|
1745
|
+
}
|
|
1746
|
+
return `${depthCh}ch`;
|
|
1747
|
+
};
|
|
1748
|
+
const getAriaExpanded = process => {
|
|
1749
|
+
switch (process.flags) {
|
|
1750
|
+
case Collapsed:
|
|
1751
|
+
return false;
|
|
1752
|
+
case Expanded:
|
|
1753
|
+
return true;
|
|
1754
|
+
default:
|
|
1755
|
+
return undefined;
|
|
1756
|
+
}
|
|
1757
|
+
};
|
|
1758
|
+
const getCellDom = (className, value, index, paddingLeft) => {
|
|
1759
|
+
return [{
|
|
1760
|
+
childCount: 1,
|
|
1761
|
+
className,
|
|
1762
|
+
name: String(index),
|
|
1763
|
+
paddingLeft,
|
|
1764
|
+
role: GridCell,
|
|
1765
|
+
tabIndex: -1,
|
|
1766
|
+
type: Td
|
|
1767
|
+
}, text(value)];
|
|
1768
|
+
};
|
|
1769
|
+
const getHeaderDom = () => {
|
|
1770
|
+
return [{
|
|
1771
|
+
childCount: 1,
|
|
1772
|
+
className: TableHead,
|
|
1773
|
+
role: RowGroup,
|
|
1774
|
+
type: THead
|
|
1775
|
+
}, {
|
|
1776
|
+
childCount: 3,
|
|
1777
|
+
className: Row,
|
|
1778
|
+
role: Row$1,
|
|
1779
|
+
type: Tr
|
|
1780
|
+
}, ...['Name', 'PID', 'Memory'].flatMap(label => [{
|
|
1781
|
+
childCount: 1,
|
|
1782
|
+
className: HeaderCell,
|
|
1783
|
+
type: Th
|
|
1784
|
+
}, text(label)])];
|
|
1785
|
+
};
|
|
1786
|
+
const getRowDom = (process, index, focused) => {
|
|
1787
|
+
return [{
|
|
1788
|
+
ariaDescription: '',
|
|
1789
|
+
ariaExpanded: getAriaExpanded(process),
|
|
1790
|
+
ariaLevel: process.depth,
|
|
1791
|
+
childCount: 3,
|
|
1792
|
+
className: getRowClassName(focused),
|
|
1793
|
+
'data-index': index,
|
|
1794
|
+
name: String(index),
|
|
1795
|
+
role: Row$1,
|
|
1796
|
+
tabIndex: focused ? 0 : -1,
|
|
1797
|
+
title: process.cmd,
|
|
1798
|
+
type: Tr
|
|
1799
|
+
}, ...getCellDom(`${Cell} ${NameCell}`, process.name, index, getPaddingLeft(process)), ...getCellDom(Cell, String(process.pid), index), ...getCellDom(Cell, formatMemory(process.memory), index)];
|
|
1800
|
+
};
|
|
1801
|
+
const getBodyDom = state => {
|
|
1802
|
+
const {
|
|
1803
|
+
focusedIndex,
|
|
1804
|
+
visibleProcesses
|
|
1805
|
+
} = state;
|
|
1806
|
+
return [{
|
|
1807
|
+
childCount: visibleProcesses.length,
|
|
1808
|
+
className: TableBody,
|
|
1809
|
+
role: RowGroup,
|
|
1810
|
+
type: TBody
|
|
1811
|
+
}, ...visibleProcesses.flatMap((process, index) => getRowDom(process, index, index === focusedIndex))];
|
|
1812
|
+
};
|
|
1813
|
+
const getErrorDom = errorMessage => {
|
|
1814
|
+
if (!errorMessage) {
|
|
1815
|
+
return [];
|
|
1816
|
+
}
|
|
1817
|
+
return [{
|
|
1818
|
+
childCount: 1,
|
|
1819
|
+
className: Error$1,
|
|
1820
|
+
type: Div
|
|
1821
|
+
}, text(errorMessage)];
|
|
1822
|
+
};
|
|
1823
|
+
const getDom = state => {
|
|
1824
|
+
if (state.initial) {
|
|
1825
|
+
return [];
|
|
1826
|
+
}
|
|
1827
|
+
const errorDom = getErrorDom(state.errorMessage);
|
|
1828
|
+
return [{
|
|
1829
|
+
childCount: 1 + (errorDom.length > 0 ? 1 : 0),
|
|
1830
|
+
className: `${Viewlet} ${ProcessExplorer}`,
|
|
1831
|
+
role: None,
|
|
1832
|
+
type: Div
|
|
1833
|
+
}, {
|
|
1834
|
+
ariaLabel: 'Process Explorer',
|
|
1835
|
+
ariaRowCount: state.visibleProcesses.length + 1,
|
|
1836
|
+
childCount: 2,
|
|
1837
|
+
className: Table,
|
|
1838
|
+
onBlur: HandleBlur,
|
|
1839
|
+
onClick: HandleClick,
|
|
1840
|
+
onContextMenu: HandleContextMenu,
|
|
1841
|
+
onDblClick: HandleDoubleClick,
|
|
1842
|
+
onFocus: HandleFocus,
|
|
1843
|
+
onPointerDown: HandlePointerDown,
|
|
1844
|
+
role: Grid,
|
|
1845
|
+
tabIndex: 0,
|
|
1846
|
+
type: Table$1
|
|
1847
|
+
}, ...getHeaderDom(), ...getBodyDom(state), ...errorDom];
|
|
1848
|
+
};
|
|
1849
|
+
const renderItems = (oldState, newState) => {
|
|
1850
|
+
return [SetDom2, newState.uid, getDom(newState)];
|
|
1851
|
+
};
|
|
1852
|
+
|
|
1853
|
+
const getRenderer = diffType => {
|
|
1854
|
+
switch (diffType) {
|
|
1855
|
+
case RenderFocus:
|
|
1856
|
+
return renderFocus;
|
|
1857
|
+
case RenderFocusContext:
|
|
1858
|
+
return renderFocusContext;
|
|
1859
|
+
case RenderItems:
|
|
1860
|
+
return renderItems;
|
|
1861
|
+
default:
|
|
1862
|
+
throw new Error(`unknown renderer ${diffType}`);
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
|
|
1866
|
+
const applyRender = (oldState, newState, diffResult) => {
|
|
1867
|
+
const commands = [];
|
|
1868
|
+
for (const item of diffResult) {
|
|
1869
|
+
const fn = getRenderer(item);
|
|
1870
|
+
const result = fn(oldState, newState);
|
|
1871
|
+
if (result.length > 0) {
|
|
1872
|
+
commands.push(result);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
return commands;
|
|
1876
|
+
};
|
|
1877
|
+
|
|
1878
|
+
const render2 = (uid, diffResult) => {
|
|
1879
|
+
const {
|
|
1880
|
+
oldState,
|
|
1881
|
+
scheduledState
|
|
1882
|
+
} = get$2(uid);
|
|
1883
|
+
set$4(uid, scheduledState, scheduledState);
|
|
1884
|
+
return applyRender(oldState, scheduledState, diffResult);
|
|
1885
|
+
};
|
|
1886
|
+
|
|
1887
|
+
const renderEventListeners = () => {
|
|
1888
|
+
return [{
|
|
1889
|
+
name: HandleFocus,
|
|
1890
|
+
params: ['handleFocus']
|
|
1891
|
+
}, {
|
|
1892
|
+
name: HandleBlur,
|
|
1893
|
+
params: ['handleBlur']
|
|
1894
|
+
}, {
|
|
1895
|
+
name: HandleClick,
|
|
1896
|
+
params: ['handleClickAt', TargetName],
|
|
1897
|
+
preventDefault: true
|
|
1898
|
+
}, {
|
|
1899
|
+
name: HandleDoubleClick,
|
|
1900
|
+
params: ['handleDoubleClick', TargetName],
|
|
1901
|
+
preventDefault: true
|
|
1902
|
+
}, {
|
|
1903
|
+
name: HandleContextMenu,
|
|
1904
|
+
params: ['handleContextMenu', TargetName, ClientX, ClientY],
|
|
1905
|
+
preventDefault: true
|
|
1906
|
+
}, {
|
|
1907
|
+
name: HandlePointerDown,
|
|
1908
|
+
params: ['handleClickAt', TargetName]
|
|
1909
|
+
}];
|
|
1910
|
+
};
|
|
1911
|
+
|
|
1912
|
+
const commandMap = {
|
|
1913
|
+
'ProcessExplorer.collapseAll': wrapCommand(collapseAll),
|
|
1914
|
+
'ProcessExplorer.create': create$9,
|
|
1915
|
+
'ProcessExplorer.diff2': diff2,
|
|
1916
|
+
'ProcessExplorer.expandAll': wrapCommand(expandAll),
|
|
1917
|
+
'ProcessExplorer.focusFirst': wrapCommand(focusFirst),
|
|
1918
|
+
'ProcessExplorer.focusLast': wrapCommand(focusLast),
|
|
1919
|
+
'ProcessExplorer.focusNext': wrapCommand(focusNext),
|
|
1920
|
+
'ProcessExplorer.focusPrevious': wrapCommand(focusPrevious),
|
|
1921
|
+
'ProcessExplorer.getCommandIds': getCommandIds,
|
|
1922
|
+
'ProcessExplorer.getKeyBindings': getKeyBindings,
|
|
1923
|
+
'ProcessExplorer.handleArrowLeft': wrapCommand(handleArrowLeft),
|
|
1924
|
+
'ProcessExplorer.handleArrowRight': wrapCommand(handleArrowRight),
|
|
1925
|
+
'ProcessExplorer.handleBlur': wrapCommand(handleBlur),
|
|
1926
|
+
'ProcessExplorer.handleClickAt': wrapCommand(handleClickAt),
|
|
1927
|
+
'ProcessExplorer.handleContextMenu': wrapCommand(handleContextMenu),
|
|
1928
|
+
'ProcessExplorer.handleDoubleClick': wrapCommand(handleDoubleClick),
|
|
1929
|
+
'ProcessExplorer.handleFocus': wrapCommand(handleFocus),
|
|
1930
|
+
'ProcessExplorer.loadContent': wrapLoadContent(loadContent),
|
|
1931
|
+
'ProcessExplorer.refresh': wrapCommand(refresh),
|
|
1932
|
+
'ProcessExplorer.render2': render2,
|
|
1933
|
+
'ProcessExplorer.renderEventListeners': renderEventListeners,
|
|
1934
|
+
'ProcessExplorer.terminate': terminate
|
|
1935
|
+
};
|
|
1936
|
+
|
|
1937
|
+
const sendMessagePortToFileSystemWorker = async port => {
|
|
1938
|
+
await sendMessagePortToFileSystemWorker$1(port, 0);
|
|
1939
|
+
};
|
|
1940
|
+
|
|
1941
|
+
const createFileSystemWorkerRpc = async () => {
|
|
1942
|
+
try {
|
|
1943
|
+
const rpc = await create$2({
|
|
1944
|
+
commandMap: {},
|
|
1945
|
+
send: sendMessagePortToFileSystemWorker
|
|
1946
|
+
});
|
|
1947
|
+
return rpc;
|
|
1948
|
+
} catch (error) {
|
|
1949
|
+
throw new VError(error, `Failed to create file system worker rpc`);
|
|
1950
|
+
}
|
|
1951
|
+
};
|
|
1952
|
+
|
|
1953
|
+
const initializeFileSystemWorker = async () => {
|
|
1954
|
+
const rpc = await createFileSystemWorkerRpc();
|
|
1955
|
+
set(rpc);
|
|
1956
|
+
};
|
|
1957
|
+
|
|
1958
|
+
const initializeRendererWorker = async () => {
|
|
1959
|
+
const rpc = await create$1({
|
|
1960
|
+
commandMap: commandMap
|
|
1961
|
+
});
|
|
1962
|
+
set$1(rpc);
|
|
1963
|
+
};
|
|
1964
|
+
|
|
1965
|
+
const listen = async () => {
|
|
1966
|
+
registerCommands(commandMap);
|
|
1967
|
+
await Promise.all([initializeRendererWorker(), initializeFileSystemWorker()]);
|
|
1968
|
+
};
|
|
1969
|
+
|
|
1970
|
+
const main = async () => {
|
|
1971
|
+
await listen();
|
|
1972
|
+
};
|
|
1973
|
+
|
|
1974
|
+
main();
|