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 🙏

© 2026 – Pkg Stats / Ryan Hefner

trim-safe

v1.0.2

Published

Safe, drop-in replacement for the abandoned trim package. Fixed ReDoS vulnerability (CVE-2020-7753).

Downloads

24

Readme

trim-safe

Safe, drop-in replacement for the abandoned trim npm package.

Why

The trim package (1M+ weekly downloads) has been effectively abandoned since 2013. Its canonical GitHub repo is dormant, the patch fork was archived in 2023, and the original source was never updated with the CVE fix.

The package contains CVE-2020-7753 / GHSA-cmr6-74hv-c9fg — a ReDoS vulnerability (CVSS 7.5 HIGH) in the regex /^\s*|\s*$/g. An attacker can craft an input string that causes catastrophic regex backtracking, consuming all CPU and hanging your process.

trim-safe fixes the vulnerability using a loop-based approach with no regex backtracking. Same API, zero security debt.

Install

npm install trim-safe

When installed into a project, trim-safe automatically checks your dependency tree. If it finds the vulnerable trim package, it prints a migration prompt:

⚠  VULNERABLE: trim (CVE-2020-7753) found in dependency tree
  3 packages still depend on the vulnerable trim package.

  Migrate:
    npm install trim-safe
    # in your code: require('trim-safe') instead of require('trim')

No extra setup needed.

Usage

var trim = require('trim-safe');

trim('  hello  ');    // 'hello'
trim('\t\ntest\r\n'); // 'test'
trim('hello');        // 'hello'
trim('   ');          // ''

Migration

Replace trim with trim-safe in your package.json:

- "trim": "^1.0.0"
+ "trim-safe": "^1.0.0"

Then in your code:

- var trim = require('trim');
+ var trim = require('trim-safe');

No API changes needed.

Security

The Vulnerability (CVE-2020-7753)

The original trim used this regex:

str.replace(/^\s*|\s*$/g, '');

The | alternation with * quantifiers on both sides creates exponential backtracking. An input like "1" + " ".repeat(50000) + "1" triggers catastrophic backtracking — the regex tries every possible way to split the string between the two alternatives.

Vulnerable version: ~2,000ms+ for 50k spaces Fixed version: <1ms for 50k spaces

The Fix

trim-safe uses two separate operations:

function left(str) {
  return str.replace(/^\s\s*/, '');  // requires 2+ spaces, no backtracking
}

function right(str) {
  var whitespace_pattern = /\s/;
  var i = str.length;
  while (whitespace_pattern.test(str.charAt(--i)));  // simple loop, no regex
  return str.slice(0, i + 1);
}

function trim(str) {
  return right(left(str));
}

Key changes:

  • /^\s\s*/ instead of /^\s*/ — requires minimum 2 whitespace chars to match (eliminates the zero-match combinatorial explosion)
  • \s*$/ replaced with a backward while loop — no regex needed, no backtracking
  • No g flag anywhere in the codebase

Benchmarks

| Input | Vulnerable | trim-safe | |-------|-----------|-----------| | ' hello ' | <1ms | <1ms | | 50k spaces surrounded by '1' | ~2,175ms | ~1ms |

API

trim(str)

Trims whitespace from both ends of a string.

  • str (string) — the string to trim
  • returns (string) — the trimmed string

Works on strings, numbers, null, undefined (coerces to string).

License

MIT