mirror of
https://github.com/bitwarden/browser
synced 2025-12-24 12:13:39 +00:00
Refactor environment service to emit a single observable. This required significant changes to how the environment service behaves and tackles much of the tech debt planned for it.
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { ReplaySubject } from "rxjs";
|
|
|
|
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
|
import {
|
|
Environment,
|
|
Region,
|
|
RegionConfig,
|
|
Urls,
|
|
} from "@bitwarden/common/platform/abstractions/environment.service";
|
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
|
import {
|
|
CloudEnvironment,
|
|
DefaultEnvironmentService,
|
|
SelfHostedEnvironment,
|
|
} from "@bitwarden/common/platform/services/default-environment.service";
|
|
import { StateProvider } from "@bitwarden/common/platform/state";
|
|
|
|
/**
|
|
* Web specific environment service. Ensures that the urls are set from the window location.
|
|
*/
|
|
export class WebEnvironmentService extends DefaultEnvironmentService {
|
|
constructor(
|
|
private win: Window,
|
|
stateProvider: StateProvider,
|
|
accountService: AccountService,
|
|
) {
|
|
super(stateProvider, accountService);
|
|
|
|
// The web vault always uses the current location as the base url
|
|
const urls = process.env.URLS as Urls;
|
|
urls.base ??= this.win.location.origin;
|
|
|
|
// Find the region
|
|
const domain = Utils.getDomain(this.win.location.href);
|
|
const region = this.availableRegions().find((r) => Utils.getDomain(r.urls.webVault) === domain);
|
|
|
|
let environment: Environment;
|
|
if (region) {
|
|
environment = new WebCloudEnvironment(region, urls);
|
|
} else {
|
|
environment = new SelfHostedEnvironment(urls);
|
|
}
|
|
|
|
// Override the environment observable with a replay subject
|
|
const subject = new ReplaySubject<Environment>(1);
|
|
subject.next(environment);
|
|
this.environment$ = subject.asObservable();
|
|
}
|
|
|
|
// Web cannot set environment
|
|
async setEnvironment(region: Region, urls?: Urls): Promise<Urls> {
|
|
return;
|
|
}
|
|
}
|
|
|
|
class WebCloudEnvironment extends CloudEnvironment {
|
|
constructor(config: RegionConfig, urls: Urls) {
|
|
super(config);
|
|
// We override the urls to avoid CORS issues
|
|
this.urls = urls;
|
|
}
|
|
}
|