fix: undefined search reference and misplaced index suffix
`search` was never declared, so any link whose entire path is `/index.html` or `/index.php` (the path is empty once the suffix is stripped) threw a ReferenceError whenever the hostname had a subdomain or an unknown SLD. `pathSegments.length` is what the check was reaching for. With that fixed, those links reached a second bug: `indexSuffix` was appended after the query and hash, so `x.io/index.html?q=1` decoded to `x.io?q=1/index.html`. It is now spliced back in before the first `?`/`#`.
This commit is contained in:
+9
-4
@@ -301,7 +301,7 @@ export function compress (input, alphabet) {
|
|||||||
// Encode either SLD + subdomain or full hostname
|
// Encode either SLD + subdomain or full hostname
|
||||||
if (!knownSLD) {
|
if (!knownSLD) {
|
||||||
// Write stopping token only if path follows
|
// Write stopping token only if path follows
|
||||||
if (path || search) number = huffmanEncode(number, domainEncode["END"]);
|
if (pathSegments.length > 0) number = huffmanEncode(number, domainEncode["END"]);
|
||||||
for (let i = hostname.length - 1; i >= 0; i --) {
|
for (let i = hostname.length - 1; i >= 0; i --) {
|
||||||
number = huffmanEncode(number, domainEncode[hostname[i]]);
|
number = huffmanEncode(number, domainEncode[hostname[i]]);
|
||||||
}
|
}
|
||||||
@@ -309,7 +309,7 @@ export function compress (input, alphabet) {
|
|||||||
// Encode subdomain
|
// Encode subdomain
|
||||||
if (subdomain) {
|
if (subdomain) {
|
||||||
// Write stopping token only if path follows
|
// Write stopping token only if path follows
|
||||||
if (path || search) number = huffmanEncode(number, domainEncode["END"]);
|
if (pathSegments.length > 0) number = huffmanEncode(number, domainEncode["END"]);
|
||||||
for (let i = subdomain.length - 1; i >= 0; i--) {
|
for (let i = subdomain.length - 1; i >= 0; i--) {
|
||||||
number = huffmanEncode(number, domainEncode[subdomain[i]]);
|
number = huffmanEncode(number, domainEncode[subdomain[i]]);
|
||||||
}
|
}
|
||||||
@@ -506,6 +506,10 @@ export function decompress (input, alphabet) {
|
|||||||
number >>= 1n;
|
number >>= 1n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const pathSplitIndex = path.search(/[?#]/);
|
||||||
|
const pathBeforeQuery = pathSplitIndex === -1 ? path : path.slice(0, pathSplitIndex);
|
||||||
|
const pathFromQuery = pathSplitIndex === -1 ? "" : path.slice(pathSplitIndex);
|
||||||
|
|
||||||
let output = ""
|
let output = ""
|
||||||
+ (isHTTPS ? "https://" : "http://")
|
+ (isHTTPS ? "https://" : "http://")
|
||||||
+ (hasWWW ? "www." : "")
|
+ (hasWWW ? "www." : "")
|
||||||
@@ -513,8 +517,9 @@ export function decompress (input, alphabet) {
|
|||||||
+ domain
|
+ domain
|
||||||
+ (tld ? "." + tld : "")
|
+ (tld ? "." + tld : "")
|
||||||
+ (hasPort ? ":" + port : "")
|
+ (hasPort ? ":" + port : "")
|
||||||
+ path
|
+ pathBeforeQuery
|
||||||
+ indexSuffix;
|
+ indexSuffix
|
||||||
|
+ pathFromQuery;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user