@lvce-editor/auth-worker 3.5.1 → 3.7.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.
@@ -478,7 +478,10 @@ const constructError = (message, type, name) => {
478
478
  if (ErrorConstructor === Error) {
479
479
  const error = new Error(message);
480
480
  if (name && name !== 'VError') {
481
- error.name = name;
481
+ Object.defineProperty(error, 'name', {
482
+ configurable: true,
483
+ value: name
484
+ });
482
485
  }
483
486
  return error;
484
487
  }
@@ -495,8 +498,10 @@ const getCurrentStack = () => {
495
498
  const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
496
499
  return currentStack;
497
500
  };
498
- const getNewLineIndex = (string, startIndex = undefined) => {
499
- return string.indexOf(NewLine, startIndex);
501
+ const getNewLineIndex = (string, startIndex) => {
502
+ {
503
+ return string.indexOf(NewLine);
504
+ }
500
505
  };
501
506
  const getParentStack = error => {
502
507
  let parentStack = error.stack || error.data || error.message || '';
@@ -507,55 +512,91 @@ const getParentStack = error => {
507
512
  };
508
513
  const MethodNotFound = -32601;
509
514
  const Custom = -32001;
515
+ const setStack = (error, stack) => {
516
+ const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
517
+ if (descriptor) {
518
+ if (!descriptor.configurable && !descriptor.writable) {
519
+ return;
520
+ }
521
+ if (!descriptor.configurable && descriptor.writable) {
522
+ error.stack = stack;
523
+ return;
524
+ }
525
+ }
526
+ Object.defineProperty(error, 'stack', {
527
+ configurable: true,
528
+ value: stack,
529
+ writable: true
530
+ });
531
+ };
532
+ const restoreExistingError = (error, currentStack) => {
533
+ if (typeof error.stack === 'string') {
534
+ setStack(error, `${error.stack}${NewLine}${currentStack}`);
535
+ }
536
+ return error;
537
+ };
538
+ const restoreMethodNotFoundError = (error, currentStack) => {
539
+ const restoredError = new JsonRpcError(error.message);
540
+ const parentStack = getParentStack(error);
541
+ setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
542
+ return restoredError;
543
+ };
544
+ const restoreStackFromData = (restoredError, error, currentStack) => {
545
+ if (error.data.stack && error.data.type && error.message) {
546
+ setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
547
+ return;
548
+ }
549
+ if (error.data.stack) {
550
+ setStack(restoredError, error.data.stack);
551
+ }
552
+ };
553
+ const applyDataProperties = (restoredError, error) => {
554
+ restoreStackFromData(restoredError, error, getCurrentStack());
555
+ if (error.data.codeFrame) {
556
+ // @ts-ignore
557
+ restoredError.codeFrame = error.data.codeFrame;
558
+ }
559
+ if (error.data.code) {
560
+ // @ts-ignore
561
+ restoredError.code = error.data.code;
562
+ }
563
+ if (error.data.type) {
564
+ // @ts-ignore
565
+ restoredError.name = error.data.type;
566
+ }
567
+ };
568
+ const applyDirectProperties = (restoredError, error) => {
569
+ if (error.stack) {
570
+ const lowerStack = restoredError.stack || '';
571
+ const indexNewLine = getNewLineIndex(lowerStack);
572
+ const parentStack = getParentStack(error);
573
+ // @ts-ignore
574
+ setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
575
+ }
576
+ if (error.codeFrame) {
577
+ // @ts-ignore
578
+ restoredError.codeFrame = error.codeFrame;
579
+ }
580
+ };
581
+ const restoreMessageError = (error, _currentStack) => {
582
+ const restoredError = constructError(error.message, error.type, error.name);
583
+ if (error.data) {
584
+ applyDataProperties(restoredError, error);
585
+ } else {
586
+ applyDirectProperties(restoredError, error);
587
+ }
588
+ return restoredError;
589
+ };
510
590
  const restoreJsonRpcError = error => {
511
591
  const currentStack = getCurrentStack();
512
592
  if (error && error instanceof Error) {
513
- if (typeof error.stack === 'string') {
514
- error.stack = error.stack + NewLine + currentStack;
515
- }
516
- return error;
593
+ return restoreExistingError(error, currentStack);
517
594
  }
518
595
  if (error && error.code && error.code === MethodNotFound) {
519
- const restoredError = new JsonRpcError(error.message);
520
- const parentStack = getParentStack(error);
521
- restoredError.stack = parentStack + NewLine + currentStack;
522
- return restoredError;
596
+ return restoreMethodNotFoundError(error, currentStack);
523
597
  }
524
598
  if (error && error.message) {
525
- const restoredError = constructError(error.message, error.type, error.name);
526
- if (error.data) {
527
- if (error.data.stack && error.data.type && error.message) {
528
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
529
- } else if (error.data.stack) {
530
- restoredError.stack = error.data.stack;
531
- }
532
- if (error.data.codeFrame) {
533
- // @ts-ignore
534
- restoredError.codeFrame = error.data.codeFrame;
535
- }
536
- if (error.data.code) {
537
- // @ts-ignore
538
- restoredError.code = error.data.code;
539
- }
540
- if (error.data.type) {
541
- // @ts-ignore
542
- restoredError.name = error.data.type;
543
- }
544
- } else {
545
- if (error.stack) {
546
- const lowerStack = restoredError.stack || '';
547
- // @ts-ignore
548
- const indexNewLine = getNewLineIndex(lowerStack);
549
- const parentStack = getParentStack(error);
550
- // @ts-ignore
551
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
552
- }
553
- if (error.codeFrame) {
554
- // @ts-ignore
555
- restoredError.codeFrame = error.codeFrame;
556
- }
557
- }
558
- return restoredError;
599
+ return restoreMessageError(error);
559
600
  }
560
601
  if (typeof error === 'string') {
561
602
  return new Error(`JsonRpc Error: ${error}`);
@@ -968,7 +1009,7 @@ const createMockRpc = ({
968
1009
  };
969
1010
 
970
1011
  const rpcs = Object.create(null);
971
- const set$2 = (id, rpc) => {
1012
+ const set$3 = (id, rpc) => {
972
1013
  rpcs[id] = rpc;
973
1014
  };
974
1015
  const get = id => {
@@ -1001,7 +1042,7 @@ const create = rpcId => {
1001
1042
  const mockRpc = createMockRpc({
1002
1043
  commandMap
1003
1044
  });
1004
- set$2(rpcId, mockRpc);
1045
+ set$3(rpcId, mockRpc);
1005
1046
  // @ts-ignore
1006
1047
  mockRpc[Symbol.dispose] = () => {
1007
1048
  remove(rpcId);
@@ -1010,33 +1051,44 @@ const create = rpcId => {
1010
1051
  return mockRpc;
1011
1052
  },
1012
1053
  set(rpc) {
1013
- set$2(rpcId, rpc);
1054
+ set$3(rpcId, rpc);
1014
1055
  }
1015
1056
  };
1016
1057
  };
1017
1058
 
1018
1059
  const {
1019
- invoke: invoke$2,
1020
- set: set$1
1060
+ invoke: invoke$3,
1061
+ set: set$2
1021
1062
  } = create(6040);
1022
1063
 
1064
+ const Web = 1;
1023
1065
  const Electron = 2;
1024
1066
 
1025
1067
  const OpenerWorker = 4561;
1026
1068
  const RendererWorker = 1;
1069
+ const SharedProcess = 1492;
1027
1070
 
1028
1071
  const {
1029
- invoke: invoke$1,
1030
- set
1072
+ invoke: invoke$2,
1073
+ set: set$1
1031
1074
  } = create(OpenerWorker);
1032
1075
 
1033
1076
  const {
1034
- invoke,
1077
+ invoke: invoke$1,
1035
1078
  invokeAndTransfer} = create(RendererWorker);
1036
1079
  const sendMessagePortToOpenerWorker$1 = async (port, rpcId) => {
1037
1080
  const command = 'HandleMessagePort.handleMessagePort';
1038
1081
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToOpenerWorker', port, command, rpcId);
1039
1082
  };
1083
+ const sendMessagePortToSharedProcess$1 = async port => {
1084
+ const command = 'HandleElectronMessagePort.handleElectronMessagePort';
1085
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToSharedProcess', port, command, 0);
1086
+ };
1087
+
1088
+ const {
1089
+ invoke,
1090
+ set
1091
+ } = create(SharedProcess);
1040
1092
 
1041
1093
  const sendMessagePortToAuthProcess = async port => {
1042
1094
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToSharedProcess', port, 'HandleMessagePortForAuthProcess.handleMessagePortForAuthProcess');
@@ -1046,7 +1098,7 @@ const initializeAuthProcess = async () => {
1046
1098
  commandMap: {},
1047
1099
  send: sendMessagePortToAuthProcess
1048
1100
  });
1049
- set$1(rpc);
1101
+ set$2(rpc);
1050
1102
  };
1051
1103
 
1052
1104
  const sendMessagePortToOpenerWorker = async port => {
@@ -1057,7 +1109,7 @@ const initializeOpenerWorker = async () => {
1057
1109
  commandMap: {},
1058
1110
  send: sendMessagePortToOpenerWorker
1059
1111
  });
1060
- set(rpc);
1112
+ set$1(rpc);
1061
1113
  };
1062
1114
 
1063
1115
  const millisecondsPerSecond = 1000;
@@ -1075,19 +1127,42 @@ const isAccessTokenValid = (accessToken, expiresAt, now = Date.now()) => {
1075
1127
  return Number.isFinite(expiresAtNumber) && expiresAtNumber > now;
1076
1128
  };
1077
1129
 
1078
- const state$2 = {
1130
+ const state$3 = {
1079
1131
  backendUrl: ''
1080
1132
  };
1081
1133
  const getAuthBackendUrl = () => {
1082
- return state$2.backendUrl;
1134
+ return state$3.backendUrl;
1083
1135
  };
1084
1136
  const setAuthBackendUrl = backendUrl => {
1085
- state$2.backendUrl = backendUrl;
1137
+ state$3.backendUrl = backendUrl;
1138
+ };
1139
+
1140
+ const state$2 = {
1141
+ platform: Web
1142
+ };
1143
+ const getAuthPlatform = () => {
1144
+ return state$2.platform;
1145
+ };
1146
+ const setAuthPlatform = value => {
1147
+ state$2.platform = value;
1148
+ };
1149
+
1150
+ const authStorageId = 'lvce-editor.auth';
1151
+ const deleteSecret = async key => {
1152
+ await invoke('SecretStorage.delete', authStorageId, key);
1153
+ };
1154
+ const getSecret = async key => {
1155
+ const value = await invoke('SecretStorage.get', authStorageId, key);
1156
+ return typeof value === 'string' ? value : '';
1157
+ };
1158
+ const storeSecret = async (key, value) => {
1159
+ await invoke('SecretStorage.store', authStorageId, key, value);
1086
1160
  };
1087
1161
 
1088
1162
  const databaseName = 'auth-worker';
1089
1163
  const objectStoreName = 'auth';
1090
1164
  const memoryStorage = new Map();
1165
+ const secretKeys = new Set(['accessToken', 'refreshToken']);
1091
1166
  const state$1 = {
1092
1167
  databasePromise: undefined
1093
1168
  };
@@ -1142,7 +1217,7 @@ const getDatabase = async () => {
1142
1217
  }
1143
1218
  return state$1.databasePromise;
1144
1219
  };
1145
- const getPersistentAuthValue = async key => {
1220
+ const getIndexedDbValue = async key => {
1146
1221
  const database = await getDatabase();
1147
1222
  if (!database) {
1148
1223
  return memoryStorage.get(key) ?? '';
@@ -1152,7 +1227,7 @@ const getPersistentAuthValue = async key => {
1152
1227
  const value = await requestToPromise(objectStore.get(key));
1153
1228
  return typeof value === 'string' ? value : '';
1154
1229
  };
1155
- const setPersistentAuthValue = async (key, value) => {
1230
+ const setIndexedDbValue = async (key, value) => {
1156
1231
  memoryStorage.set(key, value);
1157
1232
  const database = await getDatabase();
1158
1233
  if (!database) {
@@ -1163,7 +1238,7 @@ const setPersistentAuthValue = async (key, value) => {
1163
1238
  objectStore.put(value, key);
1164
1239
  await transactionToPromise(transaction);
1165
1240
  };
1166
- const clearPersistentAuthValue = async key => {
1241
+ const clearIndexedDbValue = async key => {
1167
1242
  memoryStorage.delete(key);
1168
1243
  const database = await getDatabase();
1169
1244
  if (!database) {
@@ -1174,6 +1249,44 @@ const clearPersistentAuthValue = async key => {
1174
1249
  objectStore.delete(key);
1175
1250
  await transactionToPromise(transaction);
1176
1251
  };
1252
+ const shouldUseSecretStorage = key => {
1253
+ return getAuthPlatform() === Electron && secretKeys.has(key);
1254
+ };
1255
+ const getPersistentAuthValue = async key => {
1256
+ if (!shouldUseSecretStorage(key)) {
1257
+ return getIndexedDbValue(key);
1258
+ }
1259
+ const secret = await getSecret(key);
1260
+ if (secret) {
1261
+ return secret;
1262
+ }
1263
+ const legacyValue = await getIndexedDbValue(key);
1264
+ if (!legacyValue) {
1265
+ return '';
1266
+ }
1267
+ await storeSecret(key, legacyValue);
1268
+ await clearIndexedDbValue(key);
1269
+ return legacyValue;
1270
+ };
1271
+ const setPersistentAuthValue = async (key, value) => {
1272
+ if (!shouldUseSecretStorage(key)) {
1273
+ await setIndexedDbValue(key, value);
1274
+ return;
1275
+ }
1276
+ if (value) {
1277
+ await storeSecret(key, value);
1278
+ } else {
1279
+ await deleteSecret(key);
1280
+ }
1281
+ await clearIndexedDbValue(key);
1282
+ };
1283
+ const clearPersistentAuthValue = async key => {
1284
+ if (shouldUseSecretStorage(key)) {
1285
+ await Promise.all([deleteSecret(key), clearIndexedDbValue(key)]);
1286
+ return;
1287
+ }
1288
+ await clearIndexedDbValue(key);
1289
+ };
1177
1290
 
1178
1291
  const callbackUrlKey = 'oidcCallbackUrl';
1179
1292
  const clientIdKey = 'oidcClientId';
@@ -2073,7 +2186,7 @@ const handleMessagePort = async (port, rpcId) => {
2073
2186
  messagePort: port
2074
2187
  });
2075
2188
  if (rpcId) {
2076
- set$2(rpcId, rpc);
2189
+ set$3(rpcId, rpc);
2077
2190
  }
2078
2191
  };
2079
2192
 
@@ -2107,7 +2220,7 @@ const exchangeAuthorizationCode = async (backendUrl, clientId, code, redirectUri
2107
2220
 
2108
2221
  const getCurrentHref = async () => {
2109
2222
  try {
2110
- return await invoke('Layout.getHref');
2223
+ return await invoke$1('Layout.getHref');
2111
2224
  } catch {
2112
2225
  // ignore
2113
2226
  }
@@ -2337,9 +2450,13 @@ const getBackendUrl = options => {
2337
2450
  }
2338
2451
  return options.backendUrl || '';
2339
2452
  };
2453
+ const getPlatform = options => {
2454
+ return typeof options === 'number' ? options : options.platform ?? Web;
2455
+ };
2340
2456
  const initialize = async options => {
2341
2457
  const backendUrl = getBackendUrl(options);
2342
2458
  setAuthBackendUrl(backendUrl);
2459
+ setAuthPlatform(getPlatform(options));
2343
2460
  try {
2344
2461
  if (backendUrl) {
2345
2462
  const completedBrowserLogin = await completeBrowserOidcLogin(backendUrl);
@@ -2813,7 +2930,7 @@ const errorHtml = `<!doctype html>
2813
2930
  </html>`;
2814
2931
 
2815
2932
  const getElectronRedirectUri = async uid => {
2816
- const localOauthServerPort = await invoke$2('OAuthServer.create', String(uid), successHtml, errorHtml);
2933
+ const localOauthServerPort = await invoke$3('OAuthServer.create', String(uid), successHtml, errorHtml);
2817
2934
  return `http://localhost:${localOauthServerPort}/callback`;
2818
2935
  };
2819
2936
 
@@ -2916,7 +3033,7 @@ const hasAuthorizationCode = value => {
2916
3033
  return typeof value === 'string' && value.length > 0;
2917
3034
  };
2918
3035
  const getElectronAuthorizationCode = async uid => {
2919
- return invoke$2('OAuthServer.getCode', String(uid));
3036
+ return invoke$3('OAuthServer.getCode', String(uid));
2920
3037
  };
2921
3038
  const waitForElectronBackendLogin = async (backendUrl, uid, redirectUri, codeVerifier, timeoutMs = 30_000, pollIntervalMs = 1000, getAuthorizationCode = getElectronAuthorizationCode, exchangeAuthorizationCode = exchangeElectronAuthorizationCode) => {
2922
3039
  const deadline = Date.now() + timeoutMs;
@@ -2977,7 +3094,7 @@ const getInteractiveLoginResult = async (backendUrl, platform, signingInState) =
2977
3094
  state
2978
3095
  });
2979
3096
  }
2980
- await invoke$1('Open.openUrl', loginUrl, platform, authUseRedirect);
3097
+ await invoke$2('Open.openUrl', loginUrl, platform, authUseRedirect);
2981
3098
  if (platform !== Electron && authUseRedirect) {
2982
3099
  return signingInState;
2983
3100
  }
@@ -2995,6 +3112,7 @@ const handleClickLogin = async options => {
2995
3112
  backendUrl,
2996
3113
  platform
2997
3114
  } = options;
3115
+ setAuthPlatform(platform);
2998
3116
  if (!backendUrl) {
2999
3117
  return {
3000
3118
  authErrorMessage: 'Backend URL is missing.',
@@ -3057,8 +3175,19 @@ const initializeRendererWorker = async () => {
3057
3175
  });
3058
3176
  };
3059
3177
 
3178
+ const sendMessagePortToSharedProcess = async port => {
3179
+ await sendMessagePortToSharedProcess$1(port);
3180
+ };
3181
+ const initializeSharedProcess = async () => {
3182
+ const rpc = await create$2({
3183
+ commandMap: {},
3184
+ send: sendMessagePortToSharedProcess
3185
+ });
3186
+ set(rpc);
3187
+ };
3188
+
3060
3189
  const listen = async () => {
3061
- await Promise.all([initializeRendererWorker(), initializeOpenerWorker(), initializeAuthProcess()]);
3190
+ await Promise.all([initializeRendererWorker(), initializeOpenerWorker(), initializeAuthProcess(), initializeSharedProcess()]);
3062
3191
  };
3063
3192
 
3064
3193
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/auth-worker",
3
- "version": "3.5.1",
3
+ "version": "3.7.0",
4
4
  "description": "Auth Worker",
5
5
  "repository": {
6
6
  "type": "git",