From 66251e08557566944e5e290c8c559249be3e61e6 Mon Sep 17 00:00:00 2001 From: "Damon (Toal-Rossi) Vestervand" Date: Mon, 2 Oct 2023 14:04:05 +0200 Subject: [PATCH] Use globalThis instead of global (#3763) Switches use of `global` to `globalThis`, which is better supported when building with modern build tools like Vite. Refs #2903 Signed-off-by: Damon Vestervand Signed-off-by: Damon --- spec/olm-loader.ts | 2 +- src/browser-index.ts | 8 ++++---- src/crypto/crypto.ts | 6 +++--- src/crypto/index.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/olm-loader.ts b/spec/olm-loader.ts index 29817176b..ced175811 100644 --- a/spec/olm-loader.ts +++ b/spec/olm-loader.ts @@ -20,7 +20,7 @@ import { logger } from "../src/logger"; // try to load the olm library. try { // eslint-disable-next-line @typescript-eslint/no-var-requires - global.Olm = require("@matrix-org/olm"); + globalThis.Olm = require("@matrix-org/olm"); logger.log("loaded libolm"); } catch (e) { logger.warn("unable to run crypto tests: libolm not available", e); diff --git a/src/browser-index.ts b/src/browser-index.ts index 200b2a32d..6089424d4 100644 --- a/src/browser-index.ts +++ b/src/browser-index.ts @@ -24,15 +24,15 @@ declare global { /* eslint-enable no-var */ } -if (global.__js_sdk_entrypoint) { +if (globalThis.__js_sdk_entrypoint) { throw new Error("Multiple matrix-js-sdk entrypoints detected!"); } -global.__js_sdk_entrypoint = true; +globalThis.__js_sdk_entrypoint = true; // just *accessing* indexedDB throws an exception in firefox with indexeddb disabled. let indexedDB: IDBFactory | undefined; try { - indexedDB = global.indexedDB; + indexedDB = globalThis.indexedDB; } catch (e) {} // if our browser (appears to) support indexeddb, use an indexeddb crypto store. @@ -44,4 +44,4 @@ if (indexedDB) { // It's awkward, but required. export * from "./matrix"; export default matrixcs; // keep export for browserify package deps -global.matrixcs = matrixcs; +globalThis.matrixcs = matrixcs; diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index 704754f0b..c78970708 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -16,9 +16,9 @@ limitations under the License. import { logger } from "../logger"; -export let crypto = global.window?.crypto; -export let subtleCrypto = global.window?.crypto?.subtle ?? global.window?.crypto?.webkitSubtle; -export let TextEncoder = global.window?.TextEncoder; +export let crypto = globalThis.window?.crypto; +export let subtleCrypto = globalThis.window?.crypto?.subtle ?? global.window?.crypto?.webkitSubtle; +export let TextEncoder = globalThis.window?.TextEncoder; /* eslint-disable @typescript-eslint/no-var-requires */ if (!crypto) { diff --git a/src/crypto/index.ts b/src/crypto/index.ts index d49b2e0a3..a3aea892a 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -134,7 +134,7 @@ export const verificationMethods = { export type VerificationMethod = keyof typeof verificationMethods | string; export function isCryptoAvailable(): boolean { - return Boolean(global.Olm); + return Boolean(globalThis.Olm); } // minimum time between attempting to unwedge an Olm session, if we succeeded