Detect Proxy
http://stackoverflow.com/questions/36372611/how-to-test-if-an-object-is-a-proxy
by ycw7788
JavaScript
// ---- intercept.proxy.js
(function(global) {
'use strict';
var ns = {};
var original = global.Proxy;
// Store all objects that ... from `new Proxy()` and `Proxy.revocable().proxy`
// (i.e. we determine an arbitrary object is 'proxied' if the object exists in this set)
var proxies = new WeakSet();
// Proxy the original Proxy
var morphed = new original(original, {
// intercept `new Proxy(target, handler)`
construct: function() {
var proxy = Reflect.construct.apply(Reflect, arguments);
proxies.add(proxy);
return proxy;
},
// intercept `Proxy.revocable`
get: function(target, name) {
if (name === 'revocable') {
var revocable = Reflect.get.apply(Reflect, arguments);
return new original(revocable, {
apply: function() {
var object = Reflect.apply.apply(Reflect, arguments); // {proxy(), revoke()}
proxies.add(object.proxy);
// proxy `Proxy.recovable(target, handler).revoke` so that we can delete proxied object from the set.
object.revoke = new original(object.revoke, {
apply: function() {
proxies.delete(object.proxy);
return Reflect.apply.apply(Reflect, arguments);
}
});
return object;
}
});
}
return Reflect.get.apply(Reflect, arguments);
}
});
ns.proxy = {};
// Temproraily use the morphed version of Proxy
ns.proxy.morph = function() {
global.Proxy = morphed;
};
// Recover to original Proxy
ns.proxy.unmorph = function() {
global.Proxy = original;
};
// Determine if an arbitrary object is a "proxied object"
ns.proxy.isProxied = function(object) {
return proxies.has(object);
};
ns.proxy.isNotProxied = function(object) {
return !ns.proxy.isProxied(object);
};
// expose
global.ns = ns;
}(window));
// ---- our.fictional.system.js
(function(global) {
'use strict';
...