Don't get userland get to you... use frozen-fruit!
npm install frozen-fruitfrozen-fruit is a utility script that extracts and "freezes" global objects, properties, and methods into a secure, immutable namespace called primordials. This ensures that you can access and use key built-in properties without the risk of them being modified or tampered with by the surrounding environment.
The module achieves this by copying and unbinding all relevant global properties, including their prototypes, into a new object. It handles accessor properties (getters and setters) as well as regular properties. The final object is frozen to prevent further modifications, securing the environment from unintended changes to the primordials.
- Primordial Extraction: Copies properties and methods from global objects, along with their prototypes.
- Unbinding Functions: Ensures that function methods are unbound from their original context, preventing unwanted changes to the this context.
- Accessor Handling: Copies getter and setter methods from global objects and their prototypes.
- Symbol Key Formatting: Handles global objects with symbol keys and reformats their names for better usability.
- Immutable: The resulting primordials object is frozen, making it immutable.
After importing the module, you can access built-in global properties through the frozen primordials object. This makes it ideal for environments where you want to prevent accidental or malicious modifications to the global object.
``javascript
const primordials = require('frozen-fruit');
// Access a frozen global object
console.log(primordials.MathSin); // Unbound sine function from Math.prototype
// Attempting to modify the primordials will throw an error
primordials.Math = {}; // Error: Cannot assign to read only property 'Math'
`
The module provides an object where each primordial is organized as follows:
- Methods and properties of global objects are prefixed by the object name.
- e.g., MathSin, MathCos, etc.Prototype
- Prototype methods and properties are prefixed by the object name and .ArrayPrototypeMap
- e.g., , StringPrototypeSplit, etc.Get
- Getter and setter methods are prefixed with and Set respectively.ElementGetId
- e.g., , WindowSetTitle.
You can also use the GetPrimordial() function directly to copy properties from a specific source:
`javascript
const { GetPrimordial } = require('frozen-fruit');
// Extract properties from a custom object
const myPrimordials = GetPrimordial(SomeGlobalObject);
`
Simply add the frozen-fruit file to your project and require it wherever you need access to immutable global properties.
1. Collecting Global Properties: It uses Object.getOwnPropertyNames(globalThis) to gather a set of all global properties.Reflect.ownKeys()
2. Property Copying: For each global property, it uses to iterate over the properties and Reflect.getOwnPropertyDescriptor() to extract detailed property descriptors (including getters, setters, etc.).Function.prototype.call.bind()
3. Unbinding Functions: All function values are unbound using , preventing their internal this from being altered.primordials
4. Freezing: After collecting and copying all desired properties, the resulting object is frozen using Object.freeze()`.