mirror of
https://github.com/bitwarden/browser
synced 2025-12-23 11:43:46 +00:00
Platform/pm 19/platform team file moves (#5460)
* Rename service-factory folder * Move cryptographic service factories * Move crypto models * Move crypto services * Move domain base class * Platform code owners * Move desktop log services * Move log files * Establish component library ownership * Move background listeners * Move background background * Move localization to Platform * Move browser alarms to Platform * Move browser state to Platform * Move CLI state to Platform * Move Desktop native concerns to Platform * Move flag and misc to Platform * Lint fixes * Move electron state to platform * Move web state to Platform * Move lib state to Platform * Fix broken tests * Rename interface to idiomatic TS * `npm run prettier` 🤖 * Resolve review feedback * Set platform as owners of web core and shared * Expand moved services * Fix test types --------- Co-authored-by: Hinton <hinton@users.noreply.github.com>
This commit is contained in:
96
libs/common/src/platform/services/config/config.service.ts
Normal file
96
libs/common/src/platform/services/config/config.service.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { Injectable, OnDestroy } from "@angular/core";
|
||||
import { BehaviorSubject, Subject, concatMap, from, takeUntil, timer } from "rxjs";
|
||||
|
||||
import { AuthService } from "../../../auth/abstractions/auth.service";
|
||||
import { AuthenticationStatus } from "../../../auth/enums/authentication-status";
|
||||
import { FeatureFlag } from "../../../enums/feature-flag.enum";
|
||||
import { ConfigApiServiceAbstraction } from "../../abstractions/config/config-api.service.abstraction";
|
||||
import { ConfigServiceAbstraction } from "../../abstractions/config/config.service.abstraction";
|
||||
import { ServerConfig } from "../../abstractions/config/server-config";
|
||||
import { EnvironmentService } from "../../abstractions/environment.service";
|
||||
import { StateService } from "../../abstractions/state.service";
|
||||
import { ServerConfigData } from "../../models/data/server-config.data";
|
||||
|
||||
@Injectable()
|
||||
export class ConfigService implements ConfigServiceAbstraction, OnDestroy {
|
||||
protected _serverConfig = new BehaviorSubject<ServerConfig | null>(null);
|
||||
serverConfig$ = this._serverConfig.asObservable();
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
constructor(
|
||||
private stateService: StateService,
|
||||
private configApiService: ConfigApiServiceAbstraction,
|
||||
private authService: AuthService,
|
||||
private environmentService: EnvironmentService
|
||||
) {
|
||||
// Re-fetch the server config every hour
|
||||
timer(0, 1000 * 3600)
|
||||
.pipe(concatMap(() => from(this.fetchServerConfig())))
|
||||
.subscribe((serverConfig) => {
|
||||
this._serverConfig.next(serverConfig);
|
||||
});
|
||||
|
||||
this.environmentService.urls.pipe(takeUntil(this.destroy$)).subscribe(() => {
|
||||
this.fetchServerConfig();
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
|
||||
async fetchServerConfig(): Promise<ServerConfig> {
|
||||
try {
|
||||
const response = await this.configApiService.get();
|
||||
|
||||
if (response != null) {
|
||||
const data = new ServerConfigData(response);
|
||||
const serverConfig = new ServerConfig(data);
|
||||
this._serverConfig.next(serverConfig);
|
||||
if ((await this.authService.getAuthStatus()) === AuthenticationStatus.LoggedOut) {
|
||||
return serverConfig;
|
||||
}
|
||||
await this.stateService.setServerConfig(data);
|
||||
}
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getFeatureFlagBool(key: FeatureFlag, defaultValue = false): Promise<boolean> {
|
||||
return await this.getFeatureFlag(key, defaultValue);
|
||||
}
|
||||
|
||||
async getFeatureFlagString(key: FeatureFlag, defaultValue = ""): Promise<string> {
|
||||
return await this.getFeatureFlag(key, defaultValue);
|
||||
}
|
||||
|
||||
async getFeatureFlagNumber(key: FeatureFlag, defaultValue = 0): Promise<number> {
|
||||
return await this.getFeatureFlag(key, defaultValue);
|
||||
}
|
||||
|
||||
private async getFeatureFlag<T>(key: FeatureFlag, defaultValue: T): Promise<T> {
|
||||
const serverConfig = await this.buildServerConfig();
|
||||
if (
|
||||
serverConfig == null ||
|
||||
serverConfig.featureStates == null ||
|
||||
serverConfig.featureStates[key] == null
|
||||
) {
|
||||
return defaultValue;
|
||||
}
|
||||
return serverConfig.featureStates[key] as T;
|
||||
}
|
||||
|
||||
private async buildServerConfig(): Promise<ServerConfig> {
|
||||
const data = await this.stateService.getServerConfig();
|
||||
const domain = data ? new ServerConfig(data) : this._serverConfig.getValue();
|
||||
|
||||
if (domain == null || !domain.isValid() || domain.expiresSoon()) {
|
||||
const value = await this.fetchServerConfig();
|
||||
return value ?? domain;
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user