Skip to content

Commit

Permalink
Use globalThis instead of global (#3763)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Damon <[email protected]>
  • Loading branch information
orbiteleven authored Oct 2, 2023
1 parent ff53557 commit 66251e0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion spec/olm-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/browser-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
6 changes: 3 additions & 3 deletions src/crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 66251e0

Please sign in to comment.