npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

frozen-fruit

v0.0.2

Published

Don't get userland get to you... use frozen-fruit!

Downloads

102

Readme

frozen-fruit

Overview

frozen-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.

Features

  • 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.

Usage

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.

Example

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'

Primordials Structure

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 methods and properties are prefixed by the object name and Prototype.
    • e.g., ArrayPrototypeMap, StringPrototypeSplit, etc.
  • Getter and setter methods are prefixed with Get and Set respectively.
    • e.g., ElementGetId, WindowSetTitle.

Custom Usage

You can also use the GetPrimordial() function directly to copy properties from a specific source:

const { GetPrimordial } = require('frozen-fruit');

// Extract properties from a custom object
const myPrimordials = GetPrimordial(SomeGlobalObject);

Installation

Simply add the frozen-fruit file to your project and require it wherever you need access to immutable global properties.

How it Works

  1. Collecting Global Properties: It uses Object.getOwnPropertyNames(globalThis) to gather a set of all global properties.
  2. Property Copying: For each global property, it uses Reflect.ownKeys() to iterate over the properties and Reflect.getOwnPropertyDescriptor() to extract detailed property descriptors (including getters, setters, etc.).
  3. Unbinding Functions: All function values are unbound using Function.prototype.call.bind(), preventing their internal this from being altered.
  4. Freezing: After collecting and copying all desired properties, the resulting primordials object is frozen using Object.freeze().