@@ -66,45 +66,51 @@ class ZitiClient {
6666
6767 CookieInterceptor . init ( ) ; // Hijack the `document.cookie` object
6868
69- CookieInterceptor . write . use ( async function ( cookie ) {
69+ CookieInterceptor . write . use ( function ( cookie ) {
7070
71- console . log ( '=====> CookieInterceptor sees write of Cookie: ' , cookie ) ;
71+ ( async function ( ) { // we use an IIFE because we need to run some await calls, and we cannot make
72+ // our write.use() an async func because it will then return a Promise,
73+ // which would cause Cookie storage in the browser to get corrupted.
74+
75+ console . log ( '=====> CookieInterceptor sees write of Cookie: ' , cookie ) ;
7276
73- const release = await ziti . _cookiemutex . acquire ( ) ;
74-
75- let zitiCookies = await ls . getWithExpiry ( zitiConstants . get ( ) . ZITI_COOKIES ) ;
76- if ( isNull ( zitiCookies ) ) {
77- zitiCookies = { }
78- }
79- console . log ( '=====> CookieInterceptor ZITI_COOKIES (before): ' , zitiCookies ) ;
80-
81- let name = cookie . substring ( 0 , cookie . indexOf ( "=" ) ) ;
82- let value = cookie . substring ( cookie . indexOf ( "=" ) + 1 ) ;
83- let cookie_value = value . substring ( 0 , value . indexOf ( ";" ) ) ;
84- let parts = value . split ( ";" ) ;
85- let cookiePath ;
86- let expires ;
87- for ( let j = 0 ; j < parts . length ; j ++ ) {
88- let part = parts [ j ] . trim ( ) ;
89- part = part . toLowerCase ( ) ;
90- if ( part . startsWith ( "path" ) ) {
91- cookiePath = part . substring ( part . indexOf ( "=" ) + 1 ) ;
92- }
93- else if ( part . startsWith ( "expires" ) ) {
94- expires = new Date ( part . substring ( part . indexOf ( "=" ) + 1 ) ) ;
77+ const release = await ziti . _cookiemutex . acquire ( ) ;
78+
79+ let zitiCookies = await ls . getWithExpiry ( zitiConstants . get ( ) . ZITI_COOKIES ) ;
80+ if ( isNull ( zitiCookies ) ) {
81+ zitiCookies = { }
9582 }
96- else if ( part . startsWith ( "httponly" ) ) {
97- httpOnly = true ;
83+ // console.log('=====> CookieInterceptor ZITI_COOKIES (before): ', zitiCookies);
84+
85+ let name = cookie . substring ( 0 , cookie . indexOf ( "=" ) ) ;
86+ let value = cookie . substring ( cookie . indexOf ( "=" ) + 1 ) ;
87+ let cookie_value = value . substring ( 0 , value . indexOf ( ";" ) ) ;
88+ let parts = value . split ( ";" ) ;
89+ let cookiePath ;
90+ let expires ;
91+ for ( let j = 0 ; j < parts . length ; j ++ ) {
92+ let part = parts [ j ] . trim ( ) ;
93+ part = part . toLowerCase ( ) ;
94+ if ( part . startsWith ( "path" ) ) {
95+ cookiePath = part . substring ( part . indexOf ( "=" ) + 1 ) ;
96+ }
97+ else if ( part . startsWith ( "expires" ) ) {
98+ expires = new Date ( part . substring ( part . indexOf ( "=" ) + 1 ) ) ;
99+ }
100+ else if ( part . startsWith ( "httponly" ) ) {
101+ httpOnly = true ;
102+ }
98103 }
99- }
100-
101- zitiCookies [ name ] = cookie_value ;
102-
103- console . log ( '=====> CookieInterceptor ZITI_COOKIES (after): ' , zitiCookies ) ;
104-
105- await ls . setWithExpiry ( zitiConstants . get ( ) . ZITI_COOKIES , zitiCookies , new Date ( 8640000000000000 ) ) ;
106-
107- release ( ) ;
104+
105+ zitiCookies [ name ] = cookie_value ;
106+
107+ // console.log('=====> CookieInterceptor ZITI_COOKIES (after): ', zitiCookies);
108+
109+ await ls . setWithExpiry ( zitiConstants . get ( ) . ZITI_COOKIES , zitiCookies , new Date ( 8640000000000000 ) ) ;
110+
111+ release ( ) ;
112+
113+ } ) ( )
108114
109115 return cookie ;
110116 } ) ;
@@ -903,7 +909,7 @@ if (!zitiConfig.serviceWorker.active) {
903909 * Service Worker 'message' handler'
904910 */
905911 navigator . serviceWorker . addEventListener ( 'message' , event => {
906- console . log ( '----- Client received msg from serviceWorker: ' , event ) ;
912+ console . log ( '----- Client received msg from serviceWorker: ' , event . data . command ) ;
907913
908914 if ( event . data . command === 'initClient' ) { _onMessage_initClient ( event ) ; }
909915 else if ( event . data . command === 'generateKeyPair' ) { _onMessage_generateKeyPair ( event ) ; }
0 commit comments