@lvce-editor/auth-worker 1.2.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/authWorkerMain.js +211 -26
- package/package.json +1 -1
package/dist/authWorkerMain.js
CHANGED
|
@@ -407,6 +407,100 @@ const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
|
407
407
|
listen: listen$6,
|
|
408
408
|
wrap: wrap$e
|
|
409
409
|
};
|
|
410
|
+
const addListener = (emitter, type, callback) => {
|
|
411
|
+
if ('addEventListener' in emitter) {
|
|
412
|
+
emitter.addEventListener(type, callback);
|
|
413
|
+
} else {
|
|
414
|
+
emitter.on(type, callback);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
const removeListener = (emitter, type, callback) => {
|
|
418
|
+
if ('removeEventListener' in emitter) {
|
|
419
|
+
emitter.removeEventListener(type, callback);
|
|
420
|
+
} else {
|
|
421
|
+
emitter.off(type, callback);
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
425
|
+
const {
|
|
426
|
+
promise,
|
|
427
|
+
resolve
|
|
428
|
+
} = Promise.withResolvers();
|
|
429
|
+
const listenerMap = Object.create(null);
|
|
430
|
+
const cleanup = value => {
|
|
431
|
+
for (const event of Object.keys(eventMap)) {
|
|
432
|
+
removeListener(eventEmitter, event, listenerMap[event]);
|
|
433
|
+
}
|
|
434
|
+
resolve(value);
|
|
435
|
+
};
|
|
436
|
+
for (const [event, type] of Object.entries(eventMap)) {
|
|
437
|
+
const listener = event => {
|
|
438
|
+
cleanup({
|
|
439
|
+
event,
|
|
440
|
+
type
|
|
441
|
+
});
|
|
442
|
+
};
|
|
443
|
+
addListener(eventEmitter, event, listener);
|
|
444
|
+
listenerMap[event] = listener;
|
|
445
|
+
}
|
|
446
|
+
return promise;
|
|
447
|
+
};
|
|
448
|
+
const Message$1 = 3;
|
|
449
|
+
const create$5$1 = async ({
|
|
450
|
+
isMessagePortOpen,
|
|
451
|
+
messagePort
|
|
452
|
+
}) => {
|
|
453
|
+
if (!isMessagePort(messagePort)) {
|
|
454
|
+
throw new IpcError('port must be of type MessagePort');
|
|
455
|
+
}
|
|
456
|
+
if (isMessagePortOpen) {
|
|
457
|
+
return messagePort;
|
|
458
|
+
}
|
|
459
|
+
const eventPromise = getFirstEvent(messagePort, {
|
|
460
|
+
message: Message$1
|
|
461
|
+
});
|
|
462
|
+
messagePort.start();
|
|
463
|
+
const {
|
|
464
|
+
event,
|
|
465
|
+
type
|
|
466
|
+
} = await eventPromise;
|
|
467
|
+
if (type !== Message$1) {
|
|
468
|
+
throw new IpcError('Failed to wait for ipc message');
|
|
469
|
+
}
|
|
470
|
+
if (event.data !== readyMessage) {
|
|
471
|
+
throw new IpcError('unexpected first message');
|
|
472
|
+
}
|
|
473
|
+
return messagePort;
|
|
474
|
+
};
|
|
475
|
+
const signal$1 = messagePort => {
|
|
476
|
+
messagePort.start();
|
|
477
|
+
};
|
|
478
|
+
class IpcParentWithMessagePort extends Ipc {
|
|
479
|
+
getData = getData$2;
|
|
480
|
+
send(message) {
|
|
481
|
+
this._rawIpc.postMessage(message);
|
|
482
|
+
}
|
|
483
|
+
sendAndTransfer(message) {
|
|
484
|
+
const transfer = getTransferrables(message);
|
|
485
|
+
this._rawIpc.postMessage(message, transfer);
|
|
486
|
+
}
|
|
487
|
+
dispose() {
|
|
488
|
+
this._rawIpc.close();
|
|
489
|
+
}
|
|
490
|
+
onMessage(callback) {
|
|
491
|
+
this._rawIpc.addEventListener('message', callback);
|
|
492
|
+
}
|
|
493
|
+
onClose(callback) {}
|
|
494
|
+
}
|
|
495
|
+
const wrap$5 = messagePort => {
|
|
496
|
+
return new IpcParentWithMessagePort(messagePort);
|
|
497
|
+
};
|
|
498
|
+
const IpcParentWithMessagePort$1 = {
|
|
499
|
+
__proto__: null,
|
|
500
|
+
create: create$5$1,
|
|
501
|
+
signal: signal$1,
|
|
502
|
+
wrap: wrap$5
|
|
503
|
+
};
|
|
410
504
|
|
|
411
505
|
class CommandNotFoundError extends Error {
|
|
412
506
|
constructor(command) {
|
|
@@ -643,7 +737,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
|
643
737
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
644
738
|
return create$1$1(id, errorProperty);
|
|
645
739
|
};
|
|
646
|
-
const create$
|
|
740
|
+
const create$9 = (message, result) => {
|
|
647
741
|
return {
|
|
648
742
|
id: message.id,
|
|
649
743
|
jsonrpc: Two$1,
|
|
@@ -652,7 +746,7 @@ const create$6 = (message, result) => {
|
|
|
652
746
|
};
|
|
653
747
|
const getSuccessResponse = (message, result) => {
|
|
654
748
|
const resultProperty = result ?? null;
|
|
655
|
-
return create$
|
|
749
|
+
return create$9(message, resultProperty);
|
|
656
750
|
};
|
|
657
751
|
const getErrorResponseSimple = (id, error) => {
|
|
658
752
|
return {
|
|
@@ -746,7 +840,7 @@ const handleJsonRpcMessage = async (...args) => {
|
|
|
746
840
|
|
|
747
841
|
const Two = '2.0';
|
|
748
842
|
|
|
749
|
-
const create$
|
|
843
|
+
const create$8 = (method, params) => {
|
|
750
844
|
return {
|
|
751
845
|
jsonrpc: Two,
|
|
752
846
|
method,
|
|
@@ -754,7 +848,7 @@ const create$5 = (method, params) => {
|
|
|
754
848
|
};
|
|
755
849
|
};
|
|
756
850
|
|
|
757
|
-
const create$
|
|
851
|
+
const create$7 = (id, method, params) => {
|
|
758
852
|
const message = {
|
|
759
853
|
id,
|
|
760
854
|
jsonrpc: Two,
|
|
@@ -765,12 +859,12 @@ const create$4 = (id, method, params) => {
|
|
|
765
859
|
};
|
|
766
860
|
|
|
767
861
|
let id = 0;
|
|
768
|
-
const create$
|
|
862
|
+
const create$6 = () => {
|
|
769
863
|
return ++id;
|
|
770
864
|
};
|
|
771
865
|
|
|
772
866
|
const registerPromise = map => {
|
|
773
|
-
const id = create$
|
|
867
|
+
const id = create$6();
|
|
774
868
|
const {
|
|
775
869
|
promise,
|
|
776
870
|
resolve
|
|
@@ -787,7 +881,7 @@ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer)
|
|
|
787
881
|
id,
|
|
788
882
|
promise
|
|
789
883
|
} = registerPromise(callbacks);
|
|
790
|
-
const message = create$
|
|
884
|
+
const message = create$7(id, method, params);
|
|
791
885
|
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
792
886
|
ipc.sendAndTransfer(message);
|
|
793
887
|
} else {
|
|
@@ -823,7 +917,7 @@ const createRpc = ipc => {
|
|
|
823
917
|
* @deprecated
|
|
824
918
|
*/
|
|
825
919
|
send(method, ...params) {
|
|
826
|
-
const message = create$
|
|
920
|
+
const message = create$8(method, params);
|
|
827
921
|
ipc.send(message);
|
|
828
922
|
}
|
|
829
923
|
};
|
|
@@ -863,6 +957,83 @@ const listen$1 = async (module, options) => {
|
|
|
863
957
|
return ipc;
|
|
864
958
|
};
|
|
865
959
|
|
|
960
|
+
const create$5 = async ({
|
|
961
|
+
commandMap,
|
|
962
|
+
isMessagePortOpen = true,
|
|
963
|
+
messagePort
|
|
964
|
+
}) => {
|
|
965
|
+
// TODO create a commandMap per rpc instance
|
|
966
|
+
register(commandMap);
|
|
967
|
+
const rawIpc = await IpcParentWithMessagePort$1.create({
|
|
968
|
+
isMessagePortOpen,
|
|
969
|
+
messagePort
|
|
970
|
+
});
|
|
971
|
+
const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
|
|
972
|
+
handleIpc(ipc);
|
|
973
|
+
const rpc = createRpc(ipc);
|
|
974
|
+
messagePort.start();
|
|
975
|
+
return rpc;
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
const create$4 = async ({
|
|
979
|
+
commandMap,
|
|
980
|
+
isMessagePortOpen,
|
|
981
|
+
send
|
|
982
|
+
}) => {
|
|
983
|
+
const {
|
|
984
|
+
port1,
|
|
985
|
+
port2
|
|
986
|
+
} = new MessageChannel();
|
|
987
|
+
await send(port1);
|
|
988
|
+
return create$5({
|
|
989
|
+
commandMap,
|
|
990
|
+
isMessagePortOpen,
|
|
991
|
+
messagePort: port2
|
|
992
|
+
});
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
const createSharedLazyRpc = factory => {
|
|
996
|
+
let rpcPromise;
|
|
997
|
+
const getOrCreate = () => {
|
|
998
|
+
if (!rpcPromise) {
|
|
999
|
+
rpcPromise = factory();
|
|
1000
|
+
}
|
|
1001
|
+
return rpcPromise;
|
|
1002
|
+
};
|
|
1003
|
+
return {
|
|
1004
|
+
async dispose() {
|
|
1005
|
+
const rpc = await getOrCreate();
|
|
1006
|
+
await rpc.dispose();
|
|
1007
|
+
},
|
|
1008
|
+
async invoke(method, ...params) {
|
|
1009
|
+
const rpc = await getOrCreate();
|
|
1010
|
+
return rpc.invoke(method, ...params);
|
|
1011
|
+
},
|
|
1012
|
+
async invokeAndTransfer(method, ...params) {
|
|
1013
|
+
const rpc = await getOrCreate();
|
|
1014
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
1015
|
+
},
|
|
1016
|
+
async send(method, ...params) {
|
|
1017
|
+
const rpc = await getOrCreate();
|
|
1018
|
+
rpc.send(method, ...params);
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
};
|
|
1022
|
+
|
|
1023
|
+
const create$3 = async ({
|
|
1024
|
+
commandMap,
|
|
1025
|
+
isMessagePortOpen,
|
|
1026
|
+
send
|
|
1027
|
+
}) => {
|
|
1028
|
+
return createSharedLazyRpc(() => {
|
|
1029
|
+
return create$4({
|
|
1030
|
+
commandMap,
|
|
1031
|
+
isMessagePortOpen,
|
|
1032
|
+
send
|
|
1033
|
+
});
|
|
1034
|
+
});
|
|
1035
|
+
};
|
|
1036
|
+
|
|
866
1037
|
const create$2 = async ({
|
|
867
1038
|
commandMap,
|
|
868
1039
|
messagePort
|
|
@@ -909,7 +1080,7 @@ const createMockRpc = ({
|
|
|
909
1080
|
};
|
|
910
1081
|
|
|
911
1082
|
const rpcs = Object.create(null);
|
|
912
|
-
const set$
|
|
1083
|
+
const set$2 = (id, rpc) => {
|
|
913
1084
|
rpcs[id] = rpc;
|
|
914
1085
|
};
|
|
915
1086
|
const get = id => {
|
|
@@ -942,7 +1113,7 @@ const create = rpcId => {
|
|
|
942
1113
|
const mockRpc = createMockRpc({
|
|
943
1114
|
commandMap
|
|
944
1115
|
});
|
|
945
|
-
set$
|
|
1116
|
+
set$2(rpcId, mockRpc);
|
|
946
1117
|
// @ts-ignore
|
|
947
1118
|
mockRpc[Symbol.dispose] = () => {
|
|
948
1119
|
remove(rpcId);
|
|
@@ -951,7 +1122,7 @@ const create = rpcId => {
|
|
|
951
1122
|
return mockRpc;
|
|
952
1123
|
},
|
|
953
1124
|
set(rpc) {
|
|
954
|
-
set$
|
|
1125
|
+
set$2(rpcId, rpc);
|
|
955
1126
|
}
|
|
956
1127
|
};
|
|
957
1128
|
};
|
|
@@ -960,14 +1131,32 @@ const OpenerWorker = 4561;
|
|
|
960
1131
|
const RendererWorker = 1;
|
|
961
1132
|
|
|
962
1133
|
const {
|
|
963
|
-
invoke
|
|
1134
|
+
invoke,
|
|
1135
|
+
set: set$1
|
|
1136
|
+
} = create(OpenerWorker);
|
|
964
1137
|
const openUrl = async (url, platform) => {
|
|
965
1138
|
return invoke('Open.openUrl', url, platform);
|
|
966
1139
|
};
|
|
967
1140
|
|
|
968
1141
|
const {
|
|
1142
|
+
invokeAndTransfer,
|
|
969
1143
|
set
|
|
970
1144
|
} = create(RendererWorker);
|
|
1145
|
+
const sendMessagePortToOpenerWorker$1 = async (port, rpcId) => {
|
|
1146
|
+
const command = 'HandleMessagePort.handleMessagePort';
|
|
1147
|
+
await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToOpenerWorker', port, command, rpcId);
|
|
1148
|
+
};
|
|
1149
|
+
|
|
1150
|
+
const sendMessagePortToOpenerWorker = async port => {
|
|
1151
|
+
await sendMessagePortToOpenerWorker$1(port, 0);
|
|
1152
|
+
};
|
|
1153
|
+
const initializeOpenerWorker = async () => {
|
|
1154
|
+
const rpc = await create$3({
|
|
1155
|
+
commandMap: {},
|
|
1156
|
+
send: sendMessagePortToOpenerWorker
|
|
1157
|
+
});
|
|
1158
|
+
set$1(rpc);
|
|
1159
|
+
};
|
|
971
1160
|
|
|
972
1161
|
const handleMessagePort = async port => {
|
|
973
1162
|
await create$2({
|
|
@@ -997,10 +1186,7 @@ const getLoggedOutBackendAuthState = (authErrorMessage = '') => {
|
|
|
997
1186
|
return {
|
|
998
1187
|
authAccessToken: '',
|
|
999
1188
|
authErrorMessage,
|
|
1000
|
-
|
|
1001
|
-
userState: 'loggedOut',
|
|
1002
|
-
userSubscriptionPlan: '',
|
|
1003
|
-
userUsedTokens: 0
|
|
1189
|
+
userState: 'loggedOut'
|
|
1004
1190
|
};
|
|
1005
1191
|
};
|
|
1006
1192
|
|
|
@@ -1193,16 +1379,18 @@ const isLoginResponse = value => {
|
|
|
1193
1379
|
return true;
|
|
1194
1380
|
};
|
|
1195
1381
|
|
|
1196
|
-
const handleClickLogin = async
|
|
1197
|
-
|
|
1382
|
+
const handleClickLogin = async options => {
|
|
1383
|
+
const {
|
|
1384
|
+
backendUrl,
|
|
1385
|
+
platform
|
|
1386
|
+
} = options;
|
|
1387
|
+
if (!backendUrl) {
|
|
1198
1388
|
return {
|
|
1199
|
-
...state,
|
|
1200
1389
|
authErrorMessage: 'Backend URL is missing.',
|
|
1201
1390
|
userState: 'loggedOut'
|
|
1202
1391
|
};
|
|
1203
1392
|
}
|
|
1204
1393
|
const signingInState = {
|
|
1205
|
-
...state,
|
|
1206
1394
|
authErrorMessage: '',
|
|
1207
1395
|
userState: 'loggingIn'
|
|
1208
1396
|
};
|
|
@@ -1211,24 +1399,21 @@ const handleClickLogin = async state => {
|
|
|
1211
1399
|
const response = await consumeNextLoginResponse();
|
|
1212
1400
|
if (!isLoginResponse(response)) {
|
|
1213
1401
|
return {
|
|
1214
|
-
...signingInState,
|
|
1215
1402
|
authErrorMessage: 'Backend returned an invalid login response.',
|
|
1216
1403
|
userState: 'loggedOut'
|
|
1217
1404
|
};
|
|
1218
1405
|
}
|
|
1219
1406
|
if (typeof response.error === 'string' && response.error) {
|
|
1220
1407
|
return {
|
|
1221
|
-
...signingInState,
|
|
1222
1408
|
authErrorMessage: response.error,
|
|
1223
1409
|
userState: 'loggedOut'
|
|
1224
1410
|
};
|
|
1225
1411
|
}
|
|
1226
1412
|
return getLoggedInState(signingInState, response);
|
|
1227
1413
|
}
|
|
1228
|
-
await openUrl(getBackendLoginUrl(
|
|
1229
|
-
const authState = await waitForBackendLogin(
|
|
1414
|
+
await openUrl(getBackendLoginUrl(backendUrl), platform);
|
|
1415
|
+
const authState = await waitForBackendLogin(backendUrl);
|
|
1230
1416
|
return {
|
|
1231
|
-
...signingInState,
|
|
1232
1417
|
...authState
|
|
1233
1418
|
};
|
|
1234
1419
|
} catch (error) {
|
|
@@ -1275,7 +1460,7 @@ const initializeRendererWorker = async () => {
|
|
|
1275
1460
|
};
|
|
1276
1461
|
|
|
1277
1462
|
const listen = async () => {
|
|
1278
|
-
await initializeRendererWorker();
|
|
1463
|
+
await Promise.all([initializeRendererWorker(), initializeOpenerWorker()]);
|
|
1279
1464
|
};
|
|
1280
1465
|
|
|
1281
1466
|
const main = async () => {
|