1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-25 16:59:17 +01:00

utils global

This commit is contained in:
Kyle Spearrin 2018-05-14 23:41:12 -04:00
parent 29556c5d3b
commit 5850a590ce
4 changed files with 15 additions and 6 deletions

View File

@ -5,6 +5,7 @@ export class Utils {
static inited = false; static inited = false;
static isNode = false; static isNode = false;
static isBrowser = true; static isBrowser = true;
static global: NodeJS.Global | Window = null;
static init() { static init() {
if (Utils.inited) { if (Utils.inited) {
@ -14,6 +15,7 @@ export class Utils {
Utils.inited = true; Utils.inited = true;
Utils.isNode = typeof window === 'undefined'; Utils.isNode = typeof window === 'undefined';
Utils.isBrowser = !Utils.isNode; Utils.isBrowser = !Utils.isNode;
Utils.global = Utils.isNode ? global : window;
} }
static fromB64ToArray(str: string): Uint8Array { static fromB64ToArray(str: string): Uint8Array {

View File

@ -2,6 +2,8 @@ import { EncryptionType } from '../../enums/encryptionType';
import { CryptoService } from '../../abstractions/crypto.service'; import { CryptoService } from '../../abstractions/crypto.service';
import { Utils } from '../../misc/utils';
export class CipherString { export class CipherString {
encryptedString?: string; encryptedString?: string;
encryptionType?: EncryptionType; encryptionType?: EncryptionType;
@ -93,11 +95,11 @@ export class CipherString {
} }
let cryptoService: CryptoService; let cryptoService: CryptoService;
const containerService = (window as any).bitwardenContainerService; const containerService = (Utils.global as any).bitwardenContainerService;
if (containerService) { if (containerService) {
cryptoService = containerService.getCryptoService(); cryptoService = containerService.getCryptoService();
} else { } else {
throw new Error('window.bitwardenContainerService not initialized.'); throw new Error('global bitwardenContainerService not initialized.');
} }
try { try {

View File

@ -35,7 +35,7 @@ export class LoginUriView implements View {
get domain(): string { get domain(): string {
if (this._domain == null && this.uri != null) { if (this._domain == null && this.uri != null) {
const containerService = (window as any).bitwardenContainerService; const containerService = (Utils.global as any).bitwardenContainerService;
if (containerService) { if (containerService) {
const platformUtilsService: PlatformUtilsService = containerService.getPlatformUtilsService(); const platformUtilsService: PlatformUtilsService = containerService.getPlatformUtilsService();
this._domain = platformUtilsService.getDomain(this.uri); this._domain = platformUtilsService.getDomain(this.uri);
@ -43,7 +43,7 @@ export class LoginUriView implements View {
this._domain = null; this._domain = null;
} }
} else { } else {
throw new Error('window.bitwardenContainerService not initialized.'); throw new Error('global bitwardenContainerService not initialized.');
} }
} }

View File

@ -6,9 +6,14 @@ export class ContainerService {
private platformUtilsService: PlatformUtilsService) { private platformUtilsService: PlatformUtilsService) {
} }
// deprecated, use attachToGlobal instead
attachToWindow(win: any) { attachToWindow(win: any) {
if (!win.bitwardenContainerService) { this.attachToGlobal(win);
win.bitwardenContainerService = this; }
attachToGlobal(global: any) {
if (!global.bitwardenContainerService) {
global.bitwardenContainerService = this;
} }
} }