1// ============================================================================ 2// Cookie utility in `module Website` namespace format. 3// 4// USAGE: 5// const cu = new Website.CookieUtils(); 6// cu.set('token', 'abc', { expires: 7 }); 7// cu.get('token'); 8// cu.isCookiesEnabled(); 9// 10// Angular 1 DI injection: 11// static $inject = ['CookieUtils']; 12// constructor(private CookieUtils: Website.CookieUtils) {} 13// ============================================================================ 14var Website; 15(function (Website) { 16 var CookiesManager = /** @class */ (function () { 17 function CookiesManager() { 18 // null = not yet tested | true = cookies ON | false = cookies OFF 19 this._cookiesEnabledCache = null; 20 } 21 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 22 // DOMAIN HELPER 23 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 24 /** 25 * Derives the shared root domain so cookies are accessible across 26 * all sub-domains. 27 * 28 * 'www.xyz.com' â '.xyz.com' 29 * 'app.xyz.com' â '.app.xyz.com' 30 * 'localhost' â '' 31 * '192.168.1.1' â '' 32 */ 33 CookiesManager.prototype.getCookieDomain = function () { 34 var host = window.location.hostname; 35 return host; 36 //const stripped: string = host.toLowerCase().startsWith('www.') 37 // ? host.substring(4) 38 // : host; 39 //if (stripped === 'localhost' || /^\d{1,3}(\.\d{1,3}){3}$/.test(stripped)) { 40 // return ''; 41 //} 42 //return '.' + stripped; 43 }; 44 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 45 // COOKIE-ENABLED DETECTION 46 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 47 /** 48 * Two-layer check: 49 * Layer 1 â navigator.cookieEnabled (fast path) 50 * Layer 2 â probe write â read â delete 51 * Catches privacy extensions that report cookieEnabled=true 52 * but silently discard writes. 53 * 54 * Result is cached after the first call. 55 */ 56 CookiesManager.prototype.isCookiesEnabled = function () { 57 if (this._cookiesEnabledCache !== null) { 58 return this._cookiesEnabledCache; 59 } 60 if (!navigator.cookieEnabled) { 61 this._cookiesEnabledCache = false; 62 return false; 63 } 64 var PROBE = '__ck_probe__'; 65 try { 66 document.cookie = PROBE + '=1; path=/'; 67 var found = document.cookie.indexOf(PROBE + '=') !== -1; 68 document.cookie = PROBE + '=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'; 69 this._cookiesEnabledCache = found; 70 return found; 71 } 72 catch (e) { 73 this._cookiesEnabledCache = false; 74 return false; 75 } 76 }; 77 /** Clears the cached result so the probe runs again on the next call. */ 78 CookiesManager.prototype.resetCookieEnabledCache = function () { 79 this._cookiesEnabledCache = null; 80 }; 81 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 82 // PRIVATE HELPERS 83 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 84 CookiesManager.prototype.warnDisabled = function (method, name) { 85 var label = name ? "(\"" + name + "\")" : '()'; 86 console.warn("[Website.CookieUtils] " + method + label + " skipped \u2014 cookies are disabled."); 87 }; 88 /** 89 * Builds the cookie attribute string from options. 90 * 91 * max-age vs expires precedence: 92 * If BOTH are supplied, max-age wins in every modern browser because 93 * the spec says max-age takes priority over expires. 94 * Best practice: supply one or the other, not both. 95 * 96 * max-age values: 97 * 0 or negative â delete the cookie immediately (same as remove()) 98 * positive â seconds until expiry 99 * 100 * Defaults: 101 * path â '/' 102 * domain â getCookieDomain() 103 * secure â true 104 * sameSite â commented out (uncomment to enable) 105 */ 106 CookiesManager.prototype.buildAttributes = function (opts) { 107 var parts = []; 108 // ââ max-age (seconds) â takes priority over expires in modern browsers 109 if (opts.maxAge !== undefined && opts.maxAge !== null) { 110 parts.push('max-age=' + Math.round(opts.maxAge)); 111 } 112 // ââ expires (Date or days-as-number) â fallback when max-age absent 113 if (opts.expires !== undefined && opts.expires !== null) { 114 var expDate = void 0; 115 if (typeof opts.expires === 'number') { 116 expDate = new Date(); 117 expDate.setTime(expDate.getTime() + opts.expires * 24 * 60 * 60 * 1000); 118 } 119 else { 120 expDate = opts.expires; 121 } 122 parts.push('expires=' + expDate.toUTCString()); 123 } 124 // ââ path (default: '/') 125 parts.push('path=' + (opts.path !== undefined ? opts.path : '/')); 126 // ââ domain (default: getCookieDomain()) 127 //const domain: string = opts.domain !== undefined 128 // ? opts.domain 129 // : this.getCookieDomain(); 130 //if (domain) { 131 // parts.push('domain=' + domain); 132 //} 133 // ââ secure (default: true) 134 // const useSecure: boolean = opts.secure !== undefined ? opts.secure : true; 135 //if (useSecure) { 136 parts.push('secure=true'); 137 //} 138 if (opts.sameSite !== undefined && opts.sameSite !== null) { 139 parts.push('sameSite=' + opts.sameSite); 140 } 141 return parts.join('; '); 142 }; 143 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 144 // PUBLIC API 145 // âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ 146 /** 147 * Write or update a cookie. 148 * Returns true if written, false if cookies are disabled. 149 * 150 * @param name Cookie name. 151 * @param value Cookie value (URI-encoded automatically). 152 * @param options Cookie attributes â see below. 153 * @returns true if written, false if cookies are disabled. 154 * 155 * Options 156 * âââââââ 157 * expires number | Date Days from now (number) or exact Date. 158 * Ignored when maxAge is also supplied. 159 * 160 * maxAge number Lifetime in SECONDS. 161 * Takes priority over expires in all modern browsers. 162 * maxAge: 3600 â 1 hour
163 * maxAge: 86400 â 1 day 164 * maxAge: 86400 * 7 â 1 week 165 * maxAge: 0 â delete immediately 166 * maxAge: -1 â delete immediately 167 * 168 * path string Default: '/' 169 * domain string Default: getCookieDomain() 170 * secure boolean Default: true 171 * sameSite string 'Lax' | 'Strict' | 'None' 172 * 173 * @example 174 * // Using expires (days) 175 * cu.set('authToken', 'xyz', { expires: 7 }); 176 * 177 * // Using max-age (seconds) â preferred for precision 178 * cu.set('authToken', 'xyz', { maxAge: 86400 * 7 }); // 7 days 179 * cu.set('sessionId', 'abc', { maxAge: 3600 }); // 1 hour 180 * cu.set('old', '', { maxAge: 0 }); // delete 181 */ 182 CookiesManager.prototype.set = function (name, value, options) { 183 if (!this.isCookiesEnabled()) { 184 this.warnDisabled('set', name); 185 return false; 186 } 187 var opts = options || {}; 188 document.cookie = name + '=' 189 + String(value) + '; ' 190 + this.buildAttributes(opts); 191 return true; 192 }; 193 /** 194 * Read a cookie value by name. 195 * Returns null if not found or cookies are disabled. 196 * 197 * @example 198 * const token = cu.get('authToken'); // string | null 199 */ 200 CookiesManager.prototype.get = function (name) { 201 if (!this.isCookiesEnabled()) { 202 this.warnDisabled('get', name); 203 return null; 204 } 205 var nameEQ = name + '='; 206 var cookies = document.cookie.split(';'); 207 for (var i = 0; i < cookies.length; i++) { 208 var c = cookies[i]; 209 while (c.charAt(0) === ' ') { 210 c = c.substring(1); 211 } 212 if (c.indexOf(nameEQ) === 0) { 213 return c.substring(nameEQ.length); 214 } 215 } 216 return null; 217 }; 218 /** 219 * Check whether a specific cookie exists. 220 * Returns false if cookies are disabled. 221 * 222 * @example 223 * if (cu.exists('authToken')) { ... } 224 */ 225 CookiesManager.prototype.exists = function (name) { 226 if (!this.isCookiesEnabled()) { 227 this.warnDisabled('exists', name); 228 return false; 229 } 230 return this.get(name) !== null; 231 }; 232 /** 233 * Delete a cookie by name. 234 * Pass the same domain/path used in set() or it won't be found. 235 * Does nothing if cookies are disabled. 236 * 237 * Uses max-age=0 (instant expiry) as the primary deletion mechanism, 238 * with expires=epoch as a fallback for older browsers. 239 * 240 * @example 241 * cu.remove('authToken'); 242 */ 243 CookiesManager.prototype.remove = function (name, options) { 244 if (!this.isCookiesEnabled()) { 245 this.warnDisabled('remove', name); 246 return; 247 } 248 var opts = options || {}; 249 opts.maxAge = 0; // modern browsers â instant expiry 250 opts.expires = new Date(0); // legacy fallback â epoch 251 this.set(name, '', opts); 252 }; 253 /** 254 * Return all current cookies as a plain keyâvalue object. 255 * Returns {} if cookies are disabled. 256 * 257 * @example 258 * const all = cu.getAll(); 259 * console.log(all['authToken']); 260 */ 261 CookiesManager.prototype.getAll = function () { 262 if (!this.isCookiesEnabled()) { 263 this.warnDisabled('getAll'); 264 return {}; 265 } 266 var result = {}; 267 var cookies = document.cookie.split(';'); 268 for (var i = 0; i < cookies.length; i++) { 269 var c = cookies[i].trim(); 270 var eqIdx = c.indexOf('='); 271 if (eqIdx > 0) { 272 var key = c.substring(0, eqIdx); 273 var val = c.substring(eqIdx + 1); 274 result[key] = val; 275 } 276 } 277 return result; 278 }; 279 CookiesManager.prototype.getCookie = function (name) { 280 //const value = `; ${document.cookie}`; 281 //const parts = value.split(`; ${name}=`); 282 //if (parts.length === 2) 283 // return parts.pop().split(";").shift(); 284 //return null; 285 return this.get(name); 286 }; 287 CookiesManager.prototype.deleteCookie = function (key) { 288 /*document.cookie = key + "=; path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC";*/ 289 this.remove(key, { path: '/' });
290 }; 291 CookiesManager.prototype.updateCookie = function (key, value) { 292 // Delete existing value first (mirrors original behaviour) 293 if (this.exists(key)) { 294 this.remove(key); 295 } 296 // Expire in 24 hours (24 * 60 minutes = 1 day = 1 in expires-days) 297 this.set(key, value, { 298 expires: 1, 299 path: '/', 300 secure: true // â real Secure flag, not a string 301 }); 302 }; 303 CookiesManager.prototype.localStorageAvailable = function (type) { 304 var storage; 305 try { 306 storage = window[type]; 307 var x = 'localStorageAvailable'; 308 storage.setItem(x, x); 309 storage.removeItem(x); 310 return true; 311 } 312 catch (e) { 313 return false; 314 } 315 }; 316 return CookiesManager; 317 }()); 318 Website.CookiesManager = CookiesManager; 319})(Website || (Website = {})); 320//# sourceMappingURL=CookiesManager.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.