Host check and browser client

@exo/kit/http#

Exports: validateHost, isReservedAddress. Uses node:dns and node:net.

Use it before your server fetches an address a user gave you.

  • isReservedAddress(ip) is true for any private, reserved or unparseable address, IPv4 or IPv6, in any spelling, such as ::ffff:127.0.0.1.
  • validateHost(host) refuses reserved names (localhost, *.internal), non-canonical IPv4 forms (2130706433, 0x7f000001, 127.1) and any hostname with a reserved DNS answer.
import { validateHost } from '@exo/kit/http';

const check = await validateHost(new URL(userUrl).hostname);

Check the result before connecting. A DNS check alone does not stop DNS rebinding, so also check the address on the connected socket, on every redirect hop.

@exo/kit/client#

Exports: fetchJson, jsonBody, RequestError, errorCode, refusalCode, cn, createI18n, plural, useLanguage, LANGUAGE_NAMES. Dependencies: clsx, tailwind-merge; react is an optional peer for the i18n functions. It contains no server code.

ExportWhat it does
fetchJson(url, init?, fetchImpl?)Same-origin request. A 2xx answer returns the parsed body, or null when empty. A refusal throws RequestError(status, code). No answer throws RequestError(0, 'network').
jsonBody(body, method = 'POST')A RequestInit with the JSON header and body.
refusalCode(body, status?)Reads { detail: { code } } or { code }. A 401 without a code becomes no-session.
errorCode(error)The code of a RequestError, or network for anything else.
cn(...classes)Joins class names and resolves Tailwind conflicts.
createI18n(key, { en, uk })Returns { I18nProvider, useI18n, codes, dictionaries }. useI18n() gives { lang, t, setLang, languages }.
useLanguage(key, codes)Stores the choice in localStorage, otherwise follows the browser language. Keeps <html lang> in step.
LANGUAGE_NAMESuk, en and ru under their own names.
plural(lang, n, forms)Picks a plural form with Intl.PluralRules.
import { fetchJson, jsonBody, errorCode } from '@exo/kit/client';

try {
  await fetchJson('/api/items', jsonBody({ name: 'New' }));
} catch (error) {
  if (errorCode(error) === 'no-session') redirectToSignIn();
}