@amplitude/analytics-core 2.41.0 → 2.41.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/messenger/constants.d.ts +1 -1
- package/lib/cjs/messenger/constants.js +1 -1
- package/lib/cjs/messenger/constants.js.map +1 -1
- package/lib/cjs/storage/cookie.d.ts +6 -0
- package/lib/cjs/storage/cookie.d.ts.map +1 -1
- package/lib/cjs/storage/cookie.js +223 -146
- package/lib/cjs/storage/cookie.js.map +1 -1
- package/lib/cjs/types/storage.d.ts +4 -0
- package/lib/cjs/types/storage.d.ts.map +1 -1
- package/lib/cjs/types/storage.js.map +1 -1
- package/lib/esm/messenger/constants.d.ts +1 -1
- package/lib/esm/messenger/constants.js +1 -1
- package/lib/esm/messenger/constants.js.map +1 -1
- package/lib/esm/storage/cookie.d.ts +6 -0
- package/lib/esm/storage/cookie.d.ts.map +1 -1
- package/lib/esm/storage/cookie.js +223 -146
- package/lib/esm/storage/cookie.js.map +1 -1
- package/lib/esm/types/storage.d.ts +4 -0
- package/lib/esm/types/storage.d.ts.map +1 -1
- package/lib/esm/types/storage.js.map +1 -1
- package/package.json +1 -1
|
@@ -2,5 +2,5 @@ export declare const AMPLITUDE_ORIGIN = "https://app.amplitude.com";
|
|
|
2
2
|
export declare const AMPLITUDE_ORIGIN_EU = "https://app.eu.amplitude.com";
|
|
3
3
|
export declare const AMPLITUDE_ORIGIN_STAGING = "https://apps.stag2.amplitude.com";
|
|
4
4
|
export declare const AMPLITUDE_ORIGINS_MAP: Record<string, string>;
|
|
5
|
-
export declare const AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = "https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.
|
|
5
|
+
export declare const AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = "https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.2.js.gz";
|
|
6
6
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -11,5 +11,5 @@ exports.AMPLITUDE_ORIGINS_MAP = {
|
|
|
11
11
|
STAGING: exports.AMPLITUDE_ORIGIN_STAGING,
|
|
12
12
|
};
|
|
13
13
|
// Background capture script URL (shared between autocapture and session-replay)
|
|
14
|
-
exports.AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = 'https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.
|
|
14
|
+
exports.AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = 'https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.2.js.gz';
|
|
15
15
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/messenger/constants.ts"],"names":[],"mappings":";;;AAAA,mEAAmE;AACtD,QAAA,gBAAgB,GAAG,2BAA2B,CAAC;AAC/C,QAAA,mBAAmB,GAAG,8BAA8B,CAAC;AACrD,QAAA,wBAAwB,GAAG,kCAAkC,CAAC;AAC9D,QAAA,qBAAqB,GAA2B;IAC3D,EAAE,EAAE,wBAAgB;IACpB,EAAE,EAAE,2BAAmB;IACvB,OAAO,EAAE,gCAAwB;CAClC,CAAC;AAEF,gFAAgF;AACnE,QAAA,uCAAuC,GAClD,uEAAuE,CAAC","sourcesContent":["// Shared origin constants for Amplitude cross-window communication\nexport const AMPLITUDE_ORIGIN = 'https://app.amplitude.com';\nexport const AMPLITUDE_ORIGIN_EU = 'https://app.eu.amplitude.com';\nexport const AMPLITUDE_ORIGIN_STAGING = 'https://apps.stag2.amplitude.com';\nexport const AMPLITUDE_ORIGINS_MAP: Record<string, string> = {\n US: AMPLITUDE_ORIGIN,\n EU: AMPLITUDE_ORIGIN_EU,\n STAGING: AMPLITUDE_ORIGIN_STAGING,\n};\n\n// Background capture script URL (shared between autocapture and session-replay)\nexport const AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL =\n 'https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/messenger/constants.ts"],"names":[],"mappings":";;;AAAA,mEAAmE;AACtD,QAAA,gBAAgB,GAAG,2BAA2B,CAAC;AAC/C,QAAA,mBAAmB,GAAG,8BAA8B,CAAC;AACrD,QAAA,wBAAwB,GAAG,kCAAkC,CAAC;AAC9D,QAAA,qBAAqB,GAA2B;IAC3D,EAAE,EAAE,wBAAgB;IACpB,EAAE,EAAE,2BAAmB;IACvB,OAAO,EAAE,gCAAwB;CAClC,CAAC;AAEF,gFAAgF;AACnE,QAAA,uCAAuC,GAClD,uEAAuE,CAAC","sourcesContent":["// Shared origin constants for Amplitude cross-window communication\nexport const AMPLITUDE_ORIGIN = 'https://app.amplitude.com';\nexport const AMPLITUDE_ORIGIN_EU = 'https://app.eu.amplitude.com';\nexport const AMPLITUDE_ORIGIN_STAGING = 'https://apps.stag2.amplitude.com';\nexport const AMPLITUDE_ORIGINS_MAP: Record<string, string> = {\n US: AMPLITUDE_ORIGIN,\n EU: AMPLITUDE_ORIGIN_EU,\n STAGING: AMPLITUDE_ORIGIN_STAGING,\n};\n\n// Background capture script URL (shared between autocapture and session-replay)\nexport const AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL =\n 'https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.2.js.gz';\n"]}
|
|
@@ -5,10 +5,16 @@ export declare class CookieStorage<T> implements Storage<T> {
|
|
|
5
5
|
constructor(options?: CookieStorageOptions, config?: CookieStorageConfig);
|
|
6
6
|
isEnabled(): Promise<boolean>;
|
|
7
7
|
get(key: string): Promise<T | undefined>;
|
|
8
|
+
private decodeCookieValue;
|
|
9
|
+
private getSync;
|
|
8
10
|
getRaw(key: string): Promise<string | undefined>;
|
|
11
|
+
private getRawSync;
|
|
9
12
|
set(key: string, value: T | null): Promise<void>;
|
|
13
|
+
private setSync;
|
|
10
14
|
remove(key: string): Promise<void>;
|
|
11
15
|
reset(): Promise<void>;
|
|
16
|
+
static isDomainWritable(domain: string): Promise<boolean>;
|
|
17
|
+
private transaction;
|
|
12
18
|
}
|
|
13
19
|
/**
|
|
14
20
|
* Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AA2BnG,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IACjD,OAAO,EAAE,oBAAoB,CAAC;IAC9B,MAAM,EAAE,mBAAmB,CAAC;gBAEhB,OAAO,CAAC,EAAE,oBAAoB,EAAE,MAAM,GAAE,mBAAwB;IAKtE,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAwC7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAK9C,OAAO,CAAC,iBAAiB;IAkBzB,OAAO,CAAC,OAAO;IAKT,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAiCtD,OAAO,CAAC,UAAU;IAiCZ,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAItD,OAAO,CAAC,OAAO;IAkCT,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;WAIf,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;YAqBjD,WAAW;CAsB1B;AAoBD;;;GAGG;AACH,eAAO,MAAM,iBAAiB,UAAW,MAAM,KAAG,MAAM,GAAG,SAE1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,SAAS,WAAW,MAAM,GAAG,SAAS,KAAG,OAUxF,CAAC"}
|
|
@@ -3,7 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isDomainEqual = exports.decodeCookieValue = exports.CookieStorage = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var global_scope_1 = require("../global-scope");
|
|
6
|
-
|
|
6
|
+
/* istanbul ignore next */
|
|
7
|
+
var getLocks = function () {
|
|
8
|
+
var _a;
|
|
9
|
+
var globalScope = (0, global_scope_1.getGlobalScope)();
|
|
10
|
+
return (_a = globalScope === null || globalScope === void 0 ? void 0 : globalScope.navigator) === null || _a === void 0 ? void 0 : _a.locks;
|
|
11
|
+
};
|
|
7
12
|
var CookieStorage = /** @class */ (function () {
|
|
8
13
|
function CookieStorage(options, config) {
|
|
9
14
|
if (config === void 0) { config = {}; }
|
|
@@ -11,211 +16,221 @@ var CookieStorage = /** @class */ (function () {
|
|
|
11
16
|
this.config = config;
|
|
12
17
|
}
|
|
13
18
|
CookieStorage.prototype.isEnabled = function () {
|
|
14
|
-
var _a, _b;
|
|
15
19
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
var testKey, testCookieOptions, testStorage, testValue;
|
|
21
|
+
var _this = this;
|
|
22
|
+
return tslib_1.__generator(this, function (_a) {
|
|
23
|
+
switch (_a.label) {
|
|
19
24
|
case 0:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (!globalScope || !globalScope.document) {
|
|
23
|
-
return [2 /*return*/, false];
|
|
24
|
-
}
|
|
25
|
-
testValue = String(Date.now());
|
|
26
|
-
testCookieOptions = tslib_1.__assign(tslib_1.__assign({}, this.options), { expirationDays: 0.003 });
|
|
25
|
+
testKey = 'AMP_TEST';
|
|
26
|
+
testCookieOptions = tslib_1.__assign({}, this.options);
|
|
27
27
|
testStorage = new CookieStorage(testCookieOptions);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
28
|
+
testValue = String(Date.now());
|
|
29
|
+
return [4 /*yield*/, testStorage.transaction(testKey, function (storage) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
try {
|
|
32
|
+
storage.set(testValue);
|
|
33
|
+
var value = storage.get();
|
|
34
|
+
var result = value === testValue;
|
|
35
|
+
/* istanbul ignore next */
|
|
36
|
+
if (!result && _this.config.diagnosticsClient) {
|
|
37
|
+
(_a = _this.config.diagnosticsClient) === null || _a === void 0 ? void 0 : _a.recordEvent('cookies.isEnabled.failure', {
|
|
38
|
+
reason: 'Test Value mismatch',
|
|
39
|
+
testKey: testKey,
|
|
40
|
+
testValue: testValue,
|
|
41
|
+
sync: true,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
/* istanbul ignore next */
|
|
48
|
+
if (_this.config.diagnosticsClient) {
|
|
49
|
+
var errMessage = e instanceof Error ? e.message : String(e);
|
|
50
|
+
(_b = _this.config.diagnosticsClient) === null || _b === void 0 ? void 0 : _b.recordEvent('cookies.isEnabled.failure', {
|
|
51
|
+
reason: 'Cookie getter/setter failed',
|
|
52
|
+
testKey: testKey,
|
|
53
|
+
testValue: testValue,
|
|
54
|
+
error: errMessage,
|
|
55
|
+
sync: true,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
// clean-up the AMP_TEST cookie behind us
|
|
62
|
+
storage.set(null);
|
|
63
|
+
}
|
|
64
|
+
})];
|
|
65
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
65
66
|
}
|
|
66
67
|
});
|
|
67
68
|
});
|
|
68
69
|
};
|
|
69
70
|
CookieStorage.prototype.get = function (key) {
|
|
70
71
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
71
|
-
var value
|
|
72
|
+
var value;
|
|
72
73
|
return tslib_1.__generator(this, function (_a) {
|
|
73
74
|
switch (_a.label) {
|
|
74
75
|
case 0: return [4 /*yield*/, this.getRaw(key)];
|
|
75
76
|
case 1:
|
|
76
77
|
value = _a.sent();
|
|
77
|
-
|
|
78
|
-
return [2 /*return*/, undefined];
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
decodedValue = (0, exports.decodeCookieValue)(value);
|
|
82
|
-
if (decodedValue === undefined) {
|
|
83
|
-
console.error("Amplitude Logger [Error]: Failed to decode cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
84
|
-
return [2 /*return*/, undefined];
|
|
85
|
-
}
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
87
|
-
return [2 /*return*/, JSON.parse(decodedValue)];
|
|
88
|
-
}
|
|
89
|
-
catch (_b) {
|
|
90
|
-
console.error("Amplitude Logger [Error]: Failed to parse cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
91
|
-
return [2 /*return*/, undefined];
|
|
92
|
-
}
|
|
93
|
-
return [2 /*return*/];
|
|
78
|
+
return [2 /*return*/, this.decodeCookieValue(key, value)];
|
|
94
79
|
}
|
|
95
80
|
});
|
|
96
81
|
});
|
|
97
82
|
};
|
|
83
|
+
CookieStorage.prototype.decodeCookieValue = function (key, value) {
|
|
84
|
+
if (!value) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
var decodedValue = (0, exports.decodeCookieValue)(value);
|
|
89
|
+
if (decodedValue === undefined) {
|
|
90
|
+
console.error("Amplitude Logger [Error]: Failed to decode cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
94
|
+
return JSON.parse(decodedValue);
|
|
95
|
+
}
|
|
96
|
+
catch (_a) {
|
|
97
|
+
console.error("Amplitude Logger [Error]: Failed to parse cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
CookieStorage.prototype.getSync = function (key) {
|
|
102
|
+
var value = this.getRawSync(key);
|
|
103
|
+
return this.decodeCookieValue(key, value);
|
|
104
|
+
};
|
|
98
105
|
CookieStorage.prototype.getRaw = function (key) {
|
|
99
|
-
var _a, _b
|
|
106
|
+
var _a, _b;
|
|
100
107
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
101
|
-
var globalScope, globalScopeWithCookiesStore, cookieStore,
|
|
102
|
-
var
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
switch (_f.label) {
|
|
108
|
+
var globalScope, globalScopeWithCookiesStore, cookieStore, cookies, cookies_1, cookies_1_1, cookie, ignoreError_1;
|
|
109
|
+
var e_1, _c;
|
|
110
|
+
return tslib_1.__generator(this, function (_d) {
|
|
111
|
+
switch (_d.label) {
|
|
106
112
|
case 0:
|
|
107
113
|
globalScope = (0, global_scope_1.getGlobalScope)();
|
|
108
114
|
globalScopeWithCookiesStore = globalScope;
|
|
109
|
-
|
|
115
|
+
_d.label = 1;
|
|
110
116
|
case 1:
|
|
111
|
-
|
|
117
|
+
_d.trys.push([1, 4, , 5]);
|
|
112
118
|
cookieStore = globalScopeWithCookiesStore === null || globalScopeWithCookiesStore === void 0 ? void 0 : globalScopeWithCookiesStore.cookieStore;
|
|
113
119
|
if (!cookieStore) return [3 /*break*/, 3];
|
|
114
120
|
return [4 /*yield*/, cookieStore.getAll(key)];
|
|
115
121
|
case 2:
|
|
116
|
-
|
|
117
|
-
if (
|
|
122
|
+
cookies = _d.sent();
|
|
123
|
+
if (cookies) {
|
|
118
124
|
/* istanbul ignore if */
|
|
119
|
-
if (
|
|
125
|
+
if (cookies.length > 1) {
|
|
120
126
|
(_a = this.config.diagnosticsClient) === null || _a === void 0 ? void 0 : _a.recordEvent('cookies.duplicate', {
|
|
121
|
-
cookies:
|
|
127
|
+
cookies: cookies.map(function (cookie) { return cookie.domain; }),
|
|
122
128
|
});
|
|
123
129
|
(_b = this.config.diagnosticsClient) === null || _b === void 0 ? void 0 : _b.increment('cookies.duplicate.occurrence.cookieStore');
|
|
124
130
|
}
|
|
125
131
|
try {
|
|
126
|
-
for (cookies_1 = tslib_1.__values(
|
|
132
|
+
for (cookies_1 = tslib_1.__values(cookies), cookies_1_1 = cookies_1.next(); !cookies_1_1.done; cookies_1_1 = cookies_1.next()) {
|
|
127
133
|
cookie = cookies_1_1.value;
|
|
128
134
|
if ((0, exports.isDomainEqual)(cookie.domain, this.options.domain)) {
|
|
129
135
|
return [2 /*return*/, cookie.value];
|
|
130
136
|
}
|
|
131
137
|
}
|
|
132
138
|
}
|
|
133
|
-
catch (
|
|
139
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
134
140
|
finally {
|
|
135
141
|
try {
|
|
136
|
-
if (cookies_1_1 && !cookies_1_1.done && (
|
|
142
|
+
if (cookies_1_1 && !cookies_1_1.done && (_c = cookies_1.return)) _c.call(cookies_1);
|
|
137
143
|
}
|
|
138
|
-
finally { if (
|
|
144
|
+
finally { if (e_1) throw e_1.error; }
|
|
139
145
|
}
|
|
140
146
|
}
|
|
141
|
-
|
|
147
|
+
_d.label = 3;
|
|
142
148
|
case 3: return [3 /*break*/, 5];
|
|
143
149
|
case 4:
|
|
144
|
-
ignoreError_1 =
|
|
150
|
+
ignoreError_1 = _d.sent();
|
|
145
151
|
return [3 /*break*/, 5];
|
|
146
|
-
case 5:
|
|
147
|
-
cookies = ((_d = (_c = globalScope === null || globalScope === void 0 ? void 0 : globalScope.document) === null || _c === void 0 ? void 0 : _c.cookie.split('; ')) !== null && _d !== void 0 ? _d : []).filter(function (c) { return c.indexOf(key + '=') === 0; });
|
|
148
|
-
match = undefined;
|
|
149
|
-
duplicateResolverFn = this.config.duplicateResolverFn;
|
|
150
|
-
if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {
|
|
151
|
-
match = cookies.find(function (c) {
|
|
152
|
-
var _a;
|
|
153
|
-
try {
|
|
154
|
-
var res = duplicateResolverFn(c.substring(key.length + 1));
|
|
155
|
-
if (!res) {
|
|
156
|
-
(_a = _this.config.diagnosticsClient) === null || _a === void 0 ? void 0 : _a.increment('cookies.duplicate.occurrence.document.cookie');
|
|
157
|
-
}
|
|
158
|
-
return res;
|
|
159
|
-
}
|
|
160
|
-
catch (ignoreError) {
|
|
161
|
-
/* istanbul ignore next */
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
// if match was not found, just get the first one that matches the key
|
|
167
|
-
if (!match) {
|
|
168
|
-
match = cookies[0];
|
|
169
|
-
}
|
|
170
|
-
if (!match) {
|
|
171
|
-
return [2 /*return*/, undefined];
|
|
172
|
-
}
|
|
173
|
-
return [2 /*return*/, match.substring(key.length + 1)];
|
|
152
|
+
case 5: return [2 /*return*/, this.getRawSync(key)];
|
|
174
153
|
}
|
|
175
154
|
});
|
|
176
155
|
});
|
|
177
156
|
};
|
|
178
|
-
CookieStorage.prototype.
|
|
179
|
-
var
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
157
|
+
CookieStorage.prototype.getRawSync = function (key) {
|
|
158
|
+
var _this = this;
|
|
159
|
+
var _a, _b;
|
|
160
|
+
var globalScope = (0, global_scope_1.getGlobalScope)();
|
|
161
|
+
var cookies = ((_b = (_a = globalScope === null || globalScope === void 0 ? void 0 : globalScope.document) === null || _a === void 0 ? void 0 : _a.cookie.split('; ')) !== null && _b !== void 0 ? _b : []).filter(function (c) { return c.indexOf(key + '=') === 0; });
|
|
162
|
+
var match = undefined;
|
|
163
|
+
// if matcher function is provided, use it to de-duplicate when there's more than one cookie
|
|
164
|
+
/* istanbul ignore if */
|
|
165
|
+
var duplicateResolverFn = this.config.duplicateResolverFn;
|
|
166
|
+
if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {
|
|
167
|
+
match = cookies.find(function (c) {
|
|
168
|
+
var _a;
|
|
183
169
|
try {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (expires) {
|
|
188
|
-
date = new Date();
|
|
189
|
-
date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
|
|
190
|
-
expireDate = date;
|
|
191
|
-
}
|
|
192
|
-
str = "".concat(key, "=").concat(btoa(encodeURIComponent(JSON.stringify(value))));
|
|
193
|
-
if (expireDate) {
|
|
194
|
-
str += "; expires=".concat(expireDate.toUTCString());
|
|
195
|
-
}
|
|
196
|
-
str += '; path=/';
|
|
197
|
-
if (this.options.domain) {
|
|
198
|
-
str += "; domain=".concat(this.options.domain);
|
|
199
|
-
}
|
|
200
|
-
if (this.options.secure) {
|
|
201
|
-
str += '; Secure';
|
|
202
|
-
}
|
|
203
|
-
if (this.options.sameSite) {
|
|
204
|
-
str += "; SameSite=".concat(this.options.sameSite);
|
|
205
|
-
}
|
|
206
|
-
globalScope = (0, global_scope_1.getGlobalScope)();
|
|
207
|
-
if (globalScope) {
|
|
208
|
-
globalScope.document.cookie = str;
|
|
170
|
+
var res = duplicateResolverFn(c.substring(key.length + 1));
|
|
171
|
+
if (!res) {
|
|
172
|
+
(_a = _this.config.diagnosticsClient) === null || _a === void 0 ? void 0 : _a.increment('cookies.duplicate.occurrence.document.cookie');
|
|
209
173
|
}
|
|
174
|
+
return res;
|
|
210
175
|
}
|
|
211
|
-
catch (
|
|
212
|
-
|
|
213
|
-
|
|
176
|
+
catch (ignoreError) {
|
|
177
|
+
/* istanbul ignore next */
|
|
178
|
+
return false;
|
|
214
179
|
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// if match was not found, just get the first one that matches the key
|
|
183
|
+
if (!match) {
|
|
184
|
+
match = cookies[0];
|
|
185
|
+
}
|
|
186
|
+
if (!match) {
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
return match.substring(key.length + 1);
|
|
190
|
+
};
|
|
191
|
+
CookieStorage.prototype.set = function (key, value) {
|
|
192
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
193
|
+
return tslib_1.__generator(this, function (_a) {
|
|
194
|
+
this.setSync(key, value);
|
|
215
195
|
return [2 /*return*/];
|
|
216
196
|
});
|
|
217
197
|
});
|
|
218
198
|
};
|
|
199
|
+
CookieStorage.prototype.setSync = function (key, value) {
|
|
200
|
+
var _a;
|
|
201
|
+
try {
|
|
202
|
+
var expirationDays = (_a = this.options.expirationDays) !== null && _a !== void 0 ? _a : 0;
|
|
203
|
+
var expires = value !== null ? expirationDays : -1;
|
|
204
|
+
var expireDate = undefined;
|
|
205
|
+
if (expires) {
|
|
206
|
+
var date = new Date();
|
|
207
|
+
date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
|
|
208
|
+
expireDate = date;
|
|
209
|
+
}
|
|
210
|
+
var str = "".concat(key, "=").concat(btoa(encodeURIComponent(JSON.stringify(value))));
|
|
211
|
+
if (expireDate) {
|
|
212
|
+
str += "; expires=".concat(expireDate.toUTCString());
|
|
213
|
+
}
|
|
214
|
+
str += '; path=/';
|
|
215
|
+
if (this.options.domain) {
|
|
216
|
+
str += "; domain=".concat(this.options.domain);
|
|
217
|
+
}
|
|
218
|
+
if (this.options.secure) {
|
|
219
|
+
str += '; Secure';
|
|
220
|
+
}
|
|
221
|
+
if (this.options.sameSite) {
|
|
222
|
+
str += "; SameSite=".concat(this.options.sameSite);
|
|
223
|
+
}
|
|
224
|
+
var globalScope = (0, global_scope_1.getGlobalScope)();
|
|
225
|
+
if (globalScope) {
|
|
226
|
+
globalScope.document.cookie = str;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
var errorMessage = error instanceof Error ? error.message : String(error);
|
|
231
|
+
console.error("Amplitude Logger [Error]: Failed to set cookie for key: ".concat(key, ". Error: ").concat(errorMessage));
|
|
232
|
+
}
|
|
233
|
+
};
|
|
219
234
|
CookieStorage.prototype.remove = function (key) {
|
|
220
235
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
221
236
|
return tslib_1.__generator(this, function (_a) {
|
|
@@ -235,6 +250,68 @@ var CookieStorage = /** @class */ (function () {
|
|
|
235
250
|
});
|
|
236
251
|
});
|
|
237
252
|
};
|
|
253
|
+
CookieStorage.isDomainWritable = function (domain) {
|
|
254
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
255
|
+
var options, storageKey, storage, res, error_1;
|
|
256
|
+
return tslib_1.__generator(this, function (_a) {
|
|
257
|
+
switch (_a.label) {
|
|
258
|
+
case 0:
|
|
259
|
+
options = {
|
|
260
|
+
domain: '.' + domain,
|
|
261
|
+
};
|
|
262
|
+
storageKey = 'AMP_TLDTEST';
|
|
263
|
+
storage = new CookieStorage(options);
|
|
264
|
+
_a.label = 1;
|
|
265
|
+
case 1:
|
|
266
|
+
_a.trys.push([1, 3, , 4]);
|
|
267
|
+
return [4 /*yield*/, storage.transaction(storageKey, function (storageSync) {
|
|
268
|
+
try {
|
|
269
|
+
storageSync.set(1);
|
|
270
|
+
return storageSync.get();
|
|
271
|
+
}
|
|
272
|
+
finally {
|
|
273
|
+
storageSync.set(null);
|
|
274
|
+
}
|
|
275
|
+
})];
|
|
276
|
+
case 2:
|
|
277
|
+
res = _a.sent();
|
|
278
|
+
return [2 /*return*/, !!res];
|
|
279
|
+
case 3:
|
|
280
|
+
error_1 = _a.sent();
|
|
281
|
+
return [2 /*return*/, false];
|
|
282
|
+
case 4: return [2 /*return*/];
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
CookieStorage.prototype.transaction = function (key, callback) {
|
|
288
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
289
|
+
var locks, callbackWrapper;
|
|
290
|
+
var _this = this;
|
|
291
|
+
return tslib_1.__generator(this, function (_a) {
|
|
292
|
+
switch (_a.label) {
|
|
293
|
+
case 0:
|
|
294
|
+
locks = getLocks();
|
|
295
|
+
callbackWrapper = function () {
|
|
296
|
+
// construct a sync storage object that is scoped to
|
|
297
|
+
// Cookie with name <key>
|
|
298
|
+
var storageSync = {
|
|
299
|
+
get: function () { return _this.getSync(key); },
|
|
300
|
+
set: function (value) { return _this.setSync(key, value); },
|
|
301
|
+
};
|
|
302
|
+
return callback(storageSync);
|
|
303
|
+
};
|
|
304
|
+
// if 'locks' is missing, it is a legacy browser, just call the callback directly
|
|
305
|
+
// and settle for a transaction that isn't isolated across tabs
|
|
306
|
+
if (!locks) {
|
|
307
|
+
return [2 /*return*/, callbackWrapper()];
|
|
308
|
+
}
|
|
309
|
+
return [4 /*yield*/, locks.request("com.amplitude:cookie-lock:".concat(key), callbackWrapper)];
|
|
310
|
+
case 1: return [2 /*return*/, (_a.sent())];
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
};
|
|
238
315
|
return CookieStorage;
|
|
239
316
|
}());
|
|
240
317
|
exports.CookieStorage = CookieStorage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";;;;AACA,gDAAiD;AACjD,s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C;KACxC;IAAC,WAAM;QACN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC,CAAC;AAEF,IAAM,kCAAkC,GAAG,UAAC,KAAa;IACvD,uEAAuE;IACvE,kEAAkE;IAClE,IAAI;QACF,OAAO,kBAAkB,CAAC,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;KAC5D;IAAC,WAAM;QACN,OAAO,SAAS,CAAC;KAClB;AACH,CAAC,CAAC;AAEF;;;GAGG;AACI,IAAM,iBAAiB,GAAG,UAAC,KAAa;;IAC7C,OAAO,MAAA,sBAAsB,CAAC,KAAK,CAAC,mCAAI,kCAAkC,CAAC,KAAK,CAAC,CAAC;AACpF,CAAC,CAAC;AAFW,QAAA,iBAAiB,qBAE5B;AAEF;;;;GAIG;AACI,IAAM,aAAa,GAAG,UAAC,OAA2B,EAAE,OAA2B;IACpF,IAAI,OAAO,KAAK,EAAE,IAAI,OAAO,KAAK,EAAE,EAAE;QACpC,OAAO,IAAI,CAAC;KACb;IACD,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IACD,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC,CAAC;AAVW,QAAA,aAAa,iBAUxB","sourcesContent":["import { Storage, CookieStorageOptions, CookieStorageConfig } from '../types/storage';\nimport { getGlobalScope } from '../global-scope';\nimport { UUID } from '../utils/uuid';\n\n// CookieStore is a Web API not included in standard TypeScript lib types\n// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore\ninterface CookieStoreSetOptions {\n name: string;\n value: string;\n expires?: number;\n domain?: string;\n sameSite?: 'strict' | 'lax' | 'none';\n}\n\ninterface CookieStore {\n getAll(key: string): Promise<CookieStoreSetOptions[] | undefined>;\n}\n\ntype GlobalScopeWithCookieStore = {\n cookieStore?: CookieStore;\n} & typeof global;\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n config: CookieStorageConfig;\n\n constructor(options?: CookieStorageOptions, config: CookieStorageConfig = {}) {\n this.options = { ...options };\n this.config = config;\n }\n\n async isEnabled(): Promise<boolean> {\n const globalScope = getGlobalScope();\n /* istanbul ignore if */\n if (!globalScope || !globalScope.document) {\n return false;\n }\n\n const testValue = String(Date.now());\n const testCookieOptions = {\n ...this.options,\n expirationDays: 0.003, // expire in ~5 minutes\n };\n const testStorage = new CookieStorage<string>(testCookieOptions);\n const testKey = `AMP_TEST_${UUID().substring(0, 8)}`;\n try {\n await testStorage.set(testKey, testValue);\n const value = await testStorage.get(testKey);\n /* istanbul ignore next */\n if (value !== testValue && this.config.diagnosticsClient) {\n this.config.diagnosticsClient?.recordEvent('cookies.isEnabled.failure', {\n reason: 'Test Value mismatch',\n testKey,\n testValue,\n });\n }\n return value === testValue;\n } catch (e) {\n /* istanbul ignore next */\n if (this.config.diagnosticsClient) {\n const errMessage = e instanceof Error ? e.message : String(e);\n this.config.diagnosticsClient?.recordEvent('cookies.isEnabled.failure', {\n reason: 'Cookie getter/setter failed',\n testKey,\n testValue,\n error: errMessage,\n });\n }\n return false;\n } finally {\n await testStorage.remove(testKey);\n }\n }\n\n async get(key: string): Promise<T | undefined> {\n const value = await this.getRaw(key);\n if (!value) {\n return undefined;\n }\n try {\n const decodedValue = decodeCookieValue(value);\n if (decodedValue === undefined) {\n console.error(`Amplitude Logger [Error]: Failed to decode cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(decodedValue);\n } catch {\n console.error(`Amplitude Logger [Error]: Failed to parse cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const globalScope = getGlobalScope();\n\n // use CookieStore if available and enabled\n const globalScopeWithCookiesStore = globalScope as GlobalScopeWithCookieStore;\n try {\n const cookieStore = globalScopeWithCookiesStore?.cookieStore;\n if (cookieStore) {\n const cookies = await cookieStore.getAll(key);\n if (cookies) {\n /* istanbul ignore if */\n if (cookies.length > 1) {\n this.config.diagnosticsClient?.recordEvent('cookies.duplicate', {\n cookies: cookies.map((cookie) => cookie.domain),\n });\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.cookieStore');\n }\n\n for (const cookie of cookies) {\n if (isDomainEqual(cookie.domain, this.options.domain)) {\n return cookie.value;\n }\n }\n }\n }\n } catch (ignoreError) {\n /* istanbul ignore next */\n // if cookieStore had a surprise failure, fallback to document.cookie\n }\n\n const cookies = (globalScope?.document?.cookie.split('; ') ?? []).filter((c) => c.indexOf(key + '=') === 0);\n let match: string | undefined = undefined;\n\n // if matcher function is provided, use it to de-duplicate when there's more than one cookie\n /* istanbul ignore if */\n const duplicateResolverFn = this.config.duplicateResolverFn;\n if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {\n match = cookies.find((c) => {\n try {\n const res = duplicateResolverFn(c.substring(key.length + 1));\n if (!res) {\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.document.cookie');\n }\n return res;\n } catch (ignoreError) {\n /* istanbul ignore next */\n return false;\n }\n });\n }\n\n // if match was not found, just get the first one that matches the key\n if (!match) {\n match = cookies[0];\n }\n if (!match) {\n return undefined;\n }\n return match.substring(key.length + 1);\n }\n\n async set(key: string, value: T | null): Promise<void> {\n try {\n const expirationDays = this.options.expirationDays ?? 0;\n const expires = value !== null ? expirationDays : -1;\n let expireDate: Date | undefined = undefined;\n if (expires) {\n const date = new Date();\n date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);\n expireDate = date;\n }\n let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;\n if (expireDate) {\n str += `; expires=${expireDate.toUTCString()}`;\n }\n str += '; path=/';\n if (this.options.domain) {\n str += `; domain=${this.options.domain}`;\n }\n if (this.options.secure) {\n str += '; Secure';\n }\n if (this.options.sameSite) {\n str += `; SameSite=${this.options.sameSite}`;\n }\n const globalScope = getGlobalScope();\n if (globalScope) {\n globalScope.document.cookie = str;\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n console.error(`Amplitude Logger [Error]: Failed to set cookie for key: ${key}. Error: ${errorMessage}`);\n }\n }\n\n async remove(key: string): Promise<void> {\n await this.set(key, null);\n }\n\n async reset(): Promise<void> {\n return;\n }\n}\n\nconst decodeCookiesAsDefault = (value: string): string | undefined => {\n try {\n return decodeURIComponent(atob(value));\n } catch {\n return undefined;\n }\n};\n\nconst decodeCookiesWithDoubleUrlEncoding = (value: string): string | undefined => {\n // Modern Ruby (v7+) automatically encodes cookies with URL encoding by\n // https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html\n try {\n return decodeURIComponent(atob(decodeURIComponent(value)));\n } catch {\n return undefined;\n }\n};\n\n/**\n * Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).\n * Handles both standard encoding and double URL encoding (used by Ruby Rails v7+).\n */\nexport const decodeCookieValue = (value: string): string | undefined => {\n return decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(value);\n};\n\n/**\n * Compares two domain strings for equality, ignoring leading dots.\n * This is useful for comparing cookie domains since \".example.com\" and \"example.com\"\n * are effectively equivalent for cookie scoping.\n */\nexport const isDomainEqual = (domain1: string | undefined, domain2: string | undefined): boolean => {\n if (domain1 === '' && domain2 === '') {\n return true;\n }\n if (!domain1 || !domain2) {\n return false;\n }\n const normalized1 = domain1.startsWith('.') ? domain1.substring(1) : domain1;\n const normalized2 = domain2.startsWith('.') ? domain2.substring(1) : domain2;\n return normalized1.toLowerCase() === normalized2.toLowerCase();\n};\n"]}
|
|
1
|
+
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";;;;AACA,gDAAiD;AAoBjD,0BAA0B;AAC1B,IAAM,QAAQ,GAAG;;IACf,IAAM,WAAW,GAAG,IAAA,6BAAc,GAAE,CAAC;IACrC,OAAO,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,SAAS,0CAAE,KAAK,CAAC;AACvC,CAAC,CAAC;AAEF;IAIE,uBAAY,OAA8B,EAAE,MAAgC;QAAhC,uBAAA,EAAA,WAAgC;QAC1E,IAAI,CAAC,OAAO,wBAAQ,OAAO,CAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEK,iCAAS,GAAf;;;;;;;wBACQ,OAAO,GAAG,UAAU,CAAC;wBACrB,iBAAiB,wBAAQ,IAAI,CAAC,OAAO,CAAE,CAAC;wBACxC,WAAW,GAAG,IAAI,aAAa,CAAS,iBAAiB,CAAC,CAAC;wBAC3D,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC9B,qBAAM,WAAW,CAAC,WAAW,CAAU,OAAO,EAAE,UAAC,OAA4B;;gCAClF,IAAI;oCACF,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;oCACvB,IAAM,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;oCAC5B,IAAM,MAAM,GAAG,KAAK,KAAK,SAAS,CAAC;oCACnC,0BAA0B;oCAC1B,IAAI,CAAC,MAAM,IAAI,KAAI,CAAC,MAAM,CAAC,iBAAiB,EAAE;wCAC5C,MAAA,KAAI,CAAC,MAAM,CAAC,iBAAiB,0CAAE,WAAW,CAAC,2BAA2B,EAAE;4CACtE,MAAM,EAAE,qBAAqB;4CAC7B,OAAO,SAAA;4CACP,SAAS,WAAA;4CACT,IAAI,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;;IAC7C,OAAO,MAAA,sBAAsB,CAAC,KAAK,CAAC,mCAAI,kCAAkC,CAAC,KAAK,CAAC,CAAC;AACpF,CAAC,CAAC;AAFW,QAAA,iBAAiB,qBAE5B;AAEF;;;;GAIG;AACI,IAAM,aAAa,GAAG,UAAC,OAA2B,EAAE,OAA2B;IACpF,IAAI,OAAO,KAAK,EAAE,IAAI,OAAO,KAAK,EAAE,EAAE;QACpC,OAAO,IAAI,CAAC;KACb;IACD,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,EAAE;QACxB,OAAO,KAAK,CAAC;KACd;IACD,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,IAAM,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IAC7E,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC,CAAC;AAVW,QAAA,aAAa,iBAUxB","sourcesContent":["import { Storage, StorageSync, CookieStorageOptions, CookieStorageConfig } from '../types/storage';\nimport { getGlobalScope } from '../global-scope';\n\n// CookieStore is a Web API not included in standard TypeScript lib types\n// https://developer.mozilla.org/en-US/docs/Web/API/CookieStore\ninterface CookieStoreSetOptions {\n name: string;\n value: string;\n expires?: number;\n domain?: string;\n sameSite?: 'strict' | 'lax' | 'none';\n}\n\ninterface CookieStore {\n getAll(key: string): Promise<CookieStoreSetOptions[] | undefined>;\n}\n\ntype GlobalScopeWithCookieStore = {\n cookieStore?: CookieStore;\n} & typeof global;\n\n/* istanbul ignore next */\nconst getLocks = (): typeof global.navigator.locks | undefined => {\n const globalScope = getGlobalScope();\n return globalScope?.navigator?.locks;\n};\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n config: CookieStorageConfig;\n\n constructor(options?: CookieStorageOptions, config: CookieStorageConfig = {}) {\n this.options = { ...options };\n this.config = config;\n }\n\n async isEnabled(): Promise<boolean> {\n const testKey = 'AMP_TEST';\n const testCookieOptions = { ...this.options };\n const testStorage = new CookieStorage<string>(testCookieOptions);\n const testValue = String(Date.now());\n return await testStorage.transaction<boolean>(testKey, (storage: StorageSync<string>) => {\n try {\n storage.set(testValue);\n const value = storage.get();\n const result = value === testValue;\n /* istanbul ignore next */\n if (!result && this.config.diagnosticsClient) {\n this.config.diagnosticsClient?.recordEvent('cookies.isEnabled.failure', {\n reason: 'Test Value mismatch',\n testKey,\n testValue,\n sync: true,\n });\n }\n return result;\n } catch (e) {\n /* istanbul ignore next */\n if (this.config.diagnosticsClient) {\n const errMessage = e instanceof Error ? e.message : String(e);\n this.config.diagnosticsClient?.recordEvent('cookies.isEnabled.failure', {\n reason: 'Cookie getter/setter failed',\n testKey,\n testValue,\n error: errMessage,\n sync: true,\n });\n }\n return false;\n } finally {\n // clean-up the AMP_TEST cookie behind us\n storage.set(null);\n }\n });\n }\n\n async get(key: string): Promise<T | undefined> {\n const value = await this.getRaw(key);\n return this.decodeCookieValue(key, value);\n }\n\n private decodeCookieValue(key: string, value: string | undefined): T | undefined {\n if (!value) {\n return undefined;\n }\n try {\n const decodedValue = decodeCookieValue(value);\n if (decodedValue === undefined) {\n console.error(`Amplitude Logger [Error]: Failed to decode cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return JSON.parse(decodedValue);\n } catch {\n console.error(`Amplitude Logger [Error]: Failed to parse cookie value for key: ${key}, value: ${value}`);\n return undefined;\n }\n }\n\n private getSync(key: string): T | undefined {\n const value = this.getRawSync(key);\n return this.decodeCookieValue(key, value);\n }\n\n async getRaw(key: string): Promise<string | undefined> {\n const globalScope = getGlobalScope();\n\n // use CookieStore if available and enabled\n const globalScopeWithCookiesStore = globalScope as GlobalScopeWithCookieStore;\n try {\n const cookieStore = globalScopeWithCookiesStore?.cookieStore;\n if (cookieStore) {\n const cookies = await cookieStore.getAll(key);\n if (cookies) {\n /* istanbul ignore if */\n if (cookies.length > 1) {\n this.config.diagnosticsClient?.recordEvent('cookies.duplicate', {\n cookies: cookies.map((cookie) => cookie.domain),\n });\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.cookieStore');\n }\n\n for (const cookie of cookies) {\n if (isDomainEqual(cookie.domain, this.options.domain)) {\n return cookie.value;\n }\n }\n }\n }\n } catch (ignoreError) {\n /* istanbul ignore next */\n // if cookieStore had a surprise failure, fallback to document.cookie\n }\n\n return this.getRawSync(key);\n }\n\n private getRawSync(key: string): string | undefined {\n const globalScope = getGlobalScope();\n const cookies = (globalScope?.document?.cookie.split('; ') ?? []).filter((c) => c.indexOf(key + '=') === 0);\n let match: string | undefined = undefined;\n\n // if matcher function is provided, use it to de-duplicate when there's more than one cookie\n /* istanbul ignore if */\n const duplicateResolverFn = this.config.duplicateResolverFn;\n if (typeof duplicateResolverFn === 'function' && cookies.length > 1) {\n match = cookies.find((c) => {\n try {\n const res = duplicateResolverFn(c.substring(key.length + 1));\n if (!res) {\n this.config.diagnosticsClient?.increment('cookies.duplicate.occurrence.document.cookie');\n }\n return res;\n } catch (ignoreError) {\n /* istanbul ignore next */\n return false;\n }\n });\n }\n\n // if match was not found, just get the first one that matches the key\n if (!match) {\n match = cookies[0];\n }\n if (!match) {\n return undefined;\n }\n return match.substring(key.length + 1);\n }\n\n async set(key: string, value: T | null): Promise<void> {\n this.setSync(key, value);\n }\n\n private setSync(key: string, value: T | null): void {\n try {\n const expirationDays = this.options.expirationDays ?? 0;\n const expires = value !== null ? expirationDays : -1;\n let expireDate: Date | undefined = undefined;\n if (expires) {\n const date = new Date();\n date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);\n expireDate = date;\n }\n let str = `${key}=${btoa(encodeURIComponent(JSON.stringify(value)))}`;\n if (expireDate) {\n str += `; expires=${expireDate.toUTCString()}`;\n }\n str += '; path=/';\n if (this.options.domain) {\n str += `; domain=${this.options.domain}`;\n }\n if (this.options.secure) {\n str += '; Secure';\n }\n if (this.options.sameSite) {\n str += `; SameSite=${this.options.sameSite}`;\n }\n const globalScope = getGlobalScope();\n if (globalScope) {\n globalScope.document.cookie = str;\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n console.error(`Amplitude Logger [Error]: Failed to set cookie for key: ${key}. Error: ${errorMessage}`);\n }\n }\n\n async remove(key: string): Promise<void> {\n await this.set(key, null);\n }\n\n async reset(): Promise<void> {\n return;\n }\n\n static async isDomainWritable(domain: string): Promise<boolean> {\n const options = {\n domain: '.' + domain,\n };\n const storageKey = 'AMP_TLDTEST';\n const storage = new CookieStorage<number>(options);\n try {\n const res = await storage.transaction(storageKey, (storageSync) => {\n try {\n storageSync.set(1);\n return storageSync.get();\n } finally {\n storageSync.set(null);\n }\n });\n return !!res;\n } catch (error) {\n return false;\n }\n }\n\n private async transaction<ReturnType>(\n key: string,\n callback: (storageSync: StorageSync<T>) => ReturnType,\n ): Promise<ReturnType> {\n const locks = getLocks();\n const callbackWrapper = () => {\n // construct a sync storage object that is scoped to\n // Cookie with name <key>\n const storageSync: StorageSync<T> = {\n get: () => this.getSync(key),\n set: (value: T | null) => this.setSync(key, value),\n };\n return callback(storageSync);\n };\n\n // if 'locks' is missing, it is a legacy browser, just call the callback directly\n // and settle for a transaction that isn't isolated across tabs\n if (!locks) {\n return callbackWrapper();\n }\n return (await locks.request(`com.amplitude:cookie-lock:${key}`, callbackWrapper)) as ReturnType;\n }\n}\n\nconst decodeCookiesAsDefault = (value: string): string | undefined => {\n try {\n return decodeURIComponent(atob(value));\n } catch {\n return undefined;\n }\n};\n\nconst decodeCookiesWithDoubleUrlEncoding = (value: string): string | undefined => {\n // Modern Ruby (v7+) automatically encodes cookies with URL encoding by\n // https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html\n try {\n return decodeURIComponent(atob(decodeURIComponent(value)));\n } catch {\n return undefined;\n }\n};\n\n/**\n * Decodes a cookie value that was encoded with btoa(encodeURIComponent(...)).\n * Handles both standard encoding and double URL encoding (used by Ruby Rails v7+).\n */\nexport const decodeCookieValue = (value: string): string | undefined => {\n return decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(value);\n};\n\n/**\n * Compares two domain strings for equality, ignoring leading dots.\n * This is useful for comparing cookie domains since \".example.com\" and \"example.com\"\n * are effectively equivalent for cookie scoping.\n */\nexport const isDomainEqual = (domain1: string | undefined, domain2: string | undefined): boolean => {\n if (domain1 === '' && domain2 === '') {\n return true;\n }\n if (!domain1 || !domain2) {\n return false;\n }\n const normalized1 = domain1.startsWith('.') ? domain1.substring(1) : domain1;\n const normalized2 = domain2.startsWith('.') ? domain2.substring(1) : domain2;\n return normalized1.toLowerCase() === normalized2.toLowerCase();\n};\n"]}
|
|
@@ -7,6 +7,10 @@ export interface Storage<T> {
|
|
|
7
7
|
remove(key: string): Promise<void>;
|
|
8
8
|
reset(): Promise<void>;
|
|
9
9
|
}
|
|
10
|
+
export interface StorageSync<T> {
|
|
11
|
+
get: () => T | undefined;
|
|
12
|
+
set: (value: T | null) => void;
|
|
13
|
+
}
|
|
10
14
|
export interface CookieStorageOptions {
|
|
11
15
|
domain?: string;
|
|
12
16
|
expirationDays?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../../src/types/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AAEvE,MAAM,WAAW,OAAO,CAAC,CAAC;IACxB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IACzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,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C,IAAI,CAAC,CAAC;IACnC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,MAAM,WAAW,WAAW,CAAC,CAAC;IAC5B,GAAG,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;IACzB,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,KAAK,IAAI,CAAC;CAChC;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,mBAAmB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;IACjD,iBAAiB,CAAC,EAAE,kBAAkB,CAAC;CACxC;AAED,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,cAAc,GAAG,gBAAgB,GAAG,MAAM,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/types/storage.ts"],"names":[],"mappings":"","sourcesContent":["import { IDiagnosticsClient } from '../diagnostics/diagnostics-client';\n\nexport interface Storage<T> {\n isEnabled(): Promise<boolean>;\n get(key: string): Promise<T | undefined>;\n getRaw(key: string): Promise<string | undefined>;\n set(key: string, value: T): Promise<void>;\n remove(key: string): Promise<void>;\n reset(): Promise<void>;\n}\n\nexport interface CookieStorageOptions {\n domain?: string;\n expirationDays?: number;\n sameSite?: string;\n secure?: boolean;\n}\n\n/**\n * Configuration for CookieStorage behavior.\n * Separated from options to keep storage-specific config distinct from cookie attributes.\n */\nexport interface CookieStorageConfig {\n /**\n * Function to resolve duplicate cookies when multiple cookies with the same key exist.\n * Returns true if the cookie value should be used, false otherwise.\n */\n duplicateResolverFn?: (value: string) => boolean;\n diagnosticsClient?: IDiagnosticsClient;\n}\n\nexport type IdentityStorageType = 'cookie' | 'localStorage' | 'sessionStorage' | 'none';\n"]}
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/types/storage.ts"],"names":[],"mappings":"","sourcesContent":["import { IDiagnosticsClient } from '../diagnostics/diagnostics-client';\n\nexport interface Storage<T> {\n isEnabled(): Promise<boolean>;\n get(key: string): Promise<T | undefined>;\n getRaw(key: string): Promise<string | undefined>;\n set(key: string, value: T): Promise<void>;\n remove(key: string): Promise<void>;\n reset(): Promise<void>;\n}\n\nexport interface StorageSync<T> {\n get: () => T | undefined;\n set: (value: T | null) => void;\n}\n\nexport interface CookieStorageOptions {\n domain?: string;\n expirationDays?: number;\n sameSite?: string;\n secure?: boolean;\n}\n\n/**\n * Configuration for CookieStorage behavior.\n * Separated from options to keep storage-specific config distinct from cookie attributes.\n */\nexport interface CookieStorageConfig {\n /**\n * Function to resolve duplicate cookies when multiple cookies with the same key exist.\n * Returns true if the cookie value should be used, false otherwise.\n */\n duplicateResolverFn?: (value: string) => boolean;\n diagnosticsClient?: IDiagnosticsClient;\n}\n\nexport type IdentityStorageType = 'cookie' | 'localStorage' | 'sessionStorage' | 'none';\n"]}
|
|
@@ -2,5 +2,5 @@ export declare const AMPLITUDE_ORIGIN = "https://app.amplitude.com";
|
|
|
2
2
|
export declare const AMPLITUDE_ORIGIN_EU = "https://app.eu.amplitude.com";
|
|
3
3
|
export declare const AMPLITUDE_ORIGIN_STAGING = "https://apps.stag2.amplitude.com";
|
|
4
4
|
export declare const AMPLITUDE_ORIGINS_MAP: Record<string, string>;
|
|
5
|
-
export declare const AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = "https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.
|
|
5
|
+
export declare const AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = "https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.2.js.gz";
|
|
6
6
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -8,5 +8,5 @@ export var AMPLITUDE_ORIGINS_MAP = {
|
|
|
8
8
|
STAGING: AMPLITUDE_ORIGIN_STAGING,
|
|
9
9
|
};
|
|
10
10
|
// Background capture script URL (shared between autocapture and session-replay)
|
|
11
|
-
export var AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = 'https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.
|
|
11
|
+
export var AMPLITUDE_BACKGROUND_CAPTURE_SCRIPT_URL = 'https://cdn.amplitude.com/libs/background-capture-1.0.0-alpha.2.js.gz';
|
|
12
12
|
//# sourceMappingURL=constants.js.map
|