* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
(content)
| 76 | * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content |
| 77 | */ |
| 78 | performDecode (content) { |
| 79 | const string = content.getString() |
| 80 | const bytes = [] |
| 81 | let i = 0 |
| 82 | |
| 83 | // Go through string and collect bytes |
| 84 | let char, byteString |
| 85 | while (i < string.length) { |
| 86 | char = string[i] |
| 87 | |
| 88 | if (char === '%') { |
| 89 | // Check if byte is a valid 2-digit hex string |
| 90 | byteString = string.substr(i + 1, 2) |
| 91 | if (byteString.match(/[0-9a-f]{2}/i) === null) { |
| 92 | throw new InvalidInputError( |
| 93 | `Invalid percent-encoded byte '%${byteString}' at index ${i}`) |
| 94 | } |
| 95 | |
| 96 | // Decode byte |
| 97 | bytes.push(parseInt(byteString, 16)) |
| 98 | i += 3 |
| 99 | } else if (char === '+') { |
| 100 | // Handle spaces (defined in early versions of percent-encoding) |
| 101 | bytes.push(32) |
| 102 | i++ |
| 103 | } else if (unreservedURLCharacters.indexOf(char) !== -1) { |
| 104 | // Append unreserved character |
| 105 | bytes.push(char.charCodeAt(0)) |
| 106 | i++ |
| 107 | } else { |
| 108 | // Invalid character met |
| 109 | throw new InvalidInputError( |
| 110 | `Invalid character '${char}' at index ${i}`) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // This may fail due to invalid UTF-8 encoding |
| 115 | return new Uint8Array(bytes) |
| 116 | } |
| 117 | } |