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