From b12948bbef0697d96c23fa922c2887789017df69 Mon Sep 17 00:00:00 2001 From: Eric Spencer Date: Thu, 13 Aug 2026 23:10:00 -0500 Subject: [PATCH] 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 `?`/`#`. --- compress.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/compress.js b/compress.js index 39a794d..15049f0 100644 --- a/compress.js +++ b/compress.js @@ -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; }