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
|
||||
if (!knownSLD) {
|
||||
// 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 --) {
|
||||
number = huffmanEncode(number, domainEncode[hostname[i]]);
|
||||
}
|
||||
@@ -309,7 +309,7 @@ export function compress (input, alphabet) {
|
||||
// Encode subdomain
|
||||
if (subdomain) {
|
||||
// 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--) {
|
||||
number = huffmanEncode(number, domainEncode[subdomain[i]]);
|
||||
}
|
||||
@@ -506,6 +506,10 @@ export function decompress (input, alphabet) {
|
||||
number >>= 1n;
|
||||
}
|
||||
|
||||
const pathSplitIndex = path.search(/[?#]/);
|
||||
const pathBeforeQuery = pathSplitIndex === -1 ? path : path.slice(0, pathSplitIndex);
|
||||
const pathFromQuery = pathSplitIndex === -1 ? "" : path.slice(pathSplitIndex);
|
||||
|
||||
let output = ""
|
||||
+ (isHTTPS ? "https://" : "http://")
|
||||
+ (hasWWW ? "www." : "")
|
||||
@@ -513,8 +517,9 @@ export function decompress (input, alphabet) {
|
||||
+ domain
|
||||
+ (tld ? "." + tld : "")
|
||||
+ (hasPort ? ":" + port : "")
|
||||
+ path
|
||||
+ indexSuffix;
|
||||
+ pathBeforeQuery
|
||||
+ indexSuffix
|
||||
+ pathFromQuery;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user