MCPcopy Create free account
hub / github.com/cryptii/cryptii / performDecode

Method performDecode

src/Encoder/Punycode.js:97–124  ·  view source on GitHub ↗

* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content

(content)

Source from the content-addressed store, hash-verified

95 * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
96 */
97 async performDecode (content) {
98 // Check if the given input contains any unexpected non-basic code points
99 const nonBasicIndex = this._nonBasicCodePointIndex(content.getCodePoints())
100 if (nonBasicIndex !== -1) {
101 throw new InvalidInputError(
102 `Invalid Punycode character at index ${nonBasicIndex}`)
103 }
104
105 // Split domain into labels
106 const labels = content.split('.')
107 const decodedParts = new Array(labels.length)
108
109 // Decode each part separately
110 for (let i = 0; i < labels.length; i++) {
111 // Check for the IDNA prefix
112 if (labels[i].indexOf(prefix) !== 0) {
113 // Return the part as is
114 decodedParts[i] = labels[i]
115 } else {
116 // Remove IDNA prefix and decode part using Bootstring
117 const part = labels[i].substr(prefix.length)
118 decodedParts[i] = await this._bootstringEncoder.decode(part)
119 }
120 }
121
122 // Stick domain labels back together
123 return Chain.join(decodedParts, '.')
124 }
125
126 /**
127 * Returns the first non-basic code point, if any.

Callers

nothing calls this directly

Calls 7

getCodePointsMethod · 0.80
splitMethod · 0.80
indexOfMethod · 0.80
substrMethod · 0.80
decodeMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected