mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-08 19:18:02 +01:00
add nativescript support to utils
This commit is contained in:
parent
e240085351
commit
46f9e17056
@ -1,14 +1,15 @@
|
|||||||
import { I18nService } from '../abstractions/i18n.service';
|
import { I18nService } from '../abstractions/i18n.service';
|
||||||
|
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
const nodeURL = typeof window === 'undefined' ? require('url').URL : null;
|
const nodeURL = typeof window === 'undefined' ? require('url') : null;
|
||||||
|
|
||||||
export class Utils {
|
export class Utils {
|
||||||
static inited = false;
|
static inited = false;
|
||||||
|
static isNativeScript = false;
|
||||||
static isNode = false;
|
static isNode = false;
|
||||||
static isBrowser = true;
|
static isBrowser = true;
|
||||||
static isMobileBrowser = false;
|
static isMobileBrowser = false;
|
||||||
static global: NodeJS.Global | Window = null;
|
static global: any = null;
|
||||||
|
|
||||||
static init() {
|
static init() {
|
||||||
if (Utils.inited) {
|
if (Utils.inited) {
|
||||||
@ -19,12 +20,13 @@ export class Utils {
|
|||||||
Utils.isNode = typeof process !== 'undefined' && (process as any).release != null &&
|
Utils.isNode = typeof process !== 'undefined' && (process as any).release != null &&
|
||||||
(process as any).release.name === 'node';
|
(process as any).release.name === 'node';
|
||||||
Utils.isBrowser = typeof window !== 'undefined';
|
Utils.isBrowser = typeof window !== 'undefined';
|
||||||
|
Utils.isNativeScript = !Utils.isNode && !Utils.isBrowser;
|
||||||
Utils.isMobileBrowser = Utils.isBrowser && this.isMobile(window);
|
Utils.isMobileBrowser = Utils.isBrowser && this.isMobile(window);
|
||||||
Utils.global = Utils.isNode && !Utils.isBrowser ? global : window;
|
Utils.global = Utils.isNativeScript ? new Object() : (Utils.isNode && !Utils.isBrowser ? global : window);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromB64ToArray(str: string): Uint8Array {
|
static fromB64ToArray(str: string): Uint8Array {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return new Uint8Array(Buffer.from(str, 'base64'));
|
return new Uint8Array(Buffer.from(str, 'base64'));
|
||||||
} else {
|
} else {
|
||||||
const binaryString = window.atob(str);
|
const binaryString = window.atob(str);
|
||||||
@ -37,7 +39,7 @@ export class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static fromHexToArray(str: string): Uint8Array {
|
static fromHexToArray(str: string): Uint8Array {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return new Uint8Array(Buffer.from(str, 'hex'));
|
return new Uint8Array(Buffer.from(str, 'hex'));
|
||||||
} else {
|
} else {
|
||||||
const bytes = new Uint8Array(str.length / 2);
|
const bytes = new Uint8Array(str.length / 2);
|
||||||
@ -49,7 +51,7 @@ export class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static fromUtf8ToArray(str: string): Uint8Array {
|
static fromUtf8ToArray(str: string): Uint8Array {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return new Uint8Array(Buffer.from(str, 'utf8'));
|
return new Uint8Array(Buffer.from(str, 'utf8'));
|
||||||
} else {
|
} else {
|
||||||
const strUtf8 = unescape(encodeURIComponent(str));
|
const strUtf8 = unescape(encodeURIComponent(str));
|
||||||
@ -70,7 +72,7 @@ export class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static fromBufferToB64(buffer: ArrayBuffer): string {
|
static fromBufferToB64(buffer: ArrayBuffer): string {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return Buffer.from(buffer).toString('base64');
|
return Buffer.from(buffer).toString('base64');
|
||||||
} else {
|
} else {
|
||||||
let binary = '';
|
let binary = '';
|
||||||
@ -83,7 +85,7 @@ export class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static fromBufferToUtf8(buffer: ArrayBuffer): string {
|
static fromBufferToUtf8(buffer: ArrayBuffer): string {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return Buffer.from(buffer).toString('utf8');
|
return Buffer.from(buffer).toString('utf8');
|
||||||
} else {
|
} else {
|
||||||
const bytes = new Uint8Array(buffer);
|
const bytes = new Uint8Array(buffer);
|
||||||
@ -98,7 +100,7 @@ export class Utils {
|
|||||||
|
|
||||||
// ref: https://stackoverflow.com/a/40031979/1090359
|
// ref: https://stackoverflow.com/a/40031979/1090359
|
||||||
static fromBufferToHex(buffer: ArrayBuffer): string {
|
static fromBufferToHex(buffer: ArrayBuffer): string {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return Buffer.from(buffer).toString('hex');
|
return Buffer.from(buffer).toString('hex');
|
||||||
} else {
|
} else {
|
||||||
const bytes = new Uint8Array(buffer);
|
const bytes = new Uint8Array(buffer);
|
||||||
@ -125,7 +127,7 @@ export class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static fromB64ToUtf8(b64Str: string): string {
|
static fromB64ToUtf8(b64Str: string): string {
|
||||||
if (Utils.isNode) {
|
if (Utils.isNode || Utils.isNativeScript) {
|
||||||
return Buffer.from(b64Str, 'base64').toString('utf8');
|
return Buffer.from(b64Str, 'base64').toString('utf8');
|
||||||
} else {
|
} else {
|
||||||
return decodeURIComponent(escape(window.atob(b64Str)));
|
return decodeURIComponent(escape(window.atob(b64Str)));
|
||||||
@ -225,7 +227,7 @@ export class Utils {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (nodeURL != null) {
|
if (nodeURL != null) {
|
||||||
return new nodeURL(uriString);
|
return nodeURL.URL ? new nodeURL.URL(uriString) : nodeURL.parse(uriString);
|
||||||
} else if (typeof URL === 'function') {
|
} else if (typeof URL === 'function') {
|
||||||
return new URL(uriString);
|
return new URL(uriString);
|
||||||
} else if (window != null) {
|
} else if (window != null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user