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

evalish

v0.1.8

Published

A maybe slightly safer-ish wrapper around eval Function constructors

Downloads

1,463

Readme

evalish is a small helper library that only exports a wrapper for the Function constructor: SafeFunction.

The SafeFunction constructor allows you to evaluate code and dynamically create a new function. In most environments, which at least don't have their CSP configured to disallow this, this will give you a fully executable function based on a string. As Function by default is a little safer than eval and runs everything in the global context, SafeFunction goes a step further and attempts to isolate the environment as much as possible.

It only does three simple things:

  • Isolate the global object and uses a separate object using a with statement
  • Wraps all passed through globals, like Array, in a recursive masking object that disallows access to object prototype properties
  • In the browser: Creates an iframe element and uses that frame's globals instead to prvent prototype pollution.

If you haven't run away screaming yet, maybe that's what you're looking for. Just a bit more safety. But really, I wrote this just for fun and I haven't written any tests yet and neither have I tested all edge cases. The export being named SafeFunction is really just ambitious.

However, if you found a way to break out of SafeFunction and did something to the outside JS environment, let me know and file an issue. I'm curious to see how far evalish would have to go to fully faux-isolate eval'ed code!

Usage

First install evalish alongside react:

yarn add evalish
# or
npm install --save evalish

You'll then be able to import SafeFunction and pass it argument names and code, just like the regular Function constructor.

import { SafeFunction } from 'evalish';

new SafeFunction('a', 'b', 'return a + b')(1, 2); // returns `3`
new SafeFunction('return window')(); // returns `undefined`
new SafeFunction('return Array.isArray.constructor')(); // returns `undefined`