@@ -23,6 +23,13 @@ import {
2323 mergeAuthProfileStores ,
2424 mergeOAuthFileIntoStore ,
2525} from "./persisted.js" ;
26+ import {
27+ clearRuntimeAuthProfileStoreSnapshots as clearRuntimeAuthProfileStoreSnapshotsImpl ,
28+ getRuntimeAuthProfileStoreSnapshot ,
29+ hasRuntimeAuthProfileStoreSnapshot ,
30+ replaceRuntimeAuthProfileStoreSnapshots as replaceRuntimeAuthProfileStoreSnapshotsImpl ,
31+ setRuntimeAuthProfileStoreSnapshot ,
32+ } from "./runtime-snapshots.js" ;
2633import { savePersistedAuthProfileState } from "./state.js" ;
2734import type { AuthProfileStore } from "./types.js" ;
2835
@@ -37,7 +44,6 @@ type SaveAuthProfileStoreOptions = {
3744 syncExternalCli ?: boolean ;
3845} ;
3946
40- const runtimeAuthStoreSnapshots = new Map < string , AuthProfileStore > ( ) ;
4147const loadedAuthStoreCache = new Map <
4248 string ,
4349 {
@@ -48,72 +54,36 @@ const loadedAuthStoreCache = new Map<
4854 }
4955> ( ) ;
5056
51- function resolveRuntimeStoreKey ( agentDir ?: string ) : string {
52- return resolveAuthStorePath ( agentDir ) ;
53- }
54-
5557function cloneAuthProfileStore ( store : AuthProfileStore ) : AuthProfileStore {
5658 return structuredClone ( store ) ;
5759}
5860
5961function resolveRuntimeAuthProfileStore ( agentDir ?: string ) : AuthProfileStore | null {
60- if ( runtimeAuthStoreSnapshots . size === 0 ) {
61- return null ;
62- }
63-
64- const mainKey = resolveRuntimeStoreKey ( undefined ) ;
65- const requestedKey = resolveRuntimeStoreKey ( agentDir ) ;
66- const mainStore = runtimeAuthStoreSnapshots . get ( mainKey ) ;
67- const requestedStore = runtimeAuthStoreSnapshots . get ( requestedKey ) ;
62+ const mainKey = resolveAuthStorePath ( undefined ) ;
63+ const requestedKey = resolveAuthStorePath ( agentDir ) ;
64+ const mainStore = getRuntimeAuthProfileStoreSnapshot ( undefined ) ;
65+ const requestedStore = getRuntimeAuthProfileStoreSnapshot ( agentDir ) ;
6866
6967 if ( ! agentDir || requestedKey === mainKey ) {
7068 if ( ! mainStore ) {
7169 return null ;
7270 }
73- return cloneAuthProfileStore ( mainStore ) ;
71+ return mainStore ;
7472 }
7573
7674 if ( mainStore && requestedStore ) {
77- return mergeAuthProfileStores (
78- cloneAuthProfileStore ( mainStore ) ,
79- cloneAuthProfileStore ( requestedStore ) ,
80- ) ;
75+ return mergeAuthProfileStores ( mainStore , requestedStore ) ;
8176 }
8277 if ( requestedStore ) {
83- return cloneAuthProfileStore ( requestedStore ) ;
78+ return requestedStore ;
8479 }
8580 if ( mainStore ) {
86- return cloneAuthProfileStore ( mainStore ) ;
81+ return mainStore ;
8782 }
8883
8984 return null ;
9085}
9186
92- function hasStoredAuthProfileFiles ( agentDir ?: string ) : boolean {
93- return (
94- fs . existsSync ( resolveAuthStorePath ( agentDir ) ) ||
95- fs . existsSync ( resolveAuthStatePath ( agentDir ) ) ||
96- fs . existsSync ( resolveLegacyAuthStorePath ( agentDir ) )
97- ) ;
98- }
99-
100- export function replaceRuntimeAuthProfileStoreSnapshots (
101- entries : Array < { agentDir ?: string ; store : AuthProfileStore } > ,
102- ) : void {
103- runtimeAuthStoreSnapshots . clear ( ) ;
104- for ( const entry of entries ) {
105- runtimeAuthStoreSnapshots . set (
106- resolveRuntimeStoreKey ( entry . agentDir ) ,
107- cloneAuthProfileStore ( entry . store ) ,
108- ) ;
109- }
110- }
111-
112- export function clearRuntimeAuthProfileStoreSnapshots ( ) : void {
113- runtimeAuthStoreSnapshots . clear ( ) ;
114- loadedAuthStoreCache . clear ( ) ;
115- }
116-
11787function readAuthStoreMtimeMs ( authPath : string ) : number | null {
11888 try {
11989 return fs . statSync ( authPath ) . mtimeMs ;
@@ -387,23 +357,17 @@ export function ensureAuthProfileStoreForLocalUpdate(agentDir?: string): AuthPro
387357 return mergeAuthProfileStores ( mainStore , store ) ;
388358}
389359
390- export function hasAnyAuthProfileStoreSource ( agentDir ?: string ) : boolean {
391- const runtimeStore = resolveRuntimeAuthProfileStore ( agentDir ) ;
392- if ( runtimeStore && Object . keys ( runtimeStore . profiles ) . length > 0 ) {
393- return true ;
394- }
395-
396- if ( hasStoredAuthProfileFiles ( agentDir ) ) {
397- return true ;
398- }
360+ export { hasAnyAuthProfileStoreSource } from "./source-check.js" ;
399361
400- const authPath = resolveAuthStorePath ( agentDir ) ;
401- const mainAuthPath = resolveAuthStorePath ( ) ;
402- if ( agentDir && authPath !== mainAuthPath && hasStoredAuthProfileFiles ( undefined ) ) {
403- return true ;
404- }
362+ export function replaceRuntimeAuthProfileStoreSnapshots (
363+ entries : Array < { agentDir ?: string ; store : AuthProfileStore } > ,
364+ ) : void {
365+ replaceRuntimeAuthProfileStoreSnapshotsImpl ( entries ) ;
366+ }
405367
406- return false ;
368+ export function clearRuntimeAuthProfileStoreSnapshots ( ) : void {
369+ clearRuntimeAuthProfileStoreSnapshotsImpl ( ) ;
370+ loadedAuthStoreCache . clear ( ) ;
407371}
408372
409373export function saveAuthProfileStore (
@@ -413,7 +377,6 @@ export function saveAuthProfileStore(
413377) : void {
414378 const authPath = resolveAuthStorePath ( agentDir ) ;
415379 const statePath = resolveAuthStatePath ( agentDir ) ;
416- const runtimeKey = resolveRuntimeStoreKey ( agentDir ) ;
417380 const payload = buildPersistedAuthProfileSecretsStore ( store , ( { profileId, credential } ) => {
418381 if ( credential . type !== "oauth" ) {
419382 return true ;
@@ -440,7 +403,7 @@ export function saveAuthProfileStore(
440403 stateMtimeMs : readAuthStoreMtimeMs ( statePath ) ,
441404 store : runtimeStore ,
442405 } ) ;
443- if ( runtimeAuthStoreSnapshots . has ( runtimeKey ) ) {
444- runtimeAuthStoreSnapshots . set ( runtimeKey , cloneAuthProfileStore ( runtimeStore ) ) ;
406+ if ( hasRuntimeAuthProfileStoreSnapshot ( agentDir ) ) {
407+ setRuntimeAuthProfileStoreSnapshot ( runtimeStore , agentDir ) ;
445408 }
446409}
0 commit comments