-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox-global.js
79 lines (75 loc) · 2.17 KB
/
sandbox-global.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
@license https://github.com/t2ym/reportage/blob/master/LICENSE.md
Copyright (c) 2023 Tetsuya Mori <[email protected]>. All rights reserved.
*/
// Provide a wrapper sandbox object so that mocha can run without contaminating the global scope of the browser
const _global = globalThis;
const sandbox = Object.create(_global);
let _onerror = null; // for mocha-es2018.js, onerror is the only global getter/setter property that has to be confined in the sandbox
let _wrappedOnError = null;
Object.defineProperty(sandbox, 'onerror', {
configurable: true,
enumerable: true,
get() {
return _onerror;
},
set(value) {
if (_onerror) {
_global.removeEventListener('error', _wrappedOnError);
}
/* istanbul ignore else */
if (value) {
_onerror = value;
_wrappedOnError = (errorEvent) => {
const { message, filename, lineno, colno, error } = errorEvent;
return _onerror(message, filename, lineno, colno, error);
};
_global.addEventListener('error', _wrappedOnError);
}
return _onerror;
},
});
const _globalThis = new Proxy(_global, {
get: function(target, prop, receiver) {
if (Object.hasOwn(sandbox, prop)) {
//console.log('Proxy(sandbox).get property ', prop);
return Reflect.get(sandbox, prop, target);
}
else {
//console.log('Proxy(globalThis).get property ', prop);
return Reflect.get(target, prop);
}
},
set(obj, prop, value) {
// TODO: properly handle _global getter/setter other than onerror
//console.log('Proxy(sandbox).set property ', prop, value);
return Reflect.set(sandbox, prop, value);
}
});
// return the Proxy object for global object properties
[ 'self', 'window', 'globalThis', 'frames', 'top', 'parent' ].forEach(prop => {
/* istanbul ignore else */
if (_global === _global[prop]) {
Object.defineProperty(sandbox, prop, { configurable: true, value: _globalThis, writable: false });
}
});
export { _globalThis, sandbox };
/* for bdd, sandbox object contains these properties
Mocha
mocha
before
after
beforeEach
afterEach
run
context
describe
xcontext
xdescribe
specify
it
xspecify
xit
onerror
Suite from scenarist
*/