-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: self/window/global share state with globalThis (#1256)
* feat: self/window/global share state with globalThis * chore: fix infinite loop in --no-threads * chore: add cleanup function to test-utils
- Loading branch information
1 parent
34e177f
commit fbd7974
Showing
8 changed files
with
235 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,131 @@ | ||
import { KEYS } from './jsdom-keys' | ||
|
||
const allowRewrite = new Set([ | ||
const allowRewrite = [ | ||
'Event', | ||
'EventTarget', | ||
]) | ||
] | ||
|
||
const skipKeys = [ | ||
'window', | ||
'self', | ||
] | ||
|
||
export function getWindowKeys(global: any, win: any) { | ||
const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win)) | ||
.filter((k) => { | ||
if (k.startsWith('_')) | ||
if (k.startsWith('_') || skipKeys.includes(k)) | ||
return false | ||
if (k in global) | ||
return allowRewrite.has(k) | ||
return allowRewrite.includes(k) | ||
|
||
return true | ||
})) | ||
|
||
return keys | ||
} | ||
|
||
export function populateGlobal(global: any, win: any) { | ||
const keys = getWindowKeys(global, win) | ||
|
||
const overrideObject = new Map<string | symbol, any>() | ||
for (const key of keys) { | ||
Object.defineProperty(global, key, { | ||
get() { | ||
if (overrideObject.has(key)) | ||
return overrideObject.get(key) | ||
return win[key] | ||
}, | ||
set(v) { | ||
overrideObject.set(key, v) | ||
}, | ||
configurable: true, | ||
}) | ||
} | ||
|
||
const globalKeys = new Set<string | symbol>(['window', 'self', 'GLOBAL', 'global']) | ||
|
||
globalKeys.forEach((key) => { | ||
if (!win[key]) | ||
return | ||
|
||
const proxy = new Proxy(win[key], { | ||
get(target, p, receiver) { | ||
if (overrideObject.has(p)) | ||
return overrideObject.get(p) | ||
return Reflect.get(target, p, receiver) | ||
}, | ||
set(target, p, value, receiver) { | ||
try { | ||
// if property is defined with configurable: false, | ||
// this will throw an error, but `self.prop = value` should not throw | ||
// this matches browser behaviour where it silently ignores the error | ||
// and returns previously defined value, which is a hell for debugging | ||
Object.defineProperty(global, p, { | ||
get: () => overrideObject.get(p), | ||
set: value => overrideObject.set(p, value), | ||
configurable: true, | ||
}) | ||
overrideObject.set(p, value) | ||
Reflect.set(target, p, value, receiver) | ||
} | ||
catch { | ||
// ignore | ||
} | ||
return true | ||
}, | ||
deleteProperty(target, p) { | ||
Reflect.deleteProperty(global, p) | ||
overrideObject.delete(p) | ||
return Reflect.deleteProperty(target, p) | ||
}, | ||
defineProperty(target, p, attributes) { | ||
if (attributes.writable && 'value' in attributes) { | ||
// skip - already covered by "set" | ||
} | ||
else if (attributes.get) { | ||
overrideObject.delete(p) | ||
Reflect.defineProperty(global, p, attributes) | ||
} | ||
return Reflect.defineProperty(target, p, attributes) | ||
}, | ||
}) | ||
|
||
Object.defineProperty(global, key, { | ||
get() { | ||
return proxy | ||
}, | ||
configurable: true, | ||
}) | ||
}) | ||
|
||
global.globalThis = new Proxy(global.globalThis, { | ||
set(target, key, value, receiver) { | ||
overrideObject.set(key, value) | ||
return Reflect.set(target, key, value, receiver) | ||
}, | ||
deleteProperty(target, key) { | ||
overrideObject.delete(key) | ||
return Reflect.deleteProperty(target, key) | ||
}, | ||
defineProperty(target, p, attributes) { | ||
if (attributes.writable && 'value' in attributes) { | ||
// skip - already covered by "set" | ||
} | ||
else if (attributes.get && !globalKeys.has(p)) { | ||
globalKeys.forEach((key) => { | ||
if (win[key]) | ||
Object.defineProperty(win[key], p, attributes) | ||
}) | ||
} | ||
return Reflect.defineProperty(target, p, attributes) | ||
}, | ||
}) | ||
|
||
skipKeys.forEach(k => keys.add(k)) | ||
|
||
return { | ||
keys, | ||
skipKeys, | ||
allowRewrite, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @vitest-environment happy-dom | ||
*/ | ||
|
||
/* eslint-disable vars-on-top */ | ||
|
||
import { expect, it } from 'vitest' | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var __property: unknown | ||
} | ||
|
||
it('defined on self/window are defined on global', () => { | ||
expect(self).toBeDefined() | ||
expect(window).toBeDefined() | ||
|
||
expect(self.__property).not.toBeDefined() | ||
expect(window.__property).not.toBeDefined() | ||
expect(globalThis.__property).not.toBeDefined() | ||
|
||
globalThis.__property = 'defined_value' | ||
|
||
expect(__property).toBe('defined_value') | ||
expect(self.__property).toBe('defined_value') | ||
expect(window.__property).toBe('defined_value') | ||
expect(globalThis.__property).toBe('defined_value') | ||
|
||
self.__property = 'test_value' | ||
|
||
expect(__property).toBe('test_value') | ||
expect(self.__property).toBe('test_value') | ||
expect(window.__property).toBe('test_value') | ||
expect(globalThis.__property).toBe('test_value') | ||
|
||
window.__property = 'new_value' | ||
|
||
expect(__property).toBe('new_value') | ||
expect(self.__property).toBe('new_value') | ||
expect(window.__property).toBe('new_value') | ||
expect(globalThis.__property).toBe('new_value') | ||
|
||
globalThis.__property = 'global_value' | ||
|
||
expect(__property).toBe('global_value') | ||
expect(self.__property).toBe('global_value') | ||
expect(window.__property).toBe('global_value') | ||
expect(globalThis.__property).toBe('global_value') | ||
|
||
const obj = {} | ||
|
||
self.__property = obj | ||
|
||
expect(self.__property).toBe(obj) | ||
expect(window.__property).toBe(obj) | ||
expect(globalThis.__property).toBe(obj) | ||
}) | ||
|
||
it('usage with defineProperty', () => { | ||
Object.defineProperty(self, '__property', { | ||
get: () => 'self_property', | ||
configurable: true, | ||
}) | ||
|
||
expect(__property).toBe('self_property') | ||
expect(self.__property).toBe('self_property') | ||
expect(globalThis.__property).toBe('self_property') | ||
expect(window.__property).toBe('self_property') | ||
|
||
Object.defineProperty(window, '__property', { | ||
get: () => 'window_property', | ||
configurable: true, | ||
}) | ||
|
||
expect(__property).toBe('window_property') | ||
expect(self.__property).toBe('window_property') | ||
expect(globalThis.__property).toBe('window_property') | ||
expect(window.__property).toBe('window_property') | ||
|
||
Object.defineProperty(globalThis, '__property', { | ||
get: () => 'global_property', | ||
configurable: true, | ||
}) | ||
|
||
expect(__property).toBe('global_property') | ||
expect(self.__property).toBe('global_property') | ||
expect(globalThis.__property).toBe('global_property') | ||
expect(window.__property).toBe('global_property') | ||
}) |
This file was deleted.
Oops, something went wrong.