-
Notifications
You must be signed in to change notification settings - Fork 9
/
uts46.js
118 lines (104 loc) · 3.76 KB
/
uts46.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* istanbul ignore next */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['punycode', './idna-map'], function (punycode, idna_map) {
return factory(punycode, idna_map);
});
} else if (typeof exports === 'object') {
module.exports = factory(require('punycode'), require('./idna-map'));
} else {
root.uts46 = factory(root.punycode, root.idna_map);
}
}(this, function (punycode, idna_map) {
function mapLabel(label, useStd3ASCII, transitional) {
var mapped = [];
for (var ch of label) {
var cp = ch.codePointAt(0);
var composite = idna_map.mapChar(cp);
var flags = (composite >> 23);
var kind = (composite >> 21) & 3;
var index = (composite >> 5) & 0xffff;
var length = composite & 0x1f;
var value = idna_map.mapStr.substr(index, length);
if (kind == 0 || (useStd3ASCII && (flags & 1))) {
throw new Error("Illegal char " + ch);
} else if (kind == 1) {
mapped.push(value);
} else if (kind == 2) {
mapped.push(transitional ? value : ch);
} else if (kind == 3) {
mapped.push(ch);
}
}
var newLabel = mapped.join("").normalize("NFC");
return newLabel;
}
function process(domain, transitional, useStd3ASCII) {
if (useStd3ASCII === undefined)
useStd3ASCII = false;
var mappedIDNA = mapLabel(domain, useStd3ASCII, transitional);
// Step 3. Break
var labels = mappedIDNA.split(".");
// Step 4. Convert/Validate
labels = labels.map(function (label) {
if (label.startsWith("xn--")) {
label = punycode.decode(label.substring(4));
validateLabel(label, useStd3ASCII, false);
} else {
validateLabel(label, useStd3ASCII, transitional);
}
return label;
});
return labels.join(".");
}
function validateLabel(label, useStd3ASCII, transitional) {
// 2. The label must not contain a U+002D HYPHEN-MINUS character in both the
// third position and fourth positions.
if (label[2] == '-' && label[3] == '-')
throw new Error("Failed to validate " + label);
// 3. The label must neither begin nor end with a U+002D HYPHEN-MINUS
// character.
if (label.startsWith('-') || label.endsWith('-'))
throw new Error("Failed to validate " + label);
// 4. The label must not contain a U+002E ( . ) FULL STOP.
if (label.includes('.'))
throw new Error("Failed to validate " + label);
if (mapLabel(label, useStd3ASCII, transitional) != label)
throw new Error("Failed to validate " + label);
// 5. The label must not begin with a combining mark, that is:
// General_Category=Mark.
var ch = label.codePointAt(0);
if (idna_map.mapChar(ch) & (0x2 << 23))
throw new Error("Label contains illegal character: " + ch);
}
function toAscii(domain, options) {
if (options === undefined)
options = { };
var transitional = 'transitional' in options ? options.transitional : true;
var useStd3ASCII = 'useStd3ASCII' in options ? options.useStd3ASCII : false;
var verifyDnsLength = 'verifyDnsLength' in options ? options.verifyDnsLength : false;
var labels = process(domain, transitional, useStd3ASCII).split('.');
var asciiLabels = labels.map(punycode.toASCII);
var asciiString = asciiLabels.join('.');
if (verifyDnsLength) {
if (asciiString.length < 1 || asciiString.length > 253) {
throw new Error("DNS name has wrong length: " + asciiString);
}
for (var label of asciiLabels) {
if (label.length < 1 || label.length > 63)
throw new Error("DNS label has wrong length: " + label);
}
}
return asciiString;
}
function toUnicode(domain, options) {
if (options === undefined)
options = { };
var useStd3ASCII = 'useStd3ASCII' in options ? options.useStd3ASCII : false;
return process(domain, false, useStd3ASCII);
}
return {
toUnicode: toUnicode,
toAscii: toAscii,
};
}));