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

@web-ai-sdk/detector

v0.4.0

Published

Building block for the Web's Built-in Language Detector API (Chrome)

Readme

@web-ai-sdk/detector

Building block for the Web's Built-in Language Detector API. Detect the language of any text on-device, with confidence scores and a sorted list of alternates. Session reuse, pluggable result caching, AbortSignal-driven cleanup.

Docs: https://web-ai-sdk.dev/docs/guides/detector/ · React: useDetector

Status

Language Detector ships stable in Chrome 138+ on desktop. On Edge it is a developer preview starting at Canary/Dev 147+ behind edge://flags/#edge-language-detection-api (per the Edge Language Detector API docs) — not yet in Edge stable. On any other browser this library is a no-op for the React hook (it stays in "unavailable"). The vanilla detect() throws DetectorUnavailableError so callers can branch explicitly.

Install

pnpm add @web-ai-sdk/detector
# or: npm i @web-ai-sdk/detector / bun add @web-ai-sdk/detector

The React adapter ships as a subpath export, with no extra install. react is a peer dependency only when you import the /react entry.

Vanilla TypeScript / DOM

import { detect } from "@web-ai-sdk/detector";

const result = await detect({ text: "Olá, mundo" });
console.log(result.language);   // → "pt"
console.log(result.confidence); // → 0.98
console.log(result.all);        // → full sorted list of candidates

React

import { useDetector } from "@web-ai-sdk/detector/react";

export function LangBadge({ text }: { text: string }) {
  const { status, language, confidence } = useDetector({ text });

  if (status !== "done" || !language) return null;
  return (
    <span>
      {language} · {Math.round(confidence * 100)}%
    </span>
  );
}

State machine: pending | loading | done | unavailable. The hook auto-runs on mount and re-runs whenever text changes. Stays in "pending" while the input is empty or whitespace-only.

API

detect(options): Promise<DetectResult>

interface DetectOptions {
  text: string;
  expectedInputLanguages?: readonly string[];  // bias hint
  minConfidence?: number;                      // default 0
  createOptions?: Partial<LanguageDetectorCreateOptions>;
  cache?: DetectionCache;
  cacheKey?: string;
  signal?: AbortSignal;
}

interface DetectResult {
  language: string | null;          // top BCP-47 code, or null below minConfidence
  confidence: number;               // 0..1 for the top result
  all: DetectionResult[];           // [{ detectedLanguage, confidence }, ...]
  cached: boolean;
}

isDetectorAvailable(): boolean

Feature-detect helper.

checkAvailability(opts?): Promise<LanguageDetectorAvailability | null>

Forwards to LanguageDetector.availability(). Returns null if the global is missing or the call throws.

createSessionStorageCache({ storage?, prefix? }): DetectionCache

Optional cache backend. Pass it to detect({ cache }) to enable result caching, with an optional custom storage (e.g. localStorage, an in-memory polyfill).

Lower-level helpers (advanced)

getLanguageDetectorApi, getOrCreateLanguageDetector, defaultCacheKey; exported so you can compose your own pipeline (e.g. share a session across multiple call sites, or roll your own retry).

Caching

Two layers, same as the other packages:

  • Session cache (internal, in-memory, always on): a Map<stringifiedOptions, LanguageDetector> so consecutive calls with the same expectedInputLanguages shape reuse the warm session. Cold-start is fast on this model (~100-300ms) but warm is still sub-50ms.
  • Result cache (opt-in): pass a cache (anything matching { get, set }) to memoize the full sorted list by trimmed text. Omit it for a fresh model call every time.
// Off by default; every call hits the model.
detect({ text: "hello" });

// Opt in for sessionStorage-backed caching.
detect({ text: "hello", cache: createSessionStorageCache() });

Composing with the other packages

Pair detector with summarizer / translator / prompt to skip the manual language: "en" argument when you don't know the input language ahead of time:

import { detect } from "@web-ai-sdk/detector";
import { summarize } from "@web-ai-sdk/summarizer";

const { language } = await detect({ text: articleText });
await summarize({ language: language ?? "en", text: articleText });

A first-class language: "auto" shortcut may land in a future release.

Errors and unavailability

The vanilla detect() throws DetectorUnavailableError when the API is missing or reports availability: "unavailable". The React hook absorbs this and returns status: "unavailable" instead.

AbortSignal is supported on both surfaces. The result cache is not written for aborted runs.

License

MIT © Beto Muniz