harmony-proxy
Advanced tools
Comparing version 0.0.2 to 0.1.0
111
index.js
@@ -1,3 +0,108 @@ | ||
module.exports = process.env.ESPROXY_COV | ||
? require('./lib-cov') | ||
: require('./lib'); | ||
/* vim: set shiftwidth=2 tabstop=2 noexpandtab textwidth=80 wrap : */ | ||
/*global Proxy:true */ | ||
"use strict"; | ||
// We need to have at least old-style Proxies | ||
if (typeof Proxy === 'undefined') | ||
throw new Error('Native proxies not enabled. use `node --harmony` or `node --harmony-proxies`'); | ||
// Check if we actually have new-style Proxies | ||
module.exports = typeof Proxy.create !== 'function' ? Proxy : ProxyShim; | ||
function ProxyShim(target, handler) { | ||
// this code is mostly copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Old_Proxy_API | ||
var oldHandler = { | ||
// Fundamental traps | ||
getOwnPropertyDescriptor: function (name) { | ||
if (handler.getOwnPropertyDescriptor) | ||
return handler.getOwnPropertyDescriptor(target, name); | ||
var desc = Object.getOwnPropertyDescriptor(target, name); | ||
// a trapping proxy's properties must always be configurable | ||
if (desc !== undefined) { desc.configurable = true; } | ||
return desc; | ||
}, | ||
getPropertyDescriptor: function (name) { | ||
var proto = target; | ||
var desc; | ||
do { | ||
desc = Object.getOwnPropertyDescriptor(proto, name); | ||
} while (desc === undefined && (proto = Object.getPrototypeOf(proto))); | ||
// a trapping proxy's properties must always be configurable | ||
if (desc !== undefined) { desc.configurable = true; } | ||
return desc; | ||
}, | ||
getOwnPropertyNames: function (name) { | ||
if (handler.getOwnPropertyNames) | ||
return handler.getOwnPropertyNames(target, name); | ||
return Object.getOwnPropertyNames(target); | ||
}, | ||
getPropertyNames: function () { | ||
var names = []; | ||
var proto = target; | ||
do { | ||
names = names.concat(Object.getOwnPropertyNames(proto)); | ||
} while ((proto = Object.getPrototypeOf(proto)) && proto !== Object.prototype); | ||
return names; | ||
}, | ||
defineProperty: function (name, desc) { | ||
if (handler.defineProperty) | ||
return handler.defineProperty(target, name, desc); | ||
return Object.defineProperty(target, name, desc); | ||
}, | ||
delete: function (name) { | ||
if (handler.deleteProperty) | ||
return handler.deleteProperty(target, name); | ||
return delete target[name]; | ||
}, | ||
/* TODO: freeze, seal, preventExtensions | ||
fix: function() { | ||
if (Object.isFrozen(obj)) { | ||
return Object.getOwnPropertyNames(obj).map(function(name) { | ||
return Object.getOwnPropertyDescriptor(obj, name); | ||
}); | ||
} | ||
// As long as obj is not frozen, the proxy won't allow itself to be fixed | ||
return undefined; // will cause a TypeError to be thrown | ||
},*/ | ||
}; | ||
// derived traps | ||
if (handler.has) | ||
oldHandler.has = function (name) { | ||
return handler.has(target, name); | ||
}; | ||
if (handler.hasOwn) | ||
oldHandler.hasOwn = function (name) { | ||
return handler.hasOwn(target, name); | ||
}; | ||
if (handler.get) | ||
oldHandler.get = function (receiver, name) { | ||
return handler.get(target, name, receiver); | ||
}; | ||
if (handler.set) | ||
oldHandler.set = function (receiver, name, val) { | ||
return handler.set(target, name, val, receiver); | ||
}; | ||
if (handler.enumerate) | ||
oldHandler.enumerate = function () { | ||
return handler.enumerate(target); | ||
}; | ||
if (handler.keys) | ||
oldHandler.keys = function() { | ||
return handler.keys(target); | ||
}; | ||
if (typeof target !== 'function') | ||
return Proxy.create(oldHandler, Object.getPrototypeOf(target)); | ||
return Proxy.createFunction( | ||
oldHandler, | ||
function () { | ||
if (handler.apply) | ||
return handler.apply(target, this, Array.prototype.slice.call(arguments)); | ||
return target.apply(this, Array.prototype.slice.call(arguments)); | ||
}, | ||
handler.construct && function () { | ||
return handler.construct(target, Array.prototype.slice.call(arguments)); | ||
} | ||
); | ||
} | ||
{ | ||
"name": "harmony-proxy", | ||
"description": "Small shim providing `new Proxy(target, handler)`", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"keywords": ["harmony", "proxy", "trap", "es6"], | ||
@@ -13,13 +13,13 @@ "main": "./index", | ||
"devDependencies": { | ||
"istanbul": "*", | ||
"mocha": "*", | ||
"should": "*", | ||
"jscoverage": "*", | ||
"coveralls": "*", | ||
"mocha-lcov-reporter": "*" | ||
"jshint": "*" | ||
}, | ||
"scripts": { | ||
"test": "make test && make test-coveralls" | ||
"test": "make test-coveralls" | ||
}, | ||
"repository": "git://github.com/Swatinem/proxy.git", | ||
"license": "LGPLv3" | ||
"license": "LGPL-3.0" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
5641
6
116
1
1