|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import Base64 from './base64'; |
7 | | -import Cookie from './cookie'; |
8 | 7 | import baseCookie from './base-cookie'; |
| 8 | +import getLocation from './get-location'; |
9 | 9 | import localStorage from './localstorage'; // jshint ignore:line |
| 10 | +import topDomain from './top-domain'; |
10 | 11 |
|
11 | 12 | class MetadataStorage { |
12 | 13 | constructor({storageKey, disableCookies, domain, secure, sameSite, expirationDays}) { |
13 | 14 | this.storageKey = storageKey; |
14 | | - this.disableCookieStorage = !Cookie.areCookiesEnabled() || disableCookies; |
| 15 | + this.disableCookieStorage = !baseCookie.areCookiesEnabled() || disableCookies; |
15 | 16 | this.domain = domain; |
16 | 17 | this.secure = secure; |
17 | 18 | this.sameSite = sameSite; |
18 | 19 | this.expirationDays = expirationDays; |
19 | | - this.topDomain = domain || Cookie.topDomain(); |
| 20 | + const writableTopDomain = topDomain(getLocation().href); |
| 21 | + this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null); |
20 | 22 | } |
21 | 23 |
|
22 | 24 | getCookieStorageKey() { |
23 | | - return `${this.storageKey}${this.domain ? `_${this.domain}` : ''}`; |
| 25 | + if (!this.domain) { |
| 26 | + return this.storageKey; |
| 27 | + } |
| 28 | + |
| 29 | + const suffix = this.domain.charAt(0) === '.' ? this.domain.substring(1) : this.domain; |
| 30 | + |
| 31 | + return `${this.storageKey}${suffix ? `_${suffix}` : ''}`; |
24 | 32 | } |
25 | 33 |
|
26 | 34 | save({ deviceId, userId, optOut, sessionId, lastEventTime, eventId, identifyId, sequenceNumber }) { |
27 | 35 | // do not change the order of these items |
28 | | - const value = `${deviceId}.${Base64.encode(userId || '')}.${optOut ? '1' : ''}.${sessionId}.${lastEventTime}.${eventId}.${identifyId}.${sequenceNumber}`; |
| 36 | + const value = [ |
| 37 | + deviceId, |
| 38 | + Base64.encode(userId || ''), |
| 39 | + optOut ? '1' : '', |
| 40 | + sessionId ? sessionId.toString(32) : '0', |
| 41 | + lastEventTime ? lastEventTime.toString(32) : '0', |
| 42 | + eventId ? eventId.toString(32) : '0', |
| 43 | + identifyId ? identifyId.toString(32) : '0', |
| 44 | + sequenceNumber ? sequenceNumber.toString(32) : '0' |
| 45 | + ].join('.'); |
29 | 46 |
|
30 | 47 | if (this.disableCookieStorage) { |
31 | 48 | localStorage.setItem(this.storageKey, value); |
32 | 49 | } else { |
33 | 50 | baseCookie.set( |
34 | 51 | this.getCookieStorageKey(), |
35 | 52 | value, |
36 | | - { domain: this.topDomain, secure: this.secure, sameSite: this.sameSite, expirationDays: this.expirationDays } |
| 53 | + { |
| 54 | + domain: this.cookieDomain, |
| 55 | + secure: this.secure, |
| 56 | + sameSite: this.sameSite, |
| 57 | + expirationDays: this.expirationDays |
| 58 | + } |
37 | 59 | ); |
38 | 60 | } |
39 | 61 | } |
@@ -66,11 +88,11 @@ class MetadataStorage { |
66 | 88 | deviceId: values[0], |
67 | 89 | userId, |
68 | 90 | optOut: values[2] === '1', |
69 | | - sessionId: parseInt(values[3], 10), |
70 | | - lastEventTime: parseInt(values[4], 10), |
71 | | - eventId: parseInt(values[5], 10), |
72 | | - identifyId: parseInt(values[6], 10), |
73 | | - sequenceNumber: parseInt(values[7], 10) |
| 91 | + sessionId: parseInt(values[3], 32), |
| 92 | + lastEventTime: parseInt(values[4], 32), |
| 93 | + eventId: parseInt(values[5], 32), |
| 94 | + identifyId: parseInt(values[6], 32), |
| 95 | + sequenceNumber: parseInt(values[7], 32) |
74 | 96 | }; |
75 | 97 | } |
76 | 98 | } |
|
0 commit comments