Skip to content

About

Low-level Punycode encoder/decoder without IDNA

Topics

Resources

Stars

4 stars

Watchers

3 watching

Forks

Repository files navigation

punycode.js

CI npm

0-dependency low-level Punycode encoder/decoder without IDNA, written in TypeScript.

Demo ⭐️

import { encodeLabel, decodeLabel } from '@adraffy/punycode';

Labels

Most code wants the label-level functions, which handle the xn-- prefix. They do not split on dots, so pass one label at a time.

import { encodeLabel, decodeLabel } from '@adraffy/punycode';

// (string | Iterable<number>, allowSurrogates?) => string
// input: Unicode string or code points
// output: unchanged if ASCII, otherwise "xn--" + Punycode
encodeLabel('💩'); // "xn--ls8h"
encodeLabel([0x1f4a9]); // "xn--ls8h"
encodeLabel('abc'); // "abc"

// (string | ArrayLike<number>) => string
// input: ASCII string, bytes (eg. Uint8Array), or code points
// output: decoded if prefixed by "xn--" (any case), otherwise unchanged
decodeLabel('xn--ls8h'); // "💩"
decodeLabel('xn--maana-pta'); // "mañana"
decodeLabel('abc'); // "abc"

Raw Punycode

encodePunycode and decodePunycode are RFC 3492 Bootstring for Punycode, with no prefix handling.

import { encodePunycode, decodePunycode } from '@adraffy/punycode';

encodePunycode('💩'); // "ls8h"
encodePunycode('mañana'); // "maana-pta"
encodePunycode('abc'); // "abc-" (basic code points are always followed by a delimiter)

decodePunycode('ls8h'); // "💩"
decodePunycode('LS8H'); // "💩"

Surrogates

Punycode can encode surrogates, which are not Unicode characters, and a string cannot represent them losslessly:

const s = '💩';
// two different encodings
encodeLabel(s); // "xn--ls8h"
encodeLabel([s.charCodeAt(0), s.charCodeAt(1)], true); // "xn--8c9by4f"
// that would decode to the same string

So by default, the encoders and decoders throw on surrogates. To inspect such labels, use the CodePoints variants, which return the raw code points:

import { decodeLabel, decodeLabelCodePoints, decodePunycodeCodePoints } from '@adraffy/punycode';

decodeLabel('xn--8c9by4f'); // throws RangeError: surrogate: U+D83D
decodeLabelCodePoints('xn--8c9by4f'); // [0xD83D, 0xDCA9]
decodeLabelCodePoints('xn--ls8h'); // [0x1F4A9]
decodePunycodeCodePoints('8c9by4f'); // [0xD83D, 0xDCA9]

In practice, a surrogate in a string usually means it was cut through an emoji. To encode surrogates anyway, pass allowSurrogates:

const cut = '💩'.slice(0, 1); // "\uD83D"
encodeLabel(cut); // throws RangeError: surrogate: U+D83D
encodeLabel(cut, true); // "xn--8c9b"
encodePunycode(cut, true); // "8c9b"

To accept only canonical labels:

const s = decodeLabel(label); // throws on surrogates
// rejects alternate encodings, eg. "xn--LS8H" or "xn--abc-"
if (encodeLabel(s) !== label) throw new Error('not canonical');

Hostnames

isHostname checks RFC 1123 hostname syntax: labels of 1–63 letters, digits, or hyphens, no leading or trailing hyphen, and at most 253 characters (one trailing dot allowed).

import { encodeLabel, isHostname } from '@adraffy/punycode';

const name = '💩.eth'.split('.').map((label) => encodeLabel(label)).join('.'); // "xn--ls8h.eth"
isHostname(name); // true
isHostname('💩.eth'); // false

Errors

All functions throw on invalid input:

  • TypeError when the input is not a string, iterable, or array-like
  • RangeError for an invalid code point, non-ASCII input to a decoder, invalid Punycode, overflow (§6.4), or a surrogate (except in the CodePoints decoders, or when encoding with allowSurrogates)

Migrating from 1.x

1.x 2.x
puny_encoded(x) encodeLabel(x), which now rejects surrogates (encodeLabel(x, true) allows them, like 1.x)
puny_encoded_bytes(x) [...encodeLabel(x)].map((c) => c.charCodeAt(0))
puny_decoded(x) decodeLabelCodePoints(x), or decodeLabel(x) for a string
puny_decode(x) decodePunycodeCodePoints(x), or decodePunycode(x) for a string
is_surrogate(cp) isSurrogate(cp)
MIN_CP, MAX_CP removed

New in 2.x: string decoders, encodePunycode (raw Punycode), surrogate rejection, isHostname, and overflow detection in the decoder.

Development

  • npm test — run tests (requires Node.js ≥ 22.18 for TypeScript type stripping)
  • npm run check — Biome lint/format check and tsc type check
  • npm run fix — apply Biome fixes
  • npm run build — compile src/ to dist/
  • demo/ — after npm run build, serve the repo root with any static server and open /demo/

About

Low-level Punycode encoder/decoder without IDNA

Topics

Resources

Stars

4 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages