astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
34 lines (33 loc) • 764 B
JavaScript
class MultiLevelEncodingError extends Error {
constructor() {
super("URL encoding depth exceeded the maximum number of decode iterations");
this.name = "MultiLevelEncodingError";
}
}
const MAX_DECODE_ITERATIONS = 10;
function validateAndDecodePathname(pathname) {
let decoded;
try {
decoded = decodeURI(pathname);
} catch (_e) {
throw new Error("Invalid URL encoding");
}
let iterations = 0;
while (decoded !== pathname) {
if (iterations >= MAX_DECODE_ITERATIONS) {
throw new MultiLevelEncodingError();
}
pathname = decoded;
try {
decoded = decodeURI(pathname);
} catch {
break;
}
iterations++;
}
return decoded;
}
export {
MultiLevelEncodingError,
validateAndDecodePathname
};