Remove usage/detection of NativeScript (#566)

* Remove usage of NativeScript

* npm prettier run

* Removing type from Utils.global
This commit is contained in:
Daniel James Smith 2021-12-22 19:46:25 +01:00 committed by GitHub
parent 3d7b427b0e
commit d68c1dafaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 16 deletions

View File

@ -7,7 +7,6 @@ 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;
@ -30,18 +29,13 @@ export class Utils {
(process as any).release != null && (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.isAppleMobileBrowser = Utils.isBrowser && this.isAppleMobile(window); Utils.isAppleMobileBrowser = Utils.isBrowser && this.isAppleMobile(window);
Utils.global = Utils.isNativeScript Utils.global = Utils.isNode && !Utils.isBrowser ? global : window;
? global
: Utils.isNode && !Utils.isBrowser
? global
: window;
} }
static fromB64ToArray(str: string): Uint8Array { static fromB64ToArray(str: string): Uint8Array {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
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);
@ -58,7 +52,7 @@ export class Utils {
} }
static fromHexToArray(str: string): Uint8Array { static fromHexToArray(str: string): Uint8Array {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
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);
@ -70,7 +64,7 @@ export class Utils {
} }
static fromUtf8ToArray(str: string): Uint8Array { static fromUtf8ToArray(str: string): Uint8Array {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
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));
@ -91,7 +85,7 @@ export class Utils {
} }
static fromBufferToB64(buffer: ArrayBuffer): string { static fromBufferToB64(buffer: ArrayBuffer): string {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
return Buffer.from(buffer).toString("base64"); return Buffer.from(buffer).toString("base64");
} else { } else {
let binary = ""; let binary = "";
@ -112,7 +106,7 @@ export class Utils {
} }
static fromBufferToUtf8(buffer: ArrayBuffer): string { static fromBufferToUtf8(buffer: ArrayBuffer): string {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
return Buffer.from(buffer).toString("utf8"); return Buffer.from(buffer).toString("utf8");
} else { } else {
const bytes = new Uint8Array(buffer); const bytes = new Uint8Array(buffer);
@ -127,7 +121,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 || Utils.isNativeScript) { if (Utils.isNode) {
return Buffer.from(buffer).toString("hex"); return Buffer.from(buffer).toString("hex");
} else { } else {
const bytes = new Uint8Array(buffer); const bytes = new Uint8Array(buffer);
@ -160,7 +154,7 @@ export class Utils {
} }
static fromUtf8ToB64(utfStr: string): string { static fromUtf8ToB64(utfStr: string): string {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
return Buffer.from(utfStr, "utf8").toString("base64"); return Buffer.from(utfStr, "utf8").toString("base64");
} else { } else {
return decodeURIComponent(escape(window.btoa(utfStr))); return decodeURIComponent(escape(window.btoa(utfStr)));
@ -172,7 +166,7 @@ export class Utils {
} }
static fromB64ToUtf8(b64Str: string): string { static fromB64ToUtf8(b64Str: string): string {
if (Utils.isNode || Utils.isNativeScript) { if (Utils.isNode) {
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)));
@ -387,7 +381,7 @@ export class Utils {
private static getUrlObject(uriString: string): URL { private static getUrlObject(uriString: string): URL {
try { try {
if (nodeURL != null) { if (nodeURL != null) {
return nodeURL.URL ? new nodeURL.URL(uriString) : nodeURL.parse(uriString); return new nodeURL.URL(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) {