@lvce-editor/running-extensions-view 1.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/dist/runningExtensionsViewMain.js +1420 -0
- package/package.json +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lvce Editor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,1420 @@
|
|
|
1
|
+
const normalizeLine = line => {
|
|
2
|
+
if (line.startsWith('Error: ')) {
|
|
3
|
+
return line.slice('Error: '.length);
|
|
4
|
+
}
|
|
5
|
+
if (line.startsWith('VError: ')) {
|
|
6
|
+
return line.slice('VError: '.length);
|
|
7
|
+
}
|
|
8
|
+
return line;
|
|
9
|
+
};
|
|
10
|
+
const getCombinedMessage = (error, message) => {
|
|
11
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
12
|
+
if (message) {
|
|
13
|
+
return `${message}: ${stringifiedError}`;
|
|
14
|
+
}
|
|
15
|
+
return stringifiedError;
|
|
16
|
+
};
|
|
17
|
+
const NewLine$2 = '\n';
|
|
18
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
19
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
20
|
+
};
|
|
21
|
+
const mergeStacks = (parent, child) => {
|
|
22
|
+
if (!child) {
|
|
23
|
+
return parent;
|
|
24
|
+
}
|
|
25
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
26
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
27
|
+
if (childNewLineIndex === -1) {
|
|
28
|
+
return parent;
|
|
29
|
+
}
|
|
30
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
31
|
+
const childRest = child.slice(childNewLineIndex);
|
|
32
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
33
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
34
|
+
return parentFirstLine + childRest;
|
|
35
|
+
}
|
|
36
|
+
return child;
|
|
37
|
+
};
|
|
38
|
+
class VError extends Error {
|
|
39
|
+
constructor(error, message) {
|
|
40
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
41
|
+
super(combinedMessage);
|
|
42
|
+
this.name = 'VError';
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
45
|
+
}
|
|
46
|
+
if (error.codeFrame) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
this.codeFrame = error.codeFrame;
|
|
49
|
+
}
|
|
50
|
+
if (error.code) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
this.code = error.code;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const isMessagePort = value => {
|
|
58
|
+
return value && value instanceof MessagePort;
|
|
59
|
+
};
|
|
60
|
+
const isMessagePortMain = value => {
|
|
61
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
62
|
+
};
|
|
63
|
+
const isOffscreenCanvas = value => {
|
|
64
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
65
|
+
};
|
|
66
|
+
const isInstanceOf = (value, constructorName) => {
|
|
67
|
+
return value?.constructor?.name === constructorName;
|
|
68
|
+
};
|
|
69
|
+
const isSocket = value => {
|
|
70
|
+
return isInstanceOf(value, 'Socket');
|
|
71
|
+
};
|
|
72
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
73
|
+
const isTransferrable = value => {
|
|
74
|
+
for (const fn of transferrables) {
|
|
75
|
+
if (fn(value)) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
};
|
|
81
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
82
|
+
if (!value) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (isTransferrable(value)) {
|
|
86
|
+
transferrables.push(value);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
for (const item of value) {
|
|
91
|
+
walkValue(item, transferrables, isTransferrable);
|
|
92
|
+
}
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === 'object') {
|
|
96
|
+
for (const property of Object.values(value)) {
|
|
97
|
+
walkValue(property, transferrables, isTransferrable);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const getTransferrables = value => {
|
|
102
|
+
const transferrables = [];
|
|
103
|
+
walkValue(value, transferrables, isTransferrable);
|
|
104
|
+
return transferrables;
|
|
105
|
+
};
|
|
106
|
+
const attachEvents = that => {
|
|
107
|
+
const handleMessage = (...args) => {
|
|
108
|
+
const data = that.getData(...args);
|
|
109
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
110
|
+
data
|
|
111
|
+
}));
|
|
112
|
+
};
|
|
113
|
+
that.onMessage(handleMessage);
|
|
114
|
+
const handleClose = event => {
|
|
115
|
+
that.dispatchEvent(new Event('close'));
|
|
116
|
+
};
|
|
117
|
+
that.onClose(handleClose);
|
|
118
|
+
};
|
|
119
|
+
class Ipc extends EventTarget {
|
|
120
|
+
constructor(rawIpc) {
|
|
121
|
+
super();
|
|
122
|
+
this._rawIpc = rawIpc;
|
|
123
|
+
attachEvents(this);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
127
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
128
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
129
|
+
const NewLine$1 = '\n';
|
|
130
|
+
const joinLines$1 = lines => {
|
|
131
|
+
return lines.join(NewLine$1);
|
|
132
|
+
};
|
|
133
|
+
const RE_AT = /^\s+at/;
|
|
134
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
135
|
+
const isNormalStackLine = line => {
|
|
136
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
137
|
+
};
|
|
138
|
+
const getDetails = lines => {
|
|
139
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
140
|
+
if (index === -1) {
|
|
141
|
+
return {
|
|
142
|
+
actualMessage: joinLines$1(lines),
|
|
143
|
+
rest: []
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
let lastIndex = index - 1;
|
|
147
|
+
while (++lastIndex < lines.length) {
|
|
148
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
actualMessage: lines[index - 1],
|
|
154
|
+
rest: lines.slice(index, lastIndex)
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
const splitLines$1 = lines => {
|
|
158
|
+
return lines.split(NewLine$1);
|
|
159
|
+
};
|
|
160
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
161
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
162
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
163
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
164
|
+
};
|
|
165
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
166
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
167
|
+
};
|
|
168
|
+
const getMessageCodeBlock = stderr => {
|
|
169
|
+
const lines = splitLines$1(stderr);
|
|
170
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
171
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
172
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
173
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
174
|
+
return relevantMessage;
|
|
175
|
+
};
|
|
176
|
+
const isModuleNotFoundMessage = line => {
|
|
177
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
178
|
+
};
|
|
179
|
+
const getModuleNotFoundError = stderr => {
|
|
180
|
+
const lines = splitLines$1(stderr);
|
|
181
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
182
|
+
const message = lines[messageIndex];
|
|
183
|
+
return {
|
|
184
|
+
code: ERR_MODULE_NOT_FOUND,
|
|
185
|
+
message
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
const isModuleNotFoundError = stderr => {
|
|
189
|
+
if (!stderr) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
193
|
+
};
|
|
194
|
+
const isModulesSyntaxError = stderr => {
|
|
195
|
+
if (!stderr) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
199
|
+
};
|
|
200
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
201
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
202
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
203
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
204
|
+
};
|
|
205
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
206
|
+
const message = getMessageCodeBlock(stderr);
|
|
207
|
+
return {
|
|
208
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE,
|
|
209
|
+
message: `Incompatible native node module: ${message}`
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
const getModuleSyntaxError = () => {
|
|
213
|
+
return {
|
|
214
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
|
|
215
|
+
message: `ES Modules are not supported in electron`
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
219
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
220
|
+
return getNativeModuleErrorMessage(stderr);
|
|
221
|
+
}
|
|
222
|
+
if (isModulesSyntaxError(stderr)) {
|
|
223
|
+
return getModuleSyntaxError();
|
|
224
|
+
}
|
|
225
|
+
if (isModuleNotFoundError(stderr)) {
|
|
226
|
+
return getModuleNotFoundError(stderr);
|
|
227
|
+
}
|
|
228
|
+
const lines = splitLines$1(stderr);
|
|
229
|
+
const {
|
|
230
|
+
actualMessage,
|
|
231
|
+
rest
|
|
232
|
+
} = getDetails(lines);
|
|
233
|
+
return {
|
|
234
|
+
code: '',
|
|
235
|
+
message: actualMessage,
|
|
236
|
+
stack: rest
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
class IpcError extends VError {
|
|
240
|
+
// @ts-ignore
|
|
241
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
242
|
+
if (stdout || stderr) {
|
|
243
|
+
// @ts-ignore
|
|
244
|
+
const {
|
|
245
|
+
code,
|
|
246
|
+
message,
|
|
247
|
+
stack
|
|
248
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
249
|
+
const cause = new Error(message);
|
|
250
|
+
// @ts-ignore
|
|
251
|
+
cause.code = code;
|
|
252
|
+
if (stack) {
|
|
253
|
+
Object.defineProperty(cause, 'stack', {
|
|
254
|
+
configurable: true,
|
|
255
|
+
enumerable: false,
|
|
256
|
+
value: stack,
|
|
257
|
+
writable: true
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
super(cause, betterMessage);
|
|
261
|
+
} else {
|
|
262
|
+
super(betterMessage);
|
|
263
|
+
}
|
|
264
|
+
// @ts-ignore
|
|
265
|
+
this.name = 'IpcError';
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
this.stdout = stdout;
|
|
268
|
+
// @ts-ignore
|
|
269
|
+
this.stderr = stderr;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const readyMessage = 'ready';
|
|
273
|
+
const getData$2 = event => {
|
|
274
|
+
return event.data;
|
|
275
|
+
};
|
|
276
|
+
const listen$7 = () => {
|
|
277
|
+
// @ts-ignore
|
|
278
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
279
|
+
throw new TypeError('module is not in web worker scope');
|
|
280
|
+
}
|
|
281
|
+
return globalThis;
|
|
282
|
+
};
|
|
283
|
+
const signal$8 = global => {
|
|
284
|
+
global.postMessage(readyMessage);
|
|
285
|
+
};
|
|
286
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
287
|
+
getData(event) {
|
|
288
|
+
return getData$2(event);
|
|
289
|
+
}
|
|
290
|
+
send(message) {
|
|
291
|
+
// @ts-ignore
|
|
292
|
+
this._rawIpc.postMessage(message);
|
|
293
|
+
}
|
|
294
|
+
sendAndTransfer(message) {
|
|
295
|
+
const transfer = getTransferrables(message);
|
|
296
|
+
// @ts-ignore
|
|
297
|
+
this._rawIpc.postMessage(message, transfer);
|
|
298
|
+
}
|
|
299
|
+
dispose() {
|
|
300
|
+
// ignore
|
|
301
|
+
}
|
|
302
|
+
onClose(callback) {
|
|
303
|
+
// ignore
|
|
304
|
+
}
|
|
305
|
+
onMessage(callback) {
|
|
306
|
+
this._rawIpc.addEventListener('message', callback);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const wrap$f = global => {
|
|
310
|
+
return new IpcChildWithModuleWorker(global);
|
|
311
|
+
};
|
|
312
|
+
const waitForFirstMessage = async port => {
|
|
313
|
+
const {
|
|
314
|
+
promise,
|
|
315
|
+
resolve
|
|
316
|
+
} = Promise.withResolvers();
|
|
317
|
+
port.addEventListener('message', resolve, {
|
|
318
|
+
once: true
|
|
319
|
+
});
|
|
320
|
+
const event = await promise;
|
|
321
|
+
// @ts-ignore
|
|
322
|
+
return event.data;
|
|
323
|
+
};
|
|
324
|
+
const listen$6 = async () => {
|
|
325
|
+
const parentIpcRaw = listen$7();
|
|
326
|
+
signal$8(parentIpcRaw);
|
|
327
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
328
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
329
|
+
if (firstMessage.method !== 'initialize') {
|
|
330
|
+
throw new IpcError('unexpected first message');
|
|
331
|
+
}
|
|
332
|
+
const type = firstMessage.params[0];
|
|
333
|
+
if (type === 'message-port') {
|
|
334
|
+
parentIpc.send({
|
|
335
|
+
id: firstMessage.id,
|
|
336
|
+
jsonrpc: '2.0',
|
|
337
|
+
result: null
|
|
338
|
+
});
|
|
339
|
+
parentIpc.dispose();
|
|
340
|
+
const port = firstMessage.params[1];
|
|
341
|
+
return port;
|
|
342
|
+
}
|
|
343
|
+
return globalThis;
|
|
344
|
+
};
|
|
345
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
346
|
+
getData(event) {
|
|
347
|
+
return getData$2(event);
|
|
348
|
+
}
|
|
349
|
+
send(message) {
|
|
350
|
+
this._rawIpc.postMessage(message);
|
|
351
|
+
}
|
|
352
|
+
sendAndTransfer(message) {
|
|
353
|
+
const transfer = getTransferrables(message);
|
|
354
|
+
this._rawIpc.postMessage(message, transfer);
|
|
355
|
+
}
|
|
356
|
+
dispose() {
|
|
357
|
+
if (this._rawIpc.close) {
|
|
358
|
+
this._rawIpc.close();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
onClose(callback) {
|
|
362
|
+
// ignore
|
|
363
|
+
}
|
|
364
|
+
onMessage(callback) {
|
|
365
|
+
this._rawIpc.addEventListener('message', callback);
|
|
366
|
+
this._rawIpc.start();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const wrap$e = port => {
|
|
370
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
371
|
+
};
|
|
372
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
373
|
+
__proto__: null,
|
|
374
|
+
listen: listen$6,
|
|
375
|
+
wrap: wrap$e
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
class CommandNotFoundError extends Error {
|
|
379
|
+
constructor(command) {
|
|
380
|
+
super(`Command not found ${command}`);
|
|
381
|
+
this.name = 'CommandNotFoundError';
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const commands = Object.create(null);
|
|
385
|
+
const register = commandMap => {
|
|
386
|
+
Object.assign(commands, commandMap);
|
|
387
|
+
};
|
|
388
|
+
const getCommand = key => {
|
|
389
|
+
return commands[key];
|
|
390
|
+
};
|
|
391
|
+
const execute = (command, ...args) => {
|
|
392
|
+
const fn = getCommand(command);
|
|
393
|
+
if (!fn) {
|
|
394
|
+
throw new CommandNotFoundError(command);
|
|
395
|
+
}
|
|
396
|
+
return fn(...args);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const Two$1 = '2.0';
|
|
400
|
+
const callbacks = Object.create(null);
|
|
401
|
+
const get$2 = id => {
|
|
402
|
+
return callbacks[id];
|
|
403
|
+
};
|
|
404
|
+
const remove$1 = id => {
|
|
405
|
+
delete callbacks[id];
|
|
406
|
+
};
|
|
407
|
+
class JsonRpcError extends Error {
|
|
408
|
+
constructor(message) {
|
|
409
|
+
super(message);
|
|
410
|
+
this.name = 'JsonRpcError';
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const NewLine = '\n';
|
|
414
|
+
const DomException = 'DOMException';
|
|
415
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
416
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
417
|
+
const TypeError$1 = 'TypeError';
|
|
418
|
+
const getErrorConstructor = (message, type) => {
|
|
419
|
+
if (type) {
|
|
420
|
+
switch (type) {
|
|
421
|
+
case DomException:
|
|
422
|
+
return DOMException;
|
|
423
|
+
case ReferenceError$1:
|
|
424
|
+
return ReferenceError;
|
|
425
|
+
case SyntaxError$1:
|
|
426
|
+
return SyntaxError;
|
|
427
|
+
case TypeError$1:
|
|
428
|
+
return TypeError;
|
|
429
|
+
default:
|
|
430
|
+
return Error;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (message.startsWith('TypeError: ')) {
|
|
434
|
+
return TypeError;
|
|
435
|
+
}
|
|
436
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
437
|
+
return SyntaxError;
|
|
438
|
+
}
|
|
439
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
440
|
+
return ReferenceError;
|
|
441
|
+
}
|
|
442
|
+
return Error;
|
|
443
|
+
};
|
|
444
|
+
const constructError = (message, type, name) => {
|
|
445
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
446
|
+
if (ErrorConstructor === DOMException && name) {
|
|
447
|
+
return new ErrorConstructor(message, name);
|
|
448
|
+
}
|
|
449
|
+
if (ErrorConstructor === Error) {
|
|
450
|
+
const error = new Error(message);
|
|
451
|
+
if (name && name !== 'VError') {
|
|
452
|
+
error.name = name;
|
|
453
|
+
}
|
|
454
|
+
return error;
|
|
455
|
+
}
|
|
456
|
+
return new ErrorConstructor(message);
|
|
457
|
+
};
|
|
458
|
+
const joinLines = lines => {
|
|
459
|
+
return lines.join(NewLine);
|
|
460
|
+
};
|
|
461
|
+
const splitLines = lines => {
|
|
462
|
+
return lines.split(NewLine);
|
|
463
|
+
};
|
|
464
|
+
const getCurrentStack = () => {
|
|
465
|
+
const stackLinesToSkip = 3;
|
|
466
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
467
|
+
return currentStack;
|
|
468
|
+
};
|
|
469
|
+
const getNewLineIndex = (string, startIndex) => {
|
|
470
|
+
{
|
|
471
|
+
return string.indexOf(NewLine);
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
const getParentStack = error => {
|
|
475
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
476
|
+
if (parentStack.startsWith(' at')) {
|
|
477
|
+
parentStack = error.message + NewLine + parentStack;
|
|
478
|
+
}
|
|
479
|
+
return parentStack;
|
|
480
|
+
};
|
|
481
|
+
const MethodNotFound = -32601;
|
|
482
|
+
const Custom = -32001;
|
|
483
|
+
const restoreExistingError = (error, currentStack) => {
|
|
484
|
+
if (typeof error.stack === 'string') {
|
|
485
|
+
error.stack = error.stack + NewLine + currentStack;
|
|
486
|
+
}
|
|
487
|
+
return error;
|
|
488
|
+
};
|
|
489
|
+
const restoreMethodNotFoundError = (error, currentStack) => {
|
|
490
|
+
const restoredError = new JsonRpcError(error.message);
|
|
491
|
+
const parentStack = getParentStack(error);
|
|
492
|
+
restoredError.stack = parentStack + NewLine + currentStack;
|
|
493
|
+
return restoredError;
|
|
494
|
+
};
|
|
495
|
+
const restoreStackFromData = (restoredError, error, currentStack) => {
|
|
496
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
497
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (error.data.stack) {
|
|
501
|
+
restoredError.stack = error.data.stack;
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
const applyDataProperties = (restoredError, error) => {
|
|
505
|
+
if (!error.data) {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
restoreStackFromData(restoredError, error, getCurrentStack());
|
|
509
|
+
if (error.data.codeFrame) {
|
|
510
|
+
// @ts-ignore
|
|
511
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
512
|
+
}
|
|
513
|
+
if (error.data.code) {
|
|
514
|
+
// @ts-ignore
|
|
515
|
+
restoredError.code = error.data.code;
|
|
516
|
+
}
|
|
517
|
+
if (error.data.type) {
|
|
518
|
+
// @ts-ignore
|
|
519
|
+
restoredError.name = error.data.type;
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
const applyDirectProperties = (restoredError, error) => {
|
|
523
|
+
if (error.stack) {
|
|
524
|
+
const lowerStack = restoredError.stack || '';
|
|
525
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
526
|
+
const parentStack = getParentStack(error);
|
|
527
|
+
// @ts-ignore
|
|
528
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
529
|
+
}
|
|
530
|
+
if (error.codeFrame) {
|
|
531
|
+
// @ts-ignore
|
|
532
|
+
restoredError.codeFrame = error.codeFrame;
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
const restoreMessageError = (error, _currentStack) => {
|
|
536
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
537
|
+
if (error.data) {
|
|
538
|
+
applyDataProperties(restoredError, error);
|
|
539
|
+
} else {
|
|
540
|
+
applyDirectProperties(restoredError, error);
|
|
541
|
+
}
|
|
542
|
+
return restoredError;
|
|
543
|
+
};
|
|
544
|
+
const restoreJsonRpcError = error => {
|
|
545
|
+
const currentStack = getCurrentStack();
|
|
546
|
+
if (error && error instanceof Error) {
|
|
547
|
+
return restoreExistingError(error, currentStack);
|
|
548
|
+
}
|
|
549
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
550
|
+
return restoreMethodNotFoundError(error, currentStack);
|
|
551
|
+
}
|
|
552
|
+
if (error && error.message) {
|
|
553
|
+
return restoreMessageError(error);
|
|
554
|
+
}
|
|
555
|
+
if (typeof error === 'string') {
|
|
556
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
557
|
+
}
|
|
558
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
559
|
+
};
|
|
560
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
561
|
+
if ('error' in responseMessage) {
|
|
562
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
563
|
+
throw restoredError;
|
|
564
|
+
}
|
|
565
|
+
if ('result' in responseMessage) {
|
|
566
|
+
return responseMessage.result;
|
|
567
|
+
}
|
|
568
|
+
throw new JsonRpcError('unexpected response message');
|
|
569
|
+
};
|
|
570
|
+
const warn = (...args) => {
|
|
571
|
+
console.warn(...args);
|
|
572
|
+
};
|
|
573
|
+
const resolve = (id, response) => {
|
|
574
|
+
const fn = get$2(id);
|
|
575
|
+
if (!fn) {
|
|
576
|
+
console.log(response);
|
|
577
|
+
warn(`callback ${id} may already be disposed`);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
fn(response);
|
|
581
|
+
remove$1(id);
|
|
582
|
+
};
|
|
583
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
584
|
+
const getErrorType = prettyError => {
|
|
585
|
+
if (prettyError && prettyError.type) {
|
|
586
|
+
return prettyError.type;
|
|
587
|
+
}
|
|
588
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
589
|
+
return prettyError.constructor.name;
|
|
590
|
+
}
|
|
591
|
+
return undefined;
|
|
592
|
+
};
|
|
593
|
+
const isAlreadyStack = line => {
|
|
594
|
+
return line.trim().startsWith('at ');
|
|
595
|
+
};
|
|
596
|
+
const getStack = prettyError => {
|
|
597
|
+
const stackString = prettyError.stack || '';
|
|
598
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
599
|
+
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
600
|
+
return stackString.slice(newLineIndex + 1);
|
|
601
|
+
}
|
|
602
|
+
return stackString;
|
|
603
|
+
};
|
|
604
|
+
const getErrorProperty = (error, prettyError) => {
|
|
605
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
606
|
+
return {
|
|
607
|
+
code: MethodNotFound,
|
|
608
|
+
data: error.stack,
|
|
609
|
+
message: error.message
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
code: Custom,
|
|
614
|
+
data: {
|
|
615
|
+
code: prettyError.code,
|
|
616
|
+
codeFrame: prettyError.codeFrame,
|
|
617
|
+
name: prettyError.name,
|
|
618
|
+
stack: getStack(prettyError),
|
|
619
|
+
type: getErrorType(prettyError)
|
|
620
|
+
},
|
|
621
|
+
message: prettyError.message
|
|
622
|
+
};
|
|
623
|
+
};
|
|
624
|
+
const create$1$1 = (id, error) => {
|
|
625
|
+
return {
|
|
626
|
+
error,
|
|
627
|
+
id,
|
|
628
|
+
jsonrpc: Two$1
|
|
629
|
+
};
|
|
630
|
+
};
|
|
631
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
632
|
+
const prettyError = preparePrettyError(error);
|
|
633
|
+
logError(error, prettyError);
|
|
634
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
635
|
+
return create$1$1(id, errorProperty);
|
|
636
|
+
};
|
|
637
|
+
const create$7 = (message, result) => {
|
|
638
|
+
return {
|
|
639
|
+
id: message.id,
|
|
640
|
+
jsonrpc: Two$1,
|
|
641
|
+
result: result ?? null
|
|
642
|
+
};
|
|
643
|
+
};
|
|
644
|
+
const getSuccessResponse = (message, result) => {
|
|
645
|
+
const resultProperty = result ?? null;
|
|
646
|
+
return create$7(message, resultProperty);
|
|
647
|
+
};
|
|
648
|
+
const getErrorResponseSimple = (id, error) => {
|
|
649
|
+
return {
|
|
650
|
+
error: {
|
|
651
|
+
code: Custom,
|
|
652
|
+
data: error,
|
|
653
|
+
// @ts-ignore
|
|
654
|
+
message: error.message
|
|
655
|
+
},
|
|
656
|
+
id,
|
|
657
|
+
jsonrpc: Two$1
|
|
658
|
+
};
|
|
659
|
+
};
|
|
660
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
661
|
+
try {
|
|
662
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
663
|
+
return getSuccessResponse(message, result);
|
|
664
|
+
} catch (error) {
|
|
665
|
+
if (ipc.canUseSimpleErrorResponse) {
|
|
666
|
+
return getErrorResponseSimple(message.id, error);
|
|
667
|
+
}
|
|
668
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
const defaultPreparePrettyError = error => {
|
|
672
|
+
return error;
|
|
673
|
+
};
|
|
674
|
+
const defaultLogError = () => {
|
|
675
|
+
// ignore
|
|
676
|
+
};
|
|
677
|
+
const defaultRequiresSocket = () => {
|
|
678
|
+
return false;
|
|
679
|
+
};
|
|
680
|
+
const defaultResolve = resolve;
|
|
681
|
+
|
|
682
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
683
|
+
const normalizeParams = args => {
|
|
684
|
+
if (args.length === 1) {
|
|
685
|
+
const options = args[0];
|
|
686
|
+
return {
|
|
687
|
+
execute: options.execute,
|
|
688
|
+
ipc: options.ipc,
|
|
689
|
+
logError: options.logError || defaultLogError,
|
|
690
|
+
message: options.message,
|
|
691
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
692
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket,
|
|
693
|
+
resolve: options.resolve || defaultResolve
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
return {
|
|
697
|
+
execute: args[2],
|
|
698
|
+
ipc: args[0],
|
|
699
|
+
logError: args[5],
|
|
700
|
+
message: args[1],
|
|
701
|
+
preparePrettyError: args[4],
|
|
702
|
+
requiresSocket: args[6],
|
|
703
|
+
resolve: args[3]
|
|
704
|
+
};
|
|
705
|
+
};
|
|
706
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
707
|
+
const options = normalizeParams(args);
|
|
708
|
+
const {
|
|
709
|
+
execute,
|
|
710
|
+
ipc,
|
|
711
|
+
logError,
|
|
712
|
+
message,
|
|
713
|
+
preparePrettyError,
|
|
714
|
+
requiresSocket,
|
|
715
|
+
resolve
|
|
716
|
+
} = options;
|
|
717
|
+
if ('id' in message) {
|
|
718
|
+
if ('method' in message) {
|
|
719
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
720
|
+
try {
|
|
721
|
+
ipc.send(response);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
724
|
+
ipc.send(errorResponse);
|
|
725
|
+
}
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
resolve(message.id, message);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
if ('method' in message) {
|
|
732
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
throw new JsonRpcError('unexpected message');
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
const Two = '2.0';
|
|
739
|
+
|
|
740
|
+
const create$6 = (method, params) => {
|
|
741
|
+
return {
|
|
742
|
+
jsonrpc: Two,
|
|
743
|
+
method,
|
|
744
|
+
params
|
|
745
|
+
};
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
const create$5 = (id, method, params) => {
|
|
749
|
+
const message = {
|
|
750
|
+
id,
|
|
751
|
+
jsonrpc: Two,
|
|
752
|
+
method,
|
|
753
|
+
params
|
|
754
|
+
};
|
|
755
|
+
return message;
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
let id = 0;
|
|
759
|
+
const create$4 = () => {
|
|
760
|
+
return ++id;
|
|
761
|
+
};
|
|
762
|
+
|
|
763
|
+
const registerPromise = map => {
|
|
764
|
+
const id = create$4();
|
|
765
|
+
const {
|
|
766
|
+
promise,
|
|
767
|
+
resolve
|
|
768
|
+
} = Promise.withResolvers();
|
|
769
|
+
map[id] = resolve;
|
|
770
|
+
return {
|
|
771
|
+
id,
|
|
772
|
+
promise
|
|
773
|
+
};
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
|
|
777
|
+
const {
|
|
778
|
+
id,
|
|
779
|
+
promise
|
|
780
|
+
} = registerPromise(callbacks);
|
|
781
|
+
const message = create$5(id, method, params);
|
|
782
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
783
|
+
ipc.sendAndTransfer(message);
|
|
784
|
+
} else {
|
|
785
|
+
ipc.send(message);
|
|
786
|
+
}
|
|
787
|
+
const responseMessage = await promise;
|
|
788
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
789
|
+
};
|
|
790
|
+
const createRpc = ipc => {
|
|
791
|
+
const callbacks = Object.create(null);
|
|
792
|
+
ipc._resolve = (id, response) => {
|
|
793
|
+
const fn = callbacks[id];
|
|
794
|
+
if (!fn) {
|
|
795
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
fn(response);
|
|
799
|
+
delete callbacks[id];
|
|
800
|
+
};
|
|
801
|
+
const rpc = {
|
|
802
|
+
async dispose() {
|
|
803
|
+
await ipc?.dispose();
|
|
804
|
+
},
|
|
805
|
+
invoke(method, ...params) {
|
|
806
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
807
|
+
},
|
|
808
|
+
invokeAndTransfer(method, ...params) {
|
|
809
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
810
|
+
},
|
|
811
|
+
// @ts-ignore
|
|
812
|
+
ipc,
|
|
813
|
+
/**
|
|
814
|
+
* @deprecated
|
|
815
|
+
*/
|
|
816
|
+
send(method, ...params) {
|
|
817
|
+
const message = create$6(method, params);
|
|
818
|
+
ipc.send(message);
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
return rpc;
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
const requiresSocket = () => {
|
|
825
|
+
return false;
|
|
826
|
+
};
|
|
827
|
+
const preparePrettyError = error => {
|
|
828
|
+
return error;
|
|
829
|
+
};
|
|
830
|
+
const logError = () => {
|
|
831
|
+
// handled by renderer worker
|
|
832
|
+
};
|
|
833
|
+
const handleMessage = event => {
|
|
834
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
835
|
+
const actualExecute = event?.target?.execute || execute;
|
|
836
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
const handleIpc = ipc => {
|
|
840
|
+
if ('addEventListener' in ipc) {
|
|
841
|
+
ipc.addEventListener('message', handleMessage);
|
|
842
|
+
} else if ('on' in ipc) {
|
|
843
|
+
// deprecated
|
|
844
|
+
ipc.on('message', handleMessage);
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
const listen$1 = async (module, options) => {
|
|
849
|
+
const rawIpc = await module.listen(options);
|
|
850
|
+
if (module.signal) {
|
|
851
|
+
module.signal(rawIpc);
|
|
852
|
+
}
|
|
853
|
+
const ipc = module.wrap(rawIpc);
|
|
854
|
+
return ipc;
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
const create$3 = async ({
|
|
858
|
+
commandMap
|
|
859
|
+
}) => {
|
|
860
|
+
// TODO create a commandMap per rpc instance
|
|
861
|
+
register(commandMap);
|
|
862
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
863
|
+
handleIpc(ipc);
|
|
864
|
+
const rpc = createRpc(ipc);
|
|
865
|
+
return rpc;
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
const createMockRpc = ({
|
|
869
|
+
commandMap
|
|
870
|
+
}) => {
|
|
871
|
+
const invocations = [];
|
|
872
|
+
const invoke = (method, ...params) => {
|
|
873
|
+
invocations.push([method, ...params]);
|
|
874
|
+
const command = commandMap[method];
|
|
875
|
+
if (!command) {
|
|
876
|
+
throw new Error(`command ${method} not found`);
|
|
877
|
+
}
|
|
878
|
+
return command(...params);
|
|
879
|
+
};
|
|
880
|
+
const mockRpc = {
|
|
881
|
+
invocations,
|
|
882
|
+
invoke,
|
|
883
|
+
invokeAndTransfer: invoke
|
|
884
|
+
};
|
|
885
|
+
return mockRpc;
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
const rpcs = Object.create(null);
|
|
889
|
+
const set$2 = (id, rpc) => {
|
|
890
|
+
rpcs[id] = rpc;
|
|
891
|
+
};
|
|
892
|
+
const get$1 = id => {
|
|
893
|
+
return rpcs[id];
|
|
894
|
+
};
|
|
895
|
+
const remove = id => {
|
|
896
|
+
delete rpcs[id];
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
900
|
+
const create$2 = rpcId => {
|
|
901
|
+
return {
|
|
902
|
+
async dispose() {
|
|
903
|
+
const rpc = get$1(rpcId);
|
|
904
|
+
await rpc.dispose();
|
|
905
|
+
},
|
|
906
|
+
// @ts-ignore
|
|
907
|
+
invoke(method, ...params) {
|
|
908
|
+
const rpc = get$1(rpcId);
|
|
909
|
+
// @ts-ignore
|
|
910
|
+
return rpc.invoke(method, ...params);
|
|
911
|
+
},
|
|
912
|
+
// @ts-ignore
|
|
913
|
+
invokeAndTransfer(method, ...params) {
|
|
914
|
+
const rpc = get$1(rpcId);
|
|
915
|
+
// @ts-ignore
|
|
916
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
917
|
+
},
|
|
918
|
+
registerMockRpc(commandMap) {
|
|
919
|
+
const mockRpc = createMockRpc({
|
|
920
|
+
commandMap
|
|
921
|
+
});
|
|
922
|
+
set$2(rpcId, mockRpc);
|
|
923
|
+
// @ts-ignore
|
|
924
|
+
mockRpc[Symbol.dispose] = () => {
|
|
925
|
+
remove(rpcId);
|
|
926
|
+
};
|
|
927
|
+
// @ts-ignore
|
|
928
|
+
return mockRpc;
|
|
929
|
+
},
|
|
930
|
+
set(rpc) {
|
|
931
|
+
set$2(rpcId, rpc);
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
const None = 'none';
|
|
937
|
+
|
|
938
|
+
const Audio = 0;
|
|
939
|
+
const Button = 1;
|
|
940
|
+
const Col = 2;
|
|
941
|
+
const ColGroup = 3;
|
|
942
|
+
const Div = 4;
|
|
943
|
+
const H1 = 5;
|
|
944
|
+
const Input = 6;
|
|
945
|
+
const Kbd = 7;
|
|
946
|
+
const Span = 8;
|
|
947
|
+
const Table = 9;
|
|
948
|
+
const TBody = 10;
|
|
949
|
+
const Td = 11;
|
|
950
|
+
const Text = 12;
|
|
951
|
+
const Th = 13;
|
|
952
|
+
const THead = 14;
|
|
953
|
+
const Tr = 15;
|
|
954
|
+
const I = 16;
|
|
955
|
+
const Img = 17;
|
|
956
|
+
const Root = 0;
|
|
957
|
+
const Ins = 20;
|
|
958
|
+
const Del = 21;
|
|
959
|
+
const H2 = 22;
|
|
960
|
+
const H3 = 23;
|
|
961
|
+
const H4 = 24;
|
|
962
|
+
const H5 = 25;
|
|
963
|
+
const H6 = 26;
|
|
964
|
+
const Article = 27;
|
|
965
|
+
const Aside = 28;
|
|
966
|
+
const Footer = 29;
|
|
967
|
+
const Header = 30;
|
|
968
|
+
const Nav = 40;
|
|
969
|
+
const Section = 41;
|
|
970
|
+
const Search = 42;
|
|
971
|
+
const Dd = 43;
|
|
972
|
+
const Dl = 44;
|
|
973
|
+
const Figcaption = 45;
|
|
974
|
+
const Figure = 46;
|
|
975
|
+
const Hr = 47;
|
|
976
|
+
const Li = 48;
|
|
977
|
+
const Ol = 49;
|
|
978
|
+
const P = 50;
|
|
979
|
+
const Pre = 51;
|
|
980
|
+
const A = 53;
|
|
981
|
+
const Abbr = 54;
|
|
982
|
+
const Br = 55;
|
|
983
|
+
const Cite = 56;
|
|
984
|
+
const Data = 57;
|
|
985
|
+
const Time = 58;
|
|
986
|
+
const Tfoot = 59;
|
|
987
|
+
const Ul = 60;
|
|
988
|
+
const Video = 61;
|
|
989
|
+
const TextArea = 62;
|
|
990
|
+
const Select = 63;
|
|
991
|
+
const Option = 64;
|
|
992
|
+
const Code = 65;
|
|
993
|
+
const Label = 66;
|
|
994
|
+
const Dt = 67;
|
|
995
|
+
const Iframe = 68;
|
|
996
|
+
const Main = 69;
|
|
997
|
+
const Strong = 70;
|
|
998
|
+
const Em = 71;
|
|
999
|
+
const Style = 72;
|
|
1000
|
+
const Html = 73;
|
|
1001
|
+
const Head = 74;
|
|
1002
|
+
const Title = 75;
|
|
1003
|
+
const Meta = 76;
|
|
1004
|
+
const Canvas = 77;
|
|
1005
|
+
const Form = 78;
|
|
1006
|
+
const BlockQuote = 79;
|
|
1007
|
+
const Quote = 80;
|
|
1008
|
+
const Circle = 81;
|
|
1009
|
+
const Defs = 82;
|
|
1010
|
+
const Ellipse = 83;
|
|
1011
|
+
const G = 84;
|
|
1012
|
+
const Line = 85;
|
|
1013
|
+
const Path = 86;
|
|
1014
|
+
const Polygon = 87;
|
|
1015
|
+
const Polyline = 88;
|
|
1016
|
+
const Rect = 89;
|
|
1017
|
+
const Svg = 90;
|
|
1018
|
+
const Use = 91;
|
|
1019
|
+
const Reference = 100;
|
|
1020
|
+
|
|
1021
|
+
const VirtualDomElements = {
|
|
1022
|
+
__proto__: null,
|
|
1023
|
+
A,
|
|
1024
|
+
Abbr,
|
|
1025
|
+
Article,
|
|
1026
|
+
Aside,
|
|
1027
|
+
Audio,
|
|
1028
|
+
BlockQuote,
|
|
1029
|
+
Br,
|
|
1030
|
+
Button,
|
|
1031
|
+
Canvas,
|
|
1032
|
+
Circle,
|
|
1033
|
+
Cite,
|
|
1034
|
+
Code,
|
|
1035
|
+
Col,
|
|
1036
|
+
ColGroup,
|
|
1037
|
+
Data,
|
|
1038
|
+
Dd,
|
|
1039
|
+
Defs,
|
|
1040
|
+
Del,
|
|
1041
|
+
Div,
|
|
1042
|
+
Dl,
|
|
1043
|
+
Dt,
|
|
1044
|
+
Ellipse,
|
|
1045
|
+
Em,
|
|
1046
|
+
Figcaption,
|
|
1047
|
+
Figure,
|
|
1048
|
+
Footer,
|
|
1049
|
+
Form,
|
|
1050
|
+
G,
|
|
1051
|
+
H1,
|
|
1052
|
+
H2,
|
|
1053
|
+
H3,
|
|
1054
|
+
H4,
|
|
1055
|
+
H5,
|
|
1056
|
+
H6,
|
|
1057
|
+
Head,
|
|
1058
|
+
Header,
|
|
1059
|
+
Hr,
|
|
1060
|
+
Html,
|
|
1061
|
+
I,
|
|
1062
|
+
Iframe,
|
|
1063
|
+
Img,
|
|
1064
|
+
Input,
|
|
1065
|
+
Ins,
|
|
1066
|
+
Kbd,
|
|
1067
|
+
Label,
|
|
1068
|
+
Li,
|
|
1069
|
+
Line,
|
|
1070
|
+
Main,
|
|
1071
|
+
Meta,
|
|
1072
|
+
Nav,
|
|
1073
|
+
Ol,
|
|
1074
|
+
Option,
|
|
1075
|
+
P,
|
|
1076
|
+
Path,
|
|
1077
|
+
Polygon,
|
|
1078
|
+
Polyline,
|
|
1079
|
+
Pre,
|
|
1080
|
+
Quote,
|
|
1081
|
+
Rect,
|
|
1082
|
+
Reference,
|
|
1083
|
+
Root,
|
|
1084
|
+
Search,
|
|
1085
|
+
Section,
|
|
1086
|
+
Select,
|
|
1087
|
+
Span,
|
|
1088
|
+
Strong,
|
|
1089
|
+
Style,
|
|
1090
|
+
Svg,
|
|
1091
|
+
TBody,
|
|
1092
|
+
THead,
|
|
1093
|
+
Table,
|
|
1094
|
+
Td,
|
|
1095
|
+
Text,
|
|
1096
|
+
TextArea,
|
|
1097
|
+
Tfoot,
|
|
1098
|
+
Th,
|
|
1099
|
+
Time,
|
|
1100
|
+
Title,
|
|
1101
|
+
Tr,
|
|
1102
|
+
Ul,
|
|
1103
|
+
Use,
|
|
1104
|
+
Video
|
|
1105
|
+
};
|
|
1106
|
+
|
|
1107
|
+
const RendererWorker = 1;
|
|
1108
|
+
|
|
1109
|
+
const SetCss = 'Viewlet.setCss';
|
|
1110
|
+
const SetDom2 = 'Viewlet.setDom2';
|
|
1111
|
+
|
|
1112
|
+
const {
|
|
1113
|
+
invoke,
|
|
1114
|
+
set: set$1
|
|
1115
|
+
} = create$2(RendererWorker);
|
|
1116
|
+
|
|
1117
|
+
const toCommandId = key => {
|
|
1118
|
+
const dotIndex = key.indexOf('.');
|
|
1119
|
+
return key.slice(dotIndex + 1);
|
|
1120
|
+
};
|
|
1121
|
+
const create$1 = () => {
|
|
1122
|
+
const states = Object.create(null);
|
|
1123
|
+
const commandMapRef = {};
|
|
1124
|
+
return {
|
|
1125
|
+
clear() {
|
|
1126
|
+
for (const key of Object.keys(states)) {
|
|
1127
|
+
delete states[key];
|
|
1128
|
+
}
|
|
1129
|
+
},
|
|
1130
|
+
diff(uid, modules, numbers) {
|
|
1131
|
+
const {
|
|
1132
|
+
oldState,
|
|
1133
|
+
scheduledState
|
|
1134
|
+
} = states[uid];
|
|
1135
|
+
const diffResult = [];
|
|
1136
|
+
for (let i = 0; i < modules.length; i++) {
|
|
1137
|
+
const fn = modules[i];
|
|
1138
|
+
if (!fn(oldState, scheduledState)) {
|
|
1139
|
+
diffResult.push(numbers[i]);
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
return diffResult;
|
|
1143
|
+
},
|
|
1144
|
+
dispose(uid) {
|
|
1145
|
+
delete states[uid];
|
|
1146
|
+
},
|
|
1147
|
+
get(uid) {
|
|
1148
|
+
return states[uid];
|
|
1149
|
+
},
|
|
1150
|
+
getCommandIds() {
|
|
1151
|
+
const keys = Object.keys(commandMapRef);
|
|
1152
|
+
const ids = keys.map(toCommandId);
|
|
1153
|
+
return ids;
|
|
1154
|
+
},
|
|
1155
|
+
getKeys() {
|
|
1156
|
+
return Object.keys(states).map(Number);
|
|
1157
|
+
},
|
|
1158
|
+
registerCommands(commandMap) {
|
|
1159
|
+
Object.assign(commandMapRef, commandMap);
|
|
1160
|
+
},
|
|
1161
|
+
set(uid, oldState, newState, scheduledState) {
|
|
1162
|
+
states[uid] = {
|
|
1163
|
+
newState,
|
|
1164
|
+
oldState,
|
|
1165
|
+
scheduledState: scheduledState ?? newState
|
|
1166
|
+
};
|
|
1167
|
+
},
|
|
1168
|
+
wrapCommand(fn) {
|
|
1169
|
+
const wrapped = async (uid, ...args) => {
|
|
1170
|
+
const {
|
|
1171
|
+
newState,
|
|
1172
|
+
oldState
|
|
1173
|
+
} = states[uid];
|
|
1174
|
+
const newerState = await fn(newState, ...args);
|
|
1175
|
+
if (oldState === newerState || newState === newerState) {
|
|
1176
|
+
return;
|
|
1177
|
+
}
|
|
1178
|
+
const latestOld = states[uid];
|
|
1179
|
+
const latestNew = {
|
|
1180
|
+
...latestOld.newState,
|
|
1181
|
+
...newerState
|
|
1182
|
+
};
|
|
1183
|
+
states[uid] = {
|
|
1184
|
+
newState: latestNew,
|
|
1185
|
+
oldState: latestOld.oldState,
|
|
1186
|
+
scheduledState: latestNew
|
|
1187
|
+
};
|
|
1188
|
+
};
|
|
1189
|
+
return wrapped;
|
|
1190
|
+
},
|
|
1191
|
+
wrapGetter(fn) {
|
|
1192
|
+
const wrapped = (uid, ...args) => {
|
|
1193
|
+
const {
|
|
1194
|
+
newState
|
|
1195
|
+
} = states[uid];
|
|
1196
|
+
return fn(newState, ...args);
|
|
1197
|
+
};
|
|
1198
|
+
return wrapped;
|
|
1199
|
+
},
|
|
1200
|
+
wrapLoadContent(fn) {
|
|
1201
|
+
const wrapped = async (uid, ...args) => {
|
|
1202
|
+
const {
|
|
1203
|
+
newState,
|
|
1204
|
+
oldState
|
|
1205
|
+
} = states[uid];
|
|
1206
|
+
const result = await fn(newState, ...args);
|
|
1207
|
+
const {
|
|
1208
|
+
error,
|
|
1209
|
+
state
|
|
1210
|
+
} = result;
|
|
1211
|
+
if (oldState === state || newState === state) {
|
|
1212
|
+
return {
|
|
1213
|
+
error
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
const latestOld = states[uid];
|
|
1217
|
+
const latestNew = {
|
|
1218
|
+
...latestOld.newState,
|
|
1219
|
+
...state
|
|
1220
|
+
};
|
|
1221
|
+
states[uid] = {
|
|
1222
|
+
newState: latestNew,
|
|
1223
|
+
oldState: latestOld.oldState,
|
|
1224
|
+
scheduledState: latestNew
|
|
1225
|
+
};
|
|
1226
|
+
return {
|
|
1227
|
+
error
|
|
1228
|
+
};
|
|
1229
|
+
};
|
|
1230
|
+
return wrapped;
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
};
|
|
1234
|
+
|
|
1235
|
+
const {
|
|
1236
|
+
diff,
|
|
1237
|
+
dispose,
|
|
1238
|
+
get,
|
|
1239
|
+
getCommandIds,
|
|
1240
|
+
registerCommands,
|
|
1241
|
+
set,
|
|
1242
|
+
wrapCommand
|
|
1243
|
+
} = create$1();
|
|
1244
|
+
|
|
1245
|
+
const create = (uid, uri, x, y, width, height) => {
|
|
1246
|
+
const state = {
|
|
1247
|
+
extensions: [],
|
|
1248
|
+
height,
|
|
1249
|
+
loaded: false,
|
|
1250
|
+
uid,
|
|
1251
|
+
width,
|
|
1252
|
+
x,
|
|
1253
|
+
y
|
|
1254
|
+
};
|
|
1255
|
+
set(uid, state, state);
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
const isDomEqual = (oldState, newState) => {
|
|
1259
|
+
return oldState.extensions === newState.extensions && oldState.loaded === newState.loaded;
|
|
1260
|
+
};
|
|
1261
|
+
const isCssEqual = (oldState, newState) => {
|
|
1262
|
+
return oldState.width === newState.width && oldState.height === newState.height;
|
|
1263
|
+
};
|
|
1264
|
+
|
|
1265
|
+
const RenderDom = 1;
|
|
1266
|
+
const RenderCss = 2;
|
|
1267
|
+
|
|
1268
|
+
const diff2 = uid => {
|
|
1269
|
+
return diff(uid, [isDomEqual, isCssEqual], [RenderDom, RenderCss]);
|
|
1270
|
+
};
|
|
1271
|
+
|
|
1272
|
+
const getRunningExtensions = async () => {
|
|
1273
|
+
return invoke('ExtensionManagement.getRunningExtensions');
|
|
1274
|
+
};
|
|
1275
|
+
|
|
1276
|
+
const loadContent = async state => {
|
|
1277
|
+
const extensions = await getRunningExtensions();
|
|
1278
|
+
return {
|
|
1279
|
+
...state,
|
|
1280
|
+
extensions,
|
|
1281
|
+
loaded: true
|
|
1282
|
+
};
|
|
1283
|
+
};
|
|
1284
|
+
|
|
1285
|
+
const renderCss = (oldState, newState) => {
|
|
1286
|
+
return [SetCss, newState.uid, `width: ${newState.width}px; height: ${newState.height}px;`];
|
|
1287
|
+
};
|
|
1288
|
+
|
|
1289
|
+
const text = data => {
|
|
1290
|
+
return {
|
|
1291
|
+
childCount: 0,
|
|
1292
|
+
text: data,
|
|
1293
|
+
type: Text
|
|
1294
|
+
};
|
|
1295
|
+
};
|
|
1296
|
+
|
|
1297
|
+
new Set(Object.values(VirtualDomElements));
|
|
1298
|
+
|
|
1299
|
+
const getIconVirtualDom = extension => {
|
|
1300
|
+
if (extension.icon) {
|
|
1301
|
+
return [{
|
|
1302
|
+
childCount: 0,
|
|
1303
|
+
className: 'RunningExtensionIcon',
|
|
1304
|
+
src: extension.icon,
|
|
1305
|
+
type: Img
|
|
1306
|
+
}];
|
|
1307
|
+
}
|
|
1308
|
+
return [{
|
|
1309
|
+
childCount: 0,
|
|
1310
|
+
className: 'RunningExtensionIcon RunningExtensionDefaultIcon MaskIcon MaskIconExtensions',
|
|
1311
|
+
role: None,
|
|
1312
|
+
type: Div
|
|
1313
|
+
}];
|
|
1314
|
+
};
|
|
1315
|
+
const getExtensionVirtualDom = extension => {
|
|
1316
|
+
const displayName = extension.name || extension.id;
|
|
1317
|
+
return [{
|
|
1318
|
+
childCount: 3,
|
|
1319
|
+
className: 'RunningExtension',
|
|
1320
|
+
role: 'listitem',
|
|
1321
|
+
type: Div
|
|
1322
|
+
}, ...getIconVirtualDom(extension), {
|
|
1323
|
+
childCount: 2,
|
|
1324
|
+
className: 'RunningExtensionDetails',
|
|
1325
|
+
type: Div
|
|
1326
|
+
}, {
|
|
1327
|
+
childCount: 2,
|
|
1328
|
+
className: 'RunningExtensionTitle',
|
|
1329
|
+
type: Div
|
|
1330
|
+
}, {
|
|
1331
|
+
childCount: 1,
|
|
1332
|
+
className: 'RunningExtensionName',
|
|
1333
|
+
type: Strong
|
|
1334
|
+
}, text(displayName), {
|
|
1335
|
+
childCount: 1,
|
|
1336
|
+
className: 'RunningExtensionVersion',
|
|
1337
|
+
type: Span
|
|
1338
|
+
}, text(extension.version), {
|
|
1339
|
+
childCount: 1,
|
|
1340
|
+
className: 'RunningExtensionId',
|
|
1341
|
+
type: Div
|
|
1342
|
+
}, text(extension.id), {
|
|
1343
|
+
childCount: 1,
|
|
1344
|
+
className: 'RunningExtensionActivationTime',
|
|
1345
|
+
type: Div
|
|
1346
|
+
}, text(`Activation: ${Math.round(extension.activationTime)}ms`)];
|
|
1347
|
+
};
|
|
1348
|
+
const getRunningExtensionsVirtualDom = (extensions, loaded) => {
|
|
1349
|
+
const message = loaded ? 'No running extensions' : 'Loading running extensions…';
|
|
1350
|
+
const children = extensions.length === 0 ? [{
|
|
1351
|
+
childCount: 1,
|
|
1352
|
+
className: 'RunningExtensionsEmpty',
|
|
1353
|
+
type: Div
|
|
1354
|
+
}, text(message)] : extensions.flatMap(getExtensionVirtualDom);
|
|
1355
|
+
return [{
|
|
1356
|
+
childCount: extensions.length === 0 ? 1 : extensions.length,
|
|
1357
|
+
className: 'RunningExtensions',
|
|
1358
|
+
role: 'list',
|
|
1359
|
+
type: Div
|
|
1360
|
+
}, ...children];
|
|
1361
|
+
};
|
|
1362
|
+
|
|
1363
|
+
const renderDom = (oldState, newState) => {
|
|
1364
|
+
return [SetDom2, newState.uid, getRunningExtensionsVirtualDom(newState.extensions, newState.loaded)];
|
|
1365
|
+
};
|
|
1366
|
+
|
|
1367
|
+
const getRenderer = diffType => {
|
|
1368
|
+
switch (diffType) {
|
|
1369
|
+
case RenderCss:
|
|
1370
|
+
return renderCss;
|
|
1371
|
+
case RenderDom:
|
|
1372
|
+
return renderDom;
|
|
1373
|
+
default:
|
|
1374
|
+
throw new Error(`Unknown diff type ${diffType}`);
|
|
1375
|
+
}
|
|
1376
|
+
};
|
|
1377
|
+
const render2 = (uid, diffResult) => {
|
|
1378
|
+
const {
|
|
1379
|
+
newState,
|
|
1380
|
+
oldState
|
|
1381
|
+
} = get(uid);
|
|
1382
|
+
set(uid, newState, newState);
|
|
1383
|
+
return diffResult.map(diffType => getRenderer(diffType)(oldState, newState));
|
|
1384
|
+
};
|
|
1385
|
+
|
|
1386
|
+
const renderEventListeners = () => {
|
|
1387
|
+
return [];
|
|
1388
|
+
};
|
|
1389
|
+
|
|
1390
|
+
const resize = (state, dimensions) => {
|
|
1391
|
+
return {
|
|
1392
|
+
...state,
|
|
1393
|
+
...dimensions
|
|
1394
|
+
};
|
|
1395
|
+
};
|
|
1396
|
+
|
|
1397
|
+
const commandMap = {
|
|
1398
|
+
'RunningExtensions.create': create,
|
|
1399
|
+
'RunningExtensions.diff2': diff2,
|
|
1400
|
+
'RunningExtensions.dispose': dispose,
|
|
1401
|
+
'RunningExtensions.getCommandIds': getCommandIds,
|
|
1402
|
+
'RunningExtensions.loadContent': wrapCommand(loadContent),
|
|
1403
|
+
'RunningExtensions.render2': render2,
|
|
1404
|
+
'RunningExtensions.renderEventListeners': renderEventListeners,
|
|
1405
|
+
'RunningExtensions.resize': wrapCommand(resize)
|
|
1406
|
+
};
|
|
1407
|
+
|
|
1408
|
+
const listen = async () => {
|
|
1409
|
+
registerCommands(commandMap);
|
|
1410
|
+
const rpc = await create$3({
|
|
1411
|
+
commandMap: commandMap
|
|
1412
|
+
});
|
|
1413
|
+
set$1(rpc);
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
const main = async () => {
|
|
1417
|
+
await listen();
|
|
1418
|
+
};
|
|
1419
|
+
|
|
1420
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lvce-editor/running-extensions-view",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Running Extensions View Worker",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/lvce-editor/running-extensions-view.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Lvce Editor",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/runningExtensionsViewMain.js"
|
|
13
|
+
}
|