@amplitude/analytics-client-common 2.2.2 → 2.2.4
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/global-scope.d.ts.map +1 -1
- package/lib/cjs/global-scope.js +6 -0
- package/lib/cjs/global-scope.js.map +1 -1
- package/lib/cjs/storage/cookie.js +27 -9
- package/lib/cjs/storage/cookie.js.map +1 -1
- package/lib/cjs/types/global.d.ts +2 -0
- package/lib/cjs/types/global.d.ts.map +1 -0
- package/lib/cjs/types/global.js +3 -0
- package/lib/cjs/types/global.js.map +1 -0
- package/lib/esm/global-scope.d.ts.map +1 -1
- package/lib/esm/global-scope.js +6 -0
- package/lib/esm/global-scope.js.map +1 -1
- package/lib/esm/storage/cookie.js +27 -9
- package/lib/esm/storage/cookie.js.map +1 -1
- package/lib/esm/types/global.d.ts +2 -0
- package/lib/esm/types/global.d.ts.map +1 -0
- package/lib/esm/types/global.js +4 -0
- package/lib/esm/types/global.js.map +1 -0
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-scope.d.ts","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,cAAc,QAAO,iBAAiB,GAAG,
|
|
1
|
+
{"version":3,"file":"global-scope.d.ts","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,cAAc,QAAO,iBAAiB,GAAG,SAoBrD,CAAC"}
|
package/lib/cjs/global-scope.js
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.getGlobalScope = void 0;
|
|
5
5
|
var getGlobalScope = function () {
|
|
6
|
+
// This should only be used for integrations with Amplitude that are not running in a browser environment
|
|
7
|
+
// We need to specify the name of the global variable as a string to prevent it from being minified
|
|
8
|
+
var ampIntegrationContextName = 'ampIntegrationContext';
|
|
9
|
+
if (typeof globalThis !== 'undefined' && typeof globalThis[ampIntegrationContextName] !== 'undefined') {
|
|
10
|
+
return globalThis[ampIntegrationContextName];
|
|
11
|
+
}
|
|
6
12
|
if (typeof globalThis !== 'undefined') {
|
|
7
13
|
return globalThis;
|
|
8
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-scope.js","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,6DAA6D;;;AAEtD,IAAM,cAAc,GAAG;IAC5B,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;QACrC,OAAO,UAAU,CAAC;KACnB;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QAC/B,OAAO,IAAI,CAAC;KACb;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"global-scope.js","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,6DAA6D;;;AAEtD,IAAM,cAAc,GAAG;IAC5B,yGAAyG;IACzG,qGAAqG;IACrG,IAAM,yBAAyB,GAAG,uBAAkD,CAAC;IACrF,IAAI,OAAO,UAAU,KAAK,WAAW,IAAI,OAAO,UAAU,CAAC,yBAAyB,CAAC,KAAK,WAAW,EAAE;QACrG,OAAO,UAAU,CAAC,yBAAyB,CAAsB,CAAC;KACnE;IACD,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;QACrC,OAAO,UAAU,CAAC;KACnB;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QAC/B,OAAO,IAAI,CAAC;KACb;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AApBW,QAAA,cAAc,kBAoBzB","sourcesContent":["/* eslint-disable no-restricted-globals */\n/* Only file allowed to access to globalThis, window, self */\n\nexport const getGlobalScope = (): typeof globalThis | undefined => {\n // This should only be used for integrations with Amplitude that are not running in a browser environment\n // We need to specify the name of the global variable as a string to prevent it from being minified\n const ampIntegrationContextName = 'ampIntegrationContext' as keyof typeof globalThis;\n if (typeof globalThis !== 'undefined' && typeof globalThis[ampIntegrationContextName] !== 'undefined') {\n return globalThis[ampIntegrationContextName] as typeof globalThis;\n }\n if (typeof globalThis !== 'undefined') {\n return globalThis;\n }\n if (typeof window !== 'undefined') {\n return window;\n }\n if (typeof self !== 'undefined') {\n return self;\n }\n if (typeof global !== 'undefined') {\n return global;\n }\n return undefined;\n};\n"]}
|
|
@@ -43,25 +43,25 @@ var CookieStorage = /** @class */ (function () {
|
|
|
43
43
|
});
|
|
44
44
|
};
|
|
45
45
|
CookieStorage.prototype.get = function (key) {
|
|
46
|
+
var _a;
|
|
46
47
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
47
|
-
var value;
|
|
48
|
-
return tslib_1.__generator(this, function (
|
|
49
|
-
switch (
|
|
48
|
+
var value, decodedValue;
|
|
49
|
+
return tslib_1.__generator(this, function (_b) {
|
|
50
|
+
switch (_b.label) {
|
|
50
51
|
case 0: return [4 /*yield*/, this.getRaw(key)];
|
|
51
52
|
case 1:
|
|
52
|
-
value =
|
|
53
|
+
value = _b.sent();
|
|
53
54
|
if (!value) {
|
|
54
55
|
return [2 /*return*/, undefined];
|
|
55
56
|
}
|
|
56
57
|
try {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
catch (_b) {
|
|
58
|
+
decodedValue = (_a = decodeCookiesAsDefault(value)) !== null && _a !== void 0 ? _a : decodeCookiesWithDoubleUrlEncoding(value);
|
|
59
|
+
if (decodedValue === undefined) {
|
|
61
60
|
console.error("Amplitude Logger [Error]: Failed to decode cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
61
|
+
return [2 /*return*/, undefined];
|
|
62
62
|
}
|
|
63
63
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
64
|
-
return [2 /*return*/, JSON.parse(
|
|
64
|
+
return [2 /*return*/, JSON.parse(decodedValue)];
|
|
65
65
|
}
|
|
66
66
|
catch (_c) {
|
|
67
67
|
console.error("Amplitude Logger [Error]: Failed to parse cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
@@ -150,4 +150,22 @@ var CookieStorage = /** @class */ (function () {
|
|
|
150
150
|
return CookieStorage;
|
|
151
151
|
}());
|
|
152
152
|
exports.CookieStorage = CookieStorage;
|
|
153
|
+
var decodeCookiesAsDefault = function (value) {
|
|
154
|
+
try {
|
|
155
|
+
return decodeURIComponent(atob(value));
|
|
156
|
+
}
|
|
157
|
+
catch (_a) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var decodeCookiesWithDoubleUrlEncoding = function (value) {
|
|
162
|
+
// Modern Ruby (v7+) automatically encodes cookies with URL encoding by
|
|
163
|
+
// https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html
|
|
164
|
+
try {
|
|
165
|
+
return decodeURIComponent(atob(decodeURIComponent(value)));
|
|
166
|
+
}
|
|
167
|
+
catch (_a) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
153
171
|
//# sourceMappingURL=cookie.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";;;AACA,gDAAiD;AAEjD;IAIE,uBAAY,OAA8B;QACxC,IAAI,CAAC,OAAO,wBAAQ,OAAO,CAAE,CAAC;IAChC,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IA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AAC,QAAQ,EAAE;wBACzB,GAAG,IAAI,qBAAc,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAE,CAAC;qBAC9C;oBACK,WAAW,GAAG,IAAA,6BAAc,GAAE,CAAC;oBACrC,IAAI,WAAW,EAAE;wBACf,WAAW,CAAC,QAAQ,CAAC,MAAM,GAAG,GAAG,CAAC;qBACnC;iBACF;gBAAC,OAAO,KAAK,EAAE;oBACR,YAAY,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;oBAC5E,OAAO,CAAC,KAAK,CAAC,kEAA2D,GAAG,sBAAY,YAAY,CAAE,CAAC,CAAC;iBACzG;;;;KACF;IAEK,8BAAM,GAAZ,UAAa,GAAW;;;;4BACtB,qBAAM,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,EAAA;;wBAAzB,SAAyB,CAAC;;;;;KAC3B;IAEK,6BAAK,GAAX;;;gBACE,sBAAO;;;KACR;IACH,oBAAC;AAAD,CAAC,AAnGD,IAmGC;AAnGY,sCAAa;AAqG1B,IAAM,sBAAsB,GAAG,UAAC,KAAa;IAC3C,IAAI;QACF,OAAO,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;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","sourcesContent":["import { Storage, CookieStorageOptions } from '@amplitude/analytics-types';\nimport { getGlobalScope } from '../global-scope';\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n private static testValue: undefined | string;\n\n constructor(options?: CookieStorageOptions) {\n this.options = { ...options };\n }\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!getGlobalScope()) {\n return false;\n }\n\n CookieStorage.testValue = String(Date.now());\n const testStrorage = new CookieStorage<string>(this.options);\n const testKey = 'AMP_TEST';\n try {\n await testStrorage.set(testKey, CookieStorage.testValue);\n const value = await testStrorage.get(testKey);\n return value === CookieStorage.testValue;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStrorage.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 = decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(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 const cookie = globalScope?.document?.cookie.split('; ') ?? [];\n const match = cookie.find((c) => c.indexOf(key + '=') === 0);\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global.d.ts","sourceRoot":"","sources":["../../../src/types/global.ts"],"names":[],"mappings":"AAEA,eAAO,IAAI,qBAAqB,EAAE,OAAO,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../../src/types/global.ts"],"names":[],"mappings":"","sourcesContent":["/* eslint-disable no-restricted-globals */\n// eslint-disable-next-line no-var\nexport var ampIntegrationContext: typeof globalThis;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-scope.d.ts","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,cAAc,QAAO,iBAAiB,GAAG,
|
|
1
|
+
{"version":3,"file":"global-scope.d.ts","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,cAAc,QAAO,iBAAiB,GAAG,SAoBrD,CAAC"}
|
package/lib/esm/global-scope.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/* eslint-disable no-restricted-globals */
|
|
2
2
|
/* Only file allowed to access to globalThis, window, self */
|
|
3
3
|
export var getGlobalScope = function () {
|
|
4
|
+
// This should only be used for integrations with Amplitude that are not running in a browser environment
|
|
5
|
+
// We need to specify the name of the global variable as a string to prevent it from being minified
|
|
6
|
+
var ampIntegrationContextName = 'ampIntegrationContext';
|
|
7
|
+
if (typeof globalThis !== 'undefined' && typeof globalThis[ampIntegrationContextName] !== 'undefined') {
|
|
8
|
+
return globalThis[ampIntegrationContextName];
|
|
9
|
+
}
|
|
4
10
|
if (typeof globalThis !== 'undefined') {
|
|
5
11
|
return globalThis;
|
|
6
12
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"global-scope.js","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,6DAA6D;AAE7D,MAAM,CAAC,IAAM,cAAc,GAAG;IAC5B,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;QACrC,OAAO,UAAU,CAAC;KACnB;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QAC/B,OAAO,IAAI,CAAC;KACb;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["/* eslint-disable no-restricted-globals */\n/* Only file allowed to access to globalThis, window, self */\n\nexport const getGlobalScope = (): typeof globalThis | undefined => {\n if (typeof globalThis !== 'undefined') {\n return globalThis;\n }\n if (typeof window !== 'undefined') {\n return window;\n }\n if (typeof self !== 'undefined') {\n return self;\n }\n if (typeof global !== 'undefined') {\n return global;\n }\n return undefined;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"global-scope.js","sourceRoot":"","sources":["../../src/global-scope.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,6DAA6D;AAE7D,MAAM,CAAC,IAAM,cAAc,GAAG;IAC5B,yGAAyG;IACzG,qGAAqG;IACrG,IAAM,yBAAyB,GAAG,uBAAkD,CAAC;IACrF,IAAI,OAAO,UAAU,KAAK,WAAW,IAAI,OAAO,UAAU,CAAC,yBAAyB,CAAC,KAAK,WAAW,EAAE;QACrG,OAAO,UAAU,CAAC,yBAAyB,CAAsB,CAAC;KACnE;IACD,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE;QACrC,OAAO,UAAU,CAAC;KACnB;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE;QAC/B,OAAO,IAAI,CAAC;KACb;IACD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;QACjC,OAAO,MAAM,CAAC;KACf;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["/* eslint-disable no-restricted-globals */\n/* Only file allowed to access to globalThis, window, self */\n\nexport const getGlobalScope = (): typeof globalThis | undefined => {\n // This should only be used for integrations with Amplitude that are not running in a browser environment\n // We need to specify the name of the global variable as a string to prevent it from being minified\n const ampIntegrationContextName = 'ampIntegrationContext' as keyof typeof globalThis;\n if (typeof globalThis !== 'undefined' && typeof globalThis[ampIntegrationContextName] !== 'undefined') {\n return globalThis[ampIntegrationContextName] as typeof globalThis;\n }\n if (typeof globalThis !== 'undefined') {\n return globalThis;\n }\n if (typeof window !== 'undefined') {\n return window;\n }\n if (typeof self !== 'undefined') {\n return self;\n }\n if (typeof global !== 'undefined') {\n return global;\n }\n return undefined;\n};\n"]}
|
|
@@ -41,25 +41,25 @@ var CookieStorage = /** @class */ (function () {
|
|
|
41
41
|
});
|
|
42
42
|
};
|
|
43
43
|
CookieStorage.prototype.get = function (key) {
|
|
44
|
+
var _a;
|
|
44
45
|
return __awaiter(this, void 0, void 0, function () {
|
|
45
|
-
var value;
|
|
46
|
-
return __generator(this, function (
|
|
47
|
-
switch (
|
|
46
|
+
var value, decodedValue;
|
|
47
|
+
return __generator(this, function (_b) {
|
|
48
|
+
switch (_b.label) {
|
|
48
49
|
case 0: return [4 /*yield*/, this.getRaw(key)];
|
|
49
50
|
case 1:
|
|
50
|
-
value =
|
|
51
|
+
value = _b.sent();
|
|
51
52
|
if (!value) {
|
|
52
53
|
return [2 /*return*/, undefined];
|
|
53
54
|
}
|
|
54
55
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
catch (_b) {
|
|
56
|
+
decodedValue = (_a = decodeCookiesAsDefault(value)) !== null && _a !== void 0 ? _a : decodeCookiesWithDoubleUrlEncoding(value);
|
|
57
|
+
if (decodedValue === undefined) {
|
|
59
58
|
console.error("Amplitude Logger [Error]: Failed to decode cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
59
|
+
return [2 /*return*/, undefined];
|
|
60
60
|
}
|
|
61
61
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
62
|
-
return [2 /*return*/, JSON.parse(
|
|
62
|
+
return [2 /*return*/, JSON.parse(decodedValue)];
|
|
63
63
|
}
|
|
64
64
|
catch (_c) {
|
|
65
65
|
console.error("Amplitude Logger [Error]: Failed to parse cookie value for key: ".concat(key, ", value: ").concat(value));
|
|
@@ -148,4 +148,22 @@ var CookieStorage = /** @class */ (function () {
|
|
|
148
148
|
return CookieStorage;
|
|
149
149
|
}());
|
|
150
150
|
export { CookieStorage };
|
|
151
|
+
var decodeCookiesAsDefault = function (value) {
|
|
152
|
+
try {
|
|
153
|
+
return decodeURIComponent(atob(value));
|
|
154
|
+
}
|
|
155
|
+
catch (_a) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
var decodeCookiesWithDoubleUrlEncoding = function (value) {
|
|
160
|
+
// Modern Ruby (v7+) automatically encodes cookies with URL encoding by
|
|
161
|
+
// https://api.rubyonrails.org/classes/ActionDispatch/Cookies.html
|
|
162
|
+
try {
|
|
163
|
+
return decodeURIComponent(atob(decodeURIComponent(value)));
|
|
164
|
+
}
|
|
165
|
+
catch (_a) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
151
169
|
//# sourceMappingURL=cookie.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD;IAIE,uBAAY,OAA8B;QACxC,IAAI,CAAC,OAAO,gBAAQ,OAAO,CAAE,CAAC;IAChC,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,cAAc,EAAE,EAAE;4BACrB,sBAAO,KAAK,EAAC;yBACd;wBAED,aAAa,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBACvC,YAAY,GAAG,IAAI,aAAa,CAAS,IAAI,CAAC,OAAO,CAAC,CAAC;wBACvD,OAAO,GAAG,UAAU,CAAC;;;;wBAEzB,qBAAM,YAAY,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,EAAA;;wBAAxD,SAAwD,CAAC;wBAC3C,qBAAM,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAvC,KAAK,GAAG,SAA+B;wBAC7C,sBAAO,KAAK,KAAK,aAAa,CAAC,SAAS,EAAC;;;wBAEzC,0BAA0B;wBAC1B,sBAAO,KAAK,EAAC;4BAEb,qBAAM,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,EAAA;;wBAAlC,SAAkC,CAAC;;;;;;KAEtC;IAEK,2BAAG,GAAT,UAAU,GAAW
|
|
1
|
+
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,cAAc,EAAE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BAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;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","sourcesContent":["import { Storage, CookieStorageOptions } from '@amplitude/analytics-types';\nimport { getGlobalScope } from '../global-scope';\n\nexport class CookieStorage<T> implements Storage<T> {\n options: CookieStorageOptions;\n private static testValue: undefined | string;\n\n constructor(options?: CookieStorageOptions) {\n this.options = { ...options };\n }\n\n async isEnabled(): Promise<boolean> {\n /* istanbul ignore if */\n if (!getGlobalScope()) {\n return false;\n }\n\n CookieStorage.testValue = String(Date.now());\n const testStrorage = new CookieStorage<string>(this.options);\n const testKey = 'AMP_TEST';\n try {\n await testStrorage.set(testKey, CookieStorage.testValue);\n const value = await testStrorage.get(testKey);\n return value === CookieStorage.testValue;\n } catch {\n /* istanbul ignore next */\n return false;\n } finally {\n await testStrorage.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 = decodeCookiesAsDefault(value) ?? decodeCookiesWithDoubleUrlEncoding(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 const cookie = globalScope?.document?.cookie.split('; ') ?? [];\n const match = cookie.find((c) => c.indexOf(key + '=') === 0);\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global.d.ts","sourceRoot":"","sources":["../../../src/types/global.ts"],"names":[],"mappings":"AAEA,eAAO,IAAI,qBAAqB,EAAE,OAAO,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global.js","sourceRoot":"","sources":["../../../src/types/global.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,kCAAkC;AAClC,MAAM,CAAC,IAAI,qBAAwC,CAAC","sourcesContent":["/* eslint-disable no-restricted-globals */\n// eslint-disable-next-line no-var\nexport var ampIntegrationContext: typeof globalThis;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amplitude/analytics-client-common",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "Amplitude Inc",
|
|
6
6
|
"homepage": "https://github.com/amplitude/Amplitude-TypeScript",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"files": [
|
|
44
44
|
"lib"
|
|
45
45
|
],
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "bd12b9b751890e3e1b802235a748d729c624866f"
|
|
47
47
|
}
|