MCPcopy Create free account
hub / github.com/nodejs/node / percentDecode

Function percentDecode

deps/undici/src/lib/web/fetch/data-url.js:174–215  ·  view source on GitHub ↗

@param {Uint8Array} input

(input)

Source from the content-addressed store, hash-verified

172// https://url.spec.whatwg.org/#percent-decode
173/** @param {Uint8Array} input */
174function percentDecode (input) {
175 const length = input.length
176 // 1. Let output be an empty byte sequence.
177 /** @type {Uint8Array} */
178 const output = new Uint8Array(length)
179 let j = 0
180 let i = 0
181 // 2. For each byte byte in input:
182 while (i < length) {
183 const byte = input[i]
184
185 // 1. If byte is not 0x25 (%), then append byte to output.
186 if (byte !== 0x25) {
187 output[j++] = byte
188
189 // 2. Otherwise, if byte is 0x25 (%) and the next two bytes
190 // after byte in input are not in the ranges
191 // 0x30 (0) to 0x39 (9), 0x41 (A) to 0x46 (F),
192 // and 0x61 (a) to 0x66 (f), all inclusive, append byte
193 // to output.
194 } else if (
195 byte === 0x25 &&
196 !(isHexCharByte(input[i + 1]) && isHexCharByte(input[i + 2]))
197 ) {
198 output[j++] = 0x25
199
200 // 3. Otherwise:
201 } else {
202 // 1. Let bytePoint be the two bytes after byte in input,
203 // decoded, and then interpreted as hexadecimal number.
204 // 2. Append a byte whose value is bytePoint to output.
205 output[j++] = (hexByteToNumber(input[i + 1]) << 4) | hexByteToNumber(input[i + 2])
206
207 // 3. Skip the next two bytes in input.
208 i += 2
209 }
210 ++i
211 }
212
213 // 3. Return output.
214 return length === j ? output : output.subarray(0, j)
215}
216
217// https://mimesniff.spec.whatwg.org/#parse-a-mime-type
218/** @param {string} input */

Callers 1

stringPercentDecodeFunction · 0.70

Calls 2

isHexCharByteFunction · 0.70
hexByteToNumberFunction · 0.70

Tested by

no test coverage detected