Instantly share code, notes, and snippets.
Last active
November 20, 2025 12:02
-
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/2c51c4e045aaed21e47f185244011fd7 to your computer and use it in GitHub Desktop.
Minify cookie script for keys and value for cURL request example
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 | |
| 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 FULL Minification Analysis (Keys + Values)'); | |
| 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 maps | |
| const keyMinificationMap = { | |
| 'theme': 't', | |
| 'brand': 'b', | |
| 'backend': 'be', | |
| 'redirectPaymentStatus': 'rps', | |
| 'redirectProvider': 'rp', | |
| 'redirectMethod': 'rm', | |
| 'redirectHasMadeDeposit': 'rhd' | |
| }; | |
| const valueMinificationMap = { | |
| // Theme values | |
| 'dark': 'd', | |
| 'light': 'l', | |
| // Brand values | |
| 'superbet': 'sb', | |
| // Backend values | |
| 'betler': 'bt', | |
| // Payment status values | |
| 'success': 's', | |
| 'failure': 'f', | |
| 'cancelled': 'c', | |
| 'pending': 'p', | |
| // Provider values | |
| 'nuvei': 'nv', | |
| // Method values | |
| 'online': 'on', | |
| // Boolean values | |
| 'true': '1', | |
| 'false': '0' | |
| }; | |
| // Function to minify cashier URLs (keys only) | |
| const minifyCashierUrlKeysOnly = (encodedUrl) => { | |
| 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 only | |
| Object.entries(keyMinificationMap).forEach(([longKey, shortKey]) => { | |
| 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; | |
| }; | |
| // Function to minify cashier URLs (keys and values) | |
| const minifyCashierUrlFull = (encodedUrl) => { | |
| let minified = encodedUrl; | |
| // Only minify if it contains cashier.superbet.pl | |
| if (encodedUrl.includes('cashier.superbet.pl') || encodedUrl.includes('cashier_252Esuperbet_252Epl')) { | |
| // First, replace parameter keys | |
| Object.entries(keyMinificationMap).forEach(([longKey, shortKey]) => { | |
| 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= | |
| }); | |
| // Then, replace parameter values | |
| Object.entries(valueMinificationMap).forEach(([longValue, shortValue]) => { | |
| // Replace value followed by & or end of string | |
| // Values are not encoded in the SafeCharge format, so match them directly | |
| minified = minified.replace(new RegExp(`_253D${longValue}_2526`, 'g'), `_253D${shortValue}_2526`); // =value& | |
| minified = minified.replace(new RegExp(`_253D${longValue}$`, 'g'), `_253D${shortValue}`); // =value at end | |
| // Also need to handle when value is at the end before another encoded char | |
| minified = minified.replace(new RegExp(`_253D${longValue}(_[0-9A-F]{2})`, 'g'), `_253D${shortValue}$1`); // =value_XX | |
| }); | |
| } | |
| return minified; | |
| }; | |
| // Minify the URL (keys only first) | |
| let minifiedKeysUrl = urlPart; | |
| const urlParams = ['success_5Furl', 'error_5Furl', 'back_5Furl', 'pending_5Furl', 'parent_5Furl']; | |
| urlParams.forEach(param => { | |
| const regex = new RegExp(`(${param}=)([^&]+)`, 'g'); | |
| minifiedKeysUrl = minifiedKeysUrl.replace(regex, (match, prefix, url) => { | |
| const minified = minifyCashierUrlKeysOnly(url); | |
| return prefix + minified; | |
| }); | |
| }); | |
| // Calculate keys-only minification | |
| const minifiedKeysParts = parts.map(part => part === urlPart ? minifiedKeysUrl : part); | |
| const minifiedKeysDecoded = minifiedKeysParts.join('|'); | |
| const minifiedKeysValue = encodeURIComponent(minifiedKeysDecoded); | |
| const keysSaved = Buffer.byteLength(dtSaValue, 'utf8') - Buffer.byteLength(minifiedKeysValue, 'utf8'); | |
| console.log('\nπ MINIFIED SIZE (Keys Only):'); | |
| console.log(` dtSa cookie: ${Buffer.byteLength(minifiedKeysValue, 'utf8')} bytes (${(Buffer.byteLength(minifiedKeysValue, 'utf8') / 1024).toFixed(2)} KB)`); | |
| console.log(` Savings: ${keysSaved} bytes (${((keysSaved / Buffer.byteLength(dtSaValue, 'utf8')) * 100).toFixed(1)}% reduction)`); | |
| // Now minify both keys and values | |
| let minifiedFullUrl = urlPart; | |
| urlParams.forEach(param => { | |
| const regex = new RegExp(`(${param}=)([^&]+)`, 'g'); | |
| minifiedFullUrl = minifiedFullUrl.replace(regex, (match, prefix, url) => { | |
| const minified = minifyCashierUrlFull(url); | |
| return prefix + minified; | |
| }); | |
| }); | |
| const minifiedFullParts = parts.map(part => part === urlPart ? minifiedFullUrl : part); | |
| const minifiedFullDecoded = minifiedFullParts.join('|'); | |
| const minifiedFullValue = encodeURIComponent(minifiedFullDecoded); | |
| const fullSaved = Buffer.byteLength(dtSaValue, 'utf8') - Buffer.byteLength(minifiedFullValue, 'utf8'); | |
| const additionalSaved = fullSaved - keysSaved; | |
| console.log('\nπ MINIFIED SIZE (Keys + Values):'); | |
| console.log(` dtSa cookie: ${Buffer.byteLength(minifiedFullValue, 'utf8')} bytes (${(Buffer.byteLength(minifiedFullValue, 'utf8') / 1024).toFixed(2)} KB)`); | |
| console.log(` Savings: ${fullSaved} bytes (${((fullSaved / Buffer.byteLength(dtSaValue, 'utf8')) * 100).toFixed(1)}% reduction)`); | |
| console.log(` Additional savings from values: ${additionalSaved} bytes`); | |
| console.log('\nπΊοΈ MINIFICATION MAPS:'); | |
| console.log('\n π PARAMETER KEYS:'); | |
| console.log(' βββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββββ'); | |
| console.log(' β Original Parameter β Minified β Saved β'); | |
| console.log(' βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€'); | |
| Object.entries(keyMinificationMap).forEach(([original, minified]) => { | |
| const saved = original.length - minified.length; | |
| console.log(` β ${original.padEnd(23)} β ${minified.padEnd(8)} β ${saved} chars β`); | |
| }); | |
| console.log(' βββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββββ'); | |
| console.log('\n π PARAMETER VALUES:'); | |
| console.log(' βββββββββββββββββββββββββββ¬βββββββββββ¬βββββββββββ'); | |
| console.log(' β Original Value β Minified β Saved β'); | |
| console.log(' βββββββββββββββββββββββββββΌβββββββββββΌβββββββββββ€'); | |
| Object.entries(valueMinificationMap).forEach(([original, minified]) => { | |
| const saved = original.length - minified.length; | |
| console.log(` β ${original.padEnd(23)} β ${minified.padEnd(8)} β ${saved} chars β`); | |
| }); | |
| console.log(' βββββββββββββββββββββββββββ΄βββββββββββ΄βββββββββββ'); | |
| // Helper function to parse URL | |
| 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} = ${param.value}`); | |
| console.log(` Size: ${param.size} bytes (${paramKB} KB) - ${paramPercent}% of URL`); | |
| }); | |
| } | |
| } catch (e) { | |
| console.error(`Error parsing ${label}:`, e.message); | |
| } | |
| }; | |
| // Detailed URL comparison | |
| console.log('\n\n' + 'β'.repeat(80)); | |
| console.log('π DETAILED URL ANALYSIS COMPARISON'); | |
| console.log('β'.repeat(80)); | |
| const successUrlMatch = urlPart.match(/success_5Furl=([^&]+)/); | |
| if (successUrlMatch) { | |
| const originalEncoded = successUrlMatch[1]; | |
| const minifiedKeysEncoded = minifyCashierUrlFull(originalEncoded); | |
| // Decode all versions | |
| let originalUrl = originalEncoded.replace(/_(\d{2})/g, '%$1'); | |
| let minifiedUrl = minifiedKeysEncoded.replace(/_(\d{2})/g, '%$1'); | |
| try { | |
| originalUrl = decodeURIComponent(decodeURIComponent(originalUrl)); | |
| minifiedUrl = decodeURIComponent(decodeURIComponent(minifiedUrl)); | |
| } catch (e) {} | |
| console.log('\nπ SUCCESS REDIRECT URL ANALYSIS:'); | |
| console.log('β'.repeat(80)); | |
| parseUrlDetailed(originalUrl, 'π΄ ORIGINAL'); | |
| parseUrlDetailed(minifiedUrl, '\nβ MINIFIED (Keys + Values)'); | |
| const urlSaving = Buffer.byteLength(originalEncoded, 'utf8') - Buffer.byteLength(minifiedKeysEncoded, 'utf8'); | |
| console.log(`\nπ° SAVINGS PER REDIRECT URL: ${urlSaving} bytes`); | |
| console.log(` Γ 4 redirect URLs = ${urlSaving * 4} bytes total savings`); | |
| } | |
| // Final comparison table | |
| console.log('\n\n' + 'β'.repeat(80)); | |
| console.log('π¦ FINAL COMPARISON TABLE'); | |
| console.log('β'.repeat(80)); | |
| console.log('\nπͺ dtSa Cookie Size Progression:'); | |
| console.log('β'.repeat(80)); | |
| console.log('Optimization Level β Size β vs Original β vs Previous '); | |
| console.log('βββββββββββββββββββββββΌβββββββββββΌββββββββββββββββΌβββββββββββββββ'); | |
| const originalBytes = Buffer.byteLength(dtSaValue, 'utf8'); | |
| const keysBytes = Buffer.byteLength(minifiedKeysValue, 'utf8'); | |
| const fullBytes = Buffer.byteLength(minifiedFullValue, 'utf8'); | |
| console.log(`Original β ${originalBytes.toString().padStart(6)} B β - β - `); | |
| console.log(`Keys Minified β ${keysBytes.toString().padStart(6)} B β -${keysSaved.toString().padStart(4)} B (-${((keysSaved/originalBytes)*100).toFixed(1)}%) β -${keysSaved.toString().padStart(4)} B `); | |
| console.log(`Keys + Values Minifiedβ ${fullBytes.toString().padStart(6)} B β -${fullSaved.toString().padStart(4)} B (-${((fullSaved/originalBytes)*100).toFixed(1)}%) β -${additionalSaved.toString().padStart(4)} B `); | |
| console.log('\nπ Complete Cookie Header:'); | |
| console.log('β'.repeat(80)); | |
| const originalTotal = Buffer.byteLength(cookieHeader, 'utf8'); | |
| const keysTotal = originalTotal - keysSaved; | |
| const fullTotal = originalTotal - fullSaved; | |
| console.log('Optimization Level β Size β Saved β % Saved '); | |
| console.log('βββββββββββββββββββββββΌβββββββββββΌβββββββββββΌβββββββββββ'); | |
| console.log(`Original β ${(originalTotal/1024).toFixed(2)} KB β - β - `); | |
| console.log(`Keys Minified β ${(keysTotal/1024).toFixed(2)} KB β ${(keysSaved/1024).toFixed(2)} KB β ${((keysSaved/originalTotal)*100).toFixed(1)}% `); | |
| console.log(`Keys + Values Minifiedβ ${(fullTotal/1024).toFixed(2)} KB β ${(fullSaved/1024).toFixed(2)} KB β ${((fullSaved/originalTotal)*100).toFixed(1)}% `); | |
| console.log('\n⨠SUMMARY:'); | |
| console.log(` π Original dtSa cookie: ${originalBytes} bytes (${(originalBytes/1024).toFixed(2)} KB)`); | |
| console.log(` π Keys only minified: ${keysBytes} bytes (${(keysBytes/1024).toFixed(2)} KB) - saves ${keysSaved} bytes`); | |
| console.log(` π Keys + Values minified: ${fullBytes} bytes (${(fullBytes/1024).toFixed(2)} KB) - saves ${fullSaved} bytes`); | |
| console.log(` `); | |
| console.log(` π― Total cookie header reduction: ${(originalTotal/1024).toFixed(2)} KB β ${(fullTotal/1024).toFixed(2)} KB`); | |
| console.log(` π° Total savings: ${fullSaved} bytes (${((fullSaved/originalTotal)*100).toFixed(1)}% of cookie header)`); | |
| console.log(` β Final size: ${(fullTotal/1024).toFixed(2)} KB (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