diff --git a/src/misc/utils.ts b/src/misc/utils.ts index 0ff14d3dbd..77535331df 100644 --- a/src/misc/utils.ts +++ b/src/misc/utils.ts @@ -5,6 +5,7 @@ export class Utils { static inited = false; static isNode = false; static isBrowser = true; + static global: NodeJS.Global | Window = null; static init() { if (Utils.inited) { @@ -14,6 +15,7 @@ export class Utils { Utils.inited = true; Utils.isNode = typeof window === 'undefined'; Utils.isBrowser = !Utils.isNode; + Utils.global = Utils.isNode ? global : window; } static fromB64ToArray(str: string): Uint8Array { diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index 93ff9e78da..6d49d1724e 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -2,6 +2,8 @@ import { EncryptionType } from '../../enums/encryptionType'; import { CryptoService } from '../../abstractions/crypto.service'; +import { Utils } from '../../misc/utils'; + export class CipherString { encryptedString?: string; encryptionType?: EncryptionType; @@ -93,11 +95,11 @@ export class CipherString { } let cryptoService: CryptoService; - const containerService = (window as any).bitwardenContainerService; + const containerService = (Utils.global as any).bitwardenContainerService; if (containerService) { cryptoService = containerService.getCryptoService(); } else { - throw new Error('window.bitwardenContainerService not initialized.'); + throw new Error('global bitwardenContainerService not initialized.'); } try { diff --git a/src/models/view/loginUriView.ts b/src/models/view/loginUriView.ts index 0c78bb50c8..476067959a 100644 --- a/src/models/view/loginUriView.ts +++ b/src/models/view/loginUriView.ts @@ -35,7 +35,7 @@ export class LoginUriView implements View { get domain(): string { if (this._domain == null && this.uri != null) { - const containerService = (window as any).bitwardenContainerService; + const containerService = (Utils.global as any).bitwardenContainerService; if (containerService) { const platformUtilsService: PlatformUtilsService = containerService.getPlatformUtilsService(); this._domain = platformUtilsService.getDomain(this.uri); @@ -43,7 +43,7 @@ export class LoginUriView implements View { this._domain = null; } } else { - throw new Error('window.bitwardenContainerService not initialized.'); + throw new Error('global bitwardenContainerService not initialized.'); } } diff --git a/src/services/container.service.ts b/src/services/container.service.ts index fa9a6628ce..10e3f6390e 100644 --- a/src/services/container.service.ts +++ b/src/services/container.service.ts @@ -6,9 +6,14 @@ export class ContainerService { private platformUtilsService: PlatformUtilsService) { } + // deprecated, use attachToGlobal instead attachToWindow(win: any) { - if (!win.bitwardenContainerService) { - win.bitwardenContainerService = this; + this.attachToGlobal(win); + } + + attachToGlobal(global: any) { + if (!global.bitwardenContainerService) { + global.bitwardenContainerService = this; } }