mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-24 11:01:17 -08:00
fix: clear stale sw on account changes
This commit is contained in:
1 parent
65529bb4c2
commit
85876b0513
2 files changed
+66
-7
No files matched your search
@@ -13,19 +13,25 @@ describe('service worker media auth recovery', () => {
|
||||
let swTestHooks: SwTestHooks;
|
||||
let clients: Map<string, Client>;
|
||||
let addEventListener: ReturnType<typeof vi.fn>;
|
||||
let persistedSessions: string | undefined;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
clients = new Map();
|
||||
addEventListener = vi.fn();
|
||||
persistedSessions = undefined;
|
||||
vi.stubGlobal('self', {
|
||||
__WB_MANIFEST: [],
|
||||
addEventListener,
|
||||
caches: {
|
||||
open: vi.fn(async () => ({
|
||||
delete: vi.fn(async () => true),
|
||||
match: vi.fn(async () => undefined),
|
||||
put: vi.fn(async () => undefined),
|
||||
match: vi.fn(async () =>
|
||||
persistedSessions ? new Response(persistedSessions) : undefined
|
||||
),
|
||||
put: vi.fn(async (_key: string, response: Response) => {
|
||||
persistedSessions = await response.text();
|
||||
}),
|
||||
})),
|
||||
},
|
||||
clients: {
|
||||
@@ -76,6 +82,52 @@ describe('service worker media auth recovery', () => {
|
||||
expect(fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('removes a stale persisted account when a client switches users', async () => {
|
||||
await swTestHooks.setSession(
|
||||
'client-a',
|
||||
'alice-token',
|
||||
'https://matrix.example.org',
|
||||
'@alice:example.org'
|
||||
);
|
||||
await swTestHooks.setSession(
|
||||
'client-a',
|
||||
'bob-token',
|
||||
'https://matrix.example.org',
|
||||
'@bob:example.org'
|
||||
);
|
||||
|
||||
expect(Object.keys(JSON.parse(persistedSessions ?? '{}'))).toEqual(['@bob:example.org']);
|
||||
});
|
||||
|
||||
it('keeps a persisted account until its final client logs out', async () => {
|
||||
await swTestHooks.setSession(
|
||||
'alice-a',
|
||||
'alice-token',
|
||||
'https://matrix.example.org',
|
||||
'@alice:example.org'
|
||||
);
|
||||
await swTestHooks.setSession(
|
||||
'alice-b',
|
||||
'alice-token',
|
||||
'https://matrix.example.org',
|
||||
'@alice:example.org'
|
||||
);
|
||||
await swTestHooks.setSession(
|
||||
'alice-a',
|
||||
'bob-token',
|
||||
'https://matrix.example.org',
|
||||
'@bob:example.org'
|
||||
);
|
||||
|
||||
expect(Object.keys(JSON.parse(persistedSessions ?? '{}')).toSorted()).toEqual([
|
||||
'@alice:example.org',
|
||||
'@bob:example.org',
|
||||
]);
|
||||
|
||||
await swTestHooks.setSession('alice-b', undefined, undefined);
|
||||
expect(Object.keys(JSON.parse(persistedSessions ?? '{}'))).toEqual(['@bob:example.org']);
|
||||
});
|
||||
|
||||
it('shares recovery and preserves each Range header on retry', async () => {
|
||||
const client = {
|
||||
id: 'client-a',
|
||||
|
||||
@@ -167,7 +167,8 @@ function setSession(
|
||||
baseUrl: unknown,
|
||||
userId?: unknown
|
||||
): Promise<void> {
|
||||
let persistence: Promise<void>;
|
||||
const previous = sessions.get(clientId);
|
||||
const persistence: Promise<void>[] = [];
|
||||
if (typeof accessToken === 'string' && typeof baseUrl === 'string') {
|
||||
const info: SessionInfo = {
|
||||
accessToken,
|
||||
@@ -176,13 +177,19 @@ function setSession(
|
||||
};
|
||||
sessions.set(clientId, info);
|
||||
console.debug('[SW] setSession: stored', clientId, baseUrl);
|
||||
persistence = info.userId ? updatePersistedSession(info.userId, info) : Promise.resolve();
|
||||
if (info.userId) persistence.push(updatePersistedSession(info.userId, info));
|
||||
} else {
|
||||
// Logout or invalid session
|
||||
const previous = sessions.get(clientId);
|
||||
sessions.delete(clientId);
|
||||
console.debug('[SW] setSession: removed', clientId);
|
||||
persistence = previous?.userId ? updatePersistedSession(previous.userId) : Promise.resolve();
|
||||
}
|
||||
|
||||
if (
|
||||
previous?.userId &&
|
||||
previous.userId !== sessions.get(clientId)?.userId &&
|
||||
![...sessions.values()].some((session) => session.userId === previous.userId)
|
||||
) {
|
||||
persistence.push(updatePersistedSession(previous.userId));
|
||||
}
|
||||
|
||||
const pending = pendingSessionRequests.get(clientId);
|
||||
@@ -191,7 +198,7 @@ function setSession(
|
||||
pendingSessionRequests.delete(clientId);
|
||||
}
|
||||
|
||||
return persistence;
|
||||
return Promise.all(persistence).then(() => undefined);
|
||||
}
|
||||
|
||||
function requestSession(client: Client): Promise<SessionInfo | undefined> {
|
||||
|
||||
Reference in new issue
Block a user