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

@foxen/helpers

v1.4.0

Published

Helper utilities for Foxen - userAgent, matchers, and more

Readme

@foxen/helpers

Utility functions that complement @foxen/core, including user agent parsing, geolocation, IP detection, and path matching.

Installation

bun add @foxen/helpers

Usage

User Agent Parsing

import { userAgent } from "@foxen/helpers";
import type { NextRequest } from "@foxen/core";

export async function GET(request: NextRequest) {
    const ua = userAgent(request);

    return NextResponse.json({
        browser: ua.browser, // { name: 'Chrome', version: '120.0.0' }
        os: ua.os, // { name: 'macOS', version: '14.0' }
        device: ua.device, // { type: 'desktop', vendor: 'Apple', model: 'Macintosh' }
        engine: ua.engine, // { name: 'Blink', version: '120.0.0' }
        cpu: ua.cpu, // { architecture: 'arm64' }
        isBot: ua.isBot, // false
    });
}

Geolocation

import { geolocation } from "@foxen/helpers";
import type { NextRequest } from "@foxen/core";

export async function GET(request: NextRequest) {
    const geo = geolocation(request);

    return NextResponse.json({
        city: geo.city, // 'San Francisco'
        country: geo.country, // 'US'
        region: geo.region, // 'CA'
        latitude: geo.latitude, // '37.7749'
        longitude: geo.longitude, // '-122.4194'
    });
}

IP Address Detection

import { ipAddress } from "@foxen/helpers";
import type { NextRequest } from "@foxen/core";

export async function GET(request: NextRequest) {
    const ip = ipAddress(request);

    return NextResponse.json({
        ip, // '192.168.1.1' or '::1'
    });
}

Path Matching

import { pathMatcher, matchPath } from "@foxen/helpers";

// Create a matcher function
const isApiRoute = pathMatcher("/api/:path*");
const isUserRoute = pathMatcher("/api/users/:id");

// Check paths
isApiRoute("/api/users"); // true
isApiRoute("/api/users/123"); // true
isApiRoute("/about"); // false

isUserRoute("/api/users/123"); // true
isUserRoute("/api/users"); // false

// Extract params
const match = matchPath("/api/users/:id", "/api/users/123");
// { params: { id: '123' }, matched: true }

Request Matchers

import { matchesMiddleware } from "@foxen/helpers";

const config = {
    matcher: ["/api/:path*", "/dashboard/:path*"],
};

// Check if request matches middleware config
const request = new NextRequest("https://example.com/api/users");
const matches = matchesMiddleware(request, config.matcher);
// true

API Reference

userAgent(request)

Parse the User-Agent header from a request.

function userAgent(request: NextRequest | Request): UserAgent;

interface UserAgent {
    isBot: boolean;
    browser: { name?: string; version?: string };
    device: { type?: string; vendor?: string; model?: string };
    engine: { name?: string; version?: string };
    os: { name?: string; version?: string };
    cpu: { architecture?: string };
}

geolocation(request)

Extract geolocation data from request headers.

function geolocation(request: NextRequest | Request): Geo;

interface Geo {
    city?: string;
    country?: string;
    region?: string;
    latitude?: string;
    longitude?: string;
}

ipAddress(request)

Get the client's IP address from various headers.

function ipAddress(request: NextRequest | Request): string | undefined;

Checks these headers in order:

  1. x-real-ip
  2. x-forwarded-for (first IP)
  3. cf-connecting-ip (Cloudflare)
  4. x-client-ip
  5. x-cluster-client-ip

pathMatcher(pattern)

Create a function to match paths against a pattern.

function pathMatcher(pattern: string): (path: string) => boolean;

// Supported patterns:
// - /exact/path
// - /with/:param
// - /with/:param*  (catch-all)
// - /with/:param?  (optional)

matchPath(pattern, path)

Match a path against a pattern and extract params.

function matchPath(
    pattern: string,
    path: string,
): { matched: boolean; params: Record<string, string> };

Compatibility with Next.js

These helpers mirror the Next.js server utilities:

| Next.js | Foxen | |---------|-------| | import { userAgent } from 'next/server' | import { userAgent } from '@foxen/helpers' | | import { geolocation } from 'next/server' | import { geolocation } from '@foxen/helpers' | | import { ipAddress } from 'next/server' | import { ipAddress } from '@foxen/helpers' |

License

MIT