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 🙏

© 2025 – Pkg Stats / Ryan Hefner

stahp

v0.9.0

Published

Guard against infinite loops in Javascript code by injecting timeouts

Readme

Stahp

Guard against infinite loops in Javascript code by injecting timeouts

Copyright (c) 2019 Chris Maltby, released under the MIT license.

Twitter: @maltby

Stahp is a Node module that takes in a string of Javascript code and outputs the same code updated with guards added to throw a timeout error if any loops cause the execution to take longer than a specified amount of time.

For example with the following input:

while (true) {
  console.log("Infinite Loop?");
}

Stahp will output:

var ___timeout___ = Date.now();
while (true) {
    if (Date.now() > ___timeout___ + 1000) {
        throw new Error('Timed out');
    }
    console.log('Infinite Loop?');
}

causing the execution to stop after one second.

The concept is based on a blog post by @quezo
https://codepen.io/quezo/post/stopping-infinite-loops. Internally Stahp is using esprima to convert the input Javascript into an AST which is then modified and turned back into into Javascript using escodegen.

If handling untrusted code it is recommended to use this alongside a module like VM2. While VM2 does have some support for timeouts but this isn't available in all cases and doesn't appear to work while calling from an Electron app.

Installation

npm install stahp

Quick Example

const stahp = require("stahp");

const input = `while (true) {
  console.log("Infinite Loop?");
}`;

const output = stahp(input, {
	timeout: 100
});

console.log(output);

stahp(code, options)

The first argument is a string of valid Javascript code, the second argument is an optional options object that can contain the following:

  • timeout Optional object containing additional arguments (default 1000)