0-dependency low-level Punycode encoder/decoder without IDNA, written in TypeScript.
Demo ⭐️
import { encodeLabel, decodeLabel } from '@adraffy/punycode';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"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'); // "💩"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 stringSo 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');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'); // falseAll functions throw on invalid input:
TypeErrorwhen the input is not a string, iterable, or array-likeRangeErrorfor an invalid code point, non-ASCII input to a decoder, invalid Punycode, overflow (§6.4), or a surrogate (except in theCodePointsdecoders, or when encoding withallowSurrogates)
| 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.