Instantly share code, notes, and snippets.
Created
November 20, 2025 12:07
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save EduardoAC/8932eab6ac1ba699ebe33b9365e03d77 to your computer and use it in GitHub Desktop.
Minify through using ID hashmap to prevent long query parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| import fs from 'fs'; | |
| import crypto from 'crypto'; | |
| // Read the curl request file | |
| const curlFilePath = '{path}/curl-request-size.txt'; | |
| try { | |
| const curlCommand = fs.readFileSync(curlFilePath, 'utf-8'); | |
| // Extract the Cookie header | |
| const cookieMatch = curlCommand.match(/-H "Cookie: ([^"]+)"/); | |
| if (!cookieMatch) { | |
| console.error('β No Cookie header found'); | |
| process.exit(1); | |
| } | |
| const cookieHeader = cookieMatch[1]; | |
| // Extract dtSa cookie value | |
| const dtSaMatch = cookieHeader.match(/dtSa=([^;]+)/); | |
| if (!dtSaMatch) { | |
| console.error('β No dtSa cookie found'); | |
| process.exit(1); | |
| } | |
| const dtSaValue = dtSaMatch[1]; | |
| const dtSaDecoded = decodeURIComponent(dtSaValue); | |
| // Split by pipe to get parts | |
| const parts = dtSaDecoded.split('|'); | |
| // Find the URL part | |
| const urlPart = parts.find(part => part.startsWith('https://')); | |
| if (!urlPart) { | |
| console.error('β No URL found in dtSa cookie'); | |
| process.exit(1); | |
| } | |
| console.log('π dtSa Cookie Combination Hash ID Minification'); | |
| console.log('β'.repeat(80)); | |
| console.log('\nπ‘ Concept: Map entire query string combinations to single hash IDs'); | |
| console.log(' Instead of: theme=dark&brand=superbet&backend=betler...'); | |
| console.log(' Use: q=A7 (where A7 represents that exact combination)'); | |
| console.log(''); | |
| console.log('π ORIGINAL SIZE:'); | |
| console.log(` dtSa cookie: ${Buffer.byteLength(dtSaValue, 'utf8')} bytes (${(Buffer.byteLength(dtSaValue, 'utf8') / 1024).toFixed(2)} KB)`); | |
| console.log(` URL part: ${Buffer.byteLength(urlPart, 'utf8')} bytes (${(Buffer.byteLength(urlPart, 'utf8') / 1024).toFixed(2)} KB)`); | |
| // Base62 for hash IDs | |
| const base62Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | |
| const generateShortHash = (input, length = 2) => { | |
| const hash = crypto.createHash('sha256').update(input).digest(); | |
| let result = ''; | |
| for (let i = 0; i < length; i++) { | |
| result += base62Chars[hash[i] % 62]; | |
| } | |
| return result; | |
| }; | |
| // Extract all cashier.superbet.pl URLs and their query strings | |
| const urlParams = ['success_5Furl', 'error_5Furl', 'back_5Furl', 'pending_5Furl']; | |
| const queryStringCombinations = new Map(); | |
| urlParams.forEach(param => { | |
| const regex = new RegExp(`${param}=([^&]+)`, 'g'); | |
| const matches = [...urlPart.matchAll(regex)]; | |
| matches.forEach(match => { | |
| const encodedUrl = match[1]; | |
| // Decode to get the query string | |
| let decodedUrl = encodedUrl.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| decodedUrl = decodeURIComponent(decodeURIComponent(decodedUrl)); | |
| // Extract query string | |
| const url = new URL(decodedUrl); | |
| const queryString = url.search.substring(1); // Remove the ? | |
| if (queryString && !queryStringCombinations.has(queryString)) { | |
| const hashId = generateShortHash(queryString, 2); | |
| queryStringCombinations.set(queryString, { | |
| hashId, | |
| count: 1, | |
| params: param, | |
| originalSize: Buffer.byteLength(queryString, 'utf8'), | |
| minifiedSize: 2 + 1 + hashId.length // q=XX | |
| }); | |
| } else if (queryString) { | |
| const combo = queryStringCombinations.get(queryString); | |
| combo.count++; | |
| } | |
| } catch (e) {} | |
| }); | |
| }); | |
| // Function to minify using combination hash | |
| const minifyWithComboHash = (encodedUrl) => { | |
| let minified = encodedUrl; | |
| if (encodedUrl.includes('cashier.superbet.pl') || encodedUrl.includes('cashier_252Esuperbet_252Epl')) { | |
| // Decode to work with the URL | |
| let decoded = encodedUrl.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| decoded = decodeURIComponent(decodeURIComponent(decoded)); | |
| const url = new URL(decoded); | |
| const queryString = url.search.substring(1); | |
| if (queryString && queryStringCombinations.has(queryString)) { | |
| const combo = queryStringCombinations.get(queryString); | |
| // Replace the entire query string with just q=hashId | |
| const newUrl = `${url.origin}${url.pathname}?q=${combo.hashId}`; | |
| // Re-encode | |
| minified = encodeURIComponent(encodeURIComponent(newUrl)); | |
| // Convert to SafeCharge format | |
| minified = minified.replace(/%(\d{2}[A-Fa-f])/g, '_$1'); | |
| } | |
| } catch (e) {} | |
| } | |
| return minified; | |
| }; | |
| // Calculate individual key-value minification (baseline) | |
| const readableKeyMap = { | |
| 'theme': 't', | |
| 'brand': 'b', | |
| 'backend': 'be', | |
| 'redirectPaymentStatus': 'rps', | |
| 'redirectProvider': 'rp', | |
| 'redirectMethod': 'rm', | |
| 'redirectHasMadeDeposit': 'rhd' | |
| }; | |
| const readableValueMap = { | |
| 'dark': 'd', | |
| 'light': 'l', | |
| 'superbet': 'sb', | |
| 'betler': 'bt', | |
| 'success': 's', | |
| 'failure': 'f', | |
| 'cancelled': 'c', | |
| 'pending': 'p', | |
| 'nuvei': 'nv', | |
| 'online': 'on', | |
| 'true': '1', | |
| 'false': '0' | |
| }; | |
| const minifyIndividual = (encodedUrl) => { | |
| let minified = encodedUrl; | |
| if (encodedUrl.includes('cashier.superbet.pl') || encodedUrl.includes('cashier_252Esuperbet_252Epl')) { | |
| Object.entries(readableKeyMap).forEach(([longKey, shortKey]) => { | |
| minified = minified.replace(new RegExp(`_253F${longKey}_253D`, 'g'), `_253F${shortKey}_253D`); | |
| minified = minified.replace(new RegExp(`_2526${longKey}_253D`, 'g'), `_2526${shortKey}_253D`); | |
| }); | |
| Object.entries(readableValueMap).forEach(([longValue, shortValue]) => { | |
| minified = minified.replace(new RegExp(`_253D${longValue}_2526`, 'g'), `_253D${shortValue}_2526`); | |
| minified = minified.replace(new RegExp(`_253D${longValue}$`, 'g'), `_253D${shortValue}`); | |
| minified = minified.replace(new RegExp(`_253D${longValue}(_[0-9A-F]{2})`, 'g'), `_253D${shortValue}$1`); | |
| }); | |
| } | |
| return minified; | |
| }; | |
| // Apply minification strategies | |
| const allUrlParams = ['success_5Furl', 'error_5Furl', 'back_5Furl', 'pending_5Furl', 'parent_5Furl']; | |
| // Individual minification | |
| let individualUrl = urlPart; | |
| allUrlParams.forEach(param => { | |
| const regex = new RegExp(`(${param}=)([^&]+)`, 'g'); | |
| individualUrl = individualUrl.replace(regex, (match, prefix, url) => { | |
| return prefix + minifyIndividual(url); | |
| }); | |
| }); | |
| const individualParts = parts.map(part => part === urlPart ? individualUrl : part); | |
| const individualValue = encodeURIComponent(individualParts.join('|')); | |
| const individualSaved = Buffer.byteLength(dtSaValue, 'utf8') - Buffer.byteLength(individualValue, 'utf8'); | |
| // Combination hash minification | |
| let comboUrl = urlPart; | |
| allUrlParams.forEach(param => { | |
| const regex = new RegExp(`(${param}=)([^&]+)`, 'g'); | |
| comboUrl = comboUrl.replace(regex, (match, prefix, url) => { | |
| return prefix + minifyWithComboHash(url); | |
| }); | |
| }); | |
| const comboParts = parts.map(part => part === urlPart ? comboUrl : part); | |
| const comboValue = encodeURIComponent(comboParts.join('|')); | |
| const comboSaved = Buffer.byteLength(dtSaValue, 'utf8') - Buffer.byteLength(comboValue, 'utf8'); | |
| const additionalComboSaved = comboSaved - individualSaved; | |
| console.log('\nπ MINIFICATION COMPARISON:'); | |
| console.log(''); | |
| console.log('Strategy 1: Individual Key-Value Minification'); | |
| console.log(` dtSa cookie: ${Buffer.byteLength(individualValue, 'utf8')} bytes (${(Buffer.byteLength(individualValue, 'utf8') / 1024).toFixed(2)} KB)`); | |
| console.log(` Savings: ${individualSaved} bytes (${((individualSaved / Buffer.byteLength(dtSaValue, 'utf8')) * 100).toFixed(1)}% reduction)`); | |
| console.log(''); | |
| console.log('Strategy 2: Combination Hash IDs'); | |
| console.log(` dtSa cookie: ${Buffer.byteLength(comboValue, 'utf8')} bytes (${(Buffer.byteLength(comboValue, 'utf8') / 1024).toFixed(2)} KB)`); | |
| console.log(` Savings: ${comboSaved} bytes (${((comboSaved / Buffer.byteLength(dtSaValue, 'utf8')) * 100).toFixed(1)}% reduction)`); | |
| console.log(` Additional savings: ${additionalComboSaved} bytes (${((additionalComboSaved / individualSaved) * 100).toFixed(1)}% more than individual)`); | |
| console.log('\nπΊοΈ COMBINATION HASH TABLE:'); | |
| console.log('β'.repeat(80)); | |
| let comboIndex = 1; | |
| for (const [queryString, combo] of queryStringCombinations) { | |
| console.log(`\n${comboIndex}. Hash ID: ${combo.hashId}`); | |
| console.log(` Used in: ${combo.count} redirect URL(s)`); | |
| console.log(` Original size: ${combo.originalSize} bytes`); | |
| console.log(` Minified size: ${combo.minifiedSize} bytes (q=${combo.hashId})`); | |
| console.log(` Savings per URL: ${combo.originalSize - combo.minifiedSize} bytes`); | |
| console.log(` Total savings: ${(combo.originalSize - combo.minifiedSize) * combo.count} bytes`); | |
| console.log(` Query string: ${queryString}`); | |
| comboIndex++; | |
| } | |
| // Detailed URL comparison | |
| console.log('\n\n' + 'β'.repeat(80)); | |
| console.log('π URL COMPARISON (Success Redirect)'); | |
| console.log('β'.repeat(80)); | |
| const successUrlMatch = urlPart.match(/success_5Furl=([^&]+)/); | |
| if (successUrlMatch) { | |
| const originalEncoded = successUrlMatch[1]; | |
| const individualEncoded = minifyIndividual(originalEncoded); | |
| const comboEncoded = minifyWithComboHash(originalEncoded); | |
| let originalUrl = originalEncoded.replace(/_(\d{2})/g, '%$1'); | |
| let individualUrl = individualEncoded.replace(/_(\d{2})/g, '%$1'); | |
| let comboUrl = comboEncoded.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| originalUrl = decodeURIComponent(decodeURIComponent(originalUrl)); | |
| individualUrl = decodeURIComponent(decodeURIComponent(individualUrl)); | |
| comboUrl = decodeURIComponent(decodeURIComponent(comboUrl)); | |
| } catch (e) {} | |
| console.log('\nπ΄ ORIGINAL:'); | |
| console.log(` ${originalUrl}`); | |
| console.log(` Encoded size: ${Buffer.byteLength(originalEncoded, 'utf8')} bytes`); | |
| console.log('\nπ‘ INDIVIDUAL MINIFICATION (t=d&b=sb&be=bt...):'); | |
| console.log(` ${individualUrl}`); | |
| console.log(` Encoded size: ${Buffer.byteLength(individualEncoded, 'utf8')} bytes`); | |
| console.log(` Saved: ${Buffer.byteLength(originalEncoded, 'utf8') - Buffer.byteLength(individualEncoded, 'utf8')} bytes`); | |
| console.log('\nπ’ COMBINATION HASH (q=XX):'); | |
| console.log(` ${comboUrl}`); | |
| console.log(` Encoded size: ${Buffer.byteLength(comboEncoded, 'utf8')} bytes`); | |
| console.log(` Saved vs original: ${Buffer.byteLength(originalEncoded, 'utf8') - Buffer.byteLength(comboEncoded, 'utf8')} bytes`); | |
| console.log(` Saved vs individual: ${Buffer.byteLength(individualEncoded, 'utf8') - Buffer.byteLength(comboEncoded, 'utf8')} bytes`); | |
| } | |
| // Final comparison table | |
| console.log('\n\n' + 'β'.repeat(80)); | |
| console.log('π¦ FINAL COMPARISON TABLE'); | |
| console.log('β'.repeat(80)); | |
| console.log('\nπͺ dtSa Cookie Size:'); | |
| console.log('β'.repeat(80)); | |
| console.log('Optimization Strategy β Size β Saved β % Saved '); | |
| console.log('ββββββββββββββββββββββββββββββββΌβββββββββββΌββββββββββββΌβββββββββββ'); | |
| const originalBytes = Buffer.byteLength(dtSaValue, 'utf8'); | |
| const individualBytes = Buffer.byteLength(individualValue, 'utf8'); | |
| const comboBytes = Buffer.byteLength(comboValue, 'utf8'); | |
| console.log(`Original β ${originalBytes.toString().padStart(6)} B β - β - `); | |
| console.log(`Individual Key-Value β ${individualBytes.toString().padStart(6)} B β ${individualSaved.toString().padStart(7)} B β ${((individualSaved/originalBytes)*100).toFixed(1)}% `); | |
| console.log(`Combination Hash β ${comboBytes.toString().padStart(6)} B β ${comboSaved.toString().padStart(7)} B β ${((comboSaved/originalBytes)*100).toFixed(1)}% `); | |
| console.log('\nπ Complete Cookie Header:'); | |
| console.log('β'.repeat(80)); | |
| const originalTotal = Buffer.byteLength(cookieHeader, 'utf8'); | |
| const individualTotal = originalTotal - individualSaved; | |
| const comboTotal = originalTotal - comboSaved; | |
| console.log('Optimization Strategy β Size β Saved β % Saved '); | |
| console.log('ββββββββββββββββββββββββββββββββΌβββββββββββΌββββββββββββΌβββββββββββ'); | |
| console.log(`Original β ${(originalTotal/1024).toFixed(2)} KB β - β - `); | |
| console.log(`Individual Key-Value β ${(individualTotal/1024).toFixed(2)} KB β ${(individualSaved/1024).toFixed(2)} KB β ${((individualSaved/originalTotal)*100).toFixed(1)}% `); | |
| console.log(`Combination Hash β ${(comboTotal/1024).toFixed(2)} KB β ${(comboSaved/1024).toFixed(2)} KB β ${((comboSaved/originalTotal)*100).toFixed(1)}% `); | |
| console.log('\n⨠SUMMARY:'); | |
| console.log(` π Original dtSa cookie: ${originalBytes} bytes (${(originalBytes/1024).toFixed(2)} KB)`); | |
| console.log(` π Individual minified: ${individualBytes} bytes (${(individualBytes/1024).toFixed(2)} KB) - saves ${individualSaved} bytes`); | |
| console.log(` π Combination hash: ${comboBytes} bytes (${(comboBytes/1024).toFixed(2)} KB) - saves ${comboSaved} bytes`); | |
| console.log(` `); | |
| console.log(` π― Total cookie header: ${(originalTotal/1024).toFixed(2)} KB β ${(comboTotal/1024).toFixed(2)} KB`); | |
| console.log(` π° Combination hash saves ${additionalComboSaved} bytes more (${((additionalComboSaved/individualSaved)*100).toFixed(1)}% additional)`); | |
| console.log(` β Final size: ${(comboTotal/1024).toFixed(2)} KB (well under 8 KB limit)`); | |
| console.log(` `); | |
| console.log(` π‘ How it works:`); | |
| console.log(` - Each unique query string combination gets a 2-character hash ID`); | |
| console.log(` - Instead of: ?theme=dark&brand=superbet&backend=betler... (137 chars)`); | |
| console.log(` - Use: ?q=A7 (4 chars) - lookup table maps A7 to the full query`); | |
| console.log(` - Server maintains hashβquery lookup table`); | |
| console.log(` `); | |
| console.log(` π Benefits:`); | |
| console.log(` β Maximum compression for repeated query combinations`); | |
| console.log(` β Collision-free hash IDs`); | |
| console.log(` β Simple server-side lookup`); | |
| console.log(` `); | |
| console.log(` β οΈ Considerations:`); | |
| console.log(` - Requires server-side hash table maintenance`); | |
| console.log(` - New combinations need to be registered`); | |
| console.log(` - Cache invalidation strategy needed`); | |
| } catch (error) { | |
| console.error('β Error:', error.message); | |
| console.error(error.stack); | |
| process.exit(1); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment