@lvce-editor/test-worker 1.3.0 → 1.4.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/dist/testWorkerMain.js +189 -17
- package/package.json +1 -1
package/dist/testWorkerMain.js
CHANGED
|
@@ -134,7 +134,6 @@ const getErrorConstructor = (message, type) => {
|
|
|
134
134
|
if (type) {
|
|
135
135
|
switch (type) {
|
|
136
136
|
case DomException:
|
|
137
|
-
// @ts-ignore
|
|
138
137
|
return DOMException;
|
|
139
138
|
case TypeError$1:
|
|
140
139
|
return TypeError;
|
|
@@ -159,7 +158,6 @@ const getErrorConstructor = (message, type) => {
|
|
|
159
158
|
};
|
|
160
159
|
const constructError = (message, type, name) => {
|
|
161
160
|
const ErrorConstructor = getErrorConstructor(message, type);
|
|
162
|
-
// @ts-ignore
|
|
163
161
|
if (ErrorConstructor === DOMException && name) {
|
|
164
162
|
return new ErrorConstructor(message, name);
|
|
165
163
|
}
|
|
@@ -175,6 +173,13 @@ const constructError = (message, type, name) => {
|
|
|
175
173
|
const getNewLineIndex$2 = (string, startIndex = undefined) => {
|
|
176
174
|
return string.indexOf(NewLine$3, startIndex);
|
|
177
175
|
};
|
|
176
|
+
const getParentStack = error => {
|
|
177
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
178
|
+
if (parentStack.startsWith(' at')) {
|
|
179
|
+
parentStack = error.message + NewLine$3 + parentStack;
|
|
180
|
+
}
|
|
181
|
+
return parentStack;
|
|
182
|
+
};
|
|
178
183
|
const joinLines$1 = lines => {
|
|
179
184
|
return lines.join(NewLine$3);
|
|
180
185
|
};
|
|
@@ -183,18 +188,11 @@ const Custom = -32001;
|
|
|
183
188
|
const splitLines$1 = lines => {
|
|
184
189
|
return lines.split(NewLine$3);
|
|
185
190
|
};
|
|
186
|
-
const getParentStack = error => {
|
|
187
|
-
let parentStack = error.stack || error.data || error.message || '';
|
|
188
|
-
if (parentStack.startsWith(' at')) {
|
|
189
|
-
parentStack = error.message + NewLine$3 + parentStack;
|
|
190
|
-
}
|
|
191
|
-
return parentStack;
|
|
192
|
-
};
|
|
193
191
|
const restoreJsonRpcError = error => {
|
|
194
192
|
if (error && error instanceof Error) {
|
|
195
193
|
return error;
|
|
196
194
|
}
|
|
197
|
-
const currentStack = joinLines$1(splitLines$1(new Error().stack).slice(1));
|
|
195
|
+
const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
|
|
198
196
|
if (error && error.code && error.code === MethodNotFound) {
|
|
199
197
|
const restoredError = new JsonRpcError(error.message);
|
|
200
198
|
const parentStack = getParentStack(error);
|
|
@@ -223,7 +221,6 @@ const restoreJsonRpcError = error => {
|
|
|
223
221
|
}
|
|
224
222
|
} else {
|
|
225
223
|
if (error.stack) {
|
|
226
|
-
// TODO accessing stack might be slow
|
|
227
224
|
const lowerStack = restoredError.stack || '';
|
|
228
225
|
// @ts-ignore
|
|
229
226
|
const indexNewLine = getNewLineIndex$2(lowerStack);
|
|
@@ -253,6 +250,67 @@ const unwrapJsonRpcResult = responseMessage => {
|
|
|
253
250
|
}
|
|
254
251
|
throw new JsonRpcError('unexpected response message');
|
|
255
252
|
};
|
|
253
|
+
const isMessagePort = value => {
|
|
254
|
+
return typeof MessagePort !== 'undefined' && value instanceof MessagePort;
|
|
255
|
+
};
|
|
256
|
+
const isInstanceOf = (value, constructorName) => {
|
|
257
|
+
return value?.constructor?.name === constructorName;
|
|
258
|
+
};
|
|
259
|
+
const isMessagePortMain = value => {
|
|
260
|
+
return isInstanceOf(value, 'MessagePortMain');
|
|
261
|
+
};
|
|
262
|
+
const isOffscreenCanvas = value => {
|
|
263
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
264
|
+
};
|
|
265
|
+
const isSocket = value => {
|
|
266
|
+
return isInstanceOf(value, 'Socket');
|
|
267
|
+
};
|
|
268
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
269
|
+
const isTransferrable = value => {
|
|
270
|
+
for (const fn of transferrables) {
|
|
271
|
+
if (fn(value)) {
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
};
|
|
277
|
+
const walkValue = (value, transferrables) => {
|
|
278
|
+
if (!value) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (isTransferrable(value)) {
|
|
282
|
+
transferrables.push(value);
|
|
283
|
+
}
|
|
284
|
+
if (Array.isArray(value)) {
|
|
285
|
+
for (const item of value) {
|
|
286
|
+
walkValue(item, transferrables);
|
|
287
|
+
}
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (typeof value === 'object') {
|
|
291
|
+
for (const property of Object.values(value)) {
|
|
292
|
+
walkValue(property, transferrables);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
const getTransferrables = value => {
|
|
297
|
+
const transferrables = [];
|
|
298
|
+
walkValue(value, transferrables);
|
|
299
|
+
return transferrables;
|
|
300
|
+
};
|
|
301
|
+
const isSingleTransferrable = value => {
|
|
302
|
+
return isSocket(value);
|
|
303
|
+
};
|
|
304
|
+
const getTransferrableParams = value => {
|
|
305
|
+
const transferrables = getTransferrables(value);
|
|
306
|
+
if (transferrables.length === 0) {
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
if (isSingleTransferrable(transferrables[0])) {
|
|
310
|
+
return transferrables[0];
|
|
311
|
+
}
|
|
312
|
+
return transferrables;
|
|
313
|
+
};
|
|
256
314
|
const create$1 = (message, error) => {
|
|
257
315
|
return {
|
|
258
316
|
jsonrpc: Two,
|
|
@@ -305,7 +363,42 @@ const getResponse = async (message, ipc, execute, preparePrettyError, logError,
|
|
|
305
363
|
return getErrorResponse(message, error, preparePrettyError, logError);
|
|
306
364
|
}
|
|
307
365
|
};
|
|
308
|
-
const
|
|
366
|
+
const defaultPreparePrettyError = error => {
|
|
367
|
+
return error;
|
|
368
|
+
};
|
|
369
|
+
const defaultLogError = () => {
|
|
370
|
+
// ignore
|
|
371
|
+
};
|
|
372
|
+
const defaultRequiresSocket = () => {
|
|
373
|
+
return false;
|
|
374
|
+
};
|
|
375
|
+
const defaultResolve = resolve;
|
|
376
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
377
|
+
let message;
|
|
378
|
+
let ipc;
|
|
379
|
+
let execute;
|
|
380
|
+
let preparePrettyError;
|
|
381
|
+
let logError;
|
|
382
|
+
let resolve;
|
|
383
|
+
let requiresSocket;
|
|
384
|
+
if (args.length === 1) {
|
|
385
|
+
const arg = args[0];
|
|
386
|
+
message = arg.message;
|
|
387
|
+
ipc = arg.ipc;
|
|
388
|
+
execute = arg.execute;
|
|
389
|
+
preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
|
|
390
|
+
logError = arg.logError || defaultLogError;
|
|
391
|
+
requiresSocket = arg.requiresSocket || defaultRequiresSocket;
|
|
392
|
+
resolve = arg.resolve || defaultResolve;
|
|
393
|
+
} else {
|
|
394
|
+
ipc = args[0];
|
|
395
|
+
message = args[1];
|
|
396
|
+
execute = args[2];
|
|
397
|
+
resolve = args[3];
|
|
398
|
+
preparePrettyError = args[4];
|
|
399
|
+
logError = args[5];
|
|
400
|
+
requiresSocket = args[6];
|
|
401
|
+
}
|
|
309
402
|
if ('id' in message) {
|
|
310
403
|
if ('method' in message) {
|
|
311
404
|
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
@@ -337,10 +430,33 @@ const invoke$1 = async (ipc, method, ...params) => {
|
|
|
337
430
|
return result;
|
|
338
431
|
};
|
|
339
432
|
|
|
433
|
+
// TODO deprecated old typings,
|
|
434
|
+
// always use automatic transferrable detection
|
|
435
|
+
const invokeAndTransfer$1 = async (ipc, handle, method, ...params) => {
|
|
436
|
+
let transfer = handle;
|
|
437
|
+
if (typeof handle === 'string') {
|
|
438
|
+
params = [method, ...params];
|
|
439
|
+
method = handle;
|
|
440
|
+
transfer = getTransferrableParams(params);
|
|
441
|
+
}
|
|
442
|
+
const {
|
|
443
|
+
message,
|
|
444
|
+
promise
|
|
445
|
+
} = create$2(method, params);
|
|
446
|
+
ipc.sendAndTransfer(message, transfer);
|
|
447
|
+
const responseMessage = await promise;
|
|
448
|
+
const result = unwrapJsonRpcResult(responseMessage);
|
|
449
|
+
return result;
|
|
450
|
+
};
|
|
451
|
+
|
|
340
452
|
const invoke = (method, ...params) => {
|
|
341
453
|
const ipc = get$1();
|
|
342
454
|
return invoke$1(ipc, method, ...params);
|
|
343
455
|
};
|
|
456
|
+
const invokeAndTransfer = (method, ...params) => {
|
|
457
|
+
const ipc = get$1();
|
|
458
|
+
return invokeAndTransfer$1(ipc, method, ...params);
|
|
459
|
+
};
|
|
344
460
|
const listen$4 = ipc => {
|
|
345
461
|
set$1(ipc);
|
|
346
462
|
};
|
|
@@ -348,6 +464,7 @@ const listen$4 = ipc => {
|
|
|
348
464
|
const Rpc = {
|
|
349
465
|
__proto__: null,
|
|
350
466
|
invoke,
|
|
467
|
+
invokeAndTransfer,
|
|
351
468
|
listen: listen$4
|
|
352
469
|
};
|
|
353
470
|
|
|
@@ -840,13 +957,13 @@ const TestFrameWorkComponentBaseUrl = {
|
|
|
840
957
|
getBaseUrl
|
|
841
958
|
};
|
|
842
959
|
|
|
843
|
-
const execute$
|
|
960
|
+
const execute$3 = async (id, ...args) => {
|
|
844
961
|
return invoke(id, ...args);
|
|
845
962
|
};
|
|
846
963
|
|
|
847
964
|
const TestFrameWorkComponentCommand = {
|
|
848
965
|
__proto__: null,
|
|
849
|
-
execute: execute$
|
|
966
|
+
execute: execute$3
|
|
850
967
|
};
|
|
851
968
|
|
|
852
969
|
const selectItem$1 = async text => {
|
|
@@ -1405,7 +1522,62 @@ const TestFrameWorkComponentTitleBarMenuBar = {
|
|
|
1405
1522
|
toggleMenu
|
|
1406
1523
|
};
|
|
1407
1524
|
|
|
1525
|
+
const getPortTuple = () => {
|
|
1526
|
+
const {
|
|
1527
|
+
port1,
|
|
1528
|
+
port2
|
|
1529
|
+
} = new MessageChannel();
|
|
1530
|
+
return {
|
|
1531
|
+
port1,
|
|
1532
|
+
port2
|
|
1533
|
+
};
|
|
1534
|
+
};
|
|
1535
|
+
|
|
1536
|
+
const sendPortToWebView = async (webviewId, port) => {
|
|
1537
|
+
await invokeAndTransfer('Transferrable.transferToRendererProcess', webviewId, port);
|
|
1538
|
+
console.log('did send port to renderer process');
|
|
1539
|
+
// TODO ask renderer process to transfer the port to the webview
|
|
1540
|
+
};
|
|
1541
|
+
|
|
1542
|
+
const preparePrettyError$1 = error => {
|
|
1543
|
+
return error;
|
|
1544
|
+
};
|
|
1545
|
+
const logError$1 = () => {
|
|
1546
|
+
// ignore
|
|
1547
|
+
};
|
|
1548
|
+
const execute$2 = () => {};
|
|
1549
|
+
const requiresSocket$1 = () => {
|
|
1550
|
+
return false;
|
|
1551
|
+
};
|
|
1552
|
+
const createPortIpc = async webViewId => {
|
|
1553
|
+
const {
|
|
1554
|
+
port1,
|
|
1555
|
+
port2
|
|
1556
|
+
} = getPortTuple();
|
|
1557
|
+
const firstEventPromise = new Promise(resolve => {
|
|
1558
|
+
port1.onmessage = resolve;
|
|
1559
|
+
});
|
|
1560
|
+
await sendPortToWebView(webViewId, port2);
|
|
1561
|
+
const firstEvent = await firstEventPromise;
|
|
1562
|
+
// @ts-ignore
|
|
1563
|
+
if (firstEvent.data !== 'ready') {
|
|
1564
|
+
throw new Error('unexpected first message');
|
|
1565
|
+
}
|
|
1566
|
+
const handleOtherMessage = async event => {
|
|
1567
|
+
// @ts-ignore
|
|
1568
|
+
await handleJsonRpcMessage(ipc, event.data, resolve, preparePrettyError$1, execute$2, logError$1, requiresSocket$1);
|
|
1569
|
+
};
|
|
1570
|
+
port1.onmessage = handleOtherMessage;
|
|
1571
|
+
const ipc = {
|
|
1572
|
+
send(message) {
|
|
1573
|
+
port1.postMessage(message);
|
|
1574
|
+
}
|
|
1575
|
+
};
|
|
1576
|
+
return ipc;
|
|
1577
|
+
};
|
|
1578
|
+
|
|
1408
1579
|
const fromId = async webViewId => {
|
|
1580
|
+
const ipc = await createPortIpc(webViewId);
|
|
1409
1581
|
// TODO
|
|
1410
1582
|
// 1. create messagechannel
|
|
1411
1583
|
// 2. send one message port to webview
|
|
@@ -1413,7 +1585,7 @@ const fromId = async webViewId => {
|
|
|
1413
1585
|
// 4. send test commands like locator.toBeVisible to webview
|
|
1414
1586
|
const webViewRpc = {
|
|
1415
1587
|
invoke(method, ...params) {
|
|
1416
|
-
|
|
1588
|
+
return invoke$1(ipc, method, ...params);
|
|
1417
1589
|
}
|
|
1418
1590
|
};
|
|
1419
1591
|
return {
|
|
@@ -1548,14 +1720,14 @@ const handleIpc = ipc => {
|
|
|
1548
1720
|
ipc.addEventListener('message', handleMessage);
|
|
1549
1721
|
};
|
|
1550
1722
|
|
|
1551
|
-
const MessagePort = 1;
|
|
1723
|
+
const MessagePort$1 = 1;
|
|
1552
1724
|
const ModuleWorker = 2;
|
|
1553
1725
|
const ReferencePort = 3;
|
|
1554
1726
|
const ModuleWorkerAndMessagePort = 8;
|
|
1555
1727
|
const Auto = () => {
|
|
1556
1728
|
// @ts-ignore
|
|
1557
1729
|
if (globalThis.acceptPort) {
|
|
1558
|
-
return MessagePort;
|
|
1730
|
+
return MessagePort$1;
|
|
1559
1731
|
}
|
|
1560
1732
|
// @ts-ignore
|
|
1561
1733
|
if (globalThis.acceptReferencePort) {
|