@amplitude/analytics-core 2.40.1 → 2.40.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.
@@ -1 +1 @@
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;AAsBtF,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;IAyB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAmBxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA6DhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkChD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;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,OAOxF,CAAC"}
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;AAsBtF,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;IA0B7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAmBxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA6DhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkChD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B;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,OAOxF,CAAC"}
@@ -12,12 +12,13 @@ var CookieStorage = /** @class */ (function () {
12
12
  }
13
13
  CookieStorage.prototype.isEnabled = function () {
14
14
  return tslib_1.__awaiter(this, void 0, void 0, function () {
15
- var testValue, testCookieOptions, testStorage, testKey, value, _a;
15
+ var globalScope, testValue, testCookieOptions, testStorage, testKey, value, _a;
16
16
  return tslib_1.__generator(this, function (_b) {
17
17
  switch (_b.label) {
18
18
  case 0:
19
+ globalScope = (0, global_scope_1.getGlobalScope)();
19
20
  /* istanbul ignore if */
20
- if (!(0, global_scope_1.getGlobalScope)()) {
21
+ if (!globalScope || !globalScope.document) {
21
22
  return [2 /*return*/, false];
22
23
  }
23
24
  testValue = String(Date.now());
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";;;;AACA,gDAAiD;AACjD,sCAAqC;AAoBrC;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;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,IAAA,6BAAc,GAAE,EAAE;4BACrB,sBAAO,KAAK,EAAC;yBACd;wBAEK,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC/B,iBAAiB,yCAClB,IAAI,CAAC,OAAO,KACf,cAAc,EAAE,KAAK,GACtB,CAAC;wBACI,WAAW,GAAG,IAAI,aAAa,CAAS,iBAAiB,CAAC,CAAC;wBAC3D,OAAO,GAAG,mBAAY,IAAA,WAAI,GAAE,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAE,CAAC;;;;wBAEnD,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,EAAA;;wBAAzC,SAAyC,CAAC;wBAC5B,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAtC,KAAK,GAAG,SAA8B;wBAC5C,sBAAO,KAAK,KAAK,SAAS,EAAC;;;wBAE3B,0BAA0B;wBAC1B,sBAAO,KAAK,EAAC;4BAEb,qBAAM,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,EAAA;;wBAAjC,SAAiC,CAAC;;;;;;KAErC;IAEK,2BAAG,GAAT,UAAU,GAAW;;;;;4BACL,qBAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAA;;wBAA9B,KAAK,GAAG,SAAsB;wBAC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AI,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;AAPW,QAAA,aAAa,iBAOxB","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 /* istanbul ignore if */\n if (!getGlobalScope()) {\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 return value === testValue;\n } catch {\n /* istanbul ignore next */\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 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;AACjD,sCAAqC;AAoBrC;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,WAAW,GAAG,IAAA,6BAAc,GAAE,CAAC;wBACrC,wBAAwB;wBACxB,IAAI,CAAC,WAAW,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE;4BACzC,sBAAO,KAAK,EAAC;yBACd;wBAEK,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC/B,iBAAiB,yCAClB,IAAI,CAAC,OAAO,KACf,cAAc,EAAE,KAAK,GACtB,CAAC;wBACI,WAAW,GAAG,IAAI,aAAa,CAAS,iBAAiB,CAAC,CAAC;wBAC3D,OAAO,GAAG,mBAAY,IAAA,WAAI,GAAE,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAE,CAAC;;;;wBAEnD,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,CAAC,EAAA;;wBAAzC,SAAyC,CAAC;wBAC5B,qBAAM,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,EAAA;;wBAAtC,KA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D;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,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;AAPW,QAAA,aAAa,iBAOxB","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 return value === testValue;\n } catch {\n /* istanbul ignore next */\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 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 +1 @@
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;AAsBtF,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;IAyB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAmBxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IA6DhD,GAAG,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,KAAG,MAAM,GAAG,SAE1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,SAAS,WAAW,MAAM,GAAG,SAAS,KAAG,OAOxF,CAAC"}
@@ -9,12 +9,13 @@ var CookieStorage = /** @class */ (function () {
9
9
  }
10
10
  CookieStorage.prototype.isEnabled = function () {
11
11
  return __awaiter(this, void 0, void 0, function () {
12
- var testValue, testCookieOptions, testStorage, testKey, value, _a;
12
+ var globalScope, testValue, testCookieOptions, testStorage, testKey, value, _a;
13
13
  return __generator(this, function (_b) {
14
14
  switch (_b.label) {
15
15
  case 0:
16
+ globalScope = getGlobalScope();
16
17
  /* istanbul ignore if */
17
- if (!getGlobalScope()) {
18
+ if (!globalScope || !globalScope.document) {
18
19
  return [2 /*return*/, false];
19
20
  }
20
21
  testValue = String(Date.now());
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/storage/cookie.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAoBrC;IAIE,uBAAY,OAA8B,EAAE,MAAgC;QAAhC,uBAAA,EAAA,WAAgC;QAC1E,IAAI,CAAC,OAAO,gBAAQ,OAAO,CAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACE,wBAAwB;wBACxB,IAAI,CAAC,cAAc,EAAE,EAAE;4BACrB,sBAAO,KAAK,EAAC;yBACd;wBAEK,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;wBAC/B,iBAAiB,yBAClB,IAAI,CAAC,OAAO,KACf,cAAc,EAAE,KAAK,GACtB,CAAC;wBACI,WAAW,GAAG,IAAI,aAAa,CAAS,iBAAiB,CAAC,CAAC;wBAC3D,OAAO,GAAG,mBAAY,IAAI,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H,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;AACH,MAAM,CAAC,IAAM,iBAAiB,GAAG,UAAC,KAAa;;IAC7C,OAAO,MAAA,sBAAsB,CAAC,KAAK,CAAC,mCAAI,kCAAkC,CAAC,KAAK,CAAC,CAAC;AACpF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,IAAM,aAAa,GAAG,UAAC,OAA2B,EAAE,OAA2B;IACpF,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","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 /* istanbul ignore if */\n if (!getGlobalScope()) {\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 return value === testValue;\n } catch {\n /* istanbul ignore next */\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 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,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAoBrC;IAIE,uBAAY,OAA8B,EAAE,MAAgC;QAAhC,uBAAA,EAAA,WAAgC;QAC1E,IAAI,CAAC,OAAO,gBAAQ,OAAO,CAAE,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEK,iCAAS,GAAf;;;;;;wBACQ,WAAW,GAAG,cAAc,EAAE,CAAC;wBACrC,wBAAwB;wBACxB,IAAI,CAAC,WAAW,IAAI,CAA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CAAC","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 return value === testValue;\n } catch {\n /* istanbul ignore next */\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 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amplitude/analytics-core",
3
- "version": "2.40.1",
3
+ "version": "2.40.2",
4
4
  "description": "",
5
5
  "author": "Amplitude Inc",
6
6
  "homepage": "https://github.com/amplitude/Amplitude-TypeScript",