Skip to content

Commit 61b6590

Browse files
authored
fix(cookies): respect the options passed into cookies when testing to see if they're enabled (#294)
* fix(cookies): respect the options passed into cookies when testing to see if they're enabled * some shuffling around
1 parent 48e226e commit 61b6590

File tree

3 files changed

+58
-36
lines changed

3 files changed

+58
-36
lines changed

src/base-cookie.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ const get = (name) => {
2323
};
2424

2525
const set = (name, value, opts) => {
26-
let expires = value !== null ? opts.expirationDays : -1 ;
26+
let expires = value !== null ? opts.expirationDays : -1;
2727
if (expires) {
2828
const date = new Date();
29-
date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000));
29+
date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
3030
expires = date;
3131
}
3232
let str = name + '=' + value;
@@ -46,15 +46,14 @@ const set = (name, value, opts) => {
4646
document.cookie = str;
4747
};
4848

49-
5049
// test that cookies are enabled - navigator.cookiesEnabled yields false positives in IE, need to test directly
51-
const areCookiesEnabled = () => {
50+
const areCookiesEnabled = (opts = {}) => {
5251
const uid = String(new Date());
5352
try {
5453
const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id();
55-
set(cookieName, uid, {});
54+
set(cookieName, uid, opts);
5655
const _areCookiesEnabled = get(cookieName + '=') === uid;
57-
set(cookieName, null, {});
56+
set(cookieName, null, opts);
5857
return _areCookiesEnabled;
5958
} catch (e) {}
6059
return false;
@@ -63,5 +62,5 @@ const areCookiesEnabled = () => {
6362
export default {
6463
set,
6564
get,
66-
areCookiesEnabled
65+
areCookiesEnabled,
6766
};

src/metadata-storage.js

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,69 @@ import localStorage from './localstorage'; // jshint ignore:line
1010
import topDomain from './top-domain';
1111

1212
/**
13-
* MetadataStorage involves SDK data persistance
14-
* storage priority: cookies -> localStorage -> in memory
15-
* if in localStorage, unable track users between subdomains
16-
* if in memory, then memory can't be shared between different tabs
17-
*/
18-
class MetadataStorage {
19-
constructor({storageKey, disableCookies, domain, secure, sameSite, expirationDays}) {
13+
* MetadataStorage involves SDK data persistance
14+
* storage priority: cookies -> localStorage -> in memory
15+
* if in localStorage, unable track users between subdomains
16+
* if in memory, then memory can't be shared between different tabs
17+
*/
18+
class MetadataStorage {
19+
constructor({
20+
storageKey,
21+
disableCookies,
22+
domain,
23+
secure,
24+
sameSite,
25+
expirationDays,
26+
}) {
2027
this.storageKey = storageKey;
21-
this.disableCookieStorage = !baseCookie.areCookiesEnabled() || disableCookies;
2228
this.domain = domain;
2329
this.secure = secure;
2430
this.sameSite = sameSite;
2531
this.expirationDays = expirationDays;
26-
this.cookieDomain ='';
32+
33+
this.cookieDomain = '';
2734

2835
if (!BUILD_COMPAT_REACT_NATIVE) {
2936
const writableTopDomain = topDomain(getLocation().href);
30-
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);
37+
this.cookieDomain =
38+
domain || (writableTopDomain ? '.' + writableTopDomain : null);
3139
}
40+
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+
});
3249
}
3350

3451
getCookieStorageKey() {
3552
if (!this.domain) {
3653
return this.storageKey;
3754
}
3855

39-
const suffix = this.domain.charAt(0) === '.' ? this.domain.substring(1) : this.domain;
56+
const suffix =
57+
this.domain.charAt(0) === '.' ? this.domain.substring(1) : this.domain;
4058

4159
return `${this.storageKey}${suffix ? `_${suffix}` : ''}`;
4260
}
4361

4462
/*
45-
* Data is saved as delimited values rather than JSO to minimize cookie space
46-
* Should not change order of the items
47-
*/
48-
save({ deviceId, userId, optOut, sessionId, lastEventTime, eventId, identifyId, sequenceNumber }) {
63+
* Data is saved as delimited values rather than JSO to minimize cookie space
64+
* Should not change order of the items
65+
*/
66+
save({
67+
deviceId,
68+
userId,
69+
optOut,
70+
sessionId,
71+
lastEventTime,
72+
eventId,
73+
identifyId,
74+
sequenceNumber,
75+
}) {
4976
const value = [
5077
deviceId,
5178
Base64.encode(userId || ''), // used to convert not unicode to alphanumeric since cookies only use alphanumeric
@@ -54,22 +81,18 @@ import topDomain from './top-domain';
5481
lastEventTime ? lastEventTime.toString(32) : '0', // last time an event was set
5582
eventId ? eventId.toString(32) : '0',
5683
identifyId ? identifyId.toString(32) : '0',
57-
sequenceNumber ? sequenceNumber.toString(32) : '0'
58-
].join('.');
84+
sequenceNumber ? sequenceNumber.toString(32) : '0',
85+
].join('.');
5986

6087
if (this.disableCookieStorage) {
6188
localStorage.setItem(this.storageKey, value);
6289
} else {
63-
baseCookie.set(
64-
this.getCookieStorageKey(),
65-
value,
66-
{
67-
domain: this.cookieDomain,
68-
secure: this.secure,
69-
sameSite: this.sameSite,
70-
expirationDays: this.expirationDays
71-
}
72-
);
90+
baseCookie.set(this.getCookieStorageKey(), value, {
91+
domain: this.cookieDomain,
92+
secure: this.secure,
93+
sameSite: this.sameSite,
94+
expirationDays: this.expirationDays,
95+
});
7396
}
7497
}
7598

@@ -105,7 +128,7 @@ import topDomain from './top-domain';
105128
lastEventTime: parseInt(values[4], 32),
106129
eventId: parseInt(values[5], 32),
107130
identifyId: parseInt(values[6], 32),
108-
sequenceNumber: parseInt(values[7], 32)
131+
sequenceNumber: parseInt(values[7], 32),
109132
};
110133
}
111134
}

test/amplitude-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ describe('AmplitudeClient', function() {
391391
identifyId: 60,
392392
sequenceNumber: 70
393393
}
394-
const storage = new MetadataStorage({storageKey: cookieName, disableCookieStorage: true});
394+
const storage = new MetadataStorage({storageKey: cookieName, disableCookies: true});
395395
storage.save(cookieData);
396396

397397
clock.tick(10);

0 commit comments

Comments
 (0)