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

@scratchyjs/utils

v26.3.22-1774223623833

Published

General-purpose server utilities for the Scratchy framework. Includes safe redirect validation, concurrent promise helpers, typed HTTP response factories, interval generator, IP address / locale / Sec-Fetch header utilities, and more.

Readme

@scratchyjs/utils

General-purpose server utilities for the Scratchy framework. Includes safe redirect validation, concurrent promise helpers, typed HTTP response factories, interval generator, IP address / locale / Sec-Fetch header utilities, and more.

Installation

pnpm add @scratchyjs/utils

API

safeRedirect(to, defaultRedirect?): string

Validates a redirect path so it stays within the same origin. URL-decodes the input before checking to block percent-encoded bypass attempts (%2e%2e, %2F%2F). Falls back to defaultRedirect ("/" by default) when the path is unsafe.

import { safeRedirect } from "@scratchyjs/utils";

reply.redirect(safeRedirect(request.query.redirectTo));
// "/dashboard"  → "/dashboard"  ✅
// "//evil.com"  → "/"           🛡️
// "../etc"      → "/"           🛡️

promiseHash(hash): Promise<AwaitedPromiseHash>

Promise.all for objects — resolves all values concurrently while preserving the key names.

import { promiseHash } from "@scratchyjs/utils";

const { user, posts } = await promiseHash({
  user: fetchUser(id),
  posts: fetchPosts(id),
});

timeout(promise, ms, message?): Promise<T>

Races promise against a timer. Throws TimeoutError if the timer fires first.

import { TimeoutError, timeout } from "@scratchyjs/utils";

try {
  const data = await timeout(fetchData(), 5000);
} catch (err) {
  if (err instanceof TimeoutError) {
    /* handle timeout */
  }
}

interval(ms, options?): AsyncGenerator

Async generator that yields on a fixed interval until an AbortSignal fires. Useful for SSE routes.

import { interval } from "@scratchyjs/utils";

const controller = new AbortController();
for await (const _ of interval(1000, { signal: controller.signal })) {
  reply.raw.write(`data: ${Date.now()}\n\n`);
}

redirectBack(request, options?): string

Returns a safe path based on the Referer header, normalizing absolute http(s) URLs to a relative path, or options.fallback ("/" by default) when absent.

getClientIPAddress(request): string | null

Extracts the real client IP from a Fastify request, respecting X-Forwarded-For and similar headers.

getClientLocales(request): Locales

Parses the Accept-Language header into an ordered array of locale strings.

isPrefetch(request): boolean

Returns true when the request carries a Purpose: prefetch or Sec-Purpose: prefetch header.

Response helpers

Typed Response factories with the correct Content-Type header:

| Function | Content-Type | | -------------------------- | --------------------------------------- | | notModified() | (304, no body) | | javascript(body, init?) | application/javascript; charset=utf-8 | | stylesheet(body, init?) | text/css; charset=utf-8 | | html(body, init?) | text/html; charset=utf-8 | | xml(body, init?) | application/xml; charset=utf-8 | | txt(body, init?) | text/plain; charset=utf-8 | | pdf(body, init?) | application/pdf | | image(body, type, init?) | image/<type> |

Sec-Fetch helpers

import {
  fetchDest,
  fetchMode,
  fetchSite,
  isUserInitiated,
} from "@scratchyjs/utils";

const dest = fetchDest(request); // "document" | "script" | …
const mode = fetchMode(request); // "navigate" | "cors" | …
const site = fetchSite(request); // "same-origin" | "cross-site" | …
const user = isUserInitiated(request); // boolean

Documentation

https://scratchyjs.com/middleware