Skip to content

Commit 88ee962

Browse files
committed
Calling invalidate credentials over comlink, so that worker informs tab's remote connector about invalidation.
1 parent 19b40db commit 88ee962

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

packages/common/src/client/sync/stream/AbstractRemote.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type BSONImplementation = typeof BSON;
1515

1616
export type RemoteConnector = {
1717
fetchCredentials: () => Promise<PowerSyncCredentials | null>;
18+
invalidateCredentials?: () => void;
1819
};
1920

2021
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
@@ -182,6 +183,7 @@ export abstract class AbstractRemote {
182183
*/
183184
invalidateCredentials() {
184185
this.credentials = null;
186+
this.connector.invalidateCredentials?.();
185187
}
186188

187189
getUserAgent() {
@@ -396,8 +398,7 @@ export abstract class AbstractRemote {
396398
syncQueueRequestSize, // The initial N amount
397399
{
398400
onError: (e) => {
399-
if (e.message.includes('PSYNC_S2101')) {
400-
this.logger.error('PSYNC_S2101 - 401 Unauthorized');
401+
if (e.message.includes('Authorization failed') || e.message.includes('401')) {
401402
this.invalidateCredentials();
402403
}
403404
// Don't log closed as an error

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,6 @@ The next upload iteration will be delayed.`);
687687

688688
await this.delayRetry();
689689
return;
690-
} else if (remaining_seconds < 30) {
691-
// Pre-emptively refresh the token
692-
await this.options.remote.prefetchCredentials();
693690
}
694691
this.triggerCrudUpload();
695692
} else {

packages/web/src/db/sync/SharedWebStreamingSyncImplementation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
3131
return Comlink.transfer(port, [port]);
3232
}
3333

34+
invalidateCredentials() {
35+
this.options.remote.invalidateCredentials();
36+
}
37+
3438
async fetchCredentials(): Promise<PowerSyncCredentials | null> {
3539
const credentials = await this.options.remote.getCredentials();
3640
if (credentials == null) {

packages/web/src/worker/sync/AbstractSharedSyncClientProvider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { PowerSyncCredentials, SyncStatusOptions } from '@powersync/common'
55
*/
66
export abstract class AbstractSharedSyncClientProvider {
77
abstract fetchCredentials(): Promise<PowerSyncCredentials | null>;
8+
abstract invalidateCredentials(): void;
89
abstract uploadCrud(): Promise<void>;
910
abstract statusChanged(status: SyncStatusOptions): void;
1011
abstract getDBWorkerPort(): Promise<MessagePort>;

packages/web/src/worker/sync/SharedSyncImplementation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ export class SharedSyncImplementation
308308
adapter: new SqliteBucketStorage(this.dbAdapter!, new Mutex(), this.logger),
309309
remote: new WebRemote(
310310
{
311+
invalidateCredentials: async () => {
312+
const lastPort = this.ports[this.ports.length - 1];
313+
try {
314+
this.logger.log('calling the last port client provider to invalidate credentials');
315+
lastPort.clientProvider.invalidateCredentials();
316+
} catch (ex) {
317+
this.logger.error('error invalidating credentials', ex);
318+
}
319+
},
311320
fetchCredentials: async () => {
312321
const lastPort = this.ports[this.ports.length - 1];
313322
return new Promise(async (resolve, reject) => {

0 commit comments

Comments
 (0)