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

tofua

v0.1.1

Published

A tiny User-Agent parser for OS and browser detection.

Readme

tofua

A tiny User-Agent parser for OS and browser detection.

tofua focuses on a very small surface area:

  • OS detection
  • browser detection
  • version extraction when it is reliable
  • predictable Unknown fallbacks

It does not try to do device detection, bot detection, engine detection, or Client Hints parsing.

Installation

pnpm add tofua

Quick Start

import { getUA, parseUA } from "tofua";

const userAgent =
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36";

const result = parseUA(userAgent);
const current = getUA();

console.log(result);
// {
//   os: { name: "Windows", version: "10.0" },
//   browser: { name: "Chrome", version: "135.0.0.0" },
//   raw: "Mozilla/5.0 ..."
// }

API

parseUA(userAgent: string): UAResult

Parses a User-Agent string and returns both OS and browser information.

import { parseUA } from "tofua";

const result = parseUA(
  "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1",
);

// {
//   os: { name: "iOS", version: "17.4" },
//   browser: { name: "Safari", version: "17.4" },
//   raw: "..."
// }

parseUA keeps the input string as-is in raw.

safeParseUA(userAgent?: string | null): UAResult

Defensive version of parseUA.

  • null and undefined become ""
  • string input is trimmed before parsing
  • malformed or unknown input returns Unknown
import { safeParseUA } from "tofua";

safeParseUA(undefined);
// {
//   os: { name: "Unknown", version: null },
//   browser: { name: "Unknown", version: null },
//   raw: ""
// }

getUA(): UAResult

Parses navigator.userAgent and returns the same shape as parseUA.

In browser environments, this is effectively a wrapper around parseUA(navigator.userAgent). If navigator.userAgent is unavailable, it returns an Unknown result with raw: "".

import { getUA } from "tofua";

const current = getUA();

getOS(userAgent: string): OSInfo

Returns only the OS result.

import { getOS } from "tofua";

getOS(
  "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
);
// { name: "macOS", version: "13.5.1" }

getBrowser(userAgent: string): BrowserInfo

Returns only the browser result.

import { getBrowser } from "tofua";

getBrowser(
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.3179.54",
);
// { name: "Edge", version: "135.0.3179.54" }

Types

type OSName =
  | "Windows"
  | "macOS"
  | "iOS"
  | "Android"
  | "Linux"
  | "Chrome OS"
  | "Unknown";

type BrowserName =
  | "Chrome"
  | "Edge"
  | "Safari"
  | "Firefox"
  | "Opera"
  | "Samsung Internet"
  | "Android WebView"
  | "iOS WebView"
  | "Internet Explorer"
  | "Oculus Browser"
  | "Unknown";

interface OSInfo {
  name: OSName;
  version: string | null;
}

interface BrowserInfo {
  name: BrowserName;
  version: string | null;
}

interface UAResult {
  os: OSInfo;
  browser: BrowserInfo;
  raw: string;
}

Detection Behavior

OS

Supported OS names:

  • iOS
  • Android
  • Chrome OS
  • Windows
  • macOS
  • Linux

Browser

Supported browser names:

  • Edge
  • Opera
  • Oculus Browser
  • Samsung Internet
  • Android WebView
  • Chrome
  • Safari
  • iOS WebView
  • Firefox
  • Internet Explorer

Rule order matters. For example:

  • Edge wins over Chrome
  • Opera wins over Chrome
  • Oculus Browser wins over Chrome
  • Samsung Internet wins over Chrome
  • Android WebView wins over Chrome
  • Safari requires Safari/ and Version/
  • Chromium/-only User-Agent strings return Unknown

Notes

  • When detection is inconclusive, tofua returns Unknown instead of guessing.
  • Some platforms do not expose a reliable OS version. For example, Linux returns version: null.
  • Windows returns the raw Windows NT version value.
  • User-Agent parsing is inherently imperfect and modern browsers continue to reduce UA detail.

Development

pnpm lint
pnpm typecheck
pnpm test
pnpm build
pnpm fmt
pnpm fmt:check

License

MIT