5
5
6
6
import Base64 from './base64' ;
7
7
import baseCookie from './base-cookie' ;
8
+ import Constants from './constants' ;
8
9
import getLocation from './get-location' ;
9
10
import localStorage from './localstorage' ; // jshint ignore:line
10
11
import topDomain from './top-domain' ;
11
12
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
+
12
20
/**
13
21
* MetadataStorage involves SDK data persistance
14
22
* storage priority: cookies -> localStorage -> in memory
23
+ * This priority can be overriden by setting the storage options.
15
24
* if in localStorage, unable track users between subdomains
16
25
* if in memory, then memory can't be shared between different tabs
17
26
*/
@@ -23,6 +32,7 @@ class MetadataStorage {
23
32
secure,
24
33
sameSite,
25
34
expirationDays,
35
+ storage,
26
36
} ) {
27
37
this . storageKey = storageKey ;
28
38
this . domain = domain ;
@@ -38,14 +48,23 @@ class MetadataStorage {
38
48
domain || ( writableTopDomain ? '.' + writableTopDomain : null ) ;
39
49
}
40
50
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
+ }
49
68
}
50
69
51
70
getCookieStorageKey ( ) {
@@ -73,6 +92,9 @@ class MetadataStorage {
73
92
identifyId,
74
93
sequenceNumber,
75
94
} ) {
95
+ if ( this . storage === Constants . STORAGE_NONE ) {
96
+ return ;
97
+ }
76
98
const value = [
77
99
deviceId ,
78
100
Base64 . encode ( userId || '' ) , // used to convert not unicode to alphanumeric since cookies only use alphanumeric
@@ -84,26 +106,37 @@ class MetadataStorage {
84
106
sequenceNumber ? sequenceNumber . toString ( 32 ) : '0' ,
85
107
] . join ( '.' ) ;
86
108
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 ;
96
126
}
97
127
}
98
128
99
129
load ( ) {
100
130
let str ;
101
- if ( ! this . disableCookieStorage ) {
131
+ if ( this . storage === Constants . STORAGE_COOKIES ) {
102
132
str = baseCookie . get ( this . getCookieStorageKey ( ) + '=' ) ;
103
133
}
104
134
if ( ! str ) {
105
135
str = localStorage . getItem ( this . storageKey ) ;
106
136
}
137
+ if ( ! str ) {
138
+ str = window . sessionStorage && window . sessionStorage . getItem ( this . storageKey ) ;
139
+ }
107
140
108
141
if ( ! str ) {
109
142
return null ;
0 commit comments