@@ -4,6 +4,30 @@ David Quesenberry
4403/22/2026
55CookieStorageService.js
66*/
7+ function safeDecodeCookiePart ( value ) {
8+ try {
9+ return decodeURIComponent ( String ( value || '' ) ) ;
10+ } catch {
11+ return String ( value || '' ) ;
12+ }
13+ }
14+
15+ function parseCookieString ( cookieText ) {
16+ const text = String ( cookieText || '' ) . trim ( ) ;
17+ if ( ! text ) {
18+ return [ ] ;
19+ }
20+ return text . split ( ';' ) . map ( ( part ) => part . trim ( ) ) . filter ( Boolean ) . map ( ( part ) => {
21+ const separatorIndex = part . indexOf ( '=' ) ;
22+ const rawName = separatorIndex >= 0 ? part . slice ( 0 , separatorIndex ) : part ;
23+ const rawValue = separatorIndex >= 0 ? part . slice ( separatorIndex + 1 ) : '' ;
24+ return {
25+ key : safeDecodeCookiePart ( rawName ) ,
26+ rawValue : safeDecodeCookiePart ( rawValue )
27+ } ;
28+ } ) . filter ( ( entry ) => entry . key ) ;
29+ }
30+
731export default class CookieStorageService {
832 constructor ( { documentRef = globalThis . document ?? null } = { } ) {
933 this . documentRef = documentRef ;
@@ -47,4 +71,39 @@ export default class CookieStorageService {
4771 this . documentRef . cookie = `${ encodeURIComponent ( name ) } =; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=${ path } ` ;
4872 return true ;
4973 }
74+
75+ readCookieString ( ) {
76+ try {
77+ return String ( this . documentRef ?. cookie || '' ) ;
78+ } catch {
79+ return '' ;
80+ }
81+ }
82+
83+ entries ( ) {
84+ return parseCookieString ( this . readCookieString ( ) ) ;
85+ }
86+
87+ removeEverywhere ( name ) {
88+ if ( ! this . documentRef || ! name ) {
89+ return { ok : false , message : 'cookie entry does not include a supported document and key' } ;
90+ }
91+
92+ try {
93+ const encodedKey = encodeURIComponent ( name ) ;
94+ const expires = 'expires=Thu, 01 Jan 1970 00:00:00 GMT' ;
95+ const maxAge = 'Max-Age=0' ;
96+ this . documentRef . cookie = `${ encodedKey } =; ${ maxAge } ; ${ expires } ; path=/` ;
97+ const pathname = String ( this . documentRef . location ?. pathname || '/' ) ;
98+ const pathSegments = pathname . split ( '/' ) . filter ( Boolean ) ;
99+ let currentPath = '' ;
100+ pathSegments . forEach ( ( segment ) => {
101+ currentPath += `/${ segment } ` ;
102+ this . documentRef . cookie = `${ encodedKey } =; ${ maxAge } ; ${ expires } ; path=${ currentPath } ` ;
103+ } ) ;
104+ return { ok : true } ;
105+ } catch ( error ) {
106+ return { ok : false , message : error . message } ;
107+ }
108+ }
50109}
0 commit comments