idna 
Internationalized domain names for JavaScript and TypeScript: converts between the Unicode and the ASCII (xn--) form of a domain name and validates it against IDNA 2008 (RFC 5890–5893), with the optional UTS #46 compatibility mapping.
It is a port of Python's idna package (version 3.20) and behaves like it.
- No dependencies. The library uses no Node.js APIs, so it also runs in browsers.
- ES module and CommonJS builds, with type declarations.
- Lookup tables generated from Unicode 18.0.0.
Installation
npm install idna
Node.js 18 or later.
Usage
import { decode, encode } from 'idna';
// or: const { decode, encode } = require('idna');
encode('ドメイン.テスト'); // 'xn--eckwd4c7c.xn--zckzah'
decode('xn--eckwd4c7c.xn--zckzah'); // 'ドメイン.テスト'
alabel and ulabel do the same for a single label:
alabel('пример'); // 'xn--e1afmkfd'
ulabel('xn--e1afmkfd'); // 'пример'
Compatibility mapping (UTS #46)
IDNA 2008 is strict about its input. Uppercase letters, for one, are not allowed. What a user types is usually normalized first, which is what the UTS #46 mapping does: it lowercases, folds fullwidth characters, removes soft hyphens and so on.
encode('Königsgäßchen');
// InvalidCodepoint: Codepoint U+004B at position 1 of "Königsgäßchen" not allowed
encode('Königsgäßchen', { uts46: true }); // 'xn--knigsgchen-b4a3dun'
decode('xn--knigsgchen-b4a3dun'); // 'königsgäßchen'
Decoding for display
To show a domain to a user, pass display: true. Labels that start with xn-- but cannot be decoded are then kept as they are instead of failing the whole domain:
decode('example.xn--pokxncvks'); // throws InvalidCodepoint
decode('example.xn--pokxncvks', { display: true }); // 'example.xn--pokxncvks'
API
Conversion
| Function | Description |
|---|---|
encode(domain, options?) |
Domain to its ASCII form. ASCII labels are validated and returned as they are. |
decode(domain, options?) |
Domain to its Unicode form. ASCII labels are lowercased. |
alabel(label) |
Single label to its A-label (ASCII form). |
ulabel(label) |
Single label to its U-label (Unicode form). |
uts46Remap(domain, std3Rules = true) |
Applies the UTS #46 mapping and returns the result in NFC. |
toASCII(domain), toUnicode(domain) |
encode and decode without options. |
Options of encode and decode:
| Option | Default | Description |
|---|---|---|
uts46 |
false |
Apply the UTS #46 mapping first. |
std3Rules |
false |
With uts46: reject ASCII characters other than letters, digits and hyphens during the mapping. |
strict |
false |
Split labels at . only. By default 。 (U+3002), . (U+FF0E) and 。 (U+FF61) separate labels too. |
display |
false |
decode only: keep xn-- labels that are not valid, see above. |
A trailing dot is kept. encode enforces the DNS limits of 63 characters per label and 253 per domain.
Validation
These are the checks the conversion functions run. checkLabel runs all of them.
| Function | Description |
|---|---|
checkLabel(label) |
Throws unless the label is valid under IDNA 2008. |
checkNfc(label) |
Throws unless the label is in Normalization Form C. |
checkHyphenOk(label) |
Throws for a hyphen at either end, or in both the 3rd and 4th position. |
checkInitialCombiner(label) |
Throws if the label starts with a combining mark. |
checkBidi(label, checkLtr = false) |
Throws unless the label satisfies the Bidi Rule of RFC 5893. |
validContextJ(label, pos) |
Whether the joiner at index pos is allowed there (RFC 5892, appendix A.1–A.2). |
validContextO(label, pos) |
Whether the character at index pos is allowed there (RFC 5892, appendix A.3–A.9). |
validLabelLength(label) |
Whether the label has at most 63 characters. |
validStringLength(domain, trailingDot) |
Whether the domain has at most 253 characters, 254 with a trailing dot. |
Indexes and lengths count code points, not UTF-16 code units.
Code points
getCodepointClass(cp) returns the IDNA 2008 derived property value of a code point: 'PVALID', 'CONTEXTJ', 'CONTEXTO', 'DISALLOWED' or 'UNASSIGNED'.
getCodepointClass(0x00df); // 'PVALID' (ß)
getCodepointClass(0x0041); // 'DISALLOWED' (A)
getCodepointClass(0x30fb); // 'CONTEXTO' (katakana middle dot)
unicodeVersion is the Unicode version of the tables, '18.0.0'.
Errors
Invalid input throws an IDNAError or one of its subclasses:
InvalidCodepoint: a character is not allowed in domain names.InvalidCodepointContext: a character is not allowed in this position.IDNABidiError: left-to-right and right-to-left characters are mixed in a way that is not allowed.
Besides the message, errors carry a stable code and, when a single character is at fault, text (the label or domain), codepoint and position (1-based, in code points):
try {
encode('ab😀.example');
} catch (error) {
error.name; // 'InvalidCodepoint'
error.code; // 'disallowed_codepoint'
error.codepoint; // 0x1f600
error.position; // 3
error.text; // 'ab😀'
}
The codes are input_too_long, label_too_long, domain_too_long, empty_label, empty_domain, not_nfc, hyphen_3_4, hyphen_start_end, leading_combiner, disallowed_codepoint, contextj, contexto, bidi_rule_1 to bidi_rule_6, bidi_unknown_direction, invalid_alabel, non_canonical_alabel, uts46_disallowed and uts46_std3.
Command line
$ npx idna пример.рф
xn--e1afmkfd.xn--p1ai
$ npx idna xn--e1afmkfd.xn--p1ai
пример.рф
The direction is taken from the first domain unless -e (encode) or -d (decode) is given. Domains can also be piped in, one per line. The UTS #46 mapping is applied unless --strict is given. Failures are reported on standard error and make the exit status 1.
getLabel
The original API of this package. It still works as it did in 0.0.1.
const idna = require('idna');
idna.getLabel(49, true, true); // 'PVALID'
getLabel(code, idna2003, idna2008) looks up a code point in the tables ICANN published for the IDN ccTLD Fast Track, which compare IDNA2003 and IDNA2008 for Unicode 5.2. The two flags select the table:
idna2003 |
idna2008 |
Table |
|---|---|---|
true |
true |
Characters valid under both protocols |
true |
false |
Characters valid under IDNA2003 only |
false |
true |
Characters valid under IDNA2008 only |
It returns 'PVALID', 'CONTEXTO' or 'DISALLOWED', or 'UNDEF' when the table does not list the code point, and throws when both flags are false. The tables are frozen. For the class of a code point in current Unicode use getCodepointClass.
Differences from Python's idna
- Names are camelCase, and the keyword arguments of
encodeanddecodeare an options object. - Everything is a string. There are no
bytesinputs or results, and no codec. - The deprecated
transitionalargument does not exist. - A value that is not a string throws a
TypeError, not anIDNAError. - An A-label that decodes to surrogate code points is rejected as
invalid_alabel.
Notes
- Unicode version. The tables are generated from one Unicode version, but normalization is done by the JavaScript runtime. A runtime with older Unicode data does not know how to normalize characters added since, so it can accept or reject a label with such characters differently.
- Not everything UTS #46 allows is valid. With
uts46, the mapping prepares the input, but labels are still validated against IDNA 2008. Emoji and other symbols stay invalid. - Where it differs from UTS #46. A trailing dot is accepted, and the Bidi Rule is applied to each label that has right-to-left characters, not to the other labels of the same domain.
Development
Development needs Node.js 22.18 or later, which runs the TypeScript sources directly.
npm install
npm test # run the tests
npm run typecheck # check types
npm run build # compile to dist/
The tests include the conformance test of UTS #46 (test/fixtures/IdnaTestV2.txt).
GitHub Actions runs these steps on Node.js 22, 24 and 26 for every push to master and every pull request, and tries the packed package on Node.js 18 and 20.
Updating the Unicode data
The tables in src/data are generated from the Unicode Character Database by scripts/update.ts. It follows RFC 5892 to derive the IDNA 2008 classes, and downloads the data files from unicode.org:
npm run update -- 19.0.0
Without a version, the tables are rebuilt for the version they already use. Set UNICODE_BASE_URL to download from a mirror of https://www.unicode.org/Public.
Releasing
Pushing a version tag starts the Publish workflow, which tests the package and stages it on npm:
npm version patch # or minor, major
git push --follow-tags
The staged version is published once a maintainer approves it on npmjs.com, or with npm stage approve.
The workflow uses npm trusted publishing instead of a token. For this, the package settings on npmjs.com have to list the GitHub repository tugrul/node-idna with the workflow publish.yml as trusted publisher.
License
MIT.
The validation logic is ported from Python's idna (BSD 3-Clause), and the tables are derived from Unicode data files (Unicode License v3). See THIRD_PARTY_NOTICES.md.