Instantly share code, notes, and snippets.
Created
November 20, 2025 12:04
-
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/5b65631de2c819ac0d8c8b603e490bfe to your computer and use it in GitHub Desktop.
Minify example to show how to minify example using cookie dsTa
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'; | |
| // 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 (should contain https://secure.safecharge.com) | |
| 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 URL Minification Analysis'); | |
| console.log('β'.repeat(80)); | |
| console.log('\nπ 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)`); | |
| // Create minification map for cashier.superbet.pl query parameters | |
| const paramMinificationMap = { | |
| 'theme': 't', | |
| 'brand': 'b', | |
| 'backend': 'be', | |
| 'redirectPaymentStatus': 'rps', | |
| 'redirectProvider': 'rp', | |
| 'redirectMethod': 'rm', | |
| 'redirectHasMadeDeposit': 'rhd' | |
| }; | |
| // Function to minify cashier URLs | |
| const minifyCashierUrl = (encodedUrl) => { | |
| // Work directly with the encoded string to replace parameter names | |
| let minified = encodedUrl; | |
| // Only minify if it contains cashier.superbet.pl | |
| if (encodedUrl.includes('cashier.superbet.pl') || encodedUrl.includes('cashier_252Esuperbet_252Epl')) { | |
| // Replace parameter keys with minified versions in the encoded format | |
| // Need to handle both _253F (?) and _2526 (&) as separators | |
| Object.entries(paramMinificationMap).forEach(([longKey, shortKey]) => { | |
| // Replace ?param= and ¶m= | |
| // In SafeCharge encoding: ? = _253F, & = _2526 | |
| minified = minified.replace(new RegExp(`_253F${longKey}_253D`, 'g'), `_253F${shortKey}_253D`); // ?key= | |
| minified = minified.replace(new RegExp(`_2526${longKey}_253D`, 'g'), `_2526${shortKey}_253D`); // &key= | |
| }); | |
| } | |
| return minified; | |
| }; | |
| // Minify the URL | |
| let minifiedUrl = urlPart; | |
| // Find and replace all redirect URLs (success_5Furl, error_5Furl, back_5Furl, pending_5Furl) | |
| const urlParams = ['success_5Furl', 'error_5Furl', 'back_5Furl', 'pending_5Furl', 'parent_5Furl']; | |
| urlParams.forEach(param => { | |
| const regex = new RegExp(`(${param}=)([^&]+)`, 'g'); | |
| minifiedUrl = minifiedUrl.replace(regex, (match, prefix, url) => { | |
| const minified = minifyCashierUrl(url); | |
| return prefix + minified; | |
| }); | |
| }); | |
| // Reconstruct dtSa cookie with minified URL | |
| const minifiedParts = parts.map(part => part === urlPart ? minifiedUrl : part); | |
| const minifiedDtSaDecoded = minifiedParts.join('|'); | |
| const minifiedDtSaValue = encodeURIComponent(minifiedDtSaDecoded); | |
| console.log('\nπ MINIFIED SIZE:'); | |
| console.log(` dtSa cookie: ${Buffer.byteLength(minifiedDtSaValue, 'utf8')} bytes (${(Buffer.byteLength(minifiedDtSaValue, 'utf8') / 1024).toFixed(2)} KB)`); | |
| console.log(` URL part: ${Buffer.byteLength(minifiedUrl, 'utf8')} bytes (${(Buffer.byteLength(minifiedUrl, 'utf8') / 1024).toFixed(2)} KB)`); | |
| const savedBytes = Buffer.byteLength(dtSaValue, 'utf8') - Buffer.byteLength(minifiedDtSaValue, 'utf8'); | |
| const savedPercent = ((savedBytes / Buffer.byteLength(dtSaValue, 'utf8')) * 100).toFixed(1); | |
| const urlSavedBytes = Buffer.byteLength(urlPart, 'utf8') - Buffer.byteLength(minifiedUrl, 'utf8'); | |
| const urlSavedPercent = ((urlSavedBytes / Buffer.byteLength(urlPart, 'utf8')) * 100).toFixed(1); | |
| console.log('\nπ° SAVINGS:'); | |
| console.log(` dtSa cookie: ${savedBytes} bytes saved (${savedPercent}% reduction)`); | |
| console.log(` URL part: ${urlSavedBytes} bytes saved (${urlSavedPercent}% reduction)`); | |
| console.log('\nπΊοΈ MINIFICATION MAP (cashier.superbet.pl parameters only):'); | |
| console.log(' βββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββββ'); | |
| console.log(' β Original Parameter β Minified β Saved β'); | |
| console.log(' βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€'); | |
| Object.entries(paramMinificationMap).forEach(([original, minified]) => { | |
| const saved = original.length - minified.length; | |
| console.log(` β ${original.padEnd(23)} β ${minified.padEnd(8)} β ${saved} chars β`); | |
| }); | |
| console.log(' βββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββββ'); | |
| // Count occurrences of each parameter in the URL | |
| console.log('\nπ PARAMETER OCCURRENCE COUNT (in cashier.superbet.pl redirect URLs):'); | |
| Object.entries(paramMinificationMap).forEach(([original, minified]) => { | |
| // Look for encoded versions: _253Fparam_253D (?) or _2526param_253D (&) | |
| const regex1 = new RegExp(`_253F${original}_253D`, 'g'); | |
| const regex2 = new RegExp(`_2526${original}_253D`, 'g'); | |
| const matches1 = urlPart.match(regex1) || []; | |
| const matches2 = urlPart.match(regex2) || []; | |
| const count = matches1.length + matches2.length; | |
| if (count > 0) { | |
| const totalSaved = (original.length - minified.length) * count; | |
| console.log(` ${original}: ${count}x (saves ${totalSaved} chars total)`); | |
| } | |
| }); | |
| console.log('\nπ EXAMPLE COMPARISON:'); | |
| console.log('\n BEFORE:'); | |
| console.log(' theme=dark&brand=superbet&backend=betler&redirectPaymentStatus=success'); | |
| console.log(' (67 characters)\n'); | |
| console.log(' AFTER:'); | |
| console.log(' t=dark&b=superbet&be=betler&rps=success'); | |
| console.log(' (40 characters)\n'); | |
| console.log(' Savings: 27 characters (40.3% reduction) per redirect URL'); | |
| // Show decoded URLs for comparison | |
| console.log('\nπ SAMPLE REDIRECT URL COMPARISON:'); | |
| const sampleMatch = urlPart.match(/success_5Furl=([^&]+)/); | |
| if (sampleMatch) { | |
| const originalEncoded = sampleMatch[1]; | |
| let originalDecoded = originalEncoded.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| originalDecoded = decodeURIComponent(originalDecoded); | |
| } catch (e) {} | |
| console.log('\n ORIGINAL success_url:'); | |
| console.log(` ${originalDecoded.substring(0, 120)}...`); | |
| console.log(` Size: ${Buffer.byteLength(originalEncoded, 'utf8')} bytes\n`); | |
| const minifiedEncoded = minifyCashierUrl(originalEncoded); | |
| let minifiedDecoded = minifiedEncoded.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| minifiedDecoded = decodeURIComponent(minifiedDecoded); | |
| } catch (e) {} | |
| console.log(' MINIFIED success_url:'); | |
| console.log(` ${minifiedDecoded.substring(0, 120)}...`); | |
| console.log(` Size: ${Buffer.byteLength(minifiedEncoded, 'utf8')} bytes`); | |
| console.log(` Saved: ${Buffer.byteLength(originalEncoded, 'utf8') - Buffer.byteLength(minifiedEncoded, 'utf8')} bytes per redirect URL`); | |
| } | |
| console.log('\nβ Total cookie header size would go from 4.25 KB to approximately ' + | |
| `${((Buffer.byteLength(cookieHeader, 'utf8') - savedBytes) / 1024).toFixed(2)} KB`); | |
| // Detailed URL parsing comparison | |
| console.log('\n\n' + 'β'.repeat(80)); | |
| console.log('π DETAILED URL ANALYSIS COMPARISON'); | |
| console.log('β'.repeat(80)); | |
| // Helper function to parse URL and extract query params | |
| const parseUrlDetailed = (urlString, label) => { | |
| try { | |
| const url = new URL(urlString); | |
| const queryParams = []; | |
| url.searchParams.forEach((value, key) => { | |
| const size = Buffer.byteLength(`${key}=${value}`, 'utf8'); | |
| queryParams.push({ key, value, size }); | |
| }); | |
| queryParams.sort((a, b) => b.size - a.size); | |
| console.log(`\n${label}:`); | |
| console.log(`ββ Scheme: ${url.protocol.replace(':', '')}`); | |
| console.log(`ββ Hostname: ${url.hostname}`); | |
| console.log(`ββ Pathname: ${url.pathname}`); | |
| console.log(`ββ Total URL Size: ${Buffer.byteLength(urlString, 'utf8')} bytes (${(Buffer.byteLength(urlString, 'utf8') / 1024).toFixed(2)} KB)`); | |
| if (queryParams.length > 0) { | |
| console.log(`\nπ Query String Parameters (${queryParams.length} total):`); | |
| console.log('β'.repeat(80)); | |
| queryParams.forEach((param, idx) => { | |
| const paramKB = (param.size / 1024).toFixed(3); | |
| const paramPercent = ((param.size / Buffer.byteLength(urlString, 'utf8')) * 100).toFixed(1); | |
| console.log(`\n${idx + 1}. ${param.key}`); | |
| console.log(` Size: ${param.size} bytes (${paramKB} KB) - ${paramPercent}% of URL`); | |
| const displayValue = param.value.length > 80 ? param.value.substring(0, 80) + '...' : param.value; | |
| console.log(` Value: ${displayValue}`); | |
| }); | |
| } | |
| return queryParams; | |
| } catch (e) { | |
| console.error(`Error parsing ${label}:`, e.message); | |
| return []; | |
| } | |
| }; | |
| // Find a redirect URL to analyze in detail | |
| const successUrlMatch = urlPart.match(/success_5Furl=([^&]+)/); | |
| if (successUrlMatch) { | |
| const originalEncoded = successUrlMatch[1]; | |
| const minifiedEncoded = minifyCashierUrl(originalEncoded); | |
| // Decode both for display - need double decode | |
| let originalUrl = originalEncoded.replace(/_(\d{2})/g, '%$1'); | |
| let minifiedUrl = minifiedEncoded.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| // First decode | |
| originalUrl = decodeURIComponent(originalUrl); | |
| minifiedUrl = decodeURIComponent(minifiedUrl); | |
| // Second decode (because it's double-encoded) | |
| originalUrl = decodeURIComponent(originalUrl); | |
| minifiedUrl = decodeURIComponent(minifiedUrl); | |
| } catch (e) { | |
| console.log(`Decode error: ${e.message}`); | |
| } | |
| console.log('\n\nπ SUCCESS REDIRECT URL ANALYSIS:'); | |
| console.log('β'.repeat(80)); | |
| parseUrlDetailed(originalUrl, 'π΄ ORIGINAL (before minification)'); | |
| parseUrlDetailed(minifiedUrl, '\nβ MINIFIED (after minification)'); | |
| const urlSaving = Buffer.byteLength(originalEncoded, 'utf8') - Buffer.byteLength(minifiedEncoded, 'utf8'); | |
| console.log(`\nπ° SAVINGS PER REDIRECT URL: ${urlSaving} bytes`); | |
| console.log(` Γ 4 redirect URLs = ${urlSaving * 4} bytes total savings`); | |
| } | |
| // Final dtSa cookie analysis | |
| console.log('\n\n' + 'β'.repeat(80)); | |
| console.log('π¦ FINAL dtSa COOKIE ANALYSIS'); | |
| console.log('β'.repeat(80)); | |
| const originalCookieBytes = Buffer.byteLength(dtSaValue, 'utf8'); | |
| const minifiedCookieBytes = Buffer.byteLength(minifiedDtSaValue, 'utf8'); | |
| console.log('\nπͺ dtSa Cookie Breakdown:'); | |
| console.log('β'.repeat(80)); | |
| console.log(' β ORIGINAL β MINIFIED β SAVED β % SAVED '); | |
| console.log('βββββββββββββββββββββΌβββββββββββββΌβββββββββββββΌββββββββββββΌβββββββββββ'); | |
| console.log(`dtSa Cookie Total β ${originalCookieBytes.toString().padStart(8)} B β ${minifiedCookieBytes.toString().padStart(8)} B β ${savedBytes.toString().padStart(7)} B β ${savedPercent.padStart(7)}% `); | |
| console.log(`URL Component β ${Buffer.byteLength(urlPart, 'utf8').toString().padStart(8)} B β ${Buffer.byteLength(minifiedUrl, 'utf8').toString().padStart(8)} B β ${urlSavedBytes.toString().padStart(7)} B β ${urlSavedPercent.padStart(7)}% `); | |
| // Calculate new total cookie header size | |
| const originalTotalHeader = Buffer.byteLength(cookieHeader, 'utf8'); | |
| const newTotalHeader = originalTotalHeader - savedBytes; | |
| console.log('\nπ Complete Cookie Header:'); | |
| console.log('β'.repeat(80)); | |
| console.log(' β ORIGINAL β MINIFIED β SAVED β % SAVED '); | |
| console.log('βββββββββββββββββββββΌβββββββββββββΌβββββββββββββΌββββββββββββΌβββββββββββ'); | |
| console.log(`Total Cookie Header β ${originalTotalHeader.toString().padStart(8)} B β ${newTotalHeader.toString().padStart(8)} B β ${savedBytes.toString().padStart(7)} B β ${((savedBytes/originalTotalHeader)*100).toFixed(1).padStart(7)}% `); | |
| console.log(` β ${(originalTotalHeader/1024).toFixed(2).padStart(7)} KB β ${(newTotalHeader/1024).toFixed(2).padStart(7)} KB β ${(savedBytes/1024).toFixed(2).padStart(6)} KB β `); | |
| console.log('\n⨠SUMMARY:'); | |
| console.log(` β’ Minifying 7 query parameters in cashier.superbet.pl redirect URLs`); | |
| console.log(` β’ Each parameter appears 4 times (success, error, back, pending URLs)`); | |
| console.log(` β’ Total savings: ${savedBytes} bytes (${savedPercent}% of dtSa cookie)`); | |
| console.log(` β’ New dtSa size: ${minifiedCookieBytes} bytes (${(minifiedCookieBytes/1024).toFixed(2)} KB)`); | |
| console.log(` β’ New total cookie header: ${newTotalHeader} bytes (${(newTotalHeader/1024).toFixed(2)} KB)`); | |
| console.log(` β’ Still well under 8 KB limit β `); | |
| } catch (error) { | |
| console.error('β Error:', error.message); | |
| process.exit(1); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment