@lvce-editor/preview-worker 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/previewWorkerMain.js +1403 -0
- package/package.json +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 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,1403 @@
|
|
|
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
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const getTransferrables = value => {
|
|
103
|
+
const transferrables = [];
|
|
104
|
+
walkValue(value, transferrables, isTransferrable);
|
|
105
|
+
return transferrables;
|
|
106
|
+
};
|
|
107
|
+
const attachEvents = that => {
|
|
108
|
+
const handleMessage = (...args) => {
|
|
109
|
+
const data = that.getData(...args);
|
|
110
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
111
|
+
data
|
|
112
|
+
}));
|
|
113
|
+
};
|
|
114
|
+
that.onMessage(handleMessage);
|
|
115
|
+
const handleClose = event => {
|
|
116
|
+
that.dispatchEvent(new Event('close'));
|
|
117
|
+
};
|
|
118
|
+
that.onClose(handleClose);
|
|
119
|
+
};
|
|
120
|
+
class Ipc extends EventTarget {
|
|
121
|
+
constructor(rawIpc) {
|
|
122
|
+
super();
|
|
123
|
+
this._rawIpc = rawIpc;
|
|
124
|
+
attachEvents(this);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
128
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
129
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
130
|
+
const NewLine$1 = '\n';
|
|
131
|
+
const joinLines$1 = lines => {
|
|
132
|
+
return lines.join(NewLine$1);
|
|
133
|
+
};
|
|
134
|
+
const RE_AT = /^\s+at/;
|
|
135
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
136
|
+
const isNormalStackLine = line => {
|
|
137
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
138
|
+
};
|
|
139
|
+
const getDetails = lines => {
|
|
140
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
141
|
+
if (index === -1) {
|
|
142
|
+
return {
|
|
143
|
+
actualMessage: joinLines$1(lines),
|
|
144
|
+
rest: []
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
let lastIndex = index - 1;
|
|
148
|
+
while (++lastIndex < lines.length) {
|
|
149
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
actualMessage: lines[index - 1],
|
|
155
|
+
rest: lines.slice(index, lastIndex)
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
const splitLines$1 = lines => {
|
|
159
|
+
return lines.split(NewLine$1);
|
|
160
|
+
};
|
|
161
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
162
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
163
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
164
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
165
|
+
};
|
|
166
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
167
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
168
|
+
};
|
|
169
|
+
const getMessageCodeBlock = stderr => {
|
|
170
|
+
const lines = splitLines$1(stderr);
|
|
171
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
172
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
173
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
174
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
175
|
+
return relevantMessage;
|
|
176
|
+
};
|
|
177
|
+
const isModuleNotFoundMessage = line => {
|
|
178
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
179
|
+
};
|
|
180
|
+
const getModuleNotFoundError = stderr => {
|
|
181
|
+
const lines = splitLines$1(stderr);
|
|
182
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
183
|
+
const message = lines[messageIndex];
|
|
184
|
+
return {
|
|
185
|
+
code: ERR_MODULE_NOT_FOUND,
|
|
186
|
+
message
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
const isModuleNotFoundError = stderr => {
|
|
190
|
+
if (!stderr) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
194
|
+
};
|
|
195
|
+
const isModulesSyntaxError = stderr => {
|
|
196
|
+
if (!stderr) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
200
|
+
};
|
|
201
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
202
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
203
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
204
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
205
|
+
};
|
|
206
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
207
|
+
const message = getMessageCodeBlock(stderr);
|
|
208
|
+
return {
|
|
209
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE,
|
|
210
|
+
message: `Incompatible native node module: ${message}`
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
const getModuleSyntaxError = () => {
|
|
214
|
+
return {
|
|
215
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
|
|
216
|
+
message: `ES Modules are not supported in electron`
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
220
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
221
|
+
return getNativeModuleErrorMessage(stderr);
|
|
222
|
+
}
|
|
223
|
+
if (isModulesSyntaxError(stderr)) {
|
|
224
|
+
return getModuleSyntaxError();
|
|
225
|
+
}
|
|
226
|
+
if (isModuleNotFoundError(stderr)) {
|
|
227
|
+
return getModuleNotFoundError(stderr);
|
|
228
|
+
}
|
|
229
|
+
const lines = splitLines$1(stderr);
|
|
230
|
+
const {
|
|
231
|
+
actualMessage,
|
|
232
|
+
rest
|
|
233
|
+
} = getDetails(lines);
|
|
234
|
+
return {
|
|
235
|
+
code: '',
|
|
236
|
+
message: actualMessage,
|
|
237
|
+
stack: rest
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
class IpcError extends VError {
|
|
241
|
+
// @ts-ignore
|
|
242
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
243
|
+
if (stdout || stderr) {
|
|
244
|
+
// @ts-ignore
|
|
245
|
+
const {
|
|
246
|
+
code,
|
|
247
|
+
message,
|
|
248
|
+
stack
|
|
249
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
250
|
+
const cause = new Error(message);
|
|
251
|
+
// @ts-ignore
|
|
252
|
+
cause.code = code;
|
|
253
|
+
cause.stack = stack;
|
|
254
|
+
super(cause, betterMessage);
|
|
255
|
+
} else {
|
|
256
|
+
super(betterMessage);
|
|
257
|
+
}
|
|
258
|
+
// @ts-ignore
|
|
259
|
+
this.name = 'IpcError';
|
|
260
|
+
// @ts-ignore
|
|
261
|
+
this.stdout = stdout;
|
|
262
|
+
// @ts-ignore
|
|
263
|
+
this.stderr = stderr;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const readyMessage = 'ready';
|
|
267
|
+
const getData$2 = event => {
|
|
268
|
+
return event.data;
|
|
269
|
+
};
|
|
270
|
+
const listen$7 = () => {
|
|
271
|
+
// @ts-ignore
|
|
272
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
273
|
+
throw new TypeError('module is not in web worker scope');
|
|
274
|
+
}
|
|
275
|
+
return globalThis;
|
|
276
|
+
};
|
|
277
|
+
const signal$8 = global => {
|
|
278
|
+
global.postMessage(readyMessage);
|
|
279
|
+
};
|
|
280
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
281
|
+
getData(event) {
|
|
282
|
+
return getData$2(event);
|
|
283
|
+
}
|
|
284
|
+
send(message) {
|
|
285
|
+
// @ts-ignore
|
|
286
|
+
this._rawIpc.postMessage(message);
|
|
287
|
+
}
|
|
288
|
+
sendAndTransfer(message) {
|
|
289
|
+
const transfer = getTransferrables(message);
|
|
290
|
+
// @ts-ignore
|
|
291
|
+
this._rawIpc.postMessage(message, transfer);
|
|
292
|
+
}
|
|
293
|
+
dispose() {
|
|
294
|
+
// ignore
|
|
295
|
+
}
|
|
296
|
+
onClose(callback) {
|
|
297
|
+
// ignore
|
|
298
|
+
}
|
|
299
|
+
onMessage(callback) {
|
|
300
|
+
this._rawIpc.addEventListener('message', callback);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const wrap$f = global => {
|
|
304
|
+
return new IpcChildWithModuleWorker(global);
|
|
305
|
+
};
|
|
306
|
+
const waitForFirstMessage = async port => {
|
|
307
|
+
const {
|
|
308
|
+
promise,
|
|
309
|
+
resolve
|
|
310
|
+
} = Promise.withResolvers();
|
|
311
|
+
port.addEventListener('message', resolve, {
|
|
312
|
+
once: true
|
|
313
|
+
});
|
|
314
|
+
const event = await promise;
|
|
315
|
+
// @ts-ignore
|
|
316
|
+
return event.data;
|
|
317
|
+
};
|
|
318
|
+
const listen$6 = async () => {
|
|
319
|
+
const parentIpcRaw = listen$7();
|
|
320
|
+
signal$8(parentIpcRaw);
|
|
321
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
322
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
323
|
+
if (firstMessage.method !== 'initialize') {
|
|
324
|
+
throw new IpcError('unexpected first message');
|
|
325
|
+
}
|
|
326
|
+
const type = firstMessage.params[0];
|
|
327
|
+
if (type === 'message-port') {
|
|
328
|
+
parentIpc.send({
|
|
329
|
+
id: firstMessage.id,
|
|
330
|
+
jsonrpc: '2.0',
|
|
331
|
+
result: null
|
|
332
|
+
});
|
|
333
|
+
parentIpc.dispose();
|
|
334
|
+
const port = firstMessage.params[1];
|
|
335
|
+
return port;
|
|
336
|
+
}
|
|
337
|
+
return globalThis;
|
|
338
|
+
};
|
|
339
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
340
|
+
getData(event) {
|
|
341
|
+
return getData$2(event);
|
|
342
|
+
}
|
|
343
|
+
send(message) {
|
|
344
|
+
this._rawIpc.postMessage(message);
|
|
345
|
+
}
|
|
346
|
+
sendAndTransfer(message) {
|
|
347
|
+
const transfer = getTransferrables(message);
|
|
348
|
+
this._rawIpc.postMessage(message, transfer);
|
|
349
|
+
}
|
|
350
|
+
dispose() {
|
|
351
|
+
if (this._rawIpc.close) {
|
|
352
|
+
this._rawIpc.close();
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
onClose(callback) {
|
|
356
|
+
// ignore
|
|
357
|
+
}
|
|
358
|
+
onMessage(callback) {
|
|
359
|
+
this._rawIpc.addEventListener('message', callback);
|
|
360
|
+
this._rawIpc.start();
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
const wrap$e = port => {
|
|
364
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
365
|
+
};
|
|
366
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
367
|
+
__proto__: null,
|
|
368
|
+
listen: listen$6,
|
|
369
|
+
wrap: wrap$e
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const Two$1 = '2.0';
|
|
373
|
+
const callbacks = Object.create(null);
|
|
374
|
+
const get$2 = id => {
|
|
375
|
+
return callbacks[id];
|
|
376
|
+
};
|
|
377
|
+
const remove$1 = id => {
|
|
378
|
+
delete callbacks[id];
|
|
379
|
+
};
|
|
380
|
+
class JsonRpcError extends Error {
|
|
381
|
+
constructor(message) {
|
|
382
|
+
super(message);
|
|
383
|
+
this.name = 'JsonRpcError';
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const NewLine = '\n';
|
|
387
|
+
const DomException = 'DOMException';
|
|
388
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
389
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
390
|
+
const TypeError$1 = 'TypeError';
|
|
391
|
+
const getErrorConstructor = (message, type) => {
|
|
392
|
+
if (type) {
|
|
393
|
+
switch (type) {
|
|
394
|
+
case DomException:
|
|
395
|
+
return DOMException;
|
|
396
|
+
case TypeError$1:
|
|
397
|
+
return TypeError;
|
|
398
|
+
case SyntaxError$1:
|
|
399
|
+
return SyntaxError;
|
|
400
|
+
case ReferenceError$1:
|
|
401
|
+
return ReferenceError;
|
|
402
|
+
default:
|
|
403
|
+
return Error;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (message.startsWith('TypeError: ')) {
|
|
407
|
+
return TypeError;
|
|
408
|
+
}
|
|
409
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
410
|
+
return SyntaxError;
|
|
411
|
+
}
|
|
412
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
413
|
+
return ReferenceError;
|
|
414
|
+
}
|
|
415
|
+
return Error;
|
|
416
|
+
};
|
|
417
|
+
const constructError = (message, type, name) => {
|
|
418
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
419
|
+
if (ErrorConstructor === DOMException && name) {
|
|
420
|
+
return new ErrorConstructor(message, name);
|
|
421
|
+
}
|
|
422
|
+
if (ErrorConstructor === Error) {
|
|
423
|
+
const error = new Error(message);
|
|
424
|
+
if (name && name !== 'VError') {
|
|
425
|
+
error.name = name;
|
|
426
|
+
}
|
|
427
|
+
return error;
|
|
428
|
+
}
|
|
429
|
+
return new ErrorConstructor(message);
|
|
430
|
+
};
|
|
431
|
+
const joinLines = lines => {
|
|
432
|
+
return lines.join(NewLine);
|
|
433
|
+
};
|
|
434
|
+
const splitLines = lines => {
|
|
435
|
+
return lines.split(NewLine);
|
|
436
|
+
};
|
|
437
|
+
const getCurrentStack = () => {
|
|
438
|
+
const stackLinesToSkip = 3;
|
|
439
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
440
|
+
return currentStack;
|
|
441
|
+
};
|
|
442
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
443
|
+
return string.indexOf(NewLine, startIndex);
|
|
444
|
+
};
|
|
445
|
+
const getParentStack = error => {
|
|
446
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
447
|
+
if (parentStack.startsWith(' at')) {
|
|
448
|
+
parentStack = error.message + NewLine + parentStack;
|
|
449
|
+
}
|
|
450
|
+
return parentStack;
|
|
451
|
+
};
|
|
452
|
+
const MethodNotFound = -32601;
|
|
453
|
+
const Custom = -32001;
|
|
454
|
+
const restoreJsonRpcError = error => {
|
|
455
|
+
const currentStack = getCurrentStack();
|
|
456
|
+
if (error && error instanceof Error) {
|
|
457
|
+
if (typeof error.stack === 'string') {
|
|
458
|
+
error.stack = error.stack + NewLine + currentStack;
|
|
459
|
+
}
|
|
460
|
+
return error;
|
|
461
|
+
}
|
|
462
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
463
|
+
const restoredError = new JsonRpcError(error.message);
|
|
464
|
+
const parentStack = getParentStack(error);
|
|
465
|
+
restoredError.stack = parentStack + NewLine + currentStack;
|
|
466
|
+
return restoredError;
|
|
467
|
+
}
|
|
468
|
+
if (error && error.message) {
|
|
469
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
470
|
+
if (error.data) {
|
|
471
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
472
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
473
|
+
} else if (error.data.stack) {
|
|
474
|
+
restoredError.stack = error.data.stack;
|
|
475
|
+
}
|
|
476
|
+
if (error.data.codeFrame) {
|
|
477
|
+
// @ts-ignore
|
|
478
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
479
|
+
}
|
|
480
|
+
if (error.data.code) {
|
|
481
|
+
// @ts-ignore
|
|
482
|
+
restoredError.code = error.data.code;
|
|
483
|
+
}
|
|
484
|
+
if (error.data.type) {
|
|
485
|
+
// @ts-ignore
|
|
486
|
+
restoredError.name = error.data.type;
|
|
487
|
+
}
|
|
488
|
+
} else {
|
|
489
|
+
if (error.stack) {
|
|
490
|
+
const lowerStack = restoredError.stack || '';
|
|
491
|
+
// @ts-ignore
|
|
492
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
493
|
+
const parentStack = getParentStack(error);
|
|
494
|
+
// @ts-ignore
|
|
495
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
496
|
+
}
|
|
497
|
+
if (error.codeFrame) {
|
|
498
|
+
// @ts-ignore
|
|
499
|
+
restoredError.codeFrame = error.codeFrame;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return restoredError;
|
|
503
|
+
}
|
|
504
|
+
if (typeof error === 'string') {
|
|
505
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
506
|
+
}
|
|
507
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
508
|
+
};
|
|
509
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
510
|
+
if ('error' in responseMessage) {
|
|
511
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
512
|
+
throw restoredError;
|
|
513
|
+
}
|
|
514
|
+
if ('result' in responseMessage) {
|
|
515
|
+
return responseMessage.result;
|
|
516
|
+
}
|
|
517
|
+
throw new JsonRpcError('unexpected response message');
|
|
518
|
+
};
|
|
519
|
+
const warn = (...args) => {
|
|
520
|
+
console.warn(...args);
|
|
521
|
+
};
|
|
522
|
+
const resolve = (id, response) => {
|
|
523
|
+
const fn = get$2(id);
|
|
524
|
+
if (!fn) {
|
|
525
|
+
console.log(response);
|
|
526
|
+
warn(`callback ${id} may already be disposed`);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
fn(response);
|
|
530
|
+
remove$1(id);
|
|
531
|
+
};
|
|
532
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
533
|
+
const getErrorType = prettyError => {
|
|
534
|
+
if (prettyError && prettyError.type) {
|
|
535
|
+
return prettyError.type;
|
|
536
|
+
}
|
|
537
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
538
|
+
return prettyError.constructor.name;
|
|
539
|
+
}
|
|
540
|
+
return undefined;
|
|
541
|
+
};
|
|
542
|
+
const isAlreadyStack = line => {
|
|
543
|
+
return line.trim().startsWith('at ');
|
|
544
|
+
};
|
|
545
|
+
const getStack = prettyError => {
|
|
546
|
+
const stackString = prettyError.stack || '';
|
|
547
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
548
|
+
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
549
|
+
return stackString.slice(newLineIndex + 1);
|
|
550
|
+
}
|
|
551
|
+
return stackString;
|
|
552
|
+
};
|
|
553
|
+
const getErrorProperty = (error, prettyError) => {
|
|
554
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
555
|
+
return {
|
|
556
|
+
code: MethodNotFound,
|
|
557
|
+
message: error.message,
|
|
558
|
+
data: error.stack
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
return {
|
|
562
|
+
code: Custom,
|
|
563
|
+
message: prettyError.message,
|
|
564
|
+
data: {
|
|
565
|
+
stack: getStack(prettyError),
|
|
566
|
+
codeFrame: prettyError.codeFrame,
|
|
567
|
+
type: getErrorType(prettyError),
|
|
568
|
+
code: prettyError.code,
|
|
569
|
+
name: prettyError.name
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
};
|
|
573
|
+
const create$1$1 = (id, error) => {
|
|
574
|
+
return {
|
|
575
|
+
jsonrpc: Two$1,
|
|
576
|
+
id,
|
|
577
|
+
error
|
|
578
|
+
};
|
|
579
|
+
};
|
|
580
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
581
|
+
const prettyError = preparePrettyError(error);
|
|
582
|
+
logError(error, prettyError);
|
|
583
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
584
|
+
return create$1$1(id, errorProperty);
|
|
585
|
+
};
|
|
586
|
+
const create$3 = (message, result) => {
|
|
587
|
+
return {
|
|
588
|
+
jsonrpc: Two$1,
|
|
589
|
+
id: message.id,
|
|
590
|
+
result: result ?? null
|
|
591
|
+
};
|
|
592
|
+
};
|
|
593
|
+
const getSuccessResponse = (message, result) => {
|
|
594
|
+
const resultProperty = result ?? null;
|
|
595
|
+
return create$3(message, resultProperty);
|
|
596
|
+
};
|
|
597
|
+
const getErrorResponseSimple = (id, error) => {
|
|
598
|
+
return {
|
|
599
|
+
jsonrpc: Two$1,
|
|
600
|
+
id,
|
|
601
|
+
error: {
|
|
602
|
+
code: Custom,
|
|
603
|
+
// @ts-ignore
|
|
604
|
+
message: error.message,
|
|
605
|
+
data: error
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
};
|
|
609
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
610
|
+
try {
|
|
611
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
612
|
+
return getSuccessResponse(message, result);
|
|
613
|
+
} catch (error) {
|
|
614
|
+
if (ipc.canUseSimpleErrorResponse) {
|
|
615
|
+
return getErrorResponseSimple(message.id, error);
|
|
616
|
+
}
|
|
617
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
const defaultPreparePrettyError = error => {
|
|
621
|
+
return error;
|
|
622
|
+
};
|
|
623
|
+
const defaultLogError = () => {
|
|
624
|
+
// ignore
|
|
625
|
+
};
|
|
626
|
+
const defaultRequiresSocket = () => {
|
|
627
|
+
return false;
|
|
628
|
+
};
|
|
629
|
+
const defaultResolve = resolve;
|
|
630
|
+
|
|
631
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
632
|
+
const normalizeParams = args => {
|
|
633
|
+
if (args.length === 1) {
|
|
634
|
+
const options = args[0];
|
|
635
|
+
return {
|
|
636
|
+
ipc: options.ipc,
|
|
637
|
+
message: options.message,
|
|
638
|
+
execute: options.execute,
|
|
639
|
+
resolve: options.resolve || defaultResolve,
|
|
640
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
641
|
+
logError: options.logError || defaultLogError,
|
|
642
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
return {
|
|
646
|
+
ipc: args[0],
|
|
647
|
+
message: args[1],
|
|
648
|
+
execute: args[2],
|
|
649
|
+
resolve: args[3],
|
|
650
|
+
preparePrettyError: args[4],
|
|
651
|
+
logError: args[5],
|
|
652
|
+
requiresSocket: args[6]
|
|
653
|
+
};
|
|
654
|
+
};
|
|
655
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
656
|
+
const options = normalizeParams(args);
|
|
657
|
+
const {
|
|
658
|
+
message,
|
|
659
|
+
ipc,
|
|
660
|
+
execute,
|
|
661
|
+
resolve,
|
|
662
|
+
preparePrettyError,
|
|
663
|
+
logError,
|
|
664
|
+
requiresSocket
|
|
665
|
+
} = options;
|
|
666
|
+
if ('id' in message) {
|
|
667
|
+
if ('method' in message) {
|
|
668
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
669
|
+
try {
|
|
670
|
+
ipc.send(response);
|
|
671
|
+
} catch (error) {
|
|
672
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
673
|
+
ipc.send(errorResponse);
|
|
674
|
+
}
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
resolve(message.id, message);
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if ('method' in message) {
|
|
681
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
throw new JsonRpcError('unexpected message');
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
class CommandNotFoundError extends Error {
|
|
688
|
+
constructor(command) {
|
|
689
|
+
super(`Command not found ${command}`);
|
|
690
|
+
this.name = 'CommandNotFoundError';
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
const commands = Object.create(null);
|
|
694
|
+
const register = commandMap => {
|
|
695
|
+
Object.assign(commands, commandMap);
|
|
696
|
+
};
|
|
697
|
+
const getCommand = key => {
|
|
698
|
+
return commands[key];
|
|
699
|
+
};
|
|
700
|
+
const execute = (command, ...args) => {
|
|
701
|
+
const fn = getCommand(command);
|
|
702
|
+
if (!fn) {
|
|
703
|
+
throw new CommandNotFoundError(command);
|
|
704
|
+
}
|
|
705
|
+
return fn(...args);
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
const Two = '2.0';
|
|
709
|
+
const create$t = (method, params) => {
|
|
710
|
+
return {
|
|
711
|
+
jsonrpc: Two,
|
|
712
|
+
method,
|
|
713
|
+
params
|
|
714
|
+
};
|
|
715
|
+
};
|
|
716
|
+
const create$s = (id, method, params) => {
|
|
717
|
+
const message = {
|
|
718
|
+
id,
|
|
719
|
+
jsonrpc: Two,
|
|
720
|
+
method,
|
|
721
|
+
params
|
|
722
|
+
};
|
|
723
|
+
return message;
|
|
724
|
+
};
|
|
725
|
+
let id = 0;
|
|
726
|
+
const create$r = () => {
|
|
727
|
+
return ++id;
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
/* eslint-disable n/no-unsupported-features/es-syntax */
|
|
731
|
+
|
|
732
|
+
const registerPromise = map => {
|
|
733
|
+
const id = create$r();
|
|
734
|
+
const {
|
|
735
|
+
promise,
|
|
736
|
+
resolve
|
|
737
|
+
} = Promise.withResolvers();
|
|
738
|
+
map[id] = resolve;
|
|
739
|
+
return {
|
|
740
|
+
id,
|
|
741
|
+
promise
|
|
742
|
+
};
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
// @ts-ignore
|
|
746
|
+
const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
|
|
747
|
+
const {
|
|
748
|
+
id,
|
|
749
|
+
promise
|
|
750
|
+
} = registerPromise(callbacks);
|
|
751
|
+
const message = create$s(id, method, params);
|
|
752
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
753
|
+
ipc.sendAndTransfer(message);
|
|
754
|
+
} else {
|
|
755
|
+
ipc.send(message);
|
|
756
|
+
}
|
|
757
|
+
const responseMessage = await promise;
|
|
758
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
759
|
+
};
|
|
760
|
+
const createRpc = ipc => {
|
|
761
|
+
const callbacks = Object.create(null);
|
|
762
|
+
ipc._resolve = (id, response) => {
|
|
763
|
+
const fn = callbacks[id];
|
|
764
|
+
if (!fn) {
|
|
765
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
fn(response);
|
|
769
|
+
delete callbacks[id];
|
|
770
|
+
};
|
|
771
|
+
const rpc = {
|
|
772
|
+
async dispose() {
|
|
773
|
+
await ipc?.dispose();
|
|
774
|
+
},
|
|
775
|
+
invoke(method, ...params) {
|
|
776
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
777
|
+
},
|
|
778
|
+
invokeAndTransfer(method, ...params) {
|
|
779
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
780
|
+
},
|
|
781
|
+
// @ts-ignore
|
|
782
|
+
ipc,
|
|
783
|
+
/**
|
|
784
|
+
* @deprecated
|
|
785
|
+
*/
|
|
786
|
+
send(method, ...params) {
|
|
787
|
+
const message = create$t(method, params);
|
|
788
|
+
ipc.send(message);
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
return rpc;
|
|
792
|
+
};
|
|
793
|
+
const requiresSocket = () => {
|
|
794
|
+
return false;
|
|
795
|
+
};
|
|
796
|
+
const preparePrettyError = error => {
|
|
797
|
+
return error;
|
|
798
|
+
};
|
|
799
|
+
const logError = () => {
|
|
800
|
+
// handled by renderer worker
|
|
801
|
+
};
|
|
802
|
+
const handleMessage = event => {
|
|
803
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
804
|
+
const actualExecute = event?.target?.execute || execute;
|
|
805
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
806
|
+
};
|
|
807
|
+
const handleIpc = ipc => {
|
|
808
|
+
if ('addEventListener' in ipc) {
|
|
809
|
+
ipc.addEventListener('message', handleMessage);
|
|
810
|
+
} else if ('on' in ipc) {
|
|
811
|
+
// deprecated
|
|
812
|
+
ipc.on('message', handleMessage);
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
const listen$1 = async (module, options) => {
|
|
816
|
+
const rawIpc = await module.listen(options);
|
|
817
|
+
if (module.signal) {
|
|
818
|
+
module.signal(rawIpc);
|
|
819
|
+
}
|
|
820
|
+
const ipc = module.wrap(rawIpc);
|
|
821
|
+
return ipc;
|
|
822
|
+
};
|
|
823
|
+
const create$2 = async ({
|
|
824
|
+
commandMap
|
|
825
|
+
}) => {
|
|
826
|
+
// TODO create a commandMap per rpc instance
|
|
827
|
+
register(commandMap);
|
|
828
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
829
|
+
handleIpc(ipc);
|
|
830
|
+
const rpc = createRpc(ipc);
|
|
831
|
+
return rpc;
|
|
832
|
+
};
|
|
833
|
+
const WebWorkerRpcClient = {
|
|
834
|
+
__proto__: null,
|
|
835
|
+
create: create$2
|
|
836
|
+
};
|
|
837
|
+
const createMockRpc = ({
|
|
838
|
+
commandMap
|
|
839
|
+
}) => {
|
|
840
|
+
const invocations = [];
|
|
841
|
+
const invoke = (method, ...params) => {
|
|
842
|
+
invocations.push([method, ...params]);
|
|
843
|
+
const command = commandMap[method];
|
|
844
|
+
if (!command) {
|
|
845
|
+
throw new Error(`command ${method} not found`);
|
|
846
|
+
}
|
|
847
|
+
return command(...params);
|
|
848
|
+
};
|
|
849
|
+
const mockRpc = {
|
|
850
|
+
invocations,
|
|
851
|
+
invoke,
|
|
852
|
+
invokeAndTransfer: invoke
|
|
853
|
+
};
|
|
854
|
+
return mockRpc;
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
const H1 = 5;
|
|
858
|
+
const Text = 12;
|
|
859
|
+
const Reference = 100;
|
|
860
|
+
|
|
861
|
+
const TargetName = 'event.target.name';
|
|
862
|
+
|
|
863
|
+
const RendererWorker = 1;
|
|
864
|
+
|
|
865
|
+
const SetDom2 = 'Viewlet.setDom2';
|
|
866
|
+
const SetPatches = 'Viewlet.setPatches';
|
|
867
|
+
|
|
868
|
+
const rpcs = Object.create(null);
|
|
869
|
+
const set$2 = (id, rpc) => {
|
|
870
|
+
rpcs[id] = rpc;
|
|
871
|
+
};
|
|
872
|
+
const get$1 = id => {
|
|
873
|
+
return rpcs[id];
|
|
874
|
+
};
|
|
875
|
+
const remove = id => {
|
|
876
|
+
delete rpcs[id];
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
880
|
+
const create$1 = rpcId => {
|
|
881
|
+
return {
|
|
882
|
+
async dispose() {
|
|
883
|
+
const rpc = get$1(rpcId);
|
|
884
|
+
await rpc.dispose();
|
|
885
|
+
},
|
|
886
|
+
// @ts-ignore
|
|
887
|
+
invoke(method, ...params) {
|
|
888
|
+
const rpc = get$1(rpcId);
|
|
889
|
+
// @ts-ignore
|
|
890
|
+
return rpc.invoke(method, ...params);
|
|
891
|
+
},
|
|
892
|
+
// @ts-ignore
|
|
893
|
+
invokeAndTransfer(method, ...params) {
|
|
894
|
+
const rpc = get$1(rpcId);
|
|
895
|
+
// @ts-ignore
|
|
896
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
897
|
+
},
|
|
898
|
+
registerMockRpc(commandMap) {
|
|
899
|
+
const mockRpc = createMockRpc({
|
|
900
|
+
commandMap
|
|
901
|
+
});
|
|
902
|
+
set$2(rpcId, mockRpc);
|
|
903
|
+
// @ts-ignore
|
|
904
|
+
mockRpc[Symbol.dispose] = () => {
|
|
905
|
+
remove(rpcId);
|
|
906
|
+
};
|
|
907
|
+
// @ts-ignore
|
|
908
|
+
return mockRpc;
|
|
909
|
+
},
|
|
910
|
+
set(rpc) {
|
|
911
|
+
set$2(rpcId, rpc);
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
const {
|
|
917
|
+
set: set$1
|
|
918
|
+
} = create$1(RendererWorker);
|
|
919
|
+
|
|
920
|
+
const terminate = () => {
|
|
921
|
+
globalThis.close();
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
925
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
926
|
+
|
|
927
|
+
const states = new Map();
|
|
928
|
+
const set = (uid, newState, oldState) => {
|
|
929
|
+
states.set(uid, {
|
|
930
|
+
newState,
|
|
931
|
+
oldState
|
|
932
|
+
});
|
|
933
|
+
};
|
|
934
|
+
const get = uid => {
|
|
935
|
+
const state = states.get(uid);
|
|
936
|
+
if (!state) {
|
|
937
|
+
throw new Error(`State not found for uid ${uid}`);
|
|
938
|
+
}
|
|
939
|
+
return state;
|
|
940
|
+
};
|
|
941
|
+
const getCommandIds = () => {
|
|
942
|
+
return [];
|
|
943
|
+
};
|
|
944
|
+
const wrapCommand = fn => {
|
|
945
|
+
return (uid, ...args) => {
|
|
946
|
+
const {
|
|
947
|
+
newState
|
|
948
|
+
} = get(uid);
|
|
949
|
+
return fn(newState, ...args);
|
|
950
|
+
};
|
|
951
|
+
};
|
|
952
|
+
const wrapGetter = fn => {
|
|
953
|
+
return uid => {
|
|
954
|
+
const {
|
|
955
|
+
newState
|
|
956
|
+
} = get(uid);
|
|
957
|
+
return fn(newState);
|
|
958
|
+
};
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
const create = (uid, uri, x, y, width, height, platform, assetDir) => {
|
|
962
|
+
const state = {
|
|
963
|
+
assetDir,
|
|
964
|
+
errorCount: 0,
|
|
965
|
+
initial: true,
|
|
966
|
+
platform,
|
|
967
|
+
uid,
|
|
968
|
+
warningCount: 0
|
|
969
|
+
};
|
|
970
|
+
set(uid, state, state);
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
const isEqual = (oldState, newState) => {
|
|
974
|
+
return oldState.warningCount === newState.warningCount;
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
const RenderItems = 4;
|
|
978
|
+
const RenderIncremental = 11;
|
|
979
|
+
|
|
980
|
+
const modules = [isEqual];
|
|
981
|
+
const numbers = [RenderIncremental];
|
|
982
|
+
|
|
983
|
+
const diff = (oldState, newState) => {
|
|
984
|
+
const diffResult = [];
|
|
985
|
+
for (let i = 0; i < modules.length; i++) {
|
|
986
|
+
const fn = modules[i];
|
|
987
|
+
if (!fn(oldState, newState)) {
|
|
988
|
+
diffResult.push(numbers[i]);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return diffResult;
|
|
992
|
+
};
|
|
993
|
+
|
|
994
|
+
const diff2 = uid => {
|
|
995
|
+
const {
|
|
996
|
+
newState,
|
|
997
|
+
oldState
|
|
998
|
+
} = get(uid);
|
|
999
|
+
const result = diff(oldState, newState);
|
|
1000
|
+
return result;
|
|
1001
|
+
};
|
|
1002
|
+
|
|
1003
|
+
const loadContent = async state => {
|
|
1004
|
+
return {
|
|
1005
|
+
...state,
|
|
1006
|
+
errorCount: 0,
|
|
1007
|
+
initial: false,
|
|
1008
|
+
warningCount: 0
|
|
1009
|
+
};
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
const SetText = 1;
|
|
1013
|
+
const Replace = 2;
|
|
1014
|
+
const SetAttribute = 3;
|
|
1015
|
+
const RemoveAttribute = 4;
|
|
1016
|
+
const Add = 6;
|
|
1017
|
+
const NavigateChild = 7;
|
|
1018
|
+
const NavigateParent = 8;
|
|
1019
|
+
const RemoveChild = 9;
|
|
1020
|
+
const NavigateSibling = 10;
|
|
1021
|
+
const SetReferenceNodeUid = 11;
|
|
1022
|
+
|
|
1023
|
+
const isKey = key => {
|
|
1024
|
+
return key !== 'type' && key !== 'childCount';
|
|
1025
|
+
};
|
|
1026
|
+
|
|
1027
|
+
const getKeys = node => {
|
|
1028
|
+
const keys = Object.keys(node).filter(isKey);
|
|
1029
|
+
return keys;
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
const arrayToTree = nodes => {
|
|
1033
|
+
const result = [];
|
|
1034
|
+
let i = 0;
|
|
1035
|
+
while (i < nodes.length) {
|
|
1036
|
+
const node = nodes[i];
|
|
1037
|
+
const {
|
|
1038
|
+
children,
|
|
1039
|
+
nodesConsumed
|
|
1040
|
+
} = getChildrenWithCount(nodes, i + 1, node.childCount || 0);
|
|
1041
|
+
result.push({
|
|
1042
|
+
node,
|
|
1043
|
+
children
|
|
1044
|
+
});
|
|
1045
|
+
i += 1 + nodesConsumed;
|
|
1046
|
+
}
|
|
1047
|
+
return result;
|
|
1048
|
+
};
|
|
1049
|
+
const getChildrenWithCount = (nodes, startIndex, childCount) => {
|
|
1050
|
+
if (childCount === 0) {
|
|
1051
|
+
return {
|
|
1052
|
+
children: [],
|
|
1053
|
+
nodesConsumed: 0
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
const children = [];
|
|
1057
|
+
let i = startIndex;
|
|
1058
|
+
let remaining = childCount;
|
|
1059
|
+
let totalConsumed = 0;
|
|
1060
|
+
while (remaining > 0 && i < nodes.length) {
|
|
1061
|
+
const node = nodes[i];
|
|
1062
|
+
const nodeChildCount = node.childCount || 0;
|
|
1063
|
+
const {
|
|
1064
|
+
children: nodeChildren,
|
|
1065
|
+
nodesConsumed
|
|
1066
|
+
} = getChildrenWithCount(nodes, i + 1, nodeChildCount);
|
|
1067
|
+
children.push({
|
|
1068
|
+
node,
|
|
1069
|
+
children: nodeChildren
|
|
1070
|
+
});
|
|
1071
|
+
const nodeSize = 1 + nodesConsumed;
|
|
1072
|
+
i += nodeSize;
|
|
1073
|
+
totalConsumed += nodeSize;
|
|
1074
|
+
remaining--;
|
|
1075
|
+
}
|
|
1076
|
+
return {
|
|
1077
|
+
children,
|
|
1078
|
+
nodesConsumed: totalConsumed
|
|
1079
|
+
};
|
|
1080
|
+
};
|
|
1081
|
+
|
|
1082
|
+
const compareNodes = (oldNode, newNode) => {
|
|
1083
|
+
const patches = [];
|
|
1084
|
+
// Check if node type changed - return null to signal incompatible nodes
|
|
1085
|
+
// (caller should handle this with a Replace operation)
|
|
1086
|
+
if (oldNode.type !== newNode.type) {
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
// Handle reference nodes - special handling for uid changes
|
|
1090
|
+
if (oldNode.type === Reference) {
|
|
1091
|
+
if (oldNode.uid !== newNode.uid) {
|
|
1092
|
+
patches.push({
|
|
1093
|
+
type: SetReferenceNodeUid,
|
|
1094
|
+
uid: newNode.uid
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
return patches;
|
|
1098
|
+
}
|
|
1099
|
+
// Handle text nodes
|
|
1100
|
+
if (oldNode.type === Text && newNode.type === Text) {
|
|
1101
|
+
if (oldNode.text !== newNode.text) {
|
|
1102
|
+
patches.push({
|
|
1103
|
+
type: SetText,
|
|
1104
|
+
value: newNode.text
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
return patches;
|
|
1108
|
+
}
|
|
1109
|
+
// Compare attributes
|
|
1110
|
+
const oldKeys = getKeys(oldNode);
|
|
1111
|
+
const newKeys = getKeys(newNode);
|
|
1112
|
+
// Check for attribute changes
|
|
1113
|
+
for (const key of newKeys) {
|
|
1114
|
+
if (oldNode[key] !== newNode[key]) {
|
|
1115
|
+
patches.push({
|
|
1116
|
+
type: SetAttribute,
|
|
1117
|
+
key,
|
|
1118
|
+
value: newNode[key]
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
// Check for removed attributes
|
|
1123
|
+
for (const key of oldKeys) {
|
|
1124
|
+
if (!(key in newNode)) {
|
|
1125
|
+
patches.push({
|
|
1126
|
+
type: RemoveAttribute,
|
|
1127
|
+
key
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
return patches;
|
|
1132
|
+
};
|
|
1133
|
+
|
|
1134
|
+
const treeToArray = node => {
|
|
1135
|
+
const result = [node.node];
|
|
1136
|
+
for (const child of node.children) {
|
|
1137
|
+
result.push(...treeToArray(child));
|
|
1138
|
+
}
|
|
1139
|
+
return result;
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
const diffChildren = (oldChildren, newChildren, patches) => {
|
|
1143
|
+
const maxLength = Math.max(oldChildren.length, newChildren.length);
|
|
1144
|
+
// Track where we are: -1 means at parent, >= 0 means at child index
|
|
1145
|
+
let currentChildIndex = -1;
|
|
1146
|
+
// Collect indices of children to remove (we'll add these patches at the end in reverse order)
|
|
1147
|
+
const indicesToRemove = [];
|
|
1148
|
+
for (let i = 0; i < maxLength; i++) {
|
|
1149
|
+
const oldNode = oldChildren[i];
|
|
1150
|
+
const newNode = newChildren[i];
|
|
1151
|
+
if (!oldNode && !newNode) {
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1154
|
+
if (!oldNode) {
|
|
1155
|
+
// Add new node - we should be at the parent
|
|
1156
|
+
if (currentChildIndex >= 0) {
|
|
1157
|
+
// Navigate back to parent
|
|
1158
|
+
patches.push({
|
|
1159
|
+
type: NavigateParent
|
|
1160
|
+
});
|
|
1161
|
+
currentChildIndex = -1;
|
|
1162
|
+
}
|
|
1163
|
+
// Flatten the entire subtree so renderInternal can handle it
|
|
1164
|
+
const flatNodes = treeToArray(newNode);
|
|
1165
|
+
patches.push({
|
|
1166
|
+
type: Add,
|
|
1167
|
+
nodes: flatNodes
|
|
1168
|
+
});
|
|
1169
|
+
} else if (newNode) {
|
|
1170
|
+
// Compare nodes to see if we need any patches
|
|
1171
|
+
const nodePatches = compareNodes(oldNode.node, newNode.node);
|
|
1172
|
+
// If nodePatches is null, the node types are incompatible - need to replace
|
|
1173
|
+
if (nodePatches === null) {
|
|
1174
|
+
// Navigate to this child
|
|
1175
|
+
if (currentChildIndex === -1) {
|
|
1176
|
+
patches.push({
|
|
1177
|
+
type: NavigateChild,
|
|
1178
|
+
index: i
|
|
1179
|
+
});
|
|
1180
|
+
currentChildIndex = i;
|
|
1181
|
+
} else if (currentChildIndex !== i) {
|
|
1182
|
+
patches.push({
|
|
1183
|
+
type: NavigateSibling,
|
|
1184
|
+
index: i
|
|
1185
|
+
});
|
|
1186
|
+
currentChildIndex = i;
|
|
1187
|
+
}
|
|
1188
|
+
// Replace the entire subtree
|
|
1189
|
+
const flatNodes = treeToArray(newNode);
|
|
1190
|
+
patches.push({
|
|
1191
|
+
type: Replace,
|
|
1192
|
+
nodes: flatNodes
|
|
1193
|
+
});
|
|
1194
|
+
// After replace, we're at the new element (same position)
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
// Check if we need to recurse into children
|
|
1198
|
+
const hasChildrenToCompare = oldNode.children.length > 0 || newNode.children.length > 0;
|
|
1199
|
+
// Only navigate to this element if we need to do something
|
|
1200
|
+
if (nodePatches.length > 0 || hasChildrenToCompare) {
|
|
1201
|
+
// Navigate to this child if not already there
|
|
1202
|
+
if (currentChildIndex === -1) {
|
|
1203
|
+
patches.push({
|
|
1204
|
+
type: NavigateChild,
|
|
1205
|
+
index: i
|
|
1206
|
+
});
|
|
1207
|
+
currentChildIndex = i;
|
|
1208
|
+
} else if (currentChildIndex !== i) {
|
|
1209
|
+
patches.push({
|
|
1210
|
+
type: NavigateSibling,
|
|
1211
|
+
index: i
|
|
1212
|
+
});
|
|
1213
|
+
currentChildIndex = i;
|
|
1214
|
+
}
|
|
1215
|
+
// Apply node patches (these apply to the current element, not children)
|
|
1216
|
+
if (nodePatches.length > 0) {
|
|
1217
|
+
patches.push(...nodePatches);
|
|
1218
|
+
}
|
|
1219
|
+
// Compare children recursively
|
|
1220
|
+
if (hasChildrenToCompare) {
|
|
1221
|
+
diffChildren(oldNode.children, newNode.children, patches);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
} else {
|
|
1225
|
+
// Remove old node - collect the index for later removal
|
|
1226
|
+
indicesToRemove.push(i);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
// Navigate back to parent if we ended at a child
|
|
1230
|
+
if (currentChildIndex >= 0) {
|
|
1231
|
+
patches.push({
|
|
1232
|
+
type: NavigateParent
|
|
1233
|
+
});
|
|
1234
|
+
currentChildIndex = -1;
|
|
1235
|
+
}
|
|
1236
|
+
// Add remove patches in reverse order (highest index first)
|
|
1237
|
+
// This ensures indices remain valid as we remove
|
|
1238
|
+
for (let j = indicesToRemove.length - 1; j >= 0; j--) {
|
|
1239
|
+
patches.push({
|
|
1240
|
+
type: RemoveChild,
|
|
1241
|
+
index: indicesToRemove[j]
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
};
|
|
1245
|
+
const diffTrees = (oldTree, newTree, patches, path) => {
|
|
1246
|
+
// At the root level (path.length === 0), we're already AT the element
|
|
1247
|
+
// So we compare the root node directly, then compare its children
|
|
1248
|
+
if (path.length === 0 && oldTree.length === 1 && newTree.length === 1) {
|
|
1249
|
+
const oldNode = oldTree[0];
|
|
1250
|
+
const newNode = newTree[0];
|
|
1251
|
+
// Compare root nodes
|
|
1252
|
+
const nodePatches = compareNodes(oldNode.node, newNode.node);
|
|
1253
|
+
// If nodePatches is null, the root node types are incompatible - need to replace
|
|
1254
|
+
if (nodePatches === null) {
|
|
1255
|
+
const flatNodes = treeToArray(newNode);
|
|
1256
|
+
patches.push({
|
|
1257
|
+
type: Replace,
|
|
1258
|
+
nodes: flatNodes
|
|
1259
|
+
});
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
if (nodePatches.length > 0) {
|
|
1263
|
+
patches.push(...nodePatches);
|
|
1264
|
+
}
|
|
1265
|
+
// Compare children
|
|
1266
|
+
if (oldNode.children.length > 0 || newNode.children.length > 0) {
|
|
1267
|
+
diffChildren(oldNode.children, newNode.children, patches);
|
|
1268
|
+
}
|
|
1269
|
+
} else {
|
|
1270
|
+
// Non-root level or multiple root elements - use the regular comparison
|
|
1271
|
+
diffChildren(oldTree, newTree, patches);
|
|
1272
|
+
}
|
|
1273
|
+
};
|
|
1274
|
+
|
|
1275
|
+
const removeTrailingNavigationPatches = patches => {
|
|
1276
|
+
// Find the last non-navigation patch
|
|
1277
|
+
let lastNonNavigationIndex = -1;
|
|
1278
|
+
for (let i = patches.length - 1; i >= 0; i--) {
|
|
1279
|
+
const patch = patches[i];
|
|
1280
|
+
if (patch.type !== NavigateChild && patch.type !== NavigateParent && patch.type !== NavigateSibling) {
|
|
1281
|
+
lastNonNavigationIndex = i;
|
|
1282
|
+
break;
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
// Return patches up to and including the last non-navigation patch
|
|
1286
|
+
return lastNonNavigationIndex === -1 ? [] : patches.slice(0, lastNonNavigationIndex + 1);
|
|
1287
|
+
};
|
|
1288
|
+
|
|
1289
|
+
const diffTree = (oldNodes, newNodes) => {
|
|
1290
|
+
// Step 1: Convert flat arrays to tree structures
|
|
1291
|
+
const oldTree = arrayToTree(oldNodes);
|
|
1292
|
+
const newTree = arrayToTree(newNodes);
|
|
1293
|
+
// Step 3: Compare the trees
|
|
1294
|
+
const patches = [];
|
|
1295
|
+
diffTrees(oldTree, newTree, patches, []);
|
|
1296
|
+
// Remove trailing navigation patches since they serve no purpose
|
|
1297
|
+
return removeTrailingNavigationPatches(patches);
|
|
1298
|
+
};
|
|
1299
|
+
|
|
1300
|
+
const getPreviewDom = () => {
|
|
1301
|
+
return [{
|
|
1302
|
+
childCount: 1,
|
|
1303
|
+
type: H1
|
|
1304
|
+
}, {
|
|
1305
|
+
text: 'hello from preview',
|
|
1306
|
+
type: Text
|
|
1307
|
+
}];
|
|
1308
|
+
};
|
|
1309
|
+
|
|
1310
|
+
const renderItems = (oldState, newState) => {
|
|
1311
|
+
const {
|
|
1312
|
+
uid
|
|
1313
|
+
} = newState;
|
|
1314
|
+
const dom = getPreviewDom();
|
|
1315
|
+
return [SetDom2, uid, dom];
|
|
1316
|
+
};
|
|
1317
|
+
|
|
1318
|
+
const renderIncremental = (oldState, newState) => {
|
|
1319
|
+
const oldDom = renderItems(oldState, oldState)[2];
|
|
1320
|
+
const newDom = renderItems(newState, newState)[2];
|
|
1321
|
+
const patches = diffTree(oldDom, newDom);
|
|
1322
|
+
return [SetPatches, newState.uid, patches];
|
|
1323
|
+
};
|
|
1324
|
+
|
|
1325
|
+
const getRenderer = diffType => {
|
|
1326
|
+
switch (diffType) {
|
|
1327
|
+
case RenderIncremental:
|
|
1328
|
+
return renderIncremental;
|
|
1329
|
+
case RenderItems:
|
|
1330
|
+
return renderItems;
|
|
1331
|
+
default:
|
|
1332
|
+
throw new Error('unknown renderer');
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
|
|
1336
|
+
const applyRender = (oldState, newState, diffResult) => {
|
|
1337
|
+
const commands = [];
|
|
1338
|
+
for (const item of diffResult) {
|
|
1339
|
+
const fn = getRenderer(item);
|
|
1340
|
+
const result = fn(oldState, newState);
|
|
1341
|
+
if (result.length > 0) {
|
|
1342
|
+
commands.push(result);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
return commands;
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
const render2 = (uid, diffResult) => {
|
|
1349
|
+
const {
|
|
1350
|
+
newState,
|
|
1351
|
+
oldState
|
|
1352
|
+
} = get(uid);
|
|
1353
|
+
set(uid, newState, newState);
|
|
1354
|
+
const commands = applyRender(oldState, newState, diffResult);
|
|
1355
|
+
return commands;
|
|
1356
|
+
};
|
|
1357
|
+
|
|
1358
|
+
const HandleClick = 11;
|
|
1359
|
+
|
|
1360
|
+
const renderEventListeners = () => {
|
|
1361
|
+
return [{
|
|
1362
|
+
name: HandleClick,
|
|
1363
|
+
params: ['handleClick', TargetName]
|
|
1364
|
+
}];
|
|
1365
|
+
};
|
|
1366
|
+
|
|
1367
|
+
const resize = (state, dimensions) => {
|
|
1368
|
+
return {
|
|
1369
|
+
...state,
|
|
1370
|
+
...dimensions
|
|
1371
|
+
};
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
const saveState = state => {
|
|
1375
|
+
return {
|
|
1376
|
+
x: 0
|
|
1377
|
+
};
|
|
1378
|
+
};
|
|
1379
|
+
|
|
1380
|
+
const commandMap = {
|
|
1381
|
+
'StatusBar.create': create,
|
|
1382
|
+
'StatusBar.diff2': diff2,
|
|
1383
|
+
'StatusBar.getCommandIds': getCommandIds,
|
|
1384
|
+
'StatusBar.loadContent': wrapCommand(loadContent),
|
|
1385
|
+
'StatusBar.render2': render2,
|
|
1386
|
+
'StatusBar.renderEventListeners': renderEventListeners,
|
|
1387
|
+
'StatusBar.resize': wrapCommand(resize),
|
|
1388
|
+
'StatusBar.saveState': wrapGetter(saveState),
|
|
1389
|
+
'StatusBar.terminate': terminate
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
const listen = async () => {
|
|
1393
|
+
const rpc = await WebWorkerRpcClient.create({
|
|
1394
|
+
commandMap: commandMap
|
|
1395
|
+
});
|
|
1396
|
+
set$1(rpc);
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1399
|
+
const main = async () => {
|
|
1400
|
+
await listen();
|
|
1401
|
+
};
|
|
1402
|
+
|
|
1403
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lvce-editor/preview-worker",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Preview Worker",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/lvce-editor/preview-worker.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Lvce Editor",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "dist/previewWorkerMain.js",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@lvce-editor/virtual-dom-worker": "^7.1.0"
|
|
15
|
+
}
|
|
16
|
+
}
|