Skip to content

Commit 7f57469

Browse files
authored
fix: [#1546] Use globalThis instead of global to make Happy DOM work in other runtimes such as Cloudflare workers (#1546)
`global` is a legacy, node.js-only global variable. I think `globalThis` this should be used instead, which will make global-registrator portable on other runtimes like Cloudflare workers.
1 parent 759b4fb commit 7f57469

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

packages/global-registrator/src/GlobalRegistrator.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class GlobalRegistrator {
2828
throw new Error('Failed to register. Happy DOM has already been globally registered.');
2929
}
3030

31-
const window = new GlobalWindow({ ...options, console: global.console });
31+
const window = new GlobalWindow({ ...options, console: globalThis.console });
3232

3333
this.registered = {};
3434

@@ -38,7 +38,7 @@ export default class GlobalRegistrator {
3838
for (const key of Object.keys(propertyDescriptors)) {
3939
if (!IGNORE_LIST.includes(key)) {
4040
const windowPropertyDescriptor = propertyDescriptors[key];
41-
const globalPropertyDescriptor = Object.getOwnPropertyDescriptor(global, key);
41+
const globalPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key);
4242

4343
if (
4444
globalPropertyDescriptor?.value === undefined ||
@@ -48,11 +48,11 @@ export default class GlobalRegistrator {
4848

4949
// If the property is the window object, replace it with the global object
5050
if (windowPropertyDescriptor.value === window) {
51-
window[key] = global;
52-
windowPropertyDescriptor.value = global;
51+
window[key] = globalThis;
52+
windowPropertyDescriptor.value = globalThis;
5353
}
5454

55-
Object.defineProperty(global, key, {
55+
Object.defineProperty(globalThis, key, {
5656
...windowPropertyDescriptor,
5757
configurable: true
5858
});
@@ -69,18 +69,18 @@ export default class GlobalRegistrator {
6969

7070
// If the property is the window object, replace it with the global object
7171
if (propertyDescriptor.value === window) {
72-
window[key] = global;
73-
propertyDescriptor.value = global;
72+
window[key] = globalThis;
73+
propertyDescriptor.value = globalThis;
7474
}
7575

76-
Object.defineProperty(global, key, {
76+
Object.defineProperty(globalThis, key, {
7777
...propertyDescriptor,
7878
configurable: true
7979
});
8080
}
8181

8282
// Set owner window on document to global
83-
global.document[PropertySymbol.defaultView] = global;
83+
globalThis.document[PropertySymbol.defaultView] = globalThis;
8484
}
8585

8686
/**
@@ -93,13 +93,13 @@ export default class GlobalRegistrator {
9393
);
9494
}
9595

96-
const happyDOM = global.happyDOM;
96+
const happyDOM = globalThis.happyDOM;
9797

9898
for (const key of Object.keys(this.registered)) {
9999
if (this.registered[key] !== null) {
100-
Object.defineProperty(global, key, this.registered[key]);
100+
Object.defineProperty(globalThis, key, this.registered[key]);
101101
} else {
102-
delete global[key];
102+
delete globalThis[key];
103103
}
104104
}
105105

0 commit comments

Comments
 (0)