Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronizer improvements #493

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-rocks-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@monokle/synchronizer": patch
---

Imporved error handling
5 changes: 5 additions & 0 deletions .changeset/gold-badgers-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@monokle/synchronizer": minor
---

Added option to force refresh token
11 changes: 5 additions & 6 deletions packages/synchronizer/src/handlers/storageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class StorageHandler<TData> {
return this.readStoreData(this.getStoreDataFilePath(fileName));
}

async emptyStoreData(fileName: string): Promise<boolean> {
async emptyStoreData(fileName: string): Promise<void> {
return this.writeStoreData(this.getStoreDataFilePath(fileName), '');
}

Expand All @@ -24,8 +24,8 @@ export abstract class StorageHandler<TData> {
const configDoc = new Document();
configDoc.contents = data as any;

const result = await this.writeStoreData(configPath, configDoc.toString());
return result ? configPath : undefined;
await this.writeStoreData(configPath, configDoc.toString());
return configPath;
}

getStoreDataFilePath(fileName: string): string {
Expand Down Expand Up @@ -66,9 +66,8 @@ export abstract class StorageHandler<TData> {
try {
await mkdirp(dir);
await writeFile(file, data);
return true;
} catch (err) {
return false;
} catch (err: any) {
throw new Error(`Failed to write configuration to '${file}' with error: ${err.message} and data: ${data}`,);
}
}
}
10 changes: 7 additions & 3 deletions packages/synchronizer/src/handlers/storageHandlerPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class StorageHandlerPolicy extends StorageHandler<StoragePolicyFormat> {
super(storageFolderPath);
}

async setStoreData(data: StoragePolicyFormat, fileName: string, comment?: string): Promise<string | undefined> {
async setStoreData(data: StoragePolicyFormat, fileName: string, comment?: string): Promise<string> {
const configPath = this.getStoreDataFilePath(fileName);
const configDoc = new Document();
configDoc.contents = data as any;
Expand All @@ -20,7 +20,11 @@ export class StorageHandlerPolicy extends StorageHandler<StoragePolicyFormat> {
configDoc.commentBefore = comment;
}

const result = await this.writeStoreData(configPath, configDoc.toString());
return result ? configPath : undefined;
try {
await this.writeStoreData(configPath, configDoc.toString());
return configPath;
} catch (err: any) {
throw new Error(`Failed to write configuration to '${configPath}' with error: ${err.message}`);
}
}
}
18 changes: 9 additions & 9 deletions packages/synchronizer/src/utils/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ export class Authenticator extends EventEmitter {
}

async logout() {
const success = await this._storageHandler.emptyStoreData();
try {
await this._storageHandler.emptyStoreData();

if (!success) {
throw new Error('Failed to logout.');
}

this._user = new User(null);
this._user = new User(null);

this.emit('logout');
this.emit('logout');
} catch (err: any) {
throw new Error(`Failed to logout with error: ${err.message}`);
}
}

async refreshToken() {
async refreshToken(force = false) {
const authData = this._user.data?.auth;
const tokenData = authData?.token;

Expand All @@ -92,7 +92,7 @@ export class Authenticator extends EventEmitter {

const expiresAtDateMs = new Date(tokenSetData.expires_at * 1000);
const diffMinutes = (expiresAtDateMs.getTime() - new Date().getTime()) / 1000 / 60;
if (diffMinutes < 5) {
if (diffMinutes < 5 || force) {
const newTokenData = await this._deviceFlowHandler.refreshAuthFlow(tokenSetData.refresh_token);
return this.setUserData(newTokenData);
}
Expand Down
Loading