Skip to content

Open-source Xbox-inspired desktop launcher for Xodus on Linux

TypeScript

fix: scope retained launch access to its account and expiry

domsvoboda8aff1d536ff8
diff --git a/src/main/account-library.test.ts b/src/main/account-library.test.ts
index 053d918..ce2cb24 100644
--- a/src/main/account-library.test.ts
+++ b/src/main/account-library.test.ts
@@ -455,3 +455,40 @@ it('does not extend an entitlement that expires between access evaluation and re
let calls=0;const clock=vi.spyOn(Date,'now').mockImplementation(()=>++calls<3?now:end+1);
try{await expect(library.storeAccess()).rejects.toThrow('expired');}finally{clock.mockRestore();}
});
+
+it.each(['cached','pending','signed-out'] as const)('keeps a %s launch-access reader bound to its original Store identity',async mode=>{
+ let finishOld!:(value:string)=>void;
+ const reader=vi.fn(async()=>output([row(B)],'[email protected]'));
+ if(mode==='pending')reader.mockImplementationOnce(()=>new Promise<string>(resolve=>{finishOld=resolve;}));
+ else if(mode==='cached')reader.mockResolvedValueOnce(output([row(A)]));
+ const library=new AccountLibrary(async ids=>ids.map(product),reader);
+ if(mode!=='signed-out')library.select('[email protected]','/backend','US');
+ if(mode==='cached')await library.access();
+ const oldLaunch=library.startLaunchAccess();
+ const pending=mode==='pending'?oldLaunch(1_000):undefined;
+ library.select('[email protected]','/backend','US');await library.access();
+ if(mode==='pending')finishOld(output([row(A)]));
+ if(pending)expect(await pending).toEqual({});
+ expect(await oldLaunch(0)).toEqual({});
+ expect(await library.startLaunchAccess()(0)).toEqual({[B]:'owned'});
+});
+
+it.each(['backend','market','reset','same-name-reauth'] as const)('invalidates a captured launch-access reader after %s without suppressing the current identity',async change=>{
+ let fresh=false;
+ const library=new AccountLibrary(async ids=>ids.map(product),async(_binary,market)=>output([row(fresh?B:A)],'[email protected]',market));
+ library.select('[email protected]','/backend','US');await library.access();const oldLaunch=library.startLaunchAccess();
+ expect(await oldLaunch(0)).toEqual({[A]:'owned'});fresh=true;
+ if(change==='backend')library.select('[email protected]','/replacement-backend','US');
+ else if(change==='market')library.select('[email protected]','/backend','CZ');
+ else if(change==='reset')library.reset();
+ else {library.select('[email protected]','/backend','US');library.select('[email protected]','/backend','US');}
+ await library.access();expect(await oldLaunch(0)).toEqual({});expect(await library.startLaunchAccess()(0)).toEqual({[B]:'owned'});
+});
+
+it.each(['cache-ttl','license-expiry'] as const)('rechecks %s when a launch retains an already-resolved access reader',async expiry=>{
+ const now=Date.now(),endDate=new Date(now+1_000).toISOString();
+ const library=new AccountLibrary(async ids=>ids.map(product),async()=>output([row(A,expiry==='license-expiry'?{endDate}:{})]));
+ library.select('[email protected]','/backend','US');const read=library.startLaunchAccess();expect(await read(1_000)).toEqual({[A]:'owned'});
+ const clock=vi.spyOn(Date,'now').mockReturnValue(now+(expiry==='cache-ttl'?300_001:1_001));
+ try {expect(await read(0)).toEqual({});}finally{clock.mockRestore();}
+});
diff --git a/src/main/account-library.ts b/src/main/account-library.ts
index b7fa572..0609362 100644
--- a/src/main/account-library.ts
+++ b/src/main/account-library.ts
@@ -170,13 +170,23 @@ export class AccountLibrary {
* leaving the load to finish for the next launch. Never throws.
*/
startLaunchAccess(): (waitMs: number) => Promise<AccountLibraryPage['accessByProductId']> {
- if (!this.identity || (this.cached && this.cached.until > Date.now())) return async () => this.peekAccess();
- const loaded = this.access().then(result => result.accessByProductId, () => undefined);
+ const identity = this.identity, generation = this.generation;
+ const current = () => identity === this.identity && generation === this.generation;
+ const cachedAccess = () => current() ? this.peekAccess() : {};
+ // A launch reader belongs to this selection, including the signed-out
+ // selection. Never adopt another account's cache after a reset or switch.
+ if (!identity || (this.cached && this.cached.until > Date.now())) return async () => cachedAccess();
+ const loaded = this.access().then(() => undefined, () => undefined);
return async (waitMs) => {
+ if (!current()) return {};
let timer: NodeJS.Timeout | undefined;
const timeout = new Promise<undefined>(resolve => { timer = setTimeout(resolve, waitMs, undefined); });
- try { return await Promise.race([loaded, timeout]) ?? this.peekAccess(); }
- finally { clearTimeout(timer); }
+ try {
+ await Promise.race([loaded, timeout]);
+ // Preparation can outlive the cache or a license. Re-evaluate access
+ // now instead of retaining the map returned by the original load.
+ return cachedAccess();
+ } finally { clearTimeout(timer); }
};
}
async access(refresh = false): Promise<{ checkedAt: string; accessByProductId: AccountLibraryPage['accessByProductId'] }> {