@lvce-editor/auth-worker 3.22.2 → 3.22.3

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.
@@ -1102,6 +1102,7 @@ const create = rpcId => {
1102
1102
  };
1103
1103
 
1104
1104
  const {
1105
+ dispose,
1105
1106
  invoke: invoke$3,
1106
1107
  set: set$2
1107
1108
  } = create(6040);
@@ -1248,22 +1249,22 @@ const getAuthBackendUrl = async backendUrl => {
1248
1249
  return invoke('Layout.getBackendUrl');
1249
1250
  };
1250
1251
 
1251
- const state$3 = {
1252
+ const state$4 = {
1252
1253
  platform: Web,
1253
1254
  useNativeAuth: false
1254
1255
  };
1255
1256
  const getAuthPlatform = () => {
1256
- return state$3.platform;
1257
+ return state$4.platform;
1257
1258
  };
1258
1259
  const setAuthPlatform = value => {
1259
- state$3.platform = value;
1260
+ state$4.platform = value;
1260
1261
  };
1261
1262
 
1262
1263
  // Native shells can use loopback OAuth without Electron's process or secret storage.
1263
1264
  const setUseNativeAuth = value => {
1264
- state$3.useNativeAuth = value;
1265
+ state$4.useNativeAuth = value;
1265
1266
  };
1266
- const getUseNativeAuth = () => state$3.useNativeAuth;
1267
+ const getUseNativeAuth = () => state$4.useNativeAuth;
1267
1268
 
1268
1269
  let USER_AGENT;
1269
1270
  if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
@@ -2435,15 +2436,15 @@ const databaseName = 'auth-worker';
2435
2436
  const objectStoreName = 'auth';
2436
2437
  const memoryStorage = new Map();
2437
2438
  const secretKeys = new Set(['accessToken', 'refreshToken', 'accountSessions']);
2438
- const state$2 = {
2439
+ const state$3 = {
2439
2440
  databasePromise: undefined
2440
2441
  };
2441
2442
  const getDatabase = async () => {
2442
2443
  if (typeof indexedDB === 'undefined') {
2443
2444
  return undefined;
2444
2445
  }
2445
- if (!state$2.databasePromise) {
2446
- state$2.databasePromise = openDB(databaseName, 1, {
2446
+ if (!state$3.databasePromise) {
2447
+ state$3.databasePromise = openDB(databaseName, 1, {
2447
2448
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
2448
2449
  upgrade(database) {
2449
2450
  if (!database.objectStoreNames.contains(objectStoreName)) {
@@ -2452,7 +2453,7 @@ const getDatabase = async () => {
2452
2453
  }
2453
2454
  });
2454
2455
  }
2455
- return state$2.databasePromise;
2456
+ return state$3.databasePromise;
2456
2457
  };
2457
2458
  const getIndexedDbValue = async key => {
2458
2459
  const database = await getDatabase();
@@ -2648,32 +2649,32 @@ const delay = async ms => {
2648
2649
  await new Promise(resolve => setTimeout(resolve, ms));
2649
2650
  };
2650
2651
 
2651
- const state$1 = {
2652
+ const state$2 = {
2652
2653
  nextLoginResponse: undefined,
2653
2654
  nextRefreshResponse: undefined
2654
2655
  };
2655
2656
  const setNextLoginResponse = response => {
2656
- state$1.nextLoginResponse = response;
2657
+ state$2.nextLoginResponse = response;
2657
2658
  };
2658
2659
  const setNextRefreshResponse = response => {
2659
- state$1.nextRefreshResponse = response;
2660
+ state$2.nextRefreshResponse = response;
2660
2661
  };
2661
2662
  const clear = () => {
2662
- state$1.nextLoginResponse = undefined;
2663
- state$1.nextRefreshResponse = undefined;
2663
+ state$2.nextLoginResponse = undefined;
2664
+ state$2.nextRefreshResponse = undefined;
2664
2665
  };
2665
2666
  const hasPendingMockLoginResponse = () => {
2666
- return !!state$1.nextLoginResponse;
2667
+ return !!state$2.nextLoginResponse;
2667
2668
  };
2668
2669
  const hasPendingMockRefreshResponse = () => {
2669
- return !!state$1.nextRefreshResponse;
2670
+ return !!state$2.nextRefreshResponse;
2670
2671
  };
2671
2672
  const consumeNextLoginResponse = async () => {
2672
- if (!state$1.nextLoginResponse) {
2673
+ if (!state$2.nextLoginResponse) {
2673
2674
  return undefined;
2674
2675
  }
2675
- const response = state$1.nextLoginResponse;
2676
- state$1.nextLoginResponse = undefined;
2676
+ const response = state$2.nextLoginResponse;
2677
+ state$2.nextLoginResponse = undefined;
2677
2678
  if (response.delay > 0) {
2678
2679
  await delay(response.delay);
2679
2680
  }
@@ -2683,11 +2684,11 @@ const consumeNextLoginResponse = async () => {
2683
2684
  return response.response;
2684
2685
  };
2685
2686
  const consumeNextRefreshResponse = async () => {
2686
- if (!state$1.nextRefreshResponse) {
2687
+ if (!state$2.nextRefreshResponse) {
2687
2688
  return undefined;
2688
2689
  }
2689
- const response = state$1.nextRefreshResponse;
2690
- state$1.nextRefreshResponse = undefined;
2690
+ const response = state$2.nextRefreshResponse;
2691
+ state$2.nextRefreshResponse = undefined;
2691
2692
  if (response.delay > 0) {
2692
2693
  await new Promise(resolve => setTimeout(resolve, response.delay));
2693
2694
  }
@@ -3302,6 +3303,9 @@ const waitForElectronBackendLogin = async (backendUrl, uid, redirectUri, codeVer
3302
3303
  return getLoggedOutBackendAuthState('Timed out waiting for backend login.');
3303
3304
  };
3304
3305
 
3306
+ const state$1 = {
3307
+ nativeLoginCount: 0
3308
+ };
3305
3309
  const getMockLoginResult = async (signingInState, persistResult) => {
3306
3310
  if (!hasPendingMockLoginResponse()) {
3307
3311
  return undefined;
@@ -3322,10 +3326,13 @@ const getMockLoginResult = async (signingInState, persistResult) => {
3322
3326
  return persistResult(getLoggedInState(signingInState, response));
3323
3327
  };
3324
3328
  const getInteractiveLoginResult = async (backendUrl, platform, signingInState, persistResult, selectAccount) => {
3325
- const nativeAuth = getUseNativeAuth();
3329
+ const nativeAuth = getUseNativeAuth() || platform === Electron;
3326
3330
  const transportPlatform = nativeAuth ? Electron : platform;
3327
3331
  const authUseRedirect = getAuthUseRedirect(transportPlatform);
3328
3332
  const uid = nativeAuth ? globalThis.crypto.randomUUID() : 0;
3333
+ if (nativeAuth) {
3334
+ state$1.nativeLoginCount++;
3335
+ }
3329
3336
  try {
3330
3337
  const {
3331
3338
  clientId,
@@ -3358,7 +3365,14 @@ const getInteractiveLoginResult = async (backendUrl, platform, signingInState, p
3358
3365
  });
3359
3366
  } finally {
3360
3367
  if (nativeAuth) {
3361
- await invoke$3('OAuthServer.dispose', String(uid));
3368
+ try {
3369
+ await invoke$3('OAuthServer.dispose', String(uid));
3370
+ } finally {
3371
+ state$1.nativeLoginCount--;
3372
+ if (state$1.nativeLoginCount === 0) {
3373
+ await dispose();
3374
+ }
3375
+ }
3362
3376
  }
3363
3377
  }
3364
3378
  };
@@ -3677,15 +3691,76 @@ const approveCloudflareMachine = (id, code, zoneId) => {
3677
3691
  });
3678
3692
  };
3679
3693
 
3680
- const sendMessagePortToAuthProcess = async port => {
3681
- await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToSharedProcess', port, 'HandleMessagePortForAuthProcess.handleMessagePortForAuthProcess');
3694
+ const connect = async () => {
3695
+ const {
3696
+ port1,
3697
+ port2
3698
+ } = new MessageChannel();
3699
+ const closed = Promise.withResolvers();
3700
+ const handleClose = () => closed.resolve(new Error('Auth process connection closed'));
3701
+ port2.addEventListener('close', handleClose, {
3702
+ once: true
3703
+ });
3704
+ const failOnClose = async () => {
3705
+ throw await closed.promise;
3706
+ };
3707
+ const raceClose = promise => Promise.race([promise, failOnClose()]);
3708
+ try {
3709
+ await raceClose(invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToSharedProcess', port1, 'HandleMessagePortForAuthProcess.handleMessagePortForAuthProcess'));
3710
+ const rpc = await create$4({
3711
+ commandMap: {},
3712
+ messagePort: port2
3713
+ });
3714
+ return {
3715
+ async dispose() {
3716
+ handleClose();
3717
+ port2.removeEventListener('close', handleClose);
3718
+ await rpc.dispose();
3719
+ },
3720
+ invoke: (method, ...params) => raceClose(rpc.invoke(method, ...params)),
3721
+ invokeAndTransfer: (method, ...params) => raceClose(rpc.invokeAndTransfer(method, ...params)),
3722
+ send: rpc.send
3723
+ };
3724
+ } catch (error) {
3725
+ port2.removeEventListener('close', handleClose);
3726
+ port1.close();
3727
+ port2.close();
3728
+ throw error;
3729
+ }
3730
+ };
3731
+ const createAuthProcessRpc = (create = connect) => {
3732
+ let pending;
3733
+ const get = () => {
3734
+ if (!pending) {
3735
+ pending = create();
3736
+ }
3737
+ return pending;
3738
+ };
3739
+ return {
3740
+ async dispose() {
3741
+ const previous = pending;
3742
+ // Detach before awaiting: a new login may connect while the old port closes.
3743
+ pending = undefined;
3744
+ if (previous) {
3745
+ const rpc = await previous;
3746
+ await rpc.dispose();
3747
+ }
3748
+ },
3749
+ async invoke(method, ...params) {
3750
+ const rpc = await get();
3751
+ return rpc.invoke(method, ...params);
3752
+ },
3753
+ async invokeAndTransfer(method, ...params) {
3754
+ const rpc = await get();
3755
+ return rpc.invokeAndTransfer(method, ...params);
3756
+ },
3757
+ send(method, ...params) {
3758
+ void get().then(rpc => rpc.send(method, ...params));
3759
+ }
3760
+ };
3682
3761
  };
3683
3762
  const initializeAuthProcess = async () => {
3684
- const rpc = await create$2({
3685
- commandMap: {},
3686
- send: sendMessagePortToAuthProcess
3687
- });
3688
- set$2(rpc);
3763
+ set$2(createAuthProcessRpc());
3689
3764
  };
3690
3765
 
3691
3766
  const sendMessagePortToMainProcess = async port => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/auth-worker",
3
- "version": "3.22.2",
3
+ "version": "3.22.3",
4
4
  "description": "Auth Worker",
5
5
  "repository": {
6
6
  "type": "git",