
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
babel-plugin-prototype-prop-define
Advanced tools
Transform assignments to properties on built-in prototypes to Object.defineProperty calls
A babel plugin for working around javascript's "override mistake" when dealing with frozen Primordials (built-ins).
If your primordials are frozen, such as in SES, assigning keys that are found on the prototype will throw an error.
Object.freeze(Object.prototype)
const x = {}
x.toString = () => 'hello'
// => TypeError: Cannot assign to read only property 'toString' of object '#<Object>'
Since this is a common, this plugin is provided to transform code into a safe form using Object.defineProperty
:
Object.freeze(Object.prototype)
const x = {}
(function (parent, key, value) {
Object.defineProperty(parent, key, {
value: value,
writable: true,
enumerable: true,
configurable: true
})
return value;
})(x, "toString", () => 'hello');
x.toString()
// => 'hello'
The code is transformed into an IIFE in order to ensure the "parent" (x
), "key" (y
) and "value" (() => 'hello'
) statements are evaluated exactly once, and that the whole expression resolves to the value of the "value" statement. This is needed because Object.defineProperty
calls resolve to undefined
.
For "computed" assignment, the resulting code is a little uglier.
const x = {};
const y = 'toString';
x[y] = () => 'hello'
becomes
const x = {};
const y = 'toString';
(function (parent, key, value) {
["constructor", "__defineGetter__", "__defineSetter__", "hasOwnProperty", "__lookupGetter__", "__lookupSetter__", "isPrototypeOf", "propertyIsEnumerable", "toString", "valueOf", "__proto__", "toLocaleString", "length", "concat", "find", "findIndex", "pop", "push", "shift", "unshift", "slice", "splice", "includes", "indexOf", "keys", "entries", "forEach", "filter", "map", "every", "some", "reduce", "reduceRight", "join", "reverse", "sort", "lastIndexOf", "copyWithin", "fill", "values", "name", "arguments", "caller", "apply", "bind", "call", "message"].includes(key) ? Object.defineProperty(parent, key, {
value: value,
writable: true,
enumerable: true,
configurable: true
}) : parent[key] = value;
return value;
})(x, y, () => true);
Here the check that is normally performed at parse time (checking if the key is in this whitelist), is performed at runtime to determine if normal assignment or Object.defineProperty
should be used.
FAQs
Transform assignments to properties on built-in prototypes to Object.defineProperty calls
The npm package babel-plugin-prototype-prop-define receives a total of 1 weekly downloads. As such, babel-plugin-prototype-prop-define popularity was classified as not popular.
We found that babel-plugin-prototype-prop-define demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.