55
66import Base64 from './base64' ;
77import baseCookie from './base-cookie' ;
8+ import Constants from './constants' ;
89import getLocation from './get-location' ;
910import localStorage from './localstorage' ; // jshint ignore:line
1011import topDomain from './top-domain' ;
1112
13+ const storageOptionExists = {
14+ [ Constants . STORAGE_COOKIES ] : true ,
15+ [ Constants . STORAGE_NONE ] : true ,
16+ [ Constants . STORAGE_LOCAL ] : true ,
17+ [ Constants . STORAGE_SESSION ] : true ,
18+ } ;
19+
1220/**
1321 * MetadataStorage involves SDK data persistance
1422 * storage priority: cookies -> localStorage -> in memory
23+ * This priority can be overriden by setting the storage options.
1524 * if in localStorage, unable track users between subdomains
1625 * if in memory, then memory can't be shared between different tabs
1726 */
@@ -23,6 +32,7 @@ class MetadataStorage {
2332 secure,
2433 sameSite,
2534 expirationDays,
35+ storage,
2636 } ) {
2737 this . storageKey = storageKey ;
2838 this . domain = domain ;
@@ -38,14 +48,23 @@ class MetadataStorage {
3848 domain || ( writableTopDomain ? '.' + writableTopDomain : null ) ;
3949 }
4050
41- this . disableCookieStorage =
42- disableCookies ||
43- ! baseCookie . areCookiesEnabled ( {
44- domain : this . cookieDomain ,
45- secure : this . secure ,
46- sameSite : this . sameSite ,
47- expirationDays : this . expirationDays ,
48- } ) ;
51+ if ( storageOptionExists [ storage ] ) {
52+ this . storage = storage ;
53+ } else {
54+ const disableCookieStorage =
55+ disableCookies ||
56+ ! baseCookie . areCookiesEnabled ( {
57+ domain : this . cookieDomain ,
58+ secure : this . secure ,
59+ sameSite : this . sameSite ,
60+ expirationDays : this . expirationDays ,
61+ } ) ;
62+ if ( disableCookieStorage ) {
63+ this . storage = Constants . STORAGE_LOCAL ;
64+ } else {
65+ this . storage = Constants . STORAGE_COOKIES ;
66+ }
67+ }
4968 }
5069
5170 getCookieStorageKey ( ) {
@@ -73,6 +92,9 @@ class MetadataStorage {
7392 identifyId,
7493 sequenceNumber,
7594 } ) {
95+ if ( this . storage === Constants . STORAGE_NONE ) {
96+ return ;
97+ }
7698 const value = [
7799 deviceId ,
78100 Base64 . encode ( userId || '' ) , // used to convert not unicode to alphanumeric since cookies only use alphanumeric
@@ -84,26 +106,37 @@ class MetadataStorage {
84106 sequenceNumber ? sequenceNumber . toString ( 32 ) : '0' ,
85107 ] . join ( '.' ) ;
86108
87- if ( this . disableCookieStorage ) {
88- localStorage . setItem ( this . storageKey , value ) ;
89- } else {
90- baseCookie . set ( this . getCookieStorageKey ( ) , value , {
91- domain : this . cookieDomain ,
92- secure : this . secure ,
93- sameSite : this . sameSite ,
94- expirationDays : this . expirationDays ,
95- } ) ;
109+ switch ( this . storage ) {
110+ case Constants . STORAGE_SESSION :
111+ if ( window . sessionStorage ) {
112+ window . sessionStorage . setItem ( this . storageKey , value ) ;
113+ }
114+ break ;
115+ case Constants . STORAGE_LOCAL :
116+ localStorage . setItem ( this . storageKey , value ) ;
117+ break ;
118+ case Constants . STORAGE_COOKIES :
119+ baseCookie . set ( this . getCookieStorageKey ( ) , value , {
120+ domain : this . cookieDomain ,
121+ secure : this . secure ,
122+ sameSite : this . sameSite ,
123+ expirationDays : this . expirationDays ,
124+ } ) ;
125+ break ;
96126 }
97127 }
98128
99129 load ( ) {
100130 let str ;
101- if ( ! this . disableCookieStorage ) {
131+ if ( this . storage === Constants . STORAGE_COOKIES ) {
102132 str = baseCookie . get ( this . getCookieStorageKey ( ) + '=' ) ;
103133 }
104134 if ( ! str ) {
105135 str = localStorage . getItem ( this . storageKey ) ;
106136 }
137+ if ( ! str ) {
138+ str = window . sessionStorage && window . sessionStorage . getItem ( this . storageKey ) ;
139+ }
107140
108141 if ( ! str ) {
109142 return null ;
0 commit comments