diff options
| author | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
|---|---|---|
| committer | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
| commit | bce557cc2dc767628bed6aac87301a1be7c5431b (patch) | |
| tree | b51a051228d01fe3306cd7626d4a96768aadb944 /shared | |
init commit
Diffstat (limited to 'shared')
312 files changed, 55320 insertions, 0 deletions
diff --git a/shared/apps-common/src/jet/dependencies/host.ts b/shared/apps-common/src/jet/dependencies/host.ts new file mode 100644 index 0000000..85a03f0 --- /dev/null +++ b/shared/apps-common/src/jet/dependencies/host.ts @@ -0,0 +1,57 @@ +import type { + ClientIdentifier, + Host as NativeHost, + ProcessPlatform, +} from '@jet/environment'; +import type {} from '@jet/engine'; // For ClientIdentifier.Unknown + +export class Host implements NativeHost { + platform: ProcessPlatform = 'web'; + + get osBuild(): never { + throw makeWebDoesNotImplementException('osBuild'); + } + + get deviceModel(): string { + return 'web'; + } + + get devicePhysicalModel(): never { + throw makeWebDoesNotImplementException('devicePhysicalModel'); + } + + get deviceLocalizedModel() { + return ''; + } + + get deviceModelFamily(): never { + throw makeWebDoesNotImplementException('deviceModelFamily'); + } + + get clientIdentifier(): ClientIdentifier { + // We can't directly use the `ClientIdentifier.Unknown` enum member value + // because we cannot access "ambient const enums" with our TypeScript config. + // Enum handling is known to be tough in TypeScript and, for reasons like + // this, they are generally avoided. + // This returns a value defined on this enum by `@jet/engine`'s type definition + return 'unknown' as ClientIdentifier.Unknown; + } + + get clientVersion(): never { + throw makeWebDoesNotImplementException('clientVersion'); + } + + isOSAtLeast( + _majorVersion: number, + _minorVersion: number, + _patchVersion: number, + ): boolean { + return true; + } +} + +export function makeWebDoesNotImplementException(property: keyof NativeHost) { + return new Error( + `\`Host\` property \`${property}\` is not implemented for the "web" platform`, + ); +} diff --git a/shared/apps-common/src/jet/dependencies/random.ts b/shared/apps-common/src/jet/dependencies/random.ts new file mode 100644 index 0000000..d976879 --- /dev/null +++ b/shared/apps-common/src/jet/dependencies/random.ts @@ -0,0 +1,18 @@ +import type { Random as IRandom } from '@jet/environment'; +import { generateUuid } from '@amp/web-apps-utils'; + +export class Random implements IRandom { + nextBoolean(): boolean { + // See: https://stashweb.sd.apple.com/projects/AS/repos/jet-infrastructure/browse/Frameworks/JetEngine/JetEngine/JavaScript/Stack/Native%20APIs/JSRandomObject.swift?at=e90a88fa061f5cb6b9536d29a7ffd67e5db942db#41 + return Math.random() < 0.5; + } + + nextNumber(): number { + // See: https://stashweb.sd.apple.com/projects/AS/repos/jet-infrastructure/browse/Frameworks/JetEngine/JetEngine/JavaScript/Stack/Native%20APIs/JSRandomObject.swift?at=e90a88fa061f5cb6b9536d29a7ffd67e5db942db#45 + return Math.random(); + } + + nextUUID(): string { + return generateUuid(); + } +} diff --git a/shared/apps-common/src/jet/prefetched-intents/get-prefetched-intents.ts b/shared/apps-common/src/jet/prefetched-intents/get-prefetched-intents.ts new file mode 100644 index 0000000..4d59186 --- /dev/null +++ b/shared/apps-common/src/jet/prefetched-intents/get-prefetched-intents.ts @@ -0,0 +1,58 @@ +import { getCookie } from '@amp/web-app-components/src/utils/cookie'; +import type { LoggerFactory } from '@amp/web-apps-logger'; +import { isSome } from '@amp/web-apps-utils'; +import { deserializeServerData, stableStringify } from './server-data'; +import { type PrefetchedIntent, isPrefetchedIntents } from './types'; + +export function getPrefetchedIntents( + loggerFactory: LoggerFactory, + options?: { evenIfSignedIn?: boolean; featureKitItfe?: string }, +): Map<string, unknown> { + const logger = loggerFactory.loggerFor('getPrefetchedIntents'); + const evenIfSignedIn = options?.evenIfSignedIn; + const itfe = options?.featureKitItfe; + + const data = deserializeServerData(); + if (!data || !isPrefetchedIntents(data)) { + return new Map(); + } + + // We avoid prefetched intents in two scenarios: + // + // Condition 1: User is signed in (and evenIfSignedIn is false) + // It's possible/likely that dispatching an intent when signed in behaves + // differently. + // + // Condition 2: ITFE is enabled in Feature Kit + // When ITFE is active, we discard prefetched intents so that media API + // calls are triggered in the browser, allowing Feature Kit to inject ITFE + // into those calls. + if ((!evenIfSignedIn && getCookie('media-user-token')) || itfe) { + logger.info( + 'Discarding prefetched intents - signed in user or ITFE enabled', + ); + return new Map(); + } + + logger.debug('received prefetched intents from the server:', data); + return new Map( + data + .map( + ({ + intent, + data, + }: PrefetchedIntent): [string, unknown] | null => { + try { + if (intent.$kind.includes('Library')) { + return null; + } + // NOTE: PrefetchedIntents.get depends on stableStringify + return [stableStringify(intent), data]; + } catch (e) { + return null; + } + }, + ) + .filter(isSome), + ); +} diff --git a/shared/apps-common/src/jet/prefetched-intents/index.ts b/shared/apps-common/src/jet/prefetched-intents/index.ts new file mode 100644 index 0000000..dd5d393 --- /dev/null +++ b/shared/apps-common/src/jet/prefetched-intents/index.ts @@ -0,0 +1,118 @@ +import type { LoggerFactory } from '@amp/web-apps-logger'; +import type { Intent, IntentReturnType } from '@jet/environment/dispatching'; +import { serializeServerData, stableStringify } from './server-data'; +import type { PrefetchedIntent } from './types'; +import { getPrefetchedIntents } from './get-prefetched-intents'; + +export type { PrefetchedIntent } from './types'; + +export function serializePrefetchedIntents( + loggerFactory: LoggerFactory, + prefetchedIntents: PrefetchedIntent[], +): string { + const serialized = serializeServerData( + prefetchedIntents.map(removeSeoData), + ); + + if (serialized.length === 0) { + const logger = loggerFactory.loggerFor('serializePrefetchedIntents'); + logger.warn('failed to serialize prefetched intents'); + } + + return serialized; +} + +// SEO data is never needed for the first clientside render since the server +// already adds SEO tags. The seoData convention is ubiquitous across the apps. +// See: rdar://144581413 (Etag constantly changes on pages with songs due to seoData.ogSongs) +function removeSeoData(intent: PrefetchedIntent): PrefetchedIntent { + const { data } = intent; + + // We very intentionally return the original intent to prevent + // needlessly allocating new objects. + + if (data === null || typeof data !== 'object' || !('seoData' in data)) { + return intent; + } + + const { seoData } = data; + if (seoData === null || typeof seoData !== 'object') { + return intent; + } + + let partialSeoData: + | { pageTitle?: unknown; titleHeader?: unknown } + | undefined = undefined; + if ('pageTitle' in seoData || 'titleHeader' in seoData) { + partialSeoData = {}; + + if ('pageTitle' in seoData) { + partialSeoData['pageTitle'] = seoData.pageTitle; + } + + if ('titleHeader' in seoData) { + partialSeoData['titleHeader'] = seoData.titleHeader; + } + } + + // Only if we're actually going to do the removal do we spread + return { + ...intent, + data: { + ...data, + // Page title is desirable to keep as it is occasionally consulted + // outside of MetaTags.svelte + seoData: partialSeoData, + }, + }; +} + +export class PrefetchedIntents { + static empty(): PrefetchedIntents { + return new PrefetchedIntents(new Map()); + } + + static fromDom( + loggerFactory: LoggerFactory, + options?: { evenIfSignedIn?: boolean; featureKitItfe?: string }, + ): PrefetchedIntents { + return new PrefetchedIntents( + getPrefetchedIntents(loggerFactory, options), + ); + } + + private intents: Map<string, unknown>; + + private constructor(intents: Map<string, unknown>) { + this.intents = intents; + } + + get<I extends Intent<unknown>>(intent: I): IntentReturnType<I> | undefined { + if (this.intents.size === 0) { + return; + } + + let subject: string | void; + try { + subject = stableStringify(intent); + } catch (e) { + // It's possible the intents don't stringify. If that's that case, + // then we won't find it in this.intents, since the keys of that + // are successfully stringified intents. We could try something + // sophisticated here, but it's probably not worth it as most + // intents will serialize. + return; + } + + const data = this.intents.get(subject); + + // Remove the prefetched data so that it can only be used once + this.intents.delete(subject); + + // NOTE: There really isn't a good way to be safe with types here. We + // don't have a type guard for arbitrary IntentReturnType<I>. We just + // have to trust that the serialized data is of the correct type. This + // isn't unreasonable since we control serialization. + return data as unknown as IntentReturnType<I> | undefined; + } +} diff --git a/shared/apps-common/src/jet/prefetched-intents/server-data.ts b/shared/apps-common/src/jet/prefetched-intents/server-data.ts new file mode 100644 index 0000000..fba215c --- /dev/null +++ b/shared/apps-common/src/jet/prefetched-intents/server-data.ts @@ -0,0 +1,109 @@ +import { isPOJO } from '@amp/web-apps-utils'; + +// NOTE: be careful with imports here. This file is imported by browser code, +// so we expect tree shaking to only keep these functions. + +const SERVER_DATA_ID = 'serialized-server-data'; + +// Take care with < (which has special meaning inside script tags) +// See: https://github.com/sveltejs/kit/blob/ff9a27b4/packages/kit/src/runtime/server/page/serialize_data.js#L4-L28 +const replacements = { + '<': '\\u003C', + '\u2028': '\\u2028', + '\u2029': '\\u2029', +}; + +const pattern = new RegExp(`[${Object.keys(replacements).join('')}]`, 'g'); + +/** + * Serializes a POJO into a HTML <script> tag that can be read clientside by + * `deserializeServerData`. + * + * Use this to share data between serverside and clientside. Include the + * returned HTML in the response to a client to allow it to read this data. + * + * @param data data to serialize + * @returns serialized data (or empty string if serialization fails) + */ +export function serializeServerData(data: object): string { + try { + const sanitizedData = JSON.stringify(data).replace( + pattern, + (match) => replacements[match as keyof typeof replacements], + ); + return `<script type="application/json" id="${SERVER_DATA_ID}">${sanitizedData}</script>`; + } catch (e) { + // Don't let recursive data (or other non-serializable things) throw. + // We'd rather just let the serialize no-op to avoid breaking consumers. + return ''; + } +} + +/** + * Deserializes data serialized on the server by `serializeServerData`. + * + * @returns deserialized data (or undefined if it doesn't exist/errors) + */ +export function deserializeServerData(): ReturnType<JSON['parse']> | undefined { + const script = document.getElementById(SERVER_DATA_ID); + if (!script) { + return; + } + + script.parentNode?.removeChild(script); + + try { + return JSON.parse(script.textContent || ''); + } catch (e) { + // If the content is malformed, we want to avoid throwing. This + // situation should be impossible since we control the serialization + // above. + return; + } +} + +/** + * JSON stringify a POJO value in a stable manner. Specifically, this means that + * objects which are structurally equal serialize to the same string. + * + * This is useful when comparing objects serialized by a server against objects + * build in browser. With plain JSON.stringify(), property order matters and is + * not guaranteed to be the same. In other words these two objects would + * JSON.stringify() differently: + * + * { a: 1, b: 2 } + * { b: 2, a: 1 } + * + * But these are structurally equal--they have the same keys and values. + * + * The expected use case for this function is generating keys for a Map for + * objects from a server that will be compared against objects from the browser. + * This function should be used on objects returned from `deserializeServerData` + * before they are used in such contexts. + * + * See: https://stackoverflow.com/a/43049877 + */ +export function stableStringify(data: unknown): string { + if (Array.isArray(data)) { + const items = data.map(stableStringify).join(','); + return `[${items}]`; + } + + // Sort object keys before serializing + if (isPOJO(data)) { + const keys = [...Object.keys(data)]; + keys.sort(); + + const properties = keys + // undefined values should not get included in stringification + .filter((key) => typeof data[key] !== 'undefined') + .map( + (key) => `${JSON.stringify(key)}:${stableStringify(data[key])}`, + ) + .join(','); + + return `{${properties}}`; + } + + return JSON.stringify(data); +} diff --git a/shared/apps-common/src/jet/prefetched-intents/types.ts b/shared/apps-common/src/jet/prefetched-intents/types.ts new file mode 100644 index 0000000..b44a14b --- /dev/null +++ b/shared/apps-common/src/jet/prefetched-intents/types.ts @@ -0,0 +1,27 @@ +import type { Intent } from '@jet/environment/dispatching'; + +export interface PrefetchedIntent { + intent: Intent<unknown>; + data: unknown; +} + +export function isPrefetchedIntents(v: unknown): v is PrefetchedIntent[] { + return Array.isArray(v) && v.every(isPrefetchedIntent); +} + +function isPrefetchedIntent(v: unknown): v is PrefetchedIntent { + return hasIntentAndData(v) && isIntent(v.intent); +} + +function hasIntentAndData(v: unknown): v is HasIntentAndData { + return v !== null && typeof v === 'object' && 'intent' in v && 'data' in v; +} + +interface HasIntentAndData { + intent: unknown; + data: unknown; +} + +function isIntent(v: unknown): v is Intent<unknown> { + return v !== null && typeof v === 'object' && '$kind' in v; +} diff --git a/shared/components/assets/icons/arrow.svg b/shared/components/assets/icons/arrow.svg new file mode 100644 index 0000000..99e4e93 --- /dev/null +++ b/shared/components/assets/icons/arrow.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20height='16'%20width='16'%20viewBox='0%200%2016%2016'%3e%3cpath%20d='M1.559%2016L13.795%203.764v8.962H16V0H3.274v2.205h8.962L0%2014.441%201.559%2016z'/%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/assets/icons/chevron.svg b/shared/components/assets/icons/chevron.svg new file mode 100644 index 0000000..4accf4b --- /dev/null +++ b/shared/components/assets/icons/chevron.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20stroke-linejoin='round'%20viewBox='0%200%2036%2064'%20width='36'%20height='64'%3e%3cpath%20d='m3.344%2064c.957%200%201.768-.368%202.394-.994l29.2-28.538c.701-.7%201.069-1.547%201.069-2.468%200-.957-.368-1.841-1.068-2.467l-29.165-28.502c-.662-.661-1.473-1.03-2.43-1.03-1.914-.001-3.35%201.471-3.35%203.386%200%20.884.367%201.767.956%202.393l26.808%2026.22-26.808%2026.218a3.5%203.5%200%200%200%20-.956%202.395c0%201.914%201.435%203.387%203.35%203.387z'/%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/assets/icons/close.svg b/shared/components/assets/icons/close.svg new file mode 100644 index 0000000..33ceaf8 --- /dev/null +++ b/shared/components/assets/icons/close.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20width='18px'%20height='18px'%20version='1.1'%20viewBox='0%200%2018%2018'%20aria-hidden='true'%3e%3cpath%20d='M1.2%2018C.6%2018%200%2017.5%200%2016.8c0-.4.1-.6.4-.8l7-7-7-7c-.3-.2-.4-.5-.4-.8C0%20.5.6%200%201.2%200c.3%200%20.6.1.8.3l7%207%207-7c.2-.2.5-.3.8-.3.6%200%201.2.5%201.2%201.2%200%20.3-.1.6-.4.8l-7%207%207%207c.2.2.4.5.4.8%200%20.7-.6%201.2-1.2%201.2-.3%200-.6-.1-.8-.3l-7-7-7%207c-.2.1-.5.3-.8.3z'%3e%3c/path%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/assets/icons/search.svg b/shared/components/assets/icons/search.svg new file mode 100644 index 0000000..51acbf1 --- /dev/null +++ b/shared/components/assets/icons/search.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20height='16'%20width='16'%20viewBox='0%200%2016%2016'%3e%3cpath%20d='M11.87%2010.835c.018.015.035.03.051.047l3.864%203.863a.735.735%200%201%201-1.04%201.04l-3.863-3.864a.744.744%200%200%201-.047-.051%206.667%206.667%200%201%201%201.035-1.035zM6.667%2012a5.333%205.333%200%201%200%200-10.667%205.333%205.333%200%200%200%200%2010.667z'/%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/assets/icons/star-filled.svg b/shared/components/assets/icons/star-filled.svg new file mode 100644 index 0000000..30ce915 --- /dev/null +++ b/shared/components/assets/icons/star-filled.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20class='icon'%20viewBox='0%200%2064%2064'%3e%3cpath%20d='M11.5587783,56.6753946%20C12.6607967,57.5354863%2014.0584114,57.239835%2015.7248701,56.0303671%20L29.9432738,45.5748551%20L44.1885399,56.0303671%20C45.8549435,57.239835%2047.2256958,57.5354863%2048.3545766,56.6753946%20C49.4565949,55.8422203%2049.6985766,54.4714239%2049.0265215,52.5093414%20L43.4090353,35.7913212%20L57.7616957,25.4702202%20C59.4284847,24.2875597%2060.1000443,23.0511744%2059.6701361,21.7072844%20C59.2402278,20.4171743%2057.9769251,19.7720918%2055.9072003,19.7989542%20L38.3022646,19.9065138%20L32.9535674,3.10783487%20C32.3084848,1.11886239%2031.3408885,0.12440367%2029.9432738,0.12440367%20C28.5724665,0.12440367%2027.6048701,1.11886239%2026.9597875,3.10783487%20L21.6110903,19.9065138%20L4.00609944,19.7989542%20C1.93648476,19.7720918%200.673237047,20.4171743%200.243218696,21.7072844%20C-0.213717085,23.0511744%200.485090256,24.2875597%202.15154898,25.4702202%20L16.5043196,35.7913212%20L10.8868334,52.5093414%20C10.2148884,54.4714239%2010.456815,55.8422203%2011.5587783,56.6753946%20Z'%20transform='translate(2%203.376)'%3e%3c/path%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/assets/icons/star-hollow.svg b/shared/components/assets/icons/star-hollow.svg new file mode 100644 index 0000000..a359cef --- /dev/null +++ b/shared/components/assets/icons/star-hollow.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20class='icon'%20viewBox='0%200%2064%2064'%3e%3cpath%20d='M11.5587783,56.6753946%20C12.6607967,57.5354863%2014.0584114,57.239835%2015.7248701,56.0303671%20L29.9432738,45.5748551%20L44.1885399,56.0303671%20C45.8549435,57.239835%2047.2256958,57.5354863%2048.3545766,56.6753946%20C49.4565949,55.8422203%2049.6985766,54.4714239%2049.0265215,52.5093414%20L43.4090353,35.7913212%20L57.7616957,25.4702202%20C59.4284847,24.2875597%2060.1000443,23.0511744%2059.6701361,21.7072844%20C59.2402278,20.4171743%2057.9769251,19.7720918%2055.9072003,19.7989542%20L38.3022646,19.9065138%20L32.9535674,3.10783487%20C32.3084848,1.11886239%2031.3408885,0.12440367%2029.9432738,0.12440367%20C28.5724665,0.12440367%2027.6048701,1.11886239%2026.9597875,3.10783487%20L21.6110903,19.9065138%20L4.00609944,19.7989542%20C1.93648476,19.7720918%200.673237047,20.4171743%200.243218696,21.7072844%20C-0.213717085,23.0511744%200.485090256,24.2875597%202.15154898,25.4702202%20L16.5043196,35.7913212%20L10.8868334,52.5093414%20C10.2148884,54.4714239%2010.456815,55.8422203%2011.5587783,56.6753946%20Z%20M15.4292187,51.3535927%20C15.3754389,51.2998405%2015.4023013,51.2729616%2015.4292187,51.1116937%20L20.777916,35.7375413%20C21.1542096,34.6893028%2020.9391453,33.8561285%2019.9984664,33.2110459%20L6.61323706,23.9650459%20C6.47887008,23.8844037%206.4520077,23.8306789%206.47887008,23.7500367%20C6.50573247,23.6693945%206.55951229,23.6693945%206.72079669,23.6693945%20L22.9818976,23.9650459%20C24.0838609,23.9919083%2024.7827233,23.5350276%2025.1320995,22.4330092%20L29.8088518,6.87071561%20C29.8357142,6.7094312%2029.889494,6.65570643%2029.9432738,6.65570643%20C30.0238609,6.65570643%2030.0776408,6.7094312%2030.1045032,6.87071561%20L34.7812555,22.4330092%20C35.1306866,23.5350276%2035.829494,23.9919083%2036.9315123,23.9650459%20L53.1923381,23.6693945%20C53.3536225,23.6693945%2053.4075674,23.6693945%2053.4345399,23.7500367%20C53.4615124,23.8306789%2053.4075674,23.8844037%2053.300228,23.9650459%20L39.9149435,33.2110459%20C38.9742096,33.8561285%2038.7592004,34.6893028%2039.135494,35.7375413%20L44.4841912,51.1116937%20C44.5110536,51.2729616%2044.537916,51.2998405%2044.4841912,51.3535927%20C44.4304114,51.4342294%2044.3497692,51.3804716%2044.2422646,51.2998405%20L31.3140261,41.4356698%20C30.4539343,40.7637248%2029.4594206,40.7637248%2028.5993839,41.4356698%20L15.6710903,51.2998405%20C15.5635857,51.3804716%2015.4829435,51.4342294%2015.4292187,51.3535927%20Z'%20transform='translate(2%203.376)'%3e%3c/path%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/assets/shelf/chevron-compact-left.svg b/shared/components/assets/shelf/chevron-compact-left.svg new file mode 100644 index 0000000..bef9ce1 --- /dev/null +++ b/shared/components/assets/shelf/chevron-compact-left.svg @@ -0,0 +1 @@ +export default "data:image/svg+xml,%3csvg%20viewBox='0%200%209%2031'%20xmlns='http://www.w3.org/2000/svg'%3e%3cpath%20d='M27.49%2075.5a4.59%204.59%200%200%200%204.15%203.07c2.9%200%205.05-2.1%205.05-4.95%200-1.5-.79-3.38-1.28-4.62L22.07%2035.05%2035.4%201.12c.49-1.26%201.28-3.18%201.28-4.63a4.85%204.85%200%200%200-5.05-4.95%204.57%204.57%200%200%200-4.15%203.11l-13.1%2033.29c-.86%202.21-1.93%204.97-1.93%207.11%200%202.15%201.07%204.86%201.93%207.12l13.1%2033.33Z'%20transform='matrix(.35086%200%200%20.35086%20-4.37%202.97)'/%3e%3c/svg%3e"
\ No newline at end of file diff --git a/shared/components/config/components/artwork.ts b/shared/components/config/components/artwork.ts new file mode 100644 index 0000000..daca473 --- /dev/null +++ b/shared/components/config/components/artwork.ts @@ -0,0 +1,103 @@ +// default params used by artwork component. +import type { Profile } from '@amp/web-app-components/src/components/Artwork/types'; +import type { Breakpoints } from '@amp/web-app-components/src/types'; +import { ASPECT_RATIOS } from '@amp/web-app-components/src/components/Artwork/constants'; + +export type ArtworkProfileMap<ProfileName extends string = string> = Map< + ProfileName, + Profile +>; +export interface ArtworkConfigOptions { + BREAKPOINTS?: Breakpoints; + PROFILES?: ArtworkProfileMap; +} + +interface ArtworkConfig { + get: () => ArtworkConfigOptions; + set: (obj: ArtworkConfigOptions) => void; +} + +function artworkConfig(): ArtworkConfig { + const { + HD, + ONE, + HERO, + THREE_QUARTERS, + SUPER_HERO_WIDE, + UBER, + ONE_THIRD, + HD_ASPECT_RATIO, + EDITORIAL_DEFAULT, + } = ASPECT_RATIOS; + let config: ArtworkConfigOptions = { + BREAKPOINTS: { + xsmall: { + max: 739, + }, + small: { + min: 740, + max: 999, + }, + medium: { + min: 1000, + max: 1319, + }, + large: { + min: 1320, + max: 1679, + }, + xlarge: { + min: 1680, + }, + }, + PROFILES: new Map([ + ['brick', [[340, 340, 290, 290], HD, 'sr']], + ['brick-sporting-event', [[340, 340, 290, 290], HD, 'sh']], + ['product', [[500, 500, 300, 270], ONE, 'bb']], + ['episode', [[330, 330, 305, 295], HD, 'sr']], + [ + 'editorial-card', + [[530, 530, 480, 300, 300], EDITORIAL_DEFAULT, 'fa'], + ], + ['editorial-card-cover-artwork', [[60], ONE, 'cc']], + ['editorial-card-video-art', [[88], HD_ASPECT_RATIO, 'mv']], + ['hero', [[530, 530, 600, 450], HERO, 'sr']], + ['superHeroLockup', [[330, 330, 305, 295], THREE_QUARTERS, 'bb']], + ['superHeroTall', [[600, 600, 450], THREE_QUARTERS, 'sr']], + [ + 'superHeroWide', + [[1200, 1200, 900, 600, 450], SUPER_HERO_WIDE, 'sr'], + ], + ['uber', [[1200], UBER, 'bb']], + ['episode-lockup', [[316, 316, 296, 296], ONE, 'cc']], + ['upsell-artwork', [[94], ONE, 'cc']], + ['upsell-wordmark', [[140], 140 / 14, 'bb']], + ['ellipse-lockup', [[243, 243, 220, 190, 160], ONE, 'cc']], + ['standard', [[243, 243, 220, 190, 160], ONE, 'bb']], + ['powerswoosh', [[300], ONE, 'cc']], + ['powerswooshTall', [[600, 450], THREE_QUARTERS, 'sr']], + ['category-brick', [[1040, 1040, 1040, 680], ONE_THIRD, 'sr']], + ['info-fullscreen', [[600, 600, 450], ONE, 'bb']], + ['track-list', [[40], ONE, 'bb']], + ]), + }; + + const setConfig = (obj: ArtworkConfigOptions) => { + config = { + PROFILES: new Map([...config.PROFILES, ...obj.PROFILES]), + BREAKPOINTS: { + ...config.BREAKPOINTS, + ...(obj?.BREAKPOINTS ?? {}), + }, + }; + }; + + const getConfig = (): ArtworkConfigOptions => config; + + return { + get: getConfig, + set: setConfig, + }; +} + +export const ArtworkConfig = artworkConfig(); diff --git a/shared/components/config/components/shelf.ts b/shared/components/config/components/shelf.ts new file mode 100644 index 0000000..1146e3d --- /dev/null +++ b/shared/components/config/components/shelf.ts @@ -0,0 +1,116 @@ +/* eslint-disable object-curly-newline */ +import type { Size } from '@amp/web-app-components/src/types'; +import type { GridType } from '@amp/web-app-components/src/components/Shelf/types'; + +/** + * Used to customize the shared shelf + * + * @param GRID_MAX_CONTENT - Sets the max content size of the column for each viewport + * @param GRID_ROW_GAP - Sets the row gap for a shelf in each viewport + * @param GRID_COL_GAP - Sets the column gap for a shelf in each viewport + * @param GRID_VALUES - Sets the number of items to show in a column of the grid for each viewport + * + * @example + * const ShelvesConfig = { + * GRID_MAX_CONTENT: { + * FooShelf: { xsmall: '298px' }, + * }, + * GRID_COL_GAP: { + * FooShelf: { xsmall: '10px', small:'20px', medium:'20px', large:'20px', xlarge: '30px' } + * }, + * GRID_ROW_GAP: { + * FooShelf: { xsmall: '10px', small:'20px', medium:'20px', large:'20px', xlarge: '30px' } + * }, + * GRID_VALUES: { + * FooShelf: { xsmall: 1, small: 3, medium: 5, large: 6, xlarge: 10 } + * } + * } + */ +export interface ShelfConfigOptions { + /** + * Sets the max size of the column for each viewport + * (NOTE: these values will override GRID_VALUES) + */ + GRID_MAX_CONTENT: { + [key in GridType]: { [value in Size]?: string }; + }; + /** + * Sets the row gap for a shelf in each viewport + * - Default for all shelves is { xsmall: '24px', small: '24px', medium: '24px', large: '24px', xlarge: '24px' } + */ + GRID_ROW_GAP: { + [key in GridType]?: { [value in Size]?: number | null }; + }; + /** + * Sets the column gap for a shelf in each viewport + * - Default for all shelves is { xsmall: '10px', small: '20px', medium: '20px', large: '20px', xlarge: '20px' } + */ + GRID_COL_GAP: { + [key in GridType]?: { [value in Size]?: string | null }; + }; + /** + * Sets the number of columns in the grid for each viewport + * (NOTE: this value will be overridden by values in GRID_MAX_CONTENT) + */ + GRID_VALUES: { + [key in GridType]: { [value in Size]: number | null }; + }; +} + +// Grid values correspond with dynamic-grids.scss +function ShelfConfigInit() { + let config: ShelfConfigOptions = { + GRID_MAX_CONTENT: { + A: { xsmall: '298px' }, + B: { xsmall: '298px' }, + C: { xsmall: '200px' }, + D: { xsmall: '144px' }, + E: { xsmall: '144px' }, + F: { xsmall: '270px' }, + G: { xsmall: '144px' }, + H: { xsmall: '94px' }, + I: { xsmall: '144px' }, + EllipseA: {}, + Spotlight: {}, + Single: {}, + '1-1-2-3': {}, + '2-2-3-4': { xsmall: '270px' }, + '1-2-2-2': {}, + }, + GRID_COL_GAP: {}, + GRID_ROW_GAP: { + None: { xsmall: 0, small: 0, medium: 0, large: 0, xlarge: 0 }, + '1-2-2-2': { xsmall: 0, small: 0, medium: 0, large: 0, xlarge: 0 }, + }, + GRID_VALUES: { + A: { xsmall: null, small: 2, medium: 2, large: 3, xlarge: 3 }, + B: { xsmall: null, small: 2, medium: 3, large: 4, xlarge: 4 }, + C: { xsmall: null, small: 3, medium: 4, large: 5, xlarge: 5 }, + D: { xsmall: null, small: 4, medium: 5, large: 8, xlarge: 8 }, + E: { xsmall: null, small: 5, medium: 9, large: 10, xlarge: 10 }, + F: { xsmall: null, small: 2, medium: 3, large: 3, xlarge: 3 }, + G: { xsmall: null, small: 4, medium: 5, large: 6, xlarge: 6 }, + H: { xsmall: null, small: 6, medium: 8, large: 10, xlarge: 10 }, + I: { xsmall: null, small: 5, medium: 6, large: 8, xlarge: 8 }, + Single: { xsmall: 1, small: 1, medium: 1, large: 1, xlarge: 1 }, + EllipseA: { xsmall: 2, small: 4, medium: 6, large: 6, xlarge: 6 }, + Spotlight: { xsmall: 1, small: 1, medium: 1, large: 1, xlarge: 1 }, + '1-1-2-3': { xsmall: 1, small: 1, medium: 2, large: 3, xlarge: 3 }, + '2-2-3-4': { xsmall: 2, small: 2, medium: 3, large: 4, xlarge: 4 }, + '1-2-2-2': { xsmall: 1, small: 2, medium: 2, large: 2, xlarge: 2 }, + }, + }; + + const get = () => config; + + const set = (obj: ShelfConfigOptions) => { + config = { ...config, ...obj }; + }; + + return { + set, + get, + }; +} + +export const ShelfConfig = ShelfConfigInit(); diff --git a/shared/components/node_modules/intersection-observer-admin/dist/intersection-observer-admin.es5.js b/shared/components/node_modules/intersection-observer-admin/dist/intersection-observer-admin.es5.js new file mode 100644 index 0000000..c6051f1 --- /dev/null +++ b/shared/components/node_modules/intersection-observer-admin/dist/intersection-observer-admin.es5.js @@ -0,0 +1,428 @@ +var Registry = /** @class */ (function () { + function Registry() { + this.registry = new WeakMap(); + } + Registry.prototype.elementExists = function (elem) { + return this.registry.has(elem); + }; + Registry.prototype.getElement = function (elem) { + return this.registry.get(elem); + }; + /** + * administrator for lookup in the future + * + * @method add + * @param {HTMLElement | Window} element - the item to add to root element registry + * @param {IOption} options + * @param {IOption.root} [root] - contains optional root e.g. window, container div, etc + * @param {IOption.watcher} [observer] - optional + * @public + */ + Registry.prototype.addElement = function (element, options) { + if (!element) { + return; + } + this.registry.set(element, options || {}); + }; + /** + * @method remove + * @param {HTMLElement|Window} target + * @public + */ + Registry.prototype.removeElement = function (target) { + this.registry.delete(target); + }; + /** + * reset weak map + * + * @method destroy + * @public + */ + Registry.prototype.destroyRegistry = function () { + this.registry = new WeakMap(); + }; + return Registry; +}()); + +var noop = function () { }; +var CallbackType; +(function (CallbackType) { + CallbackType["enter"] = "enter"; + CallbackType["exit"] = "exit"; +})(CallbackType || (CallbackType = {})); +var Notifications = /** @class */ (function () { + function Notifications() { + this.registry = new Registry(); + } + /** + * Adds an EventListener as a callback for an event key. + * @param type 'enter' or 'exit' + * @param key The key of the event + * @param callback The callback function to invoke when the event occurs + */ + Notifications.prototype.addCallback = function (type, element, callback) { + var _a, _b; + var entry; + if (type === CallbackType.enter) { + entry = (_a = {}, _a[CallbackType.enter] = callback, _a); + } + else { + entry = (_b = {}, _b[CallbackType.exit] = callback, _b); + } + this.registry.addElement(element, Object.assign({}, this.registry.getElement(element), entry)); + }; + /** + * @hidden + * Executes registered callbacks for key. + * @param type + * @param element + * @param data + */ + Notifications.prototype.dispatchCallback = function (type, element, data) { + if (type === CallbackType.enter) { + var _a = this.registry.getElement(element).enter, enter = _a === void 0 ? noop : _a; + enter(data); + } + else { + // no element in WeakMap possible because element may be removed from DOM by the time we get here + var found = this.registry.getElement(element); + if (found && found.exit) { + found.exit(data); + } + } + }; + return Notifications; +}()); + +var __extends = (undefined && undefined.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __assign = (undefined && undefined.__assign) || function () { + __assign = Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +var IntersectionObserverAdmin = /** @class */ (function (_super) { + __extends(IntersectionObserverAdmin, _super); + function IntersectionObserverAdmin() { + var _this = _super.call(this) || this; + _this.elementRegistry = new Registry(); + return _this; + } + /** + * Adds element to observe via IntersectionObserver and stores element + relevant callbacks and observer options in static + * administrator for lookup in the future + * + * @method observe + * @param {HTMLElement | Window} element + * @param {Object} options + * @public + */ + IntersectionObserverAdmin.prototype.observe = function (element, options) { + if (options === void 0) { options = {}; } + if (!element) { + return; + } + this.elementRegistry.addElement(element, __assign({}, options)); + this.setupObserver(element, __assign({}, options)); + }; + /** + * Unobserve target element and remove element from static admin + * + * @method unobserve + * @param {HTMLElement|Window} target + * @param {Object} options + * @public + */ + IntersectionObserverAdmin.prototype.unobserve = function (target, options) { + var matchingRootEntry = this.findMatchingRootEntry(options); + if (matchingRootEntry) { + var intersectionObserver = matchingRootEntry.intersectionObserver; + intersectionObserver.unobserve(target); + } + }; + /** + * register event to handle when intersection observer detects enter + * + * @method addEnterCallback + * @public + */ + IntersectionObserverAdmin.prototype.addEnterCallback = function (element, callback) { + this.addCallback(CallbackType.enter, element, callback); + }; + /** + * register event to handle when intersection observer detects exit + * + * @method addExitCallback + * @public + */ + IntersectionObserverAdmin.prototype.addExitCallback = function (element, callback) { + this.addCallback(CallbackType.exit, element, callback); + }; + /** + * retrieve registered callback and call with data + * + * @method dispatchEnterCallback + * @public + */ + IntersectionObserverAdmin.prototype.dispatchEnterCallback = function (element, entry) { + this.dispatchCallback(CallbackType.enter, element, entry); + }; + /** + * retrieve registered callback and call with data on exit + * + * @method dispatchExitCallback + * @public + */ + IntersectionObserverAdmin.prototype.dispatchExitCallback = function (element, entry) { + this.dispatchCallback(CallbackType.exit, element, entry); + }; + /** + * cleanup data structures and unobserve elements + * + * @method destroy + * @public + */ + IntersectionObserverAdmin.prototype.destroy = function () { + this.elementRegistry.destroyRegistry(); + }; + /** + * use function composition to curry options + * + * @method setupOnIntersection + * @param {Object} options + */ + IntersectionObserverAdmin.prototype.setupOnIntersection = function (options) { + var _this = this; + return function (ioEntries) { + return _this.onIntersection(options, ioEntries); + }; + }; + IntersectionObserverAdmin.prototype.setupObserver = function (element, options) { + var _a; + var _b = options.root, root = _b === void 0 ? window : _b; + // First - find shared root element (window or target HTMLElement) + // this root is responsible for coordinating it's set of elements + var potentialRootMatch = this.findRootFromRegistry(root); + // Second - if there is a matching root, see if an existing entry with the same options + // regardless of sort order. This is a bit of work + var matchingEntryForRoot; + if (potentialRootMatch) { + matchingEntryForRoot = this.determineMatchingElements(options, potentialRootMatch); + } + // next add found entry to elements and call observer if applicable + if (matchingEntryForRoot) { + var elements = matchingEntryForRoot.elements, intersectionObserver = matchingEntryForRoot.intersectionObserver; + elements.push(element); + if (intersectionObserver) { + intersectionObserver.observe(element); + } + } + else { + // otherwise start observing this element if applicable + // watcher is an instance that has an observe method + var intersectionObserver = this.newObserver(element, options); + var observerEntry = { + elements: [element], + intersectionObserver: intersectionObserver, + options: options + }; + // and add entry to WeakMap under a root element + // with watcher so we can use it later on + var stringifiedOptions = this.stringifyOptions(options); + if (potentialRootMatch) { + // if share same root and need to add new entry to root match + // not functional but :shrug + potentialRootMatch[stringifiedOptions] = observerEntry; + } + else { + // no root exists, so add to WeakMap + this.elementRegistry.addElement(root, (_a = {}, + _a[stringifiedOptions] = observerEntry, + _a)); + } + } + }; + IntersectionObserverAdmin.prototype.newObserver = function (element, options) { + // No matching entry for root in static admin, thus create new IntersectionObserver instance + var root = options.root, rootMargin = options.rootMargin, threshold = options.threshold; + var newIO = new IntersectionObserver(this.setupOnIntersection(options).bind(this), { root: root, rootMargin: rootMargin, threshold: threshold }); + newIO.observe(element); + return newIO; + }; + /** + * IntersectionObserver callback when element is intersecting viewport + * either when `isIntersecting` changes or `intersectionRadio` crosses on of the + * configured `threshold`s. + * Exit callback occurs eagerly (when element is initially out of scope) + * See https://stackoverflow.com/questions/53214116/intersectionobserver-callback-firing-immediately-on-page-load/53385264#53385264 + * + * @method onIntersection + * @param {Object} options + * @param {Array} ioEntries + * @private + */ + IntersectionObserverAdmin.prototype.onIntersection = function (options, ioEntries) { + var _this = this; + ioEntries.forEach(function (entry) { + var isIntersecting = entry.isIntersecting, intersectionRatio = entry.intersectionRatio; + var threshold = options.threshold || 0; + if (Array.isArray(threshold)) { + threshold = threshold[threshold.length - 1]; + } + // then find entry's callback in static administration + var matchingRootEntry = _this.findMatchingRootEntry(options); + // first determine if entry intersecting + if (isIntersecting || intersectionRatio > threshold) { + if (matchingRootEntry) { + matchingRootEntry.elements.some(function (element) { + if (element && element === entry.target) { + _this.dispatchEnterCallback(element, entry); + return true; + } + return false; + }); + } + } + else { + if (matchingRootEntry) { + matchingRootEntry.elements.some(function (element) { + if (element && element === entry.target) { + _this.dispatchExitCallback(element, entry); + return true; + } + return false; + }); + } + } + }); + }; + /** + * { root: { stringifiedOptions: { observer, elements: []...] } } + * @method findRootFromRegistry + * @param {HTMLElement|Window} root + * @private + * @return {Object} of elements that share same root + */ + IntersectionObserverAdmin.prototype.findRootFromRegistry = function (root) { + if (this.elementRegistry) { + return this.elementRegistry.getElement(root); + } + }; + /** + * We don't care about options key order because we already added + * to the static administrator + * + * @method findMatchingRootEntry + * @param {Object} options + * @return {Object} entry with elements and other options + */ + IntersectionObserverAdmin.prototype.findMatchingRootEntry = function (options) { + var _a = options.root, root = _a === void 0 ? window : _a; + var matchingRoot = this.findRootFromRegistry(root); + if (matchingRoot) { + var stringifiedOptions = this.stringifyOptions(options); + return matchingRoot[stringifiedOptions]; + } + }; + /** + * Determine if existing elements for a given root based on passed in options + * regardless of sort order of keys + * + * @method determineMatchingElements + * @param {Object} options + * @param {Object} potentialRootMatch e.g. { stringifiedOptions: { elements: [], ... }, stringifiedOptions: { elements: [], ... }} + * @private + * @return {Object} containing array of elements and other meta + */ + IntersectionObserverAdmin.prototype.determineMatchingElements = function (options, potentialRootMatch) { + var _this = this; + var matchingStringifiedOptions = Object.keys(potentialRootMatch).filter(function (key) { + var comparableOptions = potentialRootMatch[key].options; + return _this.areOptionsSame(options, comparableOptions); + })[0]; + return potentialRootMatch[matchingStringifiedOptions]; + }; + /** + * recursive method to test primitive string, number, null, etc and complex + * object equality. + * + * @method areOptionsSame + * @param {any} a + * @param {any} b + * @private + * @return {boolean} + */ + IntersectionObserverAdmin.prototype.areOptionsSame = function (a, b) { + if (a === b) { + return true; + } + // simple comparison + var type1 = Object.prototype.toString.call(a); + var type2 = Object.prototype.toString.call(b); + if (type1 !== type2) { + return false; + } + else if (type1 !== '[object Object]' && type2 !== '[object Object]') { + return a === b; + } + if (a && b && typeof a === 'object' && typeof b === 'object') { + // complex comparison for only type of [object Object] + for (var key in a) { + if (Object.prototype.hasOwnProperty.call(a, key)) { + // recursion to check nested + if (this.areOptionsSame(a[key], b[key]) === false) { + return false; + } + } + } + } + // if nothing failed + return true; + }; + /** + * Stringify options for use as a key. + * Excludes options.root so that the resulting key is stable + * + * @param {Object} options + * @private + * @return {String} + */ + IntersectionObserverAdmin.prototype.stringifyOptions = function (options) { + var root = options.root; + var replacer = function (key, value) { + if (key === 'root' && root) { + var classList = Array.prototype.slice.call(root.classList); + var classToken = classList.reduce(function (acc, item) { + return (acc += item); + }, ''); + var id = root.id; + return "".concat(id, "-").concat(classToken); + } + return value; + }; + return JSON.stringify(options, replacer); + }; + return IntersectionObserverAdmin; +}(Notifications)); + +export default IntersectionObserverAdmin; +//# sourceMappingURL=intersection-observer-admin.es5.js.map diff --git a/shared/components/src/actions/allow-drag.ts b/shared/components/src/actions/allow-drag.ts new file mode 100644 index 0000000..7758979 --- /dev/null +++ b/shared/components/src/actions/allow-drag.ts @@ -0,0 +1,291 @@ +import type { ActionReturn } from 'svelte/action'; +import type { Readable } from 'svelte/store'; +import { writable } from 'svelte/store'; + +// Duplicate assignment from '~/components/DragImage.svelte' +const PRESET_CLASS = 'preset'; +const VISIBLE_CLASS = 'visible'; +const CONTAINER_CLASS = 'drag-image--container'; +const IMAGE_ATTR = 'data-drag-image-source'; +const BADGE_ATTR = 'data-drag-image-badge'; + +// resize fallback image when artwork is video or landscape +const ASPECT_RATIO_CLASS = 'aspect-landscape'; +const IS_DRAGGING_CLASS = 'is-dragging'; + +// Workaround for WebKit `effectAllowed` bug: https://bugs.webkit.org/show_bug.cgi?id=178058 +// This store points to the active drag handler, set on dragstart and unset on dragend. +// Only store subscription is exported to prevent modification outside this file. +const { set: setActiveDragHandler, subscribe } = + writable<DragHandler<any>>(null); +export const activeDragHandler: Readable<DragHandler<any>> = { subscribe }; + +/* + FOLLOW-UP WORK: + - it now adds and destroys the handler, and destroys and creates a new one on update. + We might want to keep track of any DragHandler that got created for an element and just update the existing instance. + rdar://98074771 (Onyx: DnD: Update allow-drag and allow-drop actions to support updates) + - Have the options dragEnabled be optional. If not passed in, it should be enabled. Just not when it's set to false. + We can't update that before the above changes are in. + - Use the logger instead of console.warn directly. + - Update DragImage clases and badge count from the DragImage component if possible +*/ + +/** + * Note: dragData needs to be JSON serializable, and no recursive structure + */ +export type DragOptions = { + dragEnabled: boolean; + dragData: unknown; // Needs to be JSON serializable. The DragData type is being set on initiating a new DragHandler<DragData> based on the passed in dragData + dragImage?: HTMLElement | string; + usePlainDragImage?: boolean; + isContainer?: boolean; + badgeCount?: number; + effectAllowed?: DataTransfer['effectAllowed']; +}; + +class DragHandler<DragData> { + private readonly element: HTMLElement; + private readonly options: DragOptions; + private readonly dragData: DragData; + private readonly dragImageContainer: HTMLElement; + private readonly fallbackImage: HTMLElement; + private dragImage: HTMLElement; + + constructor( + element: HTMLElement, + options: Omit<DragOptions, 'dragData'> & { dragData: DragData }, + ) { + this.element = element; + this.options = options; + this.dragData = options.dragData; + this.dragImageContainer = document.querySelector('[data-drag-image]'); + this.fallbackImage = document.querySelector('[data-fallback-image]'); + + if (!this.dragImageContainer) { + console.warn( + 'Use the <DragImage /> component to allow app specific drag images with fallback, badge and styling', + ); + } + + this.addEventListeners(); + this.setDraggable(); + } + + private setDraggable(): void { + this.element.draggable = true; + } + + private setDraggingClass = () => { + this.element.classList.add(IS_DRAGGING_CLASS); + }; + + private removeDraggingClass = () => { + this.element.classList.remove(IS_DRAGGING_CLASS); + }; + + private addEventListeners(): void { + // Create custom drag image before dragStart, because otherwise it might be empty + this.element.addEventListener('mousedown', this.createDragImage); + this.element.addEventListener('mouseup', this.resetDragImage); + + this.element.addEventListener('dragstart', this.onDragStart, { + capture: true, + }); + this.element.addEventListener('dragend', this.onDragEnd); + } + + public destroy(): void { + this.element.draggable = false; + this.element.style.setProperty('webkitUserDrag', 'auto'); + this.element.removeEventListener('mousedown', this.createDragImage); + this.element.removeEventListener('mouseup', this.resetDragImage); + this.element.removeEventListener('dragstart', this.onDragStart, { + capture: true, + }); + this.element.removeEventListener('dragend', this.onDragEnd); + } + + private onDragStart = (e: DragEvent): void => { + if (!this.dragData) { + // Interrupt the drag event as dragging should not be enabled on the element + e.preventDefault(); + return; + } + + // Prevent drag action on parent elements + e.stopPropagation(); + + if (this.dragImage) { + if (this.dragImage === this.dragImageContainer) { + // Make temporary visible to capture snapshot + this.dragImageContainer.classList.remove(PRESET_CLASS); + this.dragImageContainer.classList.add(VISIBLE_CLASS); + } + + const { clientWidth: imgWidth, clientHeight: imgHeight } = + this.dragImage; + e.dataTransfer.setDragImage( + this.dragImage, + imgWidth / 2, + imgHeight / 2, + ); + + // Remove the DOM drag image to not show up for the user. + // It needs a timeout to have it captured before it gets removed. + setTimeout(() => this.resetDragImage(), 1); + } + + e.dataTransfer.setData('text/plain', JSON.stringify(this.dragData)); + + // "Drop effect" controls what mouse cursor is shown during DnD operations + // See: https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/effectAllowed + e.dataTransfer.effectAllowed = this.getEffectAllowed(); + this.setDraggingClass(); + + setActiveDragHandler(this); + }; + + private onDragEnd = (): void => { + setActiveDragHandler(null); + this.resetDragImage(); + this.removeDraggingClass(); + }; + + private createDragImage = (): HTMLElement | null => { + this.resetDragImage(); + + const argsDragImage = this.options.dragImage; + let dragImage: HTMLElement; + + if (argsDragImage instanceof HTMLElement) { + dragImage = argsDragImage; + } else if (typeof argsDragImage === 'string') { + // Find the drag image based on the passed selector + dragImage = this.element.querySelector(argsDragImage); + } else { + // Use artwork by default + dragImage = this.element.querySelector( + '.artwork-component picture', + ); + } + + // Do not create a shallow copy inside our drag container with pre-set sizes. + // Can be used to either use the default browser behavior of using the element as drag image, + // or use another DOM element inside the draggable object without additional styling. + if (this.options.usePlainDragImage) { + // If no drag image set, use element (default browser drag behavior) + if (!argsDragImage) { + dragImage = this.element; + } + this.dragImage = dragImage; + return dragImage; + } + + // When no drag image container found (<DragImage /> component not rendered in the app), don't use a custom drag image + if (!this.dragImageContainer) return; + + // Container items should have a bigger drag image (albums, playlists) + if (this.options.isContainer) { + this.dragImageContainer.classList.add(CONTAINER_CLASS); + } + + // Clone image and add to drag image container + if (dragImage) { + const dragImageClone = dragImage.cloneNode(true); + this.dragImageContainer + .querySelector(`[${IMAGE_ATTR}]`) + .prepend(dragImageClone); + + // Prevents fallback image from overflowing video or landscaped artwork. + // In the Tracklist. See: .aspect-landscape class via DragImage.svelte + if (dragImage.offsetWidth / dragImage.offsetHeight !== 1) { + this.fallbackImage.classList.add(ASPECT_RATIO_CLASS); + } + } + + // Add a track count badge. Container items should always have track count, even if it's 1 (like a single-track-album). + if ( + this.badgeCount > 1 || + (this.options.isContainer && this.options.badgeCount > 0) + ) { + const badge = this.dragImageContainer.querySelector( + `[${BADGE_ATTR}]`, + ); + badge.classList.add(VISIBLE_CLASS); + badge.textContent = `${this.badgeCount}`; + } + + // Make visible for loading the image and capturing for drag image + this.dragImageContainer.classList.add(PRESET_CLASS); + this.dragImage = this.dragImageContainer; + }; + + /** + * DragImage is being set from the DragImage component: '@amp/web-app-components/src/components/DragImage.svelte'. + * We should find a better way of updating that rendered component instead of modifying the elements from here. + */ + private resetDragImage = (): void => { + this.dragImage = null; + const container = this.dragImageContainer; + container.classList.remove(PRESET_CLASS); + container.classList.remove(VISIBLE_CLASS); + container.classList.remove(CONTAINER_CLASS); + this.fallbackImage.classList.remove(ASPECT_RATIO_CLASS); + container.querySelector(`[${IMAGE_ATTR}]`).innerHTML = ''; + const badge = container.querySelector(`[${BADGE_ATTR}]`); + badge.classList.remove(VISIBLE_CLASS); + badge.innerHTML = ''; + }; + + private get badgeCount(): number { + return ( + this.options.badgeCount ?? + (Array.isArray(this.dragData) && this.dragData.length) + ); + } + + public getEffectAllowed(): DataTransfer['effectAllowed'] { + return this.options?.effectAllowed || 'copy'; + } +} + +/** + * Allow Drag action + * + * Usage: + * <div use:allow-drag={{ + * dragEnabled: true, + * dragData: yourDragData, + * isContainer: true, + * badgeCount: 4 + * }}></div> + */ +export function allowDrag( + target: HTMLElement, + options: DragOptions | false, +): ActionReturn<DragOptions> { + const enabled = options !== false && (options.dragEnabled ?? true); + let dragHandler; + + if (enabled && options.dragData) { + dragHandler = new DragHandler(target, options); + } + + return { + destroy: () => { + dragHandler?.destroy(); + }, + update: (updatedOptions: DragOptions) => { + // Hotfix for updated properties. Remove handlers with data and add new ones. + // TODO: rdar://98074771 (Onyx: DnD: Update allow-drag and allow-drop actions to support updates) + dragHandler?.destroy(); + + if (updatedOptions?.dragEnabled && updatedOptions?.dragData) { + dragHandler = new DragHandler(target, updatedOptions); + } + }, + }; +} + +export default allowDrag; diff --git a/shared/components/src/actions/allow-drop.ts b/shared/components/src/actions/allow-drop.ts new file mode 100644 index 0000000..231add4 --- /dev/null +++ b/shared/components/src/actions/allow-drop.ts @@ -0,0 +1,249 @@ +import type { ActionReturn } from 'svelte/action'; +import { get } from 'svelte/store'; +import { activeDragHandler } from '@amp/web-app-components/src/actions/allow-drag'; + +/* + FOLLOW-UP WORK: + - it now adds and destroys the handler, but doesn't have a update method. + We might want to keep track of any DropHandler that got created for an element and just update the existing instance. + rdar://98074771 (Onyx: DnD: Update allow-drag and allow-drop actions to support updates) +*/ + +const DROP_AREA_DATA_ATTR = 'data-drop-area'; +const DRAG_OVER_CLASS = 'is-drag-over'; + +export type DropOptions = { + dropEnabled: boolean; + onDrop: (details: DropData) => void; + targets?: + | [DropTarget] + | [DropTarget.Top, DropTarget.Bottom] + | [DropTarget.Left, DropTarget.Right]; + dropEffect?: DataTransfer['dropEffect']; +}; + +export type DropData = { + data: unknown; + dropTarget?: DropTarget; +}; + +export enum DropTarget { + Top = 'top', + Bottom = 'bottom', + Left = 'left', + Right = 'right', +} + +const DRAG_OVER_CLASSES = { + default: DRAG_OVER_CLASS, + [DropTarget.Top]: `${DRAG_OVER_CLASS}-${DropTarget.Top}`, + [DropTarget.Bottom]: `${DRAG_OVER_CLASS}-${DropTarget.Bottom}`, + [DropTarget.Left]: `${DRAG_OVER_CLASS}-${DropTarget.Left}`, + [DropTarget.Right]: `${DRAG_OVER_CLASS}-${DropTarget.Right}`, +}; + +class DropHandler { + private readonly element: HTMLElement; + private readonly options: DropOptions; + private enterTarget: HTMLElement; + private target: DropTarget; + private lastPosition: number; + + constructor(element: HTMLElement, options: DropOptions) { + this.element = element; + this.options = options; + + this.addEventListeners(); + } + + private addEventListeners = (): void => { + this.element.setAttribute(DROP_AREA_DATA_ATTR, ''); + this.element.addEventListener('dragenter', this.onDragEnter); + this.element.addEventListener('dragover', this.onDragOver); + this.element.addEventListener('dragleave', this.onDragLeave); + this.element.addEventListener('drop', this.onDrop); + }; + + private removeEventListeners = (): void => { + this.element.removeEventListener('dragenter', this.onDragEnter); + this.element.removeEventListener('dragover', this.onDragOver); + this.element.removeEventListener('dragleave', this.onDragLeave); + this.element.removeEventListener('drop', this.onDrop); + }; + + public destroy = (): void => { + this.resetState(); + this.element.removeAttribute(DROP_AREA_DATA_ATTR); + this.removeEventListeners(); + }; + + private resetState = (): void => { + this.enterTarget = null; + this.target = null; + this.lastPosition = null; + this.removeDragOverClasses(); + }; + + private removeDragOverClasses = (): void => { + Object.keys(DRAG_OVER_CLASSES).forEach((key) => { + this.element.classList.remove(DRAG_OVER_CLASSES[key]); + }); + }; + + private setDragOverClass = (targetName: DropTarget): void => { + const target = targetName || this.target; + const dragOverClass = + DRAG_OVER_CLASSES[target] || DRAG_OVER_CLASSES.default; + // add right target class if not yet present + if (!this.element.classList.contains(dragOverClass)) { + this.removeDragOverClasses(); // clear all target classes before switching target + this.element.classList.add(dragOverClass); + } + }; + + /** + * getLocationTarget: this function determines in what target region the user currently is + * + * @param e DragEvent + * @param threshold threshold for the target location switch zone + * @returns DropTarget + */ + private getLocationTarget = (e: DragEvent, threshold = 0): DropTarget => { + const { targets } = this.options; + + // Do not check on drag over region when it has no or one target + if (!targets || targets.length === 1) { + this.target = targets?.[0]; + return this.target; + } + + let position, size; + + // When using top - bottom targets + if (targets.join('-') === `${DropTarget.Top}-${DropTarget.Bottom}`) { + // offset to drop area, instead of target (which could be a child) + position = e.clientY - this.element.getBoundingClientRect().top; + size = this.element.offsetHeight; + } + // When using left - right targets + else if ( + targets.join('-') === `${DropTarget.Left}-${DropTarget.Right}` + ) { + // offset to drop area, instead of target (which could be a child) + position = e.clientX - this.element.getBoundingClientRect().left; + size = this.element.offsetWidth; + } + + if (position && size) { + if ( + !this.lastPosition || + Math.abs(position - this.lastPosition) > threshold + ) { + this.lastPosition = position; + this.target = position <= size / 2 ? targets[0] : targets[1]; + } + } + + return this.target; + }; + + private isCompatibleDropEffect(e: DragEvent) { + // Workaround for https://bugs.webkit.org/show_bug.cgi?id=178058 + // There is a longstanding WebKit bug where any value set by the user + // on `dataTransfer.effectAllowed` in the dragstart event is ignored + // and always returns "all". This means that we cannot trust the value + // that is set in the DragEvent. As a workaround, we store and check + // the active drag handler for the effectAllowed specified in the options. + // + // const { dropEffect, effectAllowed } = e.dataTransfer; + const { dropEffect } = e.dataTransfer; + const effectAllowed = get(activeDragHandler)?.getEffectAllowed(); + + return ( + effectAllowed === 'all' || + effectAllowed.toLowerCase().includes(dropEffect) + ); + } + + private onDragEnter = (e: DragEvent): void => { + e.dataTransfer.dropEffect = this.options.dropEffect || 'copy'; + + if (!this.isCompatibleDropEffect(e)) { + return; + } + + e.stopPropagation(); + + // Set enterTarget to cover entering child elements + this.enterTarget = e.target as HTMLElement; + this.setDragOverClass(this.getLocationTarget(e)); + }; + + private onDragOver = (e: DragEvent): void => { + e.dataTransfer.dropEffect = this.options.dropEffect || 'copy'; + + if (!this.isCompatibleDropEffect(e)) { + return; + } + + e.preventDefault(); // prevent the browser from default handling of the data to allow drop + e.stopPropagation(); // prevent setting classes on parent drop areas + this.setDragOverClass(this.getLocationTarget(e, 10)); + }; + + private onDragLeave = (e: Event): void => { + // Only set drag-over to false when it leaves the drop area. Not on entering childs + if (e.target === this.enterTarget) { + this.resetState(); + } + }; + + private onDrop = (e: DragEvent): void => { + e.preventDefault(); + e.stopPropagation(); // Prevent drop action on parent elements + + const data = JSON.parse(e.dataTransfer.getData('text/plain')); + const draggedData: DropData = { data }; + + if (this.target) { + draggedData.dropTarget = this.target; + } + + this.resetState(); + this.options.onDrop(draggedData); + }; +} + +/** + * Allow Drop action + * + * Usage: + * <div use:allow-drop={{ dropEnabled: true, onDrop: dropAction }}></div> + */ +export function allowDrop( + target: HTMLElement, + options: DropOptions, +): ActionReturn<DropOptions> { + let dropHandler; + + if (options?.dropEnabled && options?.onDrop) { + dropHandler = new DropHandler(target, options); + } + + return { + destroy: () => { + dropHandler?.destroy(); + }, + update: (updatedOptions: DropOptions) => { + // Hotfix for updated properties. Remove handlers with data and add new ones. + // TODO: rdar://98074771 (Onyx: DnD: Update allow-drag and allow-drop actions to support updates) + dropHandler?.destroy(); + + if (updatedOptions?.dropEnabled && updatedOptions?.onDrop) { + dropHandler = new DropHandler(target, updatedOptions); + } + }, + }; +} + +export default allowDrop; diff --git a/shared/components/src/actions/click-outside.ts b/shared/components/src/actions/click-outside.ts new file mode 100644 index 0000000..a9475c7 --- /dev/null +++ b/shared/components/src/actions/click-outside.ts @@ -0,0 +1,18 @@ +export default function clickOutside( + node: HTMLElement, + handler: (event: any) => void, +) { + const handleClick = (event) => { + if (!node.contains(event.target)) { + handler(event); + } + }; + + document.addEventListener('click', handleClick); + + return { + destroy() { + document.removeEventListener('click', handleClick); + }, + }; +} diff --git a/shared/components/src/actions/focus-node-on-mount.ts b/shared/components/src/actions/focus-node-on-mount.ts new file mode 100644 index 0000000..92bb4a9 --- /dev/null +++ b/shared/components/src/actions/focus-node-on-mount.ts @@ -0,0 +1,5 @@ +export function focusNodeOnMount(node: HTMLElement) { + // Wrapping in queueMicrotask ensures the node is attached to the + // DOM before attempting to focus. + queueMicrotask(() => node.focus()); +} diff --git a/shared/components/src/actions/focus-node.ts b/shared/components/src/actions/focus-node.ts new file mode 100644 index 0000000..907f584 --- /dev/null +++ b/shared/components/src/actions/focus-node.ts @@ -0,0 +1,19 @@ +export default function focusNode( + node: HTMLElement, + focusedIndex: number | null, +) { + const nodeIndex = Number(node.getAttribute('data-index')); + + // Handle the initial focus when applicable + if (nodeIndex === focusedIndex) { + node.focus(); + } + + return { + update(newFocusedIndex) { + if (nodeIndex === newFocusedIndex) { + node.focus(); + } + }, + }; +} diff --git a/shared/components/src/actions/intersection-observer.ts b/shared/components/src/actions/intersection-observer.ts new file mode 100644 index 0000000..cd22760 --- /dev/null +++ b/shared/components/src/actions/intersection-observer.ts @@ -0,0 +1,100 @@ +import { getRafQueue } from '@amp/web-app-components/src/utils/rafQueue'; +// TODO: rdar://91082022 (JMOTW: Performance - Refactor IntersectionObserver Admin Locally) +import IntersectionObserverAdmin from 'intersection-observer-admin'; + +// Threshold is how much of the target element is currently visible within the +// root's intersection ratio, as a value between 0.0 and 1.0. +// https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry/intersectionRatio +// +// Examples: +// 0 = a single visible pixel counts as the target being "visible" +// 1 = a single non-visible pixel counts as the target being "not visible"" +const DEFAULT_VIEWPORT_THRESHOLD = 0.6; + +// https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver#properties +// Adding `callback` to the type since you can only pass an array or object into actions +type configObject = { + root?: Element | null; + rootMargin?: string; + threshold?: number; + callback?: Function; +}; + +let intersectionObserverAdmin; + +/** + * IntersectionObserver action to track when an element comes in to/goes out of the visible viewport. + * Useful for stopping animations of elements no longer visible, starting animations when + * they appear/reappear, applying/removing styles, etc. + * + * Callbacks will be called with a boolean depending on if the item is intersecting (true) or not (false). + * + * Utilizes Intersection Observer Admin (https://github.com/snewcomer/intersection-observer-admin) to allow + * the setup of a single Intersection Observer queue that handles observations in a way that allows each + * element to have it's own callback and IntersectionObserver configuration. + * + * @function intersectionObserver + * @param {Element} target Element to track (DOM element, Document, or null for top-level document viewport) + * @param {configObject} options callback function for handling viewport visiblity changes + * + * @example `<div use:intersectionObserver={{ callback: handleIntersectionUpdate }}></div>` + * @example `<div use:intersectionObserver={{ + * callback: handleIntersectionUpdate, + * root: document.querySelector('some-element') + * }}></div>` + * @example `<div use:intersectionObserver={{ + * callback: handleIntersectionUpdate, + * root: document.querySelector('some-element'), + * threshold: 1 + * }}></div>` + * @example `<div use:intersectionObserver={{ + * callback: handleIntersectionUpdate, + * root: document.querySelector('some-element'), + * rootMargin: '0px 0px 0px 0px', + * threshold: 1 + * }}></div>` + */ +export function intersectionObserver( + target: Element, + options: configObject = {}, +): { destroy: () => void } { + if (!('IntersectionObserver' in window)) return; + + if (!options.callback) { + console.warn( + 'Use of intersectionObserver action requires passing in a callback function', + ); + return; + } + + const rafQueue = getRafQueue(); + const customCallback = options.callback; + + // Clone options to manipulate object without side effects + // Assign initial default threshold, overridden by any settings in `options` + const optionsObj = Object.assign( + { threshold: DEFAULT_VIEWPORT_THRESHOLD }, + options, + ); + delete optionsObj.callback; + + const callback = (ioEntry) => { + rafQueue.add(() => customCallback(ioEntry.isIntersecting)); + }; + + if (!intersectionObserverAdmin) { + intersectionObserverAdmin = new IntersectionObserverAdmin(); + } + + // Add callbacks that will be called when observer detects entering and leaving viewport + intersectionObserverAdmin.addEnterCallback(target, callback); + intersectionObserverAdmin.addExitCallback(target, callback); + + intersectionObserverAdmin.observe(target, optionsObj); + + return { + destroy() { + intersectionObserverAdmin.unobserve(target, optionsObj); + }, + }; +} diff --git a/shared/components/src/actions/list-keyboard-access.ts b/shared/components/src/actions/list-keyboard-access.ts new file mode 100644 index 0000000..7ac5819 --- /dev/null +++ b/shared/components/src/actions/list-keyboard-access.ts @@ -0,0 +1,351 @@ +const NAVIGATION_KEY_NAMES = ['ArrowDown', 'ArrowUp']; +const INTERACTABLE_NODE_NAMES = ['A', 'BUTTON']; +export type configObject = { + listItemClassNames: string; + isRoving?: boolean; + listGroupElement?: HTMLElement; + syncInteractivityWithVisibility?: boolean; +}; + +type configParams = configObject & { targetElement: HTMLElement }; + +/** + * A construct that manages keyboard navigation as it relates to lists. + * @class + */ + +class ListKeyboardAccess { + private listItemClassNames: Array<string>; + private listParentElement: HTMLElement; + private boundFocusInHandler: EventListener; + private boundKeyDownHandler: EventListener; + private listGroupElement: HTMLElement | undefined; + // a current index based on an ancestor parent i.e. `listGroupElement`. + private currentRootIndex: number = -1; + // a current index based on an immediate list parent i.e. `listParentElement`. + private currentIndex: number = -1; + private isRoving: boolean = false; + private syncInteractivityWithVisibility: boolean | undefined; + private intersectionObserver: IntersectionObserver | undefined; + + static isWindowEventBound: boolean = false; + + constructor(options: configParams) { + const { + listGroupElement, + targetElement, + syncInteractivityWithVisibility, + } = options; + this.listParentElement = targetElement; + this.listGroupElement = listGroupElement; + this.isRoving = (options.isRoving ?? false) && !!this.listGroupElement; + this.syncInteractivityWithVisibility = syncInteractivityWithVisibility; + + // converting a string list into an array of CSS class names (note: not selectors). + this.listItemClassNames = options.listItemClassNames + ?.split(',') + .map((className) => className.trim()); + // Attempting to only bind this event once for the purpose of list navigation. + if (!ListKeyboardAccess.isWindowEventBound) { + window.addEventListener( + 'keydown', + ListKeyboardAccess.windowKeyUpHandler, + ); + ListKeyboardAccess.isWindowEventBound = true; + } + + if (this.listItemClassNames?.join('').length) { + this.boundFocusInHandler = this.focusInHandler.bind(this); + this.boundKeyDownHandler = this.keyDownHandler.bind(this); + + this.listParentElement.addEventListener( + 'focusin', + this.boundFocusInHandler, + { + capture: true, + }, + ); + this.listParentElement.addEventListener( + 'keydown', + this.boundKeyDownHandler, + ); + } else { + throw Error('ListKeyboardAccess requires listItemClassNames'); + } + + if (this.syncInteractivityWithVisibility) { + // Create the observer + this.intersectionObserver = new IntersectionObserver( + (entries) => { + entries.forEach((entry) => { + setItemInteractivity( + entry.target as HTMLElement, + entry.isIntersecting, + ); + }); + }, + { + root: targetElement, + rootMargin: '0px', + threshold: 0.5, + }, + ); + + const listItems = this.getListItems(); + for (let i = 0; i < listItems.length; i++) { + this.intersectionObserver.observe(listItems[i]); + } + } + } + + destroy() { + if (ListKeyboardAccess.isWindowEventBound) { + window.removeEventListener( + 'keydown', + ListKeyboardAccess.windowKeyUpHandler, + ); + ListKeyboardAccess.isWindowEventBound = false; + } + + this.listParentElement?.removeEventListener( + 'focusin', + this.boundFocusInHandler, + { + capture: true, + }, + ); + + this.listParentElement?.removeEventListener( + 'keydown', + this.boundKeyDownHandler, + ); + + this.intersectionObserver?.disconnect(); + } + + private getListItems( + fromlistGroupElement: boolean = false, + ): Array<HTMLElement> { + const { listGroupElement, listParentElement } = this; + const root = + fromlistGroupElement && listGroupElement + ? listGroupElement + : listParentElement; + const selectors = getSelectorsFromCSSClassNames( + this.listItemClassNames.join(','), + ); + return Array.from(root.querySelectorAll(selectors)); + } + + private focusInHandler(event: any) { + const currentListItem = this.findListItem(event.target); + const listItems = this.getListItems(); + // bail if no list items or currentListItem + if (!listItems.length || !currentListItem) return; + this.currentIndex = listItems.indexOf(currentListItem); + + this.currentRootIndex = this.getListItems(this.isRoving)?.indexOf( + currentListItem, + ); + + if (this.currentIndex >= 0 && this.isRoving) { + for (let i = 0; i < listItems.length; i++) { + setTabFocusable(listItems[i], i === this.currentIndex); + } + } + } + + private keyDownHandler(event: any) { + if ( + !NAVIGATION_KEY_NAMES.includes(event.key) || + this.currentIndex < 0 + ) { + return; + } + const currentIndex = this.isRoving + ? this.currentRootIndex + : this.currentIndex; + const listItems = this.getListItems(this.isRoving); + + let nextIndex = + event.key === 'ArrowUp' + ? Math.max(0, currentIndex - 1) + : Math.min(currentIndex + 1, listItems.length - 1); + + focusVisibleItemByIndex(nextIndex, currentIndex, listItems); + } + + /** + * A helper method to find the closest focusable list item. + * @param sourceElement origin of traversal + * @returns HTMLElement | null + */ + private findListItem(source: HTMLElement | null): HTMLElement | null { + if (!source || !this.listItemClassNames?.length) return null; + + const selector = this.listItemClassNames.map((c) => `.${c}`).join(','); + const hit = source.closest(selector) as HTMLElement | null; + if (hit) return hit; + + const parent = source.parentElement; + if (!parent) return null; + + // BFS over siblings and their descendants + const q: Element[] = Array.from(parent.children); + const checked = new Set<Element>([parent]); + for (let i = 0; i < q.length; i++) { + const el = q[i] as HTMLElement; + if (checked.has(el)) continue; + checked.add(el); + + if (el.matches(selector)) return el; + // enqueue children + for (const child of Array.from(el.children)) { + if (!checked.has(child)) q.push(child); + } + } + return null; + } + + /** + * Event handler for the window to stop scrolling the page when users use the arrow keys. + * @param event + */ + static windowKeyUpHandler(event: any) { + if (NAVIGATION_KEY_NAMES.includes(event.key)) { + event.preventDefault(); + } + } +} + +function focusVisibleItemByIndex( + index: number, + targetIndex: number, + listItems: Array<HTMLElement>, +) { + const direction = index - targetIndex > 0 ? 1 : -1; + const listItem = listItems[index]; + if (!listItem) { + return; + } + // Sometimes the list item itself is visible, but the parent + // is not--like the search button in the nav bar. + // Check visibility for the element and its parent before assigning focus. + if (isItemVisible(listItem) && isItemVisible(listItem.parentElement)) { + listItems[index].focus(); + } else { + focusVisibleItemByIndex(index + direction, targetIndex, listItems); + } +} + +function isItemVisible(element: HTMLElement | null): boolean { + if (element === null) return false; + const { display, visibility, opacity } = window.getComputedStyle(element); + return display !== 'none' && visibility !== 'hidden' && opacity !== '0'; +} + +function getSelectorsFromCSSClassNames(classes: string): string { + if (!classes) return ''; + return classes + .split(',') + .map((name) => `.${name.trim()}`) + .join(','); +} + +/** + * sets tabindex for an element following W3C Web standards. + * @param element HTMLElement + * @param isTabFocusable boolean "tab-focusable" refers to whether or not an element is focusable using the Tab key. + */ +export function setTabFocusable(element: HTMLElement, isTabFocusable: boolean) { + if (INTERACTABLE_NODE_NAMES.includes(element.nodeName)) { + const isAnchor = element.nodeName === 'A'; + if (isTabFocusable) { + element.removeAttribute(isAnchor ? 'tabindex' : 'disabled'); + } else { + const attribtuesToSet: [string, string] = isAnchor + ? ['tabindex', '-1'] + : ['disabled', 'true']; + element.setAttribute(...attribtuesToSet); + } + } else { + element.setAttribute('tabindex', isTabFocusable ? '0' : '-1'); + } +} + +export function setItemInteractivity( + shelfItemElement: HTMLElement, + isShelfItemVisible: boolean, +) { + if ( + INTERACTABLE_NODE_NAMES.includes(shelfItemElement.nodeName) || + shelfItemElement.getAttribute('tabindex') + ) { + // Handles the shelf item + setTabFocusable(shelfItemElement as HTMLElement, isShelfItemVisible); + } + + if (isShelfItemVisible) { + shelfItemElement.removeAttribute('aria-hidden'); + } else { + shelfItemElement.setAttribute('aria-hidden', 'true'); + } + + // handles the children in the item + const selectors: string = INTERACTABLE_NODE_NAMES.map((nodeName) => + nodeName.toLowerCase(), + ).join(','); + const interactiveContent: Array<HTMLAnchorElement | HTMLButtonElement> = + Array.from(shelfItemElement.querySelectorAll(selectors)); + for (let el of interactiveContent) { + setTabFocusable(el, isShelfItemVisible); + } +} + +/** + * set up mutation observer to ensure tab-focusablility is set appropriately based on the list item's focusability. + * @param listItemNode + * @param interactableTargets + * @returns + */ +export function initListItemObserver( + listItemNode: HTMLElement, + interactableTargets: Array<HTMLElement>, +): MutationObserver { + const observer = new MutationObserver((mutationsList) => { + let tabindex: number; + for (let mutation of mutationsList) { + if (mutation.type === 'attributes' && interactableTargets.length) { + for (let i = 0; i < interactableTargets.length; i++) { + tabindex = Number( + (mutation.target as HTMLElement).getAttribute( + 'tabindex', + ), + ); + setTabFocusable(interactableTargets[i], tabindex >= 0); + } + } + } + }); + if (listItemNode) { + observer.observe(listItemNode, { attributes: true }); + } + return observer; +} + +export function listKeyboardAccess( + targetElement: HTMLElement, + options: configObject = { listItemClassNames: '' }, +) { + const listKeyboardAXInstance = new ListKeyboardAccess({ + targetElement, + ...options, + }); + return { + destroy() { + listKeyboardAXInstance.destroy(); + }, + }; +} + +export default listKeyboardAccess; diff --git a/shared/components/src/actions/updateScrollAndWindowDependentVisuals.ts b/shared/components/src/actions/updateScrollAndWindowDependentVisuals.ts new file mode 100644 index 0000000..b934e37 --- /dev/null +++ b/shared/components/src/actions/updateScrollAndWindowDependentVisuals.ts @@ -0,0 +1,48 @@ +import { debounce } from '@amp/web-app-components/src/utils/debounce'; +import { throttle } from '@amp/web-app-components/src/utils/throttle'; +/** + * Dynamically change header and bottom gradient style when scrolling within a modal, and on window resize + */ +export function updateScrollAndWindowDependentVisuals(node) { + let animationRequest; + const handleScroll = () => { + // Get scroll details + const { scrollHeight, scrollTop, offsetHeight } = node; + const maxScroll = scrollHeight - offsetHeight; + + // Calculate whether content is scrolled + const contentIsScrolling = scrollTop > 1; + + // Calculate if bottom gradient should be hidden + const scrollingNotPossible = maxScroll === 0; + const pastMaxScroll = scrollTop >= maxScroll; + const hideGradient = scrollingNotPossible || pastMaxScroll; + + if (animationRequest) { + window.cancelAnimationFrame(animationRequest); + } + + animationRequest = window.requestAnimationFrame(() => + node.dispatchEvent( + new CustomEvent('scrollStatus', { + detail: { contentIsScrolling, hideGradient }, + }), + ), + ); + }; + + const onResize = throttle(handleScroll, 250); + const onScroll = debounce(handleScroll, 50); + node.addEventListener('scroll', onScroll, { capture: true, passive: true }); + window.addEventListener('resize', onResize); + + return { + destroy() { + node.removeEventListener('scroll', onScroll, { capture: true }); + window.removeEventListener('resize', onResize); + if (animationRequest) { + window.cancelAnimationFrame(animationRequest); + } + }, + }; +} diff --git a/shared/components/src/components/Artwork/Artwork.svelte b/shared/components/src/components/Artwork/Artwork.svelte new file mode 100644 index 0000000..c661947 --- /dev/null +++ b/shared/components/src/components/Artwork/Artwork.svelte @@ -0,0 +1,565 @@ +<script lang="ts"> + import type { SvelteComponent } from 'svelte'; + import { onMount } from 'svelte'; + import { makeSafeTick } from '@amp/web-app-components/src/utils/makeSafeTick'; + import type { Readable } from 'svelte/store'; + import LoaderSelector, { + LOADER_TYPE, + } from '@amp/web-app-components/src/components/Artwork/loaders/LoaderSelector.svelte'; + import { + getShelfAspectRatioContext, + hasShelfAspectRatioContext, + } from '@amp/web-app-components/src/utils/shelfAspectRatio'; + import { FILE_TO_MIME_TYPE, DEFAULT_FILE_TYPE } from './constants'; + import type { Artwork, ImageSettings, Profile, ChinConfig } from './types'; + import { getAspectRatio, getImageTagWidthHeight } from './utils/artProfile'; + import { getPreconnectTracker } from './utils/preconnect'; + import { buildSourceSet, getImageSizes } from './utils/srcset'; + import { deriveBackgroundColor } from './utils/validateBackground'; + + const preconnectTracker = getPreconnectTracker(); + + /** + * artwork object + * @type {{ template: string, width: number, height: number, backgroundColor: string }} Artwork + */ + export let artwork: Artwork; + /** + * alt tag to use on image. + */ + export let alt: string = ''; + /** + * id to use on image. + * @type {string} + */ + export let id: string | undefined = undefined; + /** + * Profiles are required to determine the optimal image to render for given viewports. + * @type {Profile | string} + */ + export let profile: Profile | string; + /** + * k/v map of settings that don't depend on viewport size. + * @type {ImageSettings} + */ + export let imageSettings: ImageSettings = {}; + /** + * Apply rounded secondary corner styles to top of artwork image + * @type {boolean} + */ + export let topRoundedSecondary: boolean = false; + /** + * Whether to lazy load the image. + * Set this to false if this image is expected to be the LCP. + */ + export let lazyLoad: boolean = true; + /** + * Sets the `fetchpriority` attribute on the image. + * Set this to 'high' if this image is expected to be the LCP. + */ + export let fetchPriority: 'high' | 'auto' | 'low' = 'auto'; + /** + * Turning off container styles allows for a custom wrapper to be used to provide different + * styling when an artwork is used outside of a lockup or in a different context. + * @type {boolean} + */ + export let useContainerStyle: boolean = true; + /** + * Option to disable CSS anchoring for shelf chevron. + * Useful to isolate anchor when there are multiple images in a single lockup. + * @type {boolean} + */ + export let noShelfChevronAnchor: boolean = false; + + /** + * Configuration object for chin effects including height and style. + * Used primarily by TV app for adding visual effects below the main artwork. + * @type {ChinConfig} + */ + export let chinConfig: ChinConfig | undefined = undefined; + + export let forceFullWidth: boolean = true; + + /** + * Option to disable image from being auto-centered + * in its container. Only relevant for non-square + * images. + */ + export let disableAutoCenter = false; + + /** + * `isDecorative` indicates if an image is decoration. + * Decoaration images should be attributed a presentation role (role=presentation) to avoid an oververbose auditory user experience. + * By default, it is set to false if an alt attribute is provided. + * See https://www.w3.org/WAI/tutorials/images/decorative/ + * @type {boolean} + */ + export let isDecorative: boolean = !!!alt; + + /** + * Allows artwork to be rendered without a border, regardless of it's background color or transparency. + */ + export let withoutBorder: boolean = false; + + let localShelfAspectRatioStore: Readable<string> | null = null; + + if (hasShelfAspectRatioContext()) { + const { addProfile, shelfAspectRatio } = getShelfAspectRatioContext(); + addProfile(profile); + localShelfAspectRatioStore = shelfAspectRatio; + } + + $: template = artwork && artwork.template; + + $: imageIsLoading = !!template; // start in loading state when template is available + $: thereWasAnError = !artwork; // start in clean error state unless there's no artwork passed + + $: backgroundColor = artwork?.backgroundColor; + + $: ({ fileType = DEFAULT_FILE_TYPE } = imageSettings); + + $: isBackgroundTransparent = + imageSettings?.hasTransparentBackground ?? false; + + $: validBackgroundColor = isBackgroundTransparent + ? 'transparent' + : deriveBackgroundColor(backgroundColor); + + $: srcset = + artwork && buildSourceSet(artwork, imageSettings, profile, chinConfig); + $: webpSourceSet = + artwork && + buildSourceSet( + artwork, + Object.assign({}, imageSettings, { fileType: 'webp' }), + profile, + chinConfig, + ); + $: aspectRatio = getAspectRatio(profile); + $: imageTagSizeObj = getImageTagWidthHeight(profile); + + // Calculate effective aspect ratio accounting for chin height + $: effectiveAspectRatio = (() => { + const chinHeightValue = chinConfig?.height ?? 0; + if (chinHeightValue === 0 || aspectRatio === null) { + return aspectRatio; + } + + // Get the base dimensions from the profile + const baseHeight = imageTagSizeObj.height; + const baseWidth = imageTagSizeObj.width; + + // Calculate new aspect ratio with chin height added + const newHeight = baseHeight + chinHeightValue; + return baseWidth / newHeight; + })(); + + // NOTE: We intentionally set opacity to 1 in SSR so that images will load + // in before the JS loads. + $: opacity = `${imageIsLoading && typeof window !== 'undefined' ? 0 : 1}`; + // And similarly, we force <NoLoader> so that the image markup is emitted + $: loaderType = + lazyLoad && typeof window !== 'undefined' + ? LOADER_TYPE.LAZY + : LOADER_TYPE.NONE; + + $: sizes = getImageSizes(profile, artwork?.width); + + $: wrapperStyle = (() => { + // remove the joe color background to prevent + // parts of it from bleeding through artwork + const background = + ($$slots['placeholder-component'] && thereWasAnError) || + hasTransitionInEnded || + isBackgroundTransparent + ? 'transparent' + : `${validBackgroundColor}`; + + // if backgroundColor data is unavailable, do not insert inline background styles + // (--artwork-bg-color & --placeholder-bg-color) - to allow joe color fallback + const artworkBGColor = validBackgroundColor + ? `--artwork-bg-color: ${validBackgroundColor};` + : ''; + const placeholderBGColor = background + ? `--placeholder-bg-color: ${background};` + : ''; + + return ` + ${artworkBGColor} + --aspect-ratio: ${ + effectiveAspectRatio !== null ? effectiveAspectRatio : 1 + }; + ${placeholderBGColor} + `; + })(); + + $: { + preconnectTracker?.trackUrl(template); + } + + /** + * false if image natural aspect ratio is not equal to profile + * + * @see {onImageLoad} + */ + let aspectRatioMatchesProfile = true; + + $: hasDominantShelfAspectRatio = + localShelfAspectRatioStore !== null && + $localShelfAspectRatioStore !== null; + + // Should apply joe color BG if image natural aspect ratio doesn't match shelfAspectRatio + $: shouldOverrideBG = (() => { + let overrideBG = false; + if (localShelfAspectRatioStore !== null) { + const shelfAspectRatio = parseFloat($localShelfAspectRatioStore); + if (!isNaN(shelfAspectRatio)) { + const roundedShelfAspectRatio = + Math.round(shelfAspectRatio * 100) / 100; + const roundedAspectRatio = + Math.round(effectiveAspectRatio * 100) / 100; + if (roundedShelfAspectRatio !== roundedAspectRatio) { + overrideBG = true; + } + } + } else if (!aspectRatioMatchesProfile) { + overrideBG = true; + } + return overrideBG; + })(); + + const onImageLoad = (e: Event) => { + const img = e.target as HTMLImageElement; + + if (img.naturalHeight !== 0 && img.naturalWidth !== 0) { + const actualAspectRatio = + Math.round((img.naturalWidth / img.naturalHeight) * 100) / 100; + const roundedEstimate = + Math.round(effectiveAspectRatio * 100) / 100; + + if ( + actualAspectRatio !== roundedEstimate && + Math.abs( + (actualAspectRatio - roundedEstimate) / + ((actualAspectRatio + roundedEstimate) / 2), + ) > 0.1 + ) { + aspectRatioMatchesProfile = false; + } + } + imageIsLoading = false; + }; + + let hasTransitionInEnded = false; + const onTransitionEnd = (e: TransitionEvent) => { + const img = e.target as HTMLElement; + const opacityValue = parseFloat(img.style.opacity); + + if (opacityValue === 1) { + hasTransitionInEnded = true; + } else { + hasTransitionInEnded = false; + } + }; + + const onImageError = () => { + thereWasAnError = true; + imageIsLoading = false; + }; + + let loaderComponent: SvelteComponent; + let artworkComponent: HTMLElement; + + const safeTick = makeSafeTick(); + + onMount(async () => { + await safeTick(async (tick) => { + await tick(); + loaderComponent.onSlotMount(artworkComponent); + }); + }); + + const getImageOrientation = (aspectRatio: number) => { + let orientation: 'square' | 'landscape' | 'portrait'; + if (aspectRatio === 1) { + orientation = 'square'; + } else if (aspectRatio > 1) { + orientation = 'landscape'; + } else { + orientation = 'portrait'; + } + return orientation; + }; +</script> + +<div + data-testid="artwork-component" + {id} + class={`artwork-component artwork-component--aspect-ratio artwork-component--orientation-${getImageOrientation( + effectiveAspectRatio, + )}`} + class:container-style={useContainerStyle} + class:artwork-component--downloaded={!imageIsLoading && + hasTransitionInEnded} + class:artwork-component--error={thereWasAnError} + class:artwork-component--fullwidth={forceFullWidth} + class:artwork-component--top-rounded-secondary={topRoundedSecondary} + class:artwork-component--auto-center={!disableAutoCenter && + (hasDominantShelfAspectRatio || !aspectRatioMatchesProfile)} + class:artwork-component--bg-override={shouldOverrideBG} + class:artwork-component--has-borders={!isBackgroundTransparent && + !withoutBorder} + class:artwork-component--no-anchor={noShelfChevronAnchor} + style={wrapperStyle} + on:transitionend={onTransitionEnd} + bind:this={artworkComponent} +> + {#if imageIsLoading && $$slots['loading-component']} + <div + class="artwork-component__contents" + data-testid="artwork-component__loading" + > + <slot name="loading-component" /> + </div> + {:else if thereWasAnError && $$slots['placeholder-component']} + <div + class="artwork-component__contents" + data-testid="artwork-component__placeholder" + > + <slot name="placeholder-component" /> + </div> + {/if} + <LoaderSelector {loaderType} bind:this={loaderComponent} let:isVisible> + {#if !thereWasAnError && isVisible} + <picture> + {#if webpSourceSet} + <source + {sizes} + srcset={webpSourceSet} + type={FILE_TO_MIME_TYPE.webp} + /> + {/if} + <source {sizes} {srcset} type={FILE_TO_MIME_TYPE[fileType]} /> + <img + {alt} + class="artwork-component__contents artwork-component__image" + loading={lazyLoad ? 'lazy' : null} + style:opacity + src="/assets/artwork/1x1.gif" + role={isDecorative ? 'presentation' : null} + decoding="async" + width={`${imageTagSizeObj.width}`} + height={`${ + imageTagSizeObj.height + (chinConfig?.height ?? 0) + }`} + fetchpriority={fetchPriority} + on:load={onImageLoad} + on:error={onImageError} + /> + </picture> + {/if} + </LoaderSelector> +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'amp/stylekit/core/colors' as *; + @use 'amp/stylekit/core/mixins/browser-targets' as *; + @use '@amp/web-shared-styles/app/core/globalvars' as *; + @use '@amp/web-shared-styles/app/core/mixins/after-shadow' as *; + @use '@amp/web-shared-styles/app/core/colors' as *; + @use './style/ratio-based-artwork-box.scss' as *; + + // container style design: https://pd-hi.apple.com/viewvc/Common/Modules/macOS/-Cross%20Product/_macOS%20-%20Content%20Container%20Treatment.png?revision=54684&pathrev=57428 + // TODO: rdar://79348133 (Bring in copy + pasted variables into StyleKit) + .container-style { + border-radius: var( + --global-border-radius-medium, + #{$global-border-radius-medium} + ); + + &::after { + @include after-shadow; + } + } + + .artwork-component { + width: var(--artwork-override-width, 100%); + height: var(--artwork-override-height, auto); + max-width: var(--artwork-override-max-width, none); + min-width: var(--artwork-override-min-width, 0); + min-height: var(--artwork-override-min-height, 0); + max-height: var(--artwork-override-max-height, none); + border-radius: inherit; + box-sizing: border-box; + contain: content; + overflow: hidden; + position: relative; + background-color: var( + --override-placeholder-bg-color, + var(--placeholder-bg-color, var(--genericJoeColor)) + ); + z-index: var(--z-default); + + &.artwork-component--has-borders { + &::after { + @include after-shadow; + } + } + + &.artwork-component--auto-center { + @include ratio-based-artwork-box; + + &.artwork-component--bg-override { + background-color: var(--artwork-bg-color); + } + } + } + + // Artwork with rounded-secondary border-radius on top corners + .artwork-component--top-rounded-secondary { + // Required to keep lockups/chins aligned with the same height, when 2-line clamps are visible. + flex-grow: 0; + // Applying `border-radius` and `overflow: hidden;` to prevent image/chin subpixel width mismatch + // prettier-ignore + border-radius: var(--global-border-radius-large, #{$global-border-radius-large}) var(--global-border-radius-large, #{$global-border-radius-large}) 0 0; + overflow: hidden; + + &, + &::after { + // prettier-ignore + border-radius: var(--global-border-radius-large, #{$global-border-radius-large}) var(--global-border-radius-large, #{$global-border-radius-large}) 0 0; + } + + @media (--target-desktop) { + &::after { + --global-transition-property: background-color; + transition: var(--global-transition, opacity 0.1s ease-in); + + .horizontal-poster-lockup:hover &, + .horizontal-poster-lockup:focus &, + .horizontal-poster-lockup:focus-within & { + background-color: var(--lockupHoverBGColor); + } + } + } + + // + // Webkit Box Reflect chins + // + @supports (-webkit-box-reflect: inherit) { + -webkit-box-reflect: below; + overflow: visible; + + &::after { + box-shadow: none; + } + } + } + + //Revisit for potential clean up + .artwork-component__contents { + border-radius: inherit; + transition: var(--global-transition, opacity 0.1s ease-in); + } + + .artwork-component__image { + height: var(--artwork-override-height, auto); + width: var(--artwork-override-width, 100%); + max-width: var(--artwork-override-max-width, none); + min-width: var(--artwork-override-min-width, 0); + min-height: var(--artwork-override-min-height, 0); + max-height: var(--artwork-override-max-height, none); + display: block; + object-fit: var(--artwork-override-object-fit, fill); + object-position: var(--artwork-override-object-position, center); + } + + .artwork-component:not(.artwork-component--downloaded), + // If image doesn't download/render, on error, show JoeColor in placeholders. + // .artwork-component--feature-recommended, + .artwork-component--error { + background-color: var( + --override-placeholder-bg-color, + var(--placeholder-bg-color, var(--genericJoeColor)) + ); + // for generic joe color - it provides light/dark mode. + &[style*='#ebebeb'] { + @media (prefers-color-scheme: dark) { + // Force Dark Generic joeColor for dark mode + background-color: swatch(genericJoeColor, dark); + } + } + } + + // Dynamic aspect ratios + // Create placeholders with aspect-ratio derived from `artwork-profiles.js` + // https://github.com/thierryk/aspect-ratio-via-css/tree/master/aspect-ratio-via-class-selector + // + // Apply aspect ratio to `1x1` `src` placeholders. Once downloaded, the placeholder aspect ratio is no longer needed. + // + .artwork-component--aspect-ratio:not(.artwork-component--downloaded), + // If image doesn't download/render, on error, show aspect-ratio placeholders instead. + .artwork-component--error { + // Placeholder `src` may have different aspect ratio. Hide overflow in that case. + overflow: hidden; + + &::before, + &::after { + content: ''; + display: block; + // prettier-ignore + padding-bottom: calc(100% / var(--shelf-aspect-ratio, var(--aspect-ratio))); + // Prevent distortion of overlaid border from additional padding + box-sizing: border-box; + } + + &::after { + position: absolute; + // No `min-height: 100%` on border overlay when generating aspect-ratio placeholder. + min-height: 0; + } + + // `img` may not always be the first-child. Can be an svg or another container. + > :global(:first-child), + > :global(noscript) > :global(:first-child) { + position: absolute; + width: var(--artwork-override-width, 100%); + height: var(--artwork-override-height, 100%); + max-width: var(--artwork-override-max-width, none); + min-width: var(--artwork-override-min-width, 0); + min-height: var(--artwork-override-min-height, 0); + max-height: var(--artwork-override-max-height, none); + top: 50%; + left: 50%; // RTL not needed + transform: translateY(-50%) translateX(-50%); // RTL not needed + z-index: var(--z-default); + } + + > :global(img), + > :global(noscript) > :global(img) { + height: auto; + min-height: var(--artwork-override-min-height, 0); + } + } + + // Full width (`forceFullWidth`) sizing is default, since most artwork are in responsive lockups. + // Avoid using `--artwork-override-width` or `--artwork-override-height` with `forceFullWidth` property enabled. + .artwork-component--fullwidth { + &, + > :global(noscript) { + width: 100%; + } + + > :global(noscript > picture .artwork-component__image) { + width: 100%; + height: auto; + + &::after { + width: 100%; + display: block; + content: ''; + } + } + } +</style> diff --git a/shared/components/src/components/Artwork/constants.ts b/shared/components/src/components/Artwork/constants.ts new file mode 100644 index 0000000..7fd6564 --- /dev/null +++ b/shared/components/src/components/Artwork/constants.ts @@ -0,0 +1,227 @@ +/** + * COPIED FROM: https://github.pie.apple.com/amp-ui/ember-ui-media-artwork/blob/main/addon/utils/srcset.js + * and converted public functions to TypeScript + */ + +import type { CropCode, FileExtension } from './types'; + +const baseWidthHeightRegex = '({w}|[0-9]+)x({h}|[0-9]+)'; +const baseFileTypeRegex = '{f}|([a-zA-Z]{3,4})'; +// ([A-z]{1,6}\\.[\\w]{1,8}) - copy pasta of the regex used on the backend for EffectIds +// https://github.pie.apple.com/amp/ai-imageservice/blob/84abff624a2da5b45bdf91c5bcd87b6708ad12ae/is-foundation/src/main/java/com/apple/imageservice/foundation/program/EffectId.java#L22 +const baseEffectCropCode = '[A-z]{1,6}\\.[\\w]{1,8}'; + +export const EMBEDDED_CROP_CODE_REGEX = new RegExp( + `^${baseWidthHeightRegex}([a-zA-Z]+)`, +); +export const FILE_TYPE_REGEX = new RegExp(baseFileTypeRegex); +// TODO: rdar://97913309 (JMOTW: Artwork: Quality Param regex injects quality placeholder when no hardcoded quality param exists) +export const QUALITY_PARAM_REGEX = /(-[0-9]+)?\.(\{f\}|[A-z]{2,4})$/; + +export const EFFECT_ID_REGEX = new RegExp( + `^${baseWidthHeightRegex}(${baseEffectCropCode})\\.(${baseFileTypeRegex})`, +); + +// non capturing to ignore either effect cc or regular cc +export const REPLACE_CROP_CODE_REGEX = new RegExp( + `${baseWidthHeightRegex}(?:${baseEffectCropCode}|[a-z]{1,2})\\.(${baseFileTypeRegex})`, +); + +export const DEFAULT_QUALITY = 60; + +// Specific viewport widths that don't align cleanly with media query breakpoints +export const LN_TALL_BREAKPOINT_WIDTH = 729; +export const ARTIST_VIDEO_TALL_BREAKPOINT_WIDTH = 674; + +/** + * Instead of reading pixel density (which is different in fastboot and browser), + * we'll bake in support for 1x and 2x pixel densities. This means a larger + * set of sources, but it means we don't have to recalculate and potentially double + * download images. + * @export const PIXEL_DENSITIES + * @private + */ +export const PIXEL_DENSITIES = [1, 2]; + +/** + * default cropcode if none is provided + */ +export const DEFAULT_CROP: CropCode = 'fa'; + +/** + * default fileType if none is provided + */ +export const DEFAULT_FILE_TYPE: FileExtension = 'jpg'; + +export const ASPECT_RATIOS = { + HD: 16 / 9, + ONE_THIRD: 3 / 1, + ONE: 1, + THREE_QUARTERS: 3 / 4, + UBER: 4, + HD_ASPECT_RATIO: 16 / 9, + VIDEO_LIST: 7 / 4, + VIDEO_TALL: 9 / 16, + HERO: 68 / 39, + SUPER_HERO_WIDE: 22 / 9, + WELCOME: 466 / 293, + EDITORIAL_DEFAULT: 68 / 39, +} as const; + +export const FILE_EXTENSIONS = ['jpg', 'webp', 'png'] as const; + +export const FILE_TO_MIME_TYPE = { + jpg: 'image/jpeg', + png: 'image/png', + webp: 'image/webp', +} as const; + +// https://confluence.sd.apple.com/pages/viewpage.action?spaceKey=AMPDSCE&title=Crop+Code+Master+List +export const ALL_CROP_CODES = [ + '{c}', + 'at', + 'ac', + 'bb', + 'bw', + 'bf', + 'br', + 'h', + 'w', + 'cc', + 'cx', + 'ca', + 'cb', + 'cw', + 'cu', + 'cy', + 'cv', + 'rc', + 'rs', + 'sr', + 'ss', + 'fa', + 'fb', + 'fc', + 'fd', + 'fe', + 'ff', + 'fg', + 'fh', + 'fi', + 'fj', + 'fk', + 'fl', + 'fm', + 'fn', + 'fo', + 'fp', + 'fq', + 'fr', + 'fs', + 'ft', + 'fu', + 'fv', + 'fw', + 'fx', + 'fy', + 'ea', + 'eb', + 'ec', + 'ed', + 'ee', + 'ef', + 'eg', + 'eh', + 'ei', + 'ej', + 'ek', + 'el', + 'em', + 'en', + 'eo', + 'ep', + 'eq', + 'er', + 'es', + 'et', + 'eu', + 'ev', + 'ew', + 'ex', + 'ey', + 'ez', + 'ga', + 'gb', + 'gc', + 'lg', + 'lw', + 'lc', + 'ld', + 'la', + 'lb', + 'lt', + 'lh', + 'mv', + 'mw', + 'mf', + 'nr', + 'sy', + 'sx', + 'sz', + 'sa', + 'sb', + 'sc', + 'sd', + 'se', + 'sf', + 'sg', + 'sh', + 'si', + 'sj', + 'sk', + 'va', + 'vb', + 'vc', + 'vd', + 've', + 'vf', + 'vi', + 'vj', + 'vl', + 'wp', + 'wa', + 'wb', + 'wc', + 'wd', + 'we', + 'wf', + 'wg', + 'wv', + 'wx', + 'wy', + 'wz', + 'ta', + 'tb', + 'tc', + 'td', + 'oa', + 'ob', + 'oc', + 'od', + 'oe', + 'of', + 'og', + 'oh', + 'Sports.TVAGPW01', + 'Sports.SS1x101', + 'PH.WSAHS01', +] as const; + +const isLoadingAvailable = + typeof HTMLImageElement !== 'undefined' && + 'loading' in HTMLImageElement.prototype; + +export const shouldUseLazyLoader = + typeof window !== 'undefined' && + window.IntersectionObserver && + !isLoadingAvailable; diff --git a/shared/components/src/components/Artwork/loaders/LazyLoader.svelte b/shared/components/src/components/Artwork/loaders/LazyLoader.svelte new file mode 100644 index 0000000..1857c7b --- /dev/null +++ b/shared/components/src/components/Artwork/loaders/LazyLoader.svelte @@ -0,0 +1,89 @@ +<!-- + LazyLoader Component + This component provides loading="lazy" + functionality for browsers that do not support it. + It uses Intersection Observers to evaluate + if an image needs to be loaded. + + DO NOT USE DIRECTLY use LoaderSelector +--> +<script context="module" lang="ts"> + import { get } from 'svelte/store'; + import { shouldUseLazyLoader } from '@amp/web-app-components/src/components/Artwork/constants'; + import { createArtworkLoaderStore } from '@amp/web-app-components/src/components/Artwork/stores/artworkLoader'; + import type { ArtworkLoaderStore } from '@amp/web-app-components/src/components/Artwork/stores/artworkLoader'; + import { getRafQueue } from '@amp/web-app-components/src/utils/rafQueue'; + + const rafQueue = getRafQueue(); + + let artworkLookupTable: ArtworkLoaderStore | null = null; + let observer: IntersectionObserver | null = null; + + const setupObserver = () => { + let options = { + root: null, // go off viewport + rootMargin: '0px', + threshold: 0.0, + }; + + return new IntersectionObserver((entries) => { + entries.forEach((item) => { + rafQueue.add(() => { + const storeValue = get(artworkLookupTable); + const isItemAlreadyVisible = storeValue.get(item.target); + if (!isItemAlreadyVisible) { + artworkLookupTable.addEntry( + item.target, + item.isIntersecting, + ); + } + }); + }); + }, options); + }; + if (shouldUseLazyLoader) { + observer = setupObserver(); + artworkLookupTable = createArtworkLoaderStore(); + } +</script> + +<script lang="ts"> + import { onDestroy } from 'svelte'; + + let isSubscribed = false; + + let container: Element; + let isVisible: boolean = false; + let unsubscribeToStore: () => void = () => {}; + + const cleanup = () => { + unsubscribeToStore(); + observer.unobserve(container); + artworkLookupTable.cleanupEntry(container); + }; + + $: { + if (isVisible && isSubscribed) { + cleanup(); + isSubscribed = false; + } + } + + export function onSlotMount(artworkComponent: Element) { + container = artworkComponent; + isSubscribed = true; + observer.observe(container); + + unsubscribeToStore = artworkLookupTable.subscribe((map) => { + isVisible = map.get(container); + }); + } + + onDestroy(() => { + if (isSubscribed) { + cleanup(); + } + }); +</script> + +<slot {isVisible} /> diff --git a/shared/components/src/components/Artwork/loaders/LoaderSelector.svelte b/shared/components/src/components/Artwork/loaders/LoaderSelector.svelte new file mode 100644 index 0000000..1d97814 --- /dev/null +++ b/shared/components/src/components/Artwork/loaders/LoaderSelector.svelte @@ -0,0 +1,38 @@ +<script context="module" lang="ts"> + export const LOADER_TYPE = { + LAZY: 'LAZY', + NONE: 'NONE', + } as const; +</script> + +<script lang="ts"> + import LazyLoader from '@amp/web-app-components/src/components/Artwork/loaders/LazyLoader.svelte'; + import NoLoader from '@amp/web-app-components/src/components/Artwork/loaders/NoLoader.svelte'; + import { shouldUseLazyLoader } from '@amp/web-app-components/src/components/Artwork/constants'; + import type { ValueOf } from '@amp/web-app-components/src/types'; + import type { SvelteComponent } from 'svelte'; + + type LoaderOptions = ValueOf<typeof LOADER_TYPE>; + + export let loaderType: LoaderOptions = LOADER_TYPE.LAZY; + + interface LoaderComponent extends SvelteComponent { + onSlotMount: (component: Element) => void; + } + + let currentComponent: LoaderComponent; + + export function onSlotMount(component: Element) { + currentComponent.onSlotMount(component); + } +</script> + +{#if loaderType === LOADER_TYPE.LAZY && shouldUseLazyLoader} + <LazyLoader bind:this={currentComponent} let:isVisible + ><slot {isVisible} /></LazyLoader + > +{:else} + <NoLoader bind:this={currentComponent} let:isVisible + ><slot {isVisible} /></NoLoader + > +{/if} diff --git a/shared/components/src/components/Artwork/loaders/NoLoader.svelte b/shared/components/src/components/Artwork/loaders/NoLoader.svelte new file mode 100644 index 0000000..b453e03 --- /dev/null +++ b/shared/components/src/components/Artwork/loaders/NoLoader.svelte @@ -0,0 +1,20 @@ +<!-- + NoLoader Component + This component should be used when loading="lazy" + is supported. + + DO NOT USE DIRECTLY use LoaderSelector +--> +<script lang="ts"> + let mounted = false; + + export function onSlotMount(_artworkComponent: Element) { + mounted = true; + } + + const ssr = typeof window === 'undefined'; + + $: isVisible = mounted || ssr; +</script> + +<slot {isVisible} /> diff --git a/shared/components/src/components/Artwork/stores/artworkLoader.ts b/shared/components/src/components/Artwork/stores/artworkLoader.ts new file mode 100644 index 0000000..0d7116a --- /dev/null +++ b/shared/components/src/components/Artwork/stores/artworkLoader.ts @@ -0,0 +1,30 @@ +import { writable } from 'svelte/store'; +import type { Writable } from 'svelte/store'; + +export type ArtworkLoaderStore = { + subscribe: Writable<WeakMap<Element, boolean>>['subscribe']; + addEntry: (entry: Element, isVisible: boolean) => void; + cleanupEntry: (entry: Element) => void; +}; + +export function createArtworkLoaderStore(): ArtworkLoaderStore { + const value = new WeakMap(); + const { subscribe, update } = writable(value); + + return { + subscribe, + addEntry: (entry: Element, isVisible: boolean) => { + update((map) => { + map.set(entry, isVisible); + return map; + }); + }, + + cleanupEntry: (entry: Element) => { + update((map) => { + map.delete(entry); + return map; + }); + }, + }; +} diff --git a/shared/components/src/components/Artwork/utils/artProfile.ts b/shared/components/src/components/Artwork/utils/artProfile.ts new file mode 100644 index 0000000..fccd4e5 --- /dev/null +++ b/shared/components/src/components/Artwork/utils/artProfile.ts @@ -0,0 +1,77 @@ +import type { + Profile, + ImageURLParams, + CropCode, +} from '@amp/web-app-components/src/components/Artwork/types'; +import { ArtworkConfig } from '@amp/web-app-components/config/components/artwork'; + +const ARTWORK_IDENTIFIERS = [ + 'xlarge', + 'large', + 'medium', + 'small', + 'xsmall', +] as const; + +function getArtworkProfile(profile: Profile | string): Profile { + const { PROFILES } = ArtworkConfig.get(); + const selectedProfile: Profile = + typeof profile === 'string' ? PROFILES.get(profile) : profile; + // TODO: add validation + warning / error handling for profiles + // rdar://76365525 (Artwork Component: add validation + warning / error handling for profiles) + return selectedProfile; +} + +function buildImgDimensions( + width: number, + aspectRatio: number, + crop: CropCode, +): Partial<ImageURLParams> { + const dimensions = { + width, + height: Math.round(width * (1 / aspectRatio)), + crop, + }; + + return dimensions; +} + +export type ConvertedProfile = { + [key in (typeof ARTWORK_IDENTIFIERS)[number]]?: ImageURLParams; +}; + +export const getAspectRatio = (profile: Profile | string): number => { + const [, aspectRatio] = getArtworkProfile(profile); + return aspectRatio === null ? null : aspectRatio; +}; + +type ImageTagWidthHeight = { width: number; height: number }; +export const getImageTagWidthHeight = ( + profile: Profile | string, +): ImageTagWidthHeight => { + const [imageSize, aspectRatio] = getArtworkProfile(profile); + const width = imageSize[0]; + return { + width, + height: Math.floor(width / aspectRatio), + }; +}; + +export const getDataFromProfile = ( + profile: Profile | string, +): ConvertedProfile => { + const selectedProfile = getArtworkProfile(profile); + + const [widths, aspectRatio, crop] = selectedProfile; + + const imgDimensions = widths.reduce((acc, w, indx) => { + acc[ARTWORK_IDENTIFIERS[indx]] = buildImgDimensions( + w, + aspectRatio, + crop, + ); + return acc; + }, {}); + + return imgDimensions; +}; diff --git a/shared/components/src/components/Artwork/utils/preconnect.ts b/shared/components/src/components/Artwork/utils/preconnect.ts new file mode 100644 index 0000000..652a9a8 --- /dev/null +++ b/shared/components/src/components/Artwork/utils/preconnect.ts @@ -0,0 +1,64 @@ +import { getContext } from 'svelte'; + +const CONTEXT_NAME = 'shared-components:preconnect-tracker'; + +/** + * Setup a PreconnectTracker used by <Artwork> and <MotionVideo>. + * This keeps track of the origins of rendered assets to generate the + * appropriate <link rel="preconnect"> tags. + * + * Preconnect tags should be rendered by placing a <Preconnects /> at the + * bottom of the top level <App> component. + */ +export class PreconnectTracker { + private readonly originsSet: Set<string>; + + /** + * Add a new PreconnectTracker to the Svelte context. + * This should only be called on the server. The components will no-op when + * run clientside (if this isn't called). + */ + static setup(context: Map<string, unknown>): PreconnectTracker { + const tracker = new PreconnectTracker(); + context.set(CONTEXT_NAME, tracker); + return tracker; + } + + private constructor() { + this.originsSet = new Set(); + } + + /** + * Track a URL of an asset for preconnect origin aggregation. + * This should only be called from `<Artwork>` and `<MotionVideo>`. + */ + trackUrl(url: string): void { + try { + const { origin } = new URL(url); + this.originsSet.add(origin); + } catch (_) { + // Just in case the URL parsing fails + // Worst case this misses a preconnect. We'd rather it not take + // down the whole component. + } + } + + /** + * The current list of origins of all rendered <Artwork> and <MotionVideo> + * components. + */ + get origins(): string[] { + return [...this.originsSet]; + } +} + +/** + * Gets the current PreconnectTracker instance from the Svelte context. + * + * @return locale The current instance of Locale + */ +export function getPreconnectTracker(): PreconnectTracker | undefined { + // We intentionally allow this to be missing. In the browse, we want this + // since preconnects are only needed for SSR. + return getContext(CONTEXT_NAME) as PreconnectTracker | undefined; +} diff --git a/shared/components/src/components/Artwork/utils/replaceQualityParam.ts b/shared/components/src/components/Artwork/utils/replaceQualityParam.ts new file mode 100644 index 0000000..81c971a --- /dev/null +++ b/shared/components/src/components/Artwork/utils/replaceQualityParam.ts @@ -0,0 +1,66 @@ +import { QUALITY_PARAM_REGEX } from '@amp/web-app-components/src/components/Artwork/constants'; + +/** + * Utility function that handles the replacement of quality value. + * Does not add any values to the URL string. Just replaces any hardcoded values + * with the quality placeholder. + * + * @param url image url + * @param quality quality value + * @returned url and the defaultQuality from URL + */ +// eslint-disable-next-line import/prefer-default-export +export function replaceQualityParam( + url: string, + quality?: number, +): [string, string] { + const hasQualityPlaceholder = /-\{q\}/.test(url); + // Convert url string to URL object + // Some image URLs, like those for radio stations that are formatted with effect codes, + // may have query params in the path which are used to build out the image with other + // images/effects. Ensure we only modify the image path and not the query params. + const urlObj = new URL(url); + + // Split URL.pathname into parts, so we are only modifying the very last portion of the path + const lastURLPartIdx = urlObj.pathname.lastIndexOf('/'); + const firstURLpart = urlObj.pathname.substring(0, lastURLPartIdx); + let lastURLpart = decodeURI(urlObj.pathname.substring(lastURLPartIdx)); + + let defaultQuality = ''; + + if (quality && !hasQualityPlaceholder) { + // Find an optional hardcoded quality value (e.g. `-80`) + // And then find the `.` and fileType placeholder (ext) + lastURLpart = lastURLpart.replace( + QUALITY_PARAM_REGEX, + (_match, defaultQualityVal: string, fileType: string) => { + // only pass update defaultQuality if it exists in the URL + defaultQuality = defaultQualityVal + ? defaultQualityVal.replace('-', '') + : defaultQuality; + + return `-{q}.${fileType}`; + }, + ); + } else if (!quality && hasQualityPlaceholder) { + // Strip quality param + lastURLpart = lastURLpart.replace('-{q}', ''); + } + + // Update urlObj with our modified pathname parts and then combine all + // parts into a final string. + urlObj.pathname = `${firstURLpart}${lastURLpart}`; + let updatedURL = urlObj.toString(); + + // Need to decode the URL string conversion to preserve curley braces in URL string. + // Only decoding the last part of the URL, in the event that there may be intentionally + // escaped characters in other parts of the URL. + // + // With decode: .../mza_4812113047298400850.png/{w}x{h}AM.RSMA01.jpg + // Without decode: .../mza_4812113047298400850.png/%7Bw%7Dx%7Bh%7DAM.RSMA01.jpg + updatedURL = `${updatedURL.substring(0, lastURLPartIdx)}${decodeURI( + updatedURL.substring(lastURLPartIdx), + )}`; + + return [updatedURL, defaultQuality]; +} diff --git a/shared/components/src/components/Artwork/utils/srcset.ts b/shared/components/src/components/Artwork/utils/srcset.ts new file mode 100644 index 0000000..8f419cb --- /dev/null +++ b/shared/components/src/components/Artwork/utils/srcset.ts @@ -0,0 +1,467 @@ +/** + * COPIED FROM: https://github.pie.apple.com/amp-ui/ember-ui-media-artwork/blob/957fc3e586d4ff710b2263a45d8950d4ee65616a/addon/utils/srcset.js + * and converted to TypeScript + */ +import { replaceQualityParam } from '@amp/web-app-components/src/components/Artwork/utils/replaceQualityParam'; +import { + DEFAULT_FILE_TYPE, + DEFAULT_QUALITY, + PIXEL_DENSITIES, + EMBEDDED_CROP_CODE_REGEX, + EFFECT_ID_REGEX, + FILE_TYPE_REGEX, +} from '@amp/web-app-components/src/components/Artwork/constants'; +import { ArtworkConfig } from '@amp/web-app-components/config/components/artwork'; +import { memoize } from '@amp/web-app-components/src/utils/memoize'; +import { getDataFromProfile } from '@amp/web-app-components/src/components/Artwork/utils/artProfile'; +import type { MediaConditions } from '@amp/web-app-components/src/utils/getMediaConditions'; +import { getMediaConditions } from '@amp/web-app-components/src/utils/getMediaConditions'; +import type { + FileExtension, + Artwork, + ArtworkMaxSizes, + ImageSettings, + ImageURLParams, + Profile, + CropCode, + ChinConfig, +} from '@amp/web-app-components/src/components/Artwork/types'; +import type { Size } from '@amp/web-app-components/src/types'; + +type ProfileConfig = { + width: number; + height: number; + crop: CropCode; +}; +type SizeMap = { + [key in Size]?: ProfileConfig; +}; + +const isAFillCropCode = (crop: CropCode) => crop === 'bf'; + +const getSmallestProfileSize = (sizeMap: SizeMap) => { + const { xlarge, large, medium, small, xsmall } = sizeMap; + return xsmall || small || medium || large || xlarge; +}; + +const filterSizeConfig = ( + config: ProfileConfig, + maxWidth: number | null, +): boolean => (maxWidth ? config.width <= maxWidth : true); + +const getSizesAndBreakpoints = ( + profile: Profile | string, +): [SizeMap, MediaConditions] => { + const { BREAKPOINTS } = ArtworkConfig.get(); + const profileSize = profile ? getDataFromProfile(profile) : {}; + + const mediaConditions = getMediaConditions(BREAKPOINTS); + const SIZES = Object.keys(mediaConditions); + // TODO: rdar://76402413 (Convert imperative reduce pattern + // to functionalwith Object.fromEntries once on Node 12) + const sizeMap: SizeMap = SIZES.reduce((accumulator, sizeName) => { + // only add to size map if + // profile exists for mediaCondition + + if (profileSize[sizeName]) { + const imageWidth = profileSize[sizeName].width; + const imageHeight = profileSize[sizeName].height; + const imageCrop = profileSize[sizeName].crop; + + accumulator[sizeName] = { + width: imageWidth, + height: imageHeight, + crop: imageCrop, + }; + } + + return accumulator; + }, {}); + + return [sizeMap, mediaConditions]; +}; + +function deriveUrlParamsArray( + urlParams: Partial<ImageURLParams>, + profile: Profile | string, + maxWidth: number, +): ImageURLParams[] { + const [profileBySize] = getSizesAndBreakpoints(profile); + + let filteredSizes = Object.values(profileBySize).filter((config) => + filterSizeConfig(config, maxWidth), + ); + + // if image is smaller than all profile sizes + // use the smallest profile size available + if (filteredSizes.length === 0) { + const smallestProfile = getSmallestProfileSize(profileBySize); + filteredSizes = [smallestProfile]; + } + + return filteredSizes.map((viewportProfile) => ({ + crop: viewportProfile.crop, + width: viewportProfile.width, + height: viewportProfile.height, + quality: urlParams.quality, + fileType: urlParams.fileType, + })); +} + +/** + * Converts Artwork object to expected input for image src functions. + * @param artwork Artwork object + * @param quality image quality value + * @param fileType file type + * @param chinConfig chin configuration object + */ +function deriveDataFromArtwork( + artwork: Artwork, + quality?: number, + fileType?: FileExtension, + chinConfig?: ChinConfig, +): [string, Partial<ImageURLParams>, ArtworkMaxSizes] { + const { width, height, template } = artwork; + const chinHeight = chinConfig?.height ?? 0; + + const urlParams: Partial<ImageURLParams> = { + fileType, + quality, + }; + + const ogImageSizes: ArtworkMaxSizes = { + maxHeight: height + chinHeight, + maxWidth: width, + }; + + return [template, urlParams, ogImageSizes]; +} + +/** + * Removes embedded crop codes if: + * 1. a `crop` is passed (i.e. if a user passed a crop code in the invocation of + * the outer function) + * 2. the rawURL has an embedded crop code that is not an Effect ID + * + * Exception to #2 is when using an image with an Effect ID that is being used to create + * a chin blur (i.e. chins in Power Swoosh lockups). This is a special case so we can + * have the blur effect visible in Chrome. + * + * Under these conditions the fileType is also removed, but it's not clear why. + * + * @public + * @param rawURL + * @param crop + * @param replaceEffectCode + */ +export function fixEmbeddedCropCode( + rawURL: string, + crop: string, + replaceEffectCode = false, +): string { + // Normalize URL in case crop or format are hardcoded + // Test against only the filename portion + const stringParts = rawURL.split('/'); + const fileName = stringParts.pop(); + let url = rawURL; + + const cropMatches = fileName.match(EMBEDDED_CROP_CODE_REGEX); + + // The last match will be the hard-coded crop code or the replacement indicator: {c} + const cropMatch = cropMatches ? cropMatches.pop() : null; + + // EffectIds (e.g. SH.FPESS01) are the new artwork crop codes + // that should not be replaced in the artwork url excpet when used + // for chin blurs. + const isEffectMatch = !replaceEffectCode && EFFECT_ID_REGEX.test(fileName); + + if (crop && cropMatch && !isEffectMatch) { + // Update the url to include the replacement indicator {c} instead of the hard-coded crop value + // Also update the URL to include the replacement indicator {f} if the file type is hard-coded + const updatedFilename = replaceEffectCode + ? // EFFECT_ID_REGEX also captures file type + fileName.replace(EFFECT_ID_REGEX, '$1x$2{c}.{f}') + : fileName + .replace(EMBEDDED_CROP_CODE_REGEX, '$1x$2{c}') + .replace(FILE_TYPE_REGEX, '{f}'); + + url = `${stringParts.join('/')}/${updatedFilename}`; + } + + return url; +} + +/** + * @private + * Utility for build src for images + * @param url template url for an image + * @param urlParams + * @param options + * @param chinConfig optional chin configuration for style parameter + */ +export function buildSrc( + url: string, + urlParams: ImageURLParams, + options: ImageSettings, + chinConfig?: ChinConfig, +): string | null { + if (!url) return null; + + let returnedUrl = url; + + const { width, height, quality, crop, fileType } = urlParams; + + if (options?.forceCropCode !== false) { + returnedUrl = fixEmbeddedCropCode(returnedUrl, crop); + } + const [parsedURL, defaultQuality] = replaceQualityParam( + returnedUrl, + quality, + ); + returnedUrl = parsedURL; + + const qualityValue = Number.isInteger(quality) + ? quality.toString() + : defaultQuality; + + let finalUrl = returnedUrl + .replace('{w}', width?.toString()) + .replace('{h}', height?.toString()) + .replace('{c}', crop) + .replace('{q}', qualityValue) + .replace('{f}', fileType); + + // Add style query parameter for chin effects if specified + if (chinConfig?.style) { + const separator = finalUrl.includes('?') ? '&' : '?'; + finalUrl += `${separator}style=${chinConfig.style}`; + } + + return finalUrl; +} + +/** + * Wrapper for buildSrc helper + * - Preserves effect ids in urls used for SEO + * @param {string} url + * @param {ImageURLParams} urlParams + * @return string | null + */ +export function buildSrcSeo( + url: string, + urlParams: ImageURLParams, +): string | null { + const options = { ...urlParams }; + + // Preserve effect ids when generating seo image urls + if (EFFECT_ID_REGEX.test(url)) { + delete options.crop; + } + + return buildSrc(url, options, {}); +} + +/** + * This function generates a value for the `srcset` attribute + * based on a URL and image options. + * + * @private + * @param rawURL The raw URL + * @param urlParams custom image parameters + * @param pixelDensity pixel density to optimize for + * @param options k/v map of other constant options that don't depend on viewport size. + * @return The `srcset` attribute value + * @public + */ +function buildSingleSrcset( + rawURL: string, + urlParams: ImageURLParams, + artworkSizes: ArtworkMaxSizes, + pixelDensity: number, + options: ImageSettings, + chinConfig?: ChinConfig, +): string { + const { maxWidth } = artworkSizes; + const profileHeight = urlParams.height; + const profileWidth = urlParams.width; + const chinHeight = chinConfig?.height ?? 0; + + const calculatedWidth = Math.ceil(profileWidth * pixelDensity); + const { crop } = urlParams; + + // use profile width if maxWidth is null or 0 + // TODO: rdar://92133085 (Add logging to shared components) + const artworkMaxWidth = maxWidth || calculatedWidth; + + // prevent pixel dense images from being wider + // than the OG size of the image + // unless its using a fill + const width = isAFillCropCode(crop) + ? calculatedWidth + : Math.min(calculatedWidth, artworkMaxWidth); + const height = + Math.round((width * profileHeight) / profileWidth) + + Math.round(chinHeight * pixelDensity); + + const passedOptions = options; + + const fixedUrlParams = { + ...urlParams, + crop, + width, + height, + }; + + const url = buildSrc(rawURL, fixedUrlParams, passedOptions, chinConfig); + + return `${url} ${fixedUrlParams.width}w`; +} + +/** + * Returns a string that can be used as the value for the srcset attribute. + * + * @function buildResponsiveSrcset + * @param urlParams list of `urlOptions`. See `buildSrcset` for details. + * @param options some other options to opt into behavior. See `buildSrcset` for details. + * @returns srcset string + */ +export function buildResponsiveSrcset( + url: string, + urlParams: Partial<ImageURLParams>, + profile: Profile | string, + artworkSizes: ArtworkMaxSizes, + options: ImageSettings, + chinConfig?: ChinConfig, +): string { + const urlParamsArray = deriveUrlParamsArray( + urlParams, + profile, + artworkSizes.maxWidth, + ); + const DEFAULT_OPTIONS: Partial<ImageSettings> = { + forceCropCode: false, + }; + const { + pixelDensities = PIXEL_DENSITIES, + ...optionsWithoutPixelDensities + } = options; + + // merging custom options with defaults + const finalOptions: ImageSettings = { + ...DEFAULT_OPTIONS, + ...optionsWithoutPixelDensities, + }; + + // using a Set to prevent multiple of the same srcs being added. + const srcSetStrings = new Set(); + + // eslint-disable-next-line no-restricted-syntax + for (const pixelDensity of pixelDensities) { + // eslint-disable-next-line no-restricted-syntax + for (const singleURLParam of urlParamsArray) { + srcSetStrings.add( + buildSingleSrcset( + url, + singleURLParam, + artworkSizes, + pixelDensity, + finalOptions, + chinConfig, + ), + ); + } + } + return [...srcSetStrings].join(','); +} + +/** + * get size attributes based on breakpoints. + * @param width width of image + * @param height height of image + * @param imageMultipler custom multipler to use for image sizes + */ + +function imageSizes( + profile?: Profile | string, + maxWidth: number = null, +): string { + const [sizeMap, mediaConditions] = getSizesAndBreakpoints(profile); + + const filteredSizes = Object.entries(sizeMap).filter(([, config]) => + filterSizeConfig(config, maxWidth), + ); + + const sizes = filteredSizes.map(([sizeName, config], index, arr) => { + let condition = mediaConditions[sizeName]; + const { width } = config; + const widthString = `${width}px`; + const isFirst = index === 0; + const isLast = index === arr.length - 1; + + // The smallest size in the 'sizes' attribute shouldn't have a min size + // or it will cause anything below that size to default + // to the last size (aka the largest image). + if (isFirst) { + const conditions = condition.split('and'); + if (conditions.length > 1) { + const [, maxCondition] = conditions; + condition = maxCondition; + } + } + if (isLast) { + // The last size in the `sizes` attr should not contain the media condition + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes + return widthString; + } + + // Creates an option like this: + // (min-width: something) 111px; + return `${condition} ${widthString}`; + }); + return sizes.length + ? sizes.join(',') + : `${getSmallestProfileSize(sizeMap).width}w`; +} + +export const getImageSizes = memoize(imageSizes); + +export function buildSourceSet( + artwork: Artwork, + options: ImageSettings, + profile: Profile | string, + chinConfig?: ChinConfig, +): string | null { + const fileType = options.fileType || DEFAULT_FILE_TYPE; + let qualityValue = options.quality || DEFAULT_QUALITY; + let sourceSet = null; + + const isWebp = fileType === 'webp'; + if (isWebp && qualityValue === DEFAULT_QUALITY) { + qualityValue = null; + } + + const [url, urlParams, maxSizes] = deriveDataFromArtwork( + artwork, + qualityValue, + fileType, + chinConfig, + ); + + if (url) { + // If the url doesn't have a {f} (file type) placeholder, we do not want + // to force webp sources. + const isNotWebpException = !(isWebp && !url.includes('{f}')); + if (isNotWebpException) { + sourceSet = buildResponsiveSrcset( + url, + urlParams, + profile, + maxSizes, + options, + chinConfig, + ); + } + } + + return sourceSet; +} diff --git a/shared/components/src/components/Artwork/utils/validateBackground.ts b/shared/components/src/components/Artwork/utils/validateBackground.ts new file mode 100644 index 0000000..42f6b7a --- /dev/null +++ b/shared/components/src/components/Artwork/utils/validateBackground.ts @@ -0,0 +1,16 @@ +const IS_RGB = /^rgba?\(\s*[\d.]+\s*%?\s*(,\s*[\d.]+\s*%?\s*){2,3}\)$/; +const IS_HEX = /^([0-9a-f]{3}){1,2}$/i; + +// eslint-disable-next-line import/prefer-default-export +export const deriveBackgroundColor = (str: string | null): string => { + const background = str?.replace('#', ''); + + if (IS_HEX.test(background)) { + return `#${background}`; + } + + if (IS_RGB.test(background)) { + return background; + } + return ''; +}; diff --git a/shared/components/src/components/Error/ErrorPage.svelte b/shared/components/src/components/Error/ErrorPage.svelte new file mode 100644 index 0000000..d459b4e --- /dev/null +++ b/shared/components/src/components/Error/ErrorPage.svelte @@ -0,0 +1,83 @@ +<script lang="ts"> + import Button from '@amp/web-app-components/src/components/buttons/Button.svelte'; + import { createEventDispatcher } from 'svelte'; + + const dispatch = createEventDispatcher(); + + interface ErrorUserInfo { + status: number; + } + + interface AppError { + message?: string; + isFirstPage?: boolean; + userInfo?: ErrorUserInfo; + statusCode?: number; + } + + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + + export let isRetryError: (error: AppError) => boolean = () => false; + + export let error: AppError | null = null; + export let errorLocKey: string | null = null; + + // podcasts-client-js can currently return a 204 if there is no content found. + // We want to treat this as a 204. If the following radar is ever addressed, + // we can remove the 204 conditional here: + // rdar://106657358 (Investigate if we can switch from 204 to 404s for network errors) + $: locKey = + errorLocKey || + (error?.userInfo?.status === 404 || + error?.message === '404' || + error?.statusCode === 404 || + error?.statusCode === 204 + ? 'AMP.Shared.Error.ItemNotFound' + : 'FUSE.Error.AnErrorOccurred'); + + function retry(): void { + dispatch('retryAction'); + } +</script> + +<!-- TODO: rdar://92841405 (JMOTW: Show error page when user has lost internet connection) --> +<div role="status" class="page-error"> + <h1 class="page-error__title" data-testid="page-error-title"> + {translateFn(locKey)} + </h1> + + {#if isRetryError(error)} + <Button buttonStyle="buttonB" on:buttonClick={retry}> + {translateFn('FUSE.Error.TryAgain')} + </Button> + {/if} +</div> + +<style lang="scss"> + .page-error { + --buttonTextColor: var(--systemSecondary); + --buttonBorderColor: var(--systemSecondary); + margin: auto; + padding: 0 25px; + max-width: 440px; + color: var(--systemSecondary); + position: absolute; + top: 50%; + left: 50%; // RTL not needed + transform: translate(-50%, -50%); + display: flex; + flex-direction: column; + align-items: center; + gap: 12px; + text-align: center; + z-index: var(--z-default); + } + + .page-error__title { + margin-bottom: 5px; + font: var(--title-2); + } +</style> diff --git a/shared/components/src/components/Footer/Footer.svelte b/shared/components/src/components/Footer/Footer.svelte new file mode 100644 index 0000000..82b0ff2 --- /dev/null +++ b/shared/components/src/components/Footer/Footer.svelte @@ -0,0 +1,195 @@ +<script lang="ts" context="module"> + export type Translate = ( + str: string, + options?: Record<string, string | number>, + ) => string; +</script> + +<script lang="ts"> + import type { FooterItem } from '@amp/web-app-components/src/components/Footer/types'; + /** + * Available CSS Vars: + * --footerBg + * + * StyleKit Vars: + * --keyColor + * --systemPrimary + * --systemSecondary + * --systemQuaternary + */ + + /** + * translate function provided by the parent app. + */ + export let translateFn: Translate; + /** + * A list of links to be in the footer + * @type {Array<FooterItem>} + */ + export let footerItems: FooterItem[]; + + const year = new Date().getFullYear().toString(); +</script> + +<footer data-testid="footer"> + <div class="footer-secondary-slot"> + <slot name="secondary-content" /> + </div> + + <div class="footer-contents"> + <p> + <span dir="ltr"> + <span dir="auto" + >{translateFn('AMP.Shared.Footer.CopyrightYear', { + year, + })}</span + > + <a + href={translateFn('AMP.Shared.Footer.Apple.URL')} + rel="noopener" + ><span dir="auto" + >{translateFn('AMP.Shared.Footer.Apple.Text')}</span + ></a + > + </span> + <span dir="auto" + >{translateFn('AMP.Shared.Footer.AllRightsReserved')}</span + > + </p> + <ul> + {#each footerItems as { url, locKey, id } (id)} + <li data-testid={id}> + <a href={translateFn(url)} rel="noopener" dir="auto"> + {translateFn(locKey)} + </a> + </li> + {/each} + </ul> + </div> +</footer> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/typography/specs' as *; + @use 'ac-sasskit/core/selectors' as *; + @use 'ac-sasskit/core/viewports' as *; + @use 'amp/stylekit/core/fonts' as *; + @use 'amp/stylekit/core/specs' as *; + @use 'amp/stylekit/modules/fontsubsets/core' as *; + @use '@amp/web-shared-styles/app/core/viewports' as *; + @use '@amp/web-shared-styles/app/core/globalvars' as *; + + $footer-height-sidebar-visible: 88px; + $footer-height-xsmall: 147px; + $footer-height-small: 88px; + $footer-vertical-padding-xsmall: var(--footerVerticalPadding, 15px); + $footer-vertical-padding-small: var(--footerVerticalPadding, 14px); + + footer { + flex-shrink: 0; + min-height: $footer-height-xsmall; + padding: $footer-vertical-padding-xsmall var(--bodyGutter); + background-color: var(--footerBg); + display: block; + + @include typespec(Footnote); + + // Footer.svelte should use viewport mixins for media queries + // this allows for cross compatibility with apps that may have + // differing xsmall vs small viewports set up + @include viewport('range:sidebar:hidden down') { + padding-bottom: $global-player-bar-height + + $footer-vertical-padding-xsmall; + } + + @include viewport(small) { + min-height: $footer-height-sidebar-visible; + padding-top: $footer-vertical-padding-small; + padding-bottom: $footer-vertical-padding-small; + + @include typespec(Subhead); + } + + @include viewport(xlarge) { + align-content: flex-start; + align-items: baseline; + display: var(--footerDisplay, flex); + justify-content: space-between; + } + + @include feature-detect(is-footer-hidden) { + display: none; + } + + // Hide Footer for Replay Highlights + :global(.maximize-content-area) & { + display: none; + } + } + + .footer-contents { + @include viewport(small) { + order: 1; + } + + p { + margin-bottom: 5px; + color: var(--systemSecondary); + } + + a { + --linkColor: var(--systemPrimary); + } + + ul { + display: flex; + flex-wrap: wrap; + } + + li { + display: inline-flex; + line-height: 1; + margin-top: 6px; + vertical-align: middle; + + a { + height: 100%; + padding-inline-end: 10px; + } + + &::after { + border-inline-start: 1px solid var(--systemQuaternary); + content: ''; + padding-inline-end: 10px; + } + + &:last-child::after { + content: none; + } + } + } + + .footer-secondary-slot { + --linkColor: var(--systemSecondary); + order: 1; + // Font subsets for Geos prevents `SF Pro` Web Font from being + // downloaded after `BlinkMacSystemFont` fails in Chrome. + font-family: font-family-locale(en-WW, geos); + + @each $lang, $font in font-family(geos) { + @if $lang != en-WW { + :global([lang]:lang(#{$lang})) & { + font-family: $font; + } + } + } + + @include viewport(small) { + order: 2; + } + + @include viewport('range:xsmall down') { + min-width: auto; + } + } +</style> diff --git a/shared/components/src/components/LineClamp/LineClamp.svelte b/shared/components/src/components/LineClamp/LineClamp.svelte new file mode 100644 index 0000000..9e4be3d --- /dev/null +++ b/shared/components/src/components/LineClamp/LineClamp.svelte @@ -0,0 +1,238 @@ +<script lang="ts" context="module"> + // A single observer is shared for all LineClamp instances for better performance. + // Using an observer also means recalculations are batched so layout only has to be + // recalculated once regardless of the number of instances of this component. + const resizeObserver = + typeof window !== 'undefined' && window.ResizeObserver + ? new window.ResizeObserver((entries) => { + for (const entry of entries) { + const contentHeight = Math.ceil(entry.contentRect.height); + const scrollHeight = Math.ceil(entry.target.scrollHeight); + const borderBoxHeight = Math.ceil( + entry.borderBoxSize[0].blockSize, + ); + + const style = getComputedStyle(entry.target); + + const lineHeight = parseInt( + style.getPropertyValue('line-height'), + ); + const multiline = contentHeight > lineHeight; + const multilineCount = contentHeight / lineHeight; + const truncated = scrollHeight > borderBoxHeight; + + const event = new CustomEvent<LineClampResizeDetail>( + 'lineClampResize', + { + detail: { + multiline, + multilineCount, + truncated, + }, + }, + ); + entry.target.dispatchEvent(event); + } + }) + : null; +</script> + +<script lang="ts"> + import { onMount, createEventDispatcher } from 'svelte'; + import { getRafQueue } from '@amp/web-app-components/src/utils/rafQueue'; + + /* + * Number of lines to clamp the container contents. + */ + export let clamp: number = 1; + + /** + * Whether the clamp container should be observed for multiline change events. + * + * Observed containers emit the `resize` event with event detail + * { multiline: boolean, truncated: boolean }. + * - multiline (boolean): whether the container is more than one line tall + * - truncated (boolean): whether the text is truncated + * + * This can be used for conditional styling of other clamp containers which + * may be allowed to expand if an adjacent container is only a single line. + */ + export let observe: boolean = false; + + /* + * Whether to allow focus indicators to overflow the container. + * + * Line clamping requires `overflow: hidden` in order to hide truncated contents. + * However, this will also clip focus indicators of elements inside the clamped + * container. Setting this to `true` allows focus indicators to overflow the + * clamped container while still hiding truncated contents. + * + * The amount of overflow bleed defaults to the Sass variable `$focus-size`, but + * can be adjusted using the CSS property `--overflowBleedSize`. + */ + export let allowFocusOverflow: boolean = false; + + /** + * Since slots are not able to be wrapped ( https://github.com/sveltejs/svelte/issues/5604) + * We use this prop to determine if the badge should be rendered. + */ + export let shouldRenderBadgeSlots: boolean = true; + + let clampElement: HTMLElement; + + let multiline: boolean = false; + let truncated: boolean = false; + + if (observe && resizeObserver) { + const dispatch = createEventDispatcher(); + const rafQueue = getRafQueue(); + + onMount(() => { + resizeObserver.observe(clampElement); + clampElement.addEventListener( + 'lineClampResize', + (e: CustomEvent<LineClampResizeDetail>) => { + dispatch('resize', e.detail); + + // Multiline/truncation state is used for badge positioning + if ($$slots.badge && shouldRenderBadgeSlots) { + rafQueue.add(() => { + multiline = e.detail.multiline; + truncated = e.detail.truncated; + }); + } + }, + ); + + return () => { + resizeObserver.unobserve(clampElement); + }; + }); + } +</script> + +<!-- svelte-ignore a11y-unknown-role --> +<div + class="multiline-clamp" + class:multiline-clamp--overflow={allowFocusOverflow} + class:multiline-clamp--multiline={multiline} + class:multiline-clamp--truncated={truncated} + class:multiline-clamp--with-badge={$$slots.badge && shouldRenderBadgeSlots} + style="--mc-lineClamp: var(--defaultClampOverride, {clamp});" + bind:this={clampElement} + role="text" +> + <!-- + NOTE: Any elements slotted here *must* have `display: inline`, + otherwise the clamping will not take effect! + + NOTE: In order for a multiline clamp with a badge to wrap correctly, + there must be *no whitespace* between the text element and badge + element. Otherwise, the badge will not "stick" to the last word, and + can end up wrapping onto its own line. + --> + <span class="multiline-clamp__text"><slot /></span + >{#if $$slots.badge && shouldRenderBadgeSlots}<span + class="multiline-clamp__badge"><slot name="badge" /></span + >{/if} +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/helpers' as *; + @use 'amp/stylekit/core/mixins/overflow-bleed' as *; + @use 'amp/stylekit/core/mixins/line-clamp' as *; + + // Line Clamp + // + // PUBLIC CSS PROPS + // + // *cssprop {Number} --overflowBleedSize + // *access public + // Size of overflow bleed used when component prop `allowFocusOverflow` + // is `true`. + // + // *cssprop {Number} --badgeSize + // *access public + // Size of badge placed in component's `badge` slot, used for positioning + // when the line clamp overflows to multiple lines. + // + // + // PRIVATE CSS PROPS + // + // *cssprop {Number} --mc-overflowBleedSize [var(--overflowBleedSize, 0)] + // *access private + // Size of overflow bleed. + // + // *cssprop {Number} --mc-badgeSize [var(--badgeSize, 8px)] + // *access private + // Size of badge placed in component's `badge` slot. + // + // *cssprop {Number} --mc-badgeSpacing [var(--mc-badgeSize) + var(--mc-overflowBleedSize)] + // *access private + // Positioning helper to ensure badge wraps with text and doesn't + // get truncated. + // + // *cssprop {Number} --mc-lineClamp [1] + // *access private + // Number of lines to clamp. + // + + .multiline-clamp { + --mc-overflowBleedSize: var(--overflowBleedSize, 0); + --mc-badgeSize: var(--badgeSize, 8px); + --mc-badgeSpacing: var(--mc-badgeSize); + word-break: break-word; // Allow long words to be truncated + + @include line-clamp(var(--mc-lineClamp, 1)); + } + + .multiline-clamp--overflow { + --mc-overflowBleedSize: var(--overflowBleedSize, #{$focus-size}); + --mc-badgeSpacing: calc( + var(--mc-badgeSize) + var(--mc-overflowBleedSize) + ); + + // Clip overflow contents when unfocused in order to prevent content + // that falls within the overflow padding box from being displayed. + clip-path: inset(var(--mc-overflowBleedSize)); + + // If container scrolls due to focus, keep focused item visible + scroll-padding: var(--mc-overflowBleedSize); + + @include overflow-bleed(var(--mc-overflowBleedSize)); + + &:focus-within { + clip-path: none; + } + } + + .multiline-clamp--with-badge { + &.multiline-clamp--truncated { + position: relative; + + // Adjust padding at end of clamp container so badge doesn't overlap text + padding-inline-end: var(--mc-badgeSpacing); + z-index: var(--z-default); + + .multiline-clamp__badge { + display: block; + position: absolute; + bottom: var(--mc-overflowBleedSize); + inset-inline-end: var(--mc-overflowBleedSize); + z-index: var(--z-default); + } + } + + // These styles on the text and badge create the effect of "sticking" + // the badge to the last word, so the badge never wraps to a new line on + // its own. + .multiline-clamp__text { + padding-inline-end: var(--mc-badgeSpacing); + } + + .multiline-clamp__badge:not(:empty) { + margin-inline-start: calc(-1 * var(--mc-badgeSpacing)); + } + } +</style> diff --git a/shared/components/src/components/LoadingSpinner/LoadingSpinner.svelte b/shared/components/src/components/LoadingSpinner/LoadingSpinner.svelte new file mode 100644 index 0000000..896c8b8 --- /dev/null +++ b/shared/components/src/components/LoadingSpinner/LoadingSpinner.svelte @@ -0,0 +1,260 @@ +<script lang="ts"> + // Delay until the spinner fades in + export let delay: number = 0; + export let inset: boolean = false; + export let small: boolean = false; + export let ariaLoading: string = ''; +</script> + +<div + class="loading-spinner" + class:inset + class:loading-spinner--small={small} + data-testid="loading-spinner" + style="animation-delay: {delay}ms" + aria-label={ariaLoading} +> + <div class="pulse-spinner"> + <div class="pulse-spinner__container"> + <div class="pulse-spinner__nib pulse-spinner__nib--1" /> + <div class="pulse-spinner__nib pulse-spinner__nib--2" /> + <div class="pulse-spinner__nib pulse-spinner__nib--3" /> + <div class="pulse-spinner__nib pulse-spinner__nib--4" /> + <div class="pulse-spinner__nib pulse-spinner__nib--5" /> + <div class="pulse-spinner__nib pulse-spinner__nib--6" /> + <div class="pulse-spinner__nib pulse-spinner__nib--7" /> + <div class="pulse-spinner__nib pulse-spinner__nib--8" /> + </div> + </div> +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/locale' as *; + @use 'ac-sasskit/core/selectors' as *; + @use 'amp/stylekit/core/mixins/materials' as *; + @use 'sass:math'; + + // Loading spinner contains `@amp/pulse-spinner` + + .loading-spinner { + margin: auto; + opacity: 0; + animation: fade-in 100ms; + animation-fill-mode: forwards; + text-align: center; + z-index: var(--z-default); + + &:not(.inset) { + position: absolute; + top: 50%; + left: 50%; // RTL not needed + + @media (--small) { + &:not(.loading-spinner--small) { + transform: translate(-50%, -50%); + } + } + } + + &.inset { + transform: translateX(50%); + + @include rtl { + transform: translateX(-50%); + } + } + } + + @keyframes fade-in { + 0% { + opacity: 0; + } + + 100% { + opacity: 1; + } + } + + //// + /// Pulse Spinner (Big Sur) + /// Styles from `@amp/pulse-spinner` + /// https://github.pie.apple.com/amp-web/pulse-spinner + //// + + /// + /// Spinner small container size + /// + /// @type Number + /// + $spinner-container-small: 16px; + + /// + /// Spinner large container size + /// + /// @type Number + /// + $spinner-container-large: 32px; + + /// + /// Spinner nib distance + /// + /// @type Value + /// + $spinner-nib-distance: 40px; + + /// + /// Spinner nib count + /// + /// @type Number + /// + $spinner-nibs: 8; + + /// + /// Spinner duration + /// + /// @type Number + /// + $spinner-duration: 0.8s; + + /// + /// Spinner small scaling value + /// + /// @type Value | Number + /// + $spinner-small-scale: scale(0.075); + + /// + /// Spinner large scaling value + /// + /// @type Value | Number + /// + $spinner-large-scale: 0.15; + + /// + /// Spinner inactive opacity + /// + /// @type Number + /// + $spinner-inactive-opacity: 0.5; + + .pulse-spinner { + position: relative; + width: $spinner-container-small; + height: $spinner-container-small; + + @include feature-detect($inactive-window-classname) { + opacity: $spinner-inactive-opacity; // AppKit inactive style, when window is not in focus + } + + @media (--small) { + .loading-spinner:not(.loading-spinner--small) & { + width: $spinner-container-large; + height: $spinner-container-large; + } + } + } + + .pulse-spinner__container { + position: absolute; + width: 0; + transform: $spinner-small-scale; + z-index: var(--z-default); + + @media (--small) { + .loading-spinner:not(.loading-spinner--small) & { + top: 50%; + left: 50%; + transform: scale(#{$spinner-large-scale}); + + @include rtl { + // Adjust for scale + right: #{$spinner-large-scale * 100%}; + } + } + } + } + + .pulse-spinner__nib { + position: absolute; + top: -12.5px; + width: 66px; + height: 28px; + background: transparent; + border-radius: 25% / 50%; + transform-origin: left center; + + &::before { + width: 100%; + height: 100%; + display: block; + content: ''; + background: rgb(0, 0, 0); + border-radius: 25% / 50%; + animation-duration: $spinner-duration; + animation-timing-function: linear; + animation-iteration-count: infinite; + animation-direction: normal; + animation-fill-mode: none; + animation-play-state: running; + animation-name: spinner-line-fade-default; + + @media (prefers-color-scheme: dark) { + background: rgb(255, 255, 255); + } + + @media (prefers-contrast: more) { + animation-name: spinner-line-fade-increased-contrast; + } + } + } + + @for $i from 0 to $spinner-nibs { + .pulse-spinner__nib--#{$i + 1} { + $degrees: math.div(360, $spinner-nibs) * $i; + $nib-delay: $spinner-duration - + (math.div($spinner-duration, $spinner-nibs) * $i); + transform: rotate(#{$degrees}deg) translateX($spinner-nib-distance); + + &::before { + animation-delay: -$nib-delay; + } + } + } + + $spinner-nib-minimum-opacity: 0.08; + $spinner-nib-maxiumum-opacity: 0.55; + $spinner-nib-minimum-opacity-increased-contrast: 0.1; + $spinner-nib-maxiumum-opacity-increased-contrast: 0.8; + + @keyframes spinner-line-fade-default { + 0%, + 100% { + opacity: $spinner-nib-maxiumum-opacity; + } + + 95% { + opacity: $spinner-nib-minimum-opacity; // minimum opacity + } + + 1% { + opacity: $spinner-nib-maxiumum-opacity; // maximum opacity + } + } + + // Increased Contrast Fade + @keyframes spinner-line-fade-increased-contrast { + 0%, + 100% { + opacity: $spinner-nib-maxiumum-opacity-increased-contrast; + } + + 95% { + opacity: $spinner-nib-minimum-opacity-increased-contrast; // minimum opacity + } + + 1% { + opacity: $spinner-nib-maxiumum-opacity-increased-contrast; // maximum opacity + } + } +</style> diff --git a/shared/components/src/components/MetaTags/MetaTags.svelte b/shared/components/src/components/MetaTags/MetaTags.svelte new file mode 100644 index 0000000..d526275 --- /dev/null +++ b/shared/components/src/components/MetaTags/MetaTags.svelte @@ -0,0 +1,262 @@ +<script lang="ts"> + import { LTR_MARK, RTL_MARK } from '@amp/web-app-components/src/constants'; + import type { Locale } from '@amp/web-app-components/src/types'; + import type { + SeoData, + HreflangTag, + } from '@amp/web-app-components/src/components/MetaTags/types'; + import type { ImageURLParams } from '@amp/web-app-components/src/components/Artwork/types'; + import { buildSrcSeo } from '@amp/web-app-components/src/components/Artwork/utils/srcset'; + import { serializeJSONData } from '@amp/web-app-components/src/utils/sanitize'; + + export let seoData: SeoData | undefined = undefined; + export let locale: Locale; + export let origin: string; + export let pageDir: string; + export let defaultTitle: string; + export let hreflangTags: HreflangTag[] | null = null; + + // Music's Classical Bridge prefers to use a different canonical + // for rel=canonical tags than the page url. Uses page url as fallback. + $: canonicalUrl = seoData?.canonicalUrl ?? seoData?.url; + $: pageTitle = seoData?.pageTitle ?? defaultTitle; + $: formattedLocale = locale.language.replace(/-/g, '_') || null; + $: directionMarker = pageDir === 'rtl' ? RTL_MARK : LTR_MARK; + + function processSocialImage( + artworkUrl: string, + imgParams: ImageURLParams, + ): string | undefined { + if (artworkUrl.startsWith('/')) { + artworkUrl = `${origin}${artworkUrl}`; + } + return buildSrcSeo(artworkUrl, imgParams); + } + + $: ogImageUrl = !!seoData?.artworkUrl + ? processSocialImage(seoData.artworkUrl, { + width: seoData.width, + height: seoData.height, + crop: seoData.crop, + fileType: seoData.fileType, + quality: seoData.quality, + }) + : null; + $: twitterImageUrl = !!seoData?.artworkUrl + ? processSocialImage(seoData.artworkUrl, { + width: seoData.twitterWidth, + height: seoData.twitterHeight, + crop: seoData.twitterCropCode, + fileType: seoData.fileType, + quality: seoData.quality, + }) + : null; + + $: sanitizedSchemaContent = !!seoData?.schemaContent + ? serializeJSONData(seoData.schemaContent) + : null; + + $: sanitizedBreadcrumbSchemaContent = !!seoData?.breadcrumbSchemaContent + ? serializeJSONData(seoData.breadcrumbSchemaContent) + : null; +</script> + +<svelte:head> + {#if pageTitle} + <!--directionMarker forces the direction so we don't get "....More from "some rtl text""--> + <title>{directionMarker}{pageTitle}</title> + {/if} + + {#if !!seoData} + <!-- Begin General --> + <!-- NOTE: If configuring robots tags, use one of these options, but not both --> + {#if seoData.noFollow} + <!-- Use this when you do not want your page indexed or your links followed --> + <meta name="robots" content="noindex, nofollow" /> + {:else if seoData.noIndex} + <!-- Use this when you want your links followed but not have the page indexed --> + <meta name="robots" content="noindex" /> + {/if} + + {#if seoData.description} + <meta name="description" content={seoData.description} /> + {/if} + + {#if seoData.keywords} + <meta name="keywords" content={seoData.keywords} /> + {/if} + + {#if canonicalUrl} + <link rel="canonical" href={canonicalUrl} /> + {/if} + + {#if hreflangTags} + {#each hreflangTags as langTag} + {#if langTag} + <link + rel="alternate" + href={langTag.path} + hreflang={langTag.tag} + /> + {/if} + {/each} + {/if} + <!-- End General --> + + {#if !!seoData.oembedData?.url} + <link + rel="alternate" + type="application/json+oembed" + href={`${origin}/api/oembed?url=${encodeURIComponent( + seoData.oembedData.url, + )}`} + title={seoData.oembedData.title ?? ''} + /> + {/if} + + <!-- Begin Apple-specific meta tags --> + {#if seoData.appleStoreId} + <meta name="al:ios:app_store_id" content={seoData.appleStoreId} /> + {/if} + + {#if seoData.appleStoreName} + <meta name="al:ios:app_name" content={seoData.appleStoreName} /> + {/if} + + {#if seoData.appleContentId} + <meta name="apple:content_id" content={seoData.appleContentId} /> + {/if} + + {#if seoData.appleTitle} + <meta name="apple:title" content={seoData.appleTitle} /> + {/if} + + {#if seoData.appleDescription} + <meta name="apple:description" content={seoData.appleDescription} /> + {/if} + <!-- End Apple-specific meta tags --> + + <!-- Begin OpenGraph (FaceBook, Slack, etc) --> + {#if seoData.socialTitle} + <meta property="og:title" content={seoData.socialTitle} /> + {/if} + + {#if seoData.socialDescription} + <meta + property="og:description" + content={seoData.socialDescription} + /> + {/if} + + {#if seoData.siteName} + <meta property="og:site_name" content={seoData.siteName} /> + {/if} + + {#if seoData.url} + <meta property="og:url" content={seoData.url} /> + {/if} + + {#if ogImageUrl} + <meta property="og:image" content={ogImageUrl} /> + <meta property="og:image:secure_url" content={ogImageUrl} /> + + {#if seoData.imageAltTitle} + <meta property="og:image:alt" content={seoData.imageAltTitle} /> + {:else if seoData.socialTitle} + <meta property="og:image:alt" content={seoData.socialTitle} /> + {/if} + + {#if seoData.width} + <meta + property="og:image:width" + content={seoData.width.toString()} + /> + {/if} + + {#if seoData.height} + <meta + property="og:image:height" + content={seoData.height.toString()} + /> + {/if} + + {#if seoData.fileType} + <meta + property="og:image:type" + content={`image/${seoData.fileType}`} + /> + {/if} + {/if} + + {#if seoData.ogType} + <meta property="og:type" content={seoData.ogType} /> + {/if} + + {#if seoData.socialTitle && formattedLocale} + <meta property="og:locale" content={formattedLocale} /> + {/if} + + {#if $$slots['extendedOpenGraphData']} + <slot name="extendedOpenGraphData" /> + {/if} + <!-- End OpenGraph --> + + <!-- Begin Twitter --> + {#if seoData.socialTitle} + <meta name="twitter:title" content={seoData.socialTitle} /> + {/if} + + {#if seoData.socialDescription} + <meta + name="twitter:description" + content={seoData.socialDescription} + /> + {/if} + + {#if seoData.twitterSite} + <meta name="twitter:site" content={seoData.twitterSite} /> + {/if} + + {#if twitterImageUrl} + <meta name="twitter:image" content={twitterImageUrl} /> + + {#if seoData.imageAltTitle} + <meta + name="twitter:image:alt" + content={seoData.imageAltTitle} + /> + {:else if seoData.socialTitle} + <meta name="twitter:image:alt" content={seoData.socialTitle} /> + {/if} + {/if} + + {#if seoData.twitterCardType} + <meta name="twitter:card" content={seoData.twitterCardType} /> + {/if} + <!-- End Twitter --> + + <!-- Begin schema.org --> + {#if $$slots['schemaOrganizationData']} + <slot name="schemaOrganizationData" /> + {/if} + + {#if seoData.schemaName && sanitizedSchemaContent} + {@html ` + <script id=${seoData.schemaName} type="application/ld+json"> + ${sanitizedSchemaContent} + </script> + `} + {/if} + <!-- End schema.org --> + + <!-- Begin breadcrumb schema --> + {#if seoData.breadcrumbSchemaName && sanitizedBreadcrumbSchemaContent} + {@html ` + <script id=${seoData.breadcrumbSchemaName} name=${seoData.breadcrumbSchemaName} type="application/ld+json"> + ${sanitizedBreadcrumbSchemaContent} + </script> + `} + {/if} + <!-- End breadcrumb schema --> + {/if} +</svelte:head> diff --git a/shared/components/src/components/Modal/ContentModal.svelte b/shared/components/src/components/Modal/ContentModal.svelte new file mode 100644 index 0000000..c382689 --- /dev/null +++ b/shared/components/src/components/Modal/ContentModal.svelte @@ -0,0 +1,222 @@ +<script lang="ts"> + import { createEventDispatcher, onMount } from 'svelte'; + import CloseIcon from '@amp/web-app-components/assets/icons/close.svg'; + import { updateScrollAndWindowDependentVisuals } from '@amp/web-app-components/src/actions/updateScrollAndWindowDependentVisuals'; + import { focusNodeOnMount } from '@amp/web-app-components/src/actions/focus-node-on-mount'; + import { sanitizeHtml } from '@amp/web-app-components/src/utils/sanitize-html'; + + export let title: string | null; + export let subtitle: string | null; + export let text: string = null; + export let translateFn: (key: string) => string; + export let dialogTitleId: string | null = null; + + let contentContainerElement: HTMLElement; + let contentIsScrolling = false; + let hideGradient = false; + + const dispatch = createEventDispatcher(); + + const handleCloseButton = (e: Event) => { + e.preventDefault(); + e.stopPropagation(); + + dispatch('close'); + }; + + onMount(() => { + // get initial state for hideGradient value, before user has scrolled + let { scrollHeight, offsetHeight } = contentContainerElement; + hideGradient = scrollHeight - offsetHeight === 0; + }); +</script> + +<div + data-testid="content-modal" + class="content-modal-container" + class:hide-gradient={hideGradient} + dir="auto" +> + <div class="button-container"> + <button + data-testid="content-modal-close-button" + class="close-button" + type="button" + on:click={handleCloseButton} + aria-label={translateFn('AMP.Shared.AX.Close')} + use:focusNodeOnMount + > + <CloseIcon data-testid="content-modal-close-button-svg" /> + </button> + {#if $$slots['button-container']} + <slot name="button-container" /> + {/if} + </div> + {#if title || subtitle} + <div + class="header-container" + class:content-is-scrolling={contentIsScrolling} + > + {#if title} + <h1 + id={dialogTitleId} + data-testid="content-modal-title" + class="title" + > + {title} + </h1> + {/if} + {#if subtitle} + <h2 data-testid="content-modal-subtitle" class="subtitle"> + {subtitle} + </h2> + {/if} + </div> + {/if} + {#if text || $$slots['content']} + <div + class="content-container" + bind:this={contentContainerElement} + use:updateScrollAndWindowDependentVisuals + on:scrollStatus={(e) => { + contentIsScrolling = e.detail.contentIsScrolling; + hideGradient = e.detail.hideGradient; + }} + > + {#if $$slots['content']} + <slot name="content" /> + {:else} + <p data-testid="content-modal-text"> + {@html sanitizeHtml(text)} + </p> + {/if} + </div> + {/if} +</div> + +<style lang="scss"> + .content-modal-container { + position: relative; + min-height: 230px; + max-height: calc(100vh - 160px); + height: auto; + display: flex; + flex-direction: column; + align-items: center; + max-width: 691px; + width: 80vw; + overflow: hidden; + background-color: var(--pageBG); + border-radius: var(--modalBorderRadius); + + @media (--range-xsmall-only) { + max-width: auto; + width: calc(100vw - 50px); + } + + &::after { + position: absolute; + bottom: 0; + height: 64px; + opacity: 1; + pointer-events: none; + transition-delay: 0s; + transition-duration: 300ms; + transition-property: height, width, background; + width: calc(100% - 60px); + content: ''; + background: linear-gradient( + to top, + var(--pageBG) 0%, + rgba(var(--pageBG-rgb), 0) 100% + ); + z-index: var(--z-default); + + @media (--range-xsmall-only) { + width: calc(100% - 40px); + } + } + } + + .header-container { + pointer-events: none; + position: sticky; + transition-delay: 0s; + transition-duration: 500ms; + transition-property: height, width; + width: 100%; + max-height: 120px; + padding-bottom: 22px; + z-index: var(--z-default); + } + + .content-is-scrolling { + box-shadow: 0 3px 5px var(--systemQuaternary); + } + + .button-container { + display: flex; + align-self: flex-start; + justify-content: space-between; + width: 100%; + } + + .close-button { + margin-top: 16px; + margin-bottom: 20px; + width: 18px; + height: 18px; + fill: var(--systemSecondary); + margin-inline-start: 20px; + } + + .title { + color: var(--systemPrimary); + padding: 0 30px; + font: var(--title-1-emphasized); + + @media (--range-xsmall-only) { + padding-inline-start: 20px; + padding-inline-end: 20px; + } + + @media (--small) { + font: var(--large-title-emphasized); + } + } + + .subtitle { + color: var(--systemSecondary); + padding: 0 30px; + font: var(--body); + + @media (--range-xsmall-only) { + padding-inline-start: 20px; + padding-inline-end: 20px; + } + } + + .content-container { + position: relative; + width: 100%; + height: calc(100% - 120px); + padding-bottom: 42px; + overflow-y: auto; + white-space: pre-wrap; + text-align: start; + font: var(--title-3-tall); + padding-inline-start: 30px; + padding-inline-end: 30px; + + @media (--range-xsmall-only) { + padding-inline-start: 20px; + padding-inline-end: 20px; + } + } + + .hide-gradient { + &::after { + opacity: 0; + } + } +</style> diff --git a/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherModal.svelte b/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherModal.svelte new file mode 100644 index 0000000..a248b55 --- /dev/null +++ b/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherModal.svelte @@ -0,0 +1,281 @@ +<script lang="ts"> + import { createEventDispatcher } from 'svelte'; + import ChevronIcon from '@amp/web-app-components/assets/icons/chevron.svg'; + import CloseIcon from '@amp/web-app-components/assets/icons/close.svg'; + import { focusNodeOnMount } from '@amp/web-app-components/src/actions/focus-node-on-mount'; + import type { Region } from '@amp/web-app-components/src/components/buttons/LocaleSwitcherButton/types'; + import { updateScrollAndWindowDependentVisuals } from '@amp/web-app-components/src/actions/updateScrollAndWindowDependentVisuals'; + import LocaleSwitcherRegionList from './LocaleSwitcherRegionList.svelte'; + import LocaleSwitcherRegion from './LocaleSwitcherRegion.svelte'; + + const DEFAULT_LIST_MINIMUM_LENGTH = 6; + /** + * translate function provided by the parent app. + */ + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + export let regions: Region[]; + export let defaultRoute: string; + export let dialogTitleId: string | null = null; + + let contentIsScrolling = false; + let showDefaultList = true; + let seeAllRegion: Region; + let contentContainerElement: HTMLElement; + + // the default list for each region is what shows when you first open the modal + // this consists of each storefront in the default language, with no duplicate storefronts + const regionsDefaultList = regions.map(({ name, locales }) => { + return { + name, + locales: locales.filter((locale) => locale.isDefault), + }; + }); + + const dispatch = createEventDispatcher(); + + const getExpandedRegion = (region: Region) => + regions.find((expandedRegion) => expandedRegion.name === region.name); + + const handleSeeAll = (region: Region) => { + seeAllRegion = getExpandedRegion(region); + showDefaultList = false; + contentContainerElement.scroll(0, 0); + }; + + const handleCloseButton = () => { + dispatch('close'); + }; + + const handleBack = () => { + showDefaultList = true; + }; +</script> + +<div + data-testid="locale-switcher-modal-container" + class="locale-switcher-modal-container" +> + <button + data-testid="locale-switcher-modal-close-button" + class="close-button" + type="button" + on:click={handleCloseButton} + aria-label={translateFn('AMP.Shared.AX.Close')} + use:focusNodeOnMount + > + <CloseIcon data-testid="locale-switcher-modal-close-button-svg" /> + </button> + <div + class="header-container" + class:content-is-scrolling={contentIsScrolling} + > + <span + id={dialogTitleId} + data-testid="locale-switcher-modal-title" + class="title" + > + {translateFn('AMP.Shared.LocaleSwitcher.Heading')} + </span> + </div> + <div + class="region-container" + bind:this={contentContainerElement} + use:updateScrollAndWindowDependentVisuals + on:scrollStatus={(e) => + (contentIsScrolling = e.detail.contentIsScrolling)} + > + {#if showDefaultList} + {#each regionsDefaultList as region (region.name)} + <LocaleSwitcherRegion regionName={translateFn(region.name)}> + <button + slot="button" + class="see-all-button" + class:see-all-button-hidden={region.locales.length <= + DEFAULT_LIST_MINIMUM_LENGTH} + on:click={() => handleSeeAll(region)} + >{translateFn('AMP.Shared.LocaleSwitcher.SeeAll')} + </button> + <!-- If the default list is less than or equal to 6, pass in see all list instead for the default view --> + <LocaleSwitcherRegionList + slot="list" + regionList={region.locales.length <= + DEFAULT_LIST_MINIMUM_LENGTH + ? getExpandedRegion(region)?.locales + : region.locales} + {defaultRoute} + /> + </LocaleSwitcherRegion> + {/each} + {:else} + <button class="back-button" on:click={handleBack}> + <ChevronIcon class="back-chevron" aria-hidden="true" /> + {translateFn('AMP.Shared.LocaleSwitcher.Back')} + </button> + + <LocaleSwitcherRegion regionName={translateFn(seeAllRegion.name)}> + <LocaleSwitcherRegionList + slot="list" + regionList={seeAllRegion.locales} + {defaultRoute} + /> + </LocaleSwitcherRegion> + {/if} + </div> +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/locale' as *; + @use 'amp/stylekit/core/fonts' as *; + @use 'amp/stylekit/modules/fontsubsets/core' as *; + @use '@amp/web-shared-styles/app/core/globalvars' as *; + + .locale-switcher-modal-container { + position: relative; + min-height: 230px; + height: calc(100vh - 160px); + display: flex; + flex-direction: column; + align-items: center; + overflow: hidden; + background-color: var(--pageBG); + max-width: calc(100vw - 50px); + border-radius: $modal-border-radius; + + // Font subsets for Geos prevents `SF Pro` Web Font from being downloaded + // after `BlinkMacSystemFont` fails in Chrome. + font-family: font-family-locale(en-WW, geos); + + @each $lang, $font in font-family(geos) { + @if $lang != en-WW { + :global([lang]:lang(#{$lang})) & { + font-family: $font; + } + } + } + + @media (--small) { + width: 990px; + } + + @media (--xlarge) { + width: 1250px; + } + + &::after { + position: absolute; + bottom: 0; + height: 64px; + opacity: 1; + pointer-events: none; + transition-delay: 0s; + transition-duration: 300ms; + transition-property: height, width, background; + width: calc(100% - 40px); + content: ''; + background: linear-gradient( + to top, + var(--pageBG) 0%, + rgba(var(--pageBG-rgb), 0) 100% + ); + z-index: var(--z-default); + + @media (--small) { + width: calc(100% - 60px); + } + } + } + + .header-container { + pointer-events: none; + position: sticky; + transition-delay: 0s; + transition-duration: 500ms; + transition-property: height, width; + width: 100%; + padding-top: 54px; + padding-bottom: 32px; + max-height: 120px; + z-index: var(--z-default); + } + + .content-is-scrolling { + box-shadow: 0 3px 5px var(--systemQuaternary); + transition: box-shadow 0.2s ease-in-out; + } + + .close-button { + position: absolute; + top: 0; + margin: 16px 20px 10px; + width: 18px; + height: 18px; + align-self: flex-start; + fill: var(--systemSecondary); + } + + .title { + color: var(--systemPrimary); + text-align: center; + width: 100%; + display: block; + padding-inline-start: 20px; + padding-inline-end: 20px; + font: var(--title-1-emphasized); + + @media (--medium) { + font: var(--large-title-emphasized); + } + } + + .region-container { + position: relative; + height: calc(100% - 120px); + padding-bottom: 42px; + overflow-y: auto; + padding-inline-start: 20px; + padding-inline-end: 20px; + + @media (width >= 600px) { + padding-inline-start: 50px; + padding-inline-end: 50px; + } + } + + .back-button { + color: var(--keyColor); + margin-bottom: 16px; + display: flex; + align-items: center; + + :global(.back-chevron) { + height: 12px; + fill: var(--keyColor); + transform: rotate(180deg); + margin-inline-end: 5px; + + @include rtl { + transform: rotate(0deg); + } + } + } + + // shadow-DOM RTL styles + :global(:host([dir='rtl'])) { + :global(.back-chevron) { + transform: rotate(0deg); + } + } + + .see-all-button { + min-width: 42px; + color: var(--keyColor); + } + + .see-all-button-hidden { + display: none; + } +</style> diff --git a/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherRegion.svelte b/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherRegion.svelte new file mode 100644 index 0000000..3310e87 --- /dev/null +++ b/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherRegion.svelte @@ -0,0 +1,27 @@ +<script lang="ts"> + export let regionName: string; +</script> + +<div class="region-header"> + <h2> + {regionName} + </h2> + <slot name="button" /> +</div> +<slot name="list" /> + +<style lang="scss"> + .region-header { + padding-top: 13px; + padding-bottom: 20px; + border-top: 1px solid var(--labelDivider); + display: flex; + justify-content: space-between; + align-items: baseline; + } + + h2 { + margin-inline-end: 5px; + font: var(--title-2-emphasized); + } +</style> diff --git a/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherRegionList.svelte b/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherRegionList.svelte new file mode 100644 index 0000000..f123ce0 --- /dev/null +++ b/shared/components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherRegionList.svelte @@ -0,0 +1,70 @@ +<script lang="ts"> + import type { Storefront } from '@amp/web-app-components/src/components/buttons/LocaleSwitcherButton/types'; + import { getStorefrontRoute } from '@amp/web-app-components/src/utils/getStorefrontRoute'; + + export let regionList: Storefront[]; + export let defaultRoute: string; + + const getRoute = (storefront: Storefront) => { + // the language param is only needed for non-default storefronts + return storefront.isDefault + ? getStorefrontRoute(defaultRoute, storefront.id) + : getStorefrontRoute( + defaultRoute, + storefront.id, + storefront.language, + ); + }; +</script> + +<ul> + {#each regionList as storefront} + <li> + <a href={getRoute(storefront)} data-testid="region-list-link"> + <span>{storefront.name}</span> + </a> + </li> + {/each} +</ul> + +<style lang="scss"> + ul, + li { + list-style-type: none; + margin: 0; + padding: 0; + } + + ul { + columns: 1 auto; + margin-bottom: 25px; + + @media (width >= 600px) { + columns: 3 auto; + } + + @media (--small) { + columns: 4 auto; + } + + @media (--large) { + columns: 5 auto; + } + + @media (--xlarge) { + columns: 6 auto; + } + } + + li { + padding-right: 40px; + padding-bottom: 26px; + display: inline-block; + width: 100%; + font: var(--callout); + + a { + --linkColor: var(--systemPrimary); + } + } +</style> diff --git a/shared/components/src/components/Modal/Modal.svelte b/shared/components/src/components/Modal/Modal.svelte new file mode 100644 index 0000000..a4fe147 --- /dev/null +++ b/shared/components/src/components/Modal/Modal.svelte @@ -0,0 +1,246 @@ +<script lang="ts"> + import { onMount, createEventDispatcher } from 'svelte'; + + const dispatch = createEventDispatcher(); + + export let modalTriggerElement: HTMLElement | null; + export let error: boolean = false; + export let dialogId: string = ''; + export let dialogClassNames: string = ''; + + /** + * Disable the background scrim for this modal. Used with fullscreen modal + * variants that don't apply a scrim while transitioning in or out of view. + */ + export let disableScrim: boolean = false; + + /** + * Whether to immediately display the modal when the component is mounted. + */ + export let showOnMount: boolean = false; + + /** + * If true, suppress the default `close` event fired by the native <dialog> element. + * Instead, a `close` event is dispatched to be handled by the consuming component. + * This is useful for modals that implement custom transitions and need to wait for + * transitions to end on child elements before <dialog> removes them from the DOM. + * + * Note that if this option is used, the consuming component *must* call `close()` + * on this component to properly close the modal! + */ + export let preventDefaultClose: boolean = false; + + /** + * ID for element that contains accessible modal title. + */ + export let ariaLabelledBy: string | null = null; + + /** + * Accessible modal title. Note that this should only be used when there is no element + * containing the modal title that can be associated using `ariaLabelledBy`. + */ + export let ariaLabel: string | null = null; + + let ariaHidden: boolean = true; + + let dialogElement: HTMLDialogElement; + let needsPolyfill: boolean = false; + let isDialogInShadow: boolean; + + export function showModal() { + // noscroll class ensures that when this component is in a shadow DOM context, + // the parent app can control the background scroll behavior + document.body.classList.add('noscroll'); + + /* + in non-shadow DOM contexts, add the dialog directly to the body to + avoid stacking context issues where the the dialog hides behind side nav on Music + see: https://github.com/GoogleChrome/dialog-polyfill#stacking-context + if the dialog is within the shadow DOM (being used as a web component) + do not append to the body and use showModal method to keep dialog within the shadow DOM + */ + if (needsPolyfill) { + isDialogInShadow = isInShadow(dialogElement); + if (!isDialogInShadow) { + document.body.appendChild(dialogElement); + } + } + ariaHidden = false; + dialogElement.showModal(); + } + + export function close() { + document.body.classList.remove('noscroll'); + + // in non-shadow DOM + polyfill instances we added the dialog + // directly to the body, this removes it + if (needsPolyfill && !isDialogInShadow) { + document.body.removeChild(dialogElement); + } + + ariaHidden = true; + dialogElement.close(); + modalTriggerElement?.focus(); + } + + function handleClose(e: Event) { + if (preventDefaultClose) { + e.preventDefault(); + } else { + close(); + } + dispatch('close'); + } + + function isInShadow(node: HTMLElement | ParentNode) { + for (; node; node = node.parentNode) { + if (node.toString() === '[object ShadowRoot]') { + return true; + } + } + return false; + } + + onMount(async () => { + // register polyfill for native <dialog> element if needed + needsPolyfill = !('showModal' in dialogElement); + if (needsPolyfill) { + const { default: dialogPolyfill } = await import('dialog-polyfill'); + dialogPolyfill.registerDialog(dialogElement); + dialogElement.classList.add('dialog-polyfill'); + } + + if (showOnMount) { + showModal(); + } + }); +</script> + +<!-- + @component + Dialog element wrapping a slot. + This component is multipurpose and should be used + anywhere a centered modal with a backdrop is needed + --> +<!-- svelte-ignore a11y-click-events-have-key-events --> +<!-- svelte-ignore a11y-no-noninteractive-element-interactions --> +<dialog + data-testid="dialog" + class:error + class:no-scrim={disableScrim} + class={dialogClassNames} + class:needs-polyfill={needsPolyfill} + id={dialogId} + bind:this={dialogElement} + on:click|self={handleClose} + on:close={handleClose} + on:cancel={handleClose} + aria-labelledby={ariaLabelledBy} + aria-label={ariaLabel} + aria-hidden={ariaHidden} +> + <slot {handleClose} /> +</dialog> + +<style lang="scss"> + @use '@amp/web-shared-styles/app/core/globalvars' as *; + + /* dialog polyfill styles need to be available + globally to avoid being stripped out */ + :global(.needs-polyfill) { + position: absolute; + left: 0; + right: 0; + width: fit-content; + height: fit-content; + margin: auto; + border: solid; + padding: 1em; + background: white; + color: black; + display: block; + + &:not([open]) { + display: none; + } + + & + .backdrop { + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; + background: rgba(0, 0, 0, 0.1); + } + + &._dialog_overlay { + position: fixed; + top: 0; + right: 0; + bottom: 0; + left: 0; + } + + &.fixed { + position: fixed; + top: 50%; + transform: translate(0, -50%); + } + } + + /* dialog polyfill sets position: absolute - this + needs to be reset to ensure the dialog does not + scroll to top on open */ + dialog:modal { + position: fixed; + } + + dialog { + width: var(--modalWidth, fit-content); + height: var(--modalHeight, fit-content); + max-width: var(--modalMaxWidth, initial); + max-height: var(--modalMaxHeight, initial); + border-radius: var(--modalBorderRadius, $modal-border-radius); + border: 0; + padding: 0; + color: var(--systemPrimary); + background: transparent; + + // Hide scrollbar while opening sliding modal + overflow: var(--modalOverflow, auto); + top: var(--modalTop, 0); + font: var(--body); + + &:focus { + outline: none; + } + + &::backdrop, + & + :global(.backdrop) /* for polyfill */ { + background-color: var(--modalScrimColor, rgba(0, 0, 0, 0.45)); + } + + // ::backdrop does not inherit from anything, so CSS properties must be set on + // it directly in order to have any effect. + &.no-scrim::backdrop, + &.no-scrim + :global(.backdrop) { + --modalScrimColor: transparent; + } + } + + // disable error modal animation until svelte animations are implemented + // rdar://92356192 (JMOTW: Error Modal: Use Svelte animations) + // $error-modal-duration: 0.275s; + // dialog.error { + // box-shadow: $dialog-inset-shadow, $dialog-shadow; + // animation-name: modalZoomIn; + // animation-duration: $error-modal-duration; + // animation-timing-function: cubic-bezier(0.27, 1.01, 0.43, 1.19); + // } + // @keyframes modalZoomIn { + // from { + // opacity: 0; + // transform: scale3d(0, 0, 0); + // } + // } +</style> diff --git a/shared/components/src/components/Navigation/Folder.svelte b/shared/components/src/components/Navigation/Folder.svelte new file mode 100644 index 0000000..2e1b15b --- /dev/null +++ b/shared/components/src/components/Navigation/Folder.svelte @@ -0,0 +1,277 @@ +<script lang="ts"> + import { createEventDispatcher } from 'svelte'; + import type { Writable } from 'svelte/store'; + import type { + NavigationId, + BaseNavigationItem, + } from '@amp/web-app-components/src/types'; + import { + isSameTab, + getItemComponent, + } from '@amp/web-app-components/src/components/Navigation/utils'; + import allowDrag from '@amp/web-app-components/src/actions/allow-drag'; + import allowDrop from '@amp/web-app-components/src/actions/allow-drop'; + import { subscribeFolderOpenState } from '@amp/web-app-components/src/stores/navigation-folders-open'; + import ItemContent from './ItemContent.svelte'; + + const FOLDER_EXPAND_DELAY = 1000; + const dispatch = createEventDispatcher(); + + export let item: BaseNavigationItem; + export let isEditing: boolean = false; + export let currentTab: Writable<NavigationId | null>; + export let translateFn: (key: string) => string; + export let getItemDragData: (item: BaseNavigationItem) => any = null; + export let itemDragEnabled: + | boolean + | ((item: BaseNavigationItem) => boolean) = false; + export let itemDropEnabled: + | boolean + | ((item: BaseNavigationItem) => boolean) = false; + + let delayedExpandTimeoutId: ReturnType<typeof setTimeout>; + $: itemId = item.id.resourceId; + $: children = item.children; + $: hasChildren = children?.length > 0; + $: label = item.label ? item.label : translateFn(item.locKey); + $: isExpanded = subscribeFolderOpenState(itemId); + $: dragData = !!getItemDragData ? getItemDragData(item) : item; + $: isDragEnabled = + !!dragData && + (typeof itemDragEnabled === 'function' + ? itemDragEnabled(item) + : itemDragEnabled); + $: isDropEnabled = + typeof itemDropEnabled === 'function' + ? itemDropEnabled(item) + : itemDropEnabled; + + const toggleExpand = (): void => { + if (hasChildren) { + isExpanded.set(!$isExpanded); + } + }; + + const handleKeydown = (e: KeyboardEvent) => { + switch (e.key) { + case 'Enter': + toggleExpand(); + break; + + case 'ArrowRight': + if (hasChildren && !$isExpanded) { + isExpanded.set(true); + e.preventDefault(); + e.stopPropagation(); + } + break; + + case 'ArrowLeft': + if (hasChildren && $isExpanded) { + isExpanded.set(false); + e.preventDefault(); + e.stopPropagation(); + } + break; + } + }; + + // Due to dragleave events being fired when dragging over child elements, + // we need to maintain a count of the number of elements we have entered + // within the folder to know when we have actually left the element. When + // enteredCount reaches 0, we know that we have finally left the outermost + // element. + // + // rdar://118572702 (Use event.relatedTarget to handle dragging playlists over folders) + // A more elegant solution could leverage event.relatedTarget to ignore + // dragleave events from children, but there is a Safari bug where + // relatedTarget is always null. + + let enteredCount = 0; + + const delayedExpand = (): void => { + enteredCount++; + + if (!$isExpanded && !delayedExpandTimeoutId) { + delayedExpandTimeoutId = setTimeout(() => { + isExpanded.set(true); + delayedExpandTimeoutId = null; + }, FOLDER_EXPAND_DELAY); + } + }; + + const cancelDelayedExpand = (): void => { + enteredCount--; + + if (enteredCount === 0 && delayedExpandTimeoutId) { + clearTimeout(delayedExpandTimeoutId); + delayedExpandTimeoutId = null; + } + }; +</script> + +<!-- svelte-ignore a11y-role-has-required-aria-props --> +<li + class="navigation-item navigation-item__folder" + data-testid="navigation-item__{item.id.type}" + class:navigation-item__folder--has-children={children} + class:folder-open={$isExpanded} + aria-expanded={$isExpanded} + role="treeitem" + tabindex="-1" + on:dragenter|capture|preventDefault={delayedExpand} + on:dragleave|capture|preventDefault={cancelDelayedExpand} + on:keydown|self={handleKeydown} +> + <!-- svelte-ignore a11y-no-static-element-interactions --> + <!-- svelte-ignore a11y-click-events-have-key-events --> + <span + class="navigation-item__folder-label" + class:drop-reset={!!isDropEnabled} + data-testid={itemId} + on:click|preventDefault={toggleExpand} + use:allowDrag={isDragEnabled && { + dragEnabled: true, + dragData, + usePlainDragImage: true, + }} + use:allowDrop={isDropEnabled && { + dropEnabled: true, + onDrop: (dropData) => dispatch('dropOnItem', { item, dropData }), + }} + > + {#if hasChildren} + <span + data-testid="folder-arrow-indicator" + class="folder-arrow-indicator" + role="presentation" + /> + {/if} + <ItemContent icon={item.icon} {label} /> + </span> + {#if hasChildren && $isExpanded} + <ul class="navigation-item__folder-list"> + {#each children as child} + {#if child.id.type === 'folder'} + <svelte:self + item={child} + {currentTab} + {getItemDragData} + {itemDragEnabled} + {itemDropEnabled} + {translateFn} + {isEditing} + on:selectItem + on:dropOnItem + /> + {:else} + <svelte:component + this={getItemComponent(child)} + item={child} + selected={isSameTab(child.id, $currentTab)} + {translateFn} + {isEditing} + getDragData={getItemDragData} + dragEnabled={itemDragEnabled} + dropEnabled={itemDropEnabled} + on:selectItem + on:drop={({ detail: dropData }) => + dispatch('dropOnItem', { item: child, dropData })} + /> + {/if} + {/each} + </ul> + {/if} +</li> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/locale' as *; + @use 'amp/stylekit/core/mixins/line-clamp' as *; + @use 'amp/stylekit/core/mixins/overflow-bleed' as *; + + $menuicon-folder-transition: 0.3s transform ease; + + .navigation-item__folder { + --linkHoverTextDecoration: none; + border-radius: 6px; + margin-bottom: 2px; + padding: 4px; + position: relative; + + @media (--sidebar-visible) { + height: 32px; + } + + &.folder-open { + margin-bottom: 0; + padding-bottom: 0; + } + } + + .navigation-item__folder--has-children { + height: auto; + } + + .navigation-item__folder-label { + border-radius: 6px; + box-sizing: content-box; + display: flex; + align-items: center; + + @include overflow-bleed(3px); + + .navigation-item__folder--has-children & { + cursor: pointer; + } + + &:global(.is-drag-over) { + --drag-over-color: white; + --navigation-item-text-color: var(--drag-over-color); + --navigation-item-icon-color: var(--drag-over-color); + background-color: var(--selectionColor); + } + } + + .navigation-item__folder-list { + margin-inline-start: 8px; + margin-top: 4px; + } + + .folder-arrow-indicator::before { + content: ''; + width: 0; + height: 0; + display: inline-block; + position: absolute; + top: 16px; + border-style: solid; + border-top-width: 4px; + border-top-color: transparent; + border-bottom-width: 4px; + border-bottom-color: transparent; + transform: rotate(0deg); + transition: $menuicon-folder-transition; + border-inline-end-width: 0; + border-inline-end-color: transparent; + border-inline-start-width: 6px; + border-inline-start-color: var(--systemTertiary); + inset-inline-start: -12px; + + .folder-open & { + transform: rotate(90deg); + + @include rtl { + transform: rotate(-90deg); + } + } + + @media (--sidebar-visible) { + top: 12px; + } + + @media (prefers-reduced-motion: reduce) { + transition: none; + } + } +</style> diff --git a/shared/components/src/components/Navigation/Item.svelte b/shared/components/src/components/Navigation/Item.svelte new file mode 100644 index 0000000..e10c604 --- /dev/null +++ b/shared/components/src/components/Navigation/Item.svelte @@ -0,0 +1,183 @@ +<script lang="ts"> + import { createEventDispatcher } from 'svelte'; + import type { BaseNavigationItem } from '@amp/web-app-components/src/types'; + import allowDrag from '@amp/web-app-components/src/actions/allow-drag'; + import allowDrop, { + type DropOptions, + } from '@amp/web-app-components/src/actions/allow-drop'; + import ItemContent from './ItemContent.svelte'; + + export let item: BaseNavigationItem; + export let selected: boolean = false; + export let isEditing: boolean = false; + export let isChecked: boolean = false; + export let translateFn: (key: string) => string; + export let getDragData: (item: BaseNavigationItem) => any = null; + export let dragEnabled: boolean | ((item: BaseNavigationItem) => boolean) = + false; + export let dropEnabled: boolean | ((item: BaseNavigationItem) => boolean) = + false; + export let dropTargets: DropOptions['targets'] = null; + export let dropEffect: DataTransfer['dropEffect'] = null; + export let effectAllowed: DataTransfer['effectAllowed'] = null; + + $: label = item.label ? item.label : translateFn(item.locKey); + + $: dragData = !!getDragData ? getDragData(item) : item; + $: isDragEnabled = + !!dragData && + (typeof dragEnabled === 'function' ? dragEnabled(item) : dragEnabled); + $: isDropEnabled = + typeof dropEnabled === 'function' ? dropEnabled(item) : dropEnabled; + + const dispatch = createEventDispatcher(); + + function onChangeVisibility() { + dispatch('visibilityChangeItem'); + } + + const itemClicked = (): void => { + dispatch('selectItem', item); + }; +</script> + +<!-- TODO: rdar://97308317 (Investigate svelte AX warnings in shared components) --> +<!-- svelte-ignore a11y-role-supports-aria-props --> +<li + class="navigation-item navigation-item__{item.id.type}" + class:navigation-item--selected={selected} + class:is-editing={isEditing} + class:drop-reset={!!dropEnabled} + aria-selected={selected} + data-testid="navigation-item" + use:allowDrag={isDragEnabled && + !isEditing && { + dragEnabled: true, + dragData, + usePlainDragImage: true, + effectAllowed, + }} + use:allowDrop={isDropEnabled && + !isEditing && { + dropEnabled: true, + onDrop: (dropData) => dispatch('drop', dropData), + targets: dropTargets, + dropEffect, + }} +> + <slot> + {#if isEditing} + <label + for={item.id.type} + class="navigation-item__label" + data-testid="navigation-item-editing" + > + <ItemContent icon={item.icon} {label}> + <input + class="navigation-item__checkbox" + data-testid="navigation-item-editing-checkbox" + type="checkbox" + id={item.id.type} + checked={isChecked} + on:change={onChangeVisibility} + slot="prefix" + /> + </ItemContent> + </label> + {:else} + <a + href={item.url} + class="navigation-item__link" + role="button" + data-testid={item.id.resourceId || item.id.type} + aria-pressed={selected} + on:click|preventDefault={itemClicked} + > + <ItemContent icon={item.icon} {label} /> + </a> + {/if} + </slot> +</li> + +<style lang="scss"> + @use 'amp/stylekit/core/mixins/overflow-bleed' as *; + @use '@amp/web-shared-styles/app/core/globalvars' as *; + + .navigation-item { + --linkHoverTextDecoration: none; + border-radius: 6px; + margin-bottom: 2px; + padding: 4px; + position: relative; + + &:last-child { + margin-bottom: 1px; + } + + &:not(.is-dragging) { + &:global(.is-drag-over) { + --drag-over-color: white; + --navigation-item-text-color: var(--drag-over-color); + --navigation-item-icon-color: var(--drag-over-color); + background-color: var(--selectionColor); + } + + &:global(.is-drag-over-top), + &:global(.is-drag-over-bottom) { + &::after { + content: ''; + position: absolute; + background-color: var(--keyColor); + width: 100%; + height: $drag-over-focus-size; + inset-inline-start: 0; + } + } + + &:global(.is-drag-over-top) { + &::after { + top: 0; + transform: translateY(calc(#{-$drag-over-focus-size} / 2)); + } + } + + &:global(.is-drag-over-bottom) { + &::after { + bottom: 0; + transform: translateY(calc(#{$drag-over-focus-size} / 2)); + } + } + } + + @media (--sidebar-visible) { + height: 32px; + + &.navigation-item__radio { + margin-bottom: 1px; + } + } + } + + .navigation-item--selected { + background-color: var(--navSidebarSelectedState); + } + + .navigation-item__search { + @media (--sidebar-visible) { + display: none; + } + } + + .navigation-item__link { + display: block; + box-sizing: content-box; + border-radius: inherit; + + @include overflow-bleed(3px); + } + + .navigation-item__checkbox { + accent-color: var(--keyColor); + margin-inline-end: 5px; + } +</style> diff --git a/shared/components/src/components/Navigation/ItemContent.svelte b/shared/components/src/components/Navigation/ItemContent.svelte new file mode 100644 index 0000000..4a4e69c --- /dev/null +++ b/shared/components/src/components/Navigation/ItemContent.svelte @@ -0,0 +1,71 @@ +<script lang="ts"> + import type { ComponentType } from 'svelte'; + + export let icon: ComponentType; + export let label: string; +</script> + +<div class="navigation-item__content"> + {#if $$slots['prefix']} + <slot name="prefix" /> + {/if} + + <span class="navigation-item__icon"> + <slot name="icon"> + <svelte:component this={icon} aria-hidden="true" /> + </slot> + </span> + + <span class="navigation-item__label"> + <slot name="label"> + {label} + </slot> + </span> +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'amp/stylekit/core/mixins/line-clamp' as *; + @use 'amp/stylekit/core/mixins/overflow-bleed' as *; + @use 'ac-sasskit/core/locale' as *; + + .navigation-item__content { + border-radius: inherit; + display: flex; + align-items: center; + width: 100%; + column-gap: 8px; + color: var(--navigation-item-text-color, var(--systemPrimary)); + + :global(.navigation-item--selected) & { + font: var(--title-2-emphasized); + + @media (--sidebar-visible) { + font: var(--title-3-medium); + } + } + } + + .navigation-item__icon { + line-height: 0; // Normalize line height + flex: 0 0; + flex-basis: var(--navigation-item-icon-size, 32px); + + :global(svg) { + width: 100%; + height: 100%; + fill: var(--navigation-item-icon-color, var(--keyColor)); + } + + @media (--sidebar-visible) { + flex-basis: var(--navigation-item-icon-size, 24px); + } + } + + .navigation-item__label { + flex: 1; + + @include line-clamp; + @include overflow-bleed(4px); + } +</style> diff --git a/shared/components/src/components/Navigation/MenuIcon.svelte b/shared/components/src/components/Navigation/MenuIcon.svelte new file mode 100644 index 0000000..9e9163f --- /dev/null +++ b/shared/components/src/components/Navigation/MenuIcon.svelte @@ -0,0 +1,178 @@ +<script lang="ts"> + import { + menuIsExpanded, + menuIsTransitioning, + } from '@amp/web-app-components/src/components/Navigation/store/menu-state'; + import { prefersReducedMotion } from '@amp/web-app-components/src/stores/prefers-reduced-motion'; + import { createEventDispatcher } from 'svelte'; + + export let translateFn: ( + key: string, + data?: Record<string | number, string>, + ) => string; + export let navigationId = ''; + + const OPEN_NAVIGATION_LABEL = translateFn('FUSE.AX.UI.Open.Navigation'); + const CLOSE_NAVIGATION_LABEL = translateFn('FUSE.AX.UI.Close.Navigation'); + const dispatch = createEventDispatcher(); + + // Helper vars for refocusing on menu button when the menu closes. + let menuWasExpanded = false; + let menuButton: HTMLButtonElement; + + $: ariaExpanded = $menuIsExpanded; + $: ariaLabel = ariaExpanded + ? CLOSE_NAVIGATION_LABEL + : OPEN_NAVIGATION_LABEL; + + $: if ($menuIsExpanded) { + menuWasExpanded = true; + } + + // Only focus the menu button if the menu was previously expanded and is now collapsed. + // This prevents the menu button from focusing on page mount. + $: if (!$menuIsExpanded && menuWasExpanded) { + menuButton?.focus(); + menuWasExpanded = false; + } + + function handleClick(): void { + // Only allow the menu to be expanded / contracted if a transition is not currently in flight. + if ($menuIsTransitioning) { + return; + } + + // Update the internal nav store + // Implicitly updates aria-expanded and aria-label + menuIsExpanded.set(!$menuIsExpanded); + + // dispatch event to parent app + dispatch('toggleExpansion', { + isMenuExpanded: ariaExpanded, + }); + + // If reduced motion is not preferred, the flag needs to be set + // that a transition is currently in flight. When reduced-motion is preferred, + // no transition occurs. + if (!$prefersReducedMotion) { + // Flag that the menu-transition is in flight. This gets unlocked + // by the <Navigation /> component as it has the longest duration + menuIsTransitioning.set(true); + } + } +</script> + +<button + data-testid="menuicon" + class="menuicon" + aria-controls={navigationId} + aria-label={ariaLabel} + aria-expanded={ariaExpanded} + on:click={handleClick} + bind:this={menuButton} +> + <span class="menuicon-bread menuicon-bread-top"> + <span class="menuicon-bread-crust menuicon-bread-crust-top" /> + </span> + <span class="menuicon-bread menuicon-bread-bottom"> + <span class="menuicon-bread-crust menuicon-bread-crust-bottom" /> + </span> +</button> + +<style lang="scss"> + @use '@amp/web-shared-styles/app/core/globalvars' as *; + + $shared-transition-delay: 0.1008s; + $shared-transition-duration: 0.1806s; + $amp-nav-ease-blue: cubic-bezier(0.04, 0.04, 0.12, 0.96); + $amp-nav-ease-green: cubic-bezier(0.52, 0.16, 0.52, 0.84); + + .menuicon { + height: $global-header-mobile-contracted-height; + width: $global-header-mobile-contracted-height; + position: relative; + z-index: var(--z-default); + } + + .menuicon-bread { + height: 20px; + left: 13px; + pointer-events: none; + position: absolute; + top: 12px; + transition: transform $shared-transition-duration $amp-nav-ease-blue; + width: 20px; + z-index: var(--z-default); + + /* Make sure the crust elements are not clickable to ensure correct locking. */ + span { + pointer-events: none; + } + + [aria-expanded='true'] & { + height: 24px; + left: 10px; + top: 11px; + width: 24px; + // prettier-ignore + transition: transform 0.3192s $amp-nav-ease-blue $shared-transition-delay; + } + } + + [aria-expanded='true'] { + .menuicon-bread-top { + transform: rotate(-45deg); + } + + .menuicon-bread-bottom { + transform: rotate(45deg); + } + } + + .menuicon-bread-crust { + background: var(--keyColor); + border-radius: 1px; + display: block; + height: 2px; + position: absolute; + // prettier-ignore + transition: transform 0.1596s $amp-nav-ease-green $shared-transition-delay; + width: 20px; + z-index: var(--z-default); + + [aria-expanded='true'] & { + width: 24px; + transform: translateY(0); + transition: transform $shared-transition-duration $amp-nav-ease-blue; + } + } + + .menuicon-bread-crust-top { + top: 9px; + transform: translateY(-4px); + + [aria-expanded='true'] & { + top: 11px; + } + } + + .menuicon-bread-crust-bottom { + bottom: 9px; + transform: translateY(4px); + + [aria-expanded='true'] & { + bottom: 11px; + } + } + + // Remove transitions when user prefers reduced motion + @media (prefers-reduced-motion: reduce) { + .menuicon-bread, + .menuicon-bread-crust { + &, + [aria-expanded='true'] & { + transition: none; + } + } + } +</style> diff --git a/shared/components/src/components/Navigation/Navigation.svelte b/shared/components/src/components/Navigation/Navigation.svelte new file mode 100644 index 0000000..34b3daf --- /dev/null +++ b/shared/components/src/components/Navigation/Navigation.svelte @@ -0,0 +1,298 @@ +<script lang="ts"> + import { createEventDispatcher, afterUpdate } from 'svelte'; + import type { Writable } from 'svelte/store'; + import { + menuIsExpanded, + menuIsTransitioning, + } from '@amp/web-app-components/src/components/Navigation/store/menu-state'; + import type { NavigationId } from '@amp/web-app-components/src/types'; + import type { NavigationItem } from '@amp/web-app-components/src/components/Navigation/types'; + import MenuIcon from './MenuIcon.svelte'; + import NavigationItems from './NavigationItems.svelte'; + import { allowDrop } from '@amp/web-app-components/src/actions/allow-drop'; + import { sidebarIsHidden } from '@amp/web-app-components/src/stores/sidebar-hidden'; + + const dispatch = createEventDispatcher(); + + /** + * The local storage key that contains the user-selected library items to show + * @type {string} + */ + export let visibilityPreferencesKey: string | null = null; + + /** + * A list of links to be in the navigation + * @type {Array<NavigationItem>} + */ + export let items: NavigationItem[]; + + /** + * A list of links to be in the library navigation + * @type {Array<NavigationItem>} + */ + export let libraryItems: NavigationItem[] = []; + + /** + * A list of personalized items in the navigation such as a user's playlists or stations + * @type {Array<NavigationItem>} + */ + export let personalizedItems: NavigationItem[] = []; + + /** + * Header to be used for the personalized items list + */ + export let personalizedItemsHeader: string = ''; + + /** + * translate function provided by the parent app. + */ + export let translateFn: (key: string) => string; + + /** + * The store containing the currently selected tab. + */ + export let currentTab: Writable<NavigationId | null>; + + /** + * Whether you should be able to drop on the library section + * @type {boolean} + */ + export let libraryDropEnabled: boolean = false; + + /** + * Boolean or method to indicate if it allows drop on navigation header. + * The header type can be passed in to have a conditional drop area. + * Use together with on:dropOnHeader + */ + export let headerDropEnabled: boolean | ((type: string) => boolean) = false; + + /** + * Function that maps the item to drag data. + * Uses the item by default when not set. + */ + export let getItemDragData: (item: NavigationItem) => any = null; + + /** + * Boolean or method to indicate if it allows items to be dragged. + * The item can be passed in to have conditional dragging. + * Use together with getItemDragData + */ + export let itemDragEnabled: boolean | ((item: NavigationItem) => boolean) = + false; + + /** + * Boolean or method to indicate if it allows drop on an item. + * The item can be passed in to have a conditional drop area. + * Use together with on:dropOnItem + */ + export let itemDropEnabled: boolean | ((item: NavigationItem) => boolean) = + false; + + const navigationId: string = 'navigation'; + + // If the viewport changes to show the sidebar while menu is expanded, update menu store. + // This ensures `aria-hidden="false"` on the main section and player bar. + $: if (!$sidebarIsHidden) { + $menuIsExpanded = false; + } + + let navigatableContainer: HTMLElement; +</script> + +<nav + data-testid="navigation" + class="navigation" + class:is-transitioning={$menuIsTransitioning} + class:is-expanded={$menuIsExpanded} + on:transitionend|self={() => ($menuIsTransitioning = false)} +> + <div class="navigation__header"> + {#if $sidebarIsHidden} + <MenuIcon {navigationId} {translateFn} on:toggleExpansion /> + <slot name="logo" /> + <slot name="auth" /> + {:else} + <slot name="logo" /> + <slot name="search" /> + {/if} + </div> + + <div + data-testid="navigation-content" + class="navigation__content" + id={navigationId} + aria-hidden={$sidebarIsHidden && !$menuIsExpanded ? 'true' : 'false'} + > + <!-- svelte-ignore a11y-no-static-element-interactions --> + <div + bind:this={navigatableContainer} + class="navigation__scrollable-container" + > + {#if typeof window === 'undefined' || navigatableContainer} + <NavigationItems + type="primary" + {items} + {translateFn} + {currentTab} + visibilityPreferencesKey={null} + header={null} + listGroupElement={navigatableContainer} + on:menuItemClick + /> + + {#if libraryItems.length > 0} + <div + use:allowDrop={libraryDropEnabled && { + dropEnabled: true, + onDrop: (dropData) => + dispatch('libraryDrop', dropData), + }} + data-testid="navigation-library-section" + > + <NavigationItems + type="library" + header={translateFn('AMP.Shared.Library')} + items={libraryItems} + listGroupElement={navigatableContainer} + {visibilityPreferencesKey} + {translateFn} + {currentTab} + {itemDragEnabled} + {itemDropEnabled} + on:dropOnItem + on:menuItemClick + /> + </div> + {/if} + + {#if personalizedItems.length > 0} + <NavigationItems + type="personalized" + header={personalizedItemsHeader} + items={personalizedItems} + visibilityPreferencesKey={null} + listGroupElement={navigatableContainer} + {translateFn} + {currentTab} + {getItemDragData} + {itemDragEnabled} + {itemDropEnabled} + {headerDropEnabled} + on:menuItemClick + on:dropOnItem + on:dropOnHeader + /> + {/if} + {/if} + <slot name="after-navigation-items" /> + </div> + + <div class="navigation__native-cta"> + <slot name="native-cta" /> + </div> + </div> +</nav> + +<style lang="scss"> + @use '@amp/web-shared-styles/app/core/globalvars' as *; + + // Default Values + $amp-nav-element-transition: height 0.56s cubic-bezier(0.52, 0.16, 0.24, 1); + + .navigation { + width: 100%; + display: flex; + flex-direction: column; + z-index: var(--z-web-chrome); + + @media (--range-sidebar-hidden-down) { + height: $global-header-mobile-contracted-height; + position: fixed; + overflow: hidden; + background-color: var(--mobileNavigationBG); + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); + + &.is-expanded { + height: 100%; + } + + // The transition property should only be applied when the + // navigation is actively being set to expand / contract. + // This is to prevent unintended transitions when moving from + // `sidebar:visible` to `sidebar:hidden`. + &.is-transitioning { + transition: $amp-nav-element-transition; + } + + // Remove transition when user prefers reduced motion + @media (prefers-reduced-motion: 'reduce') { + transition: none; + } + } + + @media (--sidebar-visible) { + height: 100%; + position: relative; + background-color: var(--navSidebarBG); + box-shadow: none; + border-inline-end: 1px solid var(--labelDivider); + } + } + + .navigation__header { + display: grid; + + // Mobile styles -- horizontal icons + @media (--range-sidebar-hidden-down) { + grid-template-columns: repeat(3, 1fr); + align-items: center; + margin-inline-start: 12px; + margin-inline-end: 11px; + + // Position each child correctly relative to grid cell + & > :global(:nth-child(1)) { + justify-self: start; + } + + & > :global(:nth-child(2)) { + justify-self: center; + } + + & > :global(:nth-child(3)) { + justify-self: end; + } + } + + // Desktop styles -- stacked logo + search + @media (--sidebar-visible) { + :global(.search-input-wrapper) { + min-height: $web-search-input-height; + } + } + } + + .navigation__content { + display: flex; + flex-direction: column; + overflow: hidden; + + // Explicitly set sidebar content container width to include border, per spec + @media (--sidebar-visible) { + width: var(--web-navigation-width); + flex: 1; + } + } + + .navigation__scrollable-container { + overflow-y: auto; + scroll-behavior: smooth; + + @media (--range-sidebar-hidden-down) { + padding-top: 23px; + } + + @media (--sidebar-visible) { + flex: 1; // Push CTA to bottom of sidebar + } + } +</style> diff --git a/shared/components/src/components/Navigation/NavigationItems.svelte b/shared/components/src/components/Navigation/NavigationItems.svelte new file mode 100644 index 0000000..5d9dcf7 --- /dev/null +++ b/shared/components/src/components/Navigation/NavigationItems.svelte @@ -0,0 +1,281 @@ +<script lang="ts"> + import { createEventDispatcher, onMount } from 'svelte'; + import type { Writable } from 'svelte/store'; + import type { NavigationId } from '@amp/web-app-components/src/types'; + import { menuIsExpanded } from '@amp/web-app-components/src/components/Navigation/store/menu-state'; + import type { NavigationItem } from '@amp/web-app-components/src/components/Navigation/types'; + import { + isSameTab, + getItemComponent, + } from '@amp/web-app-components/src/components/Navigation/utils'; + import Folder from './Folder.svelte'; + import { shouldShowNavigationItem } from '@amp/web-app-components/src/utils/should-show-navigation-item'; + import allowDrop from '@amp/web-app-components/src/actions/allow-drop'; + import { listKeyboardAccess } from '@amp/web-app-components/src/actions/list-keyboard-access'; + + let isEditing = false; + + /** + * The local storage key with the prefs of what library items to be visible + */ + export let visibilityPreferencesKey: string | null = null; + + /** + * The navigation tabs to display. + */ + export let items: NavigationItem[]; + + /** + * The type of navigation item to display + */ + export let type: string | null = null; + + /** + * Retrieve UI translations for a given localization key. + */ + export let translateFn: (key: string) => string; + + /** + * The navigation title header -- this appears right over the items. + */ + export let header: string | null; + + /** + * The store containing the currently selected tab. + */ + export let currentTab: Writable<NavigationId | null>; + + /** + * Boolean or method to indicate if it allows drop on header + */ + export let headerDropEnabled: boolean | ((type: string) => boolean) = false; + + /** + * Optional function to map item to drag data + */ + export let getItemDragData: (item: NavigationItem) => any = null; + + /** + * Boolean or method to indicate if it allows dragging an item + */ + export let itemDragEnabled: boolean | ((item: NavigationItem) => boolean) = + false; + + /** + * Boolean or method to indicate if it allows drop on an item + */ + export let itemDropEnabled: boolean | ((item: NavigationItem) => boolean) = + false; + + export let listGroupElement: HTMLElement = null; + + const dispatch = createEventDispatcher(); + + const setCurrentActiveItem = (event: CustomEvent<{ id: NavigationId }>) => { + currentTab.set(event.detail.id); + + // Always immediately close the menu (in XS breakpoint) + menuIsExpanded.set(false); + + dispatch('menuItemClick', event.detail); + }; + + $: ariaRole = items.find((item) => item?.children) ? 'tree' : null; + $: containingClassName = type ? `navigation-items--${type}` : ''; + $: isHeaderDropEnabled = + typeof headerDropEnabled === 'function' + ? headerDropEnabled(type) + : headerDropEnabled; + + function toggleEdit() { + isEditing = !isEditing; + } + + let data = {}; + + function visibilityChangeItem(storageKey: string) { + const currentSetting = data[storageKey]; + data = { ...data, [storageKey]: !currentSetting }; + localStorage.setItem(visibilityPreferencesKey, JSON.stringify(data)); + } + + function displayOptions() { + const current = localStorage?.getItem(visibilityPreferencesKey); + + if (current) { + data = JSON.parse(current); + } else { + data = Object.fromEntries( + items.map(({ storageKey }) => [storageKey, true]), + ); + localStorage?.setItem( + visibilityPreferencesKey, + JSON.stringify(data), + ); + } + } + + onMount(() => { + if (visibilityPreferencesKey) { + displayOptions(); + } + }); +</script> + +<div + data-testid={`navigation-items-${type}`} + class={`navigation-items ${containingClassName}`} +> + {#if header} + <div + aria-hidden="true" + class="navigation-items__header" + class:drop-reset={isHeaderDropEnabled} + data-testid={`navigation-items-header`} + use:allowDrop={isHeaderDropEnabled && + !isEditing && { + dropEnabled: true, + onDrop: (dropData) => + dispatch('dropOnHeader', { type, dropData }), + }} + > + <span> + {header} + </span> + {#if visibilityPreferencesKey} + <button + data-testid="navigation-items__toggler" + on:click={toggleEdit} + class="edit-toggle-button" + class:is-editing={isEditing} + > + {#if isEditing} + <span data-testid="navigation-items__editing-done" + >{translateFn('AMP.Shared.Done')}</span + > + {:else} + <span data-testid="navigation-items__editing-edit" + >{translateFn('AMP.Shared.Edit')}</span + > + {/if} + </button> + {/if} + </div> + {/if} + + <ul + role={ariaRole} + aria-label={header} + class="navigation-items__list" + use:listKeyboardAccess={{ + listItemClassNames: + 'navigation-item__link, navigation-item__folder, click-action', + isRoving: true, + listGroupElement: listGroupElement, + }} + > + {#each items as item (item.id)} + {#if item.id.type === 'folder'} + <Folder + item={{ ...item }} + {isEditing} + {currentTab} + {translateFn} + {getItemDragData} + {itemDragEnabled} + {itemDropEnabled} + on:selectItem={setCurrentActiveItem} + on:dropOnItem + /> + {:else if shouldShowNavigationItem(visibilityPreferencesKey, isEditing, data, item.storageKey)} + <svelte:component + this={getItemComponent(item)} + {item} + selected={isSameTab(item.id, $currentTab)} + on:selectItem={setCurrentActiveItem} + isChecked={data && data[item.storageKey]} + {isEditing} + {translateFn} + getDragData={getItemDragData} + dragEnabled={itemDragEnabled} + dropEnabled={itemDropEnabled} + on:drop={({ detail: dropData }) => + dispatch('dropOnItem', { item, dropData })} + on:visibilityChangeItem={() => + visibilityChangeItem(item.storageKey)} + /> + {/if} + {/each} + </ul> +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/app/core/globalvars' as *; + @use 'amp/stylekit/core/mixins/overflow-bleed' as *; + + .navigation-items { + grid-area: navigation-items; + padding-top: 7px; + } + + .navigation-items--primary { + padding-top: 9px; + } + + .navigation-items--library { + grid-area: library-navigation-items; + } + + .navigation-items--personalized { + grid-area: personalized-navigation-items; + } + + .navigation-items__header { + color: var(--systemSecondary); + padding: 15px 26px 3px; + display: flex; + justify-content: space-between; + font: var(--body-emphasized); + + @media (--sidebar-visible) { + margin: 0 20px -3px; + padding: 4px 6px; + border-radius: 6px; + font: var(--footnote-emphasized); + } + + &:global(.is-drag-over) { + --drag-over-color: white; + color: var(--drag-over-color); + background-color: var(--selectionColor); + } + } + + .edit-toggle-button { + color: var(--systemPrimary); + + @media (--sidebar-visible) { + opacity: 0; + transition: var(--global-transition); + + &:focus { + opacity: 1; + } + } + } + + .edit-toggle-button.is-editing, + .navigation-items__header:hover .edit-toggle-button { + opacity: 1; + } + + .navigation-items__list { + font: var(--title-2); + padding: 3px 26px; + + @media (--sidebar-visible) { + font: var(--title-3); + padding: 0 $web-navigation-inline-padding 9px; + } + } +</style> diff --git a/shared/components/src/components/Navigation/store/menu-state.ts b/shared/components/src/components/Navigation/store/menu-state.ts new file mode 100644 index 0000000..9f36519 --- /dev/null +++ b/shared/components/src/components/Navigation/store/menu-state.ts @@ -0,0 +1,4 @@ +import { writable } from 'svelte/store'; + +export const menuIsExpanded = writable(false); +export const menuIsTransitioning = writable(false); diff --git a/shared/components/src/components/Navigation/utils.ts b/shared/components/src/components/Navigation/utils.ts new file mode 100644 index 0000000..87c8e59 --- /dev/null +++ b/shared/components/src/components/Navigation/utils.ts @@ -0,0 +1,27 @@ +import type { ComponentType } from 'svelte'; +import type { + BaseNavigationItem, + NavigationId, +} from '@amp/web-app-components/src/types'; +import Item from './Item.svelte'; + +export function isSameTab( + a: NavigationId | null, + b: NavigationId | null, +): boolean { + if (a === null || b === null) { + return false; + } + + // Need deep object equality for things like + // { kind: 'playlist', id: '123' } + try { + return JSON.stringify(a) === JSON.stringify(b); + } catch { + return false; + } +} + +export function getItemComponent(item: BaseNavigationItem): ComponentType { + return item.component ?? Item; +} diff --git a/shared/components/src/components/Rating/Rating.svelte b/shared/components/src/components/Rating/Rating.svelte new file mode 100644 index 0000000..de8e478 --- /dev/null +++ b/shared/components/src/components/Rating/Rating.svelte @@ -0,0 +1,141 @@ +<script lang="ts"> + import type { RatingCountsList } from './types'; + import { calculatePercentages } from './utils'; + import FilledStarIcon from '@amp/web-app-components/assets/icons/star-filled.svg'; + + /** + * @name Rating + * + * @description + * This implements the standard rating lockup showing aggregate ratings + * + * Design: + * https://pd-hi.apple.com/viewvc/Common/Modules/macOS/Podcasts/Lockups/Review%20Lockup.png?revision=57299 + * + * Aria Discussions: + * https://quip-apple.com/yvZaAbJMnAK0#JeB9CAOHPMd + * + * POTW difference: + * No write a review on the web + */ + + export let averageRating: number | string; + export let ratingCount: number; + export let ratingCountText: string; + export let ratingCountsList: RatingCountsList; + export let totalText: string; + + $: ratingPercentList = calculatePercentages(ratingCountsList, ratingCount); +</script> + +<div class="amp-rating" data-testid="rating-component"> + <div class="stats" aria-label={`${averageRating} ${totalText}`}> + <div class="stats__main" data-testid="amp-rating__average-rating"> + {averageRating} + </div> + <div class="stats__total" data-testid="amp-rating__total-text"> + {totalText} + </div> + </div> + <div class="numbers"> + <div class="numbers__star-graph"> + {#each ratingPercentList as value, i} + <div + class={`numbers__star-graph__row row-${i}`} + aria-label={`${5 - i} star, ${value}%`} + > + <!-- TODO: rdar://79873131 (Localize Aria Label in Rating Shared Component) --> + <div class="numbers__star-graph__row__stars"> + <!-- In order to display the 5 stars to 1 stars we use the 5 - index as 0 index means 1 star and so on --> + {#each { length: 5 - i } as _} + <div class="star"><FilledStarIcon /></div> + {/each} + </div> + <div class="numbers__star-graph__row__bar"> + <div + class="numbers__star-graph__row__bar__foreground" + style={`width: ${value}%`} + data-testid={`star-row-${5 - i}`} + /> + </div> + </div> + {/each} + </div> + <div class="numbers__count" data-testid="amp-rating__rating-count-text"> + {ratingCountText} + </div> + </div> +</div> + +<style lang="scss"> + .amp-rating { + display: flex; + } + + .stats { + margin-right: 10px; + flex: 0 80px; + } + + .stats__main { + font-size: 50px; + font-weight: bold; + display: flex; + justify-content: center; + } + + .stats__total { + display: flex; + justify-content: center; + color: var(--systemSecondary-text); + font: var(--body-emphasized); + } + + .numbers { + width: 100%; + } + + .numbers__count { + display: flex; + align-items: flex-end; + justify-content: flex-end; + color: var(--systemSecondary-text); + } + + .numbers__star-graph { + margin-top: 12px; + line-height: 9px; + } + + .numbers__star-graph__row { + display: flex; + width: 100%; + } + + .numbers__star-graph__row__stars { + display: flex; + min-width: 45px; + font-size: 8px; + justify-content: flex-end; + margin-right: 6px; + + & :global(.star) { + fill: var(--systemSecondary); + width: 8px; + height: 8px; + } + } + + .numbers__star-graph__row__bar { + height: 2px; + width: 100%; + background: var(--systemQuaternary); + margin-top: 3px; + } + + .numbers__star-graph__row__bar__foreground { + height: 2px; + background: var(--ratingBarColor, --systemSecondary); + max-width: 100%; + } +</style> diff --git a/shared/components/src/components/Rating/utils.ts b/shared/components/src/components/Rating/utils.ts new file mode 100644 index 0000000..cb909b4 --- /dev/null +++ b/shared/components/src/components/Rating/utils.ts @@ -0,0 +1,10 @@ +import type { RatingCountsList } from './types'; + +// eslint-disable-next-line import/prefer-default-export +export const calculatePercentages = ( + ratingValues: RatingCountsList, + totalCount: number, +): RatingCountsList => + ratingValues?.map((value: number) => + Math.round((value / totalCount) * 100), + ) || []; diff --git a/shared/components/src/components/SearchInput/SearchInput.svelte b/shared/components/src/components/SearchInput/SearchInput.svelte new file mode 100644 index 0000000..1c34ef9 --- /dev/null +++ b/shared/components/src/components/SearchInput/SearchInput.svelte @@ -0,0 +1,530 @@ +<script lang="ts"> + import { createEventDispatcher } from 'svelte'; + import type { Writable } from 'svelte/store'; + import type { NavigationId } from '@amp/web-app-components/src/types'; + import clickOutside from '@amp/web-app-components/src/actions/click-outside'; + import SearchSuggestions from '@amp/web-app-components/src/components/SearchSuggestions/SearchSuggestions.svelte'; + import type { NavigationItem } from '@amp/web-app-components/src/components/Navigation/types'; + import { + ClearEventLocation, + SEARCH_EVENTS, + } from '@amp/web-app-components/src/constants'; + import { getUpdatedFocusedIndex } from '@amp/web-app-components/src/utils/getUpdatedFocusedIndex'; + import { debounce } from '@amp/web-app-components/src/utils/debounce'; + import type { + HighlightedSearchSuggestion, + SearchSuggestion, + } from '@amp/web-app-components/src/utils/processTextSearchSuggestion'; + import SearchIcon from '@amp/web-app-components/assets/icons/search.svg'; + + const { + SEARCH_INPUT_HAS_FOCUS, + MAKE_SEARCH_QUERY_FROM_SUGGESTION, + MAKE_SEARCH_QUERY_FROM_INPUT, + CLICKED_OUTSIDE_SUGGESTIONS, + CLICKED_OUTSIDE, + RESET_SEARCH_INPUT, + MENU_ITEM_CLICK, + SHOW_SEARCH_SUGGESTIONS, + } = SEARCH_EVENTS; + + $: debouncedHandleSearchInput = debounce(handleSearchInput, 100); + + /** + * The translate fn to be used to handle localization + * @type {function} + */ + export let translateFn: (key: string) => string; + + /** + * The handler to be executed that retrieves suggestions for a given term + * @type {function} + */ + export let getSuggestionsForPartialTerm: ( + partialTerm: string, + ) => Promise<SearchSuggestion[]> = async () => []; + + /** + * The store containing the currently selected tab. + */ + export let currentTab: Writable<NavigationId | null>; + + /** + * The pre-filled value of the text field + */ + export let defaultValue: string | null = null; + + /** + * The menu item that should be selected when a search is performed or the + * search field receives focus while not on this item. + */ + export let menuItem: NavigationItem; + + /** + * Optional argument to disable search suggestions completely + */ + export let hideSuggestions = false; + + let suggestions = []; + let cachedSuggestions = []; + let partialTerm = !!defaultValue ? defaultValue : ''; + let focusedSearchSuggestionIndex = null; + let searchInputElement: HTMLInputElement; + let showSuggestion = false; + let showCancelButton = false; + + $: showSuggestion = suggestions?.length > 0; + $: handleShowSuggestion(showSuggestion); + + const dispatch = createEventDispatcher<{ + resetSearchInput: null; // no details returned + menuItemClick: NavigationItem; + searchInputHasFocus: null; // no details returned + makeSearchQueryFromInput: { term: string }; + // Unfortunately SearchSuggestions uses Array<any> so no way to fully type this. + // rdar://137049269 ((Shared/Components) Create Types for SearchSuggestions component) + makeSearchQueryFromSuggestion: { suggestion: any }; + clickedOutsideSuggestions: null; // no details returned + clickedOutside: null; // no details returned + clear: { from: ClearEventLocation }; + showSearchSuggestions: { showSearchSuggestions: boolean }; + }>(); + + function resetSearchInputState() { + searchInputElement.value = ''; + partialTerm = ''; + suggestions = []; + cachedSuggestions = []; + focusedSearchSuggestionIndex = null; + dispatch(RESET_SEARCH_INPUT); + } + + /** + * We use a click focus here (instead of input focus) as a + * lighter touch way to detect interaction with the search input. + * + * See additional explanation here: + * rdar://83511986 (JMOTW AX Music: Focussing on Search Field should not trigger a Context Change in Routing) + */ + function handleSearchInputClickFocus() { + showCancelButton = true; + const currentTerm = searchInputElement.value; + if (currentTerm === partialTerm && cachedSuggestions.length > 0) { + suggestions = cachedSuggestions; + cachedSuggestions = []; + } + + // Only switch to the search tab if we aren't already on it + if ($currentTab !== menuItem.id) { + currentTab.set(menuItem.id); + dispatch(MENU_ITEM_CLICK, menuItem); + } + + dispatch(SEARCH_INPUT_HAS_FOCUS); + } + + function handleSearchInputSubmit(event: SubmitEvent) { + const term = searchInputElement.value; + event.preventDefault(); + + if (term) { + dispatch(MAKE_SEARCH_QUERY_FROM_INPUT, { + term, + }); + + // Submitting a search always goes to the search tab + currentTab.set(menuItem.id); + + // Cache the current list of suggestions in case searchInputElement + // becomes focused again. + cachedSuggestions = suggestions; + suggestions = []; + focusedSearchSuggestionIndex = null; + + // Also hides the suggestions if visible + searchInputElement.blur(); + } + } + + function onSearchSuggestionChosen(suggestion: HighlightedSearchSuggestion) { + dispatch(MAKE_SEARCH_QUERY_FROM_SUGGESTION, { suggestion }); + + // Clicking on a search suggestion always goes to the search tab + currentTab.set(menuItem.id); + + resetSearchInputState(); + searchInputElement.value = suggestion.displayTerm; + } + + function onSearchSuggestionFocused(index: number) { + focusedSearchSuggestionIndex = index; + } + + function containerHandleKeyDown(event: KeyboardEvent) { + switch (event.key) { + case 'ArrowDown': + case 'ArrowUp': + event.preventDefault(); + break; + } + } + + function containerHandleKeyUp(event: KeyboardEvent) { + switch (event.key) { + case 'ArrowDown': + focusedSearchSuggestionIndex = getUpdatedFocusedIndex( + 1, + focusedSearchSuggestionIndex, + suggestions.length, + ); + break; + + case 'ArrowUp': + focusedSearchSuggestionIndex = getUpdatedFocusedIndex( + -1, + focusedSearchSuggestionIndex, + suggestions.length, + ); + break; + + case 'Escape': + resetSearchInputState(); + break; + + case 'Tab': + case 'Control': + case 'Alt': + case 'Meta': + case 'Shift': + case ' ': // Spacebar + // Don't do anything for remaining navigation keys. + break; + + default: + // If this event is not a navigational key, or not a Tab the focus is returned to the input + // allowing the user to type with the this key stroke. This is necesasry because + // VoiceOver first lands on the container and not on the input field. + searchInputElement.focus(); + } + + event.preventDefault(); + } + + async function handleSearchInput(input: HTMLInputElement) { + const searchInput = input ?? searchInputElement; + partialTerm = searchInput.value; + + if (!partialTerm) { + suggestions = []; + return; + } + + let _suggestions = await getSuggestionsForPartialTerm(partialTerm); + cachedSuggestions = _suggestions; + + // rdar://93009223 (JMOTW: Hitting enter in search field before suggestions loads leaves suggestions stuck) + // + // We only want to show suggestions here if the input is focused. + // Without this condition, suggestions will show up after enter is pressed if + // it takes too long for the api to return + if (document.activeElement === searchInput) { + suggestions = _suggestions; + cachedSuggestions = []; + } + } + + /** + * We don't want `menuItemClick` to also get debounced + * Extrapolating logic here to handle the route switch as well as the input delay + * + * rdar://83511986 (AX Music: Focussing on Search Field should not trigger a Context Change in Routing) + * + * TODO: we currently have no way to re-render the search landing page if the currently selected tab + * is already on the search tab. The best solution (as of now) to re-render the search landing page + * is to check if the input value is empty. + * + * rdar://91073241 (JMOTW: Search - Find a way to stop re-renders of search landing page) + */ + function handleSearchInputActivity(e: Event) { + if ( + !(e instanceof InputEvent) && + (e.target as HTMLInputElement).value === '' + ) { + dispatch('clear', { from: ClearEventLocation.Input }); + } + const shouldDispatchMenuClick = + $currentTab !== menuItem.id || searchInputElement.value === ''; + + // From svelte docs: + // The store value gets set to the value of the argument if + // the store value is not already equal to it. + // https://svelte.dev/docs#run-time-svelte-store-writable + currentTab.set(menuItem.id); + + if (shouldDispatchMenuClick) { + menuItem.opaqueData = () => ({ from: 'searchInputClear' }); + dispatch(MENU_ITEM_CLICK, menuItem); + } + + debouncedHandleSearchInput(e.target as HTMLInputElement); + } + + function handleClickOutside(event: Event) { + const element = (event.target as HTMLElement) || null; + + const eventPath = event.composedPath ? event.composedPath() : []; + const didEventHappenInContextMenu = eventPath.some( + (item) => + 'nodeName' in item && item.nodeName === 'AMP-CONTEXTUAL-MENU', + ); + + // dont close menu if interacting with context menu + if ( + (element && element.nodeName === 'AMP-CONTEXTUAL-MENU') || + didEventHappenInContextMenu + ) { + return; + } + + if (suggestions.length > 0) { + // Cache the current list of suggestions in case searchInputElement + // becomes focused again. + cachedSuggestions = suggestions; + + // Clear out the suggestions so the suggestions disappear + suggestions = []; + + dispatch(CLICKED_OUTSIDE_SUGGESTIONS); + } + + showCancelButton = false; + dispatch(CLICKED_OUTSIDE); + } + + function handleShowSuggestion(curShowSuggestions: boolean) { + dispatch(SHOW_SEARCH_SUGGESTIONS, { + showSearchSuggestions: curShowSuggestions, + }); + } + + function handleCancelButton() { + showCancelButton = false; + searchInputElement.value = ''; + dispatch('clear', { from: ClearEventLocation.Cancel }); + } +</script> + +<div + data-testid="amp-search-input" + aria-controls="search-suggestions" + aria-expanded={suggestions && suggestions.length > 0} + aria-haspopup="listbox" + aria-owns="search-suggestions" + class="search-input-container" + tabindex="-1" + role={showSuggestion ? 'combobox' : ''} + use:clickOutside={handleClickOutside} + on:keydown={containerHandleKeyDown} + on:keyup={containerHandleKeyUp} +> + <div class="flex-container"> + <form + role="search" + id="search-input-form" + on:submit={handleSearchInputSubmit} + > + <SearchIcon class="search-svg" aria-hidden="true" /> + + <input + value={defaultValue} + aria-activedescendant={Number.isInteger( + focusedSearchSuggestionIndex, + ) && focusedSearchSuggestionIndex >= 0 + ? `search-suggestion-${focusedSearchSuggestionIndex}` + : undefined} + aria-autocomplete="list" + aria-multiline="false" + aria-controls="search-suggestions" + placeholder={translateFn('AMP.Shared.SearchInput.Placeholder')} + spellcheck={false} + autocomplete="off" + autocorrect="off" + autocapitalize="off" + type="search" + class="search-input__text-field" + bind:this={searchInputElement} + data-testid="search-input__text-field" + on:input={handleSearchInputActivity} + on:click={handleSearchInputClickFocus} + /> + </form> + + {#if showCancelButton} + <div + class="search-input__cancel-button-container" + data-testid="search-input__cancel-button-container" + > + <button + data-testid="search-input__cancel-button" + on:click={handleCancelButton} + aria-label={translateFn('FUSE.Search.Cancel')} + > + {translateFn('FUSE.Search.Cancel')} + </button> + </div> + {/if} + </div> + + <div data-testid="search-scope-bar"><slot name="searchScopeBar" /></div> + + <!-- https://github.com/sveltejs/svelte/issues/5604 --> + {#if !hideSuggestions && suggestions && suggestions.length > 0} + {#if $$slots['suggestion']} + <SearchSuggestions + on:suggestionClicked={(e) => + onSearchSuggestionChosen(e.detail.suggestion)} + on:suggestionFocused={(e) => + onSearchSuggestionFocused(e.detail.index)} + {suggestions} + focusedSuggestionIndex={focusedSearchSuggestionIndex} + {translateFn} + > + <svelte:fragment slot="suggestion" let:suggestion> + <slot name="suggestion" {suggestion} /> + </svelte:fragment> + </SearchSuggestions> + {:else} + <SearchSuggestions + on:suggestionClicked={(e) => + onSearchSuggestionChosen(e.detail.suggestion)} + on:suggestionFocused={(e) => + onSearchSuggestionFocused(e.detail.index)} + {suggestions} + focusedSuggestionIndex={focusedSearchSuggestionIndex} + {translateFn} + /> + {/if} + {/if} +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use '@amp/web-shared-styles/app/core/mixins/focus' as *; + + $search-input-text-height: 32px; + $search-svg-size-hide-sidebar: 12px; + + .search-input-container { + @media (--sidebar-visible) { + position: relative; + z-index: var(--z-default); + } + + @media (--range-sidebar-hidden-down) { + width: 100%; + } + + :global(.search-svg) { + width: 16px; + height: 16px; + top: 10px; + bottom: 10px; + position: absolute; + fill: var(--searchBoxIconFill); + inset-inline-start: 10px; + z-index: var(--z-default); + + @media (--sidebar-visible) { + width: $search-svg-size-hide-sidebar; + height: $search-svg-size-hide-sidebar; + } + } + + :global(.search-suggestion-svg) { + fill: var(--searchBoxIconFill); + } + } + + .search-input__text-field { + background-color: var(--pageBG); + border-radius: 4px; + border-style: solid; + border-width: 1px; + border-color: var(--searchBarBorderColor); + color: var(--systemPrimary-vibrant); + font-size: 12px; + font-weight: 400; + height: $search-input-text-height; + letter-spacing: 0; + line-height: 1.25; + padding-top: 6px; + padding-bottom: 5px; + width: 100%; + padding-inline-end: 5px; + + @media (--range-sidebar-hidden-down) { + height: 38px; + border-radius: 9px; + padding-inline-start: 34px; + font: var(--title-3-tall); + font-size: 16px; + } + + @media (--sidebar-visible) { + padding-inline-start: 28px; + } + } + + input::-webkit-search-decoration, + input::-webkit-search-results-decoration { + appearance: none; + } + + input::placeholder { + color: var(--systemTertiary-vibrant); + + @media (prefers-color-scheme: dark) { + color: var(--systemSecondary-vibrant); + } + } + + input:focus { + @include focus-shadow; + } + + input::-webkit-search-cancel-button { + $cancelButtonSize: 14px; + appearance: none; + background-position: center; + background-repeat: no-repeat; + background-size: $cancelButtonSize $cancelButtonSize; + height: $cancelButtonSize; + width: $cancelButtonSize; + background-image: url('/assets/icons/sidebar-searchfield-close-on-light.svg'); + + @media (prefers-color-scheme: dark) { + background-image: url('/assets/icons/sidebar-searchfield-close-on-dark.svg'); + } + } + + .search-input__cancel-button-container { + align-self: center; + color: var(--keyColor); + font: var(--title-3-tall); + margin-inline-start: 14px; + + @media (--sidebar-visible) { + display: none; + } + } + + .flex-container { + @media (--range-sidebar-hidden-down) { + display: flex; + + form { + flex-grow: 1; + } + } + } +</style> diff --git a/shared/components/src/components/SearchSuggestions/SearchSuggestions.svelte b/shared/components/src/components/SearchSuggestions/SearchSuggestions.svelte new file mode 100644 index 0000000..c3140ae --- /dev/null +++ b/shared/components/src/components/SearchSuggestions/SearchSuggestions.svelte @@ -0,0 +1,331 @@ +<script lang="ts"> + import focusNode from '@amp/web-app-components/src/actions/focus-node'; + import { onMount, onDestroy } from 'svelte'; + import { createEventDispatcher } from 'svelte'; + import { SEARCH_EVENTS } from '@amp/web-app-components/src/constants'; + import type { HighlightedSearchSuggestion } from '@amp/web-app-components/src/utils/processTextSearchSuggestion'; + import TextSearchSuggestion from '@amp/web-app-components/src/components/TextSearchSuggestion/TextSearchSuggestion.svelte'; + + /** + * The list of suggestions + * @type {Array} + */ + export let suggestions: Array<any> = []; + + /** + * The current focused suggestion index + * @type {number} + */ + export let focusedSuggestionIndex: number | null = null; + + /** + * The translate fn to be used to handle localization + * @type {function} + */ + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + + const dispatch = createEventDispatcher(); + + let searchSuggestionsScrimElement: HTMLDivElement; + let domPortalElement: HTMLDivElement; + + onMount(() => { + domPortalElement = document.createElement('div'); + domPortalElement.className = 'portal'; + domPortalElement.appendChild(searchSuggestionsScrimElement); + + // All onyx based apps use `.app-container` as top level of app elements. + // For z-indexing to be correct we need to create portal at same level as app. + const appTarget = + document.querySelector('.app-container') ?? document.body; + appTarget.appendChild(domPortalElement); + + // this is a cleanup task, same as 'onDestroy', + // if for whatever reason the onMount becomes async + // move this into an `onDestroy` + return () => { + if (domPortalElement) { + appTarget.removeChild(domPortalElement); + } + }; + }); + + function handleSuggestionClicked(suggestion: HighlightedSearchSuggestion) { + dispatch(SEARCH_EVENTS.SUGGESTION_CLICKED, { suggestion }); + } + + function handleSuggestionKeyUp( + suggestion: HighlightedSearchSuggestion, + event: KeyboardEvent, + ) { + switch (event.key) { + case 'Enter': + case ' ': // Spacebar + dispatch(SEARCH_EVENTS.SUGGESTION_CLICKED, { suggestion }); + break; + } + } + + function handleSuggestionFocused( + suggestion: HighlightedSearchSuggestion, + index: number, + ) { + dispatch(SEARCH_EVENTS.SUGGESTION_FOCUSED, { suggestion, index }); + } +</script> + +<ul + aria-label={translateFn('AMP.Shared.SearchInput.Suggestions')} + role="listbox" + data-testid="search-suggestions" + id="search-suggestions" + class="search-suggestions" +> + {#each suggestions as suggestion, index} + <!-- + Events using `self` modifier have this in order to filter out + events that are directed to a child (i.e. pressing `Enter` or + focusing on a context menu button). + --> + <li + class="search-hint" + class:search-hint--text={suggestion.kind === 'text'} + class:search-hint--lockup={suggestion.kind !== 'text'} + use:focusNode={focusedSuggestionIndex} + data-index={index} + data-testid={`suggestion-index-${index}`} + role="option" + tabindex="0" + aria-selected={focusedSuggestionIndex === index ? true : undefined} + id={`search-suggestion-${index}`} + on:click={() => handleSuggestionClicked(suggestion)} + on:keyup|self={(e) => handleSuggestionKeyUp(suggestion, e)} + on:focusin|self={() => handleSuggestionFocused(suggestion, index)} + > + {#if $$slots['suggestion']} + <slot name="suggestion" {suggestion} /> + {:else} + <TextSearchSuggestion {suggestion} /> + {/if} + </li> + {/each} +</ul> + +<div + class="search-suggestions-scrim" + data-testid="search-suggestions-scrim" + bind:this={searchSuggestionsScrimElement} +/> + +<style lang="scss"> + @use 'amp/stylekit/core/mixins/browser-targets' as *; + @use 'amp/stylekit/core/mixins/materials' as *; + @use '@amp/web-shared-styles/app/core/globalvars' as *; + @use '@amp/web-shared-styles/app/core/mixins/absolute-center' as *; + + $search-hints-vertical-padding: 6px; + + @mixin search-hint-border { + &::before { + top: 0; + inset-inline-start: var(--searchHintBorderStart, 6px); + inset-inline-end: var(--searchHintBorderEnd, 6px); + position: absolute; + content: ''; + border-top: var(--keyline-border-style); + + @content; + } + } + + .search-suggestions { + margin-top: 12px; + + @media (--sidebar-visible) { + padding: $search-hints-vertical-padding 0; + margin-top: 0; + width: 302px; + // Calculate the distance from the top of the window so we can get the height right to allow it to scroll within the page + // with exactly 25px (our $-web-navigation-inline-padding sizing). + // 3px is the distance difference in the spec from the calculations we have here. + max-height: calc( + 100vh - #{$global-player-bar-height} - #{$web-search-input-height} - + #{$web-navigation-inline-padding} + 3px + ); + position: absolute; + top: 36px; + border-radius: 9px; + overflow-x: hidden; + overflow-y: auto; + border: $dialog-border; + box-shadow: $dialog-inset-shadow, $dialog-shadow; + text-align: start; + z-index: calc(var(--z-contextual-menus) + 2); + + @include system-standard-thick-material; + + li:not(.search-hint--text) { + &:focus-visible { + outline: none; // Hide default focus ring as background color serves as focus state + } + } + } + } + + @include target-safari { + // Safari Safari 14.1 fails to render contents of `search-hint--text`, with `background-filter`, when content does not overflow + // `search-hint--text` container. `1px` of extra negative `margin-bottom` and `padding-bottom` on last element, helps trigger overflow. + // This issue is not reproducible in Safari 14.2. + li:last-child { + margin-bottom: -$search-hints-vertical-padding - 1; + padding-bottom: $search-hints-vertical-padding + 1; + } + } + + .search-hint { + position: relative; + border-radius: var( + --global-border-radius-xsmall, + #{$global-border-radius-xsmall} + ); + z-index: var(--z-default); + + // Hover/focus styles for desktop only + @media (--sidebar-visible) { + &:hover, + &:focus-visible, + &:focus-within { + // Ensure favorited badge is visible when focused + --favoriteBadgeColor: white; + background-color: var(--keyColor); + outline: none; // Hide default focus ring as background color serves as focus state + + :global(svg) { + fill: white; + } + + // Applies to all text in child <span> tags -- works for text and lockup suggestions + :global(span) { + color: white; + } + } + } + } + + .search-hint--lockup { + @include search-hint-border; + + @media (--range-sidebar-hidden-down) { + --searchHintBorderStart: var( + --searchHintBorderStartOverride, + 68px + ); // Border starts after artwork. This is overridden using `:has` in child + --searchHintBorderEnd: calc(-1 * var(--bodyGutter)); + + // Show full divider before first child, and between text and lockup hints + &:first-child, + .search-hint--text + & { + --searchHintBorderStart: 0; + } + } + + @media (--sidebar-visible) { + $top-search-list-gutter: 6px; + width: calc(100% - #{$top-search-list-gutter * 2}); + margin-inline-start: $top-search-list-gutter; + margin-inline-end: $top-search-list-gutter; + + // Hide border on currently hovered/focused item + &:hover, + &:focus-visible, + &:focus-within { + &::before { + border-color: transparent; + } + } + + // Hide border on item directly after currently hovered/focused item + &:hover + &, + &:focus-visible + &, + &:focus-within + & { + &::before { + border-color: transparent; + } + } + } + } + + .search-hint--text { + align-items: center; + display: grid; + grid-template-columns: 20px auto; + + // Add borders between text search hints on sidebar hidden + @media (--range-sidebar-hidden-down) { + --searchHintBorderStart: 26px; // Border starts after search icon + --searchHintBorderEnd: calc(-1 * var(--bodyGutter)); + padding-block: 15px; + + @include search-hint-border; + + &:first-child { + --searchHintBorderStart: 0; + } + } + + @media (--sidebar-visible) { + grid-template-columns: 16px auto; + margin: 0 6px; + padding: 4px; + font: var(--body); + + &:focus-within { + background-color: var(--keyColor); + outline: none; // Hide default focus ring as background color serves as focus state + + :global(.search-suggestion-svg) { + fill: white; + } + + :global(span) { + color: white; + } + } + } + + :global(.search-suggestion-svg) { + justify-self: center; + align-self: start; + width: 16px; + height: 16px; + transform: translateY(4px); + + @media (--sidebar-visible) { + width: 11px; + height: 11px; + transform: translateY(2.5px); + } + } + + + .search-hint--lockup { + @media (--sidebar-visible) { + margin-top: 6px; // Add small margin between '.search-hint--text' and '.search-hint--lockup' on larger viewports per spec + } + } + } + + .search-suggestions-scrim { + @include absolute-center; + + @media (--range-sidebar-hidden-down) { + display: none; + } + + @media (--sidebar-visible) { + z-index: calc(var(--z-default) + 1); + } + } +</style> diff --git a/shared/components/src/components/Shelf/Nav.svelte b/shared/components/src/components/Shelf/Nav.svelte new file mode 100644 index 0000000..1fe3933 --- /dev/null +++ b/shared/components/src/components/Shelf/Nav.svelte @@ -0,0 +1,199 @@ +<script lang="ts"> + import type { ArrowOffset } from '@amp/web-app-components/src/components/Shelf/types'; + import ChevronCompactLeft from '@amp/web-app-components/assets/shelf/chevron-compact-left.svg'; + import { createEventDispatcher } from 'svelte'; + + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + export let headerHeight: number; + export let arrowOffset: ArrowOffset; + export let hasNextPage: boolean; + export let hasPreviousPage: boolean; + export let isRTL: boolean; + + $: hasNavArrows = hasPreviousPage || hasNextPage; + + // Adjusting arrows to center on content. + // This is a fallback for browsers that don't support CSS anchor positioning. + $: addSpaceForHeader = (() => { + let offsetStyle = '0px'; + + // Custom adjustment provided by user + if (arrowOffset && arrowOffset.length) { + arrowOffset.forEach(({ direction, offset }) => { + if (direction == 'top') { + offsetStyle = ` + ${offset}px; + `; + } else { + offsetStyle = ` + calc(${offset}px * -1); + `; + } + }); + } + // Adjust for header + if (headerHeight) { + // adjust nav height to account for header + offsetStyle = ` + ${headerHeight}px; + `; + } + + return offsetStyle; + })(); + + const NAV = { + PREVIOUS: 'previous', + NEXT: 'next', + } as const; + + const dispatch = createEventDispatcher(); + const handleNextPage = () => dispatch(NAV.NEXT); + const handlePreviousPage = () => dispatch(NAV.PREVIOUS); + + $: NEXT_ARROW_PROPS = { + disabled: !hasNextPage, + 'aria-label': translateFn('AMP.Shared.NextPage'), + }; + + $: PREV_ARROW_PROPS = { + disabled: !hasPreviousPage, + 'aria-label': translateFn('AMP.Shared.PreviousPage'), + }; + + $: rightArrowProps = isRTL ? PREV_ARROW_PROPS : NEXT_ARROW_PROPS; + $: rightClick = isRTL ? handlePreviousPage : handleNextPage; + + $: leftArrowProps = isRTL ? NEXT_ARROW_PROPS : PREV_ARROW_PROPS; + $: leftClick = isRTL ? handleNextPage : handlePreviousPage; +</script> + +{#if hasNavArrows} + <button + {...leftArrowProps} + type="button" + class="shelf-grid-nav__arrow shelf-grid-nav__arrow--left" + data-testId="shelf-button-left" + on:click={leftClick} + style="--offset: {addSpaceForHeader};" + > + <ChevronCompactLeft /> + </button> + <slot name="shelf-content" /> + <button + {...rightArrowProps} + type="button" + class="shelf-grid-nav__arrow shelf-grid-nav__arrow--right" + data-testId="shelf-button-right" + on:click={rightClick} + style="--offset: {addSpaceForHeader};" + > + <ChevronCompactLeft /> + </button> +{:else} + <slot name="shelf-content" /> +{/if} + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use './style/core.scss' as *; + + .shelf-grid-nav { + list-style: none; + margin: 0; + + ul { + list-style: none; + margin: 0; + } + } + + .shelf-grid-nav__arrow { + height: $shelf-grid-arrow-height; + width: $shelf-grid-arrow-width; + align-items: center; + border: none; + cursor: pointer; + display: flex; + justify-content: center; + overflow: hidden; + position: absolute; + top: 50%; + transition: $shelf-grid-nav-transition; + translate: 0 -50%; + border-radius: 6px; + + // Non GPU-accelerated layers must be below GPU-accelerated layers. + z-index: var(--z-default); + + // Fallback for browsers that don't support CSS anchor positioning + @supports not (top: anchor(--a center)) { + transform: translateY(calc(-50% + var(--offset))); + translate: none; + } + + // CSS Anchor Positioning to vertically center paddles with artwork + // Powerswoosh intentionally not targeted — doesn't have `shelf` class. + :global(.shelf:has(.shelf-grid__list--grid-rows-1)) & { + // Set `top` to align with center of first artwork in 1-row shelf. + // Targets anchor in `Shelf.svelte`. + top: anchor(--shelf-first-artwork center, 50%); + } + + :global(svg) { + width: 8.5px; + height: 30.5px; + fill: var(--systemSecondary); + } + + &:hover, + &:focus-visible { + text-decoration: none; + background: var(--systemQuinary); + + @media (prefers-color-scheme: dark) { + background: var(--systemQuaternary); + } + } + + &:active { + background: var(--systemQuaternary); + + @media (prefers-color-scheme: dark) { + background: var(--systemTertiary); + } + + :global(svg) { + fill: var(--systemPrimary); + } + } + + &:disabled { + cursor: default; + opacity: 0; + } + + // Paddles not used in xsmall viewport + @media (--range-xsmall-down) { + display: none; + } + } + + .shelf-grid-nav__arrow--right { + right: $shelf-grid-arrow-position; + scale: -1 1; // Flip icon horizontally + } + + .shelf-grid-nav__arrow--left { + left: $shelf-grid-arrow-position; + } + + @media (--range-xsmall-down) { + .shelf-grid-nav { + display: none; + } + } +</style> diff --git a/shared/components/src/components/Shelf/Shelf.svelte b/shared/components/src/components/Shelf/Shelf.svelte new file mode 100644 index 0000000..92527bb --- /dev/null +++ b/shared/components/src/components/Shelf/Shelf.svelte @@ -0,0 +1,535 @@ +<script lang="ts"> + import { onMount } from 'svelte'; + import Nav from '@amp/web-app-components/src/components/Shelf/Nav.svelte'; + import { getGridVars } from '@amp/web-app-components/src/components/Shelf/utils/getGridVars'; + import { checkItemPositionInShelf } from '@amp/web-app-components/src/components/Shelf/utils/observerCallback'; + import { ShelfWindow } from '@amp/web-app-components/src/components/Shelf/utils/shelf-window'; + import { throttle } from '@amp/web-app-components/src/utils/throttle'; + import { GRID_COLUMN_GAP_DEFAULT } from '@amp/web-app-components/src/components/Shelf/constants'; + import scrollByPolyfill from '@amp/web-app-components/src/utils/scrollByPolyfill'; + import { TEXT_DIRECTION } from '@amp/web-app-components/src/constants'; + import type { + GridType, + ArrowOffset, + AspectRatioOverrideConfig, + } from '@amp/web-app-components/src/components/Shelf/types'; + import { observe } from '@amp/web-app-components/src/components/Shelf/actions/observe'; + import ShelfItem from '@amp/web-app-components/src/components/Shelf/ShelfItem.svelte'; + import { createVisibleIndexStore } from '@amp/web-app-components/src/components/Shelf/store/visibleStore'; + import { getMaxVisibleItems } from '@amp/web-app-components/src/components/Shelf/utils/getMaxVisibleItems'; + import { createShelfAspectRatioContext } from '@amp/web-app-components/src/utils/shelfAspectRatio'; + import type { Readable } from 'svelte/store'; + + type T = $$Generic; + + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + // eslint-disable-next-line no-undef-init + export let id: string | undefined = undefined; + export let items: T[]; + export let gridType: GridType; + export let gridRows = 1; + export let arrowOffset: ArrowOffset | null = null; + // TODO: rdar://112908912 (Update `alignItems` prop in Shelf component and config to better match its actual function) + export let alignItems = false; + export let stackXSItems = false; + export let overflowBleedBottom: string = null; + export let aspectRatioOverride: AspectRatioOverrideConfig = null; + export let getItemIdentifier: + | ((item: unknown, index?: number) => string) + | null = null; + export let pageScrollMultiplier: number = null; + + /** + * On shelf scroll this handler returns the first and last indexes + * of the items currently visible in the shelf viewport. + */ + export let onIntersectionUpdate: ( + itemIndexsInViewport: [number, number], + ) => void | null = null; + /** + * Determines the first index in the items[] that should be visible on load. + * Defaults to the start of the items[]. + */ + export let firstItemIndex: number = 0; + + // Exporting a function to scroll to a specific page number + export function scrollToPage(pageNumber: number): void { + pageScroll(pageMultiplier * pageNumber); + } + + // This makes the let:item of type T + function cast(x: T): T { + return x as T; + } + + const shelfItemIdentifier = ( + item: unknown, + index: number, + ): unknown | string => { + let id: string; + if (typeof getItemIdentifier === 'function') { + id = getItemIdentifier(item, index); + if (typeof id !== 'string') { + // TODO: rdar://92459555 (Shared Components: integrate app logger in to shared components) + console.debug( + 'Could not get unique id, falling back to default', + item, + ); + } + } else if (isObjectWithId(item)) { + id = item.id; + } + return id || item; + }; + + interface WithID { + id: string; + } + function isObjectWithId(o: unknown): o is WithID { + return typeof o === 'object' && 'id' in o; + } + + // used to center arrows + let headerHeight = 0; + + // Corresponds to `$global-container-shadow-offset` in `_globavars.scss` + const STANDARD_LOCKUP_SHADOW_OFFSET = 15; + + let shelfAspectRatioStore: Readable<string> | null = null; + if (aspectRatioOverride !== null) { + const { shelfAspectRatio } = + createShelfAspectRatioContext(aspectRatioOverride); + shelfAspectRatioStore = shelfAspectRatio; + } + + $: style = (() => { + // TODO: possibly move this to app level rdar://74522896 + let customStyles = ` + ${getGridVars(gridType)} + --grid-type: ${gridType}; + --grid-rows: ${gridRows}; + --standard-lockup-shadow-offset: ${STANDARD_LOCKUP_SHADOW_OFFSET}px; + ${ + aspectRatioOverride !== null && $shelfAspectRatioStore !== null + ? `--shelf-aspect-ratio: ${$shelfAspectRatioStore};` + : '' + } + `; + + if (overflowBleedBottom) { + customStyles += `--overflowBleedBottom: ${overflowBleedBottom};`; + } + + return customStyles; + })(); + + let scrollableContainer: HTMLUListElement = null; + + let hasPreviousPage = false; + let hasNextPage = true; + let shelfBodyBoundingRect: HTMLDivElement = null; + + let observer: IntersectionObserver = null; + let viewport: [number, number] | null = null; + $: isRTL = false; + + const visibleStore = createVisibleIndexStore(); + const initalVisibleGridItems = + getMaxVisibleItems(gridType) * (gridRows || 1); + visibleStore.updateEndIndex(initalVisibleGridItems); + + const createObserver = (shelfBody: HTMLElement) => { + const options = { + root: shelfBody, + rootMargin: '0px', + threshold: 0.5, + }; + + const shelfWindow = new ShelfWindow(); + const callback = (entries: IntersectionObserverEntry[]) => { + const LAST_ITEM = items.length - 1; + entries.forEach((entry) => { + const item = entry.target as HTMLUListElement; + const currentIndex = parseInt(item.dataset.index, 10); + + // to prevent user seeing items loading + // load a few items off screen + const EXTRA_ITEMS = 2 * gridRows || 2; + const [isFirstItemAndInView, isLastItemAndInView] = + checkItemPositionInShelf(entry, LAST_ITEM); + if (entry.isIntersecting) { + shelfWindow.enterValue(currentIndex); + + const nextIndex = currentIndex + 1; + if (nextIndex >= $visibleStore.endIndex) { + const lastIndex = currentIndex + EXTRA_ITEMS; + visibleStore.updateEndIndex(lastIndex); + } + setShelfItemInteractivity(entry.target, true); + } else { + shelfWindow.exitValue(currentIndex); + setShelfItemInteractivity(entry.target, false); + } + + if (isFirstItemAndInView !== null) { + hasPreviousPage = !isFirstItemAndInView; + } + + if (isLastItemAndInView !== null) { + hasNextPage = !isLastItemAndInView; + } + }); + + viewport = shelfWindow.getViewport(); + + if (viewport && onIntersectionUpdate) { + onIntersectionUpdate(viewport); + } + }; + return new IntersectionObserver(callback, options); + }; + + onMount(() => { + scrollByPolyfill(); + // rdar://81757000 (TLF: Make storefront / language updates happen in-place with JS instead of hard-refreshes) + isRTL = document.dir === TEXT_DIRECTION.RTL; + observer = createObserver(shelfBodyBoundingRect); + if (firstItemIndex !== 0) { + scrollToIndex(firstItemIndex); + } + + return () => { + observer.disconnect(); + }; + }); + + export function scrollToIndex(index: number) { + const shelfItems = scrollableContainer.getElementsByClassName( + 'shelf-grid__list-item', + ); + if (!shelfItems) { + return; + } + const firstItem = shelfItems[0] as HTMLDivElement; + const itemWidth = firstItem.getBoundingClientRect().width; + + let scrollAmount: number; + if (index === 0) { + scrollAmount = 0; + } else { + scrollAmount = + (itemWidth + + GRID_COLUMN_GAP_DEFAULT - + STANDARD_LOCKUP_SHADOW_OFFSET * 2) * + index; + } + + let offset = isRTL ? -scrollAmount : scrollAmount; + scrollableContainer.scrollTo({ left: offset, behavior: 'instant' }); + } + + const pageScroll = (pageCount = 1) => { + const containerWidth = + scrollableContainer.getBoundingClientRect().width; + const scrollAmount = + (containerWidth + + GRID_COLUMN_GAP_DEFAULT - + STANDARD_LOCKUP_SHADOW_OFFSET * 2) * + pageCount; + scrollableContainer.scrollBy(scrollAmount, 0); + }; + const THROTTLE_LIMIT = 300; + + const pageMultiplierNumber = pageScrollMultiplier || 1; + $: pageMultiplier = isRTL ? -pageMultiplierNumber : pageMultiplierNumber; + $: handleNextPage = throttle( + pageScroll.bind(null, pageMultiplier), + THROTTLE_LIMIT, + ); + $: handlePreviousPage = throttle( + pageScroll.bind(null, -pageMultiplier), + THROTTLE_LIMIT, + ); + + let firstKnownItem: WithID; + let initialScroll = 0; + function restoreScroll(node: HTMLElement, items: T[]) { + if (!isObjectWithId(items[0])) { + return {}; + } + firstKnownItem = items[0]; + return { + update(items: T[]) { + if ( + isObjectWithId(items[0]) && + items[0].id !== firstKnownItem.id && + initialScroll === 0 && + node.scrollLeft > 0 + ) { + node.scrollLeft = 0; + } + }, + }; + } + + function trackScrollPosition(e: UIEvent) { + initialScroll = (e.target as HTMLElement).scrollLeft; + } + + function setShelfItemInteractivity( + shelfItemElement: Element, + isShelfItemVisible: boolean, + ) { + const interactiveContent: NodeListOf< + HTMLAnchorElement | HTMLButtonElement + > = shelfItemElement.querySelectorAll('a, button'); + interactiveContent.forEach((interactiveElement) => { + if (interactiveElement.nodeName === 'A') { + if (isShelfItemVisible) { + interactiveElement.removeAttribute('tabindex'); + } else { + interactiveElement.setAttribute('tabindex', '-1'); + } + } else { + // if this is a <button> + if (isShelfItemVisible) { + interactiveElement.removeAttribute('disabled'); + } else { + interactiveElement.setAttribute('disabled', 'true'); + } + } + }); + } +</script> + +<section + {id} + data-testid="shelf-component" + class="shelf-grid shelf-grid--onhover" + {style} +> + {#if $$slots.header} + <div class="shelf-grid__header" bind:offsetHeight={headerHeight}> + <slot name="header" /> + </div> + {/if} + <div + class="shelf-grid__body" + data-testid="shelf-body" + bind:this={shelfBodyBoundingRect} + > + <!-- + Fix for rdar://101154977 (AX: JMOW: Play button in Album lockup is not announced) + + Firefox adds scrollable elements to the tab order, so we need to + remove the grid list from the tab order with `tabindex="-1"` so + item announcement works as expected with NVDA. + + Since it has a tabindex set, we also need to prevent the mouse from + being able to focus the element on mousedown. + --> + <!-- TODO: rdar://97308317 (Investigate svelte AX warnings in shared components) --> + <!-- + In Safari, list semantics are removed from the AX tree when + CSS property list-style-type: none is used (this does not include nav elements). + Including role="list" on ul elements will re-add list semantics. + See https://bugs.webkit.org/show_bug.cgi?id=170179 + --> + <Nav + on:next={handleNextPage} + on:previous={handlePreviousPage} + {headerHeight} + {translateFn} + {arrowOffset} + {hasNextPage} + {hasPreviousPage} + {isRTL} + > + <ul + slot="shelf-content" + class={`shelf-grid__list shelf-grid__list--grid-type-${gridType} shelf-grid__list--grid-rows-${gridRows}`} + class:shelf-grid__list--align-items-end={alignItems} + class:shelf-grid__list--stack-xs-items={stackXSItems} + role="list" + tabindex="-1" + data-testid="shelf-item-list" + on:scroll={trackScrollPosition} + bind:this={scrollableContainer} + use:restoreScroll={items} + > + <!-- + TODO: rdar://77578080 + (Shared Components: Create a keyed each loop shelf and non-keyed shelf) + --> + {#each items as item, index (shelfItemIdentifier(item, index))} + {@const isItemInteractable = + index >= viewport?.[0] && index <= viewport?.[1]} + <ShelfItem {index} {visibleStore} let:isRendered> + <!-- TODO: rdar://97308317 (Investigate svelte AX warnings in shared components) --> + <li + class="shelf-grid__list-item" + class:placeholder={!isRendered} + class:shelf-grid__list-item--stack-xs-items={stackXSItems} + data-index={index} + aria-hidden={isItemInteractable ? 'false' : 'true'} + use:observe={observer} + > + {#if isRendered} + <div + use:setShelfItemInteractivity={isItemInteractable} + > + <slot + name="item" + item={cast(item)} + {index} + numberOfItems={items.length} + /> + </div> + {/if} + </li> + </ShelfItem> + {/each} + </ul> + </Nav> + </div> +</section> + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/selectors' as *; + @use 'amp/stylekit/core/viewports' as *; + @use 'amp/stylekit/core/mixins/overflow-bleed' as *; + @use '@amp/web-shared-styles/app/core/globalvars' as *; + @use './style/core.scss' as *; + @use './style/base.scss' as *; + + @mixin shelf-grid-list-styles($viewport: null) { + $grid-cols: var(--grid-#{$viewport}); + $grid-offset: calc( + (#{$grid-cols} - 1) * var(--grid-column-gap-#{$viewport}) + ); + grid-auto-columns: var( + --grid-max-content-#{$viewport}, + calc((100% - #{$grid-offset}) / #{$grid-cols}) + ); + grid-template-rows: repeat(var(--grid-rows), max-content); + column-gap: var(--grid-column-gap-#{$viewport}); + row-gap: var(--grid-row-gap-#{$viewport}); + } + + .shelf-grid__list { + // Standard lockups, of different heights, should align to titles under artwork + align-items: stretch; + + @include shelf-grid-list-styles(xsmall); + + @each $viewport in ('small', 'medium', 'large', 'xlarge') { + @media (--range-#{$viewport}-only) { + @include shelf-grid-list-styles($viewport); + } + + // Reduce column count by 1 in `medium` and `large` viewports when drawer is open + @if $viewport == 'medium' or $viewport == 'large' { + @include feature-detect(is-drawer-open) { + @media (--range-#{$viewport}-only) { + // No adjustments on Grid Types `A` and `music-radio`, for parity with DMA + &:not( + .shelf-grid__list--grid-type-A, + .shelf-grid__list--grid-type-music-radio, + .shelf-grid__list--grid-type-H + ) { + // Subtract 1 column when drawer is open + $grid-cols: calc(var(--grid-#{$viewport}) - 1); + $grid-offset: calc( + (#{$grid-cols} - 1) * + var(--grid-column-gap-#{$viewport}) + ); + grid-auto-columns: var( + --grid-max-content-#{$viewport}, + calc((100% - #{$grid-offset}) / #{$grid-cols}) + ); + } + + &.shelf-grid__list--grid-type-H { + // Subtract 2 columns on grid-type "H" only + $grid-cols: calc(var(--grid-#{$viewport}) - 2); + $grid-offset: calc( + (#{$grid-cols} - 2) * + var(--grid-column-gap-#{$viewport}) + ); + grid-auto-columns: var( + --grid-max-content-#{$viewport}, + calc((100% - #{$grid-offset}) / #{$grid-cols}) + ); + } + } + } + } + } + + @media (--small) { + :first-child { + // Set anchor for shelf chevron alignment + // Use `noShelfChevronAnchor={true}` to activate `artwork-component--no-anchor` + // class and disable chevron anchoring on an `<Artwork>` component. That will help isolate + // the true anchor when there are multiple `<Artworks>`s are in a single shelf lockup. + :global(.artwork-component:not(.artwork-component--no-anchor)) { + anchor-name: --shelf-first-artwork; + } + } + } + } + + .shelf-grid--onhover { + // stylelint-disable-next-line selector-pseudo-class-no-unknown + :global(.shelf-grid-nav__arrow) { + opacity: 0; + will-change: opacity; + transition: $shelf-grid-nav-transition; + + &:focus { + opacity: 1; + } + } + + &:hover, + &:focus-within { + // stylelint-disable-next-line selector-pseudo-class-no-unknown + :global(.shelf-grid-nav__arrow:not([disabled])) { + opacity: 1; + } + } + } + + // TODO: rdar://112908912 (Update `alignItems` prop in Shelf component and config to better match its actual function) + .shelf-grid__list--align-items-end { + --override-shelf-overflow-bleed-bottom: 35px; + padding-top: 0; + } + + // TODO: rdar://88487875 (Revisit accessibility for shelf) + // allows for accurate count for VO + // .placeholder::before { + // content: '•'; + // opacity: 0; + // } + + // Stack Music Radio shelf lockups, for `xs-1` viewport only. + .shelf-grid__list--stack-xs-items { + --override-shelf-overflow-bleed-bottom: 35px; + align-items: stretch; + + @media (--range-grid-layout-xs-1-down) { + display: block; + // Add `bodyGutter` back that is intentionally removed for peeking XS shelves. + padding-inline-end: var(--bodyGutter); + + :not(:first-child) { + margin-top: $spacerC; + } + } + } +</style> diff --git a/shared/components/src/components/Shelf/ShelfItem.svelte b/shared/components/src/components/Shelf/ShelfItem.svelte new file mode 100644 index 0000000..f164421 --- /dev/null +++ b/shared/components/src/components/Shelf/ShelfItem.svelte @@ -0,0 +1,60 @@ +<script lang="ts"> + import { getRafQueue } from '@amp/web-app-components/src/utils/rafQueue'; + import { onDestroy } from 'svelte'; + import { get, type Readable } from 'svelte/store'; + import type { VisibleIndexData } from '@amp/web-app-components/src/components/Shelf/store/visibleStore'; + + export let index: number; + export let visibleStore: Readable<VisibleIndexData>; + + const rafQueue = getRafQueue(); + const isBetween = (start: number, end: number, value: number) => { + return value >= start && value <= end; + }; + // get value but dont subscribe to it. + let { startIndex, endIndex } = get(visibleStore); + $: isRendered = isBetween(startIndex, endIndex, index); + $: isSubscribed = true; + + // Elements should only be subscribed + // to the store if they are not rendered. + const unsubscribe = visibleStore.subscribe((store) => { + const { startIndex, endIndex } = store; + const currentIsRendered = isBetween(startIndex, endIndex, index); + // Manually handling subscription to + // update DOM using RAF in browser for smoother scrolling + if (currentIsRendered && !isRendered) { + rafQueue.add(() => { + isRendered = currentIsRendered; + }); + } + }); + + /** + * Unsubscribe to the store only if `isSubscribed` is true + * + * This helps ensure that we do not accidentally call `unsubscribe` twice, + * which can cause errors in Svelte. One way that can happen is by unsubscribing + * both using `onDestory` and with the callback added to the `rafQueue` + * + * See https://github.com/sveltejs/svelte/issues/4765#issuecomment-1379243063 + */ + function unsubscribeIfNeeded() { + if (isSubscribed) { + unsubscribe(); + isSubscribed = false; + } + } + + $: if (isSubscribed && isRendered) { + rafQueue.add(() => { + unsubscribeIfNeeded(); + }); + } + + onDestroy(() => { + unsubscribeIfNeeded(); + }); +</script> + +<slot {isRendered} /> diff --git a/shared/components/src/components/Shelf/actions/observe.ts b/shared/components/src/components/Shelf/actions/observe.ts new file mode 100644 index 0000000..afa9168 --- /dev/null +++ b/shared/components/src/components/Shelf/actions/observe.ts @@ -0,0 +1,31 @@ +import type { Action } from '@amp/web-app-components/src/types'; + +// eslint-disable-next-line import/prefer-default-export +export function observe( + node: HTMLElement, + observer: IntersectionObserver, +): Action { + let oldObserver: IntersectionObserver | undefined; + + function update(observerInstance: IntersectionObserver): void { + if (oldObserver === observerInstance || !observerInstance) { + return; + } + + if (oldObserver) { + oldObserver.unobserve(node); + } + + observerInstance.observe(node); + oldObserver = observerInstance; + } + + update(observer); + + return { + update, + destroy() { + oldObserver?.unobserve(node); + }, + }; +} diff --git a/shared/components/src/components/Shelf/constants.ts b/shared/components/src/components/Shelf/constants.ts new file mode 100644 index 0000000..4a52bda --- /dev/null +++ b/shared/components/src/components/Shelf/constants.ts @@ -0,0 +1,20 @@ +// eslint-disable-next-line import/prefer-default-export +export const GRID_TYPES = [ + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'EllipseA', + 'Spotlight', + '1-1-2-3', + '1-2-2-2', +] as const; + +export const GRID_COLUMN_GAP_DEFAULT = 20; +export const GRID_COLUMN_GAP_DEFAULT_XSMALL = 10; +export const GRID_ROW_GAP_DEFAULT = 24; diff --git a/shared/components/src/components/Shelf/store/visibleStore.ts b/shared/components/src/components/Shelf/store/visibleStore.ts new file mode 100644 index 0000000..09b15ec --- /dev/null +++ b/shared/components/src/components/Shelf/store/visibleStore.ts @@ -0,0 +1,33 @@ +import { writable, type Readable } from 'svelte/store'; + +export type VisibleIndexData = { + startIndex: number; + endIndex: number; +}; + +export interface VisibleStore extends Readable<VisibleIndexData> { + updateStartIndex: (num: number) => void; + updateEndIndex: (num: number) => void; +} + +/** + * Store for keeping track of items rendered in shelf. + */ +export const createVisibleIndexStore = (): VisibleStore => { + const { subscribe, update } = writable({ + startIndex: 0, + endIndex: 0, + }); + + return { + subscribe, + updateStartIndex: (startIndex: number) => + update((visibleItems) => { + return { ...visibleItems, startIndex }; + }), + updateEndIndex: (endIndex: number) => + update((visibleItems) => { + return { ...visibleItems, endIndex }; + }), + }; +}; diff --git a/shared/components/src/components/Shelf/utils/getGridVars.ts b/shared/components/src/components/Shelf/utils/getGridVars.ts new file mode 100644 index 0000000..ecfe116 --- /dev/null +++ b/shared/components/src/components/Shelf/utils/getGridVars.ts @@ -0,0 +1,98 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import type { ShelfConfigOptions } from '@amp/web-app-components/config/components/shelf'; +import { ShelfConfig } from '@amp/web-app-components/config/components/shelf'; +import { + GRID_COLUMN_GAP_DEFAULT, + GRID_COLUMN_GAP_DEFAULT_XSMALL, + GRID_ROW_GAP_DEFAULT, + // eslint-disable-next-line import/no-extraneous-dependencies +} from '@amp/web-app-components/src/components/Shelf/constants'; +import type { GridType } from '@amp/web-app-components/src/components/Shelf/types'; +import type { Sizes, Size } from '@amp/web-app-components/src/types'; + +const generateGridColSizeVars = ( + viewport: Size, + gridValues: ShelfConfigOptions['GRID_VALUES'][string], + maxContents: ShelfConfigOptions['GRID_MAX_CONTENT'][string], +): string[] => { + const value = gridValues[viewport]; + const maxContent = maxContents[viewport]; + const gridVars = []; + + if (maxContent) { + // create CSS variable for px values in grid + gridVars.push(`--grid-max-content-${viewport}: ${maxContent};`); + } else if (value) { + // create CSS variable for grid unit + gridVars.push(`--grid-${viewport}: ${value};`); + } + + return gridVars; +}; + +const generateGridGapSizeVars = ( + viewport: Size, + gridColumnGap: Partial<ShelfConfigOptions['GRID_COL_GAP'][string]>, + gridRowGap: Partial<ShelfConfigOptions['GRID_ROW_GAP'][string]>, +): string[] => { + const gridVars = []; + const defaultColGap = + viewport === 'xsmall' + ? GRID_COLUMN_GAP_DEFAULT_XSMALL + : GRID_COLUMN_GAP_DEFAULT; + + // check if gap override for certain viewport + gridVars.push( + `--grid-column-gap-${viewport}: ${ + gridColumnGap[viewport] ?? defaultColGap + }px;`, + ); + gridVars.push( + `--grid-row-gap-${viewport}: ${ + gridRowGap[viewport] ?? GRID_ROW_GAP_DEFAULT + }px;`, + ); + + return gridVars; +}; + +/** + * converts the JS configs to CSS variables. + * + * variables created: + * --grid-{viewport} - grid value to use for columns widths + * --grid-max-content-{viewport} - px value to use for column width + * --grid-column-gap-{viewport} - grid gap size // default is 20px + * */ + +// eslint-disable-next-line import/prefer-default-export +export const getGridVars = (type: GridType): string => { + const { GRID_VALUES, GRID_MAX_CONTENT, GRID_COL_GAP, GRID_ROW_GAP } = + ShelfConfig.get(); + + const gridValues = GRID_VALUES[type]; + const maxContent = GRID_MAX_CONTENT[type]; + const gridRowGap = GRID_ROW_GAP[type] || {}; + const gridColumnGap = GRID_COL_GAP[type] || {}; + const gridKeys = Object.keys(gridValues) as unknown as Sizes; + + let gridVars: string[] = []; + + gridKeys.forEach((viewport) => { + // generate variables for each viewport + const gridColumnSizeVars = generateGridColSizeVars( + viewport, + gridValues, + maxContent, + ); + const gridGapSizeVars = generateGridGapSizeVars( + viewport, + gridColumnGap, + gridRowGap, + ); + + gridVars = [...gridVars, ...gridColumnSizeVars, ...gridGapSizeVars]; + }); + + return gridVars.join(' '); +}; diff --git a/shared/components/src/components/Shelf/utils/getMaxVisibleItems.ts b/shared/components/src/components/Shelf/utils/getMaxVisibleItems.ts new file mode 100644 index 0000000..226f7ba --- /dev/null +++ b/shared/components/src/components/Shelf/utils/getMaxVisibleItems.ts @@ -0,0 +1,19 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { ShelfConfig } from '@amp/web-app-components/config/components/shelf'; +import type { GridType } from '@amp/web-app-components/src/components/Shelf/types'; + +/** + * Find the max amount of rendered items for a grid type. + */ +// eslint-disable-next-line import/prefer-default-export +export const getMaxVisibleItems = (type: GridType): number => { + const { GRID_VALUES } = ShelfConfig.get(); + + const gridValues = GRID_VALUES[type]; + + const arrayOfgridValues = [...Object.values(gridValues)].filter( + (item) => typeof item === 'number', + ); + + return Math.max(...arrayOfgridValues); +}; diff --git a/shared/components/src/components/Shelf/utils/observerCallback.ts b/shared/components/src/components/Shelf/utils/observerCallback.ts new file mode 100644 index 0000000..17ace58 --- /dev/null +++ b/shared/components/src/components/Shelf/utils/observerCallback.ts @@ -0,0 +1,30 @@ +/** + * @name checkItemPositionInShelf + * @description determine if we need to hide/show navigation arrows. + * + * @param entry entry provided by the intersection observer + * @param lastIndex index of the last item in the list + * + * @returns first/last item values ONLY when being intersected, + * otherwise will return null. + */ + +// eslint-disable-next-line import/prefer-default-export +export const checkItemPositionInShelf = ( + entry: IntersectionObserverEntry, + lastIndex: number, +): [boolean | null, boolean | null] => { + const item = entry.target as HTMLLIElement; + const itemIndexInView = item.dataset.index; + const isItemVisible = entry.isIntersecting; + + const FIRST_INDEX = '0'; + const LAST_INDEX = `${lastIndex}`; + + const isFirstItemAndInView = + itemIndexInView === FIRST_INDEX ? isItemVisible : null; + const isLastItemAndInView = + itemIndexInView === LAST_INDEX ? isItemVisible : null; + + return [isFirstItemAndInView, isLastItemAndInView]; +}; diff --git a/shared/components/src/components/Shelf/utils/shelf-window.ts b/shared/components/src/components/Shelf/utils/shelf-window.ts new file mode 100644 index 0000000..8a0501a --- /dev/null +++ b/shared/components/src/components/Shelf/utils/shelf-window.ts @@ -0,0 +1,67 @@ +/* eslint-disable import/prefer-default-export */ + +/** + * Keeps track of the items that are + * within the viewport of a shelf. + */ +export class ShelfWindow { + /** + * List of indexes of visible shelf items. + */ + private visibleShelfEntries: Set<number> = new Set(); + + /** + * The lowest visible index in the shelf viewport. + */ + private lowestIndexInVisibleShelf: number | undefined; + + /** + * The highest visible index in the shelf viewport. + */ + private highestIndexInVisibleShelf: number | undefined; + + /** + * Adds the index that has entered the viewport to to shelf item visibility set. + * @param index item's index that has entered the viewport + */ + enterValue(index: number) { + this.visibleShelfEntries.add(index); + this.setMinAndMaxValuesOfViewport(); + } + + /** + * Removes index that has left viewport from shelf item visibility set. + * + * @param index item index that has left the viewport + */ + exitValue(index: number) { + this.visibleShelfEntries.delete(index); + this.setMinAndMaxValuesOfViewport(); + } + + /** + * Set the min and max based on indexes in shelf item visiblity set. + */ + private setMinAndMaxValuesOfViewport() { + this.lowestIndexInVisibleShelf = Math.min(...this.visibleShelfEntries); + this.highestIndexInVisibleShelf = Math.max(...this.visibleShelfEntries); + } + + /** + * Get the current visible indexes for a given shelf. + * + * @returns + * the first and last item indexes in a shelf viewport + * or null if both values are not set. + */ + getViewport(): [number, number] | null { + const firstIndex = this.lowestIndexInVisibleShelf; + const secondIndex = this.highestIndexInVisibleShelf; + + if (typeof firstIndex === 'number' && typeof secondIndex === 'number') { + return [firstIndex, secondIndex]; + } + + return null; + } +} diff --git a/shared/components/src/components/TextSearchSuggestion/TextSearchSuggestion.svelte b/shared/components/src/components/TextSearchSuggestion/TextSearchSuggestion.svelte new file mode 100644 index 0000000..37793db --- /dev/null +++ b/shared/components/src/components/TextSearchSuggestion/TextSearchSuggestion.svelte @@ -0,0 +1,44 @@ +<script lang="ts"> + import SearchIcon from '@amp/web-app-components/assets/icons/search.svg'; + import type { HighlightedSearchSuggestion } from '../../utils/processTextSearchSuggestion'; + + export let suggestion: HighlightedSearchSuggestion; + $: autofillBefore = suggestion.autofillBefore; + $: highlighted = suggestion.highlighted; + $: autofillAfter = suggestion.autofillAfter; +</script> + +<SearchIcon class="search-suggestion-svg" aria-hidden="true" /> +<span class="suggestion"> + <!-- + These spans cannot be broken down onto separate lines until Svelte + supports trimming of whitespace on-demand: https://github.com/sveltejs/svelte/issues/189 + TODO: rdar://101681389 (Onxy: Remove whitespace trimming workarounds) + --> + + <!-- prettier-ignore --> + <span data-testid="suggestion-autofill-before">{autofillBefore}</span><span + class="highlighted" + data-testid="suggestion-autofill-highlighted">{highlighted}</span + ><span data-testid="suggestion-autofill-after">{autofillAfter}</span> +</span> + +<style lang="scss"> + @use 'amp/stylekit/core/mixins/line-clamp' as *; + + .suggestion { + color: var(--systemSecondary); + margin: 0 6px; + font: var(--title-2); + + @include line-clamp(var(--searchSuggestionClampedLines, 1)); + + @media (--sidebar-visible) { + font: var(--callout); + } + } + + .highlighted { + color: var(--systemPrimary); + } +</style> diff --git a/shared/components/src/components/Truncate/Truncate.svelte b/shared/components/src/components/Truncate/Truncate.svelte new file mode 100644 index 0000000..d9e859f --- /dev/null +++ b/shared/components/src/components/Truncate/Truncate.svelte @@ -0,0 +1,222 @@ +<script lang="ts"> + import { onMount } from 'svelte'; + import { makeSafeTick } from '@amp/web-app-components/src/utils/makeSafeTick'; + import Modal from '@amp/web-app-components/src/components/Modal/Modal.svelte'; + import ContentModal from '@amp/web-app-components/src/components/Modal/ContentModal.svelte'; + import { debounce } from '@amp/web-app-components/src/utils/debounce'; + import { sanitizeHtml } from '@amp/web-app-components/src/utils/sanitize-html'; + import type { SvelteComponent } from 'svelte'; + import { getUniqueIdGenerator } from '@amp/web-app-components/src/utils/uniqueId'; + import { createEventDispatcher } from 'svelte'; + + const dispatch = createEventDispatcher(); + + /** + * @name Truncate + * + * @description + * This implements Truncate component that used to show truncated text with modal. + * + * Design: + * https://pd-hi.apple.com/viewvc/Common/Modules/macOS/Music/-Common%20Elements/Truncation.png?revision=55587 + * + */ + + export let text: string; + export let lines: number = 4; // Indicate how many lines to truncate, default to 4 + export let title: string | null = null; + export let subtitle: string | null = null; + export let translateFn: (key: string) => string; + export let modalType: 'contentModal' | null = null; + export let typography: 'title-3' | null = null; + export let bodyTypography: 'body' | null = null; + export let isPortalModal: boolean = false; + export let expandText: boolean = false; + export let usePillVariant: boolean = false; + export let sanitizeHtmlOptions: object = { + allowedTags: [''], + keepChildrenWhenRemovingParent: true, + }; + + let modalComponent: SvelteComponent; + let truncateContent: HTMLElement; + let needsTruncation = false; + let modalTriggerElement = null; + + function detectTruncate() { + needsTruncation = + truncateContent.scrollHeight > truncateContent.clientHeight; + } + + function handleMoreBtnClick(e: Event) { + e.preventDefault(); + e.stopPropagation(); + + if (expandText) { + needsTruncation = false; + truncateContent.style.setProperty('--lines', 'unset'); + } else { + handleOpenModalClick(e); + } + } + + function handleOpenModalClick(e: Event) { + modalTriggerElement = e.target; + dispatch('openModal', e); + + if (modalComponent) { + modalComponent.showModal(); + } + } + + function handleModalClose() { + modalComponent.close(); + } + + const dialogTitleId = getUniqueIdGenerator()(); + const safeTick = makeSafeTick(); + const moreButtonText = translateFn('AMP.Shared.Truncate.More') ?? ''; + + onMount(async () => { + await safeTick(async (tick) => { + // To make sure Modal bind:this setup properly before onmount + await tick(); + detectTruncate(); + }); + }); +</script> + +<!-- Detect whether need truncated or not when window resizing --> +<svelte:window on:resize={debounce(detectTruncate, 100)} /> + +<div class="truncate-wrapper" class:pill={usePillVariant && needsTruncation}> + <p + data-testid="truncate-text" + bind:this={truncateContent} + dir="auto" + class="content" + class:with-more-button={needsTruncation} + class:title-3={typography === 'title-3'} + class:body={bodyTypography === 'body'} + style:--lines={lines ?? 4} + style:--line-height="var(--lineHeight, 16)" + style:--link-length={moreButtonText.length} + > + {@html sanitizeHtml(text, sanitizeHtmlOptions)} + </p> + {#if needsTruncation} + <button + data-testid="truncate-more-button" + class="more" + type="button" + on:click={handleMoreBtnClick} + > + {moreButtonText} + </button> + {/if} +</div> + +{#if needsTruncation && !isPortalModal} + <Modal + {modalTriggerElement} + bind:this={modalComponent} + ariaLabelledBy={dialogTitleId} + > + {#if modalType === 'contentModal'} + <ContentModal + {title} + {subtitle} + {text} + {translateFn} + {dialogTitleId} + on:close={handleModalClose} + /> + {/if} + </Modal> +{/if} + +<style lang="scss"> + @use '@amp/web-shared-styles/sasskit-stylekit/ac-sasskit-config'; + @use 'ac-sasskit/core/locale' as *; + @use 'amp/stylekit/core/mixins/line-clamp' as *; + + .truncate-wrapper { + position: relative; + z-index: var(--z-default); + } + + .content { + white-space: pre-wrap; + font: var(--truncate-font, var(--body-tall)); + + @include line-clamp(var(--lines)); + + &.title-3 { + font: var(--title-3); + + // The next line applies if `--lineHeight` was set by a parent. + line-height: calc(var(--lineHeight) * 1px); + } + + &.body { + font: var(--body); + + // The next line applies if `--lineHeight` was set by a parent. + line-height: calc(var(--lineHeight) * 1px); + } + } + + .with-more-button { + // CSS properties to build the mask based on the "MORE" button + // --one-ch property controls character width and font size + --fade-direction: 270deg; + word-break: break-word; + position: relative; // For `More` link positioning. + // prettier-ignore + mask: linear-gradient( + 0deg, + transparent 0, + transparent calc(var(--line-height) * 1px), + #000 calc(var(--line-height) * 1px) + ), + linear-gradient( + var(--fade-direction), + transparent 0, + transparent calc((var(--link-length) * var(--one-ch, 8)) * 1px + var(--inline-mask-offset, 0px)), + #000 calc(((var(--link-length) * var(--one-ch, 8)) + (var(--line-height) * 2)) * 1px + var(--inline-mask-offset, 0px)), + ); + mask-size: initial, initial; + mask-position: right bottom; + z-index: var(--z-default); + + @include rtl { + --fade-direction: 90deg; + mask-position: left bottom; + } + } + + .more { + position: absolute; + bottom: var(--moreBottomPositionOverride, 1px); + color: var(--moreTextColorOverride, var(--systemPrimary)); + inset-inline-end: 0; + padding-inline-start: 5px; + font: var(--moreFontOverride, var(--subhead-emphasized)); + z-index: var(--z-default); + } + + .pill { + --inline-mask-offset: 12px; // accommodate pill width in text mask + + .more { + padding: 0 6px; + border-radius: 8px; + margin-inline-start: 3px; + inset-inline-end: 2px; + bottom: var(--moreBottomPositionOverride, 2px); + font: var(--subhead-emphasized); + background-color: var(--systemSecondary-onDark); + color: white; // white per spec, no vars + } + } +</style> diff --git a/shared/components/src/components/buttons/Button.svelte b/shared/components/src/components/buttons/Button.svelte new file mode 100644 index 0000000..910b612 --- /dev/null +++ b/shared/components/src/components/buttons/Button.svelte @@ -0,0 +1,324 @@ +<script lang="ts"> + // TODO: rdar://92270447 (JMOTW: Refactor ButtonAction component to use Button component) + import { createEventDispatcher, onMount } from 'svelte'; + import { makeSafeTick } from '@amp/web-app-components/src/utils/makeSafeTick'; + + const dispatch = createEventDispatcher(); + + const handleButtonClick = () => { + dispatch('buttonClick'); + }; + + // Button A, B, etc. refers to the button spec + // https://pd-hi.apple.com/viewvc/Common/Modules/macOS/Music/-Common%20Elements/Buttons.png + // alertButton and alertButtonSecondary refer to Alert Modal spec + // https://pd-hi.apple.com/viewvc/Common/Modules/macOS/-Cross%20Product/_web%20-%20Alerts.png + type ButtonType = + | 'buttonA' + | 'buttonB' + | 'buttonD' + | 'alertButton' + | 'alertButtonSecondary' + | 'pillButton' + | 'socialProfileButton' + | 'textButton' + | null; + + export let buttonStyle: string | null = null; + export let makeFocused = false; + export let ariaLabel: string | null = null; + export let type: 'button' | 'submit' = 'button'; + export let disabled = false; + export let buttonElement: HTMLButtonElement = null; + + // Need to do this to resolve TS error: + // Type 'string' is not assignable to type 'ButtonType' + $: buttonType = buttonStyle as ButtonType; + + function handleKeyUp(e: KeyboardEvent) { + if (e.key === 'Enter' || e.key === 'Escape') { + handleButtonClick(); + } + } + + const safeTick = makeSafeTick(); + + onMount(async () => { + await safeTick(async (tick) => { + await tick(); + if (makeFocused) { + buttonElement.focus(); + } + }); + }); +</script> + +<div + class="button" + class:primary={buttonType === 'buttonA'} + class:secondary={buttonType === 'buttonB'} + class:tertiary={buttonType === 'buttonD'} + class:alert={buttonType && buttonType.startsWith('alertButton')} + class:alert-secondary={buttonType === 'alertButtonSecondary'} + class:pill={buttonType === 'pillButton'} + class:button--text-button={buttonType === 'textButton'} + class:socialProfileButton={buttonType === 'socialProfileButton'} + data-testid="button-base-wrapper" +> + <button + on:click={handleButtonClick} + data-testid="button-base" + aria-label={ariaLabel} + bind:this={buttonElement} + on:keyup={handleKeyUp} + class:link={buttonType === 'textButton'} + {type} + {disabled} + > + {#if $$slots['icon-before']} + <div class="button__icon button__icon--before"> + <slot name="icon-before" /> + </div> + {/if} + <slot /> + {#if $$slots['icon-after']} + <div class="button__icon button__icon--after"> + <slot name="icon-after" /> + </div> + {/if} + </button> +</div> + +<style lang="scss"> + @use '@amp/web-shared-styles/app/core/globalvars' as *; + @use '@amp/web-shared-styles/app/core/mixins/keycolor-button-states' as *; + + // TODO: rdar://104573582 (Refactor <Button> and <ButtonAction> styles) + .button { + width: var(--buttonWrapperWidth, 100%); + + @media (--medium) { + width: var(--buttonWrapperWidth, auto); + } + + /* TODO: rdar://78161351: this is kind of messy */ + button { + width: var(--buttonWidth, 100%); + height: var(--buttonHeight, 36px); + display: var(--buttonDisplay, flex); + color: var(--buttonTextColor, white); + background-color: var( + --buttonBackgroundColor, + var(--keyColorBG, var(--systemBlue)) + ); + align-items: center; + justify-content: var(--buttonJustifyContent, center); + border-radius: var(--buttonRadius, #{$global-border-radius-xsmall}); + font: var(--buttonFont, var(--body-emphasized)); + + @media (--medium) { + width: var(--buttonWidth, auto); + min-width: 100px; + height: var(--buttonHeight, #{$action-button-size}); + } + + &[disabled] { + opacity: var(--buttonDisabledOpacity, 0.75); + background-color: var( + --buttonDisabledBGColor, + var(--systemQuinary) + ); + color: var(--buttonDisabledTextColor, var(--systemTertiary)); + cursor: default; + + @media (prefers-color-scheme: dark) { + opacity: var(--buttonDisabledOpacityDark, 1); + background-color: var( + --buttonDisabledBGColorDark, + rgba(255, 255, 255, 0.5) + ); + color: var( + --buttonDisabledTextColorDark, + var(--systemTertiary-onLight) + ); + } + } + } + + &.primary button { + color: var(--buttonTextColor, white); + background-color: var( + --buttonBackgroundColor, + var(--keyColorBG, var(--systemBlue)) + ); + padding: 0 10px; + + &:disabled { + opacity: 0.5; + } + } + + &.secondary { + width: auto; + + button { + --buttonBackgroundColor: transparent; + min-width: var(--buttonMinWidth, 108px); + color: var(--buttonTextColor, var(--keyColor)); + border: 1px solid + var(--buttonBorderColor, var(--keyColor, var(--systemBlue))); + font: var(--body-tall); + padding-inline-start: 16px; + padding-inline-end: 16px; + } + } + + // the tertiary styles are used for button type D + // currently only used in the snapshot project + &.tertiary { + width: auto; + + button { + --buttonBackgroundColor: var(--keyColorBG, var(--systemBlue)); + --buttonTextColor: white; + padding-inline-start: 22px; + padding-inline-end: 22px; + width: var(--buttonWidth, auto); + height: var(--buttonHeight, 45px); + font: var(--buttonFont, var(--body-reduced-semibold)); + + &:hover, + &:focus, + &:focus-within { + --buttonBackgroundColor: var( + --buttonBackgroundColorHover, + var(--keyColorBG, var(--systemBlue)) + ); + transition: all 100ms ease-in-out; + } + } + } + + &.alert { + // Prevent button inside modal from shrinking in wide viewport + --buttonWrapperWidth: 100%; + --buttonWidth: 100%; + --buttonHeight: 28px; + --buttonRadius: 6px; + } + + &.alert-secondary { + --buttonTextColor: var(--systemPrimary); + --buttonBackgroundColor: var(--systemQuinary); + + @media (prefers-color-scheme: dark) { + --buttonBackgroundColor: var(--systemTertiary); + } + } + + &.pill { + --buttonBackgroundColor: rgba(var(--keyColor-rgb), 0.06); + --buttonTextColor: var(--keyColor); + + button { + min-width: var(--buttonMinWidth, 90px); + width: var(--buttonWidth, auto); + height: var(--buttonHeight, 28px); + border-radius: var(--buttonBorderRadius, 16px); + padding-inline-start: var(--buttonPadding, 16px); + padding-inline-end: var(--buttonPadding, 16px); + font: var(--body-semibold-tall); + } + } + + &.socialProfileButton { + height: auto; + border-radius: 10px; + margin-top: 27px; + width: unset; /* unset inherited value from .button */ + min-width: 90px; + background-color: var(--keyColorBG); + z-index: var(--z-default); + + @include keycolor-button-states; + } + + &.socialProfileButton button { + padding-top: 9px; + padding-bottom: 9px; + color: var(--systemPrimary-onDark); + height: auto; + font: var(--title-2); + padding-inline-start: 22px; + padding-inline-end: 22px; + + :global(.web-to-native__action) { + fill: var(--systemPrimary-onDark); + } + } + } + + // Works in conjuction with `link` class in @amp-stylekit/base/typography + .button--text-button { + --buttonBackgroundColor: transparent; + --buttonTextColor: var(--keyColor); // `link` class will inherit this + --linkHoverTextDecoration: none; // `link` custom property + + button { + white-space: nowrap; + font: var(--buttonFont, var(--body)); + } + } + + .button__icon { + display: flex; + fill: var(--buttonIconFill, currentColor); + height: var(--buttonIconHeight, 1em); + width: var(--buttonIconWidth, 1em); + padding: var(--buttonIconPadding, 0); + margin-top: var(--buttonIconMarginTop, 0); + margin-bottom: var(--buttonIconMarginBottom, 0); + + &:empty, + &:has(div:empty) { + margin: 0; + } + + @media (hover: hover) { + button:hover & { + fill: var( + --buttonIconFillHover, + var(--buttonIconFill, currentColor) + ); + } + } + + @supports #{'selector(:has(:focus-visible))'} { + button:focus-visible & { + fill: var( + --buttonIconFillFocus, + var(--buttonIconFill, currentColor) + ); + } + } + + &:active { + button:active & { + fill: var( + --buttonIconFillActive, + var(--buttonIconFill, currentColor) + ); + } + } + } + + .button__icon--before { + margin-inline-end: var(--buttonIconMargin-inlineEnd, 0.25em); + margin-inline-start: var(--buttonIconMargin-inlineStart, 0); + } + + .button__icon--after { + margin-inline-start: var(--buttonIconMargin-inlineStart, 0.25em); + margin-inline-end: var(--buttonIconMargin-inlineEnd, 0); + } +</style> diff --git a/shared/components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherButton.svelte b/shared/components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherButton.svelte new file mode 100644 index 0000000..13c666c --- /dev/null +++ b/shared/components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherButton.svelte @@ -0,0 +1,99 @@ +<script lang="ts"> + import Modal from '@amp/web-app-components/src/components/Modal/Modal.svelte'; + import LocaleSwitcherModal from '@amp/web-app-components/src/components/Modal/LocaleSwitcherModal/LocaleSwitcherModal.svelte'; + import LocaleSwitcherLanguages from '@amp/web-app-components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherLanguages.svelte'; + import type { + Region, + Languages, + Language, + } from '@amp/web-app-components/src/components/buttons/LocaleSwitcherButton/types'; + import type { Locale } from '@amp/web-app-components/src/types'; + import type { SvelteComponent } from 'svelte'; + import type { StorefrontNames } from '@amp/web-app-components/src/components/banners/types'; + + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + export let locale: Locale; + export let regions: Region[]; + export let languages: Languages; + export let defaultRoute: string; + export let storefrontNameTranslations: StorefrontNames; + + $: language = locale.language; + $: storefront = locale.storefront; + + let modalTriggerElement = null; + let modalElement: SvelteComponent; + + const handleOpenModalClick = () => { + // only open modal on click if regions is not empty + if (regions.length) { + modalElement.showModal(); + } + }; + + $: otherLanguages = languages[storefront].filter( + (l: Language) => l.tag.toLowerCase() !== language.toLowerCase(), + ); + + $: storefrontName = + storefrontNameTranslations[storefront]?.[language] ?? + storefrontNameTranslations[storefront]?.['default']; + + // rdar://102181852 (CHN AM Web app is showing language selector in traditional Chinese.) + // We should not show the locale switcher or language selector when on the CN storefront + $: isCNStorefront = storefront === 'cn'; +</script> + +{#if storefrontName && !isCNStorefront} + <div + class="button-container" + class:languages-new-line={otherLanguages.length >= 6} + > + <button + on:click={handleOpenModalClick} + class="link" + data-testid="locale-switcher-button" + > + {storefrontName} + </button> + <LocaleSwitcherLanguages {translateFn} {otherLanguages} /> + </div> +{/if} + +<Modal {modalTriggerElement} bind:this={modalElement}> + <LocaleSwitcherModal + {translateFn} + {regions} + {defaultRoute} + on:close={modalElement.close} + /> +</Modal> + +<style lang="scss"> + .button-container { + --linkColor: var(--systemPrimary); + display: flex; + margin-bottom: 25px; + + &.languages-new-line { + @media (--range-small-down) { + flex-direction: column; + + button { + margin-bottom: 5px; + } + } + } + } + + button { + line-height: 1; + display: inline-flex; + margin-top: 6px; + vertical-align: middle; + white-space: nowrap; + } +</style> diff --git a/shared/components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherLanguages.svelte b/shared/components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherLanguages.svelte new file mode 100644 index 0000000..f7cdfad --- /dev/null +++ b/shared/components/src/components/buttons/LocaleSwitcherButton/LocaleSwitcherLanguages.svelte @@ -0,0 +1,100 @@ +<script lang="ts"> + import type { Language } from '@amp/web-app-components/src/components/buttons/LocaleSwitcherButton/types'; + export let translateFn: ( + str: string, + values?: Record<string, string | number>, + ) => string; + export let otherLanguages: Language[]; + + const handleClick = (otherLanguage: string) => { + const url = new URL(window.location.href); + url.searchParams.set('l', otherLanguage); + window.location.assign(`${url.pathname}${url.search}`); + }; +</script> + +{#if otherLanguages.length > 0} + <ul class:languages-new-line={otherLanguages.length >= 6}> + {#each otherLanguages as otherLanguage} + {#if otherLanguage.tag && otherLanguage.name} + <li> + <a + on:click|preventDefault={() => + handleClick(otherLanguage.tag)} + href={`?l=${otherLanguage.tag}`} + aria-label={translateFn( + 'AMP.Shared.LocaleSwitcher.SwitchLanguage', + { language: otherLanguage.name }, + )} + data-testid={`other-language-${otherLanguage.tag}`} + > + {otherLanguage.name} + </a> + </li> + {/if} + {/each} + </ul> +{/if} + +<style lang="scss"> + a { + --linkColor: var(--systemSecondary); + white-space: nowrap; + padding-inline-end: 10px; + } + + ul { + display: flex; + flex-wrap: wrap; + padding-inline-start: 10px; + + &.languages-new-line { + @media (--range-small-down) { + padding-inline-start: 0; + + li { + &:first-of-type { + a { + padding-inline-start: 0; + } + + &::before { + content: ''; + height: 100%; + border-inline-start: none; + } + } + } + } + } + + li { + margin-top: 6px; + display: inline-flex; + line-height: 1; + vertical-align: middle; + + &:first-of-type { + a { + padding-inline-start: 10px; + } + + &::before { + content: ''; + height: 100%; + border-inline-start: 1px solid var(--systemQuaternary); + } + } + + &::after { + border-inline-start: 1px solid var(--systemQuaternary); + content: ''; + padding-inline-end: 10px; + } + + &:last-child::after { + content: none; + } + } + } +</style> diff --git a/shared/components/src/components/helpers/ResizeDetector.svelte b/shared/components/src/components/helpers/ResizeDetector.svelte new file mode 100644 index 0000000..67b2453 --- /dev/null +++ b/shared/components/src/components/helpers/ResizeDetector.svelte @@ -0,0 +1,30 @@ +<script lang="ts"> + import { createEventDispatcher } from 'svelte'; + import { throttle } from '@amp/web-app-components/src/utils/throttle'; + + const dispatch = createEventDispatcher(); + + export let resizeThrottleLimit = 100; // Limit on how often to fire resize event + export let resizeTimeoutLimit = 250; // If resize event hasn't fired in this much time, we are no longer resizing + + let isResizing: boolean = false; + let resizeTimeoutId; + + const handleResize = () => { + isResizing = true; + + if (resizeTimeoutId) { + clearInterval(resizeTimeoutId); + } + + resizeTimeoutId = setTimeout( + () => (isResizing = false), + resizeTimeoutLimit, + ); + }; + + // Dispatch event whenever isResizing updates + $: dispatch('resizeUpdate', { isResizing }); +</script> + +<svelte:window on:resize={throttle(handleResize, resizeThrottleLimit)} /> diff --git a/shared/components/src/constants.ts b/shared/components/src/constants.ts new file mode 100644 index 0000000..826257c --- /dev/null +++ b/shared/components/src/constants.ts @@ -0,0 +1,53 @@ +// eslint-disable-next-line import/prefer-default-export +export const TEXT_DIRECTION = { + LTR: 'ltr', + RTL: 'rtl', +} as const; + +// https://www.fileformat.info/info/unicode/char/200e/index.htm +// these are unicode characters in four hexadecimal digits +export const LTR_MARK = '\u200e'; +export const RTL_MARK = '\u200f'; + +export const PLAY_STATES = { + PLAY: 'play', + PAUSE: 'pause', + BUFFER: 'buffer', + PLAYING: 'playing', +} as const; + +// eslint-disable-next-line import/prefer-default-export +export const SEARCH_EVENTS = { + MAKE_SEARCH_QUERY_FROM_SUGGESTION: 'makeSearchQueryFromSuggestion', + MAKE_SEARCH_QUERY_FROM_INPUT: 'makeSearchQueryFromInput', + CLICKED_OUTSIDE_SUGGESTIONS: 'clickedOutsideSuggestions', + CLICKED_OUTSIDE: 'clickedOutside', + RESET_SEARCH_INPUT: 'resetSearchInput', + SUGGESTION_CLICKED: 'suggestionClicked', + SUGGESTION_FOCUSED: 'suggestionFocused', + SEARCH_INPUT_HAS_FOCUS: 'searchInputHasFocus', + MENU_ITEM_CLICK: 'menuItemClick', + SHOW_SEARCH_SUGGESTIONS: 'showSearchSuggestions', + CLEAR: 'clear', +} as const; + +/** + * Locations where `SearchInput` component `clear` event can be called from. + * + * @remarks + * clear event can be triggered from two different locations + * rerturn object provides a way to distinguish between + * call points. + * + */ +export enum ClearEventLocation { + Cancel = 'cancel', + Input = 'input', +} + +export enum PopoverAnchorPositioning { + Top = 'top', + Bottom = 'bottom', + Left = 'left', + Right = 'right', +} diff --git a/shared/components/src/stores/media-query.ts b/shared/components/src/stores/media-query.ts new file mode 100644 index 0000000..83cc055 --- /dev/null +++ b/shared/components/src/stores/media-query.ts @@ -0,0 +1,63 @@ +// Based on https://github.com/cibernox/svelte-media +import { readable } from 'svelte/store'; +import { ArtworkConfig } from '@amp/web-app-components/config/components/artwork'; +import { getMediaConditions } from '@amp/web-app-components/src/utils/getMediaConditions'; + +const { BREAKPOINTS } = ArtworkConfig.get(); +const mqConditions = getMediaConditions(BREAKPOINTS); + +const DEFAULT_SETTING = 'medium'; + +/** + * Filters media query results and outputs the breakpoint name with a matching media query. + * + * @param {Object} mqls media query configurations (pulled from getMediaConditions()) + * @returns {String|undefined} breakpoint string that matches current media query + */ +function calculateMediaQuery(mqls: Record<string, MediaQueryList>): string { + return Object.entries(mqls) + .filter(([_, query]) => query.matches) + .map(([name, _]) => name)[0]; +} + +/** + * This function allows to build a store that tracks which of the given media query conditions matches. + * @param initialValue The inital value for the store. It only bears importance in server side rendering + * as it will update immediately in the browser + * @param mediaQueryConditions The dictionary with the media query names and the MQ condition to match against. + * @returns Svelte.Store<string> The name of the matching media query + */ +export function buildMediaQueryStore( + initialValue: string, + mediaQueryConditions: Record<string, string> = mqConditions, +) { + return readable(initialValue, (set) => { + if ( + typeof window === 'undefined' || + typeof matchMedia === 'undefined' + ) { + set(initialValue); + return; + } + + let mqls = {}; + let updateMediaQuery = () => set(calculateMediaQuery(mqls)); + + for (const key in mediaQueryConditions) { + mqls[key] = window.matchMedia(mediaQueryConditions[key]); + // `addListener` is deprecated but should still be used for compatibility with more browsers. + mqls[key].addListener(updateMediaQuery); + } + + updateMediaQuery(); + + return function (): void { + for (let key in mqls) { + // `removeListener` is deprecated but should still be used for compatibility with more browsers. + mqls[key].removeListener(updateMediaQuery); + } + }; + }); +} + +export const mediaQueries = buildMediaQueryStore(DEFAULT_SETTING, mqConditions); diff --git a/shared/components/src/stores/navigation-folders-open.ts b/shared/components/src/stores/navigation-folders-open.ts new file mode 100644 index 0000000..b761371 --- /dev/null +++ b/shared/components/src/stores/navigation-folders-open.ts @@ -0,0 +1,21 @@ +import { type Writable, writable } from 'svelte/store'; + +type FolderState = Writable<boolean>; +const folderStates = new Map<string, FolderState>(); + +export function subscribeFolderOpenState( + id: string, + defaultState?: boolean, +): FolderState { + let stateById = folderStates.get(id); + if (!stateById) { + folderStates.set(id, writable(defaultState ?? false)); + stateById = folderStates.get(id); + } + + return stateById; +} + +export function resetFoldersOpenState() { + folderStates.clear(); +} diff --git a/shared/components/src/stores/prefers-reduced-motion.ts b/shared/components/src/stores/prefers-reduced-motion.ts new file mode 100644 index 0000000..03d9393 --- /dev/null +++ b/shared/components/src/stores/prefers-reduced-motion.ts @@ -0,0 +1,27 @@ +import { readable } from 'svelte/store'; + +const DEFAULT_SETTING = false; + +export const prefersReducedMotion = readable(DEFAULT_SETTING, (set) => { + if (typeof window === 'undefined' || typeof matchMedia === 'undefined') { + set(DEFAULT_SETTING); + return; + } + + const motionQuery = matchMedia('(prefers-reduced-motion)'); + + /* istanbul ignore next */ + const motionQueryListener = (): void => { + set(motionQuery.matches); + }; + + // `addListener` is deprecated but should still be used for compatibility with more browsers. + motionQuery.addListener(motionQueryListener); + + set(motionQuery.matches); + + return function (): void { + // `removeListener` is deprecated but should still be used for compatibility with more browsers. + motionQuery.removeListener(motionQueryListener); + }; +}); diff --git a/shared/components/src/stores/sidebar-hidden.ts b/shared/components/src/stores/sidebar-hidden.ts new file mode 100644 index 0000000..2de14d1 --- /dev/null +++ b/shared/components/src/stores/sidebar-hidden.ts @@ -0,0 +1,12 @@ +import { derived } from 'svelte/store'; +import { buildMediaQueryStore } from '@amp/web-app-components/src/stores/media-query'; + +export const sidebarHiddenQuery = buildMediaQueryStore('visible', { + hidden: '(max-width: 483px)', + visible: '(min-width: 484px)', +}); + +export const sidebarIsHidden = derived( + sidebarHiddenQuery, + ($sidebarHiddenQuery) => $sidebarHiddenQuery === 'hidden', +); diff --git a/shared/components/src/utils/cookie.ts b/shared/components/src/utils/cookie.ts new file mode 100644 index 0000000..112733f --- /dev/null +++ b/shared/components/src/utils/cookie.ts @@ -0,0 +1,71 @@ +export function getCookie(name: string): string | null { + if (typeof document === 'undefined') { + return null; + } + + const prefix = `${name}=`; + const cookie = document.cookie + .split(';') + .map((value) => value.trimStart()) + .filter((value) => value.startsWith(prefix))[0]; + + if (!cookie) { + return null; + } + + return cookie.substr(prefix.length); +} + +export function setCookie( + name: string, + value: string, + domain: string, + expires = 0, + path = '/', +): void { + if (typeof document === 'undefined') { + return undefined; + } + + // Get any potential existing instances of this particular cookie + const existingCookie = getCookie(name); + let cookieValue = value; + + if (existingCookie) { + // If exisitng cookie name does not include the value we are trying to set, + // then add it, otherwise use the existing cookie value + cookieValue = !existingCookie.includes(value) + ? `${existingCookie}+${value}` + : existingCookie; + } + + let cookieString = `${name}=${cookieValue}; path=${path}; domain=${domain};`; + + if (expires) { + const date = new Date(); + date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000); + + cookieString += ` expires=${date.toUTCString()};`; + } + + document.cookie = cookieString; + + // Returning undefined because of ESLint's "consistent-return" rule + return undefined; +} + +export function clearCookie(name: string, domain: string, path = '/'): void { + if (typeof document === 'undefined') { + return undefined; + } + + // Get any potential existing instances of this particular cookie + const existingCookie = getCookie(name); + + if (existingCookie) { + // Set the cookie's expiration date to a past date + setCookie(name, '', domain, -1, path); + } + + return undefined; +} diff --git a/shared/components/src/utils/date.ts b/shared/components/src/utils/date.ts new file mode 100644 index 0000000..f128de7 --- /dev/null +++ b/shared/components/src/utils/date.ts @@ -0,0 +1,51 @@ +// Breaks duration down from milliseconds into hours/minutes/seconds +export function getDurationParts(durationInMilliseconds: number): { + hours: number; + minutes: number; + seconds: number; +} { + // convert ms to seconds + const durationInSeconds = Math.floor(durationInMilliseconds / 1000); + const duration = Math.round(durationInSeconds); + + return { + hours: Math.floor(duration / 3600), + minutes: Math.floor(duration / 60) % 60, + seconds: duration % 60, + }; +} + +// returns normal numeric date in YYYY-MM-DD from a date string +// AKA getNumericDateFromReleaseDate but renamed to be more generic +// +// ex: getNumericDateFromDateString('2024-04-15T08:41:03Z') => '2024-04-15' +// getNumericDateFromDateString('15 April 2024 14:48 UTC') => '2024-04-15' +export function getNumericDateFromDateString( + timestamp?: string, +): string | undefined { + if (!timestamp) { + return undefined; + } + + return new Date(timestamp).toISOString().split('T')?.[0]; +} + +// Utility to format ISO8601 Duration Strings from raw milliseconds (ex: PT2M42S). +export function formatISODuration(durationInMilliseconds: number): string { + const { hours, minutes, seconds } = getDurationParts( + durationInMilliseconds, + ); + + if (!hours && !minutes && !seconds) { + return 'P0D'; + } + + return [ + 'PT', + hours && `${hours}H`, + minutes && `${minutes}M`, + seconds && `${seconds}S`, + ] + .filter(Boolean) + .join(''); +} diff --git a/shared/components/src/utils/debounce.ts b/shared/components/src/utils/debounce.ts new file mode 100644 index 0000000..fcadbef --- /dev/null +++ b/shared/components/src/utils/debounce.ts @@ -0,0 +1,40 @@ +/* eslint-disable import/prefer-default-export */ + +/** + * @name debounce + * @description + * Creates a debounced function that delays invoking func until + * after delayMs milliseconds have elapsed since the last time the + * debounced function was invoked. + * + * @param delayMs - delay in milliseconds + * @param immediate - Specify invoking on the leading edge of the timeout + * (Defaults to trailing) + * + *(f: F): (...args: Parameters<F>) => void + */ +export function debounce<F extends (...args: any[]) => any>( + fn: F, + delayMs: number, + immediate = false, +): (...args: Parameters<F>) => void { + let timerId; + + return function debounced(...args) { + const shouldCallNow = immediate && !timerId; + clearTimeout(timerId); + + if (shouldCallNow) { + fn.apply(this, args); + } + + timerId = setTimeout(() => { + timerId = null; + if (!immediate) { + fn.apply(this, args); + } + }, delayMs); + }; +} + +export const DEFAULT_MOUSE_OVER_DELAY = 300; diff --git a/shared/components/src/utils/getMediaConditions.ts b/shared/components/src/utils/getMediaConditions.ts new file mode 100644 index 0000000..2d5028b --- /dev/null +++ b/shared/components/src/utils/getMediaConditions.ts @@ -0,0 +1,117 @@ +import type { Breakpoints, Size } from '@amp/web-app-components/src/types'; + +export type MediaConditions<T extends string | number | symbol = Size> = { + [key in T]?: string; +}; + +type BasicBreapoints<T extends string | number | symbol> = Record<T, number>; + +type BreakpointOptions = { offset?: number }; + +// eslint-disable-next-line import/prefer-default-export +export function getMediaConditions<T extends string | number | symbol = Size>( + breakpoints: Breakpoints<T>, + options?: BreakpointOptions, +): MediaConditions<T> { + const viewportOrder = { + xsmall: 0, + small: 1, + medium: 2, + large: 3, + xlarge: 4, + }; + + const offset = options?.offset ?? 0; + const viewportSizes = Object.keys(breakpoints).sort( + (a, b) => viewportOrder[a] - viewportOrder[b], + ) as T[]; + + return viewportSizeToMediaConditions<T>(breakpoints, viewportSizes, offset); +} + +function viewportSizeToMediaConditions<T extends string | number | symbol>( + breakpoints: Breakpoints<T>, + viewportSizes?: T[], + offset?: number, +): MediaConditions<T> { + viewportSizes ||= Object.keys(breakpoints) as T[]; + const queries: MediaConditions<T> = {}; + viewportSizes.reduce((acc, viewport) => { + const { min, max } = { + min: undefined, + max: undefined, + ...breakpoints[viewport], + }; + + if (min && !max) { + acc[viewport] = `(min-width:${min + offset}px)`; + } else if (!min && max) { + acc[viewport] = `(max-width:${max + offset}px)`; + } else if (min && max) { + acc[viewport] = `(min-width:${min + offset}px) and (max-width:${ + max + offset + }px)`; + } + return acc; + }, queries); + return queries; +} + +/** + * Transforms a breakpoints object into media queries that match ranges between each breakpoint and the next. + * + * @param breakpoints - Object with breakpoint names as keys and pixel values as values + * @returns Object with breakpoint names as keys and media query strings as values + * + * @example + * const breakpoints = { XSM: 0, SM: 350, MD: 484, LG: 1000 }; + * const mediaQueries = breakpointsToMediaQueries(breakpoints); + * // Returns: + * // { + * // XSM: '(max-width: 349px)', + * // SM: '(min-width: 350px) and (max-width: 483px)', + * // MD: '(min-width: 484px) and (max-width: 999px)', + * // LG: '(min-width: 1000px)' + * // } + */ +export function breakpointsToMediaQueries<T extends string>( + breakpoints: BasicBreapoints<T>, +): MediaConditions<T> { + const entries = Object.entries(breakpoints) as [T, number][]; + entries.sort(([, a], [_, b]) => a - b); + const transformedBreakpoints: Breakpoints<T> = {}; + + entries.forEach(([breakpointName, minWidth], index) => { + const isFirst = index === 0; + const isLast = index === entries.length - 1; + const nextBreakpointWidth = isLast ? null : entries[index + 1][1]; + + if (isFirst && minWidth === 0) { + // First breakpoint starting at 0: only max-width + if (nextBreakpointWidth !== null) { + transformedBreakpoints[breakpointName] = { + max: nextBreakpointWidth - 1, + }; + } else { + // Edge case: only one breakpoint starting at 0 + transformedBreakpoints[breakpointName] = { min: 0 }; + } + } else if (isLast) { + // Last breakpoint: only min-width + transformedBreakpoints[breakpointName] = { min: minWidth }; + } else { + // Middle breakpoints: min-width and max-width range + transformedBreakpoints[breakpointName] = { + min: minWidth, + max: nextBreakpointWidth! - 1, + }; + } + }); + + const viewportSizes = entries.map(([breakpointName]) => breakpointName); + return viewportSizeToMediaConditions<T>( + transformedBreakpoints, + viewportSizes, + 0, + ); +} diff --git a/shared/components/src/utils/getStorefrontRoute.ts b/shared/components/src/utils/getStorefrontRoute.ts new file mode 100644 index 0000000..2aaaace --- /dev/null +++ b/shared/components/src/utils/getStorefrontRoute.ts @@ -0,0 +1,29 @@ +/** + * Defines a route based on a given default route and + * otherwise falls back to the base storefront path + * + * @param defaultRoute - ie 'browse', 'listen-now', or empty string + * @param storefront - storefront id ie 'us' + * @param language - language tag ie 'en-US' + * @returns route - ie /us/browse?l=es-MX + */ +export function getStorefrontRoute( + defaultRoute: string, + storefront: string, + language?: string, +): string { + let route; + + if (defaultRoute === '') { + route = `/${storefront}`; + } else { + route = `/${storefront}/${defaultRoute}`; + } + + // add optional language tag if that is passed in + if (language) { + route = `${route}?l=${language}`; + } + + return route; +} diff --git a/shared/components/src/utils/getUpdatedFocusedIndex.ts b/shared/components/src/utils/getUpdatedFocusedIndex.ts new file mode 100644 index 0000000..ca2c765 --- /dev/null +++ b/shared/components/src/utils/getUpdatedFocusedIndex.ts @@ -0,0 +1,25 @@ +export function getUpdatedFocusedIndex( + incrementAmount: number, + currentFocusedIndex: number | null, + numberOfItems: number, +): number { + const potentialFocusedIndex = incrementAmount + currentFocusedIndex; + + if (incrementAmount > 0) { + if (currentFocusedIndex === null) { + return 0; + } else { + return potentialFocusedIndex >= numberOfItems + ? 0 + : potentialFocusedIndex; + } + } else { + if (currentFocusedIndex === null) { + return numberOfItems - 1; + } else { + return potentialFocusedIndex < 0 + ? numberOfItems - 1 + : potentialFocusedIndex; + } + } +} diff --git a/shared/components/src/utils/internal/locale/index.ts b/shared/components/src/utils/internal/locale/index.ts new file mode 100644 index 0000000..e4165a9 --- /dev/null +++ b/shared/components/src/utils/internal/locale/index.ts @@ -0,0 +1,17 @@ +/* istanbul ignore file */ + +//TODO rdar://93379311 (Solution for sharing context between app + shared components) +import { getContext, setContext } from 'svelte'; +import type { Locale } from '@amp/web-app-components/src/types'; + +const CONTEXT_NAME = 'shared:locale'; + +// WARNING these signatures can change after rdar://93379311 +export function setLocale(context: Map<string, unknown>, locale: Locale) { + context.set(CONTEXT_NAME, locale); +} + +// WARNING these signatures can change after rdar://93379311 +export function getLocale(): Locale { + return getContext(CONTEXT_NAME) as Locale | undefined; +} diff --git a/shared/components/src/utils/makeSafeTick.ts b/shared/components/src/utils/makeSafeTick.ts new file mode 100644 index 0000000..f9ea8c2 --- /dev/null +++ b/shared/components/src/utils/makeSafeTick.ts @@ -0,0 +1,64 @@ +/* eslint-disable import/prefer-default-export */ +// eslint-disable-next-line no-restricted-imports +import { tick as svelteTick, onDestroy } from 'svelte'; + +// Unfortantely for TS to recognize that this can be awaited +// we need to leave `Promise<void | never>` otherwise TS hints +// will suggest removing the await. +// See @remarks for reason to disable `then` +type TickType = () => Omit<Promise<string>, 'then'> | Promise<void | never>; + +type SafeTickCallback = (tick: TickType) => Promise<void | never>; + +class DestroyedError extends Error { + constructor() { + super('component was destroyed before tick resolved.'); + this.name = 'DestroyedError'; + } +} + +/** + * Provides a safer way to use svelte's tick helper. + * + * This prevents code that relies on tick() from running + * if the component is destroyed while the tick resolution + * is inflight. + * + * @remarks + * To avoid floating promises (promises with no return statements) + * it is safer to use the `async/await` syntax. + * + * If this is used with the `.then()` syntax without the promise + * being returned the DestroyedError will bubble up to sentry. + * + * @example + * ```ts + * const safeTick = makeSafeTick(); + * onMount(async() => { + * await safeTick(async (tick) => { + * // Use tick normally + * await tick(); + * // ... + * }); + * }); + * ``` + */ +export const makeSafeTick = (): (( + callback: SafeTickCallback, +) => Promise<void | never>) => { + let destroyed = false; + onDestroy(() => { + destroyed = true; + }); + + return async (callback) => { + try { + await callback(async () => { + await svelteTick(); + if (destroyed) throw new DestroyedError(); + }); + } catch (e) { + if (!(e instanceof DestroyedError)) throw e; + } + }; +}; diff --git a/shared/components/src/utils/memoize.ts b/shared/components/src/utils/memoize.ts new file mode 100644 index 0000000..a5e07ef --- /dev/null +++ b/shared/components/src/utils/memoize.ts @@ -0,0 +1,26 @@ +// eslint-disable-next-line import/prefer-default-export +export function memoize<T extends unknown[], S>( + fn: (...args: T) => S, + hashFn: (...args: unknown[]) => string = JSON.stringify, + entryLimit = 5, +): (...args: T) => S { + const cache: Map<string, S> = new Map(); + + return (...args: T) => { + const value = hashFn(args); + if (cache.has(value)) { + return cache.get(value); + } + + const returnedValue: S = fn.apply(this, args); + + if (cache.size >= entryLimit) { + const iterator = cache.keys(); + const firstValue = iterator.next().value; + // remove oldest value + cache.delete(firstValue); + } + cache.set(value, returnedValue); + return returnedValue; + }; +} diff --git a/shared/components/src/utils/rafQueue.ts b/shared/components/src/utils/rafQueue.ts new file mode 100644 index 0000000..a56d9a7 --- /dev/null +++ b/shared/components/src/utils/rafQueue.ts @@ -0,0 +1,74 @@ +/** + * @name RequestAnimationFrameLimiter + * @description + * allows for multiple callbacks to be called + * within a single RAF function. + * It also spreads long running tasks across multiple + * microtask to help keep the main thread free for user interactions + * + */ +export class RequestAnimationFrameLimiter { + private queue: Array<(timestamp?: number) => void>; + private RAF_FN_LIMIT_MS: number; + private requestId: number | null; + constructor() { + this.queue = []; + // ideal limit for scroll based animations: https://developers.google.com/web/fundamentals/performance/rendering/optimize-javascript-execution#reduce_complexity_or_use_web_workers + this.RAF_FN_LIMIT_MS = 3; + this.requestId = null; + } + + private flush(): void { + this.requestId = + this.queue.length === 0 + ? null + : window.requestAnimationFrame((timestamp) => { + const start = window.performance.now(); + let ellapsedTime = 0; + const { RAF_FN_LIMIT_MS } = this; + let count = 0; + + while ( + count < this.queue.length && + ellapsedTime < RAF_FN_LIMIT_MS + ) { + let item = this.queue[count]; + if (item) { + item(timestamp); + } + const finishTime = window.performance.now(); + + count = count + 1; + ellapsedTime = finishTime - start; + } + const newQueue = this.queue.slice(count); + + this.queue = newQueue; + this.flush(); + }); + } + public add(callback: () => void): void { + this.queue.push(callback); + if (this.requestId === null) { + this.flush(); + } + } +} + +let raf: RequestAnimationFrameLimiter | ServerSafeRAFLimiter = null; + +type ServerSafeRAFLimiter = { + add: (callback: () => void) => void; +}; + +export const getRafQueue = () => { + if (typeof window === 'undefined') { + // SSR safe + raf = { + add: (callback: () => void) => callback(), + }; + } else if (raf === null) { + raf = new RequestAnimationFrameLimiter(); + } + return raf; +}; diff --git a/shared/components/src/utils/sanitize-html/browser.ts b/shared/components/src/utils/sanitize-html/browser.ts new file mode 100644 index 0000000..ad8b804 --- /dev/null +++ b/shared/components/src/utils/sanitize-html/browser.ts @@ -0,0 +1,26 @@ +// Browser ONLY logic. Must have the same exports as server.ts +// See: docs/isomorphic-imports.md + +import { type SanitizeHtmlOptions, sanitizeDocument } from './common'; + +export { type SanitizeHtmlOptions, DEFAULT_SAFE_TAGS } from './common'; + +// Shared DOMParser instance (avoids creating a new one for each sanitization) +let parser = null; + +export function sanitizeHtml( + input: string, + options: SanitizeHtmlOptions = {}, +): string { + if (!input) { + return input; + } + + if (!parser) { + parser = new DOMParser(); + } + + const unsafeDocument = parser.parseFromString(`${input}`, 'text/html'); + const unsafeNode = unsafeDocument.body; + return sanitizeDocument(unsafeDocument, unsafeNode, options); +} diff --git a/shared/components/src/utils/sanitize-html/common.ts b/shared/components/src/utils/sanitize-html/common.ts new file mode 100644 index 0000000..38b3b2e --- /dev/null +++ b/shared/components/src/utils/sanitize-html/common.ts @@ -0,0 +1,176 @@ +type AllowedTags = Set<string>; + +interface AllowedAttributes { + [tagName: string]: Set<string>; +} + +export interface SanitizeHtmlOptions { + allowedTags?: string[]; + extraAllowedTags?: string[]; + keepChildrenWhenRemovingParent?: boolean; + + /** + * When true, replaces all entities with regular spaces + * to prevent unwanted line breaks in the rendered HTML + */ + removeNbsp?: boolean; + + /** + * AllowedAttributes should be an object with tag name keys and array values + * containing all of the attributes allowed for that tag: + * + * { 'p': ['class'], 'div': ['role', 'aria-hidden'] } + * + * The above allows ONLY the class attribute for <p> and ONLY the role and + * aria-hidden attributes for <div>. + */ + allowedAttributes?: { + [tagName: string]: string[]; + }; +} + +export const DEFAULT_SAFE_TAGS: string[] = [ + 'strong', + 'em', + 'b', + 'i', + 'u', + 'br', +]; +const DEFAULT_SAFE_ATTRS = {}; + +/** + * Sanitizes HTML by removing all tags and attributes that aren't explicitly allowed. + */ +export function sanitizeDocument( + unsafeDocument: Document, + unsafeNode: Node | DocumentFragment, + { + allowedTags, + extraAllowedTags, + allowedAttributes = DEFAULT_SAFE_ATTRS, + keepChildrenWhenRemovingParent, + removeNbsp, + }: SanitizeHtmlOptions = {}, +): string { + if (allowedTags && extraAllowedTags) { + throw new Error( + 'sanitizeHtml got both allowedTags and extraAllowedTags', + ); + } + + const allowedTagsSet = new Set([ + ...(extraAllowedTags || []), + ...(allowedTags || DEFAULT_SAFE_TAGS), + ]); + + const allowedAttributeSets = {}; + for (const [tag, attributes] of Object.entries(allowedAttributes)) { + allowedAttributeSets[tag] = new Set(attributes); + } + + const sanitizedContainer = unsafeDocument.createElement('div'); + + for (const child of [...unsafeNode.childNodes]) { + const sanitizedChildArray = sanitizeNode( + child as Element, + allowedTagsSet, + allowedAttributeSets, + keepChildrenWhenRemovingParent, + ); + sanitizedChildArray.forEach((node) => { + sanitizedContainer.appendChild(node); + }); + } + + let html = sanitizedContainer.innerHTML; + + // Replace with regular spaces if removeNbsp option is enabled + if (removeNbsp) { + html = html.replace(/ /g, ' '); + } + + return html; +} + +function sanitizeNode( + node: Element, + allowedTags: AllowedTags, + allowedAttributes: AllowedAttributes, + keepChildrenWhenRemovingParent: boolean, +): Node[] | Element[] { + // Plain text is safe as is + // NOTE: The lowercase node (instead of Node) is intentional. Node is only + // accessible in browser. In Node.js, it depends on jsdom (which we + // avoid importing to exclude from the clientside vendor bundle). + // Instead of passing down window.Node or jsdom.Node depending on + // context, we rely on the fact that instances of Node (of which node + // will be one) will also have these constants set on them. + if ( + ([node.TEXT_NODE, node.CDATA_SECTION_NODE] as number[]).includes( + node.nodeType, + ) + ) { + return [node]; + } + + // Refuse anything that isn't a tag or one of the allowed tags + const tagName = (node.tagName || '').toLowerCase(); + + if (!allowedTags.has(tagName)) { + // when keepChildrenWhenRemovingParent is true + // we check children for valid nodes as well + if (keepChildrenWhenRemovingParent) { + return sanitizeChildren( + node, + allowedTags, + allowedAttributes, + keepChildrenWhenRemovingParent, + ); + } + return []; + } + + // Reconstruct node with only the allowedAttributes and sanitize its children + const sanitized = node.ownerDocument.createElement(tagName); + const currentlyAllowedAttributes = allowedAttributes[tagName] || new Set(); + + for (const { name, nodeValue: value } of [...node.attributes]) { + if (currentlyAllowedAttributes.has(name)) { + sanitized.setAttribute(name, value); + } + } + + const children = sanitizeChildren( + node, + allowedTags, + allowedAttributes, + keepChildrenWhenRemovingParent, + ); + + children.forEach((child) => { + sanitized.appendChild(child); + }); + + return [sanitized]; +} + +const sanitizeChildren = ( + node: Element, + allowedTags: AllowedTags, + allowedAttributes: AllowedAttributes, + tagsToConvertToText: boolean, +): Node[] => { + const children = [...node.childNodes] + .map((childNode) => + sanitizeNode( + childNode as Element, + allowedTags, + allowedAttributes, + tagsToConvertToText, + ), + ) + .flat(); + + return children; +}; diff --git a/shared/components/src/utils/sanitize.ts b/shared/components/src/utils/sanitize.ts new file mode 100644 index 0000000..107a543 --- /dev/null +++ b/shared/components/src/utils/sanitize.ts @@ -0,0 +1,32 @@ +// Take care with < (which has special meaning inside script tags) +// See: https://github.com/sveltejs/kit/blob/ff9a27b4/packages/kit/src/runtime/server/page/serialize_data.js#L4-L28 +const replacements = { + '<': '\\u003C', + '\u2028': '\\u2028', + '\u2029': '\\u2029', +}; + +const pattern = new RegExp(`[${Object.keys(replacements).join('')}]`, 'g'); + +/** + * Serializes a POJO into a HTML <script> tag that can be read clientside by + * `deserializeServerData`. + * + * Use this to share data between serverside and clientside. Include the + * returned HTML in the response to a client to allow it to read this data. + * + * @param data data to serialize + * @returns serialized data (or empty string if serialization fails) + */ +export function serializeJSONData(data: object): string { + try { + return JSON.stringify(data).replace( + pattern, + (match) => replacements[match], + ); + } catch (e) { + // Don't let recursive data (or other non-serializable things) throw. + // We'd rather just let the serialize no-op to avoid breaking consumers. + return ''; + } +} diff --git a/shared/components/src/utils/scrollByPolyfill.ts b/shared/components/src/utils/scrollByPolyfill.ts new file mode 100644 index 0000000..1a73a4f --- /dev/null +++ b/shared/components/src/utils/scrollByPolyfill.ts @@ -0,0 +1,143 @@ +// COPIED FROM +// https://github.pie.apple.com/amp-ui/ember-ui-media-shelf/blob/580ff07a546771bce8b3d85494c6268860e97215/addon/-private/scroll-by-polyfill.js + +const SCROLL_TIME = 468; +const Element = + typeof window !== 'undefined' ? window.HTMLElement || window.Element : null; + +let originalScrollBy; + +/** + * returns result of applying ease math function to a number + * @method ease + * @param {Number} k + * @returns {Number} + */ +function ease(k: number): number { + return 0.5 * (1 - Math.cos(Math.PI * k)); +} + +// define timing method +const now: () => number = + typeof window !== 'undefined' && window?.performance?.now + ? window.performance.now.bind(window.performance) + : Date.now; + +/** + * changes scroll position inside an element + * @method scrollElement + * @param {Number} x + * @returns {undefined} + */ +function scrollElement(x: number): void { + this.scrollLeft = x; +} + +/** + * self invoked function that, given a context, steps through scrolling + * @method step + * @param {Object} context + * @returns {undefined} + */ +type Context = { + startTime: number; + startX: number; + x: number; + method: (x: number) => void; + scrollable: HTMLElement; +}; +function step(context: Context): void { + const time = now(); + let elapsed = (time - context.startTime) / SCROLL_TIME; + + // avoid elapsed times higher than one + elapsed = Math.min(1, elapsed); + + // apply easing to elapsed time + const value = ease(elapsed); + + const currentX = context.startX + (context.x - context.startX) * value; + + context.method.call(context.scrollable, currentX); + + // scroll more if we have not reached our destination + if (currentX !== context.x) { + window.requestAnimationFrame(step.bind(window, context)); + } +} + +/** + * scrolls window or element with a smooth behavior + * @method smoothScroll + * @param {Object|Node} el + * @param {Number} x + * @returns {undefined} + */ +function smoothScroll(el: HTMLElement, x: number): void { + const startTime = now(); + // define scroll context + const startX = el.scrollLeft; + const method = scrollElement; + + // scroll looping over a frame + step({ + scrollable: el, + method, + startTime, + startX, + x, + }); +} + +let polyfillHasRun = false; +/** + * ripped partially from https://github.com/iamdustan/smoothscroll/blob/master/src/smoothscroll.js + * Only polyfill horizontal scroll space to avoid unexpected behaviour in parent apps + * + * @method scrollByPolyfill + */ +export default function scrollByPolyfill(): void { + // return if scroll behavior is supported + if ('scrollBehavior' in document.documentElement.style || polyfillHasRun) { + return; + } + + // if prefers-reduce-motion && need polyfill, navigate shelf immediately without easing + const motionMediaQuery = window.matchMedia( + '(prefers-reduced-motion: reduce)', + ); + function addScrollByToProto() { + if (motionMediaQuery.matches) { + if (originalScrollBy) { + Element.prototype.scrollBy = originalScrollBy; + } + return; + } + + function scrollByPoly(options: ScrollToOptions): void; + function scrollByPoly(x: number, _y: number): void; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + function scrollByPoly( + paramOne: number | ScrollToOptions, + _paramTwo?: number, + ): void { + let xValue = 0; + if (typeof paramOne === 'number') { + xValue = paramOne; + } else if (typeof paramOne === 'object') { + xValue = paramOne.left || 0; + } + + const moveByX = this.scrollLeft + xValue; + smoothScroll(this, moveByX); + } + + originalScrollBy = Element.prototype.scrollBy; + Element.prototype.scrollBy = scrollByPoly; + } + + motionMediaQuery.addListener(addScrollByToProto); + + addScrollByToProto(); + polyfillHasRun = true; +} diff --git a/shared/components/src/utils/shelfAspectRatio.ts b/shared/components/src/utils/shelfAspectRatio.ts new file mode 100644 index 0000000..eeb977d --- /dev/null +++ b/shared/components/src/utils/shelfAspectRatio.ts @@ -0,0 +1,75 @@ +import { getAspectRatio } from '@amp/web-app-components/src/components/Artwork/utils/artProfile'; +import { setContext, getContext, hasContext } from 'svelte'; +import { derived, writable } from 'svelte/store'; +import type { Readable } from 'svelte/store'; +import type { Profile } from '@amp/web-app-components/src/components/Artwork/types'; +import type { AspectRatioOverrideConfig } from '@amp/web-app-components/src/components/Shelf/types'; + +const SHELF_ASPECT_RATIO_KEY = 'shelf-aspect-ratio'; + +export const getShelfAspectRatioContext = (): { + shelfAspectRatio: Readable<string>; + addProfile: (profile: string | Profile) => void; +} => { + return getContext(SHELF_ASPECT_RATIO_KEY); +}; + +export const hasShelfAspectRatioContext = () => + hasContext(SHELF_ASPECT_RATIO_KEY); + +const createShelfAspectRatioStore = (config: AspectRatioOverrideConfig) => { + const { subscribe, update } = writable(new Map() as Map<string, number>); + + const addProfile = (profile: string) => { + const ratio = getAspectRatio(profile).toFixed(2); + + update((ratiosCount) => { + const currentCount = ratiosCount.get(ratio); + const newCount = ratiosCount.has(ratio) ? currentCount + 1 : 0; + ratiosCount.set(ratio, newCount); + return ratiosCount; + }); + }; + + const aspectRatioStore = { + subscribe, + addProfile, + }; + + const shelfAspectRatio = derived(aspectRatioStore, ($store) => { + let aspectRatio: string = null; + + // Don't set shelf aspect ratio when only 1 ratio is found + // + // This allows e.g. a shelf with only tall artwork Powerswooshes to use + // their native 3:4 aspect ratio, even when the shelf is set to use the + // fixed 1:1 aspect ratio or a dominant aspect ratio. + if ($store.size > 1) { + if (config.type === 'fixed') { + aspectRatio = config.aspectRatio; + } else if (config.type === 'dominant') { + let highestCount = 0; + for (const [ratio, count] of $store.entries()) { + if (highestCount < count) { + aspectRatio = ratio; + highestCount = count; + } + } + } + } + + return aspectRatio; + }); + + return { + shelfAspectRatio, + addProfile, + }; +}; + +export const createShelfAspectRatioContext = ( + config: AspectRatioOverrideConfig, +) => { + setContext(SHELF_ASPECT_RATIO_KEY, createShelfAspectRatioStore(config)); + return getShelfAspectRatioContext(); +}; diff --git a/shared/components/src/utils/should-show-navigation-item.ts b/shared/components/src/utils/should-show-navigation-item.ts new file mode 100644 index 0000000..194628a --- /dev/null +++ b/shared/components/src/utils/should-show-navigation-item.ts @@ -0,0 +1,25 @@ +export function shouldShowNavigationItem( + visibilityPreferencesKey: string | null, + isEditing: boolean, + data: Record<string, boolean> | null, + itemVisibilityPreferenceKey: string, +): boolean { + // If there are no visibility preferences, + // the item should always be shown. + if (!visibilityPreferencesKey) { + return true; + } + + // If the visibility preference of an item + // is in an editing state, it should be shown. + if (isEditing) { + return true; + } + + // Show the item if the visibility preference is to show it. + if (data && data[itemVisibilityPreferenceKey]) { + return true; + } + + return false; +} diff --git a/shared/components/src/utils/throttle.ts b/shared/components/src/utils/throttle.ts new file mode 100644 index 0000000..b5e36bc --- /dev/null +++ b/shared/components/src/utils/throttle.ts @@ -0,0 +1,49 @@ +/* eslint-disable import/prefer-default-export */ +/** + * @name throttle + * @description + * Creates a throttled function that only invokes func at most once per every limit time (ms). + * + * *NOTE: this does not capture or recall all functions that were triggered. + * This will drop function calls that happen during the throttle time* + * @param limit - time to wait between calls in ms + * @example + * Normal event + * event | | | | + * time ---------------- + * callback | | | | + * + * Throttled event [300ms] + * event | | | | + * time ---------------- + * callback | | | + * [300] [300] + */ + +export function throttle<T extends []>( + func: (..._: T) => unknown, + limit: number, +): (..._: T) => void { + let lastTimeoutId; + let lastCallTime: number; + + return function throttled(...args) { + const nextCall = () => { + func.apply(this, args); + lastCallTime = Date.now(); + }; + + if (!lastCallTime) { + nextCall(); + } else { + clearTimeout(lastTimeoutId); + const timeBetweenCalls = Date.now() - lastCallTime; + const waitTime = Math.max(0, limit - timeBetweenCalls); + lastTimeoutId = setTimeout(() => { + if (timeBetweenCalls >= limit) { + nextCall(); + } + }, waitTime); + } + }; +} diff --git a/shared/components/src/utils/uniqueId.ts b/shared/components/src/utils/uniqueId.ts new file mode 100644 index 0000000..3a6d21d --- /dev/null +++ b/shared/components/src/utils/uniqueId.ts @@ -0,0 +1,71 @@ +import { getContext } from 'svelte'; + +export const UNIQUE_ID_CONTEXT_NAME = 'amp-web-unique-id'; + +interface UniqueContext { + nextId: number; +} + +// TODO: rdar://84029606 (Extract logger into shared util) +interface Logger { + warn(...args: any[]): string; +} +interface LoggerFactory { + loggerFor(name: string): Logger; +} + +export function initializeUniqueIdContext( + context: Map<string, unknown>, + loggerFactory: LoggerFactory, +): void { + const logger = loggerFactory.loggerFor('uniqueIdContext'); + + if (context.has(UNIQUE_ID_CONTEXT_NAME)) { + logger.warn( + `${UNIQUE_ID_CONTEXT_NAME} context has already been created. Cannot be created more than once`, + ); + } else { + const INITAL_STATE: UniqueContext = { nextId: 0 }; + context.set(UNIQUE_ID_CONTEXT_NAME, INITAL_STATE); + } +} + +/** + * Creates a unique Id string based on string provided + * + * @returns unique id string + */ +export type UniqueIdGenerator = () => string; + +// Custom elements most likely will not be used in an environment has that initialized the Svelte +// context. Components that are later wrapped by a custom element should use this function so that +// they can generate unique ids automatically when used inside a Svelte app, but not throw an error +// when used in other contexts. +// +export function maybeGetUniqueIdGenerator(): UniqueIdGenerator | undefined { + const UNIQUE_ID_PREFIX = 'uid-'; + const state: UniqueContext = getContext(UNIQUE_ID_CONTEXT_NAME); + const isNextIdANumber = typeof state?.nextId === 'number'; + + if (!isNextIdANumber) { + return; + } + + return () => { + const id = `${UNIQUE_ID_PREFIX}${state.nextId}`; + state.nextId += 1; + return id; + }; +} + +export function getUniqueIdGenerator(): UniqueIdGenerator { + const uniqueIdGenerator = maybeGetUniqueIdGenerator(); + + if (!uniqueIdGenerator) { + throw new Error( + `${UNIQUE_ID_CONTEXT_NAME} context has not been initialized. Initialize at application bootstrap.`, + ); + } + + return uniqueIdGenerator; +} diff --git a/shared/featurekit/src/is-enabled.ts b/shared/featurekit/src/is-enabled.ts new file mode 100644 index 0000000..eda506f --- /dev/null +++ b/shared/featurekit/src/is-enabled.ts @@ -0,0 +1,7 @@ +export const isEnabled = (key: string): boolean => { + if (typeof window !== 'undefined') { + return window._featureKit?.isEnabled(key) ?? false; + } + + return false; +}; diff --git a/shared/fonts/src/index.ts b/shared/fonts/src/index.ts new file mode 100644 index 0000000..509d211 --- /dev/null +++ b/shared/fonts/src/index.ts @@ -0,0 +1,53 @@ +const LOCALES_REGEX = { + ARABIC: /^(ar-?)/, + HEBREW: /^(he-?)/, + HINDI: /^(hi-?)/, + JAPANESE: /^(ja-?)/, + KOREAN: /^(ko-?)/, + THAI: /^(th-?)/, +}; + +export const BASE = '//www.apple.com/wss/fonts'; + +/** + * + * @param locale %7C String %7C The locale to get the font URL for + * @param includeNewYork %7C Boolean %7C Used to specifiy whether to include the New York font which is an additional font in addition to SF Pro + * @returns URL string to fetch fonts + */ +export function getFontURL(locale: string, includeNewYork?: boolean): string { + let fonts = 'SF+Pro,v4%7CSF+Pro+Icons,v1'; + // Check for any Arabic locales first (full list here: https://confluence.sd.apple.com/display/AMPRighttoLeft/BRD) + if (LOCALES_REGEX.ARABIC.test(locale)) { + fonts = `${fonts}%7CArabic+UI,v1`; + } else if (LOCALES_REGEX.HEBREW.test(locale)) { + fonts = `${fonts}%7CArial+Hebrew,v1`; + } else if (LOCALES_REGEX.HINDI.test(locale)) { + fonts = `${fonts}%7CKohinoor+Devanagari,v1`; + } else if (LOCALES_REGEX.JAPANESE.test(locale)) { + fonts = `${fonts}%7CSF+Pro+JP,v1`; + } else if (LOCALES_REGEX.KOREAN.test(locale)) { + fonts = `${fonts}%7CSF+Pro+KR,v2`; + } else if (LOCALES_REGEX.THAI.test(locale)) { + fonts = `${fonts}%7CThonburi+Pro,v1`; + } + + switch (locale) { + case 'zh-cn': + fonts = `SF+Pro,v4%7CSF+Pro+SC,v1%7CSF+Pro+Icons,v1`; + break; + case 'zh-hk': + fonts = `SF+Pro,v4%7CSF+Pro+HK,v1%7CSF+Pro+Icons,v1`; + break; + case 'zh-mo': + case 'zh-tw': + fonts = `SF+Pro,v4%7CSF+Pro+TC,v1%7CSF+Pro+Icons,v1`; + break; + } + + if (includeNewYork) { + fonts = `${fonts}%7CNew+York+Small,v1%7CNew+York+Medium,v1%7CNew+York+Large,v1`; + } + + return `${BASE}?families=${fonts}&display=swap`; +} diff --git a/shared/localization/node_modules/make-plural/cardinals.mjs b/shared/localization/node_modules/make-plural/cardinals.mjs new file mode 100644 index 0000000..1484b3a --- /dev/null +++ b/shared/localization/node_modules/make-plural/cardinals.mjs @@ -0,0 +1,458 @@ +function a(n) { + return n == 1 ? 'one' : 'other'; +} +function b(n) { + return (n == 0 || n == 1) ? 'one' : 'other'; +} +function c(n) { + return n >= 0 && n <= 1 ? 'one' : 'other'; +} +function d(n) { + var s = String(n).split('.'), v0 = !s[1]; + return n == 1 && v0 ? 'one' : 'other'; +} +function e(n) { + return 'other'; +} +function f(n) { + return n == 1 ? 'one' + : n == 2 ? 'two' + : 'other'; +} + +export const _in = e; +export const af = a; +export const ak = b; +export const am = c; +export const an = a; +export function ar(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n, n100 = t0 && s[0].slice(-2); + return n == 0 ? 'zero' + : n == 1 ? 'one' + : n == 2 ? 'two' + : (n100 >= 3 && n100 <= 10) ? 'few' + : (n100 >= 11 && n100 <= 99) ? 'many' + : 'other'; +} +export function ars(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n, n100 = t0 && s[0].slice(-2); + return n == 0 ? 'zero' + : n == 1 ? 'one' + : n == 2 ? 'two' + : (n100 >= 3 && n100 <= 10) ? 'few' + : (n100 >= 11 && n100 <= 99) ? 'many' + : 'other'; +} +export const as = c; +export const asa = a; +export const ast = d; +export const az = a; +export function be(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2); + return n10 == 1 && n100 != 11 ? 'one' + : (n10 >= 2 && n10 <= 4) && (n100 < 12 || n100 > 14) ? 'few' + : t0 && n10 == 0 || (n10 >= 5 && n10 <= 9) || (n100 >= 11 && n100 <= 14) ? 'many' + : 'other'; +} +export const bem = a; +export const bez = a; +export const bg = a; +export const bho = b; +export const bm = e; +export const bn = c; +export const bo = e; +export function br(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2), n1000000 = t0 && s[0].slice(-6); + return n10 == 1 && n100 != 11 && n100 != 71 && n100 != 91 ? 'one' + : n10 == 2 && n100 != 12 && n100 != 72 && n100 != 92 ? 'two' + : ((n10 == 3 || n10 == 4) || n10 == 9) && (n100 < 10 || n100 > 19) && (n100 < 70 || n100 > 79) && (n100 < 90 || n100 > 99) ? 'few' + : n != 0 && t0 && n1000000 == 0 ? 'many' + : 'other'; +} +export const brx = a; +export function bs(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2), f10 = f.slice(-1), f100 = f.slice(-2); + return v0 && i10 == 1 && i100 != 11 || f10 == 1 && f100 != 11 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) || (f10 >= 2 && f10 <= 4) && (f100 < 12 || f100 > 14) ? 'few' + : 'other'; +} +export const ca = d; +export const ce = a; +export function ceb(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), f10 = f.slice(-1); + return v0 && (i == 1 || i == 2 || i == 3) || v0 && i10 != 4 && i10 != 6 && i10 != 9 || !v0 && f10 != 4 && f10 != 6 && f10 != 9 ? 'one' : 'other'; +} +export const cgg = a; +export const chr = a; +export const ckb = a; +export function cs(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1]; + return n == 1 && v0 ? 'one' + : (i >= 2 && i <= 4) && v0 ? 'few' + : !v0 ? 'many' + : 'other'; +} +export function cy(n) { + return n == 0 ? 'zero' + : n == 1 ? 'one' + : n == 2 ? 'two' + : n == 3 ? 'few' + : n == 6 ? 'many' + : 'other'; +} +export function da(n) { + var s = String(n).split('.'), i = s[0], t0 = Number(s[0]) == n; + return n == 1 || !t0 && (i == 0 || i == 1) ? 'one' : 'other'; +} +export const de = d; +export function dsb(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i100 = i.slice(-2), f100 = f.slice(-2); + return v0 && i100 == 1 || f100 == 1 ? 'one' + : v0 && i100 == 2 || f100 == 2 ? 'two' + : v0 && (i100 == 3 || i100 == 4) || (f100 == 3 || f100 == 4) ? 'few' + : 'other'; +} +export const dv = a; +export const dz = e; +export const ee = a; +export const el = a; +export const en = d; +export const eo = a; +export const es = a; +export const et = d; +export const eu = a; +export const fa = c; +export function ff(n) { + return n >= 0 && n < 2 ? 'one' : 'other'; +} +export const fi = d; +export function fil(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), f10 = f.slice(-1); + return v0 && (i == 1 || i == 2 || i == 3) || v0 && i10 != 4 && i10 != 6 && i10 != 9 || !v0 && f10 != 4 && f10 != 6 && f10 != 9 ? 'one' : 'other'; +} +export const fo = a; +export function fr(n) { + return n >= 0 && n < 2 ? 'one' : 'other'; +} +export const fur = a; +export const fy = d; +export function ga(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n; + return n == 1 ? 'one' + : n == 2 ? 'two' + : (t0 && n >= 3 && n <= 6) ? 'few' + : (t0 && n >= 7 && n <= 10) ? 'many' + : 'other'; +} +export function gd(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n; + return (n == 1 || n == 11) ? 'one' + : (n == 2 || n == 12) ? 'two' + : ((t0 && n >= 3 && n <= 10) || (t0 && n >= 13 && n <= 19)) ? 'few' + : 'other'; +} +export const gl = d; +export const gsw = a; +export const gu = c; +export const guw = b; +export function gv(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2); + return v0 && i10 == 1 ? 'one' + : v0 && i10 == 2 ? 'two' + : v0 && (i100 == 0 || i100 == 20 || i100 == 40 || i100 == 60 || i100 == 80) ? 'few' + : !v0 ? 'many' + : 'other'; +} +export const ha = a; +export const haw = a; +export function he(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1); + return n == 1 && v0 ? 'one' + : i == 2 && v0 ? 'two' + : v0 && (n < 0 || n > 10) && t0 && n10 == 0 ? 'many' + : 'other'; +} +export const hi = c; +export function hr(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2), f10 = f.slice(-1), f100 = f.slice(-2); + return v0 && i10 == 1 && i100 != 11 || f10 == 1 && f100 != 11 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) || (f10 >= 2 && f10 <= 4) && (f100 < 12 || f100 > 14) ? 'few' + : 'other'; +} +export function hsb(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i100 = i.slice(-2), f100 = f.slice(-2); + return v0 && i100 == 1 || f100 == 1 ? 'one' + : v0 && i100 == 2 || f100 == 2 ? 'two' + : v0 && (i100 == 3 || i100 == 4) || (f100 == 3 || f100 == 4) ? 'few' + : 'other'; +} +export const hu = a; +export function hy(n) { + return n >= 0 && n < 2 ? 'one' : 'other'; +} +export const ia = d; +export const id = e; +export const ig = e; +export const ii = e; +export const io = d; +export function is(n) { + var s = String(n).split('.'), i = s[0], t0 = Number(s[0]) == n, i10 = i.slice(-1), i100 = i.slice(-2); + return t0 && i10 == 1 && i100 != 11 || !t0 ? 'one' : 'other'; +} +export const it = d; +export const iu = f; +export function iw(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1); + return n == 1 && v0 ? 'one' + : i == 2 && v0 ? 'two' + : v0 && (n < 0 || n > 10) && t0 && n10 == 0 ? 'many' + : 'other'; +} +export const ja = e; +export const jbo = e; +export const jgo = a; +export const ji = d; +export const jmc = a; +export const jv = e; +export const jw = e; +export const ka = a; +export function kab(n) { + return n >= 0 && n < 2 ? 'one' : 'other'; +} +export const kaj = a; +export const kcg = a; +export const kde = e; +export const kea = e; +export const kk = a; +export const kkj = a; +export const kl = a; +export const km = e; +export const kn = c; +export const ko = e; +export const ks = a; +export const ksb = a; +export function ksh(n) { + return n == 0 ? 'zero' + : n == 1 ? 'one' + : 'other'; +} +export const ku = a; +export function kw(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n, n100 = t0 && s[0].slice(-2), n1000 = t0 && s[0].slice(-3), n100000 = t0 && s[0].slice(-5), n1000000 = t0 && s[0].slice(-6); + return n == 0 ? 'zero' + : n == 1 ? 'one' + : (n100 == 2 || n100 == 22 || n100 == 42 || n100 == 62 || n100 == 82) || t0 && n1000 == 0 && ((n100000 >= 1000 && n100000 <= 20000) || n100000 == 40000 || n100000 == 60000 || n100000 == 80000) || n != 0 && n1000000 == 100000 ? 'two' + : (n100 == 3 || n100 == 23 || n100 == 43 || n100 == 63 || n100 == 83) ? 'few' + : n != 1 && (n100 == 1 || n100 == 21 || n100 == 41 || n100 == 61 || n100 == 81) ? 'many' + : 'other'; +} +export const ky = a; +export function lag(n) { + var s = String(n).split('.'), i = s[0]; + return n == 0 ? 'zero' + : (i == 0 || i == 1) && n != 0 ? 'one' + : 'other'; +} +export const lb = a; +export const lg = a; +export const lkt = e; +export const ln = b; +export const lo = e; +export function lt(n) { + var s = String(n).split('.'), f = s[1] || '', t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2); + return n10 == 1 && (n100 < 11 || n100 > 19) ? 'one' + : (n10 >= 2 && n10 <= 9) && (n100 < 11 || n100 > 19) ? 'few' + : f != 0 ? 'many' + : 'other'; +} +export function lv(n) { + var s = String(n).split('.'), f = s[1] || '', v = f.length, t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2), f100 = f.slice(-2), f10 = f.slice(-1); + return t0 && n10 == 0 || (n100 >= 11 && n100 <= 19) || v == 2 && (f100 >= 11 && f100 <= 19) ? 'zero' + : n10 == 1 && n100 != 11 || v == 2 && f10 == 1 && f100 != 11 || v != 2 && f10 == 1 ? 'one' + : 'other'; +} +export const mas = a; +export const mg = b; +export const mgo = a; +export function mk(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2), f10 = f.slice(-1), f100 = f.slice(-2); + return v0 && i10 == 1 && i100 != 11 || f10 == 1 && f100 != 11 ? 'one' : 'other'; +} +export const ml = a; +export const mn = a; +export function mo(n) { + var s = String(n).split('.'), v0 = !s[1], t0 = Number(s[0]) == n, n100 = t0 && s[0].slice(-2); + return n == 1 && v0 ? 'one' + : !v0 || n == 0 || (n100 >= 2 && n100 <= 19) ? 'few' + : 'other'; +} +export const mr = a; +export const ms = e; +export function mt(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n, n100 = t0 && s[0].slice(-2); + return n == 1 ? 'one' + : n == 0 || (n100 >= 2 && n100 <= 10) ? 'few' + : (n100 >= 11 && n100 <= 19) ? 'many' + : 'other'; +} +export const my = e; +export const nah = a; +export const naq = f; +export const nb = a; +export const nd = a; +export const ne = a; +export const nl = d; +export const nn = a; +export const nnh = a; +export const no = a; +export const nqo = e; +export const nr = a; +export const nso = b; +export const ny = a; +export const nyn = a; +export const om = a; +export const or = a; +export const os = a; +export const osa = e; +export const pa = b; +export const pap = a; +export function pl(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2); + return n == 1 && v0 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) ? 'few' + : v0 && i != 1 && (i10 == 0 || i10 == 1) || v0 && (i10 >= 5 && i10 <= 9) || v0 && (i100 >= 12 && i100 <= 14) ? 'many' + : 'other'; +} +export function prg(n) { + var s = String(n).split('.'), f = s[1] || '', v = f.length, t0 = Number(s[0]) == n, n10 = t0 && s[0].slice(-1), n100 = t0 && s[0].slice(-2), f100 = f.slice(-2), f10 = f.slice(-1); + return t0 && n10 == 0 || (n100 >= 11 && n100 <= 19) || v == 2 && (f100 >= 11 && f100 <= 19) ? 'zero' + : n10 == 1 && n100 != 11 || v == 2 && f10 == 1 && f100 != 11 || v != 2 && f10 == 1 ? 'one' + : 'other'; +} +export const ps = a; +export function pt(n) { + var s = String(n).split('.'), i = s[0]; + return (i == 0 || i == 1) ? 'one' : 'other'; +} +export const pt_PT = d; +export const rm = a; +export function ro(n) { + var s = String(n).split('.'), v0 = !s[1], t0 = Number(s[0]) == n, n100 = t0 && s[0].slice(-2); + return n == 1 && v0 ? 'one' + : !v0 || n == 0 || (n100 >= 2 && n100 <= 19) ? 'few' + : 'other'; +} +export const rof = a; +export const root = e; +export function ru(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2); + return v0 && i10 == 1 && i100 != 11 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) ? 'few' + : v0 && i10 == 0 || v0 && (i10 >= 5 && i10 <= 9) || v0 && (i100 >= 11 && i100 <= 14) ? 'many' + : 'other'; +} +export const rwk = a; +export const sah = e; +export const saq = a; +export const sc = d; +export const scn = d; +export const sd = a; +export const sdh = a; +export const se = f; +export const seh = a; +export const ses = e; +export const sg = e; +export function sh(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2), f10 = f.slice(-1), f100 = f.slice(-2); + return v0 && i10 == 1 && i100 != 11 || f10 == 1 && f100 != 11 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) || (f10 >= 2 && f10 <= 4) && (f100 < 12 || f100 > 14) ? 'few' + : 'other'; +} +export function shi(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n; + return n >= 0 && n <= 1 ? 'one' + : (t0 && n >= 2 && n <= 10) ? 'few' + : 'other'; +} +export function si(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || ''; + return (n == 0 || n == 1) || i == 0 && f == 1 ? 'one' : 'other'; +} +export function sk(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1]; + return n == 1 && v0 ? 'one' + : (i >= 2 && i <= 4) && v0 ? 'few' + : !v0 ? 'many' + : 'other'; +} +export function sl(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], i100 = i.slice(-2); + return v0 && i100 == 1 ? 'one' + : v0 && i100 == 2 ? 'two' + : v0 && (i100 == 3 || i100 == 4) || !v0 ? 'few' + : 'other'; +} +export const sma = f; +export const smi = f; +export const smj = f; +export const smn = f; +export const sms = f; +export const sn = a; +export const so = a; +export const sq = a; +export function sr(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2), f10 = f.slice(-1), f100 = f.slice(-2); + return v0 && i10 == 1 && i100 != 11 || f10 == 1 && f100 != 11 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) || (f10 >= 2 && f10 <= 4) && (f100 < 12 || f100 > 14) ? 'few' + : 'other'; +} +export const ss = a; +export const ssy = a; +export const st = a; +export const su = e; +export const sv = d; +export const sw = d; +export const syr = a; +export const ta = a; +export const te = a; +export const teo = a; +export const th = e; +export const ti = b; +export const tig = a; +export const tk = a; +export function tl(n) { + var s = String(n).split('.'), i = s[0], f = s[1] || '', v0 = !s[1], i10 = i.slice(-1), f10 = f.slice(-1); + return v0 && (i == 1 || i == 2 || i == 3) || v0 && i10 != 4 && i10 != 6 && i10 != 9 || !v0 && f10 != 4 && f10 != 6 && f10 != 9 ? 'one' : 'other'; +} +export const tn = a; +export const to = e; +export const tr = a; +export const ts = a; +export function tzm(n) { + var s = String(n).split('.'), t0 = Number(s[0]) == n; + return (n == 0 || n == 1) || (t0 && n >= 11 && n <= 99) ? 'one' : 'other'; +} +export const ug = a; +export function uk(n) { + var s = String(n).split('.'), i = s[0], v0 = !s[1], i10 = i.slice(-1), i100 = i.slice(-2); + return v0 && i10 == 1 && i100 != 11 ? 'one' + : v0 && (i10 >= 2 && i10 <= 4) && (i100 < 12 || i100 > 14) ? 'few' + : v0 && i10 == 0 || v0 && (i10 >= 5 && i10 <= 9) || v0 && (i100 >= 11 && i100 <= 14) ? 'many' + : 'other'; +} +export const ur = d; +export const uz = a; +export const ve = a; +export const vi = e; +export const vo = a; +export const vun = a; +export const wa = b; +export const wae = a; +export const wo = e; +export const xh = a; +export const xog = a; +export const yi = d; +export const yo = e; +export const yue = e; +export const zh = e; +export const zu = c; diff --git a/shared/localization/src/getLocAttributes.ts b/shared/localization/src/getLocAttributes.ts new file mode 100644 index 0000000..2f462db --- /dev/null +++ b/shared/localization/src/getLocAttributes.ts @@ -0,0 +1,78 @@ +import { getPageDir } from './getPageDir'; + +/** + * Checks if a string contains language script + * ex. "zh-Hant-HK", "zh-Hant-TW", "zh-Hans-CN" + * @param {string} locale + * @returns {boolean} + */ +const hasSupportedLanguageScript = (locale: string): boolean => { + const SUPPORTED_SCRIPTS = ['-hans-', '-hant-']; + + const formattedLocale = locale.toLowerCase(); + return SUPPORTED_SCRIPTS.some((item) => formattedLocale.includes(item)); +}; + +/** + * + * BCP47 https://www.w3.org/International/articles/language-tags/ + * + * @param {string} language https://en.wikipedia.org/wiki/ISO_639 + * @param {string} region https://en.wikipedia.org/wiki/ISO_3166-1 + * @param {string} script https://en.wikipedia.org/wiki/ISO_15924 + + */ +const buildBcp47String = ( + language: string, + region: string, + script?: string, +): string => { + let capitalizeScript: string | null = null; + if (script) { + capitalizeScript = + script[0].toUpperCase() + script.substring(1).toLowerCase(); + } + let bcp47Arr = [ + language.toLowerCase(), + capitalizeScript, + region.toUpperCase(), + ]; + + return bcp47Arr.filter((item) => item !== null).join('-'); +}; + +/** + * @description + * get values to be used in <html> tag lang and dir attributes. + * + * @param {string} locale + * @returns { { dir: 'rtl' | 'ltr', lang: string }} HTML dir + lang values + */ + +export function getLocAttributes(locale: string): { + dir: 'rtl' | 'ltr'; + lang: string; +} { + const pageDir = getPageDir(locale); + let bcp47 = locale; + + const localeStrings = locale.split('-'); + + // region index in array + const regionIndex = hasSupportedLanguageScript(locale) ? 2 : 1; + + const language = localeStrings[0]; + const script = hasSupportedLanguageScript(locale) + ? localeStrings[1] + : undefined; + const region = localeStrings[regionIndex]; + + if (language && region) { + bcp47 = buildBcp47String(language, region, script); + } + + return { + dir: pageDir, + lang: bcp47, + }; +} diff --git a/shared/localization/src/getPageDir.ts b/shared/localization/src/getPageDir.ts new file mode 100644 index 0000000..47b855d --- /dev/null +++ b/shared/localization/src/getPageDir.ts @@ -0,0 +1,40 @@ +/** + * TODO: rdar://73010072 (Make localization utils its own package) + * Copied from: + * https://github.pie.apple.com/amp-ui/desktop-music-app/blob/main/app/utils/page-dir.js + */ + +// these overrides were determined to always show page in RTL, even if the global elements dont contain +// an he_il entry +// <rdar://problem/49297213> LOC: IW-IL: RTL: Web Preview Pages: The Preview Pages are not RTL. +const RTL_LANG_CODES_OVERRIDE = [ + 'he', // hebrew +]; + +const RTL_LANG_CODES = [ + 'ar', // arabic + 'he', // hebrew + 'ku', // kurdish + 'ur', // urdu + 'ps', // pashto + 'yi', // yiddish +]; + +/** + * Determine the page-direction for a given locale + * + * @param {String} localeCode - A string containing a language code and region code separated by a hyphen. + * @param {String|undefined|null} langParam - A language code passed from the `l=` query param. + */ +export function getPageDir( + localeCode: string, + langParam: string | undefined | null = null, +) { + const twoLettersLangCode = localeCode.split('-')[0]; + const isRTLLang = RTL_LANG_CODES.includes(twoLettersLangCode); + const isRTLLangOverride = + typeof langParam === 'string' && + RTL_LANG_CODES_OVERRIDE.includes(langParam); + + return isRTLLang || isRTLLangOverride ? 'rtl' : 'ltr'; +} diff --git a/shared/localization/src/i18n.ts b/shared/localization/src/i18n.ts new file mode 100644 index 0000000..bcd5e28 --- /dev/null +++ b/shared/localization/src/i18n.ts @@ -0,0 +1,104 @@ +import Translator from './translator'; +import type { + Locale, + InterpolationOptions, + ILocaleJSON, + ITranslator, +} from './types'; +import type { Logger } from '@amp/web-apps-logger'; + +/** @internal */ +const formatOptions = ( + options: InterpolationOptions | number, +): InterpolationOptions => + typeof options === 'number' ? { count: options } : options; + +/** + * + * Adapter class to expose expected LOC interface + * @category Localization + */ +export class I18N { + private readonly log: Logger; + private readonly locale: Locale; + private readonly translator: ITranslator; + private readonly keys: ILocaleJSON; + private readonly alwaysShowScreamers: boolean; + + /** + * builds a new I18N class + * @param locale - the locale to use default:`'en-us'` + * @param translation - translation object default: `{}` + * @param alwaysShowScreamers - optional boolean that is set upstream + * by a FeatureKit feature flag. This makes it so the LOC keys themselves are + * printed to the DOM, rather than their translations, which is helpful for QA testing + */ + constructor( + log: Logger, + locale: Locale = 'en-us', + translation: ILocaleJSON = {}, + alwaysShowScreamers: boolean = false, + ) { + this.log = log; + this.locale = locale; + this.translator = new Translator(locale, translation, { + onMissingKeyFn: (key: string): string => { + log.warn('key missing:', key); + return `**${key}**`; + }, + onMissingInterpolationFn: (key: string, interpolation: string) => { + log.warn(`key ${key} missing interpolation:`, interpolation); + }, + }); + this.keys = translation; + this.alwaysShowScreamers = alwaysShowScreamers; + } + + get currentLocale(): Locale { + return this.locale; + } + + get currentKeys(): ILocaleJSON { + return this.keys; + } + + /** + * Gets non-interpolated string. + * @category Localization + * @param key key to lookup in the translation.json + * @returns an uninterpolated string value + */ + getUninterpolatedString(key: string): string { + if (this.alwaysShowScreamers) { + return key; + } else { + return this.translator.getUninterpolatedString(key); + } + } + + /** + * Method for fetching translation based on key. + * + * If alwaysShowScreamers is true, return the key itself for QA testing purposes + * (our app tends to call into this method within Svelte templates) + * + * @category Localization + * @param key key to lookup in the translation.json + * @param options options for translations + * @returns interpolated string + */ + t = (key: string, options: number | InterpolationOptions = {}): string => { + if (this.alwaysShowScreamers) { + return key; + } + + let internalOptions: InterpolationOptions = formatOptions(options); + if (typeof key !== 'string') { + this.log.warn('received non-string key:', key); + return ''; + } + return this.translator.translate(key, internalOptions); + }; +} + +export default I18N; diff --git a/shared/localization/src/setHTMLAttributes.ts b/shared/localization/src/setHTMLAttributes.ts new file mode 100644 index 0000000..3bc0725 --- /dev/null +++ b/shared/localization/src/setHTMLAttributes.ts @@ -0,0 +1,15 @@ +import { getLocAttributes } from './getLocAttributes'; + +/** + * sets Language attributes to HTML tag. + * @param {string} language + * @returns {void} + */ +export function setHTMLAttributes(language: string): void { + if (typeof window === 'undefined') return; + const attributes = getLocAttributes(language); + + for (let [attribute, value] of Object.entries(attributes)) { + window.document.documentElement.setAttribute(attribute, value); + } +} diff --git a/shared/localization/src/translator.ts b/shared/localization/src/translator.ts new file mode 100644 index 0000000..48b901f --- /dev/null +++ b/shared/localization/src/translator.ts @@ -0,0 +1,174 @@ +//TODO: rdar://73157363 (Limit loc plural functions to only use supported locales) +import * as cardinals from 'make-plural/cardinals'; +import type { + Locale, + ILocaleJSON, + InterpolationOptions, + TranslatorOptions, + ImissingInterpolationFn, + ImissingKeyFn, + ITranslator, +} from './types'; + +const DEFAULT_MISSING_FN: ImissingKeyFn = (key: string): string => `**${key}**`; +const DEFAULT_INTERPOLATION_REGEX: RegExp = /@@(.*?)@@/g; + +/** + * Interpolates string and returns result. + * @category Localization + * @param phrase phrase to be interpolated ex. ```"hello my name is @@name@@" ``` + * @param options object containing values to subsitute ex. ``` { name: "Joe" } ``` + * @param onMissingInterpolationFn callback to be called if options object does not contain a value for the interpolation schema + * + * @returns translated string ex ``` "hello my name is Joe" ``` + */ +export function interpolateString( + key: string, + phrase: string, + options: InterpolationOptions, + onMissingInterpolationFn: ImissingInterpolationFn | null, + locale: Locale, +): string { + const result = phrase.replace( + DEFAULT_INTERPOLATION_REGEX, + function (expression: string, argument: string) { + const optionHasProperty = options.hasOwnProperty(argument); + const optionType = typeof options[argument]; + const argumentIsUndefined = optionType === 'undefined'; + const argumentIsValid = + optionType === 'string' || optionType === 'number'; + let value: string = expression; + if (optionHasProperty && argumentIsValid) { + let validValue: string | number = options[argument]; + if ( + optionType === 'number' && + options.hasOwnProperty('count') + ) { + validValue = (validValue as number).toLocaleString([ + locale, + 'en-US', + ]); + } + value = validValue as string; + } else if (onMissingInterpolationFn && argumentIsUndefined) { + onMissingInterpolationFn(key, value); + } + return value; + }, + ); + + return result; +} + +type Cardinal = (n: number | string) => cardinals.PluralCategory; + +function getCardinal(selectedLang: string): Cardinal | undefined { + // @ts-ignore-error TypeScript does not allow us to index into a namespace dynamically + return cardinals[selectedLang]; +} + +/** + * TODO: rdar://73157363 (Limit loc plural functions to only use supported locales) + * Used to select the locale specific cardinal plural form key. + * @category Localization + * @param count number to determine the cardinal value + * @param key base key + * @param locale to lookup plural + * + * Reference: + * https://confluence.sd.apple.com/pages/viewpage.action?spaceKey=ASL&title=Pluralization+Rules + * + * @returns key + correct plural ex. ```[key].[ 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'] ``` + */ + +export const getPlural = ( + count: number, + key: string, + locale: Locale, +): string => { + const lang = locale.split('-')[0]; + + // use english plural for dev strings + const selectedLang = lang === 'dev' ? 'en' : lang; + const cardinal = getCardinal(selectedLang); + + let plural: cardinals.PluralCategory | null = null; + if (cardinal) { + plural = cardinal(count); + // TODO: rdar://93665757 (JMOTW: investigate where to use 'few' and 'many' loc keys) + if (plural === 'few' || plural === 'many') plural = 'other'; + } + return plural ? `${key}.${plural}` : key; +}; + +/** + * Class that manages translations, plural rules, + * and interpolation for a single locale. + * @category Localization + */ +class Translator implements ITranslator { + private translationMap: Map<string, string>; + private locale: Locale; + private onMissingKeyFn: ImissingKeyFn; + private onMissingInterpolationFn: ImissingInterpolationFn | null; + constructor( + locale: Locale, + phrases: ILocaleJSON, + options: TranslatorOptions = {}, + ) { + const { + onMissingKeyFn = DEFAULT_MISSING_FN, + onMissingInterpolationFn = null, + } = options; + this.locale = locale; + this.translationMap = new Map(Object.entries(phrases)); + this.onMissingKeyFn = onMissingKeyFn; + this.onMissingInterpolationFn = onMissingInterpolationFn; + } + + /** + * Gets the correct value from the translation map. + * @category Localization + * @param key used to look up the value + */ + private getValue(key: string): string | null { + return this.translationMap.get(key) || null; + } + /** + * Gets an uniterpolated value of key. + * @category Localization + * @param key used to look up the value + */ + getUninterpolatedString(key: string) { + const keyValue = this.getValue(key); + return keyValue ? keyValue : this.onMissingKeyFn(key); + } + /** + * Translate string based on translation map, plural rules interpolates values. + * @category Localization + * @param key used to look up the value + * @param options used for interpolation + * @returns translated string + */ + translate(key: string, options: InterpolationOptions = {}): string { + let internalKey = key; + const { count } = options; + + if (count && !isNaN(count)) { + internalKey = getPlural(count, key, this.locale); + } + + const keyValue = this.getValue(internalKey); + return keyValue + ? interpolateString( + internalKey, + keyValue, + options, + this.onMissingInterpolationFn, + this.locale, + ) + : this.onMissingKeyFn(internalKey); + } +} + +export default Translator; diff --git a/shared/logger/node_modules/@amp-metrics/sentrykit/dist/index.mjs b/shared/logger/node_modules/@amp-metrics/sentrykit/dist/index.mjs new file mode 100644 index 0000000..5755983 --- /dev/null +++ b/shared/logger/node_modules/@amp-metrics/sentrykit/dist/index.mjs @@ -0,0 +1,815 @@ +var __defProp = Object.defineProperty; +var __defProps = Object.defineProperties; +var __getOwnPropDescs = Object.getOwnPropertyDescriptors; +var __getOwnPropSymbols = Object.getOwnPropertySymbols; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __propIsEnum = Object.prototype.propertyIsEnumerable; +var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; +var __spreadValues = (a, b) => { + for (var prop in b || (b = {})) + if (__hasOwnProp.call(b, prop)) + __defNormalProp(a, prop, b[prop]); + if (__getOwnPropSymbols) + for (var prop of __getOwnPropSymbols(b)) { + if (__propIsEnum.call(b, prop)) + __defNormalProp(a, prop, b[prop]); + } + return a; +}; +var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); + +// src/config.ts +import { SDK_VERSION as SDK_VERSION2 } from "@sentry/browser"; + +// src/transports/fetch.ts +import { rejectedSyncPromise } from "@sentry/utils"; + +// src/transports/base.ts +import { + createEnvelope, + envelopeItemTypeToDataCategory, + forEachEnvelopeItem, + isRateLimited, + logger, + makePromiseBuffer, + resolvedSyncPromise, + SentryError, + serializeEnvelope, + updateRateLimits +} from "@sentry/utils"; +var DEFAULT_TRANSPORT_BUFFER_SIZE = 30; +function createTransport(options, makeRequest, buffer = makePromiseBuffer( + options.bufferSize || DEFAULT_TRANSPORT_BUFFER_SIZE +)) { + let rateLimits = {}; + const flush = (timeout) => buffer.drain(timeout); + function send(envelope) { + const filteredEnvelopeItems = []; + forEachEnvelopeItem(envelope, (item, type) => { + const envelopeItemDataCategory = envelopeItemTypeToDataCategory(type); + if (isRateLimited(rateLimits, envelopeItemDataCategory)) { + const event = getEventForEnvelopeItem(item, type); + options.recordDroppedEvent("ratelimit_backoff", envelopeItemDataCategory, event); + } else { + filteredEnvelopeItems.push(item); + } + }); + if (filteredEnvelopeItems.length === 0) { + return resolvedSyncPromise(); + } + const filteredEnvelope = createEnvelope(envelope[0], filteredEnvelopeItems); + const recordEnvelopeLoss = (reason) => { + forEachEnvelopeItem(filteredEnvelope, (item, type) => { + const event = getEventForEnvelopeItem(item, type); + options.recordDroppedEvent(reason, envelopeItemTypeToDataCategory(type), event); + }); + }; + const getRequest = options.getRequest || ((envelope2) => ({ + body: serializeEnvelope(envelope2, options.textEncoder) + })); + const request = getRequest(filteredEnvelope); + if (!request) { + return resolvedSyncPromise(); + } + const requestTask = () => makeRequest(request).then( + (response) => { + if (response.statusCode !== void 0 && (response.statusCode < 200 || response.statusCode >= 300)) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && logger.warn(`Sentry responded with status code ${response.statusCode} to sent event.`); + } + rateLimits = updateRateLimits(rateLimits, response); + return response; + }, + (error) => { + recordEnvelopeLoss("network_error"); + throw error; + } + ); + return buffer.add(requestTask).then( + (result) => result, + (error) => { + if (error instanceof SentryError) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && logger.error("Skipped sending event because buffer is full."); + recordEnvelopeLoss("queue_overflow"); + return resolvedSyncPromise(); + } else { + throw error; + } + } + ); + } + send.__sentry__baseTransport__ = true; + return { + send, + flush + }; +} +function getEventForEnvelopeItem(item, type) { + if (type !== "event" && type !== "transaction") { + return void 0; + } + return Array.isArray(item) ? item[1] : void 0; +} + +// src/transports/utils.ts +import { isNativeFetch, logger as logger2 } from "@sentry/utils"; +import { GLOBAL_OBJ } from "@sentry/utils"; +var WINDOW = GLOBAL_OBJ; +var cachedFetchImpl = void 0; +function getNativeFetchImplementation() { + if (cachedFetchImpl) { + return cachedFetchImpl; + } + if (isNativeFetch(WINDOW.fetch)) { + return cachedFetchImpl = WINDOW.fetch.bind(WINDOW); + } + const document = WINDOW.document; + let fetchImpl = WINDOW.fetch; + if (document && typeof document.createElement === "function") { + try { + const sandbox = document.createElement("iframe"); + sandbox.hidden = true; + document.head.appendChild(sandbox); + const contentWindow = sandbox.contentWindow; + if (contentWindow && contentWindow.fetch) { + fetchImpl = contentWindow.fetch; + } + document.head.removeChild(sandbox); + } catch (e) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && logger2.warn("Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ", e); + } + } + return cachedFetchImpl = fetchImpl.bind(WINDOW); +} +function clearCachedFetchImplementation() { + cachedFetchImpl = void 0; +} + +// src/transports/fetch.ts +function makeFetchTransport(options, nativeFetch = getNativeFetchImplementation()) { + let pendingBodySize = 0; + let pendingCount = 0; + function makeRequest(request) { + const requestSize = request.body.length; + pendingBodySize += requestSize; + pendingCount++; + const requestOptions = __spreadValues({ + body: request.body, + method: "POST", + referrerPolicy: "origin", + headers: request.headers, + keepalive: pendingBodySize <= 6e4 && pendingCount < 15 + }, options.fetchOptions); + try { + return nativeFetch(request.url, requestOptions).then((response) => { + pendingBodySize -= requestSize; + pendingCount--; + return { + statusCode: response.status, + headers: { + "x-sentry-rate-limits": response.headers.get("X-Sentry-Rate-Limits"), + "retry-after": response.headers.get("Retry-After") + } + }; + }); + } catch (e) { + clearCachedFetchImplementation(); + pendingBodySize -= requestSize; + pendingCount--; + return rejectedSyncPromise(e); + } + } + return createTransport(options, makeRequest); +} + +// src/transports/xhr.ts +import { SyncPromise } from "@sentry/utils"; +var XHR_READYSTATE_DONE = 4; +function makeXHRTransport(options) { + function makeRequest(request) { + return new SyncPromise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.onerror = reject; + xhr.onreadystatechange = () => { + if (xhr.readyState === XHR_READYSTATE_DONE) { + resolve({ + statusCode: xhr.status, + headers: { + "x-sentry-rate-limits": xhr.getResponseHeader("X-Sentry-Rate-Limits"), + "retry-after": xhr.getResponseHeader("Retry-After") + } + }); + } + }; + xhr.open("POST", request.url); + for (const header in request.headers) { + if (Object.prototype.hasOwnProperty.call(request.headers, header)) { + xhr.setRequestHeader(header, request.headers[header]); + } + } + xhr.send(request.body); + }); + } + return createTransport(options, makeRequest); +} + +// src/transport.ts +import { supportsFetch } from "@sentry/utils"; + +// src/ingestion-event.ts +function envelopeToIngestionEvents(envelope, options) { + const [_envelopeHeader, items] = envelope; + delete _envelopeHeader.dsn; + const sharedFields = { + _envelopeHeader, + project: options.project, + v: 4 + }; + return items.map((item) => { + const itemType = item[0].type; + if (itemType !== "event" && itemType !== "transaction") { + return __spreadProps(__spreadValues({}, sharedFields), { + _itemHeader: item[0], + _debugLogs: [ + `Items of type "${itemType}" are not supported yet. Dropped the item.` + ] + }); + } + const [_itemHeader, payload] = item; + return __spreadValues(__spreadProps(__spreadValues({}, sharedFields), { + _itemHeader + }), payload); + }); +} + +// src/utils.ts +import { getCurrentHub, SDK_VERSION } from "@sentry/browser"; +function isHeadlessBrowser() { + return navigator.userAgent === void 0 || navigator.appVersion === void 0 || navigator.plugins === void 0 || navigator.languages === void 0 || navigator.languages.length === 0 || navigator.language === "" || navigator.webdriver || navigator.plugins.length === 0 || /HeadlessChrome/.test(navigator.userAgent) || /headless/i.test(navigator.appVersion); +} +function versionMismatchErrorMessage(sdkVersion) { + sdkVersion || (sdkVersion = "unknown"); + return `Version mismatch between the installed Sentry SDK version (${sdkVersion}) and supported SDK version (${SUPPORTED_SENTRY_VERSION}) by SentryKit. Make sure to use supported version of the Sentry SDK (${SUPPORTED_SENTRY_VERSION}).`; +} +function checkSentryKitIsCompatibleWith(sdkVersion) { + if (sdkVersion !== SUPPORTED_SENTRY_VERSION) { + console.error( + `[SentryKit Versioning Error] ${versionMismatchErrorMessage( + sdkVersion + )} All sent data will be discarded.` + ); + return false; + } + return true; +} +function checkSentrySDKCompatibility() { + return checkSentryKitIsCompatibleWith(SDK_VERSION); +} +function checkEnvelopeSentrySDKCompatibility(envelope) { + var _a; + const sdkVersion = (_a = envelope[0].sdk) == null ? void 0 : _a.version; + return checkSentryKitIsCompatibleWith(sdkVersion); +} +var correctSetupGuide = `The correct way to use SentryKit is as follows: + +import {createSentryConfig} from '@amp-metrics/sentrykit' + +Sentry.init(createSentryConfig({ + // all your configs +}))`; +function monitorSentryConfig(config) { + ; + config.__sentrykit_original_config = __spreadValues({}, config); + return config; +} +function monitorSentryHubBindClient() { + const hub = getCurrentHub(); + const originalBindClient = hub.bindClient; + hub.bindClient = (client) => { + assertCorrectSentryKitConfiguration(client.getOptions()); + originalBindClient.call(hub, client); + }; +} +function assertCorrectSentryKitConfiguration(config) { + var _a, _b; + const sdkVersion = (_b = (_a = config._metadata) == null ? void 0 : _a.sdk) == null ? void 0 : _b.version; + if (sdkVersion !== SUPPORTED_SENTRY_VERSION) { + throw new Error( + `[SentryKit Initialization Error] ${versionMismatchErrorMessage( + sdkVersion + )}` + ); + } + const originalConfig = config.__sentrykit_original_config; + if (!originalConfig) { + throw new Error( + `[SentryKit Initialization Error] Configuration has to be generated through \`createSentryConfig\` function. ${correctSetupGuide}` + ); + } + for (const key in originalConfig) { + if (key === "integrations") { + continue; + } + if (originalConfig[key] !== config[key]) { + throw new Error( + `[SentryKit Initialization Error] Configuration generated through \`createSentryConfig\` function has been changed. ${correctSetupGuide}` + ); + } + } +} + +// src/logger.ts +var originalLog = console.log; +var enabled = false; +var enableLogger = () => { + enabled = true; +}; +var createLogger = (prefix) => (...args) => { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && enabled && originalLog(prefix, ...args); +}; + +// src/privacy/rules.ts +var debug = /* @__PURE__ */ createLogger("[SentryKit Privacy rules]"); +function visit(obj, callback, parentContext) { + const parentPath = (parentContext == null ? void 0 : parentContext.path) ? parentContext.path + "." : ""; + const _visit = (key) => { + const path = parentPath + key; + const value = obj[key]; + const action = callback({ + obj, + key, + value, + path, + parentContext + }); + if (action === "remove") { + return action; + } + if (value !== null && typeof value === "object") { + visit(value, callback, { + obj, + key, + value, + path, + parentContext + }); + } + }; + if (Array.isArray(obj)) { + for (let i = 0; i < obj.length; i++) { + const action = _visit(i.toString()); + if (action === "remove") { + obj.splice(i, 1); + i--; + } + } + } else { + for (const key in obj) { + const action = _visit(key); + if (action === "remove") { + delete obj[key]; + } + } + } +} +var matchesPath = (rule, eventType, context) => { + if ((rule.type === "url" || rule.type === "url-query") && typeof context.obj[context.key] !== "string") { + return false; + } + if (rule.type === "timestamp" && typeof context.obj[context.key] !== "number") { + return false; + } + if (!rule.matchPath) { + if (rule.type === "url" || rule.type === "url-query") { + return isURLField(context); + } + if (rule.type === "timestamp") { + return isTimestampField(context); + } + return true; + } + return typeof rule.matchPath === "function" ? rule.matchPath({ + eventType, + path: context.path, + key: context.key, + value: context.value + }) : rule.matchPath.test(context.path); +}; +var matchesQuery = (rule, eventType, context, queryName, queryValue) => { + if (rule.type !== "url-query") { + return false; + } + if (!rule.matchQueryName) { + return true; + } + return typeof rule.matchQueryName === "function" ? rule.matchQueryName({ + eventType, + path: context.path, + key: context.key, + url: context.value, + queryName, + queryValue + }) : rule.matchQueryName.test(queryName); +}; +var KnownURLFields = [ + (context) => /^spans\.\d+\.data\.url$/.test(context.path) && context.parentContext.obj.op === "http.client" && (Object.defineProperty(context.obj, "_url", { + enumerable: false, + value: context.value + }) || true), + (context) => /^spans\.\d+\.description$/.test(context.path) && context.obj.op === "http.client" && context.obj.data && context.obj.description === `${context.obj.data.method} ${context.obj.data._url || context.obj.data.url}`, + (context) => /^breadcrumbs\.\d+\.data\.url$/.test(context.path) && (context.parentContext.obj.category === "fetch" || context.parentContext.obj.category === "xhr"), + (context) => /^breadcrumbs\.\d+\.data\.from$/.test(context.path) && context.parentContext.obj.category === "navigation", + (context) => /^breadcrumbs\.\d+\.data\.to$/.test(context.path) && context.parentContext.obj.category === "navigation", + (context) => /^request\.url$/.test(context.path), + (context) => /^request\.headers\.Referer$/.test(context.path) +]; +var isURLField = (context) => KnownURLFields.some((test) => test(context)); +var isTimestampField = (context) => { + return typeof context.value === "number" && context.path.toLowerCase().endsWith("timestamp"); +}; +var processForPrivacy = (event, rules) => { + const eventType = event._itemHeader.type === "transaction" ? "transaction" : "error"; + visit(event, (context) => { + const { path, key, obj } = context; + for (const [i, rule] of rules.entries()) { + if (!matchesPath(rule, eventType, context)) { + continue; + } + if ("action" in rule && rule.action === "keep") { + continue; + } + if (rule.type === "timestamp") { + const timestamp = obj[key]; + const timestampOrPrecision = typeof rule.precision === "function" ? rule.precision({ + eventType, + path, + key, + timestamp + }) : rule.precision; + if (typeof timestampOrPrecision === "number") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && timestamp !== timestampOrPrecision && debug( + `Setting timestamp to a new value of "${timestampOrPrecision}" for path: "${path}"` + ); + obj[key] = timestampOrPrecision; + } else if (timestampOrPrecision === "seconds") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug( + `Reducing timestamp to "${timestampOrPrecision}" for path: "${path}"` + ); + obj[key] = Math.round(timestamp); + } else if (timestampOrPrecision === "minutes") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug( + `Reducing timestamp to "${timestampOrPrecision}" for path: "${path}"` + ); + obj[key] = Math.round(timestamp / 60) * 60; + } + } else if (rule.type === "url-query") { + const url = obj[key]; + const [base, oldQuery = ""] = url.split("?"); + const queryParams = new URLSearchParams(oldQuery); + const entries = [...queryParams.entries()]; + entries.forEach(([queryName, queryValue]) => { + if (matchesQuery(rule, eventType, context, queryName, queryValue)) { + const hasActionAfterwards = rules.slice(i + 1).some( + (r) => matchesPath(r, eventType, context) && matchesQuery(r, eventType, context, queryName, queryValue) + ); + if (hasActionAfterwards) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug( + `Skipping the active rule as there is an overriding rule for the query "${queryName}" in the url "${url}" in path "${path}".` + ); + return; + } + if (rule.action === "remove") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug( + `Removing query "${queryName}" from the url "${url}" in the path: "${path}" (value was: "${queryValue}")` + ); + queryParams.delete(queryName); + } else if (rule.action === "replace") { + const newValue = typeof rule.replace === "function" ? rule.replace({ + key, + path, + url, + queryName, + queryValue, + eventType + }) : rule.replace; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && newValue !== queryValue && debug( + `Setting query "${queryName}" in the url "${url}" to a new value of "${newValue}" for path: "${path}" (value was: "${queryValue}")` + ); + queryParams.set(queryName, newValue); + } + } + }); + const query = queryParams.toString(); + obj[key] = base + (query ? `?${query}` : ""); + } else if (rule.type === "url") { + const hasActionAfterwards = rules.slice(i + 1).some((r) => matchesPath(r, eventType, context) && r.type === "url"); + if (hasActionAfterwards) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug( + `Skipping the active rule as there is an overriding rule for the url "${obj[key]}" in path "${path}".` + ); + continue; + } + if (rule.action === "remove") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(`Removing the url "${obj[key]}" in the path: "${path}"`); + return "remove"; + } else if (rule.action === "replace") { + const newValue = typeof rule.replace === "function" ? rule.replace({ + key, + path, + url: obj[key], + eventType + }) : rule.replace; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && newValue !== obj[key] && debug( + `Setting the url"${obj[key]}" to a new value of "${newValue}" for path: "${path}" (value was: "${obj[key]}")` + ); + obj[key] = newValue; + } + } else if (rule.type === "any" && "action" in rule) { + const hasActionAfterwards = rules.slice(i + 1).some((r) => matchesPath(r, eventType, context) && r.type === "any"); + if (hasActionAfterwards) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug( + `Skipping the active rule as there is an overriding rule for the path "${path}".` + ); + continue; + } + if (rule.action === "remove") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(`Removing the path "${path}" (value was: "${obj[key]}")`); + return "remove"; + } else if (rule.action === "replace") { + const newValue = typeof rule.replace === "function" ? rule.replace({ + key, + path, + value: obj[key], + eventType + }) : rule.replace; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && newValue !== obj[key] && debug( + `Setting a new value of "${newValue}" for path: "${path}" (value was: "${obj[key]}")` + ); + obj[key] = newValue; + } + } + } + }); + return event; +}; + +// src/transport.ts +function createTransportUrl(ingestUrl, topic) { + const url = new URL(ingestUrl); + url.pathname = "/report/2/" + topic; + return url.toString(); +} +function makeTransport(options) { + options.getRequest = (envelope) => { + if (!checkEnvelopeSentrySDKCompatibility(envelope)) { + return false; + } + const events = envelopeToIngestionEvents( + envelope, + options.sentryKitConfig + ).map( + (event) => processForPrivacy( + JSON.parse(JSON.stringify(event)), + options.sentryKitConfig.privacyRules + ) + ); + const topic = events[0]._itemHeader.type === "transaction" ? "traces" : "error"; + const url = createTransportUrl( + options.sentryKitConfig.ingestUrl, + options.sentryKitConfig.topic[topic] + ); + return { + url, + body: JSON.stringify({ events }), + headers: { + "Content-type": "application/json" + } + }; + }; + return supportsFetch() ? makeFetchTransport(options) : makeXHRTransport(options); +} + +// src/privacy/settings.ts +var debug2 = /* @__PURE__ */ createLogger("[SentryKit Privacy settings]"); +function createPrivacyRulesFrom({ + allowQueryParams, + allowExtra, + allowTags, + timestampPrecision +}) { + const privacyRules = []; + if (allowTags) { + privacyRules.push({ + type: "any", + matchPath: ({ path, key }) => { + const shouldKeep = path === `tags.${key}` && allowTags.includes(key); + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldKeep && debug2(`Keeping the tag "${key}" for path: "${path}"`); + return shouldKeep; + }, + action: "keep" + }); + } + if (allowExtra) { + privacyRules.push({ + type: "any", + matchPath: ({ path, key }) => { + const shouldKeep = path === `extra.${key}` && allowExtra.includes(key); + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldKeep && debug2(`Keeping the extra "${key}" for path: "${path}"`); + return shouldKeep; + }, + action: "keep" + }); + } + if (allowQueryParams) { + privacyRules.push({ + type: "url-query", + matchQueryName: ({ url, queryName, path }) => { + const params = typeof allowQueryParams === "function" ? allowQueryParams(url) : allowQueryParams; + const shouldKeep = params.includes(queryName); + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldKeep && debug2(`Keeping the query name "${queryName}" for path: "${path}"`); + return shouldKeep; + }, + action: "keep" + }); + } + if (timestampPrecision) { + privacyRules.push({ + type: "timestamp", + precision: ({ eventType, timestamp, path }) => { + if (eventType === "error") { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug2( + `Reducing timestamp to "${timestampPrecision}" for path: "${path}"` + ); + return timestampPrecision; + } + return timestamp; + } + }); + } + return privacyRules; +} + +// src/config.ts +var debug3 = /* @__PURE__ */ createLogger("[SentryKit Config]"); +var SUPPORTED_SENTRY_VERSION = "7.57.0"; +var ENVIRONMENTS = { + prod: "prod", + qa: "qa" +}; +var INGEST_URLS = { + prod: "https://xp.apple.com", + qa: "https://xp-qa.apple.com" +}; +var defaultPrivacyRules = [ + { + type: "any", + matchPath: /tags\..+$/, + action: "remove" + }, + { + type: "any", + matchPath: /tags\.(http\.status_code|visibilitychange|effectiveConnectionType|connectionType|deviceMemory|hardwareConcurrency|lcp\..+)$/, + action: "keep" + }, + { + type: "any", + matchPath: /^extra\.[^.]+$/, + action: "remove" + }, + { + type: "any", + matchPath: /^extra\.arguments$/, + action: "keep" + }, + { + type: "any", + matchPath: /^user\.[^.]+$/, + action: "remove" + }, + { + type: "url-query", + action: "replace", + replace: "REMOVED" + } +]; +function baseConfig(userOptions) { + const privacyRules = typeof userOptions.privacyRules === "function" ? userOptions.privacyRules(defaultPrivacyRules) : userOptions.privacyRules === false ? [] : [...defaultPrivacyRules, ...userOptions.privacyRules || []]; + if (userOptions.privacySettings) { + privacyRules.push(...createPrivacyRulesFrom(userOptions.privacySettings)); + } + const config = __spreadProps(__spreadValues({ + transport: makeTransport, + ingestUrl: "", + topic: "xp_amp_web_error_log", + environment: "qa", + filterHeadless: true, + maxBreadcrumbs: 0, + release: void 0, + redactKeys: void 0, + sampleRate: void 0, + tracesSampleRate: void 0, + tracesSampler: void 0 + }, userOptions), { + privacyRules, + dsn: "https://dsn@bypass/1", + autoSessionTracking: false, + sendClientReports: false, + replaysSessionSampleRate: void 0, + replaysOnErrorSampleRate: void 0 + }); + if (typeof config.topic === "string") { + config.topic = { + error: config.topic, + traces: "" + }; + } + const $config = config; + $config.transportOptions = __spreadProps(__spreadValues({}, $config.transportOptions), { + sentryKitConfig: $config + }); + if (!$config.ingestUrl) { + $config.ingestUrl = $config.environment === ENVIRONMENTS.prod ? INGEST_URLS.prod : INGEST_URLS.qa; + } + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug3( + `Initialized with environment "${$config.environment}" and ingestUrl "${$config.ingestUrl}".` + ); + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug3( + $config.topic.traces ? `Tracing topic is set to "${$config.topic.traces}".` : `No tracing topic is set.` + ); + return $config; +} +function beforeHooksOptions(config) { + const IS_SUPPORTED_SDK = SDK_VERSION2 === SUPPORTED_SENTRY_VERSION; + const shouldSkipEvent = !IS_SUPPORTED_SDK || config.environment === ENVIRONMENTS.prod && config.filterHeadless && isHeadlessBrowser(); + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldSkipEvent && debug3("Events and transactions will not be sent to sentry."); + return { + beforeSend: (event, hint) => { + if (shouldSkipEvent) { + return null; + } + return config.beforeSend ? config.beforeSend(event, hint) : event; + }, + beforeSendTransaction(event, hint) { + if (shouldSkipEvent) { + return null; + } + return config.beforeSendTransaction ? config.beforeSendTransaction(event, hint) : event; + }, + beforeBreadcrumb(breadcrumb, hint) { + if (breadcrumb.category === "console") { + return null; + } + return config.beforeBreadcrumb ? config.beforeBreadcrumb(breadcrumb, hint) : breadcrumb; + } + }; +} + +// src/index.ts +checkSentrySDKCompatibility(); +(typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && monitorSentryHubBindClient(); +function createSentryConfig(userOptions) { + ; + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && (userOptions == null ? void 0 : userOptions.debug) && enableLogger(); + const config = baseConfig(userOptions || {}); + if (!config.project) { + throw new Error( + "[SentryKit Configuration Error]: The required `project` field is not set." + ); + } + const hasTracesConfig = config.tracesSampleRate || config.tracesSampler; + const hasTracesTopic = config.topic.traces; + if (hasTracesConfig && !hasTracesTopic) { + throw new Error( + "[SentryKit Configuration Error]: The `topic.traces` field is not set while trace sampling is configured." + ); + } + if (hasTracesTopic && !hasTracesConfig) { + throw new Error( + "[SentryKit Configuration Error]: Trace sampling is configured but `topic.traces` is not set." + ); + } + const originalConfig = __spreadValues(__spreadValues({}, config), beforeHooksOptions(config)); + (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && monitorSentryConfig(originalConfig); + return originalConfig; +} +var SentryKit = { createSentryConfig }; +var src_default = SentryKit; +export { + SentryKit, + createSentryConfig, + src_default as default +}; diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/backgroundtab.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/backgroundtab.js new file mode 100644 index 0000000..62c1d20 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/backgroundtab.js @@ -0,0 +1,36 @@ +import { getActiveTransaction } from '@sentry/core'; +import { logger } from '@sentry/utils'; +import { WINDOW } from './types.js'; + +/** + * Add a listener that cancels and finishes a transaction when the global + * document is hidden. + */ +function registerBackgroundTabDetection() { + if (WINDOW && WINDOW.document) { + WINDOW.document.addEventListener('visibilitychange', () => { + const activeTransaction = getActiveTransaction() ; + if (WINDOW.document.hidden && activeTransaction) { + const statusType = 'cancelled'; + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + `[Tracing] Transaction: ${statusType} -> since tab moved to the background, op: ${activeTransaction.op}`, + ); + // We should not set status if it is already set, this prevent important statuses like + // error or data loss from being overwritten on transaction. + if (!activeTransaction.status) { + activeTransaction.setStatus(statusType); + } + activeTransaction.setTag('visibilitychange', 'document.hidden'); + activeTransaction.finish(); + } + }); + } else { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn('[Tracing] Could not set up background tab detection due to lack of global document'); + } +} + +export { registerBackgroundTabDetection }; +//# sourceMappingURL=backgroundtab.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/browsertracing.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/browsertracing.js new file mode 100644 index 0000000..3b79fb3 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/browsertracing.js @@ -0,0 +1,300 @@ +import { TRACING_DEFAULTS, addTracingExtensions, extractTraceparentData, startIdleTransaction, getActiveTransaction } from '@sentry/core'; +import { logger, baggageHeaderToDynamicSamplingContext, getDomElement } from '@sentry/utils'; +import { registerBackgroundTabDetection } from './backgroundtab.js'; +import { startTrackingWebVitals, startTrackingLongTasks, startTrackingInteractions, addPerformanceEntries } from './metrics/index.js'; +import { defaultRequestInstrumentationOptions, instrumentOutgoingRequests } from './request.js'; +import { instrumentRoutingWithDefaults } from './router.js'; +import { WINDOW } from './types.js'; + +const BROWSER_TRACING_INTEGRATION_ID = 'BrowserTracing'; + +/** Options for Browser Tracing integration */ + +const DEFAULT_BROWSER_TRACING_OPTIONS = { + ...TRACING_DEFAULTS, + markBackgroundTransactions: true, + routingInstrumentation: instrumentRoutingWithDefaults, + startTransactionOnLocationChange: true, + startTransactionOnPageLoad: true, + enableLongTask: true, + ...defaultRequestInstrumentationOptions, +}; + +/** + * The Browser Tracing integration automatically instruments browser pageload/navigation + * actions as transactions, and captures requests, metrics and errors as spans. + * + * The integration can be configured with a variety of options, and can be extended to use + * any routing library. This integration uses {@see IdleTransaction} to create transactions. + */ +class BrowserTracing { + // This class currently doesn't have a static `id` field like the other integration classes, because it prevented + // @sentry/tracing from being treeshaken. Tree shakers do not like static fields, because they behave like side effects. + // TODO: Come up with a better plan, than using static fields on integration classes, and use that plan on all + // integrations. + + /** Browser Tracing integration options */ + + /** + * @inheritDoc + */ + __init() {this.name = BROWSER_TRACING_INTEGRATION_ID;} + + __init2() {this._hasSetTracePropagationTargets = false;} + + constructor(_options) {BrowserTracing.prototype.__init.call(this);BrowserTracing.prototype.__init2.call(this); + addTracingExtensions(); + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + this._hasSetTracePropagationTargets = !!( + _options && + // eslint-disable-next-line deprecation/deprecation + (_options.tracePropagationTargets || _options.tracingOrigins) + ); + } + + this.options = { + ...DEFAULT_BROWSER_TRACING_OPTIONS, + ..._options, + }; + + // Special case: enableLongTask can be set in _experiments + // TODO (v8): Remove this in v8 + if (this.options._experiments.enableLongTask !== undefined) { + this.options.enableLongTask = this.options._experiments.enableLongTask; + } + + // TODO (v8): remove this block after tracingOrigins is removed + // Set tracePropagationTargets to tracingOrigins if specified by the user + // In case both are specified, tracePropagationTargets takes precedence + // eslint-disable-next-line deprecation/deprecation + if (_options && !_options.tracePropagationTargets && _options.tracingOrigins) { + // eslint-disable-next-line deprecation/deprecation + this.options.tracePropagationTargets = _options.tracingOrigins; + } + + this._collectWebVitals = startTrackingWebVitals(); + if (this.options.enableLongTask) { + startTrackingLongTasks(); + } + if (this.options._experiments.enableInteractions) { + startTrackingInteractions(); + } + } + + /** + * @inheritDoc + */ + setupOnce(_, getCurrentHub) { + this._getCurrentHub = getCurrentHub; + const hub = getCurrentHub(); + const client = hub.getClient(); + const clientOptions = client && client.getOptions(); + + const { + routingInstrumentation: instrumentRouting, + startTransactionOnLocationChange, + startTransactionOnPageLoad, + markBackgroundTransactions, + traceFetch, + traceXHR, + shouldCreateSpanForRequest, + _experiments, + } = this.options; + + const clientOptionsTracePropagationTargets = clientOptions && clientOptions.tracePropagationTargets; + // There are three ways to configure tracePropagationTargets: + // 1. via top level client option `tracePropagationTargets` + // 2. via BrowserTracing option `tracePropagationTargets` + // 3. via BrowserTracing option `tracingOrigins` (deprecated) + // + // To avoid confusion, favour top level client option `tracePropagationTargets`, and fallback to + // BrowserTracing option `tracePropagationTargets` and then `tracingOrigins` (deprecated). + // This is done as it minimizes bundle size (we don't have to have undefined checks). + // + // If both 1 and either one of 2 or 3 are set (from above), we log out a warning. + const tracePropagationTargets = clientOptionsTracePropagationTargets || this.options.tracePropagationTargets; + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && this._hasSetTracePropagationTargets && clientOptionsTracePropagationTargets) { + logger.warn( + '[Tracing] The `tracePropagationTargets` option was set in the BrowserTracing integration and top level `Sentry.init`. The top level `Sentry.init` value is being used.', + ); + } + + instrumentRouting( + (context) => { + const transaction = this._createRouteTransaction(context); + + this.options._experiments.onStartRouteTransaction && + this.options._experiments.onStartRouteTransaction(transaction, context, getCurrentHub); + + return transaction; + }, + startTransactionOnPageLoad, + startTransactionOnLocationChange, + ); + + if (markBackgroundTransactions) { + registerBackgroundTabDetection(); + } + + if (_experiments.enableInteractions) { + this._registerInteractionListener(); + } + + instrumentOutgoingRequests({ + traceFetch, + traceXHR, + tracePropagationTargets, + shouldCreateSpanForRequest, + _experiments: { + enableHTTPTimings: _experiments.enableHTTPTimings, + }, + }); + } + + /** Create routing idle transaction. */ + _createRouteTransaction(context) { + if (!this._getCurrentHub) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn(`[Tracing] Did not create ${context.op} transaction because _getCurrentHub is invalid.`); + return undefined; + } + + const { beforeNavigate, idleTimeout, finalTimeout, heartbeatInterval } = this.options; + + const isPageloadTransaction = context.op === 'pageload'; + + const sentryTraceMetaTagValue = isPageloadTransaction ? getMetaContent('sentry-trace') : null; + const baggageMetaTagValue = isPageloadTransaction ? getMetaContent('baggage') : null; + + const traceParentData = sentryTraceMetaTagValue ? extractTraceparentData(sentryTraceMetaTagValue) : undefined; + const dynamicSamplingContext = baggageMetaTagValue + ? baggageHeaderToDynamicSamplingContext(baggageMetaTagValue) + : undefined; + + const expandedContext = { + ...context, + ...traceParentData, + metadata: { + ...context.metadata, + dynamicSamplingContext: traceParentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, + }, + trimEnd: true, + }; + + const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate(expandedContext) : expandedContext; + + // For backwards compatibility reasons, beforeNavigate can return undefined to "drop" the transaction (prevent it + // from being sent to Sentry). + const finalContext = modifiedContext === undefined ? { ...expandedContext, sampled: false } : modifiedContext; + + // If `beforeNavigate` set a custom name, record that fact + finalContext.metadata = + finalContext.name !== expandedContext.name + ? { ...finalContext.metadata, source: 'custom' } + : finalContext.metadata; + + this._latestRouteName = finalContext.name; + this._latestRouteSource = finalContext.metadata && finalContext.metadata.source; + + if (finalContext.sampled === false) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log(`[Tracing] Will not send ${finalContext.op} transaction because of beforeNavigate.`); + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] Starting ${finalContext.op} transaction on scope`); + + const hub = this._getCurrentHub(); + const { location } = WINDOW; + + const idleTransaction = startIdleTransaction( + hub, + finalContext, + idleTimeout, + finalTimeout, + true, + { location }, // for use in the tracesSampler + heartbeatInterval, + ); + idleTransaction.registerBeforeFinishCallback(transaction => { + this._collectWebVitals(); + addPerformanceEntries(transaction); + }); + + return idleTransaction ; + } + + /** Start listener for interaction transactions */ + _registerInteractionListener() { + let inflightInteractionTransaction; + const registerInteractionTransaction = () => { + const { idleTimeout, finalTimeout, heartbeatInterval } = this.options; + const op = 'ui.action.click'; + + const currentTransaction = getActiveTransaction(); + if (currentTransaction && currentTransaction.op && ['navigation', 'pageload'].includes(currentTransaction.op)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `[Tracing] Did not create ${op} transaction because a pageload or navigation transaction is in progress.`, + ); + return undefined; + } + + if (inflightInteractionTransaction) { + inflightInteractionTransaction.setFinishReason('interactionInterrupted'); + inflightInteractionTransaction.finish(); + inflightInteractionTransaction = undefined; + } + + if (!this._getCurrentHub) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn(`[Tracing] Did not create ${op} transaction because _getCurrentHub is invalid.`); + return undefined; + } + + if (!this._latestRouteName) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn(`[Tracing] Did not create ${op} transaction because _latestRouteName is missing.`); + return undefined; + } + + const hub = this._getCurrentHub(); + const { location } = WINDOW; + + const context = { + name: this._latestRouteName, + op, + trimEnd: true, + metadata: { + source: this._latestRouteSource || 'url', + }, + }; + + inflightInteractionTransaction = startIdleTransaction( + hub, + context, + idleTimeout, + finalTimeout, + true, + { location }, // for use in the tracesSampler + heartbeatInterval, + ); + }; + + ['click'].forEach(type => { + addEventListener(type, registerInteractionTransaction, { once: false, capture: true }); + }); + } +} + +/** Returns the value of a meta tag */ +function getMetaContent(metaName) { + // Can't specify generic to `getDomElement` because tracing can be used + // in a variety of environments, have to disable `no-unsafe-member-access` + // as a result. + const metaTag = getDomElement(`meta[name=${metaName}]`); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return metaTag ? metaTag.getAttribute('content') : null; +} + +export { BROWSER_TRACING_INTEGRATION_ID, BrowserTracing, getMetaContent }; +//# sourceMappingURL=browsertracing.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/metrics/index.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/metrics/index.js new file mode 100644 index 0000000..5bca7af --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/metrics/index.js @@ -0,0 +1,484 @@ +import { getActiveTransaction } from '@sentry/core'; +import { browserPerformanceTimeOrigin, logger, htmlTreeAsString } from '@sentry/utils'; +import { WINDOW } from '../types.js'; +import { onCLS } from '../web-vitals/getCLS.js'; +import { onFID } from '../web-vitals/getFID.js'; +import { onLCP } from '../web-vitals/getLCP.js'; +import { getVisibilityWatcher } from '../web-vitals/lib/getVisibilityWatcher.js'; +import { observe } from '../web-vitals/lib/observe.js'; +import { _startChild, isMeasurementValue } from './utils.js'; + +/** + * Converts from milliseconds to seconds + * @param time time in ms + */ +function msToSec(time) { + return time / 1000; +} + +function getBrowserPerformanceAPI() { + // @ts-ignore we want to make sure all of these are available, even if TS is sure they are + return WINDOW && WINDOW.addEventListener && WINDOW.performance; +} + +let _performanceCursor = 0; + +let _measurements = {}; +let _lcpEntry; +let _clsEntry; + +/** + * Start tracking web vitals + * + * @returns A function that forces web vitals collection + */ +function startTrackingWebVitals() { + const performance = getBrowserPerformanceAPI(); + if (performance && browserPerformanceTimeOrigin) { + // @ts-ignore we want to make sure all of these are available, even if TS is sure they are + if (performance.mark) { + WINDOW.performance.mark('sentry-tracing-init'); + } + _trackFID(); + const clsCallback = _trackCLS(); + const lcpCallback = _trackLCP(); + + return () => { + if (clsCallback) { + clsCallback(); + } + if (lcpCallback) { + lcpCallback(); + } + }; + } + + return () => undefined; +} + +/** + * Start tracking long tasks. + */ +function startTrackingLongTasks() { + const entryHandler = (entries) => { + for (const entry of entries) { + const transaction = getActiveTransaction() ; + if (!transaction) { + return; + } + const startTime = msToSec((browserPerformanceTimeOrigin ) + entry.startTime); + const duration = msToSec(entry.duration); + + transaction.startChild({ + description: 'Main UI thread blocked', + op: 'ui.long-task', + startTimestamp: startTime, + endTimestamp: startTime + duration, + }); + } + }; + + observe('longtask', entryHandler); +} + +/** + * Start tracking interaction events. + */ +function startTrackingInteractions() { + const entryHandler = (entries) => { + for (const entry of entries) { + const transaction = getActiveTransaction() ; + if (!transaction) { + return; + } + + if (entry.name === 'click') { + const startTime = msToSec((browserPerformanceTimeOrigin ) + entry.startTime); + const duration = msToSec(entry.duration); + + transaction.startChild({ + description: htmlTreeAsString(entry.target), + op: `ui.interaction.${entry.name}`, + startTimestamp: startTime, + endTimestamp: startTime + duration, + }); + } + } + }; + + observe('event', entryHandler, { durationThreshold: 0 }); +} + +/** Starts tracking the Cumulative Layout Shift on the current page. */ +function _trackCLS() { + // See: + // https://web.dev/evolving-cls/ + // https://web.dev/cls-web-tooling/ + return onCLS(metric => { + const entry = metric.entries.pop(); + if (!entry) { + return; + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding CLS'); + _measurements['cls'] = { value: metric.value, unit: '' }; + _clsEntry = entry ; + }); +} + +/** Starts tracking the Largest Contentful Paint on the current page. */ +function _trackLCP() { + return onLCP(metric => { + const entry = metric.entries.pop(); + if (!entry) { + return; + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding LCP'); + _measurements['lcp'] = { value: metric.value, unit: 'millisecond' }; + _lcpEntry = entry ; + }); +} + +/** Starts tracking the First Input Delay on the current page. */ +function _trackFID() { + onFID(metric => { + const entry = metric.entries.pop(); + if (!entry) { + return; + } + + const timeOrigin = msToSec(browserPerformanceTimeOrigin ); + const startTime = msToSec(entry.startTime); + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding FID'); + _measurements['fid'] = { value: metric.value, unit: 'millisecond' }; + _measurements['mark.fid'] = { value: timeOrigin + startTime, unit: 'second' }; + }); +} + +/** Add performance related spans to a transaction */ +function addPerformanceEntries(transaction) { + const performance = getBrowserPerformanceAPI(); + if (!performance || !WINDOW.performance.getEntries || !browserPerformanceTimeOrigin) { + // Gatekeeper if performance API not available + return; + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] Adding & adjusting spans using Performance API'); + const timeOrigin = msToSec(browserPerformanceTimeOrigin); + + const performanceEntries = performance.getEntries(); + + let responseStartTimestamp; + let requestStartTimestamp; + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + performanceEntries.slice(_performanceCursor).forEach((entry) => { + const startTime = msToSec(entry.startTime); + const duration = msToSec(entry.duration); + + if (transaction.op === 'navigation' && timeOrigin + startTime < transaction.startTimestamp) { + return; + } + + switch (entry.entryType) { + case 'navigation': { + _addNavigationSpans(transaction, entry, timeOrigin); + responseStartTimestamp = timeOrigin + msToSec(entry.responseStart); + requestStartTimestamp = timeOrigin + msToSec(entry.requestStart); + break; + } + case 'mark': + case 'paint': + case 'measure': { + _addMeasureSpans(transaction, entry, startTime, duration, timeOrigin); + + // capture web vitals + const firstHidden = getVisibilityWatcher(); + // Only report if the page wasn't hidden prior to the web vital. + const shouldRecord = entry.startTime < firstHidden.firstHiddenTime; + + if (entry.name === 'first-paint' && shouldRecord) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding FP'); + _measurements['fp'] = { value: entry.startTime, unit: 'millisecond' }; + } + if (entry.name === 'first-contentful-paint' && shouldRecord) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding FCP'); + _measurements['fcp'] = { value: entry.startTime, unit: 'millisecond' }; + } + break; + } + case 'resource': { + const resourceName = (entry.name ).replace(WINDOW.location.origin, ''); + _addResourceSpans(transaction, entry, resourceName, startTime, duration, timeOrigin); + break; + } + // Ignore other entry types. + } + }); + + _performanceCursor = Math.max(performanceEntries.length - 1, 0); + + _trackNavigator(transaction); + + // Measurements are only available for pageload transactions + if (transaction.op === 'pageload') { + // Generate TTFB (Time to First Byte), which measured as the time between the beginning of the transaction and the + // start of the response in milliseconds + if (typeof responseStartTimestamp === 'number') { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding TTFB'); + _measurements['ttfb'] = { + value: (responseStartTimestamp - transaction.startTimestamp) * 1000, + unit: 'millisecond', + }; + + if (typeof requestStartTimestamp === 'number' && requestStartTimestamp <= responseStartTimestamp) { + // Capture the time spent making the request and receiving the first byte of the response. + // This is the time between the start of the request and the start of the response in milliseconds. + _measurements['ttfb.requestTime'] = { + value: (responseStartTimestamp - requestStartTimestamp) * 1000, + unit: 'millisecond', + }; + } + } + + ['fcp', 'fp', 'lcp'].forEach(name => { + if (!_measurements[name] || timeOrigin >= transaction.startTimestamp) { + return; + } + // The web vitals, fcp, fp, lcp, and ttfb, all measure relative to timeOrigin. + // Unfortunately, timeOrigin is not captured within the transaction span data, so these web vitals will need + // to be adjusted to be relative to transaction.startTimestamp. + const oldValue = _measurements[name].value; + const measurementTimestamp = timeOrigin + msToSec(oldValue); + + // normalizedValue should be in milliseconds + const normalizedValue = Math.abs((measurementTimestamp - transaction.startTimestamp) * 1000); + const delta = normalizedValue - oldValue; + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log(`[Measurements] Normalized ${name} from ${oldValue} to ${normalizedValue} (${delta})`); + _measurements[name].value = normalizedValue; + }); + + const fidMark = _measurements['mark.fid']; + if (fidMark && _measurements['fid']) { + // create span for FID + _startChild(transaction, { + description: 'first input delay', + endTimestamp: fidMark.value + msToSec(_measurements['fid'].value), + op: 'ui.action', + startTimestamp: fidMark.value, + }); + + // Delete mark.fid as we don't want it to be part of final payload + delete _measurements['mark.fid']; + } + + // If FCP is not recorded we should not record the cls value + // according to the new definition of CLS. + if (!('fcp' in _measurements)) { + delete _measurements.cls; + } + + Object.keys(_measurements).forEach(measurementName => { + transaction.setMeasurement( + measurementName, + _measurements[measurementName].value, + _measurements[measurementName].unit, + ); + }); + + _tagMetricInfo(transaction); + } + + _lcpEntry = undefined; + _clsEntry = undefined; + _measurements = {}; +} + +/** Create measure related spans */ +function _addMeasureSpans( + transaction, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + entry, + startTime, + duration, + timeOrigin, +) { + const measureStartTimestamp = timeOrigin + startTime; + const measureEndTimestamp = measureStartTimestamp + duration; + + _startChild(transaction, { + description: entry.name , + endTimestamp: measureEndTimestamp, + op: entry.entryType , + startTimestamp: measureStartTimestamp, + }); + + return measureStartTimestamp; +} + +/** Instrument navigation entries */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function _addNavigationSpans(transaction, entry, timeOrigin) { + ['unloadEvent', 'redirect', 'domContentLoadedEvent', 'loadEvent', 'connect'].forEach(event => { + _addPerformanceNavigationTiming(transaction, entry, event, timeOrigin); + }); + _addPerformanceNavigationTiming(transaction, entry, 'secureConnection', timeOrigin, 'TLS/SSL', 'connectEnd'); + _addPerformanceNavigationTiming(transaction, entry, 'fetch', timeOrigin, 'cache', 'domainLookupStart'); + _addPerformanceNavigationTiming(transaction, entry, 'domainLookup', timeOrigin, 'DNS'); + _addRequest(transaction, entry, timeOrigin); +} + +/** Create performance navigation related spans */ +function _addPerformanceNavigationTiming( + transaction, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + entry, + event, + timeOrigin, + description, + eventEnd, +) { + const end = eventEnd ? (entry[eventEnd] ) : (entry[`${event}End`] ); + const start = entry[`${event}Start`] ; + if (!start || !end) { + return; + } + _startChild(transaction, { + op: 'browser', + description: description || event, + startTimestamp: timeOrigin + msToSec(start), + endTimestamp: timeOrigin + msToSec(end), + }); +} + +/** Create request and response related spans */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function _addRequest(transaction, entry, timeOrigin) { + _startChild(transaction, { + op: 'browser', + description: 'request', + startTimestamp: timeOrigin + msToSec(entry.requestStart ), + endTimestamp: timeOrigin + msToSec(entry.responseEnd ), + }); + + _startChild(transaction, { + op: 'browser', + description: 'response', + startTimestamp: timeOrigin + msToSec(entry.responseStart ), + endTimestamp: timeOrigin + msToSec(entry.responseEnd ), + }); +} + +/** Create resource-related spans */ +function _addResourceSpans( + transaction, + entry, + resourceName, + startTime, + duration, + timeOrigin, +) { + // we already instrument based on fetch and xhr, so we don't need to + // duplicate spans here. + if (entry.initiatorType === 'xmlhttprequest' || entry.initiatorType === 'fetch') { + return; + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const data = {}; + if ('transferSize' in entry) { + data['http.response_transfer_size'] = entry.transferSize; + } + if ('encodedBodySize' in entry) { + data['http.response_content_length'] = entry.encodedBodySize; + } + if ('decodedBodySize' in entry) { + data['http.decoded_response_content_length'] = entry.decodedBodySize; + } + if ('renderBlockingStatus' in entry) { + data['resource.render_blocking_status'] = entry.renderBlockingStatus; + } + + const startTimestamp = timeOrigin + startTime; + const endTimestamp = startTimestamp + duration; + + _startChild(transaction, { + description: resourceName, + endTimestamp, + op: entry.initiatorType ? `resource.${entry.initiatorType}` : 'resource.other', + startTimestamp, + data, + }); +} + +/** + * Capture the information of the user agent. + */ +function _trackNavigator(transaction) { + const navigator = WINDOW.navigator ; + if (!navigator) { + return; + } + + // track network connectivity + const connection = navigator.connection; + if (connection) { + if (connection.effectiveType) { + transaction.setTag('effectiveConnectionType', connection.effectiveType); + } + + if (connection.type) { + transaction.setTag('connectionType', connection.type); + } + + if (isMeasurementValue(connection.rtt)) { + _measurements['connection.rtt'] = { value: connection.rtt, unit: 'millisecond' }; + } + } + + if (isMeasurementValue(navigator.deviceMemory)) { + transaction.setTag('deviceMemory', `${navigator.deviceMemory} GB`); + } + + if (isMeasurementValue(navigator.hardwareConcurrency)) { + transaction.setTag('hardwareConcurrency', String(navigator.hardwareConcurrency)); + } +} + +/** Add LCP / CLS data to transaction to allow debugging */ +function _tagMetricInfo(transaction) { + if (_lcpEntry) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding LCP Data'); + + // Capture Properties of the LCP element that contributes to the LCP. + + if (_lcpEntry.element) { + transaction.setTag('lcp.element', htmlTreeAsString(_lcpEntry.element)); + } + + if (_lcpEntry.id) { + transaction.setTag('lcp.id', _lcpEntry.id); + } + + if (_lcpEntry.url) { + // Trim URL to the first 200 characters. + transaction.setTag('lcp.url', _lcpEntry.url.trim().slice(0, 200)); + } + + transaction.setTag('lcp.size', _lcpEntry.size); + } + + // See: https://developer.mozilla.org/en-US/docs/Web/API/LayoutShift + if (_clsEntry && _clsEntry.sources) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Measurements] Adding CLS Data'); + _clsEntry.sources.forEach((source, index) => + transaction.setTag(`cls.source.${index + 1}`, htmlTreeAsString(source.node)), + ); + } +} + +export { _addMeasureSpans, _addResourceSpans, addPerformanceEntries, startTrackingInteractions, startTrackingLongTasks, startTrackingWebVitals }; +//# sourceMappingURL=index.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/metrics/utils.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/metrics/utils.js new file mode 100644 index 0000000..ecb4873 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/metrics/utils.js @@ -0,0 +1,25 @@ +/** + * Checks if a given value is a valid measurement value. + */ +function isMeasurementValue(value) { + return typeof value === 'number' && isFinite(value); +} + +/** + * Helper function to start child on transactions. This function will make sure that the transaction will + * use the start timestamp of the created child span if it is earlier than the transactions actual + * start timestamp. + */ +function _startChild(transaction, { startTimestamp, ...ctx }) { + if (startTimestamp && transaction.startTimestamp > startTimestamp) { + transaction.startTimestamp = startTimestamp; + } + + return transaction.startChild({ + startTimestamp, + ...ctx, + }); +} + +export { _startChild, isMeasurementValue }; +//# sourceMappingURL=utils.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/request.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/request.js new file mode 100644 index 0000000..d505546 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/request.js @@ -0,0 +1,335 @@ +import { _optionalChain } from '@sentry/utils/esm/buildPolyfills'; +import { hasTracingEnabled, getCurrentHub } from '@sentry/core'; +import { addInstrumentationHandler, browserPerformanceTimeOrigin, dynamicSamplingContextToSentryBaggageHeader, isInstanceOf, BAGGAGE_HEADER_NAME, SENTRY_XHR_DATA_KEY, stringMatchesSomePattern } from '@sentry/utils'; + +/* eslint-disable max-lines */ + +const DEFAULT_TRACE_PROPAGATION_TARGETS = ['localhost', /^\/(?!\/)/]; + +/** Options for Request Instrumentation */ + +const defaultRequestInstrumentationOptions = { + traceFetch: true, + traceXHR: true, + // TODO (v8): Remove this property + tracingOrigins: DEFAULT_TRACE_PROPAGATION_TARGETS, + tracePropagationTargets: DEFAULT_TRACE_PROPAGATION_TARGETS, + _experiments: {}, +}; + +/** Registers span creators for xhr and fetch requests */ +function instrumentOutgoingRequests(_options) { + // eslint-disable-next-line deprecation/deprecation + const { traceFetch, traceXHR, tracePropagationTargets, tracingOrigins, shouldCreateSpanForRequest, _experiments } = { + traceFetch: defaultRequestInstrumentationOptions.traceFetch, + traceXHR: defaultRequestInstrumentationOptions.traceXHR, + ..._options, + }; + + const shouldCreateSpan = + typeof shouldCreateSpanForRequest === 'function' ? shouldCreateSpanForRequest : (_) => true; + + // TODO(v8) Remove tracingOrigins here + // The only reason we're passing it in here is because this instrumentOutgoingRequests function is publicly exported + // and we don't want to break the API. We can remove it in v8. + const shouldAttachHeadersWithTargets = (url) => + shouldAttachHeaders(url, tracePropagationTargets || tracingOrigins); + + const spans = {}; + + if (traceFetch) { + addInstrumentationHandler('fetch', (handlerData) => { + const createdSpan = fetchCallback(handlerData, shouldCreateSpan, shouldAttachHeadersWithTargets, spans); + if (_optionalChain([_experiments, 'optionalAccess', _2 => _2.enableHTTPTimings]) && createdSpan) { + addHTTPTimings(createdSpan); + } + }); + } + + if (traceXHR) { + addInstrumentationHandler('xhr', (handlerData) => { + const createdSpan = xhrCallback(handlerData, shouldCreateSpan, shouldAttachHeadersWithTargets, spans); + if (_optionalChain([_experiments, 'optionalAccess', _3 => _3.enableHTTPTimings]) && createdSpan) { + addHTTPTimings(createdSpan); + } + }); + } +} + +/** + * Creates a temporary observer to listen to the next fetch/xhr resourcing timings, + * so that when timings hit their per-browser limit they don't need to be removed. + * + * @param span A span that has yet to be finished, must contain `url` on data. + */ +function addHTTPTimings(span) { + const url = span.data.url; + const observer = new PerformanceObserver(list => { + const entries = list.getEntries() ; + entries.forEach(entry => { + if ((entry.initiatorType === 'fetch' || entry.initiatorType === 'xmlhttprequest') && entry.name.endsWith(url)) { + const spanData = resourceTimingEntryToSpanData(entry); + spanData.forEach(data => span.setData(...data)); + observer.disconnect(); + } + }); + }); + observer.observe({ + entryTypes: ['resource'], + }); +} + +function resourceTimingEntryToSpanData(resourceTiming) { + const version = resourceTiming.nextHopProtocol.split('/')[1] || 'none'; + + const timingSpanData = []; + if (version) { + timingSpanData.push(['network.protocol.version', version]); + } + + if (!browserPerformanceTimeOrigin) { + return timingSpanData; + } + return [ + ...timingSpanData, + ['http.request.connect_start', (browserPerformanceTimeOrigin + resourceTiming.connectStart) / 1000], + ['http.request.request_start', (browserPerformanceTimeOrigin + resourceTiming.requestStart) / 1000], + ['http.request.response_start', (browserPerformanceTimeOrigin + resourceTiming.responseStart) / 1000], + ]; +} + +/** + * A function that determines whether to attach tracing headers to a request. + * This was extracted from `instrumentOutgoingRequests` to make it easier to test shouldAttachHeaders. + * We only export this fuction for testing purposes. + */ +function shouldAttachHeaders(url, tracePropagationTargets) { + return stringMatchesSomePattern(url, tracePropagationTargets || DEFAULT_TRACE_PROPAGATION_TARGETS); +} + +/** + * Create and track fetch request spans + * + * @returns Span if a span was created, otherwise void. + */ +function fetchCallback( + handlerData, + shouldCreateSpan, + shouldAttachHeaders, + spans, +) { + if (!hasTracingEnabled() || !(handlerData.fetchData && shouldCreateSpan(handlerData.fetchData.url))) { + return; + } + + if (handlerData.endTimestamp) { + const spanId = handlerData.fetchData.__span; + if (!spanId) return; + + const span = spans[spanId]; + if (span) { + if (handlerData.response) { + // TODO (kmclb) remove this once types PR goes through + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + span.setHttpStatus(handlerData.response.status); + + const contentLength = + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + handlerData.response && handlerData.response.headers && handlerData.response.headers.get('content-length'); + + const contentLengthNum = parseInt(contentLength); + if (contentLengthNum > 0) { + span.setData('http.response_content_length', contentLengthNum); + } + } else if (handlerData.error) { + span.setStatus('internal_error'); + } + span.finish(); + + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete spans[spanId]; + } + return; + } + + const currentSpan = getCurrentHub().getScope().getSpan(); + const activeTransaction = currentSpan && currentSpan.transaction; + + if (currentSpan && activeTransaction) { + const { method, url } = handlerData.fetchData; + const span = currentSpan.startChild({ + data: { + url, + type: 'fetch', + 'http.method': method, + }, + description: `${method} ${url}`, + op: 'http.client', + }); + + handlerData.fetchData.__span = span.spanId; + spans[span.spanId] = span; + + const request = handlerData.args[0]; + + // In case the user hasn't set the second argument of a fetch call we default it to `{}`. + handlerData.args[1] = handlerData.args[1] || {}; + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const options = handlerData.args[1]; + + if (shouldAttachHeaders(handlerData.fetchData.url)) { + options.headers = addTracingHeadersToFetchRequest( + request, + activeTransaction.getDynamicSamplingContext(), + span, + options, + ); + } + return span; + } +} + +/** + * Adds sentry-trace and baggage headers to the various forms of fetch headers + */ +function addTracingHeadersToFetchRequest( + request, // unknown is actually type Request but we can't export DOM types from this package, + dynamicSamplingContext, + span, + options + +, +) { + const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); + const sentryTraceHeader = span.toTraceparent(); + + const headers = + typeof Request !== 'undefined' && isInstanceOf(request, Request) ? (request ).headers : options.headers; + + if (!headers) { + return { 'sentry-trace': sentryTraceHeader, baggage: sentryBaggageHeader }; + } else if (typeof Headers !== 'undefined' && isInstanceOf(headers, Headers)) { + const newHeaders = new Headers(headers ); + + newHeaders.append('sentry-trace', sentryTraceHeader); + + if (sentryBaggageHeader) { + // If the same header is appended multiple times the browser will merge the values into a single request header. + // Its therefore safe to simply push a "baggage" entry, even though there might already be another baggage header. + newHeaders.append(BAGGAGE_HEADER_NAME, sentryBaggageHeader); + } + + return newHeaders ; + } else if (Array.isArray(headers)) { + const newHeaders = [...headers, ['sentry-trace', sentryTraceHeader]]; + + if (sentryBaggageHeader) { + // If there are multiple entries with the same key, the browser will merge the values into a single request header. + // Its therefore safe to simply push a "baggage" entry, even though there might already be another baggage header. + newHeaders.push([BAGGAGE_HEADER_NAME, sentryBaggageHeader]); + } + + return newHeaders ; + } else { + const existingBaggageHeader = 'baggage' in headers ? headers.baggage : undefined; + const newBaggageHeaders = []; + + if (Array.isArray(existingBaggageHeader)) { + newBaggageHeaders.push(...existingBaggageHeader); + } else if (existingBaggageHeader) { + newBaggageHeaders.push(existingBaggageHeader); + } + + if (sentryBaggageHeader) { + newBaggageHeaders.push(sentryBaggageHeader); + } + + return { + ...(headers ), + 'sentry-trace': sentryTraceHeader, + baggage: newBaggageHeaders.length > 0 ? newBaggageHeaders.join(',') : undefined, + }; + } +} + +/** + * Create and track xhr request spans + * + * @returns Span if a span was created, otherwise void. + */ +function xhrCallback( + handlerData, + shouldCreateSpan, + shouldAttachHeaders, + spans, +) { + const xhr = handlerData.xhr; + const sentryXhrData = xhr && xhr[SENTRY_XHR_DATA_KEY]; + + if ( + !hasTracingEnabled() || + (xhr && xhr.__sentry_own_request__) || + !(xhr && sentryXhrData && shouldCreateSpan(sentryXhrData.url)) + ) { + return; + } + + // check first if the request has finished and is tracked by an existing span which should now end + if (handlerData.endTimestamp) { + const spanId = xhr.__sentry_xhr_span_id__; + if (!spanId) return; + + const span = spans[spanId]; + if (span) { + span.setHttpStatus(sentryXhrData.status_code); + span.finish(); + + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete spans[spanId]; + } + return; + } + + const currentSpan = getCurrentHub().getScope().getSpan(); + const activeTransaction = currentSpan && currentSpan.transaction; + + if (currentSpan && activeTransaction) { + const span = currentSpan.startChild({ + data: { + ...sentryXhrData.data, + type: 'xhr', + 'http.method': sentryXhrData.method, + url: sentryXhrData.url, + }, + description: `${sentryXhrData.method} ${sentryXhrData.url}`, + op: 'http.client', + }); + + xhr.__sentry_xhr_span_id__ = span.spanId; + spans[xhr.__sentry_xhr_span_id__] = span; + + if (xhr.setRequestHeader && shouldAttachHeaders(sentryXhrData.url)) { + try { + xhr.setRequestHeader('sentry-trace', span.toTraceparent()); + + const dynamicSamplingContext = activeTransaction.getDynamicSamplingContext(); + const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); + + if (sentryBaggageHeader) { + // From MDN: "If this method is called several times with the same header, the values are merged into one single request header." + // We can therefore simply set a baggage header without checking what was there before + // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader + xhr.setRequestHeader(BAGGAGE_HEADER_NAME, sentryBaggageHeader); + } + } catch (_) { + // Error: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED. + } + } + + return span; + } +} + +export { DEFAULT_TRACE_PROPAGATION_TARGETS, addTracingHeadersToFetchRequest, defaultRequestInstrumentationOptions, instrumentOutgoingRequests, shouldAttachHeaders }; +//# sourceMappingURL=request.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js new file mode 100644 index 0000000..8c70bef --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/router.js @@ -0,0 +1,64 @@ +import { logger, browserPerformanceTimeOrigin, addInstrumentationHandler } from '@sentry/utils'; +import { WINDOW } from './types.js'; + +/** + * Default function implementing pageload and navigation transactions + */ +function instrumentRoutingWithDefaults( + customStartTransaction, + startTransactionOnPageLoad = true, + startTransactionOnLocationChange = true, +) { + if (!WINDOW || !WINDOW.location) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Could not initialize routing instrumentation due to invalid location'); + return; + } + + let startingUrl = WINDOW.location.href; + + let activeTransaction; + if (startTransactionOnPageLoad) { + activeTransaction = customStartTransaction({ + name: WINDOW.location.pathname, + // pageload should always start at timeOrigin (and needs to be in s, not ms) + startTimestamp: browserPerformanceTimeOrigin ? browserPerformanceTimeOrigin / 1000 : undefined, + op: 'pageload', + metadata: { source: 'url' }, + }); + } + + if (startTransactionOnLocationChange) { + addInstrumentationHandler('history', ({ to, from }) => { + /** + * This early return is there to account for some cases where a navigation transaction starts right after + * long-running pageload. We make sure that if `from` is undefined and a valid `startingURL` exists, we don't + * create an uneccessary navigation transaction. + * + * This was hard to duplicate, but this behavior stopped as soon as this fix was applied. This issue might also + * only be caused in certain development environments where the usage of a hot module reloader is causing + * errors. + */ + if (from === undefined && startingUrl && startingUrl.indexOf(to) !== -1) { + startingUrl = undefined; + return; + } + + if (from !== to) { + startingUrl = undefined; + if (activeTransaction) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] Finishing current transaction with op: ${activeTransaction.op}`); + // If there's an open transaction on the scope, we need to finish it before creating an new one. + activeTransaction.finish(); + } + activeTransaction = customStartTransaction({ + name: WINDOW.location.pathname, + op: 'navigation', + metadata: { source: 'url' }, + }); + } + }); + } +} + +export { instrumentRoutingWithDefaults }; +//# sourceMappingURL=router.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/types.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/types.js new file mode 100644 index 0000000..b01c87e --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/types.js @@ -0,0 +1,6 @@ +import { GLOBAL_OBJ } from '@sentry/utils'; + +const WINDOW = GLOBAL_OBJ ; + +export { WINDOW }; +//# sourceMappingURL=types.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getCLS.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getCLS.js new file mode 100644 index 0000000..22f1922 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getCLS.js @@ -0,0 +1,105 @@ +import { bindReporter } from './lib/bindReporter.js'; +import { initMetric } from './lib/initMetric.js'; +import { observe } from './lib/observe.js'; +import { onHidden } from './lib/onHidden.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Calculates the [CLS](https://web.dev/cls/) value for the current page and + * calls the `callback` function once the value is ready to be reported, along + * with all `layout-shift` performance entries that were used in the metric + * value calculation. The reported value is a `double` (corresponding to a + * [layout shift score](https://web.dev/cls/#layout-shift-score)). + * + * If the `reportAllChanges` configuration option is set to `true`, the + * `callback` function will be called as soon as the value is initially + * determined as well as any time the value changes throughout the page + * lifespan. + * + * _**Important:** CLS should be continually monitored for changes throughout + * the entire lifespan of a page—including if the user returns to the page after + * it's been hidden/backgrounded. However, since browsers often [will not fire + * additional callbacks once the user has backgrounded a + * page](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden), + * `callback` is always called when the page's visibility state changes to + * hidden. As a result, the `callback` function might be called multiple times + * during the same page load._ + */ +const onCLS = (onReport) => { + const metric = initMetric('CLS', 0); + let report; + + let sessionValue = 0; + let sessionEntries = []; + + // const handleEntries = (entries: Metric['entries']) => { + const handleEntries = (entries) => { + entries.forEach(entry => { + // Only count layout shifts without recent user input. + if (!entry.hadRecentInput) { + const firstSessionEntry = sessionEntries[0]; + const lastSessionEntry = sessionEntries[sessionEntries.length - 1]; + + // If the entry occurred less than 1 second after the previous entry and + // less than 5 seconds after the first entry in the session, include the + // entry in the current session. Otherwise, start a new session. + if ( + sessionValue && + sessionEntries.length !== 0 && + entry.startTime - lastSessionEntry.startTime < 1000 && + entry.startTime - firstSessionEntry.startTime < 5000 + ) { + sessionValue += entry.value; + sessionEntries.push(entry); + } else { + sessionValue = entry.value; + sessionEntries = [entry]; + } + + // If the current session value is larger than the current CLS value, + // update CLS and the entries contributing to it. + if (sessionValue > metric.value) { + metric.value = sessionValue; + metric.entries = sessionEntries; + if (report) { + report(); + } + } + } + }); + }; + + const po = observe('layout-shift', handleEntries); + if (po) { + report = bindReporter(onReport, metric); + + const stopListening = () => { + handleEntries(po.takeRecords() ); + report(true); + }; + + onHidden(stopListening); + + return stopListening; + } + + return; +}; + +export { onCLS }; +//# sourceMappingURL=getCLS.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getFID.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getFID.js new file mode 100644 index 0000000..fc135c8 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getFID.js @@ -0,0 +1,63 @@ +import { bindReporter } from './lib/bindReporter.js'; +import { getVisibilityWatcher } from './lib/getVisibilityWatcher.js'; +import { initMetric } from './lib/initMetric.js'; +import { observe } from './lib/observe.js'; +import { onHidden } from './lib/onHidden.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Calculates the [FID](https://web.dev/fid/) value for the current page and + * calls the `callback` function once the value is ready, along with the + * relevant `first-input` performance entry used to determine the value. The + * reported value is a `DOMHighResTimeStamp`. + * + * _**Important:** since FID is only reported after the user interacts with the + * page, it's possible that it will not be reported for some page loads._ + */ +const onFID = (onReport) => { + const visibilityWatcher = getVisibilityWatcher(); + const metric = initMetric('FID'); + // eslint-disable-next-line prefer-const + let report; + + const handleEntry = (entry) => { + // Only report if the page wasn't hidden prior to the first input. + if (entry.startTime < visibilityWatcher.firstHiddenTime) { + metric.value = entry.processingStart - entry.startTime; + metric.entries.push(entry); + report(true); + } + }; + + const handleEntries = (entries) => { + (entries ).forEach(handleEntry); + }; + + const po = observe('first-input', handleEntries); + report = bindReporter(onReport, metric); + + if (po) { + onHidden(() => { + handleEntries(po.takeRecords() ); + po.disconnect(); + }, true); + } +}; + +export { onFID }; +//# sourceMappingURL=getFID.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js new file mode 100644 index 0000000..0d3c610 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/getLCP.js @@ -0,0 +1,85 @@ +import { bindReporter } from './lib/bindReporter.js'; +import { getActivationStart } from './lib/getActivationStart.js'; +import { getVisibilityWatcher } from './lib/getVisibilityWatcher.js'; +import { initMetric } from './lib/initMetric.js'; +import { observe } from './lib/observe.js'; +import { onHidden } from './lib/onHidden.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const reportedMetricIDs = {}; + +/** + * Calculates the [LCP](https://web.dev/lcp/) value for the current page and + * calls the `callback` function once the value is ready (along with the + * relevant `largest-contentful-paint` performance entry used to determine the + * value). The reported value is a `DOMHighResTimeStamp`. + */ +const onLCP = (onReport) => { + const visibilityWatcher = getVisibilityWatcher(); + const metric = initMetric('LCP'); + let report; + + const handleEntries = (entries) => { + const lastEntry = entries[entries.length - 1] ; + if (lastEntry) { + // The startTime attribute returns the value of the renderTime if it is + // not 0, and the value of the loadTime otherwise. The activationStart + // reference is used because LCP should be relative to page activation + // rather than navigation start if the page was prerendered. + const value = Math.max(lastEntry.startTime - getActivationStart(), 0); + + // Only report if the page wasn't hidden prior to LCP. + if (value < visibilityWatcher.firstHiddenTime) { + metric.value = value; + metric.entries = [lastEntry]; + report(); + } + } + }; + + const po = observe('largest-contentful-paint', handleEntries); + + if (po) { + report = bindReporter(onReport, metric); + + const stopListening = () => { + if (!reportedMetricIDs[metric.id]) { + handleEntries(po.takeRecords() ); + po.disconnect(); + reportedMetricIDs[metric.id] = true; + report(true); + } + }; + + // Stop listening after input. Note: while scrolling is an input that + // stop LCP observation, it's unreliable since it can be programmatically + // generated. See: https://github.com/GoogleChrome/web-vitals/issues/75 + ['keydown', 'click'].forEach(type => { + addEventListener(type, stopListening, { once: true, capture: true }); + }); + + onHidden(stopListening, true); + + return stopListening; + } + + return; +}; + +export { onLCP }; +//# sourceMappingURL=getLCP.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/bindReporter.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/bindReporter.js new file mode 100644 index 0000000..dc66278 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/bindReporter.js @@ -0,0 +1,28 @@ +const bindReporter = ( + callback, + metric, + reportAllChanges, +) => { + let prevValue; + let delta; + return (forceReport) => { + if (metric.value >= 0) { + if (forceReport || reportAllChanges) { + delta = metric.value - (prevValue || 0); + + // Report the metric if there's a non-zero delta or if no previous + // value exists (which can happen in the case of the document becoming + // hidden when the metric value is 0). + // See: https://github.com/GoogleChrome/web-vitals/issues/14 + if (delta || prevValue === undefined) { + prevValue = metric.value; + metric.delta = delta; + callback(metric); + } + } + } + }; +}; + +export { bindReporter }; +//# sourceMappingURL=bindReporter.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/generateUniqueID.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/generateUniqueID.js new file mode 100644 index 0000000..dfde3bb --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/generateUniqueID.js @@ -0,0 +1,27 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Performantly generate a unique, 30-char string by combining a version + * number, the current timestamp with a 13-digit number integer. + * @return {string} + */ +const generateUniqueID = () => { + return `v3-${Date.now()}-${Math.floor(Math.random() * (9e12 - 1)) + 1e12}`; +}; + +export { generateUniqueID }; +//# sourceMappingURL=generateUniqueID.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getActivationStart.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getActivationStart.js new file mode 100644 index 0000000..e7b7f65 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getActivationStart.js @@ -0,0 +1,25 @@ +import { getNavigationEntry } from './getNavigationEntry.js'; + +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const getActivationStart = () => { + const navEntry = getNavigationEntry(); + return (navEntry && navEntry.activationStart) || 0; +}; + +export { getActivationStart }; +//# sourceMappingURL=getActivationStart.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getNavigationEntry.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getNavigationEntry.js new file mode 100644 index 0000000..89a65a5 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getNavigationEntry.js @@ -0,0 +1,53 @@ +import { WINDOW } from '../../types.js'; + +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const getNavigationEntryFromPerformanceTiming = () => { + // eslint-disable-next-line deprecation/deprecation + const timing = WINDOW.performance.timing; + // eslint-disable-next-line deprecation/deprecation + const type = WINDOW.performance.navigation.type; + + const navigationEntry = { + entryType: 'navigation', + startTime: 0, + type: type == 2 ? 'back_forward' : type === 1 ? 'reload' : 'navigate', + }; + + for (const key in timing) { + if (key !== 'navigationStart' && key !== 'toJSON') { + // eslint-disable-next-line deprecation/deprecation + navigationEntry[key] = Math.max((timing[key ] ) - timing.navigationStart, 0); + } + } + return navigationEntry ; +}; + +const getNavigationEntry = () => { + if (WINDOW.__WEB_VITALS_POLYFILL__) { + return ( + WINDOW.performance && + ((performance.getEntriesByType && performance.getEntriesByType('navigation')[0]) || + getNavigationEntryFromPerformanceTiming()) + ); + } else { + return WINDOW.performance && performance.getEntriesByType && performance.getEntriesByType('navigation')[0]; + } +}; + +export { getNavigationEntry }; +//# sourceMappingURL=getNavigationEntry.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getVisibilityWatcher.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getVisibilityWatcher.js new file mode 100644 index 0000000..fb86cc2 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/getVisibilityWatcher.js @@ -0,0 +1,54 @@ +import { WINDOW } from '../../types.js'; +import { onHidden } from './onHidden.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +let firstHiddenTime = -1; + +const initHiddenTime = () => { + // If the document is hidden and not prerendering, assume it was always + // hidden and the page was loaded in the background. + return WINDOW.document.visibilityState === 'hidden' && !WINDOW.document.prerendering ? 0 : Infinity; +}; + +const trackChanges = () => { + // Update the time if/when the document becomes hidden. + onHidden(({ timeStamp }) => { + firstHiddenTime = timeStamp; + }, true); +}; + +const getVisibilityWatcher = ( + +) => { + if (firstHiddenTime < 0) { + // If the document is hidden when this code runs, assume it was hidden + // since navigation start. This isn't a perfect heuristic, but it's the + // best we can do until an API is available to support querying past + // visibilityState. + firstHiddenTime = initHiddenTime(); + trackChanges(); + } + return { + get firstHiddenTime() { + return firstHiddenTime; + }, + }; +}; + +export { getVisibilityWatcher }; +//# sourceMappingURL=getVisibilityWatcher.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/initMetric.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/initMetric.js new file mode 100644 index 0000000..498c63d --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/initMetric.js @@ -0,0 +1,46 @@ +import { WINDOW } from '../../types.js'; +import { generateUniqueID } from './generateUniqueID.js'; +import { getActivationStart } from './getActivationStart.js'; +import { getNavigationEntry } from './getNavigationEntry.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const initMetric = (name, value) => { + const navEntry = getNavigationEntry(); + let navigationType = 'navigate'; + + if (navEntry) { + if (WINDOW.document.prerendering || getActivationStart() > 0) { + navigationType = 'prerender'; + } else { + navigationType = navEntry.type.replace(/_/g, '-') ; + } + } + + return { + name, + value: typeof value === 'undefined' ? -1 : value, + rating: 'good', // Will be updated if the value changes. + delta: 0, + entries: [], + id: generateUniqueID(), + navigationType, + }; +}; + +export { initMetric }; +//# sourceMappingURL=initMetric.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/observe.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/observe.js new file mode 100644 index 0000000..94b7351 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/observe.js @@ -0,0 +1,37 @@ +/** + * Takes a performance entry type and a callback function, and creates a + * `PerformanceObserver` instance that will observe the specified entry type + * with buffering enabled and call the callback _for each entry_. + * + * This function also feature-detects entry support and wraps the logic in a + * try/catch to avoid errors in unsupporting browsers. + */ +const observe = ( + type, + callback, + opts, +) => { + try { + if (PerformanceObserver.supportedEntryTypes.includes(type)) { + const po = new PerformanceObserver(list => { + callback(list.getEntries() ); + }); + po.observe( + Object.assign( + { + type, + buffered: true, + }, + opts || {}, + ) , + ); + return po; + } + } catch (e) { + // Do nothing. + } + return; +}; + +export { observe }; +//# sourceMappingURL=observe.js.map diff --git a/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/onHidden.js b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/onHidden.js new file mode 100644 index 0000000..78bb128 --- /dev/null +++ b/shared/logger/node_modules/@sentry-internal/tracing/esm/browser/web-vitals/lib/onHidden.js @@ -0,0 +1,36 @@ +import { WINDOW } from '../../types.js'; + +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const onHidden = (cb, once) => { + const onHiddenOrPageHide = (event) => { + if (event.type === 'pagehide' || WINDOW.document.visibilityState === 'hidden') { + cb(event); + if (once) { + removeEventListener('visibilitychange', onHiddenOrPageHide, true); + removeEventListener('pagehide', onHiddenOrPageHide, true); + } + } + }; + addEventListener('visibilitychange', onHiddenOrPageHide, true); + // Some browsers have buggy implementations of visibilitychange, + // so we use pagehide in addition, just to be safe. + addEventListener('pagehide', onHiddenOrPageHide, true); +}; + +export { onHidden }; +//# sourceMappingURL=onHidden.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/client.js b/shared/logger/node_modules/@sentry/browser/esm/client.js new file mode 100644 index 0000000..a171d99 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/client.js @@ -0,0 +1,139 @@ +import { BaseClient, SDK_VERSION } from '@sentry/core'; +import { getSDKSource, logger, createClientReportEnvelope, dsnToString } from '@sentry/utils'; +import { eventFromException, eventFromMessage } from './eventbuilder.js'; +import { WINDOW } from './helpers.js'; +import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs.js'; +import { createUserFeedbackEnvelope } from './userfeedback.js'; + +/** + * Configuration options for the Sentry Browser SDK. + * @see @sentry/types Options for more information. + */ + +/** + * The Sentry Browser SDK Client. + * + * @see BrowserOptions for documentation on configuration options. + * @see SentryClient for usage documentation. + */ +class BrowserClient extends BaseClient { + /** + * Creates a new Browser SDK instance. + * + * @param options Configuration options for this SDK. + */ + constructor(options) { + const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource(); + + options._metadata = options._metadata || {}; + options._metadata.sdk = options._metadata.sdk || { + name: 'sentry.javascript.browser', + packages: [ + { + name: `${sdkSource}:@sentry/browser`, + version: SDK_VERSION, + }, + ], + version: SDK_VERSION, + }; + + super(options); + + if (options.sendClientReports && WINDOW.document) { + WINDOW.document.addEventListener('visibilitychange', () => { + if (WINDOW.document.visibilityState === 'hidden') { + this._flushOutcomes(); + } + }); + } + } + + /** + * @inheritDoc + */ + eventFromException(exception, hint) { + return eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace); + } + + /** + * @inheritDoc + */ + eventFromMessage( + message, + // eslint-disable-next-line deprecation/deprecation + level = 'info', + hint, + ) { + return eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace); + } + + /** + * @inheritDoc + */ + sendEvent(event, hint) { + // We only want to add the sentry event breadcrumb when the user has the breadcrumb integration installed and + // activated its `sentry` option. + // We also do not want to use the `Breadcrumbs` class here directly, because we do not want it to be included in + // bundles, if it is not used by the SDK. + // This all sadly is a bit ugly, but we currently don't have a "pre-send" hook on the integrations so we do it this + // way for now. + const breadcrumbIntegration = this.getIntegrationById(BREADCRUMB_INTEGRATION_ID) ; + // We check for definedness of `addSentryBreadcrumb` in case users provided their own integration with id + // "Breadcrumbs" that does not have this function. + if (breadcrumbIntegration && breadcrumbIntegration.addSentryBreadcrumb) { + breadcrumbIntegration.addSentryBreadcrumb(event); + } + + super.sendEvent(event, hint); + } + + /** + * Sends user feedback to Sentry. + */ + captureUserFeedback(feedback) { + if (!this._isEnabled()) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('SDK not enabled, will not capture user feedback.'); + return; + } + + const envelope = createUserFeedbackEnvelope(feedback, { + metadata: this.getSdkMetadata(), + dsn: this.getDsn(), + tunnel: this.getOptions().tunnel, + }); + void this._sendEnvelope(envelope); + } + + /** + * @inheritDoc + */ + _prepareEvent(event, hint, scope) { + event.platform = event.platform || 'javascript'; + return super._prepareEvent(event, hint, scope); + } + + /** + * Sends client reports as an envelope. + */ + _flushOutcomes() { + const outcomes = this._clearOutcomes(); + + if (outcomes.length === 0) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('No outcomes to send'); + return; + } + + if (!this._dsn) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('No dsn provided, will not send outcomes'); + return; + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('Sending outcomes:', outcomes); + + const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn)); + void this._sendEnvelope(envelope); + } +} + +export { BrowserClient }; +//# sourceMappingURL=client.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/eventbuilder.js b/shared/logger/node_modules/@sentry/browser/esm/eventbuilder.js new file mode 100644 index 0000000..08e5653 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/eventbuilder.js @@ -0,0 +1,304 @@ +import { getCurrentHub } from '@sentry/core'; +import { addExceptionMechanism, resolvedSyncPromise, isErrorEvent, isDOMError, isDOMException, addExceptionTypeValue, isError, isPlainObject, isEvent, normalizeToSize, extractExceptionKeysForMessage } from '@sentry/utils'; + +/** + * This function creates an exception from a JavaScript Error + */ +function exceptionFromError(stackParser, ex) { + // Get the frames first since Opera can lose the stack if we touch anything else first + const frames = parseStackFrames(stackParser, ex); + + const exception = { + type: ex && ex.name, + value: extractMessage(ex), + }; + + if (frames.length) { + exception.stacktrace = { frames }; + } + + if (exception.type === undefined && exception.value === '') { + exception.value = 'Unrecoverable error caught'; + } + + return exception; +} + +/** + * @hidden + */ +function eventFromPlainObject( + stackParser, + exception, + syntheticException, + isUnhandledRejection, +) { + const hub = getCurrentHub(); + const client = hub.getClient(); + const normalizeDepth = client && client.getOptions().normalizeDepth; + + const event = { + exception: { + values: [ + { + type: isEvent(exception) ? exception.constructor.name : isUnhandledRejection ? 'UnhandledRejection' : 'Error', + value: getNonErrorObjectExceptionValue(exception, { isUnhandledRejection }), + }, + ], + }, + extra: { + __serialized__: normalizeToSize(exception, normalizeDepth), + }, + }; + + if (syntheticException) { + const frames = parseStackFrames(stackParser, syntheticException); + if (frames.length) { + // event.exception.values[0] has been set above + (event.exception ).values[0].stacktrace = { frames }; + } + } + + return event; +} + +/** + * @hidden + */ +function eventFromError(stackParser, ex) { + return { + exception: { + values: [exceptionFromError(stackParser, ex)], + }, + }; +} + +/** Parses stack frames from an error */ +function parseStackFrames( + stackParser, + ex, +) { + // Access and store the stacktrace property before doing ANYTHING + // else to it because Opera is not very good at providing it + // reliably in other circumstances. + const stacktrace = ex.stacktrace || ex.stack || ''; + + const popSize = getPopSize(ex); + + try { + return stackParser(stacktrace, popSize); + } catch (e) { + // no-empty + } + + return []; +} + +// Based on our own mapping pattern - https://github.com/getsentry/sentry/blob/9f08305e09866c8bd6d0c24f5b0aabdd7dd6c59c/src/sentry/lang/javascript/errormapping.py#L83-L108 +const reactMinifiedRegexp = /Minified React error #\d+;/i; + +function getPopSize(ex) { + if (ex) { + if (typeof ex.framesToPop === 'number') { + return ex.framesToPop; + } + + if (reactMinifiedRegexp.test(ex.message)) { + return 1; + } + } + + return 0; +} + +/** + * There are cases where stacktrace.message is an Event object + * https://github.com/getsentry/sentry-javascript/issues/1949 + * In this specific case we try to extract stacktrace.message.error.message + */ +function extractMessage(ex) { + const message = ex && ex.message; + if (!message) { + return 'No error message'; + } + if (message.error && typeof message.error.message === 'string') { + return message.error.message; + } + return message; +} + +/** + * Creates an {@link Event} from all inputs to `captureException` and non-primitive inputs to `captureMessage`. + * @hidden + */ +function eventFromException( + stackParser, + exception, + hint, + attachStacktrace, +) { + const syntheticException = (hint && hint.syntheticException) || undefined; + const event = eventFromUnknownInput(stackParser, exception, syntheticException, attachStacktrace); + addExceptionMechanism(event); // defaults to { type: 'generic', handled: true } + event.level = 'error'; + if (hint && hint.event_id) { + event.event_id = hint.event_id; + } + return resolvedSyncPromise(event); +} + +/** + * Builds and Event from a Message + * @hidden + */ +function eventFromMessage( + stackParser, + message, + // eslint-disable-next-line deprecation/deprecation + level = 'info', + hint, + attachStacktrace, +) { + const syntheticException = (hint && hint.syntheticException) || undefined; + const event = eventFromString(stackParser, message, syntheticException, attachStacktrace); + event.level = level; + if (hint && hint.event_id) { + event.event_id = hint.event_id; + } + return resolvedSyncPromise(event); +} + +/** + * @hidden + */ +function eventFromUnknownInput( + stackParser, + exception, + syntheticException, + attachStacktrace, + isUnhandledRejection, +) { + let event; + + if (isErrorEvent(exception ) && (exception ).error) { + // If it is an ErrorEvent with `error` property, extract it to get actual Error + const errorEvent = exception ; + return eventFromError(stackParser, errorEvent.error ); + } + + // If it is a `DOMError` (which is a legacy API, but still supported in some browsers) then we just extract the name + // and message, as it doesn't provide anything else. According to the spec, all `DOMExceptions` should also be + // `Error`s, but that's not the case in IE11, so in that case we treat it the same as we do a `DOMError`. + // + // https://developer.mozilla.org/en-US/docs/Web/API/DOMError + // https://developer.mozilla.org/en-US/docs/Web/API/DOMException + // https://webidl.spec.whatwg.org/#es-DOMException-specialness + if (isDOMError(exception) || isDOMException(exception )) { + const domException = exception ; + + if ('stack' in (exception )) { + event = eventFromError(stackParser, exception ); + } else { + const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException'); + const message = domException.message ? `${name}: ${domException.message}` : name; + event = eventFromString(stackParser, message, syntheticException, attachStacktrace); + addExceptionTypeValue(event, message); + } + if ('code' in domException) { + // eslint-disable-next-line deprecation/deprecation + event.tags = { ...event.tags, 'DOMException.code': `${domException.code}` }; + } + + return event; + } + if (isError(exception)) { + // we have a real Error object, do nothing + return eventFromError(stackParser, exception); + } + if (isPlainObject(exception) || isEvent(exception)) { + // If it's a plain object or an instance of `Event` (the built-in JS kind, not this SDK's `Event` type), serialize + // it manually. This will allow us to group events based on top-level keys which is much better than creating a new + // group on any key/value change. + const objectException = exception ; + event = eventFromPlainObject(stackParser, objectException, syntheticException, isUnhandledRejection); + addExceptionMechanism(event, { + synthetic: true, + }); + return event; + } + + // If none of previous checks were valid, then it means that it's not: + // - an instance of DOMError + // - an instance of DOMException + // - an instance of Event + // - an instance of Error + // - a valid ErrorEvent (one with an error property) + // - a plain Object + // + // So bail out and capture it as a simple message: + event = eventFromString(stackParser, exception , syntheticException, attachStacktrace); + addExceptionTypeValue(event, `${exception}`, undefined); + addExceptionMechanism(event, { + synthetic: true, + }); + + return event; +} + +/** + * @hidden + */ +function eventFromString( + stackParser, + input, + syntheticException, + attachStacktrace, +) { + const event = { + message: input, + }; + + if (attachStacktrace && syntheticException) { + const frames = parseStackFrames(stackParser, syntheticException); + if (frames.length) { + event.exception = { + values: [{ value: input, stacktrace: { frames } }], + }; + } + } + + return event; +} + +function getNonErrorObjectExceptionValue( + exception, + { isUnhandledRejection }, +) { + const keys = extractExceptionKeysForMessage(exception); + const captureType = isUnhandledRejection ? 'promise rejection' : 'exception'; + + // Some ErrorEvent instances do not have an `error` property, which is why they are not handled before + // We still want to try to get a decent message for these cases + if (isErrorEvent(exception)) { + return `Event \`ErrorEvent\` captured as ${captureType} with message \`${exception.message}\``; + } + + if (isEvent(exception)) { + const className = getObjectClassName(exception); + return `Event \`${className}\` (type=${exception.type}) captured as ${captureType}`; + } + + return `Object captured as ${captureType} with keys: ${keys}`; +} + +function getObjectClassName(obj) { + try { + const prototype = Object.getPrototypeOf(obj); + return prototype ? prototype.constructor.name : undefined; + } catch (e) { + // ignore errors here + } +} + +export { eventFromError, eventFromException, eventFromMessage, eventFromPlainObject, eventFromString, eventFromUnknownInput, exceptionFromError, parseStackFrames }; +//# sourceMappingURL=eventbuilder.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/helpers.js b/shared/logger/node_modules/@sentry/browser/esm/helpers.js new file mode 100644 index 0000000..cfa1229 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/helpers.js @@ -0,0 +1,154 @@ +import { withScope, captureException } from '@sentry/core'; +import { GLOBAL_OBJ, getOriginalFunction, markFunctionWrapped, addNonEnumerableProperty, addExceptionTypeValue, addExceptionMechanism } from '@sentry/utils'; + +const WINDOW = GLOBAL_OBJ ; + +let ignoreOnError = 0; + +/** + * @hidden + */ +function shouldIgnoreOnError() { + return ignoreOnError > 0; +} + +/** + * @hidden + */ +function ignoreNextOnError() { + // onerror should trigger before setTimeout + ignoreOnError++; + setTimeout(() => { + ignoreOnError--; + }); +} + +/** + * Instruments the given function and sends an event to Sentry every time the + * function throws an exception. + * + * @param fn A function to wrap. It is generally safe to pass an unbound function, because the returned wrapper always + * has a correct `this` context. + * @returns The wrapped function. + * @hidden + */ +function wrap( + fn, + options + + = {}, + before, + // eslint-disable-next-line @typescript-eslint/no-explicit-any +) { + // for future readers what this does is wrap a function and then create + // a bi-directional wrapping between them. + // + // example: wrapped = wrap(original); + // original.__sentry_wrapped__ -> wrapped + // wrapped.__sentry_original__ -> original + + if (typeof fn !== 'function') { + return fn; + } + + try { + // if we're dealing with a function that was previously wrapped, return + // the original wrapper. + const wrapper = fn.__sentry_wrapped__; + if (wrapper) { + return wrapper; + } + + // We don't wanna wrap it twice + if (getOriginalFunction(fn)) { + return fn; + } + } catch (e) { + // Just accessing custom props in some Selenium environments + // can cause a "Permission denied" exception (see raven-js#495). + // Bail on wrapping and return the function as-is (defers to window.onerror). + return fn; + } + + /* eslint-disable prefer-rest-params */ + // It is important that `sentryWrapped` is not an arrow function to preserve the context of `this` + const sentryWrapped = function () { + const args = Array.prototype.slice.call(arguments); + + try { + if (before && typeof before === 'function') { + before.apply(this, arguments); + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access + const wrappedArguments = args.map((arg) => wrap(arg, options)); + + // Attempt to invoke user-land function + // NOTE: If you are a Sentry user, and you are seeing this stack frame, it + // means the sentry.javascript SDK caught an error invoking your application code. This + // is expected behavior and NOT indicative of a bug with sentry.javascript. + return fn.apply(this, wrappedArguments); + } catch (ex) { + ignoreNextOnError(); + + withScope((scope) => { + scope.addEventProcessor((event) => { + if (options.mechanism) { + addExceptionTypeValue(event, undefined, undefined); + addExceptionMechanism(event, options.mechanism); + } + + event.extra = { + ...event.extra, + arguments: args, + }; + + return event; + }); + + captureException(ex); + }); + + throw ex; + } + }; + /* eslint-enable prefer-rest-params */ + + // Accessing some objects may throw + // ref: https://github.com/getsentry/sentry-javascript/issues/1168 + try { + for (const property in fn) { + if (Object.prototype.hasOwnProperty.call(fn, property)) { + sentryWrapped[property] = fn[property]; + } + } + } catch (_oO) {} // eslint-disable-line no-empty + + // Signal that this function has been wrapped/filled already + // for both debugging and to prevent it to being wrapped/filled twice + markFunctionWrapped(sentryWrapped, fn); + + addNonEnumerableProperty(fn, '__sentry_wrapped__', sentryWrapped); + + // Restore original function name (not all browsers allow that) + try { + const descriptor = Object.getOwnPropertyDescriptor(sentryWrapped, 'name') ; + if (descriptor.configurable) { + Object.defineProperty(sentryWrapped, 'name', { + get() { + return fn.name; + }, + }); + } + // eslint-disable-next-line no-empty + } catch (_oO) {} + + return sentryWrapped; +} + +/** + * All properties the report dialog supports + */ + +export { WINDOW, ignoreNextOnError, shouldIgnoreOnError, wrap }; +//# sourceMappingURL=helpers.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/index.js b/shared/logger/node_modules/@sentry/browser/esm/index.js new file mode 100644 index 0000000..188f9c0 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/index.js @@ -0,0 +1,39 @@ +import { Integrations } from '@sentry/core'; +export { FunctionToString, Hub, InboundFilters, SDK_VERSION, Scope, addBreadcrumb, addGlobalEventProcessor, addTracingExtensions, captureEvent, captureException, captureMessage, configureScope, createTransport, extractTraceparentData, getActiveTransaction, getCurrentHub, getHubFromCarrier, makeMain, makeMultiplexedTransport, setContext, setExtra, setExtras, setTag, setTags, setUser, spanStatusfromHttpCode, startTransaction, trace, withScope } from '@sentry/core'; +import { WINDOW } from './helpers.js'; +export { WINDOW } from './helpers.js'; +export { BrowserClient } from './client.js'; +export { makeFetchTransport } from './transports/fetch.js'; +export { makeXHRTransport } from './transports/xhr.js'; +export { chromeStackLineParser, defaultStackLineParsers, defaultStackParser, geckoStackLineParser, opera10StackLineParser, opera11StackLineParser, winjsStackLineParser } from './stack-parsers.js'; +export { eventFromException, eventFromMessage } from './eventbuilder.js'; +export { createUserFeedbackEnvelope } from './userfeedback.js'; +export { captureUserFeedback, close, defaultIntegrations, flush, forceLoad, init, lastEventId, onLoad, showReportDialog, wrap } from './sdk.js'; +import * as index from './integrations/index.js'; +export { Replay } from '@sentry/replay'; +export { BrowserTracing, defaultRequestInstrumentationOptions, instrumentOutgoingRequests } from '@sentry-internal/tracing'; +export { makeBrowserOfflineTransport } from './transports/offline.js'; +export { onProfilingStartRouteTransaction } from './profiling/hubextensions.js'; +export { BrowserProfilingIntegration } from './profiling/integration.js'; +export { GlobalHandlers } from './integrations/globalhandlers.js'; +export { TryCatch } from './integrations/trycatch.js'; +export { Breadcrumbs } from './integrations/breadcrumbs.js'; +export { LinkedErrors } from './integrations/linkederrors.js'; +export { HttpContext } from './integrations/httpcontext.js'; +export { Dedupe } from './integrations/dedupe.js'; + +let windowIntegrations = {}; + +// This block is needed to add compatibility with the integrations packages when used with a CDN +if (WINDOW.Sentry && WINDOW.Sentry.Integrations) { + windowIntegrations = WINDOW.Sentry.Integrations; +} + +const INTEGRATIONS = { + ...windowIntegrations, + ...Integrations, + ...index, +}; + +export { INTEGRATIONS as Integrations }; +//# sourceMappingURL=index.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/integrations/breadcrumbs.js b/shared/logger/node_modules/@sentry/browser/esm/integrations/breadcrumbs.js new file mode 100644 index 0000000..4117cc4 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/integrations/breadcrumbs.js @@ -0,0 +1,320 @@ +import { getCurrentHub } from '@sentry/core'; +import { addInstrumentationHandler, getEventDescription, severityLevelFromString, safeJoin, SENTRY_XHR_DATA_KEY, parseUrl, logger, htmlTreeAsString } from '@sentry/utils'; +import { WINDOW } from '../helpers.js'; + +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ + +/** maxStringLength gets capped to prevent 100 breadcrumbs exceeding 1MB event payload size */ +const MAX_ALLOWED_STRING_LENGTH = 1024; + +const BREADCRUMB_INTEGRATION_ID = 'Breadcrumbs'; + +/** + * Default Breadcrumbs instrumentations + * TODO: Deprecated - with v6, this will be renamed to `Instrument` + */ +class Breadcrumbs { + /** + * @inheritDoc + */ + static __initStatic() {this.id = BREADCRUMB_INTEGRATION_ID;} + + /** + * @inheritDoc + */ + __init() {this.name = Breadcrumbs.id;} + + /** + * Options of the breadcrumbs integration. + */ + // This field is public, because we use it in the browser client to check if the `sentry` option is enabled. + + /** + * @inheritDoc + */ + constructor(options) {Breadcrumbs.prototype.__init.call(this); + this.options = { + console: true, + dom: true, + fetch: true, + history: true, + sentry: true, + xhr: true, + ...options, + }; + } + + /** + * Instrument browser built-ins w/ breadcrumb capturing + * - Console API + * - DOM API (click/typing) + * - XMLHttpRequest API + * - Fetch API + * - History API + */ + setupOnce() { + if (this.options.console) { + addInstrumentationHandler('console', _consoleBreadcrumb); + } + if (this.options.dom) { + addInstrumentationHandler('dom', _domBreadcrumb(this.options.dom)); + } + if (this.options.xhr) { + addInstrumentationHandler('xhr', _xhrBreadcrumb); + } + if (this.options.fetch) { + addInstrumentationHandler('fetch', _fetchBreadcrumb); + } + if (this.options.history) { + addInstrumentationHandler('history', _historyBreadcrumb); + } + } + + /** + * Adds a breadcrumb for Sentry events or transactions if this option is enabled. + */ + addSentryBreadcrumb(event) { + if (this.options.sentry) { + getCurrentHub().addBreadcrumb( + { + category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`, + event_id: event.event_id, + level: event.level, + message: getEventDescription(event), + }, + { + event, + }, + ); + } + } +} Breadcrumbs.__initStatic(); + +/** + * A HOC that creaes a function that creates breadcrumbs from DOM API calls. + * This is a HOC so that we get access to dom options in the closure. + */ +function _domBreadcrumb(dom) { + function _innerDomBreadcrumb(handlerData) { + let target; + let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined; + + let maxStringLength = + typeof dom === 'object' && typeof dom.maxStringLength === 'number' ? dom.maxStringLength : undefined; + if (maxStringLength && maxStringLength > MAX_ALLOWED_STRING_LENGTH) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `\`dom.maxStringLength\` cannot exceed ${MAX_ALLOWED_STRING_LENGTH}, but a value of ${maxStringLength} was configured. Sentry will use ${MAX_ALLOWED_STRING_LENGTH} instead.`, + ); + maxStringLength = MAX_ALLOWED_STRING_LENGTH; + } + + if (typeof keyAttrs === 'string') { + keyAttrs = [keyAttrs]; + } + + // Accessing event.target can throw (see getsentry/raven-js#838, #768) + try { + const event = handlerData.event ; + target = _isEvent(event) + ? htmlTreeAsString(event.target, { keyAttrs, maxStringLength }) + : htmlTreeAsString(event, { keyAttrs, maxStringLength }); + } catch (e) { + target = '<unknown>'; + } + + if (target.length === 0) { + return; + } + + getCurrentHub().addBreadcrumb( + { + category: `ui.${handlerData.name}`, + message: target, + }, + { + event: handlerData.event, + name: handlerData.name, + global: handlerData.global, + }, + ); + } + + return _innerDomBreadcrumb; +} + +/** + * Creates breadcrumbs from console API calls + */ +function _consoleBreadcrumb(handlerData) { + // This is a hack to fix a Vue3-specific bug that causes an infinite loop of + // console warnings. This happens when a Vue template is rendered with + // an undeclared variable, which we try to stringify, ultimately causing + // Vue to issue another warning which repeats indefinitely. + // see: https://github.com/getsentry/sentry-javascript/pull/6010 + // see: https://github.com/getsentry/sentry-javascript/issues/5916 + for (let i = 0; i < handlerData.args.length; i++) { + if (handlerData.args[i] === 'ref=Ref<') { + handlerData.args[i + 1] = 'viewRef'; + break; + } + } + const breadcrumb = { + category: 'console', + data: { + arguments: handlerData.args, + logger: 'console', + }, + level: severityLevelFromString(handlerData.level), + message: safeJoin(handlerData.args, ' '), + }; + + if (handlerData.level === 'assert') { + if (handlerData.args[0] === false) { + breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`; + breadcrumb.data.arguments = handlerData.args.slice(1); + } else { + // Don't capture a breadcrumb for passed assertions + return; + } + } + + getCurrentHub().addBreadcrumb(breadcrumb, { + input: handlerData.args, + level: handlerData.level, + }); +} + +/** + * Creates breadcrumbs from XHR API calls + */ +function _xhrBreadcrumb(handlerData) { + const { startTimestamp, endTimestamp } = handlerData; + + const sentryXhrData = handlerData.xhr[SENTRY_XHR_DATA_KEY]; + + // We only capture complete, non-sentry requests + if (!startTimestamp || !endTimestamp || !sentryXhrData) { + return; + } + + const { method, url, status_code, body } = sentryXhrData; + + const data = { + method, + url, + status_code, + }; + + const hint = { + xhr: handlerData.xhr, + input: body, + startTimestamp, + endTimestamp, + }; + + getCurrentHub().addBreadcrumb( + { + category: 'xhr', + data, + type: 'http', + }, + hint, + ); +} + +/** + * Creates breadcrumbs from fetch API calls + */ +function _fetchBreadcrumb(handlerData) { + const { startTimestamp, endTimestamp } = handlerData; + + // We only capture complete fetch requests + if (!endTimestamp) { + return; + } + + if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') { + // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests) + return; + } + + if (handlerData.error) { + const data = handlerData.fetchData; + const hint = { + data: handlerData.error, + input: handlerData.args, + startTimestamp, + endTimestamp, + }; + + getCurrentHub().addBreadcrumb( + { + category: 'fetch', + data, + level: 'error', + type: 'http', + }, + hint, + ); + } else { + const data = { + ...handlerData.fetchData, + status_code: handlerData.response && handlerData.response.status, + }; + const hint = { + input: handlerData.args, + response: handlerData.response, + startTimestamp, + endTimestamp, + }; + getCurrentHub().addBreadcrumb( + { + category: 'fetch', + data, + type: 'http', + }, + hint, + ); + } +} + +/** + * Creates breadcrumbs from history API calls + */ +function _historyBreadcrumb(handlerData) { + let from = handlerData.from; + let to = handlerData.to; + const parsedLoc = parseUrl(WINDOW.location.href); + let parsedFrom = parseUrl(from); + const parsedTo = parseUrl(to); + + // Initial pushState doesn't provide `from` information + if (!parsedFrom.path) { + parsedFrom = parsedLoc; + } + + // Use only the path component of the URL if the URL matches the current + // document (almost all the time when using pushState) + if (parsedLoc.protocol === parsedTo.protocol && parsedLoc.host === parsedTo.host) { + to = parsedTo.relative; + } + if (parsedLoc.protocol === parsedFrom.protocol && parsedLoc.host === parsedFrom.host) { + from = parsedFrom.relative; + } + + getCurrentHub().addBreadcrumb({ + category: 'navigation', + data: { + from, + to, + }, + }); +} + +function _isEvent(event) { + return !!event && !!(event ).target; +} + +export { BREADCRUMB_INTEGRATION_ID, Breadcrumbs }; +//# sourceMappingURL=breadcrumbs.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/integrations/dedupe.js b/shared/logger/node_modules/@sentry/browser/esm/integrations/dedupe.js new file mode 100644 index 0000000..591a552 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/integrations/dedupe.js @@ -0,0 +1,211 @@ +import { logger } from '@sentry/utils'; + +/** Deduplication filter */ +class Dedupe {constructor() { Dedupe.prototype.__init.call(this); } + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'Dedupe';} + + /** + * @inheritDoc + */ + __init() {this.name = Dedupe.id;} + + /** + * @inheritDoc + */ + + /** + * @inheritDoc + */ + setupOnce(addGlobalEventProcessor, getCurrentHub) { + const eventProcessor = currentEvent => { + // We want to ignore any non-error type events, e.g. transactions or replays + // These should never be deduped, and also not be compared against as _previousEvent. + if (currentEvent.type) { + return currentEvent; + } + + const self = getCurrentHub().getIntegration(Dedupe); + if (self) { + // Juuust in case something goes wrong + try { + if (_shouldDropEvent(currentEvent, self._previousEvent)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Event dropped due to being a duplicate of previously captured event.'); + return null; + } + } catch (_oO) { + return (self._previousEvent = currentEvent); + } + + return (self._previousEvent = currentEvent); + } + return currentEvent; + }; + + eventProcessor.id = this.name; + addGlobalEventProcessor(eventProcessor); + } +} Dedupe.__initStatic(); + +/** JSDoc */ +function _shouldDropEvent(currentEvent, previousEvent) { + if (!previousEvent) { + return false; + } + + if (_isSameMessageEvent(currentEvent, previousEvent)) { + return true; + } + + if (_isSameExceptionEvent(currentEvent, previousEvent)) { + return true; + } + + return false; +} + +/** JSDoc */ +function _isSameMessageEvent(currentEvent, previousEvent) { + const currentMessage = currentEvent.message; + const previousMessage = previousEvent.message; + + // If neither event has a message property, they were both exceptions, so bail out + if (!currentMessage && !previousMessage) { + return false; + } + + // If only one event has a stacktrace, but not the other one, they are not the same + if ((currentMessage && !previousMessage) || (!currentMessage && previousMessage)) { + return false; + } + + if (currentMessage !== previousMessage) { + return false; + } + + if (!_isSameFingerprint(currentEvent, previousEvent)) { + return false; + } + + if (!_isSameStacktrace(currentEvent, previousEvent)) { + return false; + } + + return true; +} + +/** JSDoc */ +function _isSameExceptionEvent(currentEvent, previousEvent) { + const previousException = _getExceptionFromEvent(previousEvent); + const currentException = _getExceptionFromEvent(currentEvent); + + if (!previousException || !currentException) { + return false; + } + + if (previousException.type !== currentException.type || previousException.value !== currentException.value) { + return false; + } + + if (!_isSameFingerprint(currentEvent, previousEvent)) { + return false; + } + + if (!_isSameStacktrace(currentEvent, previousEvent)) { + return false; + } + + return true; +} + +/** JSDoc */ +function _isSameStacktrace(currentEvent, previousEvent) { + let currentFrames = _getFramesFromEvent(currentEvent); + let previousFrames = _getFramesFromEvent(previousEvent); + + // If neither event has a stacktrace, they are assumed to be the same + if (!currentFrames && !previousFrames) { + return true; + } + + // If only one event has a stacktrace, but not the other one, they are not the same + if ((currentFrames && !previousFrames) || (!currentFrames && previousFrames)) { + return false; + } + + currentFrames = currentFrames ; + previousFrames = previousFrames ; + + // If number of frames differ, they are not the same + if (previousFrames.length !== currentFrames.length) { + return false; + } + + // Otherwise, compare the two + for (let i = 0; i < previousFrames.length; i++) { + const frameA = previousFrames[i]; + const frameB = currentFrames[i]; + + if ( + frameA.filename !== frameB.filename || + frameA.lineno !== frameB.lineno || + frameA.colno !== frameB.colno || + frameA.function !== frameB.function + ) { + return false; + } + } + + return true; +} + +/** JSDoc */ +function _isSameFingerprint(currentEvent, previousEvent) { + let currentFingerprint = currentEvent.fingerprint; + let previousFingerprint = previousEvent.fingerprint; + + // If neither event has a fingerprint, they are assumed to be the same + if (!currentFingerprint && !previousFingerprint) { + return true; + } + + // If only one event has a fingerprint, but not the other one, they are not the same + if ((currentFingerprint && !previousFingerprint) || (!currentFingerprint && previousFingerprint)) { + return false; + } + + currentFingerprint = currentFingerprint ; + previousFingerprint = previousFingerprint ; + + // Otherwise, compare the two + try { + return !!(currentFingerprint.join('') === previousFingerprint.join('')); + } catch (_oO) { + return false; + } +} + +/** JSDoc */ +function _getExceptionFromEvent(event) { + return event.exception && event.exception.values && event.exception.values[0]; +} + +/** JSDoc */ +function _getFramesFromEvent(event) { + const exception = event.exception; + + if (exception) { + try { + // @ts-ignore Object could be undefined + return exception.values[0].stacktrace.frames; + } catch (_oO) { + return undefined; + } + } + return undefined; +} + +export { Dedupe }; +//# sourceMappingURL=dedupe.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/integrations/globalhandlers.js b/shared/logger/node_modules/@sentry/browser/esm/integrations/globalhandlers.js new file mode 100644 index 0000000..65c343c --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/integrations/globalhandlers.js @@ -0,0 +1,248 @@ +import { getCurrentHub } from '@sentry/core'; +import { addInstrumentationHandler, isString, isPrimitive, isErrorEvent, getLocationHref, logger, addExceptionMechanism } from '@sentry/utils'; +import { eventFromUnknownInput } from '../eventbuilder.js'; +import { shouldIgnoreOnError } from '../helpers.js'; + +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ + +/** Global handlers */ +class GlobalHandlers { + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'GlobalHandlers';} + + /** + * @inheritDoc + */ + __init() {this.name = GlobalHandlers.id;} + + /** JSDoc */ + + /** + * Stores references functions to installing handlers. Will set to undefined + * after they have been run so that they are not used twice. + */ + __init2() {this._installFunc = { + onerror: _installGlobalOnErrorHandler, + onunhandledrejection: _installGlobalOnUnhandledRejectionHandler, + };} + + /** JSDoc */ + constructor(options) {GlobalHandlers.prototype.__init.call(this);GlobalHandlers.prototype.__init2.call(this); + this._options = { + onerror: true, + onunhandledrejection: true, + ...options, + }; + } + /** + * @inheritDoc + */ + setupOnce() { + Error.stackTraceLimit = 50; + const options = this._options; + + // We can disable guard-for-in as we construct the options object above + do checks against + // `this._installFunc` for the property. + // eslint-disable-next-line guard-for-in + for (const key in options) { + const installFunc = this._installFunc[key ]; + if (installFunc && options[key ]) { + globalHandlerLog(key); + installFunc(); + this._installFunc[key ] = undefined; + } + } + } +} GlobalHandlers.__initStatic(); + +/** JSDoc */ +function _installGlobalOnErrorHandler() { + addInstrumentationHandler( + 'error', + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (data) => { + const [hub, stackParser, attachStacktrace] = getHubAndOptions(); + if (!hub.getIntegration(GlobalHandlers)) { + return; + } + const { msg, url, line, column, error } = data; + if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) { + return; + } + + const event = + error === undefined && isString(msg) + ? _eventFromIncompleteOnError(msg, url, line, column) + : _enhanceEventWithInitialFrame( + eventFromUnknownInput(stackParser, error || msg, undefined, attachStacktrace, false), + url, + line, + column, + ); + + event.level = 'error'; + + addMechanismAndCapture(hub, error, event, 'onerror'); + }, + ); +} + +/** JSDoc */ +function _installGlobalOnUnhandledRejectionHandler() { + addInstrumentationHandler( + 'unhandledrejection', + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (e) => { + const [hub, stackParser, attachStacktrace] = getHubAndOptions(); + if (!hub.getIntegration(GlobalHandlers)) { + return; + } + let error = e; + + // dig the object of the rejection out of known event types + try { + // PromiseRejectionEvents store the object of the rejection under 'reason' + // see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent + if ('reason' in e) { + error = e.reason; + } + // something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents + // to CustomEvents, moving the `promise` and `reason` attributes of the PRE into + // the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec + // see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and + // https://github.com/getsentry/sentry-javascript/issues/2380 + else if ('detail' in e && 'reason' in e.detail) { + error = e.detail.reason; + } + } catch (_oO) { + // no-empty + } + + if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) { + return true; + } + + const event = isPrimitive(error) + ? _eventFromRejectionWithPrimitive(error) + : eventFromUnknownInput(stackParser, error, undefined, attachStacktrace, true); + + event.level = 'error'; + + addMechanismAndCapture(hub, error, event, 'onunhandledrejection'); + return; + }, + ); +} + +/** + * Create an event from a promise rejection where the `reason` is a primitive. + * + * @param reason: The `reason` property of the promise rejection + * @returns An Event object with an appropriate `exception` value + */ +function _eventFromRejectionWithPrimitive(reason) { + return { + exception: { + values: [ + { + type: 'UnhandledRejection', + // String() is needed because the Primitive type includes symbols (which can't be automatically stringified) + value: `Non-Error promise rejection captured with value: ${String(reason)}`, + }, + ], + }, + }; +} + +/** + * This function creates a stack from an old, error-less onerror handler. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function _eventFromIncompleteOnError(msg, url, line, column) { + const ERROR_TYPES_RE = + /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/i; + + // If 'message' is ErrorEvent, get real message from inside + let message = isErrorEvent(msg) ? msg.message : msg; + let name = 'Error'; + + const groups = message.match(ERROR_TYPES_RE); + if (groups) { + name = groups[1]; + message = groups[2]; + } + + const event = { + exception: { + values: [ + { + type: name, + value: message, + }, + ], + }, + }; + + return _enhanceEventWithInitialFrame(event, url, line, column); +} + +/** JSDoc */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function _enhanceEventWithInitialFrame(event, url, line, column) { + // event.exception + const e = (event.exception = event.exception || {}); + // event.exception.values + const ev = (e.values = e.values || []); + // event.exception.values[0] + const ev0 = (ev[0] = ev[0] || {}); + // event.exception.values[0].stacktrace + const ev0s = (ev0.stacktrace = ev0.stacktrace || {}); + // event.exception.values[0].stacktrace.frames + const ev0sf = (ev0s.frames = ev0s.frames || []); + + const colno = isNaN(parseInt(column, 10)) ? undefined : column; + const lineno = isNaN(parseInt(line, 10)) ? undefined : line; + const filename = isString(url) && url.length > 0 ? url : getLocationHref(); + + // event.exception.values[0].stacktrace.frames + if (ev0sf.length === 0) { + ev0sf.push({ + colno, + filename, + function: '?', + in_app: true, + lineno, + }); + } + + return event; +} + +function globalHandlerLog(type) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`Global Handler attached: ${type}`); +} + +function addMechanismAndCapture(hub, error, event, type) { + addExceptionMechanism(event, { + handled: false, + type, + }); + hub.captureEvent(event, { + originalException: error, + }); +} + +function getHubAndOptions() { + const hub = getCurrentHub(); + const client = hub.getClient(); + const options = (client && client.getOptions()) || { + stackParser: () => [], + attachStacktrace: false, + }; + return [hub, options.stackParser, options.attachStacktrace]; +} + +export { GlobalHandlers }; +//# sourceMappingURL=globalhandlers.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/integrations/httpcontext.js b/shared/logger/node_modules/@sentry/browser/esm/integrations/httpcontext.js new file mode 100644 index 0000000..014a182 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/integrations/httpcontext.js @@ -0,0 +1,47 @@ +import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core'; +import { WINDOW } from '../helpers.js'; + +/** HttpContext integration collects information about HTTP request headers */ +class HttpContext {constructor() { HttpContext.prototype.__init.call(this); } + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'HttpContext';} + + /** + * @inheritDoc + */ + __init() {this.name = HttpContext.id;} + + /** + * @inheritDoc + */ + setupOnce() { + addGlobalEventProcessor((event) => { + if (getCurrentHub().getIntegration(HttpContext)) { + // if none of the information we want exists, don't bother + if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) { + return event; + } + + // grab as much info as exists and add it to the event + const url = (event.request && event.request.url) || (WINDOW.location && WINDOW.location.href); + const { referrer } = WINDOW.document || {}; + const { userAgent } = WINDOW.navigator || {}; + + const headers = { + ...(event.request && event.request.headers), + ...(referrer && { Referer: referrer }), + ...(userAgent && { 'User-Agent': userAgent }), + }; + const request = { ...event.request, ...(url && { url }), headers }; + + return { ...event, request }; + } + return event; + }); + } +} HttpContext.__initStatic(); + +export { HttpContext }; +//# sourceMappingURL=httpcontext.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/integrations/linkederrors.js b/shared/logger/node_modules/@sentry/browser/esm/integrations/linkederrors.js new file mode 100644 index 0000000..d44a5e9 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/integrations/linkederrors.js @@ -0,0 +1,87 @@ +import { getCurrentHub, addGlobalEventProcessor } from '@sentry/core'; +import { isInstanceOf } from '@sentry/utils'; +import { exceptionFromError } from '../eventbuilder.js'; + +const DEFAULT_KEY = 'cause'; +const DEFAULT_LIMIT = 5; + +/** Adds SDK info to an event. */ +class LinkedErrors { + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'LinkedErrors';} + + /** + * @inheritDoc + */ + __init() {this.name = LinkedErrors.id;} + + /** + * @inheritDoc + */ + + /** + * @inheritDoc + */ + + /** + * @inheritDoc + */ + constructor(options = {}) {LinkedErrors.prototype.__init.call(this); + this._key = options.key || DEFAULT_KEY; + this._limit = options.limit || DEFAULT_LIMIT; + } + + /** + * @inheritDoc + */ + setupOnce() { + const client = getCurrentHub().getClient(); + if (!client) { + return; + } + addGlobalEventProcessor((event, hint) => { + const self = getCurrentHub().getIntegration(LinkedErrors); + return self ? _handler(client.getOptions().stackParser, self._key, self._limit, event, hint) : event; + }); + } +} LinkedErrors.__initStatic(); + +/** + * @inheritDoc + */ +function _handler( + parser, + key, + limit, + event, + hint, +) { + if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) { + return event; + } + const linkedErrors = _walkErrorTree(parser, limit, hint.originalException , key); + event.exception.values = [...linkedErrors, ...event.exception.values]; + return event; +} + +/** + * JSDOC + */ +function _walkErrorTree( + parser, + limit, + error, + key, + stack = [], +) { + if (!isInstanceOf(error[key], Error) || stack.length + 1 >= limit) { + return stack; + } + const exception = exceptionFromError(parser, error[key]); + return _walkErrorTree(parser, limit, error[key], key, [exception, ...stack]); +} + +export { LinkedErrors, _handler, _walkErrorTree }; +//# sourceMappingURL=linkederrors.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/integrations/trycatch.js b/shared/logger/node_modules/@sentry/browser/esm/integrations/trycatch.js new file mode 100644 index 0000000..69e112f --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/integrations/trycatch.js @@ -0,0 +1,281 @@ +import { fill, getFunctionName, getOriginalFunction } from '@sentry/utils'; +import { WINDOW, wrap } from '../helpers.js'; + +const DEFAULT_EVENT_TARGET = [ + 'EventTarget', + 'Window', + 'Node', + 'ApplicationCache', + 'AudioTrackList', + 'ChannelMergerNode', + 'CryptoOperation', + 'EventSource', + 'FileReader', + 'HTMLUnknownElement', + 'IDBDatabase', + 'IDBRequest', + 'IDBTransaction', + 'KeyOperation', + 'MediaController', + 'MessagePort', + 'ModalWindow', + 'Notification', + 'SVGElementInstance', + 'Screen', + 'TextTrack', + 'TextTrackCue', + 'TextTrackList', + 'WebSocket', + 'WebSocketWorker', + 'Worker', + 'XMLHttpRequest', + 'XMLHttpRequestEventTarget', + 'XMLHttpRequestUpload', +]; + +/** Wrap timer functions and event targets to catch errors and provide better meta data */ +class TryCatch { + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'TryCatch';} + + /** + * @inheritDoc + */ + __init() {this.name = TryCatch.id;} + + /** JSDoc */ + + /** + * @inheritDoc + */ + constructor(options) {TryCatch.prototype.__init.call(this); + this._options = { + XMLHttpRequest: true, + eventTarget: true, + requestAnimationFrame: true, + setInterval: true, + setTimeout: true, + ...options, + }; + } + + /** + * Wrap timer functions and event targets to catch errors + * and provide better metadata. + */ + setupOnce() { + if (this._options.setTimeout) { + fill(WINDOW, 'setTimeout', _wrapTimeFunction); + } + + if (this._options.setInterval) { + fill(WINDOW, 'setInterval', _wrapTimeFunction); + } + + if (this._options.requestAnimationFrame) { + fill(WINDOW, 'requestAnimationFrame', _wrapRAF); + } + + if (this._options.XMLHttpRequest && 'XMLHttpRequest' in WINDOW) { + fill(XMLHttpRequest.prototype, 'send', _wrapXHR); + } + + const eventTargetOption = this._options.eventTarget; + if (eventTargetOption) { + const eventTarget = Array.isArray(eventTargetOption) ? eventTargetOption : DEFAULT_EVENT_TARGET; + eventTarget.forEach(_wrapEventTarget); + } + } +} TryCatch.__initStatic(); + +/** JSDoc */ +function _wrapTimeFunction(original) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return function ( ...args) { + const originalCallback = args[0]; + args[0] = wrap(originalCallback, { + mechanism: { + data: { function: getFunctionName(original) }, + handled: true, + type: 'instrument', + }, + }); + return original.apply(this, args); + }; +} + +/** JSDoc */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function _wrapRAF(original) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return function ( callback) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return original.apply(this, [ + wrap(callback, { + mechanism: { + data: { + function: 'requestAnimationFrame', + handler: getFunctionName(original), + }, + handled: true, + type: 'instrument', + }, + }), + ]); + }; +} + +/** JSDoc */ +function _wrapXHR(originalSend) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return function ( ...args) { + // eslint-disable-next-line @typescript-eslint/no-this-alias + const xhr = this; + const xmlHttpRequestProps = ['onload', 'onerror', 'onprogress', 'onreadystatechange']; + + xmlHttpRequestProps.forEach(prop => { + if (prop in xhr && typeof xhr[prop] === 'function') { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + fill(xhr, prop, function (original) { + const wrapOptions = { + mechanism: { + data: { + function: prop, + handler: getFunctionName(original), + }, + handled: true, + type: 'instrument', + }, + }; + + // If Instrument integration has been called before TryCatch, get the name of original function + const originalFunction = getOriginalFunction(original); + if (originalFunction) { + wrapOptions.mechanism.data.handler = getFunctionName(originalFunction); + } + + // Otherwise wrap directly + return wrap(original, wrapOptions); + }); + } + }); + + return originalSend.apply(this, args); + }; +} + +/** JSDoc */ +function _wrapEventTarget(target) { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const globalObject = WINDOW ; + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + const proto = globalObject[target] && globalObject[target].prototype; + + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins + if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) { + return; + } + + fill(proto, 'addEventListener', function (original) + + { + return function ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + + eventName, + fn, + options, + ) { + try { + if (typeof fn.handleEvent === 'function') { + // ESlint disable explanation: + // First, it is generally safe to call `wrap` with an unbound function. Furthermore, using `.bind()` would + // introduce a bug here, because bind returns a new function that doesn't have our + // flags(like __sentry_original__) attached. `wrap` checks for those flags to avoid unnecessary wrapping. + // Without those flags, every call to addEventListener wraps the function again, causing a memory leak. + // eslint-disable-next-line @typescript-eslint/unbound-method + fn.handleEvent = wrap(fn.handleEvent, { + mechanism: { + data: { + function: 'handleEvent', + handler: getFunctionName(fn), + target, + }, + handled: true, + type: 'instrument', + }, + }); + } + } catch (err) { + // can sometimes get 'Permission denied to access property "handle Event' + } + + return original.apply(this, [ + eventName, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + wrap(fn , { + mechanism: { + data: { + function: 'addEventListener', + handler: getFunctionName(fn), + target, + }, + handled: true, + type: 'instrument', + }, + }), + options, + ]); + }; + }); + + fill( + proto, + 'removeEventListener', + function ( + originalRemoveEventListener, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ) { + return function ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + + eventName, + fn, + options, + ) { + /** + * There are 2 possible scenarios here: + * + * 1. Someone passes a callback, which was attached prior to Sentry initialization, or by using unmodified + * method, eg. `document.addEventListener.call(el, name, handler). In this case, we treat this function + * as a pass-through, and call original `removeEventListener` with it. + * + * 2. Someone passes a callback, which was attached after Sentry was initialized, which means that it was using + * our wrapped version of `addEventListener`, which internally calls `wrap` helper. + * This helper "wraps" whole callback inside a try/catch statement, and attached appropriate metadata to it, + * in order for us to make a distinction between wrapped/non-wrapped functions possible. + * If a function was wrapped, it has additional property of `__sentry_wrapped__`, holding the handler. + * + * When someone adds a handler prior to initialization, and then do it again, but after, + * then we have to detach both of them. Otherwise, if we'd detach only wrapped one, it'd be impossible + * to get rid of the initial handler and it'd stick there forever. + */ + const wrappedEventHandler = fn ; + try { + const originalEventHandler = wrappedEventHandler && wrappedEventHandler.__sentry_wrapped__; + if (originalEventHandler) { + originalRemoveEventListener.call(this, eventName, originalEventHandler, options); + } + } catch (e) { + // ignore, accessing __sentry_wrapped__ will throw in some Selenium environments + } + return originalRemoveEventListener.call(this, eventName, wrappedEventHandler, options); + }; + }, + ); +} + +export { TryCatch }; +//# sourceMappingURL=trycatch.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/profiling/hubextensions.js b/shared/logger/node_modules/@sentry/browser/esm/profiling/hubextensions.js new file mode 100644 index 0000000..c1cd92d --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/profiling/hubextensions.js @@ -0,0 +1,240 @@ +import { getCurrentHub } from '@sentry/core'; +import { logger, uuid4 } from '@sentry/utils'; +import { WINDOW } from '../helpers.js'; +import { isValidSampleRate, addProfileToMap } from './utils.js'; + +/* eslint-disable complexity */ + +const MAX_PROFILE_DURATION_MS = 30000; +// Keep a flag value to avoid re-initializing the profiler constructor. If it fails +// once, it will always fail and this allows us to early return. +let PROFILING_CONSTRUCTOR_FAILED = false; + +/** + * Check if profiler constructor is available. + * @param maybeProfiler + */ +function isJSProfilerSupported(maybeProfiler) { + return typeof maybeProfiler === 'function'; +} + +/** + * Safety wrapper for startTransaction for the unlikely case that transaction starts before tracing is imported - + * if that happens we want to avoid throwing an error from profiling code. + * see https://github.com/getsentry/sentry-javascript/issues/4731. + * + * @experimental + */ +function onProfilingStartRouteTransaction(transaction) { + if (!transaction) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log('[Profiling] Transaction is undefined, skipping profiling'); + } + return transaction; + } + + return wrapTransactionWithProfiling(transaction); +} + +/** + * Wraps startTransaction and stopTransaction with profiling related logic. + * startProfiling is called after the call to startTransaction in order to avoid our own code from + * being profiled. Because of that same reason, stopProfiling is called before the call to stopTransaction. + */ +function wrapTransactionWithProfiling(transaction) { + // Feature support check first + const JSProfilerConstructor = WINDOW.Profiler; + + if (!isJSProfilerSupported(JSProfilerConstructor)) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log( + '[Profiling] Profiling is not supported by this browser, Profiler interface missing on window object.', + ); + } + return transaction; + } + + // If constructor failed once, it will always fail, so we can early return. + if (PROFILING_CONSTRUCTOR_FAILED) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log('[Profiling] Profiling has been disabled for the duration of the current user session.'); + } + return transaction; + } + + const client = getCurrentHub().getClient(); + const options = client && client.getOptions(); + if (!options) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Profiling] Profiling disabled, no options found.'); + return transaction; + } + + // @ts-ignore profilesSampleRate is not part of the browser options yet + const profilesSampleRate = options.profilesSampleRate; + + // Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The + // only valid values are booleans or numbers between 0 and 1.) + if (!isValidSampleRate(profilesSampleRate)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('[Profiling] Discarding profile because of invalid sample rate.'); + return transaction; + } + + // if the function returned 0 (or false), or if `profileSampleRate` is 0, it's a sign the profile should be dropped + if (!profilesSampleRate) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + '[Profiling] Discarding profile because a negative sampling decision was inherited or profileSampleRate is set to 0', + ); + return transaction; + } + + // Now we roll the dice. Math.random is inclusive of 0, but not of 1, so strict < is safe here. In case sampleRate is + // a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false. + const sampled = profilesSampleRate === true ? true : Math.random() < profilesSampleRate; + // Check if we should sample this profile + if (!sampled) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + `[Profiling] Discarding profile because it's not included in the random sample (sampling rate = ${Number( + profilesSampleRate, + )})`, + ); + return transaction; + } + + // From initial testing, it seems that the minimum value for sampleInterval is 10ms. + const samplingIntervalMS = 10; + // Start the profiler + const maxSamples = Math.floor(MAX_PROFILE_DURATION_MS / samplingIntervalMS); + let profiler; + + // Attempt to initialize the profiler constructor, if it fails, we disable profiling for the current user session. + // This is likely due to a missing 'Document-Policy': 'js-profiling' header. We do not want to throw an error if this happens + // as we risk breaking the user's application, so just disable profiling and log an error. + try { + profiler = new JSProfilerConstructor({ sampleInterval: samplingIntervalMS, maxBufferSize: maxSamples }); + } catch (e) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log( + "[Profiling] Failed to initialize the Profiling constructor, this is likely due to a missing 'Document-Policy': 'js-profiling' header.", + ); + logger.log('[Profiling] Disabling profiling for current user session.'); + } + PROFILING_CONSTRUCTOR_FAILED = true; + } + + // We failed to construct the profiler, fallback to original transaction - there is no need to log + // anything as we already did that in the try/catch block. + if (!profiler) { + return transaction; + } + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log(`[Profiling] started profiling transaction: ${transaction.name || transaction.description}`); + } + + // We create "unique" transaction names to avoid concurrent transactions with same names + // from being ignored by the profiler. From here on, only this transaction name should be used when + // calling the profiler methods. Note: we log the original name to the user to avoid confusion. + const profileId = uuid4(); + + /** + * Idempotent handler for profile stop + */ + async function onProfileHandler() { + // Check if the profile exists and return it the behavior has to be idempotent as users may call transaction.finish multiple times. + if (!transaction) { + return null; + } + // Satisfy the type checker, but profiler will always be defined here. + if (!profiler) { + return null; + } + + // This is temporary - we will use the collected span data to evaluate + // if deferring txn.finish until profiler resolves is a viable approach. + const stopProfilerSpan = transaction.startChild({ description: 'profiler.stop', op: 'profiler' }); + + return profiler + .stop() + .then((p) => { + stopProfilerSpan.finish(); + + if (maxDurationTimeoutID) { + WINDOW.clearTimeout(maxDurationTimeoutID); + maxDurationTimeoutID = undefined; + } + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log(`[Profiling] stopped profiling of transaction: ${transaction.name || transaction.description}`); + } + + // In case of an overlapping transaction, stopProfiling may return null and silently ignore the overlapping profile. + if (!p) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log( + `[Profiling] profiler returned null profile for: ${transaction.name || transaction.description}`, + 'this may indicate an overlapping transaction or a call to stopProfiling with a profile title that was never started', + ); + } + return null; + } + + addProfileToMap(profileId, p); + return null; + }) + .catch(error => { + stopProfilerSpan.finish(); + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log('[Profiling] error while stopping profiler:', error); + } + return null; + }); + } + + // Enqueue a timeout to prevent profiles from running over max duration. + let maxDurationTimeoutID = WINDOW.setTimeout(() => { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log( + '[Profiling] max profile duration elapsed, stopping profiling for:', + transaction.name || transaction.description, + ); + } + // If the timeout exceeds, we want to stop profiling, but not finish the transaction + void onProfileHandler(); + }, MAX_PROFILE_DURATION_MS); + + // We need to reference the original finish call to avoid creating an infinite loop + const originalFinish = transaction.finish.bind(transaction); + + /** + * Wraps startTransaction and stopTransaction with profiling related logic. + * startProfiling is called after the call to startTransaction in order to avoid our own code from + * being profiled. Because of that same reason, stopProfiling is called before the call to stopTransaction. + */ + function profilingWrappedTransactionFinish() { + if (!transaction) { + return originalFinish(); + } + // onProfileHandler should always return the same profile even if this is called multiple times. + // Always call onProfileHandler to ensure stopProfiling is called and the timeout is cleared. + void onProfileHandler().then( + () => { + transaction.setContext('profile', { profile_id: profileId }); + originalFinish(); + }, + () => { + // If onProfileHandler fails, we still want to call the original finish method. + originalFinish(); + }, + ); + + return transaction; + } + + transaction.finish = profilingWrappedTransactionFinish; + return transaction; +} + +export { MAX_PROFILE_DURATION_MS, onProfilingStartRouteTransaction, wrapTransactionWithProfiling }; +//# sourceMappingURL=hubextensions.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/profiling/integration.js b/shared/logger/node_modules/@sentry/browser/esm/profiling/integration.js new file mode 100644 index 0000000..f09e4ed --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/profiling/integration.js @@ -0,0 +1,81 @@ +import { logger } from '@sentry/utils'; +import { wrapTransactionWithProfiling } from './hubextensions.js'; +import { PROFILE_MAP, findProfiledTransactionsFromEnvelope, createProfilingEvent, addProfilesToEnvelope } from './utils.js'; + +/** + * Browser profiling integration. Stores any event that has contexts["profile"]["profile_id"] + * This exists because we do not want to await async profiler.stop calls as transaction.finish is called + * in a synchronous context. Instead, we handle sending the profile async from the promise callback and + * rely on being able to pull the event from the cache when we need to construct the envelope. This makes the + * integration less reliable as we might be dropping profiles when the cache is full. + * + * @experimental + */ +class BrowserProfilingIntegration {constructor() { BrowserProfilingIntegration.prototype.__init.call(this);BrowserProfilingIntegration.prototype.__init2.call(this); } + __init() {this.name = 'BrowserProfilingIntegration';} + __init2() {this.getCurrentHub = undefined;} + + /** + * @inheritDoc + */ + setupOnce(addGlobalEventProcessor, getCurrentHub) { + this.getCurrentHub = getCurrentHub; + const client = this.getCurrentHub().getClient() ; + + if (client && typeof client.on === 'function') { + client.on('startTransaction', (transaction) => { + wrapTransactionWithProfiling(transaction); + }); + + client.on('beforeEnvelope', (envelope) => { + // if not profiles are in queue, there is nothing to add to the envelope. + if (!PROFILE_MAP['size']) { + return; + } + + const profiledTransactionEvents = findProfiledTransactionsFromEnvelope(envelope); + if (!profiledTransactionEvents.length) { + return; + } + + const profilesToAddToEnvelope = []; + + for (const profiledTransaction of profiledTransactionEvents) { + const context = profiledTransaction && profiledTransaction.contexts; + const profile_id = context && context['profile'] && (context['profile']['profile_id'] ); + + if (!profile_id) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log('[Profiling] cannot find profile for a transaction without a profile context'); + continue; + } + + // Remove the profile from the transaction context before sending, relay will take care of the rest. + if (context && context['profile']) { + delete context.profile; + } + + const profile = PROFILE_MAP.get(profile_id); + if (!profile) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Profiling] Could not retrieve profile for transaction: ${profile_id}`); + continue; + } + + PROFILE_MAP.delete(profile_id); + const profileEvent = createProfilingEvent(profile_id, profile, profiledTransaction ); + + if (profileEvent) { + profilesToAddToEnvelope.push(profileEvent); + } + } + + addProfilesToEnvelope(envelope, profilesToAddToEnvelope); + }); + } else { + logger.warn('[Profiling] Client does not support hooks, profiling will be disabled'); + } + } +} + +export { BrowserProfilingIntegration }; +//# sourceMappingURL=integration.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/profiling/utils.js b/shared/logger/node_modules/@sentry/browser/esm/profiling/utils.js new file mode 100644 index 0000000..e8beae2 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/profiling/utils.js @@ -0,0 +1,438 @@ +import { DEFAULT_ENVIRONMENT, getCurrentHub } from '@sentry/core'; +import { forEachEnvelopeItem, logger, uuid4, GLOBAL_OBJ } from '@sentry/utils'; +import { WINDOW } from '../helpers.js'; + +/* eslint-disable max-lines */ + +const MS_TO_NS = 1e6; +// Use 0 as main thread id which is identical to threadId in node:worker_threads +// where main logs 0 and workers seem to log in increments of 1 +const THREAD_ID_STRING = String(0); +const THREAD_NAME = 'main'; + +// Machine properties (eval only once) +let OS_PLATFORM = ''; +let OS_PLATFORM_VERSION = ''; +let OS_ARCH = ''; +let OS_BROWSER = (WINDOW.navigator && WINDOW.navigator.userAgent) || ''; +let OS_MODEL = ''; +const OS_LOCALE = + (WINDOW.navigator && WINDOW.navigator.language) || + (WINDOW.navigator && WINDOW.navigator.languages && WINDOW.navigator.languages[0]) || + ''; + +function isUserAgentData(data) { + return typeof data === 'object' && data !== null && 'getHighEntropyValues' in data; +} + +// @ts-ignore userAgentData is not part of the navigator interface yet +const userAgentData = WINDOW.navigator && WINDOW.navigator.userAgentData; + +if (isUserAgentData(userAgentData)) { + userAgentData + .getHighEntropyValues(['architecture', 'model', 'platform', 'platformVersion', 'fullVersionList']) + .then((ua) => { + OS_PLATFORM = ua.platform || ''; + OS_ARCH = ua.architecture || ''; + OS_MODEL = ua.model || ''; + OS_PLATFORM_VERSION = ua.platformVersion || ''; + + if (ua.fullVersionList && ua.fullVersionList.length > 0) { + const firstUa = ua.fullVersionList[ua.fullVersionList.length - 1]; + OS_BROWSER = `${firstUa.brand} ${firstUa.version}`; + } + }) + .catch(e => void e); +} + +function isProcessedJSSelfProfile(profile) { + return !('thread_metadata' in profile); +} + +// Enriches the profile with threadId of the current thread. +// This is done in node as we seem to not be able to get the info from C native code. +/** + * + */ +function enrichWithThreadInformation(profile) { + if (!isProcessedJSSelfProfile(profile)) { + return profile; + } + + return convertJSSelfProfileToSampledFormat(profile); +} + +// Profile is marked as optional because it is deleted from the metadata +// by the integration before the event is processed by other integrations. + +function getTraceId(event) { + const traceId = event && event.contexts && event.contexts['trace'] && event.contexts['trace']['trace_id']; + // Log a warning if the profile has an invalid traceId (should be uuidv4). + // All profiles and transactions are rejected if this is the case and we want to + // warn users that this is happening if they enable debug flag + if (typeof traceId === 'string' && traceId.length !== 32) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log(`[Profiling] Invalid traceId: ${traceId} on profiled event`); + } + } + if (typeof traceId !== 'string') { + return ''; + } + + return traceId; +} +/** + * Creates a profiling event envelope from a Sentry event. If profile does not pass + * validation, returns null. + * @param event + * @param dsn + * @param metadata + * @param tunnel + * @returns {EventEnvelope | null} + */ + +/** + * Creates a profiling event envelope from a Sentry event. + */ +function createProfilePayload( + event, + processedProfile, + profile_id, +) { + if (event.type !== 'transaction') { + // createProfilingEventEnvelope should only be called for transactions, + // we type guard this behavior with isProfiledTransactionEvent. + throw new TypeError('Profiling events may only be attached to transactions, this should never occur.'); + } + + if (processedProfile === undefined || processedProfile === null) { + throw new TypeError( + `Cannot construct profiling event envelope without a valid profile. Got ${processedProfile} instead.`, + ); + } + + const traceId = getTraceId(event); + const enrichedThreadProfile = enrichWithThreadInformation(processedProfile); + const transactionStartMs = typeof event.start_timestamp === 'number' ? event.start_timestamp * 1000 : Date.now(); + const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : Date.now(); + + const profile = { + event_id: profile_id, + timestamp: new Date(transactionStartMs).toISOString(), + platform: 'javascript', + version: '1', + release: event.release || '', + environment: event.environment || DEFAULT_ENVIRONMENT, + runtime: { + name: 'javascript', + version: WINDOW.navigator.userAgent, + }, + os: { + name: OS_PLATFORM, + version: OS_PLATFORM_VERSION, + build_number: OS_BROWSER, + }, + device: { + locale: OS_LOCALE, + model: OS_MODEL, + manufacturer: OS_BROWSER, + architecture: OS_ARCH, + is_emulator: false, + }, + debug_meta: { + images: applyDebugMetadata(processedProfile.resources), + }, + profile: enrichedThreadProfile, + transactions: [ + { + name: event.transaction || '', + id: event.event_id || uuid4(), + trace_id: traceId, + active_thread_id: THREAD_ID_STRING, + relative_start_ns: '0', + relative_end_ns: ((transactionEndMs - transactionStartMs) * 1e6).toFixed(0), + }, + ], + }; + + return profile; +} + +/** + * Converts a JSSelfProfile to a our sampled format. + * Does not currently perform stack indexing. + */ +function convertJSSelfProfileToSampledFormat(input) { + let EMPTY_STACK_ID = undefined; + let STACK_ID = 0; + + // Initialize the profile that we will fill with data + const profile = { + samples: [], + stacks: [], + frames: [], + thread_metadata: { + [THREAD_ID_STRING]: { name: THREAD_NAME }, + }, + }; + + if (!input.samples.length) { + return profile; + } + + // We assert samples.length > 0 above and timestamp should always be present + const start = input.samples[0].timestamp; + + for (let i = 0; i < input.samples.length; i++) { + const jsSample = input.samples[i]; + + // If sample has no stack, add an empty sample + if (jsSample.stackId === undefined) { + if (EMPTY_STACK_ID === undefined) { + EMPTY_STACK_ID = STACK_ID; + profile.stacks[EMPTY_STACK_ID] = []; + STACK_ID++; + } + + profile['samples'][i] = { + // convert ms timestamp to ns + elapsed_since_start_ns: ((jsSample.timestamp - start) * MS_TO_NS).toFixed(0), + stack_id: EMPTY_STACK_ID, + thread_id: THREAD_ID_STRING, + }; + continue; + } + + let stackTop = input.stacks[jsSample.stackId]; + + // Functions in top->down order (root is last) + // We follow the stackTop.parentId trail and collect each visited frameId + const stack = []; + + while (stackTop) { + stack.push(stackTop.frameId); + + const frame = input.frames[stackTop.frameId]; + + // If our frame has not been indexed yet, index it + if (profile.frames[stackTop.frameId] === undefined) { + profile.frames[stackTop.frameId] = { + function: frame.name, + file: frame.resourceId ? input.resources[frame.resourceId] : undefined, + line: frame.line, + column: frame.column, + }; + } + + stackTop = stackTop.parentId === undefined ? undefined : input.stacks[stackTop.parentId]; + } + + const sample = { + // convert ms timestamp to ns + elapsed_since_start_ns: ((jsSample.timestamp - start) * MS_TO_NS).toFixed(0), + stack_id: STACK_ID, + thread_id: THREAD_ID_STRING, + }; + + profile['stacks'][STACK_ID] = stack; + profile['samples'][i] = sample; + STACK_ID++; + } + + return profile; +} + +/** + * Adds items to envelope if they are not already present - mutates the envelope. + * @param envelope + */ +function addProfilesToEnvelope(envelope, profiles) { + if (!profiles.length) { + return envelope; + } + + for (const profile of profiles) { + // @ts-ignore untyped envelope + envelope[1].push([{ type: 'profile' }, profile]); + } + return envelope; +} + +/** + * Finds transactions with profile_id context in the envelope + * @param envelope + * @returns + */ +function findProfiledTransactionsFromEnvelope(envelope) { + const events = []; + + forEachEnvelopeItem(envelope, (item, type) => { + if (type !== 'transaction') { + return; + } + + for (let j = 1; j < item.length; j++) { + const event = item[j] ; + + if (event && event.contexts && event.contexts['profile'] && event.contexts['profile']['profile_id']) { + events.push(item[j] ); + } + } + }); + + return events; +} + +const debugIdStackParserCache = new WeakMap(); +/** + * Applies debug meta data to an event from a list of paths to resources (sourcemaps) + */ +function applyDebugMetadata(resource_paths) { + const debugIdMap = GLOBAL_OBJ._sentryDebugIds; + + if (!debugIdMap) { + return []; + } + + const hub = getCurrentHub(); + if (!hub) { + return []; + } + const client = hub.getClient(); + if (!client) { + return []; + } + const options = client.getOptions(); + if (!options) { + return []; + } + const stackParser = options.stackParser; + if (!stackParser) { + return []; + } + + let debugIdStackFramesCache; + const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser); + if (cachedDebugIdStackFrameCache) { + debugIdStackFramesCache = cachedDebugIdStackFrameCache; + } else { + debugIdStackFramesCache = new Map(); + debugIdStackParserCache.set(stackParser, debugIdStackFramesCache); + } + + // Build a map of filename -> debug_id + const filenameDebugIdMap = Object.keys(debugIdMap).reduce((acc, debugIdStackTrace) => { + let parsedStack; + + const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace); + if (cachedParsedStack) { + parsedStack = cachedParsedStack; + } else { + parsedStack = stackParser(debugIdStackTrace); + debugIdStackFramesCache.set(debugIdStackTrace, parsedStack); + } + + for (let i = parsedStack.length - 1; i >= 0; i--) { + const stackFrame = parsedStack[i]; + const file = stackFrame && stackFrame.filename; + + if (stackFrame && file) { + acc[file] = debugIdMap[debugIdStackTrace] ; + break; + } + } + return acc; + }, {}); + + const images = []; + for (const path of resource_paths) { + if (path && filenameDebugIdMap[path]) { + images.push({ + type: 'sourcemap', + code_file: path, + debug_id: filenameDebugIdMap[path] , + }); + } + } + + return images; +} + +/** + * Checks the given sample rate to make sure it is valid type and value (a boolean, or a number between 0 and 1). + */ +function isValidSampleRate(rate) { + // we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck + if ((typeof rate !== 'number' && typeof rate !== 'boolean') || (typeof rate === 'number' && isNaN(rate))) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `[Profiling] Invalid sample rate. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify( + rate, + )} of type ${JSON.stringify(typeof rate)}.`, + ); + return false; + } + + // Boolean sample rates are always valid + if (rate === true || rate === false) { + return true; + } + + // in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false + if (rate < 0 || rate > 1) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn(`[Profiling] Invalid sample rate. Sample rate must be between 0 and 1. Got ${rate}.`); + return false; + } + return true; +} + +function isValidProfile(profile) { + if (profile.samples.length < 2) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + // Log a warning if the profile has less than 2 samples so users can know why + // they are not seeing any profiling data and we cant avoid the back and forth + // of asking them to provide us with a dump of the profile data. + logger.log('[Profiling] Discarding profile because it contains less than 2 samples'); + } + return false; + } + + if (!profile.frames.length) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.log('[Profiling] Discarding profile because it contains no frames'); + } + return false; + } + + return true; +} + +/** + * Creates a profiling envelope item, if the profile does not pass validation, returns null. + * @param event + * @returns {Profile | null} + */ +function createProfilingEvent(profile_id, profile, event) { + if (!isValidProfile(profile)) { + return null; + } + + return createProfilePayload(event, profile, profile_id); +} + +const PROFILE_MAP = new Map(); +/** + * + */ +function addProfileToMap(profile_id, profile) { + PROFILE_MAP.set(profile_id, profile); + + if (PROFILE_MAP.size > 30) { + const last = PROFILE_MAP.keys().next().value; + PROFILE_MAP.delete(last); + } +} + +export { PROFILE_MAP, addProfileToMap, addProfilesToEnvelope, applyDebugMetadata, convertJSSelfProfileToSampledFormat, createProfilePayload, createProfilingEvent, enrichWithThreadInformation, findProfiledTransactionsFromEnvelope, isValidSampleRate }; +//# sourceMappingURL=utils.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/sdk.js b/shared/logger/node_modules/@sentry/browser/esm/sdk.js new file mode 100644 index 0000000..2b4edf2 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/sdk.js @@ -0,0 +1,293 @@ +import { Integrations, getIntegrationsToSetup, initAndBind, getReportDialogEndpoint, getCurrentHub } from '@sentry/core'; +import { stackParserFromStackParserOptions, supportsFetch, logger, resolvedSyncPromise, addInstrumentationHandler } from '@sentry/utils'; +import { BrowserClient } from './client.js'; +import { WINDOW, wrap as wrap$1 } from './helpers.js'; +import { GlobalHandlers } from './integrations/globalhandlers.js'; +import { TryCatch } from './integrations/trycatch.js'; +import { Breadcrumbs } from './integrations/breadcrumbs.js'; +import { LinkedErrors } from './integrations/linkederrors.js'; +import { HttpContext } from './integrations/httpcontext.js'; +import { Dedupe } from './integrations/dedupe.js'; +import { defaultStackParser } from './stack-parsers.js'; +import { makeFetchTransport } from './transports/fetch.js'; +import { makeXHRTransport } from './transports/xhr.js'; + +const defaultIntegrations = [ + new Integrations.InboundFilters(), + new Integrations.FunctionToString(), + new TryCatch(), + new Breadcrumbs(), + new GlobalHandlers(), + new LinkedErrors(), + new Dedupe(), + new HttpContext(), +]; + +/** + * A magic string that build tooling can leverage in order to inject a release value into the SDK. + */ + +/** + * The Sentry Browser SDK Client. + * + * To use this SDK, call the {@link init} function as early as possible when + * loading the web page. To set context information or send manual events, use + * the provided methods. + * + * @example + * + * ``` + * + * import { init } from '@sentry/browser'; + * + * init({ + * dsn: '__DSN__', + * // ... + * }); + * ``` + * + * @example + * ``` + * + * import { configureScope } from '@sentry/browser'; + * configureScope((scope: Scope) => { + * scope.setExtra({ battery: 0.7 }); + * scope.setTag({ user_mode: 'admin' }); + * scope.setUser({ id: '4711' }); + * }); + * ``` + * + * @example + * ``` + * + * import { addBreadcrumb } from '@sentry/browser'; + * addBreadcrumb({ + * message: 'My Breadcrumb', + * // ... + * }); + * ``` + * + * @example + * + * ``` + * + * import * as Sentry from '@sentry/browser'; + * Sentry.captureMessage('Hello, world!'); + * Sentry.captureException(new Error('Good bye')); + * Sentry.captureEvent({ + * message: 'Manual', + * stacktrace: [ + * // ... + * ], + * }); + * ``` + * + * @see {@link BrowserOptions} for documentation on configuration options. + */ +function init(options = {}) { + if (options.defaultIntegrations === undefined) { + options.defaultIntegrations = defaultIntegrations; + } + if (options.release === undefined) { + // This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value + if (typeof __SENTRY_RELEASE__ === 'string') { + options.release = __SENTRY_RELEASE__; + } + + // This supports the variable that sentry-webpack-plugin injects + if (WINDOW.SENTRY_RELEASE && WINDOW.SENTRY_RELEASE.id) { + options.release = WINDOW.SENTRY_RELEASE.id; + } + } + if (options.autoSessionTracking === undefined) { + options.autoSessionTracking = true; + } + if (options.sendClientReports === undefined) { + options.sendClientReports = true; + } + + const clientOptions = { + ...options, + stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser), + integrations: getIntegrationsToSetup(options), + transport: options.transport || (supportsFetch() ? makeFetchTransport : makeXHRTransport), + }; + + initAndBind(BrowserClient, clientOptions); + + if (options.autoSessionTracking) { + startSessionTracking(); + } +} + +/** + * Present the user with a report dialog. + * + * @param options Everything is optional, we try to fetch all info need from the global scope. + */ +function showReportDialog(options = {}, hub = getCurrentHub()) { + // doesn't work without a document (React Native) + if (!WINDOW.document) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Global document not defined in showReportDialog call'); + return; + } + + const { client, scope } = hub.getStackTop(); + const dsn = options.dsn || (client && client.getDsn()); + if (!dsn) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('DSN not configured for showReportDialog call'); + return; + } + + if (scope) { + options.user = { + ...scope.getUser(), + ...options.user, + }; + } + + if (!options.eventId) { + options.eventId = hub.lastEventId(); + } + + const script = WINDOW.document.createElement('script'); + script.async = true; + script.src = getReportDialogEndpoint(dsn, options); + + if (options.onLoad) { + script.onload = options.onLoad; + } + + const injectionPoint = WINDOW.document.head || WINDOW.document.body; + if (injectionPoint) { + injectionPoint.appendChild(script); + } else { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Not injecting report dialog. No injection point found in HTML'); + } +} + +/** + * This is the getter for lastEventId. + * + * @returns The last event id of a captured event. + */ +function lastEventId() { + return getCurrentHub().lastEventId(); +} + +/** + * This function is here to be API compatible with the loader. + * @hidden + */ +function forceLoad() { + // Noop +} + +/** + * This function is here to be API compatible with the loader. + * @hidden + */ +function onLoad(callback) { + callback(); +} + +/** + * Call `flush()` on the current client, if there is one. See {@link Client.flush}. + * + * @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause + * the client to wait until all events are sent before resolving the promise. + * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it + * doesn't (or if there's no client defined). + */ +function flush(timeout) { + const client = getCurrentHub().getClient(); + if (client) { + return client.flush(timeout); + } + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot flush events. No client defined.'); + return resolvedSyncPromise(false); +} + +/** + * Call `close()` on the current client, if there is one. See {@link Client.close}. + * + * @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this + * parameter will cause the client to wait until all events are sent before disabling itself. + * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it + * doesn't (or if there's no client defined). + */ +function close(timeout) { + const client = getCurrentHub().getClient(); + if (client) { + return client.close(timeout); + } + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot flush events and disable SDK. No client defined.'); + return resolvedSyncPromise(false); +} + +/** + * Wrap code within a try/catch block so the SDK is able to capture errors. + * + * @param fn A function to wrap. + * + * @returns The result of wrapped function call. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function wrap(fn) { + return wrap$1(fn)(); +} + +function startSessionOnHub(hub) { + hub.startSession({ ignoreDuration: true }); + hub.captureSession(); +} + +/** + * Enable automatic Session Tracking for the initial page load. + */ +function startSessionTracking() { + if (typeof WINDOW.document === 'undefined') { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.'); + return; + } + + const hub = getCurrentHub(); + + // The only way for this to be false is for there to be a version mismatch between @sentry/browser (>= 6.0.0) and + // @sentry/hub (< 5.27.0). In the simple case, there won't ever be such a mismatch, because the two packages are + // pinned at the same version in package.json, but there are edge cases where it's possible. See + // https://github.com/getsentry/sentry-javascript/issues/3207 and + // https://github.com/getsentry/sentry-javascript/issues/3234 and + // https://github.com/getsentry/sentry-javascript/issues/3278. + if (!hub.captureSession) { + return; + } + + // The session duration for browser sessions does not track a meaningful + // concept that can be used as a metric. + // Automatically captured sessions are akin to page views, and thus we + // discard their duration. + startSessionOnHub(hub); + + // We want to create a session for every navigation as well + addInstrumentationHandler('history', ({ from, to }) => { + // Don't create an additional session for the initial route or if the location did not change + if (!(from === undefined || from === to)) { + startSessionOnHub(getCurrentHub()); + } + }); +} + +/** + * Captures user feedback and sends it to Sentry. + */ +function captureUserFeedback(feedback) { + const client = getCurrentHub().getClient(); + if (client) { + client.captureUserFeedback(feedback); + } +} + +export { captureUserFeedback, close, defaultIntegrations, flush, forceLoad, init, lastEventId, onLoad, showReportDialog, wrap }; +//# sourceMappingURL=sdk.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/stack-parsers.js b/shared/logger/node_modules/@sentry/browser/esm/stack-parsers.js new file mode 100644 index 0000000..4f3da89 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/stack-parsers.js @@ -0,0 +1,168 @@ +import { createStackParser } from '@sentry/utils'; + +// global reference to slice +const UNKNOWN_FUNCTION = '?'; + +const OPERA10_PRIORITY = 10; +const OPERA11_PRIORITY = 20; +const CHROME_PRIORITY = 30; +const WINJS_PRIORITY = 40; +const GECKO_PRIORITY = 50; + +function createFrame(filename, func, lineno, colno) { + const frame = { + filename, + function: func, + in_app: true, // All browser frames are considered in_app + }; + + if (lineno !== undefined) { + frame.lineno = lineno; + } + + if (colno !== undefined) { + frame.colno = colno; + } + + return frame; +} + +// Chromium based browsers: Chrome, Brave, new Opera, new Edge +const chromeRegex = + /^\s*at (?:(.+?\)(?: \[.+\])?|.*?) ?\((?:address at )?)?(?:async )?((?:<anonymous>|[-a-z]+:|.*bundle|\/)?.*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i; +const chromeEvalRegex = /\((\S*)(?::(\d+))(?::(\d+))\)/; + +const chrome = line => { + const parts = chromeRegex.exec(line); + + if (parts) { + const isEval = parts[2] && parts[2].indexOf('eval') === 0; // start of line + + if (isEval) { + const subMatch = chromeEvalRegex.exec(parts[2]); + + if (subMatch) { + // throw out eval line/column and use top-most line/column number + parts[2] = subMatch[1]; // url + parts[3] = subMatch[2]; // line + parts[4] = subMatch[3]; // column + } + } + + // Kamil: One more hack won't hurt us right? Understanding and adding more rules on top of these regexps right now + // would be way too time consuming. (TODO: Rewrite whole RegExp to be more readable) + const [func, filename] = extractSafariExtensionDetails(parts[1] || UNKNOWN_FUNCTION, parts[2]); + + return createFrame(filename, func, parts[3] ? +parts[3] : undefined, parts[4] ? +parts[4] : undefined); + } + + return; +}; + +const chromeStackLineParser = [CHROME_PRIORITY, chrome]; + +// gecko regex: `(?:bundle|\d+\.js)`: `bundle` is for react native, `\d+\.js` also but specifically for ram bundles because it +// generates filenames without a prefix like `file://` the filenames in the stacktrace are just 42.js +// We need this specific case for now because we want no other regex to match. +const geckoREgex = + /^\s*(.*?)(?:\((.*?)\))?(?:^|@)?((?:[-a-z]+)?:\/.*?|\[native code\]|[^@]*(?:bundle|\d+\.js)|\/[\w\-. /=]+)(?::(\d+))?(?::(\d+))?\s*$/i; +const geckoEvalRegex = /(\S+) line (\d+)(?: > eval line \d+)* > eval/i; + +const gecko = line => { + const parts = geckoREgex.exec(line); + + if (parts) { + const isEval = parts[3] && parts[3].indexOf(' > eval') > -1; + if (isEval) { + const subMatch = geckoEvalRegex.exec(parts[3]); + + if (subMatch) { + // throw out eval line/column and use top-most line number + parts[1] = parts[1] || 'eval'; + parts[3] = subMatch[1]; + parts[4] = subMatch[2]; + parts[5] = ''; // no column when eval + } + } + + let filename = parts[3]; + let func = parts[1] || UNKNOWN_FUNCTION; + [func, filename] = extractSafariExtensionDetails(func, filename); + + return createFrame(filename, func, parts[4] ? +parts[4] : undefined, parts[5] ? +parts[5] : undefined); + } + + return; +}; + +const geckoStackLineParser = [GECKO_PRIORITY, gecko]; + +const winjsRegex = /^\s*at (?:((?:\[object object\])?.+) )?\(?((?:[-a-z]+):.*?):(\d+)(?::(\d+))?\)?\s*$/i; + +const winjs = line => { + const parts = winjsRegex.exec(line); + + return parts + ? createFrame(parts[2], parts[1] || UNKNOWN_FUNCTION, +parts[3], parts[4] ? +parts[4] : undefined) + : undefined; +}; + +const winjsStackLineParser = [WINJS_PRIORITY, winjs]; + +const opera10Regex = / line (\d+).*script (?:in )?(\S+)(?:: in function (\S+))?$/i; + +const opera10 = line => { + const parts = opera10Regex.exec(line); + return parts ? createFrame(parts[2], parts[3] || UNKNOWN_FUNCTION, +parts[1]) : undefined; +}; + +const opera10StackLineParser = [OPERA10_PRIORITY, opera10]; + +const opera11Regex = + / line (\d+), column (\d+)\s*(?:in (?:<anonymous function: ([^>]+)>|([^)]+))\(.*\))? in (.*):\s*$/i; + +const opera11 = line => { + const parts = opera11Regex.exec(line); + return parts ? createFrame(parts[5], parts[3] || parts[4] || UNKNOWN_FUNCTION, +parts[1], +parts[2]) : undefined; +}; + +const opera11StackLineParser = [OPERA11_PRIORITY, opera11]; + +const defaultStackLineParsers = [chromeStackLineParser, geckoStackLineParser, winjsStackLineParser]; + +const defaultStackParser = createStackParser(...defaultStackLineParsers); + +/** + * Safari web extensions, starting version unknown, can produce "frames-only" stacktraces. + * What it means, is that instead of format like: + * + * Error: wat + * at function@url:row:col + * at function@url:row:col + * at function@url:row:col + * + * it produces something like: + * + * function@url:row:col + * function@url:row:col + * function@url:row:col + * + * Because of that, it won't be captured by `chrome` RegExp and will fall into `Gecko` branch. + * This function is extracted so that we can use it in both places without duplicating the logic. + * Unfortunately "just" changing RegExp is too complicated now and making it pass all tests + * and fix this case seems like an impossible, or at least way too time-consuming task. + */ +const extractSafariExtensionDetails = (func, filename) => { + const isSafariExtension = func.indexOf('safari-extension') !== -1; + const isSafariWebExtension = func.indexOf('safari-web-extension') !== -1; + + return isSafariExtension || isSafariWebExtension + ? [ + func.indexOf('@') !== -1 ? func.split('@')[0] : UNKNOWN_FUNCTION, + isSafariExtension ? `safari-extension:${filename}` : `safari-web-extension:${filename}`, + ] + : [func, filename]; +}; + +export { chromeStackLineParser, defaultStackLineParsers, defaultStackParser, geckoStackLineParser, opera10StackLineParser, opera11StackLineParser, winjsStackLineParser }; +//# sourceMappingURL=stack-parsers.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/transports/fetch.js b/shared/logger/node_modules/@sentry/browser/esm/transports/fetch.js new file mode 100644 index 0000000..bb836e3 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/transports/fetch.js @@ -0,0 +1,64 @@ +import { createTransport } from '@sentry/core'; +import { rejectedSyncPromise } from '@sentry/utils'; +import { getNativeFetchImplementation, clearCachedFetchImplementation } from './utils.js'; + +/** + * Creates a Transport that uses the Fetch API to send events to Sentry. + */ +function makeFetchTransport( + options, + nativeFetch = getNativeFetchImplementation(), +) { + let pendingBodySize = 0; + let pendingCount = 0; + + function makeRequest(request) { + const requestSize = request.body.length; + pendingBodySize += requestSize; + pendingCount++; + + const requestOptions = { + body: request.body, + method: 'POST', + referrerPolicy: 'origin', + headers: options.headers, + // Outgoing requests are usually cancelled when navigating to a different page, causing a "TypeError: Failed to + // fetch" error and sending a "network_error" client-outcome - in Chrome, the request status shows "(cancelled)". + // The `keepalive` flag keeps outgoing requests alive, even when switching pages. We want this since we're + // frequently sending events right before the user is switching pages (eg. whenfinishing navigation transactions). + // Gotchas: + // - `keepalive` isn't supported by Firefox + // - As per spec (https://fetch.spec.whatwg.org/#http-network-or-cache-fetch): + // If the sum of contentLength and inflightKeepaliveBytes is greater than 64 kibibytes, then return a network error. + // We will therefore only activate the flag when we're below that limit. + // There is also a limit of requests that can be open at the same time, so we also limit this to 15 + // See https://github.com/getsentry/sentry-javascript/pull/7553 for details + keepalive: pendingBodySize <= 60000 && pendingCount < 15, + ...options.fetchOptions, + }; + + try { + return nativeFetch(options.url, requestOptions).then(response => { + pendingBodySize -= requestSize; + pendingCount--; + return { + statusCode: response.status, + headers: { + 'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'), + 'retry-after': response.headers.get('Retry-After'), + }, + }; + }); + } catch (e) { + clearCachedFetchImplementation(); + pendingBodySize -= requestSize; + pendingCount--; + return rejectedSyncPromise(e); + } + } + + return createTransport(options, makeRequest); +} + +export { makeFetchTransport }; +//# sourceMappingURL=fetch.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/transports/offline.js b/shared/logger/node_modules/@sentry/browser/esm/transports/offline.js new file mode 100644 index 0000000..c60a02d --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/transports/offline.js @@ -0,0 +1,133 @@ +import { makeOfflineTransport } from '@sentry/core'; +import { serializeEnvelope, parseEnvelope } from '@sentry/utils'; + +// 'Store', 'promisifyRequest' and 'createStore' were originally copied from the 'idb-keyval' package before being +// modified and simplified: https://github.com/jakearchibald/idb-keyval +// +// At commit: 0420a704fd6cbb4225429c536b1f61112d012fca +// Original licence: + +// Copyright 2016, Jake Archibald +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +function promisifyRequest(request) { + return new Promise((resolve, reject) => { + // @ts-ignore - file size hacks + request.oncomplete = request.onsuccess = () => resolve(request.result); + // @ts-ignore - file size hacks + request.onabort = request.onerror = () => reject(request.error); + }); +} + +/** Create or open an IndexedDb store */ +function createStore(dbName, storeName) { + const request = indexedDB.open(dbName); + request.onupgradeneeded = () => request.result.createObjectStore(storeName); + const dbp = promisifyRequest(request); + + return callback => dbp.then(db => callback(db.transaction(storeName, 'readwrite').objectStore(storeName))); +} + +function keys(store) { + return promisifyRequest(store.getAllKeys() ); +} + +/** Insert into the store */ +function insert(store, value, maxQueueSize) { + return store(store => { + return keys(store).then(keys => { + if (keys.length >= maxQueueSize) { + return; + } + + // We insert with an incremented key so that the entries are popped in order + store.put(value, Math.max(...keys, 0) + 1); + return promisifyRequest(store.transaction); + }); + }); +} + +/** Pop the oldest value from the store */ +function pop(store) { + return store(store => { + return keys(store).then(keys => { + if (keys.length === 0) { + return undefined; + } + + return promisifyRequest(store.get(keys[0])).then(value => { + store.delete(keys[0]); + return promisifyRequest(store.transaction).then(() => value); + }); + }); + }); +} + +function createIndexedDbStore(options) { + let store; + + // Lazily create the store only when it's needed + function getStore() { + if (store == undefined) { + store = createStore(options.dbName || 'sentry-offline', options.storeName || 'queue'); + } + + return store; + } + + return { + insert: async (env) => { + try { + const serialized = await serializeEnvelope(env, options.textEncoder); + await insert(getStore(), serialized, options.maxQueueSize || 30); + } catch (_) { + // + } + }, + pop: async () => { + try { + const deserialized = await pop(getStore()); + if (deserialized) { + return parseEnvelope( + deserialized, + options.textEncoder || new TextEncoder(), + options.textDecoder || new TextDecoder(), + ); + } + } catch (_) { + // + } + + return undefined; + }, + }; +} + +function makeIndexedDbOfflineTransport( + createTransport, +) { + return options => createTransport({ ...options, createStore: createIndexedDbStore }); +} + +/** + * Creates a transport that uses IndexedDb to store events when offline. + */ +function makeBrowserOfflineTransport( + createTransport, +) { + return makeIndexedDbOfflineTransport(makeOfflineTransport(createTransport)); +} + +export { createStore, insert, makeBrowserOfflineTransport, pop }; +//# sourceMappingURL=offline.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/transports/utils.js b/shared/logger/node_modules/@sentry/browser/esm/transports/utils.js new file mode 100644 index 0000000..bc50f0c --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/transports/utils.js @@ -0,0 +1,85 @@ +import { isNativeFetch, logger } from '@sentry/utils'; +import { WINDOW } from '../helpers.js'; + +let cachedFetchImpl = undefined; + +/** + * A special usecase for incorrectly wrapped Fetch APIs in conjunction with ad-blockers. + * Whenever someone wraps the Fetch API and returns the wrong promise chain, + * this chain becomes orphaned and there is no possible way to capture it's rejections + * other than allowing it bubble up to this very handler. eg. + * + * const f = window.fetch; + * window.fetch = function () { + * const p = f.apply(this, arguments); + * + * p.then(function() { + * console.log('hi.'); + * }); + * + * return p; + * } + * + * `p.then(function () { ... })` is producing a completely separate promise chain, + * however, what's returned is `p` - the result of original `fetch` call. + * + * This mean, that whenever we use the Fetch API to send our own requests, _and_ + * some ad-blocker blocks it, this orphaned chain will _always_ reject, + * effectively causing another event to be captured. + * This makes a whole process become an infinite loop, which we need to somehow + * deal with, and break it in one way or another. + * + * To deal with this issue, we are making sure that we _always_ use the real + * browser Fetch API, instead of relying on what `window.fetch` exposes. + * The only downside to this would be missing our own requests as breadcrumbs, + * but because we are already not doing this, it should be just fine. + * + * Possible failed fetch error messages per-browser: + * + * Chrome: Failed to fetch + * Edge: Failed to Fetch + * Firefox: NetworkError when attempting to fetch resource + * Safari: resource blocked by content blocker + */ +function getNativeFetchImplementation() { + if (cachedFetchImpl) { + return cachedFetchImpl; + } + + /* eslint-disable @typescript-eslint/unbound-method */ + + // Fast path to avoid DOM I/O + if (isNativeFetch(WINDOW.fetch)) { + return (cachedFetchImpl = WINDOW.fetch.bind(WINDOW)); + } + + const document = WINDOW.document; + let fetchImpl = WINDOW.fetch; + // eslint-disable-next-line deprecation/deprecation + if (document && typeof document.createElement === 'function') { + try { + const sandbox = document.createElement('iframe'); + sandbox.hidden = true; + document.head.appendChild(sandbox); + const contentWindow = sandbox.contentWindow; + if (contentWindow && contentWindow.fetch) { + fetchImpl = contentWindow.fetch; + } + document.head.removeChild(sandbox); + } catch (e) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', e); + } + } + + return (cachedFetchImpl = fetchImpl.bind(WINDOW)); + /* eslint-enable @typescript-eslint/unbound-method */ +} + +/** Clears cached fetch impl */ +function clearCachedFetchImplementation() { + cachedFetchImpl = undefined; +} + +export { clearCachedFetchImplementation, getNativeFetchImplementation }; +//# sourceMappingURL=utils.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/transports/xhr.js b/shared/logger/node_modules/@sentry/browser/esm/transports/xhr.js new file mode 100644 index 0000000..4cb34a0 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/transports/xhr.js @@ -0,0 +1,52 @@ +import { createTransport } from '@sentry/core'; +import { SyncPromise } from '@sentry/utils'; + +/** + * The DONE ready state for XmlHttpRequest + * + * Defining it here as a constant b/c XMLHttpRequest.DONE is not always defined + * (e.g. during testing, it is `undefined`) + * + * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState} + */ +const XHR_READYSTATE_DONE = 4; + +/** + * Creates a Transport that uses the XMLHttpRequest API to send events to Sentry. + */ +function makeXHRTransport(options) { + function makeRequest(request) { + return new SyncPromise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + + xhr.onerror = reject; + + xhr.onreadystatechange = () => { + if (xhr.readyState === XHR_READYSTATE_DONE) { + resolve({ + statusCode: xhr.status, + headers: { + 'x-sentry-rate-limits': xhr.getResponseHeader('X-Sentry-Rate-Limits'), + 'retry-after': xhr.getResponseHeader('Retry-After'), + }, + }); + } + }; + + xhr.open('POST', options.url); + + for (const header in options.headers) { + if (Object.prototype.hasOwnProperty.call(options.headers, header)) { + xhr.setRequestHeader(header, options.headers[header]); + } + } + + xhr.send(request.body); + }); + } + + return createTransport(options, makeRequest); +} + +export { makeXHRTransport }; +//# sourceMappingURL=xhr.js.map diff --git a/shared/logger/node_modules/@sentry/browser/esm/userfeedback.js b/shared/logger/node_modules/@sentry/browser/esm/userfeedback.js new file mode 100644 index 0000000..4765c98 --- /dev/null +++ b/shared/logger/node_modules/@sentry/browser/esm/userfeedback.js @@ -0,0 +1,41 @@ +import { dsnToString, createEnvelope } from '@sentry/utils'; + +/** + * Creates an envelope from a user feedback. + */ +function createUserFeedbackEnvelope( + feedback, + { + metadata, + tunnel, + dsn, + } + +, +) { + const headers = { + event_id: feedback.event_id, + sent_at: new Date().toISOString(), + ...(metadata && + metadata.sdk && { + sdk: { + name: metadata.sdk.name, + version: metadata.sdk.version, + }, + }), + ...(!!tunnel && !!dsn && { dsn: dsnToString(dsn) }), + }; + const item = createUserFeedbackEnvelopeItem(feedback); + + return createEnvelope(headers, [item]); +} + +function createUserFeedbackEnvelopeItem(feedback) { + const feedbackHeaders = { + type: 'user_report', + }; + return [feedbackHeaders, feedback]; +} + +export { createUserFeedbackEnvelope }; +//# sourceMappingURL=userfeedback.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/api.js b/shared/logger/node_modules/@sentry/core/esm/api.js new file mode 100644 index 0000000..d8dd466 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/api.js @@ -0,0 +1,90 @@ +import { makeDsn, dsnToString, urlEncode } from '@sentry/utils'; + +const SENTRY_API_VERSION = '7'; + +/** Returns the prefix to construct Sentry ingestion API endpoints. */ +function getBaseApiEndpoint(dsn) { + const protocol = dsn.protocol ? `${dsn.protocol}:` : ''; + const port = dsn.port ? `:${dsn.port}` : ''; + return `${protocol}//${dsn.host}${port}${dsn.path ? `/${dsn.path}` : ''}/api/`; +} + +/** Returns the ingest API endpoint for target. */ +function _getIngestEndpoint(dsn) { + return `${getBaseApiEndpoint(dsn)}${dsn.projectId}/envelope/`; +} + +/** Returns a URL-encoded string with auth config suitable for a query string. */ +function _encodedAuth(dsn, sdkInfo) { + return urlEncode({ + // We send only the minimum set of required information. See + // https://github.com/getsentry/sentry-javascript/issues/2572. + sentry_key: dsn.publicKey, + sentry_version: SENTRY_API_VERSION, + ...(sdkInfo && { sentry_client: `${sdkInfo.name}/${sdkInfo.version}` }), + }); +} + +/** + * Returns the envelope endpoint URL with auth in the query string. + * + * Sending auth as part of the query string and not as custom HTTP headers avoids CORS preflight requests. + */ +function getEnvelopeEndpointWithUrlEncodedAuth( + dsn, + // TODO (v8): Remove `tunnelOrOptions` in favor of `options`, and use the substitute code below + // options: ClientOptions = {} as ClientOptions, + tunnelOrOptions = {} , +) { + // TODO (v8): Use this code instead + // const { tunnel, _metadata = {} } = options; + // return tunnel ? tunnel : `${_getIngestEndpoint(dsn)}?${_encodedAuth(dsn, _metadata.sdk)}`; + + const tunnel = typeof tunnelOrOptions === 'string' ? tunnelOrOptions : tunnelOrOptions.tunnel; + const sdkInfo = + typeof tunnelOrOptions === 'string' || !tunnelOrOptions._metadata ? undefined : tunnelOrOptions._metadata.sdk; + + return tunnel ? tunnel : `${_getIngestEndpoint(dsn)}?${_encodedAuth(dsn, sdkInfo)}`; +} + +/** Returns the url to the report dialog endpoint. */ +function getReportDialogEndpoint( + dsnLike, + dialogOptions + +, +) { + const dsn = makeDsn(dsnLike); + if (!dsn) { + return ''; + } + + const endpoint = `${getBaseApiEndpoint(dsn)}embed/error-page/`; + + let encodedOptions = `dsn=${dsnToString(dsn)}`; + for (const key in dialogOptions) { + if (key === 'dsn') { + continue; + } + + if (key === 'user') { + const user = dialogOptions.user; + if (!user) { + continue; + } + if (user.name) { + encodedOptions += `&name=${encodeURIComponent(user.name)}`; + } + if (user.email) { + encodedOptions += `&email=${encodeURIComponent(user.email)}`; + } + } else { + encodedOptions += `&${encodeURIComponent(key)}=${encodeURIComponent(dialogOptions[key] )}`; + } + } + + return `${endpoint}?${encodedOptions}`; +} + +export { getEnvelopeEndpointWithUrlEncodedAuth, getReportDialogEndpoint }; +//# sourceMappingURL=api.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/baseclient.js b/shared/logger/node_modules/@sentry/core/esm/baseclient.js new file mode 100644 index 0000000..d6e23b0 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/baseclient.js @@ -0,0 +1,674 @@ +import { makeDsn, logger, checkOrSetAlreadyCaught, isPrimitive, resolvedSyncPromise, addItemToEnvelope, createAttachmentEnvelopeItem, SyncPromise, rejectedSyncPromise, SentryError, isThenable, isPlainObject } from '@sentry/utils'; +import { getEnvelopeEndpointWithUrlEncodedAuth } from './api.js'; +import { createEventEnvelope, createSessionEnvelope } from './envelope.js'; +import { setupIntegrations, setupIntegration } from './integration.js'; +import { updateSession } from './session.js'; +import { prepareEvent } from './utils/prepareEvent.js'; + +const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured."; + +/** + * Base implementation for all JavaScript SDK clients. + * + * Call the constructor with the corresponding options + * specific to the client subclass. To access these options later, use + * {@link Client.getOptions}. + * + * If a Dsn is specified in the options, it will be parsed and stored. Use + * {@link Client.getDsn} to retrieve the Dsn at any moment. In case the Dsn is + * invalid, the constructor will throw a {@link SentryException}. Note that + * without a valid Dsn, the SDK will not send any events to Sentry. + * + * Before sending an event, it is passed through + * {@link BaseClient._prepareEvent} to add SDK information and scope data + * (breadcrumbs and context). To add more custom information, override this + * method and extend the resulting prepared event. + * + * To issue automatically created events (e.g. via instrumentation), use + * {@link Client.captureEvent}. It will prepare the event and pass it through + * the callback lifecycle. To issue auto-breadcrumbs, use + * {@link Client.addBreadcrumb}. + * + * @example + * class NodeClient extends BaseClient<NodeOptions> { + * public constructor(options: NodeOptions) { + * super(options); + * } + * + * // ... + * } + */ +class BaseClient { + /** Options passed to the SDK. */ + + /** The client Dsn, if specified in options. Without this Dsn, the SDK will be disabled. */ + + /** Array of set up integrations. */ + __init() {this._integrations = {};} + + /** Indicates whether this client's integrations have been set up. */ + __init2() {this._integrationsInitialized = false;} + + /** Number of calls being processed */ + __init3() {this._numProcessing = 0;} + + /** Holds flushable */ + __init4() {this._outcomes = {};} + + // eslint-disable-next-line @typescript-eslint/ban-types + __init5() {this._hooks = {};} + + /** + * Initializes this client instance. + * + * @param options Options for the client. + */ + constructor(options) {BaseClient.prototype.__init.call(this);BaseClient.prototype.__init2.call(this);BaseClient.prototype.__init3.call(this);BaseClient.prototype.__init4.call(this);BaseClient.prototype.__init5.call(this); + this._options = options; + + if (options.dsn) { + this._dsn = makeDsn(options.dsn); + } else { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('No DSN provided, client will not do anything.'); + } + + if (this._dsn) { + const url = getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, options); + this._transport = options.transport({ + recordDroppedEvent: this.recordDroppedEvent.bind(this), + ...options.transportOptions, + url, + }); + } + } + + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types + captureException(exception, hint, scope) { + // ensure we haven't captured this very object before + if (checkOrSetAlreadyCaught(exception)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(ALREADY_SEEN_ERROR); + return; + } + + let eventId = hint && hint.event_id; + + this._process( + this.eventFromException(exception, hint) + .then(event => this._captureEvent(event, hint, scope)) + .then(result => { + eventId = result; + }), + ); + + return eventId; + } + + /** + * @inheritDoc + */ + captureMessage( + message, + // eslint-disable-next-line deprecation/deprecation + level, + hint, + scope, + ) { + let eventId = hint && hint.event_id; + + const promisedEvent = isPrimitive(message) + ? this.eventFromMessage(String(message), level, hint) + : this.eventFromException(message, hint); + + this._process( + promisedEvent + .then(event => this._captureEvent(event, hint, scope)) + .then(result => { + eventId = result; + }), + ); + + return eventId; + } + + /** + * @inheritDoc + */ + captureEvent(event, hint, scope) { + // ensure we haven't captured this very object before + if (hint && hint.originalException && checkOrSetAlreadyCaught(hint.originalException)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(ALREADY_SEEN_ERROR); + return; + } + + let eventId = hint && hint.event_id; + + this._process( + this._captureEvent(event, hint, scope).then(result => { + eventId = result; + }), + ); + + return eventId; + } + + /** + * @inheritDoc + */ + captureSession(session) { + if (!this._isEnabled()) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('SDK not enabled, will not capture session.'); + return; + } + + if (!(typeof session.release === 'string')) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Discarded session because of missing or non-string release'); + } else { + this.sendSession(session); + // After sending, we set init false to indicate it's not the first occurrence + updateSession(session, { init: false }); + } + } + + /** + * @inheritDoc + */ + getDsn() { + return this._dsn; + } + + /** + * @inheritDoc + */ + getOptions() { + return this._options; + } + + /** + * @see SdkMetadata in @sentry/types + * + * @return The metadata of the SDK + */ + getSdkMetadata() { + return this._options._metadata; + } + + /** + * @inheritDoc + */ + getTransport() { + return this._transport; + } + + /** + * @inheritDoc + */ + flush(timeout) { + const transport = this._transport; + if (transport) { + return this._isClientDoneProcessing(timeout).then(clientFinished => { + return transport.flush(timeout).then(transportFlushed => clientFinished && transportFlushed); + }); + } else { + return resolvedSyncPromise(true); + } + } + + /** + * @inheritDoc + */ + close(timeout) { + return this.flush(timeout).then(result => { + this.getOptions().enabled = false; + return result; + }); + } + + /** + * Sets up the integrations + */ + setupIntegrations() { + if (this._isEnabled() && !this._integrationsInitialized) { + this._integrations = setupIntegrations(this._options.integrations); + this._integrationsInitialized = true; + } + } + + /** + * Gets an installed integration by its `id`. + * + * @returns The installed integration or `undefined` if no integration with that `id` was installed. + */ + getIntegrationById(integrationId) { + return this._integrations[integrationId]; + } + + /** + * @inheritDoc + */ + getIntegration(integration) { + try { + return (this._integrations[integration.id] ) || null; + } catch (_oO) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn(`Cannot retrieve integration ${integration.id} from the current Client`); + return null; + } + } + + /** + * @inheritDoc + */ + addIntegration(integration) { + setupIntegration(integration, this._integrations); + } + + /** + * @inheritDoc + */ + sendEvent(event, hint = {}) { + if (this._dsn) { + let env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel); + + for (const attachment of hint.attachments || []) { + env = addItemToEnvelope( + env, + createAttachmentEnvelopeItem( + attachment, + this._options.transportOptions && this._options.transportOptions.textEncoder, + ), + ); + } + + const promise = this._sendEnvelope(env); + if (promise) { + promise.then(sendResponse => this.emit('afterSendEvent', event, sendResponse), null); + } + } + } + + /** + * @inheritDoc + */ + sendSession(session) { + if (this._dsn) { + const env = createSessionEnvelope(session, this._dsn, this._options._metadata, this._options.tunnel); + void this._sendEnvelope(env); + } + } + + /** + * @inheritDoc + */ + recordDroppedEvent(reason, category, _event) { + // Note: we use `event` in replay, where we overwrite this hook. + + if (this._options.sendClientReports) { + // We want to track each category (error, transaction, session, replay_event) separately + // but still keep the distinction between different type of outcomes. + // We could use nested maps, but it's much easier to read and type this way. + // A correct type for map-based implementation if we want to go that route + // would be `Partial<Record<SentryRequestType, Partial<Record<Outcome, number>>>>` + // With typescript 4.1 we could even use template literal types + const key = `${reason}:${category}`; + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`Adding outcome: "${key}"`); + + // The following works because undefined + 1 === NaN and NaN is falsy + this._outcomes[key] = this._outcomes[key] + 1 || 1; + } + } + + // Keep on() & emit() signatures in sync with types' client.ts interface + + /** @inheritdoc */ + + /** @inheritdoc */ + on(hook, callback) { + if (!this._hooks[hook]) { + this._hooks[hook] = []; + } + + // @ts-ignore We assue the types are correct + this._hooks[hook].push(callback); + } + + /** @inheritdoc */ + + /** @inheritdoc */ + emit(hook, ...rest) { + if (this._hooks[hook]) { + // @ts-ignore we cannot enforce the callback to match the hook + this._hooks[hook].forEach(callback => callback(...rest)); + } + } + + /** Updates existing session based on the provided event */ + _updateSessionFromEvent(session, event) { + let crashed = false; + let errored = false; + const exceptions = event.exception && event.exception.values; + + if (exceptions) { + errored = true; + + for (const ex of exceptions) { + const mechanism = ex.mechanism; + if (mechanism && mechanism.handled === false) { + crashed = true; + break; + } + } + } + + // A session is updated and that session update is sent in only one of the two following scenarios: + // 1. Session with non terminal status and 0 errors + an error occurred -> Will set error count to 1 and send update + // 2. Session with non terminal status and 1 error + a crash occurred -> Will set status crashed and send update + const sessionNonTerminal = session.status === 'ok'; + const shouldUpdateAndSend = (sessionNonTerminal && session.errors === 0) || (sessionNonTerminal && crashed); + + if (shouldUpdateAndSend) { + updateSession(session, { + ...(crashed && { status: 'crashed' }), + errors: session.errors || Number(errored || crashed), + }); + this.captureSession(session); + } + } + + /** + * Determine if the client is finished processing. Returns a promise because it will wait `timeout` ms before saying + * "no" (resolving to `false`) in order to give the client a chance to potentially finish first. + * + * @param timeout The time, in ms, after which to resolve to `false` if the client is still busy. Passing `0` (or not + * passing anything) will make the promise wait as long as it takes for processing to finish before resolving to + * `true`. + * @returns A promise which will resolve to `true` if processing is already done or finishes before the timeout, and + * `false` otherwise + */ + _isClientDoneProcessing(timeout) { + return new SyncPromise(resolve => { + let ticked = 0; + const tick = 1; + + const interval = setInterval(() => { + if (this._numProcessing == 0) { + clearInterval(interval); + resolve(true); + } else { + ticked += tick; + if (timeout && ticked >= timeout) { + clearInterval(interval); + resolve(false); + } + } + }, tick); + }); + } + + /** Determines whether this SDK is enabled and a valid Dsn is present. */ + _isEnabled() { + return this.getOptions().enabled !== false && this._dsn !== undefined; + } + + /** + * Adds common information to events. + * + * The information includes release and environment from `options`, + * breadcrumbs and context (extra, tags and user) from the scope. + * + * Information that is already present in the event is never overwritten. For + * nested objects, such as the context, keys are merged. + * + * @param event The original event. + * @param hint May contain additional information about the original exception. + * @param scope A scope containing event metadata. + * @returns A new event with more information. + */ + _prepareEvent(event, hint, scope) { + const options = this.getOptions(); + const integrations = Object.keys(this._integrations); + if (!hint.integrations && integrations.length > 0) { + hint.integrations = integrations; + } + return prepareEvent(options, event, hint, scope); + } + + /** + * Processes the event and logs an error in case of rejection + * @param event + * @param hint + * @param scope + */ + _captureEvent(event, hint = {}, scope) { + return this._processEvent(event, hint, scope).then( + finalEvent => { + return finalEvent.event_id; + }, + reason => { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + // If something's gone wrong, log the error as a warning. If it's just us having used a `SentryError` for + // control flow, log just the message (no stack) as a log-level log. + const sentryError = reason ; + if (sentryError.logLevel === 'log') { + logger.log(sentryError.message); + } else { + logger.warn(sentryError); + } + } + return undefined; + }, + ); + } + + /** + * Processes an event (either error or message) and sends it to Sentry. + * + * This also adds breadcrumbs and context information to the event. However, + * platform specific meta data (such as the User's IP address) must be added + * by the SDK implementor. + * + * + * @param event The event to send to Sentry. + * @param hint May contain additional information about the original exception. + * @param scope A scope containing event metadata. + * @returns A SyncPromise that resolves with the event or rejects in case event was/will not be send. + */ + _processEvent(event, hint, scope) { + const options = this.getOptions(); + const { sampleRate } = options; + + if (!this._isEnabled()) { + return rejectedSyncPromise(new SentryError('SDK not enabled, will not capture event.', 'log')); + } + + const isTransaction = isTransactionEvent(event); + const isError = isErrorEvent(event); + const eventType = event.type || 'error'; + const beforeSendLabel = `before send for type \`${eventType}\``; + + // 1.0 === 100% events are sent + // 0.0 === 0% events are sent + // Sampling for transaction happens somewhere else + if (isError && typeof sampleRate === 'number' && Math.random() > sampleRate) { + this.recordDroppedEvent('sample_rate', 'error', event); + return rejectedSyncPromise( + new SentryError( + `Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`, + 'log', + ), + ); + } + + const dataCategory = eventType === 'replay_event' ? 'replay' : eventType; + + return this._prepareEvent(event, hint, scope) + .then(prepared => { + if (prepared === null) { + this.recordDroppedEvent('event_processor', dataCategory, event); + throw new SentryError('An event processor returned `null`, will not send event.', 'log'); + } + + const isInternalException = hint.data && (hint.data ).__sentry__ === true; + if (isInternalException) { + return prepared; + } + + const result = processBeforeSend(options, prepared, hint); + return _validateBeforeSendResult(result, beforeSendLabel); + }) + .then(processedEvent => { + if (processedEvent === null) { + this.recordDroppedEvent('before_send', dataCategory, event); + throw new SentryError(`${beforeSendLabel} returned \`null\`, will not send event.`, 'log'); + } + + const session = scope && scope.getSession(); + if (!isTransaction && session) { + this._updateSessionFromEvent(session, processedEvent); + } + + // None of the Sentry built event processor will update transaction name, + // so if the transaction name has been changed by an event processor, we know + // it has to come from custom event processor added by a user + const transactionInfo = processedEvent.transaction_info; + if (isTransaction && transactionInfo && processedEvent.transaction !== event.transaction) { + const source = 'custom'; + processedEvent.transaction_info = { + ...transactionInfo, + source, + }; + } + + this.sendEvent(processedEvent, hint); + return processedEvent; + }) + .then(null, reason => { + if (reason instanceof SentryError) { + throw reason; + } + + this.captureException(reason, { + data: { + __sentry__: true, + }, + originalException: reason, + }); + throw new SentryError( + `Event processing pipeline threw an error, original event will not be sent. Details have been sent as a new event.\nReason: ${reason}`, + ); + }); + } + + /** + * Occupies the client with processing and event + */ + _process(promise) { + this._numProcessing++; + void promise.then( + value => { + this._numProcessing--; + return value; + }, + reason => { + this._numProcessing--; + return reason; + }, + ); + } + + /** + * @inheritdoc + */ + _sendEnvelope(envelope) { + if (this._transport && this._dsn) { + this.emit('beforeEnvelope', envelope); + + return this._transport.send(envelope).then(null, reason => { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Error while sending event:', reason); + }); + } else { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Transport disabled'); + } + } + + /** + * Clears outcomes on this client and returns them. + */ + _clearOutcomes() { + const outcomes = this._outcomes; + this._outcomes = {}; + return Object.keys(outcomes).map(key => { + const [reason, category] = key.split(':') ; + return { + reason, + category, + quantity: outcomes[key], + }; + }); + } + + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types + +} + +/** + * Verifies that return value of configured `beforeSend` or `beforeSendTransaction` is of expected type, and returns the value if so. + */ +function _validateBeforeSendResult( + beforeSendResult, + beforeSendLabel, +) { + const invalidValueError = `${beforeSendLabel} must return \`null\` or a valid event.`; + if (isThenable(beforeSendResult)) { + return beforeSendResult.then( + event => { + if (!isPlainObject(event) && event !== null) { + throw new SentryError(invalidValueError); + } + return event; + }, + e => { + throw new SentryError(`${beforeSendLabel} rejected with ${e}`); + }, + ); + } else if (!isPlainObject(beforeSendResult) && beforeSendResult !== null) { + throw new SentryError(invalidValueError); + } + return beforeSendResult; +} + +/** + * Process the matching `beforeSendXXX` callback. + */ +function processBeforeSend( + options, + event, + hint, +) { + const { beforeSend, beforeSendTransaction } = options; + + if (isErrorEvent(event) && beforeSend) { + return beforeSend(event, hint); + } + + if (isTransactionEvent(event) && beforeSendTransaction) { + return beforeSendTransaction(event, hint); + } + + return event; +} + +function isErrorEvent(event) { + return event.type === undefined; +} + +function isTransactionEvent(event) { + return event.type === 'transaction'; +} + +export { BaseClient }; +//# sourceMappingURL=baseclient.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/constants.js b/shared/logger/node_modules/@sentry/core/esm/constants.js new file mode 100644 index 0000000..2c81ff6 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/constants.js @@ -0,0 +1,4 @@ +const DEFAULT_ENVIRONMENT = 'production'; + +export { DEFAULT_ENVIRONMENT }; +//# sourceMappingURL=constants.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/envelope.js b/shared/logger/node_modules/@sentry/core/esm/envelope.js new file mode 100644 index 0000000..2d1047d --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/envelope.js @@ -0,0 +1,74 @@ +import { getSdkMetadataForEnvelopeHeader, dsnToString, createEnvelope, createEventEnvelopeHeaders } from '@sentry/utils'; + +/** + * Apply SdkInfo (name, version, packages, integrations) to the corresponding event key. + * Merge with existing data if any. + **/ +function enhanceEventWithSdkInfo(event, sdkInfo) { + if (!sdkInfo) { + return event; + } + event.sdk = event.sdk || {}; + event.sdk.name = event.sdk.name || sdkInfo.name; + event.sdk.version = event.sdk.version || sdkInfo.version; + event.sdk.integrations = [...(event.sdk.integrations || []), ...(sdkInfo.integrations || [])]; + event.sdk.packages = [...(event.sdk.packages || []), ...(sdkInfo.packages || [])]; + return event; +} + +/** Creates an envelope from a Session */ +function createSessionEnvelope( + session, + dsn, + metadata, + tunnel, +) { + const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata); + const envelopeHeaders = { + sent_at: new Date().toISOString(), + ...(sdkInfo && { sdk: sdkInfo }), + ...(!!tunnel && { dsn: dsnToString(dsn) }), + }; + + const envelopeItem = + 'aggregates' in session ? [{ type: 'sessions' }, session] : [{ type: 'session' }, session.toJSON()]; + + return createEnvelope(envelopeHeaders, [envelopeItem]); +} + +/** + * Create an Envelope from an event. + */ +function createEventEnvelope( + event, + dsn, + metadata, + tunnel, +) { + const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata); + + /* + Note: Due to TS, event.type may be `replay_event`, theoretically. + In practice, we never call `createEventEnvelope` with `replay_event` type, + and we'd have to adjut a looot of types to make this work properly. + We want to avoid casting this around, as that could lead to bugs (e.g. when we add another type) + So the safe choice is to really guard against the replay_event type here. + */ + const eventType = event.type && event.type !== 'replay_event' ? event.type : 'event'; + + enhanceEventWithSdkInfo(event, metadata && metadata.sdk); + + const envelopeHeaders = createEventEnvelopeHeaders(event, sdkInfo, tunnel, dsn); + + // Prevent this data (which, if it exists, was used in earlier steps in the processing pipeline) from being sent to + // sentry. (Note: Our use of this property comes and goes with whatever we might be debugging, whatever hacks we may + // have temporarily added, etc. Even if we don't happen to be using it at some point in the future, let's not get rid + // of this `delete`, lest we miss putting it back in the next time the property is in use.) + delete event.sdkProcessingMetadata; + + const eventItem = [{ type: eventType }, event]; + return createEnvelope(envelopeHeaders, [eventItem]); +} + +export { createEventEnvelope, createSessionEnvelope }; +//# sourceMappingURL=envelope.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/exports.js b/shared/logger/node_modules/@sentry/core/esm/exports.js new file mode 100644 index 0000000..a7d8592 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/exports.js @@ -0,0 +1,193 @@ +import { logger, uuid4 } from '@sentry/utils'; +import { getCurrentHub } from './hub.js'; + +// Note: All functions in this file are typed with a return value of `ReturnType<Hub[HUB_FUNCTION]>`, +// where HUB_FUNCTION is some method on the Hub class. +// +// This is done to make sure the top level SDK methods stay in sync with the hub methods. +// Although every method here has an explicit return type, some of them (that map to void returns) do not +// contain `return` keywords. This is done to save on bundle size, as `return` is not minifiable. + +/** + * Captures an exception event and sends it to Sentry. + * + * @param exception An exception-like object. + * @param captureContext Additional scope data to apply to exception event. + * @returns The generated eventId. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types +function captureException(exception, captureContext) { + return getCurrentHub().captureException(exception, { captureContext }); +} + +/** + * Captures a message event and sends it to Sentry. + * + * @param message The message to send to Sentry. + * @param Severity Define the level of the message. + * @returns The generated eventId. + */ +function captureMessage( + message, + // eslint-disable-next-line deprecation/deprecation + captureContext, +) { + // This is necessary to provide explicit scopes upgrade, without changing the original + // arity of the `captureMessage(message, level)` method. + const level = typeof captureContext === 'string' ? captureContext : undefined; + const context = typeof captureContext !== 'string' ? { captureContext } : undefined; + return getCurrentHub().captureMessage(message, level, context); +} + +/** + * Captures a manually created event and sends it to Sentry. + * + * @param event The event to send to Sentry. + * @returns The generated eventId. + */ +function captureEvent(event, hint) { + return getCurrentHub().captureEvent(event, hint); +} + +/** + * Callback to set context information onto the scope. + * @param callback Callback function that receives Scope. + */ +function configureScope(callback) { + getCurrentHub().configureScope(callback); +} + +/** + * Records a new breadcrumb which will be attached to future events. + * + * Breadcrumbs will be added to subsequent events to provide more context on + * user's actions prior to an error or crash. + * + * @param breadcrumb The breadcrumb to record. + */ +function addBreadcrumb(breadcrumb) { + getCurrentHub().addBreadcrumb(breadcrumb); +} + +/** + * Sets context data with the given name. + * @param name of the context + * @param context Any kind of data. This data will be normalized. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function setContext(name, context) { + getCurrentHub().setContext(name, context); +} + +/** + * Set an object that will be merged sent as extra data with the event. + * @param extras Extras object to merge into current context. + */ +function setExtras(extras) { + getCurrentHub().setExtras(extras); +} + +/** + * Set key:value that will be sent as extra data with the event. + * @param key String of extra + * @param extra Any kind of data. This data will be normalized. + */ +function setExtra(key, extra) { + getCurrentHub().setExtra(key, extra); +} + +/** + * Set an object that will be merged sent as tags data with the event. + * @param tags Tags context object to merge into current context. + */ +function setTags(tags) { + getCurrentHub().setTags(tags); +} + +/** + * Set key:value that will be sent as tags data with the event. + * + * Can also be used to unset a tag, by passing `undefined`. + * + * @param key String key of tag + * @param value Value of tag + */ +function setTag(key, value) { + getCurrentHub().setTag(key, value); +} + +/** + * Updates user context information for future events. + * + * @param user User context object to be set in the current context. Pass `null` to unset the user. + */ +function setUser(user) { + getCurrentHub().setUser(user); +} + +/** + * Creates a new scope with and executes the given operation within. + * The scope is automatically removed once the operation + * finishes or throws. + * + * This is essentially a convenience function for: + * + * pushScope(); + * callback(); + * popScope(); + * + * @param callback that will be enclosed into push/popScope. + */ +function withScope(callback) { + getCurrentHub().withScope(callback); +} + +/** + * Starts a new `Transaction` and returns it. This is the entry point to manual tracing instrumentation. + * + * A tree structure can be built by adding child spans to the transaction, and child spans to other spans. To start a + * new child span within the transaction or any span, call the respective `.startChild()` method. + * + * Every child span must be finished before the transaction is finished, otherwise the unfinished spans are discarded. + * + * The transaction must be finished with a call to its `.finish()` method, at which point the transaction with all its + * finished child spans will be sent to Sentry. + * + * NOTE: This function should only be used for *manual* instrumentation. Auto-instrumentation should call + * `startTransaction` directly on the hub. + * + * @param context Properties of the new `Transaction`. + * @param customSamplingContext Information given to the transaction sampling function (along with context-dependent + * default values). See {@link Options.tracesSampler}. + * + * @returns The transaction which was just started + */ +function startTransaction( + context, + customSamplingContext, +) { + return getCurrentHub().startTransaction({ ...context }, customSamplingContext); +} + +/** + * Create a cron monitor check in and send it to Sentry. + * + * @param checkIn An object that describes a check in. + * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want + * to create a monitor automatically when sending a check in. + */ +function captureCheckIn(checkIn, upsertMonitorConfig) { + const client = getCurrentHub().getClient(); + if (!client) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot capture check-in. No client defined.'); + } else if (!client.captureCheckIn) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot capture check-in. Client does not support sending check-ins.'); + } else { + return client.captureCheckIn(checkIn, upsertMonitorConfig); + } + + return uuid4(); +} + +export { addBreadcrumb, captureCheckIn, captureEvent, captureException, captureMessage, configureScope, setContext, setExtra, setExtras, setTag, setTags, setUser, startTransaction, withScope }; +//# sourceMappingURL=exports.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/hub.js b/shared/logger/node_modules/@sentry/core/esm/hub.js new file mode 100644 index 0000000..2dd6764 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/hub.js @@ -0,0 +1,564 @@ +import { uuid4, dateTimestampInSeconds, consoleSandbox, logger, GLOBAL_OBJ, getGlobalSingleton } from '@sentry/utils'; +import { DEFAULT_ENVIRONMENT } from './constants.js'; +import { Scope } from './scope.js'; +import { closeSession, makeSession, updateSession } from './session.js'; + +/** + * API compatibility version of this hub. + * + * WARNING: This number should only be increased when the global interface + * changes and new methods are introduced. + * + * @hidden + */ +const API_VERSION = 4; + +/** + * Default maximum number of breadcrumbs added to an event. Can be overwritten + * with {@link Options.maxBreadcrumbs}. + */ +const DEFAULT_BREADCRUMBS = 100; + +/** + * @inheritDoc + */ +class Hub { + /** Is a {@link Layer}[] containing the client and scope */ + + /** Contains the last event id of a captured event. */ + + /** + * Creates a new instance of the hub, will push one {@link Layer} into the + * internal stack on creation. + * + * @param client bound to the hub. + * @param scope bound to the hub. + * @param version number, higher number means higher priority. + */ + constructor(client, scope = new Scope(), _version = API_VERSION) {this._version = _version; + this._stack = [{ scope }]; + if (client) { + this.bindClient(client); + } + } + + /** + * @inheritDoc + */ + isOlderThan(version) { + return this._version < version; + } + + /** + * @inheritDoc + */ + bindClient(client) { + const top = this.getStackTop(); + top.client = client; + if (client && client.setupIntegrations) { + client.setupIntegrations(); + } + } + + /** + * @inheritDoc + */ + pushScope() { + // We want to clone the content of prev scope + const scope = Scope.clone(this.getScope()); + this.getStack().push({ + client: this.getClient(), + scope, + }); + return scope; + } + + /** + * @inheritDoc + */ + popScope() { + if (this.getStack().length <= 1) return false; + return !!this.getStack().pop(); + } + + /** + * @inheritDoc + */ + withScope(callback) { + const scope = this.pushScope(); + try { + callback(scope); + } finally { + this.popScope(); + } + } + + /** + * @inheritDoc + */ + getClient() { + return this.getStackTop().client ; + } + + /** Returns the scope of the top stack. */ + getScope() { + return this.getStackTop().scope; + } + + /** Returns the scope stack for domains or the process. */ + getStack() { + return this._stack; + } + + /** Returns the topmost scope layer in the order domain > local > process. */ + getStackTop() { + return this._stack[this._stack.length - 1]; + } + + /** + * @inheritDoc + */ + captureException(exception, hint) { + const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); + const syntheticException = new Error('Sentry syntheticException'); + this._withClient((client, scope) => { + client.captureException( + exception, + { + originalException: exception, + syntheticException, + ...hint, + event_id: eventId, + }, + scope, + ); + }); + return eventId; + } + + /** + * @inheritDoc + */ + captureMessage( + message, + // eslint-disable-next-line deprecation/deprecation + level, + hint, + ) { + const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); + const syntheticException = new Error(message); + this._withClient((client, scope) => { + client.captureMessage( + message, + level, + { + originalException: message, + syntheticException, + ...hint, + event_id: eventId, + }, + scope, + ); + }); + return eventId; + } + + /** + * @inheritDoc + */ + captureEvent(event, hint) { + const eventId = hint && hint.event_id ? hint.event_id : uuid4(); + if (!event.type) { + this._lastEventId = eventId; + } + + this._withClient((client, scope) => { + client.captureEvent(event, { ...hint, event_id: eventId }, scope); + }); + return eventId; + } + + /** + * @inheritDoc + */ + lastEventId() { + return this._lastEventId; + } + + /** + * @inheritDoc + */ + addBreadcrumb(breadcrumb, hint) { + const { scope, client } = this.getStackTop(); + + if (!client) return; + + const { beforeBreadcrumb = null, maxBreadcrumbs = DEFAULT_BREADCRUMBS } = + (client.getOptions && client.getOptions()) || {}; + + if (maxBreadcrumbs <= 0) return; + + const timestamp = dateTimestampInSeconds(); + const mergedBreadcrumb = { timestamp, ...breadcrumb }; + const finalBreadcrumb = beforeBreadcrumb + ? (consoleSandbox(() => beforeBreadcrumb(mergedBreadcrumb, hint)) ) + : mergedBreadcrumb; + + if (finalBreadcrumb === null) return; + + if (client.emit) { + client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint); + } + + scope.addBreadcrumb(finalBreadcrumb, maxBreadcrumbs); + } + + /** + * @inheritDoc + */ + setUser(user) { + this.getScope().setUser(user); + } + + /** + * @inheritDoc + */ + setTags(tags) { + this.getScope().setTags(tags); + } + + /** + * @inheritDoc + */ + setExtras(extras) { + this.getScope().setExtras(extras); + } + + /** + * @inheritDoc + */ + setTag(key, value) { + this.getScope().setTag(key, value); + } + + /** + * @inheritDoc + */ + setExtra(key, extra) { + this.getScope().setExtra(key, extra); + } + + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + setContext(name, context) { + this.getScope().setContext(name, context); + } + + /** + * @inheritDoc + */ + configureScope(callback) { + const { scope, client } = this.getStackTop(); + if (client) { + callback(scope); + } + } + + /** + * @inheritDoc + */ + run(callback) { + const oldHub = makeMain(this); + try { + callback(this); + } finally { + makeMain(oldHub); + } + } + + /** + * @inheritDoc + */ + getIntegration(integration) { + const client = this.getClient(); + if (!client) return null; + try { + return client.getIntegration(integration); + } catch (_oO) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn(`Cannot retrieve integration ${integration.id} from the current Hub`); + return null; + } + } + + /** + * @inheritDoc + */ + startTransaction(context, customSamplingContext) { + const result = this._callExtensionMethod('startTransaction', context, customSamplingContext); + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && !result) { + // eslint-disable-next-line no-console + console.warn(`Tracing extension 'startTransaction' has not been added. Call 'addTracingExtensions' before calling 'init': +Sentry.addTracingExtensions(); +Sentry.init({...}); +`); + } + + return result; + } + + /** + * @inheritDoc + */ + traceHeaders() { + return this._callExtensionMethod('traceHeaders'); + } + + /** + * @inheritDoc + */ + captureSession(endSession = false) { + // both send the update and pull the session from the scope + if (endSession) { + return this.endSession(); + } + + // only send the update + this._sendSessionUpdate(); + } + + /** + * @inheritDoc + */ + endSession() { + const layer = this.getStackTop(); + const scope = layer.scope; + const session = scope.getSession(); + if (session) { + closeSession(session); + } + this._sendSessionUpdate(); + + // the session is over; take it off of the scope + scope.setSession(); + } + + /** + * @inheritDoc + */ + startSession(context) { + const { scope, client } = this.getStackTop(); + const { release, environment = DEFAULT_ENVIRONMENT } = (client && client.getOptions()) || {}; + + // Will fetch userAgent if called from browser sdk + const { userAgent } = GLOBAL_OBJ.navigator || {}; + + const session = makeSession({ + release, + environment, + user: scope.getUser(), + ...(userAgent && { userAgent }), + ...context, + }); + + // End existing session if there's one + const currentSession = scope.getSession && scope.getSession(); + if (currentSession && currentSession.status === 'ok') { + updateSession(currentSession, { status: 'exited' }); + } + this.endSession(); + + // Afterwards we set the new session on the scope + scope.setSession(session); + + return session; + } + + /** + * Returns if default PII should be sent to Sentry and propagated in ourgoing requests + * when Tracing is used. + */ + shouldSendDefaultPii() { + const client = this.getClient(); + const options = client && client.getOptions(); + return Boolean(options && options.sendDefaultPii); + } + + /** + * Sends the current Session on the scope + */ + _sendSessionUpdate() { + const { scope, client } = this.getStackTop(); + + const session = scope.getSession(); + if (session && client && client.captureSession) { + client.captureSession(session); + } + } + + /** + * Internal helper function to call a method on the top client if it exists. + * + * @param method The method to call on the client. + * @param args Arguments to pass to the client function. + */ + _withClient(callback) { + const { scope, client } = this.getStackTop(); + if (client) { + callback(client, scope); + } + } + + /** + * Calls global extension method and binding current instance to the function call + */ + // @ts-ignore Function lacks ending return statement and return type does not include 'undefined'. ts(2366) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + _callExtensionMethod(method, ...args) { + const carrier = getMainCarrier(); + const sentry = carrier.__SENTRY__; + if (sentry && sentry.extensions && typeof sentry.extensions[method] === 'function') { + return sentry.extensions[method].apply(this, args); + } + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn(`Extension method ${method} couldn't be found, doing nothing.`); + } +} + +/** + * Returns the global shim registry. + * + * FIXME: This function is problematic, because despite always returning a valid Carrier, + * it has an optional `__SENTRY__` property, which then in turn requires us to always perform an unnecessary check + * at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there. + **/ +function getMainCarrier() { + GLOBAL_OBJ.__SENTRY__ = GLOBAL_OBJ.__SENTRY__ || { + extensions: {}, + hub: undefined, + }; + return GLOBAL_OBJ; +} + +/** + * Replaces the current main hub with the passed one on the global object + * + * @returns The old replaced hub + */ +function makeMain(hub) { + const registry = getMainCarrier(); + const oldHub = getHubFromCarrier(registry); + setHubOnCarrier(registry, hub); + return oldHub; +} + +/** + * Returns the default hub instance. + * + * If a hub is already registered in the global carrier but this module + * contains a more recent version, it replaces the registered version. + * Otherwise, the currently registered hub will be returned. + */ +function getCurrentHub() { + // Get main carrier (global for every environment) + const registry = getMainCarrier(); + + if (registry.__SENTRY__ && registry.__SENTRY__.acs) { + const hub = registry.__SENTRY__.acs.getCurrentHub(); + + if (hub) { + return hub; + } + } + + // Return hub that lives on a global object + return getGlobalHub(registry); +} + +function getGlobalHub(registry = getMainCarrier()) { + // If there's no hub, or its an old API, assign a new one + if (!hasHubOnCarrier(registry) || getHubFromCarrier(registry).isOlderThan(API_VERSION)) { + setHubOnCarrier(registry, new Hub()); + } + + // Return hub that lives on a global object + return getHubFromCarrier(registry); +} + +/** + * @private Private API with no semver guarantees! + * + * If the carrier does not contain a hub, a new hub is created with the global hub client and scope. + */ +function ensureHubOnCarrier(carrier, parent = getGlobalHub()) { + // If there's no hub on current domain, or it's an old API, assign a new one + if (!hasHubOnCarrier(carrier) || getHubFromCarrier(carrier).isOlderThan(API_VERSION)) { + const globalHubTopStack = parent.getStackTop(); + setHubOnCarrier(carrier, new Hub(globalHubTopStack.client, Scope.clone(globalHubTopStack.scope))); + } +} + +/** + * @private Private API with no semver guarantees! + * + * Sets the global async context strategy + */ +function setAsyncContextStrategy(strategy) { + // Get main carrier (global for every environment) + const registry = getMainCarrier(); + registry.__SENTRY__ = registry.__SENTRY__ || {}; + registry.__SENTRY__.acs = strategy; +} + +/** + * Runs the supplied callback in its own async context. Async Context strategies are defined per SDK. + * + * @param callback The callback to run in its own async context + * @param options Options to pass to the async context strategy + * @returns The result of the callback + */ +function runWithAsyncContext(callback, options = {}) { + const registry = getMainCarrier(); + + if (registry.__SENTRY__ && registry.__SENTRY__.acs) { + return registry.__SENTRY__.acs.runWithAsyncContext(callback, options); + } + + // if there was no strategy, fallback to just calling the callback + return callback(); +} + +/** + * This will tell whether a carrier has a hub on it or not + * @param carrier object + */ +function hasHubOnCarrier(carrier) { + return !!(carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub); +} + +/** + * This will create a new {@link Hub} and add to the passed object on + * __SENTRY__.hub. + * @param carrier object + * @hidden + */ +function getHubFromCarrier(carrier) { + return getGlobalSingleton('hub', () => new Hub(), carrier); +} + +/** + * This will set passed {@link Hub} on the passed object's __SENTRY__.hub attribute + * @param carrier object + * @param hub Hub + * @returns A boolean indicating success or failure + */ +function setHubOnCarrier(carrier, hub) { + if (!carrier) return false; + const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {}); + __SENTRY__.hub = hub; + return true; +} + +export { API_VERSION, Hub, ensureHubOnCarrier, getCurrentHub, getHubFromCarrier, getMainCarrier, makeMain, runWithAsyncContext, setAsyncContextStrategy, setHubOnCarrier }; +//# sourceMappingURL=hub.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/integration.js b/shared/logger/node_modules/@sentry/core/esm/integration.js new file mode 100644 index 0000000..db16727 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/integration.js @@ -0,0 +1,112 @@ +import { arrayify, logger } from '@sentry/utils'; +import { getCurrentHub } from './hub.js'; +import { addGlobalEventProcessor } from './scope.js'; + +const installedIntegrations = []; + +/** Map of integrations assigned to a client */ + +/** + * Remove duplicates from the given array, preferring the last instance of any duplicate. Not guaranteed to + * preseve the order of integrations in the array. + * + * @private + */ +function filterDuplicates(integrations) { + const integrationsByName = {}; + + integrations.forEach(currentInstance => { + const { name } = currentInstance; + + const existingInstance = integrationsByName[name]; + + // We want integrations later in the array to overwrite earlier ones of the same type, except that we never want a + // default instance to overwrite an existing user instance + if (existingInstance && !existingInstance.isDefaultInstance && currentInstance.isDefaultInstance) { + return; + } + + integrationsByName[name] = currentInstance; + }); + + return Object.keys(integrationsByName).map(k => integrationsByName[k]); +} + +/** Gets integrations to install */ +function getIntegrationsToSetup(options) { + const defaultIntegrations = options.defaultIntegrations || []; + const userIntegrations = options.integrations; + + // We flag default instances, so that later we can tell them apart from any user-created instances of the same class + defaultIntegrations.forEach(integration => { + integration.isDefaultInstance = true; + }); + + let integrations; + + if (Array.isArray(userIntegrations)) { + integrations = [...defaultIntegrations, ...userIntegrations]; + } else if (typeof userIntegrations === 'function') { + integrations = arrayify(userIntegrations(defaultIntegrations)); + } else { + integrations = defaultIntegrations; + } + + const finalIntegrations = filterDuplicates(integrations); + + // The `Debug` integration prints copies of the `event` and `hint` which will be passed to `beforeSend` or + // `beforeSendTransaction`. It therefore has to run after all other integrations, so that the changes of all event + // processors will be reflected in the printed values. For lack of a more elegant way to guarantee that, we therefore + // locate it and, assuming it exists, pop it out of its current spot and shove it onto the end of the array. + const debugIndex = findIndex(finalIntegrations, integration => integration.name === 'Debug'); + if (debugIndex !== -1) { + const [debugInstance] = finalIntegrations.splice(debugIndex, 1); + finalIntegrations.push(debugInstance); + } + + return finalIntegrations; +} + +/** + * Given a list of integration instances this installs them all. When `withDefaults` is set to `true` then all default + * integrations are added unless they were already provided before. + * @param integrations array of integration instances + * @param withDefault should enable default integrations + */ +function setupIntegrations(integrations) { + const integrationIndex = {}; + + integrations.forEach(integration => { + // guard against empty provided integrations + if (integration) { + setupIntegration(integration, integrationIndex); + } + }); + + return integrationIndex; +} + +/** Setup a single integration. */ +function setupIntegration(integration, integrationIndex) { + integrationIndex[integration.name] = integration; + + if (installedIntegrations.indexOf(integration.name) === -1) { + integration.setupOnce(addGlobalEventProcessor, getCurrentHub); + installedIntegrations.push(integration.name); + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`Integration installed: ${integration.name}`); + } +} + +// Polyfill for Array.findIndex(), which is not supported in ES5 +function findIndex(arr, callback) { + for (let i = 0; i < arr.length; i++) { + if (callback(arr[i]) === true) { + return i; + } + } + + return -1; +} + +export { getIntegrationsToSetup, installedIntegrations, setupIntegration, setupIntegrations }; +//# sourceMappingURL=integration.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/integrations/functiontostring.js b/shared/logger/node_modules/@sentry/core/esm/integrations/functiontostring.js new file mode 100644 index 0000000..dfe714a --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/integrations/functiontostring.js @@ -0,0 +1,39 @@ +import { getOriginalFunction } from '@sentry/utils'; + +let originalFunctionToString; + +/** Patch toString calls to return proper name for wrapped functions */ +class FunctionToString {constructor() { FunctionToString.prototype.__init.call(this); } + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'FunctionToString';} + + /** + * @inheritDoc + */ + __init() {this.name = FunctionToString.id;} + + /** + * @inheritDoc + */ + setupOnce() { + // eslint-disable-next-line @typescript-eslint/unbound-method + originalFunctionToString = Function.prototype.toString; + + // intrinsics (like Function.prototype) might be immutable in some environments + // e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal) + try { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Function.prototype.toString = function ( ...args) { + const context = getOriginalFunction(this) || this; + return originalFunctionToString.apply(context, args); + }; + } catch (e) { + // ignore errors here, just don't patch this + } + } +} FunctionToString.__initStatic(); + +export { FunctionToString }; +//# sourceMappingURL=functiontostring.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/integrations/inboundfilters.js b/shared/logger/node_modules/@sentry/core/esm/integrations/inboundfilters.js new file mode 100644 index 0000000..a673271 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/integrations/inboundfilters.js @@ -0,0 +1,213 @@ +import { logger, getEventDescription, stringMatchesSomePattern } from '@sentry/utils'; + +// "Script error." is hard coded into browsers for errors that it can't read. +// this is the result of a script being pulled in from an external domain and CORS. +const DEFAULT_IGNORE_ERRORS = [/^Script error\.?$/, /^Javascript error: Script error\.? on line 0$/]; + +const DEFAULT_IGNORE_TRANSACTIONS = [ + /^.*healthcheck.*$/, + /^.*healthy.*$/, + /^.*live.*$/, + /^.*ready.*$/, + /^.*heartbeat.*$/, + /^.*\/health$/, + /^.*\/healthz$/, +]; + +/** Options for the InboundFilters integration */ + +/** Inbound filters configurable by the user */ +class InboundFilters { + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'InboundFilters';} + + /** + * @inheritDoc + */ + __init() {this.name = InboundFilters.id;} + + constructor( _options = {}) {this._options = _options;InboundFilters.prototype.__init.call(this);} + + /** + * @inheritDoc + */ + setupOnce(addGlobalEventProcessor, getCurrentHub) { + const eventProcess = (event) => { + const hub = getCurrentHub(); + if (hub) { + const self = hub.getIntegration(InboundFilters); + if (self) { + const client = hub.getClient(); + const clientOptions = client ? client.getOptions() : {}; + const options = _mergeOptions(self._options, clientOptions); + return _shouldDropEvent(event, options) ? null : event; + } + } + return event; + }; + + eventProcess.id = this.name; + addGlobalEventProcessor(eventProcess); + } +} InboundFilters.__initStatic(); + +/** JSDoc */ +function _mergeOptions( + internalOptions = {}, + clientOptions = {}, +) { + return { + allowUrls: [...(internalOptions.allowUrls || []), ...(clientOptions.allowUrls || [])], + denyUrls: [...(internalOptions.denyUrls || []), ...(clientOptions.denyUrls || [])], + ignoreErrors: [ + ...(internalOptions.ignoreErrors || []), + ...(clientOptions.ignoreErrors || []), + ...(internalOptions.disableErrorDefaults ? [] : DEFAULT_IGNORE_ERRORS), + ], + ignoreTransactions: [ + ...(internalOptions.ignoreTransactions || []), + ...(clientOptions.ignoreTransactions || []), + ...(internalOptions.disableTransactionDefaults ? [] : DEFAULT_IGNORE_TRANSACTIONS), + ], + ignoreInternal: internalOptions.ignoreInternal !== undefined ? internalOptions.ignoreInternal : true, + }; +} + +/** JSDoc */ +function _shouldDropEvent(event, options) { + if (options.ignoreInternal && _isSentryError(event)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`); + return true; + } + if (_isIgnoredError(event, options.ignoreErrors)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`, + ); + return true; + } + if (_isIgnoredTransaction(event, options.ignoreTransactions)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `Event dropped due to being matched by \`ignoreTransactions\` option.\nEvent: ${getEventDescription(event)}`, + ); + return true; + } + if (_isDeniedUrl(event, options.denyUrls)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription( + event, + )}.\nUrl: ${_getEventFilterUrl(event)}`, + ); + return true; + } + if (!_isAllowedUrl(event, options.allowUrls)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription( + event, + )}.\nUrl: ${_getEventFilterUrl(event)}`, + ); + return true; + } + return false; +} + +function _isIgnoredError(event, ignoreErrors) { + // If event.type, this is not an error + if (event.type || !ignoreErrors || !ignoreErrors.length) { + return false; + } + + return _getPossibleEventMessages(event).some(message => stringMatchesSomePattern(message, ignoreErrors)); +} + +function _isIgnoredTransaction(event, ignoreTransactions) { + if (event.type !== 'transaction' || !ignoreTransactions || !ignoreTransactions.length) { + return false; + } + + const name = event.transaction; + return name ? stringMatchesSomePattern(name, ignoreTransactions) : false; +} + +function _isDeniedUrl(event, denyUrls) { + // TODO: Use Glob instead? + if (!denyUrls || !denyUrls.length) { + return false; + } + const url = _getEventFilterUrl(event); + return !url ? false : stringMatchesSomePattern(url, denyUrls); +} + +function _isAllowedUrl(event, allowUrls) { + // TODO: Use Glob instead? + if (!allowUrls || !allowUrls.length) { + return true; + } + const url = _getEventFilterUrl(event); + return !url ? true : stringMatchesSomePattern(url, allowUrls); +} + +function _getPossibleEventMessages(event) { + if (event.message) { + return [event.message]; + } + if (event.exception) { + const { values } = event.exception; + try { + const { type = '', value = '' } = (values && values[values.length - 1]) || {}; + return [`${value}`, `${type}: ${value}`]; + } catch (oO) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(`Cannot extract message for event ${getEventDescription(event)}`); + return []; + } + } + return []; +} + +function _isSentryError(event) { + try { + // @ts-ignore can't be a sentry error if undefined + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return event.exception.values[0].type === 'SentryError'; + } catch (e) { + // ignore + } + return false; +} + +function _getLastValidUrl(frames = []) { + for (let i = frames.length - 1; i >= 0; i--) { + const frame = frames[i]; + + if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') { + return frame.filename || null; + } + } + + return null; +} + +function _getEventFilterUrl(event) { + try { + let frames; + try { + // @ts-ignore we only care about frames if the whole thing here is defined + frames = event.exception.values[0].stacktrace.frames; + } catch (e) { + // ignore + } + return frames ? _getLastValidUrl(frames) : null; + } catch (oO) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(`Cannot extract url for event ${getEventDescription(event)}`); + return null; + } +} + +export { InboundFilters, _mergeOptions, _shouldDropEvent }; +//# sourceMappingURL=inboundfilters.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/scope.js b/shared/logger/node_modules/@sentry/core/esm/scope.js new file mode 100644 index 0000000..0869323 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/scope.js @@ -0,0 +1,555 @@ +import { isPlainObject, dateTimestampInSeconds, SyncPromise, logger, isThenable, arrayify, getGlobalSingleton } from '@sentry/utils'; +import { updateSession } from './session.js'; + +/** + * Default value for maximum number of breadcrumbs added to an event. + */ +const DEFAULT_MAX_BREADCRUMBS = 100; + +/** + * Holds additional event information. {@link Scope.applyToEvent} will be + * called by the client before an event will be sent. + */ +class Scope { + /** Flag if notifying is happening. */ + + /** Callback for client to receive scope changes. */ + + /** Callback list that will be called after {@link applyToEvent}. */ + + /** Array of breadcrumbs. */ + + /** User */ + + /** Tags */ + + /** Extra */ + + /** Contexts */ + + /** Attachments */ + + /** + * A place to stash data which is needed at some point in the SDK's event processing pipeline but which shouldn't get + * sent to Sentry + */ + + /** Fingerprint */ + + /** Severity */ + // eslint-disable-next-line deprecation/deprecation + + /** Transaction Name */ + + /** Span */ + + /** Session */ + + /** Request Mode Session Status */ + + // NOTE: Any field which gets added here should get added not only to the constructor but also to the `clone` method. + + constructor() { + this._notifyingListeners = false; + this._scopeListeners = []; + this._eventProcessors = []; + this._breadcrumbs = []; + this._attachments = []; + this._user = {}; + this._tags = {}; + this._extra = {}; + this._contexts = {}; + this._sdkProcessingMetadata = {}; + } + + /** + * Inherit values from the parent scope. + * @param scope to clone. + */ + static clone(scope) { + const newScope = new Scope(); + if (scope) { + newScope._breadcrumbs = [...scope._breadcrumbs]; + newScope._tags = { ...scope._tags }; + newScope._extra = { ...scope._extra }; + newScope._contexts = { ...scope._contexts }; + newScope._user = scope._user; + newScope._level = scope._level; + newScope._span = scope._span; + newScope._session = scope._session; + newScope._transactionName = scope._transactionName; + newScope._fingerprint = scope._fingerprint; + newScope._eventProcessors = [...scope._eventProcessors]; + newScope._requestSession = scope._requestSession; + newScope._attachments = [...scope._attachments]; + newScope._sdkProcessingMetadata = { ...scope._sdkProcessingMetadata }; + } + return newScope; + } + + /** + * Add internal on change listener. Used for sub SDKs that need to store the scope. + * @hidden + */ + addScopeListener(callback) { + this._scopeListeners.push(callback); + } + + /** + * @inheritDoc + */ + addEventProcessor(callback) { + this._eventProcessors.push(callback); + return this; + } + + /** + * @inheritDoc + */ + setUser(user) { + this._user = user || {}; + if (this._session) { + updateSession(this._session, { user }); + } + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + getUser() { + return this._user; + } + + /** + * @inheritDoc + */ + getRequestSession() { + return this._requestSession; + } + + /** + * @inheritDoc + */ + setRequestSession(requestSession) { + this._requestSession = requestSession; + return this; + } + + /** + * @inheritDoc + */ + setTags(tags) { + this._tags = { + ...this._tags, + ...tags, + }; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setTag(key, value) { + this._tags = { ...this._tags, [key]: value }; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setExtras(extras) { + this._extra = { + ...this._extra, + ...extras, + }; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setExtra(key, extra) { + this._extra = { ...this._extra, [key]: extra }; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setFingerprint(fingerprint) { + this._fingerprint = fingerprint; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setLevel( + // eslint-disable-next-line deprecation/deprecation + level, + ) { + this._level = level; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setTransactionName(name) { + this._transactionName = name; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setContext(key, context) { + if (context === null) { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete this._contexts[key]; + } else { + this._contexts[key] = context; + } + + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + setSpan(span) { + this._span = span; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + getSpan() { + return this._span; + } + + /** + * @inheritDoc + */ + getTransaction() { + // Often, this span (if it exists at all) will be a transaction, but it's not guaranteed to be. Regardless, it will + // have a pointer to the currently-active transaction. + const span = this.getSpan(); + return span && span.transaction; + } + + /** + * @inheritDoc + */ + setSession(session) { + if (!session) { + delete this._session; + } else { + this._session = session; + } + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + getSession() { + return this._session; + } + + /** + * @inheritDoc + */ + update(captureContext) { + if (!captureContext) { + return this; + } + + if (typeof captureContext === 'function') { + const updatedScope = (captureContext )(this); + return updatedScope instanceof Scope ? updatedScope : this; + } + + if (captureContext instanceof Scope) { + this._tags = { ...this._tags, ...captureContext._tags }; + this._extra = { ...this._extra, ...captureContext._extra }; + this._contexts = { ...this._contexts, ...captureContext._contexts }; + if (captureContext._user && Object.keys(captureContext._user).length) { + this._user = captureContext._user; + } + if (captureContext._level) { + this._level = captureContext._level; + } + if (captureContext._fingerprint) { + this._fingerprint = captureContext._fingerprint; + } + if (captureContext._requestSession) { + this._requestSession = captureContext._requestSession; + } + } else if (isPlainObject(captureContext)) { + // eslint-disable-next-line no-param-reassign + captureContext = captureContext ; + this._tags = { ...this._tags, ...captureContext.tags }; + this._extra = { ...this._extra, ...captureContext.extra }; + this._contexts = { ...this._contexts, ...captureContext.contexts }; + if (captureContext.user) { + this._user = captureContext.user; + } + if (captureContext.level) { + this._level = captureContext.level; + } + if (captureContext.fingerprint) { + this._fingerprint = captureContext.fingerprint; + } + if (captureContext.requestSession) { + this._requestSession = captureContext.requestSession; + } + } + + return this; + } + + /** + * @inheritDoc + */ + clear() { + this._breadcrumbs = []; + this._tags = {}; + this._extra = {}; + this._user = {}; + this._contexts = {}; + this._level = undefined; + this._transactionName = undefined; + this._fingerprint = undefined; + this._requestSession = undefined; + this._span = undefined; + this._session = undefined; + this._notifyScopeListeners(); + this._attachments = []; + return this; + } + + /** + * @inheritDoc + */ + addBreadcrumb(breadcrumb, maxBreadcrumbs) { + const maxCrumbs = typeof maxBreadcrumbs === 'number' ? maxBreadcrumbs : DEFAULT_MAX_BREADCRUMBS; + + // No data has been changed, so don't notify scope listeners + if (maxCrumbs <= 0) { + return this; + } + + const mergedBreadcrumb = { + timestamp: dateTimestampInSeconds(), + ...breadcrumb, + }; + this._breadcrumbs = [...this._breadcrumbs, mergedBreadcrumb].slice(-maxCrumbs); + this._notifyScopeListeners(); + + return this; + } + + /** + * @inheritDoc + */ + getLastBreadcrumb() { + return this._breadcrumbs[this._breadcrumbs.length - 1]; + } + + /** + * @inheritDoc + */ + clearBreadcrumbs() { + this._breadcrumbs = []; + this._notifyScopeListeners(); + return this; + } + + /** + * @inheritDoc + */ + addAttachment(attachment) { + this._attachments.push(attachment); + return this; + } + + /** + * @inheritDoc + */ + getAttachments() { + return this._attachments; + } + + /** + * @inheritDoc + */ + clearAttachments() { + this._attachments = []; + return this; + } + + /** + * Applies data from the scope to the event and runs all event processors on it. + * + * @param event Event + * @param hint Object containing additional information about the original exception, for use by the event processors. + * @hidden + */ + applyToEvent(event, hint = {}) { + if (this._extra && Object.keys(this._extra).length) { + event.extra = { ...this._extra, ...event.extra }; + } + if (this._tags && Object.keys(this._tags).length) { + event.tags = { ...this._tags, ...event.tags }; + } + if (this._user && Object.keys(this._user).length) { + event.user = { ...this._user, ...event.user }; + } + if (this._contexts && Object.keys(this._contexts).length) { + event.contexts = { ...this._contexts, ...event.contexts }; + } + if (this._level) { + event.level = this._level; + } + if (this._transactionName) { + event.transaction = this._transactionName; + } + + // We want to set the trace context for normal events only if there isn't already + // a trace context on the event. There is a product feature in place where we link + // errors with transaction and it relies on that. + if (this._span) { + event.contexts = { trace: this._span.getTraceContext(), ...event.contexts }; + const transaction = this._span.transaction; + if (transaction) { + event.sdkProcessingMetadata = { + dynamicSamplingContext: transaction.getDynamicSamplingContext(), + ...event.sdkProcessingMetadata, + }; + const transactionName = transaction.name; + if (transactionName) { + event.tags = { transaction: transactionName, ...event.tags }; + } + } + } + + this._applyFingerprint(event); + + event.breadcrumbs = [...(event.breadcrumbs || []), ...this._breadcrumbs]; + event.breadcrumbs = event.breadcrumbs.length > 0 ? event.breadcrumbs : undefined; + + event.sdkProcessingMetadata = { ...event.sdkProcessingMetadata, ...this._sdkProcessingMetadata }; + + return this._notifyEventProcessors([...getGlobalEventProcessors(), ...this._eventProcessors], event, hint); + } + + /** + * Add data which will be accessible during event processing but won't get sent to Sentry + */ + setSDKProcessingMetadata(newData) { + this._sdkProcessingMetadata = { ...this._sdkProcessingMetadata, ...newData }; + + return this; + } + + /** + * This will be called after {@link applyToEvent} is finished. + */ + _notifyEventProcessors( + processors, + event, + hint, + index = 0, + ) { + return new SyncPromise((resolve, reject) => { + const processor = processors[index]; + if (event === null || typeof processor !== 'function') { + resolve(event); + } else { + const result = processor({ ...event }, hint) ; + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + processor.id && + result === null && + logger.log(`Event processor "${processor.id}" dropped event`); + + if (isThenable(result)) { + void result + .then(final => this._notifyEventProcessors(processors, final, hint, index + 1).then(resolve)) + .then(null, reject); + } else { + void this._notifyEventProcessors(processors, result, hint, index + 1) + .then(resolve) + .then(null, reject); + } + } + }); + } + + /** + * This will be called on every set call. + */ + _notifyScopeListeners() { + // We need this check for this._notifyingListeners to be able to work on scope during updates + // If this check is not here we'll produce endless recursion when something is done with the scope + // during the callback. + if (!this._notifyingListeners) { + this._notifyingListeners = true; + this._scopeListeners.forEach(callback => { + callback(this); + }); + this._notifyingListeners = false; + } + } + + /** + * Applies fingerprint from the scope to the event if there's one, + * uses message if there's one instead or get rid of empty fingerprint + */ + _applyFingerprint(event) { + // Make sure it's an array first and we actually have something in place + event.fingerprint = event.fingerprint ? arrayify(event.fingerprint) : []; + + // If we have something on the scope, then merge it with event + if (this._fingerprint) { + event.fingerprint = event.fingerprint.concat(this._fingerprint); + } + + // If we have no data at all, remove empty array default + if (event.fingerprint && !event.fingerprint.length) { + delete event.fingerprint; + } + } +} + +/** + * Returns the global event processors. + */ +function getGlobalEventProcessors() { + return getGlobalSingleton('globalEventProcessors', () => []); +} + +/** + * Add a EventProcessor to be kept globally. + * @param callback EventProcessor to add + */ +function addGlobalEventProcessor(callback) { + getGlobalEventProcessors().push(callback); +} + +export { Scope, addGlobalEventProcessor }; +//# sourceMappingURL=scope.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/sdk.js b/shared/logger/node_modules/@sentry/core/esm/sdk.js new file mode 100644 index 0000000..5cb2237 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/sdk.js @@ -0,0 +1,35 @@ +import { logger } from '@sentry/utils'; +import { getCurrentHub } from './hub.js'; + +/** A class object that can instantiate Client objects. */ + +/** + * Internal function to create a new SDK client instance. The client is + * installed and then bound to the current scope. + * + * @param clientClass The client class to instantiate. + * @param options Options to pass to the client. + */ +function initAndBind( + clientClass, + options, +) { + if (options.debug === true) { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger.enable(); + } else { + // use `console.warn` rather than `logger.warn` since by non-debug bundles have all `logger.x` statements stripped + // eslint-disable-next-line no-console + console.warn('[Sentry] Cannot initialize SDK with `debug` option using a non-debug bundle.'); + } + } + const hub = getCurrentHub(); + const scope = hub.getScope(); + scope.update(options.initialScope); + + const client = new clientClass(options); + hub.bindClient(client); +} + +export { initAndBind }; +//# sourceMappingURL=sdk.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/session.js b/shared/logger/node_modules/@sentry/core/esm/session.js new file mode 100644 index 0000000..48a948f --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/session.js @@ -0,0 +1,155 @@ +import { timestampInSeconds, uuid4, dropUndefinedKeys } from '@sentry/utils'; + +/** + * Creates a new `Session` object by setting certain default parameters. If optional @param context + * is passed, the passed properties are applied to the session object. + * + * @param context (optional) additional properties to be applied to the returned session object + * + * @returns a new `Session` object + */ +function makeSession(context) { + // Both timestamp and started are in seconds since the UNIX epoch. + const startingTime = timestampInSeconds(); + + const session = { + sid: uuid4(), + init: true, + timestamp: startingTime, + started: startingTime, + duration: 0, + status: 'ok', + errors: 0, + ignoreDuration: false, + toJSON: () => sessionToJSON(session), + }; + + if (context) { + updateSession(session, context); + } + + return session; +} + +/** + * Updates a session object with the properties passed in the context. + * + * Note that this function mutates the passed object and returns void. + * (Had to do this instead of returning a new and updated session because closing and sending a session + * makes an update to the session after it was passed to the sending logic. + * @see BaseClient.captureSession ) + * + * @param session the `Session` to update + * @param context the `SessionContext` holding the properties that should be updated in @param session + */ +// eslint-disable-next-line complexity +function updateSession(session, context = {}) { + if (context.user) { + if (!session.ipAddress && context.user.ip_address) { + session.ipAddress = context.user.ip_address; + } + + if (!session.did && !context.did) { + session.did = context.user.id || context.user.email || context.user.username; + } + } + + session.timestamp = context.timestamp || timestampInSeconds(); + + if (context.ignoreDuration) { + session.ignoreDuration = context.ignoreDuration; + } + if (context.sid) { + // Good enough uuid validation. — Kamil + session.sid = context.sid.length === 32 ? context.sid : uuid4(); + } + if (context.init !== undefined) { + session.init = context.init; + } + if (!session.did && context.did) { + session.did = `${context.did}`; + } + if (typeof context.started === 'number') { + session.started = context.started; + } + if (session.ignoreDuration) { + session.duration = undefined; + } else if (typeof context.duration === 'number') { + session.duration = context.duration; + } else { + const duration = session.timestamp - session.started; + session.duration = duration >= 0 ? duration : 0; + } + if (context.release) { + session.release = context.release; + } + if (context.environment) { + session.environment = context.environment; + } + if (!session.ipAddress && context.ipAddress) { + session.ipAddress = context.ipAddress; + } + if (!session.userAgent && context.userAgent) { + session.userAgent = context.userAgent; + } + if (typeof context.errors === 'number') { + session.errors = context.errors; + } + if (context.status) { + session.status = context.status; + } +} + +/** + * Closes a session by setting its status and updating the session object with it. + * Internally calls `updateSession` to update the passed session object. + * + * Note that this function mutates the passed session (@see updateSession for explanation). + * + * @param session the `Session` object to be closed + * @param status the `SessionStatus` with which the session was closed. If you don't pass a status, + * this function will keep the previously set status, unless it was `'ok'` in which case + * it is changed to `'exited'`. + */ +function closeSession(session, status) { + let context = {}; + if (status) { + context = { status }; + } else if (session.status === 'ok') { + context = { status: 'exited' }; + } + + updateSession(session, context); +} + +/** + * Serializes a passed session object to a JSON object with a slightly different structure. + * This is necessary because the Sentry backend requires a slightly different schema of a session + * than the one the JS SDKs use internally. + * + * @param session the session to be converted + * + * @returns a JSON object of the passed session + */ +function sessionToJSON(session) { + return dropUndefinedKeys({ + sid: `${session.sid}`, + init: session.init, + // Make sure that sec is converted to ms for date constructor + started: new Date(session.started * 1000).toISOString(), + timestamp: new Date(session.timestamp * 1000).toISOString(), + status: session.status, + errors: session.errors, + did: typeof session.did === 'number' || typeof session.did === 'string' ? `${session.did}` : undefined, + duration: session.duration, + attrs: { + release: session.release, + environment: session.environment, + ip_address: session.ipAddress, + user_agent: session.userAgent, + }, + }); +} + +export { closeSession, makeSession, updateSession }; +//# sourceMappingURL=session.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/errors.js b/shared/logger/node_modules/@sentry/core/esm/tracing/errors.js new file mode 100644 index 0000000..ddfd49b --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/errors.js @@ -0,0 +1,36 @@ +import { addInstrumentationHandler, logger } from '@sentry/utils'; +import { getActiveTransaction } from './utils.js'; + +let errorsInstrumented = false; + +/** + * Configures global error listeners + */ +function registerErrorInstrumentation() { + if (errorsInstrumented) { + return; + } + + errorsInstrumented = true; + addInstrumentationHandler('error', errorCallback); + addInstrumentationHandler('unhandledrejection', errorCallback); +} + +/** + * If an error or unhandled promise occurs, we mark the active transaction as failed + */ +function errorCallback() { + const activeTransaction = getActiveTransaction(); + if (activeTransaction) { + const status = 'internal_error'; + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] Transaction: ${status} -> Global error occured`); + activeTransaction.setStatus(status); + } +} + +// The function name will be lost when bundling but we need to be able to identify this listener later to maintain the +// node.js default exit behaviour +errorCallback.tag = 'sentry_tracingErrorCallback'; + +export { registerErrorInstrumentation }; +//# sourceMappingURL=errors.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/hubextensions.js b/shared/logger/node_modules/@sentry/core/esm/tracing/hubextensions.js new file mode 100644 index 0000000..f96f922 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/hubextensions.js @@ -0,0 +1,241 @@ +import { logger, isNaN } from '@sentry/utils'; +import { getMainCarrier } from '../hub.js'; +import { hasTracingEnabled } from '../utils/hasTracingEnabled.js'; +import { registerErrorInstrumentation } from './errors.js'; +import { IdleTransaction } from './idletransaction.js'; +import { Transaction } from './transaction.js'; + +/** Returns all trace headers that are currently on the top scope. */ +function traceHeaders() { + const scope = this.getScope(); + const span = scope.getSpan(); + + return span + ? { + 'sentry-trace': span.toTraceparent(), + } + : {}; +} + +/** + * Makes a sampling decision for the given transaction and stores it on the transaction. + * + * Called every time a transaction is created. Only transactions which emerge with a `sampled` value of `true` will be + * sent to Sentry. + * + * @param transaction: The transaction needing a sampling decision + * @param options: The current client's options, so we can access `tracesSampleRate` and/or `tracesSampler` + * @param samplingContext: Default and user-provided data which may be used to help make the decision + * + * @returns The given transaction with its `sampled` value set + */ +function sample( + transaction, + options, + samplingContext, +) { + // nothing to do if tracing is not enabled + if (!hasTracingEnabled(options)) { + transaction.sampled = false; + return transaction; + } + + // if the user has forced a sampling decision by passing a `sampled` value in their transaction context, go with that + if (transaction.sampled !== undefined) { + transaction.setMetadata({ + sampleRate: Number(transaction.sampled), + }); + return transaction; + } + + // we would have bailed already if neither `tracesSampler` nor `tracesSampleRate` nor `enableTracing` were defined, so one of these should + // work; prefer the hook if so + let sampleRate; + if (typeof options.tracesSampler === 'function') { + sampleRate = options.tracesSampler(samplingContext); + transaction.setMetadata({ + sampleRate: Number(sampleRate), + }); + } else if (samplingContext.parentSampled !== undefined) { + sampleRate = samplingContext.parentSampled; + } else if (typeof options.tracesSampleRate !== 'undefined') { + sampleRate = options.tracesSampleRate; + transaction.setMetadata({ + sampleRate: Number(sampleRate), + }); + } else { + // When `enableTracing === true`, we use a sample rate of 100% + sampleRate = 1; + transaction.setMetadata({ + sampleRate, + }); + } + + // Since this is coming from the user (or from a function provided by the user), who knows what we might get. (The + // only valid values are booleans or numbers between 0 and 1.) + if (!isValidSampleRate(sampleRate)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('[Tracing] Discarding transaction because of invalid sample rate.'); + transaction.sampled = false; + return transaction; + } + + // if the function returned 0 (or false), or if `tracesSampleRate` is 0, it's a sign the transaction should be dropped + if (!sampleRate) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + `[Tracing] Discarding transaction because ${ + typeof options.tracesSampler === 'function' + ? 'tracesSampler returned 0 or false' + : 'a negative sampling decision was inherited or tracesSampleRate is set to 0' + }`, + ); + transaction.sampled = false; + return transaction; + } + + // Now we roll the dice. Math.random is inclusive of 0, but not of 1, so strict < is safe here. In case sampleRate is + // a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false. + transaction.sampled = Math.random() < (sampleRate ); + + // if we're not going to keep it, we're done + if (!transaction.sampled) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + `[Tracing] Discarding transaction because it's not included in the random sample (sampling rate = ${Number( + sampleRate, + )})`, + ); + return transaction; + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] starting ${transaction.op} transaction - ${transaction.name}`); + return transaction; +} + +/** + * Checks the given sample rate to make sure it is valid type and value (a boolean, or a number between 0 and 1). + */ +function isValidSampleRate(rate) { + // we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if (isNaN(rate) || !(typeof rate === 'number' || typeof rate === 'boolean')) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `[Tracing] Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify( + rate, + )} of type ${JSON.stringify(typeof rate)}.`, + ); + return false; + } + + // in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false + if (rate < 0 || rate > 1) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn(`[Tracing] Given sample rate is invalid. Sample rate must be between 0 and 1. Got ${rate}.`); + return false; + } + return true; +} + +/** + * Creates a new transaction and adds a sampling decision if it doesn't yet have one. + * + * The Hub.startTransaction method delegates to this method to do its work, passing the Hub instance in as `this`, as if + * it had been called on the hub directly. Exists as a separate function so that it can be injected into the class as an + * "extension method." + * + * @param this: The Hub starting the transaction + * @param transactionContext: Data used to configure the transaction + * @param CustomSamplingContext: Optional data to be provided to the `tracesSampler` function (if any) + * + * @returns The new transaction + * + * @see {@link Hub.startTransaction} + */ +function _startTransaction( + + transactionContext, + customSamplingContext, +) { + const client = this.getClient(); + const options = (client && client.getOptions()) || {}; + + const configInstrumenter = options.instrumenter || 'sentry'; + const transactionInstrumenter = transactionContext.instrumenter || 'sentry'; + + if (configInstrumenter !== transactionInstrumenter) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.error( + `A transaction was started with instrumenter=\`${transactionInstrumenter}\`, but the SDK is configured with the \`${configInstrumenter}\` instrumenter. +The transaction will not be sampled. Please use the ${configInstrumenter} instrumentation to start transactions.`, + ); + + transactionContext.sampled = false; + } + + let transaction = new Transaction(transactionContext, this); + transaction = sample(transaction, options, { + parentSampled: transactionContext.parentSampled, + transactionContext, + ...customSamplingContext, + }); + if (transaction.sampled) { + transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans )); + } + if (client && client.emit) { + client.emit('startTransaction', transaction); + } + return transaction; +} + +/** + * Create new idle transaction. + */ +function startIdleTransaction( + hub, + transactionContext, + idleTimeout, + finalTimeout, + onScope, + customSamplingContext, + heartbeatInterval, +) { + const client = hub.getClient(); + const options = (client && client.getOptions()) || {}; + + let transaction = new IdleTransaction(transactionContext, hub, idleTimeout, finalTimeout, heartbeatInterval, onScope); + transaction = sample(transaction, options, { + parentSampled: transactionContext.parentSampled, + transactionContext, + ...customSamplingContext, + }); + if (transaction.sampled) { + transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans )); + } + if (client && client.emit) { + client.emit('startTransaction', transaction); + } + return transaction; +} + +/** + * Adds tracing extensions to the global hub. + */ +function addTracingExtensions() { + const carrier = getMainCarrier(); + if (!carrier.__SENTRY__) { + return; + } + carrier.__SENTRY__.extensions = carrier.__SENTRY__.extensions || {}; + if (!carrier.__SENTRY__.extensions.startTransaction) { + carrier.__SENTRY__.extensions.startTransaction = _startTransaction; + } + if (!carrier.__SENTRY__.extensions.traceHeaders) { + carrier.__SENTRY__.extensions.traceHeaders = traceHeaders; + } + + registerErrorInstrumentation(); +} + +export { addTracingExtensions, startIdleTransaction }; +//# sourceMappingURL=hubextensions.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/idletransaction.js b/shared/logger/node_modules/@sentry/core/esm/tracing/idletransaction.js new file mode 100644 index 0000000..760c450 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/idletransaction.js @@ -0,0 +1,347 @@ +import { logger, timestampInSeconds } from '@sentry/utils'; +import { SpanRecorder } from './span.js'; +import { Transaction } from './transaction.js'; + +const TRACING_DEFAULTS = { + idleTimeout: 1000, + finalTimeout: 30000, + heartbeatInterval: 5000, +}; + +const FINISH_REASON_TAG = 'finishReason'; + +const IDLE_TRANSACTION_FINISH_REASONS = [ + 'heartbeatFailed', + 'idleTimeout', + 'documentHidden', + 'finalTimeout', + 'externalFinish', + 'cancelled', +]; + +/** + * @inheritDoc + */ +class IdleTransactionSpanRecorder extends SpanRecorder { + constructor( + _pushActivity, + _popActivity, + transactionSpanId, + maxlen, + ) { + super(maxlen);this._pushActivity = _pushActivity;this._popActivity = _popActivity;this.transactionSpanId = transactionSpanId; } + + /** + * @inheritDoc + */ + add(span) { + // We should make sure we do not push and pop activities for + // the transaction that this span recorder belongs to. + if (span.spanId !== this.transactionSpanId) { + // We patch span.finish() to pop an activity after setting an endTimestamp. + span.finish = (endTimestamp) => { + span.endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampInSeconds(); + this._popActivity(span.spanId); + }; + + // We should only push new activities if the span does not have an end timestamp. + if (span.endTimestamp === undefined) { + this._pushActivity(span.spanId); + } + } + + super.add(span); + } +} + +/** + * An IdleTransaction is a transaction that automatically finishes. It does this by tracking child spans as activities. + * You can have multiple IdleTransactions active, but if the `onScope` option is specified, the idle transaction will + * put itself on the scope on creation. + */ +class IdleTransaction extends Transaction { + // Activities store a list of active spans + __init() {this.activities = {};} + + // Track state of activities in previous heartbeat + + // Amount of times heartbeat has counted. Will cause transaction to finish after 3 beats. + __init2() {this._heartbeatCounter = 0;} + + // We should not use heartbeat if we finished a transaction + __init3() {this._finished = false;} + + // Idle timeout was canceled and we should finish the transaction with the last span end. + __init4() {this._idleTimeoutCanceledPermanently = false;} + + __init5() {this._beforeFinishCallbacks = [];} + + /** + * Timer that tracks Transaction idleTimeout + */ + + __init6() {this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[4];} + + constructor( + transactionContext, + _idleHub, + /** + * The time to wait in ms until the idle transaction will be finished. This timer is started each time + * there are no active spans on this transaction. + */ + _idleTimeout = TRACING_DEFAULTS.idleTimeout, + /** + * The final value in ms that a transaction cannot exceed + */ + _finalTimeout = TRACING_DEFAULTS.finalTimeout, + _heartbeatInterval = TRACING_DEFAULTS.heartbeatInterval, + // Whether or not the transaction should put itself on the scope when it starts and pop itself off when it ends + _onScope = false, + ) { + super(transactionContext, _idleHub);this._idleHub = _idleHub;this._idleTimeout = _idleTimeout;this._finalTimeout = _finalTimeout;this._heartbeatInterval = _heartbeatInterval;this._onScope = _onScope;IdleTransaction.prototype.__init.call(this);IdleTransaction.prototype.__init2.call(this);IdleTransaction.prototype.__init3.call(this);IdleTransaction.prototype.__init4.call(this);IdleTransaction.prototype.__init5.call(this);IdleTransaction.prototype.__init6.call(this); + if (_onScope) { + // We set the transaction here on the scope so error events pick up the trace + // context and attach it to the error. + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`Setting idle transaction on scope. Span ID: ${this.spanId}`); + _idleHub.configureScope(scope => scope.setSpan(this)); + } + + this._restartIdleTimeout(); + setTimeout(() => { + if (!this._finished) { + this.setStatus('deadline_exceeded'); + this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[3]; + this.finish(); + } + }, this._finalTimeout); + } + + /** {@inheritDoc} */ + finish(endTimestamp = timestampInSeconds()) { + this._finished = true; + this.activities = {}; + + if (this.op === 'ui.action.click') { + this.setTag(FINISH_REASON_TAG, this._finishReason); + } + + if (this.spanRecorder) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log('[Tracing] finishing IdleTransaction', new Date(endTimestamp * 1000).toISOString(), this.op); + + for (const callback of this._beforeFinishCallbacks) { + callback(this, endTimestamp); + } + + this.spanRecorder.spans = this.spanRecorder.spans.filter((span) => { + // If we are dealing with the transaction itself, we just return it + if (span.spanId === this.spanId) { + return true; + } + + // We cancel all pending spans with status "cancelled" to indicate the idle transaction was finished early + if (!span.endTimestamp) { + span.endTimestamp = endTimestamp; + span.setStatus('cancelled'); + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log('[Tracing] cancelling span since transaction ended early', JSON.stringify(span, undefined, 2)); + } + + const keepSpan = span.startTimestamp < endTimestamp; + if (!keepSpan) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + '[Tracing] discarding Span since it happened after Transaction was finished', + JSON.stringify(span, undefined, 2), + ); + } + return keepSpan; + }); + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] flushing IdleTransaction'); + } else { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] No active IdleTransaction'); + } + + // if `this._onScope` is `true`, the transaction put itself on the scope when it started + if (this._onScope) { + const scope = this._idleHub.getScope(); + if (scope.getTransaction() === this) { + scope.setSpan(undefined); + } + } + + return super.finish(endTimestamp); + } + + /** + * Register a callback function that gets excecuted before the transaction finishes. + * Useful for cleanup or if you want to add any additional spans based on current context. + * + * This is exposed because users have no other way of running something before an idle transaction + * finishes. + */ + registerBeforeFinishCallback(callback) { + this._beforeFinishCallbacks.push(callback); + } + + /** + * @inheritDoc + */ + initSpanRecorder(maxlen) { + if (!this.spanRecorder) { + const pushActivity = (id) => { + if (this._finished) { + return; + } + this._pushActivity(id); + }; + const popActivity = (id) => { + if (this._finished) { + return; + } + this._popActivity(id); + }; + + this.spanRecorder = new IdleTransactionSpanRecorder(pushActivity, popActivity, this.spanId, maxlen); + + // Start heartbeat so that transactions do not run forever. + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('Starting heartbeat'); + this._pingHeartbeat(); + } + this.spanRecorder.add(this); + } + + /** + * Cancels the existing idle timeout, if there is one. + * @param restartOnChildSpanChange Default is `true`. + * If set to false the transaction will end + * with the last child span. + */ + cancelIdleTimeout( + endTimestamp, + { + restartOnChildSpanChange, + } + + = { + restartOnChildSpanChange: true, + }, + ) { + this._idleTimeoutCanceledPermanently = restartOnChildSpanChange === false; + if (this._idleTimeoutID) { + clearTimeout(this._idleTimeoutID); + this._idleTimeoutID = undefined; + + if (Object.keys(this.activities).length === 0 && this._idleTimeoutCanceledPermanently) { + this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[5]; + this.finish(endTimestamp); + } + } + } + + /** + * Temporary method used to externally set the transaction's `finishReason` + * + * ** WARNING** + * This is for the purpose of experimentation only and will be removed in the near future, do not use! + * + * @internal + * + */ + setFinishReason(reason) { + this._finishReason = reason; + } + + /** + * Restarts idle timeout, if there is no running idle timeout it will start one. + */ + _restartIdleTimeout(endTimestamp) { + this.cancelIdleTimeout(); + this._idleTimeoutID = setTimeout(() => { + if (!this._finished && Object.keys(this.activities).length === 0) { + this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[1]; + this.finish(endTimestamp); + } + }, this._idleTimeout); + } + + /** + * Start tracking a specific activity. + * @param spanId The span id that represents the activity + */ + _pushActivity(spanId) { + this.cancelIdleTimeout(undefined, { restartOnChildSpanChange: !this._idleTimeoutCanceledPermanently }); + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] pushActivity: ${spanId}`); + this.activities[spanId] = true; + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] new activities count', Object.keys(this.activities).length); + } + + /** + * Remove an activity from usage + * @param spanId The span id that represents the activity + */ + _popActivity(spanId) { + if (this.activities[spanId]) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] popActivity ${spanId}`); + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete this.activities[spanId]; + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] new activities count', Object.keys(this.activities).length); + } + + if (Object.keys(this.activities).length === 0) { + const endTimestamp = timestampInSeconds(); + if (this._idleTimeoutCanceledPermanently) { + this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[5]; + this.finish(endTimestamp); + } else { + // We need to add the timeout here to have the real endtimestamp of the transaction + // Remember timestampInSeconds is in seconds, timeout is in ms + this._restartIdleTimeout(endTimestamp + this._idleTimeout / 1000); + } + } + } + + /** + * Checks when entries of this.activities are not changing for 3 beats. + * If this occurs we finish the transaction. + */ + _beat() { + // We should not be running heartbeat if the idle transaction is finished. + if (this._finished) { + return; + } + + const heartbeatString = Object.keys(this.activities).join(''); + + if (heartbeatString === this._prevHeartbeatString) { + this._heartbeatCounter++; + } else { + this._heartbeatCounter = 1; + } + + this._prevHeartbeatString = heartbeatString; + + if (this._heartbeatCounter >= 3) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] Transaction finished because of no change for 3 heart beats'); + this.setStatus('deadline_exceeded'); + this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[0]; + this.finish(); + } else { + this._pingHeartbeat(); + } + } + + /** + * Pings the heartbeat + */ + _pingHeartbeat() { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`pinging Heartbeat -> current counter: ${this._heartbeatCounter}`); + setTimeout(() => { + this._beat(); + }, this._heartbeatInterval); + } +} + +export { IdleTransaction, IdleTransactionSpanRecorder, TRACING_DEFAULTS }; +//# sourceMappingURL=idletransaction.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/span.js b/shared/logger/node_modules/@sentry/core/esm/tracing/span.js new file mode 100644 index 0000000..83ad954 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/span.js @@ -0,0 +1,378 @@ +import { uuid4, timestampInSeconds, logger, dropUndefinedKeys } from '@sentry/utils'; + +/** + * Keeps track of finished spans for a given transaction + * @internal + * @hideconstructor + * @hidden + */ +class SpanRecorder { + __init() {this.spans = [];} + + constructor(maxlen = 1000) {SpanRecorder.prototype.__init.call(this); + this._maxlen = maxlen; + } + + /** + * This is just so that we don't run out of memory while recording a lot + * of spans. At some point we just stop and flush out the start of the + * trace tree (i.e.the first n spans with the smallest + * start_timestamp). + */ + add(span) { + if (this.spans.length > this._maxlen) { + span.spanRecorder = undefined; + } else { + this.spans.push(span); + } + } +} + +/** + * Span contains all data about a span + */ +class Span { + /** + * @inheritDoc + */ + __init2() {this.traceId = uuid4();} + + /** + * @inheritDoc + */ + __init3() {this.spanId = uuid4().substring(16);} + + /** + * @inheritDoc + */ + + /** + * Internal keeper of the status + */ + + /** + * @inheritDoc + */ + + /** + * Timestamp in seconds when the span was created. + */ + __init4() {this.startTimestamp = timestampInSeconds();} + + /** + * Timestamp in seconds when the span ended. + */ + + /** + * @inheritDoc + */ + + /** + * @inheritDoc + */ + + /** + * @inheritDoc + */ + __init5() {this.tags = {};} + + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + __init6() {this.data = {};} + + /** + * List of spans that were finalized + */ + + /** + * @inheritDoc + */ + + /** + * The instrumenter that created this span. + */ + __init7() {this.instrumenter = 'sentry';} + + /** + * You should never call the constructor manually, always use `Sentry.startTransaction()` + * or call `startChild()` on an existing span. + * @internal + * @hideconstructor + * @hidden + */ + constructor(spanContext) {Span.prototype.__init2.call(this);Span.prototype.__init3.call(this);Span.prototype.__init4.call(this);Span.prototype.__init5.call(this);Span.prototype.__init6.call(this);Span.prototype.__init7.call(this); + if (!spanContext) { + return this; + } + if (spanContext.traceId) { + this.traceId = spanContext.traceId; + } + if (spanContext.spanId) { + this.spanId = spanContext.spanId; + } + if (spanContext.parentSpanId) { + this.parentSpanId = spanContext.parentSpanId; + } + // We want to include booleans as well here + if ('sampled' in spanContext) { + this.sampled = spanContext.sampled; + } + if (spanContext.op) { + this.op = spanContext.op; + } + if (spanContext.description) { + this.description = spanContext.description; + } + if (spanContext.data) { + this.data = spanContext.data; + } + if (spanContext.tags) { + this.tags = spanContext.tags; + } + if (spanContext.status) { + this.status = spanContext.status; + } + if (spanContext.startTimestamp) { + this.startTimestamp = spanContext.startTimestamp; + } + if (spanContext.endTimestamp) { + this.endTimestamp = spanContext.endTimestamp; + } + if (spanContext.instrumenter) { + this.instrumenter = spanContext.instrumenter; + } + } + + /** + * @inheritDoc + */ + startChild( + spanContext, + ) { + const childSpan = new Span({ + ...spanContext, + parentSpanId: this.spanId, + sampled: this.sampled, + traceId: this.traceId, + }); + + childSpan.spanRecorder = this.spanRecorder; + if (childSpan.spanRecorder) { + childSpan.spanRecorder.add(childSpan); + } + + childSpan.transaction = this.transaction; + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && childSpan.transaction) { + const opStr = (spanContext && spanContext.op) || '< unknown op >'; + const nameStr = childSpan.transaction.name || '< unknown name >'; + const idStr = childSpan.transaction.spanId; + + const logMessage = `[Tracing] Starting '${opStr}' span on transaction '${nameStr}' (${idStr}).`; + childSpan.transaction.metadata.spanMetadata[childSpan.spanId] = { logMessage }; + logger.log(logMessage); + } + + return childSpan; + } + + /** + * @inheritDoc + */ + setTag(key, value) { + this.tags = { ...this.tags, [key]: value }; + return this; + } + + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types + setData(key, value) { + this.data = { ...this.data, [key]: value }; + return this; + } + + /** + * @inheritDoc + */ + setStatus(value) { + this.status = value; + return this; + } + + /** + * @inheritDoc + */ + setHttpStatus(httpStatus) { + this.setTag('http.status_code', String(httpStatus)); + this.setData('http.response.status_code', httpStatus); + const spanStatus = spanStatusfromHttpCode(httpStatus); + if (spanStatus !== 'unknown_error') { + this.setStatus(spanStatus); + } + return this; + } + + /** + * @inheritDoc + */ + isSuccess() { + return this.status === 'ok'; + } + + /** + * @inheritDoc + */ + finish(endTimestamp) { + if ( + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + // Don't call this for transactions + this.transaction && + this.transaction.spanId !== this.spanId + ) { + const { logMessage } = this.transaction.metadata.spanMetadata[this.spanId]; + if (logMessage) { + logger.log((logMessage ).replace('Starting', 'Finishing')); + } + } + + this.endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampInSeconds(); + } + + /** + * @inheritDoc + */ + toTraceparent() { + let sampledString = ''; + if (this.sampled !== undefined) { + sampledString = this.sampled ? '-1' : '-0'; + } + return `${this.traceId}-${this.spanId}${sampledString}`; + } + + /** + * @inheritDoc + */ + toContext() { + return dropUndefinedKeys({ + data: this.data, + description: this.description, + endTimestamp: this.endTimestamp, + op: this.op, + parentSpanId: this.parentSpanId, + sampled: this.sampled, + spanId: this.spanId, + startTimestamp: this.startTimestamp, + status: this.status, + tags: this.tags, + traceId: this.traceId, + }); + } + + /** + * @inheritDoc + */ + updateWithContext(spanContext) { + this.data = spanContext.data || {}; + this.description = spanContext.description; + this.endTimestamp = spanContext.endTimestamp; + this.op = spanContext.op; + this.parentSpanId = spanContext.parentSpanId; + this.sampled = spanContext.sampled; + this.spanId = spanContext.spanId || this.spanId; + this.startTimestamp = spanContext.startTimestamp || this.startTimestamp; + this.status = spanContext.status; + this.tags = spanContext.tags || {}; + this.traceId = spanContext.traceId || this.traceId; + + return this; + } + + /** + * @inheritDoc + */ + getTraceContext() { + return dropUndefinedKeys({ + data: Object.keys(this.data).length > 0 ? this.data : undefined, + description: this.description, + op: this.op, + parent_span_id: this.parentSpanId, + span_id: this.spanId, + status: this.status, + tags: Object.keys(this.tags).length > 0 ? this.tags : undefined, + trace_id: this.traceId, + }); + } + + /** + * @inheritDoc + */ + toJSON() + + { + return dropUndefinedKeys({ + data: Object.keys(this.data).length > 0 ? this.data : undefined, + description: this.description, + op: this.op, + parent_span_id: this.parentSpanId, + span_id: this.spanId, + start_timestamp: this.startTimestamp, + status: this.status, + tags: Object.keys(this.tags).length > 0 ? this.tags : undefined, + timestamp: this.endTimestamp, + trace_id: this.traceId, + }); + } +} + +/** + * Converts a HTTP status code into a {@link SpanStatusType}. + * + * @param httpStatus The HTTP response status code. + * @returns The span status or unknown_error. + */ +function spanStatusfromHttpCode(httpStatus) { + if (httpStatus < 400 && httpStatus >= 100) { + return 'ok'; + } + + if (httpStatus >= 400 && httpStatus < 500) { + switch (httpStatus) { + case 401: + return 'unauthenticated'; + case 403: + return 'permission_denied'; + case 404: + return 'not_found'; + case 409: + return 'already_exists'; + case 413: + return 'failed_precondition'; + case 429: + return 'resource_exhausted'; + default: + return 'invalid_argument'; + } + } + + if (httpStatus >= 500 && httpStatus < 600) { + switch (httpStatus) { + case 501: + return 'unimplemented'; + case 503: + return 'unavailable'; + case 504: + return 'deadline_exceeded'; + default: + return 'internal_error'; + } + } + + return 'unknown_error'; +} + +export { Span, SpanRecorder, spanStatusfromHttpCode }; +//# sourceMappingURL=span.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/trace.js b/shared/logger/node_modules/@sentry/core/esm/tracing/trace.js new file mode 100644 index 0000000..3892c89 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/trace.js @@ -0,0 +1,78 @@ +import { isThenable } from '@sentry/utils'; +import { getCurrentHub } from '../hub.js'; +import { hasTracingEnabled } from '../utils/hasTracingEnabled.js'; + +/** + * Wraps a function with a transaction/span and finishes the span after the function is done. + * + * Note that if you have not enabled tracing extensions via `addTracingExtensions` + * or you didn't set `tracesSampleRate`, this function will not generate spans + * and the `span` returned from the callback will be undefined. + * + * This function is meant to be used internally and may break at any time. Use at your own risk. + * + * @internal + * @private + */ +function trace( + context, + callback, + // eslint-disable-next-line @typescript-eslint/no-empty-function + onError = () => {}, +) { + const ctx = { ...context }; + // If a name is set and a description is not, set the description to the name. + if (ctx.name !== undefined && ctx.description === undefined) { + ctx.description = ctx.name; + } + + const hub = getCurrentHub(); + const scope = hub.getScope(); + + const parentSpan = scope.getSpan(); + + function getActiveSpan() { + if (!hasTracingEnabled()) { + return undefined; + } + return parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx); + } + + const activeSpan = getActiveSpan(); + scope.setSpan(activeSpan); + + function finishAndSetSpan() { + activeSpan && activeSpan.finish(); + hub.getScope().setSpan(parentSpan); + } + + let maybePromiseResult; + try { + maybePromiseResult = callback(activeSpan); + } catch (e) { + activeSpan && activeSpan.setStatus('internal_error'); + onError(e); + finishAndSetSpan(); + throw e; + } + + if (isThenable(maybePromiseResult)) { + Promise.resolve(maybePromiseResult).then( + () => { + finishAndSetSpan(); + }, + e => { + activeSpan && activeSpan.setStatus('internal_error'); + onError(e); + finishAndSetSpan(); + }, + ); + } else { + finishAndSetSpan(); + } + + return maybePromiseResult; +} + +export { trace }; +//# sourceMappingURL=trace.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/transaction.js b/shared/logger/node_modules/@sentry/core/esm/tracing/transaction.js new file mode 100644 index 0000000..134f7fa --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/transaction.js @@ -0,0 +1,276 @@ +import { logger, dropUndefinedKeys } from '@sentry/utils'; +import { DEFAULT_ENVIRONMENT } from '../constants.js'; +import { getCurrentHub } from '../hub.js'; +import { Span, SpanRecorder } from './span.js'; + +/** JSDoc */ +class Transaction extends Span { + + /** + * The reference to the current hub. + */ + + __init() {this._measurements = {};} + + __init2() {this._contexts = {};} + + __init3() {this._frozenDynamicSamplingContext = undefined;} + + /** + * This constructor should never be called manually. Those instrumenting tracing should use + * `Sentry.startTransaction()`, and internal methods should use `hub.startTransaction()`. + * @internal + * @hideconstructor + * @hidden + */ + constructor(transactionContext, hub) { + super(transactionContext);Transaction.prototype.__init.call(this);Transaction.prototype.__init2.call(this);Transaction.prototype.__init3.call(this); + this._hub = hub || getCurrentHub(); + + this._name = transactionContext.name || ''; + + this.metadata = { + source: 'custom', + ...transactionContext.metadata, + spanMetadata: {}, + }; + + this._trimEnd = transactionContext.trimEnd; + + // this is because transactions are also spans, and spans have a transaction pointer + this.transaction = this; + + // If Dynamic Sampling Context is provided during the creation of the transaction, we freeze it as it usually means + // there is incoming Dynamic Sampling Context. (Either through an incoming request, a baggage meta-tag, or other means) + const incomingDynamicSamplingContext = this.metadata.dynamicSamplingContext; + if (incomingDynamicSamplingContext) { + // We shallow copy this in case anything writes to the original reference of the passed in `dynamicSamplingContext` + this._frozenDynamicSamplingContext = { ...incomingDynamicSamplingContext }; + } + } + + /** Getter for `name` property */ + get name() { + return this._name; + } + + /** Setter for `name` property, which also sets `source` as custom */ + set name(newName) { + this.setName(newName); + } + + /** + * JSDoc + */ + setName(name, source = 'custom') { + this._name = name; + this.metadata.source = source; + } + + /** + * Attaches SpanRecorder to the span itself + * @param maxlen maximum number of spans that can be recorded + */ + initSpanRecorder(maxlen = 1000) { + if (!this.spanRecorder) { + this.spanRecorder = new SpanRecorder(maxlen); + } + this.spanRecorder.add(this); + } + + /** + * @inheritDoc + */ + setContext(key, context) { + if (context === null) { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete this._contexts[key]; + } else { + this._contexts[key] = context; + } + } + + /** + * @inheritDoc + */ + setMeasurement(name, value, unit = '') { + this._measurements[name] = { value, unit }; + } + + /** + * @inheritDoc + */ + setMetadata(newMetadata) { + this.metadata = { ...this.metadata, ...newMetadata }; + } + + /** + * @inheritDoc + */ + finish(endTimestamp) { + // This transaction is already finished, so we should not flush it again. + if (this.endTimestamp !== undefined) { + return undefined; + } + + if (!this.name) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Transaction has no name, falling back to `<unlabeled transaction>`.'); + this.name = '<unlabeled transaction>'; + } + + // just sets the end timestamp + super.finish(endTimestamp); + + const client = this._hub.getClient(); + if (client && client.emit) { + client.emit('finishTransaction', this); + } + + if (this.sampled !== true) { + // At this point if `sampled !== true` we want to discard the transaction. + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Tracing] Discarding transaction because its trace was not chosen to be sampled.'); + + if (client) { + client.recordDroppedEvent('sample_rate', 'transaction'); + } + + return undefined; + } + + const finishedSpans = this.spanRecorder ? this.spanRecorder.spans.filter(s => s !== this && s.endTimestamp) : []; + + if (this._trimEnd && finishedSpans.length > 0) { + this.endTimestamp = finishedSpans.reduce((prev, current) => { + if (prev.endTimestamp && current.endTimestamp) { + return prev.endTimestamp > current.endTimestamp ? prev : current; + } + return prev; + }).endTimestamp; + } + + const metadata = this.metadata; + + const transaction = { + contexts: { + ...this._contexts, + // We don't want to override trace context + trace: this.getTraceContext(), + }, + spans: finishedSpans, + start_timestamp: this.startTimestamp, + tags: this.tags, + timestamp: this.endTimestamp, + transaction: this.name, + type: 'transaction', + sdkProcessingMetadata: { + ...metadata, + dynamicSamplingContext: this.getDynamicSamplingContext(), + }, + ...(metadata.source && { + transaction_info: { + source: metadata.source, + }, + }), + }; + + const hasMeasurements = Object.keys(this._measurements).length > 0; + + if (hasMeasurements) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.log( + '[Measurements] Adding measurements to transaction', + JSON.stringify(this._measurements, undefined, 2), + ); + transaction.measurements = this._measurements; + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Tracing] Finishing ${this.op} transaction: ${this.name}.`); + + return this._hub.captureEvent(transaction); + } + + /** + * @inheritDoc + */ + toContext() { + const spanContext = super.toContext(); + + return dropUndefinedKeys({ + ...spanContext, + name: this.name, + trimEnd: this._trimEnd, + }); + } + + /** + * @inheritDoc + */ + updateWithContext(transactionContext) { + super.updateWithContext(transactionContext); + + this.name = transactionContext.name || ''; + + this._trimEnd = transactionContext.trimEnd; + + return this; + } + + /** + * @inheritdoc + * + * @experimental + */ + getDynamicSamplingContext() { + if (this._frozenDynamicSamplingContext) { + return this._frozenDynamicSamplingContext; + } + + const hub = this._hub || getCurrentHub(); + const client = hub && hub.getClient(); + + if (!client) return {}; + + const { environment, release } = client.getOptions() || {}; + const { publicKey: public_key } = client.getDsn() || {}; + + const maybeSampleRate = this.metadata.sampleRate; + const sample_rate = maybeSampleRate !== undefined ? maybeSampleRate.toString() : undefined; + + const { segment: user_segment } = hub.getScope().getUser() || {}; + + const source = this.metadata.source; + + // We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII + const transaction = source && source !== 'url' ? this.name : undefined; + + const dsc = dropUndefinedKeys({ + environment: environment || DEFAULT_ENVIRONMENT, + release, + transaction, + user_segment, + public_key, + trace_id: this.traceId, + sample_rate, + }); + + // Uncomment if we want to make DSC immutable + // this._frozenDynamicSamplingContext = dsc; + + client.emit && client.emit('createDsc', dsc); + + return dsc; + } + + /** + * Override the current hub with a new one. + * Used if you want another hub to finish the transaction. + * + * @internal + */ + setHub(hub) { + this._hub = hub; + } +} + +export { Transaction }; +//# sourceMappingURL=transaction.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/tracing/utils.js b/shared/logger/node_modules/@sentry/core/esm/tracing/utils.js new file mode 100644 index 0000000..e3e7818 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/tracing/utils.js @@ -0,0 +1,12 @@ +import { getCurrentHub } from '../hub.js'; +export { TRACEPARENT_REGEXP, extractTraceparentData, stripUrlQueryAndFragment } from '@sentry/utils'; + +/** Grabs active transaction off scope, if any */ +function getActiveTransaction(maybeHub) { + const hub = maybeHub || getCurrentHub(); + const scope = hub.getScope(); + return scope.getTransaction() ; +} + +export { getActiveTransaction }; +//# sourceMappingURL=utils.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/transports/base.js b/shared/logger/node_modules/@sentry/core/esm/transports/base.js new file mode 100644 index 0000000..07adadd --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/transports/base.js @@ -0,0 +1,101 @@ +import { makePromiseBuffer, forEachEnvelopeItem, envelopeItemTypeToDataCategory, isRateLimited, resolvedSyncPromise, createEnvelope, SentryError, logger, serializeEnvelope, updateRateLimits } from '@sentry/utils'; + +const DEFAULT_TRANSPORT_BUFFER_SIZE = 30; + +/** + * Creates an instance of a Sentry `Transport` + * + * @param options + * @param makeRequest + */ +function createTransport( + options, + makeRequest, + buffer = makePromiseBuffer( + options.bufferSize || DEFAULT_TRANSPORT_BUFFER_SIZE, + ), +) { + let rateLimits = {}; + const flush = (timeout) => buffer.drain(timeout); + + function send(envelope) { + const filteredEnvelopeItems = []; + + // Drop rate limited items from envelope + forEachEnvelopeItem(envelope, (item, type) => { + const envelopeItemDataCategory = envelopeItemTypeToDataCategory(type); + if (isRateLimited(rateLimits, envelopeItemDataCategory)) { + const event = getEventForEnvelopeItem(item, type); + options.recordDroppedEvent('ratelimit_backoff', envelopeItemDataCategory, event); + } else { + filteredEnvelopeItems.push(item); + } + }); + + // Skip sending if envelope is empty after filtering out rate limited events + if (filteredEnvelopeItems.length === 0) { + return resolvedSyncPromise(); + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const filteredEnvelope = createEnvelope(envelope[0], filteredEnvelopeItems ); + + // Creates client report for each item in an envelope + const recordEnvelopeLoss = (reason) => { + forEachEnvelopeItem(filteredEnvelope, (item, type) => { + const event = getEventForEnvelopeItem(item, type); + options.recordDroppedEvent(reason, envelopeItemTypeToDataCategory(type), event); + }); + }; + + const requestTask = () => + makeRequest({ body: serializeEnvelope(filteredEnvelope, options.textEncoder) }).then( + response => { + // We don't want to throw on NOK responses, but we want to at least log them + if (response.statusCode !== undefined && (response.statusCode < 200 || response.statusCode >= 300)) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn(`Sentry responded with status code ${response.statusCode} to sent event.`); + } + + rateLimits = updateRateLimits(rateLimits, response); + return response; + }, + error => { + recordEnvelopeLoss('network_error'); + throw error; + }, + ); + + return buffer.add(requestTask).then( + result => result, + error => { + if (error instanceof SentryError) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Skipped sending event because buffer is full.'); + recordEnvelopeLoss('queue_overflow'); + return resolvedSyncPromise(); + } else { + throw error; + } + }, + ); + } + + // We use this to identifify if the transport is the base transport + // TODO (v8): Remove this again as we'll no longer need it + send.__sentry__baseTransport__ = true; + + return { + send, + flush, + }; +} + +function getEventForEnvelopeItem(item, type) { + if (type !== 'event' && type !== 'transaction') { + return undefined; + } + + return Array.isArray(item) ? (item )[1] : undefined; +} + +export { DEFAULT_TRANSPORT_BUFFER_SIZE, createTransport }; +//# sourceMappingURL=base.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/transports/multiplexed.js b/shared/logger/node_modules/@sentry/core/esm/transports/multiplexed.js new file mode 100644 index 0000000..0ef2030 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/transports/multiplexed.js @@ -0,0 +1,76 @@ +import { dsnFromString, forEachEnvelopeItem } from '@sentry/utils'; +import { getEnvelopeEndpointWithUrlEncodedAuth } from '../api.js'; + +function eventFromEnvelope(env, types) { + let event; + + forEachEnvelopeItem(env, (item, type) => { + if (types.includes(type)) { + event = Array.isArray(item) ? (item )[1] : undefined; + } + // bail out if we found an event + return !!event; + }); + + return event; +} + +/** + * Creates a transport that can send events to different DSNs depending on the envelope contents. + */ +function makeMultiplexedTransport( + createTransport, + matcher, +) { + return options => { + const fallbackTransport = createTransport(options); + const otherTransports = {}; + + function getTransport(dsn) { + if (!otherTransports[dsn]) { + const validatedDsn = dsnFromString(dsn); + if (!validatedDsn) { + return undefined; + } + const url = getEnvelopeEndpointWithUrlEncodedAuth(validatedDsn); + otherTransports[dsn] = createTransport({ ...options, url }); + } + + return otherTransports[dsn]; + } + + async function send(envelope) { + function getEvent(types) { + const eventTypes = types && types.length ? types : ['event']; + return eventFromEnvelope(envelope, eventTypes); + } + + const transports = matcher({ envelope, getEvent }) + .map(dsn => getTransport(dsn)) + .filter((t) => !!t); + + // If we have no transports to send to, use the fallback transport + if (transports.length === 0) { + transports.push(fallbackTransport); + } + + const results = await Promise.all(transports.map(transport => transport.send(envelope))); + + return results[0]; + } + + async function flush(timeout) { + const allTransports = [...Object.keys(otherTransports).map(dsn => otherTransports[dsn]), fallbackTransport]; + const results = await Promise.all(allTransports.map(transport => transport.flush(timeout))); + return results.every(r => r); + } + + return { + send, + flush, + }; + }; +} + +export { makeMultiplexedTransport }; +//# sourceMappingURL=multiplexed.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/transports/offline.js b/shared/logger/node_modules/@sentry/core/esm/transports/offline.js new file mode 100644 index 0000000..c9de714 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/transports/offline.js @@ -0,0 +1,122 @@ +import { parseRetryAfterHeader, logger, envelopeContainsItemType } from '@sentry/utils'; + +const MIN_DELAY = 100; // 100 ms +const START_DELAY = 5000; // 5 seconds +const MAX_DELAY = 3.6e6; // 1 hour + +function log(msg, error) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.info(`[Offline]: ${msg}`, error); +} + +/** + * Wraps a transport and stores and retries events when they fail to send. + * + * @param createTransport The transport to wrap. + */ +function makeOfflineTransport( + createTransport, +) { + return options => { + const transport = createTransport(options); + const store = options.createStore ? options.createStore(options) : undefined; + + let retryDelay = START_DELAY; + let flushTimer; + + function shouldQueue(env, error, retryDelay) { + // We don't queue Session Replay envelopes because they are: + // - Ordered and Replay relies on the response status to know when they're successfully sent. + // - Likely to fill the queue quickly and block other events from being sent. + // We also want to drop client reports because they can be generated when we retry sending events while offline. + if (envelopeContainsItemType(env, ['replay_event', 'replay_recording', 'client_report'])) { + return false; + } + + if (options.shouldStore) { + return options.shouldStore(env, error, retryDelay); + } + + return true; + } + + function flushIn(delay) { + if (!store) { + return; + } + + if (flushTimer) { + clearTimeout(flushTimer ); + } + + flushTimer = setTimeout(async () => { + flushTimer = undefined; + + const found = await store.pop(); + if (found) { + log('Attempting to send previously queued event'); + void send(found).catch(e => { + log('Failed to retry sending', e); + }); + } + }, delay) ; + + // We need to unref the timer in node.js, otherwise the node process never exit. + if (typeof flushTimer !== 'number' && flushTimer.unref) { + flushTimer.unref(); + } + } + + function flushWithBackOff() { + if (flushTimer) { + return; + } + + flushIn(retryDelay); + + retryDelay = Math.min(retryDelay * 2, MAX_DELAY); + } + + async function send(envelope) { + try { + const result = await transport.send(envelope); + + let delay = MIN_DELAY; + + if (result) { + // If there's a retry-after header, use that as the next delay. + if (result.headers && result.headers['retry-after']) { + delay = parseRetryAfterHeader(result.headers['retry-after']); + } // If we have a server error, return now so we don't flush the queue. + else if ((result.statusCode || 0) >= 400) { + return result; + } + } + + flushIn(delay); + retryDelay = START_DELAY; + return result; + } catch (e) { + if (store && (await shouldQueue(envelope, e , retryDelay))) { + await store.insert(envelope); + flushWithBackOff(); + log('Error sending. Event queued', e ); + return {}; + } else { + throw e; + } + } + } + + if (options.flushAtStartup) { + flushWithBackOff(); + } + + return { + send, + flush: t => transport.flush(t), + }; + }; +} + +export { MIN_DELAY, START_DELAY, makeOfflineTransport }; +//# sourceMappingURL=offline.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/utils/hasTracingEnabled.js b/shared/logger/node_modules/@sentry/core/esm/utils/hasTracingEnabled.js new file mode 100644 index 0000000..d71bd5a --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/utils/hasTracingEnabled.js @@ -0,0 +1,23 @@ +import { getCurrentHub } from '../hub.js'; + +// Treeshakable guard to remove all code related to tracing + +/** + * Determines if tracing is currently enabled. + * + * Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config. + */ +function hasTracingEnabled( + maybeOptions, +) { + if (typeof __SENTRY_TRACING__ === 'boolean' && !__SENTRY_TRACING__) { + return false; + } + + const client = getCurrentHub().getClient(); + const options = maybeOptions || (client && client.getOptions()); + return !!options && (options.enableTracing || 'tracesSampleRate' in options || 'tracesSampler' in options); +} + +export { hasTracingEnabled }; +//# sourceMappingURL=hasTracingEnabled.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/utils/prepareEvent.js b/shared/logger/node_modules/@sentry/core/esm/utils/prepareEvent.js new file mode 100644 index 0000000..eb695a8 --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/utils/prepareEvent.js @@ -0,0 +1,304 @@ +import { uuid4, dateTimestampInSeconds, resolvedSyncPromise, truncate, GLOBAL_OBJ, normalize } from '@sentry/utils'; +import { DEFAULT_ENVIRONMENT } from '../constants.js'; +import { Scope } from '../scope.js'; + +/** + * Adds common information to events. + * + * The information includes release and environment from `options`, + * breadcrumbs and context (extra, tags and user) from the scope. + * + * Information that is already present in the event is never overwritten. For + * nested objects, such as the context, keys are merged. + * + * Note: This also triggers callbacks for `addGlobalEventProcessor`, but not `beforeSend`. + * + * @param event The original event. + * @param hint May contain additional information about the original exception. + * @param scope A scope containing event metadata. + * @returns A new event with more information. + * @hidden + */ +function prepareEvent( + options, + event, + hint, + scope, +) { + const { normalizeDepth = 3, normalizeMaxBreadth = 1000 } = options; + const prepared = { + ...event, + event_id: event.event_id || hint.event_id || uuid4(), + timestamp: event.timestamp || dateTimestampInSeconds(), + }; + const integrations = hint.integrations || options.integrations.map(i => i.name); + + applyClientOptions(prepared, options); + applyIntegrationsMetadata(prepared, integrations); + + // Only put debug IDs onto frames for error events. + if (event.type === undefined) { + applyDebugIds(prepared, options.stackParser); + } + + // If we have scope given to us, use it as the base for further modifications. + // This allows us to prevent unnecessary copying of data if `captureContext` is not provided. + let finalScope = scope; + if (hint.captureContext) { + finalScope = Scope.clone(finalScope).update(hint.captureContext); + } + + // We prepare the result here with a resolved Event. + let result = resolvedSyncPromise(prepared); + + // This should be the last thing called, since we want that + // {@link Hub.addEventProcessor} gets the finished prepared event. + // + // We need to check for the existence of `finalScope.getAttachments` + // because `getAttachments` can be undefined if users are using an older version + // of `@sentry/core` that does not have the `getAttachments` method. + // See: https://github.com/getsentry/sentry-javascript/issues/5229 + if (finalScope) { + // Collect attachments from the hint and scope + if (finalScope.getAttachments) { + const attachments = [...(hint.attachments || []), ...finalScope.getAttachments()]; + + if (attachments.length) { + hint.attachments = attachments; + } + } + + // In case we have a hub we reassign it. + result = finalScope.applyToEvent(prepared, hint); + } + + return result.then(evt => { + if (evt) { + // We apply the debug_meta field only after all event processors have ran, so that if any event processors modified + // file names (e.g.the RewriteFrames integration) the filename -> debug ID relationship isn't destroyed. + // This should not cause any PII issues, since we're only moving data that is already on the event and not adding + // any new data + applyDebugMeta(evt); + } + + if (typeof normalizeDepth === 'number' && normalizeDepth > 0) { + return normalizeEvent(evt, normalizeDepth, normalizeMaxBreadth); + } + return evt; + }); +} + +/** + * Enhances event using the client configuration. + * It takes care of all "static" values like environment, release and `dist`, + * as well as truncating overly long values. + * @param event event instance to be enhanced + */ +function applyClientOptions(event, options) { + const { environment, release, dist, maxValueLength = 250 } = options; + + if (!('environment' in event)) { + event.environment = 'environment' in options ? environment : DEFAULT_ENVIRONMENT; + } + + if (event.release === undefined && release !== undefined) { + event.release = release; + } + + if (event.dist === undefined && dist !== undefined) { + event.dist = dist; + } + + if (event.message) { + event.message = truncate(event.message, maxValueLength); + } + + const exception = event.exception && event.exception.values && event.exception.values[0]; + if (exception && exception.value) { + exception.value = truncate(exception.value, maxValueLength); + } + + const request = event.request; + if (request && request.url) { + request.url = truncate(request.url, maxValueLength); + } +} + +const debugIdStackParserCache = new WeakMap(); + +/** + * Puts debug IDs into the stack frames of an error event. + */ +function applyDebugIds(event, stackParser) { + const debugIdMap = GLOBAL_OBJ._sentryDebugIds; + + if (!debugIdMap) { + return; + } + + let debugIdStackFramesCache; + const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser); + if (cachedDebugIdStackFrameCache) { + debugIdStackFramesCache = cachedDebugIdStackFrameCache; + } else { + debugIdStackFramesCache = new Map(); + debugIdStackParserCache.set(stackParser, debugIdStackFramesCache); + } + + // Build a map of filename -> debug_id + const filenameDebugIdMap = Object.keys(debugIdMap).reduce((acc, debugIdStackTrace) => { + let parsedStack; + const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace); + if (cachedParsedStack) { + parsedStack = cachedParsedStack; + } else { + parsedStack = stackParser(debugIdStackTrace); + debugIdStackFramesCache.set(debugIdStackTrace, parsedStack); + } + + for (let i = parsedStack.length - 1; i >= 0; i--) { + const stackFrame = parsedStack[i]; + if (stackFrame.filename) { + acc[stackFrame.filename] = debugIdMap[debugIdStackTrace]; + break; + } + } + return acc; + }, {}); + + try { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + event.exception.values.forEach(exception => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + exception.stacktrace.frames.forEach(frame => { + if (frame.filename) { + frame.debug_id = filenameDebugIdMap[frame.filename]; + } + }); + }); + } catch (e) { + // To save bundle size we're just try catching here instead of checking for the existence of all the different objects. + } +} + +/** + * Moves debug IDs from the stack frames of an error event into the debug_meta field. + */ +function applyDebugMeta(event) { + // Extract debug IDs and filenames from the stack frames on the event. + const filenameDebugIdMap = {}; + try { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + event.exception.values.forEach(exception => { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + exception.stacktrace.frames.forEach(frame => { + if (frame.debug_id) { + if (frame.abs_path) { + filenameDebugIdMap[frame.abs_path] = frame.debug_id; + } else if (frame.filename) { + filenameDebugIdMap[frame.filename] = frame.debug_id; + } + delete frame.debug_id; + } + }); + }); + } catch (e) { + // To save bundle size we're just try catching here instead of checking for the existence of all the different objects. + } + + if (Object.keys(filenameDebugIdMap).length === 0) { + return; + } + + // Fill debug_meta information + event.debug_meta = event.debug_meta || {}; + event.debug_meta.images = event.debug_meta.images || []; + const images = event.debug_meta.images; + Object.keys(filenameDebugIdMap).forEach(filename => { + images.push({ + type: 'sourcemap', + code_file: filename, + debug_id: filenameDebugIdMap[filename], + }); + }); +} + +/** + * This function adds all used integrations to the SDK info in the event. + * @param event The event that will be filled with all integrations. + */ +function applyIntegrationsMetadata(event, integrationNames) { + if (integrationNames.length > 0) { + event.sdk = event.sdk || {}; + event.sdk.integrations = [...(event.sdk.integrations || []), ...integrationNames]; + } +} + +/** + * Applies `normalize` function on necessary `Event` attributes to make them safe for serialization. + * Normalized keys: + * - `breadcrumbs.data` + * - `user` + * - `contexts` + * - `extra` + * @param event Event + * @returns Normalized event + */ +function normalizeEvent(event, depth, maxBreadth) { + if (!event) { + return null; + } + + const normalized = { + ...event, + ...(event.breadcrumbs && { + breadcrumbs: event.breadcrumbs.map(b => ({ + ...b, + ...(b.data && { + data: normalize(b.data, depth, maxBreadth), + }), + })), + }), + ...(event.user && { + user: normalize(event.user, depth, maxBreadth), + }), + ...(event.contexts && { + contexts: normalize(event.contexts, depth, maxBreadth), + }), + ...(event.extra && { + extra: normalize(event.extra, depth, maxBreadth), + }), + }; + + // event.contexts.trace stores information about a Transaction. Similarly, + // event.spans[] stores information about child Spans. Given that a + // Transaction is conceptually a Span, normalization should apply to both + // Transactions and Spans consistently. + // For now the decision is to skip normalization of Transactions and Spans, + // so this block overwrites the normalized event to add back the original + // Transaction information prior to normalization. + if (event.contexts && event.contexts.trace && normalized.contexts) { + normalized.contexts.trace = event.contexts.trace; + + // event.contexts.trace.data may contain circular/dangerous data so we need to normalize it + if (event.contexts.trace.data) { + normalized.contexts.trace.data = normalize(event.contexts.trace.data, depth, maxBreadth); + } + } + + // event.spans[].data may contain circular/dangerous data so we need to normalize it + if (event.spans) { + normalized.spans = event.spans.map(span => { + // We cannot use the spread operator here because `toJSON` on `span` is non-enumerable + if (span.data) { + span.data = normalize(span.data, depth, maxBreadth); + } + return span; + }); + } + + return normalized; +} + +export { applyDebugIds, applyDebugMeta, prepareEvent }; +//# sourceMappingURL=prepareEvent.js.map diff --git a/shared/logger/node_modules/@sentry/core/esm/version.js b/shared/logger/node_modules/@sentry/core/esm/version.js new file mode 100644 index 0000000..adb490c --- /dev/null +++ b/shared/logger/node_modules/@sentry/core/esm/version.js @@ -0,0 +1,4 @@ +const SDK_VERSION = '7.57.0'; + +export { SDK_VERSION }; +//# sourceMappingURL=version.js.map diff --git a/shared/logger/node_modules/@sentry/replay/esm/index.js b/shared/logger/node_modules/@sentry/replay/esm/index.js new file mode 100644 index 0000000..84a600d --- /dev/null +++ b/shared/logger/node_modules/@sentry/replay/esm/index.js @@ -0,0 +1,8542 @@ +import { getCurrentHub, addGlobalEventProcessor, prepareEvent, setContext, captureException } from '@sentry/core'; +import { GLOBAL_OBJ, normalize, fill, htmlTreeAsString, logger, uuid4, SENTRY_XHR_DATA_KEY, dropUndefinedKeys, stringMatchesSomePattern, addInstrumentationHandler, browserPerformanceTimeOrigin, createEnvelope, createEventEnvelopeHeaders, getSdkMetadataForEnvelopeHeader, isNodeEnv } from '@sentry/utils'; + +// exporting a separate copy of `WINDOW` rather than exporting the one from `@sentry/browser` +// prevents the browser package from being bundled in the CDN bundle, and avoids a +// circular dependency between the browser and replay packages should `@sentry/browser` import +// from `@sentry/replay` in the future +const WINDOW = GLOBAL_OBJ ; + +const REPLAY_SESSION_KEY = 'sentryReplaySession'; +const REPLAY_EVENT_NAME = 'replay_event'; +const UNABLE_TO_SEND_REPLAY = 'Unable to send Replay'; + +// The idle limit for a session after which recording is paused. +const SESSION_IDLE_PAUSE_DURATION = 300000; // 5 minutes in ms + +// The idle limit for a session after which the session expires. +const SESSION_IDLE_EXPIRE_DURATION = 900000; // 15 minutes in ms + +// The maximum length of a session +const MAX_SESSION_LIFE = 3600000; // 60 minutes in ms + +/** Default flush delays */ +const DEFAULT_FLUSH_MIN_DELAY = 5000; +// XXX: Temp fix for our debounce logic where `maxWait` would never occur if it +// was the same as `wait` +const DEFAULT_FLUSH_MAX_DELAY = 5500; + +/* How long to wait for error checkouts */ +const BUFFER_CHECKOUT_TIME = 60000; + +const RETRY_BASE_INTERVAL = 5000; +const RETRY_MAX_COUNT = 3; + +/* The max (uncompressed) size in bytes of a network body. Any body larger than this will be truncated. */ +const NETWORK_BODY_MAX_SIZE = 150000; + +/* The max size of a single console arg that is captured. Any arg larger than this will be truncated. */ +const CONSOLE_ARG_MAX_SIZE = 5000; + +/* Min. time to wait before we consider something a slow click. */ +const SLOW_CLICK_THRESHOLD = 3000; +/* For scroll actions after a click, we only look for a very short time period to detect programmatic scrolling. */ +const SLOW_CLICK_SCROLL_TIMEOUT = 300; +/* Clicks in this time period are considered e.g. double/triple clicks. */ +const MULTI_CLICK_TIMEOUT = 1000; + +/** When encountering a total segment size exceeding this size, stop the replay (as we cannot properly ingest it). */ +const REPLAY_MAX_EVENT_BUFFER_SIZE = 20000000; // ~20MB + +var NodeType$1; +(function (NodeType) { + NodeType[NodeType["Document"] = 0] = "Document"; + NodeType[NodeType["DocumentType"] = 1] = "DocumentType"; + NodeType[NodeType["Element"] = 2] = "Element"; + NodeType[NodeType["Text"] = 3] = "Text"; + NodeType[NodeType["CDATA"] = 4] = "CDATA"; + NodeType[NodeType["Comment"] = 5] = "Comment"; +})(NodeType$1 || (NodeType$1 = {})); + +function isElement(n) { + return n.nodeType === n.ELEMENT_NODE; +} +function isShadowRoot(n) { + const host = n === null || n === void 0 ? void 0 : n.host; + return Boolean(host && host.shadowRoot && host.shadowRoot === n); +} +function isInputTypeMasked({ maskInputOptions, tagName, type, }) { + if (tagName.toLowerCase() === 'option') { + tagName = 'select'; + } + const actualType = typeof type === 'string' ? type.toLowerCase() : undefined; + return (maskInputOptions[tagName.toLowerCase()] || + (actualType && maskInputOptions[actualType]) || + actualType === 'password' || + (tagName === 'input' && !type && maskInputOptions['text'])); +} +function hasInputMaskOptions({ tagName, type, maskInputOptions, maskInputSelector, }) { + return (maskInputSelector || isInputTypeMasked({ maskInputOptions, tagName, type })); +} +function maskInputValue({ input, maskInputSelector, unmaskInputSelector, maskInputOptions, tagName, type, value, maskInputFn, }) { + let text = value || ''; + if (unmaskInputSelector && input.matches(unmaskInputSelector)) { + return text; + } + if (input.hasAttribute('data-rr-is-password')) { + type = 'password'; + } + if (isInputTypeMasked({ maskInputOptions, tagName, type }) || + (maskInputSelector && input.matches(maskInputSelector))) { + if (maskInputFn) { + text = maskInputFn(text); + } + else { + text = '*'.repeat(text.length); + } + } + return text; +} +const ORIGINAL_ATTRIBUTE_NAME = '__rrweb_original__'; +function is2DCanvasBlank(canvas) { + const ctx = canvas.getContext('2d'); + if (!ctx) + return true; + const chunkSize = 50; + for (let x = 0; x < canvas.width; x += chunkSize) { + for (let y = 0; y < canvas.height; y += chunkSize) { + const getImageData = ctx.getImageData; + const originalGetImageData = ORIGINAL_ATTRIBUTE_NAME in getImageData + ? getImageData[ORIGINAL_ATTRIBUTE_NAME] + : getImageData; + const pixelBuffer = new Uint32Array(originalGetImageData.call(ctx, x, y, Math.min(chunkSize, canvas.width - x), Math.min(chunkSize, canvas.height - y)).data.buffer); + if (pixelBuffer.some((pixel) => pixel !== 0)) + return false; + } + } + return true; +} +function getInputType(element) { + const type = element.type; + return element.hasAttribute('data-rr-is-password') + ? 'password' + : type + ? type.toLowerCase() + : null; +} +function getInputValue(el, tagName, type) { + typeof type === 'string' ? type.toLowerCase() : ''; + if (tagName === 'INPUT' && (type === 'radio' || type === 'checkbox')) { + return el.getAttribute('value') || ''; + } + return el.value; +} + +let _id = 1; +const tagNameRegex = new RegExp('[^a-z0-9-_:]'); +const IGNORED_NODE = -2; +function defaultMaskFn(str) { + return str ? str.replace(/[\S]/g, '*') : ''; +} +function genId() { + return _id++; +} +function getValidTagName(element) { + if (element instanceof HTMLFormElement) { + return 'form'; + } + const processedTagName = element.tagName.toLowerCase().trim(); + if (tagNameRegex.test(processedTagName)) { + return 'div'; + } + return processedTagName; +} +function getCssRulesString(s) { + try { + const rules = s.rules || s.cssRules; + return rules ? Array.from(rules).map(getCssRuleString).join('') : null; + } + catch (error) { + return null; + } +} +function getCssRuleString(rule) { + let cssStringified = rule.cssText; + if (isCSSImportRule(rule)) { + try { + cssStringified = getCssRulesString(rule.styleSheet) || cssStringified; + } + catch (_a) { + } + } + return validateStringifiedCssRule(cssStringified); +} +function validateStringifiedCssRule(cssStringified) { + if (cssStringified.indexOf(':') > -1) { + const regex = /(\[(?:[\w-]+)[^\\])(:(?:[\w-]+)\])/gm; + return cssStringified.replace(regex, '$1\\$2'); + } + return cssStringified; +} +function isCSSImportRule(rule) { + return 'styleSheet' in rule; +} +function stringifyStyleSheet(sheet) { + return sheet.cssRules + ? Array.from(sheet.cssRules) + .map((rule) => rule.cssText ? validateStringifiedCssRule(rule.cssText) : '') + .join('') + : ''; +} +function extractOrigin(url) { + let origin = ''; + if (url.indexOf('//') > -1) { + origin = url.split('/').slice(0, 3).join('/'); + } + else { + origin = url.split('/')[0]; + } + origin = origin.split('?')[0]; + return origin; +} +let canvasService; +let canvasCtx; +const URL_IN_CSS_REF = /url\((?:(')([^']*)'|(")(.*?)"|([^)]*))\)/gm; +const RELATIVE_PATH = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/|#).*/; +const DATA_URI = /^(data:)([^,]*),(.*)/i; +function absoluteToStylesheet(cssText, href) { + return (cssText || '').replace(URL_IN_CSS_REF, (origin, quote1, path1, quote2, path2, path3) => { + const filePath = path1 || path2 || path3; + const maybeQuote = quote1 || quote2 || ''; + if (!filePath) { + return origin; + } + if (!RELATIVE_PATH.test(filePath)) { + return `url(${maybeQuote}${filePath}${maybeQuote})`; + } + if (DATA_URI.test(filePath)) { + return `url(${maybeQuote}${filePath}${maybeQuote})`; + } + if (filePath[0] === '/') { + return `url(${maybeQuote}${extractOrigin(href) + filePath}${maybeQuote})`; + } + const stack = href.split('/'); + const parts = filePath.split('/'); + stack.pop(); + for (const part of parts) { + if (part === '.') { + continue; + } + else if (part === '..') { + stack.pop(); + } + else { + stack.push(part); + } + } + return `url(${maybeQuote}${stack.join('/')}${maybeQuote})`; + }); +} +const SRCSET_NOT_SPACES = /^[^ \t\n\r\u000c]+/; +const SRCSET_COMMAS_OR_SPACES = /^[, \t\n\r\u000c]+/; +function getAbsoluteSrcsetString(doc, attributeValue) { + if (attributeValue.trim() === '') { + return attributeValue; + } + let pos = 0; + function collectCharacters(regEx) { + let chars; + let match = regEx.exec(attributeValue.substring(pos)); + if (match) { + chars = match[0]; + pos += chars.length; + return chars; + } + return ''; + } + let output = []; + while (true) { + collectCharacters(SRCSET_COMMAS_OR_SPACES); + if (pos >= attributeValue.length) { + break; + } + let url = collectCharacters(SRCSET_NOT_SPACES); + if (url.slice(-1) === ',') { + url = absoluteToDoc(doc, url.substring(0, url.length - 1)); + output.push(url); + } + else { + let descriptorsStr = ''; + url = absoluteToDoc(doc, url); + let inParens = false; + while (true) { + let c = attributeValue.charAt(pos); + if (c === '') { + output.push((url + descriptorsStr).trim()); + break; + } + else if (!inParens) { + if (c === ',') { + pos += 1; + output.push((url + descriptorsStr).trim()); + break; + } + else if (c === '(') { + inParens = true; + } + } + else { + if (c === ')') { + inParens = false; + } + } + descriptorsStr += c; + pos += 1; + } + } + } + return output.join(', '); +} +function absoluteToDoc(doc, attributeValue) { + if (!attributeValue || attributeValue.trim() === '') { + return attributeValue; + } + const a = doc.createElement('a'); + a.href = attributeValue; + return a.href; +} +function isSVGElement(el) { + return Boolean(el.tagName === 'svg' || el.ownerSVGElement); +} +function getHref() { + const a = document.createElement('a'); + a.href = ''; + return a.href; +} +function transformAttribute(doc, element, _tagName, _name, value, maskAllText, unmaskTextSelector, maskTextFn) { + if (!value) { + return value; + } + const name = _name.toLowerCase(); + const tagName = _tagName.toLowerCase(); + if (name === 'src' || name === 'href') { + return absoluteToDoc(doc, value); + } + else if (name === 'xlink:href' && value[0] !== '#') { + return absoluteToDoc(doc, value); + } + else if (name === 'background' && + (tagName === 'table' || tagName === 'td' || tagName === 'th')) { + return absoluteToDoc(doc, value); + } + else if (name === 'srcset') { + return getAbsoluteSrcsetString(doc, value); + } + else if (name === 'style') { + return absoluteToStylesheet(value, getHref()); + } + else if (tagName === 'object' && name === 'data') { + return absoluteToDoc(doc, value); + } + else if (maskAllText && + _shouldMaskAttribute(element, name, tagName, unmaskTextSelector)) { + return maskTextFn ? maskTextFn(value) : defaultMaskFn(value); + } + return value; +} +function _shouldMaskAttribute(element, attribute, tagName, unmaskTextSelector) { + if (unmaskTextSelector && element.matches(unmaskTextSelector)) { + return false; + } + return (['placeholder', 'title', 'aria-label'].indexOf(attribute) > -1 || + (tagName === 'input' && + attribute === 'value' && + element.hasAttribute('type') && + ['submit', 'button'].indexOf(element.getAttribute('type').toLowerCase()) > -1)); +} +function _isBlockedElement(element, blockClass, blockSelector, unblockSelector) { + if (unblockSelector && element.matches(unblockSelector)) { + return false; + } + if (typeof blockClass === 'string') { + if (element.classList.contains(blockClass)) { + return true; + } + } + else { + for (let eIndex = 0; eIndex < element.classList.length; eIndex++) { + const className = element.classList[eIndex]; + if (blockClass.test(className)) { + return true; + } + } + } + if (blockSelector) { + return element.matches(blockSelector); + } + return false; +} +function needMaskingText(node, maskTextClass, maskTextSelector, unmaskTextSelector, maskAllText) { + if (!node) { + return false; + } + if (node.nodeType !== node.ELEMENT_NODE) { + return needMaskingText(node.parentNode, maskTextClass, maskTextSelector, unmaskTextSelector, maskAllText); + } + if (unmaskTextSelector) { + if (node.matches(unmaskTextSelector) || + node.closest(unmaskTextSelector)) { + return false; + } + } + if (maskAllText) { + return true; + } + if (typeof maskTextClass === 'string') { + if (node.classList.contains(maskTextClass)) { + return true; + } + } + else { + for (let eIndex = 0; eIndex < node.classList.length; eIndex++) { + const className = node.classList[eIndex]; + if (maskTextClass.test(className)) { + return true; + } + } + } + if (maskTextSelector) { + if (node.matches(maskTextSelector)) { + return true; + } + } + return needMaskingText(node.parentNode, maskTextClass, maskTextSelector, unmaskTextSelector, maskAllText); +} +function onceIframeLoaded(iframeEl, listener, iframeLoadTimeout) { + const win = iframeEl.contentWindow; + if (!win) { + return; + } + let fired = false; + let readyState; + try { + readyState = win.document.readyState; + } + catch (error) { + return; + } + if (readyState !== 'complete') { + const timer = setTimeout(() => { + if (!fired) { + listener(); + fired = true; + } + }, iframeLoadTimeout); + iframeEl.addEventListener('load', () => { + clearTimeout(timer); + fired = true; + listener(); + }); + return; + } + const blankUrl = 'about:blank'; + if (win.location.href !== blankUrl || + iframeEl.src === blankUrl || + iframeEl.src === '') { + setTimeout(listener, 0); + return; + } + iframeEl.addEventListener('load', listener); +} +function serializeNode(n, options) { + var _a; + const { doc, blockClass, blockSelector, unblockSelector, maskTextClass, maskTextSelector, unmaskTextSelector, inlineStylesheet, maskInputSelector, unmaskInputSelector, maskAllText, maskInputOptions = {}, maskTextFn, maskInputFn, dataURLOptions = {}, inlineImages, recordCanvas, keepIframeSrcFn, } = options; + let rootId; + if (doc.__sn) { + const docId = doc.__sn.id; + rootId = docId === 1 ? undefined : docId; + } + switch (n.nodeType) { + case n.DOCUMENT_NODE: + if (n.compatMode !== 'CSS1Compat') { + return { + type: NodeType$1.Document, + childNodes: [], + compatMode: n.compatMode, + rootId, + }; + } + else { + return { + type: NodeType$1.Document, + childNodes: [], + rootId, + }; + } + case n.DOCUMENT_TYPE_NODE: + return { + type: NodeType$1.DocumentType, + name: n.name, + publicId: n.publicId, + systemId: n.systemId, + rootId, + }; + case n.ELEMENT_NODE: + const needBlock = _isBlockedElement(n, blockClass, blockSelector, unblockSelector); + const tagName = getValidTagName(n); + let attributes = {}; + for (const { name, value } of Array.from(n.attributes)) { + if (!skipAttribute(tagName, name)) { + attributes[name] = transformAttribute(doc, n, tagName, name, value, maskAllText, unmaskTextSelector, maskTextFn); + } + } + if (tagName === 'link' && inlineStylesheet) { + const stylesheet = Array.from(doc.styleSheets).find((s) => { + return s.href === n.href; + }); + let cssText = null; + if (stylesheet) { + cssText = getCssRulesString(stylesheet); + } + if (cssText) { + delete attributes.rel; + delete attributes.href; + attributes._cssText = absoluteToStylesheet(cssText, stylesheet.href); + } + } + if (tagName === 'style' && + n.sheet && + !(n.innerText || + n.textContent || + '').trim().length) { + const cssText = getCssRulesString(n.sheet); + if (cssText) { + attributes._cssText = absoluteToStylesheet(cssText, getHref()); + } + } + if (tagName === 'input' || + tagName === 'textarea' || + tagName === 'select' || + tagName === 'option') { + const el = n; + const type = getInputType(el); + const value = getInputValue(el, tagName.toUpperCase(), type); + const checked = n.checked; + if (type !== 'submit' && + type !== 'button' && + value) { + attributes.value = maskInputValue({ + input: el, + type, + tagName, + value, + maskInputSelector, + unmaskInputSelector, + maskInputOptions, + maskInputFn, + }); + } + if (checked) { + attributes.checked = checked; + } + } + if (tagName === 'option') { + if (n.selected && !maskInputOptions['select']) { + attributes.selected = true; + } + else { + delete attributes.selected; + } + } + if (tagName === 'canvas' && recordCanvas) { + if (n.__context === '2d') { + if (!is2DCanvasBlank(n)) { + attributes.rr_dataURL = n.toDataURL(dataURLOptions.type, dataURLOptions.quality); + } + } + else if (!('__context' in n)) { + const canvasDataURL = n.toDataURL(dataURLOptions.type, dataURLOptions.quality); + const blankCanvas = document.createElement('canvas'); + blankCanvas.width = n.width; + blankCanvas.height = n.height; + const blankCanvasDataURL = blankCanvas.toDataURL(dataURLOptions.type, dataURLOptions.quality); + if (canvasDataURL !== blankCanvasDataURL) { + attributes.rr_dataURL = canvasDataURL; + } + } + } + if (tagName === 'img' && inlineImages) { + if (!canvasService) { + canvasService = doc.createElement('canvas'); + canvasCtx = canvasService.getContext('2d'); + } + const image = n; + const oldValue = image.crossOrigin; + image.crossOrigin = 'anonymous'; + const recordInlineImage = () => { + try { + canvasService.width = image.naturalWidth; + canvasService.height = image.naturalHeight; + canvasCtx.drawImage(image, 0, 0); + attributes.rr_dataURL = canvasService.toDataURL(dataURLOptions.type, dataURLOptions.quality); + } + catch (err) { + console.warn(`Cannot inline img src=${image.currentSrc}! Error: ${err}`); + } + oldValue + ? (attributes.crossOrigin = oldValue) + : delete attributes.crossOrigin; + }; + if (image.complete && image.naturalWidth !== 0) + recordInlineImage(); + else + image.onload = recordInlineImage; + } + if (tagName === 'audio' || tagName === 'video') { + attributes.rr_mediaState = n.paused + ? 'paused' + : 'played'; + attributes.rr_mediaCurrentTime = n.currentTime; + } + if (n.scrollLeft) { + attributes.rr_scrollLeft = n.scrollLeft; + } + if (n.scrollTop) { + attributes.rr_scrollTop = n.scrollTop; + } + if (needBlock) { + const { width, height } = n.getBoundingClientRect(); + attributes = { + class: attributes.class, + rr_width: `${width}px`, + rr_height: `${height}px`, + }; + } + if (tagName === 'iframe' && !keepIframeSrcFn(attributes.src)) { + if (!n.contentDocument) { + attributes.rr_src = attributes.src; + } + delete attributes.src; + } + return { + type: NodeType$1.Element, + tagName, + attributes, + childNodes: [], + isSVG: isSVGElement(n) || undefined, + needBlock, + rootId, + }; + case n.TEXT_NODE: + const parentTagName = n.parentNode && n.parentNode.tagName; + let textContent = n.textContent; + const isStyle = parentTagName === 'STYLE' ? true : undefined; + const isScript = parentTagName === 'SCRIPT' ? true : undefined; + if (isStyle && textContent) { + try { + if (n.nextSibling || n.previousSibling) { + } + else if ((_a = n.parentNode.sheet) === null || _a === void 0 ? void 0 : _a.cssRules) { + textContent = stringifyStyleSheet(n.parentNode.sheet); + } + } + catch (err) { + console.warn(`Cannot get CSS styles from text's parentNode. Error: ${err}`, n); + } + textContent = absoluteToStylesheet(textContent, getHref()); + } + if (isScript) { + textContent = 'SCRIPT_PLACEHOLDER'; + } + if (parentTagName === 'TEXTAREA' && textContent) { + textContent = ''; + } + else if (parentTagName === 'OPTION' && textContent) { + const option = n.parentNode; + textContent = maskInputValue({ + input: option, + type: null, + tagName: parentTagName, + value: textContent, + maskInputSelector, + unmaskInputSelector, + maskInputOptions, + maskInputFn, + }); + } + else if (!isStyle && + !isScript && + needMaskingText(n, maskTextClass, maskTextSelector, unmaskTextSelector, maskAllText) && + textContent) { + textContent = maskTextFn + ? maskTextFn(textContent) + : defaultMaskFn(textContent); + } + return { + type: NodeType$1.Text, + textContent: textContent || '', + isStyle, + rootId, + }; + case n.CDATA_SECTION_NODE: + return { + type: NodeType$1.CDATA, + textContent: '', + rootId, + }; + case n.COMMENT_NODE: + return { + type: NodeType$1.Comment, + textContent: n.textContent || '', + rootId, + }; + default: + return false; + } +} +function lowerIfExists(maybeAttr) { + if (maybeAttr === undefined || maybeAttr === null) { + return ''; + } + else { + return maybeAttr.toLowerCase(); + } +} +function slimDOMExcluded(sn, slimDOMOptions) { + if (slimDOMOptions.comment && sn.type === NodeType$1.Comment) { + return true; + } + else if (sn.type === NodeType$1.Element) { + if (slimDOMOptions.script && + (sn.tagName === 'script' || + (sn.tagName === 'link' && + (sn.attributes.rel === 'preload' || + sn.attributes.rel === 'modulepreload') && + sn.attributes.as === 'script') || + (sn.tagName === 'link' && + sn.attributes.rel === 'prefetch' && + typeof sn.attributes.href === 'string' && + sn.attributes.href.endsWith('.js')))) { + return true; + } + else if (slimDOMOptions.headFavicon && + ((sn.tagName === 'link' && sn.attributes.rel === 'shortcut icon') || + (sn.tagName === 'meta' && + (lowerIfExists(sn.attributes.name).match(/^msapplication-tile(image|color)$/) || + lowerIfExists(sn.attributes.name) === 'application-name' || + lowerIfExists(sn.attributes.rel) === 'icon' || + lowerIfExists(sn.attributes.rel) === 'apple-touch-icon' || + lowerIfExists(sn.attributes.rel) === 'shortcut icon')))) { + return true; + } + else if (sn.tagName === 'meta') { + if (slimDOMOptions.headMetaDescKeywords && + lowerIfExists(sn.attributes.name).match(/^description|keywords$/)) { + return true; + } + else if (slimDOMOptions.headMetaSocial && + (lowerIfExists(sn.attributes.property).match(/^(og|twitter|fb):/) || + lowerIfExists(sn.attributes.name).match(/^(og|twitter):/) || + lowerIfExists(sn.attributes.name) === 'pinterest')) { + return true; + } + else if (slimDOMOptions.headMetaRobots && + (lowerIfExists(sn.attributes.name) === 'robots' || + lowerIfExists(sn.attributes.name) === 'googlebot' || + lowerIfExists(sn.attributes.name) === 'bingbot')) { + return true; + } + else if (slimDOMOptions.headMetaHttpEquiv && + sn.attributes['http-equiv'] !== undefined) { + return true; + } + else if (slimDOMOptions.headMetaAuthorship && + (lowerIfExists(sn.attributes.name) === 'author' || + lowerIfExists(sn.attributes.name) === 'generator' || + lowerIfExists(sn.attributes.name) === 'framework' || + lowerIfExists(sn.attributes.name) === 'publisher' || + lowerIfExists(sn.attributes.name) === 'progid' || + lowerIfExists(sn.attributes.property).match(/^article:/) || + lowerIfExists(sn.attributes.property).match(/^product:/))) { + return true; + } + else if (slimDOMOptions.headMetaVerification && + (lowerIfExists(sn.attributes.name) === 'google-site-verification' || + lowerIfExists(sn.attributes.name) === 'yandex-verification' || + lowerIfExists(sn.attributes.name) === 'csrf-token' || + lowerIfExists(sn.attributes.name) === 'p:domain_verify' || + lowerIfExists(sn.attributes.name) === 'verify-v1' || + lowerIfExists(sn.attributes.name) === 'verification' || + lowerIfExists(sn.attributes.name) === 'shopify-checkout-api-token')) { + return true; + } + } + } + return false; +} +function serializeNodeWithId(n, options) { + const { doc, map, blockClass, blockSelector, unblockSelector, maskTextClass, maskTextSelector, unmaskTextSelector, skipChild = false, inlineStylesheet = true, maskInputSelector, unmaskInputSelector, maskAllText, maskInputOptions = {}, maskTextFn, maskInputFn, slimDOMOptions, dataURLOptions = {}, inlineImages = false, recordCanvas = false, onSerialize, onIframeLoad, iframeLoadTimeout = 5000, keepIframeSrcFn = () => false, } = options; + let { preserveWhiteSpace = true } = options; + const _serializedNode = serializeNode(n, { + doc, + blockClass, + blockSelector, + unblockSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + inlineStylesheet, + maskInputSelector, + unmaskInputSelector, + maskAllText, + maskInputOptions, + maskTextFn, + maskInputFn, + dataURLOptions, + inlineImages, + recordCanvas, + keepIframeSrcFn, + }); + if (!_serializedNode) { + console.warn(n, 'not serialized'); + return null; + } + let id; + if ('__sn' in n) { + id = n.__sn.id; + } + else if (slimDOMExcluded(_serializedNode, slimDOMOptions) || + (!preserveWhiteSpace && + _serializedNode.type === NodeType$1.Text && + !_serializedNode.isStyle && + !_serializedNode.textContent.replace(/^\s+|\s+$/gm, '').length)) { + id = IGNORED_NODE; + } + else { + id = genId(); + } + const serializedNode = Object.assign(_serializedNode, { id }); + n.__sn = serializedNode; + if (id === IGNORED_NODE) { + return null; + } + map[id] = n; + if (onSerialize) { + onSerialize(n); + } + let recordChild = !skipChild; + if (serializedNode.type === NodeType$1.Element) { + recordChild = recordChild && !serializedNode.needBlock; + delete serializedNode.needBlock; + if (n.shadowRoot) + serializedNode.isShadowHost = true; + } + if ((serializedNode.type === NodeType$1.Document || + serializedNode.type === NodeType$1.Element) && + recordChild) { + if (slimDOMOptions.headWhitespace && + _serializedNode.type === NodeType$1.Element && + _serializedNode.tagName === 'head') { + preserveWhiteSpace = false; + } + const bypassOptions = { + doc, + map, + blockClass, + blockSelector, + unblockSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + skipChild, + inlineStylesheet, + maskInputSelector, + unmaskInputSelector, + maskAllText, + maskInputOptions, + maskTextFn, + maskInputFn, + slimDOMOptions, + dataURLOptions, + inlineImages, + recordCanvas, + preserveWhiteSpace, + onSerialize, + onIframeLoad, + iframeLoadTimeout, + keepIframeSrcFn, + }; + for (const childN of Array.from(n.childNodes)) { + const serializedChildNode = serializeNodeWithId(childN, bypassOptions); + if (serializedChildNode) { + serializedNode.childNodes.push(serializedChildNode); + } + } + if (isElement(n) && n.shadowRoot) { + for (const childN of Array.from(n.shadowRoot.childNodes)) { + const serializedChildNode = serializeNodeWithId(childN, bypassOptions); + if (serializedChildNode) { + serializedChildNode.isShadow = true; + serializedNode.childNodes.push(serializedChildNode); + } + } + } + } + if (n.parentNode && isShadowRoot(n.parentNode)) { + serializedNode.isShadow = true; + } + if (serializedNode.type === NodeType$1.Element && + serializedNode.tagName === 'iframe') { + onceIframeLoaded(n, () => { + const iframeDoc = n.contentDocument; + if (iframeDoc && onIframeLoad) { + const serializedIframeNode = serializeNodeWithId(iframeDoc, { + doc: iframeDoc, + map, + blockClass, + blockSelector, + unblockSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + skipChild: false, + inlineStylesheet, + maskInputSelector, + unmaskInputSelector, + maskAllText, + maskInputOptions, + maskTextFn, + maskInputFn, + slimDOMOptions, + dataURLOptions, + inlineImages, + recordCanvas, + preserveWhiteSpace, + onSerialize, + onIframeLoad, + iframeLoadTimeout, + keepIframeSrcFn, + }); + if (serializedIframeNode) { + onIframeLoad(n, serializedIframeNode); + } + } + }, iframeLoadTimeout); + } + return serializedNode; +} +function snapshot(n, options) { + const { blockClass = 'rr-block', blockSelector = null, unblockSelector = null, maskTextClass = 'rr-mask', maskTextSelector = null, unmaskTextSelector = null, inlineStylesheet = true, inlineImages = false, recordCanvas = false, maskInputSelector = null, unmaskInputSelector = null, maskAllText = false, maskAllInputs = false, maskTextFn, maskInputFn, slimDOM = false, dataURLOptions, preserveWhiteSpace, onSerialize, onIframeLoad, iframeLoadTimeout, keepIframeSrcFn = () => false, } = options || {}; + const idNodeMap = {}; + const maskInputOptions = maskAllInputs === true + ? { + color: true, + date: true, + 'datetime-local': true, + email: true, + month: true, + number: true, + range: true, + search: true, + tel: true, + text: true, + time: true, + url: true, + week: true, + textarea: true, + select: true, + } + : maskAllInputs === false + ? {} + : maskAllInputs; + const slimDOMOptions = slimDOM === true || slimDOM === 'all' + ? + { + script: true, + comment: true, + headFavicon: true, + headWhitespace: true, + headMetaDescKeywords: slimDOM === 'all', + headMetaSocial: true, + headMetaRobots: true, + headMetaHttpEquiv: true, + headMetaAuthorship: true, + headMetaVerification: true, + } + : slimDOM === false + ? {} + : slimDOM; + return [ + serializeNodeWithId(n, { + doc: n, + map: idNodeMap, + blockClass, + blockSelector, + unblockSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + skipChild: false, + inlineStylesheet, + maskInputSelector, + unmaskInputSelector, + maskAllText, + maskInputOptions, + maskTextFn, + maskInputFn, + slimDOMOptions, + dataURLOptions, + inlineImages, + recordCanvas, + preserveWhiteSpace, + onSerialize, + onIframeLoad, + iframeLoadTimeout, + keepIframeSrcFn, + }), + idNodeMap, + ]; +} +function skipAttribute(tagName, attributeName, value) { + return ((tagName === 'video' || tagName === 'audio') && attributeName === 'autoplay'); +} + +var EventType; +(function (EventType) { + EventType[EventType["DomContentLoaded"] = 0] = "DomContentLoaded"; + EventType[EventType["Load"] = 1] = "Load"; + EventType[EventType["FullSnapshot"] = 2] = "FullSnapshot"; + EventType[EventType["IncrementalSnapshot"] = 3] = "IncrementalSnapshot"; + EventType[EventType["Meta"] = 4] = "Meta"; + EventType[EventType["Custom"] = 5] = "Custom"; + EventType[EventType["Plugin"] = 6] = "Plugin"; +})(EventType || (EventType = {})); +var IncrementalSource; +(function (IncrementalSource) { + IncrementalSource[IncrementalSource["Mutation"] = 0] = "Mutation"; + IncrementalSource[IncrementalSource["MouseMove"] = 1] = "MouseMove"; + IncrementalSource[IncrementalSource["MouseInteraction"] = 2] = "MouseInteraction"; + IncrementalSource[IncrementalSource["Scroll"] = 3] = "Scroll"; + IncrementalSource[IncrementalSource["ViewportResize"] = 4] = "ViewportResize"; + IncrementalSource[IncrementalSource["Input"] = 5] = "Input"; + IncrementalSource[IncrementalSource["TouchMove"] = 6] = "TouchMove"; + IncrementalSource[IncrementalSource["MediaInteraction"] = 7] = "MediaInteraction"; + IncrementalSource[IncrementalSource["StyleSheetRule"] = 8] = "StyleSheetRule"; + IncrementalSource[IncrementalSource["CanvasMutation"] = 9] = "CanvasMutation"; + IncrementalSource[IncrementalSource["Font"] = 10] = "Font"; + IncrementalSource[IncrementalSource["Log"] = 11] = "Log"; + IncrementalSource[IncrementalSource["Drag"] = 12] = "Drag"; + IncrementalSource[IncrementalSource["StyleDeclaration"] = 13] = "StyleDeclaration"; +})(IncrementalSource || (IncrementalSource = {})); +var MouseInteractions; +(function (MouseInteractions) { + MouseInteractions[MouseInteractions["MouseUp"] = 0] = "MouseUp"; + MouseInteractions[MouseInteractions["MouseDown"] = 1] = "MouseDown"; + MouseInteractions[MouseInteractions["Click"] = 2] = "Click"; + MouseInteractions[MouseInteractions["ContextMenu"] = 3] = "ContextMenu"; + MouseInteractions[MouseInteractions["DblClick"] = 4] = "DblClick"; + MouseInteractions[MouseInteractions["Focus"] = 5] = "Focus"; + MouseInteractions[MouseInteractions["Blur"] = 6] = "Blur"; + MouseInteractions[MouseInteractions["TouchStart"] = 7] = "TouchStart"; + MouseInteractions[MouseInteractions["TouchMove_Departed"] = 8] = "TouchMove_Departed"; + MouseInteractions[MouseInteractions["TouchEnd"] = 9] = "TouchEnd"; + MouseInteractions[MouseInteractions["TouchCancel"] = 10] = "TouchCancel"; +})(MouseInteractions || (MouseInteractions = {})); +var CanvasContext; +(function (CanvasContext) { + CanvasContext[CanvasContext["2D"] = 0] = "2D"; + CanvasContext[CanvasContext["WebGL"] = 1] = "WebGL"; + CanvasContext[CanvasContext["WebGL2"] = 2] = "WebGL2"; +})(CanvasContext || (CanvasContext = {})); +var MediaInteractions; +(function (MediaInteractions) { + MediaInteractions[MediaInteractions["Play"] = 0] = "Play"; + MediaInteractions[MediaInteractions["Pause"] = 1] = "Pause"; + MediaInteractions[MediaInteractions["Seeked"] = 2] = "Seeked"; + MediaInteractions[MediaInteractions["VolumeChange"] = 3] = "VolumeChange"; +})(MediaInteractions || (MediaInteractions = {})); +var ReplayerEvents; +(function (ReplayerEvents) { + ReplayerEvents["Start"] = "start"; + ReplayerEvents["Pause"] = "pause"; + ReplayerEvents["Resume"] = "resume"; + ReplayerEvents["Resize"] = "resize"; + ReplayerEvents["Finish"] = "finish"; + ReplayerEvents["FullsnapshotRebuilded"] = "fullsnapshot-rebuilded"; + ReplayerEvents["LoadStylesheetStart"] = "load-stylesheet-start"; + ReplayerEvents["LoadStylesheetEnd"] = "load-stylesheet-end"; + ReplayerEvents["SkipStart"] = "skip-start"; + ReplayerEvents["SkipEnd"] = "skip-end"; + ReplayerEvents["MouseInteraction"] = "mouse-interaction"; + ReplayerEvents["EventCast"] = "event-cast"; + ReplayerEvents["CustomEvent"] = "custom-event"; + ReplayerEvents["Flush"] = "flush"; + ReplayerEvents["StateChange"] = "state-change"; + ReplayerEvents["PlayBack"] = "play-back"; +})(ReplayerEvents || (ReplayerEvents = {})); + +function on(type, fn, target = document) { + const options = { capture: true, passive: true }; + target.addEventListener(type, fn, options); + return () => target.removeEventListener(type, fn, options); +} +function createMirror() { + return { + map: {}, + getId(n) { + if (!n || !n.__sn) { + return -1; + } + return n.__sn.id; + }, + getNode(id) { + return this.map[id] || null; + }, + removeNodeFromMap(n) { + const id = n.__sn && n.__sn.id; + delete this.map[id]; + if (n.childNodes) { + n.childNodes.forEach((child) => this.removeNodeFromMap(child)); + } + }, + has(id) { + return this.map.hasOwnProperty(id); + }, + reset() { + this.map = {}; + }, + }; +} +const DEPARTED_MIRROR_ACCESS_WARNING = 'Please stop import mirror directly. Instead of that,' + + '\r\n' + + 'now you can use replayer.getMirror() to access the mirror instance of a replayer,' + + '\r\n' + + 'or you can use record.mirror to access the mirror instance during recording.'; +let _mirror = { + map: {}, + getId() { + console.error(DEPARTED_MIRROR_ACCESS_WARNING); + return -1; + }, + getNode() { + console.error(DEPARTED_MIRROR_ACCESS_WARNING); + return null; + }, + removeNodeFromMap() { + console.error(DEPARTED_MIRROR_ACCESS_WARNING); + }, + has() { + console.error(DEPARTED_MIRROR_ACCESS_WARNING); + return false; + }, + reset() { + console.error(DEPARTED_MIRROR_ACCESS_WARNING); + }, +}; +if (typeof window !== 'undefined' && window.Proxy && window.Reflect) { + _mirror = new Proxy(_mirror, { + get(target, prop, receiver) { + if (prop === 'map') { + console.error(DEPARTED_MIRROR_ACCESS_WARNING); + } + return Reflect.get(target, prop, receiver); + }, + }); +} +function throttle$1(func, wait, options = {}) { + let timeout = null; + let previous = 0; + return function (arg) { + let now = Date.now(); + if (!previous && options.leading === false) { + previous = now; + } + let remaining = wait - (now - previous); + let context = this; + let args = arguments; + if (remaining <= 0 || remaining > wait) { + if (timeout) { + clearTimeout(timeout); + timeout = null; + } + previous = now; + func.apply(context, args); + } + else if (!timeout && options.trailing !== false) { + timeout = setTimeout(() => { + previous = options.leading === false ? 0 : Date.now(); + timeout = null; + func.apply(context, args); + }, remaining); + } + }; +} +function hookSetter(target, key, d, isRevoked, win = window) { + const original = win.Object.getOwnPropertyDescriptor(target, key); + win.Object.defineProperty(target, key, isRevoked + ? d + : { + set(value) { + setTimeout(() => { + d.set.call(this, value); + }, 0); + if (original && original.set) { + original.set.call(this, value); + } + }, + }); + return () => hookSetter(target, key, original || {}, true); +} +function patch(source, name, replacement) { + try { + if (!(name in source)) { + return () => { }; + } + const original = source[name]; + const wrapped = replacement(original); + if (typeof wrapped === 'function') { + wrapped.prototype = wrapped.prototype || {}; + Object.defineProperties(wrapped, { + __rrweb_original__: { + enumerable: false, + value: original, + }, + }); + } + source[name] = wrapped; + return () => { + source[name] = original; + }; + } + catch (_a) { + return () => { }; + } +} +function getWindowHeight() { + return (window.innerHeight || + (document.documentElement && document.documentElement.clientHeight) || + (document.body && document.body.clientHeight)); +} +function getWindowWidth() { + return (window.innerWidth || + (document.documentElement && document.documentElement.clientWidth) || + (document.body && document.body.clientWidth)); +} +function isBlocked(node, blockClass, blockSelector, unblockSelector) { + if (!node) { + return false; + } + if (node.nodeType === node.ELEMENT_NODE) { + let needBlock = false; + const needUnblock = unblockSelector && node.matches(unblockSelector); + if (typeof blockClass === 'string') { + if (node.closest !== undefined) { + needBlock = + !needUnblock && + node.closest('.' + blockClass) !== null; + } + else { + needBlock = + !needUnblock && node.classList.contains(blockClass); + } + } + else { + !needUnblock && + node.classList.forEach((className) => { + if (blockClass.test(className)) { + needBlock = true; + } + }); + } + if (!needBlock && blockSelector) { + needBlock = node.matches(blockSelector); + } + return ((!needUnblock && needBlock) || + isBlocked(node.parentNode, blockClass, blockSelector, unblockSelector)); + } + if (node.nodeType === node.TEXT_NODE) { + return isBlocked(node.parentNode, blockClass, blockSelector, unblockSelector); + } + return isBlocked(node.parentNode, blockClass, blockSelector, unblockSelector); +} +function isIgnored(n) { + if ('__sn' in n) { + return n.__sn.id === IGNORED_NODE; + } + return false; +} +function isAncestorRemoved(target, mirror) { + if (isShadowRoot(target)) { + return false; + } + const id = mirror.getId(target); + if (!mirror.has(id)) { + return true; + } + if (target.parentNode && + target.parentNode.nodeType === target.DOCUMENT_NODE) { + return false; + } + if (!target.parentNode) { + return true; + } + return isAncestorRemoved(target.parentNode, mirror); +} +function isTouchEvent(event) { + return Boolean(event.changedTouches); +} +function polyfill(win = window) { + if ('NodeList' in win && !win.NodeList.prototype.forEach) { + win.NodeList.prototype.forEach = Array.prototype + .forEach; + } + if ('DOMTokenList' in win && !win.DOMTokenList.prototype.forEach) { + win.DOMTokenList.prototype.forEach = Array.prototype + .forEach; + } + if (!Node.prototype.contains) { + Node.prototype.contains = function contains(node) { + if (!(0 in arguments)) { + throw new TypeError('1 argument is required'); + } + do { + if (this === node) { + return true; + } + } while ((node = node && node.parentNode)); + return false; + }; + } +} +function isIframeINode(node) { + if ('__sn' in node) { + return (node.__sn.type === NodeType$1.Element && node.__sn.tagName === 'iframe'); + } + return false; +} +function hasShadowRoot(n) { + return Boolean(n === null || n === void 0 ? void 0 : n.shadowRoot); +} + +function isNodeInLinkedList(n) { + return '__ln' in n; +} +class DoubleLinkedList { + constructor() { + this.length = 0; + this.head = null; + } + get(position) { + if (position >= this.length) { + throw new Error('Position outside of list range'); + } + let current = this.head; + for (let index = 0; index < position; index++) { + current = (current === null || current === void 0 ? void 0 : current.next) || null; + } + return current; + } + addNode(n) { + const node = { + value: n, + previous: null, + next: null, + }; + n.__ln = node; + if (n.previousSibling && isNodeInLinkedList(n.previousSibling)) { + const current = n.previousSibling.__ln.next; + node.next = current; + node.previous = n.previousSibling.__ln; + n.previousSibling.__ln.next = node; + if (current) { + current.previous = node; + } + } + else if (n.nextSibling && + isNodeInLinkedList(n.nextSibling) && + n.nextSibling.__ln.previous) { + const current = n.nextSibling.__ln.previous; + node.previous = current; + node.next = n.nextSibling.__ln; + n.nextSibling.__ln.previous = node; + if (current) { + current.next = node; + } + } + else { + if (this.head) { + this.head.previous = node; + } + node.next = this.head; + this.head = node; + } + this.length++; + } + removeNode(n) { + const current = n.__ln; + if (!this.head) { + return; + } + if (!current.previous) { + this.head = current.next; + if (this.head) { + this.head.previous = null; + } + } + else { + current.previous.next = current.next; + if (current.next) { + current.next.previous = current.previous; + } + } + if (n.__ln) { + delete n.__ln; + } + this.length--; + } +} +const moveKey = (id, parentId) => `${id}@${parentId}`; +function isINode(n) { + return '__sn' in n; +} +class MutationBuffer { + constructor() { + this.frozen = false; + this.locked = false; + this.texts = []; + this.attributes = []; + this.removes = []; + this.mapRemoves = []; + this.movedMap = {}; + this.addedSet = new Set(); + this.movedSet = new Set(); + this.droppedSet = new Set(); + this.processMutations = (mutations) => { + mutations.forEach(this.processMutation); + this.emit(); + }; + this.emit = () => { + if (this.frozen || this.locked) { + return; + } + const adds = []; + const addList = new DoubleLinkedList(); + const getNextId = (n) => { + let ns = n; + let nextId = IGNORED_NODE; + while (nextId === IGNORED_NODE) { + ns = ns && ns.nextSibling; + nextId = ns && this.mirror.getId(ns); + } + return nextId; + }; + const pushAdd = (n) => { + var _a, _b, _c, _d, _e; + const shadowHost = n.getRootNode + ? (_a = n.getRootNode()) === null || _a === void 0 ? void 0 : _a.host + : null; + let rootShadowHost = shadowHost; + while ((_c = (_b = rootShadowHost === null || rootShadowHost === void 0 ? void 0 : rootShadowHost.getRootNode) === null || _b === void 0 ? void 0 : _b.call(rootShadowHost)) === null || _c === void 0 ? void 0 : _c.host) + rootShadowHost = + ((_e = (_d = rootShadowHost === null || rootShadowHost === void 0 ? void 0 : rootShadowHost.getRootNode) === null || _d === void 0 ? void 0 : _d.call(rootShadowHost)) === null || _e === void 0 ? void 0 : _e.host) || + null; + const notInDoc = !this.doc.contains(n) && + (!rootShadowHost || !this.doc.contains(rootShadowHost)); + if (!n.parentNode || notInDoc) { + return; + } + const parentId = isShadowRoot(n.parentNode) + ? this.mirror.getId(shadowHost) + : this.mirror.getId(n.parentNode); + const nextId = getNextId(n); + if (parentId === -1 || nextId === -1) { + return addList.addNode(n); + } + let sn = serializeNodeWithId(n, { + doc: this.doc, + map: this.mirror.map, + blockClass: this.blockClass, + blockSelector: this.blockSelector, + unblockSelector: this.unblockSelector, + maskTextClass: this.maskTextClass, + maskTextSelector: this.maskTextSelector, + unmaskTextSelector: this.unmaskTextSelector, + maskInputSelector: this.maskInputSelector, + unmaskInputSelector: this.unmaskInputSelector, + skipChild: true, + inlineStylesheet: this.inlineStylesheet, + maskAllText: this.maskAllText, + maskInputOptions: this.maskInputOptions, + maskTextFn: this.maskTextFn, + maskInputFn: this.maskInputFn, + slimDOMOptions: this.slimDOMOptions, + recordCanvas: this.recordCanvas, + inlineImages: this.inlineImages, + onSerialize: (currentN) => { + if (isIframeINode(currentN)) { + this.iframeManager.addIframe(currentN); + } + if (hasShadowRoot(n)) { + this.shadowDomManager.addShadowRoot(n.shadowRoot, document); + } + }, + onIframeLoad: (iframe, childSn) => { + this.iframeManager.attachIframe(iframe, childSn); + this.shadowDomManager.observeAttachShadow(iframe); + }, + }); + if (sn) { + adds.push({ + parentId, + nextId, + node: sn, + }); + } + }; + while (this.mapRemoves.length) { + this.mirror.removeNodeFromMap(this.mapRemoves.shift()); + } + for (const n of this.movedSet) { + if (isParentRemoved(this.removes, n, this.mirror) && + !this.movedSet.has(n.parentNode)) { + continue; + } + pushAdd(n); + } + for (const n of this.addedSet) { + if (!isAncestorInSet(this.droppedSet, n) && + !isParentRemoved(this.removes, n, this.mirror)) { + pushAdd(n); + } + else if (isAncestorInSet(this.movedSet, n)) { + pushAdd(n); + } + else { + this.droppedSet.add(n); + } + } + let candidate = null; + while (addList.length) { + let node = null; + if (candidate) { + const parentId = this.mirror.getId(candidate.value.parentNode); + const nextId = getNextId(candidate.value); + if (parentId !== -1 && nextId !== -1) { + node = candidate; + } + } + if (!node) { + for (let index = addList.length - 1; index >= 0; index--) { + const _node = addList.get(index); + if (_node) { + const parentId = this.mirror.getId(_node.value.parentNode); + const nextId = getNextId(_node.value); + if (parentId !== -1 && nextId !== -1) { + node = _node; + break; + } + } + } + } + if (!node) { + while (addList.head) { + addList.removeNode(addList.head.value); + } + break; + } + candidate = node.previous; + addList.removeNode(node.value); + pushAdd(node.value); + } + const payload = { + texts: this.texts + .map((text) => ({ + id: this.mirror.getId(text.node), + value: text.value, + })) + .filter((text) => this.mirror.has(text.id)), + attributes: this.attributes + .map((attribute) => ({ + id: this.mirror.getId(attribute.node), + attributes: attribute.attributes, + })) + .filter((attribute) => this.mirror.has(attribute.id)), + removes: this.removes, + adds, + }; + if (!payload.texts.length && + !payload.attributes.length && + !payload.removes.length && + !payload.adds.length) { + return; + } + this.texts = []; + this.attributes = []; + this.removes = []; + this.addedSet = new Set(); + this.movedSet = new Set(); + this.droppedSet = new Set(); + this.movedMap = {}; + this.mutationCb(payload); + }; + this.processMutation = (m) => { + if (isIgnored(m.target)) { + return; + } + switch (m.type) { + case 'characterData': { + const value = m.target.textContent; + if (!isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector) && value !== m.oldValue) { + this.texts.push({ + value: needMaskingText(m.target, this.maskTextClass, this.maskTextSelector, this.unmaskTextSelector, this.maskAllText) && value + ? this.maskTextFn + ? this.maskTextFn(value) + : value.replace(/[\S]/g, '*') + : value, + node: m.target, + }); + } + break; + } + case 'attributes': { + const target = m.target; + let value = target.getAttribute(m.attributeName); + if (m.attributeName === 'value') { + value = maskInputValue({ + input: target, + maskInputSelector: this.maskInputSelector, + unmaskInputSelector: this.unmaskInputSelector, + maskInputOptions: this.maskInputOptions, + tagName: target.tagName, + type: target.getAttribute('type'), + value, + maskInputFn: this.maskInputFn, + }); + } + if (isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector) || value === m.oldValue) { + return; + } + let item = this.attributes.find((a) => a.node === m.target); + if (!item) { + item = { + node: m.target, + attributes: {}, + }; + this.attributes.push(item); + } + if (m.attributeName === 'type' && + target.tagName === 'INPUT' && + (m.oldValue || '').toLowerCase() === 'password') { + target.setAttribute('data-rr-is-password', 'true'); + } + if (m.attributeName === 'style') { + const old = this.doc.createElement('span'); + if (m.oldValue) { + old.setAttribute('style', m.oldValue); + } + if (item.attributes.style === undefined || + item.attributes.style === null) { + item.attributes.style = {}; + } + try { + const styleObj = item.attributes.style; + for (const pname of Array.from(target.style)) { + const newValue = target.style.getPropertyValue(pname); + const newPriority = target.style.getPropertyPriority(pname); + if (newValue !== old.style.getPropertyValue(pname) || + newPriority !== old.style.getPropertyPriority(pname)) { + if (newPriority === '') { + styleObj[pname] = newValue; + } + else { + styleObj[pname] = [newValue, newPriority]; + } + } + } + for (const pname of Array.from(old.style)) { + if (target.style.getPropertyValue(pname) === '') { + styleObj[pname] = false; + } + } + } + catch (error) { + console.warn('[rrweb] Error when parsing update to style attribute:', error); + } + } + else { + const element = m.target; + item.attributes[m.attributeName] = transformAttribute(this.doc, element, element.tagName, m.attributeName, value, this.maskAllText, this.unmaskTextSelector, this.maskTextFn); + } + break; + } + case 'childList': { + m.addedNodes.forEach((n) => this.genAdds(n, m.target)); + m.removedNodes.forEach((n) => { + const nodeId = this.mirror.getId(n); + const parentId = isShadowRoot(m.target) + ? this.mirror.getId(m.target.host) + : this.mirror.getId(m.target); + if (isBlocked(m.target, this.blockClass, this.blockSelector, this.unblockSelector) || isIgnored(n)) { + return; + } + if (this.addedSet.has(n)) { + deepDelete(this.addedSet, n); + this.droppedSet.add(n); + } + else if (this.addedSet.has(m.target) && nodeId === -1) ; + else if (isAncestorRemoved(m.target, this.mirror)) ; + else if (this.movedSet.has(n) && + this.movedMap[moveKey(nodeId, parentId)]) { + deepDelete(this.movedSet, n); + } + else { + this.removes.push({ + parentId, + id: nodeId, + isShadow: isShadowRoot(m.target) ? true : undefined, + }); + } + this.mapRemoves.push(n); + }); + break; + } + } + }; + this.genAdds = (n, target) => { + if (target && isBlocked(target, this.blockClass, this.blockSelector, this.unblockSelector)) { + return; + } + if (isINode(n)) { + if (isIgnored(n)) { + return; + } + this.movedSet.add(n); + let targetId = null; + if (target && isINode(target)) { + targetId = target.__sn.id; + } + if (targetId) { + this.movedMap[moveKey(n.__sn.id, targetId)] = true; + } + } + else { + this.addedSet.add(n); + this.droppedSet.delete(n); + } + if (!isBlocked(n, this.blockClass, this.blockSelector, this.unblockSelector)) + n.childNodes.forEach((childN) => this.genAdds(childN)); + }; + } + init(options) { + [ + 'mutationCb', + 'blockClass', + 'blockSelector', + 'unblockSelector', + 'maskTextClass', + 'maskTextSelector', + 'unmaskTextSelector', + 'maskInputSelector', + 'unmaskInputSelector', + 'inlineStylesheet', + 'maskAllText', + 'maskInputOptions', + 'maskTextFn', + 'maskInputFn', + 'recordCanvas', + 'inlineImages', + 'slimDOMOptions', + 'doc', + 'mirror', + 'iframeManager', + 'shadowDomManager', + 'canvasManager', + ].forEach((key) => { + this[key] = options[key]; + }); + } + freeze() { + this.frozen = true; + this.canvasManager.freeze(); + } + unfreeze() { + this.frozen = false; + this.canvasManager.unfreeze(); + this.emit(); + } + isFrozen() { + return this.frozen; + } + lock() { + this.locked = true; + this.canvasManager.lock(); + } + unlock() { + this.locked = false; + this.canvasManager.unlock(); + this.emit(); + } + reset() { + this.shadowDomManager.reset(); + this.canvasManager.reset(); + } +} +function deepDelete(addsSet, n) { + addsSet.delete(n); + n.childNodes.forEach((childN) => deepDelete(addsSet, childN)); +} +function isParentRemoved(removes, n, mirror) { + const { parentNode } = n; + if (!parentNode) { + return false; + } + const parentId = mirror.getId(parentNode); + if (removes.some((r) => r.id === parentId)) { + return true; + } + return isParentRemoved(removes, parentNode, mirror); +} +function isAncestorInSet(set, n) { + const { parentNode } = n; + if (!parentNode) { + return false; + } + if (set.has(parentNode)) { + return true; + } + return isAncestorInSet(set, parentNode); +} + +const callbackWrapper = (cb) => { + const rrwebWrapped = (...rest) => { + try { + return cb(...rest); + } + catch (error) { + try { + error.__rrweb__ = true; + } + catch (_a) { + } + throw error; + } + }; + return rrwebWrapped; +}; + +const mutationBuffers = []; +function getEventTarget(event) { + try { + if ('composedPath' in event) { + const path = event.composedPath(); + if (path.length) { + return path[0]; + } + } + else if ('path' in event && event.path.length) { + return event.path[0]; + } + } + catch (_a) { } + return event && event.target; +} +function initMutationObserver(options, rootEl) { + var _a, _b; + const mutationBuffer = new MutationBuffer(); + mutationBuffers.push(mutationBuffer); + mutationBuffer.init(options); + let mutationObserverCtor = window.MutationObserver || + window.__rrMutationObserver; + const angularZoneSymbol = (_b = (_a = window === null || window === void 0 ? void 0 : window.Zone) === null || _a === void 0 ? void 0 : _a.__symbol__) === null || _b === void 0 ? void 0 : _b.call(_a, 'MutationObserver'); + if (angularZoneSymbol && + window[angularZoneSymbol]) { + mutationObserverCtor = window[angularZoneSymbol]; + } + const observer = new mutationObserverCtor(callbackWrapper((mutations) => { + if (options.onMutation && options.onMutation(mutations) === false) { + return; + } + mutationBuffer.processMutations(mutations); + })); + observer.observe(rootEl, { + attributes: true, + attributeOldValue: true, + characterData: true, + characterDataOldValue: true, + childList: true, + subtree: true, + }); + return observer; +} +function initMoveObserver({ mousemoveCb, sampling, doc, mirror, }) { + if (sampling.mousemove === false) { + return () => { }; + } + const threshold = typeof sampling.mousemove === 'number' ? sampling.mousemove : 50; + const callbackThreshold = typeof sampling.mousemoveCallback === 'number' + ? sampling.mousemoveCallback + : 500; + let positions = []; + let timeBaseline; + const wrappedCb = throttle$1((source) => { + const totalOffset = Date.now() - timeBaseline; + callbackWrapper(mousemoveCb)(positions.map((p) => { + p.timeOffset -= totalOffset; + return p; + }), source); + positions = []; + timeBaseline = null; + }, callbackThreshold); + const updatePosition = throttle$1((evt) => { + const target = getEventTarget(evt); + const { clientX, clientY } = isTouchEvent(evt) + ? evt.changedTouches[0] + : evt; + if (!timeBaseline) { + timeBaseline = Date.now(); + } + positions.push({ + x: clientX, + y: clientY, + id: mirror.getId(target), + timeOffset: Date.now() - timeBaseline, + }); + wrappedCb(typeof DragEvent !== 'undefined' && evt instanceof DragEvent + ? IncrementalSource.Drag + : evt instanceof MouseEvent + ? IncrementalSource.MouseMove + : IncrementalSource.TouchMove); + }, threshold, { + trailing: false, + }); + const handlers = [ + on('mousemove', callbackWrapper(updatePosition), doc), + on('touchmove', callbackWrapper(updatePosition), doc), + on('drag', callbackWrapper(updatePosition), doc), + ]; + return callbackWrapper(() => { + handlers.forEach((h) => h()); + }); +} +function initMouseInteractionObserver({ mouseInteractionCb, doc, mirror, blockClass, blockSelector, unblockSelector, sampling, }) { + if (sampling.mouseInteraction === false) { + return () => { }; + } + const disableMap = sampling.mouseInteraction === true || + sampling.mouseInteraction === undefined + ? {} + : sampling.mouseInteraction; + const handlers = []; + const getHandler = (eventKey) => { + return (event) => { + const target = getEventTarget(event); + if (isBlocked(target, blockClass, blockSelector, unblockSelector)) { + return; + } + const e = isTouchEvent(event) ? event.changedTouches[0] : event; + if (!e) { + return; + } + const id = mirror.getId(target); + const { clientX, clientY } = e; + callbackWrapper(mouseInteractionCb)({ + type: MouseInteractions[eventKey], + id, + x: clientX, + y: clientY, + }); + }; + }; + Object.keys(MouseInteractions) + .filter((key) => Number.isNaN(Number(key)) && + !key.endsWith('_Departed') && + disableMap[key] !== false) + .forEach((eventKey) => { + const eventName = eventKey.toLowerCase(); + const handler = callbackWrapper(getHandler(eventKey)); + handlers.push(on(eventName, handler, doc)); + }); + return callbackWrapper(() => { + handlers.forEach((h) => h()); + }); +} +function initScrollObserver({ scrollCb, doc, mirror, blockClass, blockSelector, unblockSelector, sampling, }) { + const updatePosition = throttle$1((evt) => { + const target = getEventTarget(evt); + if (!target || + isBlocked(target, blockClass, blockSelector, unblockSelector)) { + return; + } + const id = mirror.getId(target); + if (target === doc) { + const scrollEl = (doc.scrollingElement || doc.documentElement); + callbackWrapper(scrollCb)({ + id, + x: scrollEl.scrollLeft, + y: scrollEl.scrollTop, + }); + } + else { + callbackWrapper(scrollCb)({ + id, + x: target.scrollLeft, + y: target.scrollTop, + }); + } + }, sampling.scroll || 100); + return on('scroll', callbackWrapper(updatePosition), doc); +} +function initViewportResizeObserver({ viewportResizeCb, }) { + let lastH = -1; + let lastW = -1; + const updateDimension = throttle$1(() => { + const height = getWindowHeight(); + const width = getWindowWidth(); + if (lastH !== height || lastW !== width) { + callbackWrapper(viewportResizeCb)({ + width: Number(width), + height: Number(height), + }); + lastH = height; + lastW = width; + } + }, 200); + return on('resize', callbackWrapper(updateDimension), window); +} +function wrapEventWithUserTriggeredFlag(v, enable) { + const value = Object.assign({}, v); + if (!enable) + delete value.userTriggered; + return value; +} +const INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT']; +const lastInputValueMap = new WeakMap(); +function initInputObserver({ inputCb, doc, mirror, blockClass, blockSelector, unblockSelector, ignoreClass, ignoreSelector, maskInputSelector, unmaskInputSelector, maskInputOptions, maskInputFn, sampling, userTriggeredOnInput, }) { + function eventHandler(event) { + let target = getEventTarget(event); + const tagName = target && target.tagName; + const userTriggered = event.isTrusted; + if (tagName === 'OPTION') + target = target.parentElement; + if (!target || + !tagName || + INPUT_TAGS.indexOf(tagName) < 0 || + isBlocked(target, blockClass, blockSelector, unblockSelector)) { + return; + } + const el = target; + const type = getInputType(el); + if (el.classList.contains(ignoreClass) || + (ignoreSelector && el.matches(ignoreSelector))) { + return; + } + let text = getInputValue(el, tagName, type); + let isChecked = false; + if (type === 'radio' || type === 'checkbox') { + isChecked = target.checked; + } + if (hasInputMaskOptions({ + maskInputOptions, + maskInputSelector, + tagName, + type, + })) { + text = maskInputValue({ + input: el, + maskInputOptions, + maskInputSelector, + unmaskInputSelector, + tagName, + type, + value: text, + maskInputFn, + }); + } + cbWithDedup(target, callbackWrapper(wrapEventWithUserTriggeredFlag)({ text, isChecked, userTriggered }, userTriggeredOnInput)); + const name = target.name; + if (type === 'radio' && name && isChecked) { + doc + .querySelectorAll(`input[type="radio"][name="${name}"]`) + .forEach((el) => { + if (el !== target) { + const text = maskInputValue({ + input: el, + maskInputOptions, + maskInputSelector, + unmaskInputSelector, + tagName, + type, + value: getInputValue(el, tagName, type), + maskInputFn, + }); + cbWithDedup(el, callbackWrapper(wrapEventWithUserTriggeredFlag)({ + text, + isChecked: !isChecked, + userTriggered: false, + }, userTriggeredOnInput)); + } + }); + } + } + function cbWithDedup(target, v) { + const lastInputValue = lastInputValueMap.get(target); + if (!lastInputValue || + lastInputValue.text !== v.text || + lastInputValue.isChecked !== v.isChecked) { + lastInputValueMap.set(target, v); + const id = mirror.getId(target); + inputCb(Object.assign(Object.assign({}, v), { id })); + } + } + const events = sampling.input === 'last' ? ['change'] : ['input', 'change']; + const handlers = events.map((eventName) => on(eventName, callbackWrapper(eventHandler), doc)); + const propertyDescriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value'); + const hookProperties = [ + [HTMLInputElement.prototype, 'value'], + [HTMLInputElement.prototype, 'checked'], + [HTMLSelectElement.prototype, 'value'], + [HTMLTextAreaElement.prototype, 'value'], + [HTMLSelectElement.prototype, 'selectedIndex'], + [HTMLOptionElement.prototype, 'selected'], + ]; + if (propertyDescriptor && propertyDescriptor.set) { + handlers.push(...hookProperties.map((p) => hookSetter(p[0], p[1], { + set() { + callbackWrapper(eventHandler)({ target: this }); + }, + }))); + } + return callbackWrapper(() => { + handlers.forEach((h) => h()); + }); +} +function getNestedCSSRulePositions(rule) { + const positions = []; + function recurse(childRule, pos) { + if ((hasNestedCSSRule('CSSGroupingRule') && + childRule.parentRule instanceof CSSGroupingRule) || + (hasNestedCSSRule('CSSMediaRule') && + childRule.parentRule instanceof CSSMediaRule) || + (hasNestedCSSRule('CSSSupportsRule') && + childRule.parentRule instanceof CSSSupportsRule) || + (hasNestedCSSRule('CSSConditionRule') && + childRule.parentRule instanceof CSSConditionRule)) { + const rules = Array.from(childRule.parentRule.cssRules); + const index = rules.indexOf(childRule); + pos.unshift(index); + } + else { + const rules = Array.from(childRule.parentStyleSheet.cssRules); + const index = rules.indexOf(childRule); + pos.unshift(index); + } + return pos; + } + return recurse(rule, positions); +} +function initStyleSheetObserver({ styleSheetRuleCb, mirror }, { win }) { + if (!win.CSSStyleSheet || !win.CSSStyleSheet.prototype) { + return () => { }; + } + const insertRule = win.CSSStyleSheet.prototype.insertRule; + win.CSSStyleSheet.prototype.insertRule = new Proxy(insertRule, { + apply: callbackWrapper((target, thisArg, argumentsList) => { + const [rule, index] = argumentsList; + const id = mirror.getId(thisArg.ownerNode); + if (id !== -1) { + styleSheetRuleCb({ + id, + adds: [{ rule, index }], + }); + } + return target.apply(thisArg, argumentsList); + }), + }); + const deleteRule = win.CSSStyleSheet.prototype.deleteRule; + win.CSSStyleSheet.prototype.deleteRule = new Proxy(deleteRule, { + apply: callbackWrapper((target, thisArg, argumentsList) => { + const [index] = argumentsList; + const id = mirror.getId(thisArg.ownerNode); + if (id !== -1) { + styleSheetRuleCb({ + id, + removes: [{ index }], + }); + } + return target.apply(thisArg, argumentsList); + }), + }); + const supportedNestedCSSRuleTypes = {}; + if (canMonkeyPatchNestedCSSRule('CSSGroupingRule')) { + supportedNestedCSSRuleTypes.CSSGroupingRule = win.CSSGroupingRule; + } + else { + if (canMonkeyPatchNestedCSSRule('CSSMediaRule')) { + supportedNestedCSSRuleTypes.CSSMediaRule = win.CSSMediaRule; + } + if (canMonkeyPatchNestedCSSRule('CSSConditionRule')) { + supportedNestedCSSRuleTypes.CSSConditionRule = win.CSSConditionRule; + } + if (canMonkeyPatchNestedCSSRule('CSSSupportsRule')) { + supportedNestedCSSRuleTypes.CSSSupportsRule = win.CSSSupportsRule; + } + } + const unmodifiedFunctions = {}; + Object.entries(supportedNestedCSSRuleTypes).forEach(([typeKey, type]) => { + unmodifiedFunctions[typeKey] = { + insertRule: type.prototype.insertRule, + deleteRule: type.prototype.deleteRule, + }; + type.prototype.insertRule = new Proxy(unmodifiedFunctions[typeKey].insertRule, { + apply: callbackWrapper((target, thisArg, argumentsList) => { + const [rule, index] = argumentsList; + const id = mirror.getId(thisArg.parentStyleSheet.ownerNode); + if (id !== -1) { + styleSheetRuleCb({ + id, + adds: [ + { + rule, + index: [ + ...getNestedCSSRulePositions(thisArg), + index || 0, + ], + }, + ], + }); + } + return target.apply(thisArg, argumentsList); + }), + }); + type.prototype.deleteRule = new Proxy(unmodifiedFunctions[typeKey].deleteRule, { + apply: callbackWrapper((target, thisArg, argumentsList) => { + const [index] = argumentsList; + const id = mirror.getId(thisArg.parentStyleSheet.ownerNode); + if (id !== -1) { + styleSheetRuleCb({ + id, + removes: [ + { index: [...getNestedCSSRulePositions(thisArg), index] }, + ], + }); + } + return target.apply(thisArg, argumentsList); + }), + }); + }); + return callbackWrapper(() => { + win.CSSStyleSheet.prototype.insertRule = insertRule; + win.CSSStyleSheet.prototype.deleteRule = deleteRule; + Object.entries(supportedNestedCSSRuleTypes).forEach(([typeKey, type]) => { + type.prototype.insertRule = unmodifiedFunctions[typeKey].insertRule; + type.prototype.deleteRule = unmodifiedFunctions[typeKey].deleteRule; + }); + }); +} +function initStyleDeclarationObserver({ styleDeclarationCb, mirror }, { win }) { + const setProperty = win.CSSStyleDeclaration.prototype.setProperty; + win.CSSStyleDeclaration.prototype.setProperty = new Proxy(setProperty, { + apply: callbackWrapper((target, thisArg, argumentsList) => { + var _a, _b; + const [property, value, priority] = argumentsList; + const id = mirror.getId((_b = (_a = thisArg.parentRule) === null || _a === void 0 ? void 0 : _a.parentStyleSheet) === null || _b === void 0 ? void 0 : _b.ownerNode); + if (id !== -1) { + styleDeclarationCb({ + id, + set: { + property, + value, + priority, + }, + index: getNestedCSSRulePositions(thisArg.parentRule), + }); + } + return target.apply(thisArg, argumentsList); + }), + }); + const removeProperty = win.CSSStyleDeclaration.prototype.removeProperty; + win.CSSStyleDeclaration.prototype.removeProperty = new Proxy(removeProperty, { + apply: callbackWrapper((target, thisArg, argumentsList) => { + var _a, _b; + const [property] = argumentsList; + const id = mirror.getId((_b = (_a = thisArg.parentRule) === null || _a === void 0 ? void 0 : _a.parentStyleSheet) === null || _b === void 0 ? void 0 : _b.ownerNode); + if (id !== -1) { + styleDeclarationCb({ + id, + remove: { + property, + }, + index: getNestedCSSRulePositions(thisArg.parentRule), + }); + } + return target.apply(thisArg, argumentsList); + }), + }); + return callbackWrapper(() => { + win.CSSStyleDeclaration.prototype.setProperty = setProperty; + win.CSSStyleDeclaration.prototype.removeProperty = removeProperty; + }); +} +function initMediaInteractionObserver({ mediaInteractionCb, blockClass, blockSelector, unblockSelector, mirror, sampling, }) { + const handler = (type) => throttle$1(callbackWrapper((event) => { + const target = getEventTarget(event); + if (!target || + isBlocked(target, blockClass, blockSelector, unblockSelector)) { + return; + } + const { currentTime, volume, muted } = target; + mediaInteractionCb({ + type, + id: mirror.getId(target), + currentTime, + volume, + muted, + }); + }), sampling.media || 500); + const handlers = [ + on('play', handler(0)), + on('pause', handler(1)), + on('seeked', handler(2)), + on('volumechange', handler(3)), + ]; + return callbackWrapper(() => { + handlers.forEach((h) => h()); + }); +} +function initFontObserver({ fontCb, doc }) { + const win = doc.defaultView; + if (!win) { + return () => { }; + } + const handlers = []; + const fontMap = new WeakMap(); + const originalFontFace = win.FontFace; + win.FontFace = function FontFace(family, source, descriptors) { + const fontFace = new originalFontFace(family, source, descriptors); + fontMap.set(fontFace, { + family, + buffer: typeof source !== 'string', + descriptors, + fontSource: typeof source === 'string' + ? source + : + JSON.stringify(Array.from(new Uint8Array(source))), + }); + return fontFace; + }; + const restoreHandler = patch(doc.fonts, 'add', function (original) { + return function (fontFace) { + setTimeout(() => { + const p = fontMap.get(fontFace); + if (p) { + fontCb(p); + fontMap.delete(fontFace); + } + }, 0); + return original.apply(this, [fontFace]); + }; + }); + handlers.push(() => { + win.FontFace = originalFontFace; + }); + handlers.push(restoreHandler); + return callbackWrapper(() => { + handlers.forEach((h) => h()); + }); +} +function mergeHooks(o, hooks) { + const { mutationCb, mousemoveCb, mouseInteractionCb, scrollCb, viewportResizeCb, inputCb, mediaInteractionCb, styleSheetRuleCb, styleDeclarationCb, canvasMutationCb, fontCb, } = o; + o.mutationCb = (...p) => { + if (hooks.mutation) { + hooks.mutation(...p); + } + mutationCb(...p); + }; + o.mousemoveCb = (...p) => { + if (hooks.mousemove) { + hooks.mousemove(...p); + } + mousemoveCb(...p); + }; + o.mouseInteractionCb = (...p) => { + if (hooks.mouseInteraction) { + hooks.mouseInteraction(...p); + } + mouseInteractionCb(...p); + }; + o.scrollCb = (...p) => { + if (hooks.scroll) { + hooks.scroll(...p); + } + scrollCb(...p); + }; + o.viewportResizeCb = (...p) => { + if (hooks.viewportResize) { + hooks.viewportResize(...p); + } + viewportResizeCb(...p); + }; + o.inputCb = (...p) => { + if (hooks.input) { + hooks.input(...p); + } + inputCb(...p); + }; + o.mediaInteractionCb = (...p) => { + if (hooks.mediaInteaction) { + hooks.mediaInteaction(...p); + } + mediaInteractionCb(...p); + }; + o.styleSheetRuleCb = (...p) => { + if (hooks.styleSheetRule) { + hooks.styleSheetRule(...p); + } + styleSheetRuleCb(...p); + }; + o.styleDeclarationCb = (...p) => { + if (hooks.styleDeclaration) { + hooks.styleDeclaration(...p); + } + styleDeclarationCb(...p); + }; + o.canvasMutationCb = (...p) => { + if (hooks.canvasMutation) { + hooks.canvasMutation(...p); + } + canvasMutationCb(...p); + }; + o.fontCb = (...p) => { + if (hooks.font) { + hooks.font(...p); + } + fontCb(...p); + }; +} +function initObservers(o, hooks = {}) { + const currentWindow = o.doc.defaultView; + if (!currentWindow) { + return () => { }; + } + mergeHooks(o, hooks); + const mutationObserver = initMutationObserver(o, o.doc); + const mousemoveHandler = initMoveObserver(o); + const mouseInteractionHandler = initMouseInteractionObserver(o); + const scrollHandler = initScrollObserver(o); + const viewportResizeHandler = initViewportResizeObserver(o); + const inputHandler = initInputObserver(o); + const mediaInteractionHandler = initMediaInteractionObserver(o); + const styleSheetObserver = initStyleSheetObserver(o, { win: currentWindow }); + const styleDeclarationObserver = initStyleDeclarationObserver(o, { + win: currentWindow, + }); + const fontObserver = o.collectFonts ? initFontObserver(o) : () => { }; + const pluginHandlers = []; + for (const plugin of o.plugins) { + pluginHandlers.push(plugin.observer(plugin.callback, currentWindow, plugin.options)); + } + return callbackWrapper(() => { + mutationBuffers.forEach((b) => b.reset()); + mutationObserver.disconnect(); + mousemoveHandler(); + mouseInteractionHandler(); + scrollHandler(); + viewportResizeHandler(); + inputHandler(); + mediaInteractionHandler(); + try { + styleSheetObserver(); + styleDeclarationObserver(); + } + catch (e) { + } + fontObserver(); + pluginHandlers.forEach((h) => h()); + }); +} +function hasNestedCSSRule(prop) { + return typeof window[prop] !== 'undefined'; +} +function canMonkeyPatchNestedCSSRule(prop) { + return Boolean(typeof window[prop] !== 'undefined' && + window[prop].prototype && + 'insertRule' in window[prop].prototype && + 'deleteRule' in window[prop].prototype); +} + +class IframeManager { + constructor(options) { + this.iframes = new WeakMap(); + this.mutationCb = options.mutationCb; + } + addIframe(iframeEl) { + this.iframes.set(iframeEl, true); + } + addLoadListener(cb) { + this.loadListener = cb; + } + attachIframe(iframeEl, childSn) { + var _a; + this.mutationCb({ + adds: [ + { + parentId: iframeEl.__sn.id, + nextId: null, + node: childSn, + }, + ], + removes: [], + texts: [], + attributes: [], + isAttachIframe: true, + }); + (_a = this.loadListener) === null || _a === void 0 ? void 0 : _a.call(this, iframeEl); + } +} + +class ShadowDomManager { + constructor(options) { + this.restorePatches = []; + this.mutationCb = options.mutationCb; + this.scrollCb = options.scrollCb; + this.bypassOptions = options.bypassOptions; + this.mirror = options.mirror; + const manager = this; + this.restorePatches.push(patch(HTMLElement.prototype, 'attachShadow', function (original) { + return function () { + const shadowRoot = original.apply(this, arguments); + if (this.shadowRoot) + manager.addShadowRoot(this.shadowRoot, this.ownerDocument); + return shadowRoot; + }; + })); + } + addShadowRoot(shadowRoot, doc) { + initMutationObserver(Object.assign(Object.assign({}, this.bypassOptions), { doc, mutationCb: this.mutationCb, mirror: this.mirror, shadowDomManager: this }), shadowRoot); + initScrollObserver(Object.assign(Object.assign({}, this.bypassOptions), { scrollCb: this.scrollCb, doc: shadowRoot, mirror: this.mirror })); + } + observeAttachShadow(iframeElement) { + if (iframeElement.contentWindow) { + const manager = this; + this.restorePatches.push(patch(iframeElement.contentWindow.HTMLElement.prototype, 'attachShadow', function (original) { + return function () { + const shadowRoot = original.apply(this, arguments); + if (this.shadowRoot) + manager.addShadowRoot(this.shadowRoot, iframeElement.contentDocument); + return shadowRoot; + }; + })); + } + } + reset() { + this.restorePatches.forEach((restorePatch) => restorePatch()); + } +} + +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +} + +function initCanvas2DMutationObserver(cb, win, blockClass, unblockSelector, blockSelector, mirror) { + const handlers = []; + const props2D = Object.getOwnPropertyNames(win.CanvasRenderingContext2D.prototype); + for (const prop of props2D) { + try { + if (typeof win.CanvasRenderingContext2D.prototype[prop] !== 'function') { + continue; + } + const restoreHandler = patch(win.CanvasRenderingContext2D.prototype, prop, function (original) { + return function (...args) { + if (!isBlocked(this.canvas, blockClass, blockSelector, unblockSelector)) { + setTimeout(() => { + const recordArgs = [...args]; + if (prop === 'drawImage') { + if (recordArgs[0] && + recordArgs[0] instanceof HTMLCanvasElement) { + const canvas = recordArgs[0]; + const ctx = canvas.getContext('2d'); + let imgd = ctx === null || ctx === void 0 ? void 0 : ctx.getImageData(0, 0, canvas.width, canvas.height); + let pix = imgd === null || imgd === void 0 ? void 0 : imgd.data; + recordArgs[0] = JSON.stringify(pix); + } + } + cb(this.canvas, { + type: CanvasContext['2D'], + property: prop, + args: recordArgs, + }); + }, 0); + } + return original.apply(this, args); + }; + }); + handlers.push(restoreHandler); + } + catch (_a) { + const hookHandler = hookSetter(win.CanvasRenderingContext2D.prototype, prop, { + set(v) { + cb(this.canvas, { + type: CanvasContext['2D'], + property: prop, + args: [v], + setter: true, + }); + }, + }); + handlers.push(hookHandler); + } + } + return () => { + handlers.forEach((h) => h()); + }; +} + +function initCanvasContextObserver(win, blockClass, blockSelector, unblockSelector) { + const handlers = []; + try { + const restoreHandler = patch(win.HTMLCanvasElement.prototype, 'getContext', function (original) { + return function (contextType, ...args) { + if (!isBlocked(this, blockClass, blockSelector, unblockSelector)) { + if (!('__context' in this)) + this.__context = contextType; + } + return original.apply(this, [contextType, ...args]); + }; + }); + handlers.push(restoreHandler); + } + catch (_a) { + console.error('failed to patch HTMLCanvasElement.prototype.getContext'); + } + return () => { + handlers.forEach((h) => h()); + }; +} + +/* + * base64-arraybuffer 1.0.2 <https://github.com/niklasvh/base64-arraybuffer> + * Copyright (c) 2022 Niklas von Hertzen <https://hertzen.com> + * Released under MIT License + */ +var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; +// Use a lookup table to find the index. +var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256); +for (var i = 0; i < chars.length; i++) { + lookup[chars.charCodeAt(i)] = i; +} +var encode = function (arraybuffer) { + var bytes = new Uint8Array(arraybuffer), i, len = bytes.length, base64 = ''; + for (i = 0; i < len; i += 3) { + base64 += chars[bytes[i] >> 2]; + base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; + base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; + base64 += chars[bytes[i + 2] & 63]; + } + if (len % 3 === 2) { + base64 = base64.substring(0, base64.length - 1) + '='; + } + else if (len % 3 === 1) { + base64 = base64.substring(0, base64.length - 2) + '=='; + } + return base64; +}; + +const webGLVarMap = new Map(); +function variableListFor(ctx, ctor) { + let contextMap = webGLVarMap.get(ctx); + if (!contextMap) { + contextMap = new Map(); + webGLVarMap.set(ctx, contextMap); + } + if (!contextMap.has(ctor)) { + contextMap.set(ctor, []); + } + return contextMap.get(ctor); +} +const saveWebGLVar = (value, win, ctx) => { + if (!value || + !(isInstanceOfWebGLObject(value, win) || typeof value === 'object')) + return; + const name = value.constructor.name; + const list = variableListFor(ctx, name); + let index = list.indexOf(value); + if (index === -1) { + index = list.length; + list.push(value); + } + return index; +}; +function serializeArg(value, win, ctx) { + if (value instanceof Array) { + return value.map((arg) => serializeArg(arg, win, ctx)); + } + else if (value === null) { + return value; + } + else if (value instanceof Float32Array || + value instanceof Float64Array || + value instanceof Int32Array || + value instanceof Uint32Array || + value instanceof Uint8Array || + value instanceof Uint16Array || + value instanceof Int16Array || + value instanceof Int8Array || + value instanceof Uint8ClampedArray) { + const name = value.constructor.name; + return { + rr_type: name, + args: [Object.values(value)], + }; + } + else if (value instanceof ArrayBuffer) { + const name = value.constructor.name; + const base64 = encode(value); + return { + rr_type: name, + base64, + }; + } + else if (value instanceof DataView) { + const name = value.constructor.name; + return { + rr_type: name, + args: [ + serializeArg(value.buffer, win, ctx), + value.byteOffset, + value.byteLength, + ], + }; + } + else if (value instanceof HTMLImageElement) { + const name = value.constructor.name; + const { src } = value; + return { + rr_type: name, + src, + }; + } + else if (value instanceof ImageData) { + const name = value.constructor.name; + return { + rr_type: name, + args: [serializeArg(value.data, win, ctx), value.width, value.height], + }; + } + else if (isInstanceOfWebGLObject(value, win) || typeof value === 'object') { + const name = value.constructor.name; + const index = saveWebGLVar(value, win, ctx); + return { + rr_type: name, + index: index, + }; + } + return value; +} +const serializeArgs = (args, win, ctx) => { + return [...args].map((arg) => serializeArg(arg, win, ctx)); +}; +const isInstanceOfWebGLObject = (value, win) => { + const webGLConstructorNames = [ + 'WebGLActiveInfo', + 'WebGLBuffer', + 'WebGLFramebuffer', + 'WebGLProgram', + 'WebGLRenderbuffer', + 'WebGLShader', + 'WebGLShaderPrecisionFormat', + 'WebGLTexture', + 'WebGLUniformLocation', + 'WebGLVertexArrayObject', + 'WebGLVertexArrayObjectOES', + ]; + const supportedWebGLConstructorNames = webGLConstructorNames.filter((name) => typeof win[name] === 'function'); + return Boolean(supportedWebGLConstructorNames.find((name) => value instanceof win[name])); +}; + +function patchGLPrototype(prototype, type, cb, blockClass, unblockSelector, blockSelector, mirror, win) { + const handlers = []; + const props = Object.getOwnPropertyNames(prototype); + for (const prop of props) { + try { + if (typeof prototype[prop] !== 'function') { + continue; + } + const restoreHandler = patch(prototype, prop, function (original) { + return function (...args) { + const result = original.apply(this, args); + saveWebGLVar(result, win, prototype); + if (!isBlocked(this.canvas, blockClass, blockSelector, unblockSelector)) { + const id = mirror.getId(this.canvas); + const recordArgs = serializeArgs([...args], win, prototype); + const mutation = { + type, + property: prop, + args: recordArgs, + }; + cb(this.canvas, mutation); + } + return result; + }; + }); + handlers.push(restoreHandler); + } + catch (_a) { + const hookHandler = hookSetter(prototype, prop, { + set(v) { + cb(this.canvas, { + type, + property: prop, + args: [v], + setter: true, + }); + }, + }); + handlers.push(hookHandler); + } + } + return handlers; +} +function initCanvasWebGLMutationObserver(cb, win, blockClass, blockSelector, unblockSelector, mirror) { + const handlers = []; + handlers.push(...patchGLPrototype(win.WebGLRenderingContext.prototype, CanvasContext.WebGL, cb, blockClass, blockSelector, unblockSelector, mirror, win)); + if (typeof win.WebGL2RenderingContext !== 'undefined') { + handlers.push(...patchGLPrototype(win.WebGL2RenderingContext.prototype, CanvasContext.WebGL2, cb, blockClass, blockSelector, unblockSelector, mirror, win)); + } + return () => { + handlers.forEach((h) => h()); + }; +} + +class CanvasManager { + reset() { + this.pendingCanvasMutations.clear(); + this.resetObservers && this.resetObservers(); + } + freeze() { + this.frozen = true; + } + unfreeze() { + this.frozen = false; + } + lock() { + this.locked = true; + } + unlock() { + this.locked = false; + } + constructor(options) { + this.pendingCanvasMutations = new Map(); + this.rafStamps = { latestId: 0, invokeId: null }; + this.frozen = false; + this.locked = false; + this.processMutation = function (target, mutation) { + const newFrame = this.rafStamps.invokeId && + this.rafStamps.latestId !== this.rafStamps.invokeId; + if (newFrame || !this.rafStamps.invokeId) + this.rafStamps.invokeId = this.rafStamps.latestId; + if (!this.pendingCanvasMutations.has(target)) { + this.pendingCanvasMutations.set(target, []); + } + this.pendingCanvasMutations.get(target).push(mutation); + }; + this.mutationCb = options.mutationCb; + this.mirror = options.mirror; + if (options.recordCanvas === true) + this.initCanvasMutationObserver(options.win, options.blockClass, options.blockSelector, options.unblockSelector); + } + initCanvasMutationObserver(win, blockClass, unblockSelector, blockSelector) { + this.startRAFTimestamping(); + this.startPendingCanvasMutationFlusher(); + const canvasContextReset = initCanvasContextObserver(win, blockClass, blockSelector, unblockSelector); + const canvas2DReset = initCanvas2DMutationObserver(this.processMutation.bind(this), win, blockClass, blockSelector, unblockSelector, this.mirror); + const canvasWebGL1and2Reset = initCanvasWebGLMutationObserver(this.processMutation.bind(this), win, blockClass, blockSelector, unblockSelector, this.mirror); + this.resetObservers = () => { + canvasContextReset(); + canvas2DReset(); + canvasWebGL1and2Reset(); + }; + } + startPendingCanvasMutationFlusher() { + requestAnimationFrame(() => this.flushPendingCanvasMutations()); + } + startRAFTimestamping() { + const setLatestRAFTimestamp = (timestamp) => { + this.rafStamps.latestId = timestamp; + requestAnimationFrame(setLatestRAFTimestamp); + }; + requestAnimationFrame(setLatestRAFTimestamp); + } + flushPendingCanvasMutations() { + this.pendingCanvasMutations.forEach((values, canvas) => { + const id = this.mirror.getId(canvas); + this.flushPendingCanvasMutationFor(canvas, id); + }); + requestAnimationFrame(() => this.flushPendingCanvasMutations()); + } + flushPendingCanvasMutationFor(canvas, id) { + if (this.frozen || this.locked) { + return; + } + const valuesWithType = this.pendingCanvasMutations.get(canvas); + if (!valuesWithType || id === -1) + return; + const values = valuesWithType.map((value) => { + const rest = __rest(value, ["type"]); + return rest; + }); + const { type } = valuesWithType[0]; + this.mutationCb({ id, type, commands: values }); + this.pendingCanvasMutations.delete(canvas); + } +} + +function wrapEvent(e) { + return Object.assign(Object.assign({}, e), { timestamp: Date.now() }); +} +let wrappedEmit; +let takeFullSnapshot; +const mirror = createMirror(); +function record(options = {}) { + const { emit, checkoutEveryNms, checkoutEveryNth, blockClass = 'rr-block', blockSelector = null, unblockSelector = null, ignoreClass = 'rr-ignore', ignoreSelector = null, maskTextClass = 'rr-mask', maskTextSelector = null, maskInputSelector = null, unmaskTextSelector = null, unmaskInputSelector = null, inlineStylesheet = true, maskAllText = false, maskAllInputs, maskInputOptions: _maskInputOptions, slimDOMOptions: _slimDOMOptions, maskInputFn, maskTextFn, hooks, packFn, sampling = {}, mousemoveWait, recordCanvas = false, userTriggeredOnInput = false, collectFonts = false, inlineImages = false, plugins, keepIframeSrcFn = () => false, onMutation, } = options; + if (!emit) { + throw new Error('emit function is required'); + } + if (mousemoveWait !== undefined && sampling.mousemove === undefined) { + sampling.mousemove = mousemoveWait; + } + const maskInputOptions = maskAllInputs === true + ? { + color: true, + date: true, + 'datetime-local': true, + email: true, + month: true, + number: true, + range: true, + search: true, + tel: true, + text: true, + time: true, + url: true, + week: true, + textarea: true, + select: true, + radio: true, + checkbox: true, + } + : _maskInputOptions !== undefined + ? _maskInputOptions + : {}; + const slimDOMOptions = _slimDOMOptions === true || _slimDOMOptions === 'all' + ? { + script: true, + comment: true, + headFavicon: true, + headWhitespace: true, + headMetaSocial: true, + headMetaRobots: true, + headMetaHttpEquiv: true, + headMetaVerification: true, + headMetaAuthorship: _slimDOMOptions === 'all', + headMetaDescKeywords: _slimDOMOptions === 'all', + } + : _slimDOMOptions + ? _slimDOMOptions + : {}; + polyfill(); + let lastFullSnapshotEvent; + let incrementalSnapshotCount = 0; + const eventProcessor = (e) => { + for (const plugin of plugins || []) { + if (plugin.eventProcessor) { + e = plugin.eventProcessor(e); + } + } + if (packFn) { + e = packFn(e); + } + return e; + }; + wrappedEmit = (e, isCheckout) => { + var _a; + if (((_a = mutationBuffers[0]) === null || _a === void 0 ? void 0 : _a.isFrozen()) && + e.type !== EventType.FullSnapshot && + !(e.type === EventType.IncrementalSnapshot && + e.data.source === IncrementalSource.Mutation)) { + mutationBuffers.forEach((buf) => buf.unfreeze()); + } + emit(eventProcessor(e), isCheckout); + if (e.type === EventType.FullSnapshot) { + lastFullSnapshotEvent = e; + incrementalSnapshotCount = 0; + } + else if (e.type === EventType.IncrementalSnapshot) { + if (e.data.source === IncrementalSource.Mutation && + e.data.isAttachIframe) { + return; + } + incrementalSnapshotCount++; + const exceedCount = checkoutEveryNth && incrementalSnapshotCount >= checkoutEveryNth; + const exceedTime = checkoutEveryNms && + e.timestamp - lastFullSnapshotEvent.timestamp > checkoutEveryNms; + if (exceedCount || exceedTime) { + takeFullSnapshot(true); + } + } + }; + const wrappedMutationEmit = (m) => { + wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.Mutation }, m), + })); + }; + const wrappedScrollEmit = (p) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.Scroll }, p), + })); + const wrappedCanvasMutationEmit = (p) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.CanvasMutation }, p), + })); + const iframeManager = new IframeManager({ + mutationCb: wrappedMutationEmit, + }); + const canvasManager = new CanvasManager({ + recordCanvas, + mutationCb: wrappedCanvasMutationEmit, + win: window, + blockClass, + blockSelector, + unblockSelector, + mirror, + }); + const shadowDomManager = new ShadowDomManager({ + mutationCb: wrappedMutationEmit, + scrollCb: wrappedScrollEmit, + bypassOptions: { + onMutation, + blockClass, + blockSelector, + unblockSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + maskInputSelector, + unmaskInputSelector, + inlineStylesheet, + maskAllText, + maskInputOptions, + maskTextFn, + maskInputFn, + recordCanvas, + inlineImages, + sampling, + slimDOMOptions, + iframeManager, + canvasManager, + }, + mirror, + }); + takeFullSnapshot = (isCheckout = false) => { + var _a, _b, _c, _d; + wrappedEmit(wrapEvent({ + type: EventType.Meta, + data: { + href: window.location.href, + width: getWindowWidth(), + height: getWindowHeight(), + }, + }), isCheckout); + mutationBuffers.forEach((buf) => buf.lock()); + const [node, idNodeMap] = snapshot(document, { + blockClass, + blockSelector, + unblockSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + maskInputSelector, + unmaskInputSelector, + inlineStylesheet, + maskAllText, + maskAllInputs: maskInputOptions, + maskTextFn, + slimDOM: slimDOMOptions, + recordCanvas, + inlineImages, + onSerialize: (n) => { + if (isIframeINode(n)) { + iframeManager.addIframe(n); + } + if (hasShadowRoot(n)) { + shadowDomManager.addShadowRoot(n.shadowRoot, document); + } + }, + onIframeLoad: (iframe, childSn) => { + iframeManager.attachIframe(iframe, childSn); + shadowDomManager.observeAttachShadow(iframe); + }, + keepIframeSrcFn, + }); + if (!node) { + return console.warn('Failed to snapshot the document'); + } + mirror.map = idNodeMap; + wrappedEmit(wrapEvent({ + type: EventType.FullSnapshot, + data: { + node, + initialOffset: { + left: window.pageXOffset !== undefined + ? window.pageXOffset + : (document === null || document === void 0 ? void 0 : document.documentElement.scrollLeft) || + ((_b = (_a = document === null || document === void 0 ? void 0 : document.body) === null || _a === void 0 ? void 0 : _a.parentElement) === null || _b === void 0 ? void 0 : _b.scrollLeft) || + (document === null || document === void 0 ? void 0 : document.body.scrollLeft) || + 0, + top: window.pageYOffset !== undefined + ? window.pageYOffset + : (document === null || document === void 0 ? void 0 : document.documentElement.scrollTop) || + ((_d = (_c = document === null || document === void 0 ? void 0 : document.body) === null || _c === void 0 ? void 0 : _c.parentElement) === null || _d === void 0 ? void 0 : _d.scrollTop) || + (document === null || document === void 0 ? void 0 : document.body.scrollTop) || + 0, + }, + }, + })); + mutationBuffers.forEach((buf) => buf.unlock()); + }; + try { + const handlers = []; + handlers.push(on('DOMContentLoaded', () => { + wrappedEmit(wrapEvent({ + type: EventType.DomContentLoaded, + data: {}, + })); + })); + const observe = (doc) => { + var _a; + return callbackWrapper(initObservers)({ + onMutation, + mutationCb: wrappedMutationEmit, + mousemoveCb: (positions, source) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: { + source, + positions, + }, + })), + mouseInteractionCb: (d) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.MouseInteraction }, d), + })), + scrollCb: wrappedScrollEmit, + viewportResizeCb: (d) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.ViewportResize }, d), + })), + inputCb: (v) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.Input }, v), + })), + mediaInteractionCb: (p) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.MediaInteraction }, p), + })), + styleSheetRuleCb: (r) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.StyleSheetRule }, r), + })), + styleDeclarationCb: (r) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.StyleDeclaration }, r), + })), + canvasMutationCb: wrappedCanvasMutationEmit, + fontCb: (p) => wrappedEmit(wrapEvent({ + type: EventType.IncrementalSnapshot, + data: Object.assign({ source: IncrementalSource.Font }, p), + })), + blockClass, + ignoreClass, + ignoreSelector, + maskTextClass, + maskTextSelector, + unmaskTextSelector, + maskInputSelector, + unmaskInputSelector, + maskInputOptions, + inlineStylesheet, + sampling, + recordCanvas, + inlineImages, + userTriggeredOnInput, + collectFonts, + doc, + maskAllText, + maskInputFn, + maskTextFn, + blockSelector, + unblockSelector, + slimDOMOptions, + mirror, + iframeManager, + shadowDomManager, + canvasManager, + plugins: ((_a = plugins === null || plugins === void 0 ? void 0 : plugins.filter((p) => p.observer)) === null || _a === void 0 ? void 0 : _a.map((p) => ({ + observer: p.observer, + options: p.options, + callback: (payload) => wrappedEmit(wrapEvent({ + type: EventType.Plugin, + data: { + plugin: p.name, + payload, + }, + })), + }))) || [], + }, hooks); + }; + iframeManager.addLoadListener((iframeEl) => { + try { + handlers.push(observe(iframeEl.contentDocument)); + } + catch (error) { + console.warn(error); + } + }); + const init = () => { + takeFullSnapshot(); + handlers.push(observe(document)); + }; + if (document.readyState === 'interactive' || + document.readyState === 'complete') { + init(); + } + else { + handlers.push(on('load', () => { + wrappedEmit(wrapEvent({ + type: EventType.Load, + data: {}, + })); + init(); + }, window)); + } + return () => { + handlers.forEach((h) => h()); + }; + } + catch (error) { + console.warn(error); + } +} +record.addCustomEvent = (tag, payload) => { + if (!wrappedEmit) { + throw new Error('please add custom event after start recording'); + } + wrappedEmit(wrapEvent({ + type: EventType.Custom, + data: { + tag, + payload, + }, + })); +}; +record.freezePage = () => { + mutationBuffers.forEach((buf) => buf.freeze()); +}; +record.takeFullSnapshot = (isCheckout) => { + if (!takeFullSnapshot) { + throw new Error('please take full snapshot after start recording'); + } + takeFullSnapshot(isCheckout); +}; +record.mirror = mirror; + +/** + * Add a breadcrumb event to replay. + */ +function addBreadcrumbEvent(replay, breadcrumb) { + if (breadcrumb.category === 'sentry.transaction') { + return; + } + + if (['ui.click', 'ui.input'].includes(breadcrumb.category )) { + replay.triggerUserActivity(); + } else { + replay.checkAndHandleExpiredSession(); + } + + replay.addUpdate(() => { + void replay.throttledAddEvent({ + type: EventType.Custom, + // TODO: We were converting from ms to seconds for breadcrumbs, spans, + // but maybe we should just keep them as milliseconds + timestamp: (breadcrumb.timestamp || 0) * 1000, + data: { + tag: 'breadcrumb', + // normalize to max. 10 depth and 1_000 properties per object + payload: normalize(breadcrumb, 10, 1000), + }, + }); + + // Do not flush after console log messages + return breadcrumb.category === 'console'; + }); +} + +const INTERACTIVE_SELECTOR = 'button,a'; + +/** + * For clicks, we check if the target is inside of a button or link + * If so, we use this as the target instead + * This is useful because if you click on the image in <button><img></button>, + * The target will be the image, not the button, which we don't want here + */ +function getClickTargetNode(event) { + const target = getTargetNode(event); + + if (!target || !(target instanceof Element)) { + return target; + } + + const closestInteractive = target.closest(INTERACTIVE_SELECTOR); + return closestInteractive || target; +} + +/** Get the event target node. */ +function getTargetNode(event) { + if (isEventWithTarget(event)) { + return event.target ; + } + + return event; +} + +function isEventWithTarget(event) { + return typeof event === 'object' && !!event && 'target' in event; +} + +let handlers; + +/** + * Register a handler to be called when `window.open()` is called. + * Returns a cleanup function. + */ +function onWindowOpen(cb) { + // Ensure to only register this once + if (!handlers) { + handlers = []; + monkeyPatchWindowOpen(); + } + + handlers.push(cb); + + return () => { + const pos = handlers ? handlers.indexOf(cb) : -1; + if (pos > -1) { + (handlers ).splice(pos, 1); + } + }; +} + +function monkeyPatchWindowOpen() { + fill(WINDOW, 'open', function (originalWindowOpen) { + return function (...args) { + if (handlers) { + try { + handlers.forEach(handler => handler()); + } catch (e) { + // ignore errors in here + } + } + + return originalWindowOpen.apply(WINDOW, args); + }; + }); +} + +/** Handle a click. */ +function handleClick(clickDetector, clickBreadcrumb, node) { + clickDetector.handleClick(clickBreadcrumb, node); +} + +/** A click detector class that can be used to detect slow or rage clicks on elements. */ +class ClickDetector { + // protected for testing + __init() {this._lastMutation = 0;} + __init2() {this._lastScroll = 0;} + + __init3() {this._clicks = [];} + + constructor( + replay, + slowClickConfig, + // Just for easier testing + _addBreadcrumbEvent = addBreadcrumbEvent, + ) {ClickDetector.prototype.__init.call(this);ClickDetector.prototype.__init2.call(this);ClickDetector.prototype.__init3.call(this); + // We want everything in s, but options are in ms + this._timeout = slowClickConfig.timeout / 1000; + this._multiClickTimeout = slowClickConfig.multiClickTimeout / 1000; + this._threshold = slowClickConfig.threshold / 1000; + this._scollTimeout = slowClickConfig.scrollTimeout / 1000; + this._replay = replay; + this._ignoreSelector = slowClickConfig.ignoreSelector; + this._addBreadcrumbEvent = _addBreadcrumbEvent; + } + + /** Register click detection handlers on mutation or scroll. */ + addListeners() { + const mutationHandler = () => { + this._lastMutation = nowInSeconds(); + }; + + const scrollHandler = () => { + this._lastScroll = nowInSeconds(); + }; + + const cleanupWindowOpen = onWindowOpen(() => { + // Treat window.open as mutation + this._lastMutation = nowInSeconds(); + }); + + const clickHandler = (event) => { + if (!event.target) { + return; + } + + const node = getClickTargetNode(event); + if (node) { + this._handleMultiClick(node ); + } + }; + + const obs = new MutationObserver(mutationHandler); + + obs.observe(WINDOW.document.documentElement, { + attributes: true, + characterData: true, + childList: true, + subtree: true, + }); + + WINDOW.addEventListener('scroll', scrollHandler, { passive: true }); + WINDOW.addEventListener('click', clickHandler, { passive: true }); + + this._teardown = () => { + WINDOW.removeEventListener('scroll', scrollHandler); + WINDOW.removeEventListener('click', clickHandler); + cleanupWindowOpen(); + + obs.disconnect(); + this._clicks = []; + this._lastMutation = 0; + this._lastScroll = 0; + }; + } + + /** Clean up listeners. */ + removeListeners() { + if (this._teardown) { + this._teardown(); + } + + if (this._checkClickTimeout) { + clearTimeout(this._checkClickTimeout); + } + } + + /** Handle a click */ + handleClick(breadcrumb, node) { + if (ignoreElement(node, this._ignoreSelector) || !isClickBreadcrumb(breadcrumb)) { + return; + } + + const click = this._getClick(node); + + if (click) { + // this means a click on the same element was captured in the last 1s, so we consider this a multi click + return; + } + + const newClick = { + timestamp: breadcrumb.timestamp, + clickBreadcrumb: breadcrumb, + // Set this to 0 so we know it originates from the click breadcrumb + clickCount: 0, + node, + }; + this._clicks.push(newClick); + + // If this is the first new click, set a timeout to check for multi clicks + if (this._clicks.length === 1) { + this._scheduleCheckClicks(); + } + } + + /** Count multiple clicks on elements. */ + _handleMultiClick(node) { + const click = this._getClick(node); + + if (!click) { + return; + } + + click.clickCount++; + } + + /** Try to get an existing click on the given element. */ + _getClick(node) { + const now = nowInSeconds(); + + // Find any click on the same element in the last second + // If one exists, we consider this click as a double/triple/etc click + return this._clicks.find(click => click.node === node && now - click.timestamp < this._multiClickTimeout); + } + + /** Check the clicks that happened. */ + _checkClicks() { + const timedOutClicks = []; + + const now = nowInSeconds(); + + this._clicks.forEach(click => { + if (!click.mutationAfter && this._lastMutation) { + click.mutationAfter = click.timestamp <= this._lastMutation ? this._lastMutation - click.timestamp : undefined; + } + if (!click.scrollAfter && this._lastScroll) { + click.scrollAfter = click.timestamp <= this._lastScroll ? this._lastScroll - click.timestamp : undefined; + } + + // If an action happens after the multi click threshold, we can skip waiting and handle the click right away + const actionTime = click.scrollAfter || click.mutationAfter || 0; + if (actionTime && actionTime >= this._multiClickTimeout) { + timedOutClicks.push(click); + return; + } + + if (click.timestamp + this._timeout <= now) { + timedOutClicks.push(click); + } + }); + + // Remove "old" clicks + for (const click of timedOutClicks) { + this._generateBreadcrumbs(click); + + const pos = this._clicks.indexOf(click); + if (pos !== -1) { + this._clicks.splice(pos, 1); + } + } + + // Trigger new check, unless no clicks left + if (this._clicks.length) { + this._scheduleCheckClicks(); + } + } + + /** Generate matching breadcrumb(s) for the click. */ + _generateBreadcrumbs(click) { + const replay = this._replay; + const hadScroll = click.scrollAfter && click.scrollAfter <= this._scollTimeout; + const hadMutation = click.mutationAfter && click.mutationAfter <= this._threshold; + + const isSlowClick = !hadScroll && !hadMutation; + const { clickCount, clickBreadcrumb } = click; + + // Slow click + if (isSlowClick) { + // If `mutationAfter` is set, it means a mutation happened after the threshold, but before the timeout + // If not, it means we just timed out without scroll & mutation + const timeAfterClickMs = Math.min(click.mutationAfter || this._timeout, this._timeout) * 1000; + const endReason = timeAfterClickMs < this._timeout * 1000 ? 'mutation' : 'timeout'; + + const breadcrumb = { + type: 'default', + message: clickBreadcrumb.message, + timestamp: clickBreadcrumb.timestamp, + category: 'ui.slowClickDetected', + data: { + ...clickBreadcrumb.data, + url: WINDOW.location.href, + route: replay.getCurrentRoute(), + timeAfterClickMs, + endReason, + // If clickCount === 0, it means multiClick was not correctly captured here + // - we still want to send 1 in this case + clickCount: clickCount || 1, + }, + }; + + this._addBreadcrumbEvent(replay, breadcrumb); + return; + } + + // Multi click + if (clickCount > 1) { + const breadcrumb = { + type: 'default', + message: clickBreadcrumb.message, + timestamp: clickBreadcrumb.timestamp, + category: 'ui.multiClick', + data: { + ...clickBreadcrumb.data, + url: WINDOW.location.href, + route: replay.getCurrentRoute(), + clickCount, + metric: true, + }, + }; + + this._addBreadcrumbEvent(replay, breadcrumb); + } + } + + /** Schedule to check current clicks. */ + _scheduleCheckClicks() { + this._checkClickTimeout = setTimeout(() => this._checkClicks(), 1000); + } +} + +const SLOW_CLICK_TAGS = ['A', 'BUTTON', 'INPUT']; + +/** exported for tests only */ +function ignoreElement(node, ignoreSelector) { + if (!SLOW_CLICK_TAGS.includes(node.tagName)) { + return true; + } + + // If <input> tag, we only want to consider input[type='submit'] & input[type='button'] + if (node.tagName === 'INPUT' && !['submit', 'button'].includes(node.getAttribute('type') || '')) { + return true; + } + + // If <a> tag, detect special variants that may not lead to an action + // If target !== _self, we may open the link somewhere else, which would lead to no action + // Also, when downloading a file, we may not leave the page, but still not trigger an action + if ( + node.tagName === 'A' && + (node.hasAttribute('download') || (node.hasAttribute('target') && node.getAttribute('target') !== '_self')) + ) { + return true; + } + + if (ignoreSelector && node.matches(ignoreSelector)) { + return true; + } + + return false; +} + +function isClickBreadcrumb(breadcrumb) { + return !!(breadcrumb.data && typeof breadcrumb.data.nodeId === 'number' && breadcrumb.timestamp); +} + +// This is good enough for us, and is easier to test/mock than `timestampInSeconds` +function nowInSeconds() { + return Date.now() / 1000; +} + +/** + * Create a breadcrumb for a replay. + */ +function createBreadcrumb( + breadcrumb, +) { + return { + timestamp: Date.now() / 1000, + type: 'default', + ...breadcrumb, + }; +} + +var NodeType; +(function (NodeType) { + NodeType[NodeType["Document"] = 0] = "Document"; + NodeType[NodeType["DocumentType"] = 1] = "DocumentType"; + NodeType[NodeType["Element"] = 2] = "Element"; + NodeType[NodeType["Text"] = 3] = "Text"; + NodeType[NodeType["CDATA"] = 4] = "CDATA"; + NodeType[NodeType["Comment"] = 5] = "Comment"; +})(NodeType || (NodeType = {})); + +// Note that these are the serialized attributes and not attributes directly on +// the DOM Node. Attributes we are interested in: +const ATTRIBUTES_TO_RECORD = new Set([ + 'id', + 'class', + 'aria-label', + 'role', + 'name', + 'alt', + 'title', + 'data-test-id', + 'data-testid', + 'disabled', + 'aria-disabled', +]); + +/** + * Inclusion list of attributes that we want to record from the DOM element + */ +function getAttributesToRecord(attributes) { + const obj = {}; + for (const key in attributes) { + if (ATTRIBUTES_TO_RECORD.has(key)) { + let normalizedKey = key; + + if (key === 'data-testid' || key === 'data-test-id') { + normalizedKey = 'testId'; + } + + obj[normalizedKey] = attributes[key]; + } + } + + return obj; +} + +const handleDomListener = ( + replay, +) => { + return (handlerData) => { + if (!replay.isEnabled()) { + return; + } + + const result = handleDom(handlerData); + + if (!result) { + return; + } + + const isClick = handlerData.name === 'click'; + const event = isClick && (handlerData.event ); + // Ignore clicks if ctrl/alt/meta keys are held down as they alter behavior of clicks (e.g. open in new tab) + if (isClick && replay.clickDetector && event && !event.altKey && !event.metaKey && !event.ctrlKey) { + handleClick( + replay.clickDetector, + result , + getClickTargetNode(handlerData.event) , + ); + } + + addBreadcrumbEvent(replay, result); + }; +}; + +/** Get the base DOM breadcrumb. */ +function getBaseDomBreadcrumb(target, message) { + // `__sn` property is the serialized node created by rrweb + const serializedNode = target && isRrwebNode(target) && target.__sn.type === NodeType.Element ? target.__sn : null; + + return { + message, + data: serializedNode + ? { + nodeId: serializedNode.id, + node: { + id: serializedNode.id, + tagName: serializedNode.tagName, + textContent: target + ? Array.from(target.childNodes) + .map( + (node) => '__sn' in node && node.__sn.type === NodeType.Text && node.__sn.textContent, + ) + .filter(Boolean) // filter out empty values + .map(text => (text ).trim()) + .join('') + : '', + attributes: getAttributesToRecord(serializedNode.attributes), + }, + } + : {}, + }; +} + +/** + * An event handler to react to DOM events. + * Exported for tests. + */ +function handleDom(handlerData) { + const { target, message } = getDomTarget(handlerData); + + return createBreadcrumb({ + category: `ui.${handlerData.name}`, + ...getBaseDomBreadcrumb(target, message), + }); +} + +function getDomTarget(handlerData) { + const isClick = handlerData.name === 'click'; + + let message; + let target = null; + + // Accessing event.target can throw (see getsentry/raven-js#838, #768) + try { + target = isClick ? getClickTargetNode(handlerData.event) : getTargetNode(handlerData.event); + message = htmlTreeAsString(target, { maxStringLength: 200 }) || '<unknown>'; + } catch (e) { + message = '<unknown>'; + } + + return { target, message }; +} + +function isRrwebNode(node) { + return '__sn' in node; +} + +/** Handle keyboard events & create breadcrumbs. */ +function handleKeyboardEvent(replay, event) { + if (!replay.isEnabled()) { + return; + } + + // Update user activity, but do not restart recording as it can create + // noisy/low-value replays (e.g. user comes back from idle, hits alt-tab, new + // session with a single "keydown" breadcrumb is created) + replay.updateUserActivity(); + + const breadcrumb = getKeyboardBreadcrumb(event); + + if (!breadcrumb) { + return; + } + + addBreadcrumbEvent(replay, breadcrumb); +} + +/** exported only for tests */ +function getKeyboardBreadcrumb(event) { + const { metaKey, shiftKey, ctrlKey, altKey, key, target } = event; + + // never capture for input fields + if (!target || isInputElement(target ) || !key) { + return null; + } + + // Note: We do not consider shift here, as that means "uppercase" + const hasModifierKey = metaKey || ctrlKey || altKey; + const isCharacterKey = key.length === 1; // other keys like Escape, Tab, etc have a longer length + + // Do not capture breadcrumb if only a word key is pressed + // This could leak e.g. user input + if (!hasModifierKey && isCharacterKey) { + return null; + } + + const message = htmlTreeAsString(target, { maxStringLength: 200 }) || '<unknown>'; + const baseBreadcrumb = getBaseDomBreadcrumb(target , message); + + return createBreadcrumb({ + category: 'ui.keyDown', + message, + data: { + ...baseBreadcrumb.data, + metaKey, + shiftKey, + ctrlKey, + altKey, + key, + }, + }); +} + +function isInputElement(target) { + return target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable; +} + +const NAVIGATION_ENTRY_KEYS = [ + 'name', + 'type', + 'startTime', + 'transferSize', + 'duration', +]; + +function isNavigationEntryEqual(a) { + return function (b) { + return NAVIGATION_ENTRY_KEYS.every(key => a[key] === b[key]); + }; +} + +/** + * There are some difficulties diagnosing why there are duplicate navigation + * entries. We've witnessed several intermittent results: + * - duplicate entries have duration = 0 + * - duplicate entries are the same object reference + * - none of the above + * + * Compare the values of several keys to determine if the entries are duplicates or not. + */ +// TODO (high-prio): Figure out wth is returned here +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type +function dedupePerformanceEntries( + currentList, + newList, +) { + // Partition `currentList` into 3 different lists based on entryType + const [existingNavigationEntries, existingLcpEntries, existingEntries] = currentList.reduce( + (acc, entry) => { + if (entry.entryType === 'navigation') { + acc[0].push(entry ); + } else if (entry.entryType === 'largest-contentful-paint') { + acc[1].push(entry ); + } else { + acc[2].push(entry); + } + return acc; + }, + [[], [], []], + ); + + const newEntries = []; + const newNavigationEntries = []; + let newLcpEntry = existingLcpEntries.length + ? existingLcpEntries[existingLcpEntries.length - 1] // Take the last element as list is sorted + : undefined; + + newList.forEach(entry => { + if (entry.entryType === 'largest-contentful-paint') { + // We want the latest LCP event only + if (!newLcpEntry || newLcpEntry.startTime < entry.startTime) { + newLcpEntry = entry; + } + return; + } + + if (entry.entryType === 'navigation') { + const navigationEntry = entry ; + + // Check if the navigation entry is contained in currentList or newList + if ( + // Ignore any navigation entries with duration 0, as they are likely duplicates + entry.duration > 0 && + // Ensure new entry does not already exist in existing entries + !existingNavigationEntries.find(isNavigationEntryEqual(navigationEntry)) && + // Ensure new entry does not already exist in new list of navigation entries + !newNavigationEntries.find(isNavigationEntryEqual(navigationEntry)) + ) { + newNavigationEntries.push(navigationEntry); + } + + // Otherwise this navigation entry is considered a duplicate and is thrown away + return; + } + + newEntries.push(entry); + }); + + // Re-combine and sort by startTime + return [ + ...(newLcpEntry ? [newLcpEntry] : []), + ...existingNavigationEntries, + ...existingEntries, + ...newEntries, + ...newNavigationEntries, + ].sort((a, b) => a.startTime - b.startTime); +} + +/** + * Sets up a PerformanceObserver to listen to all performance entry types. + */ +function setupPerformanceObserver(replay) { + const performanceObserverHandler = (list) => { + // For whatever reason the observer was returning duplicate navigation + // entries (the other entry types were not duplicated). + const newPerformanceEntries = dedupePerformanceEntries( + replay.performanceEvents, + list.getEntries() , + ); + replay.performanceEvents = newPerformanceEntries; + }; + + const performanceObserver = new PerformanceObserver(performanceObserverHandler); + + [ + 'element', + 'event', + 'first-input', + 'largest-contentful-paint', + 'layout-shift', + 'longtask', + 'navigation', + 'paint', + 'resource', + ].forEach(type => { + try { + performanceObserver.observe({ + type, + buffered: true, + }); + } catch (e) { + // This can throw if an entry type is not supported in the browser. + // Ignore these errors. + } + }); + + return performanceObserver; +} + +const r = `/*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */ +function t(t){let e=t.length;for(;--e>=0;)t[e]=0}const e=new Uint8Array([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0]),a=new Uint8Array([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13]),i=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7]),n=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),s=new Array(576);t(s);const r=new Array(60);t(r);const o=new Array(512);t(o);const l=new Array(256);t(l);const h=new Array(29);t(h);const d=new Array(30);function _(t,e,a,i,n){this.static_tree=t,this.extra_bits=e,this.extra_base=a,this.elems=i,this.max_length=n,this.has_stree=t&&t.length}let f,c,u;function w(t,e){this.dyn_tree=t,this.max_code=0,this.stat_desc=e}t(d);const m=t=>t<256?o[t]:o[256+(t>>>7)],b=(t,e)=>{t.pending_buf[t.pending++]=255&e,t.pending_buf[t.pending++]=e>>>8&255},g=(t,e,a)=>{t.bi_valid>16-a?(t.bi_buf|=e<<t.bi_valid&65535,b(t,t.bi_buf),t.bi_buf=e>>16-t.bi_valid,t.bi_valid+=a-16):(t.bi_buf|=e<<t.bi_valid&65535,t.bi_valid+=a)},p=(t,e,a)=>{g(t,a[2*e],a[2*e+1])},k=(t,e)=>{let a=0;do{a|=1&t,t>>>=1,a<<=1}while(--e>0);return a>>>1},v=(t,e,a)=>{const i=new Array(16);let n,s,r=0;for(n=1;n<=15;n++)r=r+a[n-1]<<1,i[n]=r;for(s=0;s<=e;s++){let e=t[2*s+1];0!==e&&(t[2*s]=k(i[e]++,e))}},y=t=>{let e;for(e=0;e<286;e++)t.dyn_ltree[2*e]=0;for(e=0;e<30;e++)t.dyn_dtree[2*e]=0;for(e=0;e<19;e++)t.bl_tree[2*e]=0;t.dyn_ltree[512]=1,t.opt_len=t.static_len=0,t.sym_next=t.matches=0},x=t=>{t.bi_valid>8?b(t,t.bi_buf):t.bi_valid>0&&(t.pending_buf[t.pending++]=t.bi_buf),t.bi_buf=0,t.bi_valid=0},z=(t,e,a,i)=>{const n=2*e,s=2*a;return t[n]<t[s]||t[n]===t[s]&&i[e]<=i[a]},A=(t,e,a)=>{const i=t.heap[a];let n=a<<1;for(;n<=t.heap_len&&(n<t.heap_len&&z(e,t.heap[n+1],t.heap[n],t.depth)&&n++,!z(e,i,t.heap[n],t.depth));)t.heap[a]=t.heap[n],a=n,n<<=1;t.heap[a]=i},E=(t,i,n)=>{let s,r,o,_,f=0;if(0!==t.sym_next)do{s=255&t.pending_buf[t.sym_buf+f++],s+=(255&t.pending_buf[t.sym_buf+f++])<<8,r=t.pending_buf[t.sym_buf+f++],0===s?p(t,r,i):(o=l[r],p(t,o+256+1,i),_=e[o],0!==_&&(r-=h[o],g(t,r,_)),s--,o=m(s),p(t,o,n),_=a[o],0!==_&&(s-=d[o],g(t,s,_)))}while(f<t.sym_next);p(t,256,i)},R=(t,e)=>{const a=e.dyn_tree,i=e.stat_desc.static_tree,n=e.stat_desc.has_stree,s=e.stat_desc.elems;let r,o,l,h=-1;for(t.heap_len=0,t.heap_max=573,r=0;r<s;r++)0!==a[2*r]?(t.heap[++t.heap_len]=h=r,t.depth[r]=0):a[2*r+1]=0;for(;t.heap_len<2;)l=t.heap[++t.heap_len]=h<2?++h:0,a[2*l]=1,t.depth[l]=0,t.opt_len--,n&&(t.static_len-=i[2*l+1]);for(e.max_code=h,r=t.heap_len>>1;r>=1;r--)A(t,a,r);l=s;do{r=t.heap[1],t.heap[1]=t.heap[t.heap_len--],A(t,a,1),o=t.heap[1],t.heap[--t.heap_max]=r,t.heap[--t.heap_max]=o,a[2*l]=a[2*r]+a[2*o],t.depth[l]=(t.depth[r]>=t.depth[o]?t.depth[r]:t.depth[o])+1,a[2*r+1]=a[2*o+1]=l,t.heap[1]=l++,A(t,a,1)}while(t.heap_len>=2);t.heap[--t.heap_max]=t.heap[1],((t,e)=>{const a=e.dyn_tree,i=e.max_code,n=e.stat_desc.static_tree,s=e.stat_desc.has_stree,r=e.stat_desc.extra_bits,o=e.stat_desc.extra_base,l=e.stat_desc.max_length;let h,d,_,f,c,u,w=0;for(f=0;f<=15;f++)t.bl_count[f]=0;for(a[2*t.heap[t.heap_max]+1]=0,h=t.heap_max+1;h<573;h++)d=t.heap[h],f=a[2*a[2*d+1]+1]+1,f>l&&(f=l,w++),a[2*d+1]=f,d>i||(t.bl_count[f]++,c=0,d>=o&&(c=r[d-o]),u=a[2*d],t.opt_len+=u*(f+c),s&&(t.static_len+=u*(n[2*d+1]+c)));if(0!==w){do{for(f=l-1;0===t.bl_count[f];)f--;t.bl_count[f]--,t.bl_count[f+1]+=2,t.bl_count[l]--,w-=2}while(w>0);for(f=l;0!==f;f--)for(d=t.bl_count[f];0!==d;)_=t.heap[--h],_>i||(a[2*_+1]!==f&&(t.opt_len+=(f-a[2*_+1])*a[2*_],a[2*_+1]=f),d--)}})(t,e),v(a,h,t.bl_count)},Z=(t,e,a)=>{let i,n,s=-1,r=e[1],o=0,l=7,h=4;for(0===r&&(l=138,h=3),e[2*(a+1)+1]=65535,i=0;i<=a;i++)n=r,r=e[2*(i+1)+1],++o<l&&n===r||(o<h?t.bl_tree[2*n]+=o:0!==n?(n!==s&&t.bl_tree[2*n]++,t.bl_tree[32]++):o<=10?t.bl_tree[34]++:t.bl_tree[36]++,o=0,s=n,0===r?(l=138,h=3):n===r?(l=6,h=3):(l=7,h=4))},U=(t,e,a)=>{let i,n,s=-1,r=e[1],o=0,l=7,h=4;for(0===r&&(l=138,h=3),i=0;i<=a;i++)if(n=r,r=e[2*(i+1)+1],!(++o<l&&n===r)){if(o<h)do{p(t,n,t.bl_tree)}while(0!=--o);else 0!==n?(n!==s&&(p(t,n,t.bl_tree),o--),p(t,16,t.bl_tree),g(t,o-3,2)):o<=10?(p(t,17,t.bl_tree),g(t,o-3,3)):(p(t,18,t.bl_tree),g(t,o-11,7));o=0,s=n,0===r?(l=138,h=3):n===r?(l=6,h=3):(l=7,h=4)}};let S=!1;const D=(t,e,a,i)=>{g(t,0+(i?1:0),3),x(t),b(t,a),b(t,~a),a&&t.pending_buf.set(t.window.subarray(e,e+a),t.pending),t.pending+=a};var T=(t,e,a,i)=>{let o,l,h=0;t.level>0?(2===t.strm.data_type&&(t.strm.data_type=(t=>{let e,a=4093624447;for(e=0;e<=31;e++,a>>>=1)if(1&a&&0!==t.dyn_ltree[2*e])return 0;if(0!==t.dyn_ltree[18]||0!==t.dyn_ltree[20]||0!==t.dyn_ltree[26])return 1;for(e=32;e<256;e++)if(0!==t.dyn_ltree[2*e])return 1;return 0})(t)),R(t,t.l_desc),R(t,t.d_desc),h=(t=>{let e;for(Z(t,t.dyn_ltree,t.l_desc.max_code),Z(t,t.dyn_dtree,t.d_desc.max_code),R(t,t.bl_desc),e=18;e>=3&&0===t.bl_tree[2*n[e]+1];e--);return t.opt_len+=3*(e+1)+5+5+4,e})(t),o=t.opt_len+3+7>>>3,l=t.static_len+3+7>>>3,l<=o&&(o=l)):o=l=a+5,a+4<=o&&-1!==e?D(t,e,a,i):4===t.strategy||l===o?(g(t,2+(i?1:0),3),E(t,s,r)):(g(t,4+(i?1:0),3),((t,e,a,i)=>{let s;for(g(t,e-257,5),g(t,a-1,5),g(t,i-4,4),s=0;s<i;s++)g(t,t.bl_tree[2*n[s]+1],3);U(t,t.dyn_ltree,e-1),U(t,t.dyn_dtree,a-1)})(t,t.l_desc.max_code+1,t.d_desc.max_code+1,h+1),E(t,t.dyn_ltree,t.dyn_dtree)),y(t),i&&x(t)},O={_tr_init:t=>{S||((()=>{let t,n,w,m,b;const g=new Array(16);for(w=0,m=0;m<28;m++)for(h[m]=w,t=0;t<1<<e[m];t++)l[w++]=m;for(l[w-1]=m,b=0,m=0;m<16;m++)for(d[m]=b,t=0;t<1<<a[m];t++)o[b++]=m;for(b>>=7;m<30;m++)for(d[m]=b<<7,t=0;t<1<<a[m]-7;t++)o[256+b++]=m;for(n=0;n<=15;n++)g[n]=0;for(t=0;t<=143;)s[2*t+1]=8,t++,g[8]++;for(;t<=255;)s[2*t+1]=9,t++,g[9]++;for(;t<=279;)s[2*t+1]=7,t++,g[7]++;for(;t<=287;)s[2*t+1]=8,t++,g[8]++;for(v(s,287,g),t=0;t<30;t++)r[2*t+1]=5,r[2*t]=k(t,5);f=new _(s,e,257,286,15),c=new _(r,a,0,30,15),u=new _(new Array(0),i,0,19,7)})(),S=!0),t.l_desc=new w(t.dyn_ltree,f),t.d_desc=new w(t.dyn_dtree,c),t.bl_desc=new w(t.bl_tree,u),t.bi_buf=0,t.bi_valid=0,y(t)},_tr_stored_block:D,_tr_flush_block:T,_tr_tally:(t,e,a)=>(t.pending_buf[t.sym_buf+t.sym_next++]=e,t.pending_buf[t.sym_buf+t.sym_next++]=e>>8,t.pending_buf[t.sym_buf+t.sym_next++]=a,0===e?t.dyn_ltree[2*a]++:(t.matches++,e--,t.dyn_ltree[2*(l[a]+256+1)]++,t.dyn_dtree[2*m(e)]++),t.sym_next===t.sym_end),_tr_align:t=>{g(t,2,3),p(t,256,s),(t=>{16===t.bi_valid?(b(t,t.bi_buf),t.bi_buf=0,t.bi_valid=0):t.bi_valid>=8&&(t.pending_buf[t.pending++]=255&t.bi_buf,t.bi_buf>>=8,t.bi_valid-=8)})(t)}};var F=(t,e,a,i)=>{let n=65535&t|0,s=t>>>16&65535|0,r=0;for(;0!==a;){r=a>2e3?2e3:a,a-=r;do{n=n+e[i++]|0,s=s+n|0}while(--r);n%=65521,s%=65521}return n|s<<16|0};const L=new Uint32Array((()=>{let t,e=[];for(var a=0;a<256;a++){t=a;for(var i=0;i<8;i++)t=1&t?3988292384^t>>>1:t>>>1;e[a]=t}return e})());var N=(t,e,a,i)=>{const n=L,s=i+a;t^=-1;for(let a=i;a<s;a++)t=t>>>8^n[255&(t^e[a])];return-1^t},I={2:"need dictionary",1:"stream end",0:"","-1":"file error","-2":"stream error","-3":"data error","-4":"insufficient memory","-5":"buffer error","-6":"incompatible version"},B={Z_NO_FLUSH:0,Z_PARTIAL_FLUSH:1,Z_SYNC_FLUSH:2,Z_FULL_FLUSH:3,Z_FINISH:4,Z_BLOCK:5,Z_TREES:6,Z_OK:0,Z_STREAM_END:1,Z_NEED_DICT:2,Z_ERRNO:-1,Z_STREAM_ERROR:-2,Z_DATA_ERROR:-3,Z_MEM_ERROR:-4,Z_BUF_ERROR:-5,Z_NO_COMPRESSION:0,Z_BEST_SPEED:1,Z_BEST_COMPRESSION:9,Z_DEFAULT_COMPRESSION:-1,Z_FILTERED:1,Z_HUFFMAN_ONLY:2,Z_RLE:3,Z_FIXED:4,Z_DEFAULT_STRATEGY:0,Z_BINARY:0,Z_TEXT:1,Z_UNKNOWN:2,Z_DEFLATED:8};const{_tr_init:C,_tr_stored_block:H,_tr_flush_block:M,_tr_tally:j,_tr_align:K}=O,{Z_NO_FLUSH:P,Z_PARTIAL_FLUSH:Y,Z_FULL_FLUSH:G,Z_FINISH:X,Z_BLOCK:W,Z_OK:q,Z_STREAM_END:J,Z_STREAM_ERROR:Q,Z_DATA_ERROR:V,Z_BUF_ERROR:$,Z_DEFAULT_COMPRESSION:tt,Z_FILTERED:et,Z_HUFFMAN_ONLY:at,Z_RLE:it,Z_FIXED:nt,Z_DEFAULT_STRATEGY:st,Z_UNKNOWN:rt,Z_DEFLATED:ot}=B,lt=(t,e)=>(t.msg=I[e],e),ht=t=>2*t-(t>4?9:0),dt=t=>{let e=t.length;for(;--e>=0;)t[e]=0},_t=t=>{let e,a,i,n=t.w_size;e=t.hash_size,i=e;do{a=t.head[--i],t.head[i]=a>=n?a-n:0}while(--e);e=n,i=e;do{a=t.prev[--i],t.prev[i]=a>=n?a-n:0}while(--e)};let ft=(t,e,a)=>(e<<t.hash_shift^a)&t.hash_mask;const ct=t=>{const e=t.state;let a=e.pending;a>t.avail_out&&(a=t.avail_out),0!==a&&(t.output.set(e.pending_buf.subarray(e.pending_out,e.pending_out+a),t.next_out),t.next_out+=a,e.pending_out+=a,t.total_out+=a,t.avail_out-=a,e.pending-=a,0===e.pending&&(e.pending_out=0))},ut=(t,e)=>{M(t,t.block_start>=0?t.block_start:-1,t.strstart-t.block_start,e),t.block_start=t.strstart,ct(t.strm)},wt=(t,e)=>{t.pending_buf[t.pending++]=e},mt=(t,e)=>{t.pending_buf[t.pending++]=e>>>8&255,t.pending_buf[t.pending++]=255&e},bt=(t,e,a,i)=>{let n=t.avail_in;return n>i&&(n=i),0===n?0:(t.avail_in-=n,e.set(t.input.subarray(t.next_in,t.next_in+n),a),1===t.state.wrap?t.adler=F(t.adler,e,n,a):2===t.state.wrap&&(t.adler=N(t.adler,e,n,a)),t.next_in+=n,t.total_in+=n,n)},gt=(t,e)=>{let a,i,n=t.max_chain_length,s=t.strstart,r=t.prev_length,o=t.nice_match;const l=t.strstart>t.w_size-262?t.strstart-(t.w_size-262):0,h=t.window,d=t.w_mask,_=t.prev,f=t.strstart+258;let c=h[s+r-1],u=h[s+r];t.prev_length>=t.good_match&&(n>>=2),o>t.lookahead&&(o=t.lookahead);do{if(a=e,h[a+r]===u&&h[a+r-1]===c&&h[a]===h[s]&&h[++a]===h[s+1]){s+=2,a++;do{}while(h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&h[++s]===h[++a]&&s<f);if(i=258-(f-s),s=f-258,i>r){if(t.match_start=e,r=i,i>=o)break;c=h[s+r-1],u=h[s+r]}}}while((e=_[e&d])>l&&0!=--n);return r<=t.lookahead?r:t.lookahead},pt=t=>{const e=t.w_size;let a,i,n;do{if(i=t.window_size-t.lookahead-t.strstart,t.strstart>=e+(e-262)&&(t.window.set(t.window.subarray(e,e+e-i),0),t.match_start-=e,t.strstart-=e,t.block_start-=e,t.insert>t.strstart&&(t.insert=t.strstart),_t(t),i+=e),0===t.strm.avail_in)break;if(a=bt(t.strm,t.window,t.strstart+t.lookahead,i),t.lookahead+=a,t.lookahead+t.insert>=3)for(n=t.strstart-t.insert,t.ins_h=t.window[n],t.ins_h=ft(t,t.ins_h,t.window[n+1]);t.insert&&(t.ins_h=ft(t,t.ins_h,t.window[n+3-1]),t.prev[n&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=n,n++,t.insert--,!(t.lookahead+t.insert<3)););}while(t.lookahead<262&&0!==t.strm.avail_in)},kt=(t,e)=>{let a,i,n,s=t.pending_buf_size-5>t.w_size?t.w_size:t.pending_buf_size-5,r=0,o=t.strm.avail_in;do{if(a=65535,n=t.bi_valid+42>>3,t.strm.avail_out<n)break;if(n=t.strm.avail_out-n,i=t.strstart-t.block_start,a>i+t.strm.avail_in&&(a=i+t.strm.avail_in),a>n&&(a=n),a<s&&(0===a&&e!==X||e===P||a!==i+t.strm.avail_in))break;r=e===X&&a===i+t.strm.avail_in?1:0,H(t,0,0,r),t.pending_buf[t.pending-4]=a,t.pending_buf[t.pending-3]=a>>8,t.pending_buf[t.pending-2]=~a,t.pending_buf[t.pending-1]=~a>>8,ct(t.strm),i&&(i>a&&(i=a),t.strm.output.set(t.window.subarray(t.block_start,t.block_start+i),t.strm.next_out),t.strm.next_out+=i,t.strm.avail_out-=i,t.strm.total_out+=i,t.block_start+=i,a-=i),a&&(bt(t.strm,t.strm.output,t.strm.next_out,a),t.strm.next_out+=a,t.strm.avail_out-=a,t.strm.total_out+=a)}while(0===r);return o-=t.strm.avail_in,o&&(o>=t.w_size?(t.matches=2,t.window.set(t.strm.input.subarray(t.strm.next_in-t.w_size,t.strm.next_in),0),t.strstart=t.w_size,t.insert=t.strstart):(t.window_size-t.strstart<=o&&(t.strstart-=t.w_size,t.window.set(t.window.subarray(t.w_size,t.w_size+t.strstart),0),t.matches<2&&t.matches++,t.insert>t.strstart&&(t.insert=t.strstart)),t.window.set(t.strm.input.subarray(t.strm.next_in-o,t.strm.next_in),t.strstart),t.strstart+=o,t.insert+=o>t.w_size-t.insert?t.w_size-t.insert:o),t.block_start=t.strstart),t.high_water<t.strstart&&(t.high_water=t.strstart),r?4:e!==P&&e!==X&&0===t.strm.avail_in&&t.strstart===t.block_start?2:(n=t.window_size-t.strstart,t.strm.avail_in>n&&t.block_start>=t.w_size&&(t.block_start-=t.w_size,t.strstart-=t.w_size,t.window.set(t.window.subarray(t.w_size,t.w_size+t.strstart),0),t.matches<2&&t.matches++,n+=t.w_size,t.insert>t.strstart&&(t.insert=t.strstart)),n>t.strm.avail_in&&(n=t.strm.avail_in),n&&(bt(t.strm,t.window,t.strstart,n),t.strstart+=n,t.insert+=n>t.w_size-t.insert?t.w_size-t.insert:n),t.high_water<t.strstart&&(t.high_water=t.strstart),n=t.bi_valid+42>>3,n=t.pending_buf_size-n>65535?65535:t.pending_buf_size-n,s=n>t.w_size?t.w_size:n,i=t.strstart-t.block_start,(i>=s||(i||e===X)&&e!==P&&0===t.strm.avail_in&&i<=n)&&(a=i>n?n:i,r=e===X&&0===t.strm.avail_in&&a===i?1:0,H(t,t.block_start,a,r),t.block_start+=a,ct(t.strm)),r?3:1)},vt=(t,e)=>{let a,i;for(;;){if(t.lookahead<262){if(pt(t),t.lookahead<262&&e===P)return 1;if(0===t.lookahead)break}if(a=0,t.lookahead>=3&&(t.ins_h=ft(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart),0!==a&&t.strstart-a<=t.w_size-262&&(t.match_length=gt(t,a)),t.match_length>=3)if(i=j(t,t.strstart-t.match_start,t.match_length-3),t.lookahead-=t.match_length,t.match_length<=t.max_lazy_match&&t.lookahead>=3){t.match_length--;do{t.strstart++,t.ins_h=ft(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart}while(0!=--t.match_length);t.strstart++}else t.strstart+=t.match_length,t.match_length=0,t.ins_h=t.window[t.strstart],t.ins_h=ft(t,t.ins_h,t.window[t.strstart+1]);else i=j(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++;if(i&&(ut(t,!1),0===t.strm.avail_out))return 1}return t.insert=t.strstart<2?t.strstart:2,e===X?(ut(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(ut(t,!1),0===t.strm.avail_out)?1:2},yt=(t,e)=>{let a,i,n;for(;;){if(t.lookahead<262){if(pt(t),t.lookahead<262&&e===P)return 1;if(0===t.lookahead)break}if(a=0,t.lookahead>=3&&(t.ins_h=ft(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart),t.prev_length=t.match_length,t.prev_match=t.match_start,t.match_length=2,0!==a&&t.prev_length<t.max_lazy_match&&t.strstart-a<=t.w_size-262&&(t.match_length=gt(t,a),t.match_length<=5&&(t.strategy===et||3===t.match_length&&t.strstart-t.match_start>4096)&&(t.match_length=2)),t.prev_length>=3&&t.match_length<=t.prev_length){n=t.strstart+t.lookahead-3,i=j(t,t.strstart-1-t.prev_match,t.prev_length-3),t.lookahead-=t.prev_length-1,t.prev_length-=2;do{++t.strstart<=n&&(t.ins_h=ft(t,t.ins_h,t.window[t.strstart+3-1]),a=t.prev[t.strstart&t.w_mask]=t.head[t.ins_h],t.head[t.ins_h]=t.strstart)}while(0!=--t.prev_length);if(t.match_available=0,t.match_length=2,t.strstart++,i&&(ut(t,!1),0===t.strm.avail_out))return 1}else if(t.match_available){if(i=j(t,0,t.window[t.strstart-1]),i&&ut(t,!1),t.strstart++,t.lookahead--,0===t.strm.avail_out)return 1}else t.match_available=1,t.strstart++,t.lookahead--}return t.match_available&&(i=j(t,0,t.window[t.strstart-1]),t.match_available=0),t.insert=t.strstart<2?t.strstart:2,e===X?(ut(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(ut(t,!1),0===t.strm.avail_out)?1:2};function xt(t,e,a,i,n){this.good_length=t,this.max_lazy=e,this.nice_length=a,this.max_chain=i,this.func=n}const zt=[new xt(0,0,0,0,kt),new xt(4,4,8,4,vt),new xt(4,5,16,8,vt),new xt(4,6,32,32,vt),new xt(4,4,16,16,yt),new xt(8,16,32,32,yt),new xt(8,16,128,128,yt),new xt(8,32,128,256,yt),new xt(32,128,258,1024,yt),new xt(32,258,258,4096,yt)];function At(){this.strm=null,this.status=0,this.pending_buf=null,this.pending_buf_size=0,this.pending_out=0,this.pending=0,this.wrap=0,this.gzhead=null,this.gzindex=0,this.method=ot,this.last_flush=-1,this.w_size=0,this.w_bits=0,this.w_mask=0,this.window=null,this.window_size=0,this.prev=null,this.head=null,this.ins_h=0,this.hash_size=0,this.hash_bits=0,this.hash_mask=0,this.hash_shift=0,this.block_start=0,this.match_length=0,this.prev_match=0,this.match_available=0,this.strstart=0,this.match_start=0,this.lookahead=0,this.prev_length=0,this.max_chain_length=0,this.max_lazy_match=0,this.level=0,this.strategy=0,this.good_match=0,this.nice_match=0,this.dyn_ltree=new Uint16Array(1146),this.dyn_dtree=new Uint16Array(122),this.bl_tree=new Uint16Array(78),dt(this.dyn_ltree),dt(this.dyn_dtree),dt(this.bl_tree),this.l_desc=null,this.d_desc=null,this.bl_desc=null,this.bl_count=new Uint16Array(16),this.heap=new Uint16Array(573),dt(this.heap),this.heap_len=0,this.heap_max=0,this.depth=new Uint16Array(573),dt(this.depth),this.sym_buf=0,this.lit_bufsize=0,this.sym_next=0,this.sym_end=0,this.opt_len=0,this.static_len=0,this.matches=0,this.insert=0,this.bi_buf=0,this.bi_valid=0}const Et=t=>{if(!t)return 1;const e=t.state;return!e||e.strm!==t||42!==e.status&&57!==e.status&&69!==e.status&&73!==e.status&&91!==e.status&&103!==e.status&&113!==e.status&&666!==e.status?1:0},Rt=t=>{if(Et(t))return lt(t,Q);t.total_in=t.total_out=0,t.data_type=rt;const e=t.state;return e.pending=0,e.pending_out=0,e.wrap<0&&(e.wrap=-e.wrap),e.status=2===e.wrap?57:e.wrap?42:113,t.adler=2===e.wrap?0:1,e.last_flush=-2,C(e),q},Zt=t=>{const e=Rt(t);var a;return e===q&&((a=t.state).window_size=2*a.w_size,dt(a.head),a.max_lazy_match=zt[a.level].max_lazy,a.good_match=zt[a.level].good_length,a.nice_match=zt[a.level].nice_length,a.max_chain_length=zt[a.level].max_chain,a.strstart=0,a.block_start=0,a.lookahead=0,a.insert=0,a.match_length=a.prev_length=2,a.match_available=0,a.ins_h=0),e},Ut=(t,e,a,i,n,s)=>{if(!t)return Q;let r=1;if(e===tt&&(e=6),i<0?(r=0,i=-i):i>15&&(r=2,i-=16),n<1||n>9||a!==ot||i<8||i>15||e<0||e>9||s<0||s>nt||8===i&&1!==r)return lt(t,Q);8===i&&(i=9);const o=new At;return t.state=o,o.strm=t,o.status=42,o.wrap=r,o.gzhead=null,o.w_bits=i,o.w_size=1<<o.w_bits,o.w_mask=o.w_size-1,o.hash_bits=n+7,o.hash_size=1<<o.hash_bits,o.hash_mask=o.hash_size-1,o.hash_shift=~~((o.hash_bits+3-1)/3),o.window=new Uint8Array(2*o.w_size),o.head=new Uint16Array(o.hash_size),o.prev=new Uint16Array(o.w_size),o.lit_bufsize=1<<n+6,o.pending_buf_size=4*o.lit_bufsize,o.pending_buf=new Uint8Array(o.pending_buf_size),o.sym_buf=o.lit_bufsize,o.sym_end=3*(o.lit_bufsize-1),o.level=e,o.strategy=s,o.method=a,Zt(t)};var St={deflateInit:(t,e)=>Ut(t,e,ot,15,8,st),deflateInit2:Ut,deflateReset:Zt,deflateResetKeep:Rt,deflateSetHeader:(t,e)=>Et(t)||2!==t.state.wrap?Q:(t.state.gzhead=e,q),deflate:(t,e)=>{if(Et(t)||e>W||e<0)return t?lt(t,Q):Q;const a=t.state;if(!t.output||0!==t.avail_in&&!t.input||666===a.status&&e!==X)return lt(t,0===t.avail_out?$:Q);const i=a.last_flush;if(a.last_flush=e,0!==a.pending){if(ct(t),0===t.avail_out)return a.last_flush=-1,q}else if(0===t.avail_in&&ht(e)<=ht(i)&&e!==X)return lt(t,$);if(666===a.status&&0!==t.avail_in)return lt(t,$);if(42===a.status&&0===a.wrap&&(a.status=113),42===a.status){let e=ot+(a.w_bits-8<<4)<<8,i=-1;if(i=a.strategy>=at||a.level<2?0:a.level<6?1:6===a.level?2:3,e|=i<<6,0!==a.strstart&&(e|=32),e+=31-e%31,mt(a,e),0!==a.strstart&&(mt(a,t.adler>>>16),mt(a,65535&t.adler)),t.adler=1,a.status=113,ct(t),0!==a.pending)return a.last_flush=-1,q}if(57===a.status)if(t.adler=0,wt(a,31),wt(a,139),wt(a,8),a.gzhead)wt(a,(a.gzhead.text?1:0)+(a.gzhead.hcrc?2:0)+(a.gzhead.extra?4:0)+(a.gzhead.name?8:0)+(a.gzhead.comment?16:0)),wt(a,255&a.gzhead.time),wt(a,a.gzhead.time>>8&255),wt(a,a.gzhead.time>>16&255),wt(a,a.gzhead.time>>24&255),wt(a,9===a.level?2:a.strategy>=at||a.level<2?4:0),wt(a,255&a.gzhead.os),a.gzhead.extra&&a.gzhead.extra.length&&(wt(a,255&a.gzhead.extra.length),wt(a,a.gzhead.extra.length>>8&255)),a.gzhead.hcrc&&(t.adler=N(t.adler,a.pending_buf,a.pending,0)),a.gzindex=0,a.status=69;else if(wt(a,0),wt(a,0),wt(a,0),wt(a,0),wt(a,0),wt(a,9===a.level?2:a.strategy>=at||a.level<2?4:0),wt(a,3),a.status=113,ct(t),0!==a.pending)return a.last_flush=-1,q;if(69===a.status){if(a.gzhead.extra){let e=a.pending,i=(65535&a.gzhead.extra.length)-a.gzindex;for(;a.pending+i>a.pending_buf_size;){let n=a.pending_buf_size-a.pending;if(a.pending_buf.set(a.gzhead.extra.subarray(a.gzindex,a.gzindex+n),a.pending),a.pending=a.pending_buf_size,a.gzhead.hcrc&&a.pending>e&&(t.adler=N(t.adler,a.pending_buf,a.pending-e,e)),a.gzindex+=n,ct(t),0!==a.pending)return a.last_flush=-1,q;e=0,i-=n}let n=new Uint8Array(a.gzhead.extra);a.pending_buf.set(n.subarray(a.gzindex,a.gzindex+i),a.pending),a.pending+=i,a.gzhead.hcrc&&a.pending>e&&(t.adler=N(t.adler,a.pending_buf,a.pending-e,e)),a.gzindex=0}a.status=73}if(73===a.status){if(a.gzhead.name){let e,i=a.pending;do{if(a.pending===a.pending_buf_size){if(a.gzhead.hcrc&&a.pending>i&&(t.adler=N(t.adler,a.pending_buf,a.pending-i,i)),ct(t),0!==a.pending)return a.last_flush=-1,q;i=0}e=a.gzindex<a.gzhead.name.length?255&a.gzhead.name.charCodeAt(a.gzindex++):0,wt(a,e)}while(0!==e);a.gzhead.hcrc&&a.pending>i&&(t.adler=N(t.adler,a.pending_buf,a.pending-i,i)),a.gzindex=0}a.status=91}if(91===a.status){if(a.gzhead.comment){let e,i=a.pending;do{if(a.pending===a.pending_buf_size){if(a.gzhead.hcrc&&a.pending>i&&(t.adler=N(t.adler,a.pending_buf,a.pending-i,i)),ct(t),0!==a.pending)return a.last_flush=-1,q;i=0}e=a.gzindex<a.gzhead.comment.length?255&a.gzhead.comment.charCodeAt(a.gzindex++):0,wt(a,e)}while(0!==e);a.gzhead.hcrc&&a.pending>i&&(t.adler=N(t.adler,a.pending_buf,a.pending-i,i))}a.status=103}if(103===a.status){if(a.gzhead.hcrc){if(a.pending+2>a.pending_buf_size&&(ct(t),0!==a.pending))return a.last_flush=-1,q;wt(a,255&t.adler),wt(a,t.adler>>8&255),t.adler=0}if(a.status=113,ct(t),0!==a.pending)return a.last_flush=-1,q}if(0!==t.avail_in||0!==a.lookahead||e!==P&&666!==a.status){let i=0===a.level?kt(a,e):a.strategy===at?((t,e)=>{let a;for(;;){if(0===t.lookahead&&(pt(t),0===t.lookahead)){if(e===P)return 1;break}if(t.match_length=0,a=j(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++,a&&(ut(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===X?(ut(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(ut(t,!1),0===t.strm.avail_out)?1:2})(a,e):a.strategy===it?((t,e)=>{let a,i,n,s;const r=t.window;for(;;){if(t.lookahead<=258){if(pt(t),t.lookahead<=258&&e===P)return 1;if(0===t.lookahead)break}if(t.match_length=0,t.lookahead>=3&&t.strstart>0&&(n=t.strstart-1,i=r[n],i===r[++n]&&i===r[++n]&&i===r[++n])){s=t.strstart+258;do{}while(i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&i===r[++n]&&n<s);t.match_length=258-(s-n),t.match_length>t.lookahead&&(t.match_length=t.lookahead)}if(t.match_length>=3?(a=j(t,1,t.match_length-3),t.lookahead-=t.match_length,t.strstart+=t.match_length,t.match_length=0):(a=j(t,0,t.window[t.strstart]),t.lookahead--,t.strstart++),a&&(ut(t,!1),0===t.strm.avail_out))return 1}return t.insert=0,e===X?(ut(t,!0),0===t.strm.avail_out?3:4):t.sym_next&&(ut(t,!1),0===t.strm.avail_out)?1:2})(a,e):zt[a.level].func(a,e);if(3!==i&&4!==i||(a.status=666),1===i||3===i)return 0===t.avail_out&&(a.last_flush=-1),q;if(2===i&&(e===Y?K(a):e!==W&&(H(a,0,0,!1),e===G&&(dt(a.head),0===a.lookahead&&(a.strstart=0,a.block_start=0,a.insert=0))),ct(t),0===t.avail_out))return a.last_flush=-1,q}return e!==X?q:a.wrap<=0?J:(2===a.wrap?(wt(a,255&t.adler),wt(a,t.adler>>8&255),wt(a,t.adler>>16&255),wt(a,t.adler>>24&255),wt(a,255&t.total_in),wt(a,t.total_in>>8&255),wt(a,t.total_in>>16&255),wt(a,t.total_in>>24&255)):(mt(a,t.adler>>>16),mt(a,65535&t.adler)),ct(t),a.wrap>0&&(a.wrap=-a.wrap),0!==a.pending?q:J)},deflateEnd:t=>{if(Et(t))return Q;const e=t.state.status;return t.state=null,113===e?lt(t,V):q},deflateSetDictionary:(t,e)=>{let a=e.length;if(Et(t))return Q;const i=t.state,n=i.wrap;if(2===n||1===n&&42!==i.status||i.lookahead)return Q;if(1===n&&(t.adler=F(t.adler,e,a,0)),i.wrap=0,a>=i.w_size){0===n&&(dt(i.head),i.strstart=0,i.block_start=0,i.insert=0);let t=new Uint8Array(i.w_size);t.set(e.subarray(a-i.w_size,a),0),e=t,a=i.w_size}const s=t.avail_in,r=t.next_in,o=t.input;for(t.avail_in=a,t.next_in=0,t.input=e,pt(i);i.lookahead>=3;){let t=i.strstart,e=i.lookahead-2;do{i.ins_h=ft(i,i.ins_h,i.window[t+3-1]),i.prev[t&i.w_mask]=i.head[i.ins_h],i.head[i.ins_h]=t,t++}while(--e);i.strstart=t,i.lookahead=2,pt(i)}return i.strstart+=i.lookahead,i.block_start=i.strstart,i.insert=i.lookahead,i.lookahead=0,i.match_length=i.prev_length=2,i.match_available=0,t.next_in=r,t.input=o,t.avail_in=s,i.wrap=n,q},deflateInfo:"pako deflate (from Nodeca project)"};const Dt=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var Tt=function(t){const e=Array.prototype.slice.call(arguments,1);for(;e.length;){const a=e.shift();if(a){if("object"!=typeof a)throw new TypeError(a+"must be non-object");for(const e in a)Dt(a,e)&&(t[e]=a[e])}}return t},Ot=t=>{let e=0;for(let a=0,i=t.length;a<i;a++)e+=t[a].length;const a=new Uint8Array(e);for(let e=0,i=0,n=t.length;e<n;e++){let n=t[e];a.set(n,i),i+=n.length}return a};let Ft=!0;try{String.fromCharCode.apply(null,new Uint8Array(1))}catch(t){Ft=!1}const Lt=new Uint8Array(256);for(let t=0;t<256;t++)Lt[t]=t>=252?6:t>=248?5:t>=240?4:t>=224?3:t>=192?2:1;Lt[254]=Lt[254]=1;var Nt=t=>{if("function"==typeof TextEncoder&&TextEncoder.prototype.encode)return(new TextEncoder).encode(t);let e,a,i,n,s,r=t.length,o=0;for(n=0;n<r;n++)a=t.charCodeAt(n),55296==(64512&a)&&n+1<r&&(i=t.charCodeAt(n+1),56320==(64512&i)&&(a=65536+(a-55296<<10)+(i-56320),n++)),o+=a<128?1:a<2048?2:a<65536?3:4;for(e=new Uint8Array(o),s=0,n=0;s<o;n++)a=t.charCodeAt(n),55296==(64512&a)&&n+1<r&&(i=t.charCodeAt(n+1),56320==(64512&i)&&(a=65536+(a-55296<<10)+(i-56320),n++)),a<128?e[s++]=a:a<2048?(e[s++]=192|a>>>6,e[s++]=128|63&a):a<65536?(e[s++]=224|a>>>12,e[s++]=128|a>>>6&63,e[s++]=128|63&a):(e[s++]=240|a>>>18,e[s++]=128|a>>>12&63,e[s++]=128|a>>>6&63,e[s++]=128|63&a);return e},It=(t,e)=>{const a=e||t.length;if("function"==typeof TextDecoder&&TextDecoder.prototype.decode)return(new TextDecoder).decode(t.subarray(0,e));let i,n;const s=new Array(2*a);for(n=0,i=0;i<a;){let e=t[i++];if(e<128){s[n++]=e;continue}let r=Lt[e];if(r>4)s[n++]=65533,i+=r-1;else{for(e&=2===r?31:3===r?15:7;r>1&&i<a;)e=e<<6|63&t[i++],r--;r>1?s[n++]=65533:e<65536?s[n++]=e:(e-=65536,s[n++]=55296|e>>10&1023,s[n++]=56320|1023&e)}}return((t,e)=>{if(e<65534&&t.subarray&&Ft)return String.fromCharCode.apply(null,t.length===e?t:t.subarray(0,e));let a="";for(let i=0;i<e;i++)a+=String.fromCharCode(t[i]);return a})(s,n)},Bt=(t,e)=>{(e=e||t.length)>t.length&&(e=t.length);let a=e-1;for(;a>=0&&128==(192&t[a]);)a--;return a<0||0===a?e:a+Lt[t[a]]>e?a:e};var Ct=function(){this.input=null,this.next_in=0,this.avail_in=0,this.total_in=0,this.output=null,this.next_out=0,this.avail_out=0,this.total_out=0,this.msg="",this.state=null,this.data_type=2,this.adler=0};const Ht=Object.prototype.toString,{Z_NO_FLUSH:Mt,Z_SYNC_FLUSH:jt,Z_FULL_FLUSH:Kt,Z_FINISH:Pt,Z_OK:Yt,Z_STREAM_END:Gt,Z_DEFAULT_COMPRESSION:Xt,Z_DEFAULT_STRATEGY:Wt,Z_DEFLATED:qt}=B;function Jt(t){this.options=Tt({level:Xt,method:qt,chunkSize:16384,windowBits:15,memLevel:8,strategy:Wt},t||{});let e=this.options;e.raw&&e.windowBits>0?e.windowBits=-e.windowBits:e.gzip&&e.windowBits>0&&e.windowBits<16&&(e.windowBits+=16),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new Ct,this.strm.avail_out=0;let a=St.deflateInit2(this.strm,e.level,e.method,e.windowBits,e.memLevel,e.strategy);if(a!==Yt)throw new Error(I[a]);if(e.header&&St.deflateSetHeader(this.strm,e.header),e.dictionary){let t;if(t="string"==typeof e.dictionary?Nt(e.dictionary):"[object ArrayBuffer]"===Ht.call(e.dictionary)?new Uint8Array(e.dictionary):e.dictionary,a=St.deflateSetDictionary(this.strm,t),a!==Yt)throw new Error(I[a]);this._dict_set=!0}}function Qt(t,e){const a=new Jt(e);if(a.push(t,!0),a.err)throw a.msg||I[a.err];return a.result}Jt.prototype.push=function(t,e){const a=this.strm,i=this.options.chunkSize;let n,s;if(this.ended)return!1;for(s=e===~~e?e:!0===e?Pt:Mt,"string"==typeof t?a.input=Nt(t):"[object ArrayBuffer]"===Ht.call(t)?a.input=new Uint8Array(t):a.input=t,a.next_in=0,a.avail_in=a.input.length;;)if(0===a.avail_out&&(a.output=new Uint8Array(i),a.next_out=0,a.avail_out=i),(s===jt||s===Kt)&&a.avail_out<=6)this.onData(a.output.subarray(0,a.next_out)),a.avail_out=0;else{if(n=St.deflate(a,s),n===Gt)return a.next_out>0&&this.onData(a.output.subarray(0,a.next_out)),n=St.deflateEnd(this.strm),this.onEnd(n),this.ended=!0,n===Yt;if(0!==a.avail_out){if(s>0&&a.next_out>0)this.onData(a.output.subarray(0,a.next_out)),a.avail_out=0;else if(0===a.avail_in)break}else this.onData(a.output)}return!0},Jt.prototype.onData=function(t){this.chunks.push(t)},Jt.prototype.onEnd=function(t){t===Yt&&(this.result=Ot(this.chunks)),this.chunks=[],this.err=t,this.msg=this.strm.msg};var Vt={Deflate:Jt,deflate:Qt,deflateRaw:function(t,e){return(e=e||{}).raw=!0,Qt(t,e)},gzip:function(t,e){return(e=e||{}).gzip=!0,Qt(t,e)},constants:B};var $t=function(t,e){let a,i,n,s,r,o,l,h,d,_,f,c,u,w,m,b,g,p,k,v,y,x,z,A;const E=t.state;a=t.next_in,z=t.input,i=a+(t.avail_in-5),n=t.next_out,A=t.output,s=n-(e-t.avail_out),r=n+(t.avail_out-257),o=E.dmax,l=E.wsize,h=E.whave,d=E.wnext,_=E.window,f=E.hold,c=E.bits,u=E.lencode,w=E.distcode,m=(1<<E.lenbits)-1,b=(1<<E.distbits)-1;t:do{c<15&&(f+=z[a++]<<c,c+=8,f+=z[a++]<<c,c+=8),g=u[f&m];e:for(;;){if(p=g>>>24,f>>>=p,c-=p,p=g>>>16&255,0===p)A[n++]=65535&g;else{if(!(16&p)){if(0==(64&p)){g=u[(65535&g)+(f&(1<<p)-1)];continue e}if(32&p){E.mode=16191;break t}t.msg="invalid literal/length code",E.mode=16209;break t}k=65535&g,p&=15,p&&(c<p&&(f+=z[a++]<<c,c+=8),k+=f&(1<<p)-1,f>>>=p,c-=p),c<15&&(f+=z[a++]<<c,c+=8,f+=z[a++]<<c,c+=8),g=w[f&b];a:for(;;){if(p=g>>>24,f>>>=p,c-=p,p=g>>>16&255,!(16&p)){if(0==(64&p)){g=w[(65535&g)+(f&(1<<p)-1)];continue a}t.msg="invalid distance code",E.mode=16209;break t}if(v=65535&g,p&=15,c<p&&(f+=z[a++]<<c,c+=8,c<p&&(f+=z[a++]<<c,c+=8)),v+=f&(1<<p)-1,v>o){t.msg="invalid distance too far back",E.mode=16209;break t}if(f>>>=p,c-=p,p=n-s,v>p){if(p=v-p,p>h&&E.sane){t.msg="invalid distance too far back",E.mode=16209;break t}if(y=0,x=_,0===d){if(y+=l-p,p<k){k-=p;do{A[n++]=_[y++]}while(--p);y=n-v,x=A}}else if(d<p){if(y+=l+d-p,p-=d,p<k){k-=p;do{A[n++]=_[y++]}while(--p);if(y=0,d<k){p=d,k-=p;do{A[n++]=_[y++]}while(--p);y=n-v,x=A}}}else if(y+=d-p,p<k){k-=p;do{A[n++]=_[y++]}while(--p);y=n-v,x=A}for(;k>2;)A[n++]=x[y++],A[n++]=x[y++],A[n++]=x[y++],k-=3;k&&(A[n++]=x[y++],k>1&&(A[n++]=x[y++]))}else{y=n-v;do{A[n++]=A[y++],A[n++]=A[y++],A[n++]=A[y++],k-=3}while(k>2);k&&(A[n++]=A[y++],k>1&&(A[n++]=A[y++]))}break}}break}}while(a<i&&n<r);k=c>>3,a-=k,c-=k<<3,f&=(1<<c)-1,t.next_in=a,t.next_out=n,t.avail_in=a<i?i-a+5:5-(a-i),t.avail_out=n<r?r-n+257:257-(n-r),E.hold=f,E.bits=c};const te=new Uint16Array([3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,0,0]),ee=new Uint8Array([16,16,16,16,16,16,16,16,17,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,16,72,78]),ae=new Uint16Array([1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0]),ie=new Uint8Array([16,16,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,64,64]);var ne=(t,e,a,i,n,s,r,o)=>{const l=o.bits;let h,d,_,f,c,u,w=0,m=0,b=0,g=0,p=0,k=0,v=0,y=0,x=0,z=0,A=null;const E=new Uint16Array(16),R=new Uint16Array(16);let Z,U,S,D=null;for(w=0;w<=15;w++)E[w]=0;for(m=0;m<i;m++)E[e[a+m]]++;for(p=l,g=15;g>=1&&0===E[g];g--);if(p>g&&(p=g),0===g)return n[s++]=20971520,n[s++]=20971520,o.bits=1,0;for(b=1;b<g&&0===E[b];b++);for(p<b&&(p=b),y=1,w=1;w<=15;w++)if(y<<=1,y-=E[w],y<0)return-1;if(y>0&&(0===t||1!==g))return-1;for(R[1]=0,w=1;w<15;w++)R[w+1]=R[w]+E[w];for(m=0;m<i;m++)0!==e[a+m]&&(r[R[e[a+m]]++]=m);if(0===t?(A=D=r,u=20):1===t?(A=te,D=ee,u=257):(A=ae,D=ie,u=0),z=0,m=0,w=b,c=s,k=p,v=0,_=-1,x=1<<p,f=x-1,1===t&&x>852||2===t&&x>592)return 1;for(;;){Z=w-v,r[m]+1<u?(U=0,S=r[m]):r[m]>=u?(U=D[r[m]-u],S=A[r[m]-u]):(U=96,S=0),h=1<<w-v,d=1<<k,b=d;do{d-=h,n[c+(z>>v)+d]=Z<<24|U<<16|S|0}while(0!==d);for(h=1<<w-1;z&h;)h>>=1;if(0!==h?(z&=h-1,z+=h):z=0,m++,0==--E[w]){if(w===g)break;w=e[a+r[m]]}if(w>p&&(z&f)!==_){for(0===v&&(v=p),c+=b,k=w-v,y=1<<k;k+v<g&&(y-=E[k+v],!(y<=0));)k++,y<<=1;if(x+=1<<k,1===t&&x>852||2===t&&x>592)return 1;_=z&f,n[_]=p<<24|k<<16|c-s|0}}return 0!==z&&(n[c+z]=w-v<<24|64<<16|0),o.bits=p,0};const{Z_FINISH:se,Z_BLOCK:re,Z_TREES:oe,Z_OK:le,Z_STREAM_END:he,Z_NEED_DICT:de,Z_STREAM_ERROR:_e,Z_DATA_ERROR:fe,Z_MEM_ERROR:ce,Z_BUF_ERROR:ue,Z_DEFLATED:we}=B,me=16209,be=t=>(t>>>24&255)+(t>>>8&65280)+((65280&t)<<8)+((255&t)<<24);function ge(){this.strm=null,this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}const pe=t=>{if(!t)return 1;const e=t.state;return!e||e.strm!==t||e.mode<16180||e.mode>16211?1:0},ke=t=>{if(pe(t))return _e;const e=t.state;return t.total_in=t.total_out=e.total=0,t.msg="",e.wrap&&(t.adler=1&e.wrap),e.mode=16180,e.last=0,e.havedict=0,e.flags=-1,e.dmax=32768,e.head=null,e.hold=0,e.bits=0,e.lencode=e.lendyn=new Int32Array(852),e.distcode=e.distdyn=new Int32Array(592),e.sane=1,e.back=-1,le},ve=t=>{if(pe(t))return _e;const e=t.state;return e.wsize=0,e.whave=0,e.wnext=0,ke(t)},ye=(t,e)=>{let a;if(pe(t))return _e;const i=t.state;return e<0?(a=0,e=-e):(a=5+(e>>4),e<48&&(e&=15)),e&&(e<8||e>15)?_e:(null!==i.window&&i.wbits!==e&&(i.window=null),i.wrap=a,i.wbits=e,ve(t))},xe=(t,e)=>{if(!t)return _e;const a=new ge;t.state=a,a.strm=t,a.window=null,a.mode=16180;const i=ye(t,e);return i!==le&&(t.state=null),i};let ze,Ae,Ee=!0;const Re=t=>{if(Ee){ze=new Int32Array(512),Ae=new Int32Array(32);let e=0;for(;e<144;)t.lens[e++]=8;for(;e<256;)t.lens[e++]=9;for(;e<280;)t.lens[e++]=7;for(;e<288;)t.lens[e++]=8;for(ne(1,t.lens,0,288,ze,0,t.work,{bits:9}),e=0;e<32;)t.lens[e++]=5;ne(2,t.lens,0,32,Ae,0,t.work,{bits:5}),Ee=!1}t.lencode=ze,t.lenbits=9,t.distcode=Ae,t.distbits=5},Ze=(t,e,a,i)=>{let n;const s=t.state;return null===s.window&&(s.wsize=1<<s.wbits,s.wnext=0,s.whave=0,s.window=new Uint8Array(s.wsize)),i>=s.wsize?(s.window.set(e.subarray(a-s.wsize,a),0),s.wnext=0,s.whave=s.wsize):(n=s.wsize-s.wnext,n>i&&(n=i),s.window.set(e.subarray(a-i,a-i+n),s.wnext),(i-=n)?(s.window.set(e.subarray(a-i,a),0),s.wnext=i,s.whave=s.wsize):(s.wnext+=n,s.wnext===s.wsize&&(s.wnext=0),s.whave<s.wsize&&(s.whave+=n))),0};var Ue={inflateReset:ve,inflateReset2:ye,inflateResetKeep:ke,inflateInit:t=>xe(t,15),inflateInit2:xe,inflate:(t,e)=>{let a,i,n,s,r,o,l,h,d,_,f,c,u,w,m,b,g,p,k,v,y,x,z=0;const A=new Uint8Array(4);let E,R;const Z=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);if(pe(t)||!t.output||!t.input&&0!==t.avail_in)return _e;a=t.state,16191===a.mode&&(a.mode=16192),r=t.next_out,n=t.output,l=t.avail_out,s=t.next_in,i=t.input,o=t.avail_in,h=a.hold,d=a.bits,_=o,f=l,x=le;t:for(;;)switch(a.mode){case 16180:if(0===a.wrap){a.mode=16192;break}for(;d<16;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(2&a.wrap&&35615===h){0===a.wbits&&(a.wbits=15),a.check=0,A[0]=255&h,A[1]=h>>>8&255,a.check=N(a.check,A,2,0),h=0,d=0,a.mode=16181;break}if(a.head&&(a.head.done=!1),!(1&a.wrap)||(((255&h)<<8)+(h>>8))%31){t.msg="incorrect header check",a.mode=me;break}if((15&h)!==we){t.msg="unknown compression method",a.mode=me;break}if(h>>>=4,d-=4,y=8+(15&h),0===a.wbits&&(a.wbits=y),y>15||y>a.wbits){t.msg="invalid window size",a.mode=me;break}a.dmax=1<<a.wbits,a.flags=0,t.adler=a.check=1,a.mode=512&h?16189:16191,h=0,d=0;break;case 16181:for(;d<16;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(a.flags=h,(255&a.flags)!==we){t.msg="unknown compression method",a.mode=me;break}if(57344&a.flags){t.msg="unknown header flags set",a.mode=me;break}a.head&&(a.head.text=h>>8&1),512&a.flags&&4&a.wrap&&(A[0]=255&h,A[1]=h>>>8&255,a.check=N(a.check,A,2,0)),h=0,d=0,a.mode=16182;case 16182:for(;d<32;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}a.head&&(a.head.time=h),512&a.flags&&4&a.wrap&&(A[0]=255&h,A[1]=h>>>8&255,A[2]=h>>>16&255,A[3]=h>>>24&255,a.check=N(a.check,A,4,0)),h=0,d=0,a.mode=16183;case 16183:for(;d<16;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}a.head&&(a.head.xflags=255&h,a.head.os=h>>8),512&a.flags&&4&a.wrap&&(A[0]=255&h,A[1]=h>>>8&255,a.check=N(a.check,A,2,0)),h=0,d=0,a.mode=16184;case 16184:if(1024&a.flags){for(;d<16;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}a.length=h,a.head&&(a.head.extra_len=h),512&a.flags&&4&a.wrap&&(A[0]=255&h,A[1]=h>>>8&255,a.check=N(a.check,A,2,0)),h=0,d=0}else a.head&&(a.head.extra=null);a.mode=16185;case 16185:if(1024&a.flags&&(c=a.length,c>o&&(c=o),c&&(a.head&&(y=a.head.extra_len-a.length,a.head.extra||(a.head.extra=new Uint8Array(a.head.extra_len)),a.head.extra.set(i.subarray(s,s+c),y)),512&a.flags&&4&a.wrap&&(a.check=N(a.check,i,c,s)),o-=c,s+=c,a.length-=c),a.length))break t;a.length=0,a.mode=16186;case 16186:if(2048&a.flags){if(0===o)break t;c=0;do{y=i[s+c++],a.head&&y&&a.length<65536&&(a.head.name+=String.fromCharCode(y))}while(y&&c<o);if(512&a.flags&&4&a.wrap&&(a.check=N(a.check,i,c,s)),o-=c,s+=c,y)break t}else a.head&&(a.head.name=null);a.length=0,a.mode=16187;case 16187:if(4096&a.flags){if(0===o)break t;c=0;do{y=i[s+c++],a.head&&y&&a.length<65536&&(a.head.comment+=String.fromCharCode(y))}while(y&&c<o);if(512&a.flags&&4&a.wrap&&(a.check=N(a.check,i,c,s)),o-=c,s+=c,y)break t}else a.head&&(a.head.comment=null);a.mode=16188;case 16188:if(512&a.flags){for(;d<16;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(4&a.wrap&&h!==(65535&a.check)){t.msg="header crc mismatch",a.mode=me;break}h=0,d=0}a.head&&(a.head.hcrc=a.flags>>9&1,a.head.done=!0),t.adler=a.check=0,a.mode=16191;break;case 16189:for(;d<32;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}t.adler=a.check=be(h),h=0,d=0,a.mode=16190;case 16190:if(0===a.havedict)return t.next_out=r,t.avail_out=l,t.next_in=s,t.avail_in=o,a.hold=h,a.bits=d,de;t.adler=a.check=1,a.mode=16191;case 16191:if(e===re||e===oe)break t;case 16192:if(a.last){h>>>=7&d,d-=7&d,a.mode=16206;break}for(;d<3;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}switch(a.last=1&h,h>>>=1,d-=1,3&h){case 0:a.mode=16193;break;case 1:if(Re(a),a.mode=16199,e===oe){h>>>=2,d-=2;break t}break;case 2:a.mode=16196;break;case 3:t.msg="invalid block type",a.mode=me}h>>>=2,d-=2;break;case 16193:for(h>>>=7&d,d-=7&d;d<32;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if((65535&h)!=(h>>>16^65535)){t.msg="invalid stored block lengths",a.mode=me;break}if(a.length=65535&h,h=0,d=0,a.mode=16194,e===oe)break t;case 16194:a.mode=16195;case 16195:if(c=a.length,c){if(c>o&&(c=o),c>l&&(c=l),0===c)break t;n.set(i.subarray(s,s+c),r),o-=c,s+=c,l-=c,r+=c,a.length-=c;break}a.mode=16191;break;case 16196:for(;d<14;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(a.nlen=257+(31&h),h>>>=5,d-=5,a.ndist=1+(31&h),h>>>=5,d-=5,a.ncode=4+(15&h),h>>>=4,d-=4,a.nlen>286||a.ndist>30){t.msg="too many length or distance symbols",a.mode=me;break}a.have=0,a.mode=16197;case 16197:for(;a.have<a.ncode;){for(;d<3;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}a.lens[Z[a.have++]]=7&h,h>>>=3,d-=3}for(;a.have<19;)a.lens[Z[a.have++]]=0;if(a.lencode=a.lendyn,a.lenbits=7,E={bits:a.lenbits},x=ne(0,a.lens,0,19,a.lencode,0,a.work,E),a.lenbits=E.bits,x){t.msg="invalid code lengths set",a.mode=me;break}a.have=0,a.mode=16198;case 16198:for(;a.have<a.nlen+a.ndist;){for(;z=a.lencode[h&(1<<a.lenbits)-1],m=z>>>24,b=z>>>16&255,g=65535&z,!(m<=d);){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(g<16)h>>>=m,d-=m,a.lens[a.have++]=g;else{if(16===g){for(R=m+2;d<R;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(h>>>=m,d-=m,0===a.have){t.msg="invalid bit length repeat",a.mode=me;break}y=a.lens[a.have-1],c=3+(3&h),h>>>=2,d-=2}else if(17===g){for(R=m+3;d<R;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}h>>>=m,d-=m,y=0,c=3+(7&h),h>>>=3,d-=3}else{for(R=m+7;d<R;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}h>>>=m,d-=m,y=0,c=11+(127&h),h>>>=7,d-=7}if(a.have+c>a.nlen+a.ndist){t.msg="invalid bit length repeat",a.mode=me;break}for(;c--;)a.lens[a.have++]=y}}if(a.mode===me)break;if(0===a.lens[256]){t.msg="invalid code -- missing end-of-block",a.mode=me;break}if(a.lenbits=9,E={bits:a.lenbits},x=ne(1,a.lens,0,a.nlen,a.lencode,0,a.work,E),a.lenbits=E.bits,x){t.msg="invalid literal/lengths set",a.mode=me;break}if(a.distbits=6,a.distcode=a.distdyn,E={bits:a.distbits},x=ne(2,a.lens,a.nlen,a.ndist,a.distcode,0,a.work,E),a.distbits=E.bits,x){t.msg="invalid distances set",a.mode=me;break}if(a.mode=16199,e===oe)break t;case 16199:a.mode=16200;case 16200:if(o>=6&&l>=258){t.next_out=r,t.avail_out=l,t.next_in=s,t.avail_in=o,a.hold=h,a.bits=d,$t(t,f),r=t.next_out,n=t.output,l=t.avail_out,s=t.next_in,i=t.input,o=t.avail_in,h=a.hold,d=a.bits,16191===a.mode&&(a.back=-1);break}for(a.back=0;z=a.lencode[h&(1<<a.lenbits)-1],m=z>>>24,b=z>>>16&255,g=65535&z,!(m<=d);){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(b&&0==(240&b)){for(p=m,k=b,v=g;z=a.lencode[v+((h&(1<<p+k)-1)>>p)],m=z>>>24,b=z>>>16&255,g=65535&z,!(p+m<=d);){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}h>>>=p,d-=p,a.back+=p}if(h>>>=m,d-=m,a.back+=m,a.length=g,0===b){a.mode=16205;break}if(32&b){a.back=-1,a.mode=16191;break}if(64&b){t.msg="invalid literal/length code",a.mode=me;break}a.extra=15&b,a.mode=16201;case 16201:if(a.extra){for(R=a.extra;d<R;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}a.length+=h&(1<<a.extra)-1,h>>>=a.extra,d-=a.extra,a.back+=a.extra}a.was=a.length,a.mode=16202;case 16202:for(;z=a.distcode[h&(1<<a.distbits)-1],m=z>>>24,b=z>>>16&255,g=65535&z,!(m<=d);){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(0==(240&b)){for(p=m,k=b,v=g;z=a.distcode[v+((h&(1<<p+k)-1)>>p)],m=z>>>24,b=z>>>16&255,g=65535&z,!(p+m<=d);){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}h>>>=p,d-=p,a.back+=p}if(h>>>=m,d-=m,a.back+=m,64&b){t.msg="invalid distance code",a.mode=me;break}a.offset=g,a.extra=15&b,a.mode=16203;case 16203:if(a.extra){for(R=a.extra;d<R;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}a.offset+=h&(1<<a.extra)-1,h>>>=a.extra,d-=a.extra,a.back+=a.extra}if(a.offset>a.dmax){t.msg="invalid distance too far back",a.mode=me;break}a.mode=16204;case 16204:if(0===l)break t;if(c=f-l,a.offset>c){if(c=a.offset-c,c>a.whave&&a.sane){t.msg="invalid distance too far back",a.mode=me;break}c>a.wnext?(c-=a.wnext,u=a.wsize-c):u=a.wnext-c,c>a.length&&(c=a.length),w=a.window}else w=n,u=r-a.offset,c=a.length;c>l&&(c=l),l-=c,a.length-=c;do{n[r++]=w[u++]}while(--c);0===a.length&&(a.mode=16200);break;case 16205:if(0===l)break t;n[r++]=a.length,l--,a.mode=16200;break;case 16206:if(a.wrap){for(;d<32;){if(0===o)break t;o--,h|=i[s++]<<d,d+=8}if(f-=l,t.total_out+=f,a.total+=f,4&a.wrap&&f&&(t.adler=a.check=a.flags?N(a.check,n,f,r-f):F(a.check,n,f,r-f)),f=l,4&a.wrap&&(a.flags?h:be(h))!==a.check){t.msg="incorrect data check",a.mode=me;break}h=0,d=0}a.mode=16207;case 16207:if(a.wrap&&a.flags){for(;d<32;){if(0===o)break t;o--,h+=i[s++]<<d,d+=8}if(4&a.wrap&&h!==(4294967295&a.total)){t.msg="incorrect length check",a.mode=me;break}h=0,d=0}a.mode=16208;case 16208:x=he;break t;case me:x=fe;break t;case 16210:return ce;default:return _e}return t.next_out=r,t.avail_out=l,t.next_in=s,t.avail_in=o,a.hold=h,a.bits=d,(a.wsize||f!==t.avail_out&&a.mode<me&&(a.mode<16206||e!==se))&&Ze(t,t.output,t.next_out,f-t.avail_out),_-=t.avail_in,f-=t.avail_out,t.total_in+=_,t.total_out+=f,a.total+=f,4&a.wrap&&f&&(t.adler=a.check=a.flags?N(a.check,n,f,t.next_out-f):F(a.check,n,f,t.next_out-f)),t.data_type=a.bits+(a.last?64:0)+(16191===a.mode?128:0)+(16199===a.mode||16194===a.mode?256:0),(0===_&&0===f||e===se)&&x===le&&(x=ue),x},inflateEnd:t=>{if(pe(t))return _e;let e=t.state;return e.window&&(e.window=null),t.state=null,le},inflateGetHeader:(t,e)=>{if(pe(t))return _e;const a=t.state;return 0==(2&a.wrap)?_e:(a.head=e,e.done=!1,le)},inflateSetDictionary:(t,e)=>{const a=e.length;let i,n,s;return pe(t)?_e:(i=t.state,0!==i.wrap&&16190!==i.mode?_e:16190===i.mode&&(n=1,n=F(n,e,a,0),n!==i.check)?fe:(s=Ze(t,e,a,a),s?(i.mode=16210,ce):(i.havedict=1,le)))},inflateInfo:"pako inflate (from Nodeca project)"};var Se=function(){this.text=0,this.time=0,this.xflags=0,this.os=0,this.extra=null,this.extra_len=0,this.name="",this.comment="",this.hcrc=0,this.done=!1};const De=Object.prototype.toString,{Z_NO_FLUSH:Te,Z_FINISH:Oe,Z_OK:Fe,Z_STREAM_END:Le,Z_NEED_DICT:Ne,Z_STREAM_ERROR:Ie,Z_DATA_ERROR:Be,Z_MEM_ERROR:Ce}=B;function He(t){this.options=Tt({chunkSize:65536,windowBits:15,to:""},t||{});const e=this.options;e.raw&&e.windowBits>=0&&e.windowBits<16&&(e.windowBits=-e.windowBits,0===e.windowBits&&(e.windowBits=-15)),!(e.windowBits>=0&&e.windowBits<16)||t&&t.windowBits||(e.windowBits+=32),e.windowBits>15&&e.windowBits<48&&0==(15&e.windowBits)&&(e.windowBits|=15),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new Ct,this.strm.avail_out=0;let a=Ue.inflateInit2(this.strm,e.windowBits);if(a!==Fe)throw new Error(I[a]);if(this.header=new Se,Ue.inflateGetHeader(this.strm,this.header),e.dictionary&&("string"==typeof e.dictionary?e.dictionary=Nt(e.dictionary):"[object ArrayBuffer]"===De.call(e.dictionary)&&(e.dictionary=new Uint8Array(e.dictionary)),e.raw&&(a=Ue.inflateSetDictionary(this.strm,e.dictionary),a!==Fe)))throw new Error(I[a])}He.prototype.push=function(t,e){const a=this.strm,i=this.options.chunkSize,n=this.options.dictionary;let s,r,o;if(this.ended)return!1;for(r=e===~~e?e:!0===e?Oe:Te,"[object ArrayBuffer]"===De.call(t)?a.input=new Uint8Array(t):a.input=t,a.next_in=0,a.avail_in=a.input.length;;){for(0===a.avail_out&&(a.output=new Uint8Array(i),a.next_out=0,a.avail_out=i),s=Ue.inflate(a,r),s===Ne&&n&&(s=Ue.inflateSetDictionary(a,n),s===Fe?s=Ue.inflate(a,r):s===Be&&(s=Ne));a.avail_in>0&&s===Le&&a.state.wrap>0&&0!==t[a.next_in];)Ue.inflateReset(a),s=Ue.inflate(a,r);switch(s){case Ie:case Be:case Ne:case Ce:return this.onEnd(s),this.ended=!0,!1}if(o=a.avail_out,a.next_out&&(0===a.avail_out||s===Le))if("string"===this.options.to){let t=Bt(a.output,a.next_out),e=a.next_out-t,n=It(a.output,t);a.next_out=e,a.avail_out=i-e,e&&a.output.set(a.output.subarray(t,t+e),0),this.onData(n)}else this.onData(a.output.length===a.next_out?a.output:a.output.subarray(0,a.next_out));if(s!==Fe||0!==o){if(s===Le)return s=Ue.inflateEnd(this.strm),this.onEnd(s),this.ended=!0,!0;if(0===a.avail_in)break}}return!0},He.prototype.onData=function(t){this.chunks.push(t)},He.prototype.onEnd=function(t){t===Fe&&("string"===this.options.to?this.result=this.chunks.join(""):this.result=Ot(this.chunks)),this.chunks=[],this.err=t,this.msg=this.strm.msg};const{Deflate:Me,deflate:je,deflateRaw:Ke,gzip:Pe}=Vt;var Ye=Me,Ge=je,Xe=B;const We=new class{constructor(){this._init()}clear(){this._init()}addEvent(t){if(!t)throw new Error("Adding invalid event");const e=this._hasEvents?",":"";this.deflate.push(e+t,Xe.Z_SYNC_FLUSH),this._hasEvents=!0}finish(){if(this.deflate.push("]",Xe.Z_FINISH),this.deflate.err)throw this.deflate.err;const t=this.deflate.result;return this._init(),t}_init(){this._hasEvents=!1,this.deflate=new Ye,this.deflate.push("[",Xe.Z_NO_FLUSH)}},qe={clear:()=>{We.clear()},addEvent:t=>We.addEvent(t),finish:()=>We.finish(),compress:t=>function(t){return Ge(t)}(t)};addEventListener("message",(function(t){const e=t.data.method,a=t.data.id,i=t.data.arg;if(e in qe&&"function"==typeof qe[e])try{const t=qe[e](i);postMessage({id:a,method:e,success:!0,response:t})}catch(t){postMessage({id:a,method:e,success:!1,response:t.message}),console.error(t)}})),postMessage({id:void 0,method:"init",success:!0,response:void 0});`; + +function e(){const e=new Blob([r]);return URL.createObjectURL(e)} + +/** + * Converts a timestamp to ms, if it was in s, or keeps it as ms. + */ +function timestampToMs(timestamp) { + const isMs = timestamp > 9999999999; + return isMs ? timestamp : timestamp * 1000; +} + +/** This error indicates that the event buffer size exceeded the limit.. */ +class EventBufferSizeExceededError extends Error { + constructor() { + super(`Event buffer exceeded maximum size of ${REPLAY_MAX_EVENT_BUFFER_SIZE}.`); + } +} + +/** + * A basic event buffer that does not do any compression. + * Used as fallback if the compression worker cannot be loaded or is disabled. + */ +class EventBufferArray { + /** All the events that are buffered to be sent. */ + + __init() {this._totalSize = 0;} + + constructor() {EventBufferArray.prototype.__init.call(this); + this.events = []; + } + + /** @inheritdoc */ + get hasEvents() { + return this.events.length > 0; + } + + /** @inheritdoc */ + get type() { + return 'sync'; + } + + /** @inheritdoc */ + destroy() { + this.events = []; + } + + /** @inheritdoc */ + async addEvent(event) { + const eventSize = JSON.stringify(event).length; + this._totalSize += eventSize; + if (this._totalSize > REPLAY_MAX_EVENT_BUFFER_SIZE) { + throw new EventBufferSizeExceededError(); + } + + this.events.push(event); + } + + /** @inheritdoc */ + finish() { + return new Promise(resolve => { + // Make a copy of the events array reference and immediately clear the + // events member so that we do not lose new events while uploading + // attachment. + const eventsRet = this.events; + this.clear(); + resolve(JSON.stringify(eventsRet)); + }); + } + + /** @inheritdoc */ + clear() { + this.events = []; + this._totalSize = 0; + } + + /** @inheritdoc */ + getEarliestTimestamp() { + const timestamp = this.events.map(event => event.timestamp).sort()[0]; + + if (!timestamp) { + return null; + } + + return timestampToMs(timestamp); + } +} + +/** + * Event buffer that uses a web worker to compress events. + * Exported only for testing. + */ +class WorkerHandler { + + constructor(worker) { + this._worker = worker; + this._id = 0; + } + + /** + * Ensure the worker is ready (or not). + * This will either resolve when the worker is ready, or reject if an error occured. + */ + ensureReady() { + // Ensure we only check once + if (this._ensureReadyPromise) { + return this._ensureReadyPromise; + } + + this._ensureReadyPromise = new Promise((resolve, reject) => { + this._worker.addEventListener( + 'message', + ({ data }) => { + if ((data ).success) { + resolve(); + } else { + reject(); + } + }, + { once: true }, + ); + + this._worker.addEventListener( + 'error', + error => { + reject(error); + }, + { once: true }, + ); + }); + + return this._ensureReadyPromise; + } + + /** + * Destroy the worker. + */ + destroy() { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Destroying compression worker'); + this._worker.terminate(); + } + + /** + * Post message to worker and wait for response before resolving promise. + */ + postMessage(method, arg) { + const id = this._getAndIncrementId(); + + return new Promise((resolve, reject) => { + const listener = ({ data }) => { + const response = data ; + if (response.method !== method) { + return; + } + + // There can be multiple listeners for a single method, the id ensures + // that the response matches the caller. + if (response.id !== id) { + return; + } + + // At this point, we'll always want to remove listener regardless of result status + this._worker.removeEventListener('message', listener); + + if (!response.success) { + // TODO: Do some error handling, not sure what + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay]', response.response); + + reject(new Error('Error in compression worker')); + return; + } + + resolve(response.response ); + }; + + // Note: we can't use `once` option because it's possible it needs to + // listen to multiple messages + this._worker.addEventListener('message', listener); + this._worker.postMessage({ id, method, arg }); + }); + } + + /** Get the current ID and increment it for the next call. */ + _getAndIncrementId() { + return this._id++; + } +} + +/** + * Event buffer that uses a web worker to compress events. + * Exported only for testing. + */ +class EventBufferCompressionWorker { + + __init() {this._totalSize = 0;} + + constructor(worker) {EventBufferCompressionWorker.prototype.__init.call(this); + this._worker = new WorkerHandler(worker); + this._earliestTimestamp = null; + } + + /** @inheritdoc */ + get hasEvents() { + return !!this._earliestTimestamp; + } + + /** @inheritdoc */ + get type() { + return 'worker'; + } + + /** + * Ensure the worker is ready (or not). + * This will either resolve when the worker is ready, or reject if an error occured. + */ + ensureReady() { + return this._worker.ensureReady(); + } + + /** + * Destroy the event buffer. + */ + destroy() { + this._worker.destroy(); + } + + /** + * Add an event to the event buffer. + * + * Returns true if event was successfuly received and processed by worker. + */ + addEvent(event) { + const timestamp = timestampToMs(event.timestamp); + if (!this._earliestTimestamp || timestamp < this._earliestTimestamp) { + this._earliestTimestamp = timestamp; + } + + const data = JSON.stringify(event); + this._totalSize += data.length; + + if (this._totalSize > REPLAY_MAX_EVENT_BUFFER_SIZE) { + return Promise.reject(new EventBufferSizeExceededError()); + } + + return this._sendEventToWorker(data); + } + + /** + * Finish the event buffer and return the compressed data. + */ + finish() { + return this._finishRequest(); + } + + /** @inheritdoc */ + clear() { + this._earliestTimestamp = null; + this._totalSize = 0; + // We do not wait on this, as we assume the order of messages is consistent for the worker + void this._worker.postMessage('clear'); + } + + /** @inheritdoc */ + getEarliestTimestamp() { + return this._earliestTimestamp; + } + + /** + * Send the event to the worker. + */ + _sendEventToWorker(data) { + return this._worker.postMessage('addEvent', data); + } + + /** + * Finish the request and return the compressed data from the worker. + */ + async _finishRequest() { + const response = await this._worker.postMessage('finish'); + + this._earliestTimestamp = null; + this._totalSize = 0; + + return response; + } +} + +/** + * This proxy will try to use the compression worker, and fall back to use the simple buffer if an error occurs there. + * This can happen e.g. if the worker cannot be loaded. + * Exported only for testing. + */ +class EventBufferProxy { + + constructor(worker) { + this._fallback = new EventBufferArray(); + this._compression = new EventBufferCompressionWorker(worker); + this._used = this._fallback; + + this._ensureWorkerIsLoadedPromise = this._ensureWorkerIsLoaded(); + } + + /** @inheritdoc */ + get type() { + return this._used.type; + } + + /** @inheritDoc */ + get hasEvents() { + return this._used.hasEvents; + } + + /** @inheritDoc */ + destroy() { + this._fallback.destroy(); + this._compression.destroy(); + } + + /** @inheritdoc */ + clear() { + return this._used.clear(); + } + + /** @inheritdoc */ + getEarliestTimestamp() { + return this._used.getEarliestTimestamp(); + } + + /** + * Add an event to the event buffer. + * + * Returns true if event was successfully added. + */ + addEvent(event) { + return this._used.addEvent(event); + } + + /** @inheritDoc */ + async finish() { + // Ensure the worker is loaded, so the sent event is compressed + await this.ensureWorkerIsLoaded(); + + return this._used.finish(); + } + + /** Ensure the worker has loaded. */ + ensureWorkerIsLoaded() { + return this._ensureWorkerIsLoadedPromise; + } + + /** Actually check if the worker has been loaded. */ + async _ensureWorkerIsLoaded() { + try { + await this._compression.ensureReady(); + } catch (error) { + // If the worker fails to load, we fall back to the simple buffer. + // Nothing more to do from our side here + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Failed to load the compression worker, falling back to simple buffer'); + return; + } + + // Now we need to switch over the array buffer to the compression worker + await this._switchToCompressionWorker(); + } + + /** Switch the used buffer to the compression worker. */ + async _switchToCompressionWorker() { + const { events } = this._fallback; + + const addEventPromises = []; + for (const event of events) { + addEventPromises.push(this._compression.addEvent(event)); + } + + // We switch over to the new buffer immediately - any further events will be added + // after the previously buffered ones + this._used = this._compression; + + // Wait for original events to be re-added before resolving + try { + await Promise.all(addEventPromises); + } catch (error) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('[Replay] Failed to add events when switching buffers.', error); + } + } +} + +/** + * Create an event buffer for replays. + */ +function createEventBuffer({ useCompression }) { + // eslint-disable-next-line no-restricted-globals + if (useCompression && window.Worker) { + try { + const workerUrl = e(); + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Using compression worker'); + const worker = new Worker(workerUrl); + return new EventBufferProxy(worker); + } catch (error) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Failed to create compression worker'); + // Fall back to use simple event buffer array + } + } + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Using simple buffer'); + return new EventBufferArray(); +} + +/** If sessionStorage is available. */ +function hasSessionStorage() { + return 'sessionStorage' in WINDOW && !!WINDOW.sessionStorage; +} + +/** + * Removes the session from Session Storage and unsets session in replay instance + */ +function clearSession(replay) { + deleteSession(); + replay.session = undefined; +} + +/** + * Deletes a session from storage + */ +function deleteSession() { + if (!hasSessionStorage()) { + return; + } + + try { + WINDOW.sessionStorage.removeItem(REPLAY_SESSION_KEY); + } catch (e) { + // Ignore potential SecurityError exceptions + } +} + +/** + * Given an initial timestamp and an expiry duration, checks to see if current + * time should be considered as expired. + */ +function isExpired( + initialTime, + expiry, + targetTime = +new Date(), +) { + // Always expired if < 0 + if (initialTime === null || expiry === undefined || expiry < 0) { + return true; + } + + // Never expires if == 0 + if (expiry === 0) { + return false; + } + + return initialTime + expiry <= targetTime; +} + +/** + * Checks to see if session is expired + */ +function isSessionExpired(session, timeouts, targetTime = +new Date()) { + return ( + // First, check that maximum session length has not been exceeded + isExpired(session.started, timeouts.maxSessionLife, targetTime) || + // check that the idle timeout has not been exceeded (i.e. user has + // performed an action within the last `sessionIdleExpire` ms) + isExpired(session.lastActivity, timeouts.sessionIdleExpire, targetTime) + ); +} + +/** + * Given a sample rate, returns true if replay should be sampled. + * + * 1.0 = 100% sampling + * 0.0 = 0% sampling + */ +function isSampled(sampleRate) { + if (sampleRate === undefined) { + return false; + } + + // Math.random() returns a number in range of 0 to 1 (inclusive of 0, but not 1) + return Math.random() < sampleRate; +} + +/** + * Save a session to session storage. + */ +function saveSession(session) { + if (!hasSessionStorage()) { + return; + } + + try { + WINDOW.sessionStorage.setItem(REPLAY_SESSION_KEY, JSON.stringify(session)); + } catch (e) { + // Ignore potential SecurityError exceptions + } +} + +/** + * Get a session with defaults & applied sampling. + */ +function makeSession(session) { + const now = Date.now(); + const id = session.id || uuid4(); + // Note that this means we cannot set a started/lastActivity of `0`, but this should not be relevant outside of tests. + const started = session.started || now; + const lastActivity = session.lastActivity || now; + const segmentId = session.segmentId || 0; + const sampled = session.sampled; + + return { + id, + started, + lastActivity, + segmentId, + sampled, + shouldRefresh: true, + }; +} + +/** + * Get the sampled status for a session based on sample rates & current sampled status. + */ +function getSessionSampleType(sessionSampleRate, allowBuffering) { + return isSampled(sessionSampleRate) ? 'session' : allowBuffering ? 'buffer' : false; +} + +/** + * Create a new session, which in its current implementation is a Sentry event + * that all replays will be saved to as attachments. Currently, we only expect + * one of these Sentry events per "replay session". + */ +function createSession({ sessionSampleRate, allowBuffering, stickySession = false }) { + const sampled = getSessionSampleType(sessionSampleRate, allowBuffering); + const session = makeSession({ + sampled, + }); + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`[Replay] Creating new session: ${session.id}`); + + if (stickySession) { + saveSession(session); + } + + return session; +} + +/** + * Fetches a session from storage + */ +function fetchSession() { + if (!hasSessionStorage()) { + return null; + } + + try { + // This can throw if cookies are disabled + const sessionStringFromStorage = WINDOW.sessionStorage.getItem(REPLAY_SESSION_KEY); + + if (!sessionStringFromStorage) { + return null; + } + + const sessionObj = JSON.parse(sessionStringFromStorage) ; + + return makeSession(sessionObj); + } catch (e) { + return null; + } +} + +/** + * Get or create a session + */ +function getSession({ + timeouts, + currentSession, + stickySession, + sessionSampleRate, + allowBuffering, +}) { + // If session exists and is passed, use it instead of always hitting session storage + const session = currentSession || (stickySession && fetchSession()); + + if (session) { + // If there is a session, check if it is valid (e.g. "last activity" time + // should be within the "session idle time", and "session started" time is + // within "max session time"). + const isExpired = isSessionExpired(session, timeouts); + + if (!isExpired || (allowBuffering && session.shouldRefresh)) { + return { type: 'saved', session }; + } else if (!session.shouldRefresh) { + // This is the case if we have an error session that is completed + // (=triggered an error). Session will continue as session-based replay, + // and when this session is expired, it will not be renewed until user + // reloads. + const discardedSession = makeSession({ sampled: false }); + return { type: 'new', session: discardedSession }; + } else { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Session has expired'); + } + // Otherwise continue to create a new session + } + + const newSession = createSession({ + stickySession, + sessionSampleRate, + allowBuffering, + }); + + return { type: 'new', session: newSession }; +} + +function isCustomEvent(event) { + return event.type === EventType.Custom; +} + +/** + * Add an event to the event buffer. + * `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`. + */ +async function addEvent( + replay, + event, + isCheckout, +) { + if (!replay.eventBuffer) { + // This implies that `_isEnabled` is false + return null; + } + + if (replay.isPaused()) { + // Do not add to event buffer when recording is paused + return null; + } + + const timestampInMs = timestampToMs(event.timestamp); + + // Throw out events that happen more than 5 minutes ago. This can happen if + // page has been left open and idle for a long period of time and user + // comes back to trigger a new session. The performance entries rely on + // `performance.timeOrigin`, which is when the page first opened. + if (timestampInMs + replay.timeouts.sessionIdlePause < Date.now()) { + return null; + } + + try { + if (isCheckout) { + replay.eventBuffer.clear(); + } + + const replayOptions = replay.getOptions(); + + const eventAfterPossibleCallback = + typeof replayOptions.beforeAddRecordingEvent === 'function' && isCustomEvent(event) + ? replayOptions.beforeAddRecordingEvent(event) + : event; + + if (!eventAfterPossibleCallback) { + return; + } + + return await replay.eventBuffer.addEvent(eventAfterPossibleCallback); + } catch (error) { + const reason = error && error instanceof EventBufferSizeExceededError ? 'addEventSizeExceeded' : 'addEvent'; + + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(error); + await replay.stop(reason); + + const client = getCurrentHub().getClient(); + + if (client) { + client.recordDroppedEvent('internal_sdk_error', 'replay'); + } + } +} + +/** If the event is an error event */ +function isErrorEvent(event) { + return !event.type; +} + +/** If the event is a transaction event */ +function isTransactionEvent(event) { + return event.type === 'transaction'; +} + +/** If the event is an replay event */ +function isReplayEvent(event) { + return event.type === 'replay_event'; +} + +/** + * Returns a listener to be added to `client.on('afterSendErrorEvent, listener)`. + */ +function handleAfterSendEvent(replay) { + // Custom transports may still be returning `Promise<void>`, which means we cannot expect the status code to be available there + // TODO (v8): remove this check as it will no longer be necessary + const enforceStatusCode = isBaseTransportSend(); + + return (event, sendResponse) => { + if (!isErrorEvent(event) && !isTransactionEvent(event)) { + return; + } + + const statusCode = sendResponse && sendResponse.statusCode; + + // We only want to do stuff on successful error sending, otherwise you get error replays without errors attached + // If not using the base transport, we allow `undefined` response (as a custom transport may not implement this correctly yet) + // If we do use the base transport, we skip if we encountered an non-OK status code + if (enforceStatusCode && (!statusCode || statusCode < 200 || statusCode >= 300)) { + return; + } + + // Collect traceIds in _context regardless of `recordingMode` + // In error mode, _context gets cleared on every checkout + if (isTransactionEvent(event) && event.contexts && event.contexts.trace && event.contexts.trace.trace_id) { + replay.getContext().traceIds.add(event.contexts.trace.trace_id ); + return; + } + + // Everything below is just for error events + if (!isErrorEvent(event)) { + return; + } + + // Add error to list of errorIds of replay. This is ok to do even if not + // sampled because context will get reset at next checkout. + // XXX: There is also a race condition where it's possible to capture an + // error to Sentry before Replay SDK has loaded, but response returns after + // it was loaded, and this gets called. + if (event.event_id) { + replay.getContext().errorIds.add(event.event_id); + } + + // If error event is tagged with replay id it means it was sampled (when in buffer mode) + // Need to be very careful that this does not cause an infinite loop + if (replay.recordingMode === 'buffer' && event.tags && event.tags.replayId) { + setTimeout(() => { + // Capture current event buffer as new replay + void replay.sendBufferedReplayOrFlush(); + }); + } + }; +} + +function isBaseTransportSend() { + const client = getCurrentHub().getClient(); + if (!client) { + return false; + } + + const transport = client.getTransport(); + if (!transport) { + return false; + } + + return ( + (transport.send ).__sentry__baseTransport__ || false + ); +} + +/** + * Returns true if we think the given event is an error originating inside of rrweb. + */ +function isRrwebError(event, hint) { + if (event.type || !event.exception || !event.exception.values || !event.exception.values.length) { + return false; + } + + // @ts-ignore this may be set by rrweb when it finds errors + if (hint.originalException && hint.originalException.__rrweb__) { + return true; + } + + // Check if any exception originates from rrweb + return event.exception.values.some(exception => { + if (!exception.stacktrace || !exception.stacktrace.frames || !exception.stacktrace.frames.length) { + return false; + } + + return exception.stacktrace.frames.some(frame => frame.filename && frame.filename.includes('/rrweb/src/')); + }); +} + +/** + * Determine if event should be sampled (only applies in buffer mode). + * When an event is captured by `hanldleGlobalEvent`, when in buffer mode + * we determine if we want to sample the error or not. + */ +function shouldSampleForBufferEvent(replay, event) { + if (replay.recordingMode !== 'buffer') { + return false; + } + + // ignore this error because otherwise we could loop indefinitely with + // trying to capture replay and failing + if (event.message === UNABLE_TO_SEND_REPLAY) { + return false; + } + + // Require the event to be an error event & to have an exception + if (!event.exception || event.type) { + return false; + } + + return isSampled(replay.getOptions().errorSampleRate); +} + +/** + * Returns a listener to be added to `addGlobalEventProcessor(listener)`. + */ +function handleGlobalEventListener( + replay, + includeAfterSendEventHandling = false, +) { + const afterSendHandler = includeAfterSendEventHandling ? handleAfterSendEvent(replay) : undefined; + + return (event, hint) => { + if (isReplayEvent(event)) { + // Replays have separate set of breadcrumbs, do not include breadcrumbs + // from core SDK + delete event.breadcrumbs; + return event; + } + + // We only want to handle errors & transactions, nothing else + if (!isErrorEvent(event) && !isTransactionEvent(event)) { + return event; + } + + // Unless `captureExceptions` is enabled, we want to ignore errors coming from rrweb + // As there can be a bunch of stuff going wrong in internals there, that we don't want to bubble up to users + if (isRrwebError(event, hint) && !replay.getOptions()._experiments.captureExceptions) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Ignoring error from rrweb internals', event); + return null; + } + + // When in buffer mode, we decide to sample here. + // Later, in `handleAfterSendEvent`, if the replayId is set, we know that we sampled + // And convert the buffer session to a full session + const isErrorEventSampled = shouldSampleForBufferEvent(replay, event); + + // Tag errors if it has been sampled in buffer mode, or if it is session mode + // Only tag transactions if in session mode + const shouldTagReplayId = isErrorEventSampled || replay.recordingMode === 'session'; + + if (shouldTagReplayId) { + event.tags = { ...event.tags, replayId: replay.getSessionId() }; + } + + // In cases where a custom client is used that does not support the new hooks (yet), + // we manually call this hook method here + if (afterSendHandler) { + // Pretend the error had a 200 response so we always capture it + afterSendHandler(event, { statusCode: 200 }); + } + + return event; + }; +} + +/** + * Create a "span" for each performance entry. + */ +function createPerformanceSpans( + replay, + entries, +) { + return entries.map(({ type, start, end, name, data }) => { + const response = replay.throttledAddEvent({ + type: EventType.Custom, + timestamp: start, + data: { + tag: 'performanceSpan', + payload: { + op: type, + description: name, + startTimestamp: start, + endTimestamp: end, + data, + }, + }, + }); + + // If response is a string, it means its either THROTTLED or SKIPPED + return typeof response === 'string' ? Promise.resolve(null) : response; + }); +} + +function handleHistory(handlerData) { + const { from, to } = handlerData; + + const now = Date.now() / 1000; + + return { + type: 'navigation.push', + start: now, + end: now, + name: to, + data: { + previous: from, + }, + }; +} + +/** + * Returns a listener to be added to `addInstrumentationHandler('history', listener)`. + */ +function handleHistorySpanListener(replay) { + return (handlerData) => { + if (!replay.isEnabled()) { + return; + } + + const result = handleHistory(handlerData); + + if (result === null) { + return; + } + + // Need to collect visited URLs + replay.getContext().urls.push(result.name); + replay.triggerUserActivity(); + + replay.addUpdate(() => { + createPerformanceSpans(replay, [result]); + // Returning false to flush + return false; + }); + }; +} + +/** + * Check whether a given request URL should be filtered out. This is so we + * don't log Sentry ingest requests. + */ +function shouldFilterRequest(replay, url) { + // If we enabled the `traceInternals` experiment, we want to trace everything + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && replay.getOptions()._experiments.traceInternals) { + return false; + } + + return _isSentryRequest(url); +} + +/** + * Checks wether a given URL belongs to the configured Sentry DSN. + */ +function _isSentryRequest(url) { + const client = getCurrentHub().getClient(); + const dsn = client && client.getDsn(); + return dsn ? url.includes(dsn.host) : false; +} + +/** Add a performance entry breadcrumb */ +function addNetworkBreadcrumb( + replay, + result, +) { + if (!replay.isEnabled()) { + return; + } + + if (result === null) { + return; + } + + if (shouldFilterRequest(replay, result.name)) { + return; + } + + replay.addUpdate(() => { + createPerformanceSpans(replay, [result]); + // Returning true will cause `addUpdate` to not flush + // We do not want network requests to cause a flush. This will prevent + // recurring/polling requests from keeping the replay session alive. + return true; + }); +} + +/** only exported for tests */ +function handleFetch(handlerData) { + const { startTimestamp, endTimestamp, fetchData, response } = handlerData; + + if (!endTimestamp) { + return null; + } + + // This is only used as a fallback, so we know the body sizes are never set here + const { method, url } = fetchData; + + return { + type: 'resource.fetch', + start: startTimestamp / 1000, + end: endTimestamp / 1000, + name: url, + data: { + method, + statusCode: response ? (response ).status : undefined, + }, + }; +} + +/** + * Returns a listener to be added to `addInstrumentationHandler('fetch', listener)`. + */ +function handleFetchSpanListener(replay) { + return (handlerData) => { + if (!replay.isEnabled()) { + return; + } + + const result = handleFetch(handlerData); + + addNetworkBreadcrumb(replay, result); + }; +} + +/** only exported for tests */ +function handleXhr(handlerData) { + const { startTimestamp, endTimestamp, xhr } = handlerData; + + const sentryXhrData = xhr[SENTRY_XHR_DATA_KEY]; + + if (!startTimestamp || !endTimestamp || !sentryXhrData) { + return null; + } + + // This is only used as a fallback, so we know the body sizes are never set here + const { method, url, status_code: statusCode } = sentryXhrData; + + if (url === undefined) { + return null; + } + + return { + type: 'resource.xhr', + name: url, + start: startTimestamp / 1000, + end: endTimestamp / 1000, + data: { + method, + statusCode, + }, + }; +} + +/** + * Returns a listener to be added to `addInstrumentationHandler('xhr', listener)`. + */ +function handleXhrSpanListener(replay) { + return (handlerData) => { + if (!replay.isEnabled()) { + return; + } + + const result = handleXhr(handlerData); + + addNetworkBreadcrumb(replay, result); + }; +} + +const OBJ = 10; +const OBJ_KEY = 11; +const OBJ_KEY_STR = 12; +const OBJ_VAL = 13; +const OBJ_VAL_STR = 14; +const OBJ_VAL_COMPLETED = 15; + +const ARR = 20; +const ARR_VAL = 21; +const ARR_VAL_STR = 22; +const ARR_VAL_COMPLETED = 23; + +const ALLOWED_PRIMITIVES = ['true', 'false', 'null']; + +/** + * Complete an incomplete JSON string. + * This will ensure that the last element always has a `"~~"` to indicate it was truncated. + * For example, `[1,2,` will be completed to `[1,2,"~~"]` + * and `{"aa":"b` will be completed to `{"aa":"b~~"}` + */ +function completeJson(incompleteJson, stack) { + if (!stack.length) { + return incompleteJson; + } + + let json = incompleteJson; + + // Most checks are only needed for the last step in the stack + const lastPos = stack.length - 1; + const lastStep = stack[lastPos]; + + json = _fixLastStep(json, lastStep); + + // Complete remaining steps - just add closing brackets + for (let i = lastPos; i >= 0; i--) { + const step = stack[i]; + + switch (step) { + case OBJ: + json = `${json}}`; + break; + case ARR: + json = `${json}]`; + break; + } + } + + return json; +} + +function _fixLastStep(json, lastStep) { + switch (lastStep) { + // Object cases + case OBJ: + return `${json}"~~":"~~"`; + case OBJ_KEY: + return `${json}:"~~"`; + case OBJ_KEY_STR: + return `${json}~~":"~~"`; + case OBJ_VAL: + return _maybeFixIncompleteObjValue(json); + case OBJ_VAL_STR: + return `${json}~~"`; + case OBJ_VAL_COMPLETED: + return `${json},"~~":"~~"`; + + // Array cases + case ARR: + return `${json}"~~"`; + case ARR_VAL: + return _maybeFixIncompleteArrValue(json); + case ARR_VAL_STR: + return `${json}~~"`; + case ARR_VAL_COMPLETED: + return `${json},"~~"`; + } + + return json; +} + +function _maybeFixIncompleteArrValue(json) { + const pos = _findLastArrayDelimiter(json); + + if (pos > -1) { + const part = json.slice(pos + 1); + + if (ALLOWED_PRIMITIVES.includes(part.trim())) { + return `${json},"~~"`; + } + + // Everything else is replaced with `"~~"` + return `${json.slice(0, pos + 1)}"~~"`; + } + + // fallback, this shouldn't happen, to be save + return json; +} + +function _findLastArrayDelimiter(json) { + for (let i = json.length - 1; i >= 0; i--) { + const char = json[i]; + + if (char === ',' || char === '[') { + return i; + } + } + + return -1; +} + +function _maybeFixIncompleteObjValue(json) { + const startPos = json.lastIndexOf(':'); + + const part = json.slice(startPos + 1); + + if (ALLOWED_PRIMITIVES.includes(part.trim())) { + return `${json},"~~":"~~"`; + } + + // Everything else is replaced with `"~~"` + // This also means we do not have incomplete numbers, e.g `[1` is replaced with `["~~"]` + return `${json.slice(0, startPos + 1)}"~~"`; +} + +/** + * Evaluate an (incomplete) JSON string. + */ +function evaluateJson(json) { + const stack = []; + + for (let pos = 0; pos < json.length; pos++) { + _evaluateJsonPos(stack, json, pos); + } + + return stack; +} + +function _evaluateJsonPos(stack, json, pos) { + const curStep = stack[stack.length - 1]; + + const char = json[pos]; + + const whitespaceRegex = /\s/; + + if (whitespaceRegex.test(char)) { + return; + } + + if (char === '"' && !_isEscaped(json, pos)) { + _handleQuote(stack, curStep); + return; + } + + switch (char) { + case '{': + _handleObj(stack, curStep); + break; + case '[': + _handleArr(stack, curStep); + break; + case ':': + _handleColon(stack, curStep); + break; + case ',': + _handleComma(stack, curStep); + break; + case '}': + _handleObjClose(stack, curStep); + break; + case ']': + _handleArrClose(stack, curStep); + break; + } +} + +function _handleQuote(stack, curStep) { + // End of obj value + if (curStep === OBJ_VAL_STR) { + stack.pop(); + stack.push(OBJ_VAL_COMPLETED); + return; + } + + // End of arr value + if (curStep === ARR_VAL_STR) { + stack.pop(); + stack.push(ARR_VAL_COMPLETED); + return; + } + + // Start of obj value + if (curStep === OBJ_VAL) { + stack.push(OBJ_VAL_STR); + return; + } + + // Start of arr value + if (curStep === ARR_VAL) { + stack.push(ARR_VAL_STR); + return; + } + + // Start of obj key + if (curStep === OBJ) { + stack.push(OBJ_KEY_STR); + return; + } + + // End of obj key + if (curStep === OBJ_KEY_STR) { + stack.pop(); + stack.push(OBJ_KEY); + return; + } +} + +function _handleObj(stack, curStep) { + // Initial object + if (!curStep) { + stack.push(OBJ); + return; + } + + // New object as obj value + if (curStep === OBJ_VAL) { + stack.push(OBJ); + return; + } + + // New object as array element + if (curStep === ARR_VAL) { + stack.push(OBJ); + } + + // New object as first array element + if (curStep === ARR) { + stack.push(OBJ); + return; + } +} + +function _handleArr(stack, curStep) { + // Initial array + if (!curStep) { + stack.push(ARR); + stack.push(ARR_VAL); + return; + } + + // New array as obj value + if (curStep === OBJ_VAL) { + stack.push(ARR); + stack.push(ARR_VAL); + return; + } + + // New array as array element + if (curStep === ARR_VAL) { + stack.push(ARR); + stack.push(ARR_VAL); + } + + // New array as first array element + if (curStep === ARR) { + stack.push(ARR); + stack.push(ARR_VAL); + return; + } +} + +function _handleColon(stack, curStep) { + if (curStep === OBJ_KEY) { + stack.pop(); + stack.push(OBJ_VAL); + } +} + +function _handleComma(stack, curStep) { + // Comma after obj value + if (curStep === OBJ_VAL) { + stack.pop(); + return; + } + if (curStep === OBJ_VAL_COMPLETED) { + // Pop OBJ_VAL_COMPLETED & OBJ_VAL + stack.pop(); + stack.pop(); + return; + } + + // Comma after arr value + if (curStep === ARR_VAL) { + // do nothing - basically we'd pop ARR_VAL but add it right back + return; + } + + if (curStep === ARR_VAL_COMPLETED) { + // Pop ARR_VAL_COMPLETED + stack.pop(); + + // basically we'd pop ARR_VAL but add it right back + return; + } +} + +function _handleObjClose(stack, curStep) { + // Empty object {} + if (curStep === OBJ) { + stack.pop(); + } + + // Object with element + if (curStep === OBJ_VAL) { + // Pop OBJ_VAL, OBJ + stack.pop(); + stack.pop(); + } + + // Obj with element + if (curStep === OBJ_VAL_COMPLETED) { + // Pop OBJ_VAL_COMPLETED, OBJ_VAL, OBJ + stack.pop(); + stack.pop(); + stack.pop(); + } + + // if was obj value, complete it + if (stack[stack.length - 1] === OBJ_VAL) { + stack.push(OBJ_VAL_COMPLETED); + } + + // if was arr value, complete it + if (stack[stack.length - 1] === ARR_VAL) { + stack.push(ARR_VAL_COMPLETED); + } +} + +function _handleArrClose(stack, curStep) { + // Empty array [] + if (curStep === ARR) { + stack.pop(); + } + + // Array with element + if (curStep === ARR_VAL) { + // Pop ARR_VAL, ARR + stack.pop(); + stack.pop(); + } + + // Array with element + if (curStep === ARR_VAL_COMPLETED) { + // Pop ARR_VAL_COMPLETED, ARR_VAL, ARR + stack.pop(); + stack.pop(); + stack.pop(); + } + + // if was obj value, complete it + if (stack[stack.length - 1] === OBJ_VAL) { + stack.push(OBJ_VAL_COMPLETED); + } + + // if was arr value, complete it + if (stack[stack.length - 1] === ARR_VAL) { + stack.push(ARR_VAL_COMPLETED); + } +} + +function _isEscaped(str, pos) { + const previousChar = str[pos - 1]; + + return previousChar === '\\' && !_isEscaped(str, pos - 1); +} + +/* eslint-disable max-lines */ + +/** + * Takes an incomplete JSON string, and returns a hopefully valid JSON string. + * Note that this _can_ fail, so you should check the return value is valid JSON. + */ +function fixJson(incompleteJson) { + const stack = evaluateJson(incompleteJson); + + return completeJson(incompleteJson, stack); +} + +/** Get the size of a body. */ +function getBodySize( + body, + textEncoder, +) { + if (!body) { + return undefined; + } + + try { + if (typeof body === 'string') { + return textEncoder.encode(body).length; + } + + if (body instanceof URLSearchParams) { + return textEncoder.encode(body.toString()).length; + } + + if (body instanceof FormData) { + const formDataStr = _serializeFormData(body); + return textEncoder.encode(formDataStr).length; + } + + if (body instanceof Blob) { + return body.size; + } + + if (body instanceof ArrayBuffer) { + return body.byteLength; + } + + // Currently unhandled types: ArrayBufferView, ReadableStream + } catch (e) { + // just return undefined + } + + return undefined; +} + +/** Convert a Content-Length header to number/undefined. */ +function parseContentLengthHeader(header) { + if (!header) { + return undefined; + } + + const size = parseInt(header, 10); + return isNaN(size) ? undefined : size; +} + +/** Get the string representation of a body. */ +function getBodyString(body) { + if (typeof body === 'string') { + return body; + } + + if (body instanceof URLSearchParams) { + return body.toString(); + } + + if (body instanceof FormData) { + return _serializeFormData(body); + } + + return undefined; +} + +/** Convert ReplayNetworkRequestData to a PerformanceEntry. */ +function makeNetworkReplayBreadcrumb( + type, + data, +) { + if (!data) { + return null; + } + + const { startTimestamp, endTimestamp, url, method, statusCode, request, response } = data; + + const result = { + type, + start: startTimestamp / 1000, + end: endTimestamp / 1000, + name: url, + data: dropUndefinedKeys({ + method, + statusCode, + request, + response, + }), + }; + + return result; +} + +/** Build the request or response part of a replay network breadcrumb that was skipped. */ +function buildSkippedNetworkRequestOrResponse(bodySize) { + return { + headers: {}, + size: bodySize, + _meta: { + warnings: ['URL_SKIPPED'], + }, + }; +} + +/** Build the request or response part of a replay network breadcrumb. */ +function buildNetworkRequestOrResponse( + headers, + bodySize, + body, +) { + if (!bodySize && Object.keys(headers).length === 0) { + return undefined; + } + + if (!bodySize) { + return { + headers, + }; + } + + if (!body) { + return { + headers, + size: bodySize, + }; + } + + const info = { + headers, + size: bodySize, + }; + + const { body: normalizedBody, warnings } = normalizeNetworkBody(body); + info.body = normalizedBody; + if (warnings.length > 0) { + info._meta = { + warnings, + }; + } + + return info; +} + +/** Filter a set of headers */ +function getAllowedHeaders(headers, allowedHeaders) { + return Object.keys(headers).reduce((filteredHeaders, key) => { + const normalizedKey = key.toLowerCase(); + // Avoid putting empty strings into the headers + if (allowedHeaders.includes(normalizedKey) && headers[key]) { + filteredHeaders[normalizedKey] = headers[key]; + } + return filteredHeaders; + }, {}); +} + +function _serializeFormData(formData) { + // This is a bit simplified, but gives us a decent estimate + // This converts e.g. { name: 'Anne Smith', age: 13 } to 'name=Anne+Smith&age=13' + // @ts-ignore passing FormData to URLSearchParams actually works + return new URLSearchParams(formData).toString(); +} + +function normalizeNetworkBody(body) + + { + if (!body || typeof body !== 'string') { + return { + body, + warnings: [], + }; + } + + const exceedsSizeLimit = body.length > NETWORK_BODY_MAX_SIZE; + + if (_strIsProbablyJson(body)) { + try { + const json = exceedsSizeLimit ? fixJson(body.slice(0, NETWORK_BODY_MAX_SIZE)) : body; + const normalizedBody = JSON.parse(json); + return { + body: normalizedBody, + warnings: exceedsSizeLimit ? ['JSON_TRUNCATED'] : [], + }; + } catch (e3) { + return { + body: exceedsSizeLimit ? `${body.slice(0, NETWORK_BODY_MAX_SIZE)}…` : body, + warnings: exceedsSizeLimit ? ['INVALID_JSON', 'TEXT_TRUNCATED'] : ['INVALID_JSON'], + }; + } + } + + return { + body: exceedsSizeLimit ? `${body.slice(0, NETWORK_BODY_MAX_SIZE)}…` : body, + warnings: exceedsSizeLimit ? ['TEXT_TRUNCATED'] : [], + }; +} + +function _strIsProbablyJson(str) { + const first = str[0]; + const last = str[str.length - 1]; + + // Simple check: If this does not start & end with {} or [], it's not JSON + return (first === '[' && last === ']') || (first === '{' && last === '}'); +} + +/** Match an URL against a list of strings/Regex. */ +function urlMatches(url, urls) { + const fullUrl = getFullUrl(url); + + return stringMatchesSomePattern(fullUrl, urls); +} + +/** exported for tests */ +function getFullUrl(url, baseURI = WINDOW.document.baseURI) { + // Short circuit for common cases: + if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith(WINDOW.location.origin)) { + return url; + } + const fixedUrl = new URL(url, baseURI); + + // If these do not match, we are not dealing with a relative URL, so just return it + if (fixedUrl.origin !== new URL(baseURI).origin) { + return url; + } + + const fullUrl = fixedUrl.href; + + // Remove trailing slashes, if they don't match the original URL + if (!url.endsWith('/') && fullUrl.endsWith('/')) { + return fullUrl.slice(0, -1); + } + + return fullUrl; +} + +/** + * Capture a fetch breadcrumb to a replay. + * This adds additional data (where approriate). + */ +async function captureFetchBreadcrumbToReplay( + breadcrumb, + hint, + options + +, +) { + try { + const data = await _prepareFetchData(breadcrumb, hint, options); + + // Create a replay performance entry from this breadcrumb + const result = makeNetworkReplayBreadcrumb('resource.fetch', data); + addNetworkBreadcrumb(options.replay, result); + } catch (error) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] Failed to capture fetch breadcrumb', error); + } +} + +/** + * Enrich a breadcrumb with additional data. + * This has to be sync & mutate the given breadcrumb, + * as the breadcrumb is afterwards consumed by other handlers. + */ +function enrichFetchBreadcrumb( + breadcrumb, + hint, + options, +) { + const { input, response } = hint; + + const body = _getFetchRequestArgBody(input); + const reqSize = getBodySize(body, options.textEncoder); + + const resSize = response ? parseContentLengthHeader(response.headers.get('content-length')) : undefined; + + if (reqSize !== undefined) { + breadcrumb.data.request_body_size = reqSize; + } + if (resSize !== undefined) { + breadcrumb.data.response_body_size = resSize; + } +} + +async function _prepareFetchData( + breadcrumb, + hint, + options + +, +) { + const { startTimestamp, endTimestamp } = hint; + + const { + url, + method, + status_code: statusCode = 0, + request_body_size: requestBodySize, + response_body_size: responseBodySize, + } = breadcrumb.data; + + const captureDetails = urlMatches(url, options.networkDetailAllowUrls); + + const request = captureDetails + ? _getRequestInfo(options, hint.input, requestBodySize) + : buildSkippedNetworkRequestOrResponse(requestBodySize); + const response = await _getResponseInfo(captureDetails, options, hint.response, responseBodySize); + + return { + startTimestamp, + endTimestamp, + url, + method, + statusCode, + request, + response, + }; +} + +function _getRequestInfo( + { networkCaptureBodies, networkRequestHeaders }, + input, + requestBodySize, +) { + const headers = getRequestHeaders(input, networkRequestHeaders); + + if (!networkCaptureBodies) { + return buildNetworkRequestOrResponse(headers, requestBodySize, undefined); + } + + // We only want to transmit string or string-like bodies + const requestBody = _getFetchRequestArgBody(input); + const bodyStr = getBodyString(requestBody); + return buildNetworkRequestOrResponse(headers, requestBodySize, bodyStr); +} + +async function _getResponseInfo( + captureDetails, + { + networkCaptureBodies, + textEncoder, + networkResponseHeaders, + } + +, + response, + responseBodySize, +) { + if (!captureDetails && responseBodySize !== undefined) { + return buildSkippedNetworkRequestOrResponse(responseBodySize); + } + + const headers = getAllHeaders(response.headers, networkResponseHeaders); + + if (!networkCaptureBodies && responseBodySize !== undefined) { + return buildNetworkRequestOrResponse(headers, responseBodySize, undefined); + } + + // Only clone the response if we need to + try { + // We have to clone this, as the body can only be read once + const res = response.clone(); + const bodyText = await _parseFetchBody(res); + + const size = + bodyText && bodyText.length && responseBodySize === undefined + ? getBodySize(bodyText, textEncoder) + : responseBodySize; + + if (!captureDetails) { + return buildSkippedNetworkRequestOrResponse(size); + } + + if (networkCaptureBodies) { + return buildNetworkRequestOrResponse(headers, size, bodyText); + } + + return buildNetworkRequestOrResponse(headers, size, undefined); + } catch (e) { + // fallback + return buildNetworkRequestOrResponse(headers, responseBodySize, undefined); + } +} + +async function _parseFetchBody(response) { + try { + return await response.text(); + } catch (e2) { + return undefined; + } +} + +function _getFetchRequestArgBody(fetchArgs = []) { + // We only support getting the body from the fetch options + if (fetchArgs.length !== 2 || typeof fetchArgs[1] !== 'object') { + return undefined; + } + + return (fetchArgs[1] ).body; +} + +function getAllHeaders(headers, allowedHeaders) { + const allHeaders = {}; + + allowedHeaders.forEach(header => { + if (headers.get(header)) { + allHeaders[header] = headers.get(header) ; + } + }); + + return allHeaders; +} + +function getRequestHeaders(fetchArgs, allowedHeaders) { + if (fetchArgs.length === 1 && typeof fetchArgs[0] !== 'string') { + return getHeadersFromOptions(fetchArgs[0] , allowedHeaders); + } + + if (fetchArgs.length === 2) { + return getHeadersFromOptions(fetchArgs[1] , allowedHeaders); + } + + return {}; +} + +function getHeadersFromOptions( + input, + allowedHeaders, +) { + if (!input) { + return {}; + } + + const headers = input.headers; + + if (!headers) { + return {}; + } + + if (headers instanceof Headers) { + return getAllHeaders(headers, allowedHeaders); + } + + // We do not support this, as it is not really documented (anymore?) + if (Array.isArray(headers)) { + return {}; + } + + return getAllowedHeaders(headers, allowedHeaders); +} + +/** + * Capture an XHR breadcrumb to a replay. + * This adds additional data (where approriate). + */ +async function captureXhrBreadcrumbToReplay( + breadcrumb, + hint, + options, +) { + try { + const data = _prepareXhrData(breadcrumb, hint, options); + + // Create a replay performance entry from this breadcrumb + const result = makeNetworkReplayBreadcrumb('resource.xhr', data); + addNetworkBreadcrumb(options.replay, result); + } catch (error) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] Failed to capture fetch breadcrumb', error); + } +} + +/** + * Enrich a breadcrumb with additional data. + * This has to be sync & mutate the given breadcrumb, + * as the breadcrumb is afterwards consumed by other handlers. + */ +function enrichXhrBreadcrumb( + breadcrumb, + hint, + options, +) { + const { xhr, input } = hint; + + const reqSize = getBodySize(input, options.textEncoder); + const resSize = xhr.getResponseHeader('content-length') + ? parseContentLengthHeader(xhr.getResponseHeader('content-length')) + : getBodySize(xhr.response, options.textEncoder); + + if (reqSize !== undefined) { + breadcrumb.data.request_body_size = reqSize; + } + if (resSize !== undefined) { + breadcrumb.data.response_body_size = resSize; + } +} + +function _prepareXhrData( + breadcrumb, + hint, + options, +) { + const { startTimestamp, endTimestamp, input, xhr } = hint; + + const { + url, + method, + status_code: statusCode = 0, + request_body_size: requestBodySize, + response_body_size: responseBodySize, + } = breadcrumb.data; + + if (!url) { + return null; + } + + if (!urlMatches(url, options.networkDetailAllowUrls)) { + const request = buildSkippedNetworkRequestOrResponse(requestBodySize); + const response = buildSkippedNetworkRequestOrResponse(responseBodySize); + return { + startTimestamp, + endTimestamp, + url, + method, + statusCode, + request, + response, + }; + } + + const xhrInfo = xhr[SENTRY_XHR_DATA_KEY]; + const networkRequestHeaders = xhrInfo + ? getAllowedHeaders(xhrInfo.request_headers, options.networkRequestHeaders) + : {}; + const networkResponseHeaders = getAllowedHeaders(getResponseHeaders(xhr), options.networkResponseHeaders); + + const request = buildNetworkRequestOrResponse( + networkRequestHeaders, + requestBodySize, + options.networkCaptureBodies ? getBodyString(input) : undefined, + ); + const response = buildNetworkRequestOrResponse( + networkResponseHeaders, + responseBodySize, + options.networkCaptureBodies ? hint.xhr.responseText : undefined, + ); + + return { + startTimestamp, + endTimestamp, + url, + method, + statusCode, + request, + response, + }; +} + +function getResponseHeaders(xhr) { + const headers = xhr.getAllResponseHeaders(); + + if (!headers) { + return {}; + } + + return headers.split('\r\n').reduce((acc, line) => { + const [key, value] = line.split(': '); + acc[key.toLowerCase()] = value; + return acc; + }, {}); +} + +/** + * This method does two things: + * - It enriches the regular XHR/fetch breadcrumbs with request/response size data + * - It captures the XHR/fetch breadcrumbs to the replay + * (enriching it with further data that is _not_ added to the regular breadcrumbs) + */ +function handleNetworkBreadcrumbs(replay) { + const client = getCurrentHub().getClient(); + + try { + const textEncoder = new TextEncoder(); + + const { networkDetailAllowUrls, networkCaptureBodies, networkRequestHeaders, networkResponseHeaders } = + replay.getOptions(); + + const options = { + replay, + textEncoder, + networkDetailAllowUrls, + networkCaptureBodies, + networkRequestHeaders, + networkResponseHeaders, + }; + + if (client && client.on) { + client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint)); + } else { + // Fallback behavior + addInstrumentationHandler('fetch', handleFetchSpanListener(replay)); + addInstrumentationHandler('xhr', handleXhrSpanListener(replay)); + } + } catch (e2) { + // Do nothing + } +} + +/** just exported for tests */ +function beforeAddNetworkBreadcrumb( + options, + breadcrumb, + hint, +) { + if (!breadcrumb.data) { + return; + } + + try { + if (_isXhrBreadcrumb(breadcrumb) && _isXhrHint(hint)) { + // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick + // Because the hook runs synchronously, and the breadcrumb is afterwards passed on + // So any async mutations to it will not be reflected in the final breadcrumb + enrichXhrBreadcrumb(breadcrumb, hint, options); + + void captureXhrBreadcrumbToReplay(breadcrumb, hint, options); + } + + if (_isFetchBreadcrumb(breadcrumb) && _isFetchHint(hint)) { + // This has to be sync, as we need to ensure the breadcrumb is enriched in the same tick + // Because the hook runs synchronously, and the breadcrumb is afterwards passed on + // So any async mutations to it will not be reflected in the final breadcrumb + enrichFetchBreadcrumb(breadcrumb, hint, options); + + void captureFetchBreadcrumbToReplay(breadcrumb, hint, options); + } + } catch (e) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Error when enriching network breadcrumb'); + } +} + +function _isXhrBreadcrumb(breadcrumb) { + return breadcrumb.category === 'xhr'; +} + +function _isFetchBreadcrumb(breadcrumb) { + return breadcrumb.category === 'fetch'; +} + +function _isXhrHint(hint) { + return hint && hint.xhr; +} + +function _isFetchHint(hint) { + return hint && hint.response; +} + +let _LAST_BREADCRUMB = null; + +function isBreadcrumbWithCategory(breadcrumb) { + return !!breadcrumb.category; +} + +const handleScopeListener = + (replay) => + (scope) => { + if (!replay.isEnabled()) { + return; + } + + const result = handleScope(scope); + + if (!result) { + return; + } + + addBreadcrumbEvent(replay, result); + }; + +/** + * An event handler to handle scope changes. + */ +function handleScope(scope) { + // TODO (v8): Remove this guard. This was put in place because we introduced + // Scope.getLastBreadcrumb mid-v7 which caused incompatibilities with older SDKs. + // For now, we'll just return null if the method doesn't exist but we should eventually + // get rid of this guard. + const newBreadcrumb = scope.getLastBreadcrumb && scope.getLastBreadcrumb(); + + // Listener can be called when breadcrumbs have not changed, so we store the + // reference to the last crumb and only return a crumb if it has changed + if (_LAST_BREADCRUMB === newBreadcrumb || !newBreadcrumb) { + return null; + } + + _LAST_BREADCRUMB = newBreadcrumb; + + if ( + !isBreadcrumbWithCategory(newBreadcrumb) || + ['fetch', 'xhr', 'sentry.event', 'sentry.transaction'].includes(newBreadcrumb.category) || + newBreadcrumb.category.startsWith('ui.') + ) { + return null; + } + + if (newBreadcrumb.category === 'console') { + return normalizeConsoleBreadcrumb(newBreadcrumb); + } + + return createBreadcrumb(newBreadcrumb); +} + +/** exported for tests only */ +function normalizeConsoleBreadcrumb( + breadcrumb, +) { + const args = breadcrumb.data && breadcrumb.data.arguments; + + if (!Array.isArray(args) || args.length === 0) { + return createBreadcrumb(breadcrumb); + } + + let isTruncated = false; + + // Avoid giant args captures + const normalizedArgs = args.map(arg => { + if (!arg) { + return arg; + } + if (typeof arg === 'string') { + if (arg.length > CONSOLE_ARG_MAX_SIZE) { + isTruncated = true; + return `${arg.slice(0, CONSOLE_ARG_MAX_SIZE)}…`; + } + + return arg; + } + if (typeof arg === 'object') { + try { + const normalizedArg = normalize(arg, 7); + const stringified = JSON.stringify(normalizedArg); + if (stringified.length > CONSOLE_ARG_MAX_SIZE) { + const fixedJson = fixJson(stringified.slice(0, CONSOLE_ARG_MAX_SIZE)); + const json = JSON.parse(fixedJson); + // We only set this after JSON.parse() was successfull, so we know we didn't run into `catch` + isTruncated = true; + return json; + } + return normalizedArg; + } catch (e) { + // fall back to default + } + } + + return arg; + }); + + return createBreadcrumb({ + ...breadcrumb, + data: { + ...breadcrumb.data, + arguments: normalizedArgs, + ...(isTruncated ? { _meta: { warnings: ['CONSOLE_ARG_TRUNCATED'] } } : {}), + }, + }); +} + +/** + * Add global listeners that cannot be removed. + */ +function addGlobalListeners(replay) { + // Listeners from core SDK // + const scope = getCurrentHub().getScope(); + const client = getCurrentHub().getClient(); + + if (scope) { + scope.addScopeListener(handleScopeListener(replay)); + } + addInstrumentationHandler('dom', handleDomListener(replay)); + addInstrumentationHandler('history', handleHistorySpanListener(replay)); + handleNetworkBreadcrumbs(replay); + + // Tag all (non replay) events that get sent to Sentry with the current + // replay ID so that we can reference them later in the UI + addGlobalEventProcessor(handleGlobalEventListener(replay, !hasHooks(client))); + + // If a custom client has no hooks yet, we continue to use the "old" implementation + if (hasHooks(client)) { + client.on('afterSendEvent', handleAfterSendEvent(replay)); + client.on('createDsc', (dsc) => { + const replayId = replay.getSessionId(); + // We do not want to set the DSC when in buffer mode, as that means the replay has not been sent (yet) + if (replayId && replay.isEnabled() && replay.recordingMode === 'session') { + dsc.replay_id = replayId; + } + }); + + client.on('startTransaction', transaction => { + replay.lastTransaction = transaction; + }); + + // We may be missing the initial startTransaction due to timing issues, + // so we capture it on finish again. + client.on('finishTransaction', transaction => { + replay.lastTransaction = transaction; + }); + } +} + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function hasHooks(client) { + return !!(client && client.on); +} + +/** + * Create a "span" for the total amount of memory being used by JS objects + * (including v8 internal objects). + */ +async function addMemoryEntry(replay) { + // window.performance.memory is a non-standard API and doesn't work on all browsers, so we try-catch this + try { + return Promise.all( + createPerformanceSpans(replay, [ + // @ts-ignore memory doesn't exist on type Performance as the API is non-standard (we check that it exists above) + createMemoryEntry(WINDOW.performance.memory), + ]), + ); + } catch (error) { + // Do nothing + return []; + } +} + +function createMemoryEntry(memoryEntry) { + const { jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize } = memoryEntry; + // we don't want to use `getAbsoluteTime` because it adds the event time to the + // time origin, so we get the current timestamp instead + const time = Date.now() / 1000; + return { + type: 'memory', + name: 'memory', + start: time, + end: time, + data: { + memory: { + jsHeapSizeLimit, + totalJSHeapSize, + usedJSHeapSize, + }, + }, + }; +} + +// Map entryType -> function to normalize data for event +// @ts-ignore TODO: entry type does not fit the create* functions entry type +const ENTRY_TYPES + + = { + // @ts-ignore TODO: entry type does not fit the create* functions entry type + resource: createResourceEntry, + paint: createPaintEntry, + // @ts-ignore TODO: entry type does not fit the create* functions entry type + navigation: createNavigationEntry, + // @ts-ignore TODO: entry type does not fit the create* functions entry type + ['largest-contentful-paint']: createLargestContentfulPaint, +}; + +/** + * Create replay performance entries from the browser performance entries. + */ +function createPerformanceEntries( + entries, +) { + return entries.map(createPerformanceEntry).filter(Boolean) ; +} + +function createPerformanceEntry(entry) { + if (ENTRY_TYPES[entry.entryType] === undefined) { + return null; + } + + return ENTRY_TYPES[entry.entryType](entry); +} + +function getAbsoluteTime(time) { + // browserPerformanceTimeOrigin can be undefined if `performance` or + // `performance.now` doesn't exist, but this is already checked by this integration + return ((browserPerformanceTimeOrigin || WINDOW.performance.timeOrigin) + time) / 1000; +} + +function createPaintEntry(entry) { + const { duration, entryType, name, startTime } = entry; + + const start = getAbsoluteTime(startTime); + return { + type: entryType, + name, + start, + end: start + duration, + data: undefined, + }; +} + +function createNavigationEntry(entry) { + const { + entryType, + name, + decodedBodySize, + duration, + domComplete, + encodedBodySize, + domContentLoadedEventStart, + domContentLoadedEventEnd, + domInteractive, + loadEventStart, + loadEventEnd, + redirectCount, + startTime, + transferSize, + type, + } = entry; + + // Ignore entries with no duration, they do not seem to be useful and cause dupes + if (duration === 0) { + return null; + } + + return { + type: `${entryType}.${type}`, + start: getAbsoluteTime(startTime), + end: getAbsoluteTime(domComplete), + name, + data: { + size: transferSize, + decodedBodySize, + encodedBodySize, + duration, + domInteractive, + domContentLoadedEventStart, + domContentLoadedEventEnd, + loadEventStart, + loadEventEnd, + domComplete, + redirectCount, + }, + }; +} + +function createResourceEntry( + entry, +) { + const { + entryType, + initiatorType, + name, + responseEnd, + startTime, + decodedBodySize, + encodedBodySize, + responseStatus, + transferSize, + } = entry; + + // Core SDK handles these + if (['fetch', 'xmlhttprequest'].includes(initiatorType)) { + return null; + } + + return { + type: `${entryType}.${initiatorType}`, + start: getAbsoluteTime(startTime), + end: getAbsoluteTime(responseEnd), + name, + data: { + size: transferSize, + statusCode: responseStatus, + decodedBodySize, + encodedBodySize, + }, + }; +} + +function createLargestContentfulPaint( + entry, +) { + const { entryType, startTime, size } = entry; + + let startTimeOrNavigationActivation = 0; + + if (WINDOW.performance) { + const navEntry = WINDOW.performance.getEntriesByType('navigation')[0] + +; + + // See https://github.com/GoogleChrome/web-vitals/blob/9f11c4c6578fb4c5ee6fa4e32b9d1d756475f135/src/lib/getActivationStart.ts#L21 + startTimeOrNavigationActivation = (navEntry && navEntry.activationStart) || 0; + } + + // value is in ms + const value = Math.max(startTime - startTimeOrNavigationActivation, 0); + // LCP doesn't have a "duration", it just happens at a single point in time. + // But the UI expects both, so use end (in seconds) for both timestamps. + const end = getAbsoluteTime(startTimeOrNavigationActivation) + value / 1000; + + return { + type: entryType, + name: entryType, + start: end, + end, + data: { + value, // LCP "duration" in ms + size, + // Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode') + // eslint-disable-next-line @typescript-eslint/no-explicit-any + nodeId: record.mirror.getId(entry.element ), + }, + }; +} + +/** + * Heavily simplified debounce function based on lodash.debounce. + * + * This function takes a callback function (@param fun) and delays its invocation + * by @param wait milliseconds. Optionally, a maxWait can be specified in @param options, + * which ensures that the callback is invoked at least once after the specified max. wait time. + * + * @param func the function whose invocation is to be debounced + * @param wait the minimum time until the function is invoked after it was called once + * @param options the options object, which can contain the `maxWait` property + * + * @returns the debounced version of the function, which needs to be called at least once to start the + * debouncing process. Subsequent calls will reset the debouncing timer and, in case @paramfunc + * was already invoked in the meantime, return @param func's return value. + * The debounced function has two additional properties: + * - `flush`: Invokes the debounced function immediately and returns its return value + * - `cancel`: Cancels the debouncing process and resets the debouncing timer + */ +function debounce(func, wait, options) { + let callbackReturnValue; + + let timerId; + let maxTimerId; + + const maxWait = options && options.maxWait ? Math.max(options.maxWait, wait) : 0; + + function invokeFunc() { + cancelTimers(); + callbackReturnValue = func(); + return callbackReturnValue; + } + + function cancelTimers() { + timerId !== undefined && clearTimeout(timerId); + maxTimerId !== undefined && clearTimeout(maxTimerId); + timerId = maxTimerId = undefined; + } + + function flush() { + if (timerId !== undefined || maxTimerId !== undefined) { + return invokeFunc(); + } + return callbackReturnValue; + } + + function debounced() { + if (timerId) { + clearTimeout(timerId); + } + timerId = setTimeout(invokeFunc, wait); + + if (maxWait && maxTimerId === undefined) { + maxTimerId = setTimeout(invokeFunc, maxWait); + } + + return callbackReturnValue; + } + + debounced.cancel = cancelTimers; + debounced.flush = flush; + return debounced; +} + +/** + * Handler for recording events. + * + * Adds to event buffer, and has varying flushing behaviors if the event was a checkout. + */ +function getHandleRecordingEmit(replay) { + let hadFirstEvent = false; + + return (event, _isCheckout) => { + // If this is false, it means session is expired, create and a new session and wait for checkout + if (!replay.checkAndHandleExpiredSession()) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('[Replay] Received replay event after session expired.'); + + return; + } + + // `_isCheckout` is only set when the checkout is due to `checkoutEveryNms` + // We also want to treat the first event as a checkout, so we handle this specifically here + const isCheckout = _isCheckout || !hadFirstEvent; + hadFirstEvent = true; + + // The handler returns `true` if we do not want to trigger debounced flush, `false` if we want to debounce flush. + replay.addUpdate(() => { + // The session is always started immediately on pageload/init, but for + // error-only replays, it should reflect the most recent checkout + // when an error occurs. Clear any state that happens before this current + // checkout. This needs to happen before `addEvent()` which updates state + // dependent on this reset. + if (replay.recordingMode === 'buffer' && isCheckout) { + replay.setInitialState(); + } + + // We need to clear existing events on a checkout, otherwise they are + // incremental event updates and should be appended + void addEvent(replay, event, isCheckout); + + // Different behavior for full snapshots (type=2), ignore other event types + // See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16 + if (!isCheckout) { + return false; + } + + // Additionally, create a meta event that will capture certain SDK settings. + // In order to handle buffer mode, this needs to either be done when we + // receive checkout events or at flush time. + // + // `isCheckout` is always true, but want to be explicit that it should + // only be added for checkouts + void addSettingsEvent(replay, isCheckout); + + // If there is a previousSessionId after a full snapshot occurs, then + // the replay session was started due to session expiration. The new session + // is started before triggering a new checkout and contains the id + // of the previous session. Do not immediately flush in this case + // to avoid capturing only the checkout and instead the replay will + // be captured if they perform any follow-up actions. + if (replay.session && replay.session.previousSessionId) { + return true; + } + + // When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer + // this should usually be the timestamp of the checkout event, but to be safe... + if (replay.recordingMode === 'buffer' && replay.session && replay.eventBuffer) { + const earliestEvent = replay.eventBuffer.getEarliestTimestamp(); + if (earliestEvent) { + replay.session.started = earliestEvent; + + if (replay.getOptions().stickySession) { + saveSession(replay.session); + } + } + } + + if (replay.recordingMode === 'session') { + // If the full snapshot is due to an initial load, we will not have + // a previous session ID. In this case, we want to buffer events + // for a set amount of time before flushing. This can help avoid + // capturing replays of users that immediately close the window. + void replay.flush(); + } + + return true; + }); + }; +} + +/** + * Exported for tests + */ +function createOptionsEvent(replay) { + const options = replay.getOptions(); + return { + type: EventType.Custom, + timestamp: Date.now(), + data: { + tag: 'options', + payload: { + sessionSampleRate: options.sessionSampleRate, + errorSampleRate: options.errorSampleRate, + useCompressionOption: options.useCompression, + blockAllMedia: options.blockAllMedia, + maskAllText: options.maskAllText, + maskAllInputs: options.maskAllInputs, + useCompression: replay.eventBuffer ? replay.eventBuffer.type === 'worker' : false, + networkDetailHasUrls: options.networkDetailAllowUrls.length > 0, + networkCaptureBodies: options.networkCaptureBodies, + networkRequestHasHeaders: options.networkRequestHeaders.length > 0, + networkResponseHasHeaders: options.networkResponseHeaders.length > 0, + }, + }, + }; +} + +/** + * Add a "meta" event that contains a simplified view on current configuration + * options. This should only be included on the first segment of a recording. + */ +function addSettingsEvent(replay, isCheckout) { + // Only need to add this event when sending the first segment + if (!isCheckout || !replay.session || replay.session.segmentId !== 0) { + return Promise.resolve(null); + } + + return addEvent(replay, createOptionsEvent(replay), false); +} + +/** + * Create a replay envelope ready to be sent. + * This includes both the replay event, as well as the recording data. + */ +function createReplayEnvelope( + replayEvent, + recordingData, + dsn, + tunnel, +) { + return createEnvelope( + createEventEnvelopeHeaders(replayEvent, getSdkMetadataForEnvelopeHeader(replayEvent), tunnel, dsn), + [ + [{ type: 'replay_event' }, replayEvent], + [ + { + type: 'replay_recording', + // If string then we need to encode to UTF8, otherwise will have + // wrong size. TextEncoder has similar browser support to + // MutationObserver, although it does not accept IE11. + length: + typeof recordingData === 'string' ? new TextEncoder().encode(recordingData).length : recordingData.length, + }, + recordingData, + ], + ], + ); +} + +/** + * Prepare the recording data ready to be sent. + */ +function prepareRecordingData({ + recordingData, + headers, +} + +) { + let payloadWithSequence; + + // XXX: newline is needed to separate sequence id from events + const replayHeaders = `${JSON.stringify(headers)} +`; + + if (typeof recordingData === 'string') { + payloadWithSequence = `${replayHeaders}${recordingData}`; + } else { + const enc = new TextEncoder(); + // XXX: newline is needed to separate sequence id from events + const sequence = enc.encode(replayHeaders); + // Merge the two Uint8Arrays + payloadWithSequence = new Uint8Array(sequence.length + recordingData.length); + payloadWithSequence.set(sequence); + payloadWithSequence.set(recordingData, sequence.length); + } + + return payloadWithSequence; +} + +/** + * Prepare a replay event & enrich it with the SDK metadata. + */ +async function prepareReplayEvent({ + client, + scope, + replayId: event_id, + event, +} + +) { + const integrations = + typeof client._integrations === 'object' && client._integrations !== null && !Array.isArray(client._integrations) + ? Object.keys(client._integrations) + : undefined; + const preparedEvent = (await prepareEvent( + client.getOptions(), + event, + { event_id, integrations }, + scope, + )) ; + + // If e.g. a global event processor returned null + if (!preparedEvent) { + return null; + } + + // This normally happens in browser client "_prepareEvent" + // but since we do not use this private method from the client, but rather the plain import + // we need to do this manually. + preparedEvent.platform = preparedEvent.platform || 'javascript'; + + // extract the SDK name because `client._prepareEvent` doesn't add it to the event + const metadata = client.getSdkMetadata && client.getSdkMetadata(); + const { name, version } = (metadata && metadata.sdk) || {}; + + preparedEvent.sdk = { + ...preparedEvent.sdk, + name: name || 'sentry.javascript.unknown', + version: version || '0.0.0', + }; + + return preparedEvent; +} + +/** + * Send replay attachment using `fetch()` + */ +async function sendReplayRequest({ + recordingData, + replayId, + segmentId: segment_id, + eventContext, + timestamp, + session, +}) { + const preparedRecordingData = prepareRecordingData({ + recordingData, + headers: { + segment_id, + }, + }); + + const { urls, errorIds, traceIds, initialTimestamp } = eventContext; + + const hub = getCurrentHub(); + const client = hub.getClient(); + const scope = hub.getScope(); + const transport = client && client.getTransport(); + const dsn = client && client.getDsn(); + + if (!client || !transport || !dsn || !session.sampled) { + return; + } + + const baseEvent = { + type: REPLAY_EVENT_NAME, + replay_start_timestamp: initialTimestamp / 1000, + timestamp: timestamp / 1000, + error_ids: errorIds, + trace_ids: traceIds, + urls, + replay_id: replayId, + segment_id, + replay_type: session.sampled, + }; + + const replayEvent = await prepareReplayEvent({ scope, client, replayId, event: baseEvent }); + + if (!replayEvent) { + // Taken from baseclient's `_processEvent` method, where this is handled for errors/transactions + client.recordDroppedEvent('event_processor', 'replay', baseEvent); + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('An event processor returned `null`, will not send event.'); + return; + } + + /* + For reference, the fully built event looks something like this: + { + "type": "replay_event", + "timestamp": 1670837008.634, + "error_ids": [ + "errorId" + ], + "trace_ids": [ + "traceId" + ], + "urls": [ + "https://example.com" + ], + "replay_id": "eventId", + "segment_id": 3, + "replay_type": "error", + "platform": "javascript", + "event_id": "eventId", + "environment": "production", + "sdk": { + "integrations": [ + "BrowserTracing", + "Replay" + ], + "name": "sentry.javascript.browser", + "version": "7.25.0" + }, + "sdkProcessingMetadata": {}, + "contexts": { + }, + } + */ + + const envelope = createReplayEnvelope(replayEvent, preparedRecordingData, dsn, client.getOptions().tunnel); + + let response; + + try { + response = await transport.send(envelope); + } catch (err) { + const error = new Error(UNABLE_TO_SEND_REPLAY); + + try { + // In case browsers don't allow this property to be writable + // @ts-ignore This needs lib es2022 and newer + error.cause = err; + } catch (e) { + // nothing to do + } + throw error; + } + + // TODO (v8): we can remove this guard once transport.send's type signature doesn't include void anymore + if (!response) { + return response; + } + + // If the status code is invalid, we want to immediately stop & not retry + if (typeof response.statusCode === 'number' && (response.statusCode < 200 || response.statusCode >= 300)) { + throw new TransportStatusCodeError(response.statusCode); + } + + return response; +} + +/** + * This error indicates that the transport returned an invalid status code. + */ +class TransportStatusCodeError extends Error { + constructor(statusCode) { + super(`Transport returned status code ${statusCode}`); + } +} + +/** + * Finalize and send the current replay event to Sentry + */ +async function sendReplay( + replayData, + retryConfig = { + count: 0, + interval: RETRY_BASE_INTERVAL, + }, +) { + const { recordingData, options } = replayData; + + // short circuit if there's no events to upload (this shouldn't happen as _runFlush makes this check) + if (!recordingData.length) { + return; + } + + try { + await sendReplayRequest(replayData); + return true; + } catch (err) { + if (err instanceof TransportStatusCodeError) { + throw err; + } + + // Capture error for every failed replay + setContext('Replays', { + _retryCount: retryConfig.count, + }); + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && options._experiments && options._experiments.captureExceptions) { + captureException(err); + } + + // If an error happened here, it's likely that uploading the attachment + // failed, we'll can retry with the same events payload + if (retryConfig.count >= RETRY_MAX_COUNT) { + const error = new Error(`${UNABLE_TO_SEND_REPLAY} - max retries exceeded`); + + try { + // In case browsers don't allow this property to be writable + // @ts-ignore This needs lib es2022 and newer + error.cause = err; + } catch (e) { + // nothing to do + } + + throw error; + } + + // will retry in intervals of 5, 10, 30 + retryConfig.interval *= ++retryConfig.count; + + return new Promise((resolve, reject) => { + setTimeout(async () => { + try { + await sendReplay(replayData, retryConfig); + resolve(true); + } catch (err) { + reject(err); + } + }, retryConfig.interval); + }); + } +} + +const THROTTLED = '__THROTTLED'; +const SKIPPED = '__SKIPPED'; + +/** + * Create a throttled function off a given function. + * When calling the throttled function, it will call the original function only + * if it hasn't been called more than `maxCount` times in the last `durationSeconds`. + * + * Returns `THROTTLED` if throttled for the first time, after that `SKIPPED`, + * or else the return value of the original function. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function throttle( + fn, + maxCount, + durationSeconds, +) { + const counter = new Map(); + + const _cleanup = (now) => { + const threshold = now - durationSeconds; + counter.forEach((_value, key) => { + if (key < threshold) { + counter.delete(key); + } + }); + }; + + const _getTotalCount = () => { + return [...counter.values()].reduce((a, b) => a + b, 0); + }; + + let isThrottled = false; + + return (...rest) => { + // Date in second-precision, which we use as basis for the throttling + const now = Math.floor(Date.now() / 1000); + + // First, make sure to delete any old entries + _cleanup(now); + + // If already over limit, do nothing + if (_getTotalCount() >= maxCount) { + const wasThrottled = isThrottled; + isThrottled = true; + return wasThrottled ? SKIPPED : THROTTLED; + } + + isThrottled = false; + const count = counter.get(now) || 0; + counter.set(now, count + 1); + + return fn(...rest); + }; +} + +/* eslint-disable max-lines */ // TODO: We might want to split this file up + +/** + * The main replay container class, which holds all the state and methods for recording and sending replays. + */ +class ReplayContainer { + __init() {this.eventBuffer = null;} + + /** + * List of PerformanceEntry from PerformanceObserver + */ + __init2() {this.performanceEvents = [];} + + /** + * Recording can happen in one of three modes: + * - session: Record the whole session, sending it continuously + * - buffer: Always keep the last 60s of recording, requires: + * - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs + * - or calling `flush()` to send the replay + */ + __init3() {this.recordingMode = 'session';} + + /** + * The current or last active transcation. + * This is only available when performance is enabled. + */ + + /** + * These are here so we can overwrite them in tests etc. + * @hidden + */ + __init4() {this.timeouts = { + sessionIdlePause: SESSION_IDLE_PAUSE_DURATION, + sessionIdleExpire: SESSION_IDLE_EXPIRE_DURATION, + maxSessionLife: MAX_SESSION_LIFE, + }; } + + /** + * Options to pass to `rrweb.record()` + */ + + __init5() {this._performanceObserver = null;} + + __init6() {this._flushLock = null;} + + /** + * Timestamp of the last user activity. This lives across sessions. + */ + __init7() {this._lastActivity = Date.now();} + + /** + * Is the integration currently active? + */ + __init8() {this._isEnabled = false;} + + /** + * Paused is a state where: + * - DOM Recording is not listening at all + * - Nothing will be added to event buffer (e.g. core SDK events) + */ + __init9() {this._isPaused = false;} + + /** + * Have we attached listeners to the core SDK? + * Note we have to track this as there is no way to remove instrumentation handlers. + */ + __init10() {this._hasInitializedCoreListeners = false;} + + /** + * Function to stop recording + */ + __init11() {this._stopRecording = null;} + + __init12() {this._context = { + errorIds: new Set(), + traceIds: new Set(), + urls: [], + initialTimestamp: Date.now(), + initialUrl: '', + };} + + constructor({ + options, + recordingOptions, + } + +) {ReplayContainer.prototype.__init.call(this);ReplayContainer.prototype.__init2.call(this);ReplayContainer.prototype.__init3.call(this);ReplayContainer.prototype.__init4.call(this);ReplayContainer.prototype.__init5.call(this);ReplayContainer.prototype.__init6.call(this);ReplayContainer.prototype.__init7.call(this);ReplayContainer.prototype.__init8.call(this);ReplayContainer.prototype.__init9.call(this);ReplayContainer.prototype.__init10.call(this);ReplayContainer.prototype.__init11.call(this);ReplayContainer.prototype.__init12.call(this);ReplayContainer.prototype.__init13.call(this);ReplayContainer.prototype.__init14.call(this);ReplayContainer.prototype.__init15.call(this);ReplayContainer.prototype.__init16.call(this);ReplayContainer.prototype.__init17.call(this);ReplayContainer.prototype.__init18.call(this); + this._recordingOptions = recordingOptions; + this._options = options; + + this._debouncedFlush = debounce(() => this._flush(), this._options.flushMinDelay, { + maxWait: this._options.flushMaxDelay, + }); + + this._throttledAddEvent = throttle( + (event, isCheckout) => addEvent(this, event, isCheckout), + // Max 300 events... + 300, + // ... per 5s + 5, + ); + + const { slowClickTimeout, slowClickIgnoreSelectors } = this.getOptions(); + + const slowClickConfig = slowClickTimeout + ? { + threshold: Math.min(SLOW_CLICK_THRESHOLD, slowClickTimeout), + timeout: slowClickTimeout, + scrollTimeout: SLOW_CLICK_SCROLL_TIMEOUT, + ignoreSelector: slowClickIgnoreSelectors ? slowClickIgnoreSelectors.join(',') : '', + multiClickTimeout: MULTI_CLICK_TIMEOUT, + } + : undefined; + + if (slowClickConfig) { + this.clickDetector = new ClickDetector(this, slowClickConfig); + } + } + + /** Get the event context. */ + getContext() { + return this._context; + } + + /** If recording is currently enabled. */ + isEnabled() { + return this._isEnabled; + } + + /** If recording is currently paused. */ + isPaused() { + return this._isPaused; + } + + /** Get the replay integration options. */ + getOptions() { + return this._options; + } + + /** + * Initializes the plugin based on sampling configuration. Should not be + * called outside of constructor. + */ + initializeSampling() { + const { errorSampleRate, sessionSampleRate } = this._options; + + // If neither sample rate is > 0, then do nothing - user will need to call one of + // `start()` or `startBuffering` themselves. + if (errorSampleRate <= 0 && sessionSampleRate <= 0) { + return; + } + + // Otherwise if there is _any_ sample rate set, try to load an existing + // session, or create a new one. + const isSessionSampled = this._loadAndCheckSession(); + + if (!isSessionSampled) { + // This should only occur if `errorSampleRate` is 0 and was unsampled for + // session-based replay. In this case there is nothing to do. + return; + } + + if (!this.session) { + // This should not happen, something wrong has occurred + this._handleException(new Error('Unable to initialize and create session')); + return; + } + + if (this.session.sampled && this.session.sampled !== 'session') { + // If not sampled as session-based, then recording mode will be `buffer` + // Note that we don't explicitly check if `sampled === 'buffer'` because we + // could have sessions from Session storage that are still `error` from + // prior SDK version. + this.recordingMode = 'buffer'; + } + + this._initializeRecording(); + } + + /** + * Start a replay regardless of sampling rate. Calling this will always + * create a new session. Will throw an error if replay is already in progress. + * + * Creates or loads a session, attaches listeners to varying events (DOM, + * _performanceObserver, Recording, Sentry SDK, etc) + */ + start() { + if (this._isEnabled && this.recordingMode === 'session') { + throw new Error('Replay recording is already in progress'); + } + + if (this._isEnabled && this.recordingMode === 'buffer') { + throw new Error('Replay buffering is in progress, call `flush()` to save the replay'); + } + + const previousSessionId = this.session && this.session.id; + + const { session } = getSession({ + timeouts: this.timeouts, + stickySession: Boolean(this._options.stickySession), + currentSession: this.session, + // This is intentional: create a new session-based replay when calling `start()` + sessionSampleRate: 1, + allowBuffering: false, + }); + + session.previousSessionId = previousSessionId; + this.session = session; + + this._initializeRecording(); + } + + /** + * Start replay buffering. Buffers until `flush()` is called or, if + * `replaysOnErrorSampleRate` > 0, an error occurs. + */ + startBuffering() { + if (this._isEnabled) { + throw new Error('Replay recording is already in progress'); + } + + const previousSessionId = this.session && this.session.id; + + const { session } = getSession({ + timeouts: this.timeouts, + stickySession: Boolean(this._options.stickySession), + currentSession: this.session, + sessionSampleRate: 0, + allowBuffering: true, + }); + + session.previousSessionId = previousSessionId; + this.session = session; + + this.recordingMode = 'buffer'; + this._initializeRecording(); + } + + /** + * Start recording. + * + * Note that this will cause a new DOM checkout + */ + startRecording() { + try { + this._stopRecording = record({ + ...this._recordingOptions, + // When running in error sampling mode, we need to overwrite `checkoutEveryNms` + // Without this, it would record forever, until an error happens, which we don't want + // instead, we'll always keep the last 60 seconds of replay before an error happened + ...(this.recordingMode === 'buffer' && { checkoutEveryNms: BUFFER_CHECKOUT_TIME }), + emit: getHandleRecordingEmit(this), + onMutation: this._onMutationHandler, + }); + } catch (err) { + this._handleException(err); + } + } + + /** + * Stops the recording, if it was running. + * + * Returns true if it was previously stopped, or is now stopped, + * otherwise false. + */ + stopRecording() { + try { + if (this._stopRecording) { + this._stopRecording(); + this._stopRecording = undefined; + } + + return true; + } catch (err) { + this._handleException(err); + return false; + } + } + + /** + * Currently, this needs to be manually called (e.g. for tests). Sentry SDK + * does not support a teardown + */ + async stop(reason) { + if (!this._isEnabled) { + return; + } + + try { + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + const msg = `[Replay] Stopping Replay${reason ? ` triggered by ${reason}` : ''}`; + + // When `traceInternals` is enabled, we want to log this to the console + // Else, use the regular debug output + // eslint-disable-next-line + const log = this.getOptions()._experiments.traceInternals ? console.warn : logger.log; + log(msg); + } + + // We can't move `_isEnabled` after awaiting a flush, otherwise we can + // enter into an infinite loop when `stop()` is called while flushing. + this._isEnabled = false; + this._removeListeners(); + this.stopRecording(); + + this._debouncedFlush.cancel(); + // See comment above re: `_isEnabled`, we "force" a flush, ignoring the + // `_isEnabled` state of the plugin since it was disabled above. + if (this.recordingMode === 'session') { + await this._flush({ force: true }); + } + + // After flush, destroy event buffer + this.eventBuffer && this.eventBuffer.destroy(); + this.eventBuffer = null; + + // Clear session from session storage, note this means if a new session + // is started after, it will not have `previousSessionId` + clearSession(this); + } catch (err) { + this._handleException(err); + } + } + + /** + * Pause some replay functionality. See comments for `_isPaused`. + * This differs from stop as this only stops DOM recording, it is + * not as thorough of a shutdown as `stop()`. + */ + pause() { + this._isPaused = true; + this.stopRecording(); + } + + /** + * Resumes recording, see notes for `pause(). + * + * Note that calling `startRecording()` here will cause a + * new DOM checkout.` + */ + resume() { + if (!this._loadAndCheckSession()) { + return; + } + + this._isPaused = false; + this.startRecording(); + } + + /** + * If not in "session" recording mode, flush event buffer which will create a new replay. + * Unless `continueRecording` is false, the replay will continue to record and + * behave as a "session"-based replay. + * + * Otherwise, queue up a flush. + */ + async sendBufferedReplayOrFlush({ continueRecording = true } = {}) { + if (this.recordingMode === 'session') { + return this.flushImmediate(); + } + + const activityTime = Date.now(); + + // Allow flush to complete before resuming as a session recording, otherwise + // the checkout from `startRecording` may be included in the payload. + // Prefer to keep the error replay as a separate (and smaller) segment + // than the session replay. + await this.flushImmediate(); + + const hasStoppedRecording = this.stopRecording(); + + if (!continueRecording || !hasStoppedRecording) { + return; + } + + // Re-start recording, but in "session" recording mode + + // Reset all "capture on error" configuration before + // starting a new recording + this.recordingMode = 'session'; + + // Once this session ends, we do not want to refresh it + if (this.session) { + this.session.shouldRefresh = false; + + // It's possible that the session lifespan is > max session lifespan + // because we have been buffering beyond max session lifespan (we ignore + // expiration given that `shouldRefresh` is true). Since we flip + // `shouldRefresh`, the session could be considered expired due to + // lifespan, which is not what we want. Update session start date to be + // the current timestamp, so that session is not considered to be + // expired. This means that max replay duration can be MAX_SESSION_LIFE + + // (length of buffer), which we are ok with. + this._updateUserActivity(activityTime); + this._updateSessionActivity(activityTime); + this.session.started = activityTime; + this._maybeSaveSession(); + } + + this.startRecording(); + } + + /** + * We want to batch uploads of replay events. Save events only if + * `<flushMinDelay>` milliseconds have elapsed since the last event + * *OR* if `<flushMaxDelay>` milliseconds have elapsed. + * + * Accepts a callback to perform side-effects and returns true to stop batch + * processing and hand back control to caller. + */ + addUpdate(cb) { + // We need to always run `cb` (e.g. in the case of `this.recordingMode == 'buffer'`) + const cbResult = cb(); + + // If this option is turned on then we will only want to call `flush` + // explicitly + if (this.recordingMode === 'buffer') { + return; + } + + // If callback is true, we do not want to continue with flushing -- the + // caller will need to handle it. + if (cbResult === true) { + return; + } + + // addUpdate is called quite frequently - use _debouncedFlush so that it + // respects the flush delays and does not flush immediately + this._debouncedFlush(); + } + + /** + * Updates the user activity timestamp and resumes recording. This should be + * called in an event handler for a user action that we consider as the user + * being "active" (e.g. a mouse click). + */ + triggerUserActivity() { + this._updateUserActivity(); + + // This case means that recording was once stopped due to inactivity. + // Ensure that recording is resumed. + if (!this._stopRecording) { + // Create a new session, otherwise when the user action is flushed, it + // will get rejected due to an expired session. + if (!this._loadAndCheckSession()) { + return; + } + + // Note: This will cause a new DOM checkout + this.resume(); + return; + } + + // Otherwise... recording was never suspended, continue as normalish + this.checkAndHandleExpiredSession(); + + this._updateSessionActivity(); + } + + /** + * Updates the user activity timestamp *without* resuming + * recording. Some user events (e.g. keydown) can be create + * low-value replays that only contain the keypress as a + * breadcrumb. Instead this would require other events to + * create a new replay after a session has expired. + */ + updateUserActivity() { + this._updateUserActivity(); + this._updateSessionActivity(); + } + + /** + * Only flush if `this.recordingMode === 'session'` + */ + conditionalFlush() { + if (this.recordingMode === 'buffer') { + return Promise.resolve(); + } + + return this.flushImmediate(); + } + + /** + * Flush using debounce flush + */ + flush() { + return this._debouncedFlush() ; + } + + /** + * Always flush via `_debouncedFlush` so that we do not have flushes triggered + * from calling both `flush` and `_debouncedFlush`. Otherwise, there could be + * cases of mulitple flushes happening closely together. + */ + flushImmediate() { + this._debouncedFlush(); + // `.flush` is provided by the debounced function, analogously to lodash.debounce + return this._debouncedFlush.flush() ; + } + + /** + * Cancels queued up flushes. + */ + cancelFlush() { + this._debouncedFlush.cancel(); + } + + /** Get the current sesion (=replay) ID */ + getSessionId() { + return this.session && this.session.id; + } + + /** + * Checks if recording should be stopped due to user inactivity. Otherwise + * check if session is expired and create a new session if so. Triggers a new + * full snapshot on new session. + * + * Returns true if session is not expired, false otherwise. + * @hidden + */ + checkAndHandleExpiredSession() { + const oldSessionId = this.getSessionId(); + + // Prevent starting a new session if the last user activity is older than + // SESSION_IDLE_PAUSE_DURATION. Otherwise non-user activity can trigger a new + // session+recording. This creates noisy replays that do not have much + // content in them. + if ( + this._lastActivity && + isExpired(this._lastActivity, this.timeouts.sessionIdlePause) && + this.session && + this.session.sampled === 'session' + ) { + // Pause recording only for session-based replays. Otherwise, resuming + // will create a new replay and will conflict with users who only choose + // to record error-based replays only. (e.g. the resumed replay will not + // contain a reference to an error) + this.pause(); + return; + } + + // --- There is recent user activity --- // + // This will create a new session if expired, based on expiry length + if (!this._loadAndCheckSession()) { + return; + } + + // Session was expired if session ids do not match + const expired = oldSessionId !== this.getSessionId(); + + if (!expired) { + return true; + } + + // Session is expired, trigger a full snapshot (which will create a new session) + this._triggerFullSnapshot(); + + return false; + } + + /** + * Capture some initial state that can change throughout the lifespan of the + * replay. This is required because otherwise they would be captured at the + * first flush. + */ + setInitialState() { + const urlPath = `${WINDOW.location.pathname}${WINDOW.location.hash}${WINDOW.location.search}`; + const url = `${WINDOW.location.origin}${urlPath}`; + + this.performanceEvents = []; + + // Reset _context as well + this._clearContext(); + + this._context.initialUrl = url; + this._context.initialTimestamp = Date.now(); + this._context.urls.push(url); + } + + /** + * Add a breadcrumb event, that may be throttled. + * If it was throttled, we add a custom breadcrumb to indicate that. + */ + throttledAddEvent( + event, + isCheckout, + ) { + const res = this._throttledAddEvent(event, isCheckout); + + // If this is THROTTLED, it means we have throttled the event for the first time + // In this case, we want to add a breadcrumb indicating that something was skipped + if (res === THROTTLED) { + const breadcrumb = createBreadcrumb({ + category: 'replay.throttled', + }); + + this.addUpdate(() => { + void addEvent(this, { + type: EventType.Custom, + timestamp: breadcrumb.timestamp || 0, + data: { + tag: 'breadcrumb', + payload: breadcrumb, + metric: true, + }, + }); + }); + } + + return res; + } + + /** + * This will get the parametrized route name of the current page. + * This is only available if performance is enabled, and if an instrumented router is used. + */ + getCurrentRoute() { + const lastTransaction = this.lastTransaction || getCurrentHub().getScope().getTransaction(); + if (!lastTransaction || !['route', 'custom'].includes(lastTransaction.metadata.source)) { + return undefined; + } + + return lastTransaction.name; + } + + /** + * Initialize and start all listeners to varying events (DOM, + * Performance Observer, Recording, Sentry SDK, etc) + */ + _initializeRecording() { + this.setInitialState(); + + // this method is generally called on page load or manually - in both cases + // we should treat it as an activity + this._updateSessionActivity(); + + this.eventBuffer = createEventBuffer({ + useCompression: this._options.useCompression, + }); + + this._removeListeners(); + this._addListeners(); + + // Need to set as enabled before we start recording, as `record()` can trigger a flush with a new checkout + this._isEnabled = true; + + this.startRecording(); + } + + /** A wrapper to conditionally capture exceptions. */ + _handleException(error) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay]', error); + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && this._options._experiments && this._options._experiments.captureExceptions) { + captureException(error); + } + } + + /** + * Loads (or refreshes) the current session. + * Returns false if session is not recorded. + */ + _loadAndCheckSession() { + const { type, session } = getSession({ + timeouts: this.timeouts, + stickySession: Boolean(this._options.stickySession), + currentSession: this.session, + sessionSampleRate: this._options.sessionSampleRate, + allowBuffering: this._options.errorSampleRate > 0 || this.recordingMode === 'buffer', + }); + + // If session was newly created (i.e. was not loaded from storage), then + // enable flag to create the root replay + if (type === 'new') { + this.setInitialState(); + } + + const currentSessionId = this.getSessionId(); + if (session.id !== currentSessionId) { + session.previousSessionId = currentSessionId; + } + + this.session = session; + + if (!this.session.sampled) { + void this.stop('session unsampled'); + return false; + } + + return true; + } + + /** + * Adds listeners to record events for the replay + */ + _addListeners() { + try { + WINDOW.document.addEventListener('visibilitychange', this._handleVisibilityChange); + WINDOW.addEventListener('blur', this._handleWindowBlur); + WINDOW.addEventListener('focus', this._handleWindowFocus); + WINDOW.addEventListener('keydown', this._handleKeyboardEvent); + + if (this.clickDetector) { + this.clickDetector.addListeners(); + } + + // There is no way to remove these listeners, so ensure they are only added once + if (!this._hasInitializedCoreListeners) { + addGlobalListeners(this); + + this._hasInitializedCoreListeners = true; + } + } catch (err) { + this._handleException(err); + } + + // PerformanceObserver // + if (!('PerformanceObserver' in WINDOW)) { + return; + } + + this._performanceObserver = setupPerformanceObserver(this); + } + + /** + * Cleans up listeners that were created in `_addListeners` + */ + _removeListeners() { + try { + WINDOW.document.removeEventListener('visibilitychange', this._handleVisibilityChange); + + WINDOW.removeEventListener('blur', this._handleWindowBlur); + WINDOW.removeEventListener('focus', this._handleWindowFocus); + WINDOW.removeEventListener('keydown', this._handleKeyboardEvent); + + if (this.clickDetector) { + this.clickDetector.removeListeners(); + } + + if (this._performanceObserver) { + this._performanceObserver.disconnect(); + this._performanceObserver = null; + } + } catch (err) { + this._handleException(err); + } + } + + /** + * Handle when visibility of the page content changes. Opening a new tab will + * cause the state to change to hidden because of content of current page will + * be hidden. Likewise, moving a different window to cover the contents of the + * page will also trigger a change to a hidden state. + */ + __init13() {this._handleVisibilityChange = () => { + if (WINDOW.document.visibilityState === 'visible') { + this._doChangeToForegroundTasks(); + } else { + this._doChangeToBackgroundTasks(); + } + };} + + /** + * Handle when page is blurred + */ + __init14() {this._handleWindowBlur = () => { + const breadcrumb = createBreadcrumb({ + category: 'ui.blur', + }); + + // Do not count blur as a user action -- it's part of the process of them + // leaving the page + this._doChangeToBackgroundTasks(breadcrumb); + };} + + /** + * Handle when page is focused + */ + __init15() {this._handleWindowFocus = () => { + const breadcrumb = createBreadcrumb({ + category: 'ui.focus', + }); + + // Do not count focus as a user action -- instead wait until they focus and + // interactive with page + this._doChangeToForegroundTasks(breadcrumb); + };} + + /** Ensure page remains active when a key is pressed. */ + __init16() {this._handleKeyboardEvent = (event) => { + handleKeyboardEvent(this, event); + };} + + /** + * Tasks to run when we consider a page to be hidden (via blurring and/or visibility) + */ + _doChangeToBackgroundTasks(breadcrumb) { + if (!this.session) { + return; + } + + const expired = isSessionExpired(this.session, this.timeouts); + + if (breadcrumb && !expired) { + this._createCustomBreadcrumb(breadcrumb); + } + + // Send replay when the page/tab becomes hidden. There is no reason to send + // replay if it becomes visible, since no actions we care about were done + // while it was hidden + void this.conditionalFlush(); + } + + /** + * Tasks to run when we consider a page to be visible (via focus and/or visibility) + */ + _doChangeToForegroundTasks(breadcrumb) { + if (!this.session) { + return; + } + + const isSessionActive = this.checkAndHandleExpiredSession(); + + if (!isSessionActive) { + // If the user has come back to the page within SESSION_IDLE_PAUSE_DURATION + // ms, we will re-use the existing session, otherwise create a new + // session + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Document has become active, but session has expired'); + return; + } + + if (breadcrumb) { + this._createCustomBreadcrumb(breadcrumb); + } + } + + /** + * Trigger rrweb to take a full snapshot which will cause this plugin to + * create a new Replay event. + */ + _triggerFullSnapshot(checkout = true) { + try { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('[Replay] Taking full rrweb snapshot'); + record.takeFullSnapshot(checkout); + } catch (err) { + this._handleException(err); + } + } + + /** + * Update user activity (across session lifespans) + */ + _updateUserActivity(_lastActivity = Date.now()) { + this._lastActivity = _lastActivity; + } + + /** + * Updates the session's last activity timestamp + */ + _updateSessionActivity(_lastActivity = Date.now()) { + if (this.session) { + this.session.lastActivity = _lastActivity; + this._maybeSaveSession(); + } + } + + /** + * Helper to create (and buffer) a replay breadcrumb from a core SDK breadcrumb + */ + _createCustomBreadcrumb(breadcrumb) { + this.addUpdate(() => { + void this.throttledAddEvent({ + type: EventType.Custom, + timestamp: breadcrumb.timestamp || 0, + data: { + tag: 'breadcrumb', + payload: breadcrumb, + }, + }); + }); + } + + /** + * Observed performance events are added to `this.performanceEvents`. These + * are included in the replay event before it is finished and sent to Sentry. + */ + _addPerformanceEntries() { + // Copy and reset entries before processing + const entries = [...this.performanceEvents]; + this.performanceEvents = []; + + return Promise.all(createPerformanceSpans(this, createPerformanceEntries(entries))); + } + + /** + * Clear _context + */ + _clearContext() { + // XXX: `initialTimestamp` and `initialUrl` do not get cleared + this._context.errorIds.clear(); + this._context.traceIds.clear(); + this._context.urls = []; + } + + /** Update the initial timestamp based on the buffer content. */ + _updateInitialTimestampFromEventBuffer() { + const { session, eventBuffer } = this; + if (!session || !eventBuffer) { + return; + } + + // we only ever update this on the initial segment + if (session.segmentId) { + return; + } + + const earliestEvent = eventBuffer.getEarliestTimestamp(); + if (earliestEvent && earliestEvent < this._context.initialTimestamp) { + this._context.initialTimestamp = earliestEvent; + } + } + + /** + * Return and clear _context + */ + _popEventContext() { + const _context = { + initialTimestamp: this._context.initialTimestamp, + initialUrl: this._context.initialUrl, + errorIds: Array.from(this._context.errorIds), + traceIds: Array.from(this._context.traceIds), + urls: this._context.urls, + }; + + this._clearContext(); + + return _context; + } + + /** + * Flushes replay event buffer to Sentry. + * + * Performance events are only added right before flushing - this is + * due to the buffered performance observer events. + * + * Should never be called directly, only by `flush` + */ + async _runFlush() { + if (!this.session || !this.eventBuffer) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] No session or eventBuffer found to flush.'); + return; + } + + await this._addPerformanceEntries(); + + // Check eventBuffer again, as it could have been stopped in the meanwhile + if (!this.eventBuffer || !this.eventBuffer.hasEvents) { + return; + } + + // Only attach memory event if eventBuffer is not empty + await addMemoryEntry(this); + + // Check eventBuffer again, as it could have been stopped in the meanwhile + if (!this.eventBuffer) { + return; + } + + try { + // This uses the data from the eventBuffer, so we need to call this before `finish() + this._updateInitialTimestampFromEventBuffer(); + + // Note this empties the event buffer regardless of outcome of sending replay + const recordingData = await this.eventBuffer.finish(); + + // NOTE: Copy values from instance members, as it's possible they could + // change before the flush finishes. + const replayId = this.session.id; + const eventContext = this._popEventContext(); + // Always increment segmentId regardless of outcome of sending replay + const segmentId = this.session.segmentId++; + this._maybeSaveSession(); + + await sendReplay({ + replayId, + recordingData, + segmentId, + eventContext, + session: this.session, + options: this.getOptions(), + timestamp: Date.now(), + }); + } catch (err) { + this._handleException(err); + + // This means we retried 3 times and all of them failed, + // or we ran into a problem we don't want to retry, like rate limiting. + // In this case, we want to completely stop the replay - otherwise, we may get inconsistent segments + void this.stop('sendReplay'); + + const client = getCurrentHub().getClient(); + + if (client) { + client.recordDroppedEvent('send_error', 'replay'); + } + } + } + + /** + * Flush recording data to Sentry. Creates a lock so that only a single flush + * can be active at a time. Do not call this directly. + */ + __init17() {this._flush = async ({ + force = false, + } + + = {}) => { + if (!this._isEnabled && !force) { + // This can happen if e.g. the replay was stopped because of exceeding the retry limit + return; + } + + if (!this.checkAndHandleExpiredSession()) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] Attempting to finish replay event after session expired.'); + return; + } + + if (!this.session) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('[Replay] No session found to flush.'); + return; + } + + // A flush is about to happen, cancel any queued flushes + this._debouncedFlush.cancel(); + + // this._flushLock acts as a lock so that future calls to `_flush()` + // will be blocked until this promise resolves + if (!this._flushLock) { + this._flushLock = this._runFlush(); + await this._flushLock; + this._flushLock = null; + return; + } + + // Wait for previous flush to finish, then call the debounced `_flush()`. + // It's possible there are other flush requests queued and waiting for it + // to resolve. We want to reduce all outstanding requests (as well as any + // new flush requests that occur within a second of the locked flush + // completing) into a single flush. + + try { + await this._flushLock; + } catch (err) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(err); + } finally { + this._debouncedFlush(); + } + };} + + /** Save the session, if it is sticky */ + _maybeSaveSession() { + if (this.session && this._options.stickySession) { + saveSession(this.session); + } + } + + /** Handler for rrweb.record.onMutation */ + __init18() {this._onMutationHandler = (mutations) => { + const count = mutations.length; + + const mutationLimit = this._options.mutationLimit; + const mutationBreadcrumbLimit = this._options.mutationBreadcrumbLimit; + const overMutationLimit = mutationLimit && count > mutationLimit; + + // Create a breadcrumb if a lot of mutations happen at the same time + // We can show this in the UI as an information with potential performance improvements + if (count > mutationBreadcrumbLimit || overMutationLimit) { + const breadcrumb = createBreadcrumb({ + category: 'replay.mutations', + data: { + count, + limit: overMutationLimit, + }, + }); + this._createCustomBreadcrumb(breadcrumb); + } + + // Stop replay if over the mutation limit + if (overMutationLimit) { + void this.stop('mutationLimit'); + return false; + } + + // `true` means we use the regular mutation handling by rrweb + return true; + };} +} + +function getOption( + selectors, + defaultSelectors, + deprecatedClassOption, + deprecatedSelectorOption, +) { + const deprecatedSelectors = typeof deprecatedSelectorOption === 'string' ? deprecatedSelectorOption.split(',') : []; + + const allSelectors = [ + ...selectors, + // @deprecated + ...deprecatedSelectors, + + // sentry defaults + ...defaultSelectors, + ]; + + // @deprecated + if (typeof deprecatedClassOption !== 'undefined') { + // NOTE: No support for RegExp + if (typeof deprecatedClassOption === 'string') { + allSelectors.push(`.${deprecatedClassOption}`); + } + + // eslint-disable-next-line no-console + console.warn( + '[Replay] You are using a deprecated configuration item for privacy. Read the documentation on how to use the new privacy configuration.', + ); + } + + return allSelectors.join(','); +} + +/** + * Returns privacy related configuration for use in rrweb + */ +function getPrivacyOptions({ + mask, + unmask, + block, + unblock, + ignore, + + // eslint-disable-next-line deprecation/deprecation + blockClass, + // eslint-disable-next-line deprecation/deprecation + blockSelector, + // eslint-disable-next-line deprecation/deprecation + maskTextClass, + // eslint-disable-next-line deprecation/deprecation + maskTextSelector, + // eslint-disable-next-line deprecation/deprecation + ignoreClass, +}) { + const defaultBlockedElements = ['base[href="/"]']; + + const maskSelector = getOption(mask, ['.sentry-mask', '[data-sentry-mask]'], maskTextClass, maskTextSelector); + const unmaskSelector = getOption(unmask, ['.sentry-unmask', '[data-sentry-unmask]']); + + const options = { + // We are making the decision to make text and input selectors the same + maskTextSelector: maskSelector, + unmaskTextSelector: unmaskSelector, + maskInputSelector: maskSelector, + unmaskInputSelector: unmaskSelector, + + blockSelector: getOption( + block, + ['.sentry-block', '[data-sentry-block]', ...defaultBlockedElements], + blockClass, + blockSelector, + ), + unblockSelector: getOption(unblock, ['.sentry-unblock', '[data-sentry-unblock]']), + ignoreSelector: getOption(ignore, ['.sentry-ignore', '[data-sentry-ignore]', 'input[type="file"]'], ignoreClass), + }; + + if (blockClass instanceof RegExp) { + options.blockClass = blockClass; + } + + if (maskTextClass instanceof RegExp) { + options.maskTextClass = maskTextClass; + } + + return options; +} + +/** + * Returns true if we are in the browser. + */ +function isBrowser() { + // eslint-disable-next-line no-restricted-globals + return typeof window !== 'undefined' && (!isNodeEnv() || isElectronNodeRenderer()); +} + +// Electron renderers with nodeIntegration enabled are detected as Node.js so we specifically test for them +function isElectronNodeRenderer() { + return typeof process !== 'undefined' && (process ).type === 'renderer'; +} + +const MEDIA_SELECTORS = + 'img,image,svg,video,object,picture,embed,map,audio,link[rel="icon"],link[rel="apple-touch-icon"]'; + +const DEFAULT_NETWORK_HEADERS = ['content-length', 'content-type', 'accept']; + +let _initialized = false; + +/** + * The main replay integration class, to be passed to `init({ integrations: [] })`. + */ +class Replay { + /** + * @inheritDoc + */ + static __initStatic() {this.id = 'Replay';} + + /** + * @inheritDoc + */ + __init() {this.name = Replay.id;} + + /** + * Options to pass to `rrweb.record()` + */ + + /** + * Initial options passed to the replay integration, merged with default values. + * Note: `sessionSampleRate` and `errorSampleRate` are not required here, as they + * can only be finally set when setupOnce() is called. + * + * @private + */ + + constructor({ + flushMinDelay = DEFAULT_FLUSH_MIN_DELAY, + flushMaxDelay = DEFAULT_FLUSH_MAX_DELAY, + stickySession = true, + useCompression = true, + _experiments = {}, + sessionSampleRate, + errorSampleRate, + maskAllText = true, + maskAllInputs = true, + blockAllMedia = true, + + mutationBreadcrumbLimit = 750, + mutationLimit = 10000, + + slowClickTimeout = 7000, + slowClickIgnoreSelectors = [], + + networkDetailAllowUrls = [], + networkCaptureBodies = true, + networkRequestHeaders = [], + networkResponseHeaders = [], + + mask = [], + unmask = [], + block = [], + unblock = [], + ignore = [], + maskFn, + + beforeAddRecordingEvent, + + // eslint-disable-next-line deprecation/deprecation + blockClass, + // eslint-disable-next-line deprecation/deprecation + blockSelector, + // eslint-disable-next-line deprecation/deprecation + maskInputOptions, + // eslint-disable-next-line deprecation/deprecation + maskTextClass, + // eslint-disable-next-line deprecation/deprecation + maskTextSelector, + // eslint-disable-next-line deprecation/deprecation + ignoreClass, + } = {}) {Replay.prototype.__init.call(this); + this._recordingOptions = { + maskAllInputs, + maskAllText, + maskInputOptions: { ...(maskInputOptions || {}), password: true }, + maskTextFn: maskFn, + maskInputFn: maskFn, + + ...getPrivacyOptions({ + mask, + unmask, + block, + unblock, + ignore, + blockClass, + blockSelector, + maskTextClass, + maskTextSelector, + ignoreClass, + }), + + // Our defaults + slimDOMOptions: 'all', + inlineStylesheet: true, + // Disable inline images as it will increase segment/replay size + inlineImages: false, + // collect fonts, but be aware that `sentry.io` needs to be an allowed + // origin for playback + collectFonts: true, + }; + + this._initialOptions = { + flushMinDelay, + flushMaxDelay, + stickySession, + sessionSampleRate, + errorSampleRate, + useCompression, + blockAllMedia, + maskAllInputs, + maskAllText, + mutationBreadcrumbLimit, + mutationLimit, + slowClickTimeout, + slowClickIgnoreSelectors, + networkDetailAllowUrls, + networkCaptureBodies, + networkRequestHeaders: _getMergedNetworkHeaders(networkRequestHeaders), + networkResponseHeaders: _getMergedNetworkHeaders(networkResponseHeaders), + beforeAddRecordingEvent, + + _experiments, + }; + + if (typeof sessionSampleRate === 'number') { + // eslint-disable-next-line + console.warn( + `[Replay] You are passing \`sessionSampleRate\` to the Replay integration. +This option is deprecated and will be removed soon. +Instead, configure \`replaysSessionSampleRate\` directly in the SDK init options, e.g.: +Sentry.init({ replaysSessionSampleRate: ${sessionSampleRate} })`, + ); + + this._initialOptions.sessionSampleRate = sessionSampleRate; + } + + if (typeof errorSampleRate === 'number') { + // eslint-disable-next-line + console.warn( + `[Replay] You are passing \`errorSampleRate\` to the Replay integration. +This option is deprecated and will be removed soon. +Instead, configure \`replaysOnErrorSampleRate\` directly in the SDK init options, e.g.: +Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, + ); + + this._initialOptions.errorSampleRate = errorSampleRate; + } + + if (this._initialOptions.blockAllMedia) { + // `blockAllMedia` is a more user friendly option to configure blocking + // embedded media elements + this._recordingOptions.blockSelector = !this._recordingOptions.blockSelector + ? MEDIA_SELECTORS + : `${this._recordingOptions.blockSelector},${MEDIA_SELECTORS}`; + } + + if (this._isInitialized && isBrowser()) { + throw new Error('Multiple Sentry Session Replay instances are not supported'); + } + + this._isInitialized = true; + } + + /** If replay has already been initialized */ + get _isInitialized() { + return _initialized; + } + + /** Update _isInitialized */ + set _isInitialized(value) { + _initialized = value; + } + + /** + * Setup and initialize replay container + */ + setupOnce() { + if (!isBrowser()) { + return; + } + + this._setup(); + + // Once upon a time, we tried to create a transaction in `setupOnce` and it would + // potentially create a transaction before some native SDK integrations have run + // and applied their own global event processor. An example is: + // https://github.com/getsentry/sentry-javascript/blob/b47ceafbdac7f8b99093ce6023726ad4687edc48/packages/browser/src/integrations/useragent.ts + // + // So we call `this._initialize()` in next event loop as a workaround to wait for other + // global event processors to finish. This is no longer needed, but keeping it + // here to avoid any future issues. + setTimeout(() => this._initialize()); + } + + /** + * Start a replay regardless of sampling rate. Calling this will always + * create a new session. Will throw an error if replay is already in progress. + * + * Creates or loads a session, attaches listeners to varying events (DOM, + * PerformanceObserver, Recording, Sentry SDK, etc) + */ + start() { + if (!this._replay) { + return; + } + + this._replay.start(); + } + + /** + * Start replay buffering. Buffers until `flush()` is called or, if + * `replaysOnErrorSampleRate` > 0, until an error occurs. + */ + startBuffering() { + if (!this._replay) { + return; + } + + this._replay.startBuffering(); + } + + /** + * Currently, this needs to be manually called (e.g. for tests). Sentry SDK + * does not support a teardown + */ + stop() { + if (!this._replay) { + return Promise.resolve(); + } + + return this._replay.stop(); + } + + /** + * If not in "session" recording mode, flush event buffer which will create a new replay. + * Unless `continueRecording` is false, the replay will continue to record and + * behave as a "session"-based replay. + * + * Otherwise, queue up a flush. + */ + flush(options) { + if (!this._replay || !this._replay.isEnabled()) { + return Promise.resolve(); + } + + return this._replay.sendBufferedReplayOrFlush(options); + } + + /** + * Get the current session ID. + */ + getReplayId() { + if (!this._replay || !this._replay.isEnabled()) { + return; + } + + return this._replay.getSessionId(); + } + /** + * Initializes replay. + */ + _initialize() { + if (!this._replay) { + return; + } + + this._replay.initializeSampling(); + } + + /** Setup the integration. */ + _setup() { + // Client is not available in constructor, so we need to wait until setupOnce + const finalOptions = loadReplayOptionsFromClient(this._initialOptions); + + this._replay = new ReplayContainer({ + options: finalOptions, + recordingOptions: this._recordingOptions, + }); + } +} Replay.__initStatic(); + +/** Parse Replay-related options from SDK options */ +function loadReplayOptionsFromClient(initialOptions) { + const client = getCurrentHub().getClient(); + const opt = client && (client.getOptions() ); + + const finalOptions = { sessionSampleRate: 0, errorSampleRate: 0, ...dropUndefinedKeys(initialOptions) }; + + if (!opt) { + // eslint-disable-next-line no-console + console.warn('SDK client is not available.'); + return finalOptions; + } + + if ( + initialOptions.sessionSampleRate == null && // TODO remove once deprecated rates are removed + initialOptions.errorSampleRate == null && // TODO remove once deprecated rates are removed + opt.replaysSessionSampleRate == null && + opt.replaysOnErrorSampleRate == null + ) { + // eslint-disable-next-line no-console + console.warn( + 'Replay is disabled because neither `replaysSessionSampleRate` nor `replaysOnErrorSampleRate` are set.', + ); + } + + if (typeof opt.replaysSessionSampleRate === 'number') { + finalOptions.sessionSampleRate = opt.replaysSessionSampleRate; + } + + if (typeof opt.replaysOnErrorSampleRate === 'number') { + finalOptions.errorSampleRate = opt.replaysOnErrorSampleRate; + } + + return finalOptions; +} + +function _getMergedNetworkHeaders(headers) { + return [...DEFAULT_NETWORK_HEADERS, ...headers.map(header => header.toLowerCase())]; +} + +export { Replay }; +//# sourceMappingURL=index.js.map diff --git a/shared/logger/node_modules/@sentry/types/esm/severity.js b/shared/logger/node_modules/@sentry/types/esm/severity.js new file mode 100644 index 0000000..ae90303 --- /dev/null +++ b/shared/logger/node_modules/@sentry/types/esm/severity.js @@ -0,0 +1,24 @@ +/** + * TODO(v7): Remove this enum and replace with SeverityLevel + */ +export var Severity; +(function (Severity) { + /** JSDoc */ + Severity["Fatal"] = "fatal"; + /** JSDoc */ + Severity["Error"] = "error"; + /** JSDoc */ + Severity["Warning"] = "warning"; + /** JSDoc */ + Severity["Log"] = "log"; + /** JSDoc */ + Severity["Info"] = "info"; + /** JSDoc */ + Severity["Debug"] = "debug"; + /** JSDoc */ + Severity["Critical"] = "critical"; +})(Severity || (Severity = {})); +// TODO: in v7, these can disappear, because they now also exist in `@sentry/utils`. (Having them there rather than here +// is nice because then it enforces the idea that only types are exported from `@sentry/types`.) +export var SeverityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug', 'critical']; +//# sourceMappingURL=severity.js.map
\ No newline at end of file diff --git a/shared/logger/node_modules/@sentry/utils/esm/baggage.js b/shared/logger/node_modules/@sentry/utils/esm/baggage.js new file mode 100644 index 0000000..88297b4 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/baggage.js @@ -0,0 +1,145 @@ +import { isString } from './is.js'; +import { logger } from './logger.js'; + +const BAGGAGE_HEADER_NAME = 'baggage'; + +const SENTRY_BAGGAGE_KEY_PREFIX = 'sentry-'; + +const SENTRY_BAGGAGE_KEY_PREFIX_REGEX = /^sentry-/; + +/** + * Max length of a serialized baggage string + * + * https://www.w3.org/TR/baggage/#limits + */ +const MAX_BAGGAGE_STRING_LENGTH = 8192; + +/** + * Takes a baggage header and turns it into Dynamic Sampling Context, by extracting all the "sentry-" prefixed values + * from it. + * + * @param baggageHeader A very bread definition of a baggage header as it might appear in various frameworks. + * @returns The Dynamic Sampling Context that was found on `baggageHeader`, if there was any, `undefined` otherwise. + */ +function baggageHeaderToDynamicSamplingContext( + // Very liberal definition of what any incoming header might look like + baggageHeader, +) { + if (!isString(baggageHeader) && !Array.isArray(baggageHeader)) { + return undefined; + } + + // Intermediary object to store baggage key value pairs of incoming baggage headers on. + // It is later used to read Sentry-DSC-values from. + let baggageObject = {}; + + if (Array.isArray(baggageHeader)) { + // Combine all baggage headers into one object containing the baggage values so we can later read the Sentry-DSC-values from it + baggageObject = baggageHeader.reduce((acc, curr) => { + const currBaggageObject = baggageHeaderToObject(curr); + return { + ...acc, + ...currBaggageObject, + }; + }, {}); + } else { + // Return undefined if baggage header is an empty string (technically an empty baggage header is not spec conform but + // this is how we choose to handle it) + if (!baggageHeader) { + return undefined; + } + + baggageObject = baggageHeaderToObject(baggageHeader); + } + + // Read all "sentry-" prefixed values out of the baggage object and put it onto a dynamic sampling context object. + const dynamicSamplingContext = Object.entries(baggageObject).reduce((acc, [key, value]) => { + if (key.match(SENTRY_BAGGAGE_KEY_PREFIX_REGEX)) { + const nonPrefixedKey = key.slice(SENTRY_BAGGAGE_KEY_PREFIX.length); + acc[nonPrefixedKey] = value; + } + return acc; + }, {}); + + // Only return a dynamic sampling context object if there are keys in it. + // A keyless object means there were no sentry values on the header, which means that there is no DSC. + if (Object.keys(dynamicSamplingContext).length > 0) { + return dynamicSamplingContext ; + } else { + return undefined; + } +} + +/** + * Turns a Dynamic Sampling Object into a baggage header by prefixing all the keys on the object with "sentry-". + * + * @param dynamicSamplingContext The Dynamic Sampling Context to turn into a header. For convenience and compatibility + * with the `getDynamicSamplingContext` method on the Transaction class ,this argument can also be `undefined`. If it is + * `undefined` the function will return `undefined`. + * @returns a baggage header, created from `dynamicSamplingContext`, or `undefined` either if `dynamicSamplingContext` + * was `undefined`, or if `dynamicSamplingContext` didn't contain any values. + */ +function dynamicSamplingContextToSentryBaggageHeader( + // this also takes undefined for convenience and bundle size in other places + dynamicSamplingContext, +) { + // Prefix all DSC keys with "sentry-" and put them into a new object + const sentryPrefixedDSC = Object.entries(dynamicSamplingContext).reduce( + (acc, [dscKey, dscValue]) => { + if (dscValue) { + acc[`${SENTRY_BAGGAGE_KEY_PREFIX}${dscKey}`] = dscValue; + } + return acc; + }, + {}, + ); + + return objectToBaggageHeader(sentryPrefixedDSC); +} + +/** + * Will parse a baggage header, which is a simple key-value map, into a flat object. + * + * @param baggageHeader The baggage header to parse. + * @returns a flat object containing all the key-value pairs from `baggageHeader`. + */ +function baggageHeaderToObject(baggageHeader) { + return baggageHeader + .split(',') + .map(baggageEntry => baggageEntry.split('=').map(keyOrValue => decodeURIComponent(keyOrValue.trim()))) + .reduce((acc, [key, value]) => { + acc[key] = value; + return acc; + }, {}); +} + +/** + * Turns a flat object (key-value pairs) into a baggage header, which is also just key-value pairs. + * + * @param object The object to turn into a baggage header. + * @returns a baggage header string, or `undefined` if the object didn't have any values, since an empty baggage header + * is not spec compliant. + */ +function objectToBaggageHeader(object) { + if (Object.keys(object).length === 0) { + // An empty baggage header is not spec compliant: We return undefined. + return undefined; + } + + return Object.entries(object).reduce((baggageHeader, [objectKey, objectValue], currentIndex) => { + const baggageEntry = `${encodeURIComponent(objectKey)}=${encodeURIComponent(objectValue)}`; + const newBaggageHeader = currentIndex === 0 ? baggageEntry : `${baggageHeader},${baggageEntry}`; + if (newBaggageHeader.length > MAX_BAGGAGE_STRING_LENGTH) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn( + `Not adding key: ${objectKey} with val: ${objectValue} to baggage header due to exceeding baggage size limits.`, + ); + return baggageHeader; + } else { + return newBaggageHeader; + } + }, ''); +} + +export { BAGGAGE_HEADER_NAME, MAX_BAGGAGE_STRING_LENGTH, SENTRY_BAGGAGE_KEY_PREFIX, SENTRY_BAGGAGE_KEY_PREFIX_REGEX, baggageHeaderToDynamicSamplingContext, dynamicSamplingContextToSentryBaggageHeader }; +//# sourceMappingURL=baggage.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/browser.js b/shared/logger/node_modules/@sentry/utils/esm/browser.js new file mode 100644 index 0000000..aef06de --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/browser.js @@ -0,0 +1,152 @@ +import { isString } from './is.js'; +import { getGlobalObject } from './worldwide.js'; + +// eslint-disable-next-line deprecation/deprecation +const WINDOW = getGlobalObject(); + +const DEFAULT_MAX_STRING_LENGTH = 80; + +/** + * Given a child DOM element, returns a query-selector statement describing that + * and its ancestors + * e.g. [HTMLElement] => body > div > input#foo.btn[name=baz] + * @returns generated DOM path + */ +function htmlTreeAsString( + elem, + options = {}, +) { + + // try/catch both: + // - accessing event.target (see getsentry/raven-js#838, #768) + // - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly + // - can throw an exception in some circumstances. + try { + let currentElem = elem ; + const MAX_TRAVERSE_HEIGHT = 5; + const out = []; + let height = 0; + let len = 0; + const separator = ' > '; + const sepLength = separator.length; + let nextStr; + const keyAttrs = Array.isArray(options) ? options : options.keyAttrs; + const maxStringLength = (!Array.isArray(options) && options.maxStringLength) || DEFAULT_MAX_STRING_LENGTH; + + while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) { + nextStr = _htmlElementAsString(currentElem, keyAttrs); + // bail out if + // - nextStr is the 'html' element + // - the length of the string that would be created exceeds maxStringLength + // (ignore this limit if we are on the first iteration) + if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= maxStringLength)) { + break; + } + + out.push(nextStr); + + len += nextStr.length; + currentElem = currentElem.parentNode; + } + + return out.reverse().join(separator); + } catch (_oO) { + return '<unknown>'; + } +} + +/** + * Returns a simple, query-selector representation of a DOM element + * e.g. [HTMLElement] => input#foo.btn[name=baz] + * @returns generated DOM path + */ +function _htmlElementAsString(el, keyAttrs) { + const elem = el + +; + + const out = []; + let className; + let classes; + let key; + let attr; + let i; + + if (!elem || !elem.tagName) { + return ''; + } + + out.push(elem.tagName.toLowerCase()); + + // Pairs of attribute keys defined in `serializeAttribute` and their values on element. + const keyAttrPairs = + keyAttrs && keyAttrs.length + ? keyAttrs.filter(keyAttr => elem.getAttribute(keyAttr)).map(keyAttr => [keyAttr, elem.getAttribute(keyAttr)]) + : null; + + if (keyAttrPairs && keyAttrPairs.length) { + keyAttrPairs.forEach(keyAttrPair => { + out.push(`[${keyAttrPair[0]}="${keyAttrPair[1]}"]`); + }); + } else { + if (elem.id) { + out.push(`#${elem.id}`); + } + + // eslint-disable-next-line prefer-const + className = elem.className; + if (className && isString(className)) { + classes = className.split(/\s+/); + for (i = 0; i < classes.length; i++) { + out.push(`.${classes[i]}`); + } + } + } + const allowedAttrs = ['aria-label', 'type', 'name', 'title', 'alt']; + for (i = 0; i < allowedAttrs.length; i++) { + key = allowedAttrs[i]; + attr = elem.getAttribute(key); + if (attr) { + out.push(`[${key}="${attr}"]`); + } + } + return out.join(''); +} + +/** + * A safe form of location.href + */ +function getLocationHref() { + try { + return WINDOW.document.location.href; + } catch (oO) { + return ''; + } +} + +/** + * Gets a DOM element by using document.querySelector. + * + * This wrapper will first check for the existance of the function before + * actually calling it so that we don't have to take care of this check, + * every time we want to access the DOM. + * + * Reason: DOM/querySelector is not available in all environments. + * + * We have to cast to any because utils can be consumed by a variety of environments, + * and we don't want to break TS users. If you know what element will be selected by + * `document.querySelector`, specify it as part of the generic call. For example, + * `const element = getDomElement<Element>('selector');` + * + * @param selector the selector string passed on to document.querySelector + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function getDomElement(selector) { + if (WINDOW.document && WINDOW.document.querySelector) { + return WINDOW.document.querySelector(selector) ; + } + return null; +} + +export { getDomElement, getLocationHref, htmlTreeAsString }; +//# sourceMappingURL=browser.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/buildPolyfills/_optionalChain.js b/shared/logger/node_modules/@sentry/utils/esm/buildPolyfills/_optionalChain.js new file mode 100644 index 0000000..6b4f012 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/buildPolyfills/_optionalChain.js @@ -0,0 +1,59 @@ +/** + * Polyfill for the optional chain operator, `?.`, given previous conversion of the expression into an array of values, + * descriptors, and functions. + * + * Adapted from Sucrase (https://github.com/alangpierce/sucrase) + * See https://github.com/alangpierce/sucrase/blob/265887868966917f3b924ce38dfad01fbab1329f/src/transformers/OptionalChainingNullishTransformer.ts#L15 + * + * @param ops Array result of expression conversion + * @returns The value of the expression + */ +function _optionalChain(ops) { + let lastAccessLHS = undefined; + let value = ops[0]; + let i = 1; + while (i < ops.length) { + const op = ops[i] ; + const fn = ops[i + 1] ; + i += 2; + // by checking for loose equality to `null`, we catch both `null` and `undefined` + if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { + // really we're meaning to return `undefined` as an actual value here, but it saves bytes not to write it + return; + } + if (op === 'access' || op === 'optionalAccess') { + lastAccessLHS = value; + value = fn(value); + } else if (op === 'call' || op === 'optionalCall') { + value = fn((...args) => (value ).call(lastAccessLHS, ...args)); + lastAccessLHS = undefined; + } + } + return value; +} + +// Sucrase version +// function _optionalChain(ops) { +// let lastAccessLHS = undefined; +// let value = ops[0]; +// let i = 1; +// while (i < ops.length) { +// const op = ops[i]; +// const fn = ops[i + 1]; +// i += 2; +// if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { +// return undefined; +// } +// if (op === 'access' || op === 'optionalAccess') { +// lastAccessLHS = value; +// value = fn(value); +// } else if (op === 'call' || op === 'optionalCall') { +// value = fn((...args) => value.call(lastAccessLHS, ...args)); +// lastAccessLHS = undefined; +// } +// } +// return value; +// } + +export { _optionalChain }; +//# sourceMappingURL=_optionalChain.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/clientreport.js b/shared/logger/node_modules/@sentry/utils/esm/clientreport.js new file mode 100644 index 0000000..22c1c30 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/clientreport.js @@ -0,0 +1,25 @@ +import { createEnvelope } from './envelope.js'; +import { dateTimestampInSeconds } from './time.js'; + +/** + * Creates client report envelope + * @param discarded_events An array of discard events + * @param dsn A DSN that can be set on the header. Optional. + */ +function createClientReportEnvelope( + discarded_events, + dsn, + timestamp, +) { + const clientReportItem = [ + { type: 'client_report' }, + { + timestamp: timestamp || dateTimestampInSeconds(), + discarded_events, + }, + ]; + return createEnvelope(dsn ? { dsn } : {}, [clientReportItem]); +} + +export { createClientReportEnvelope }; +//# sourceMappingURL=clientreport.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/dsn.js b/shared/logger/node_modules/@sentry/utils/esm/dsn.js new file mode 100644 index 0000000..4fe84be --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/dsn.js @@ -0,0 +1,126 @@ +import { logger } from './logger.js'; + +/** Regular expression used to parse a Dsn. */ +const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)([\w.-]+)(?::(\d+))?\/(.+)/; + +function isValidProtocol(protocol) { + return protocol === 'http' || protocol === 'https'; +} + +/** + * Renders the string representation of this Dsn. + * + * By default, this will render the public representation without the password + * component. To get the deprecated private representation, set `withPassword` + * to true. + * + * @param withPassword When set to true, the password will be included. + */ +function dsnToString(dsn, withPassword = false) { + const { host, path, pass, port, projectId, protocol, publicKey } = dsn; + return ( + `${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` + + `@${host}${port ? `:${port}` : ''}/${path ? `${path}/` : path}${projectId}` + ); +} + +/** + * Parses a Dsn from a given string. + * + * @param str A Dsn as string + * @returns Dsn as DsnComponents or undefined if @param str is not a valid DSN string + */ +function dsnFromString(str) { + const match = DSN_REGEX.exec(str); + + if (!match) { + // This should be logged to the console + // eslint-disable-next-line no-console + console.error(`Invalid Sentry Dsn: ${str}`); + return undefined; + } + + const [protocol, publicKey, pass = '', host, port = '', lastPath] = match.slice(1); + let path = ''; + let projectId = lastPath; + + const split = projectId.split('/'); + if (split.length > 1) { + path = split.slice(0, -1).join('/'); + projectId = split.pop() ; + } + + if (projectId) { + const projectMatch = projectId.match(/^\d+/); + if (projectMatch) { + projectId = projectMatch[0]; + } + } + + return dsnFromComponents({ host, pass, path, projectId, port, protocol: protocol , publicKey }); +} + +function dsnFromComponents(components) { + return { + protocol: components.protocol, + publicKey: components.publicKey || '', + pass: components.pass || '', + host: components.host, + port: components.port || '', + path: components.path || '', + projectId: components.projectId, + }; +} + +function validateDsn(dsn) { + if (!(typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + return true; + } + + const { port, projectId, protocol } = dsn; + + const requiredComponents = ['protocol', 'publicKey', 'host', 'projectId']; + const hasMissingRequiredComponent = requiredComponents.find(component => { + if (!dsn[component]) { + logger.error(`Invalid Sentry Dsn: ${component} missing`); + return true; + } + return false; + }); + + if (hasMissingRequiredComponent) { + return false; + } + + if (!projectId.match(/^\d+$/)) { + logger.error(`Invalid Sentry Dsn: Invalid projectId ${projectId}`); + return false; + } + + if (!isValidProtocol(protocol)) { + logger.error(`Invalid Sentry Dsn: Invalid protocol ${protocol}`); + return false; + } + + if (port && isNaN(parseInt(port, 10))) { + logger.error(`Invalid Sentry Dsn: Invalid port ${port}`); + return false; + } + + return true; +} + +/** + * Creates a valid Sentry Dsn object, identifying a Sentry instance and project. + * @returns a valid DsnComponents object or `undefined` if @param from is an invalid DSN source + */ +function makeDsn(from) { + const components = typeof from === 'string' ? dsnFromString(from) : dsnFromComponents(from); + if (!components || !validateDsn(components)) { + return undefined; + } + return components; +} + +export { dsnFromString, dsnToString, makeDsn }; +//# sourceMappingURL=dsn.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/env.js b/shared/logger/node_modules/@sentry/utils/esm/env.js new file mode 100644 index 0000000..dacd2f1 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/env.js @@ -0,0 +1,34 @@ +/* + * This module exists for optimizations in the build process through rollup and terser. We define some global + * constants, which can be overridden during build. By guarding certain pieces of code with functions that return these + * constants, we can control whether or not they appear in the final bundle. (Any code guarded by a false condition will + * never run, and will hence be dropped during treeshaking.) The two primary uses for this are stripping out calls to + * `logger` and preventing node-related code from appearing in browser bundles. + * + * Attention: + * This file should not be used to define constants/flags that are intended to be used for tree-shaking conducted by + * users. These flags should live in their respective packages, as we identified user tooling (specifically webpack) + * having issues tree-shaking these constants across package boundaries. + * An example for this is the __SENTRY_DEBUG__ constant. It is declared in each package individually because we want + * users to be able to shake away expressions that it guards. + */ + +/** + * Figures out if we're building a browser bundle. + * + * @returns true if this is a browser bundle build. + */ +function isBrowserBundle() { + return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__; +} + +/** + * Get source of SDK. + */ +function getSDKSource() { + // @ts-ignore "npm" is injected by rollup during build process + return "npm"; +} + +export { getSDKSource, isBrowserBundle }; +//# sourceMappingURL=env.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/envelope.js b/shared/logger/node_modules/@sentry/utils/esm/envelope.js new file mode 100644 index 0000000..a2cc042 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/envelope.js @@ -0,0 +1,232 @@ +import { dsnToString } from './dsn.js'; +import { normalize } from './normalize.js'; +import { dropUndefinedKeys } from './object.js'; + +/** + * Creates an envelope. + * Make sure to always explicitly provide the generic to this function + * so that the envelope types resolve correctly. + */ +function createEnvelope(headers, items = []) { + return [headers, items] ; +} + +/** + * Add an item to an envelope. + * Make sure to always explicitly provide the generic to this function + * so that the envelope types resolve correctly. + */ +function addItemToEnvelope(envelope, newItem) { + const [headers, items] = envelope; + return [headers, [...items, newItem]] ; +} + +/** + * Convenience function to loop through the items and item types of an envelope. + * (This function was mostly created because working with envelope types is painful at the moment) + * + * If the callback returns true, the rest of the items will be skipped. + */ +function forEachEnvelopeItem( + envelope, + callback, +) { + const envelopeItems = envelope[1]; + + for (const envelopeItem of envelopeItems) { + const envelopeItemType = envelopeItem[0].type; + const result = callback(envelopeItem, envelopeItemType); + + if (result) { + return true; + } + } + + return false; +} + +/** + * Returns true if the envelope contains any of the given envelope item types + */ +function envelopeContainsItemType(envelope, types) { + return forEachEnvelopeItem(envelope, (_, type) => types.includes(type)); +} + +/** + * Encode a string to UTF8. + */ +function encodeUTF8(input, textEncoder) { + const utf8 = textEncoder || new TextEncoder(); + return utf8.encode(input); +} + +/** + * Serializes an envelope. + */ +function serializeEnvelope(envelope, textEncoder) { + const [envHeaders, items] = envelope; + + // Initially we construct our envelope as a string and only convert to binary chunks if we encounter binary data + let parts = JSON.stringify(envHeaders); + + function append(next) { + if (typeof parts === 'string') { + parts = typeof next === 'string' ? parts + next : [encodeUTF8(parts, textEncoder), next]; + } else { + parts.push(typeof next === 'string' ? encodeUTF8(next, textEncoder) : next); + } + } + + for (const item of items) { + const [itemHeaders, payload] = item; + + append(`\n${JSON.stringify(itemHeaders)}\n`); + + if (typeof payload === 'string' || payload instanceof Uint8Array) { + append(payload); + } else { + let stringifiedPayload; + try { + stringifiedPayload = JSON.stringify(payload); + } catch (e) { + // In case, despite all our efforts to keep `payload` circular-dependency-free, `JSON.strinify()` still + // fails, we try again after normalizing it again with infinite normalization depth. This of course has a + // performance impact but in this case a performance hit is better than throwing. + stringifiedPayload = JSON.stringify(normalize(payload)); + } + append(stringifiedPayload); + } + } + + return typeof parts === 'string' ? parts : concatBuffers(parts); +} + +function concatBuffers(buffers) { + const totalLength = buffers.reduce((acc, buf) => acc + buf.length, 0); + + const merged = new Uint8Array(totalLength); + let offset = 0; + for (const buffer of buffers) { + merged.set(buffer, offset); + offset += buffer.length; + } + + return merged; +} + +/** + * Parses an envelope + */ +function parseEnvelope( + env, + textEncoder, + textDecoder, +) { + let buffer = typeof env === 'string' ? textEncoder.encode(env) : env; + + function readBinary(length) { + const bin = buffer.subarray(0, length); + // Replace the buffer with the remaining data excluding trailing newline + buffer = buffer.subarray(length + 1); + return bin; + } + + function readJson() { + let i = buffer.indexOf(0xa); + // If we couldn't find a newline, we must have found the end of the buffer + if (i < 0) { + i = buffer.length; + } + + return JSON.parse(textDecoder.decode(readBinary(i))) ; + } + + const envelopeHeader = readJson(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const items = []; + + while (buffer.length) { + const itemHeader = readJson(); + const binaryLength = typeof itemHeader.length === 'number' ? itemHeader.length : undefined; + + items.push([itemHeader, binaryLength ? readBinary(binaryLength) : readJson()]); + } + + return [envelopeHeader, items]; +} + +/** + * Creates attachment envelope items + */ +function createAttachmentEnvelopeItem( + attachment, + textEncoder, +) { + const buffer = typeof attachment.data === 'string' ? encodeUTF8(attachment.data, textEncoder) : attachment.data; + + return [ + dropUndefinedKeys({ + type: 'attachment', + length: buffer.length, + filename: attachment.filename, + content_type: attachment.contentType, + attachment_type: attachment.attachmentType, + }), + buffer, + ]; +} + +const ITEM_TYPE_TO_DATA_CATEGORY_MAP = { + session: 'session', + sessions: 'session', + attachment: 'attachment', + transaction: 'transaction', + event: 'error', + client_report: 'internal', + user_report: 'default', + profile: 'profile', + replay_event: 'replay', + replay_recording: 'replay', + check_in: 'monitor', +}; + +/** + * Maps the type of an envelope item to a data category. + */ +function envelopeItemTypeToDataCategory(type) { + return ITEM_TYPE_TO_DATA_CATEGORY_MAP[type]; +} + +/** Extracts the minimal SDK info from from the metadata or an events */ +function getSdkMetadataForEnvelopeHeader(metadataOrEvent) { + if (!metadataOrEvent || !metadataOrEvent.sdk) { + return; + } + const { name, version } = metadataOrEvent.sdk; + return { name, version }; +} + +/** + * Creates event envelope headers, based on event, sdk info and tunnel + * Note: This function was extracted from the core package to make it available in Replay + */ +function createEventEnvelopeHeaders( + event, + sdkInfo, + tunnel, + dsn, +) { + const dynamicSamplingContext = event.sdkProcessingMetadata && event.sdkProcessingMetadata.dynamicSamplingContext; + return { + event_id: event.event_id , + sent_at: new Date().toISOString(), + ...(sdkInfo && { sdk: sdkInfo }), + ...(!!tunnel && { dsn: dsnToString(dsn) }), + ...(dynamicSamplingContext && { + trace: dropUndefinedKeys({ ...dynamicSamplingContext }), + }), + }; +} + +export { addItemToEnvelope, createAttachmentEnvelopeItem, createEnvelope, createEventEnvelopeHeaders, envelopeContainsItemType, envelopeItemTypeToDataCategory, forEachEnvelopeItem, getSdkMetadataForEnvelopeHeader, parseEnvelope, serializeEnvelope }; +//# sourceMappingURL=envelope.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/error.js b/shared/logger/node_modules/@sentry/utils/esm/error.js new file mode 100644 index 0000000..5266404 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/error.js @@ -0,0 +1,17 @@ +/** An error emitted by Sentry SDKs and related utilities. */ +class SentryError extends Error { + /** Display name of this error instance. */ + + constructor( message, logLevel = 'warn') { + super(message);this.message = message; + this.name = new.target.prototype.constructor.name; + // This sets the prototype to be `Error`, not `SentryError`. It's unclear why we do this, but commenting this line + // out causes various (seemingly totally unrelated) playwright tests consistently time out. FYI, this makes + // instances of `SentryError` fail `obj instanceof SentryError` checks. + Object.setPrototypeOf(this, new.target.prototype); + this.logLevel = logLevel; + } +} + +export { SentryError }; +//# sourceMappingURL=error.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/instrument.js b/shared/logger/node_modules/@sentry/utils/esm/instrument.js new file mode 100644 index 0000000..618cf3c --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/instrument.js @@ -0,0 +1,631 @@ +import { isString } from './is.js'; +import { logger, CONSOLE_LEVELS } from './logger.js'; +import { fill } from './object.js'; +import { getFunctionName } from './stacktrace.js'; +import { supportsNativeFetch } from './supports.js'; +import { getGlobalObject } from './worldwide.js'; +import { supportsHistory } from './vendor/supportsHistory.js'; + +// eslint-disable-next-line deprecation/deprecation +const WINDOW = getGlobalObject(); + +const SENTRY_XHR_DATA_KEY = '__sentry_xhr_v2__'; + +/** + * Instrument native APIs to call handlers that can be used to create breadcrumbs, APM spans etc. + * - Console API + * - Fetch API + * - XHR API + * - History API + * - DOM API (click/typing) + * - Error API + * - UnhandledRejection API + */ + +const handlers = {}; +const instrumented = {}; + +/** Instruments given API */ +function instrument(type) { + if (instrumented[type]) { + return; + } + + instrumented[type] = true; + + switch (type) { + case 'console': + instrumentConsole(); + break; + case 'dom': + instrumentDOM(); + break; + case 'xhr': + instrumentXHR(); + break; + case 'fetch': + instrumentFetch(); + break; + case 'history': + instrumentHistory(); + break; + case 'error': + instrumentError(); + break; + case 'unhandledrejection': + instrumentUnhandledRejection(); + break; + default: + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('unknown instrumentation type:', type); + return; + } +} + +/** + * Add handler that will be called when given type of instrumentation triggers. + * Use at your own risk, this might break without changelog notice, only used internally. + * @hidden + */ +function addInstrumentationHandler(type, callback) { + handlers[type] = handlers[type] || []; + (handlers[type] ).push(callback); + instrument(type); +} + +/** JSDoc */ +function triggerHandlers(type, data) { + if (!type || !handlers[type]) { + return; + } + + for (const handler of handlers[type] || []) { + try { + handler(data); + } catch (e) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.error( + `Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(handler)}\nError:`, + e, + ); + } + } +} + +/** JSDoc */ +function instrumentConsole() { + if (!('console' in WINDOW)) { + return; + } + + CONSOLE_LEVELS.forEach(function (level) { + if (!(level in WINDOW.console)) { + return; + } + + fill(WINDOW.console, level, function (originalConsoleMethod) { + return function (...args) { + triggerHandlers('console', { args, level }); + + // this fails for some browsers. :( + if (originalConsoleMethod) { + originalConsoleMethod.apply(WINDOW.console, args); + } + }; + }); + }); +} + +/** JSDoc */ +function instrumentFetch() { + if (!supportsNativeFetch()) { + return; + } + + fill(WINDOW, 'fetch', function (originalFetch) { + return function (...args) { + const { method, url } = parseFetchArgs(args); + + const handlerData = { + args, + fetchData: { + method, + url, + }, + startTimestamp: Date.now(), + }; + + triggerHandlers('fetch', { + ...handlerData, + }); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return originalFetch.apply(WINDOW, args).then( + (response) => { + triggerHandlers('fetch', { + ...handlerData, + endTimestamp: Date.now(), + response, + }); + return response; + }, + (error) => { + triggerHandlers('fetch', { + ...handlerData, + endTimestamp: Date.now(), + error, + }); + // NOTE: If you are a Sentry user, and you are seeing this stack frame, + // it means the sentry.javascript SDK caught an error invoking your application code. + // This is expected behavior and NOT indicative of a bug with sentry.javascript. + throw error; + }, + ); + }; + }); +} + +function hasProp(obj, prop) { + return !!obj && typeof obj === 'object' && !!(obj )[prop]; +} + +function getUrlFromResource(resource) { + if (typeof resource === 'string') { + return resource; + } + + if (!resource) { + return ''; + } + + if (hasProp(resource, 'url')) { + return resource.url; + } + + if (resource.toString) { + return resource.toString(); + } + + return ''; +} + +/** + * Parses the fetch arguments to find the used Http method and the url of the request + */ +function parseFetchArgs(fetchArgs) { + if (fetchArgs.length === 0) { + return { method: 'GET', url: '' }; + } + + if (fetchArgs.length === 2) { + const [url, options] = fetchArgs ; + + return { + url: getUrlFromResource(url), + method: hasProp(options, 'method') ? String(options.method).toUpperCase() : 'GET', + }; + } + + const arg = fetchArgs[0]; + return { + url: getUrlFromResource(arg ), + method: hasProp(arg, 'method') ? String(arg.method).toUpperCase() : 'GET', + }; +} + +/** JSDoc */ +function instrumentXHR() { + if (!('XMLHttpRequest' in WINDOW)) { + return; + } + + const xhrproto = XMLHttpRequest.prototype; + + fill(xhrproto, 'open', function (originalOpen) { + return function ( ...args) { + const url = args[1]; + const xhrInfo = (this[SENTRY_XHR_DATA_KEY] = { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + method: isString(args[0]) ? args[0].toUpperCase() : args[0], + url: args[1], + request_headers: {}, + }); + + // if Sentry key appears in URL, don't capture it as a request + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + if (isString(url) && xhrInfo.method === 'POST' && url.match(/sentry_key/)) { + this.__sentry_own_request__ = true; + } + + const onreadystatechangeHandler = () => { + // For whatever reason, this is not the same instance here as from the outer method + const xhrInfo = this[SENTRY_XHR_DATA_KEY]; + + if (!xhrInfo) { + return; + } + + if (this.readyState === 4) { + try { + // touching statusCode in some platforms throws + // an exception + xhrInfo.status_code = this.status; + } catch (e) { + /* do nothing */ + } + + triggerHandlers('xhr', { + args: args , + endTimestamp: Date.now(), + startTimestamp: Date.now(), + xhr: this, + } ); + } + }; + + if ('onreadystatechange' in this && typeof this.onreadystatechange === 'function') { + fill(this, 'onreadystatechange', function (original) { + return function ( ...readyStateArgs) { + onreadystatechangeHandler(); + return original.apply(this, readyStateArgs); + }; + }); + } else { + this.addEventListener('readystatechange', onreadystatechangeHandler); + } + + // Intercepting `setRequestHeader` to access the request headers of XHR instance. + // This will only work for user/library defined headers, not for the default/browser-assigned headers. + // Request cookies are also unavailable for XHR, as `Cookie` header can't be defined by `setRequestHeader`. + fill(this, 'setRequestHeader', function (original) { + return function ( ...setRequestHeaderArgs) { + const [header, value] = setRequestHeaderArgs ; + + const xhrInfo = this[SENTRY_XHR_DATA_KEY]; + + if (xhrInfo) { + xhrInfo.request_headers[header.toLowerCase()] = value; + } + + return original.apply(this, setRequestHeaderArgs); + }; + }); + + return originalOpen.apply(this, args); + }; + }); + + fill(xhrproto, 'send', function (originalSend) { + return function ( ...args) { + const sentryXhrData = this[SENTRY_XHR_DATA_KEY]; + if (sentryXhrData && args[0] !== undefined) { + sentryXhrData.body = args[0]; + } + + triggerHandlers('xhr', { + args, + startTimestamp: Date.now(), + xhr: this, + }); + + return originalSend.apply(this, args); + }; + }); +} + +let lastHref; + +/** JSDoc */ +function instrumentHistory() { + if (!supportsHistory()) { + return; + } + + const oldOnPopState = WINDOW.onpopstate; + WINDOW.onpopstate = function ( ...args) { + const to = WINDOW.location.href; + // keep track of the current URL state, as we always receive only the updated state + const from = lastHref; + lastHref = to; + triggerHandlers('history', { + from, + to, + }); + if (oldOnPopState) { + // Apparently this can throw in Firefox when incorrectly implemented plugin is installed. + // https://github.com/getsentry/sentry-javascript/issues/3344 + // https://github.com/bugsnag/bugsnag-js/issues/469 + try { + return oldOnPopState.apply(this, args); + } catch (_oO) { + // no-empty + } + } + }; + + /** @hidden */ + function historyReplacementFunction(originalHistoryFunction) { + return function ( ...args) { + const url = args.length > 2 ? args[2] : undefined; + if (url) { + // coerce to string (this is what pushState does) + const from = lastHref; + const to = String(url); + // keep track of the current URL state, as we always receive only the updated state + lastHref = to; + triggerHandlers('history', { + from, + to, + }); + } + return originalHistoryFunction.apply(this, args); + }; + } + + fill(WINDOW.history, 'pushState', historyReplacementFunction); + fill(WINDOW.history, 'replaceState', historyReplacementFunction); +} + +const debounceDuration = 1000; +let debounceTimerID; +let lastCapturedEvent; + +/** + * Decide whether the current event should finish the debounce of previously captured one. + * @param previous previously captured event + * @param current event to be captured + */ +function shouldShortcircuitPreviousDebounce(previous, current) { + // If there was no previous event, it should always be swapped for the new one. + if (!previous) { + return true; + } + + // If both events have different type, then user definitely performed two separate actions. e.g. click + keypress. + if (previous.type !== current.type) { + return true; + } + + try { + // If both events have the same type, it's still possible that actions were performed on different targets. + // e.g. 2 clicks on different buttons. + if (previous.target !== current.target) { + return true; + } + } catch (e) { + // just accessing `target` property can throw an exception in some rare circumstances + // see: https://github.com/getsentry/sentry-javascript/issues/838 + } + + // If both events have the same type _and_ same `target` (an element which triggered an event, _not necessarily_ + // to which an event listener was attached), we treat them as the same action, as we want to capture + // only one breadcrumb. e.g. multiple clicks on the same button, or typing inside a user input box. + return false; +} + +/** + * Decide whether an event should be captured. + * @param event event to be captured + */ +function shouldSkipDOMEvent(event) { + // We are only interested in filtering `keypress` events for now. + if (event.type !== 'keypress') { + return false; + } + + try { + const target = event.target ; + + if (!target || !target.tagName) { + return true; + } + + // Only consider keypress events on actual input elements. This will disregard keypresses targeting body + // e.g.tabbing through elements, hotkeys, etc. + if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable) { + return false; + } + } catch (e) { + // just accessing `target` property can throw an exception in some rare circumstances + // see: https://github.com/getsentry/sentry-javascript/issues/838 + } + + return true; +} + +/** + * Wraps addEventListener to capture UI breadcrumbs + * @param handler function that will be triggered + * @param globalListener indicates whether event was captured by the global event listener + * @returns wrapped breadcrumb events handler + * @hidden + */ +function makeDOMEventHandler(handler, globalListener = false) { + return (event) => { + // It's possible this handler might trigger multiple times for the same + // event (e.g. event propagation through node ancestors). + // Ignore if we've already captured that event. + if (!event || lastCapturedEvent === event) { + return; + } + + // We always want to skip _some_ events. + if (shouldSkipDOMEvent(event)) { + return; + } + + const name = event.type === 'keypress' ? 'input' : event.type; + + // If there is no debounce timer, it means that we can safely capture the new event and store it for future comparisons. + if (debounceTimerID === undefined) { + handler({ + event: event, + name, + global: globalListener, + }); + lastCapturedEvent = event; + } + // If there is a debounce awaiting, see if the new event is different enough to treat it as a unique one. + // If that's the case, emit the previous event and store locally the newly-captured DOM event. + else if (shouldShortcircuitPreviousDebounce(lastCapturedEvent, event)) { + handler({ + event: event, + name, + global: globalListener, + }); + lastCapturedEvent = event; + } + + // Start a new debounce timer that will prevent us from capturing multiple events that should be grouped together. + clearTimeout(debounceTimerID); + debounceTimerID = WINDOW.setTimeout(() => { + debounceTimerID = undefined; + }, debounceDuration); + }; +} + +/** JSDoc */ +function instrumentDOM() { + if (!('document' in WINDOW)) { + return; + } + + // Make it so that any click or keypress that is unhandled / bubbled up all the way to the document triggers our dom + // handlers. (Normally we have only one, which captures a breadcrumb for each click or keypress.) Do this before + // we instrument `addEventListener` so that we don't end up attaching this handler twice. + const triggerDOMHandler = triggerHandlers.bind(null, 'dom'); + const globalDOMEventHandler = makeDOMEventHandler(triggerDOMHandler, true); + WINDOW.document.addEventListener('click', globalDOMEventHandler, false); + WINDOW.document.addEventListener('keypress', globalDOMEventHandler, false); + + // After hooking into click and keypress events bubbled up to `document`, we also hook into user-handled + // clicks & keypresses, by adding an event listener of our own to any element to which they add a listener. That + // way, whenever one of their handlers is triggered, ours will be, too. (This is needed because their handler + // could potentially prevent the event from bubbling up to our global listeners. This way, our handler are still + // guaranteed to fire at least once.) + ['EventTarget', 'Node'].forEach((target) => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + const proto = (WINDOW )[target] && (WINDOW )[target].prototype; + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins + if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) { + return; + } + + fill(proto, 'addEventListener', function (originalAddEventListener) { + return function ( + + type, + listener, + options, + ) { + if (type === 'click' || type == 'keypress') { + try { + const el = this ; + const handlers = (el.__sentry_instrumentation_handlers__ = el.__sentry_instrumentation_handlers__ || {}); + const handlerForType = (handlers[type] = handlers[type] || { refCount: 0 }); + + if (!handlerForType.handler) { + const handler = makeDOMEventHandler(triggerDOMHandler); + handlerForType.handler = handler; + originalAddEventListener.call(this, type, handler, options); + } + + handlerForType.refCount++; + } catch (e) { + // Accessing dom properties is always fragile. + // Also allows us to skip `addEventListenrs` calls with no proper `this` context. + } + } + + return originalAddEventListener.call(this, type, listener, options); + }; + }); + + fill( + proto, + 'removeEventListener', + function (originalRemoveEventListener) { + return function ( + + type, + listener, + options, + ) { + if (type === 'click' || type == 'keypress') { + try { + const el = this ; + const handlers = el.__sentry_instrumentation_handlers__ || {}; + const handlerForType = handlers[type]; + + if (handlerForType) { + handlerForType.refCount--; + // If there are no longer any custom handlers of the current type on this element, we can remove ours, too. + if (handlerForType.refCount <= 0) { + originalRemoveEventListener.call(this, type, handlerForType.handler, options); + handlerForType.handler = undefined; + delete handlers[type]; // eslint-disable-line @typescript-eslint/no-dynamic-delete + } + + // If there are no longer any custom handlers of any type on this element, cleanup everything. + if (Object.keys(handlers).length === 0) { + delete el.__sentry_instrumentation_handlers__; + } + } + } catch (e) { + // Accessing dom properties is always fragile. + // Also allows us to skip `addEventListenrs` calls with no proper `this` context. + } + } + + return originalRemoveEventListener.call(this, type, listener, options); + }; + }, + ); + }); +} + +let _oldOnErrorHandler = null; +/** JSDoc */ +function instrumentError() { + _oldOnErrorHandler = WINDOW.onerror; + + WINDOW.onerror = function (msg, url, line, column, error) { + triggerHandlers('error', { + column, + error, + line, + msg, + url, + }); + + if (_oldOnErrorHandler && !_oldOnErrorHandler.__SENTRY_LOADER__) { + // eslint-disable-next-line prefer-rest-params + return _oldOnErrorHandler.apply(this, arguments); + } + + return false; + }; + + WINDOW.onerror.__SENTRY_INSTRUMENTED__ = true; +} + +let _oldOnUnhandledRejectionHandler = null; +/** JSDoc */ +function instrumentUnhandledRejection() { + _oldOnUnhandledRejectionHandler = WINDOW.onunhandledrejection; + + WINDOW.onunhandledrejection = function (e) { + triggerHandlers('unhandledrejection', e); + + if (_oldOnUnhandledRejectionHandler && !_oldOnUnhandledRejectionHandler.__SENTRY_LOADER__) { + // eslint-disable-next-line prefer-rest-params + return _oldOnUnhandledRejectionHandler.apply(this, arguments); + } + + return true; + }; + + WINDOW.onunhandledrejection.__SENTRY_INSTRUMENTED__ = true; +} + +export { SENTRY_XHR_DATA_KEY, addInstrumentationHandler, parseFetchArgs }; +//# sourceMappingURL=instrument.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/is.js b/shared/logger/node_modules/@sentry/utils/esm/is.js new file mode 100644 index 0000000..8b60633 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/is.js @@ -0,0 +1,179 @@ +// eslint-disable-next-line @typescript-eslint/unbound-method +const objectToString = Object.prototype.toString; + +/** + * Checks whether given value's type is one of a few Error or Error-like + * {@link isError}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isError(wat) { + switch (objectToString.call(wat)) { + case '[object Error]': + case '[object Exception]': + case '[object DOMException]': + return true; + default: + return isInstanceOf(wat, Error); + } +} +/** + * Checks whether given value is an instance of the given built-in class. + * + * @param wat The value to be checked + * @param className + * @returns A boolean representing the result. + */ +function isBuiltin(wat, className) { + return objectToString.call(wat) === `[object ${className}]`; +} + +/** + * Checks whether given value's type is ErrorEvent + * {@link isErrorEvent}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isErrorEvent(wat) { + return isBuiltin(wat, 'ErrorEvent'); +} + +/** + * Checks whether given value's type is DOMError + * {@link isDOMError}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isDOMError(wat) { + return isBuiltin(wat, 'DOMError'); +} + +/** + * Checks whether given value's type is DOMException + * {@link isDOMException}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isDOMException(wat) { + return isBuiltin(wat, 'DOMException'); +} + +/** + * Checks whether given value's type is a string + * {@link isString}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isString(wat) { + return isBuiltin(wat, 'String'); +} + +/** + * Checks whether given value is a primitive (undefined, null, number, boolean, string, bigint, symbol) + * {@link isPrimitive}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isPrimitive(wat) { + return wat === null || (typeof wat !== 'object' && typeof wat !== 'function'); +} + +/** + * Checks whether given value's type is an object literal + * {@link isPlainObject}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isPlainObject(wat) { + return isBuiltin(wat, 'Object'); +} + +/** + * Checks whether given value's type is an Event instance + * {@link isEvent}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isEvent(wat) { + return typeof Event !== 'undefined' && isInstanceOf(wat, Event); +} + +/** + * Checks whether given value's type is an Element instance + * {@link isElement}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isElement(wat) { + return typeof Element !== 'undefined' && isInstanceOf(wat, Element); +} + +/** + * Checks whether given value's type is an regexp + * {@link isRegExp}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isRegExp(wat) { + return isBuiltin(wat, 'RegExp'); +} + +/** + * Checks whether given value has a then function. + * @param wat A value to be checked. + */ +function isThenable(wat) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return Boolean(wat && wat.then && typeof wat.then === 'function'); +} + +/** + * Checks whether given value's type is a SyntheticEvent + * {@link isSyntheticEvent}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isSyntheticEvent(wat) { + return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat; +} + +/** + * Checks whether given value is NaN + * {@link isNaN}. + * + * @param wat A value to be checked. + * @returns A boolean representing the result. + */ +function isNaN(wat) { + return typeof wat === 'number' && wat !== wat; +} + +/** + * Checks whether given value's type is an instance of provided constructor. + * {@link isInstanceOf}. + * + * @param wat A value to be checked. + * @param base A constructor to be used in a check. + * @returns A boolean representing the result. + */ +function isInstanceOf(wat, base) { + try { + return wat instanceof base; + } catch (_e) { + return false; + } +} + +export { isDOMError, isDOMException, isElement, isError, isErrorEvent, isEvent, isInstanceOf, isNaN, isPlainObject, isPrimitive, isRegExp, isString, isSyntheticEvent, isThenable }; +//# sourceMappingURL=is.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/logger.js b/shared/logger/node_modules/@sentry/utils/esm/logger.js new file mode 100644 index 0000000..6c8c644 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/logger.js @@ -0,0 +1,83 @@ +import { getGlobalSingleton, GLOBAL_OBJ } from './worldwide.js'; + +/** Prefix for logging strings */ +const PREFIX = 'Sentry Logger '; + +const CONSOLE_LEVELS = ['debug', 'info', 'warn', 'error', 'log', 'assert', 'trace'] ; + +/** + * Temporarily disable sentry console instrumentations. + * + * @param callback The function to run against the original `console` messages + * @returns The results of the callback + */ +function consoleSandbox(callback) { + if (!('console' in GLOBAL_OBJ)) { + return callback(); + } + + const originalConsole = GLOBAL_OBJ.console ; + const wrappedLevels = {}; + + // Restore all wrapped console methods + CONSOLE_LEVELS.forEach(level => { + // TODO(v7): Remove this check as it's only needed for Node 6 + const originalWrappedFunc = + originalConsole[level] && (originalConsole[level] ).__sentry_original__; + if (level in originalConsole && originalWrappedFunc) { + wrappedLevels[level] = originalConsole[level] ; + originalConsole[level] = originalWrappedFunc ; + } + }); + + try { + return callback(); + } finally { + // Revert restoration to wrapped state + Object.keys(wrappedLevels).forEach(level => { + originalConsole[level] = wrappedLevels[level ]; + }); + } +} + +function makeLogger() { + let enabled = false; + const logger = { + enable: () => { + enabled = true; + }, + disable: () => { + enabled = false; + }, + }; + + if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + CONSOLE_LEVELS.forEach(name => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + logger[name] = (...args) => { + if (enabled) { + consoleSandbox(() => { + GLOBAL_OBJ.console[name](`${PREFIX}[${name}]:`, ...args); + }); + } + }; + }); + } else { + CONSOLE_LEVELS.forEach(name => { + logger[name] = () => undefined; + }); + } + + return logger ; +} + +// Ensure we only have a single logger instance, even if multiple versions of @sentry/utils are being used +let logger; +if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) { + logger = getGlobalSingleton('logger', makeLogger); +} else { + logger = makeLogger(); +} + +export { CONSOLE_LEVELS, consoleSandbox, logger }; +//# sourceMappingURL=logger.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/memo.js b/shared/logger/node_modules/@sentry/utils/esm/memo.js new file mode 100644 index 0000000..fe00c9c --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/memo.js @@ -0,0 +1,45 @@ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +/** + * Helper to decycle json objects + */ +function memoBuilder() { + const hasWeakSet = typeof WeakSet === 'function'; + const inner = hasWeakSet ? new WeakSet() : []; + function memoize(obj) { + if (hasWeakSet) { + if (inner.has(obj)) { + return true; + } + inner.add(obj); + return false; + } + // eslint-disable-next-line @typescript-eslint/prefer-for-of + for (let i = 0; i < inner.length; i++) { + const value = inner[i]; + if (value === obj) { + return true; + } + } + inner.push(obj); + return false; + } + + function unmemoize(obj) { + if (hasWeakSet) { + inner.delete(obj); + } else { + for (let i = 0; i < inner.length; i++) { + if (inner[i] === obj) { + inner.splice(i, 1); + break; + } + } + } + } + return [memoize, unmemoize]; +} + +export { memoBuilder }; +//# sourceMappingURL=memo.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/misc.js b/shared/logger/node_modules/@sentry/utils/esm/misc.js new file mode 100644 index 0000000..858f159 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/misc.js @@ -0,0 +1,197 @@ +import { addNonEnumerableProperty } from './object.js'; +import { snipLine } from './string.js'; +import { GLOBAL_OBJ } from './worldwide.js'; + +/** + * UUID4 generator + * + * @returns string Generated UUID4. + */ +function uuid4() { + const gbl = GLOBAL_OBJ ; + const crypto = gbl.crypto || gbl.msCrypto; + + if (crypto && crypto.randomUUID) { + return crypto.randomUUID().replace(/-/g, ''); + } + + const getRandomByte = + crypto && crypto.getRandomValues ? () => crypto.getRandomValues(new Uint8Array(1))[0] : () => Math.random() * 16; + + // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523 + // Concatenating the following numbers as strings results in '10000000100040008000100000000000' + return (([1e7] ) + 1e3 + 4e3 + 8e3 + 1e11).replace(/[018]/g, c => + // eslint-disable-next-line no-bitwise + ((c ) ^ ((getRandomByte() & 15) >> ((c ) / 4))).toString(16), + ); +} + +function getFirstException(event) { + return event.exception && event.exception.values ? event.exception.values[0] : undefined; +} + +/** + * Extracts either message or type+value from an event that can be used for user-facing logs + * @returns event's description + */ +function getEventDescription(event) { + const { message, event_id: eventId } = event; + if (message) { + return message; + } + + const firstException = getFirstException(event); + if (firstException) { + if (firstException.type && firstException.value) { + return `${firstException.type}: ${firstException.value}`; + } + return firstException.type || firstException.value || eventId || '<unknown>'; + } + return eventId || '<unknown>'; +} + +/** + * Adds exception values, type and value to an synthetic Exception. + * @param event The event to modify. + * @param value Value of the exception. + * @param type Type of the exception. + * @hidden + */ +function addExceptionTypeValue(event, value, type) { + const exception = (event.exception = event.exception || {}); + const values = (exception.values = exception.values || []); + const firstException = (values[0] = values[0] || {}); + if (!firstException.value) { + firstException.value = value || ''; + } + if (!firstException.type) { + firstException.type = type || 'Error'; + } +} + +/** + * Adds exception mechanism data to a given event. Uses defaults if the second parameter is not passed. + * + * @param event The event to modify. + * @param newMechanism Mechanism data to add to the event. + * @hidden + */ +function addExceptionMechanism(event, newMechanism) { + const firstException = getFirstException(event); + if (!firstException) { + return; + } + + const defaultMechanism = { type: 'generic', handled: true }; + const currentMechanism = firstException.mechanism; + firstException.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism }; + + if (newMechanism && 'data' in newMechanism) { + const mergedData = { ...(currentMechanism && currentMechanism.data), ...newMechanism.data }; + firstException.mechanism.data = mergedData; + } +} + +// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string +const SEMVER_REGEXP = + /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; + +/** + * Represents Semantic Versioning object + */ + +/** + * Parses input into a SemVer interface + * @param input string representation of a semver version + */ +function parseSemver(input) { + const match = input.match(SEMVER_REGEXP) || []; + const major = parseInt(match[1], 10); + const minor = parseInt(match[2], 10); + const patch = parseInt(match[3], 10); + return { + buildmetadata: match[5], + major: isNaN(major) ? undefined : major, + minor: isNaN(minor) ? undefined : minor, + patch: isNaN(patch) ? undefined : patch, + prerelease: match[4], + }; +} + +/** + * This function adds context (pre/post/line) lines to the provided frame + * + * @param lines string[] containing all lines + * @param frame StackFrame that will be mutated + * @param linesOfContext number of context lines we want to add pre/post + */ +function addContextToFrame(lines, frame, linesOfContext = 5) { + // When there is no line number in the frame, attaching context is nonsensical and will even break grouping + if (frame.lineno === undefined) { + return; + } + + const maxLines = lines.length; + const sourceLine = Math.max(Math.min(maxLines, frame.lineno - 1), 0); + + frame.pre_context = lines + .slice(Math.max(0, sourceLine - linesOfContext), sourceLine) + .map((line) => snipLine(line, 0)); + + frame.context_line = snipLine(lines[Math.min(maxLines - 1, sourceLine)], frame.colno || 0); + + frame.post_context = lines + .slice(Math.min(sourceLine + 1, maxLines), sourceLine + 1 + linesOfContext) + .map((line) => snipLine(line, 0)); +} + +/** + * Checks whether or not we've already captured the given exception (note: not an identical exception - the very object + * in question), and marks it captured if not. + * + * This is useful because it's possible for an error to get captured by more than one mechanism. After we intercept and + * record an error, we rethrow it (assuming we've intercepted it before it's reached the top-level global handlers), so + * that we don't interfere with whatever effects the error might have had were the SDK not there. At that point, because + * the error has been rethrown, it's possible for it to bubble up to some other code we've instrumented. If it's not + * caught after that, it will bubble all the way up to the global handlers (which of course we also instrument). This + * function helps us ensure that even if we encounter the same error more than once, we only record it the first time we + * see it. + * + * Note: It will ignore primitives (always return `false` and not mark them as seen), as properties can't be set on + * them. {@link: Object.objectify} can be used on exceptions to convert any that are primitives into their equivalent + * object wrapper forms so that this check will always work. However, because we need to flag the exact object which + * will get rethrown, and because that rethrowing happens outside of the event processing pipeline, the objectification + * must be done before the exception captured. + * + * @param A thrown exception to check or flag as having been seen + * @returns `true` if the exception has already been captured, `false` if not (with the side effect of marking it seen) + */ +function checkOrSetAlreadyCaught(exception) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + if (exception && (exception ).__sentry_captured__) { + return true; + } + + try { + // set it this way rather than by assignment so that it's not ennumerable and therefore isn't recorded by the + // `ExtraErrorData` integration + addNonEnumerableProperty(exception , '__sentry_captured__', true); + } catch (err) { + // `exception` is a primitive, so we can't mark it seen + } + + return false; +} + +/** + * Checks whether the given input is already an array, and if it isn't, wraps it in one. + * + * @param maybeArray Input to turn into an array, if necessary + * @returns The input, if already an array, or an array with the input as the only element, if not + */ +function arrayify(maybeArray) { + return Array.isArray(maybeArray) ? maybeArray : [maybeArray]; +} + +export { addContextToFrame, addExceptionMechanism, addExceptionTypeValue, arrayify, checkOrSetAlreadyCaught, getEventDescription, parseSemver, uuid4 }; +//# sourceMappingURL=misc.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/node.js b/shared/logger/node_modules/@sentry/utils/esm/node.js new file mode 100644 index 0000000..49eb7d7 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/node.js @@ -0,0 +1,66 @@ +import { isBrowserBundle } from './env.js'; + +/** + * NOTE: In order to avoid circular dependencies, if you add a function to this module and it needs to print something, + * you must either a) use `console.log` rather than the logger, or b) put your function elsewhere. + */ + +/** + * Checks whether we're in the Node.js or Browser environment + * + * @returns Answer to given question + */ +function isNodeEnv() { + // explicitly check for browser bundles as those can be optimized statically + // by terser/rollup. + return ( + !isBrowserBundle() && + Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]' + ); +} + +/** + * Requires a module which is protected against bundler minification. + * + * @param request The module path to resolve + */ +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any +function dynamicRequire(mod, request) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return mod.require(request); +} + +/** + * Helper for dynamically loading module that should work with linked dependencies. + * The problem is that we _should_ be using `require(require.resolve(moduleName, { paths: [cwd()] }))` + * However it's _not possible_ to do that with Webpack, as it has to know all the dependencies during + * build time. `require.resolve` is also not available in any other way, so we cannot create, + * a fake helper like we do with `dynamicRequire`. + * + * We always prefer to use local package, thus the value is not returned early from each `try/catch` block. + * That is to mimic the behavior of `require.resolve` exactly. + * + * @param moduleName module name to require + * @returns possibly required module + */ +function loadModule(moduleName) { + let mod; + + try { + mod = dynamicRequire(module, moduleName); + } catch (e) { + // no-empty + } + + try { + const { cwd } = dynamicRequire(module, 'process'); + mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) ; + } catch (e) { + // no-empty + } + + return mod; +} + +export { dynamicRequire, isNodeEnv, loadModule }; +//# sourceMappingURL=node.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/normalize.js b/shared/logger/node_modules/@sentry/utils/esm/normalize.js new file mode 100644 index 0000000..5d52c58 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/normalize.js @@ -0,0 +1,263 @@ +import { isNaN, isSyntheticEvent } from './is.js'; +import { memoBuilder } from './memo.js'; +import { convertToPlainObject } from './object.js'; +import { getFunctionName } from './stacktrace.js'; + +/** + * Recursively normalizes the given object. + * + * - Creates a copy to prevent original input mutation + * - Skips non-enumerable properties + * - When stringifying, calls `toJSON` if implemented + * - Removes circular references + * - Translates non-serializable values (`undefined`/`NaN`/functions) to serializable format + * - Translates known global objects/classes to a string representations + * - Takes care of `Error` object serialization + * - Optionally limits depth of final output + * - Optionally limits number of properties/elements included in any single object/array + * + * @param input The object to be normalized. + * @param depth The max depth to which to normalize the object. (Anything deeper stringified whole.) + * @param maxProperties The max number of elements or properties to be included in any single array or + * object in the normallized output. + * @returns A normalized version of the object, or `"**non-serializable**"` if any errors are thrown during normalization. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function normalize(input, depth = 100, maxProperties = +Infinity) { + try { + // since we're at the outermost level, we don't provide a key + return visit('', input, depth, maxProperties); + } catch (err) { + return { ERROR: `**non-serializable** (${err})` }; + } +} + +/** JSDoc */ +function normalizeToSize( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + object, + // Default Node.js REPL depth + depth = 3, + // 100kB, as 200kB is max payload size, so half sounds reasonable + maxSize = 100 * 1024, +) { + const normalized = normalize(object, depth); + + if (jsonSize(normalized) > maxSize) { + return normalizeToSize(object, depth - 1, maxSize); + } + + return normalized ; +} + +/** + * Visits a node to perform normalization on it + * + * @param key The key corresponding to the given node + * @param value The node to be visited + * @param depth Optional number indicating the maximum recursion depth + * @param maxProperties Optional maximum number of properties/elements included in any single object/array + * @param memo Optional Memo class handling decycling + */ +function visit( + key, + value, + depth = +Infinity, + maxProperties = +Infinity, + memo = memoBuilder(), +) { + const [memoize, unmemoize] = memo; + + // Get the simple cases out of the way first + if ( + value == null || // this matches null and undefined -> eqeq not eqeqeq + (['number', 'boolean', 'string'].includes(typeof value) && !isNaN(value)) + ) { + return value ; + } + + const stringified = stringifyValue(key, value); + + // Anything we could potentially dig into more (objects or arrays) will have come back as `"[object XXXX]"`. + // Everything else will have already been serialized, so if we don't see that pattern, we're done. + if (!stringified.startsWith('[object ')) { + return stringified; + } + + // From here on, we can assert that `value` is either an object or an array. + + // Do not normalize objects that we know have already been normalized. As a general rule, the + // "__sentry_skip_normalization__" property should only be used sparingly and only should only be set on objects that + // have already been normalized. + if ((value )['__sentry_skip_normalization__']) { + return value ; + } + + // We can set `__sentry_override_normalization_depth__` on an object to ensure that from there + // We keep a certain amount of depth. + // This should be used sparingly, e.g. we use it for the redux integration to ensure we get a certain amount of state. + const remainingDepth = + typeof (value )['__sentry_override_normalization_depth__'] === 'number' + ? ((value )['__sentry_override_normalization_depth__'] ) + : depth; + + // We're also done if we've reached the max depth + if (remainingDepth === 0) { + // At this point we know `serialized` is a string of the form `"[object XXXX]"`. Clean it up so it's just `"[XXXX]"`. + return stringified.replace('object ', ''); + } + + // If we've already visited this branch, bail out, as it's circular reference. If not, note that we're seeing it now. + if (memoize(value)) { + return '[Circular ~]'; + } + + // If the value has a `toJSON` method, we call it to extract more information + const valueWithToJSON = value ; + if (valueWithToJSON && typeof valueWithToJSON.toJSON === 'function') { + try { + const jsonValue = valueWithToJSON.toJSON(); + // We need to normalize the return value of `.toJSON()` in case it has circular references + return visit('', jsonValue, remainingDepth - 1, maxProperties, memo); + } catch (err) { + // pass (The built-in `toJSON` failed, but we can still try to do it ourselves) + } + } + + // At this point we know we either have an object or an array, we haven't seen it before, and we're going to recurse + // because we haven't yet reached the max depth. Create an accumulator to hold the results of visiting each + // property/entry, and keep track of the number of items we add to it. + const normalized = (Array.isArray(value) ? [] : {}) ; + let numAdded = 0; + + // Before we begin, convert`Error` and`Event` instances into plain objects, since some of each of their relevant + // properties are non-enumerable and otherwise would get missed. + const visitable = convertToPlainObject(value ); + + for (const visitKey in visitable) { + // Avoid iterating over fields in the prototype if they've somehow been exposed to enumeration. + if (!Object.prototype.hasOwnProperty.call(visitable, visitKey)) { + continue; + } + + if (numAdded >= maxProperties) { + normalized[visitKey] = '[MaxProperties ~]'; + break; + } + + // Recursively visit all the child nodes + const visitValue = visitable[visitKey]; + normalized[visitKey] = visit(visitKey, visitValue, remainingDepth - 1, maxProperties, memo); + + numAdded++; + } + + // Once we've visited all the branches, remove the parent from memo storage + unmemoize(value); + + // Return accumulated values + return normalized; +} + +/* eslint-disable complexity */ +/** + * Stringify the given value. Handles various known special values and types. + * + * Not meant to be used on simple primitives which already have a string representation, as it will, for example, turn + * the number 1231 into "[Object Number]", nor on `null`, as it will throw. + * + * @param value The value to stringify + * @returns A stringified representation of the given value + */ +function stringifyValue( + key, + // this type is a tiny bit of a cheat, since this function does handle NaN (which is technically a number), but for + // our internal use, it'll do + value, +) { + try { + if (key === 'domain' && value && typeof value === 'object' && (value )._events) { + return '[Domain]'; + } + + if (key === 'domainEmitter') { + return '[DomainEmitter]'; + } + + // It's safe to use `global`, `window`, and `document` here in this manner, as we are asserting using `typeof` first + // which won't throw if they are not present. + + if (typeof global !== 'undefined' && value === global) { + return '[Global]'; + } + + // eslint-disable-next-line no-restricted-globals + if (typeof window !== 'undefined' && value === window) { + return '[Window]'; + } + + // eslint-disable-next-line no-restricted-globals + if (typeof document !== 'undefined' && value === document) { + return '[Document]'; + } + + // React's SyntheticEvent thingy + if (isSyntheticEvent(value)) { + return '[SyntheticEvent]'; + } + + if (typeof value === 'number' && value !== value) { + return '[NaN]'; + } + + if (typeof value === 'function') { + return `[Function: ${getFunctionName(value)}]`; + } + + if (typeof value === 'symbol') { + return `[${String(value)}]`; + } + + // stringified BigInts are indistinguishable from regular numbers, so we need to label them to avoid confusion + if (typeof value === 'bigint') { + return `[BigInt: ${String(value)}]`; + } + + // Now that we've knocked out all the special cases and the primitives, all we have left are objects. Simply casting + // them to strings means that instances of classes which haven't defined their `toStringTag` will just come out as + // `"[object Object]"`. If we instead look at the constructor's name (which is the same as the name of the class), + // we can make sure that only plain objects come out that way. + const objName = getConstructorName(value); + + // Handle HTML Elements + if (/^HTML(\w*)Element$/.test(objName)) { + return `[HTMLElement: ${objName}]`; + } + + return `[object ${objName}]`; + } catch (err) { + return `**non-serializable** (${err})`; + } +} +/* eslint-enable complexity */ + +function getConstructorName(value) { + const prototype = Object.getPrototypeOf(value); + + return prototype ? prototype.constructor.name : 'null prototype'; +} + +/** Calculates bytes size of input string */ +function utf8Length(value) { + // eslint-disable-next-line no-bitwise + return ~-encodeURI(value).split(/%..|./).length; +} + +/** Calculates bytes size of input object */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function jsonSize(value) { + return utf8Length(JSON.stringify(value)); +} + +export { normalize, normalizeToSize, visit as walk }; +//# sourceMappingURL=normalize.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/object.js b/shared/logger/node_modules/@sentry/utils/esm/object.js new file mode 100644 index 0000000..0f5c411 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/object.js @@ -0,0 +1,279 @@ +import { htmlTreeAsString } from './browser.js'; +import { isError, isEvent, isInstanceOf, isElement, isPlainObject, isPrimitive } from './is.js'; +import { truncate } from './string.js'; + +/** + * Replace a method in an object with a wrapped version of itself. + * + * @param source An object that contains a method to be wrapped. + * @param name The name of the method to be wrapped. + * @param replacementFactory A higher-order function that takes the original version of the given method and returns a + * wrapped version. Note: The function returned by `replacementFactory` needs to be a non-arrow function, in order to + * preserve the correct value of `this`, and the original method must be called using `origMethod.call(this, <other + * args>)` or `origMethod.apply(this, [<other args>])` (rather than being called directly), again to preserve `this`. + * @returns void + */ +function fill(source, name, replacementFactory) { + if (!(name in source)) { + return; + } + + const original = source[name] ; + const wrapped = replacementFactory(original) ; + + // Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work + // otherwise it'll throw "TypeError: Object.defineProperties called on non-object" + if (typeof wrapped === 'function') { + try { + markFunctionWrapped(wrapped, original); + } catch (_Oo) { + // This can throw if multiple fill happens on a global object like XMLHttpRequest + // Fixes https://github.com/getsentry/sentry-javascript/issues/2043 + } + } + + source[name] = wrapped; +} + +/** + * Defines a non-enumerable property on the given object. + * + * @param obj The object on which to set the property + * @param name The name of the property to be set + * @param value The value to which to set the property + */ +function addNonEnumerableProperty(obj, name, value) { + Object.defineProperty(obj, name, { + // enumerable: false, // the default, so we can save on bundle size by not explicitly setting it + value: value, + writable: true, + configurable: true, + }); +} + +/** + * Remembers the original function on the wrapped function and + * patches up the prototype. + * + * @param wrapped the wrapper function + * @param original the original function that gets wrapped + */ +function markFunctionWrapped(wrapped, original) { + const proto = original.prototype || {}; + wrapped.prototype = original.prototype = proto; + addNonEnumerableProperty(wrapped, '__sentry_original__', original); +} + +/** + * This extracts the original function if available. See + * `markFunctionWrapped` for more information. + * + * @param func the function to unwrap + * @returns the unwrapped version of the function if available. + */ +function getOriginalFunction(func) { + return func.__sentry_original__; +} + +/** + * Encodes given object into url-friendly format + * + * @param object An object that contains serializable values + * @returns string Encoded + */ +function urlEncode(object) { + return Object.keys(object) + .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(object[key])}`) + .join('&'); +} + +/** + * Transforms any `Error` or `Event` into a plain object with all of their enumerable properties, and some of their + * non-enumerable properties attached. + * + * @param value Initial source that we have to transform in order for it to be usable by the serializer + * @returns An Event or Error turned into an object - or the value argurment itself, when value is neither an Event nor + * an Error. + */ +function convertToPlainObject(value) + + { + if (isError(value)) { + return { + message: value.message, + name: value.name, + stack: value.stack, + ...getOwnProperties(value), + }; + } else if (isEvent(value)) { + const newObj + + = { + type: value.type, + target: serializeEventTarget(value.target), + currentTarget: serializeEventTarget(value.currentTarget), + ...getOwnProperties(value), + }; + + if (typeof CustomEvent !== 'undefined' && isInstanceOf(value, CustomEvent)) { + newObj.detail = value.detail; + } + + return newObj; + } else { + return value; + } +} + +/** Creates a string representation of the target of an `Event` object */ +function serializeEventTarget(target) { + try { + return isElement(target) ? htmlTreeAsString(target) : Object.prototype.toString.call(target); + } catch (_oO) { + return '<unknown>'; + } +} + +/** Filters out all but an object's own properties */ +function getOwnProperties(obj) { + if (typeof obj === 'object' && obj !== null) { + const extractedProps = {}; + for (const property in obj) { + if (Object.prototype.hasOwnProperty.call(obj, property)) { + extractedProps[property] = (obj )[property]; + } + } + return extractedProps; + } else { + return {}; + } +} + +/** + * Given any captured exception, extract its keys and create a sorted + * and truncated list that will be used inside the event message. + * eg. `Non-error exception captured with keys: foo, bar, baz` + */ +function extractExceptionKeysForMessage(exception, maxLength = 40) { + const keys = Object.keys(convertToPlainObject(exception)); + keys.sort(); + + if (!keys.length) { + return '[object has no keys]'; + } + + if (keys[0].length >= maxLength) { + return truncate(keys[0], maxLength); + } + + for (let includedKeys = keys.length; includedKeys > 0; includedKeys--) { + const serialized = keys.slice(0, includedKeys).join(', '); + if (serialized.length > maxLength) { + continue; + } + if (includedKeys === keys.length) { + return serialized; + } + return truncate(serialized, maxLength); + } + + return ''; +} + +/** + * Given any object, return a new object having removed all fields whose value was `undefined`. + * Works recursively on objects and arrays. + * + * Attention: This function keeps circular references in the returned object. + */ +function dropUndefinedKeys(inputValue) { + // This map keeps track of what already visited nodes map to. + // Our Set - based memoBuilder doesn't work here because we want to the output object to have the same circular + // references as the input object. + const memoizationMap = new Map(); + + // This function just proxies `_dropUndefinedKeys` to keep the `memoBuilder` out of this function's API + return _dropUndefinedKeys(inputValue, memoizationMap); +} + +function _dropUndefinedKeys(inputValue, memoizationMap) { + if (isPlainObject(inputValue)) { + // If this node has already been visited due to a circular reference, return the object it was mapped to in the new object + const memoVal = memoizationMap.get(inputValue); + if (memoVal !== undefined) { + return memoVal ; + } + + const returnValue = {}; + // Store the mapping of this value in case we visit it again, in case of circular data + memoizationMap.set(inputValue, returnValue); + + for (const key of Object.keys(inputValue)) { + if (typeof inputValue[key] !== 'undefined') { + returnValue[key] = _dropUndefinedKeys(inputValue[key], memoizationMap); + } + } + + return returnValue ; + } + + if (Array.isArray(inputValue)) { + // If this node has already been visited due to a circular reference, return the array it was mapped to in the new object + const memoVal = memoizationMap.get(inputValue); + if (memoVal !== undefined) { + return memoVal ; + } + + const returnValue = []; + // Store the mapping of this value in case we visit it again, in case of circular data + memoizationMap.set(inputValue, returnValue); + + inputValue.forEach((item) => { + returnValue.push(_dropUndefinedKeys(item, memoizationMap)); + }); + + return returnValue ; + } + + return inputValue; +} + +/** + * Ensure that something is an object. + * + * Turns `undefined` and `null` into `String`s and all other primitives into instances of their respective wrapper + * classes (String, Boolean, Number, etc.). Acts as the identity function on non-primitives. + * + * @param wat The subject of the objectification + * @returns A version of `wat` which can safely be used with `Object` class methods + */ +function objectify(wat) { + let objectified; + switch (true) { + case wat === undefined || wat === null: + objectified = new String(wat); + break; + + // Though symbols and bigints do have wrapper classes (`Symbol` and `BigInt`, respectively), for whatever reason + // those classes don't have constructors which can be used with the `new` keyword. We therefore need to cast each as + // an object in order to wrap it. + case typeof wat === 'symbol' || typeof wat === 'bigint': + objectified = Object(wat); + break; + + // this will catch the remaining primitives: `String`, `Number`, and `Boolean` + case isPrimitive(wat): + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + objectified = new (wat ).constructor(wat); + break; + + // by process of elimination, at this point we know that `wat` must already be an object + default: + objectified = wat; + break; + } + return objectified; +} + +export { addNonEnumerableProperty, convertToPlainObject, dropUndefinedKeys, extractExceptionKeysForMessage, fill, getOriginalFunction, markFunctionWrapped, objectify, urlEncode }; +//# sourceMappingURL=object.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/promisebuffer.js b/shared/logger/node_modules/@sentry/utils/esm/promisebuffer.js new file mode 100644 index 0000000..fea1ed0 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/promisebuffer.js @@ -0,0 +1,102 @@ +import { SentryError } from './error.js'; +import { rejectedSyncPromise, SyncPromise, resolvedSyncPromise } from './syncpromise.js'; + +/** + * Creates an new PromiseBuffer object with the specified limit + * @param limit max number of promises that can be stored in the buffer + */ +function makePromiseBuffer(limit) { + const buffer = []; + + function isReady() { + return limit === undefined || buffer.length < limit; + } + + /** + * Remove a promise from the queue. + * + * @param task Can be any PromiseLike<T> + * @returns Removed promise. + */ + function remove(task) { + return buffer.splice(buffer.indexOf(task), 1)[0]; + } + + /** + * Add a promise (representing an in-flight action) to the queue, and set it to remove itself on fulfillment. + * + * @param taskProducer A function producing any PromiseLike<T>; In previous versions this used to be `task: + * PromiseLike<T>`, but under that model, Promises were instantly created on the call-site and their executor + * functions therefore ran immediately. Thus, even if the buffer was full, the action still happened. By + * requiring the promise to be wrapped in a function, we can defer promise creation until after the buffer + * limit check. + * @returns The original promise. + */ + function add(taskProducer) { + if (!isReady()) { + return rejectedSyncPromise(new SentryError('Not adding Promise because buffer limit was reached.')); + } + + // start the task and add its promise to the queue + const task = taskProducer(); + if (buffer.indexOf(task) === -1) { + buffer.push(task); + } + void task + .then(() => remove(task)) + // Use `then(null, rejectionHandler)` rather than `catch(rejectionHandler)` so that we can use `PromiseLike` + // rather than `Promise`. `PromiseLike` doesn't have a `.catch` method, making its polyfill smaller. (ES5 didn't + // have promises, so TS has to polyfill when down-compiling.) + .then(null, () => + remove(task).then(null, () => { + // We have to add another catch here because `remove()` starts a new promise chain. + }), + ); + return task; + } + + /** + * Wait for all promises in the queue to resolve or for timeout to expire, whichever comes first. + * + * @param timeout The time, in ms, after which to resolve to `false` if the queue is still non-empty. Passing `0` (or + * not passing anything) will make the promise wait as long as it takes for the queue to drain before resolving to + * `true`. + * @returns A promise which will resolve to `true` if the queue is already empty or drains before the timeout, and + * `false` otherwise + */ + function drain(timeout) { + return new SyncPromise((resolve, reject) => { + let counter = buffer.length; + + if (!counter) { + return resolve(true); + } + + // wait for `timeout` ms and then resolve to `false` (if not cancelled first) + const capturedSetTimeout = setTimeout(() => { + if (timeout && timeout > 0) { + resolve(false); + } + }, timeout); + + // if all promises resolve in time, cancel the timer and resolve to `true` + buffer.forEach(item => { + void resolvedSyncPromise(item).then(() => { + if (!--counter) { + clearTimeout(capturedSetTimeout); + resolve(true); + } + }, reject); + }); + }); + } + + return { + $: buffer, + add, + drain, + }; +} + +export { makePromiseBuffer }; +//# sourceMappingURL=promisebuffer.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/ratelimit.js b/shared/logger/node_modules/@sentry/utils/esm/ratelimit.js new file mode 100644 index 0000000..becfdf0 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/ratelimit.js @@ -0,0 +1,97 @@ +// Intentionally keeping the key broad, as we don't know for sure what rate limit headers get returned from backend + +const DEFAULT_RETRY_AFTER = 60 * 1000; // 60 seconds + +/** + * Extracts Retry-After value from the request header or returns default value + * @param header string representation of 'Retry-After' header + * @param now current unix timestamp + * + */ +function parseRetryAfterHeader(header, now = Date.now()) { + const headerDelay = parseInt(`${header}`, 10); + if (!isNaN(headerDelay)) { + return headerDelay * 1000; + } + + const headerDate = Date.parse(`${header}`); + if (!isNaN(headerDate)) { + return headerDate - now; + } + + return DEFAULT_RETRY_AFTER; +} + +/** + * Gets the time that the given category is disabled until for rate limiting. + * In case no category-specific limit is set but a general rate limit across all categories is active, + * that time is returned. + * + * @return the time in ms that the category is disabled until or 0 if there's no active rate limit. + */ +function disabledUntil(limits, category) { + return limits[category] || limits.all || 0; +} + +/** + * Checks if a category is rate limited + */ +function isRateLimited(limits, category, now = Date.now()) { + return disabledUntil(limits, category) > now; +} + +/** + * Update ratelimits from incoming headers. + * + * @return the updated RateLimits object. + */ +function updateRateLimits( + limits, + { statusCode, headers }, + now = Date.now(), +) { + const updatedRateLimits = { + ...limits, + }; + + // "The name is case-insensitive." + // https://developer.mozilla.org/en-US/docs/Web/API/Headers/get + const rateLimitHeader = headers && headers['x-sentry-rate-limits']; + const retryAfterHeader = headers && headers['retry-after']; + + if (rateLimitHeader) { + /** + * rate limit headers are of the form + * <header>,<header>,.. + * where each <header> is of the form + * <retry_after>: <categories>: <scope>: <reason_code> + * where + * <retry_after> is a delay in seconds + * <categories> is the event type(s) (error, transaction, etc) being rate limited and is of the form + * <category>;<category>;... + * <scope> is what's being limited (org, project, or key) - ignored by SDK + * <reason_code> is an arbitrary string like "org_quota" - ignored by SDK + */ + for (const limit of rateLimitHeader.trim().split(',')) { + const [retryAfter, categories] = limit.split(':', 2); + const headerDelay = parseInt(retryAfter, 10); + const delay = (!isNaN(headerDelay) ? headerDelay : 60) * 1000; // 60sec default + if (!categories) { + updatedRateLimits.all = now + delay; + } else { + for (const category of categories.split(';')) { + updatedRateLimits[category] = now + delay; + } + } + } + } else if (retryAfterHeader) { + updatedRateLimits.all = now + parseRetryAfterHeader(retryAfterHeader, now); + } else if (statusCode === 429) { + updatedRateLimits.all = now + 60 * 1000; + } + + return updatedRateLimits; +} + +export { DEFAULT_RETRY_AFTER, disabledUntil, isRateLimited, parseRetryAfterHeader, updateRateLimits }; +//# sourceMappingURL=ratelimit.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/severity.js b/shared/logger/node_modules/@sentry/utils/esm/severity.js new file mode 100644 index 0000000..3e5128e --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/severity.js @@ -0,0 +1,36 @@ +// Note: Ideally the `SeverityLevel` type would be derived from `validSeverityLevels`, but that would mean either +// +// a) moving `validSeverityLevels` to `@sentry/types`, +// b) moving the`SeverityLevel` type here, or +// c) importing `validSeverityLevels` from here into `@sentry/types`. +// +// Option A would make `@sentry/types` a runtime dependency of `@sentry/utils` (not good), and options B and C would +// create a circular dependency between `@sentry/types` and `@sentry/utils` (also not good). So a TODO accompanying the +// type, reminding anyone who changes it to change this list also, will have to do. + +const validSeverityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug']; + +/** + * Converts a string-based level into a member of the deprecated {@link Severity} enum. + * + * @deprecated `severityFromString` is deprecated. Please use `severityLevelFromString` instead. + * + * @param level String representation of Severity + * @returns Severity + */ +function severityFromString(level) { + return severityLevelFromString(level) ; +} + +/** + * Converts a string-based level into a `SeverityLevel`, normalizing it along the way. + * + * @param level String representation of desired `SeverityLevel`. + * @returns The `SeverityLevel` corresponding to the given string, or 'log' if the string isn't a valid level. + */ +function severityLevelFromString(level) { + return (level === 'warn' ? 'warning' : validSeverityLevels.includes(level) ? level : 'log') ; +} + +export { severityFromString, severityLevelFromString, validSeverityLevels }; +//# sourceMappingURL=severity.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/stacktrace.js b/shared/logger/node_modules/@sentry/utils/esm/stacktrace.js new file mode 100644 index 0000000..ffb3614 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/stacktrace.js @@ -0,0 +1,136 @@ +import { node } from './node-stack-trace.js'; + +const STACKTRACE_FRAME_LIMIT = 50; +// Used to sanitize webpack (error: *) wrapped stack errors +const WEBPACK_ERROR_REGEXP = /\(error: (.*)\)/; + +/** + * Creates a stack parser with the supplied line parsers + * + * StackFrames are returned in the correct order for Sentry Exception + * frames and with Sentry SDK internal frames removed from the top and bottom + * + */ +function createStackParser(...parsers) { + const sortedParsers = parsers.sort((a, b) => a[0] - b[0]).map(p => p[1]); + + return (stack, skipFirst = 0) => { + const frames = []; + const lines = stack.split('\n'); + + for (let i = skipFirst; i < lines.length; i++) { + const line = lines[i]; + // Ignore lines over 1kb as they are unlikely to be stack frames. + // Many of the regular expressions use backtracking which results in run time that increases exponentially with + // input size. Huge strings can result in hangs/Denial of Service: + // https://github.com/getsentry/sentry-javascript/issues/2286 + if (line.length > 1024) { + continue; + } + + // https://github.com/getsentry/sentry-javascript/issues/5459 + // Remove webpack (error: *) wrappers + const cleanedLine = WEBPACK_ERROR_REGEXP.test(line) ? line.replace(WEBPACK_ERROR_REGEXP, '$1') : line; + + // https://github.com/getsentry/sentry-javascript/issues/7813 + // Skip Error: lines + if (cleanedLine.match(/\S*Error: /)) { + continue; + } + + for (const parser of sortedParsers) { + const frame = parser(cleanedLine); + + if (frame) { + frames.push(frame); + break; + } + } + + if (frames.length >= STACKTRACE_FRAME_LIMIT) { + break; + } + } + + return stripSentryFramesAndReverse(frames); + }; +} + +/** + * Gets a stack parser implementation from Options.stackParser + * @see Options + * + * If options contains an array of line parsers, it is converted into a parser + */ +function stackParserFromStackParserOptions(stackParser) { + if (Array.isArray(stackParser)) { + return createStackParser(...stackParser); + } + return stackParser; +} + +/** + * Removes Sentry frames from the top and bottom of the stack if present and enforces a limit of max number of frames. + * Assumes stack input is ordered from top to bottom and returns the reverse representation so call site of the + * function that caused the crash is the last frame in the array. + * @hidden + */ +function stripSentryFramesAndReverse(stack) { + if (!stack.length) { + return []; + } + + const localStack = stack.slice(0, STACKTRACE_FRAME_LIMIT); + + const lastFrameFunction = localStack[localStack.length - 1].function; + // If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call) + if (lastFrameFunction && /sentryWrapped/.test(lastFrameFunction)) { + localStack.pop(); + } + + // Reversing in the middle of the procedure allows us to just pop the values off the stack + localStack.reverse(); + + const firstFrameFunction = localStack[localStack.length - 1].function; + // If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call) + if (firstFrameFunction && /captureMessage|captureException/.test(firstFrameFunction)) { + localStack.pop(); + } + + return localStack.map(frame => ({ + ...frame, + filename: frame.filename || localStack[localStack.length - 1].filename, + function: frame.function || '?', + })); +} + +const defaultFunctionName = '<anonymous>'; + +/** + * Safely extract function name from itself + */ +function getFunctionName(fn) { + try { + if (!fn || typeof fn !== 'function') { + return defaultFunctionName; + } + return fn.name || defaultFunctionName; + } catch (e) { + // Just accessing custom props in some Selenium environments + // can cause a "Permission denied" exception (see raven-js#495). + return defaultFunctionName; + } +} + +/** + * Node.js stack line parser + * + * This is in @sentry/utils so it can be used from the Electron SDK in the browser for when `nodeIntegration == true`. + * This allows it to be used without referencing or importing any node specific code which causes bundlers to complain + */ +function nodeStackLineParser(getModule) { + return [90, node(getModule)]; +} + +export { createStackParser, getFunctionName, nodeStackLineParser, stackParserFromStackParserOptions, stripSentryFramesAndReverse }; +//# sourceMappingURL=stacktrace.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/string.js b/shared/logger/node_modules/@sentry/utils/esm/string.js new file mode 100644 index 0000000..3398545 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/string.js @@ -0,0 +1,132 @@ +import { isString, isRegExp } from './is.js'; + +/** + * Truncates given string to the maximum characters count + * + * @param str An object that contains serializable values + * @param max Maximum number of characters in truncated string (0 = unlimited) + * @returns string Encoded + */ +function truncate(str, max = 0) { + if (typeof str !== 'string' || max === 0) { + return str; + } + return str.length <= max ? str : `${str.slice(0, max)}...`; +} + +/** + * This is basically just `trim_line` from + * https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67 + * + * @param str An object that contains serializable values + * @param max Maximum number of characters in truncated string + * @returns string Encoded + */ +function snipLine(line, colno) { + let newLine = line; + const lineLength = newLine.length; + if (lineLength <= 150) { + return newLine; + } + if (colno > lineLength) { + // eslint-disable-next-line no-param-reassign + colno = lineLength; + } + + let start = Math.max(colno - 60, 0); + if (start < 5) { + start = 0; + } + + let end = Math.min(start + 140, lineLength); + if (end > lineLength - 5) { + end = lineLength; + } + if (end === lineLength) { + start = Math.max(end - 140, 0); + } + + newLine = newLine.slice(start, end); + if (start > 0) { + newLine = `'{snip} ${newLine}`; + } + if (end < lineLength) { + newLine += ' {snip}'; + } + + return newLine; +} + +/** + * Join values in array + * @param input array of values to be joined together + * @param delimiter string to be placed in-between values + * @returns Joined values + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function safeJoin(input, delimiter) { + if (!Array.isArray(input)) { + return ''; + } + + const output = []; + // eslint-disable-next-line @typescript-eslint/prefer-for-of + for (let i = 0; i < input.length; i++) { + const value = input[i]; + try { + output.push(String(value)); + } catch (e) { + output.push('[value cannot be serialized]'); + } + } + + return output.join(delimiter); +} + +/** + * Checks if the given value matches a regex or string + * + * @param value The string to test + * @param pattern Either a regex or a string against which `value` will be matched + * @param requireExactStringMatch If true, `value` must match `pattern` exactly. If false, `value` will match + * `pattern` if it contains `pattern`. Only applies to string-type patterns. + */ +function isMatchingPattern( + value, + pattern, + requireExactStringMatch = false, +) { + if (!isString(value)) { + return false; + } + + if (isRegExp(pattern)) { + return pattern.test(value); + } + if (isString(pattern)) { + return requireExactStringMatch ? value === pattern : value.includes(pattern); + } + + return false; +} + +/** + * Test the given string against an array of strings and regexes. By default, string matching is done on a + * substring-inclusion basis rather than a strict equality basis + * + * @param testString The string to test + * @param patterns The patterns against which to test the string + * @param requireExactStringMatch If true, `testString` must match one of the given string patterns exactly in order to + * count. If false, `testString` will match a string pattern if it contains that pattern. + * @returns + */ +function stringMatchesSomePattern( + testString, + patterns = [], + requireExactStringMatch = false, +) { + return patterns.some(pattern => isMatchingPattern(testString, pattern, requireExactStringMatch)); +} + +export { isMatchingPattern, safeJoin, snipLine, stringMatchesSomePattern, truncate }; +//# sourceMappingURL=string.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/supports.js b/shared/logger/node_modules/@sentry/utils/esm/supports.js new file mode 100644 index 0000000..7a560a3 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/supports.js @@ -0,0 +1,161 @@ +import { logger } from './logger.js'; +import { getGlobalObject } from './worldwide.js'; + +// eslint-disable-next-line deprecation/deprecation +const WINDOW = getGlobalObject(); + +/** + * Tells whether current environment supports ErrorEvent objects + * {@link supportsErrorEvent}. + * + * @returns Answer to the given question. + */ +function supportsErrorEvent() { + try { + new ErrorEvent(''); + return true; + } catch (e) { + return false; + } +} + +/** + * Tells whether current environment supports DOMError objects + * {@link supportsDOMError}. + * + * @returns Answer to the given question. + */ +function supportsDOMError() { + try { + // Chrome: VM89:1 Uncaught TypeError: Failed to construct 'DOMError': + // 1 argument required, but only 0 present. + // @ts-ignore It really needs 1 argument, not 0. + new DOMError(''); + return true; + } catch (e) { + return false; + } +} + +/** + * Tells whether current environment supports DOMException objects + * {@link supportsDOMException}. + * + * @returns Answer to the given question. + */ +function supportsDOMException() { + try { + new DOMException(''); + return true; + } catch (e) { + return false; + } +} + +/** + * Tells whether current environment supports Fetch API + * {@link supportsFetch}. + * + * @returns Answer to the given question. + */ +function supportsFetch() { + if (!('fetch' in WINDOW)) { + return false; + } + + try { + new Headers(); + new Request('http://www.example.com'); + new Response(); + return true; + } catch (e) { + return false; + } +} +/** + * isNativeFetch checks if the given function is a native implementation of fetch() + */ +// eslint-disable-next-line @typescript-eslint/ban-types +function isNativeFetch(func) { + return func && /^function fetch\(\)\s+\{\s+\[native code\]\s+\}$/.test(func.toString()); +} + +/** + * Tells whether current environment supports Fetch API natively + * {@link supportsNativeFetch}. + * + * @returns true if `window.fetch` is natively implemented, false otherwise + */ +function supportsNativeFetch() { + if (!supportsFetch()) { + return false; + } + + // Fast path to avoid DOM I/O + // eslint-disable-next-line @typescript-eslint/unbound-method + if (isNativeFetch(WINDOW.fetch)) { + return true; + } + + // window.fetch is implemented, but is polyfilled or already wrapped (e.g: by a chrome extension) + // so create a "pure" iframe to see if that has native fetch + let result = false; + const doc = WINDOW.document; + // eslint-disable-next-line deprecation/deprecation + if (doc && typeof (doc.createElement ) === 'function') { + try { + const sandbox = doc.createElement('iframe'); + sandbox.hidden = true; + doc.head.appendChild(sandbox); + if (sandbox.contentWindow && sandbox.contentWindow.fetch) { + // eslint-disable-next-line @typescript-eslint/unbound-method + result = isNativeFetch(sandbox.contentWindow.fetch); + } + doc.head.removeChild(sandbox); + } catch (err) { + (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && + logger.warn('Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ', err); + } + } + + return result; +} + +/** + * Tells whether current environment supports ReportingObserver API + * {@link supportsReportingObserver}. + * + * @returns Answer to the given question. + */ +function supportsReportingObserver() { + return 'ReportingObserver' in WINDOW; +} + +/** + * Tells whether current environment supports Referrer Policy API + * {@link supportsReferrerPolicy}. + * + * @returns Answer to the given question. + */ +function supportsReferrerPolicy() { + // Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default' + // (see https://caniuse.com/#feat=referrer-policy), + // it doesn't. And it throws an exception instead of ignoring this parameter... + // REF: https://github.com/getsentry/raven-js/issues/1233 + + if (!supportsFetch()) { + return false; + } + + try { + new Request('_', { + referrerPolicy: 'origin' , + }); + return true; + } catch (e) { + return false; + } +} + +export { isNativeFetch, supportsDOMError, supportsDOMException, supportsErrorEvent, supportsFetch, supportsNativeFetch, supportsReferrerPolicy, supportsReportingObserver }; +//# sourceMappingURL=supports.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/syncpromise.js b/shared/logger/node_modules/@sentry/utils/esm/syncpromise.js new file mode 100644 index 0000000..ad04190 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/syncpromise.js @@ -0,0 +1,191 @@ +import { isThenable } from './is.js'; + +/* eslint-disable @typescript-eslint/explicit-function-return-type */ + +/** SyncPromise internal states */ +var States; (function (States) { + /** Pending */ + const PENDING = 0; States[States["PENDING"] = PENDING] = "PENDING"; + /** Resolved / OK */ + const RESOLVED = 1; States[States["RESOLVED"] = RESOLVED] = "RESOLVED"; + /** Rejected / Error */ + const REJECTED = 2; States[States["REJECTED"] = REJECTED] = "REJECTED"; +})(States || (States = {})); + +// Overloads so we can call resolvedSyncPromise without arguments and generic argument + +/** + * Creates a resolved sync promise. + * + * @param value the value to resolve the promise with + * @returns the resolved sync promise + */ +function resolvedSyncPromise(value) { + return new SyncPromise(resolve => { + resolve(value); + }); +} + +/** + * Creates a rejected sync promise. + * + * @param value the value to reject the promise with + * @returns the rejected sync promise + */ +function rejectedSyncPromise(reason) { + return new SyncPromise((_, reject) => { + reject(reason); + }); +} + +/** + * Thenable class that behaves like a Promise and follows it's interface + * but is not async internally + */ +class SyncPromise { + __init() {this._state = States.PENDING;} + __init2() {this._handlers = [];} + + constructor( + executor, + ) {SyncPromise.prototype.__init.call(this);SyncPromise.prototype.__init2.call(this);SyncPromise.prototype.__init3.call(this);SyncPromise.prototype.__init4.call(this);SyncPromise.prototype.__init5.call(this);SyncPromise.prototype.__init6.call(this); + try { + executor(this._resolve, this._reject); + } catch (e) { + this._reject(e); + } + } + + /** JSDoc */ + then( + onfulfilled, + onrejected, + ) { + return new SyncPromise((resolve, reject) => { + this._handlers.push([ + false, + result => { + if (!onfulfilled) { + // TODO: ¯\_(ツ)_/¯ + // TODO: FIXME + resolve(result ); + } else { + try { + resolve(onfulfilled(result)); + } catch (e) { + reject(e); + } + } + }, + reason => { + if (!onrejected) { + reject(reason); + } else { + try { + resolve(onrejected(reason)); + } catch (e) { + reject(e); + } + } + }, + ]); + this._executeHandlers(); + }); + } + + /** JSDoc */ + catch( + onrejected, + ) { + return this.then(val => val, onrejected); + } + + /** JSDoc */ + finally(onfinally) { + return new SyncPromise((resolve, reject) => { + let val; + let isRejected; + + return this.then( + value => { + isRejected = false; + val = value; + if (onfinally) { + onfinally(); + } + }, + reason => { + isRejected = true; + val = reason; + if (onfinally) { + onfinally(); + } + }, + ).then(() => { + if (isRejected) { + reject(val); + return; + } + + resolve(val ); + }); + }); + } + + /** JSDoc */ + __init3() {this._resolve = (value) => { + this._setResult(States.RESOLVED, value); + };} + + /** JSDoc */ + __init4() {this._reject = (reason) => { + this._setResult(States.REJECTED, reason); + };} + + /** JSDoc */ + __init5() {this._setResult = (state, value) => { + if (this._state !== States.PENDING) { + return; + } + + if (isThenable(value)) { + void (value ).then(this._resolve, this._reject); + return; + } + + this._state = state; + this._value = value; + + this._executeHandlers(); + };} + + /** JSDoc */ + __init6() {this._executeHandlers = () => { + if (this._state === States.PENDING) { + return; + } + + const cachedHandlers = this._handlers.slice(); + this._handlers = []; + + cachedHandlers.forEach(handler => { + if (handler[0]) { + return; + } + + if (this._state === States.RESOLVED) { + // eslint-disable-next-line @typescript-eslint/no-floating-promises + handler[1](this._value ); + } + + if (this._state === States.REJECTED) { + handler[2](this._value); + } + + handler[0] = true; + }); + };} +} + +export { SyncPromise, rejectedSyncPromise, resolvedSyncPromise }; +//# sourceMappingURL=syncpromise.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/time.js b/shared/logger/node_modules/@sentry/utils/esm/time.js new file mode 100644 index 0000000..2703ed2 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/time.js @@ -0,0 +1,183 @@ +import { isNodeEnv, dynamicRequire } from './node.js'; +import { getGlobalObject } from './worldwide.js'; + +// eslint-disable-next-line deprecation/deprecation +const WINDOW = getGlobalObject(); + +/** + * An object that can return the current timestamp in seconds since the UNIX epoch. + */ + +/** + * A TimestampSource implementation for environments that do not support the Performance Web API natively. + * + * Note that this TimestampSource does not use a monotonic clock. A call to `nowSeconds` may return a timestamp earlier + * than a previously returned value. We do not try to emulate a monotonic behavior in order to facilitate debugging. It + * is more obvious to explain "why does my span have negative duration" than "why my spans have zero duration". + */ +const dateTimestampSource = { + nowSeconds: () => Date.now() / 1000, +}; + +/** + * A partial definition of the [Performance Web API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Performance} + * for accessing a high-resolution monotonic clock. + */ + +/** + * Returns a wrapper around the native Performance API browser implementation, or undefined for browsers that do not + * support the API. + * + * Wrapping the native API works around differences in behavior from different browsers. + */ +function getBrowserPerformance() { + const { performance } = WINDOW; + if (!performance || !performance.now) { + return undefined; + } + + // Replace performance.timeOrigin with our own timeOrigin based on Date.now(). + // + // This is a partial workaround for browsers reporting performance.timeOrigin such that performance.timeOrigin + + // performance.now() gives a date arbitrarily in the past. + // + // Additionally, computing timeOrigin in this way fills the gap for browsers where performance.timeOrigin is + // undefined. + // + // The assumption that performance.timeOrigin + performance.now() ~= Date.now() is flawed, but we depend on it to + // interact with data coming out of performance entries. + // + // Note that despite recommendations against it in the spec, browsers implement the Performance API with a clock that + // might stop when the computer is asleep (and perhaps under other circumstances). Such behavior causes + // performance.timeOrigin + performance.now() to have an arbitrary skew over Date.now(). In laptop computers, we have + // observed skews that can be as long as days, weeks or months. + // + // See https://github.com/getsentry/sentry-javascript/issues/2590. + // + // BUG: despite our best intentions, this workaround has its limitations. It mostly addresses timings of pageload + // transactions, but ignores the skew built up over time that can aversely affect timestamps of navigation + // transactions of long-lived web pages. + const timeOrigin = Date.now() - performance.now(); + + return { + now: () => performance.now(), + timeOrigin, + }; +} + +/** + * Returns the native Performance API implementation from Node.js. Returns undefined in old Node.js versions that don't + * implement the API. + */ +function getNodePerformance() { + try { + const perfHooks = dynamicRequire(module, 'perf_hooks') ; + return perfHooks.performance; + } catch (_) { + return undefined; + } +} + +/** + * The Performance API implementation for the current platform, if available. + */ +const platformPerformance = isNodeEnv() ? getNodePerformance() : getBrowserPerformance(); + +const timestampSource = + platformPerformance === undefined + ? dateTimestampSource + : { + nowSeconds: () => (platformPerformance.timeOrigin + platformPerformance.now()) / 1000, + }; + +/** + * Returns a timestamp in seconds since the UNIX epoch using the Date API. + */ +const dateTimestampInSeconds = dateTimestampSource.nowSeconds.bind(dateTimestampSource); + +/** + * Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the + * availability of the Performance API. + * + * See `usingPerformanceAPI` to test whether the Performance API is used. + * + * BUG: Note that because of how browsers implement the Performance API, the clock might stop when the computer is + * asleep. This creates a skew between `dateTimestampInSeconds` and `timestampInSeconds`. The + * skew can grow to arbitrary amounts like days, weeks or months. + * See https://github.com/getsentry/sentry-javascript/issues/2590. + */ +const timestampInSeconds = timestampSource.nowSeconds.bind(timestampSource); + +/** + * Re-exported with an old name for backwards-compatibility. + * TODO (v8): Remove this + * + * @deprecated Use `timestampInSeconds` instead. + */ +const timestampWithMs = timestampInSeconds; + +/** + * A boolean that is true when timestampInSeconds uses the Performance API to produce monotonic timestamps. + */ +const usingPerformanceAPI = platformPerformance !== undefined; + +/** + * Internal helper to store what is the source of browserPerformanceTimeOrigin below. For debugging only. + */ +let _browserPerformanceTimeOriginMode; + +/** + * The number of milliseconds since the UNIX epoch. This value is only usable in a browser, and only when the + * performance API is available. + */ +const browserPerformanceTimeOrigin = (() => { + // Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or + // performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin + // data as reliable if they are within a reasonable threshold of the current time. + + const { performance } = WINDOW; + if (!performance || !performance.now) { + _browserPerformanceTimeOriginMode = 'none'; + return undefined; + } + + const threshold = 3600 * 1000; + const performanceNow = performance.now(); + const dateNow = Date.now(); + + // if timeOrigin isn't available set delta to threshold so it isn't used + const timeOriginDelta = performance.timeOrigin + ? Math.abs(performance.timeOrigin + performanceNow - dateNow) + : threshold; + const timeOriginIsReliable = timeOriginDelta < threshold; + + // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin + // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. + // Also as of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always + // a valid fallback. In the absence of an initial time provided by the browser, fallback to the current time from the + // Date API. + // eslint-disable-next-line deprecation/deprecation + const navigationStart = performance.timing && performance.timing.navigationStart; + const hasNavigationStart = typeof navigationStart === 'number'; + // if navigationStart isn't available set delta to threshold so it isn't used + const navigationStartDelta = hasNavigationStart ? Math.abs(navigationStart + performanceNow - dateNow) : threshold; + const navigationStartIsReliable = navigationStartDelta < threshold; + + if (timeOriginIsReliable || navigationStartIsReliable) { + // Use the more reliable time origin + if (timeOriginDelta <= navigationStartDelta) { + _browserPerformanceTimeOriginMode = 'timeOrigin'; + return performance.timeOrigin; + } else { + _browserPerformanceTimeOriginMode = 'navigationStart'; + return navigationStart; + } + } + + // Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date. + _browserPerformanceTimeOriginMode = 'dateNow'; + return dateNow; +})(); + +export { _browserPerformanceTimeOriginMode, browserPerformanceTimeOrigin, dateTimestampInSeconds, timestampInSeconds, timestampWithMs, usingPerformanceAPI }; +//# sourceMappingURL=time.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/tracing.js b/shared/logger/node_modules/@sentry/utils/esm/tracing.js new file mode 100644 index 0000000..546507c --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/tracing.js @@ -0,0 +1,39 @@ +const TRACEPARENT_REGEXP = new RegExp( + '^[ \\t]*' + // whitespace + '([0-9a-f]{32})?' + // trace_id + '-?([0-9a-f]{16})?' + // span_id + '-?([01])?' + // sampled + '[ \\t]*$', // whitespace +); + +/** + * Extract transaction context data from a `sentry-trace` header. + * + * @param traceparent Traceparent string + * + * @returns Object containing data from the header, or undefined if traceparent string is malformed + */ +function extractTraceparentData(traceparent) { + const matches = traceparent.match(TRACEPARENT_REGEXP); + + if (!traceparent || !matches) { + // empty string or no matches is invalid traceparent data + return undefined; + } + + let parentSampled; + if (matches[3] === '1') { + parentSampled = true; + } else if (matches[3] === '0') { + parentSampled = false; + } + + return { + traceId: matches[1], + parentSampled, + parentSpanId: matches[2], + }; +} + +export { TRACEPARENT_REGEXP, extractTraceparentData }; +//# sourceMappingURL=tracing.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/url.js b/shared/logger/node_modules/@sentry/utils/esm/url.js new file mode 100644 index 0000000..2e0bd40 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/url.js @@ -0,0 +1,72 @@ +/** + * Parses string form of URL into an object + * // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B + * // intentionally using regex and not <a/> href parsing trick because React Native and other + * // environments where DOM might not be available + * @returns parsed URL object + */ +function parseUrl(url) { + if (!url) { + return {}; + } + + const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/); + + if (!match) { + return {}; + } + + // coerce to undefined values to empty string so we don't get 'undefined' + const query = match[6] || ''; + const fragment = match[8] || ''; + return { + host: match[4], + path: match[5], + protocol: match[2], + search: query, + hash: fragment, + relative: match[5] + query + fragment, // everything minus origin + }; +} + +/** + * Strip the query string and fragment off of a given URL or path (if present) + * + * @param urlPath Full URL or path, including possible query string and/or fragment + * @returns URL or path without query string or fragment + */ +function stripUrlQueryAndFragment(urlPath) { + // eslint-disable-next-line no-useless-escape + return urlPath.split(/[\?#]/, 1)[0]; +} + +/** + * Returns number of URL segments of a passed string URL. + */ +function getNumberOfUrlSegments(url) { + // split at '/' or at '\/' to split regex urls correctly + return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length; +} + +/** + * Takes a URL object and returns a sanitized string which is safe to use as span description + * see: https://develop.sentry.dev/sdk/data-handling/#structuring-data + */ +function getSanitizedUrlString(url) { + const { protocol, host, path } = url; + + const filteredHost = + (host && + host + // Always filter out authority + .replace(/^.*@/, '[filtered]:[filtered]@') + // Don't show standard :80 (http) and :443 (https) ports to reduce the noise + .replace(':80', '') + .replace(':443', '')) || + ''; + + return `${protocol ? `${protocol}://` : ''}${filteredHost}${path}`; +} + +export { getNumberOfUrlSegments, getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment }; +//# sourceMappingURL=url.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/vendor/supportsHistory.js b/shared/logger/node_modules/@sentry/utils/esm/vendor/supportsHistory.js new file mode 100644 index 0000000..cf33d64 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/vendor/supportsHistory.js @@ -0,0 +1,29 @@ +import { getGlobalObject } from '../worldwide.js'; + +// Based on https://github.com/angular/angular.js/pull/13945/files + +// eslint-disable-next-line deprecation/deprecation +const WINDOW = getGlobalObject(); + +/** + * Tells whether current environment supports History API + * {@link supportsHistory}. + * + * @returns Answer to the given question. + */ +function supportsHistory() { + // NOTE: in Chrome App environment, touching history.pushState, *even inside + // a try/catch block*, will cause Chrome to output an error to console.error + // borrowed from: https://github.com/angular/angular.js/pull/13945/files + /* eslint-disable @typescript-eslint/no-unsafe-member-access */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const chrome = (WINDOW ).chrome; + const isChromePackagedApp = chrome && chrome.app && chrome.app.runtime; + /* eslint-enable @typescript-eslint/no-unsafe-member-access */ + const hasHistoryApi = 'history' in WINDOW && !!WINDOW.history.pushState && !!WINDOW.history.replaceState; + + return !isChromePackagedApp && hasHistoryApi; +} + +export { supportsHistory }; +//# sourceMappingURL=supportsHistory.js.map diff --git a/shared/logger/node_modules/@sentry/utils/esm/worldwide.js b/shared/logger/node_modules/@sentry/utils/esm/worldwide.js new file mode 100644 index 0000000..4c54705 --- /dev/null +++ b/shared/logger/node_modules/@sentry/utils/esm/worldwide.js @@ -0,0 +1,70 @@ +/** Internal global with common properties and Sentry extensions */ + +// The code below for 'isGlobalObj' and 'GLOBAL_OBJ' was copied from core-js before modification +// https://github.com/zloirock/core-js/blob/1b944df55282cdc99c90db5f49eb0b6eda2cc0a3/packages/core-js/internals/global.js +// core-js has the following licence: +// +// Copyright (c) 2014-2022 Denis Pushkarev +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +/** Returns 'obj' if it's the global object, otherwise returns undefined */ +function isGlobalObj(obj) { + return obj && obj.Math == Math ? obj : undefined; +} + +/** Get's the global object for the current JavaScript runtime */ +const GLOBAL_OBJ = + (typeof globalThis == 'object' && isGlobalObj(globalThis)) || + // eslint-disable-next-line no-restricted-globals + (typeof window == 'object' && isGlobalObj(window)) || + (typeof self == 'object' && isGlobalObj(self)) || + (typeof global == 'object' && isGlobalObj(global)) || + (function () { + return this; + })() || + {}; + +/** + * @deprecated Use GLOBAL_OBJ instead or WINDOW from @sentry/browser. This will be removed in v8 + */ +function getGlobalObject() { + return GLOBAL_OBJ ; +} + +/** + * Returns a global singleton contained in the global `__SENTRY__` object. + * + * If the singleton doesn't already exist in `__SENTRY__`, it will be created using the given factory + * function and added to the `__SENTRY__` object. + * + * @param name name of the global singleton on __SENTRY__ + * @param creator creator Factory function to create the singleton if it doesn't already exist on `__SENTRY__` + * @param obj (Optional) The global object on which to look for `__SENTRY__`, if not `GLOBAL_OBJ`'s return value + * @returns the singleton + */ +function getGlobalSingleton(name, creator, obj) { + const gbl = (obj || GLOBAL_OBJ) ; + const __SENTRY__ = (gbl.__SENTRY__ = gbl.__SENTRY__ || {}); + const singleton = __SENTRY__[name] || (__SENTRY__[name] = creator()); + return singleton; +} + +export { GLOBAL_OBJ, getGlobalObject, getGlobalSingleton }; +//# sourceMappingURL=worldwide.js.map diff --git a/shared/logger/src/base.ts b/shared/logger/src/base.ts new file mode 100644 index 0000000..1f6df0b --- /dev/null +++ b/shared/logger/src/base.ts @@ -0,0 +1,67 @@ +import type { Level, Logger } from './types'; + +export abstract class BaseLogger<Args extends unknown[] = unknown[]> + implements Logger<Args> +{ + constructor(protected readonly name: string) {} + + /** + * Log a debug level message. + * Appropriate for verbose logging that explains steps/details of the inner state of + * a code unit. + * + * Example uses include in a size-constrain datastructure, logging when the size + * exceeds the threshold and elements are removed, or in a virtual scrolling + * component logging when a scroll event causes a new page of elements to be loaded. + * + * @param args Arguments to log (same as console.debug) + * @return empty string (for use in brackets {} in svelte components) + */ + debug(...args: Args): string { + return this.log('debug', ...args); + } + + /** + * Log an info level message. + * Appropriate for informational messages that may be relevant to consumers of a code + * unit. + * + * Example uses include a router logging when transitions occur or a button logging + * clicks. + * + * @param args Arguments to log (same as console.info) + * @return empty string (for use in brackets {} in svelte components) + */ + info(...args: Args): string { + return this.log('info', ...args); + } + + /** + * Log a warn level message. + * Appropriate for situations where state has been (or likely will be) corrupted or + * invariants have been broken. + * + * Example uses include a data structure warning when it is used before being fully + * initialized. + * + * @param args Arguments to log (same as console.warn) + * @return empty string (for use in brackets {} in svelte components) + */ + warn(...args: Args): string { + return this.log('warn', ...args); + } + + /** + * Log an error message. + * Appropriate for thrown errors or situations where the apps breaks or has to + * engage in fallback behavior to avoid a more catastrophic failure. + * + * @param args Arguments to log (same as console.error) + * @return empty string (for use in brackets {} in svelte components) + */ + error(...args: Args): string { + return this.log('error', ...args); + } + + protected abstract log(method: Level, ...args: Args): string; +} diff --git a/shared/logger/src/composite.ts b/shared/logger/src/composite.ts new file mode 100644 index 0000000..a3c4d69 --- /dev/null +++ b/shared/logger/src/composite.ts @@ -0,0 +1,92 @@ +import type { LoggerFactory, Logger } from './types'; + +export class CompositeLoggerFactory implements LoggerFactory { + private readonly factories: LoggerFactory[]; + + constructor(factories: LoggerFactory[]) { + this.factories = factories; + } + + loggerFor(name: string): Logger { + return new CompositeLogger( + this.factories.map((factory) => factory.loggerFor(name)), + ); + } +} + +export class CompositeLogger implements Logger { + private readonly loggers: Logger[]; + + constructor(loggers: Logger[]) { + this.loggers = loggers; + } + + /** + * Log a debug level message. + * Appropriate for verbose logging that explains steps/details of the inner state of + * a code unit. + * + * Example uses include in a size-constrain datastructure, logging when the size + * exceeds the threshold and elements are removed, or in a virtual scrolling + * component logging when a scroll event causes a new page of elements to be loaded. + * + * @param args Arguments to log (same as console.debug) + * @return empty string (for use in brackets {} in svelte components) + */ + debug(...args: unknown[]): string { + return this.callAll('debug', args); + } + + /** + * Log an info level message. + * Appropriate for informational messages that may be relevant to consumers of a code + * unit. + * + * Example uses include a router logging when transitions occur or a button logging + * clicks. + * + * @param args Arguments to log (same as console.info) + * @return empty string (for use in brackets {} in svelte components) + */ + info(...args: unknown[]): string { + return this.callAll('info', args); + } + + /** + * Log a warn level message. + * Appropriate for situations where state has been (or likely will be) corrupted or + * invariants have been broken. + * + * Example uses include a data structure warning when it is used before being fully + * initialized. + * + * @param args Arguments to log (same as console.warn) + * @return empty string (for use in brackets {} in svelte components) + */ + warn(...args: unknown[]): string { + return this.callAll('warn', args); + } + + /** + * Log an error message. + * Appropriate for thrown errors or situations where the apps breaks or has to + * engage in fallback behavior to avoid a more catastrophic failure. + * + * @param args Arguments to log (same as console.error) + * @return empty string (for use in brackets {} in svelte components) + */ + error(...args: unknown[]): string { + return this.callAll('error', args); + } + + private callAll( + method: 'debug' | 'info' | 'warn' | 'error', + args: unknown[], + ): string { + for (const logger of this.loggers) { + logger[method](...args); + } + + return ''; + } +} diff --git a/shared/logger/src/console.ts b/shared/logger/src/console.ts new file mode 100644 index 0000000..408002b --- /dev/null +++ b/shared/logger/src/console.ts @@ -0,0 +1,29 @@ +import { BaseLogger } from './base'; +import type { Level, LoggerFactory, Logger } from './types'; +import { shouldLog } from './local-storage-filter'; + +export class ConsoleLoggerFactory implements LoggerFactory { + loggerFor(name: string): Logger { + return new ConsoleLogger(name); + } +} + +export class ConsoleLogger extends BaseLogger { + protected log(method: Level, ...args: unknown[]): string { + if (!shouldLog(this.name, method)) { + return ''; + } + + const log = console[method]; + const prefix = `[${this.name}]`; + const [firstArg, ...rest] = args; + + if (typeof firstArg === 'string') { + log(`${prefix} ${firstArg}`, ...rest); + } else { + log(prefix, ...args); + } + + return ''; + } +} diff --git a/shared/logger/src/errorkit/errorkit-logger.ts b/shared/logger/src/errorkit/errorkit-logger.ts new file mode 100644 index 0000000..1290c41 --- /dev/null +++ b/shared/logger/src/errorkit/errorkit-logger.ts @@ -0,0 +1,93 @@ +import type { ErrorHub, ValueOf } from './types'; +import type { LoggerFactory, Logger } from '../types'; + +/** + * Determines the level of logs to send to sentry. + * + */ +export const ERROR_REPORT_LEVEL = { + error: 'error', + error_warn: 'error_warn', +} as const; + +type ReportLevel = ValueOf<typeof ERROR_REPORT_LEVEL>; + +export class ErrorKitLoggerFactory implements LoggerFactory { + private readonly errorKit: ErrorHub; + private readonly reportLevel: ReportLevel; + constructor(errorKit: ErrorHub, reportLevel?: ReportLevel) { + this.errorKit = errorKit; + this.reportLevel = reportLevel ?? ERROR_REPORT_LEVEL.error; + } + loggerFor(name: string): Logger { + return new ErrorKitLogger(name, this.errorKit, this.reportLevel); + } +} + +interface HasToString { + toString(): string; +} + +export class ErrorKitLogger implements Logger { + private readonly name: string; + private readonly errorKit: ErrorHub; + private readonly reportLevel: ReportLevel; + constructor(name: string, errorKit: ErrorHub, reportLevel: ReportLevel) { + this.name = name; + this.errorKit = errorKit; + this.reportLevel = reportLevel; + } + + private stringifyConsoleArgs(...args: unknown[]): string { + return args.reduce((acc: string, val: unknown) => { + let tempVal: HasToString; + switch (true) { + case val instanceof Error: { + tempVal = (val as unknown as InstanceType<typeof Error>) + .message; + break; + } + case typeof val === 'object': { + try { + tempVal = JSON.stringify(val); + } catch (e) { + tempVal = `failed to stringify ${val}`; + } + break; + } + case typeof val === 'undefined' || val === null: { + tempVal = `${val}`; + break; + } + default: { + tempVal = val as HasToString; + } + } + + return `${acc} ${tempVal.toString()}`; + }, `[${this.name}]`) as string; + } + + debug(..._args: unknown[]): string { + return ''; + } + info(..._args: unknown[]): string { + return ''; + } + warn(...args: unknown[]): string { + if (this.reportLevel === ERROR_REPORT_LEVEL.error_warn) { + this.errorKit.captureMessage(this.stringifyConsoleArgs(...args)); + } + return ''; + } + error(...args: unknown[]): string { + const errors = args.filter((item) => item instanceof Error) as Error[]; + const message = this.stringifyConsoleArgs(...args); + + const error = errors.length === 0 ? new Error(message) : errors[0]; + error.message = message; + + this.errorKit.captureException(error); + return ''; + } +} diff --git a/shared/logger/src/errorkit/errorkit.ts b/shared/logger/src/errorkit/errorkit.ts new file mode 100644 index 0000000..dd40e26 --- /dev/null +++ b/shared/logger/src/errorkit/errorkit.ts @@ -0,0 +1,108 @@ +import { Severity } from '@sentry/types'; +import type { Logger, LoggerFactory } from '../types'; +import type { + captureException, + captureMessage, + addBreadcrumb, + ErrorHub, + ErrorKitConfig, +} from './types'; + +type PartialSentryModule = { + captureException: typeof captureException; + captureMessage: typeof captureMessage; + addBreadcrumb: typeof addBreadcrumb; +}; + +export type ErrorKitInstance = InstanceType<typeof ErrorKit>; + +export const setupErrorKit = ( + config: ErrorKitConfig, + loggerFactory: LoggerFactory, +): ErrorKitInstance | undefined => { + if (typeof window === 'undefined') return; + const log = loggerFactory.loggerFor('errorkit'); + const isMultiDev = window.location.href.includes('multidev'); + const BUILD_ENV = process.env.NODE_ENV; + const isErrorKitEnabled = BUILD_ENV === 'production' && !isMultiDev; + + const initializeErrorKit = + async (): Promise<PartialSentryModule | null> => { + let sentry: PartialSentryModule | null = null; + + if (isErrorKitEnabled) { + try { + const { createSentryConfig } = await import( + '@amp-metrics/sentrykit' + ); + const Sentry = await import('@sentry/browser'); + Sentry.init(createSentryConfig(config)); + + sentry = { + addBreadcrumb: Sentry.addBreadcrumb, + captureException: Sentry.captureException, + captureMessage: Sentry.captureMessage, + }; + } catch (e) { + log.error('something went wrong setting up errorKit', e); + } + } + + return sentry; + }; + + return new ErrorKit(initializeErrorKit(), log, isErrorKitEnabled); +}; + +class ErrorKit implements ErrorHub { + private readonly sentry: Promise<PartialSentryModule | null>; + private readonly logger: Logger; + private readonly isErrorKitEnabled: boolean; + constructor( + sentry: Promise<PartialSentryModule | null>, + log: Logger, + isErrorKitEnabled: boolean, + ) { + this.sentry = sentry; + this.logger = log; + this.isErrorKitEnabled = isErrorKitEnabled; + + if (!isErrorKitEnabled) { + log.debug('errorkit is disabled'); + } + } + + async captureMessage(message: string) { + if (!this.isErrorKitEnabled) return; + const sentry = await this.sentry; + + if (sentry) { + sentry.addBreadcrumb({ + category: 'log.warn', + level: Severity.Warning, + }); + sentry.captureMessage(message, Severity.Warning); + } else { + this.logger.warn(`${message} was not sent to errorKit`); + } + } + + async captureException(exception: Error) { + if (!this.isErrorKitEnabled) return; + const sentry = await this.sentry; + + if (sentry) { + sentry.addBreadcrumb({ + type: 'error', + category: 'error', + level: Severity.Error, + }); + sentry.captureException(exception); + } else { + this.logger.warn( + `The following exception was not sent to errorKit:`, + exception, + ); + } + } +} diff --git a/shared/logger/src/index.ts b/shared/logger/src/index.ts new file mode 100644 index 0000000..dc786e3 --- /dev/null +++ b/shared/logger/src/index.ts @@ -0,0 +1,31 @@ +import { getContext } from 'svelte'; +import type { Logger, LoggerFactory } from './types'; + +export * from './composite'; +export * from './console'; +export * from './deferred'; +export * from './recording'; +export * from './sampled'; +export * from './types'; +export * from './void'; + +const CONTEXT_NAME = 'loggerFactory'; + +export function setContext( + context: Map<string, unknown>, + factory: LoggerFactory, +): void { + context.set(CONTEXT_NAME, factory); +} + +export function loggerFor(subject: string): Logger { + const factory = getContext(CONTEXT_NAME) as LoggerFactory | undefined; + + if (!factory) { + throw new Error( + 'loggerFor called before setContext or outside of svelte component init', + ); + } + + return factory.loggerFor(subject); +} diff --git a/shared/logger/src/local-storage-filter.ts b/shared/logger/src/local-storage-filter.ts new file mode 100644 index 0000000..18a42fa --- /dev/null +++ b/shared/logger/src/local-storage-filter.ts @@ -0,0 +1,122 @@ +export type Level = 'debug' | 'info' | 'warn' | 'error'; +// Numbers correspond to the levels above, with 0 meaning "no level" +type LevelNum = 4 | 3 | 2 | 1 | 0; + +interface Rules { + named?: Record<string, LevelNum>; + defaultLevel?: LevelNum; +} + +const LEVEL_TO_NUM: Record<Level | 'off' | '*' | '', LevelNum> = { + '*': 4, + debug: 4, + info: 3, + warn: 2, + error: 1, + off: 0, + '': 0, +}; + +/** + * Parses log filtering instructions from localStorage.onyxLog. + * The instructions are a series of comma separated directives that restrict + * logging. Restrictions indicate the highest log level that a named logger + * will emit. The name of the logger is the string passed to + * LoggerFactory.loggerFor. + * + * By default (ex. empty rule string), no logs will be emitted. + * + * The format of the directives is NAME=LEVEL. LEVEL can be one of: + * + * - * - all levels are logged (debug, info, warn, error) + * - debug - same as above + * - info - everything but debug is logged + * - warn - everything but info and debug is logged + * - error - only errors are logged + * - off (or empty string, ex. "MyClass=") - nothing will be logged + * + * Some examples: + * + * - '*=*' will emit all log levels from all loggers + * - '*=info,Foo=off' will emit everything but debug except or logs from + * the named logger Foo (which will be entirely suppressed) + * - 'Bar=error,Baz=warn' will emit errors from Bar and Baz and warnings from + * Baz + * + * NOTE: Keep this in sync with README.md! + */ +function parseRules(): Rules { + const onyxLog: string = (() => { + try { + // The typeof check is for SSR + return ( + (typeof window !== 'undefined' + ? window.localStorage.onyxLog + : '') || '' + ); + } catch { + // window.localStorage will throw when referenced (at all) when + // Chrome has it disabled + // See: rdar://93367396 (Guard localStorage and sessionStorage use) + return ''; + } + })(); + + const PRODUCTION_DEFAULT = {}; // no logs unless specified + const DEV_DEFAULT = { + defaultLevel: LEVEL_TO_NUM['*'], // All logs unless specified + }; + const isDevelopment = (() => { + // This is a little tricky. The ENV var is not real. It's replaced by + // rollup-plugin-replace. Thus, we can't do the usual of testing for + // the existence of `process` and then doing `process?.env` etc. + // Instead, we just try the whole thing and try/catch. This way, + // rollup-plugin-replace sees that entire string verbatim and can + // replace it with the proper environment. + try { + // @ts-ignore + return process.env.NODE_ENV !== 'production'; + } catch { + return false; + } + })(); + const defaultRules = isDevelopment ? DEV_DEFAULT : PRODUCTION_DEFAULT; + + // If the localStorage is specified, start from a clean slate. Otherwise, + // use the environment default + const rules: Rules = onyxLog.length > 0 ? {} : defaultRules; + + for (const directive of onyxLog.split(',').filter((v) => v)) { + // Invalid directive, must be of the form 'name=level' + const parts = directive.split('='); + if (parts.length !== 2) { + continue; + } + + const [name, maxLevelName] = parts; + const maxLevel = + LEVEL_TO_NUM[maxLevelName as keyof typeof LEVEL_TO_NUM]; + + // Invalid level + if (typeof maxLevel === 'undefined') { + continue; + } + + if (name === '*') { + rules.defaultLevel = maxLevel; + } else { + rules.named = rules.named ?? {}; + rules.named[name] = maxLevel; + } + } + + return rules; +} + +export function shouldLog(name: string, level: Level): boolean { + const rules = parseRules(); + + // Rules for the named logger take precedence over the default + const maxLevel = (rules.named || {})[name] ?? rules.defaultLevel ?? 0; + return LEVEL_TO_NUM[level] <= maxLevel; +} diff --git a/shared/metrics-8/node_modules/@amp-metrics/ae-client-kit-core/dist/ae-client-kit-core.esm.js b/shared/metrics-8/node_modules/@amp-metrics/ae-client-kit-core/dist/ae-client-kit-core.esm.js new file mode 100644 index 0000000..c5828f0 --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/ae-client-kit-core/dist/ae-client-kit-core.esm.js @@ -0,0 +1,901 @@ +import { reflect, string, storage } from '@amp-metrics/mt-metricskit-utils-private'; + +/* + * src/helpers/constants.js + * ae-client-kit-core + * + * Copyright © 2019 Apple Inc. All rights reserved. + * + */ + +var _allBaseFieldNames; +var _environmentBaseFieldNames; + +function requiredEnvironmentBaseFieldNames() { + return [ + 'app', + 'appVersion', + 'hardwareFamily', + 'hardwareModel', + 'os', + 'osBuildNumber', + 'osLanguages', + 'osVersion', + 'resourceRevNum', + 'screenHeight', + 'screenWidth', + 'userAgent' + ]; +} + +function optionalEnvironmentBaseFieldNames() { + return ['delegateApp', 'hardwareBrand', 'storeFrontCountryCode', 'storeFrontHeader', 'storeFrontLanguage']; +} + +function otherBaseFieldNames() { + return [ + 'baseVersion', + 'clientEventId', + 'connection', + 'eventTime', + 'eventType', + 'eventVersion', + 'timezoneOffset', + 'xpPostFrequency', + 'xpSendMethod' + ]; +} + +function environmentBaseFieldNames() { + if (!_environmentBaseFieldNames) { + _environmentBaseFieldNames = requiredEnvironmentBaseFieldNames().concat(optionalEnvironmentBaseFieldNames()); + } + + return _environmentBaseFieldNames; +} + +function allBaseFieldNames() { + if (!_allBaseFieldNames) { + _allBaseFieldNames = environmentBaseFieldNames().concat(otherBaseFieldNames()); + } + + return _allBaseFieldNames; +} + +/* + * src/event_handlers/Base.js + * ae-client-kit-core + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +var attachDelegate = reflect.attachDelegate; +var cryptoRandomBase62String = string.cryptoRandomBase62String; +var exceptionString = string.exceptionString; + +var _prototypeInitialized; + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the "base" fields common to all metrics events. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * Kits can also extend this class and additional methods specific to their event model. + * @example + * // extend kit-core Base class + * var MetricsKitBase = function MetricsKitBase() { + * kitCore.eventHandlers.Base.apply(this, arguments); // invoke Base constructor + * }; + * MetricsKitBase.prototype = new kitCore.eventHandlers.Base(); + * MetricsKitBase.prototype.constructor = MetricsKitBase; + * + * // set Kit-specific methods + * MetricsKitBase.prototype.environment = function() { return metricsKit.system.environment; } + * MetricsKitBase.prototype.eventRecorder = function() { return metricsKit.system.eventRecorder; } + * MetricsKitBase.prototype.metricsData = function(pageId, pageType, pageContext, callerSuppliedFieldsMapsN) { ... } + * MetricsKitBase.prototype.processMetricsData = function( ... ) { ... } + * + * // extend kit-core known fields + * MetricsKitBase.prototype.knownFields = function knownFields() { + * var parentKnownFields = Object.getPrototypeOf(MetricsKitBase.prototype).knownFields(); + * return parentKnownFields.concat(['dsId', 'anotherUserExperienceOnlyField']); + * }; + * + * // add Kit-specific accessor functions + * MetricsKitBase.prototype.dsId = function dsId() { return this.environment().dsId(); }; + * MetricsKitBase.prototype.anotherUserExperienceOnlyField = function() { ... }; + * + * // create a Kit-specific class instance + * metricsKit.eventHandlers.base = new MetricsKitBase(metricsKit); + * @param {MetricsKit/PerfKit/VPAFKit} kit + * @delegatable + * @constructor + */ +function Base(processor) { + if (!reflect.isDefinedNonNull(processor)) { + throw new Error('A processor instance is required for creating BaseEventHandler.'); + } + // @private + this._processor = processor; + + if (!_prototypeInitialized) { + _prototypeInitialized = true; + environmentBaseFieldNames().forEach(function (fieldName) { + Base.prototype[fieldName] = function (callerSuppliedEventFields) { + var returnValue; + + if (callerSuppliedEventFields && callerSuppliedEventFields.hasOwnProperty(fieldName)) { + returnValue = callerSuppliedEventFields[fieldName]; + } else { + returnValue = this.environment()[fieldName](); + } + + return returnValue; + }; + }); + } +} + +Base._className = 'eventHandlers.base'; + +/** + * Allows replacement of one or more of this class instance's functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes (these methods will not be copied to the target object). + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to replace some number of methods that need custom implementations. + * If, for example, a client wants to use the standard logger implementation with the exception of, say, the "debug" method, they can + * call "setDelegate()" with their own delegate containing only a single method of "debug" as the delegate, which would leave all the other methods intact. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * base.setDelegate({ app: function() { return 'myApp'; }); + * To override one or more methods with a separate object: + * base.setDelegate(customBaseDelegate); + * (where "customBaseDelegate" might be defined elsewhere as, e.g.: + * var customBaseDelegate = { app: function() { return Device.appIdentifier; }; + * appVersion: function() { return Device.appVersion; } }; + * To override one or more methods with an instantiated object from a class definition: + * base.setDelegate(new CustomBaseDelegate()); + * (where "CustomBaseDelegate" might be defined elsewhere as, e.g.: + * function CustomBaseDelegate() {} + * CustomBaseDelegate.prototype.app = function app() { return Device.appIdentifier; }; + * CustomBaseDelegate.prototype.appVersion = function appVersion() { return Device.appVersion; }; + * To override one or more methods with a class object (with "static" methods): + * base.setDelegate(CustomBaseDelegate); + * (where "CustomBaseDelegate" might be defined elsewhere as, e.g.: + * function CustomBaseDelegate() {} + * CustomBaseDelegate.app = function app() { return Device.appIdentifier; }; + * CustomBaseDelegate.appVersion = function appVersion() { return Device.appVersion; }; + * @param {Object} delegate Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Base.prototype.setDelegate = function setDelegate(delegate) { + return attachDelegate(this, delegate); +}; + +/** + * The active environment class + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @see src/system/Environment + * @return {Environment} + * @overridable + */ +Base.prototype.environment = function environment() { + // Don't wrap the throw in a helper function or the backtrace won't be as nice. + throw exceptionString(Base._className, 'environment'); +}; + +/** + * The active eventRecorder + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @return {Object} an event recorder that implements a sendMethod() function + * @overridable + */ +Base.prototype.eventRecorder = function eventRecorder() { + // Don't wrap the throw in a helper function or the backtrace won't be as nice. + throw exceptionString(Base._className, 'eventRecorder'); +}; + +/** + * Creates a simple map object (dictionary) with all the "base" fields required by AMP Analytics + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers. + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @returns key/value pairs of all "base" fields required by AMP Analytics. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + * + * TODO: consider adding default implementation for shared Kit use + */ +Base.prototype.metricsData = function metricsData() { + // Don't wrap the throw in a helper function or the backtrace won't be as nice. + throw exceptionString(Base._className, 'metricsData'); +}; + +/** + * All of the various eventHandlers invoke this method to generate their metrics data + * The data is a simple map object (dictionary) with all the fields required by AMP Analytics for that event + * Some fields can be derived by this class itself. + * This function typically expects to be called with the correct context + * (e.g. base.processMetricsData.apply(this, arguments)) + * @returns {Object} key/value pairs of all fields required by AMP Analytics. + * WARNING: May return "null" if metrics and/or the specific eventType for this handler is disabled, or on error. + * + * TODO: consider adding default implementation for shared Kit use + */ +Base.prototype.processMetricsData = function processMetricsData() { + // Don't wrap the throw in a helper function or the backtrace won't be as nice. + throw exceptionString(Base._className, 'processMetricsData'); +}; + +/** + * @return {Array} all the fields that base eventHandlers know about + */ +Base.prototype.knownFields = function knownFields() { + return allBaseFieldNames(); +}; + +/** + * ********************* ACCESSOR FUNCTIONS ********************* + * We create accessor functions for every data field because: + * 1. Cleans/simplifies all methods that use it. + * 2. Facilitates writing test case shims + * 3. Allows specific feature suppliers to be overridden (via setDelegate())) + */ + +/** + * The app identifier of the binary app + * @param {Map} callerSuppliedEventFields + * @returns {String} The app identifier of the binary app + * @example "com.apple.appstore" or "com.apple.gamecenter" + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.app = function app(callerSuppliedEventFields) { }; + +/** + * The version number of this application + * @example "1.0", "5.43", etc. + * @param {Map} callerSuppliedEventFields + * @returns {String} the version number of this application + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.appVersion = function appVersion(callerSuppliedEventFields) { }; + +/** + * The version of the set of base data to be sent up + * @returns {number} the version of the set of base data to be sent up + * @overridable + */ +Base.prototype.baseVersion = function baseVersion() { + return 1; +}; + +/** + * A unique identifier for each event + * @return {String} + * @overridable + */ +Base.prototype.clientEventId = function clientEventId(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.clientEventId) || cryptoRandomBase62String(true); +}; + +/** + * Type of internet connection. + * Only applicable to devices + * Beware that users on WiFi may actually be receiving 3G speeds (i.e. if device is tethered to a portable hotspot.) + * @example "WiFi, "3G, etc. + * @param {Map} callerSuppliedEventFields + * @returns {String} type of internet connection + * @overridable + */ +Base.prototype.connection = function connection(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.connection) || this.environment().connectionType(); +}; + +/** + * The identifier of the process generating the event, if different from “app”, or blank otherwise. + * @example 'web-experience-app' + * @returns {String} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.delegateApp = function delegateApp(callerSuppliedEventFields) { }; + +/** + * The id of this user ("directory service id"). + * This id will get mapped to a consumerId on the server. + * @example 659261189 + * @param {Map} callerSuppliedEventFields + * @returns {String} + * @overridable + */ +Base.prototype.dsId = function dsId(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.dsId) || this.environment().dsId(); +}; + +/** + * The time (UTC) in milliseconds at which this event happened. + * This field is central to determining the sequence of user events + * Use online epoch converter to test your values. + * @example 1437356433388 (http://www.epochconverter.com converts that to: July 19, 2015 at 6:40:33 PM PDT GMT-7:00 DST) + * @param {Map} callerSuppliedEventFields + * @returns {number} the time (UTC) in milliseconds at which this event happened + * @overridable + */ +Base.prototype.eventTime = function eventTime(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventTime) || Date.now(); +}; + +/** + * The version of the set of data to be sent up + * @return {Number} + * @overridable + */ +Base.prototype.eventVersion = function eventVersion(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || null; +}; + +/** + * The hardware brand of the device. Not required for Apple devices. + * @example "Samsung", "LG", "Google" + * @param {Map} callerSuppliedEventFields + * @returns {String} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.hardwareBrand = function hardwareBrand(callerSuppliedEventFields) { }; + +/** + * The hardware family of the device + * @example "iPhone", "Macbook Pro" + * @param {Map} callerSuppliedEventFields + * @returns {String} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.hardwareFamily = function hardwareFamily(callerSuppliedEventFields) { }; + +/** + * The model of the device + * @example "iPhone10,2", "MacbookPro11,5" + * @returns {String} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.hardwareModel = function hardwareModel(callerSuppliedEventFields) { }; + +/** + * The name of the OS + * @example "ios", "macos", "windows" + * @param {Map} callerSuppliedEventFields + * @returns {String} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.os = function os(callerSuppliedEventFields) { }; + +/** + * The build number of the OS + * @example "15D60", "17E192" + * @param {Map} callerSuppliedEventFields + * @returns {String} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.osBuildNumber = function osBuildNumber(callerSuppliedEventFields) { }; + +/** + * A string array of language IDs, ordered in descending preference + * @example ["en-US", "fr-CA"] + * @param {Map} callerSuppliedEventFields + * @returns {Array} + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.osLanguages = function osLanguages(callerSuppliedEventFields) { }; + +/** + * The full OS version number + * In ITML, the value can be retrieved via Device.systemVersion + * @example "8.2.1" (iOS) "10.10.3" (Desktop) + * @param {Map} callerSuppliedEventFields + * @returns {String} the full OS version number + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.osVersion = function osVersion(callerSuppliedEventFields) { }; + +/** + * The HTML resources revision number + * @example 2C97 or 8.4.0.0.103 + * @param {Map} callerSuppliedEventFields + * @returns {String} the HTML resources revision number + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.resourceRevNum = function resourceRevNum(callerSuppliedEventFields) { }; + +/** + * The client screen height in pixels + * @example 568 + * @param {Map} callerSuppliedEventFields + * @returns {number} the client screen height in pixels + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.screenHeight = function screenHeight(callerSuppliedEventFields) { }; + +/** + * The client screen width in pixels + * @example 320 + * @param {Map} callerSuppliedEventFields + * @returns {number} the client screen width in pixels + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.screenWidth = function screenWidth(callerSuppliedEventFields) { }; + +/** + * ISO 3166 Country Code. Apps that cannot provide a storeFrontHeader should provide a storeFrontCountryCode instead + * @example US + * @param {Map} callerSuppliedEventFields + * @returns {String} the store front country code + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.storeFrontCountryCode = function storeFrontCountryCode(callerSuppliedEventFields) { }; + +/** + * The value contained in the X-Apple-Store-Front header value at the time the event is being created. + * @example K143441-1,29 ab:rSwnYxS0 + * @param {Map} callerSuppliedEventFields + * @returns {String} the value contained in the X-Apple-Store-Front header value at the time the event is being created + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.storeFrontHeader = function storeFrontHeader(callerSuppliedEventFields) { }; + +/** + * The difference, in minutes, between GMT (UTC) and timezone of event origin ("local time). + * This means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. + * Daylight saving time prevents this value from being a constant, even for a given locale + * @example 420 (PST, not -420) or -600 (Australian Eastern Standard Time, UTC+10) + * @param {Map} callerSuppliedEventFields + * @returns {number} the difference, in minutes, between GMT (UTC) and timezone of event origin ("local time). + * @overridable + */ +Base.prototype.timezoneOffset = function timezoneOffset(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.timezoneOffset) || new Date().getTimezoneOffset(); +}; + +/** + * The client’s user agent string. If the "app field is not provided, "userAgent may be used to derive the value of the "app field + * @example AppStore/2.0 iOS/8.3 model/iPhone7,2 build/12F70 (6; dt:106) + * @param {Map} callerSuppliedEventFields + * @returns {String} the client’s user agent string. If the "app field is not provided, "userAgent may be used to derive the value of the "app field + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Base.prototype.userAgent = function userAgent(callerSuppliedEventFields) { }; + +/** + * How often, in milliseconds, batches of events should get sent to the server. + * This field should be based on the client's most recent config value of "postFrequency". + * This is valuable for problem analysis because it indicates if and how clients are honoring the "postFrequency" value + * they were supplied with. + * This cannot be a "passthrough" field, because it can change (via new config) during program execution, so the value + * in effect at event creation time is what is needed. + * @example 60000 + * @param {Map} callerSuppliedEventFields + * @returns {number} how often, in milliseconds, batches of events should get sent to the server + * @overridable + */ +Base.prototype.xpPostFrequency = function xpPostFrequency(callerSuppliedEventFields) { + return ( + (callerSuppliedEventFields && callerSuppliedEventFields.xpPostFrequency) || + this._processor.config.value('postFrequency') + ); +}; + +/** + * The methodology being used by the eventRecorder to send batches of events to the server + * This field should be hardcoded in the client based on what method it is using to encode and send its events to Figaro. + * The three typical values are: + * "itms" - use this value when/if JavaScript code enqueues events for sending via the "itms.recordEvent()" method in ITML. + * "itunes" - use this value when/if JavaScript code enqueues events by calling the "iTunes.recordEvent()" method in Desktop Store apps. + * "javascript" - use this value when/if JavaScript code enqueues events for sending via the JavaScript eventQueue management. This is typically only used by older clients which don't have the built-in functionality of itms or iTunes available to them. + * @example "itms", "itunes", "javascript" + * @param {Map} callerSuppliedEventFields + * @returns {String} the methodology being used by the eventRecorder to send batches of events to the server + * @overridable + */ +Base.prototype.xpSendMethod = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.xpSendMethod) || this.eventRecorder().sendMethod(); +}; + +/* + * src/event_handlers/index.js + * ae-client-kit-core + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +var eventHandlers = { + Base: Base +}; + +/* + * src/system/Environment.js + * ae-client-kit-core + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +var attachDelegate$1 = reflect.attachDelegate; +var exceptionString$1 = string.exceptionString; +var localStorageObject = storage.localStorageObject; +var sessionStorageObject = storage.sessionStorageObject; + +var _prototypeInitialized$1; + +/** + * Provides a set of environment-specific (platform-specific) functions which can be individually overridden for the needs + * of the particular environment, or replaced en masse by providing a single replacement environment delegate object. + * Kits can extend this class and additional methods specific to their event model. + * @example + * // MetricsKit code + * + * // extend kit-core Environment class + * var MetricsKitEnvironment = function MetricsKitEnvironment() { + * kitCore.system.Environment.apply(this, arguments); // invoke Environment constructor + * }; + * MetricsKitEnvironment.prototype = new kitCore.system.Environment(); + * MetricsKitEnvironment.prototype.constructor = MetricsKitEnvironment; + * + * // add Kit-specific functions + * MetricsKitEnvironment.prototype.dsId = function dsId() { + * throw this._exceptionString(_utils.reflect.functionName()); + * }; + * MetricsKitEnvironment.prototype.anotherUserExperienceOnlyField = function() { ... }; + * + * // create a Kit-specific class instance + * metricsKit.system.environment = new MetricsKitEnvironment(); + * + * // client app code remains unchanged: + * var customEnvironmentDelegate = { + * app: function() { return 'myAppName'; }, + * appVersion: function() { ... } // etc. + * }; + * + * metricsKit.system.environment.setDelegate(customEnvironmentDelegate); + * + * @constructor + */ +function Environment() { + // consider moving this code into a separate init method + if (!_prototypeInitialized$1) { + _prototypeInitialized$1 = true; + requiredEnvironmentBaseFieldNames().forEach(function (fieldName) { + Environment.prototype[fieldName] = function () { + throw exceptionString$1(Environment._className, fieldName); + }; + }); + + optionalEnvironmentBaseFieldNames().forEach(function (fieldName) { + Environment.prototype[fieldName] = function () {}; + }); + } +} + +Environment._className = 'system.environment'; + +/** + * Allows replacement of one or more of this class instance's functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes (these methods will not be copied to the target object). + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to replace some number of methods that need custom implementations. + * If, for example, a client wants to use the standard logger implementation with the exception of, say, the "debug" method, they can + * call "setDelegate()" with their own delegate containing only a single method of "debug" as the delegate, which would leave all the other methods intact. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * environment.setDelegate({ app: function() { return 'myApp'; }); + * To override one or more methods with a separate object: + * environment.setDelegate(customEnvironmentDelegate); + * (where "customEnvironmentDelegate" might be defined elsewhere as, e.g.: + * var customEnvironmentDelegate = { app: function() { return Device.appIdentifier; }, + * appVersion: function() { return Device.appVersion; } }; + * To override one or more methods with an instantiated object from a class definition: + * environment.setDelegate(new CustomEnvironmentDelegate()); + * (where "CustomEnvironmentDelegate" might be defined elsewhere as, e.g.: + * function CustomEnvironmentDelegate() {} + * CustomEnvironmentDelegate.prototype.app = function app() { return Device.appIdentifier; }; + * CustomEnvironmentDelegate.prototype.appVersion = function appVersion() { return Device.appVersion; }; + * To override one or more methods with a class object (with "static" methods): + * environment.setDelegate(CustomEnvironmentDelegate); + * (where "CustomEnvironmentDelegate" might be defined elsewhere as, e.g.: + * function CustomEnvironmentDelegate() {} + * CustomEnvironmentDelegate.app = function app() { return Device.appIdentifier; }; + * CustomEnvironmentDelegate.appVersion = function appVersion() { return Device.appVersion; }; + * @param {Object} delegate Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Environment.prototype.setDelegate = function setDelegate(delegate) { + return attachDelegate$1(this, delegate); +}; + +/** + * The app identifier of the binary app + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "com.apple.appstore" or "com.apple.gamecenter" + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.app = function app() { }; + +/** + * The version number of this application + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "1.0", "5.43", etc. + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.appVersion = function appVersion() { }; + +/** + * Type of internet connection. + * Only applicable to devices + * Beware that users on WiFi may actually be receiving 3G speeds (i.e. if device is tethered to a portable hotspot.) + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "WiFi, "3G, etc. + * @returns {String} + */ +Environment.prototype.connectionType = function connectionType() { + // Don't wrap the throw in a helper function or the backtrace won't be as nice. + // TODO: use a constant instead of a hardcoded string + throw exceptionString$1(Environment._className, 'connectionType'); +}; + +/** + * The identifier of the process generating the event, if different from “app”, or blank otherwise. + * NO DEFAULT IMPLEMENTATION... HOWEVER: these fields are not required, so an exception will not be thrown if they + * are omitted. + * @example 'web-experience-app' + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.delegateApp = function delegateApp(callerSuppliedEventFields) { }; + +/** + * The id of this user ("directory service id"). + * This id will get mapped to a consumerId on the server. + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example 659261189 + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +Environment.prototype.dsId = function dsId() { + // Don't throw an exception to avoid breaking minor version backwards compatibility. + // Consider adding an exception in the next major version. +}; + +/** + * The hardware brand of the device. Not required for Apple devices. + * NO DEFAULT IMPLEMENTATION... HOWEVER: these fields are not required, so an exception will not be thrown if they + * are omitted. + * @example "Samsung", "LG", "Google" + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.hardwareBrand = function hardwareBrand() { }; + +/** + * The hardware family of the device + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "iPhone", "Macbook Pro" + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.hardwareFamily = function hardwareFamily() { }; + +/** + * The model of the device + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "iPhone10,2", "MacbookPro11,5" + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.hardwareModel = function hardwareModel() { }; + +/** + * The name of the OS + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "ios", "macos", "windows" + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.os = function os() { }; + +/** + * The build number of the OS + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "15D60", "17E192" + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.osBuildNumber = function osBuildNumber() { }; + +/** + * A string array of language IDs, ordered in descending preference + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example ["en-US", "fr-CA"] + * @returns {Array} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.osLanguages = function osLanguages() { }; + +/** + * The full OS version number + * In ITML, the value can be retrieved via Device.systemVersion + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "8.2.1" (iOS) "10.10.3" (Desktop) + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.osVersion = function osVersion() { }; + +/** + * HTML resources revision number + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example 2C97 or 8.4.0.0.103 + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.resourceRevNum = function resourceRevNum() { }; + +/** + * Client screen height in pixels + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example 568 + * @returns {number} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.screenHeight = function screenHeight() { }; + +/** + * Client screen width in pixels + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example 320 + * @returns {number} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.screenWidth = function screenWidth() { }; + +/** + * ISO 3166 Country Code. Apps that cannot provide a storeFrontHeader should provide a storeFrontCountryCode instead + * NO DEFAULT IMPLEMENTATION... Either this method or storeFrontHeader must be replaced. + * are omitted. + * @example US + * @param {Map} callerSuppliedEventFields + * @returns {String} the store front country code + * @overridable + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.storeFrontCountryCode = function storeFrontCountryCode(callerSuppliedEventFields) { }; + +/** + * The value contained in the X-Apple-Store-Front header value at the time the event is being created. + * NO DEFAULT IMPLEMENTATION... Either this method or storeFrontHeader must be replaced. + * @example K143441-1,29 ab:rSwnYxS0 + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.storeFrontHeader = function storeFrontHeader() { }; + +/** + * Client’s user agent string. If the "app field is not provided, "userAgent may be used to derive the value of the "app field + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example AppStore/2.0 iOS/8.3 model/iPhone7,2 build/12F70 (6; dt:106) + * @returns {String} + */ +// This prototype method is created dynamically upon instance initialization +// Environment.prototype.userAgent = function userAgent() { }; + +/** + * Some clients have platform-specific implementations of these objects (e.g. iTunes.sessionStorage), + * so we cover them in case they need to be overridden. + */ +Environment.prototype.localStorageObject = localStorageObject; +Environment.prototype.sessionStorageObject = sessionStorageObject; + +/** + * Fetching identifier entity from AMS Metrics Identifier API + * @param {String} idNamespace - The id namespace that is defined under 'metricsIdentifier' in the bag + * @param {'userid' | 'clientid'} idType - The identifier type (userid or clientid) + * @param {Boolean} crossDeviceSync - The boolean flag to indicate whether the identifier is synced across devices + * @returns {Promise} + * @overridable + */ +Environment.prototype.platformIdentifier = function platformIdentifier() {}; + +/* + * src/system/index.js + * ae-client-kit-core + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +var system = { + Environment: Environment +}; + +/* + * src/helpers/MetricsData.js + * ae-client-kit-core + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns a MetricsData instance with APIs to store event data and create and send events to AMP Analytics + * @delegatable + * @constructor + * @return {MetricsData} + */ +function MetricsData() { + // @private + this._eventData = {}; // this data may be unclean; it should be cleaned before being enqueued as an event +} + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Add event data to this metricsData object + * This data may be unclean; it should be cleaned before being enqueued as an event + * @param {Object<varargs>} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, + * each containing key/value pairs representing event fields to include when sending the event. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * @overridable + */ +MetricsData.prototype.updateData = function updateData(/* callerSuppliedEventFieldsMapN(varargs) */) { + reflect.extend.apply(null, [this._eventData].concat(Array.prototype.slice.call(arguments))); +}; + +/* + * src/helpers/index.js + * ae-client-kit-core + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +var helpers = { + MetricsData: MetricsData +}; + +export { eventHandlers, helpers, system }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-client-config/dist/mt-client-config.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-client-config/dist/mt-client-config.esm.js new file mode 100644 index 0000000..555fd31 --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-client-config/dist/mt-client-config.esm.js @@ -0,0 +1,987 @@ +import { storage, reflect, network, keyValue, number } from '@amp-metrics/mt-metricskit-utils-private'; +import Logger from '@amp-metrics/mt-client-logger-core'; + +/* + * src/environment.js + * mt-client-config + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +/** + * Provides a set of environment-specific (platform-specific) functions which can be individually overridden for the needs + * of the particular environment, or replaced en masse by providing a single replacement environment delegate object + * The functionality in this class is typically replaced via a delegate. + * @see setDelegate + * @delegatable + * @constructor + */ +var Environment = function () {}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Environment.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +Environment.prototype.localStorageObject = storage.localStorageObject; +Environment.prototype.sessionStorageObject = storage.sessionStorageObject; + +/* + * src/network.js + * mt-client-config + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +/** + * Network request methods exposed so delegate callers can override + * @constructor + */ +var Network = function () {}; + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Network.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * Covers private utils implementation of makeAjaxRequest for delegation + */ +Network.prototype.makeAjaxRequest = network.makeAjaxRequest; + +/* + * src/config.js + * mt-client-config + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +var NO_TOPIC_KEY = 'noTopicConfig'; +var COMPOUND_SEPARATOR = '_'; + +/** + * The default config should not include keys which have meaning if they are omitted. + * For example, a configSource without impressions means no impressions should be recorded, and in this case we should + * not have impressions in defaults so we don't inadvertently disobey the client's configSource. + */ +var DEFAULTS = { + configBaseUrl: 'https://xp.apple.com/config/1/report', + disabled: true // Prevent sending event if no config is available +}; + +// @private +var _defaultConfig; + +/** + * Appends the provided source and its subsection if the subsection key matches the provided topic to the provided configSources array. + * @param {Array} configSources + * @param {String} topic + * @param {Object} source + */ +var _appendSectionAndSubsectionToConfigSources = function _appendSectionAndSubsectionToConfigSources( + configSources, + topic, + source +) { + if (source) { + configSources.push(source); + var subsection = source[topic]; + if (subsection && reflect.hasAnyKeys(subsection)) { + configSources.push(subsection); + } + } +}; + +/** + * Provides config-related functionality including managing config sources and values. + * The functionality in this class is typically replaced via a delegate. + * @see setDelegate + * @delegatable + * @constructor + * @param {String} topic + */ +var Config = function Config(topic) { + // @public + this.environment = new Environment(); + + /** + * @public + * @deprecated + */ + this.network = new Network(); + + // @public + this.logger = /*#__PURE__*/ new Logger('mt-client-config'); + + // @private + this._topic = topic || NO_TOPIC_KEY; + + // @private + this._debugSource = null; + // @private + this._cachedSource = null; + // @private + this._serviceSource = null; + // @private + this._initCalled = false; + // @private + this._initialized = false; + // @private + this._showedDebugWarning = false; + // @private + this._showedNoProvidedSourceWarning = false; + // @private + this._keyPathsThatSuppressWarning = { + configBaseUrl: true // used to fetch a config source, so we do not expect sources to be set when retrieving this value + }; + // @private + this._configFetchPromise = null; + + // @const + this.DEBUG_SOURCE_KEY = 'mtClientConfig_debugSource' + COMPOUND_SEPARATOR + this._topic; + // @const + this.CACHED_SOURCE_KEY = 'mtClientConfig_cachedSource' + COMPOUND_SEPARATOR + this._topic; +}; + +/** + * @return {Config} the default config instance, which is not associated with any topic + */ +Config.defaultConfig = function defaultConfig() { + if (!_defaultConfig) { + _defaultConfig = new Config(NO_TOPIC_KEY); + } + return _defaultConfig; +}; + +/** + ************************************ PSEUDO-PRIVATE METHODS/IVARS ************************************ + * These functions need to be accessible for ease of testing, but should not be used by clients + */ +Config.prototype._defaults = function _defaults() { + return DEFAULTS; +}; + +Config.prototype._setInitialized = function _setInitialized(initialized) { + this._initialized = initialized; +}; + +Config.prototype._setInitCalled = function _setInitCalled(initCalled) { + this._initCalled = initCalled; +}; + +Config.prototype._setShowedDebugWarning = function _setShowedDebugWarning(value) { + this._showedDebugWarning = value; +}; + +Config.prototype._setShowedNoProvidedSourceWarning = function _setShowedNoProvidedSourceWarning(value) { + this._showedNoProvidedSourceWarning = value; +}; + +/** + ************************************ PUBLIC METHODS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Config.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * Reset the attached delegates from + * @param delegate + */ +Config.prototype.resetDelegate = function resetDelegate() { + // reset the delegates from environment, network and logger + reflect.detachMethods(this); + // Detach the attached methods from prototype(source, debugSource and attached utility methods from mt-metricskit-utils-private, etc.) of the config instance + reflect.resetDelegates(this); +}; + +/** + * @return {String} the Figaro topic that this config corresponds with + * Most clients do not need to override this method + */ +Config.prototype.topic = function topic() { + return this._topic; +}; + +/** + * Returns the metrics config endpoint hostname + * Most clients do not need to override this method + * @deprecated + * @return {String} a hostname e.g.xp.apple.com + */ +Config.prototype.configHostname = function configHostname() {}; + +/** + * Returns a constructed URL to the metrics config endpoint for use with getConfig() + * Most clients do not need to override this method + * @deprecated + * @return {Promise} A Promise with a URL to the metrics config endpoint e.g. https://xp.apple.com/config/1/report/xp_its_main + */ +Config.prototype.configUrl = function configUrl() { + var configHostname = this.configHostname(); + var returnUrlPromise; + + if (configHostname) { + returnUrlPromise = Promise.resolve('https://' + configHostname + '/config/1/report'); + } else { + returnUrlPromise = this.value('configBaseUrl'); + } + + return returnUrlPromise.then( + function (returnUrl) { + if (this._topic !== NO_TOPIC_KEY) { + returnUrl += '/' + this.topic(); + } else { + this.logger.error('config.configUrl(): Topic must be provided'); + } + return returnUrl; + }.bind(this) + ); +}; + +/** + * This function will be used to access all of the sources for configuration data (e.g. disabled, blacklistedEvents, fieldsMap, etc.) + * THIS METHOD MUST BE PROVIDED BY A DELEGATE + * Returns an array of key/value objects (dictionaries) for all of the config sources (e.g. the bag, the page, the control parent, etc.). + * This method will be called frequently and repeatedly from the metrics.config.* functions. + * MetricsKit will use this return value to traverse the list of config objects, looking for config values. + * If later dictionaries of key/value pairs contain any keys already collected, the most late (farthest in the area) config value will overwrite earlier ones. + * IMPORTANT: This function might be called frequently and repeatedly so should be optimized for performance. + * It will be called frequently (rather than having this API provide a "setConfig" method for permanently setting the config values) since + * these config values can always be changing from one call to the next, so this ensures that they are current. + * THEREFORE: This function should NOT try to "simplify" by, e.g., merging all config sources into a single object to return when called. + * Doing so is an enormous amount of work to do each time MetricsKit needs to look up a single value. + * It is far more efficient for MetricsKit to simply interogate each configSource for the required key, rather than + * the delegate function merging *all* keys of *all* sources (looping through all values of all sources) in order to look up a single value. + * @example var sources = function() { return [itms.getBag().metrics, pageData.metrics.config]; }; // return an array of sources of app config + * @example var sources = function() { return metrics.utils.keyValue.sourcesArray(itms.getBag().metrics, pageData.metrics.config, swooshData.metrics.config); }; // Let MetricsKit help build the array from a varargs list of sources + * @example var sources = function() { return metrics.utils.keyValue.sourcesArray(existingArrayOfConfigSources, pageData.metrics.config, swooshData.metrics.config); }; // Let MetricsKit help build the array from a varargs list of sources where some args are already arrays of other sources (only works one level deep) + * @see metrics.system.configSources.sources for simple creation of the return value of config sources. + */ +Config.prototype.sources = function sources() {}; + +/** + * Search through the configuration sources provided by "metrics.system.configSources()", looking for the highest precedence value for "key" + * If no config sources provided, look for values in the DEFAULTS constant + * "keypath" can be a simple top-level config key such as "postFrequency" or a compound "path", such as fieldsMap.cookies + * @param {String} keyPath the dot-separated (".") path to the desired config value. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} A Promise of the value at the reached keypath. Returns "null" if the key or key's value is not found. + * Values found in later (farthest in array) dictionaries of key/value pairs overwrite earlier dictionaries. + * @see metrics.system.configSources.setDelegate() + */ +Config.prototype.value = function value(keyPath, topic) { + var getConfigValueFn = function () { + var cachedSource = this.cachedSource(); + var serviceSource = this.serviceSource(); + var configSourcesArray = this.sources(); + var savedDebugSource = this.debugSource(); // NOTE: Always go through the accessor method, since it may need to be retrieved from localStorage + var sourceProvided = cachedSource || serviceSource || configSourcesArray || savedDebugSource; + var sourcesToCheck; + + // show relevant warnings + if (!configSourcesArray && !serviceSource && !(keyPath in this._keyPathsThatSuppressWarning)) { + if (!this._showedNoProvidedSourceWarning) { + this._showedNoProvidedSourceWarning = true; + this.logger.warn( + 'Metrics config: No config provided via delegate or fetched via init(), using cached config values.' + ); + } + } + if (savedDebugSource) { + if (!this._showedDebugWarning) { + // We do this in case developers forget to clear this and wonder why the app isn't working correctly in normal running. + this._showedDebugWarning = true; + this.logger.warn( + '"debugSource" found.\nThis will override any same-named client-supplied configSource fields.\nThis setting "sticks" across session, use "setDebugSource(null)" to clear' + ); + } + } + + if (!reflect.isArray(configSourcesArray)) { + configSourcesArray = [configSourcesArray]; + } + + // disable the event only if there are no provided sources + // In case of fetch config failed and no cached config in local + if (keyPath === 'disabled') { + if (sourceProvided) { + sourcesToCheck = [cachedSource, serviceSource, configSourcesArray, savedDebugSource]; + } else { + sourcesToCheck = [DEFAULTS]; + } + } else { + // For non-disabling rules, check all sources + // Let "DEFAULTS" be overwritten by cachedSource, then serviceSource, then client-supplied configSources, + // and then let "savedDebugSource" overwrite everyone + sourcesToCheck = [DEFAULTS, cachedSource, serviceSource, configSourcesArray, savedDebugSource]; + } + + sourcesToCheck = this.configSourcesWithOverrides(sourcesToCheck, topic || this.topic()); + // Pass each source as an individual argument; valueForKeyPath won't expand configSourcesArray if it is nested in sourcesToCheck + return keyValue.valueForKeyPath.apply(null, [keyPath].concat(sourcesToCheck)); + }.bind(this); + + if (this._configFetchPromise) { + return this._configFetchPromise.then(getConfigValueFn); + } else { + var value = getConfigValueFn(); + return Promise.resolve(value); + } +}; + +/** + * Pushes any subsections within a config source to the front of the provided config sources array if the subsection key matches with the specified topic. + * This method is used by the default implementation of 'value(keyPath)' to determine precedence + * of config sources for the given topic. + * Subsections that are later in the array will take precedence over earlier subsections. + * Note that if one of the config sources is an array, this method will traverse one level deep + * to check for the presence of a dictionary that may contain a desired subsection. + * We traverse one level deep to maintain parity with _utils.keyValue.valueForKeyPath() which will be used to traverse the config sources + * @param {Array} configSources + * @param {String} topic + * @returns {Array} returnSources + */ +Config.prototype.configSourcesWithOverrides = function configSourcesWithOverrides(configSources, topic) { + var returnSources = configSources; + if (configSources && configSources.length && topic) { + returnSources = []; + for (var i = 0; i < configSources.length; i++) { + var source = configSources[i]; + if (source) { + if (reflect.isArray(source) && source.length) { + var subarray = []; + for (var j = 0; j < source.length; j++) { + _appendSectionAndSubsectionToConfigSources(subarray, topic, source[j]); + } + returnSources.push(subarray); + } else { + _appendSectionAndSubsectionToConfigSources(returnSources, topic, source); + } + } + } + } + return returnSources; +}; + +/** + * Set's a "priority" configSource that will override any same-named client-supplied configSource fields. + * This can be done in code (e.g. in testcases) or, more typically, in Web Inspector when debugging or testing a running app (client) using MetricsKit. + * This is useful when testing, both in testcases and at runtime. One example would be to set a temporary "bag" structure that could then be tweaked at will, e.g. blacklisting fields, etc. + * This setting "sticks" across session so that it can be set, the app restarted, and the values will be present at the earliest runnings of the app. + * Use "setDebugSource(null)" to clear this value + * NOTE: If no "localStorage" is available (e.g. when testing), this value will only last for the session or until it is explicitly cleared. + * Here is an example of how to use this to test an app at runtime (and/or launch time) + * Run the app + * Open Web Inspector + * Set a variable to bag contents, e.g. var debugBag = itms.getBag(); + * Call: metrics.config.setDebugSource(debugBag.metrics); + * enter: debugBag.metrics.disabled=true; + * Then do some things to generate events and make sure that: + * The app doesn’t crash + * No JavaScript events are generated in the console + * The events aren’t sent + * Reset that by entering: delete debugBag.metrics.disabled + * enter: debugBag.metrics.blacklistedEvents = [“page”, “click”]; + * Then do some things to generate events and make sure that: + * The app doesn’t crash + * No JavaScript events are generated in the console + * Those events, and only those events, aren’t sent + * I save the object you set via metrics.config.setDebugSource to localStorage to facilitate testing at app startup, so remember to clear it out when you’re done or you’ll be confused why things aren’t working right in the future: + * metrics.config.setDebugSource(); + * If there are values you want to be in effect at app-launch, just do the tweaking to the bag before your call to metrics.config.setDebugSource(debugBag.metrics);, passing in the tweaked bag. + * @param aDebugSource a plain old JavaScript object with keys and values which will be searched when config is requested. + * @returns {*} debugSource + */ +Config.prototype.setDebugSource = function setDebugSource(aDebugSource) { + this._debugSource = aDebugSource || null; + return storage.saveObjectToStorage(this.environment.localStorageObject(), this.DEBUG_SOURCE_KEY, this._debugSource); +}; + +/** + * Return any previously set debugSource which would be stored in localStorage. + * If not present in localStorage, the class variable value of debugSource will be returned. + * @returns {*} debugSource + */ +Config.prototype.debugSource = function debugSource() { + if (this._debugSource) ; else { + // Otherwise look in localStorage for one... + this._debugSource = storage.objectFromStorage(this.environment.localStorageObject(), this.DEBUG_SOURCE_KEY); + } + return this._debugSource; +}; + +/** + * Save a source to localStorage which will have config values that can be used in the next visit until a fresh config is fetched + * @param {Object} aSource + * @return {Object} + */ +Config.prototype.setCachedSource = function setCachedSource(aSource) { + this._cachedSource = aSource || null; + return storage.saveObjectToStorage( + this.environment.localStorageObject(), + this.CACHED_SOURCE_KEY, + this._cachedSource + ); +}; + +/** + * Return any previously set savedSource which would be stored in localStorage. + * The class variable value of savedSource will be returned if non-null to avoid expensive reads from disk. + * @return {Object} + */ +Config.prototype.cachedSource = function cachedSource() { + if (this._cachedSource) ; else { + // Otherwise look in localStorage for one... + this._cachedSource = storage.objectFromStorage(this.environment.localStorageObject(), this.CACHED_SOURCE_KEY); + } + return this._cachedSource; +}; + +/** + * Set a source, typically one that was fetched from the config endpoint, + * which will have config values that can be overwritten by clients via config.setDelegate + * @param {Object} aServiceSource + * @deprecated + * @return {Object} + */ +Config.prototype.setServiceSource = function setServiceSource(aServiceSource) { + this._serviceSource = aServiceSource; + return this._serviceSource; +}; + +/** + * Returns a config source that was retrieved from the metrics config service endpoint + * @deprecated + * @return {Object} + */ +Config.prototype.serviceSource = function serviceSource() { + return this._serviceSource; +}; + +/** + * Initialize config by setting config delegate or fetching it from the config endpoint as necessary + * If we wanted config to persist across page turns, we could save it via setCachedSource and fetch it every time we wake up, + * but we expect most clients to be single page apps + * + * @param {Function} (optional) configSourcesFn - a function that returns an array of key/value objects (dictionaries) for all of the config sources + * (e.g. the bag, the page, the control parent, etc.). + * @return {Promise} A Promise for the Config initialization. Returns an rejected Promise if failed to fetch config from server + */ +Config.prototype.init = function init(configSourcesFn) { + var isDefaultConfig = this._topic === NO_TOPIC_KEY || !reflect.isFunction(configSourcesFn); + + if (isDefaultConfig) { + this.logger.warn( + 'config.init(): Falling back to default config because configSourcesFn or a valid topic was not provided' + ); + } + + // default config does not need to initialize + if (this._initCalled || isDefaultConfig) { + return Promise.resolve(this); + } + + this._initCalled = true; + var self = this; + + this._configFetchPromise = Promise.resolve(configSourcesFn()) + .then(function (configSources) { + self.setDelegate({ + sources: function sources() { + return configSources; + } + }); + self._initialized = true; + return self; + }) + .catch(function (error) { + self._initCalled = false; + self._initialized = false; + throw error; + }); + + return this._configFetchPromise; +}; + +/** + * Release resources of the config + */ +Config.prototype.cleanup = function cleanup() { + this._initCalled = this._initialized = false; + this.setCachedSource(); + this.setDebugSource(); + this.resetDelegate(); + this.environment = null; + this.network = null; + this.logger = null; + this._topic = null; + this._debugSource = null; + this._cachedSource = null; + this._serviceSource = null; + this._initCalled = false; + this._initialized = false; + this._showedDebugWarning = false; + this._showedNoProvidedSourceWarning = false; + this._configFetchPromise = null; +}; + +/** + * Indicates whether or not config has initialized. + * Initialization is accomplished in one of the following ways: + * 1) a service source is set via a call to config.setServiceSource() TODO: Deprecated + * 2) a source function delegate is set via config.setDelegate() + * @return {Boolean} + */ +Config.prototype.initialized = function initialized() { + return this._initialized; +}; + +/* + * src/impls/metrics_config.js + * mt-client-config + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +/** + * Metrics config implementation for Config + * This class is a subclass of Config to provide the business methods for metrics collection + * @param topic + * @constructor + */ +var MetricsConfig = function MetricsConfig(topic) { + Config.call(this, topic); +}; + +MetricsConfig.prototype = Object.create(Config.prototype); +MetricsConfig.prototype.constructor = MetricsConfig; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Boolean config value which, when "true", tells clients to avoid all metrics code paths (different than simply not sending metrics). + * Useful for avoiding discovered client bugs. + * NOTE1: This will cause unrecoverable event loss, as the clients will not be recording events at all. + * NOTE2: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} + */ +MetricsConfig.prototype.disabled = function disabled(topic) { + return this.value('disabled', topic).then(function (disabled) { + return !!disabled; + }); +}; + +/** @DEPERECATED per Inclusive Software Efforts; use denylistedEvents instead + * Array config value which instructs clients to avoid sending particular event types. + * Useful for reducing server processing in emergencies by abandoning less-critical events. + * Useful for dealing with urgent privacy concerns, etc., around specific events. + * NOTE1: This will cause unrecoverable event loss, as the clients will not be recording events at all. + * NOTE2: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} Guaranteed to always return a Promise with the valid array, though the array may be empty if the value was unset in config + */ +MetricsConfig.prototype.blacklistedEvents = function blacklistedEvents(topic) { + return this.denylistedEvents(topic); +}; + +/** + * Array config value which instructs clients to avoid sending particular event types. + * Useful for reducing server processing in emergencies by abandoning less-critical events. + * Useful for dealing with urgent privacy concerns, etc., around specific events. + * NOTE1: This will cause unrecoverable event loss, as the clients will not be recording events at all. + * NOTE2: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} Guaranteed to always return a Promise with the valid array, though it may be empty if the value was unset in config + */ +MetricsConfig.prototype.denylistedEvents = function denylistedEvents(topic) { + var blacklistedEventsArray = this.value('blacklistedEvents', topic) + .then(function (returnArray) { + return returnArray || []; + }) + .catch(function () { + return []; + }); + var denylistedEventsArray = this.value('denylistedEvents', topic) + .then(function (returnArray) { + return returnArray || []; + }) + .catch(function () { + return []; + }); + return Promise.all([blacklistedEventsArray, denylistedEventsArray]).then(function (outputs) { + var blacklistedEventsArray = outputs[0]; + var denylistedEventsArray = outputs[1]; + return dedupedArray(blacklistedEventsArray, denylistedEventsArray); + }); +}; + +/** + * Returns a deduped array (similar to a Set object) using the contents from both arrayA and arrayB + * @param {Array} arrayA the first array to dedupe + * @param {Array} arrayB the other array to dedupe + * @return {Array} The deduped array + */ +function dedupedArray(arrayA, arrayB) { + var tempDict = {}; // necessary for returning array of unique values and not having access to Set object in ES5 + var returnArray = []; + if (arrayA) { + for (var i = 0; i < arrayA.length; i++) { + tempDict[arrayA[i]] = 0; // value for key doesn't matter; we want a list of unique values + } + } + if (arrayB) { + for (var j = 0; j < arrayB.length; j++) { + tempDict[arrayB[j]] = 0; // value for key doesn't matter; we want a list of unique values + } + } + returnArray = Object.keys(tempDict); + return returnArray; +} + +/** + * Array config value which instructs clients to avoid sending particular event fields. + * Useful for dealing with urgent privacy concerns, etc., around specific event fields (e.g. dsid) + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} Guaranteed to always return a Promise with the valid array, though it may be empty if the value was unset in config + */ +MetricsConfig.prototype.blacklistedFields = function blacklistedFields(topic) { + return this.denylistedFields(topic); +}; + +/** + * Array config value which instructs clients to avoid sending particular event fields. + * Useful for dealing with urgent privacy concerns, etc., around specific event fields (e.g. dsid) + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} Guaranteed to always return a Promise valid array, though it may be empty if the value was unset in config + */ +MetricsConfig.prototype.denylistedFields = function denylistedFields(topic) { + var blacklistedFieldsArray = this.value('blacklistedFields', topic) + .then(function (returnArray) { + return returnArray || []; + }) + .catch(function () { + return []; + }); + var denylistedFieldsArray = this.value('denylistedFields', topic) + .then(function (returnArray) { + return returnArray || []; + }) + .catch(function () { + return []; + }); + return Promise.all([blacklistedFieldsArray, denylistedFieldsArray]).then(function (outputs) { + var blacklistedFieldsArray = outputs[0]; + var denylistedFieldsArray = outputs[1]; + return dedupedArray(blacklistedFieldsArray, denylistedFieldsArray); + }); +}; + +/** @DEPRECATED per Inclusive Software Efforts; use removeDenylistedFields instead + * Remove all blacklisted fields from the passed-in object. + * IMPORTANT: This action is performed in-place for performance of not having to create new objects each time. + * NOTE: Typically all event_handlers will call this in addition to "recordEvent()" calling it because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {Object} eventFields a dictionary of event data + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} the passed-in object with any blacklisted fields removed + */ +MetricsConfig.prototype.removeBlacklistedFields = function removeBlacklistedFields(eventFields, topic) { + return this.removeDenylistedFields(eventFields, topic); +}; + +/** + * Remove all denylisted fields from the passed-in object. + * IMPORTANT: This action is performed in-place for performance of not having to create new objects each time. + * NOTE: Typically all event_handlers will call this in addition to "recordEvent()" calling it because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {Object} eventFields a dictionary of event data + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} the passed-in object with any denylisted fields removed + */ +MetricsConfig.prototype.removeDenylistedFields = function removeDenylistedFields(eventFields, topic) { + if (eventFields) { + return this.denylistedFields(topic).then(function (denylistedFieldsArray) { + for (var ii = 0; ii < denylistedFieldsArray.length; ii++) { + var aDenylistedField = denylistedFieldsArray[ii]; + // Double check this is not null (or empty string), or "delete" will blow up... + if (aDenylistedField) { + if (aDenylistedField in eventFields) { + delete eventFields[aDenylistedField]; + } + } + } + return eventFields; + }); + } else { + return Promise.resolve(eventFields); + } +}; + +/** @DEPRECATED per Inclusive Software Efforts; use metricsDisabledOrDenylistedEvent instead + * Convenience function used by event handlers to determine if they should build and return metricsData. + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} anEventType + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} returns "true" if <b>either</b> "disabled()" is true or "blacklistedEvents()" contains this eventType + */ +MetricsConfig.prototype.metricsDisabledOrBlacklistedEvent = function metricsDisabledOrBlacklistedEvent( + anEventType, + topic +) { + return this.metricsDisabledOrDenylistedEvent(anEventType, topic); +}; + +/** + * Convenience function used by event handlers to determine if they should build and return metricsData. + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} anEventType + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Boolean} returns "true" if <b>either</b> "disabled()" is true or "denylistedEvents()" contains this eventType + */ +MetricsConfig.prototype.metricsDisabledOrDenylistedEvent = function metricsDisabledOrDenylistedEvent( + anEventType, + topic +) { + return this.disabled(topic).then( + function (disabled) { + return ( + disabled || + (anEventType + ? this.denylistedEvents(topic).then(function (denylistedEvents) { + return denylistedEvents.indexOf(anEventType) > -1; + }) + : false) + ); + }.bind(this) + ); +}; + +/** + * Config map which instructs clients to de-res (lower the resolution of) particular event fields. + * The Privacy team typically requires device capacity information to be de-resed. + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} An array of config objects { fieldName, (optional) magnitude, (optional) significantDigits } + * Guaranteed to always return a valid array, though it may be empty if the value was unset in config + */ +MetricsConfig.prototype.deResFields = function deResFields(topic) { + return this.value('deResFields', topic).then(function (returnArray) { + return returnArray || []; + }); +}; + +/** + * De-res appropriate fields in the passed-in object by lowering the resolution of those field values. + * For example, a raw number of bytes "de-res"'d to MB, but without the "floor" filter, would look like these examples: + * 31708938240/1024/1024 ==> 30240 + * 15854469120/1024/1024 ==> 15120 + * 63417876480/1024/1024 ==> 60480 + * + * With the "floor" formula we replace the two least significant digits with "00" + * Doing so will convert values like so: + * + * 31708938240/1024/1024 ==> 30200 + * 15854469120/1024/1024 ==> 15100 + * 63417876480/1024/1024 ==> 60400 + * + * IMPORTANT: This action is performed in-place for performance of not having to create new objects each time. + * NOTE: Be careful not to call this method more than once for a given event, as de-resing a number more than + * once can lead to inaccurate reporting (numbers will likely be smaller than their real values) + * @param {Object} eventFields a dictionary of event data + * @param {String} (optional) topic an 'override' topic which will override the main topic. + * @returns {Promise} the passed-in object with any fields de-resed + */ +MetricsConfig.prototype.applyDeRes = function applyDeRes(eventFields, topic) { + if (eventFields) { + return this.deResFields(topic).then(function (deResFieldsConfigArray) { + var fieldName; + + deResFieldsConfigArray.forEach(function (deResFieldConfig) { + fieldName = deResFieldConfig.fieldName; + if (fieldName in eventFields) { + eventFields[fieldName] = number.deResNumber( + eventFields[fieldName], + deResFieldConfig.magnitude, + deResFieldConfig.significantDigits + ); + } + }); + + return eventFields; + }); + } else { + return Promise.resolve(eventFields); + } +}; + +export default Config; +export { MetricsConfig }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-client-constraints/dist/mt-client-constraints.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-client-constraints/dist/mt-client-constraints.esm.js new file mode 100644 index 0000000..c72d602 --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-client-constraints/dist/mt-client-constraints.esm.js @@ -0,0 +1,3103 @@ +import { storage, reflect, string, eventFields } from '@amp-metrics/mt-metricskit-utils-private'; +import { loggerNamed } from '@amp-metrics/mt-client-logger-core'; + +/* + * src/system/environment.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +/** + * Provides a set of environment-specific (platform-specific) functions which can be individually overridden for the needs + * of the particular environment, or replaced en masse by providing a single replacement environment delegate object + * The functionality in this class is typically replaced via a delegate. + * @see setDelegate + * @delegatable + * @constructor + */ +var Environment = function Environment() {}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Environment.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * Some clients have platform-specific implementations of these objects (e.g. iTunes.sessionStorage), so we cover them in case they need to be overriden. + */ +Environment.prototype.localStorageObject = storage.localStorageObject; +Environment.prototype.sessionStorageObject = storage.sessionStorageObject; + +/** + * Fetching identifier entity from AMS Metrics Identifier API + * @param {String} idNamespace - The id namespace that is defined under 'metricsIdentifier' in the bag + * @param {'userid' | 'clientid'} idType - The identifier type (userid or clientid) + * @param {Boolean} crossDeviceSync - The boolean flag to indicate whether the identifier is synced across devices + * @returns {Promise} + * @overridable + */ +Environment.prototype.platformIdentifier = function platformIdentifier(idNamespace, idType, crossDeviceSync) {}; + +/* + * src/system/index.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +var System = function System() { + this.environment = new Environment(); + this.logger = loggerNamed('mt-client-constraints'); +}; + +/* + * src/utils/key_value.js + * mt-client-constraints + * + * Copyright © 2023 Apple Inc. All rights reserved. + * + */ + +/** + * Recursively look up the given keyPath in the provided object. + * @param {Object} object an object that going to be used for seeking the keyPath + * @param {String} keyPath a string used to search one or more fields in the object + * @param {Boolean} inplace a boolean to indicate passing the original/cloned parent to the callback. + * @param {Function} callback a function will be called when the keyPath has been found in the object + * @example + * + * var target = { + * aField: { + * bField: { + * cArray: [1, 2, 3], + * dField: 12345 + * }, + * eArray: [{ + * gField: 'hello' + * }, { + * gField: 'world' + * }] + * } + * } + * + * // loop an object field + * lookForKeyPath(target, 'aField.bField', false, (value, key, keyPath, object) => { + * // value = target.aField.bField + * // key = 'bField' + * // keyPath = 'aField.bField' + * // object = target.aField; + * }); + * + * // loop an array field + * lookForKeyPath(target, 'aField.bField.cArray[]', false, (value, key, keyPath, object) => { + * // Will be called with 3 times with: + * // value = 1, key = 0, keyPath = aField.bField.cArray[0], object = target.aField.bField.cArray + * // value = 2, key = 1, keyPath = aField.bField.cArray[1], object = target.aField.bField.cArray + * // value = 3, key = 2, keyPath = aField.bField.cArray[2], object = target.aField.bField.cArray + * }); + * + * // loop a nested field + * lookForKeyPath(target, 'aField.bField.dField', false, (value, key, keyPath, object) => { + * // value = 12345 + * // key = 'dField' + * // keyPath = 'aField.bField.dField'; + * // object = target.aField.bField; + * }); + * + * // loop a field of an object in an array field + * lookForKeyPath(target, 'aField.eArray[].gField',false, (value, key, keyPath, object) => { + * // Will be called with 2 times with: + * // value = hello, key = gField, keyPath = aField.eArray[0].gField, object = aField.eArray[0] + * // value = world, key = gField, keyPath = aField.eArray[1].gField, object = aField.eArray[1] + * }); + */ +function lookForKeyPath(object, keyPath, inplace, callback) { + if (!reflect.isDefined(object) || !reflect.isDefinedNonNullNonEmpty(keyPath) || !reflect.isFunction(callback)) { + return object; + } + var keyPathArray = keyPath.split('.'); + return _lookForKeyPath(object, keyPathArray, null, [], null, inplace, callback); +} + +function _lookForKeyPath(object, keyPathArray, key, keyPath, parent, inplace, callback) { + if (reflect.isFunction(object)) { + return parent || object; + } + + keyPath.push(key); + + // Handle the leaf fields + if (keyPathArray.length === 0) { + callback(object, key, keyPath.slice(1).join('.'), parent); + return parent || object; + } + + if (!reflect.isDefined(object)) { + return parent || object; + } + + var clonedObject = inplace ? object : {}; + var fieldName = keyPathArray.shift(); + // Handle array values + if (fieldName.length > 2 && fieldName.indexOf('[]') === fieldName.length - 2) { + fieldName = fieldName.slice(0, -2); // remove [] + keyPath.push(fieldName); + reflect.extend(clonedObject, object); + var arrayValue = clonedObject[fieldName]; + if (reflect.isDefinedNonNull(arrayValue)) { + var processedArray = arrayValue.map(function (arrayItem, i) { + var updatedArray = inplace ? arrayValue : arrayValue.slice(); + _lookForKeyPath(arrayItem, keyPathArray.slice(), i, keyPath, updatedArray, inplace, callback); + keyPath.pop(); + return updatedArray[i]; + }); + clonedObject[fieldName] = processedArray; + } + } else { + var fieldValue = object[fieldName]; + reflect.extend(clonedObject, object); + // Handle normal values + clonedObject = _lookForKeyPath(fieldValue, keyPathArray, fieldName, keyPath, clonedObject, inplace, callback); + } + + keyPath.pop(); + + if (parent) { + parent[key] = clonedObject; + return parent; + } else { + return clonedObject; + } +} + +/* + * src/treatment_matchers/nested_fields_match + * mt-client-constraints + * + * Copyright © 2023 Apple Inc. All rights reserved. + * + */ + +var MATCH_TYPES_CONFIG = { + // "MATCH_TYPES_CONFIG.all" is used for checking if all items of the nested fields meet the filter condition. + all: { + initMatchValue: true, + accumulateMatchResult: function (accumulatedResult, matchResult) { + return accumulatedResult && matchResult; + } + }, + // "MATCH_TYPES_CONFIG.any" is used for checking if any of the items of the nested fields meet the filter condition. + any: { + initMatchValue: false, + accumulateMatchResult: function (accumulatedResult, matchResult) { + return accumulatedResult || matchResult; + } + } +}; + +function getMatchTypeConfig(matchType) { + var matchConfig = MATCH_TYPES_CONFIG[matchType]; + if (!reflect.isDefinedNonNull(matchConfig)) { + matchConfig = MATCH_TYPES_CONFIG.all; + } + return matchConfig; +} + +/** + * + * @param {String} fieldName - name of field in eventData + * @param {Object} eventData - a dictionary of event data + * @param {Object} matchOptions - an object contains the configurations that related to nested fields and the match options for the compouned matches. + * @param {Object} matchOptions.matchType - a flag to indicate the match type to apply to the nested fields. Available values "all", "any" + * @param {Object} matchOptions.matches - an object contains key/value pairs for the actual matches. + * @returns {Boolean} return true if the field value exists in "fieldMatchValues" otherwise return false + */ +function nestedFieldCompoundMatch(fieldName, eventData, matchOptions) { + if (!reflect.isObject(eventData) || !reflect.isObject(matchOptions)) { + return false; + } + var matchType = matchOptions.matchType; + var compoundMatches = matchOptions.matches; + if (!reflect.isDefinedNonNullNonEmpty(compoundMatches)) { + return false; + } + var matchTypeConfig = getMatchTypeConfig(matchType); + var isMatched = matchTypeConfig.initMatchValue; + lookForKeyPath(eventData, fieldName, false, function (_value, key, _keyPath, object) { + var matchResult = Object.keys(compoundMatches).every(function (matcherName) { + var matcherParam = compoundMatches[matcherName]; + + if (reflect.isDefinedNonNull(matchers[matcherName])) { + return matchers[matcherName](key, object, matcherParam); + } else { + return false; + } + }); + isMatched = matchTypeConfig.accumulateMatchResult(isMatched, matchResult); + }); + + return !!isMatched; +} + +/* + * src/treatment_matchers/non_empty_match + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +/** + * + * @param {String} fieldName - name of field in eventData + * @param {Object} eventData - a dictionary of event data + * @returns {Boolean} return true if the fieldName does exist in the eventData otherwise return false + */ +function nonEmptyMatch(fieldName, eventData) { + // Since the isObject will return undefined/null if the eventData is undefined/null. + // workaround here to convert the return value to boolean here to ensure this function returns boolean value. Should be fix it in the isObject() + return ( + !!reflect.isObject(eventData) && + eventData.hasOwnProperty(fieldName) && + reflect.isDefinedNonNullNonEmpty(eventData[fieldName]) + ); +} + +/* + * src/treatment_matchers/value_match + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +/** + * + * @param {String} fieldName - name of field in eventData + * @param {Object} eventData - a dictionary of event data + * @param {Array} fieldMatchValues - a list of possible values to match for that field + * @returns {Boolean} return true if the field value exists in "fieldMatchValues" otherwise return false + */ +function valueMatch(fieldName, eventData, fieldMatchValues) { + if (!reflect.isObject(eventData)) { + return false; + } + + var fieldValue = eventData[fieldName]; + return eventData.hasOwnProperty(fieldName) && fieldMatchValues.indexOf(fieldValue) > -1; +} + +/* + * src/treatment_matchers/non_value_match + * mt-client-constraints + * + * Copyright © 2023 Apple Inc. All rights reserved. + * + */ + +/** + * + * @param {String} fieldName - name of field in eventData + * @param {Object} eventData - a dictionary of event data + * @param {Array} fieldNotMatchValues - a list of values to not match for that field + * @returns {Boolean} return true if the field value do not match ALL the values in "fieldNotMatchValues" otherwise return false + */ +function nonValueMatch(fieldName, eventData, fieldNotMatchValues) { + if (!reflect.isObject(eventData) || !reflect.isArray(fieldNotMatchValues)) { + return false; + } + + var fieldValue = eventData[fieldName]; + return eventData.hasOwnProperty(fieldName) && fieldNotMatchValues.indexOf(fieldValue) === -1; +} + +/* + * src/treatment_matchers/index.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var matchers = { + nonEmpty: nonEmptyMatch, + valueMatches: valueMatch, + nonValueMatches: nonValueMatch, + nestedFieldMatches: nestedFieldCompoundMatch +}; + +/* + * src/utils/constants.js + * mt-client-constraints + * + * Copyright © 2019 Apple Inc. All rights reserved. + * + */ + +var FIELD_RULES = { + OVERRIDE_FIELD_VALUE: 'overrideFieldValue' +}; + +/* + * src/field_handlers/base.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +/** + * Provides methods to manage field constraints that apply to all fields + * @constructor + */ +var Base = function Base() {}; + +/** + ************************************ PUBLIC METHODS ************************************ + */ +/** + * @param {Object} eventFields a dictionary of event data + * @param {Object} fieldRules includes information about how to constrain a field + * @param {String} fieldName the name of the field to constrain + * @return {any} a field value that adheres to the provided rules + * @overridable + */ +Base.prototype.constrainedValue = function constrainedValue(eventFields, fieldRules, fieldName) { + var fieldValue = eventFields && eventFields.hasOwnProperty(fieldName) ? eventFields[fieldName] : null; + return this.applyConstraintRules(fieldValue, fieldRules); +}; + +/** + * @param {any} fieldValue an unconstrained value + * @param {Object} fieldRules includes information about how to constrain a field + * @return {any} a field value that adheres to the provided rules + * @overridable + */ +Base.prototype.applyConstraintRules = function applyConstraintRules(fieldValue, fieldRules) { + var returnValue = fieldValue; + if (fieldRules) { + var denylisted = fieldRules.denylisted || fieldRules.blacklisted; + if (denylisted) { + returnValue = null; + } else if (fieldRules.hasOwnProperty(FIELD_RULES.OVERRIDE_FIELD_VALUE)) { + returnValue = fieldRules.overrideFieldValue; + } + } + return returnValue; +}; + +/* + * src/field_actions/base.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ +var exceptionString = string.exceptionString; + +/** + * Parent class of field_actions classes + * @param {Object} constraintsInstance - the instance of Constraints class + * @constructor + */ +var Base$1 = function Base(constraintsInstance) { + // @private + this._constraintsInstance = constraintsInstance; +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Base$1.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * Abstract method to constrain value + * @param {Any} value - the value of fieldName in eventData, null if the eventData does not exist or the fieldName does not exist in the eventData + * @param {Object} fieldRules - includes information about how to constrain the field + * @param {Object} eventData - a dictionary of event data, which should include a pre-existing (unconstrained) field + * @param {String} fieldName - field name/path that can locate the "value" parameter in eventData + * @return {Any} the constrained value + */ +Base$1.prototype.constrainedValue = function constrainedValue(value, fieldRules, eventData, fieldName) { + throw exceptionString('field_actions.Base', 'constrainedValue'); +}; + +/** + * A public method to wrap the "constrainedValue" method to contains common code for all field_actions subclasses. + * @param {Any} value - field value in eventData that is performed by field_actions + * @param {String} fieldName - field name/path that can locate the "value" parameter in eventData + * @param {Object} eventData - a dictionary of event data, which should include a pre-existing (unconstrained) field + * @param {Object} fieldRules - includes information about how to constrain the field + * @return {Any} the constrained value + */ +Base$1.prototype.performAction = function performAction(value, fieldName, eventData, fieldRules) { + // return the original value if there are no rules to apply + if (reflect.isDefinedNonNull(fieldRules) && !reflect.isEmptyObject(fieldRules)) { + value = this.constrainedValue(value, fieldRules, eventData, fieldName); + } + return value; +}; + +/* + * src/utils/url.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * @param {String} aUrl + * @return {String} the hostname part of the provided url e.g. www.apple.com + * @overridable + * Note: see https://nodejs.org/api/url.html for a diagram illustrating the different parts of a URL + */ +function hostname(aUrl) { + aUrl = aUrl || ''; + + var urlParts = withoutParams(aUrl).split('/'); + var hostAndAuth; + var host; + var hostname; + + if (aUrl.indexOf('//') === -1) { + hostAndAuth = urlParts[0]; + } else { + hostAndAuth = urlParts[2]; + } + + host = hostAndAuth.substring(hostAndAuth.indexOf('@') + 1); + hostname = host.split(':')[0]; + + return hostname; +} + +/** + * @param {String} aUrl + * @return {String} the domain part of the provided url shortened to the "main" part of the domain (apple.com or apple.co.uk) + * @overridable + * Note: this method uses a heuristic to determine if the url has a country code second level domain and will miss + * ccSLDs that are not exactly two chracters long or one of: 'com', 'org, 'net', edu', 'gov' + * For example, "www.example.ltd.uk" will be shortened to "ltd.uk" + * All two-letter top-level domains are ccTLDs: https://en.wikipedia.org/wiki/Country_code_top-level_domain + */ +function mainDomain(aUrl) { + var urlSegments = hostname(aUrl).split('.'); + var lastSegment = urlSegments[urlSegments.length - 1]; + var secondToLastSegment = urlSegments[urlSegments.length - 2]; + var segmentsToKeep = 2; + + if ( + lastSegment && + lastSegment.length === 2 && + secondToLastSegment && + (secondToLastSegment.length === 2 || secondToLastSegment in reservedCCSLDs()) + ) { + segmentsToKeep = 3; + } + + return urlSegments.slice(-1 * segmentsToKeep).join('.'); +} + +/** + * @return {Object} a map of country-code second level domains (ccSLDs) used in the heuristic + * that determines the main part of a domain (defined as TLD + ccSLD + 1) + * @overridable + */ +function reservedCCSLDs() { + var reservedCCSLDs = { + com: true, + org: true, + net: true, + edu: true, + gov: true + }; + + return reservedCCSLDs; +} + +/** + * @param {String} aUrl + * @param {Array|Object} allowedParams An array of allowed params or an object with each param containing its allowed values + * @return {String} the url with any disallowed query parameters and/or hash removed + * @overridable + * + * @example + * withoutParams('https://itunes.apple.com/?p1=10&p2=hello', ['p1']); + * // returns 'https://itunes.apple.com/?p1=10' + * + * withoutParams('https://itunes.apple.com/?p1=10&p2=hello', { + * p1: { + * allowedValues: ['20', '30'] + * }, + * . p2: { + * allowedValues: ['hello'] + * } + * }); + * // returns 'https://itunes.apple.com/?p2=hello' + */ +function withoutParams(aUrl, allowedParams) { + var url = aUrl || ''; + var urlParts = url.split('?'); + var urlPrefix = urlParts[0]; + var urlParams = withoutHash(urlParts[1]).split('&'); + + var filteredParams = urlParams + .filter(function (paramString) { + var keyAndVal = paramString.split('='); + var paramName = keyAndVal[0]; + var paramVal = keyAndVal[1]; + + if (reflect.isArray(allowedParams)) { + return allowedParams.indexOf(paramName) !== -1; + } + + if (reflect.isObject(allowedParams)) { + return ( + reflect.isObject(allowedParams[paramName]) && + reflect.isArray(allowedParams[paramName].allowedValues) && + allowedParams[paramName].allowedValues.indexOf(paramVal) !== -1 + ); + } + + return false; + }) + .join('&'); + + return filteredParams.length > 0 ? urlPrefix + '?' + filteredParams : urlPrefix; +} + +/** + * Returns the url with the hash removed + * @param {String} aUrl + * @return {String} the url with any hash removed + * @overridable + * @example + * withoutHash('https://itunes.apple.com:80/music?param1=abc¶m2=def¶m3=ghi#someHash') + * // returns 'https://itunes.apple.com:80/music?param1=abc¶m2=def¶m3=ghi' + */ +function withoutHash(aUrl) { + var url = aUrl || ''; + return url.split('#')[0]; +} + +/** + * Returns the url with all of the string replacements applied + * @param {String} aUrl + * @param {Object} replacements The list of replacements that should be applied on the url + * @param {String} replacements.searchPattern A stringified regex pattern to search for + * @param {String} replacements.replaceVal The string to replace the match with + * @param {String} replacements.flags Regex flags to include with the search pattern (ex: 'g') + * @return {String} The url with all replacements applied + * @overridable + * + * @example + * withReplacements('https://itunes.apple.com:80/music?param1=abc¶m2=def¶m3=ghi#someHash', [ + * { + * searchPattern: 'music', + * replaceVal: 'm' + * } + * ]) + * // returns 'https://itunes.apple.com:80/m?param1=abc¶m2=def¶m3=ghi#someHash' + * + * withReplacements('https://apple.com/1234', [ + * { + * searchPattern: '\d', + * replaceVal: 'X', + * flags: 'g' + * } + * ]) + * // returns 'https://apple.com/XXXX' + */ +function withReplacements(aUrl, replacements) { + var url = aUrl || ''; + var urlReplacements = replacements || []; + + var replacedUrl = urlReplacements.reduce(function (url, replacement) { + var searchPattern = new RegExp(replacement.searchPattern, replacement.flags); + var replaceVal = replacement.replaceVal; + return url.replace(searchPattern, replaceVal); + }, url); + + return replacedUrl; +} + +/* + * src/utils/id_generator.js + * mt-client-constraints + * + * Copyright © 2023 Apple Inc. All rights reserved. + * + */ + +var ID_SEPARATOR = '-'; +var DEFAULT_GENERATED_ID_SEPARATOR = 'z'; + +/** + * @param {Object} options includes - information about how to generate an ID + * @param {Number} options.idVersion - the version of the ID + * @param {Number} options.time - the time to be a part of the ID (optional) + * @param {String} options.generatedIdSeparator - a token-separated hex string of metadata to attach to a ID (optional) default to "z" + * @return {String} a generated ID + */ +function generateId(options) { + if (!reflect.isDefinedNonNull(options) || !reflect.isInteger(options.idVersion)) { + return '0'; + } + var uuid = string.uuid(); + var generatedIdSeparator = options.generatedIdSeparator || DEFAULT_GENERATED_ID_SEPARATOR; + var idString = generatedIdMetadata(options) + ID_SEPARATOR + uuid || ''; + + var convertedIdString = idString + .split(ID_SEPARATOR) + .map(function (segment) { + var segmentAsNumber = parseInt(segment, 16); + return string.convertNumberToBaseAlphabet(segmentAsNumber, string.base61Alphabet); + }) + .join(generatedIdSeparator); + + return convertedIdString; +} + +/** + * @param {Object} options includes - information about how to generate an ID + * @param {Number} options.idVersion - the version of the ID + * @param {Number} options.time - the time to be a part of the ID (optional) + * @return {String} a token-separated hex string of metadata to attach to a ID, + */ +function generatedIdMetadata(options) { + var parameters = [options.idVersion]; + + if (options.time) { + parameters.push(options.time); + } + + return parameters + .map(function (param) { + return param.toString(16); + }) + .join(ID_SEPARATOR); +} + +/* + * src/field_actions/id_action/time_based_action.js + * mt-client-constraints + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +/** + * The time-based ID generating strategy + * The ID value will change after its lifespan expires + */ +function constrainedValue(idString, idRules, eventData, fieldName) { + var storageKey = this.storageKey(fieldName, eventData, idRules); + var environment = this._constraintsInstance.system.environment; + var idData = storage.objectFromStorage(environment.localStorageObject(), storageKey) || {}; + + idData.value = this.idString(idData, idRules); + if ( + this.rulesHaveLifespan(idRules) && + (!reflect.isNumber(idData.expirationTime) || this.timeExpired(idData.expirationTime)) + ) { + idData.expirationTime = this.expirationTime(idRules.lifespan); + } + + storage.saveObjectToStorage(environment.localStorageObject(), storageKey, idData); + idString = idData.value; + + return idString; +} + +/* + * src/field_actions/id_action/session_time_based_action.js + * mt-client-constraints + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +// @private +// A global cache storage to store the served clientIds by the clientId storageKey +// Make it as a global variable to ensure the cached clientId can be shared between multiple MK instances +var _sessionIdCache = {}; + +/** + * The user-session-based + time-based ID generating strategy + * When the id getting expired, this function will return a consistent ID until the current user session ends, even if the ID is scheduled to expire in the middle of the session + */ +function constrainedValue$1(idString, idRules, eventData, fieldName) { + var storageKey = this.storageKey(fieldName, eventData, idRules); + var returnedIdString = _sessionIdCache[storageKey]; + + if (!returnedIdString) { + returnedIdString = constrainedValue.apply(this, arguments); + _sessionIdCache[storageKey] = returnedIdString; + } + return returnedIdString; +} + +/* + * src/field_actions/id_action/id_action.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var STORAGE_KEY_SEPARATOR = '_'; +var MT_ID_NAMESPACE = 'mtId'; + +var IdAction = function IdAction() { + Base$1.apply(this, arguments); +}; + +IdAction.prototype = Object.create(Base$1.prototype); +IdAction.prototype.constructor = IdAction; + +/* + * Possible strategies that can be used to scope an ID value + */ +IdAction.prototype.SCOPE_STRATEGIES = { + ALL: 'all', + MAIN_DOMAIN: 'mainDomain' +}; + +/** + * @param {Object} (optional) idRules includes information about when to expire the ID + * @return {Boolean} + */ +IdAction.prototype.rulesHaveLifespan = function rulesHaveLifespan(idRules) { + idRules = idRules || {}; + + return reflect.isNumber(idRules.lifespan); +}; + +/** + * @param {Number} (optional) lifespan the amount of time, in milliseconds, that an ID should be valid for + * @return {Number} a timestamp in ms since epoch, or null if no lifespan was provided + */ +IdAction.prototype.expirationTime = function expirationTime(lifespan) { + return lifespan ? Date.now() + lifespan : null; +}; + +/** + * @param {String} fieldName - name of the field being field_actions in eventData + * @param {Object} eventData a dictionary of event data + * @param {Object} idRules includes information about how to namespace/scope the id + * @return {String} the key that id data should be stored under + * @example + * (storageKeyPrefix ? storageKeyPrefix : mtId_<fieldName>)_<namespace>_(scopeStrategy ? <eventData[scopeFieldName]> : 'all') + * @overridable + */ +IdAction.prototype.storageKey = function storageKey(fieldName, eventData, idRules) { + var scope = this.scope(eventData, idRules); + return this.storageKeyPrefix(idRules, fieldName) + (scope ? STORAGE_KEY_SEPARATOR + scope : ''); +}; + +/** + * @param {Object} idRules includes information about how to namespace/scope the id + * @param {String} fieldName - name of the field being field_actions in eventData + * @return {String} a prefix to be used when storing id data in localStorage + * @overridable + */ +IdAction.prototype.storageKeyPrefix = function storageKeyPrefix(idRules, fieldName) { + return idRules && reflect.isString(idRules.storageKeyPrefix) && idRules.storageKeyPrefix.length > 0 + ? idRules.storageKeyPrefix + : MT_ID_NAMESPACE + STORAGE_KEY_SEPARATOR + fieldName; +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} idRules includes information about how to namespace/scope the id + * @return {String} the namespace/scope for this set of event data and rules + * @overridable + */ +IdAction.prototype.scope = function scope(eventData, idRules) { + var idKey = ''; + + if (idRules) { + if (idRules.namespace) { + idKey += idRules.namespace; + } + if (idRules.scopeStrategy) { + var domainScope; + + switch (idRules.scopeStrategy) { + case this.SCOPE_STRATEGIES.MAIN_DOMAIN: + var scopeFieldName = idRules.scopeFieldName; + domainScope = mainDomain(eventData[scopeFieldName]) || 'unknownDomain'; + break; + case this.SCOPE_STRATEGIES.ALL: /* fall through */ + default: + // no scope + domainScope = this.SCOPE_STRATEGIES.ALL; + break; + } + + if (idKey.length) { + idKey += STORAGE_KEY_SEPARATOR; + } + idKey += domainScope; + } + } + + return idKey; +}; + +/** + * @param {Object} (optional) existingIdData + * @param {Object} (optional) idRules includes information about when to expire the ID + * @return {String} an ID + * @overridable + */ +IdAction.prototype.idString = function idString(existingIdData, idRules) { + var existingId = existingIdData ? existingIdData.value : null; + var returnValue = existingId; + + if ( + !existingId || + (reflect.isNumber(existingIdData.expirationTime) && this.timeExpired(existingIdData.expirationTime)) + ) { + returnValue = this.generateId(idRules); + } + + return returnValue; +}; + +/** + * @param {Object} (optional) idRules includes information about how to constrain the field + * @return {String} a generated ID + * @overridable + * @see comments in the related MTClientId.java + */ +IdAction.prototype.generateId = function generateId$1(idRules) { + idRules = idRules || {}; + return generateId({ + idVersion: this.generatedIdVersion(), + time: this.expirationTime(idRules.lifespan), + generatedIdSeparator: this.generatedIdSeparator(idRules.tokenSeparator) + }); +}; + +/** + * @return {Number} the version of the generated ID + * @overridable + */ +IdAction.prototype.generatedIdVersion = function generatedIdVersion() { + return 4; +}; + +/** + * @return {String} the separator used to tokenize sections of an unformatted ID string + * @overridable + */ +IdAction.prototype.idTokenSeparator = function idTokenSeparator() { + return '-'; +}; + +/** + * @param {String} (optional) separator + * @return {String} the separator used to tokenize sections of a finalized, formatted ID string + * @overridable + */ +IdAction.prototype.generatedIdSeparator = function generatedIdSeparator(separator) { + return separator || 'z'; +}; + +/** + * @param {Number} timestamp a timestamp in ms since epoch + * @return {Boolean} + * @overridable + */ +IdAction.prototype.timeExpired = function timeExpired(timestamp) { + return timestamp <= Date.now(); +}; + +/** + * @param {String} idString - the ID field in eventData + * @param {Object} idRules - includes information about how to constrain the field + * @param {String}(optional) idRules.storageKeyPrefix - a prefix to be used when storing ID data in localStorage, default is MT_ID_NAMESPACE + * @param {String}(optional) idRules.namespace - a string to be used when storing ID data in localStorage. + * @param {String}(optional) idRules.scopeStrategy - a strategy that can be used to scope a ID value [all/mainDomain] + * @param {String}(optional) idRules.scopeFieldName - name of the scope field in eventData, the value would be an URL and used to get the main domain as a part of scope. It is used when parameters.scopeStrategy set to "mainDomain" + * @param {String}(optional) idRules.tokenSeparator - the separator used to tokenize sections of a finalized, formatted ID string. Default is 'z' + * @param {Integer}(optional) idRules.lifespan - the expiration period for the ID (milliseconds) + * @param {Boolean}(optional) idRules.persistIdForSession - a boolean to indicate whether to persist the ID until the current user session ends, even if it is scheduled to expire in the middle of the session + * @param {Object} eventData - a dictionary of event data, which should include a pre-existing (unconstrained) ID field + * @param {String} fieldName - name of the field being field_actions in eventData + * @return {String} the constrained ID + */ +IdAction.prototype.constrainedValue = function constrainedValue$2(idString, idRules, eventData, fieldName) { + if (eventData && idRules && !reflect.isEmptyObject(idRules)) { + if (idRules.persistIdForSession === true) { + idString = constrainedValue$1.apply(this, arguments); + } else { + idString = constrainedValue.apply(this, arguments); + } + } + return idString; +}; + +/* + * src/field_handlers/client_id.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +/** + * Provides methods to manage clientId field constraints. + * @constructor + */ +var ClientId = function ClientId(base, constraintsInstance) { + // @private + this._base = base; + // @private + this._idAction = new IdAction(constraintsInstance); + this._idAction.setDelegate({ + storageKey: function storageKey(fieldName, eventData, idRules) { + return this.storageKeyPrefix() + '_' + this.scope(eventData, idRules); + }.bind(this._idAction), + storageKeyPrefix: function storageKeyPrefix() { + return 'mtClientId'; + } + }); +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * @param {Object} eventFields a dictionary of event data, which may include a pre-existing (unconstrained) clientId + * @param {Object} clientIdRules includes information about when to expire the clientId and how to namespace/scope it + * @return {String} a clientId that adheres to the provided rules + * @overridable + */ +ClientId.prototype.constrainedValue = function constrainedValue(eventFields, clientIdRules) { + // adapt expirationPeriod to lifespan + var clonedRules = clientIdRules; + if (clientIdRules && reflect.isNumber(clientIdRules.expirationPeriod)) { + clonedRules = reflect.extend({}, clientIdRules); + clonedRules.lifespan = clonedRules.expirationPeriod; + delete clonedRules.expirationPeriod; + } + var clientId = eventFields ? eventFields.clientId : null; + var clientIdString = this._idAction.performAction(clientId, 'clientId', eventFields, clonedRules); + + return this._base.applyConstraintRules(clientIdString, clientIdRules); +}; + +/* + * src/field_actions/url_action.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var UrlAction = function UrlAction() { + Base$1.apply(this, arguments); +}; + +UrlAction.prototype = Object.create(Base$1.prototype); +UrlAction.prototype.constructor = UrlAction; + +/* + * Possible truncation strategies that can be applied to a parentPageUrl + */ +UrlAction.prototype.SCOPES = { + HOSTNAME: 'hostname', + FULL: 'full', + FULL_WITHOUT_PARAMS: 'fullWithoutParams', + FULL_WITH_REPLACEMENTS: 'fullWithReplacements' +}; + +/** + * @param {String} url - the URL field in eventData + * @param {Object} fieldRules - includes information about how to constrain the field + * @return {String} the constrained URL + */ +UrlAction.prototype.constrainedValue = function constrainedValue(url, fieldRules) { + if (url && fieldRules && fieldRules.scope) { + switch (fieldRules.scope) { + case this.SCOPES.HOSTNAME: + url = hostname(url); + break; + case this.SCOPES.FULL_WITHOUT_PARAMS: + url = withoutParams(url, fieldRules.allowedParams); + break; + case this.SCOPES.FULL_WITH_REPLACEMENTS: + url = withReplacements(url, fieldRules.replacements); + break; + case this.SCOPES.FULL: /* fall through */ + } + } + + return url; +}; + +/* + * src/field_handlers/parent_page_url.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +/** + * Provides methods to manage parentPageUrl field constraints. + * @constructor + */ +var ParentPageUrl = function ParentPageUrl(base) { + // @private + this._base = base; + // @private + this._urlAction = new UrlAction(); +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * @param {Object} eventFields a dictionary of event data, which should include a pre-existing (unconstrained) parentPageUrl + * @param {Object} parentPageUrlRules includes information about whether to strip certain parts of the URL + * @return {String} a parentPageUrl modified according to the provided rules + * @overridable + */ +ParentPageUrl.prototype.constrainedValue = function constrainedValue(eventFields, parentPageUrlRules) { + var parentPageUrl = eventFields ? eventFields.parentPageUrl : null; + var modifiedUrl = this._urlAction.performAction(parentPageUrl, 'parentPageUrl', eventFields, parentPageUrlRules); + return this._base.applyConstraintRules(modifiedUrl, parentPageUrlRules); +}; + +/* + * src/field_handlers/index.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +/** + * @deprecated the field handlers has been deprecated and replaced with "De-res treatments(src/field_actions/*)" + * @param constraintsInstance + * @constructor + */ +var FieldHandlers = function (constraintsInstance) { + this.base = new Base(constraintsInstance); + this.clientId = new ClientId(this.base, constraintsInstance); + this.parentPageUrl = new ParentPageUrl(this.base, constraintsInstance); +}; + +/* + * src/treatment/legacy_treatment.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var LegacyTreatment = function LegacyTreatment(constraintInstance) { + // @private + this._fieldHandlers = new FieldHandlers(constraintInstance); +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} eventConstraints a set of constraints to apply to this event + * @return {Object} the event data modified according to the appropriate constraints + * Note: event fields will be modified in place and also returned + * @example + * var eventData = { + * eventType: 'click', + * pageType: 'TopCharts', + * parentPageUrl: 'https://itunes.apple.com/music/topcharts/12345', + * // etc. + * }; + * var eventConstraints = { + * fieldConstraints: { parentPageUrl: { scope: 'hostname' } } + * } + * legacyTreatment.applyConstraints(eventData, eventConstraints) => + * { + * eventType: click, + * pageType: 'TopCharts', + * parentPageUrl: 'itunes.apple.com', // truncated to hostname only + * etc... // all other fields remain the same + * } + */ +LegacyTreatment.prototype.applyConstraints = function applyConstraints(eventData, eventConstraints) { + if (eventConstraints && eventConstraints.fieldConstraints) { + eventData = this.applyFieldConstraints(eventData, eventConstraints.fieldConstraints); + } + + return eventData; +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} fieldConstraints a set of constraints to apply to fields in this event, keyed by field name + * @return {Object} the event data modified according to the appropriate constraints + * Note: event fields will be modified in place and also returned + */ +LegacyTreatment.prototype.applyFieldConstraints = function applyFieldConstraints(eventData, fieldConstraints) { + if (fieldConstraints) { + var constrainedFieldValues = {}; + var constrainedValue; + var fieldRules; + var fieldName; + + for (fieldName in fieldConstraints) { + fieldRules = fieldConstraints[fieldName]; + if ( + eventData.hasOwnProperty(fieldName) || + fieldRules.generateValue === true || + fieldRules.hasOwnProperty(FIELD_RULES.OVERRIDE_FIELD_VALUE) + ) { + if (fieldName in this._fieldHandlers) { + constrainedValue = this._fieldHandlers[fieldName].constrainedValue(eventData, fieldRules); + } else { + constrainedValue = this._fieldHandlers.base.constrainedValue(eventData, fieldRules, fieldName); + } + + constrainedFieldValues[fieldName] = constrainedValue; + } + } + + // assign constrained values only after all of them have been calculated + // in case some constrained values depend on more than one original field values + for (fieldName in constrainedFieldValues) { + eventData[fieldName] = constrainedFieldValues[fieldName]; + } + + eventData = eventFields.mergeAndCleanEventFields(eventData); + } + + return eventData; +}; + +/* + * src/utils/constraint_generator + * mt-client-constraints + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +/** + * Adding/replacing the rule properties of targetRules with the ones of newRules + * @param {Object} targetRules - an object contains an <fieldConstraintsName> property with field rules in it. + * @param {Object} newRules - an object contains an <fieldConstraintsName> property with new field rules in it. + * @param {String} fieldRulesKeyName - the field rule property name + * @param {Function} initialFieldRules - a callback function to decide the target field rule object. Function signature: function (targetRules, sourceRules, fieldName) + * @returns {Object} updated target rules, all replacements are in-place updating unless the passed-in targetRules object does not exist + */ +function updateFieldRulesets(targetRules, newRules, fieldRulesKeyName, initialFieldRules) { + var updatedRules = targetRules || {}; + initialFieldRules = + initialFieldRules || + function (targetRules, sourceRules, fieldName) { + return targetRules[fieldName] || {}; + }; + + if (newRules && newRules[fieldRulesKeyName]) { + var fieldName; + var propertyName; + var updatedFieldRules = updatedRules[fieldRulesKeyName] || {}; + updatedRules[fieldRulesKeyName] = updatedFieldRules; + + // copying the top level rules over is sufficient for a simple rule structure + for (fieldName in newRules[fieldRulesKeyName]) { + var fieldRules = initialFieldRules(updatedFieldRules, newRules[fieldRulesKeyName], fieldName); + updatedFieldRules[fieldName] = fieldRules; + for (propertyName in newRules[fieldRulesKeyName][fieldName]) { + fieldRules[propertyName] = newRules[fieldRulesKeyName][fieldName][propertyName]; + } + } + } + + return updatedRules; +} + +/* + * src/constraint_generator/legacy_constraint_generator.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +var LegacyConstraintGenerator = function LegacyConstraintGenerator(constraintInstance) { + this.treatment = new LegacyTreatment(constraintInstance); +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} topicConfig the AMP Metrics topic config to use to look up constraint_generator values + * @param {String}(optional) topic defines the AMP Analytics "topic" to look up the constraint profile + * @return {Object} a set of constraints to apply to this event + * @overridable + * Constraint rules will be applied in the order they are provided in config. + * @example + * Given the following config: + * { constraints: { + * defaultProfile: 'strict', + * profiles: { + * strict: { + * precedenceOrderedRules: [ + * { + * filters: 'any', + * fieldConstraints: { + * clientId: { + * generateValue: true, + * tokenSeparator: 'z', + * scopeFieldName: 'parentPageUrl', + * scopeStrategy: 'mainDomain', // apple.com or apple.co.uk + * expirationPeriod: 86400000 // 24h + * }, + * parentPageUrl: { + * scope: 'hostname' // www.apple.com + * } + * } + * }, + * { + * filters: { + * valueMatches: { + * eventType: ['click'], + * actionType: ['signUp'] + * }, + * nonEmptyFields: ['pageHistory'] + * }, + * fieldConstraints: { + * parentPageUrl: { + * scope: 'fullWithoutParams' + * } + * } + * }, + * { + * filters: { + * valueMatches: { + * userType: ['signedIn'] + * } + * }, + * fieldConstraints: { + * clientId: { + * scopeStrategy: 'all' + * }, + * dsId: { blacklisted: true } + * } + * } + * ] + * } + * } + * } } + * + * new Constraints(config).constraintsForEvent({ eventType: 'click', userType: 'signedIn', actionType: 'navigate', ... }) returns: + * { + * fieldConstraints: { + * clientId: { + * generateValue: true, // from 'any' match + * tokenSeparator: 'z', // from 'any' match + * scopeFieldName: 'parentPageUrl', // from 'any' match + * scopeStrategy: 'all', // from userType=signdIn match + * expirationPeriod: 86400000 // from 'any' match + * }, + * dsId: { blacklisted: true }, // from userType=signedIn match + * parentPageUrl: { + * scope: 'hostname' // from 'any' match + * // (the event did not match the rule with eventType=click, + * // actionType=signUp, nonEmpty pageHistory) + * } + * } + * } + */ +LegacyConstraintGenerator.prototype.constraintsForEvent = function constraintsForEvent(eventData, topicConfig, topic) { + if (!topicConfig) { + return Promise.resolve(null); + } + var self = this; + + // Use Promise.resolve to wrap the constraintProfiles() here in case of the client delegate the constraintProfile method and returns a non-promise value + return Promise.resolve(topicConfig.constraintProfile(topic)) + .then(function (constraintProfile) { + if (!constraintProfile) { + return null; + } + var profilePath = 'constraints.profiles.' + constraintProfile; + return topicConfig.value(profilePath, topic); + }) + .then(function (constraintsConfig) { + var constraints = null; + + if (constraintsConfig && constraintsConfig.precedenceOrderedRules) { + constraints = constraintsConfig.precedenceOrderedRules.reduce(function (accumulatedRules, rule) { + if (self.eventMatchesRule(eventData, rule)) { + accumulatedRules = self.updateRules(accumulatedRules, rule); + } + + return accumulatedRules; + }, {}); + } + + return constraints; + }); +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} matchRule contains information about whether an event matches a rule + * @return {Boolean} + * @example + * var event = { eventType: 'click', userType: 'signedIn', actionType: 'navigate', ... }; + * var matchRule = { + * filters: { + * valueMatches: { + * eventType: ['click'] + * }, + * nonEmptyFields: ['actionType'] + * }, + * fieldConstraints: { ... } + * }; + * eventMatchesRule(event, matchRule); // => true + */ +LegacyConstraintGenerator.prototype.eventMatchesRule = function eventMatchesRule(eventData, matchRule) { + var returnValue = false; + + if (eventData && matchRule.filters) { + if (matchRule.filters === 'any') { + returnValue = true; + } else if (reflect.isObject(matchRule.filters)) { + returnValue = + this.eventMatchesNonEmptyFields(eventData, matchRule.filters.nonEmptyFields) && + this.eventMatchesFieldValues(eventData, matchRule.filters.valueMatches); + } + } + + return returnValue; +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Array<string>} nonEmptyFieldNames + * @return {Boolean} + */ +LegacyConstraintGenerator.prototype.eventMatchesNonEmptyFields = function eventMatchesNonEmptyFields( + eventData, + nonEmptyFieldNames +) { + var returnValue = false; + + if (eventData) { + if (!nonEmptyFieldNames || !reflect.isArray(nonEmptyFieldNames)) { + returnValue = true; + } else { + returnValue = nonEmptyFieldNames.every(function (fieldName) { + return matchers.nonEmpty(fieldName, eventData); + }); + } + } + + return returnValue; +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} valueMatches a mapping of field names to lists of possible values to match for that field. A matching field value is determined using strict equality. Field values can be of any primitive type. + * @return {Boolean} + */ +LegacyConstraintGenerator.prototype.eventMatchesFieldValues = function eventMatchesFieldValues( + eventData, + valueMatches +) { + var returnValue = false; + + if (eventData) { + if (!valueMatches || !reflect.isObject(valueMatches) || reflect.isEmptyObject(valueMatches)) { + returnValue = true; + } else { + returnValue = Object.keys(valueMatches).every(function (fieldMatchName) { + var fieldMatchValues = valueMatches[fieldMatchName]; + return matchers.valueMatches(fieldMatchName, eventData, fieldMatchValues); + }); + } + } + + return returnValue; +}; + +/** + * @param {Object} currentRules a dictionary of constraint rules + * @param {Object} newRule a dictionary of rule data to be added to the current rules + * @return {Object} the updated set of rules + * Note: the current rules will be modified in place and also returned + */ +LegacyConstraintGenerator.prototype.updateRules = function updateRules(currentRules, newRule) { + return updateFieldRulesets(currentRules, newRule, 'fieldConstraints'); +}; + +/* + * src/event_actions/denylisted_event_action.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var DenylistedEventAction = function DenylistedEventAction() {}; + +/** + * denylisting the event if the denylisted parameter is true + * @param {Object} eventData a dictionary of event data + * @param {Object} originalEventData the original event data + * @param {Boolean} isDenylisted true if denylisting the entire event + * @return {Object} return the passed-in eventData if denylisted not equals to false, otherwise, return "null" + */ +DenylistedEventAction.prototype.performAction = function performAction(eventData, originalEventData, isDenylisted) { + return isDenylisted !== true ? eventData : null; +}; + +/* + * src/event_actions/denylisted_fields_action.js + * mt-client-constraints + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +var DenylistedFieldsAction = function DenylistedFieldsAction() {}; + +/** + * remove the denylisted event fields from the passed-in eventData + * @param {Object} eventData a dictionary of event data + * @param {Object} originalEventData the original event data + * @param {Array} denylistedFields the denylisted fields + * @return {Object} return a dictionary of event data that excluded the denylisted fields or "null" if all fields are removed. + */ +DenylistedFieldsAction.prototype.performAction = function performAction( + eventData, + originalEventData, + denylistedFields +) { + if (!eventData || !reflect.isArray(denylistedFields) || reflect.isEmptyArray(denylistedFields)) { + return eventData; + } + eventData = reflect.extend({}, eventData); + + denylistedFields.forEach(function (denylistedField) { + delete eventData[denylistedField]; + }); + + return reflect.isEmptyObject(eventData) ? null : eventData; +}; + +/* + * src/event_actions/allowlisted_fields_action.js + * mt-client-constraints + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +var AllowlistedFieldsAction = function AllowlistedFieldsAction() {}; + +/** + * remove the event fields from the passed-in eventData if it is not in the allowlistedFields + * @param {Object} eventData a dictionary of event data + * @param {Object} originalEventData the original event data + * @param {Array} allowlistedFields the allowlisted fields + * @return {Object} return a dictionary of event data that only included the allowlisted fields + */ +AllowlistedFieldsAction.prototype.performAction = function performAction( + eventData, + originalEventData, + allowlistedFields +) { + // Ignoring an empty allowlistedFields to have consistent behavior with Native MetricsKit + if (!eventData || !reflect.isArray(allowlistedFields) || reflect.isEmptyArray(allowlistedFields)) { + return eventData; + } + var returnedData = {}; + + allowlistedFields.forEach(function (allowlistedField) { + if (reflect.isDefinedNonNull(eventData[allowlistedField])) { + returnedData[allowlistedField] = eventData[allowlistedField]; + } + }); + + return !reflect.isEmptyObject(returnedData) ? returnedData : null; +}; + +/* + * src/event_actions/sessionization_fields_action.js + * mt-client-constraints + * + * Copyright © 2023 Apple Inc. All rights reserved. + * + */ + +var MT_SESSIONIZATION_NAMESPACE = 'mtSessionization'; +var STORAGE_KEY_SEPARATOR$1 = '_'; +var ID_TOKEN_SEPARATOR = '-'; +var SESSION_ID_KEY = 'sessionId'; +var SESSION_START_TIME_KEY = 'sessionStartTime'; +var SessionizationFieldsAction = function SessionizationFieldsAction(constraintsInstance) { + this._constraintsInstance = constraintsInstance; +}; + +/** + * attach the session related fields to the event + * @param {Object} eventData - a dictionary of event data + * @param {Object} originalEventData - the original event data + * @param {Object} sessionRules - the session rule object + * @param {String}(optional) sessionRules.storageKeyPrefix - a prefix to be used when storing ID data in localStorage, default is "mtSessionization" + * @param {String}(optional) sessionRules.namespace - a string to be used when storing session metadata in localStorage. + * @param {String}(optional) sessionRules.scopeFieldName - the field name to indicate the scope for the session metadata + * @param {Number}(optional) sessionRules.idVersion - the version of the session ID + * @param {String}(optional) sessionRules.tokenSeparator - the separator used to tokenize sections of a finalized, formatted ID string. Default is 'z' + * @param {Boolean}(optional) sessionRules.sessionStartTime - the flag to indicate whether record "sessionStartTime". Default is false + * @param {String}(optional) sessionRules.endSessionConditions - the object that contains the conditions to end the existing session + * @param {String}(optional) sessionRules.endSessionConditions.lifespan - the maximum lifespan for the session (milliseconds) + * @param {String}(optional) sessionRules.endSessionConditions.idleSpan - the maximum idle span to end the session (milliseconds) + * @param {String}(optional) sessionRules.endSessionConditions.eventCount - the maximum event count for the session + * @param {Object}(optional) sessionRules.sessionResetOptions - the reset session options to determine resetting session (deleting the session metadata from storage) + * @param {Object} sessionRules.sessionResetOptions.filters - the filters-like conditions to execute the session reset + * @param {Boolean}(optional) sessionRules.sessionResetOptions.newSessionAfterReset - the flag to indicate whether generate a new session after resetting the previous session. Default is false + * @param {Object}(optional) sessionRules.sessionFields - the mapping of the session field in the event payload + * @return {Object} return a dictionary of event data that included the session fields + */ +SessionizationFieldsAction.prototype.performAction = function performAction( + eventData, + originalEventData, + sessionRules +) { + if (!reflect.isDefinedNonNull(eventData) || !reflect.isDefined(sessionRules)) { + return eventData; + } + + if (reflect.isDefinedNonNull(sessionRules.sessionResetOptions)) { + if (!reflect.isDefinedNonNull(sessionRules.sessionResetOptions.filters)) { + throw new SyntaxError('sessionizationFields Action: unable to find the required config "filters"'); + } + var newSessionAfterReset = this._resetSession(eventData, originalEventData, sessionRules); + if (newSessionAfterReset !== true) { + return eventData; + } + } + + eventData = reflect.extend({}, eventData); + + var storageKey = this._storageKey(eventData, sessionRules); + var environment = this._constraintsInstance.system.environment; + var sessionMetadata = storage.objectFromStorage(environment.localStorageObject(), storageKey) || {}; + + if (this._shouldCreateNewSession(originalEventData, sessionMetadata, sessionRules)) { + sessionMetadata.sessionId = this._generateSessionId(sessionRules); + sessionMetadata.rawFirstEventTimeInSession = originalEventData.eventTime; + sessionMetadata.firstEventTimeInSession = eventData.eventTime; + sessionMetadata.eventCount = 0; + } + sessionMetadata.rawLastEventTimeInSession = originalEventData.eventTime; + sessionMetadata.lastEventTimeInSession = eventData.eventTime; + sessionMetadata.eventCount += 1; + + storage.saveObjectToStorage(environment.localStorageObject(), storageKey, sessionMetadata); + + var sessionFieldMap = this._getSessionFieldNames(sessionRules); + eventData[sessionFieldMap.sessionId] = sessionMetadata.sessionId; + if (sessionRules.sessionStartTime) { + eventData[sessionFieldMap.sessionStartTime] = sessionMetadata.firstEventTimeInSession; + } + + return eventData; +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} sessionRules includes information about how to namespace/scope the session data + * @return {String} the key that session data should be stored under + * @example + * (storageKeyPrefix ? storageKeyPrefix : mtSessionization)_<namespace>_(scopeFieldName ? <eventData[scopeFieldName]> : '') + */ +SessionizationFieldsAction.prototype._storageKey = function _storageKey(eventData, sessionRules) { + var scope = this._scope(eventData, sessionRules); + return this._storageKeyPrefix(sessionRules) + (!reflect.isEmptyString(scope) ? STORAGE_KEY_SEPARATOR$1 + scope : ''); +}; + +/** + * @param {Object} sessionRules includes information about how to namespace/scope the session data + * @return {String} a prefix to be used when storing session data in localStorage + */ +SessionizationFieldsAction.prototype._storageKeyPrefix = function _storageKeyPrefix(sessionRules) { + return sessionRules && reflect.isString(sessionRules.storageKeyPrefix) && sessionRules.storageKeyPrefix.length > 0 + ? sessionRules.storageKeyPrefix + : MT_SESSIONIZATION_NAMESPACE; +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} sessionRules includes information about how to namespace/scope the session data + * @return {String} the namespace/scope for this set of event data and rules + */ +SessionizationFieldsAction.prototype._scope = function _scope(eventData, sessionRules) { + var sessionScope = ''; + + if (reflect.isDefined(sessionRules)) { + if (reflect.isString(sessionRules.namespace)) { + sessionScope += sessionRules.namespace; + } + + if ( + reflect.isString(sessionRules.scopeFieldName) && + reflect.isDefinedNonNull(eventData[sessionRules.scopeFieldName]) + ) { + sessionScope += STORAGE_KEY_SEPARATOR$1; + sessionScope += eventData[sessionRules.scopeFieldName].toString(); + } + } + + return sessionScope; +}; + +/** + * generate session ID based on the provided session rules + * @param {Object} sessionRules + * @returns {String} the generated session ID + * @private + */ +SessionizationFieldsAction.prototype._generateSessionId = function _generateSessionId(sessionRules) { + return generateId({ + idVersion: 1, + time: Date.now(), + idTokenSeparator: ID_TOKEN_SEPARATOR, + generatedIdSeparator: sessionRules.tokenSeparator + }); +}; + +/** + * Decide whether create a new session based on the current session metadata and the session rules + * @param sessionMetadata + * @param sessionRules + * @returns {Boolean} + * @private + */ +SessionizationFieldsAction.prototype._shouldCreateNewSession = function _shouldCreateNewSession( + event, + sessionMetadata, + sessionRules +) { + var shouldCreateNewSession = false; + shouldCreateNewSession |= !reflect.isDefinedNonNull(sessionMetadata.sessionId); + + if (reflect.isDefinedNonNull(sessionRules.endSessionConditions)) { + if (reflect.isDefinedNonNull(sessionRules.endSessionConditions.lifespan)) { + shouldCreateNewSession |= + event.eventTime >= + sessionMetadata.rawFirstEventTimeInSession + sessionRules.endSessionConditions.lifespan; + } + + if (reflect.isDefinedNonNull(sessionRules.endSessionConditions.idleSpan)) { + shouldCreateNewSession |= + event.eventTime >= + sessionMetadata.rawLastEventTimeInSession + sessionRules.endSessionConditions.idleSpan; + } + + if (reflect.isDefinedNonNull(sessionRules.endSessionConditions.eventCount)) { + shouldCreateNewSession |= sessionMetadata.eventCount >= sessionRules.endSessionConditions.eventCount; + } + } + + return shouldCreateNewSession; +}; + +/** + * Reset the existing session based on the session rules + * @param {Object} eventData + * @param {Object} originalEventData + * @param {Object} sessionRules + * @returns {Boolean} The flag indicates whether to process the rest of logic + */ +SessionizationFieldsAction.prototype._resetSession = function _resetSession( + eventData, + originalEventData, + sessionRules +) { + var sessionResetOptions = sessionRules.sessionResetOptions; + var constraintsGenerator = this._constraintsInstance._constraintGenerator; + if ( + reflect.isDefinedNonNull(constraintsGenerator) && + reflect.isDefinedNonNull(constraintsGenerator.eventMatchesTreatment) && + constraintsGenerator.eventMatchesTreatment(originalEventData, sessionResetOptions) + ) { + var storageKey = this._storageKey(eventData, sessionRules); + var environment = this._constraintsInstance.system.environment; + storage.saveObjectToStorage(environment.localStorageObject(), storageKey, undefined); + + return sessionResetOptions.newSessionAfterReset; + } + // Always continue the sessionization logic if the resetting is not applied. + return true; +}; + +/** + * Return a map from session field names to their associated event field names + * @param {Object} sessionRoles + * @returns {Object} A map between the session field names and their associated event field names + */ +SessionizationFieldsAction.prototype._getSessionFieldNames = function _getSessionFieldNames(sessionRoles) { + var sessionFieldNames = { + sessionId: SESSION_ID_KEY, + sessionStartTime: SESSION_START_TIME_KEY + }; + if (reflect.isDefinedNonNull(sessionRoles.sessionFields)) { + if (reflect.isDefinedNonNullNonEmpty(sessionRoles.sessionFields[SESSION_ID_KEY])) { + sessionFieldNames.sessionId = sessionRoles.sessionFields[SESSION_ID_KEY]; + } + if (reflect.isDefinedNonNullNonEmpty(sessionRoles.sessionFields[SESSION_START_TIME_KEY])) { + sessionFieldNames.sessionStartTime = sessionRoles.sessionFields[SESSION_START_TIME_KEY]; + } + } + + return sessionFieldNames; +}; + +/* + * src/event_actions/index.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var ACTIONS = { + blacklistedEventAction: 'blacklisted', // DEPRECATED, use denylistedEventAction instead + denylistedEventAction: 'denylisted', + blacklistedFieldsAction: 'blacklistedFields', // DEPRECATED, use denylistedFieldsAction instead + denylistedFieldsAction: 'denylistedFields', + whitelistedFieldsAction: 'whitelistedFields', // DEPRECATED, use allowlistedFieldsAction instead + allowlistedFieldsAction: 'allowlistedFields', + sessionizationFieldsAction: 'sessionizationFields' +}; + +var EventActions = function EventActions(constraintsInstance) { + var denylistedEventAction = new DenylistedEventAction(); + var denylistedFieldsAction = new DenylistedFieldsAction(); + var allowlistedFieldsAction = new AllowlistedFieldsAction(); + var sessionizationFieldsAction = new SessionizationFieldsAction(constraintsInstance); + + // @private + this._actions = {}; + this._actions[ACTIONS.blacklistedEventAction] = denylistedEventAction; // mapping to equivalent but Inclusive Termed method + this._actions[ACTIONS.denylistedEventAction] = denylistedEventAction; + this._actions[ACTIONS.blacklistedFieldsAction] = denylistedFieldsAction; // mapping to equivalent but Inclusive Termed method + this._actions[ACTIONS.denylistedFieldsAction] = denylistedFieldsAction; + this._actions[ACTIONS.whitelistedFieldsAction] = allowlistedFieldsAction; // mapping to equivalent but Inclusive Termed method + this._actions[ACTIONS.allowlistedFieldsAction] = allowlistedFieldsAction; + this._actions[ACTIONS.sessionizationFieldsAction] = sessionizationFieldsAction; +}; + +EventActions.prototype.getAction = function getAction(actionName) { + return this._actions[actionName]; +}; + +/* + * src/field_actions/number_action.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var START_KEY = 'start'; +var VALUE_KEY = 'value'; + +/** + * Returns the index at which you should insert the object in order to maintain a sorted array + * @param {Array} array - The sorted array to inspect, must be a list without undefined/null values. + * @param {Number} value - The value to evaluate + * @returns {number} Returns the index at which `value` should be inserted, -1 if the value is less than the first item, the array or value is undefined/null + */ +var searchInsertionIndexOf = function searchInsertionIndexOf(array, value) { + var NOT_FOUND_OUTPUT = -1; + var index = NOT_FOUND_OUTPUT; + + if ( + !reflect.isDefinedNonNull(value) || + array.length === 0 || + // classify the numbers less than the lowest bucket + // -> array = [10, 20, 30], value = 9 + // <- -1 + (reflect.isDefinedNonNull(array[0]) && value < array[0][START_KEY]) + ) { + return NOT_FOUND_OUTPUT; + } + + // Using a linear search instead of binary search because the array won't be large and less error-prone + if (array[array.length - 1][START_KEY] < value) { + index = array.length - 1; + } else { + for (var i = 0; i < array.length; i++) { + var start = array[i][START_KEY]; + if (start === value) { + index = i; + break; + } else if (start > value) { + index = i - 1; + break; + } + } + } + + return index; +}; + +var NumberAction = function NumberAction() { + Base$1.apply(this, arguments); +}; + +NumberAction.prototype = Object.create(Base$1.prototype); +NumberAction.prototype.constructor = NumberAction; + +/** + * @param {Number} aNumber - a number being constrained + * @param {Object} fieldRules - includes information about how to constrain the field + * @param {Number} fieldRules.precision - must be a positive integer + * @return {Number} the constrained number + */ +NumberAction.prototype.constrainedValue = function constrainedValue(aNumber, fieldRules) { + var precision = fieldRules ? fieldRules.precision : 0; + var buckets = fieldRules ? fieldRules.buckets : null; + + if (reflect.isDefinedNonNullNonEmpty(buckets)) { + buckets = buckets.slice().sort(function (a, b) { + return a[START_KEY] - b[START_KEY]; + }); + + var bucketIndex = searchInsertionIndexOf(buckets, aNumber); + var bucket = buckets[bucketIndex]; + + if (reflect.isDefinedNonNull(bucket)) { + aNumber = bucket[VALUE_KEY]; + } + } else if (reflect.isNumber(aNumber) && reflect.isNumber(precision) && precision > 0) { + aNumber = Math.floor(aNumber / precision) * precision; + } + + return aNumber; +}; + +/* + * src/utils/serial_number_generator.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var DEFAULT_NAMESPACE = 'mt_serial_number'; +var EXPIRATION_STORAGE_KEY = 'exp'; +var SERIAL_NUMBER_STORAGE_KEY = 'sn'; + +/** + * + * @param {Object} options - An object that contains parameters for generating serial numbers + * @param {String} options.namespace (optional) - A key that is used to store the serial numbers in the Storage. Default to "mt_serial_number" + * @param {Number} options.initialSerialNumber (optional) - An initialization serial number. Default to 0 + * @param {Number} options.nextRotationTime (optional) - A timestamp that indicates when should reset the serial number. Default to Number.POSITIVE_INFINITY(never rotate) + * @param {Number} options.rotationPeriod (optional) - A millisecond to indicate how long the serial number could be alive. Default to Number.POSITIVE_INFINITY(never rotate) + * @constructor + */ +var SerialNumberGenerator = function SerialNumberGenerator(options) { + options = options || {}; + // @private + this._nextRotationTime = options.nextRotationTime || Number.POSITIVE_INFINITY; + // @private + this._storageKey = options.namespace || DEFAULT_NAMESPACE; + // @private + this._initialSerialNumber = options.initialSerialNumber || 0; + // @private + this._rotationPeriod = options.rotationPeriod || Number.POSITIVE_INFINITY; +}; + +SerialNumberGenerator.prototype.setDelegate = function setDelegate(delegate) { + reflect.attachDelegate(this, delegate); +}; + +SerialNumberGenerator.prototype.localStorageObject = function localStorageObject() { + return storage.localStorageObject(); +}; + +/** + * Return the increased serial number + * @param {Number} increment (optional) - The amount to increment. Defaults to 1 + * @returns {Number} the increased serial number + */ +SerialNumberGenerator.prototype.getNextSerialNumber = function getNextSerialNumber(increment) { + var storageKey = this._storageKey; + var serialNumberData = this._getCurrentSerialNumberData(storageKey); + + var serialNum = serialNumberData[SERIAL_NUMBER_STORAGE_KEY]; + increment = reflect.isNumber(increment) ? increment : 1; + serialNum = parseInt(serialNum, 10); + + if (isNaN(serialNum)) { + // Reset the serial number to the initialized one, to ensure the logic won't break if the sequence number is an invalid number. + serialNum = this._initialSerialNumber; + } + serialNum = this._increaseSerialNumber(serialNum, increment); + // Store the increased serial number to storage + serialNumberData[SERIAL_NUMBER_STORAGE_KEY] = serialNum; + storage.saveObjectToStorage(this.localStorageObject(), this._storageKey, serialNumberData); + + return serialNum; +}; + +/** + * Reset the serial number + */ +SerialNumberGenerator.prototype.resetSerialNumber = function resetSerialNumber() { + var serialNumberData = storage.objectFromStorage(this.localStorageObject(), this._storageKey); + + if (reflect.isDefinedNonNull(serialNumberData)) { + this._resetSerialNumber(serialNumberData[EXPIRATION_STORAGE_KEY]); + } +}; + +/** + * Delegable method to return the time for calculating rotation + * @returns {number} + */ +SerialNumberGenerator.prototype.getTime = function getTime() { + return Date.now(); +}; + +/** + * Increasing the giving serial number by plus one + * @param {Number} serialNum + * @returns {number} + * @private + */ +SerialNumberGenerator.prototype._increaseSerialNumber = function _increaseSerialNumber(serialNum, increment) { + return serialNum + increment; +}; + +/** + * Rotate and return the serial number data + * @param {String} storageKey + * @returns {Object} rotated serial number data + */ +SerialNumberGenerator.prototype._getCurrentSerialNumberData = function _getCurrentSerialNumberData(storageKey) { + var serialNumberData = storage.objectFromStorage(this.localStorageObject(), storageKey); + var rotationTime; + var nextRotationTime; + if (serialNumberData) { + rotationTime = serialNumberData[EXPIRATION_STORAGE_KEY]; + rotationTime = parseInt(rotationTime, 10); + serialNumberData[EXPIRATION_STORAGE_KEY] = rotationTime = isNaN(rotationTime) + ? this._nextRotationTime + : rotationTime; + } else { + // use the "nextRotationTime - rotationPeriod" as the rotationTime if the serialNumberData is not existing in the storage, to check if need to reset serial number + rotationTime = this._nextRotationTime - this._rotationPeriod; + } + + // Reset the serial number data if it has expired or never initialized + // Checking "!serialNumberData" in here to cover the case of when both of this._nextRotationTime and this._rotationPeriod are not provided, "this._nextRotationTime(Infinite) - this._rotationPeriod(Infinite) = NaN" which is always less than "this.getTime()" + // Use while loop here to catch up to the latest rotation time. + while (!serialNumberData || this.getTime() >= rotationTime) { + rotationTime = nextRotationTime = rotationTime + this._rotationPeriod; + serialNumberData = this._resetSerialNumber(nextRotationTime); + } + return serialNumberData; +}; + +/** + * Reset the serial number and expiration + * @param {Number} nextRotationTime - A timestamp that indicates when should reset the serial number + * @returns reset serialNumberData + * { + * exp: nextRotationTime, + * sn: serialNumber + * } + */ +SerialNumberGenerator.prototype._resetSerialNumber = function _resetSerialNumber(nextRotationTime) { + var serialNumberData = {}; + serialNumberData[EXPIRATION_STORAGE_KEY] = nextRotationTime; + serialNumberData[SERIAL_NUMBER_STORAGE_KEY] = this._initialSerialNumber; + storage.saveObjectToStorage(this.localStorageObject(), this._storageKey, serialNumberData); + return serialNumberData; +}; + +/* + * src/field_actions/time_action.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var STORAGE_KEY_SEPARATOR$2 = '_'; +var STORAGE_PREFIX_DEFAULT = 'mtTimestamp'; + +var TimeAction = function TimeAction() { + Base$1.apply(this, arguments); + // @private + this._storage = this._constraintsInstance.system.environment.localStorageObject(); + // @private + /* + * Store the end time of the giving time precision base on namespace + time fields + */ + this._precisionEndTimeCache = {}; + // @private + this._serialNumberGenerator = null; +}; + +TimeAction.prototype = Object.create(Base$1.prototype); +TimeAction.prototype.constructor = TimeAction; + +/** + * @param {Number} time - a timestamp being constrained + * @param {Object} fieldRules - includes information about how to constrain the field + * @param {Object} fieldRules.precision - The time must be a positive integer + * @param {String} fieldRules.storageKeyPrefix - a prefix to be used when storing timestamp de-res related data, default is "mt-timestamp" + * @param {String} fieldRules.namespace - a namespace for the timestamp de-res related data, default is empty. + * @param {Object} eventData - a dictionary of event data, which should include a pre-existing (unconstrained) field + * @param {String} fieldName - name of the field being modified in eventData + * @return {Number} the constrained time or the original value if time is not defined or fieldRules is unavailable + */ +TimeAction.prototype.constrainedValue = function constrainedValue(time, fieldRules, eventData, fieldName) { + var returnTimestamp = time; + if ( + reflect.isNumber(time) && + reflect.isObject(fieldRules) && + reflect.isNumber(fieldRules.precision) && + fieldRules.precision > 0 + ) { + var precisionStartTime = this._computePrecisionStartTime(time, fieldRules); + this._serialNumberGenerator = new SerialNumberGenerator({ + namespace: this._persistentStorageKey(fieldRules, fieldName), + nextRotationTime: precisionStartTime + fieldRules.precision, + rotationPeriod: fieldRules.precision + }); + this._serialNumberGenerator.setDelegate(this._constraintsInstance.system.environment); + this._serialNumberGenerator.setDelegate({ + getTime: function () { + return time; + } + }); + var serialNumber = this._serialNumberGenerator.getNextSerialNumber(); + returnTimestamp = this._computeTimestamp(precisionStartTime, serialNumber); + this._serialNumberGenerator = null; // Release the serial number generator. + } + + return returnTimestamp; +}; + +TimeAction.prototype._computeTimestamp = function _computeTimestamp(precisionStartTime, sequenceNum) { + return precisionStartTime + sequenceNum; +}; + +TimeAction.prototype._persistentStorageKey = function _persistentStorageKey(fieldRules, fieldName) { + var namespaceSegment = fieldRules.namespace ? STORAGE_KEY_SEPARATOR$2 + fieldRules.namespace : ''; + return ( + (fieldRules.storageKeyPrefix || STORAGE_PREFIX_DEFAULT) + namespaceSegment + STORAGE_KEY_SEPARATOR$2 + fieldName + ); +}; + +TimeAction.prototype._computePrecisionStartTime = function _computePrecisionStartTime(time, fieldRules) { + var precision = fieldRules.precision; + return Math.floor(time / precision) * precision; +}; + +/* + * src/field_actions/hash_action.js + * mt-client-constraints + * + * Copyright © 2022 Apple Inc. All rights reserved. + * + */ + +var STORAGE_KEY_SEPARATOR$3 = '_'; +var STORAGE_PREFIX_DEFAULT$1 = 'mtHash'; +var STORAGE_SALT_KEY = 'salt'; +var SALT_CHAR_LENGTH = 10; + +var HashAction = function HashAction() { + Base$1.apply(this, arguments); +}; +HashAction.prototype = Object.create(Base$1.prototype); +HashAction.prototype.constructor = HashAction; + +/** + * Build the storage key for salt data + * key format: <prefix|mtHash>_<namespace?>_salt_<fieldName> + * @param fieldRules + * @param fieldName + * @returns {string} + */ +function buildSaltStorageKey(fieldRules, fieldName) { + var namespaceSegment = fieldRules.namespace ? STORAGE_KEY_SEPARATOR$3 + fieldRules.namespace : ''; + return ( + (fieldRules.storageKeyPrefix || STORAGE_PREFIX_DEFAULT$1) + + namespaceSegment + + STORAGE_KEY_SEPARATOR$3 + + STORAGE_SALT_KEY + + STORAGE_KEY_SEPARATOR$3 + + fieldName + ); +} + +function generateSalt() { + var salt = ''; + while (salt.length < SALT_CHAR_LENGTH) { + salt += string.randomHexCharacter(); + } + return salt; +} + +// The hash logic is borrowed from String.hashcode() of Java +function hashCode(value, salt) { + return [value, salt] + .map(function (segment) { + var hash = 0; + // undefined, null and '' will return 0 as the hash code. + if (reflect.isDefinedNonNullNonEmpty(segment)) { + for (var i = 0; i < segment.length; i++) { + var charCode = segment.charCodeAt(i); + hash = (hash << 5) - hash + charCode; // "(hash << 5) - hash" is similar to "hash * 31" but faster. + } + } + var hashedValue = Math.abs(hash); + hashedValue = parseInt(hashedValue, 16); + return string.convertNumberToBaseAlphabet(hashedValue, string.base62Alphabet); + }) + .join(''); +} + +/** + * + * @param {String} value The value that will be hashed + * @param {Object} fieldRules - includes information about how to constrain the field + * @param {String} fieldRules.storageKeyPrefix - a prefix to be used when storing hash related data, default is "mtHash" + * @param {String} fieldRules.namespace - a namespace for storing the hash data, default is empty. + * @param {Number} fieldRules.saltLifespan - a lifespan (milliseconds) of the salt + * @param {Object} fieldRules.platformBasedSalt - a config section includes the configures for loading salt from platform API + * @param {String} fieldRules.platformBasedSalt.saltNamespace - a namespace that stores the salt configuration in the "metricsIdentifier" section of the bag + * @param {String} fieldRules.platformBasedSalt.crossDeviceSync + * @param {Object} eventData - a dictionary of event data, which should include a pre-existing (unconstrained) field + * @param {String} fieldName - name of the field being modified in eventData + * @return {String | Promise<string>} The hashed value on the top of provided value with the stored salt (rotated for every <fieldRules.saltLifespan> milliseconds). + */ +HashAction.prototype.constrainedValue = function constrainedValue(value, fieldRules, _eventData, fieldName) { + if (reflect.isDefinedNonNullNonEmpty(value)) { + return this._loadPlatformBasedSalt(fieldRules, fieldName).then(function (salt) { + return hashCode(value, salt); + }); + } + return value; +}; + +/** + * @param {Number} timestamp a timestamp in ms since epoch + * @return {Boolean} return false if timestamp does not exist + * @overridable + */ +HashAction.prototype.timeExpired = function timeExpired(timestamp) { + return timestamp ? timestamp <= Date.now() : false; +}; + +/** + * @param {Number} (optional) lifespan the amount of time, in milliseconds, that an ID should be valid for + * @return {Number} a timestamp in ms since epoch, or null if no lifespan was provided + * @overridable + */ +HashAction.prototype.expirationTime = function expirationTime(lifespan) { + return lifespan ? Date.now() + lifespan : null; +}; + +HashAction.prototype._loadPlatformBasedSalt = function _loadPlatformBasedSalt(fieldRules, fieldName) { + var saltPromise = null; + var self = this; + var platformBasedSaltConfig = fieldRules.platformBasedSalt; + if (reflect.isDefinedNonNull(platformBasedSaltConfig)) { + saltPromise = this._constraintsInstance.system.environment.platformIdentifier( + platformBasedSaltConfig.saltNamespace, + 'userid', + platformBasedSaltConfig.crossDeviceSync || true + ); + if (reflect.isDefinedNonNull(saltPromise)) { + saltPromise = saltPromise.then(function (salt) { + if (!reflect.isDefinedNonNull(salt)) { + self._constraintsInstance.system.logger.warn( + 'Hash: platform returned an empty salt. Will use default salt generator to generate the salt.' + ); + salt = self._getSalt(fieldRules, fieldName); + } + return salt; + }); + } else { + saltPromise = Promise.resolve(this._getSalt(fieldRules, fieldName)); + } + } else { + saltPromise = Promise.resolve(this._getSalt(fieldRules, fieldName)); + } + + return saltPromise; +}; + +// This method retrieves the salt from storage, otherwise generates a new salt if it doesn't exist or has expired +HashAction.prototype._getSalt = function _getSalt(fieldRules, fieldName) { + var saltMetadata = this._retrieveSaltFromStorage(fieldRules, fieldName); + var saltLifespan = fieldRules.saltLifespan; + if (!reflect.isDefinedNonNull(saltMetadata) || this.timeExpired(saltMetadata.expirationTime)) { + var localStorage = this._constraintsInstance.system.environment.localStorageObject(); + var salt = generateSalt(); + saltMetadata = { + salt: salt, + expirationTime: this.expirationTime(saltLifespan) + }; + storage.saveObjectToStorage(localStorage, buildSaltStorageKey(fieldRules, fieldName), saltMetadata); + } + + return saltMetadata.salt; +}; + +HashAction.prototype._retrieveSaltFromStorage = function _retrieveSaltFromStorage(fieldRules, fieldName) { + var localStorage = this._constraintsInstance.system.environment.localStorageObject(); + var saltMetadata = storage.objectFromStorage(localStorage, buildSaltStorageKey(fieldRules, fieldName)); + return saltMetadata; +}; + +/* + * src/field_actions/index.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var ACTIONS$1 = { + ID: 'idGenerator', + NUMBER: 'numberDeres', + TIME: 'timeDeres', + URL: 'urlDeres', + HASH: 'hash' +}; + +var FieldActions = function DeresHandlers(constraintsInstance) { + this.actions = {}; + this.actions[ACTIONS$1.ID] = new IdAction(constraintsInstance); + this.actions[ACTIONS$1.NUMBER] = new NumberAction(constraintsInstance); + this.actions[ACTIONS$1.TIME] = new TimeAction(constraintsInstance); + this.actions[ACTIONS$1.URL] = new UrlAction(constraintsInstance); + this.actions[ACTIONS$1.HASH] = new HashAction(constraintsInstance); +}; + +FieldActions.prototype.getAction = function getAction(actionName) { + return this.actions[actionName]; +}; + +/* + * src/treatment/action_treatment.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +var ActionTreatment = function ActionTreatment(constraintInstance) { + // @private + this._eventActions = new EventActions(constraintInstance); + + // @private + this._fieldActions = new FieldActions(constraintInstance); +}; + +/** + * @param {Object} eventData a dictionary of event data + * @param {Object} eventConstraints a set of constraints to apply to this event + * @return {Object | Promise} constraints the event data modified according to the appropriate constraints or "null" if the event is blacklisted + * Note: + * 1. create a new dictionary if the event data is constrained/modified + * 2. return the original eventData if the constraints parameter is null or an empty dictionary + * @example + * var eventData = { + * eventType: 'click', + * pageType: 'TopCharts', + * parentPageUrl: 'https://itunes.apple.com/music/topcharts/12345', + * // etc. + * }; + * var eventConstraints = { + * eventActions: { blacklistedFields: ['cookies', 'pageDetails'] }, + * fieldActions: { + * parentPageUrl: { + * treatmentType: 'urlDeres', + * scope: 'hostname' + * } + * } + * } + * constraints.eventFields.applyEventConstraints(eventData, eventConstraints) => + * { + * eventType: click, + * pageType: 'TopCharts', + * parentPageUrl: 'itunes.apple.com', // truncated to hostname only + * etc... // all other fields remain the same, except "cookies", "pageDetails" + * } + */ +ActionTreatment.prototype.applyConstraints = function applyConstraints(eventData, constraints) { + var returnEventData = eventData; // Set the original eventData to the returning variable to return the original eventData if neither event actions nor field actions were applied. + + if (constraints && !reflect.isEmptyObject(constraints)) { + var promiseTasks = []; + var self = this; + + if (constraints.fieldActions && !reflect.isEmptyObject(constraints.fieldActions)) { + var isAnyFieldChanged = false; + var eventDataCopy = returnEventData; + eventDataCopy = Object.keys(constraints.fieldActions).reduce(function (accumulatedFields, fieldName) { + var fieldRules = constraints.fieldActions[fieldName]; + if (fieldRules) { + var denylisted = fieldRules.denylisted || fieldRules.blacklisted; + var fieldAction = fieldRules.treatmentType; + var fieldActionHandler = self._fieldActions.getAction(fieldAction); + + accumulatedFields = lookForKeyPath( + accumulatedFields, + fieldName, + false, + function (value, key, keyPath, object) { + if (denylisted) { + delete object[key]; + isAnyFieldChanged = true; + } else if (fieldRules.hasOwnProperty(FIELD_RULES.OVERRIDE_FIELD_VALUE)) { + object[key] = fieldRules[FIELD_RULES.OVERRIDE_FIELD_VALUE]; + isAnyFieldChanged = true; + } else if (fieldActionHandler) { + var returnedValue = fieldActionHandler.performAction( + value, + fieldName, + returnEventData, + fieldRules + ); + object[key] = returnedValue; + if (returnedValue instanceof Promise) { + promiseTasks.push( + returnedValue.then(function (processedValue) { + lookForKeyPath( + eventDataCopy, + fieldName, + true, + function (_value, targetKey, targetKeyPath, targetObject) { + if (targetKeyPath === keyPath) { + targetObject[targetKey] = processedValue; + } + } + ); + }) + ); + } + isAnyFieldChanged = true; + } + } + ); + } + return accumulatedFields; + }, eventDataCopy); + + // If any field has been constrained, we create a new object to contain the merged fields instead of merging the changes to the original eventData. + // eventDataCopy has been re-built by "lookForKeyPath" above. + if (isAnyFieldChanged) { + if (promiseTasks.length > 0) { + returnEventData = Promise.all(promiseTasks).then(function () { + return eventDataCopy; + }); + } else { + returnEventData = eventDataCopy; + } + } + } + + // perform event actions after the field actions to ensure removing denied fields or keeping allowed fields for those generated fields(e.g. IdGenerator). + if (constraints.eventActions && !reflect.isEmptyObject(constraints.eventActions)) { + var eventActionNames = Object.keys(constraints.eventActions); + var processEventActions = function (processingEventData) { + eventActionNames.forEach(function (eventAction) { + var eventActionHandler = self._eventActions.getAction(eventAction); + if (eventActionHandler) { + var actionRules = constraints.eventActions[eventAction]; + processingEventData = eventActionHandler.performAction( + processingEventData, + eventData, + actionRules + ); + } + }); + return processingEventData; + }; + + if (returnEventData instanceof Promise) { + returnEventData = Promise.resolve(returnEventData).then(function (processedEventData) { + return processEventActions(processedEventData); + }); + } else { + returnEventData = processEventActions(returnEventData); + } + } + } + + return returnEventData; +}; + +/* + * src/constraint_generator/treatment_generator.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +var TREATMENT_FILTERS_FIELD = 'filters'; +var TREATMENT_FILTERS_ALL = 'any'; +var TREATMENT_EVENT_ACTIONS = 'eventActions'; +var TREATMENT_FIELD_ACTIONS = 'fieldActions'; + +function _updateTreatment(accumulatedTreatment, treatment) { + var currentTreatment = accumulatedTreatment || {}; + // update event actions + _updateEventActions(currentTreatment, treatment); + // update field actions + _updateFieldActions(currentTreatment, treatment); + + return currentTreatment; +} + +function _updateEventActions(targetTreatment, sourceTreatment) { + if (!targetTreatment[TREATMENT_EVENT_ACTIONS]) { + targetTreatment[TREATMENT_EVENT_ACTIONS] = {}; + } + var currentTreatmentEventActions = targetTreatment[TREATMENT_EVENT_ACTIONS]; + var treatmentEventActions = sourceTreatment[TREATMENT_EVENT_ACTIONS]; + + if (treatmentEventActions) { + Object.keys(treatmentEventActions).reduce(function (accumulatedEventActions, eventAction) { + var existingActionValue = accumulatedEventActions[eventAction]; + var actionValue = treatmentEventActions[eventAction]; + // Merge the event action values from different treatments + if (reflect.isArray(existingActionValue)) { + // If the action value is not an array, treat it as a bad data and discard it. + if (reflect.isArray(actionValue)) { + actionValue.forEach(function (value) { + if (existingActionValue.indexOf(value) === -1) { + existingActionValue.push(value); + } + }); + } + } else { + // Currently only have array type and primitive type parameters. Ignore the other types of parameter values. + if (reflect.isArray(actionValue)) { + // Clone the array value, to avoid the original array is changed by other treatments. + accumulatedEventActions[eventAction] = actionValue.slice(); + } else if ( + reflect.isObject(actionValue) || + (!reflect.isObject(actionValue) && !reflect.isFunction(actionValue)) + ) { + // object, primitive type, null and undefined + // set the existing action value with the primitive type value. + accumulatedEventActions[eventAction] = actionValue; + } + } + + return accumulatedEventActions; + }, currentTreatmentEventActions); + } +} + +function _updateFieldActions(targetTreatment, sourceTreatment) { + if (!targetTreatment[TREATMENT_FIELD_ACTIONS]) { + targetTreatment[TREATMENT_FIELD_ACTIONS] = {}; + } + updateFieldRulesets( + targetTreatment, + sourceTreatment, + TREATMENT_FIELD_ACTIONS, + function (targetRules, sourceRules, fieldName) { + // if the target field rule has the same treatmentType as the source field rule, then return the target field rule to replace its rule props with the source ones. + // otherwise, all of the source field rules will overwrite all of the target field rules + if ( + targetRules[fieldName] && + targetRules[fieldName].treatmentType === sourceRules[fieldName].treatmentType + ) { + return targetRules[fieldName]; + } else { + // if the treatmentType is not the same between field rules, return an empty object to take the latter field rules + /* + { + treatments: [{ + ..., + fieldActions: { + afield: { treatmentType: 'a', propA: 123 } + } + }, { + ..., + fieldActions: { + afield: { treatmentType: 'b', propB: 123 } + } + }] + } + + expected output: + { + treatments: [{ + ..., + fieldActions: { + afield: { treatmentType: 'b', propB: 123 } + } + }] + } + */ + return {}; + } + } + ); +} + +var TreatmentGenerator = function TreatmentGenerator(constraintsInstance) { + // @private + this._constraintsInstance = constraintsInstance; + this.treatment = new ActionTreatment(constraintsInstance); +}; + +/** + * Combine treatments from multiple profiles + * @param {Array} ConstraintProfiles the constraint profile names + * @param {Object} topicConfig An AMP Metrics Config + * @param {String}(optional) topic defines the AMP Analytics "topic" to look up the constraint profile + * @returns {Promise} a Promise that returns an Array of combined treatments from multiple constraint profiles + * @private + */ +TreatmentGenerator.prototype._combineTreatments = function _combineTreatments(constraintProfiles, topicConfig, topic) { + var combinedTreatmentsPromise; + var buildTreatmentTasks = []; + + if (reflect.isArray(constraintProfiles)) { + constraintProfiles.forEach(function (constraintProfile) { + if (!constraintProfile) { + return; + } + + var profileName = 'treatmentProfiles.' + constraintProfile; + var constraintsPromise = topicConfig.value(profileName, topic).then(function (constraints) { + return constraints && constraints.treatments ? constraints.treatments : []; + }); + buildTreatmentTasks.push(constraintsPromise); + }); + + combinedTreatmentsPromise = Promise.all(buildTreatmentTasks).then(function (profilesTreatments) { + var combinedTreatments = []; + profilesTreatments.forEach(function (profileTreatments) { + combinedTreatments = combinedTreatments.concat(profileTreatments); + }); + + return combinedTreatments; + }); + } else { + combinedTreatmentsPromise = Promise.resolve([]); + } + + return combinedTreatmentsPromise; +}; + +/** + * Build the properly constraints for the passed-in event data + * @param {Object} eventData a dictionary of event data + * @param {Object} topicConfig An AMP Metrics Config + * @param {String}(optional) topic defines the AMP Analytics "topic" to look up the constraint profile + * @return {Object} a set of constraints to apply to this event. + * returns null (MetricsKit will send the original event) if: + * 1. no topic config available + * 2. defaultTreatmentProfile is undefined + * 3. the profile is found but no treatment matched + * @throws {TypeError} throws a type error if the topic config contains any invalid element. + * @throws {SyntaxError} throws a syntax error if: + * 1. topicConfig.constraintProfiles(topic) is not found in the topic config + * 2. the treatment configuration is invalid + * @overridable + * Constraint rules will be applied in the order they are provided in config. + * @example + * Given the following config: + * metrics: { + * ... + * low_res_topic: { + * defaultTreatmentProfiles: ['iosStores', 'embeddedWeb'], + * }, + * defaultTreatmentProfiles: ['embeddedWeb'] + * treatmentProfiles: { + * iosStores: { version: 1, treatments: [ ... ] }, + * embeddedWeb: { + * version: 2, + * treatments: [ + * { + * filters: { + * eventType: { valueMatches: ['enter', 'exit' ] }, + * isSignedIn: { valueMatches: [true] } + * }, + * eventActions: { + * blacklistedFields: ['cookies'] + * } + * }, + * { + * filters: { + * eventType: { valueMatches: [ 'exit' ] } + * }, + * eventActions: { + * blacklistedFields: ['cookies', 'pageDetails'] + * }, + * fieldActions: { + * clientId: { + * treatmentType: 'idDeres', + * storageKeyPrefix: 'mtClientId', + * namespace: 'test', + * scopeStrategy: 'mainDomain', + * scopeFieldName: 'https://www.apple.com/products/', + * tokenSeparator: 'z', + * lifespan: 86400000 + * } + * } + * }, + * { + * filters: { + * userType: { valueMatches: ['signedIn'] }, + * actionType: { valueMatches: ['navigate'] } + * }, + * fieldActions: { + * os: { blacklisted: true }, + * // round down time to 1 day + * eventTime: { + * treatmentType: "numberDeres", + * precision: 86400000 + * }, + * // remove query params + * pageUrl: { + * treatmentType: "urlDeres", + * scope: 'fullWithoutParams' + * }, + * // Deres disk available space round down to MB + * capacityDiskAvailable: { + * treatmentType: "numberDeres", + * precision: 1000000, // 1MB + * }, + * clientId: { + * treatmentType: 'idDeres', + * scopeFieldName: 'https://www.apple.com/', + * lifespan: 3600000 + * } + * } + * } + * ] + * } + * }, + * ... + * } + * + * constraintsForEvent({ eventType: 'exit', isSignedIn: true, userType: 'signedIn', actionType: 'navigate', ... }, topicConfig) returns: + * { + * eventActions: { + * blacklistedFields: ['cookies', 'pageDetails'] // from "eventType: { valueMatches: [ 'exit' ] }", override the one of "treatments[0]" + * }, + * fieldActions: { + * os: { blacklisted: true }, + * eventTime: { + * treatmentType: "numberDeres", + * precision: 86400000 + * }, + * pageUrl: { + * treatmentType: "urlDeres", + * scope: 'fullWithoutParams' + * }, + * capacityDiskAvailable: { + * treatmentType: "numberDeres", + * precision: 1000000, // 1MB + * }, + * clientId: { + * treatmentType: 'idDeres', + * storageKeyPrefix: 'mtClientId', + * namespace: 'test', + * scopeStrategy: 'mainDomain', + * scopeFieldName: 'https://www.apple.com/', // override the value from "treatments[1].clientId" + * tokenSeparator: 'z', + * lifespan: 3600000 // override the value from "treatments[1].clientId" + * } + * } + * } + */ +TreatmentGenerator.prototype.constraintsForEvent = function constraintsForEvent(eventData, topicConfig, topic) { + if (!topicConfig) { + return Promise.resolve(null); + } + var self = this; + + // Use Promise.resolve to wrap the constraintProfiles() here in case of the client delegate the constraintProfiles method and returns non-promise value + return Promise.resolve(topicConfig.constraintProfiles(topic)) + .then(function (constraintProfiles) { + // Adapt the v1 profile to v2 profiles + if (!reflect.isDefinedNonNull(constraintProfiles)) { + return Promise.resolve(topicConfig.constraintProfile(topic)).then(function (constraintProfile) { + return reflect.isDefinedNonNull(constraintProfile) ? [constraintProfile] : null; + }); + } else { + return constraintProfiles; + } + }) + .then(function (constraintProfiles) { + // rdar://71993234 if there is no default treatment profile and the client did not declare a treatment profile, do not modify the event + if (reflect.isDefinedNonNull(constraintProfiles)) { + if (!reflect.isArray(constraintProfiles)) { + throw new TypeError( + '"constraintProfiles" should be an Array, but got: ' + + (constraintProfiles ? constraintProfiles.constructor : constraintProfiles) + ); + } + return self + ._combineTreatments(constraintProfiles, topicConfig, topic) + .then(function (combinedTreatments) { + // rdar://71993234 if the treatments are not found in the topic config + if (combinedTreatments.length === 0) { + throw new SyntaxError( + 'The constraintProfiles: ' + + constraintProfiles.join(', ') + + ' are not found in the topic config' + ); + } + return combinedTreatments; + }); + } else { + return Promise.resolve([]); + } + }) + .then(function (combinedTreatments) { + var returnTreatments = combinedTreatments.reduce(function (accumulatedTreatment, treatment) { + if (self.eventMatchesTreatment(eventData, treatment)) { + accumulatedTreatment = _updateTreatment(accumulatedTreatment, treatment); + } + return accumulatedTreatment; + }, null); + + return returnTreatments; + }); +}; + +TreatmentGenerator.prototype.eventMatchesTreatment = function eventMatchesTreatment(eventData, treatment) { + var filters = treatment[TREATMENT_FILTERS_FIELD]; + + // Fast false for free-form filter since JS does not support it at the moment + // Applying the treatment to all events if there is no filters to align the behavior with the native implementation. + if (!reflect.isDefinedNonNull(filters)) { + return true; + } + // Applying the treatment to all events if the value equals to "any" + if (reflect.isString(filters)) { + return filters === TREATMENT_FILTERS_ALL; + } + + // If the filter element is an empty filter list. We consider it is an incorrect config. + if (Object.keys(filters).length === 0) { + throw new SyntaxError('Unable to find the filter in \n' + JSON.stringify(treatment)); + } + + return Object.keys(filters).every(function (filterField) { + var fieldFilters = filters[filterField]; + + // Fast false for free-form filter since JS does not support it at the moment + if (fieldFilters && reflect.isString(fieldFilters)) { + return false; + } + // if a field isn't an object or doesn't have any matchers. We consider this is a bad filter and discard the event + if (!fieldFilters || !reflect.isObject(fieldFilters) || reflect.isEmptyObject(fieldFilters)) { + throw new SyntaxError( + 'Invalid filter object for field (' + filterField + ') in \n' + JSON.stringify(treatment) + ); + } + // Only return the treatments where all treatments match. + // Current, only one condition for one field. + return Object.keys(fieldFilters).every(function (matcherName) { + var matcherParam = fieldFilters[matcherName]; + + if (matchers[matcherName]) { + return matchers[matcherName](filterField, eventData, matcherParam); + } else { + throw new SyntaxError( + 'Unable to find the filter (' + + matcherName + + ') for field (' + + filterField + + ')in \n' + + JSON.stringify(treatment) + ); + } + }); + }); +}; + +/* + * src/config.js + * mt-client-constraints + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +/** + * The constraints config delegate + * Constraints attach this delegate to the topic config to have constraints features on the topic config + */ +var constraintsConfig = { + /** + * Return the constraint profile from a config with constraint syntax v1 + * @param {String}(optional) topic defines the AMP Analytics "topic" that this event should be stored under + * @return {Promise} a Promise that returns the name of the constraint profile from constraint syntax v1 to use + */ + constraintProfile: function constraintProfile(topic) { + return this.value('constraints.defaultProfile', topic); + }, + + /** + * Return the constraint profiles from a config with constraint syntax v2 + * @param {String}(optional) topic defines the AMP Analytics "topic" that this event should be stored under + * @return {Promise} a Promise that returns an array of the names of the constraint profile to use + */ + constraintProfiles: function constraintProfiles(topic) { + return this.value('defaultTreatmentProfiles', topic); + } +}; + +/* + * src/index.js + * mt-client-constraints + * + * Copyright © 2017-2018 Apple Inc. All rights reserved. + * + */ + +function _validateConfig(config) { + var isValid = true; + + isValid &= reflect.isDefinedNonNull(config); + if (isValid) { + isValid &= !reflect.isEmptyObject(config); + isValid &= reflect.isFunction(config.initialized); + isValid &= reflect.isFunction(config.value); + isValid &= reflect.isFunction(config.constraintProfile); + } + + return isValid; +} + +/** + * Attaching config related methods for Constraints + * @param {Config} topicConfig An AMP Metrics Config + * @returns {Config} the passed-in config with constraint-related methods attached + */ +function connectConstraintConfig(topicConfig) { + // return the topic config if it has already been attached with the Constraint methods. + if (reflect.isFunction(topicConfig.constraintProfile) && reflect.isFunction(topicConfig.constraintProfiles)) { + return topicConfig; + } + reflect.attachMethods(topicConfig, constraintsConfig, topicConfig); + + return topicConfig; +} + +/** + * Supplies the single JavaScript entrypoint to constraint functionality + * Since JavaScript is prototype-based and not class-based, and doesn't provide + * an "official" object model, this API is presented as a functional API, but + * still retains the ability to override and customize functionality via the + * "setDelegate()" method. In this way, it doesn't carry with it the spare + * baggage of exposing a bolt-on object model which may differ from a bolt-on + * (or homegrown) object model already existing in the app. + * @module + * @param {Object} topicConfig a topic config + * @param {Object} delegates + * @constructor + * + * @example + * import * as delegates from '@amp-metrics/mt-metricskit-delegates-html'; + * import Constraints, { connectConstraintConfig } from '@amp-metrics/mt-client-constraints'; + * import Config from '@amp-metrics/mt-client-config'; + * + * const topicConfig = new Config('topic'); + * connectConstraintConfig(topicConfig); + * + * var eventData = {...}; + * var constraints = new Constraints(topicConfig, delegates); + * var constrainedEventData = constraints.applyConstraintTreatments(eventData); + */ +var Constraints = function Constraints(topicConfig, delegate) { + if (!_validateConfig(topicConfig)) { + throw new Error('The topic config is not a valid instance of "mt-client-config".'); + } + + // @private + this._isInitialized = false; + + // @private + this._topicConfig = topicConfig; + + /** + * constraint generator for specific topic config + * @type {ConstraintGenerator} + */ + // @private + this._constraintGenerator = null; + + /** + * system/platform-specific classes + */ + this.system = new System(); + + reflect.setDelegates(this.system, delegate || {}); +}; + +/** + * get constraint generator based on the Constraints' config + * @returns {Promise} a Promise that returns the active constraint generator + */ +Constraints.prototype._getConstraintGenerator = function _getConstraintGenerator() { + var self = this; + + if (this._constraintGenerator) { + return Promise.resolve(this._constraintGenerator); + } else { + return this._topicConfig.value('treatmentProfiles').then(function (treatmentConfig) { + if (reflect.isDefinedNonNull(treatmentConfig)) { + self._constraintGenerator = new TreatmentGenerator(self); + } else { + self._constraintGenerator = new LegacyConstraintGenerator(self); + } + return self._constraintGenerator; + }); + } +}; + +/** + * Build constraints with the eventData + * @param {Object} eventData a dictionary of event data + * @param {String}(optional) topic defines the AMP Analytics "topic" that this event should be stored under + * @return {Promise} a Promise that returns a set of constraints to apply to this event + * @throws {SyntaxError/TypeError} throws a type error if the topic config contains any invalid element. + */ +Constraints.prototype.constraintsForEvent = function constraintsForEvent(eventData, topic) { + var self = this; + return this._getConstraintGenerator().then(function (constraintGenerator) { + return constraintGenerator.constraintsForEvent(eventData, self._topicConfig, topic); + }); +}; + +/** + * Apply the given eventData with associated constraints + * @param {Object} eventData a dictionary of event data + * @param {Object}(optional) constraints a set of constraints to apply to this event + * @returns {Promise} a Promise that returns the performed event Data with the given constraints or null if the event is blacklisted or should be discard + */ +Constraints.prototype.applyConstraintTreatments = function applyConstraints(eventData, constraints) { + var constraintsPromise = constraints ? Promise.resolve(constraints) : this.constraintsForEvent(eventData); + var self = this; + + return Promise.all([constraintsPromise, this._getConstraintGenerator()]) + .then(function (output) { + var constraints = output[0]; + var constraintGenerator = output[1]; + return constraintGenerator.treatment.applyConstraints(eventData, constraints); + }) + .catch(function (e) { + self.system.logger.warn('An error occurred while applying constraints: ' + e.message || e); + return null; + }); +}; + +export default Constraints; +export { connectConstraintConfig }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-client-logger-core/dist/mt-client-logger-core.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-client-logger-core/dist/mt-client-logger-core.esm.js new file mode 100644 index 0000000..2ef3193 --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-client-logger-core/dist/mt-client-logger-core.esm.js @@ -0,0 +1,533 @@ +import { reflect, string } from '@amp-metrics/mt-metricskit-utils-private'; + +/* + * src/utils.js + * mt-client-logger-core + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +function FlagSymbol(key) { + this.key = key; +} +FlagSymbol.prototype.toString = function toString() { + return this.key; +}; + +var utils = { + /** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + + /** Special flag classes that can be passed as arguments to logger methods in order to dictate logging behavior + * Use class instances to guarantee that flag arguments are unique, and use constructor names for O(1) lookup */ + flagArguments: { + /** + * When logging, if any of the arguments is an instance of this class, the log output will include a call stack trace. + * @example usage: logger.warn('danger!', logger.INCLUDE_CALL_STACK); + */ + INCLUDE_CALL_STACK: new FlagSymbol('INCLUDE_CALL_STACK'), + /** + * When logging, if any of the arguments is an instance of this class, the remaining arguments will be mirrored to the logging server + * @example usage: logger.info('some message', logger.MIRROR_TO_SERVER); + */ + MIRROR_TO_SERVER: new FlagSymbol('MIRROR_TO_SERVER'), + /** + * When logging, if any of the arguments is an instance of this class, the client (console) output will be suppressed + * This would typically be used when callers want to log an event to the server without printing it + * @example usage: logger.debug(someDiagnosticsInfoObject, logger.MIRROR_TO_SERVER, logger.SUPPRESS_CLIENT_OUTPUT); + */ + SUPPRESS_CLIENT_OUTPUT: new FlagSymbol('SUPPRESS_CLIENT_OUTPUT') + }, + + /** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes (these methods will not be copied to the target object). + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to replace some number of methods that need custom implementations. + * If, for example, a client wants to use the standard logger implementation with the exception of, say, the "debug" method, they can + * call "setDelegate()" with their own delegate containing only a single method of "debug" as the delegate, which would leave all the other methods intact. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * logger.setDelegate({ debug: console.debug }); + * To override one or more methods with a separate object: + * logger.setDelegate(customLoggerDelegate); + * (where "customLoggerDelegate" might be defined elsewhere as, e.g.: + * var customLoggerDelegate = { debug: function(msg) { document.getElementById('debugMsg').innerHTML = msg; }, + * serverUrl: function() { return 'https://custom-log-server.apple.com'; } }; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new CustomLoggerDelegate()); + * (where "CustomLoggerDelegate" might be defined elsewhere as, e.g.: + * function CustomLoggerDelegate() { + * } + * CustomLoggerDelegate.prototype.debug = function debug(msg) { + * document.getElementById('debugMsg').innerHTML = msg; + * }; + * CustomLoggerDelegate.prototype.serverUrl = function serverUrl() { + * return 'https://custom-log-server.apple.com'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(CustomLoggerDelegate); + * (where "CustomLoggerDelegate" might be defined elsewhere as, e.g.: + * function CustomLoggerDelegate() { + * } + * CustomLoggerDelegate.debug = function debug(msg) { + * document.getElementById('debugMsg').innerHTML = msg; + * }; + * CustomLoggerDelegate.serverUrl = function serverUrl() { + * return 'https://custom-log-server.apple.com'; + * }; + * @param {Object} delegate Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ + setDelegate: function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); + }, + + /** + * If the log level allows, logs/throws an error to the console and mirrors the log event to the server + * @param {Logger} logger + * @param {String} methodName + * @param {Array-like Object} origArguments + */ + execute: function execute(logger, methodName, origArguments) { + var methodLevel = logger.levelStringToIntMap[methodName]; + + if (logger.level() !== logger.NONE && logger.level() <= methodLevel) { + var argumentsArray = Array.prototype.slice.call(origArguments); + var logArguments = utils.nonFlagLogArguments(argumentsArray); + var logOptions = utils.logOptions(logger, methodLevel, argumentsArray); + var callstack = logOptions.includeCallStack ? new Error().stack : null; + var enrichedLogArguments = callstack ? logArguments.concat('\n' + callstack) : logArguments; // add newline for nicer output + + // so testing harness can verify logging done within tested functions: + logger[methodName]._lastLog = enrichedLogArguments; + + if (logOptions.mirrorToServer) { + utils.sendToServer(logger, methodName, logArguments, callstack); + } + + if (logOptions.throwInsteadOfPrint) { + throw new Error(logArguments.toString()); + } else if (!logOptions.suppressClientOutput) { + if (console[methodName]) { + console[methodName].apply(console, enrichedLogArguments); + } else { + // fallback to console.log - node does not have console.debug + console.log.apply(console, enrichedLogArguments); + } + } + } + }, + + /** + * Indicates whether an item is a specific flag object that dictates logging behavior + * @param {*} argument + * @return {Boolean} + */ + isFlagObject: function isFlagObject(argument) { + return argument && argument === utils.flagArguments[argument.toString()]; + }, + + /** + * Creates a new array without specific arguments that dictate logging behavior (and are not intended to be logged) + * @param {Array} argumentsArray + * @return {Array} + */ + nonFlagLogArguments: function nonFlagLogArguments(argumentsArray) { + return argumentsArray.filter(function (argument) { + return !utils.isFlagObject(argument); + }); + }, + + /** + * Inspects an array of arguments for specific flag objects that dictate log behavior and returns an object representing the intended behavior + * By checking for all of the various flags in one pass, we avoid looping over the arguments array more than necessary + * @param {Logger} logger + * @param {Int} methodLevel + * @param {Array} argumentsArray + * @return {Object} + */ + logOptions: function logOptions(logger, methodLevel, argumentsArray) { + var logOptions = {}; + var optionName; + + argumentsArray.forEach(function (argument) { + if (utils.isFlagObject(argument)) { + optionName = string.snakeCaseToCamelCase(argument.toString()); + logOptions[optionName] = true; + } + }); + + if ( + reflect.isFunction(logger.mirrorToServerLevel) && + logger.mirrorToServerLevel() !== logger.NONE && + logger.mirrorToServerLevel() <= methodLevel + ) { + logOptions.mirrorToServer = true; + } + if (logger.throwLevel() !== logger.NONE && logger.throwLevel() <= methodLevel) { + logOptions.throwInsteadOfPrint = true; + } + + return logOptions; + }, + + /** + * Sends a log event to the server immediately without checking resolution + * TODO: refactor to use eventRecorder once it is a standalone package + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @param {Logger} logger + * @param {String} level + * @param {Array} logArguments + * @param {String} (optional) callstack + * @return {String} the JSON-stringified event that was sent to the server + * @overridable + */ + sendToServer: function sendToServer(logger, level, logArguments, callstack) {} +}; + +/* + * src/logger.js + * mt-client-logger-core + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PRIVATE METHODS/IVARS ************************************ + */ + +// Define log levels separately to expose this constant. +// TODO clean constants up when consolidate. +var LOG_LEVELS = { + NONE: 0, + DEBUG: 1, + INFO: 2, + WARN: 3, + ERROR: 4 +}; +var LOGGER_LEVELS = { + MIN_LEVEL: LOG_LEVELS.NONE, + MAX_LEVEL: LOG_LEVELS.ERROR, + levelIntToStringMap: { + 0: 'none', + 1: 'debug', + 2: 'info', + 3: 'warn', + 4: 'error' + }, + levelStringToIntMap: { + none: 0, + debug: 1, + info: 2, + warn: 3, + error: 4 + } +}; + +reflect.extend(LOGGER_LEVELS, LOG_LEVELS); + +/** Global properties */ +var LOGGER_PROPERTIES = { + loggerName: 'defaultLogger', + level: LOGGER_LEVELS.INFO, + throwLevel: LOGGER_LEVELS.NONE +}; + +var _initialized = false; + +/** A map of logger names to Logger instances */ +var _loggers = {}; + +/** + * Provides basic "log4j" type functionality. + * The functionality in this class is typically replaced via a delegate. + * NOTE: This class has a "secret" field extending each logger function called "_lastLog" which allows us to inspect logged errors from within our test cases of various functionality + * to ensure that the correct errors are thrown. + * DEFAULT implementation: console logging + * DEFAULT logger level: INFO + * @see setDelegate + * @delegatable + * @constructor + * @param {String} loggerName + */ +function Logger(loggerName) { + // @private + this._loggerName = loggerName; + + /* These variables are enumerated here for clarity */ + // @private + this._level; + // @private + this._throwLevel; + + // lazily add prototype properties + if (!_initialized) { + _initialized = true; + reflect.extend(Logger.prototype, LOGGER_LEVELS); + reflect.extend(Logger.prototype, utils.flagArguments); + } +} + +/** + * Returns the logger instance that has the name <loggerName>, creating a new one if it doesn't exist + * @param {String} loggerName + * @return {Logger} + */ +function loggerNamed(loggerName) { + loggerName = loggerName || LOGGER_PROPERTIES.loggerName; + var returnLogger = _loggers[loggerName]; + + if (!returnLogger) { + returnLogger = new Logger(loggerName); + _loggers[loggerName] = returnLogger; + } + + return returnLogger; +} + +/** + * Remove a logger from the cache + * @param loggerName + */ +function removeLogger(loggerName) { + if (_loggers) { + delete _loggers[loggerName]; + } +} + +function resetLoggerCache() { + _loggers = {}; +} + +/** Default class property setters and getters */ +Logger.level = function level() { + return LOGGER_PROPERTIES.level; +}; +Logger.throwLevel = function throwLevel() { + return LOGGER_PROPERTIES.throwLevel; +}; + +// TODO: new PR with this, flesh out and make app-wide with docs +// Logger.setDelegate = function setDelegate() { }; +// Logger.logCallback = function logCallback() { }; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class instance's functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes (these methods will not be copied to the target object). + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to replace some number of methods that need custom implementations. + * If, for example, a client wants to use the standard logger implementation with the exception of, say, the "debug" method, they can + * call "setDelegate()" with their own delegate containing only a single method of "debug" as the delegate, which would leave all the other methods intact. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * logger.setDelegate({ debug: console.debug }); + * To override one or more methods with a separate object: + * logger.setDelegate(customLoggerDelegate); + * (where "customLoggerDelegate" might be defined elsewhere as, e.g.: + * var customLoggerDelegate = { debug: function(msg) { document.getElementById('debugMsg').innerHTML = msg; }, + * serverUrl: function() { return 'https://custom-log-server.apple.com'; } }; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new CustomLoggerDelegate()); + * (where "CustomLoggerDelegate" might be defined elsewhere as, e.g.: + * function CustomLoggerDelegate() { + * } + * CustomLoggerDelegate.prototype.debug = function debug(msg) { + * document.getElementById('debugMsg').innerHTML = msg; + * }; + * CustomLoggerDelegate.prototype.serverUrl = function serverUrl() { + * return 'https://custom-log-server.apple.com'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(CustomLoggerDelegate); + * (where "CustomLoggerDelegate" might be defined elsewhere as, e.g.: + * function CustomLoggerDelegate() { + * } + * CustomLoggerDelegate.debug = function debug(msg) { + * document.getElementById('debugMsg').innerHTML = msg; + * }; + * CustomLoggerDelegate.serverUrl = function serverUrl() { + * return 'https://custom-log-server.apple.com'; + * }; + * @param {Object} delegate Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Logger.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * The name of this logger + * @returns {String} + * @overridable + */ +Logger.prototype.loggerName = function loggerName() { + return this._loggerName; +}; + +/** + * Deduces the integer level from either a string or integer + * @param {*} level loglevel which may be either a string (e.g. 'debug', 'DEBUG', 'Debug', etc.) or an integer (e.g. 1, 2, 3 or logger.DEBUG, logger.INFO, logger.WARN, etc. + * @return {Int} the level as an integer or null if an invalid level argument was passed + * @overrideable + */ +Logger.prototype.levelParameterAsInt = function levelParameterAsInt(level) { + var returnLevel = null; + var integerLevel; + + if (reflect.isString(level)) { + integerLevel = this.levelStringToIntMap[level.toLowerCase()]; + } else if (reflect.isNumber(level)) { + integerLevel = level; + } + + if (integerLevel >= this.MIN_LEVEL && integerLevel <= this.MAX_LEVEL) { + returnLevel = integerLevel; + } + + return returnLevel; +}; + +/** + * Sets the level at which we will log at or above + * @param {*} level loglevel which may be either a string (e.g. 'debug', 'DEBUG', 'Debug', etc.) or an integer (e.g. 1, 2, 3 or logger.DEBUG, logger.INFO, logger.WARN, etc. + * @overridable + */ +Logger.prototype.setLevel = function setLevel(level) { + var integerLevel = this.levelParameterAsInt(level); + if (integerLevel !== null) { + this._level = integerLevel; + } +}; + +/** + * NOTE: This setting should be honored by all delegates. + * This setting will cause any emitted log message at or above the specified level to throw an exception with the log message instead of logging to the console. + * This is useful during testcase execution when we would expect to have no log output, or perhaps only "info" log output, etc. + * @param {*} throwLevel loglevel which may be either a string (e.g. 'debug', 'DEBUG', 'Debug', etc.) or an integer (e.g. 1, 2, 3 or logger.DEBUG, logger.INFO, logger.WARN, etc. + */ +Logger.prototype.setThrowLevel = function setThrowLevel(throwLevel) { + var integerLevel = this.levelParameterAsInt(throwLevel); + if (integerLevel !== null) { + this._throwLevel = integerLevel; + } +}; + +/** + * Returns the current logger level as an integer + * @overridable + */ +Logger.prototype.level = function level() { + var level = this._level; + return reflect.isNumber(level) ? level : Logger.level(); +}; + +/** + * Returns the current logger level as a string + * @overridable + */ +Logger.prototype.levelString = function levelString() { + return this.levelIntToStringMap[this.level()]; +}; + +/** + * Returns the current logger throw level as an integer + * @overridable + */ +Logger.prototype.throwLevel = function throwLevel() { + var throwLevel = this._throwLevel; + return reflect.isNumber(throwLevel) ? throwLevel : Logger.throwLevel(); +}; + +/** + * Emits the log message if log level is set to "debug". + * DEFAULT implementation: console.debug() + * @param {Object} a list of objects (perhaps a single string) to be stringified and emitted as the log message + * @api public + * @overridable + */ +Logger.prototype.debug = function debug() { + utils.execute(this, 'debug', arguments); +}; + +/** + * Emits the log message if log level is set to "info". + * DEFAULT implementation: console.info() + * @param {Object} a list of objects (perhaps a single string) to be stringified and emitted as the log message + * @api public + * @overridable + */ +Logger.prototype.info = function info() { + utils.execute(this, 'info', arguments); +}; + +/** + * Emits the log message if log level is set to "warn". + * DEFAULT implementation: console.warn() + * @param {Object} a list of objects (perhaps a single string) to be stringified and emitted as the log message + * @api public + * @overridable + */ +Logger.prototype.warn = function warn() { + utils.execute(this, 'warn', arguments); +}; + +/** + * Emits the log message if log level is set to "error". + * DEFAULT implementation: console.error() + * @param {Object} a list of objects (perhaps a single string) to be stringified and emitted as the log message + * @api public + * @overridable + */ +Logger.prototype.error = function error() { + utils.execute(this, 'error', arguments); +}; + +/** + * @param {String} levelString + * @return {String} the most recent log event for this level + */ +Logger.prototype.lastLog = function lastLog(levelString) { + return this[levelString] ? this[levelString]._lastLog : null; +}; + +var level = Logger.level; +var throwLevel = Logger.throwLevel; + +/* + * mt-client-logger-core/index.js + * mt-client-logger-core + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +export default Logger; +export { LOG_LEVELS, level, loggerNamed, removeLogger, resetLoggerCache, throwLevel, utils }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-event-queue/dist/mt-event-queue.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-event-queue/dist/mt-event-queue.esm.js new file mode 100644 index 0000000..772b4cc --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-event-queue/dist/mt-event-queue.esm.js @@ -0,0 +1,1364 @@ +import { loggerNamed } from '@amp-metrics/mt-client-logger-core'; +import { reflect, network as network$1 } from '@amp-metrics/mt-metricskit-utils-private'; + +/* + * src/environment.js + * mt-event-queue + * + * Copyright © 2016-2019 Apple Inc. All rights reserved. + * + */ + +var environment = { + /** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + + /** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ + setDelegate: function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); + }, + + /** + * An object that conforms to the WindowOrWorkerGlobalScope API: + * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope + * @return {WindowOrWorkerGlobalScope} + */ + globalScope: function globalScope() { + return reflect.globalScope(); + } +}; + +/* + * src/utils/constants.js + * mt-event-queue + * + * Copyright © 2019 Apple Inc. All rights reserved. + * + */ + +// These constants are exposed publicly + +/** + * Possible send method types to record events + */ +var SEND_METHOD = { + AJAX: 'ajax', + AJAX_SYNCHRONOUS: 'ajaxSynchronous', + IMAGE: 'image', + BEACON: 'beacon', + BEACON_SYNCHRONOUS: 'beaconSynchronous' +}; + +/* + * src/event_recorder/base.js + * mt-event-queue + * + * Copyright © 2016-2019 Apple Inc. All rights reserved. + * + */ + +var IDENTIFIABLE_FIELDS = ['dsId', 'consumerId']; + +/** + * Clone the passed-in source config with the provided topic + * + * Since we support multiple config instances instead of singleton config in mt-client-config. + * We have to pass the topicConfig instance to all places that we were passing a topic string to in the past. + * The problem there was if a caller wants to send an event to a sub-topic of the config and the config has some overriding values for the sub-topic, + * we need both config and the sub-topic to collect the values for the sub-topic. + * we either have to pass topicConfig and the sub-topic everywhere or create a new config with the sub-topic based on the topic config of the main topic. + * So we clone a new config by the main topic config with the sub-topic in the root entry(evenRecorder.recordEvent()) then all related logic would use that cloned config to load the config for the sub topic + * + * NOTE: We create an object and put the main topic config to the prototype of it because in that way, + * the cloned config still be able to share the same functions from the source config and when the source config getting cleanup, the cloned config also gets cleanup. + * The main purpose of the clone is overriding the main topic with the passed-in topic + * + * For Example: + * // We have a topic config with "topic_A" as the main topic, and a "topic_B" as the sub-topic inside of the config + * var topicConfig = { + * metricsUrl: 'https://xp.apple.com/report', + * postFrequency: 60000 + * topic_B: { + * postFrequency: 30000 + * } + * }; + * var metricsKit = new MetricsKit('topic_A'); // which will have the above topicConfig for "topic_A" + * var eventFields = metricsKit.eventHandlers.enter.metricsData(); + * var eventRecorder = new EventRecorder(metricsKit); + * eventRecorder.recordEvent('topic_B', eventFields); + * + * For above example, the recordEvent function will clone a new config by the topicConfig(topic_A) with the "topic_B". + * So underneath of the event recorder, we could get the correct values for "topic_B" + * topicConfig.value('postFrequency'); -> 30000 + * event_queue.metricsUrlForConfig() -> https://xp.apple.com/report/topic_B + * + * @param sourceConfig + * @param topic + */ +function cloneTopicConfigWithTopic(sourceConfig, topic) { + var clonedConfig = { + _topic: topic + }; + + // inherit the class to have fields, methods and delegated methods from the source config + Object.setPrototypeOf(clonedConfig, sourceConfig); + + return clonedConfig; +} + +/** + * Abstract Event Recorder class + * @constructor + */ +var Base = function Base(configInstance) { + this._validateConfig(configInstance); + // @private + this._config = configInstance; + + // @private + this._topicConfigCache = {}; + + // @private + this._topicPropsCache = {}; + + this.logger = loggerNamed('mt-event-queue'); +}; + +/** + * Previously, this private method validated the config from metricskit but now it will validate the + * config directly + * + * @param {Object} config Configuration object + */ +Base.prototype._validateConfig = function validateConfig(config) { + var errorPostfix = 'please call constructor with a valid Config instance first.'; + if (!config) { + throw new TypeError('Unable to find config, ' + errorPostfix); + } else if (!config.topic || !reflect.isFunction(config.topic)) { + throw new TypeError('Unable to find config.topic function, ' + errorPostfix); + } else if ( + !config.metricsDisabledOrDenylistedEvent || + !reflect.isFunction(config.metricsDisabledOrDenylistedEvent) + ) { + throw new TypeError('Unable to find config.metricsDisabledOrDenylistedEvent function, ' + errorPostfix); + } else if (!config.removeDenylistedFields || !reflect.isFunction(config.removeDenylistedFields)) { + throw new TypeError('Unable to find config.removeDenylistedFields function, ' + errorPostfix); + } +}; + +/** + * Remove identify fields from the eventFields based on the topic properties + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @param {Object} eventFields a JavaScript object which will be converted to a JSON string and sent to AMP Analytics immediately. + * @private + */ +Base.prototype._removeIdentifiableFieldsForTopic = function _removeIdentifiableFieldsForTopic(topic, eventFields) { + this._topicPropsCache[topic] = this._topicPropsCache[topic] || {}; + if (this._topicPropsCache[topic].anonymous) { + IDENTIFIABLE_FIELDS.forEach(function (field) { + delete eventFields[field]; + }); + } +}; + +/** + * Record event + * Subclasses implement this method to handle how to record an event + * @abstract + * @param {Object} eventFields a JavaScript object which will be converted to a JSON string and sent to AMP Analytics immediately. + * @returns {Promise} + */ +Base.prototype._record = function _record(eventFields) {}; + +/** + * clean resources of event recorder + * Subclasses implement this method to handle how to clean resources + * @returns {Promise} returns a Promise if the cleanup will asynchronously execute or undefined for synchronously executing + */ +Base.prototype.cleanup = function cleanup() { + this._config = null; + this._topicConfigCache = null; + this._topicPropsCache = {}; +}; + +/** + * Records an event as JSON + * TODO: We should look at simplifying the process of using multiple topics. By deprecating recordEvent(topic, eventFields) in favor of recordEvent(eventFields) using the topic from the Kit. + * @param {String} topic an 'override' topic which will override the main topic. + * NOTE: + * 1. RecordEvent needs to check the denylisted event/fields. The passed-in topic may not be enough to check them. If MetricsKit's config had a subsection for the passed-in topic, then the denylisting would work properly. + * 2. The eventFields were generated with the config of Metricskit. If sending them to another topic, the eventFields might have incorrect values. + * @param {Object} eventFields a JavaScript object which will be converted to a JSON string and sent to AMP Analytics immediately. + * @returns {Promise} a Promise that returns the recorded event, or "null" if no object was recorded (e.g. if "eventFields" is null, or "disabled" is true, eventFields.eventType is one of the denylistedEvents, etc.) + */ +Base.prototype.recordEvent = function recordEvent(topic, eventFields) { + if (!this._config || !eventFields) { + return Promise.resolve(null); + } + var config = this._config; + var self = this; + var args = arguments; + + // We do this if `topic` is a sub-topic of the configuration + if (reflect.isDefinedNonNullNonEmpty(topic) && topic !== config.topic()) { + config = this._topicConfigCache[topic]; + + if (!config) { + config = this._topicConfigCache[topic] = cloneTopicConfigWithTopic(this._config, topic); + } + } + + // NOTE: Typically all event_handlers will check for this as well because that way if a client overrides "recordEvent", these checks will still take effect. + // We also test it here in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + return config + .metricsDisabledOrDenylistedEvent(eventFields.eventType) + .then(function (disabledOrBlacklistedEvent) { + if (disabledOrBlacklistedEvent) { + return null; + } + return config + .removeDenylistedFields(eventFields) + .then(function () { + self._removeIdentifiableFieldsForTopic(topic, eventFields); + return self._record.apply(self, [config].concat(Array.prototype.slice.call(args, 1))); + }) + .then(function () { + return eventFields; + }); + }) + .catch(function (error) { + self.logger.error(error); + return null; + }); +}; + +/** + * The methodology being used to send batches of events to the server + * This field should be hardcoded in the client based on what method it is using to encode and send its events to Figaro. + * The three typical values are: + * "itms" - use this value when/if JavaScript code enqueues events for sending via the "itms.recordEvent()" method in ITML. + * "itunes" - use this value when/if JavaScript code enqueues events by calling the "iTunes.recordEvent()" method in Desktop Store apps. + * "javascript" - use this value when/if JavaScript code enqueues events for sending via the JavaScript eventQueue management. This is typically only used by older clients which don't have the built-in functionality of itms or iTunes available to them. + * @example "itms", "itunes", "javascript" + * @returns {String} + */ +Base.prototype.sendMethod = function sendMethod() { + return 'javascript'; +}; + +/** + * Set related properties for the giving topic + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @param {Object} properties the properties for the topic + * @param {Boolean} properties.anonymous true if sending all events for the topic with credentials omitted(no cookies, no PII fields) + */ +Base.prototype.setProperties = function setProperties(topic, properties) { + this._topicPropsCache[topic] = this._topicPropsCache[topic] || {}; + this._topicPropsCache[topic] = properties; +}; + +/* + * src/network.js + * mt-event-queue + * + * Copyright © 2017 Apple Inc. All rights reserved. + * + */ + +/** + * Network request methods exposed so delegate callers can override + * @constructor + */ +var network = { + /** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ + setDelegate: function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); + }, + /** + * Covers private util network functions for delegation + */ + makeAjaxRequest: network$1.makeAjaxRequest +}; + +/* + * src/event_queue.js + * mt-event-queue + * + * A low-level, cross-platform, metrics batcher and emitter. + * Adapted from Jingle (its.MetricsQueue) + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +/** + * Some ugly user agent detection, because iOS browsers refuse to send image pings onpagehide + * @return {Boolean} true if this browser is running on an iOS device + */ +function _isIOS() { + var userAgent = navigator.userAgent; + + // IE for Windows Phone 8.1 contains the string 'iPhone', so we have to check for that too... + // https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx + return /iPad|iPhone|iPod/.test(userAgent) && userAgent.indexOf('IEMobile') == -1; +} + +/** + * Check if the fetch API is supported by the Browser and check if "keepalive" is available by checking for Firefox + * @returns {Boolean} true if fetch API is supported + */ +function _isFetchAndKeepaliveAvailable() { + return reflect.isFunction(environment.globalScope().fetch) && !/Firefox/.test(navigator.userAgent); +} + +function _logger() { + return loggerNamed('mt-event-queue'); +} + +var CONSTANTS = { + DEFAULT_REQUEST_TIMEOUT: 10000, // TODO: move to config + EVENTS_KEY: 'events', + EVENT_DELIVERY_VERSION: '1.0', + MAX_PERSISTENT_QUEUE_SIZE: 100, // TODO: move to config + RETRY_EXPONENT_BASE: 2, // TODO: move to config + SEND_METHOD: SEND_METHOD, + URL_DELIVERY_VERSION: 2, + PROPERTIES_KEY: 'properties' +}; + +var _eventQueue = { + /** + * A dictionary of event queues by topic + * Each topic queue is an object that holds an array of the events themselves and information about send attempts for that topic (postIntervalToken, retryAttempts) + * We use an in-memory queue for two reasons: + * 1) We expect all remaining events to get sent on app exit via flushUnreportedEvents (ignoring the failed send case) + * 2) Stringifying and writing is expensive - as much as 5ms to write 100 events to localStorage on an older (2013) mobile device, + * and that would have to happen each time we did an event insertion, since localStorage only allows the saving of stringified (serialized) data. + */ + eventQueues: {}, + + /** + * Determines whether events will be sent via a post interval. + * If this value is false then the client must perform flushing manually and events will not be scheduled or sent automatically. + */ + postIntervalEnabled: true, + + /** + * Enqueue an event to a particular topic queue + * @param {Object} topicConfig a config instance for the Figaro "topic" that this event should be stored under + * @param {Object} eventFields a JavaScript object which will be converted to a JSON string and enqued for sending to Figaro according to the postFrequency schedule + * @return {Promise} a Promise that returns the current event, if it was successfully queued, otherwise null + */ + enqueueEvent: function enqueueEvent(topicConfig, eventFields) { + if (topicConfig && eventFields && topicConfig.topic()) { + var topic = topicConfig.topic(); + _eventQueue.eventQueues = _eventQueue.eventQueues || {}; + _eventQueue.eventQueues[topic] = _eventQueue.eventQueues[topic] || {}; + _eventQueue.eventQueues[topic].topicConfig = topicConfig; + _eventQueue.eventQueues[topic].flushConfig = _eventQueue.eventQueues[topic].flushConfig || {}; + _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY] = + _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY] || []; + _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY].push(eventFields); // Add the new event to the end of our eventQueue + + // Set flushing related config values + if (Object.keys(_eventQueue.eventQueues[topic].flushConfig).length === 0) { + Promise.all([ + topicConfig.value('metricsUrl'), + topicConfig.value('requestTimeout'), + topicConfig.value('postFrequency') + ]).then(function (results) { + var flushConfig = _eventQueue.eventQueues[topic].flushConfig; + flushConfig.metricsUrl = results[0]; + flushConfig.requestTimeout = results[1]; + flushConfig.postFrequency = results[2]; + }); + } + + return topicConfig.value('maxPersistentQueueSize').then(function (maxQueueSize) { + maxQueueSize = maxQueueSize || CONSTANTS.MAX_PERSISTENT_QUEUE_SIZE; + _eventQueue.trimEventQueues(_eventQueue.eventQueues, maxQueueSize); + return eventFields; + }); + } else { + return Promise.resolve(null); + } + }, + + /** + * Currently this just ensures that each queue is <= maxEventsPerQueue, but in the future we could remove oldest events in a queue-independent way + * @param {Object} eventQueues a dictionary of event queues (arrays) by topic + * @param {int} maxEventsPerQueue + */ + trimEventQueues: function trimEventQueues(eventQueues, maxEventsPerQueue) { + var topics = Object.keys(eventQueues); + if (topics.length) { + topics.forEach(function (topic) { + var events = eventQueues[topic][CONSTANTS.EVENTS_KEY]; + if (events && events.length && events.length > maxEventsPerQueue) { + _logger().warn( + 'eventQueue overflow, deleting LRU events: size is: ' + + events.length + + ' which is over max size: ' + + maxEventsPerQueue + ); + eventQueues[topic][CONSTANTS.EVENTS_KEY] = events.slice(-maxEventsPerQueue); + } + }); + } + }, + + /** + * Clears an event queue for a topic + * @param {String} topic defines the Figaro "topic" queue that should be cleared + */ + resetTopicQueue: function resetTopicQueue(topic) { + if (_eventQueue.eventQueues[topic]) { + _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY] = null; + } + }, + + /** + * Resets the retry attempt counter for a topic, called when a send for that topic is successful + * @param {String} topic defines the Figaro "topic" queue whose counter should be cleared + */ + resetTopicRetryAttempts: function resetTopicRetryAttempts(topic) { + if (_eventQueue.eventQueues[topic]) { + _eventQueue.eventQueues[topic].retryAttempts = 0; + } + }, + + /** + * Increments the retry attempt counter for a topic, called when a send for that topic results in a 5XX response + * Sets the next send time for the topic according to an exponential backoff strategy + * @param {Object} A config for the Figaro "topic" queue whose counter should be incremented + * @returns {Promise} + */ + scheduleNextTopicRetryAttempt: function scheduleNextTopicRetryAttempt(topicConfig) { + var topic = topicConfig.topic(); + if (_eventQueue.eventQueues[topic] && this.postIntervalEnabled) { + return topicConfig.value('postFrequency').then(function (postFrequency) { + var topicEventQueue = _eventQueue.eventQueues[topic]; + topicEventQueue.retryAttempts = topicEventQueue.retryAttempts || 0; + topicEventQueue.retryAttempts++; + + var nextSendTime = + Math.pow(CONSTANTS.RETRY_EXPONENT_BASE, topicEventQueue.retryAttempts) * postFrequency; + _eventQueue.resetTopicPostInterval(topic); + _eventQueue.setTopicPostInterval(topicConfig, nextSendTime); + }); + } else { + return Promise.resolve(); + } + }, + + /** + * Send queued events to the ingestion server + * If a particular topic send has previously failed, RETRY_BACKOFF_SKIP_COUNT_KEY will be nonzero, indicating we should skip sending that number of times + * @param {String} sendMethod "image" or "ajax" (default) or "ajaxSynchronous" + * @param {Boolean} postNow will be true when clients force a send outside of the regular postFrequency interval + * @returns {Promise} + */ + sendEvents: function sendEvents(sendMethod, postNow) { + var sendingTasks = []; + for (var topic in _eventQueue.eventQueues) { + var topicConfig = _eventQueue.eventQueues[topic].topicConfig; + var sendingPromise = _eventQueue.sendEventsForTopicConfig(topicConfig, sendMethod, postNow); + sendingTasks.push(sendingPromise); + } + + return Promise.all(sendingTasks); + }, + + /** + * Send events for a single topic queue + * @param {Object} topicConfig defines the Figaro "topic" queue + * @param {String} sendMethod "image" or "ajax" (default) or "ajaxSynchronous" + * @param {Boolean} postNow will be true when clients force a send outside of the regular postFrequency interval + * @returns {Promise} + */ + sendEventsForTopicConfig: function sendEventsForTopicConfig(topicConfig, sendMethod, postNow) { + var topic = topicConfig.topic(); + var topicQueue = _eventQueue.eventQueues[topic]; + + return Promise.all([ + topicConfig.value('testExponentialBackoff'), + topicConfig.value('metricsUrl'), + topicConfig.disabled(), + topicConfig.value('postFrequency') + ]).then(function (outputs) { + var testExponentialBackoff = outputs[0]; + var metricsUrl = outputs[1]; + var topicDisabled = outputs[2]; + var postFrequency = outputs[3]; + + if (topicQueue && metricsUrl && !topicDisabled && !testExponentialBackoff) { + // Do not send if we are trying to postNow in the middle of a backoff + if (!(topicQueue.retryAttempts && postNow)) { + // The rule is "we post every postFrequency milliseconds", so even if it's been less than that (e.g. postNow), we reset + _eventQueue.resetTopicPostInterval(topic); + _eventQueue.setTopicPostInterval(topicConfig, postFrequency); + var sendingPromise; + + switch (sendMethod) { + case CONSTANTS.SEND_METHOD.IMAGE: + sendingPromise = _eventQueue.sendEventsViaImage(topicConfig); + break; + case CONSTANTS.SEND_METHOD.BEACON: + sendingPromise = _eventQueue.sendEventsViaBeacon(topicConfig); + break; + case CONSTANTS.SEND_METHOD.AJAX_SYNCHRONOUS: + sendingPromise = _eventQueue.sendEventsViaAjax(topicConfig, false); + break; + case CONSTANTS.SEND_METHOD.AJAX: /* falls through */ + default: + sendingPromise = _eventQueue.sendEventsViaAjax(topicConfig, true); + break; + } + + return sendingPromise; + } + } + // Fail automatically if test flag present + else if (testExponentialBackoff) { + return _eventQueue.scheduleNextTopicRetryAttempt(topicConfig); + } + }); + }, + + /** + * Makes one image ping per event in a queue of events, then clears the queue + * This is typically called on page / app close when the JS context is about to disappear and thus we will not know if the events made it to the server + * Current testing shows that browsers support 100+ image pings sent onpagehide, but we may need to alter our approach if this becomes unreliable + * (For example, we could send multiple events in a single ping instead, but there might be URL length issues in IE) + * @param {Object} topicConfig a config instance for the Figaro "topic" to send to + * @returns {Promise} + */ + sendEventsViaImage: function sendEventsViaImage(topicConfig) { + var topic = topicConfig.topic(); + var returnedPromise = Promise.resolve(); + + if (_eventQueue.eventQueues[topic]) { + returnedPromise = resolveTopicConfigWithCallback(topicConfig, function (resolvedTopicConfig) { + var topicUrl = resolvedTopicConfig.metricsUrl; + var qpSeparator = topicUrl.indexOf('?') == -1 ? '?' : '&'; + var imageBaseUrl = topicUrl + qpSeparator + 'responseType=image'; + var events = _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY]; + + if (events && events.length) { + events.forEach(function (event) { + var imageParams = _eventQueue.createQueryParams(event); + if (imageParams) { + var imgUrl = imageBaseUrl + '&' + imageParams; + var imgObject = new Image(); + var properties = _eventQueue.eventQueues[topic][CONSTANTS.PROPERTIES_KEY]; + if (properties && properties.anonymous) { + imgObject.setAttribute('crossOrigin', 'anonymous'); + } + imgObject.src = imgUrl; + } + }); + } + + _eventQueue.resetTopicQueue(topic); + }); + } + return returnedPromise; + }, + + /** + * Convert an event object into a query parameter string, without a leading separator + * Guaranteed to return "null" if there are no event fields + * @param {Object} event key/value pairs containing event data + */ + createQueryParams: function createQueryParams(event) { + var val; + var stringVal; + var returnValue = ''; + Object.keys(event).forEach(function (key, index, eventKeys) { + val = event[key]; + // do not double-encode strings otherwise they will be reported as: '<value>' + stringVal = reflect.isString(val) ? val : JSON.stringify(val); + returnValue += key + '=' + encodeURIComponent(stringVal); + if (index < eventKeys.length - 1) { + // don't add a trailing ampersand + returnValue += '&'; + } + }); + return returnValue.length ? returnValue : null; + }, + + /** + * Makes one AJAX request per topic and clears the queue for that topic on success + * If any queue fails, retry using an exponential backoff strategy for that queue + * Refer to Metrics documentation for more details + * @param {Object} topicConfig a config instance for the Figaro "topic" to send to + * @param {Boolean} async - send asynchronously + * @returns {Promise} + */ + sendEventsViaAjax: function sendEventsViaAjax(topicConfig, async) { + var returnedPromise = Promise.resolve(); + var topic = topicConfig.topic(); + if (_eventQueue.eventQueues[topic] && _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY]) { + // Store events to be sent and reset the queue + var events = _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY]; + var jsonEventsString = enrichAndSerializeEvents(events); + _eventQueue.resetTopicQueue(topic); + + if (jsonEventsString) { + returnedPromise = resolveTopicConfigWithCallback(topicConfig, function (resolvedTopicConfig) { + var topicUrl = resolvedTopicConfig.metricsUrl; + var requestTimeout = resolvedTopicConfig.requestTimeout; + var resetRetryAttempts = function resetRetryAttempts() { + _eventQueue.resetTopicRetryAttempts(topic); + }; + var onAjaxFailure = function onAjaxFailure(error, statusCode) { + // We're being told not to keep resending these events. + if (statusCode >= 400 && statusCode < 500) { + resetRetryAttempts(); + } else { + // Prepend the events that failed to send back onto the queue + var newEvents = _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY] || []; + _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY] = events.concat(newEvents); + + _eventQueue.scheduleNextTopicRetryAttempt(topicConfig); + } + }; + var eventQueueProps = _eventQueue.eventQueues[topic][CONSTANTS.PROPERTIES_KEY] || {}; + var options = { + async: async, + timeout: requestTimeout + }; + + if (eventQueueProps.anonymous) { + options.withCredentials = false; + } + network.makeAjaxRequest(topicUrl, 'POST', jsonEventsString, resetRetryAttempts, onAjaxFailure, options); + }); + } + } + return returnedPromise; + }, + + /** + * Makes an HTTP POST request via navigator.sendBeacon() if it is in the environment. + * Note: The sendBeacon() method returns true if the user agent successfully queued the data for transfer, otherwise false. + * sendBeacon() always includes credentials, so we fallback to the IMAGE/AJAX_SYNCHRONOUS methods to handle the anonymous use case. + * While testing out possible failures of sendBeacon(), the only 2 cases we encountered was using a non-"simple" content type like "application/json", + * or by passing data that is too large. Both cases would lead to failures when retrying, so retrying logic has intentionally been omitted in this approach. + * @param {Object} topicConfig a config instance for the Figaro "topic" to send to + */ + sendEventsViaBeacon: function sendEventsViaBeacon(topicConfig) { + var sendPromise = Promise.resolve(); + + if (!reflect.isFunction(navigator.sendBeacon)) { + _logger().error('navigator.sendBeacon() is not available in the environment'); + return sendPromise; + } + + var topic = topicConfig.topic(); + var topicQueue = _eventQueue.eventQueues[topic]; + + if (topicQueue) { + var eventQueueProps = topicQueue[CONSTANTS.PROPERTIES_KEY]; + + // Fallback to other send methods to handle the anonymous use case + if (eventQueueProps && eventQueueProps.anonymous) { + // using fetch with keepalive to send events to fix the large event content issue (impressions field). + if (_isFetchAndKeepaliveAvailable()) { + sendPromise = _eventQueue.sendEventsViaFetch(topicConfig, { keepalive: true }); + } else if (_isIOS()) { + // iOS browsers do not allow image pings to send onpagehide; use (deprecated) synchronous AJAX in those browsers + sendPromise = _eventQueue.sendEventsViaAjax(topicConfig, false); + } else { + sendPromise = _eventQueue.sendEventsViaImage(topicConfig); + } + } else { + var jsonEventsString = enrichAndSerializeEvents(topicQueue[CONSTANTS.EVENTS_KEY]); + if (jsonEventsString) { + _eventQueue.resetTopicQueue(topic); + + sendPromise = resolveTopicConfigWithCallback(topicConfig, function (resolvedTopicConfig) { + var topicUrl = resolvedTopicConfig.metricsUrl; + var Blob = environment.globalScope().Blob; + var eventsBlob = new Blob([jsonEventsString], { type: 'application/json' }); + var beaconResponse = navigator.sendBeacon(topicUrl, eventsBlob); + + if (!beaconResponse) { + _logger().error('navigator.sendBeacon() was unable to queue the data for transfer'); + } + }); + } + } + } + + return sendPromise; + }, + + /** + * Makes an HTTP POST request via fetch() + * @param {Object} topicConfig a config instance for the Figaro "topic" to send to + * @param {Object} options an object contains the options of fetch API + * @param {Boolean} options.keepalive whether allow the request to outlive the page + */ + sendEventsViaFetch: function sendEventsViaFetch(topicConfig, options) { + var sendPromise = Promise.resolve(); + var topic = topicConfig.topic(); + var topicQueue = _eventQueue.eventQueues[topic]; + var keepalive = reflect.isDefinedNonNull(options) ? options.keepalive : null; + + if (reflect.isDefinedNonNull(topicQueue)) { + var jsonEventsString = enrichAndSerializeEvents(topicQueue[CONSTANTS.EVENTS_KEY]); + if (jsonEventsString) { + _eventQueue.resetTopicQueue(topic); + + var eventQueueProps = topicQueue[CONSTANTS.PROPERTIES_KEY] || {}; + sendPromise = resolveTopicConfigWithCallback(topicConfig, function (resolvedTopicConfig) { + var topicUrl = resolvedTopicConfig.metricsUrl; + reflect.globalScope().fetch(topicUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: jsonEventsString, + credentials: eventQueueProps.anonymous === true ? 'omit' : 'same-origin', + keepalive: reflect.isDefinedNonNull(keepalive) ? keepalive : true + }); + }); + } + } + return sendPromise; + }, + + /** + * If no postInterval is currently set, sets postInterval to "postInterval" + * Note: Currently, only _sendEvents calls this function, but in the future, if other callers wanted to set a new interval due to, say, a config change, + * then events enqueued under the old postFrequency time will now have to wait (or be expedited) to the new time. To fix this, when any old + * interval fires, the callback can check to see if the interval passed in is different than _eventQueue.postIntervalToken and if so, it will tear down its timer. + * @param {Object} topic config + * @param {int} postInterval in ms + */ + setTopicPostInterval: function setTopicPostInterval(topicConfig, postInterval) { + var topic = topicConfig.topic(); + if (_eventQueue.eventQueues[topic] && postInterval && this.postIntervalEnabled) { + this.resetTopicPostInterval(topic); + _eventQueue.eventQueues[topic].postIntervalToken = environment + .globalScope() + .setInterval(function onPostIntervalTrigger() { + _logger().debug( + 'MetricsKit: triggering postIntervalTimer for ' + topic + ' at ' + new Date().toString() + ); + _eventQueue.sendEventsForTopicConfig(topicConfig); + }, postInterval); + } + }, + + resetTopicPostInterval: function resetTopicPostInterval(topic) { + if (_eventQueue.eventQueues[topic]) { + environment.globalScope().clearInterval(_eventQueue.eventQueues[topic].postIntervalToken); + _eventQueue.eventQueues[topic].postIntervalToken = null; + } + }, + + resetQueuePostIntervals: function resetQueuePostIntervals() { + for (var topic in _eventQueue.eventQueues) { + _eventQueue.resetTopicPostInterval(topic); + } + }, + + setQueuePostIntervals: function setQueuePostIntervals() { + var tasks = []; + var setTopicPostIntervalFn = function (topicConfig) { + return function (postFrequency) { + _eventQueue.setTopicPostInterval(topicConfig, postFrequency); + }; + }; + for (var topic in _eventQueue.eventQueues) { + var events = _eventQueue.eventQueues[topic][CONSTANTS.EVENTS_KEY]; + var topicConfig = _eventQueue.eventQueues[topic].topicConfig; + if (events && events.length) { + var taskPromise = topicConfig.value('postFrequency').then(setTopicPostIntervalFn(topicConfig)); + tasks.push(taskPromise); + } + } + return Promise.all(tasks); + }, + + /** + * Determines whether the object contains the provided value. + * Note that values that are functions will be ignored. + * @param {Object} object the object whose values will be evaluated + * @param {Any} value the requested value to search for on the provided object + * @returns {Boolean} whether the object contains the value + * TODO: consider moving to utils + */ + objectContainsValue: function objectContainsValue(object, value) { + var result = false; + for (var property in object) { + var aValue = object[property]; + if (object.hasOwnProperty(property) && !reflect.isFunction(aValue) && aValue === value) { + result = true; + break; + } + } + return result; + }, + + /** + * Set event queue related properties for the giving topic + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @param {Object} properties the event queue properties for the topic + * @param {Boolean} properties.anonymous true if sending all events for the topic with credentials omitted(no cookies, no PII fields) + */ + setProperties: function setProperties(topic, properties) { + _eventQueue.eventQueues = _eventQueue.eventQueues || {}; + _eventQueue.eventQueues[topic] = _eventQueue.eventQueues[topic] || {}; + _eventQueue.eventQueues[topic][CONSTANTS.PROPERTIES_KEY] = properties; + } +}; + +/** + ************************************ PSEUDO-PRIVATE METHODS/IVARS ************************************ + * These functions need to be accessible for ease of testing, but should not be used by clients + */ +function _utQueue() { + return _eventQueue; +} + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Adds all the supplemental fields like "postTime", etc. + * Guaranteed to return "null" if there are no events + * @param {Array} eventQueue a list of events to send + * @return A stringified version of our eventQueue as a batch, including supplementary top-level fields, all ready to deliver in a ping + */ +function enrichAndSerializeEvents(eventQueue) { + var eventsBatchString = null; + + if (eventQueue && eventQueue.length) { + var eventsDict = {}; + eventsDict['deliveryVersion'] = CONSTANTS.EVENT_DELIVERY_VERSION; + eventsDict['postTime'] = Date.now(); + eventsDict[CONSTANTS.EVENTS_KEY] = eventQueue; + try { + eventsBatchString = JSON.stringify(eventsDict); + } catch (e) { + _logger().error('Error stringifying events as JSON: ' + e); + } + } + return eventsBatchString; +} + +function enrichAndSerializeEvent(event) { + return enrichAndSerializeEvents([event]); +} + +/** + * @param {Object} topicConfig instance + * @return {Promise} a Promise that returns the metrics URL for this topic + */ +function metricsUrlForConfig(topicConfig) { + return topicConfig.value('metricsUrl').then(function (metricsUrl) { + var baseMetricsUrl = metricsUrl + '/' + CONSTANTS.URL_DELIVERY_VERSION + '/'; + return baseMetricsUrl + topicConfig.topic(); + }); +} + +/** + * This method is used to hide the topicConfig.value implementation for multiple properties, in order to support async and sync logic in sendEventsViaAjax(). + * @param {Object} topicConfig an instance of TopicConfig(with a method named "value" and returns Promise from it) or an object that contains the "metricsUrl" and "topic()" properties + * @param {Function} callback a function to invoke with an object has metricsUrl, requestTimeout + */ +function resolveTopicConfigWithCallback(topicConfig, callback) { + if (!reflect.isFunction(callback)) { + _logger().warn('No callback function is provided for resolveTopicConfigWithCallback()'); + return; + } + + if (reflect.isString(topicConfig.metricsUrl) && !reflect.isFunction(topicConfig.value)) { + var topic = reflect.isFunction(topicConfig.topic) ? topicConfig.topic() : topicConfig.topic; + var metricsUrl = topicConfig.metricsUrl + '/' + CONSTANTS.URL_DELIVERY_VERSION + '/' + topic; + var requestTimeout = topicConfig.requestTimeout || CONSTANTS.DEFAULT_REQUEST_TIMEOUT; + var postFrequency = topicConfig.postFrequency; + + return callback({ + requestTimeout: Math.min(requestTimeout, postFrequency), + metricsUrl: metricsUrl + }); + } else { + return Promise.all([ + topicConfig.value('requestTimeout'), + topicConfig.value('postFrequency'), + metricsUrlForConfig(topicConfig) + ]).then(function (outputs) { + var requestTimeout = outputs[0] || CONSTANTS.DEFAULT_REQUEST_TIMEOUT; + var postFrequency = outputs[1]; + var metricsUrl = outputs[2]; + return callback({ + requestTimeout: Math.min(requestTimeout, postFrequency), + metricsUrl: metricsUrl + }); + }); + } +} + +/** + * @param {Object} topicConfig instance + * @return {Promise} a Promise that returns the request timeout for this config + * Defaults to CONSTANTS.DEFAULT_REQUEST_TIMEOUT + */ +function requestTimeoutForConfig(topicConfig) { + return Promise.all([topicConfig.value('requestTimeout'), topicConfig.value('postFrequency')]).then(function ( + outputs + ) { + var requestTimeout = outputs[0] || CONSTANTS.DEFAULT_REQUEST_TIMEOUT; + var postFrequency = outputs[1]; + return Math.min(requestTimeout, postFrequency); + }); +} + +/** + * Determines whether events will be sent via a post interval. + * If this value is false then the client must perform flushing manually and events will not be scheduled or sent automatically. + * @param {Bool} enabled whether the postInterval is enabled or not + * @returns {Promise} + */ +function setPostIntervalEnabled(enabled) { + _eventQueue.postIntervalEnabled = enabled; + if (enabled) { + return _eventQueue.setQueuePostIntervals(); + } else { + return Promise.resolve(_eventQueue.resetQueuePostIntervals()); + } +} + +/** + * Enqueues events to be sent to the server in batches after "postFrequency" milliseconds since the previous send. + * The queue is stored in memory, and will retry on failed sends for as long as the session is open. + * Immediately before a page turn/tab close (usually onpagehide), clients can call flushUnreportedEvents() to send up any events still left in the queue. + * + * Flow: + * 1. Normal: + * a. [recordEvent()} Events posted to in-memory eventQueue via "recordEvent() (this method)". + * NOTE1: Only remember the most recent MAX_PERSISTENT_QUEUE_SIZE events per queue so we don't eat up all browser memory if we can't send for a very long time. + * NOTE2: We could have another set of "waitingForAck" queues which was also in-memory, but then + * we'd need code to merge two queues on re-tries (since _sendEvents() already merges in previously-queued events before attempting to [re]send) + * Since the send failure case is a rare case anyway, we err on the side of not adding code complexity to deal with it. + * b. [_sendEvents()] Wait "_postFrequency()" milliseconds before attempting actual "_sendEvents()" + * c. Set a timeout for the send which should be the lower of "_postFrquency()" or config.requestTimeout (which defaults to DEFAULT_REQUEST_TIMEOUT). + * This guarantees that only one send attempt per topic is pending at any given time. + * d. Wait for status=200 response ack, and clear event queue when received (see "Edge Cases", below) + * 2. Unsent due to ingestion server problem (5XX response): + * a. Continue gathering events, adding to in-memory eventQueue + * b. Use an exponential back-off strategy by waiting 2^1 = times "_postFrequency()" milliseconds before attempting another "_sendEvents()", as in "Normal" case, above + * c. If the ingestion server responds with another 5XX code, wait 2^2 = 4 times "_postFrequency()" milliseconds before attempting another send, and so on + * d. Continue as in 1c, above + * 3. Unsent due to failed conection or other error: + * a. Continue gathering events, adding to in-memory eventQueue + * b. Wait "_postFrequency()" milliseconds before attempting another "_sendEvents()", as in "Normal" case, above + * c. Continue as in 1c, above + * + * Edge Cases: Here are the small edge-case windows of failure in the in-memory event queue facility: + * 1. If two batches of events are sent out before the first one returns and clears the queue, the second one will try to send any queued events as well. + * For this to happen, we'd have to be in a situation where we're not leaving the page, event sends are taking some time to respond, + * causing us to build up a queue, and then two "sends" are attempted back-to-back by a client calling recordEvent() with postNow=true or flushUnreportedEvents(). + * It's so rare that the code complexity to try and prevent this case is not worth it (synchronous event posting is the most straightforward way to handle it). + * 2. If we send a batch of events, the last of which causes, say, a page turn, and invokes flushUnreportedEvents(), we will never get the ack that the batch made it to the server + * so we just clear the queue and assume that they made it. During empirical testing, events did continue to get sent event after the browser is closed as long as there is a network connection. + * 3. If the ingestion server is down, we would only retry as long as the session is open, and lose all events once the user leaves. Events are usually flushed on page turns + * without the opportunity to see if the send was successful (see 2 above) so if we wanted to preserve events in the case of ingestion server failure, + * we would have to a) keep track of the previous send attempts, b) not flush events on page turn, c) stash the events and the retry state in persistent storage, + * and d) fetch the events from persistent storage and restore the retry state, which we choose not to do in order to avoid all of this complexity for what is probably a rare scenario. + * + * Note: each topic has its own queue, and all of the above logic applies to each individual queue. + * For example, exponential backoff works independently on a per-topic basis, in case the server tells us to back off of one topic, but not another. + * Testing tips: + * 1) A debug message is logged every time a postInterval timer is triggered, but the metricskit logger debug messages are off by default. + * They can be enabled in Web inspector via: metrics.system.logger.setLevel(metrics.system.logger.DEBUG); + * 2) Exponential backoff can be tested by using a debug source with the 'testExpontentialBackoff' flag enabled. + * When this flag is enabled, the metrics queue will skip sending and instead schedule the next retry for _postFrequency() * RETRY_EXPONENT_BASE ^ n milliseconds + * where n is the current retry attempt. (see mt-metricskit docs for more details on using debug sources) + * To see this in action, you can enable debug logs as in (1) above, then set a debug source with testExponentialBackoff=true (and optionally, a lower postFrequency), + * then call recordEvent() to enqueue an event. + * You should see a debug message (which includes a timestamp) after postFrequency milliseconds, and then every postFrequency * 2^n milliseconds thereafter. + * + * @param {Object} topicConfig - a instance of Config class(mt-client-config) which contains the topic defined the Figaro "topic" that this event should be stored under + * @param {Object} eventFields a JavaScript object which will be converted to a JSON string and enqued for sending to Figaro according to the postFrequency schedule + * @param {Boolean} postNow - effectively forces an immediate send, along with any other messages that may be sitting in the queue + * @returns {Promise} + */ +function recordEvent(topicConfig, eventFields, postNow) { + var topic = topicConfig.topic(); + + return topicConfig.disabled().then(function (disabled) { + if (!disabled) { + return topicConfig + .value('postFrequency') + .then(function (postFrequency) { + if (postFrequency === 0) { + postNow = true; + } + // The "pagehide" event was tested and works reliably, so we only need to keep the queue in memory, and expect clients to clear the queue on app close + return _eventQueue.enqueueEvent(topicConfig, eventFields).then(function () { + return postFrequency; + }); + }) + .then(function (postFrequency) { + if (postNow) { + return _eventQueue.sendEvents(CONSTANTS.SEND_METHOD.AJAX, true); + } else if (!_eventQueue.eventQueues[topic].postIntervalToken && _eventQueue.postIntervalEnabled) { + // Schedule the next send if the timer isn't already running + _eventQueue.setTopicPostInterval(topicConfig, postFrequency); + } + }); + } + }); +} + +/** + * Sends any remaining events in the queue, then clears it. + * Events will be sent as individual image pings or as synchronous AJAX requests for iOS (these are the only send methods that actually + * get through during a pagehide event). We expect clients to call this function before the app closes in order to clear the event queues, + * otherwise any remaining events will be lost. We could theoretically add our own event listener so this call happens automatically, + * but single page apps could be using history.pushState which triggers onpagehide, and in those cases they would not + * need to flush until the app is actually closing. + * Note: This is typically called on page / app close when the JS context is about to disappear and thus we will not know if the events made it to the server + * @param {Boolean} appIsExiting - Pass true if events are being flushed due to your app exiting or page going away + * (the send method will be different in order to attempt to post events prior to actual termination) + * @param {String} appExitSendMethod (optional) the send method for how events will be flushed when the app is exiting. + * Possible options are enumerated in the `eventRecorder.SEND_METHOD` object. + * Note: This argument will be ignored if appIsExiting is false. + * @returns {Promise} + */ +function flushUnreportedEvents(appIsExiting, appExitSendMethod) { + if (appIsExiting) { + if (appExitSendMethod === SEND_METHOD.BEACON_SYNCHRONOUS) { + return flushUnreportedEventsSynchronously(); + } + if (reflect.isString(appExitSendMethod) && _eventQueue.objectContainsValue(SEND_METHOD, appExitSendMethod)) { + return _eventQueue.sendEvents(appExitSendMethod, true); + } else if (reflect.isFunction(navigator.sendBeacon)) { + return _eventQueue.sendEvents(CONSTANTS.SEND_METHOD.BEACON, true); + } else { + // iOS browsers do not allow image pings to send onpagehide; use (deprecated) synchronous AJAX in those browsers + if (_isIOS()) { + return _eventQueue.sendEvents(CONSTANTS.SEND_METHOD.AJAX_SYNCHRONOUS, true); + } else { + return _eventQueue.sendEvents(CONSTANTS.SEND_METHOD.IMAGE, true); + } + } + } else { + return _eventQueue.sendEvents(CONSTANTS.SEND_METHOD.AJAX, true); + } +} + +function flushUnreportedEventsSynchronously() { + for (var topic in _eventQueue.eventQueues) { + var topicQueue = _eventQueue.eventQueues[topic]; + var flushConfig = topicQueue.flushConfig; + var resolvedTopicConfig = reflect.extend({}, flushConfig, { + _topic: topic, + topic: function () { + return this._topic; + } + }); + + _eventQueue.sendEventsViaBeacon(resolvedTopicConfig); + } +} + +/* + * src/event_recorder.js + * mt-event-queue + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +/** + * Provides a pre-built delegate to use against the metrics.system.eventRecorder delegate via metrics.system.eventRecorder.setDelegate() + * If you want to use *most* of these methods, but not *all* of them, you can set this delegate and then create your own with whichever few methods you need to + * customize additionally, and then setDelegate() *that* delegate, in order to override those methods. + * @constructor + * @param {Object} kit An object that implements the Kit interface + */ +var QueuedEventRecorder = function QueuedEventRecorder(kit) { + Base.apply(this, arguments); +}; + +QueuedEventRecorder.prototype = Object.create(Base.prototype); + +QueuedEventRecorder.prototype.constructor = QueuedEventRecorder; + +/** + ************************************ PSEUDO-PRIVATE METHODS/IVARS ************************************ + * These functions need to be accessible for ease of testing, but should not be used by clients + */ + +QueuedEventRecorder.prototype._utResetQueue = function _utResetQueue() { + for (var topic in _utQueue().eventQueues) { + _utQueue().resetTopicPostInterval(topic); + } + _utQueue().eventQueues = {}; +}; + +/** + * An implementation of _record method in the parent class. + * @param {Object} topicConfig a config instance for the Figaro "topic" to send to + * @param {Object} eventFields a JavaScript object which will be converted to a JSON string and enqued for sending to Figaro according to the postFrequency schedule. + * @param {Boolean} postNow - effectively forces an immediate send, along with any other messages that may be sitting in the queue + * @returns {Promise} + */ +QueuedEventRecorder.prototype._record = function record(topicConfig, eventFields, postNow) { + return recordEvent(topicConfig, eventFields, postNow); +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +QueuedEventRecorder.prototype.SEND_METHOD = SEND_METHOD; + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +QueuedEventRecorder.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * Sends any remaining events in the queue, then clears it + * This is typically called on page / app close when the JS context is about to disappear and thus we will + * not know if the events made it to the server + * @param {Boolean} appIsExiting - Pass true if events are being flushed due to your app exiting or page going away + * (the send method will be different in order to attempt to post events prior to actual termination) + * @param {String} appExitSendMethod (optional) the send method for how events will be flushed when the app is exiting. + * Possible options are enumerated in the `eventRecorder.SEND_METHOD` object. + * Note: This argument will be ignored if appIsExiting is false. + * @returns {Promise} + */ +QueuedEventRecorder.prototype.flushUnreportedEvents = function flushUnreportedEvents$1(appIsExiting, appExitSendMethod) { + return flushUnreportedEvents.apply(null, arguments); +}; + +/** + * Set event queue related properties for the giving topic + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @param {Object} properties the event queue properties for the topic + * @param {Boolean} properties.anonymous true if sending all events for the topic with credentials omitted(no cookies, no PII fields) + */ +QueuedEventRecorder.prototype.setProperties = function setProperties(topic, properties) { + Object.getPrototypeOf(QueuedEventRecorder.prototype).setProperties.call(this, topic, properties); + _utQueue().setProperties(topic, properties); +}; + +/** + * An implementation of cleanup method in the parent class. + */ +QueuedEventRecorder.prototype.cleanup = function cleanup() { + Object.getPrototypeOf(QueuedEventRecorder.prototype).cleanup.call(this); + this._utResetQueue(); +}; + +/* + * src/immediate_event_recorder.js + * mt-event-queue + * + * Copyright © 2016-2019 Apple Inc. All rights reserved. + * + */ + +/** + * Provides a pre-built delegate to use against the metrics.system.eventRecorder delegate via metrics.system.eventRecorder.setDelegate() + * If you want to use *most* of these methods, but not *all* of them, you can set this delegate and then create your own with whichever few methods you need to + * customize additionally, and then setDelegate() *that* delegate, in order to override those methods. + * @constructor + * @param {Object} kit An object that implements the Kit interface + */ +var ImmediateEventRecorder = function ImmediateEventRecorder(kit) { + Base.apply(this, arguments); +}; + +ImmediateEventRecorder.prototype = Object.create(Base.prototype); +ImmediateEventRecorder.prototype.constructor = ImmediateEventRecorder; + +/** + ************************************ PRIVATE METHODS/IVARS ************************************ + */ + +/** + * An implementation of _record method in the parent class. + * @param {Object} topicConfig a config instance for the Figaro "topic" to send to + * @param eventFields + * @returns {Promise} + */ +ImmediateEventRecorder.prototype._record = function record(topicConfig, eventFields) { + var jsonEventsString = enrichAndSerializeEvent(eventFields); + if (jsonEventsString) { + return Promise.all([metricsUrlForConfig(topicConfig), requestTimeoutForConfig(topicConfig)]).then( + function (outputs) { + var topicUrl = outputs[0]; + var requestTimeout = outputs[1]; + var options = { timeout: requestTimeout }; + if ( + this._topicPropsCache[topicConfig.topic()] && + this._topicPropsCache[topicConfig.topic()].anonymous + ) { + options.withCredentials = false; + } + network.makeAjaxRequest(topicUrl, 'POST', jsonEventsString, null, null, options); + }.bind(this) + ); + } +}; + +/* + * mt-event-queue/index.js + * mt-event-queue + * + * Copyright © 2016-2017 Apple Inc. All rights reserved. + * + */ + +var logger = /*#__PURE__*/ loggerNamed('mt-event-queue'); + +export { QueuedEventRecorder as EventRecorder, ImmediateEventRecorder, environment, logger, network, setPostIntervalEnabled as setEventQueuePostIntervalEnabled }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-delegates-core/dist/mt-metricskit-delegates-core.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-delegates-core/dist/mt-metricskit-delegates-core.esm.js new file mode 100644 index 0000000..b2e713f --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-delegates-core/dist/mt-metricskit-delegates-core.esm.js @@ -0,0 +1,289 @@ +import { MetricsConfig } from '@amp-metrics/mt-client-config'; +import { reflect } from '@amp-metrics/mt-metricskit-utils-private'; + +/* + * src/delegate.js + * mt-metricskit-delegates-core + * + * Copyright © 2022 Apple Inc. All rights reserved. + * + */ + +/** + * Abstract class for delegates. + * All delegate implementations should extend from this class. + * @param {String} topic - Defines the AMP Analytics "topic" for events to be stored under + * @param {Object} platformImpls - A map that include the platform-based components. + * @param {Environment} platformImpls.environment - An Environment that provide the event field accessor for the platform + * @param {EventRecorder} platformImpls.eventRecorder - An EventRecorder that provide the EventRecorder implementation for the platform + * @constructor + */ +var Delegates = function Delegates(topic, platformImpls) { + if (!reflect.isDefinedNonNullNonEmpty(topic) || !reflect.isString(topic)) { + throw new Error('No valid topic was provided to Delegates.'); + } + + this.config = this.getOrCreateConfig(topic); + // TODO change platformImpls as an required argument in the next major version or supporting Typescript + // since every platforms should provide their own impl for the Environment and EventHandler to the Delegates. + if (reflect.isDefinedNonNull(platformImpls)) { + this.environment = platformImpls.environment; + this.eventRecorder = platformImpls.eventRecorder; + } + // A flag to indicate whether using the original execution context when calling the delegate methods. Default is false + // TODO Consider to change this to true by default in the next major release + // because using the execution context of the delegate methods may not necessary as accessing the the delegate methods' execution context is straightforward. + this._useOrginalContextForDelegateFunc = false; +}; + +/** + * Initializes the delegate by setting up the config. + * @param delegates + * @return {Promise} + */ +Delegates.prototype.init = function () { + if (!reflect.isDefinedNonNull(this.environment)) { + throw new Error('No environment was provided to Delegate options.'); + } + + if (!reflect.isDefinedNonNull(this.eventRecorder)) { + throw new Error('No eventRecorder was provided to Delegate options.'); + } + + this.config.environment.setDelegate(this.environment); + this.config.logger.setDelegate(this.logger); + this.config.network.setDelegate(this.network); + + var configSources = reflect.isFunction(this.configSources) ? this.configSources.bind(this) : null; + + return this.config.init(configSources); +}; + +/** + * Merge the provided delegates into the current Delegate. + * NOTE: Any delegates which already exist in the Delegate won't be merged. + * @param {Object} delegates - A key/value map of the system delegates + */ +Delegates.prototype.mergeDelegates = function (delegates) { + var self = this; + for (var key in delegates) { + if (!self[key]) { + self[key] = delegates[key]; + } + } + + this.config.setDelegate(delegates.config); +}; + +/** + * Cleans up resources used by the delegate. + */ +Delegates.prototype.cleanup = function cleanup() { + if (reflect.isFunction(this.eventRecorder.cleanup)) { + this.eventRecorder.cleanup(); + } + + this.config = null; + this.eventRecorder = null; + this.environment = null; +}; + +/** + * Overrides the platform-specific event recorder for the Delegate. + * @param {object} eventRecorderDelegates + * @returns {Delegates} + */ +Delegates.prototype.setEventRecorder = function setEventRecorder(eventRecorderDelegates) { + if (reflect.isDefinedNonNull(eventRecorderDelegates)) { + if (!reflect.isDefinedNonNull(this.eventRecorder)) { + this.eventRecorder = eventRecorderDelegates; + } else { + reflect.setDelegates(this.eventRecorder, eventRecorderDelegates); + this.eventRecorder.setDelegate(eventRecorderDelegates); + } + } + + return this; +}; + +/** + * Overrides the existing environment implementations. + * @param {object} environment + * @return {Delegates} + */ +Delegates.prototype.setEnvironment = function setEnvironment(environment) { + if (!reflect.isDefinedNonNull(this.environment)) { + this.environment = environment; + } else { + var newEnvironment = Object.create(this.environment); + reflect.extend(newEnvironment, environment); + this.environment = newEnvironment; + } + + return this; +}; + +/** + * Overrides the config methods + * @param config + * @return {Delegates} + */ +Delegates.prototype.setConfig = function setConfig(config) { + this.config.setDelegate(config); + + return this; +}; + +/** + * Access the config instance from the child delegates before/during calling Delegates.constructor, + * this method will create new config instance and set it to the Delegates instance and return the config when the config doesn't exist + * @param {String} topic - The topic to create the metrics config instance + * @return {MetricsConfig} + */ +Delegates.prototype.getOrCreateConfig = function getOrCreateConfig(topic) { + if (!reflect.isDefinedNonNull(this.config)) { + this.config = new MetricsConfig(topic); + } + + return this.config; +}; + +/** + * Retrieves the config sources. This method must be implemented by subdelegates. + * @abstract + * @return {Promise} + */ +Delegates.prototype.configSources = function configSources() { + throw new Error('This method should be implemented by subdelegates.'); +}; + +/* + * src/abstract_event_recorder.js + * mt-metricskit-delegates-core + * + * Copyright © 2022 Apple Inc. All rights reserved. + * + */ + +/** + * An abstract event recorder to provide common logic across platform event recorders + * @constructor + */ +function AbstractEventRecorder() { + this._operationPromiseChain = Promise.resolve(); +} + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: when the delegate function is called, it will include an additional final parameter representing the original function that it replaced (the callee would typically name this parameter "replacedFunction"). + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "replacedFunction" parameter will be the previous delegate. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +AbstractEventRecorder.prototype.setDelegate = function setDelegate(delegate) { + return reflect.attachDelegate(this, delegate); +}; + +/** + * Public method to interact with Processors to record an event + * @param {String} topic - an 'override' topic which will override the main topic. + * @param {Promise|Object} eventFields - a Promise/JavaScript object which will be converted to a JSON string and sent to AMP Analytics. + * @return {Promise} + */ +AbstractEventRecorder.prototype.recordEvent = function recordEvent(topic, eventFields) { + var vargs = Array.prototype.slice.call(arguments, 2); + var self = this; + this._operationPromiseChain = this._operationPromiseChain.then(function () { + return Promise.resolve(eventFields).then(function (eventFields) { + return self._recordEvent.apply(self, [topic, eventFields].concat(vargs)); + }); + }); + + return this._operationPromiseChain; +}; + +/** + * Sends any remaining events in the queue, then clears it + * This is typically called on page / app close when the JS context is about to disappear and thus we will + * not know if the events made it to the server + * @param {Boolean} appIsExiting - Pass true if events are being flushed due to your app exiting or page going away + * (the send method will be different in order to attempt to post events prior to actual termination) + * @param {String} appExitSendMethod (optional) the send method for how events will be flushed when the app is exiting. + * Possible options are enumerated in the `eventRecorder.SEND_METHOD` object. + * Note: This argument will be ignored if appIsExiting is false. + * @returns {Promise} + */ +AbstractEventRecorder.prototype.flushUnreportedEvents = function flushUnreportedEvents() { + var args = Array.prototype.slice.call(arguments); + var self = this; + return this._operationPromiseChain.then(function () { + // Reset the promise chain + self._operationPromiseChain = Promise.resolve(); + return self._flushUnreportedEvents.apply(self, args); + }); +}; + +/** + * Abstract recordEvent method + * Subclasses implement this method to handle how to record an event + * @abstract + * @protected + * @param {String} topic - an 'override' topic which will override the main topic. + * @param {Object} eventFields - a JavaScript object which will be converted to a JSON string and sent to AMP Analytics. + * @returns {Promise} + */ +AbstractEventRecorder.prototype._recordEvent = function _recordEvent(topic, eventFields) {}; + +/** + * Abstract flushUnreportedEvents method + * Subclasses implement this method to handle how to flush the cache events + * @abstract + * @protected + * @param {Boolean} appIsExiting - if events are being flushed due to your app exiting (or page going away for web-apps), pass "true". + * This allows MetricsKit to modify its flush strategy to attempt to post events prior to actual termination. + * In cases where appIsExiting==false, the parameter may be omitted. + * @returns {Promise} + */ +AbstractEventRecorder.prototype._flushUnreportedEvents = function _flushUnreportedEvents(appIsExiting) {}; + +export { AbstractEventRecorder, Delegates }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-delegates-web/dist/mt-metricskit-delegates-web.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-delegates-web/dist/mt-metricskit-delegates-web.esm.js new file mode 100644 index 0000000..0772179 --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-delegates-web/dist/mt-metricskit-delegates-web.esm.js @@ -0,0 +1,728 @@ +import { AbstractEventRecorder, Delegates } from '@amp-metrics/mt-metricskit-delegates-core'; +import { EventRecorder as EventRecorder$1, ImmediateEventRecorder, environment, network, logger } from '@amp-metrics/mt-event-queue'; +import { reflect, backoff } from '@amp-metrics/mt-metricskit-utils-private'; + +/* + * src/environment.js + * mt-metricskit-delegates-web + * + * Copyright © 2022 Apple Inc. All rights reserved. + * + */ + +/** + * Provides a pre-built HTML delegate to use against the metrics.system.environment delegate via metrics.system.environment.setDelegate() + * If you want to use *most* of these methods, but not *all* of them, you can set this delegate and then create your own with whichever few methods you need to + * customize additionally, and then setDelegate() *that* delegate, in order to override those methods. + * @constructor + * @param {Config} config (optional) - An instance of Config, which contains the topic and the relevant configurations for the topic. + */ + +function Environment(config) { + this._config = config; +} + +/** + ************************************ PSEUDO-PRIVATE METHODS/IVARS ************************************ + * These functions need to be accessible for ease of testing, but should not be used by clients + */ +Environment.prototype._document = function _document() { + if (typeof document != 'undefined') { + return document; + } else { + throw "metricskit-delegates-html.environment HTML delegate 'document' object not found"; + } +}; + +Environment.prototype._window = function _window() { + if (typeof window != 'undefined') { + return window; + } else { + throw "metricskit-delegates-html.environment HTML delegate 'window' object not found"; + } +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * The name of the browser that generated the event, it only return browser field if "browserMaps" config property exists in the topic config + * Common userAgent format: "Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>" + * This implementation will read the browser identifer from the first item in the extensions part of the userAgent. + * For those browsers which do not store their browser identifier in the first item (can be configured in "specifiedBrowsers" config, see the below example), + * the function will search the defined string in the userAgent to verify if it is the case. + * It will load the browser related configurations from "browserMaps" in the topic. The config looks like + * { + * specifiedBrowsers: string[], // listing the browsers identifiers which does not located in the first item in "extensions" in userAgent. + * browserMap: Record<string, string> // map the browser identifiers to readable string. For example, Edg -> Edge, OPR -> Opera + * } + * @example Safari + * @returns {Promise<string | null> | null} + * @overridable + */ +Environment.prototype.browser = function browser() { + var userAgent = this._window().navigator.userAgent; + + if (!reflect.isDefinedNonNull(this._config) || !reflect.isDefinedNonNullNonEmpty(userAgent)) { + return null; + } + + return this._config.value('browserMaps').then(function (browserConfig) { + if (!reflect.isDefinedNonNull(browserConfig)) { + return null; + } + var specifiedBrowsers = browserConfig.specifiedBrowsers || []; + var browserMap = browserConfig.browserMap || {}; + var browserIdentifier = null; + for (var i = 0; i < specifiedBrowsers.length; i++) { + var specifiedBrowser = specifiedBrowsers[i]; + if (userAgent.indexOf(specifiedBrowser) > -1) { + browserIdentifier = specifiedBrowser; + break; + } + } + if (!reflect.isDefinedNonNull(browserIdentifier)) { + // The Named capturing group: (?<name>...) requires "ECMAScript 2018" + var matches = userAgent.match(/Mozilla\/5.0 \((.*?)\)(\s|$)((.*?)\/(.*?)(\s)\(.*\))?(.*?)\/(.*?)(\s|$)/); + if (reflect.isDefinedNonNullNonEmpty(matches) && matches.length >= 8) { + browserIdentifier = matches[7]; + } + } + + if (reflect.isDefinedNonNull(browserIdentifier)) { + browserIdentifier = browserIdentifier.trim(); + } else { + browserIdentifier = 'unknown'; + } + + var browser = browserMap[browserIdentifier] || browserIdentifier; + return browser; + }); +}; + +/** + * The cookie string, e.g. "iTunes.cookie" (iTunes desktop), "iTunes.cookieForDefaultURL" (HTML iOS), "itms.cookie" (itml app), "document.cookie" (browser) + * NOTE: Callers should override this method if they want to supply a different cookie. + * @overridable + */ +Environment.prototype.cookie = function cookie() { + return this._window().document.cookie; +}; + +/** + * The URL that represents this page. + * Typically this is a "deep link" type URL. + * If no URL is available, this field may be omitted. + * @example "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewGrouping?cc=us&mt=8" + * @returns {String} + * @overridable + */ +Environment.prototype.pageUrl = function pageUrl() { + return this._window().location.href; +}; + +/** + * The URL of the parent page, if the app is embedded in a parent context. + * Typically this is a "deep link" type URL. + * If no URL is available, or if the app is not embedded, this field may be omitted. + * @example "https://www.apple.com/blog/top-tracks.html" + * @returns {String} + * @overridable + * Note: due to iframe sandbox rules, the parent window's location may not be accessible. + * In that case, we fall back to document.referrer, which should be reliable if the app + * within the iframe is a single page app (document.referrer changes on every page turn). + * If the app in the iframe is not a single page app, we will have to persist the + * original referrer from the first page across page turns via e.g. localStorage. + * However, this use case is not currently needed by any client. + */ +Environment.prototype.parentPageUrl = function parentPageUrl() { + var windowObject = this._window(); + var parentWindow = windowObject.parent; + var parentPageUrl; + + if (parentWindow !== windowObject) { + try { + parentPageUrl = parentWindow.location.href; + } catch (e) { + parentPageUrl = this._document().referrer; + } + } + + return parentPageUrl; +}; + +/** + * Pixel multiplier factor + * @example 2 + * @returns {number} + * @overridable + */ +Environment.prototype.pixelRatio = function pixelRatio() { + return this._window().devicePixelRatio; +}; + +/** + * Client screen height in pixels + * @example 568 + * @returns {number} + * @overridable + */ +Environment.prototype.screenHeight = function screenHeight() { + return this._window().screen.height; +}; + +/** + * Client screen width in pixels + * @example 320 + * @returns {number} + * @overridable + */ +Environment.prototype.screenWidth = function screenWidth() { + return this._window().screen.width; +}; + +/** + * Client’s user agent string. If the "app field is not provided, "userAgent may be used to derive the value of the "app field + * @example AppStore/2.0 iOS/8.3 model/iPhone7,2 build/12F70 (6; dt:106) + * @returns {String} + * @overridable + */ +Environment.prototype.userAgent = function userAgent() { + return this._window().navigator.userAgent; +}; + +/** + * App viewport height in pixels. Does not include window “chrome”, status bars, etc. + * Typically only available on desktop windowing systems. + * @example 1920 + * @returns {number/undefined} + * @overridable + */ +Environment.prototype.windowInnerHeight = function windowInnerHeight() { + return this._window().innerHeight; +}; + +/** + * App viewport width in pixels. Does not include window “chrome”, status bars, etc. + * Typically only available on desktop windowing systems. + * @example 1080 + * @returns {number/undefined} + * @overridable + */ +Environment.prototype.windowInnerWidth = function windowInnerWidth() { + return this._window().innerWidth; +}; + +/** + * Height in pixels of containing window, encompassing app viewport as well as window chrome, status bars, etc. + * Typically only available on desktop windowing systems. + * @example 1080 + * @returns {number/undefined} + * @overridable + */ +Environment.prototype.windowOuterHeight = function windowOuterHeight() { + return this._window().outerHeight; +}; + +/** + * Width in pixels of containing window, encompassing app viewport as well as window chrome, status bars, etc. + * Typically only available on desktop windowing systems. + * @example 1920 + * @returns {number/undefined} + * @overridable + */ +Environment.prototype.windowOuterWidth = function windowOuterWidth() { + return this._window().outerWidth; +}; + +/** + * The offset between W3C timing entry timestamps (which are relative to the page lifecycle) and the epoch time + * and the epoch time, in milliseconds + * @return {Number} + * @overridable + * Note: this is only currently used by PerfKit + * TODO: <rdar://problem/44976037> Refactor: Delegates: revisit HTML delegate packaging + */ +Environment.prototype.timeOriginOffset = function timeOriginOffset() { + var returnValue = null; + var performance = this._window().performance; + + if (performance && performance.timing) { + returnValue = performance.timing.navigationStart; + } + + return returnValue; +}; + +/** + * THE FOLLOWING DATA ARE UNAVAILABLE IN A PURE WEB BROWSER CONTEXT, + * BUT MAY BE IMPLEMENTED (VIA POTENTIALLY DIFFERENT APIS) IN VARIOUS HTML WEB VIEW CONTEXTS (iOS vs Desktop vs tvOS) + * THEY ARE LEFT UNIMPLEMENTED FOR CONTEXT-SPECIFIC DELEGATES TO OVERWRITE IF APPLICABLE + */ + +/** + * The app identifier of the binary app + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example "com.apple.appstore" or "com.apple.gamecenter" + * @returns {String} + * @overridable + */ +Environment.prototype.app = function app() {}; + +/** + * The version number of this application + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example "1.0", "5.43", etc. + * @returns {String} + * @overridable + * @defaultimpl navigator.appVersion + */ +Environment.prototype.appVersion = function appVersion() {}; + +/** + * The total data capacity of the system, without regard for what's already been used or not. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @returns {number} + * @overridable + */ +Environment.prototype.capacityData = function capacityData() {}; + +/** + * The total available data capacity of the system. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @returns {number} + * @overridable + */ +Environment.prototype.capacityDataAvailable = function capacityDataAvailable() {}; + +/** + * The total disk capacity of the system, without regard for what's already been used or not. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @returns {number} + * @overridable + */ +Environment.prototype.capacityDisk = function capacityDisk() {}; + +/** + * The total system capacity, without regard for what's already been used or not. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @returns {number} + * @overridable + */ +Environment.prototype.capacitySystem = function capacitySystem() {}; + +/** + * The total available system capacity of the system. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @api public + * @overridable + */ +Environment.prototype.capacitySystemAvailable = function capacitySystemAvailable() {}; + +/** + * Type of internet connection. + * Only applicable to devices + * Beware that users on WiFi may actually be receiving 3G speeds (i.e. if device is tethered to a portable hotspot.) + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example "WiFi, "3G, etc. + * @returns {String} + * @overridable + */ +Environment.prototype.connectionType = function connectionType() {}; + +/** + * The id of this user ("directory service id"). + * This id will get anonymized on the server prior to being saved. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example 659261189 + * @returns {String} + * @overridable + */ +Environment.prototype.dsId = function dsId() {}; + +/** + * The hardware brand of the device. Not required for Apple devices. + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "Samsung", "LG", "Google" + * @returns {String} + */ +Environment.prototype.hardwareBrand = function hardwareBrand() {}; + +/** + * The hardware family of the device + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "iPhone", "Macbook Pro" + * @returns {String} + */ +Environment.prototype.hardwareFamily = function hardwareFamily() {}; + +/** + * The model of the device + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "iPhone10,2", "MacbookPro11,5" + * @returns {String} + */ +Environment.prototype.hardwareModel = function hardwareModel() {}; + +/** + * App that is hosting the storesheet or app + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example com.rovio.AngryBirds + * @returns {String} + * @overridable + */ +Environment.prototype.hostApp = function hostApp() {}; + +/** + * Version of the app that is hosting the storesheet or app + * NO DEFAULT IMPLEMENTATION... HOWEVER: this field is optional + * @example "1.0.1" + * @returns {String} + * @overridable + */ +Environment.prototype.hostAppVersion = function hostAppVersion() { + // Optional field value +}; + +/** + * The name of the OS + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "ios", "macos", "windows" + * @returns {String} + */ +Environment.prototype.os = function os() {}; + +/** + * The build number of the OS + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "15D60", "17E192" + * @returns {String} + */ +Environment.prototype.osBuildNumber = function osBuildNumber() {}; + +/** + * A string array of language IDs, ordered in descending preference + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example ["en-US", "fr-CA"] + * @returns {Array} + */ +Environment.prototype.osLanguages = function osLanguages() {}; + +/** + * The full OS version number + * In ITML, the value can be retrieved via Device.systemVersion + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example "8.2.1" (iOS) "10.10.3" (Desktop) + * @returns {String} + * @overridable + */ +Environment.prototype.osVersion = function osVersion() {}; + +/** + * HTML resources revision number + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example 2C97 or 8.4.0.0.103 + * @returns {String} + * @overridable + */ +Environment.prototype.resourceRevNum = function resourceRevNum() {}; + +/** + * ISO 3166 Country Code. Apps that cannot provide a storeFrontHeader should provide a storeFrontCountryCode instead + * NO DEFAULT IMPLEMENTATION... Either this method or storeFrontHeader must be replaced. + * @example US + * @returns {String} + * @overridable + */ +Environment.prototype.storeFrontCountryCode = function storeFrontCountryCode() {}; + +/** + * The value contained in the X-Apple-Store-Front header value at the time the event is being created. + * NO DEFAULT IMPLEMENTATION... Either this method or storeFrontCountryCode must be replaced. + * @example K143441-1,29 ab:rSwnYxS0 + * @returns {String} + * @overridable + */ +Environment.prototype.storeFrontHeader = function storeFrontHeader() {}; + +/** + * The best supported language for a storefront + * NO DEFAULT IMPLEMENTATION... HOWEVER: this field is optional + * @example en-US + * @returns {String} + * @overridable + */ +Environment.prototype.storeFrontLanguage = function storeFrontLanguage() {}; + +/** + * The type of subscriber this user is. + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED + * @example subscribed, notSubscribed, unknown, needsAuthentication + * @returns {String} + * @overridable + */ +Environment.prototype.userType = function userType() {}; + +/* + * src/event_recorder + * mt-metricskit-delegates-web + * + * Copyright © 2022 Apple Inc. All rights reserved. + * + */ + +/** + * A proxy EventRecorder to bridge the delegates layer to the mt-event-queue lib + * @param eventRecorder - A eventRecorder implementation from the mt-event-queue lib + * @constructor + */ +function EventRecorder(eventRecorder) { + AbstractEventRecorder.call(this); + this._proxyEventRecorder = eventRecorder; + this.SEND_METHOD = eventRecorder.SEND_METHOD; +} + +EventRecorder.prototype = Object.create(AbstractEventRecorder.prototype); +EventRecorder.prototype.constructor = EventRecorder; + +/** + * recordEvent implementation + * This is an implementation for "AbstractEventRecorder._recordEvent" + * @override + * @param topic + * @param eventFields + * @private + */ +EventRecorder.prototype._recordEvent = function _recordEvent(topic, eventFields) { + return this._proxyEventRecorder.recordEvent.apply(this._proxyEventRecorder, arguments); +}; + +/** + * This overrides the same method in AbstractEventRecorder to ignore the pending recorded event when the appExitSendMethod === 'SEND_METHOD.BEACON_SYNCHRONOUS'. + * @param {Boolean} appIsExiting - Pass true if events are being flushed due to your app exiting or page going away + * (the send method will be different in order to attempt to post events prior to actual termination) + * @param {String} appExitSendMethod (optional) the send method for how events will be flushed when the app is exiting. + * Possible options are enumerated in the `eventRecorder.SEND_METHOD` object. + * Note: This argument will be ignored if appIsExiting is false. + * @returns {Promise} + */ +EventRecorder.prototype.flushUnreportedEvents = function flushUnreportedEvents(appIsExiting, appExitSendMethod) { + var self = this; + var args = Array.prototype.slice.call(arguments); + // if this._proxyEventRecorder is an instance of QueuedEventRecorder and the callers wanted to flush events synchronously, ignore the pending events + if ( + reflect.isDefinedNonNull(this._proxyEventRecorder.SEND_METHOD) && + appExitSendMethod === this._proxyEventRecorder.SEND_METHOD.BEACON_SYNCHRONOUS + ) { + return this._proxyEventRecorder.flushUnreportedEvents.apply(this._proxyEventRecorder, arguments); + } else { + return this._operationPromiseChain.then(function () { + // Reset the promise chain + self._operationPromiseChain = Promise.resolve(); + return self._proxyEventRecorder.flushUnreportedEvents.apply(self._proxyEventRecorder, args); + }); + } +}; + +/** + * Sends any remaining events in the queue + * This is an implementation for "AbstractEventRecorder._flushUnreportedEvents" + * @override + */ +EventRecorder.prototype._flushUnreportedEvents = function _flushUnreportedEvents() { + return this._proxyEventRecorder.flushUnreportedEvents.apply(this._proxyEventRecorder, arguments); +}; + +/** + * The methodology being used to send batches of events to the server + * This field should be hardcoded in the client based on what method it is using to encode and send its events to Figaro. + * The three typical values are: + * "itms" - use this value when/if JavaScript code enqueues events for sending via the "itms.recordEvent()" method in ITML. + * "itunes" - use this value when/if JavaScript code enqueues events by calling the "iTunes.recordEvent()" method in Desktop Store apps. + * "javascript" - use this value when/if JavaScript code enqueues events for sending via the JavaScript eventQueue management. This is typically only used by older clients which don't have the built-in functionality of itms or iTunes available to them. + * DEFAULT implementation: console.debug() + * @example "itms", "itunes", "javascript" + * @returns {String} + * @overridable + */ +EventRecorder.prototype.sendMethod = function sendMethod() { + return this._proxyEventRecorder.sendMethod.apply(this._proxyEventRecorder, arguments); +}; + +/** + * Set event queue related properties for the giving topic + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @param {Object} properties the event queue properties for the topic + * @param {Boolean} properties.anonymous true if sending all events for the topic with credentials omitted(no cookies, no PII fields) + */ +EventRecorder.prototype.setProperties = function setProperties(topic, properties) { + return this._proxyEventRecorder.setProperties.apply(this._proxyEventRecorder, arguments); +}; + +/** + * clean resources of event recorder + * Subclasses implement this method to handle how to clean resources + * @returns {Promise} returns a Promise if the cleanup will asynchronously execute or undefined for synchronously executing + */ +EventRecorder.prototype.cleanup = function cleanup() { + return this._proxyEventRecorder.cleanup.apply(this._proxyEventRecorder, arguments); +}; + +/* + * src/web_delegate.js + * mt-metricskit-delegates-web + * + * Copyright © 2022 Apple Inc. All rights reserved. + * + */ + +/** + * Delegate for providing access to the "canned" web MetricsKit delegates. + * If further modification of these delegates is required, clients may pass in delegate options to override any of the fields within any delegate. + * @constructor + * @param {String} topic - Defines the AMP Analytics "topic" for events to be stored under + * @param {Object} delegateOptions (optional) - Options that can be passed to either add additional delegates or to extend/override existing ones. + */ +var WebDelegates = function WebDelegates(topic, delegateOptions) { + var config = this.getOrCreateConfig(topic); + Delegates.call(this, topic, { + environment: new Environment(config), + eventRecorder: new EventRecorder(new EventRecorder$1(config)) + }); + + this.immediateEventRecorder = new EventRecorder(new ImmediateEventRecorder(config)); + this.network = null; + this.logger = null; + + if (delegateOptions) { + this.mergeDelegates(delegateOptions); + + if (delegateOptions.environment) { + this.setEnvironment(delegateOptions.environment); + } + + if (delegateOptions.eventRecorder) { + this.setEventRecorder(delegateOptions.eventRecorder); + } + + if (delegateOptions.network) { + this.setNetwork(delegateOptions.network); + } + + if (delegateOptions.logger) { + this.setLogger(delegateOptions.logger); + } + + /** + * TODO: We are temporarily setting this configUrl() delegate on the config to avoid breaking changes. + * In the next major release, we will remove this and just fetch the config from + * the delegateOptions.config.url directly instead of reading it from + * this config method (defaulting to https://xp.apple.com/config/1/report/<topic>). + */ + if (delegateOptions.config) { + if (delegateOptions.config.url) { + this.config.setDelegate({ + configUrl: delegateOptions.config.url + }); + } + this.setConfig(delegateOptions.config); + } + } +}; + +/** + * Inherit from the base Delegate class + */ +WebDelegates.prototype = Object.create(Delegates.prototype); +WebDelegates.prototype.constructor = WebDelegates; + +/** + * Sets the environment for the web-specific event queue as well as setting it on the delegate itself. + * @param {Object} environment + * @returns {WebDelegates} + */ +WebDelegates.prototype.setEnvironment = function (environment$1) { + environment.setDelegate(environment$1); + return Delegates.prototype.setEnvironment.call(this, environment$1); +}; + +/** + * Sets the network for the web-specific event queue as well as setting it on the delegate itself. + * @param {Object} network + * @returns {WebDelegates} + */ +WebDelegates.prototype.setNetwork = function (network$1) { + if (network$1) { + this.network = network$1; + network.setDelegate(network$1); + } + return this; +}; + +/** + * Sets the logger for the web-specific event queue as well as setting it on the delegate itself. + * @param {Object} logger + * @returns {WebDelegates} + */ +WebDelegates.prototype.setLogger = function (logger$1) { + if (logger$1) { + this.logger = logger$1; + logger.setDelegate(logger$1); + } + return this; +}; + +/** + * Sets the immediate event recorder for the delegate. + * Note: Immediate event recorders are specific to web delegates. + * @param {Object} immediateEventRecorder + * @returns {WebDelegates} + */ +WebDelegates.prototype.setImmediateEventRecorder = function (immediateEventRecorder) { + if (immediateEventRecorder) { + var newImmediateEventRecorder = Object.create(this.immediateEventRecorder); + Object.assign(newImmediateEventRecorder, immediateEventRecorder); + this.immediateEventRecorder = newImmediateEventRecorder; + } + return this; +}; + +/** + * @returns {Object} The config sources. + */ +WebDelegates.prototype.configSources = function configSources() { + var self = this; + + return new Promise(function (resolve, reject) { + var onFetchSuccess = function onFetchSuccess(response) { + try { + var configObject = JSON.parse(response); + self.config.setCachedSource(configObject); + self.config.setServiceSource(configObject); // TODO: Deprecated + resolve(configObject); + } catch (error) { + onFetchFailure(error); + } + }; + + var onFetchFailure = function onFetchFailure(error) { + self.config.setCachedSource(self.config.cachedSource()); + reject(error); + }; + + var configUrlPromise = Promise.resolve(self.config.configUrl()); + + configUrlPromise + .then(function (configUrl) { + backoff.exponentialBackoff( + self.config.network.makeAjaxRequest.bind(self.config.network, configUrl, 'GET', null), + onFetchSuccess, + onFetchFailure + ); + }) + .catch(onFetchFailure); + }); +}; + +export { Environment, WebDelegates }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-processor-clickstream/dist/mt-metricskit-processor-clickstream.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-processor-clickstream/dist/mt-metricskit-processor-clickstream.esm.js new file mode 100644 index 0000000..517960f --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-processor-clickstream/dist/mt-metricskit-processor-clickstream.esm.js @@ -0,0 +1,4045 @@ +import { reflect as reflect$1, string as string$1, cookies, eventFields, delegatesInfo, keyValue, network } from '@amp-metrics/mt-metricskit-utils-private'; +import Constraints, { connectConstraintConfig } from '@amp-metrics/mt-client-constraints'; +import { system, eventHandlers } from '@amp-metrics/ae-client-kit-core'; +import { loggerNamed } from '@amp-metrics/mt-client-logger-core'; + +var info = { version: '8.6.3', name: 'mt-metricskit-processor-clickstream' }; + +/* + * src/metrics/utils/delegate_extension.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2016 Apple Inc. All rights reserved. + * + */ +/* + * Delegate info functions to be used by _utils.reflect.attachDelegate + */ +var delegateInfo = { + /** + * Returns the name of this repository + * Used by _utils.reflect.attachDelegate to create + * a delegate dependence tree for the optional + * base field, xpDelegatesInfo + * @returns {String} Name of this repository + */ + mtName: function () { + return info.name; + }, + /** + * Returns version of this repo + * Used by _utils.reflect.attachDelegate to create + * a delegate dependence tree for the optional + * base field, xpDelegatesInfo + * @returns {String} Version of this repo + */ + mtVersion: function () { + return info.version; + } +}; + +/** + * Delegate-related functions (intended to eventually contain + * all needed functions to be delegateable, like 'setDelegate') + * @constructor + */ +var delegateExtension = { + /** + * Adds delegate info methods to the target (delegateable) object + * @param {Object} targetObj Delegateable object to attach info methods to + */ + attachDelegateInfo: function (targetObj) { + reflect$1.extend(targetObj, delegateInfo); + } +}; + +/* + * src/merics/config.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015-2016 Apple Inc. All rights reserved. + * + */ + +/** + * Extend the metrics config instance with convenience functions and delegate info + * This should be done for every config instance that MetricsKit uses: + * once for the default config, and once for the topic-specific config if metrics.init() is used + * @param {Config} configInstance + */ +function initializeConfig(configInstance) { + connectConstraintConfig(configInstance); + delegateExtension.attachDelegateInfo(configInstance); +} + +function cleanupConfig(topicConfig) { + topicConfig.cleanup(); +} + +/* + * src/metrics/utils/constants.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2019 Apple Inc. All rights reserved. + * + */ + +var BASE_FIELDS$1 = [ + 'constraintProfile', + 'constraintProfiles', + 'clientId', + 'isSignedIn', + 'page', + 'pageContext', + 'pageDetails', + 'pageId', + 'pageType', + 'xpVersionMetricsKit', + 'xpDelegatesInfo' +]; + +var ENVIRONMENT_REQUIRED_FIELDS = [ + 'capacityData', + 'capacityDataAvailable', + 'capacityDisk', + 'capacitySystem', + 'capacitySystemAvailable', + 'dsId', + 'hostApp', + 'pageUrl', + 'pixelRatio', + 'userType', + 'windowInnerHeight', + 'windowInnerWidth', + 'windowOuterHeight', + 'windowOuterWidth' +]; + +var ENVIRONMENT_OPTIONAL_FIELDS = [ + 'browser', + 'consumerId', + 'consumerNs', + 'hostAppVersion', + 'parentPageUrl', + 'userId', + 'xpUserIdSyncState', + 'xpAccountsMatch' +]; + +// These constants are used internally but not exposed publicly +var constants = { + METRICS_KIT_BASE_FIELDS: BASE_FIELDS$1.concat(ENVIRONMENT_REQUIRED_FIELDS, ENVIRONMENT_OPTIONAL_FIELDS), + // Used to remove the fields which are not base field in MetricsKit + IGNORED_BASE_FIELDS: [ + 'osLanguages' // only available in "enter" event handler + ], + REQUIRED_ENVIRONMENT_FIELD_NAMES: ENVIRONMENT_REQUIRED_FIELDS.concat('connectionType'), + OPTIONAL_ENVIRONMENT_FIELD_NAMES: ENVIRONMENT_OPTIONAL_FIELDS.concat(['clientId', 'cookie', 'osLanguages']) // This is an optional field in MetricsKit, but a required field in client-kit-core +}; + +/* + * src/metrics/system/environment.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +var Environment = system.Environment; +var exceptionString$1 = string$1.exceptionString; +var REQUIRED_ENVIRONMENT_FIELD_NAMES = constants.REQUIRED_ENVIRONMENT_FIELD_NAMES; +var OPTIONAL_ENVIRONMENT_FIELD_NAMES = constants.OPTIONAL_ENVIRONMENT_FIELD_NAMES; +var _prototypeInitialized = false; +var noOp = function () {}; + +/** + * Provides a set of environment-specific (platform-specific) functions which can be individually overridden for the needs + * of the particular environment, or replaced en masse by providing a single replacement environment delegate object + * The functionality in this class is typically replaced via a delegate. + * DEFAULT implementation: console logging + * @see setDelegate + * @delegatable + * @constructor + */ +var MetricsKitEnvironment = function MetricsKitEnvironment() { + Environment.apply(this, arguments); + if (!_prototypeInitialized) { + _prototypeInitialized = true; + REQUIRED_ENVIRONMENT_FIELD_NAMES.forEach(function (fieldName) { + MetricsKitEnvironment.prototype[fieldName] = function () { + throw exceptionString$1('metrics.system.environment', fieldName); + }; + }); + + OPTIONAL_ENVIRONMENT_FIELD_NAMES.forEach(function (fieldName) { + MetricsKitEnvironment.prototype[fieldName] = noOp; + }); + } +}; + +MetricsKitEnvironment.prototype = new Environment(); +MetricsKitEnvironment.prototype.constructor = MetricsKitEnvironment; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +MetricsKitEnvironment.prototype.setDelegate = function setDelegate(delegate) { + // This is a bit of a cheat, but it allows us to not force our users to have a whole delegate just for cookies... + // we let them merge it in with their environment delegate: + cookies.setDelegate(delegate); + return reflect$1.attachDelegate(this, delegate); +}; + +/* + * src/metrics/system/event_recorder.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Provides a replaceable "recordEvent" function to enqueue events to be sent to the metrics server. + * The functionality in this class is typically replaced via a delegate. + * DEFAULT implementation: error logged via logger. + * @see setDelegate + * @delegatable + * @constructor + */ +var EventRecorder = function EventRecorder() {}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +EventRecorder.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Enqueues a JSON an event as JSON + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @param {Promise|Object} eventFields a Promise/JavaScript object which will be converted to a JSON string and enqued for sending to Figaro according to the postFrequency schedule. + * @returns {Object} the recorded event, or "null" if no object was recorded (e.g. if "eventFields" is null, or "disabled" is true, eventFields.eventType is one of the blacklistedEvents, etc.) + * @overridable + */ +EventRecorder.prototype.recordEvent = function recordEvent(topic, eventFields) { + // Don't wrap this in a helper function or the backtrace won't be as nice. + throw string$1.exceptionString('metrics.system.event_recorder', 'recordEvent'); +}; + +/** + * The methodology being used to send batches of events to the server + * This field should be hardcoded in the client based on what method it is using to encode and send its events to Figaro. + * The three typical values are: + * "itms" - use this value when/if JavaScript code enqueues events for sending via the "itms.recordEvent()" method in ITML. + * "itunes" - use this value when/if JavaScript code enqueues events by calling the "iTunes.recordEvent()" method in Desktop Store apps. + * "javascript" - use this value when/if JavaScript code enqueues events for sending via the JavaScript eventQueue management. This is typically only used by older clients which don't have the built-in functionality of itms or iTunes available to them. + * NO DEFAULT IMPLEMENTATION... THIS METHOD MUST BE REPLACED + * @example "itms", "itunes", "javascript" + * @returns {String} + * @overridable + */ +EventRecorder.prototype.sendMethod = function sendMethod() { + // Don't wrap this in a helper function or the backtrace won't be as nice. + throw string$1.exceptionString('metrics.system.event_recorder', 'sendMethod'); +}; + +/** + * Sends any remaining events in the queue, then clears it + * NO DEFAULT IMPLEMENTATION... THIS METHOD SHOULD BE REPLACED IF THE DELEGATE ENVIRONMENT SUPPORTS IT + * @param {Boolean} appIsExiting - if events are being flushed due to your app exiting (or page going away for web-apps), pass "true". + * This allows MetricsKit to modify its flush strategy to attempt to post events prior to actual termination. + * In cases where appIsExiting==false, the parameter may be omitted. + */ +EventRecorder.prototype.flushUnreportedEvents = function flushUnreportedEvents(appIsExiting) {}; + +/* + * src/metrics/system/logger.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ +var logger = loggerNamed('mt-metricskit-processor-clickstream'); + +/* + * src/metrics/system/index.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +var System = function System() { + this.environment = new MetricsKitEnvironment(); + this.eventRecorder = new EventRecorder(); + this.logger = logger; + + for (var key in this) { + delegateExtension.attachDelegateInfo(this[key]); + } +}; + +/* + * src/utils/metrics_data.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +/** + * + * @param props + * @param props.processor + * @param props.eventMetricsDataPromise + * @constructor + */ +var MetricsData = function MetricsData(props) { + // @private + this._processor = props.processor; + // @private + this._eventMetricsDataPromise = props.eventMetricsDataPromise; +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +MetricsData.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +MetricsData.prototype.toJSON = function toJSON() { + return this._eventMetricsDataPromise.catch( + function (err) { + this._processor.system.logger.error('An error occurred when generating Metrics Data. Error: \n' + err); + return null; // return null to ignore the event + }.bind(this) + ); +}; + +/** + * Enqueues the event data to the topic queue + * @param {String} topic defines the Figaro "topic" that this event should be stored under + * @returns {Promise} a Promise that includes the recorded event or "null" if no object was recorded (e.g. if "eventFields" is null, or "disabled" is true, eventFields.eventType is one of the blacklistedEvents, etc.) + */ +MetricsData.prototype.recordEvent = function recordEvent(topic) { + var vargs = Array.prototype.slice.call(arguments, 1); + return this._processor.system.eventRecorder.recordEvent.apply( + this._processor.system.eventRecorder, + [topic, this.toJSON()].concat(vargs) + ); +}; + +/* + * src/metrics/event_handlers/click_stream_event_handler.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +var exceptionString = string$1.exceptionString; +var ClickStreamEventHandler = function ClickStreamEventHandler(clickstreamProcessor) { + // @private + this._processor = clickstreamProcessor; +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple PerfKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @return {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +ClickStreamEventHandler.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + * @overridable + * NOTE: this method must be overridden by the sub class + */ +ClickStreamEventHandler.prototype.knownFields = function knownFields() { + throw exceptionString('ClickStreamEventHandler', 'knownFields'); +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + * NOTE: this method must be overridden by the sub class + */ +ClickStreamEventHandler.prototype.eventType = function (callerSuppliedEventFields) { + throw exceptionString('ClickStreamEventHandler', 'eventType'); +}; + +/** + * All of the various eventHandlers invoke this method to generate their metrics data + * The data is a simple map object (dictionary) with all the fields required by Figaro for that event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * This function expects to be called with the correct context (eg base.processMetricsData.apply(this, arguments)) + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * This method will check for any disabled/blacklisted events or fields, add base fields, and process/merge all data + * @returns {Object} key/value pairs of all "page" fields + "base" fields required by Figaro + * WARNING: May return "null" if metrics and/or the specific eventType for this handler is disabled, or on error. + */ +ClickStreamEventHandler.prototype.processMetricsData = function processMetricsData( + pageId, + pageType, + pageContext /*, callerSuppliedEventFieldsMapN(varargs) */ +) { + var callArguments = arguments; + var callerSuppliedEventFieldsMapsArray = Array.prototype.slice.call(callArguments, 3); + var eventType = this.eventType(callerSuppliedEventFieldsMapsArray); + var config = this._processor.config; + var constraints = this._processor._constraints; + var logger = this._processor.system.logger; + + var eventMetricsDataPromise = config + .metricsDisabledOrBlacklistedEvent(eventType) + .then(function (disabled) { + if (disabled) { + throw 'event was disabled'; + } + }) + .then( + function () { + var includeBaseFields = + typeof this.mtIncludeBaseFields == 'function' ? this.mtIncludeBaseFields() : true; + var baseEventFields = null; + if (includeBaseFields) { + // Get the base fields from the base event handler + var baseEventHandler = this._processor.eventHandlers.base; + baseEventFields = baseEventHandler.metricsData.apply(baseEventHandler, callArguments); + } else { + baseEventFields = {}; + } + + return baseEventFields; + }.bind(this) + ) + .then( + function (baseMetricsFields) { + var valueHandlerTasks = []; + callerSuppliedEventFieldsMapsArray = [baseMetricsFields].concat(callerSuppliedEventFieldsMapsArray); + var eventFieldPromises = eventFields.processMetricsData( + this, + this.knownFields(), + true, + callerSuppliedEventFieldsMapsArray + ); + var metricsData = {}; + Object.keys(eventFieldPromises).forEach(function (field) { + var fieldValue = eventFieldPromises[field]; + var valueHandler = Promise.resolve(fieldValue).then(function (value) { + metricsData[field] = value; + }); + valueHandlerTasks.push(valueHandler); + }); + + return Promise.all(valueHandlerTasks).then(function () { + return metricsData; + }); + }.bind(this) + ) + .then(function (eventFields) { + return constraints.applyConstraintTreatments(eventFields); + }) + .then(function (eventFields) { + return config.removeBlacklistedFields(eventFields); + }) + .then(function (eventFields) { + return config.applyDeRes(eventFields); + }) + .catch( + function (e) { + logger.error( + 'MetricsKit: Unable to generate the event (' + + this.eventType(callerSuppliedEventFieldsMapsArray) + + ') for the topic ' + + this._processor.config.topic() + + ', due to ' + + e + ); + return null; + }.bind(this) + ); + + return new MetricsData({ + processor: this._processor, + eventMetricsDataPromise: eventMetricsDataPromise + }); +}; + +/* + * src/metrics/event_handlers/account.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Account = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Account.prototype = Object.create(ClickStreamEventHandler.prototype); +Account.prototype.constructor = Account; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Account.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @example a "hint", "related" click, "filter" click, etc. + * If this event is representing a plain typed account, this field's value may be null + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}; someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Account.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Account.prototype.knownFields = function knownFields() { + var knownFields = ['eventType', 'eventVersion', 'type']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Account.prototype.eventType = function (callerSuppliedEventFields) { + return 'account'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Account.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/* + * src/metrics/event_handlers/base.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +var BASE_FIELDS = constants.METRICS_KIT_BASE_FIELDS; +var IGNORED_BASE_FIELDS = constants.IGNORED_BASE_FIELDS; +var Base$1 = eventHandlers.Base; + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the "base" fields common to all metrics events. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var ClickstreamProcessorBase = function (clickstreamProcessor) { + Base$1.apply(this, arguments); +}; + +ClickstreamProcessorBase.prototype = Object.create(Base$1.prototype); +ClickstreamProcessorBase.prototype.constructor = ClickstreamProcessorBase; + +/** + * The active environment class + * @see src/metrics/system/Environment + * @return {Environment} + */ +ClickstreamProcessorBase.prototype.environment = function environment() { + return this._processor.system.environment; +}; + +/** + * The active eventRecorder + * @see src/metrics/system/eventRecorder + * @return {Object} an eventRecorder + */ +ClickstreamProcessorBase.prototype.eventRecorder = function eventRecorder() { + return this._processor.system.eventRecorder; +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +ClickstreamProcessorBase.prototype.knownFields = function knownFields() { + var parentKnownFields = Base$1.prototype.knownFields.call(this); + if (IGNORED_BASE_FIELDS && IGNORED_BASE_FIELDS.length > 0) { + parentKnownFields = parentKnownFields.slice(); + IGNORED_BASE_FIELDS.forEach(function (ignoredField) { + var ignoredFieldIndex = parentKnownFields.indexOf(ignoredField); + if (ignoredFieldIndex > -1) { + parentKnownFields.splice(ignoredFieldIndex, 1); + } + }); + } + + return parentKnownFields.concat(BASE_FIELDS); +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Creates a simple map object (dictionary) with all the "base" fields required by Figaro + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each *known* field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.base.metricsData(appData.pageId, appData.pageType, appData.pageContext, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}; someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns {Promise} A Promise that with key/value pairs of all "page" fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +ClickstreamProcessorBase.prototype.metricsData = function metricsData( + pageId, + pageType, + pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/ +) { + var metricsData = {}; + var valueTasks = []; + var callerSuppliedEventFieldsMapsArray = Array.prototype.slice.call(arguments, 3); + var utils = this._processor.utils; + + return this._processor.config + .value('metricsBase') + .then( + function (configBaseFields) { + if (configBaseFields) { + callerSuppliedEventFieldsMapsArray.push(configBaseFields); + } + var eventFieldPromises = utils.eventFields.processMetricsData( + this, + this.knownFields(), + pageId, + pageType, + pageContext, + callerSuppliedEventFieldsMapsArray + ); + Object.keys(eventFieldPromises).forEach(function (field) { + var fieldValue = eventFieldPromises[field]; + var valueHandler = Promise.resolve(fieldValue).then(function (value) { + metricsData[field] = value; + }); + valueTasks.push(valueHandler); + }); + + return valueTasks; + }.bind(this) + ) + .then(function (valueTasks) { + return Promise.all(valueTasks).then(function () { + return metricsData; + }); + }); +}; + +// ********************* ACCESSOR FUNCTIONS ********************* +/** + * We create accessor functions for every data field because: + * 1. Cleans/simplifies all methods that use it. + * 2. Facilitates writing test case shims + * 3. Allows specific feature suppliers to be overridden (via setDelegate())) + */ + +// Generate the metricskit specified base field accessors +// NOTE: dynamically generate environment based accessors before the customized accessors to make these accessors to be overridable by the customized accessors. +BASE_FIELDS.forEach(function (fieldName) { + ClickstreamProcessorBase.prototype[fieldName] = function (callerSuppliedEventFields) { + var environment = this._processor.system.environment; + return (callerSuppliedEventFields && callerSuppliedEventFields[fieldName]) || environment[fieldName](); + }; +}); + +/** + * The name of the constraint profile used to apply constraints to fields within an event. + * @example "strict" + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {Promise|string} + * @overridable + */ +ClickstreamProcessorBase.prototype.constraintProfile = function constraintProfile(callerSuppliedEventFields) { + var config = this._processor.config; + return (callerSuppliedEventFields && callerSuppliedEventFields.constraintProfile) || config.constraintProfile(); +}; + +/** + * The names of the constraint profiles used to apply constraints to fields within an event. + * NOTE: This method returns the constraint profiles that are used for Constraints v2 syntax. The constraintProfile() returns the profile that is used for Constraints v1 syntax + * @example "[strict]" + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {Promise|string} + * @overridable + */ +ClickstreamProcessorBase.prototype.constraintProfiles = function constraintProfiles(callerSuppliedEventFields) { + var config = this._processor.config; + return (callerSuppliedEventFields && callerSuppliedEventFields.constraintProfiles) || config.constraintProfiles(); +}; + +/** + * A unique identifier for each event + * @returns {String} + * @overridable + */ +ClickstreamProcessorBase.prototype.clientEventId = function clientEventId(callerSuppliedEventFields) { + var eventId = callerSuppliedEventFields && callerSuppliedEventFields.clientEventId; + if (!eventId) { + if (string$1.cryptoRandomBase62String) { + eventId = string$1.cryptoRandomBase62String(true); + } + if (!eventId) { + eventId = string$1.uuid(); + } + } + return eventId; +}; + +/** + * Return the value of the "xp_ci" cookie + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {Promise|String} the value of the "xp_ci" cookie + * @overridable + */ +ClickstreamProcessorBase.prototype.clientId = function clientId(callerSuppliedEventFields) { + var clientId; + var config = this._processor.config; + + if (callerSuppliedEventFields && callerSuppliedEventFields.clientId) { + clientId = callerSuppliedEventFields.clientId; + } else if (this._processor.system.environment.clientId()) { + clientId = this._processor.system.environment.clientId(); + } else { + clientId = config.value('ignoreClientIdCookie').then(function (ignoreClientIdCookie) { + if (!ignoreClientIdCookie) { + return cookies.get('xp_ci'); + } + return undefined; + }); + } + + return clientId; +}; + +/** + * Whether or not the user is signed in + * @example true, false + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {boolean} true if the user is signed in (app has access to dsid), false if not + * @overridable + */ +ClickstreamProcessorBase.prototype.isSignedIn = function isSignedIn(callerSuppliedEventFields) { + return callerSuppliedEventFields && 'isSignedIn' in callerSuppliedEventFields + ? callerSuppliedEventFields.isSignedIn + : !!this.dsId(callerSuppliedEventFields); +}; + +/** + * A unique descriptor for the page. + * Usually [pageType_pageId] + * If no pageId exists, "page" may be a textual identifier (e.g. "NewStation_Genres", "LearnMore_HD"). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), + * then this field would be generated by the client. + * @example Genre_168577 + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {Promise|String} a unique descriptor for the page + * @overridable + */ +ClickstreamProcessorBase.prototype.page = function page(callerSuppliedEventFields) { + if (callerSuppliedEventFields) { + if (callerSuppliedEventFields.page) { + return callerSuppliedEventFields.page; + } else if (this.pageType(callerSuppliedEventFields) && this.pageId(callerSuppliedEventFields)) { + var config = this._processor.config; + var pageType = this.pageType(callerSuppliedEventFields); + var pageId = this.pageId(callerSuppliedEventFields); + + return config.value('compoundSeparator').then(function (separator) { + return pageType + separator + pageId; + }); + } + } else { + throw 'No data provided to event_handlers/base page function'; + } +}; + +/** + * The context within which a page is viewed. Contexts usually provide independent streams of activity. + * Many implementations return the tab that a user is currently on. + * @example + * iTunes Desktop: "InTheStore" or "Main" + * iOS iTunes apps: tab name (e.g. "Featured", "TopCharts", etc) + * iOS Store sheets: "Sheet" (e.g. AppStore page launched as a sheet within Mail, Springboard’s "Near Me", Maps’ "Nearby Apps") + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {String} the context within which a page is viewed. Contexts usually provide independent streams of activity + * @overridable + */ +ClickstreamProcessorBase.prototype.pageContext = function pageContext(callerSuppliedEventFields) { + return callerSuppliedEventFields && callerSuppliedEventFields.pageContext; +}; + +/** + * IMPORTANT: This field cannot be deduced by this class, so either: + * a. this method needs to be overridden by the caller, + * b. the caller should pass this in as part of the key/value in the "callerSuppliedEventFieldsMapN(varargs)" parameter on the metricsData() call + * User-readable details describing page. + * May contain localized values (e.g. "Alicia Keys-Girl On Fire", "MusicMain"). + * Max length 25 characters. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), + * then this field would be generated by the client, if needed. + * @example "Top Paid iPhone Apps_Mobile Software Applications" + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {String} user-readable details describing page + * @overridable + */ +ClickstreamProcessorBase.prototype.pageDetails = function pageDetails(callerSuppliedEventFields) { + return callerSuppliedEventFields && callerSuppliedEventFields.pageDetails; +}; + +/** + * IMPORTANT: This field cannot be deduced by this class, so either: + * a. this method needs to be overridden by the caller, + * b. the caller should pass this in as part of the key/value in the "callerSuppliedEventFieldsMapN(varargs)" parameter on the metricsData() call + * String ID of the page’s content. + * Used to concatenate with the "pageType" param using the "compoundSeparator" config value (typically "_") to produce "page" values of the form: pageType_pageId + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally the identifier in some persistent store. + * Search Results pages may have a pageId which refers to their specific dataSet, otherwise known as dataSetId. + * If the page is better identified by a descriptive string rather than a content ID, this field may be hard-coded, but it should be unique within the set of pages displayable by the app + * the descriptive string may appear in the "page" field instead. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), + * then this field would be generated by the client. + * @example 168577, 1, "Welcome", etc. + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {String} string ID of the page’s content + * @overridable + */ +ClickstreamProcessorBase.prototype.pageId = function pageId(callerSuppliedEventFields) { + // TODO:KBERN: add screamer here if this is not provided? + return callerSuppliedEventFields && callerSuppliedEventFields.pageId; +}; + +/** + * IMPORTANT: This field cannot be deduced by this class, so either: + * a. this method needs to be overridden by the caller, + * b. the caller should pass this in as part of the key/value in the "callerSuppliedEventFieldsMapN(varargs)" parameter on the metricsData() call + * Name for the group of pages this page is (e.g. "Album" or "Grouping"). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), + * then this field would be generated by the client. + * Used to concatenate with the "pageId" param using the "compoundSeparator" config value (typically "_") to produce "page" values of the form: pageType_pageId + * @example "Genre", "Album", "Grouping", "Picker", "Recommendations", "Feed", "Search", "Subscribe", etc.) + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {String} name for the group of pages this page is (e.g. "Album" or "Grouping"). + * @overridable + */ +ClickstreamProcessorBase.prototype.pageType = function pageType(callerSuppliedEventFields) { + // TODO:KBERN: add screamer here if this is not provided? + return callerSuppliedEventFields && callerSuppliedEventFields.pageType; +}; + +/** + * The percentage, from 0 to 1, that an item should be on screen before being considered impressionable + * This field should be based on the client's most recent config value of "viewablePercentage". + * This is valuable for problem analysis because it indicates if and how clients are honoring the "viewablePercentage" value + * they were supplied with. + * This cannot be a "passthrough" field, because it can change (via new config) during program execution, so the value + * in effect at event creation time is what is needed. + * @example 0.5 + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {Promise|number} the percentage, from 0 to 1, that an item should be on screen before being considered impressionable + * @overridable + */ +ClickstreamProcessorBase.prototype.xpViewablePercentage = function xpViewablePercentage(callerSuppliedEventFields) { + var config = this._processor.config; + return ( + (callerSuppliedEventFields && callerSuppliedEventFields.xpViewablePercentage) || + config.value('impressions.viewablePercentage') + ); +}; + +/** + * The version of MetricsKit being used + * Pulled from the version in the package.json via + * the dynamically created info.js + * @example "0.1.3", "2.0.1" + * @returns {String} version of MetricsKit being used + * @overridable //this should be changed to not be overridable + */ +ClickstreamProcessorBase.prototype.xpVersionMetricsKit = function xpVersionMetricsKit() { + return info.version; +}; + +/** + * The versions and names, and delegate dependencies of delegates attached to MetricsKit + * @example + * // If a delegate with the same name/version value is added + * // to the same target multiple times, (such as a case where + * // portions of one delegate are attached separately to a target) + * // each delegate is only represented + * // once per 'level' of delegate children + * [{ + * version: '1.2.3', + * name: '@amp-metrics/mt-metricskit-delegates-itml' + * } + * { + * version: '3.2.1', + * name: '@amp-metrics/mt-metricskit-delegates-html', + * delegates:[{ + * version: '2.1.3', + * name: '@amp-metrics/mt-metricskit-delegates-html-ios' + * }] + * }] + * @returns {Array} Info of delegates attached to MetricsKit + * @overridable //this should be changed to not be overridable + */ +ClickstreamProcessorBase.prototype.xpDelegatesInfo = function xpDelegatesInfo() { + var delegateTree = delegatesInfo.getStoredDelegateObject(this); + var delegateChildren = delegateTree && delegateTree.delegates; + // If no delegates have been attached to MetricsKit, + // return undefined so the field is removed from the event + return delegateChildren ? delegateChildren : undefined; +}; + +/* + * src/metrics/event_handlers/buyConfirmed.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * <p> + * PLEASE READ: + * What follows is an important discussion of how buys operate in iTunes Stores, and how to accurately collect metrics data during that flow. + * <p> + * The typical flow of a "buy" in an iTunes Store is: + * <ol> + * <li>User performs some "buy" action (e.g. click a "buy" button or a "subscribe" button) + * <li>JavaScript code then creates some "buyData" (including some "buyParams") needed by the Finance servers in order to execute the buy. + * <li>JavaScript code then invokes a native "buy" call such as itms.buy(buyData) or itunes.buy(buyData). + * <li>In response to the "buy()" call, the native code returns immediately, making the rest of the procedure asynchronous to the JavaScript code. + * <li>Native code then makes an HTTP request to the Finance servers with the buyData + * <li>Finance servers then reply with a "success" or "failure" response. + * <li>When Finance response comes back from Finance, the native code will invoke a JavaScript callback such as "onBuy(buyInfo)". + * <li>The "buyInfo" parameter may contain a "result" value (buyInfo.result). + * <li>JavaScript code checks buyInfo.return and if present and non-zero/non-null typically invokes (or publishes) a "buyConfirmed" JavaScript event, and if not, invokes (or publishes) a "buyFailed" event. + * <li>Within that "buyConfirmed" callback function (perhaps subscribed to), is where the metrics "buyConfirmed" event should be created a and recorded. + * </ol> + * </p> + * That is the <i>typical</i> flow. But things can go wrong. + * The Finance servers can, for example, in the response to the "buy" request, instruct the native client code to "detour" the user through a Finance flow such as credit card validation, terms of service agreement, etc. + * When that occurs, the native code <i>will invoke the JavaScript "onBuy" function with a result value indicating buy failure!</i>. + * However, the buy may not have "really" failed... after the user completes the Finance flow, the "buy" request might eventually succeed, in which case the native code is notified and it will call the JavaScript client's "onBuy" callback with a <i>success</i> return value (causing the "buyConfirmed" to be published), even though it has already called it with a failure code! + * <p> + * Due to this special case, the metrics code needs to stash away the metrics data in place at the time the user requests the "buy" and then use that data later (perhaps much later) when/if the "buyConfirmed" actually occurs. + * In order to marry the metrics data from the time of the "buy", to the event returned in any given "buyConfirmed" (remember, it's all asynchronous), the client creates a "clientBuyId" and uses that as a key to store metrics data. + * It also passes that key as a query param on the "buy" request so that... + * When/if the "buyConfirmed" callback is finally called, it will contain the "clientBuyId" value which the metrics code can then use to look up the metrics data that was in place at the time of the "buy". + * <p> + * This "detoured" "edge case" of users being redirected through Finance flows can happen a significant percentage of the time (possibly 10-20%) so it has to be handled for accurate metrics. + * <p> + * MetricsKit attempts to hide as much of this edge-case craziness as possible, and aid in facilitating the handling of it. + * <p> + * <b>To properly instrument buyConfirmed events, JavaScript clients should:</b> + * <ol> + * <li>at <b>"buy"</b> time + * <ol> + * <li> Create a "clientBuyId" via a call to: + * <br><b><i>metrics.eventHandlers.buyConfirmed.createClientBuyId()</i></b>. + * <br>NOTE: each successive call to this function will return a different clientBuyId + * <li> Include that value with the "buyParams" via a call to: + * <br><b><i>metrics.eventHandlers.buyConfirmed.clientBuyIdQueryParamString(clientBuyId)</i></b> + * <li> Cache all metrics data needed for the (ulitmate) "buyConfirmed" event, keying off that clientBuyId, via a call to: + * <br><b><i>metrics.eventHandlers.buyConfirmed.cacheMetricsBuyData(clientBuyId, metricsBuyData)</i></b> + * <br>Caching the data here ensures that the metrics buy data accurately reflects the user state at the time of the "buy" vs. their state when the asynchronous "buyConfirmed" callback is received. + * <li> Invoke the native "itms/iTunes.buy(buyData)" function. + * <br>The clientBuyId will be passed through all the subsequent Finance calls, and eventually be returned to the client when the buy finally succeeds or fails. + * </ol> + * <li>at <b>"buyFailed"</b> time (which will only happen if user gets "detoured" through a Finance flow) + * <ol> + * <li> notify MetricsKit that the buy may be detoured via a call to: + * <br><b><i>metrics.eventHandlers.buyConfirmed.buyFailureOccurred(clientBuyId)</i></b> (MetricsKit will add a "detoured" flag to the stashed data) + * </ol> + * <li>at <b>"buyConfirmed"</b> time (regardless of whether or not it previously failed) + * <ol> + * <li> retrieve the clientBuyId from the buyData included with the "buyConfirmed" native callback, e.g.: + * <br> var clientBuyId = buyInfo.options.clientBuyId; + * <br> This is the same clientBuyId that was supplied as a query parameter during the initial itms/iTunes.buy() call and is plumbed through any Finance flows that occurred. + * <li> retrieve the cached metrics data via a call to: + * <br><b><i>metrics.eventHandlers.buyConfirmed.uncacheMetricsBuyData(clientBuyId)</i></b>. + * <br><b>IMPORTANT</b>: If cached data is successfully retrieved for the supplied clientBuyId, that data will be removed in order to keep the cache size under control + * <li> prepare the data required by the "buyConfirmed" event (be sure to include the "detoured" flag, if present) + * <li> create the "buyConfirmed" metrics event via a call to: + * <br><b><i>metrics.eventHandlers.buyConfirmed.metricsData()</i></b>, including the retrieved cached metrics data. + * </ol> + * </ol> + * + * JavaScript clients should + * + * @delegatable + * @constructor + */ +var BuyConfirmed = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +BuyConfirmed.prototype = Object.create(ClickStreamEventHandler.prototype); +BuyConfirmed.prototype.constructor = BuyConfirmed; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} delegate Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +BuyConfirmed.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @example a "hint", "related" click, "filter" click, etc. + * If this event is representing a plain typed buyConfirmed, this field's value may be null + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}; someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns {map} key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +BuyConfirmed.prototype.metricsData = function ( + pageId, + pageType, + pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/ +) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @returns {array} all the fields that this eventHandler knows about + */ +BuyConfirmed.prototype.knownFields = function knownFields() { + var knownFields = ['eventType', 'eventVersion']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +BuyConfirmed.prototype.eventType = function (callerSuppliedEventFields) { + return 'buyConfirmed'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +BuyConfirmed.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/** + * <i>NOTE: Please read the full description of the "buy" process at the top of this page before attempting to utilize these methdos</i> + * Called at the time a user initiates a "buy" (e.g. typically immediately before the itms/iTunes.buy(buyData) calls) + * This function is used as a cacheKey to store metrics data when the user initiates a "buy" and to retrieve that information if the buy "fails" by virtue of the user + * being forced through, e.g., a Finance flow (CC validation, TOS acceptance, etc.), and then later a "buySuccess" (buyConfirmed) comes through, this value allows + * the client application to bind to the metrics data that would have been included if the buy had initially succeeded. + * @returns {number} returns a unique integer value with each call. The uniqueness only last for a single JavaScript "session" (it is merely an incremented value) + */ +BuyConfirmed.prototype.createClientBuyId = function () { + var newClientId = null; + var environment = this._processor.system.environment; + var previousClientBuyId = environment.sessionStorageObject().getItem('mtMetricsKit_previousClientBuyId'); + + // May coerce type of previousClientBuyId to Number, NaN result should be reset to 1 + newClientId = ++previousClientBuyId; + if (!previousClientBuyId) { + this._processor.system.logger.warn( + 'Metrics: buyConfirmed.createClientBuyId: clientBuyId did not exist or was of incorrect type, reset to 1.' + ); + newClientId = 1; + } + // We use sessionStorage in case our page/JS context goes away and then comes back, we continue where we left off. + environment.sessionStorageObject().setItem('mtMetricsKit_previousClientBuyId', newClientId); + + return newClientId; +}; + +// NOTE: I know this method is a trivial convenience and is not so helpful to callers, but what it *does* do is that its existence makes it explicit to callers that clientBuyId needs to be added to their buParams query parameters +// It also leaves the name of that query param under our control, preventing errors and allowing for transparent changes in the future. +/** + * <i>NOTE: Please read the full description of the "buy" process at the top of this page before attempting to utilize these methdos</i> + * Called at the time a user initiates a "buy" (e.g. typically immediately before the itms/iTunes.buy(buyData) calls) + * Convenience method for creating the appropriate query param string to include with the "buyParams" on a typical "itms/iTunes.buy(buyInfo)" call + * @param {number} clientBuyId is the value returned by the "createClientBuyId()". Remember to only call that method once, as it returns a different value each time. + * @returns {String} a query param string with "clientBuyId" as a key and the value of the passed-in "clientBuyId" as a value, e.g. "&clientBuyId=2" + * <br>NOTE: The returned string is always prefaced with an ampersand (&), never a question mark (?) + * @example + * var clientBuyId = metrics.eventHandlers.buyConfirmed.createClientBuyId(); + * buyData.offers[i].buyParams = buyData.offers[i].buyParams + "&someBuyParam=" + someBuyParamValue + metrics.eventHandlers.buyConfirmed.clientBuyIdQueryParamString(clientBuyId(clientBuyId); + */ +BuyConfirmed.prototype.clientBuyIdQueryParamString = function (clientBuyId) { + return '&clientBuyId=' + clientBuyId; +}; + +/** + * <i>NOTE: Please read the full description of the "buy" process at the top of this page before attempting to utilize these methods</i> + * <i>NOTE: This code is not actually used in the generation of "buyConfirmed" events, but still seems like the best home for it.</i> + * Called at the time a user initiates a "buy" (e.g. typically immediately before the itms/iTunes.buy(buyData) calls) + * Convenience method for creating the appropriate query param string to include with the "buyParams" on a typical "itms/iTunes.buy(buyInfo)" call + * @param {String} pageId is the pageId of the page where the buy originated. + * @param {String} pageType is the pageType of the page where the buy originated. + * @param {String} pageContext is the pageContext of the page where the buy originated. + * @param {String} (optional) topic is the topic that will be used to send "dialog" events to if dialogs are presented as a result of the "buy()" call. If omitted, it will default to "xp_its_main" + * @param {varargs} (optional) callerSuppliedBuyParamsMapsN a variable number of Object arguments from 0-N, + * each containing key/value pairs representing buy parameters to include in + * the returned string + * @returns {Promise} a Promise that returns an unescaped query param string with 'mt' prefixed keys and the value of the passed-in parameters, plus 'mtPrevPage', derived from the + * MetricsKit-cached pageHistory (see page event documentation) + * - mtPrevPage (The previous page, using the pageHistory field) + * - mtPageType (The pageType field of the page from which the purchase occurs) + * - mtPageId (The pageId field of the page from which the purchase occurs). Figaro can use this to ensure that the app being purchased shares an ID with the product page. + * - mtPageContext (The pageContext field of the page from which the purchase occurs) + * - mtTopic (The context of an event (e.g. “xp_its_main”).) + * - mtRequestId (A client generated UUID for every request, to be used in visit stitching to stitch client side event together with server side buy.) + * Refer to base field documentation for undocumented fields below + * - mtApp + * - mtEventTime + * - mtClientId + * e.g. "&mtPrevPage=Picker_Welcome&mtPageId=Subscribe&pageType=Picker&pageContext=ForYou" + * <br>NOTE: The returned string is always prefaced with an ampersand (&), never a question mark (?) + * @example + * var metricsBuyParamsString = await metrics.eventHandlers.buyConfirmed.metricsBuyParamsString(pageId, pageType, pageContext, "xp_its_main", { mtHardwareModel: "iPhone", extRefApp2: "com.apple.Mail" }); + * buyData.offers[i].buyParams = buyData.offers[i].buyParams + "&someBuyParam=" + someBuyParamValue + "&" + metricsBuyParamsString; + */ +BuyConfirmed.prototype.metricsBuyParamsString = function ( + pageId, + pageType, + pageContext, + topic /*, callerSuppliedBuyParamsMapsN(varargs)*/ +) { + var base = this._processor.eventHandlers.base; + var page = this._processor.eventHandlers.page; + var callerSuppliedBuyParamsMapsArray = Array.prototype.slice.call(arguments, 4); + var pageHistory = page.pageHistory(); + var clientIdPromise = base.clientId(); + var mtPrevPage; + + if (Array.isArray(pageHistory)) { + if (pageHistory.length >= 2) { + mtPrevPage = pageHistory[pageHistory.length - 2]; + } + } else { + this._processor.system.logger.warn('MetricsKit: metricsBuyParamsString: pageHistory is not an Array'); + } + + return Promise.resolve(clientIdPromise).then( + function (clientId) { + var buyParams = { + mtApp: base.app(callerSuppliedBuyParamsMapsArray), + mtEventTime: Date.now(), + mtHardwareBrand: base.hardwareBrand(callerSuppliedBuyParamsMapsArray), + mtHardwareFamily: base.hardwareFamily(callerSuppliedBuyParamsMapsArray), + mtHardwareModel: base.hardwareModel(callerSuppliedBuyParamsMapsArray), + mtHostApp: base.hostApp(callerSuppliedBuyParamsMapsArray), + mtHostAppVersion: base.hostAppVersion(callerSuppliedBuyParamsMapsArray), + mtOs: base.os(callerSuppliedBuyParamsMapsArray), + mtOsBuildNumber: base.osBuildNumber(callerSuppliedBuyParamsMapsArray), + mtOsVersion: base.osVersion(callerSuppliedBuyParamsMapsArray), + mtPageId: pageId, + mtPageType: pageType, + mtPageContext: pageContext, + mtTopic: topic || 'xp_its_main', + mtPrevPage: mtPrevPage, + mtRequestId: string$1.uuid(), + mtClientId: clientId + }; + + reflect$1.extend.apply(reflect$1, [buyParams].concat(callerSuppliedBuyParamsMapsArray)); + + // NOTE: if any of these fields are null or "undefined", + // they will be omitted in the "paramString()" function. + return string$1.paramString(buyParams); + }.bind(this) + ); +}; + +/** + * <i>NOTE: Please read the full description of the "buy" process at the top of this page before attempting to utilize these methdos</i> + * <i><b>IMPORTANT</b>: If this method successfully retrieves data for the supplied clientBuyId, that data will be removed in order to keep the cache size under control + * Called at the time a user initiates a "buy" (e.g. typically immediately before the itms/iTunes.buy(buyData) calls) + * This function is used to serialize (to string) and cache metrics data when the user initiates a "buy". + * This data will be retrieved later during the "buyConfirmed" native callback (via the "uncacheMetricsBuyData()" function) and ensures that the metrics buy data accurately reflects the user state at the time of the "buy" vs. their state when the asynchronous "buyConfirmed" callback is received. + * For example, the user may have been forced through a Finance flow (CC validation, TOS acceptance, etc.) and then later a "buySuccess" (buyConfirmed) comes through. + * @param {number} clientBuyId is the value returned by the "createClientBuyId()". Remember to only call that method once, as it returns a different value each time. + * @param {object} metricsBuyData is the value returned by the "createClientBuyId()". Remember to only call that method once, as it returns a different value each time. + */ +BuyConfirmed.prototype.cacheMetricsBuyData = function (clientBuyId, metricsBuyData) { + var environment = this._processor.system.environment; + // Since these methods are similarly named, let's just help out the caller by checking... + if (arguments.length != 2) { + this._processor.system.logger.error( + 'buyConfirmed.cacheMetricsBuyData(): function invoked with incorrect number of parameters. Perhaps you meant to retrieve cached data instead of setting it, which would be a call to uncacheMetricsBuyData(clientBuyId)?' + ); + } else { + // sessionStorage only accepts string values... + var serializedData = JSON.stringify(metricsBuyData); + environment + .sessionStorageObject() + .setItem('mtMetricsKit_metricsBuyData_for_clientBuyId_' + clientBuyId, serializedData); + } +}; + +/** + * <i>NOTE: Please read the full description of the "buy" process at the top of this page before attempting to utilize these methdos</i> + * Called at the time JavaScript receives a "buyConfirmed" callback from the native code. + * This function is used to deserialize (from string) and return the metrics data that was cached when the user initiated the "buy" via the cacheMetricsBuyData() function. + * This data should be the data used to include when creating the buyConfirmed event via the metrics.eventHandlers.buyConfirmed.metricsData() function. + * @param {number} clientBuyId is retrieved from the buyData included with the "buyConfirmed" native callback, e.g.: + * <br> var clientBuyId = buyInfo.options.clientBuyId; + * <br> This is the same clientBuyId that was supplied as a query parameter during the initial itms/iTunes.buy() call and is plumbed through any Finance flows that occurred. + * @returns {object} the same data that was initially stored with this clientBuyId at itms/iTunes.buy() time via the call to: "cacheMetricsBuyData()" + * If no data is found for this clientBuyId, "null" will be returned (typically that should never happen) + */ +BuyConfirmed.prototype.uncacheMetricsBuyData = function (clientBuyId) { + var returnValue = null; + var environment = this._processor.system.environment; + + // Since these methods are similarly named, let's just help out the caller by checking... + if (arguments.length != 1) { + this._processor.system.logger.error( + 'buyConfirmed.uncacheMetricsBuyData(): function invoked with incorrect number of parameters. Perhaps you meant to set cached data instead of retrieving it, which would be a call to cacheMetricsBuyData(clientBuyId, metricsBuyData)?' + ); + } else { + // sessionStorage only accepts string values... + var serializedData = environment + .sessionStorageObject() + .getItem('mtMetricsKit_metricsBuyData_for_clientBuyId_' + clientBuyId); + + if (serializedData) { + returnValue = JSON.parse(serializedData); + environment.sessionStorageObject().removeItem('mtMetricsKit_metricsBuyData_for_clientBuyId_' + clientBuyId); + } + } + return returnValue; +}; + +/** + * <i>NOTE: Please read the full description of the "buy" process at the top of this page before attempting to utilize these methdos</i> + * Called at the time JavaScript receives a "buyFailed" callback from the native code. + * This function is used to flag "buys" that have been "detoured" through a Finance flow with the "detoured=true" flag. + * If a buy fails (which is why "buyFailed" would be called) but then later succeeds (after a Finance detour), we will have set the "detoured" flag by virtue of this call. + * @param {number} clientBuyId is retrieved from the buyData included with the "buyFailed" native callback, e.g.: + * <br> var clientBuyId = buyInfo.options.clientBuyId; + * <br> This is the same clientBuyId that was supplied as a query parameter during the initial itms/iTunes.buy() call and is plumbed through any Finance flows that occurred. + */ +BuyConfirmed.prototype.buyFailureOccurred = function (clientBuyId) { + var uncacheMetricsBuyData = this.uncacheMetricsBuyData(clientBuyId); + + if (uncacheMetricsBuyData) { + uncacheMetricsBuyData.detoured = true; + // Put it back, since a) we added a field and b) asking for it removed it. + this.cacheMetricsBuyData(clientBuyId, uncacheMetricsBuyData); + } +}; + +/* + * src/metrics/event_handlers/click.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +// TODO: move to utils-private +var safeJSONParse = function safeJSONParse(text) { + var returnValue = null; + + try { + returnValue = JSON.parse(text); + } catch (e) { + logger.error('MetricsKit: error parsing click data - ' + e); + } + + return returnValue; +}; + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Click = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Click.prototype = Object.create(ClickStreamEventHandler.prototype); +Click.prototype.constructor = Click; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @param {Object} targetElement The element from which the click event originated + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * @overridable + */ +Click.prototype.metricsData = function ( + pageId, + pageType, + pageContext, + targetElement /*, callerSuppliedEventFieldsMapN(varargs)*/ +) { + var argumentsArray = [pageId, pageType, pageContext]; + var utils = this._processor.utils; + + if (targetElement) { + argumentsArray.push({ + location: utils.eventFields.buildLocationStructure(targetElement, this.locationDataForElement) + }); + argumentsArray.push(this.dataForElement(targetElement) || {}); + } + argumentsArray = argumentsArray.concat(Array.prototype.slice.call(arguments, 4)); + + return this.processMetricsData.apply(this, argumentsArray); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Click.prototype.knownFields = function knownFields() { + var knownFields = [ + 'actionContext', + 'actionDetails', + 'actionType', + 'actionUrl', + 'eventType', + 'eventVersion', + 'impressions', + 'location', + 'targetId', + 'targetType', + 'positionX', + 'positionY', + 'xpViewablePercentage' + ]; + + return knownFields; +}; + +/** + * The parsed click data that was attached to a clicked element + * @param {Object} targetElement The clicked element + * @return {Object} the parsed click data that was attached to the element as a data attribute, if present and valid, otherwise null + * @overridable + */ +Click.prototype.dataForElement = function dataForElement(element) { + var returnValue = null; + + if (element && reflect$1.isFunction(element.hasAttribute) && reflect$1.isFunction(element.getAttribute)) { + var clickDataAttribute = this.dataAttribute(); + if (element.hasAttribute(clickDataAttribute)) { + returnValue = safeJSONParse(element.getAttribute(clickDataAttribute)); + } + } + + return returnValue; +}; + +/** + * The data attribute used to attach click data to DOM-like elements in the view model + * @returns {String} + * @overridable + */ +Click.prototype.dataAttribute = function dataAttribute() { + return 'data-metrics-click'; +}; + +/** + * The relevant location dictionary for an element + * @param {Object} element an element in the view heirarchy + * @return {Object} the location data for the element, if present and valid, otherwise null + * The default implementation looks for a data attribute called 'data-metrics-location' + * and, if present, parses the attribute into a dictionary, and adds the elements' position + * within its parent container as a field called 'locationPosition' + * @overridable + */ +Click.prototype.locationDataForElement = function locationDataForElement(element) { + var parentNode = element.parentNode; + var position = 0; + var location = null; + + var siblingElements; + var sibling; + var siblingLocationInfo; + var siblingLocationType; + + // if this element has a location type defined (in other words, it has a semantic value from the end user + // point of view like shelf, lockup etc) then add it to the location stack otherwise don't. + // for ex. we may have a bunch of <section> elements in our dom tree but there is no point in including + // them in the location stack + if (element.hasAttribute && element.hasAttribute('data-metrics-location')) { + location = safeJSONParse(element.getAttribute('data-metrics-location')); + if (location.locationType) { + if (parentNode) { + // determine our location in the children array of our parent + siblingElements = parentNode.childNodes; + for (var i = 0; i < siblingElements.length; i++) { + // ITML childNodes list uses .item(i) accessor instead of [i] + sibling = + (typeof siblingElements.item === 'function' && siblingElements.item(i)) || siblingElements[i]; + siblingLocationInfo = + sibling.hasAttribute && sibling.hasAttribute('data-metrics-location') + ? safeJSONParse(sibling.getAttribute('data-metrics-location')) + : undefined; + siblingLocationType = siblingLocationInfo ? siblingLocationInfo.locationType : undefined; + if (siblingLocationType) { + if (sibling === element) { + // we now know the position of the element, so stop here + break; + } else { + // we consider all siblings that have the data-metrics-location attribute and the same parent container as the target element + // for calculating position. for ex. on a grouping page, we may have 2 swooshes, followed by a title text div and then + // one more swoosh. We count the preceding swooshes and the title text div sibling element when calculating the position + // of the 3rd swoosh. The position of the last swoosh should be '3' (with '0' based index). + position++; + } + } + } + } + location.locationPosition = position; + } + } + + return location; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Click.prototype.eventType = function (callerSuppliedEventFields) { + return 'click'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Click.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 4; +}; + +/** + * A list of flattened impression objects for this event + * Default behavior assumes the impressions have already been flattened and just returns them, but clients can override this with their own flattening implementation + * @returns {Array} an array of impression objects, or undefined if no event fields were provided by the caller + * @overridable + */ +Click.prototype.impressions = function impressions(callerSuppliedEventFields) { + return callerSuppliedEventFields ? callerSuppliedEventFields.impressions : undefined; +}; + +/** + * The percentage, from 0 to 1, that an item should be on screen before being considered impressionable + * This field should be based on the client's most recent config value of "viewablePercentage". + * @returns {number} the percentage, from 0 to 1, that an item should be on screen before being considered impressionable + * @overridable + */ +Click.prototype.xpViewablePercentage = function xpViewablePercentage(callerSuppliedEventFields) { + return this._processor.eventHandlers.base.xpViewablePercentage(callerSuppliedEventFields); +}; + +/* + * src/metrics/event_handlers/dialog.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Dialog = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Dialog.prototype = Object.create(ClickStreamEventHandler.prototype); +Dialog.prototype.constructor = Dialog; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Dialog.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @example a "hint", "related" click, "filter" click, etc. + * If this event is representing a plain typed dialog, this field's value may be null + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Dialog.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Dialog.prototype.knownFields = function knownFields() { + var knownFields = ['buttons', 'code', 'details', 'message', 'type', 'eventType', 'eventVersion', 'type']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Dialog.prototype.eventType = function (callerSuppliedEventFields) { + return 'dialog'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Dialog.prototype.eventVersion = function (callerSuppliedEventFields) { + // Since these have an additional field "type" beyond eventVersion 1 (sent via server-requested "GET" URL ping in MXPFailure.java) + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 2; +}; + +/* + * src/metrics/event_handlers/enter.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Enter = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Enter.prototype = Object.create(ClickStreamEventHandler.prototype); +Enter.prototype.constructor = Enter; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Enter.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @example a "hint", "related" click, "filter" click, etc. + * If this event is representing a plain typed enter, this field's value may be null + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Enter.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Enter.prototype.knownFields = function knownFields() { + var knownFields = ['eventType', 'eventVersion', 'extRefUrl', 'osLanguages', 'refApp', 'type']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Enter.prototype.eventType = function (callerSuppliedEventFields) { + return 'enter'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Enter.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/** + * OS language preferences; a string array of language IDs, ordered in descending preference + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke its accessor, if one is available, in case *its* value is derived or massaged. + * @returns {Array} a list of preferred languages (strings) e.g. ['en-US', 'fr-CA'] + * @overridable + */ +Enter.prototype.osLanguages = function osLanguages(callerSuppliedEventFields) { + return ( + (callerSuppliedEventFields && callerSuppliedEventFields.osLanguages) || + this._processor.system.environment.osLanguages() + ); +}; + +/* + * src/metrics/event_handlers/exit.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Exit = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Exit.prototype = Object.create(ClickStreamEventHandler.prototype); +Exit.prototype.constructor = Exit; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Exit.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @example a "hint", "related" click, "filter" click, etc. + * If this event is representing a plain typed exit, this field's value may be null + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Exit.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Exit.prototype.knownFields = function knownFields() { + var knownFields = ['destinationUrl', 'eventType', 'eventVersion', 'type']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Exit.prototype.eventType = function (callerSuppliedEventFields) { + return 'exit'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Exit.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/* + * src/metrics/event_handlers/flexible.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Flexible = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Flexible.prototype = Object.create(ClickStreamEventHandler.prototype); +Flexible.prototype.constructor = Flexible; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Creates a simple map object (dictionary) + * @param {String} eventType required for all event creation. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.flexible.metricsData('anEventType', {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns {Promise} A promise that will return key/value pairs of all event fields, merged and cleaned (removes keys that are typeof 'function', keys with 'null' values, keys with 'undefined' values) + * WARNING: The Promise may return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Flexible.prototype.metricsData = function (eventType /*, callerSuppliedEventFieldsMapN(varargs)*/) { + var eventData = [undefined, undefined, undefined]; // leave page fields undefined + + // include eventType so it gets processed + eventData.push({ eventType: eventType }); + + var callerSuppliedEventFieldsMapsArray = Array.prototype.slice.call(arguments, 1); + eventData = eventData.concat(callerSuppliedEventFieldsMapsArray); + + return this.processMetricsData.apply(this, eventData); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Flexible.prototype.knownFields = function knownFields() { + var knownFields = ['eventTime', 'eventType']; + + return knownFields; +}; + +/** + * A flag indicating whether this event should include base fields. Defaults to false for flexible events. + * @returns {Boolean} + * @overridable + */ +Flexible.prototype.mtIncludeBaseFields = function mtIncludeBaseFields() { + return false; +}; + +/** + * The time (UTC) in milliseconds at which this event happened. + * This field is central to determining the sequence of user events + * Use online epoch converter to test your values. + * @example 1437356433388 (http://www.epochconverter.com converts that to: July 19, 2015 at 6:40:33 PM PDT GMT-7:00 DST) + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke it's accessor, if one is available, in case *it's* value is derived or massaged. + * @returns {number} the time (UTC) in milliseconds at which this event happened + * @overridable + */ +Flexible.prototype.eventTime = function eventTime(callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventTime) || Date.now(); +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Flexible.prototype.eventType = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventType) || undefined; +}; + +/* + * src/metrics/event_handlers/impressions.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Impressions = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Impressions.prototype = Object.create(ClickStreamEventHandler.prototype); +Impressions.prototype.constructor = Impressions; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Impressions.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Impressions.prototype.metricsData = function ( + pageId, + pageType, + pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/ +) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Impressions.prototype.knownFields = function knownFields() { + var knownFields = ['eventType', 'eventVersion', 'impressions', 'xpViewablePercentage', 'xpViewableThreshold']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Impressions.prototype.eventType = function (callerSuppliedEventFields) { + return 'impressions'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Impressions.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 3; +}; + +/** + * A list of flattened impression objects for this event + * Default behavior assumes the impressions have already been flattened and just returns them, but clients can override this with their own flattening implementation + * @returns {Array} an array of impression objects, or undefined if no event fields were provided by the caller + * @overridable + */ +Impressions.prototype.impressions = function impressions(callerSuppliedEventFields) { + return callerSuppliedEventFields ? callerSuppliedEventFields.impressions : undefined; +}; + +/** + * The percentage, from 0 to 1, that an item should be on screen before being considered impressionable + * This field should be based on the client's most recent config value of "viewablePercentage". + * @returns {number} the percentage, from 0 to 1, that an item should be on screen before being considered impressionable + * @overridable + */ +Impressions.prototype.xpViewablePercentage = function xpViewablePercentage(callerSuppliedEventFields) { + var base = this._processor.eventHandlers.base; + return base.xpViewablePercentage(callerSuppliedEventFields); +}; + +/** + * The continuous duration, in milliseconds, that an item should be on screen before being considered impressed + * This field should be based on the client's most recent config value of "viewableThreshold". + * This is valuable for problem analysis because it indicates if and how clients are honoring the "viewableThreshold" value + * they were supplied with. + * This cannot be a "passthrough" field, because it can change (via new config) during program execution, so the value + * in effect at event creation time is what is needed. + * @example 1000 + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke it's accessor, if one is available, in case *it's* value is derived or massaged. + * @returns {number} the continuous duration, in milliseconds, that an item should be on screen before being considered impressed + * @overridable + */ +Impressions.prototype.xpViewableThreshold = function xpViewableThreshold(callerSuppliedEventFields) { + var config = this._processor.config; + return ( + (callerSuppliedEventFields && callerSuppliedEventFields.xpViewableThreshold) || + config.value('impressions.viewableThreshold') + ); +}; + +/* + * src/metrics/event_handlers/media.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the "media" fields + all of the "base" events common to all metrics events. + * To override any functionality in this class, use the "utils.override() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Media = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Media.prototype = Object.create(ClickStreamEventHandler.prototype); +Media.prototype.constructor = Media; + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Media.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.media.metricsData(appData.pageId, appData.pageType, appData.pageContext, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Media.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Media.prototype.knownFields = function knownFields() { + var knownFields = [ + 'eventType', + 'eventVersion', + 'id', + 'idType', + 'type', + 'typeDetails', + 'actionType', + 'actionDetails', + 'url', + 'duration', + 'position' + ]; + return knownFields; +}; + +// ********************* ACCESSOR FUNCTIONS ********************* +/** + * We create accessor functions for every data field because: + * 1. Cleans/simplifies all methods that use it. + * 2. Facilitates writing test case shims + * 3. Allows specific feature suppliers to be overridden (via setDelegate()) + */ + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Media.prototype.eventType = function (callerSuppliedEventFields) { + return 'media'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Media.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/* + * src/metrics/event_handlers/page.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the "page" fields + all of the "base" events common to all metrics events. + * To override any functionality in this class, use the "utils.override() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Page = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); + // @private + this.pageHistoryCache = []; +}; + +Page.prototype = Object.create(ClickStreamEventHandler.prototype); +Page.prototype.constructor = Page; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Page.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Page.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Page.prototype.knownFields = function knownFields() { + var knownFields = [ + 'eventType', + 'eventVersion', + 'extRefUrl', + 'hostApp', + 'refApp', + 'refUrl', + 'requestStartTime', + 'responseStartTime', + 'responseEndTime', + 'pageHistory', + 'pageLoadTime', + 'pageRenderTime', + 'searchFilters', + 'searchTerm' + ]; + + return knownFields; +}; + +// ********************* ACCESSOR FUNCTIONS ********************* +/** + * We create accessor functions for every data field because: + * 1. Cleans/simplifies all methods that use it. + * 2. Facilitates writing test case shims + * 3. Allows specific feature suppliers to be overridden (via setDelegate()) + */ + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Page.prototype.eventType = function (callerSuppliedEventFields) { + return 'page'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Page.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/** + * If a value for this accessor's field exists in "callerSuppliedEventFields", the accessor may want to honor the caller's desire and return that, iff no massaging is needed. + * Returns an array of the past <= 5 values of 'page' fields from prior page events, + * storing the current value for use in the next request + * @param {Map} callerSuppliedEventFields - NOTE: If you want to use one of these fields to help derive *this* eventField, you should + * invoke it's accessor, if one is available, in case *it's* value is derived or massaged. + * @return {Array} The past 5 or fewer page event's 'page' fields, *not including* the current page. + */ +Page.prototype.pageHistory = function pageHistory(callerSuppliedEventFields) { + var returnValue; + + callerSuppliedEventFields = callerSuppliedEventFields || {}; + if (callerSuppliedEventFields.pageHistory) { + returnValue = callerSuppliedEventFields.pageHistory; + } else { + returnValue = this.pageHistoryCache.slice(0); // returns a clone + var currentPageName = callerSuppliedEventFields.page; + + if (currentPageName) { + if (this.pageHistoryCache.length >= 5) { + this.pageHistoryCache.shift(); + } + this.pageHistoryCache.push(currentPageName); + } + } + return returnValue; +}; + +/* + * src/metrics/event_handlers/search.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Search = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Search.prototype = Object.create(ClickStreamEventHandler.prototype); +Search.prototype.constructor = Search; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Search.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. pageId, pageType, pageContext) + * @param {String} pageId required for all event creation. Indicates the id of the page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * The type of ID may vary (e.g. adam id, grouping id, result id, etc), but is generally + * the identifier in some persistent store. Search Results pages may have a pageId which refers to their specific dataSet, + * otherwise known as dataSetId. If the page is better identified by a descriptive string rather than a content ID, + * this field may be hard-coded, but it should be unique within the set of pages displayable by the app. + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client. + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageType required for all event creation. Indicates the type of page this event took place on. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * This value should represent the group of pages (e.g. "Genre", “Album”, “Grouping”, "Picker", "Recommendations", "Feed", + * "Search", "Subscribe", etc.). + * If an application is not client/server "page" oriented (where the client needs to request data from the server for each "page" of the app), then + * this field would be generated by the client (potentially being hard-coded). + * Note: A unique "page" value will created by concatenating pageType+"_"+pageId + * @param {String} pageContext required for all event creation. Indicates the context within which a page is viewed. + * This value *will* be overridden if found in any of the provided callerSuppliedEventFieldsMapN dictionaries, which is consistent with later dictionary fields overriding earlier ones. + * Contexts usually provide independent streams of activity and can typically appear as UI "tab" elements or primary navigation elements. + * @example: iTunes Desktop: “InTheStore” or “Main”. + * @example: iOS iTunes apps: tab name (e.g. “Featured”, “TopCharts”, etc) + * @example: iOS Store sheets: “Sheet” (e.g. AppStore page launched as a sheet within Mail, Springboard’s “Near Me”, Maps’ “Nearby Apps”) + * @example a "hint", "related" click, "filter" click, etc. + * If this event is representing a plain typed search, this field's value may be null + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the known page fields will be included + * @example metrics.eventHandlers.page.metricsData(appData.pageId, appData.pageType, appData.pageContext, element, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Search.prototype.metricsData = function (pageId, pageType, pageContext /*, callerSuppliedEventFieldsMapN(varargs)*/) { + // TODO:ATHOMPSON: Add back in with ability to add/not add targetElement param + return this.processMetricsData.apply(this, arguments); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Search.prototype.knownFields = function knownFields() { + var knownFields = [ + 'actionDetails', + 'actionType', + 'actionUrl', + 'eventType', + 'eventVersion', + 'filters', + 'location', + 'targetId', + 'targetType', + 'term' + ]; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Search.prototype.eventType = function (callerSuppliedEventFields) { + return 'search'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Search.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 2; +}; + +/* + * src/metrics/event_handlers/transaction.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Creates and returns an object (key/value data map (dictionary)) containing all of the fields needed for this metrics event. + * To override any functionality in this class, use the "setDelegate() method in order to override the specific functions that need customization. + * @delegatable + * @constructor + */ +var Transaction = function (metricsKit) { + ClickStreamEventHandler.apply(this, arguments); +}; + +Transaction.prototype = Object.create(ClickStreamEventHandler.prototype); +Transaction.prototype.constructor = Transaction; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Transaction.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Creates a simple map object (dictionary) with all the fields required by Figaro for this event + * Some fields can be derived by this class itself. + * Some fields need to be provided by callers (e.g. the transaction fields and values) + * @param {Map<String:Any>} transactionMetrics key/value pairs associated with this transaction; typically provided in the response from the buyProduct endpoint + * @param {varargs} callerSuppliedEventFieldsMapN a variable number of Object arguments from 0-N, each containing key/value pairs representing event fields to include with the returned metricsData + * All event fields will be merged. + * An attempt will be made to invoke an accessor method for each field, providing the caller the opportunity to override accessor methods. + * If no accessor method is found, the event field value will be included in the returned metricsData as-is. + * Later objects take precedence over earlier ones, overriding any field value that may have already been there. + * If this parameter is null or omitted, only the transaction metrics will be included + * @example metrics.eventHandlers.transaction.metricsData(buyProductResponseMetrics, {some:"some", known:"known", eventFieldValues:"eventFieldValues"}, someOtherKnownEventValues, appData.someAdditionalBaseFieldValues); + * @returns key/value pairs of all event fields + "base" fields required by Figaro. + * WARNING: May return "null" if metrics are disabled via the metrics.disabled config source value, or on error. + * @overridable + */ +Transaction.prototype.metricsData = function metricsData( + transactionMetrics /*, callerSuppliedEventFieldsMapN(varargs)*/ +) { + var argsArray = [null, null, null].concat(Array.prototype.slice.call(arguments)); + return this.processMetricsData.apply(this, argsArray); +}; + +/** + * Returns all the fields that this eventHandler knows about. + * The eventHandler may have accessor functions which derive some of these fields. + * The "metricsData()" method will use this list of fields to attempt to invoke accessor methods to get field values. + * Therefore, if callers override this function, and add additional values, and provide accessors for those values, then + * the "metricsData()" function will ultimately call those accessors as well. + * @return all the fields that this eventHandler knows about + */ +Transaction.prototype.knownFields = function knownFields() { + var knownFields = ['eventType', 'eventVersion']; + + return knownFields; +}; + +/** + * The type of event this is + * @returns {String} + * @overridable + */ +Transaction.prototype.eventType = function (callerSuppliedEventFields) { + return 'transaction'; +}; + +/** + * The version of the set of data to be sent up + * @returns {number} + * @overridable + */ +Transaction.prototype.eventVersion = function (callerSuppliedEventFields) { + return (callerSuppliedEventFields && callerSuppliedEventFields.eventVersion) || 1; +}; + +/* + * src/metrics/event_handlers/index.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +var EventHandlers = function (metricsKitInstance) { + this.account = new Account(metricsKitInstance); + this.base = new ClickstreamProcessorBase(metricsKitInstance); + this.buyConfirmed = new BuyConfirmed(metricsKitInstance); + this.click = new Click(metricsKitInstance); + this.dialog = new Dialog(metricsKitInstance); + this.enter = new Enter(metricsKitInstance); + this.exit = new Exit(metricsKitInstance); + this.flexible = new Flexible(metricsKitInstance); + this.impressions = new Impressions(metricsKitInstance); + this.media = new Media(metricsKitInstance); + this.page = new Page(metricsKitInstance); + this.search = new Search(metricsKitInstance); + this.transaction = new Transaction(metricsKitInstance); + + delegateExtension.attachDelegateInfo(this.account); + delegateExtension.attachDelegateInfo(this.base); + delegateExtension.attachDelegateInfo(this.buyConfirmed); + delegateExtension.attachDelegateInfo(this.click); + delegateExtension.attachDelegateInfo(this.enter); + delegateExtension.attachDelegateInfo(this.exit); + delegateExtension.attachDelegateInfo(this.flexible); + delegateExtension.attachDelegateInfo(this.impressions); + delegateExtension.attachDelegateInfo(this.media); + delegateExtension.attachDelegateInfo(this.page); + delegateExtension.attachDelegateInfo(this.search); + delegateExtension.attachDelegateInfo(this.transaction); +}; + +/* + * src/metrics/utils/event_fields.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Removes duplicate impressions from an array of sibling impressions (i.e. impressions with the same parent) based on whether its index is unique + * This is needed because the impressions spec: + * https://connectme.apple.com/docs/DOC-240953 + * states that: + * iii. If items come into view, then go out of view, then come back, they are not counted twice. + * So, if a user scrolls a swoosh away from a lockup, and then back, so that the lockup is in view again, it will appear twice (with the same index-within-parent) in the impressions-object-tree + * returned from ITMLKit, but we don't want it included twice, so we remove the dupes here. + * NOTE: This should never happen with an impressions list retrieved via "itms.snapshotImpressions()" but for caller-simplicity, and since our caller is typically walking the tree already, some of our callers may invoke it even when unnecessary. + * @param {Array} impressions An array of impressions to dedupe + * @return {Array} A set of unique impressions + */ +var dedupeSiblingImpressions = function (impressions) { + var uniqueIndices = {}; + var dedupedSiblingImpressions = []; + var currentIndex; + if (impressions && impressions[0] && impressions[0].index !== undefined) { + for (var i = 0; i < impressions.length; ++i) { + currentIndex = impressions[i].index; + if (!uniqueIndices[currentIndex]) { + uniqueIndices[currentIndex] = true; + dedupedSiblingImpressions.push(impressions[i]); + } + } + } + return dedupedSiblingImpressions; +}; + +/** + * These routines are useful for clients to create and access Metrics data and config + * @constructor + */ +var EventFields$1 = function (processor) { + // @private + this._processor = processor; +}; + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ +/** + * Uses the provided id to generate its idType + * @param {String} idString The id to generate a type for + * @param {String} (Optional) separator id separator, if not provided, this function will return a Promise otherwise the function will return the id string. + * @return {Promise|String} returns a string of the idType of the provided id if providing separator otherwise returns a Promise + */ +EventFields$1.prototype.getIdType = function (idString, separator) { + var config = this._processor.config; + var defaultPrefix = 'its'; + var prefixSeparatorIndex = idString.indexOf('.'); + var prefix = prefixSeparatorIndex !== -1 ? idString.substring(0, prefixSeparatorIndex) : defaultPrefix; + + return reflect$1.isString(separator) + ? prefix + separator + 'id' + : config.value('compoundSeparator').then(function (separator) { + return prefix + separator + 'id'; + }); +}; + +/** + * This method is the workhorse of all the various eventHandlers. + * It will take all of the parameters of the callers "metricsData()" method, merge them together, + * invoke accessors on their known fields, and return the resultant map. + * @param eventHandler the calling eventHandler + * @param knownFields the calling eventHandler's list (array) of strings that are that handler's known field values. + * If the caller has accessors to be invoked, they must be present in the "knownFields" array + * @returns {Arguments} all of the arguments that the calling eventHandler received. + * @example: + * Page.prototype.metricsData = function(pageId, pageType, pageContext, eventFieldsMapN(varargs)) + * {return utils.eventFields.processMetricsData(this, arguments); + */ +EventFields$1.prototype.processMetricsData = function ( + eventHandler, + knownFields, + pageId, + pageType, + pageContext, + callerSuppliedEventFieldsMapsArray +) { + // Combine all passed-in fields... + var combinedFieldsMapsArray = [ + { + pageId: pageId, + pageType: pageType, + pageContext: pageContext + } + ]; + if (reflect$1.isArray(callerSuppliedEventFieldsMapsArray)) { + combinedFieldsMapsArray = combinedFieldsMapsArray.concat(callerSuppliedEventFieldsMapsArray); + } + + return eventFields.processMetricsData(eventHandler, knownFields, true, combinedFieldsMapsArray); +}; +/** + * Returns an object containing the intersection of properties in + * data and matching string values in the fieldMap property corresponding to 'sectionName' + * ( e.g. fieldMap.custom[sectionName] is an object containing arrays of strings which + * correspond to the keys desired in the mappedFields object ) + * @param {Object} data The model data corresponding to element we're mapping fields for + * @param {String} sectionName Specifies which custom fieldMap to use (eg: 'impressions', 'location', or 'custom') + * @return {Promise} A Promise that returns an object contains intersection of data and fieldsMap values + * @example + * // where impressionFieldsMapSection = { + * // impressionType: ['type', 'impressionType'], + * // id: ['targetId', 'id'] + * //}; + * applyFieldsMap({type: 'button', id: '123', name: 'playbutton'}, 'impressions') + * // returns {impressionType: 'button', id: '123'} + */ +EventFields$1.prototype.applyFieldsMap = function (data, sectionName) { + var config = this._processor.config; + var self = this; + + if (data && sectionName) { + return config.value('fieldsMap').then(function (fieldsMap) { + var mappedFields = {}; + fieldsMap = fieldsMap || {}; + var fieldsMapSection = keyValue.valueForKeyPath(sectionName, fieldsMap, fieldsMap.custom); + if (fieldsMapSection) { + var i; + if (Array.isArray(fieldsMapSection)) { + for (i = 0; i < fieldsMapSection.length; ++i) { + if (data[fieldsMapSection[i]]) { + mappedFields[fieldsMapSection[i]] = data[fieldsMapSection[i]]; + } + } + } else if (typeof fieldsMapSection === 'object') { + for (var key in fieldsMapSection) { + for (i = 0; i < fieldsMapSection[key].length; ++i) { + var value = keyValue.valueForKeyPath(fieldsMapSection[key][i], data); + if (value) { + mappedFields[key] = value; + break; + } + } + } + } else { + self._processor.system.logger.error( + 'mt-metricskit-processor-clickstream: incorrect data type provided to applyFieldsMap (only accepts objects and Arrays)' + ); + } + } else { + self._processor.system.logger.error( + 'mt-metricskit-processor-clickstream: unable to get fieldsMap from config-source' + ); + } + + return mappedFields; + }); + } else { + if (!data) { + this._processor.system.logger.error( + 'mt-metricskit-processor-clickstream: No data provided to applyFieldsMap' + ); + } + if (!sectionName) { + this._processor.system.logger.error( + 'mt-metricskit-processor-clickstream: No sectionName provided to applyFieldsMap' + ); + } + return Promise.resolve(undefined); + } +}; +/** + * Creates a deduped & flattened representation of the list of impressions tree nodes provided + * reassigns some impressions values for use with metrics processing + * @param {Array} impressions Array of IKJSImpressions objects + * @param {number} currentId The index at which to increment impresionId off of + * @return {Promise} A Promise returns impressionsArray array of Objects corresponding to and created from data in impressions + */ +EventFields$1.prototype.flattenImpressions = function (impressions, currentId) { + var config = this._processor.config; + + // Because this function needs to load "compoundSeparator" from config in the loop and also recursively build impressions. + // So create the internal flattenImpressions function to make we load the "compoundSeparator" outside of the function to make the function simple and clean. + var recursivelyFlattenImpressions = function recursivelyFlattenImpressions(impressions, currentId, separator) { + var impressionsArray = []; + var anImpression; + var impressionData; + var childrenArray; + var decodedData; + var impressionId = currentId || 1; + + if (impressions) { + impressions = dedupeSiblingImpressions(impressions); + for (var i = 0; i < impressions.length; i++) { + anImpression = impressions[i]; + if (typeof anImpression.data === 'string') { + try { + impressionData = JSON.parse(anImpression.data); + } catch (anException) { + // Lets' see if the exception happened because someone put URIEncoded data in the impression... + decodedData = decodeURIComponent(anImpression.data); + try { + impressionData = JSON.parse(decodedData); + } catch (anotherException) { + // So, the error is not [necessarily] due to encoding. Let's not stop the app here by letting this exception propogate, throw the original exception. + // (see the discussion about catching exceptions here in this radar: <rdar://problem/23810404> Metricskit: JSON parse error in flattenImpressions + this._processor.system.logger.error( + 'mt-metricskit-processor-clickstream: non-JSON serialized data found on impression object. Cannot parse.', + anException + ); + } + } + } else { + impressionData = anImpression; + } + if (impressionData) { + impressionData.impressionTimes = anImpression.timestamps; + impressionData.impressionIndex = anImpression.index; + if (impressionData.id && !impressionData.idType) { + if (anImpression.kind === 'genre') { + // Remove when server provides genre id prefixes + impressionData.idType = 'label' + separator + 'id'; + } else { + impressionData.idType = this.getIdType(impressionData.id.toString(), separator); + } + } + if (anImpression.parent && anImpression.parent.impressionId !== undefined) { + // if no parent, parent === null + impressionData.impressionParentId = anImpression.parent.impressionId; + } + impressionData.impressionId = impressionId; + anImpression.impressionId = impressionId; // saves this value for use as parentId + ++impressionId; + impressionsArray.push(impressionData); + if (keyValue.valueForKeyPath('children.length', anImpression) > 0) { + childrenArray = recursivelyFlattenImpressions(anImpression.children, impressionId, separator); + impressionsArray = impressionsArray.concat(childrenArray); + impressionId += childrenArray.length; + } + } + } + } else { + this._processor.system.logger.warn('Fuse-Metrics: No impressions provided to to flattenImpressions'); + } + + return impressionsArray; + }.bind(this); + + return config.value('compoundSeparator').then(function (separator) { + return recursivelyFlattenImpressions(impressions, currentId, separator); + }); +}; + +/** + * Returns a flat array representing the location of the clicked element + * @param {Object} targetElement The clicked element, requires attribute data-metrics-location with obj key of locationType + * @param {Function} locationFn a function that takes an element as an argument and calculates + * the location dictionary for that element + * @return {Array} A flat array representing the location of the target element + */ +EventFields$1.prototype.buildLocationStructure = function buildLocationStructure(targetElement, locationFn) { + var currentElement = targetElement; + var locationStack = []; + var location; + + //loop and add element locations to the the location stack to be returned + while (currentElement) { + location = locationFn.call(locationFn, currentElement); + if (location) { + locationStack.push(location); + } + + currentElement = currentElement.parentNode; + } + return locationStack; +}; + +/* + * src/network.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2018 Apple Inc. All rights reserved. + * + */ + +/** + * Network request methods exposed so delegate callers can override + * @constructor + */ +var Network = function () {}; + +/** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * "setDelegate()" may be called repeatedly, with the functions in the most-recently set delegates replacing any functions matching those in the earlier delegates, as well as any as-yet unreplaced functions. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If, for example, a client wants to use the "canned" itml/environment delegate with the exception of, say, the "appVersion" method, they can set itml/environment as the delegate, and + * then call "setDelegate()" again with their own delegate containing only a single method of "appVersion" as the delegate, which would leave all the other "replaced" methods intact, + * but override the "appVersion" method again, this time with their own supplied delegate. + * + * NOTE: The delegate function will have a property called origFunction representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ +Network.prototype.setDelegate = function setDelegate(delegate) { + return reflect$1.attachDelegate(this, delegate); +}; + +/** + * Covers private util network functions for delegation + */ +Network.prototype.makeAjaxRequest = network.makeAjaxRequest; + +/* + * src/metrics/utils/reflect.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015 Apple Inc. All rights reserved. + * + */ + +/** + * Object Reflection Primitive Methods (e.g. "Map" type methods) + * copied from private utils so delegate callers can use them + * @constructor + */ +var reflect = { + /** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + /** + * Replace any "target" methods found on "delegate" with the delegate's version of the method. + * The replacement function will actually be our own wrapper function with the original function attached as a property called origFunction + * in case the delegate's replacement method wants to, essentially, call "super" + * We do delegation this way, vs. checking each time a "target" function is called, because this way we don't pollute the implementation + * of all the target's functions. + * Subsequent calls to "attachDelegate" will then replace whatever methods *they* match, including methods that have already been replaced. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * NOTE: Only methods present on "target" will be replaced. If a delegate method is not found on target, "false" will be returned. + * If a replaced method is overridden again with a subsequent "setDelegate()" call, the "origFunction" property will be the previous delegate's function. + * @param target + * @param delegate + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + */ + attachDelegate: function attachDelegate(target, delegate) { + return reflect$1.attachDelegate(target, delegate); + }, + + /** + * Adds all the fields of the objects in the varargs to the fields in the first parameter, "obj". + * *All* "hasOwnProperty" fields will be added, including functions and fields with no values. + * @param {Object} targetObject an object with keys and values. If only one parameter is provided, the return value will be the non-null values of that single object. + * @param {varargs} sourceObjectN a variable number of Object arguments from 0-N. Each object's fields will be copied into targetObject. Later objects take precedence over earlier ones. + * @return targetObject (*not* a clone) with the additional fields added.. + */ + extend: function extend(targetObject /* , ...sourceObjectN(varargs) */) { + return reflect$1.extend.apply(reflect$1, arguments); + }, + + /** + * Bind the execution context of the methods of given Object to itself. + * NOTE: Only bind the functions that are owned by the parameter object + * @param {Object} targetObject an object with keys and values. + */ + bindFunctionsContext: function bindFunctionsContext(targetObject) { + if (targetObject) { + for (var key in targetObject) { + if (typeof targetObject[key] === 'function') { + targetObject[key] = targetObject[key].bind(targetObject); + } + } + } + } +}; + +/* + * src/metrics/utils/string.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + * String related util methods + * copied from private utils so delegate callers can use them + * @constructor + */ +var string = { + /** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + /** + * Parses a user agent string for a particular product name and returns its version + * @param {String} userAgent that conforms with RFC 7231 section 5.5.3 regarding User-Agents + * @param {String} (optional) productName the name of a product identifier to search for e.g. 'iTunes'; if omitted, defaults to the first identifier + * @return {String} the version of the product, or null if none found + * @example + * productVersionFromUserAgent('iTunes/12.6 (Macintosh; OS X 10.12.4) AppleWebKit/603.1.30.0.34') returns '12.6' + * productVersionFromUserAgent('iTunes/12.6 (Macintosh; OS X 10.12.4) AppleWebKit/603.1.30.0.34', 'AppleWebKit') returns '603.1.30.0.34' + * productVersionFromUserAgent('iTunes/12.6 (Macintosh; OS X 10.12.4) AppleWebKit/603.1.30.0.34', 'Macintosh') returns null + * (strings contained in parentheses are counted as comments, not product identifiers) + */ + versionStringFromUserAgent: function versionStringFromUserAgent(userAgent, productName) { + return string$1.versionStringFromUserAgent(userAgent, productName); + } +}; + +/* + * src/metrics/utils/index.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2015-2016 Apple Inc. All rights reserved. + * + */ + +var Utils = function Utils(mtkitInstance) { + // TODO if no client is using it, maybe it could be removed from Utils class + this.delegateExtension = delegateExtension; + // assign new eventFields instance to utils, to give eventFields the metricsKit instance context. + this.eventFields = new EventFields$1(mtkitInstance); + // bind the context to eventHandlers to avoid losing the runtime context if the client assign function to a variable. + reflect.bindFunctionsContext(this.eventFields); + + // copy keyValue from private utils for historical reasons in case clients are using it + // this can probably be removed with more research + // TODO if no client is using it, maybe it could be removed from Utils class + this.keyValue = keyValue; + this.network = new Network(); + // TODO if no client is using it, maybe it could be removed from Utils class + this.reflect = reflect; + // TODO if no client is using it, maybe it could be removed from Utils class + this.string = string; +}; + +/* + * src/utils/event_field_accessors/base.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +var Base = function (metricsKit) { + // @private + this._eventHandler = new Flexible(metricsKit); +}; + +/** + ************************************ PUBLIC METHODS ************************************ + */ + +/** + * Return the corresponding clientId with the giving status + * @param {Object} qualifiers + * @param {Boolean} qualifiers.isSignedIn - A boolean value indicating the returned clientId is for signed-in status or signed-out status. + * @returns {Promise} A Promise that resolved the clientId, will return null if + * 1. the entire event is denied by config + * 2. the clientId is a denied field + */ +Base.prototype.clientId = function clientId(qualifiers) { + return this._eventHandler + .metricsData('', qualifiers) + .toJSON() + .then(function (eventFields) { + return eventFields ? eventFields.clientId : null; + }); +}; + +/* + * src/event_fields/index.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2021 Apple Inc. All rights reserved. + * + */ + +var EventFields = function (metricsKitInstance) { + this.base = new Base(metricsKitInstance); +}; + +/* + * src/metrics/clickstream_processor.js + * mt-metricskit-processor-clickstream + * + * Copyright © 2020 Apple Inc. All rights reserved. + * + */ + +/** + * Supplies a JavaScript entrypoint to metrics functionality + * Since JavaScript is prototype-based and not class-based, and doesn't provide an "official" object model, this API is presented as a functional API, but still retains the ability to + * override and customize functionality via the "setDelegate()" method. In this way, it doesn't carry with it the spare baggage of exposing a bolt-on object model which may + * differ from a bolt-on (or homegrown) object model already existing in the app. + * @module src/metrics + * @param {Object} delegates - The below interface will be generated by Metrics delegates(mt-metricskit-delegates-*). Clients don't have to generate it by themselves + * For more details, please reference the respective document of mt-metricskit-delegates-* + * @constructor + */ +var ClickstreamProcessor = function ClickstreamProcessor(delegates) { + if (!reflect$1.isDefinedNonNull(delegates)) { + throw new Error('No delegate is provided to ClickstreamProcessor'); + } + + /** + ************************************ PRIVATE IVARS ************************************ + */ + + // @private + this._initCalled = false; + + // @private + this._delegatePackage = delegates; + + /** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + + /** + * Access the various "system" classes defined in ClickstreamProcessor. + * These classes are typically system/platform specific + * These classes can have their functionality replaced either en masse or piecemeal via the setDelegate() method. + * Available system classes are: + * environment + * eventRecorder + * logger + * @example: metrics.system.environment.setDelegate(myEnvironmentFunctions) + * @see setDelegate() + */ + this.system = new System(); + + /** + * Access the various configuration values. + * These are basically pretty wrappers on config fields retrieved via the "configValue" method. + * Note: Must be created in the constructor in order for a client to set a debug source before fetching config with init() + * @example: metrics.config.isDisabled() + */ + this.config = this._delegatePackage.config; + + /** + * Sub-library providing access to all the various eventHandlers of ClickstreamProcessor. + */ + this.eventHandlers = new EventHandlers(this); + + /** + * Sub-library providing access to all the various eventFields of ClickstreamProcessor. + */ + this.eventFields = new EventFields(this); + + /** + * Sub-library providing various utility functions which are useful when interacting with ClickstreamProcessor + */ + this.utils = new Utils(this); + + this._constraints = null; +}; + +/** + * Initialize ClickstreamProcessor - will register, fetch config, set delegates, etc. + * NOTE: This API should not be called more than once. Subsequent calls will have no effect (no-op). + * NOTE 2: This function initializes metrics config values which are required for proper event generation. + * This initialization will be asynchronous unless a configSourcesFn is provided. + * Events that are generated before initialization have completed will use config defaults. + * If clients want to ensure that config is fetched before events are generated, + * they can do one of the following when calling clickstreamProcessor.init(): + * (1) provide a configSourcesFn; in existing clients the config sources function is provided via the metrics.config.setDelegate() API + * (2) wait until the init() callback completes before generating and recording any events (Note that when using this method, + * any user actions that occured before the callback completes will have later eventTimes than when they actually occured) + * + * ADDITIONAL DESIGN NOTES: + * Promise/PubSub: + * These would require some polyfill or additional module support, which we do not want to bake into our framework. + * We will expect that clients will honor the init() contract by not calling it more than once, and we provide a completion callback. + * Deferring event generation: + * We could store a queue of events to generate once we have config. However, this is complex and error prone, and requires + * the eventRecorder to have special knowledge that this is happening and knowledge about how to properly generate those events + * (for example, the eventTime should reflect the time the deferred event was queued, not the time it was later generated). + * Using defaults: + * Most config values are common to the majority of apps and do not change very much, so in the interest of simplicity, + * we choose to fall back to default config values for any events generated before config resolves. + * The ingestion server will enrich clientId on events in a batch missing clientId. + * As an additional fallback, we store the most recently fetched config in localStorage for any future visits from the same browser. + * + * @returns {Promise} A Promise that resolved the MetricsKit initialization + */ +ClickstreamProcessor.prototype.init = function init() { + var initPromise = Promise.resolve(); + if (!this._initCalled) { + this._initCalled = true; + + // set any provided delegates + if (this._delegatePackage) { + reflect$1.setDelegates(this.eventHandlers, this._delegatePackage); + reflect$1.setDelegates(this.system, this._delegatePackage); + // TODO rdar://92551827 (Add test case for clickstream processor delegate initialization) + reflect$1.setDelegates(this.utils, this._delegatePackage); + } + + // Init config + initializeConfig(this.config); + initPromise = this._delegatePackage.init(); + + this._constraints = new Constraints(this.config, { + environment: this.system.environment + }); + } + + return initPromise; +}; + +/** + * Release resources from MetricsKit + */ +ClickstreamProcessor.prototype.cleanup = function cleanup() { + if (this._delegatePackage && reflect$1.isFunction(this._delegatePackage.cleanup)) { + // cleanup delegate before cleanup config, in case the cleanup method of delegate uses the config + this._delegatePackage.cleanup(); + } + cleanupConfig(this.config); + reflect$1.resetDelegates(this.eventHandlers); + reflect$1.resetDelegates(this.system); + reflect$1.resetDelegates(this.utils); + this.config = null; + this.system = null; + this.eventHandlers = null; + this.utils = null; + this._delegatePackage = null; + this._constraints = null; + this._initCalled = false; +}; + +export { ClickstreamProcessor }; diff --git a/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-utils-private/dist/mt-metricskit-utils-private.esm.js b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-utils-private/dist/mt-metricskit-utils-private.esm.js new file mode 100644 index 0000000..435731d --- /dev/null +++ b/shared/metrics-8/node_modules/@amp-metrics/mt-metricskit-utils-private/dist/mt-metricskit-utils-private.esm.js @@ -0,0 +1,2428 @@ +/* + * src/reflect.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PRIVATE METHODS/IVARS ************************************ + */ +var _nonOverrideableFunctions = { setDelegate: true }; + +/** + ************************************ PSEUDO-PRIVATE METHODS/IVARS ************************************ + * These functions need to be accessible for ease of testing, but should not be used by clients + */ +function _utResetNonOverridableFunctions() { + _nonOverrideableFunctions = { setDelegate: true }; +} + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Simple shallow clone which just copies over top-level keys and values (without "hasOwnProperty" checks). + * Useful for using a passed-in map without having that parameter data be corrupted by the function it's passed to. + * @param source + * @returns {object} will never return null... worst case will return an empty object. + */ +function shallowClone(source) { + var dest = {}; + var sourceHasGetterAndSetterMethods = hasGetterAndSetterMethods(source); + var aGetter; + var aSetter; + + for (var key in source) { + aGetter = null; + aSetter = null; + + if (sourceHasGetterAndSetterMethods) { + // Be careful to copy aGetter and setter methods properly, per: http://ejohn.org/blog/javascript-getters-and-setters/ + aGetter = source.__lookupGetter__(key); + aSetter = source.__lookupSetter__(key); + } + + if (aGetter || aSetter) { + if (aGetter) { + dest.__defineGetter__(key, aGetter); + } + if (aSetter) { + dest.__defineSetter__(key, aSetter); + } + } else { + dest[key] = source[key]; + } + } + return dest; +} + +/** + * Returns true if the specified obj is not undefined + * NOTE: Does not work for global variables (because the variable gets defined by virtue of passing it in)... use "typeof()" directly + */ +function isDefined(anObject) { + return typeof anObject !== 'undefined'; +} + +/** + * Returns true if the specified obj is not undefined and not null + * NOTE: Does not work for global variables... in that case, use "typeof()" directly, because the act of passing them to here will make them appear to be defined + */ +function isDefinedNonNull(anObject) { + return isDefined(anObject) && anObject !== null; +} + +/** + * Returns true if the specified obj is not undefined and not null and not empty + * NOTE: Does not work for global variables... in that case, use "typeof()" directly, because the act of passing them to here will make them appear to be defined + */ +function isDefinedNonNullNonEmpty(anObject) { + return ( + isDefinedNonNull(anObject) && !isEmptyString(anObject) && !isEmptyArray(anObject) && !isEmptyObject(anObject) + ); +} + +function isEmptyString(anObject) { + return isString(anObject) && anObject.length === 0; +} + +function isEmptyArray(anObject) { + return isArray(anObject) && anObject.length === 0; +} + +function isEmptyObject(anObject) { + return isObject(anObject) && Object.keys(anObject).length === 0; +} + +function isFunction(anObject) { + // the following works for regular functions and native functions, e.g. iTunes.buy + return typeof anObject === 'function'; +} + +function isNumber(anObject) { + return typeof anObject == 'number'; +} + +function isInteger(anObject) { + return isNumber(anObject) && anObject % 1 === 0; +} + +function isString(anObject) { + return typeof anObject === 'string' || anObject instanceof String; +} + +function isElement(anObject) { + return !!anObject && anObject.nodeType == 1; +} + +function isArray(anObject) { + return !!anObject && anObject.constructor === Array; +} + +function isObject(anObject) { + return !!anObject && anObject.constructor === Object; +} + +/* + * NOTE: this method skips object properties that are functions. + */ +function values(anObject) { + var values = []; + for (var property in anObject) { + var aValue = anObject[property]; + if (anObject.hasOwnProperty(property) && !isFunction(aValue)) { + values.push(aValue); + } + } + return values; +} + +function keys(anObject) { + var keys = []; + for (var property in anObject) { + if (anObject.hasOwnProperty(property) && !isFunction(anObject[property])) { + keys.push(property); + } + } + return keys; +} + +/** + * Returns "true" if the passed-in object has any values at all on it. + * NOTE: this method skips object properties that are functions. + */ +function hasAnyKeys(anObject) { + for (var aKey in anObject) { + if (anObject.hasOwnProperty(aKey)) { + return true; + } + } +} + +/** + * Returns "true" if the passed-in object has any values on it at all *and* at least one of those values is non-null. + * NOTE: this method skips object properties that are functions. + */ +function hasAnyNonNullKeys(anObject) { + for (var aKey in anObject) { + if (anObject.hasOwnProperty(aKey) && anObject[aKey]) { + return true; + } + } +} + +/** + * @return {boolean} true if the object has the default object getter and setter methods (e.g. __lookupGetter__()) + * see <rdar://problem/33045481> MetricsKit: Protect against JS errors when __lookupGetter__ is not on the object prototype + */ +function hasGetterAndSetterMethods(anObject) { + return ( + isObject(anObject) && + isFunction(anObject.__lookupGetter__) && + isFunction(anObject.__lookupSetter__) && + isFunction(anObject.__defineGetter__) && + isFunction(anObject.__defineSetter__) + ); +} + +/* + * NOTE: this method returns *only* object properties that are functions. + */ +function methods(anObject) { + var methods = []; + for (var property in anObject) { + var aValue = anObject[property]; + if (anObject.hasOwnProperty(property) && isFunction(aValue)) { + methods.push(aValue); + } + } + return methods; +} + +/* + * NOTE: this method skips object properties that are functions. + */ +function invert(anObject) { + var invertedMap = {}; + for (var property in anObject) { + if (anObject.hasOwnProperty(property) && !isFunction(anObject[property])) { + invertedMap[anObject[property]] = property; + } + } + return invertedMap; +} + +/** + * Adds all the fields of the objects in the varargs to the fields in the first parameter, "obj". + * *All* "hasOwnProperty" fields will be added, including functions and fields with no values. + * @param {Object} targetObject an object with keys and values. If only one parameter is provided, the return value will be the non-null values of that single object. + * @param {varargs} sourceObjectN a variable number of Object arguments from 0-N. Each object's fields will be copied into targetObject. Later objects take precedence over earlier ones. + * @return targetObject (*not* a clone) with the additional fields added.. + */ +function extend(targetObject /* , ...sourceObjectN(varargs) */) { + var argumentsArray = [true, true, true].concat(Array.prototype.slice.call(arguments)); + return copyKeysAndValues.apply(null, argumentsArray); +} + +/** + * Takes one or more objects, [possibly] cleans them (removes keys that are typeof 'function', keys with 'null' values, keys with 'undefined' values), + * merges them (later objects take precedence), and returns a single object with the union of all remaining fields. + * @param keepNullsAndUndefined a boolean if true fields with a "null" or "undefined" value will still be copied. + * Otherwise, if false, any field with a "null" or "undefined" value will not be copied. + * @param keepFunctions a boolean if true fields whose value is typeof 'function' will still be copied. + * Otherwise, if false, any field with a typeof 'function' value will not be copied. + * @param inPlace a boolean if true will copy all results to the "targetObject" object, rather than copying them all to a new object. + * Otherwise if "inPlace" is false, a new object will be returned and all passed in objects are treated as immutable and so will never be modified. + * @param {Object} targetObject an object with keys and values. If only one parameter is provided, the return value will be the non-null values of that single object. + * @param {varargs} sourceObjectN a variable number of Object arguments from 0-N. Each object's fields will be copied into targetObject. Later objects take precedence over earlier ones. + * @return either targetObject (if "inPlace" is true) or a new object (if "inPlace" is false or "targetObject" is null) with the + * union of all fields, filtered based on the values of the keepNullsAndUndefined and keepFunctions parameters + * @example copyKeysAndValues(true, {}) ===> {} + * @example copyKeysAndValues(true, null) ===> {} + * @example copyKeysAndValues({true, "foo":10}) ===> {"foo":10} + * @example copyKeysAndValues({true, "foo":10, "bar":null}) ===> {"foo":10} + * @example copyKeysAndValues({false, "foo":10, "bar":null}) ===> {"foo":10, "bar":null} + * @example copyKeysAndValues({true, "foo":10, "bar":null}, {"cat":null}) ===> {"foo":10} + * @example copyKeysAndValues({true, "foo":10, "bar":null}, {"cat":null, "mouse":"gray"}) ===> {"foo":10, "mouse":"gray"} + * @example copyKeysAndValues({true, "foo":10, "bar":null}, {"cat":null, "mouse":"gray", "dog":"bark"}) ===> {"foo":10, "mouse":"gray", "dog":"bark"} + * @example copyKeysAndValues({true, "foo":10, "bar":null}, {"cat":null, "mouse":"gray", "dog":"bark", "foo":11}) ===> {"foo":11, "mouse":"gray", "dog":"bark"} + * @example copyKeysAndValues({true, "foo":10, "bar":null}, {"cat":null, "mouse":"gray", "dog":"bark", "foo":11}, {"foo":12}) ===> {"foo":12, "mouse":"gray", "dog":"bark"} + */ +function copyKeysAndValues(keepNullsAndUndefined, keepFunctions, inPlace, targetObject /*, sourceObjectN(varargs)*/) { + var returnValue = inPlace ? targetObject || {} : {}; + var sourceObject; + + for (var ii = 3; ii < arguments.length; ii++) { + sourceObject = arguments[ii]; + + for (var key in sourceObject) { + if (Object.prototype.hasOwnProperty.call(sourceObject, key)) { + var value = sourceObject[key]; + + if (keepNullsAndUndefined || (value !== null && value !== undefined)) { + if (keepFunctions || typeof value !== 'function') { + returnValue[key] = value; + } + } + } + } + } + return returnValue; +} + +/** + * Add one or more function names to the list of non overrideable functions + * attachDelegate will check this list and not override any functions with matching names + * @param {Array} functionNames + * @returns undefined + */ +function addNonOverrideableFunctions(functionNames) { + for (var i = 0; i < functionNames.length; i++) { + var functionName = functionNames[i]; + _nonOverrideableFunctions[functionName] = true; + } +} + +/** + * Replace any "target" methods found on "delegate" with the delegate's version of the method. + * The replacement function will actually be our own wrapper function with the original function attached as a property called origFunction + * in case the delegate's replacement method wants to, essentially, call "super" + * We do delegation this way, vs. checking each time a "target" function is called, because this way we don't pollute the implementation + * of all the target's functions. + * Subsequent calls to "attachDelegate" will then replace whatever methods *they* match, including methods that have already been replaced. + * This allows callers to use "canned" delegates to get most of their functionality, but still replace some number of methods that need custom implementations. + * If a replaced method is overridden again with a subsequent "setDelegate" call, the "origFunction" parameter will be the previous delegate's function. + * NOTE: Only methods present on "target" will be replaced + * @param {Object} target + * @param {Object} delegate + * @param {Boolean} useOriginalContext whether to use the execution context of the original method for the delegate method. Default to false + * @returns {Boolean} true if one or more methods on the delegate object were attached to the target object + */ +function attachDelegate(target, delegate, useOriginalContext) { + var returnValue = false; + useOriginalContext = useOriginalContext || false; + + if (target && delegate && target !== delegate) { + // only attach methods that exist on the target + var methodsToOmitMap = {}; + Object.keys(delegate).forEach(function (methodName) { + if (!target[methodName]) { + methodsToOmitMap[methodName] = true; + } + }); + + // ignore nonOverrideableFunctions + extend(methodsToOmitMap, _nonOverrideableFunctions); + + returnValue = attachMethods(target, delegate, useOriginalContext ? target : null, methodsToOmitMap); + } + + return returnValue; +} + +/** + * Replace any "target" methods found on "source" with the source's version of the method. + * The replacement function will actually be our own wrapper function with the original function attached as a property called origFunction + * in case the source's replacement method wants to, essentially, call "super" + * We do delegation this way, vs. checking each time a "target" function is called, because this way we don't pollute the implementation + * of all the target's functions. + * Subsequent calls to "attachMethods" will then replace whatever methods *they* match, including methods that have already been replaced. + * This allows callers to use "canned" sources to get most of their functionality, but still replace some number of methods that need custom implementations. + * If a replaced method is overridden again with a subsequent "attachMethod" call, the "origFunction" parameter will be the previous source's function. + * @param {Object} target + * @param {Object} source + * @param {Object} (optional) methodContext 'this' context to apply to any bound methods; defaults to the source object + * @param {Object} (optional) methodsToOmitMap a dictionary of method names to ignore when copying + * @returns {Boolean} true if one or more methods on the source object were attached to the target object + */ +function attachMethods(target, source, methodContext, methodsToOmitMap) { + var returnValue = false; + + if (target && source) { + methodsToOmitMap = methodsToOmitMap || {}; + methodContext = methodContext || source; + + var captureFunction = function captureFunction( + methodContext, + capturedOrigFunction, + source, + origFunctionContext, + functionName + ) { + var returnValue = function () { + // dereference the source so delegate chaining (source object delegating to another object) works properly + return source[functionName].apply(methodContext, arguments); + }; + // Attach the origFunction in case its needed later by the caller for delegate chaining. + // Do that in here too, so it doesn't get overwritten in the loop... + if (capturedOrigFunction) { + returnValue.origFunction = capturedOrigFunction; + } + // we need to add a symbol to the attached function that doesn't have an original function, in order to remove them by "detachMethods()". + returnValue.attachedMethod = true; + + return returnValue; + }; + + for (var functionName in source) { + if (!(functionName in methodsToOmitMap)) { + if (source[functionName] && isFunction(source[functionName])) { + var origFunction = target[functionName]; + var origFunctionExists = origFunction && isFunction(origFunction); + var capturedOrigFunction = null; + if (origFunctionExists) { + // Avoid binding the delegate function repeatedly + // We need to avoid binding the delegated function in order to the "detachMethods" function could reset the delegated function to the original function by the "origFunction" chain. + // And the delegated function's context was already bound in captureFunction. + if (origFunction.attachedMethod === true) { + capturedOrigFunction = origFunction; + } else { + capturedOrigFunction = origFunction.bind(target); + } + } + + // Careful! This is that tough "closure in a loop" case where the local variables captured by the closure + // will change even for previously created closure functions on each iteration of this loop! + // So, we need to capture it in an additional, invoked, closure... + // See (and cited links): http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example + target[functionName] = captureFunction( + methodContext, + capturedOrigFunction, + source, + target, + functionName + ); + returnValue = true; + } + } + } + } + return returnValue; +} + +/** + * detach the any of delegated methods(the functions which have 'attachedMethod' symbol) from the provided object + * @param target + * @returns {boolean} true if one or more delegate methods were detached from the target object + */ +function detachMethods(target) { + var returnValue = false; + for (var propKey in target) { + if (isFunction(target[propKey]) && target[propKey].attachedMethod === true) { + var methodIsDelegated = target[propKey].origFunction; + if (methodIsDelegated) { + while (target[propKey].origFunction) { + target[propKey] = target[propKey].origFunction; + returnValue = true; + } + } else { + delete target[propKey]; + } + } + } + return returnValue; +} + +/** + * Convenience method to loop over a set of delegatable targets and apply appropriate delegates + * @param {Object} targetMap containing one or more delegatable target objects + * @param {Object} delegateMap containing one or more delegates + * @return {Object} contains the results of all setDelegate calls + * @example usage: + * var targetMap = { environment: environmentObject, eventRecorder: eventRecorderObject, foo: fooObject }; + * var delegateMap = { environment: environmentDelegate, eventRecorder: eventRecorderDelegate }; + * setDelegates(targetMap, delegateMap); // returns { environment: true, eventRecorder: true } + * // targetMap.environment's delegate is now environmentDelegate and targetMap.eventRecorder's delegate is now eventRecorderDelegate + */ +function setDelegates(targetMap, delegateMap) { + var resultObject = {}; + + for (var targetName in targetMap) { + if (delegateMap[targetName] && isFunction(targetMap[targetName].setDelegate)) { + resultObject[targetName] = targetMap[targetName].setDelegate.apply(targetMap[targetName], [delegateMap[targetName]].concat(Array.prototype.slice.call(arguments, 2))); + } + } + + return resultObject; +} + +/** + * Reset the delegate functions from the objects of provided target map. + * @param {Object} targetMap containing one or more delegatable target objects + * @returns {Boolean} true if one or more delegate methods were reset from the target object + */ +var resetDelegates = function resetDelegates(targetMap) { + var returnValue = false; + + for (var targetName in targetMap) { + var delegateObject = targetMap[targetName]; + if (delegateObject && typeof delegateObject === 'object' && isFunction(delegateObject.setDelegate)) { + returnValue |= detachMethods(delegateObject); + } + } + return !!returnValue; +}; + +/** + * @param {Object} sourceObject + * @param {Object} targetObject + * @returns {Boolean} true if one or more methods on the target was delegated, false otherwise + * If one (or more) of the source object's methods has been delegated to a function, + * the same method on the target object will also be delegated to that function + */ +function copyDelegatedFunctions(sourceObject, targetObject) { + var returnValue = null; + + if (sourceObject && targetObject && targetObject.setDelegate) { + var delegatedFunctions = {}; + var functionName; + + for (functionName in sourceObject) { + if (isFunction(sourceObject[functionName]) && sourceObject[functionName].origFunction) { + delegatedFunctions[functionName] = sourceObject[functionName]; + } + } + + returnValue = targetObject.setDelegate(delegatedFunctions); + } + + return returnValue; +} + +/** + * Returns a deduped array (similar to a Set object) using the contents from both arrayA and arrayB + * @param {Array} arrayA the first array to dedupe + * @param {Array} arrayB the other array to dedupe + * @return {Array} The deduped array + */ +function dedupedArray(arrayA, arrayB) { + var tempDict = {}; // necessary for returning array of unique values and not having access to Set object in ES5 + if (arrayA) { + for (var i = 0; i < arrayA.length; i++) { + tempDict[arrayA[i]] = 0; // value for key doesn't matter; we want a list of unique values + } + } + if (arrayB) { + for (var j = 0; j < arrayB.length; j++) { + tempDict[arrayB[j]] = 0; // value for key doesn't matter; we want a list of unique values + } + } + return Object.keys(tempDict); // equivalent to a Set of unique values +} + +/** + * Returns the global scope object associated with the platform + */ +function globalScope() { + if (typeof globalThis !== 'undefined') { + return globalThis; + } else if (typeof global !== 'undefined') { + return global; + } else if (typeof window !== 'undefined') { + return window; + } else { + return {}; + } +} + +var reflect = /*#__PURE__*/Object.freeze({ + __proto__: null, + _utResetNonOverridableFunctions: _utResetNonOverridableFunctions, + shallowClone: shallowClone, + isDefined: isDefined, + isDefinedNonNull: isDefinedNonNull, + isDefinedNonNullNonEmpty: isDefinedNonNullNonEmpty, + isEmptyString: isEmptyString, + isEmptyArray: isEmptyArray, + isEmptyObject: isEmptyObject, + isFunction: isFunction, + isNumber: isNumber, + isInteger: isInteger, + isString: isString, + isElement: isElement, + isArray: isArray, + isObject: isObject, + values: values, + keys: keys, + hasAnyKeys: hasAnyKeys, + hasAnyNonNullKeys: hasAnyNonNullKeys, + hasGetterAndSetterMethods: hasGetterAndSetterMethods, + methods: methods, + invert: invert, + extend: extend, + copyKeysAndValues: copyKeysAndValues, + addNonOverrideableFunctions: addNonOverrideableFunctions, + attachMethods: attachMethods, + detachMethods: detachMethods, + attachDelegate: attachDelegate, + setDelegates: setDelegates, + resetDelegates: resetDelegates, + copyDelegatedFunctions: copyDelegatedFunctions, + dedupedArray: dedupedArray, + globalScope: globalScope +}); + +/* + * src/backoff.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +var DEFAULTS = { + exponential: { + maxWait: 1500, + initialDelay: 100, + factor: 2 + } +}; + +/** + * @param {int} (optional) initialDelay - time in ms before the first reattempt (each subsequent reattempt will wait exponentially longer than the previous one) + * @param {int} (optional) maxWait - max cumulative time in ms to wait before giving up (does not include the time taken by the function to execute) + setting this to 0 will cause the strategy to retry indefinitely + * @param {int} (optional) factor - multiplier to apply when delaying subsequent reattempts. Defaults to DEFAULT_EXPONENT_FACTOR + */ +var ExponentialStrategy = function (initialDelay, maxWait, factor) { + this.delay = initialDelay || DEFAULTS.exponential.initialDelay; + this.maxWait = isNumber(maxWait) ? maxWait : DEFAULTS.exponential.maxWait; + this.factor = factor || DEFAULTS.exponential.factor; + + this.timeWaited = 0; +}; + +ExponentialStrategy.prototype.nextDelay = function nextDelay() { + var returnValue = null; + + var timeRemaining = this.maxWait - this.timeWaited; + + if (timeRemaining > 0) { + this.delay = Math.min(this.delay, timeRemaining); + this.timeWaited += this.delay; + } + + if (this.maxWait === 0 || timeRemaining > 0) { + returnValue = this.delay; + this.delay = this.delay * this.factor; // increase the delay for next time + } + + return returnValue; +}; + +/** + * Execute a function according to a given backoff failure strategy + * @param {Object} strategy is an object representing a failure stategy. It has a nextDelay() method that returns the time in ms to wait until reattempting + * @param {Function} networkRequestor - the function to execute. It should accept an onSuccessHandler and an onFailureHandler as its final arguments + * @param {Function} onSuccessHandler - callback to execute on success + * @param {Function} onFailureHandler - callback to execute on failure + */ +function _backoff(strategy, networkRequestor, onSuccessHandler, onFailureHandler) { + var onBackoff = function onBackoff() { + var delay = strategy.nextDelay(); + if (delay) { + setTimeout(_backoff.bind(null, strategy, networkRequestor, onSuccessHandler, onFailureHandler), delay); + } else { + onFailureHandler.apply(onFailureHandler, arguments); + } + }; + + networkRequestor.call(networkRequestor, onSuccessHandler, onBackoff); +} + +/** + * Execute a function according to an exponential backoff failure strategy + * @param {Function} networkRequestor - the function to execute. It should accept an onSuccessHandler and an onFailureHandler as its final arguments + * @param {Function} onSuccessHandler - callback to execute on success + * @param {Function} onFailureHandler - callback to execute on failure + * @param {int} (optional) initialDelay - time in ms before the first reattempt (each subsequent reattempt will wait exponentially longer than the previous one) + * @param {int} (optional) maxWait - max cumulative time in ms to wait before giving up (does not include the time taken by the function to execute) + setting this to 0 will cause the strategy to retry indefinitely + * @param {int} (optional) factor - multiplier to apply when delaying subsequent reattempts. Defaults to DEFAULT_EXPONENT_FACTOR + */ +function exponentialBackoff(networkRequestor, onSuccessHandler, onFailureHandler, initialDelay, maxWait, factor) { + var strategy = new ExponentialStrategy(initialDelay, maxWait, factor); + _backoff(strategy, networkRequestor, onSuccessHandler, onFailureHandler); +} + +var backoff = /*#__PURE__*/Object.freeze({ + __proto__: null, + exponentialBackoff: exponentialBackoff +}); + +/* + * src/number.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * "De-res" a number (lower the resolution of the number) per the Privacy Team and these radars: + * <rdar://problem/17423020> Add "capacityXXX" fields to UserXP Figaro reporting. + * <rdar://problem/23571925> Privacy: De-res capacityXXX fields + * Default behavior will de-res numbers by a magnitude of 1024^2 ie. bytes to megabytes and remove the last two significant digits + * For example, a raw number of bytes "de-res"'d to MB, but without the "floor" filter, would look like these examples: + * 31708938240/1024/1024 ==> 30240 + * 15854469120/1024/1024 ==> 15120 + * 63417876480/1024/1024 ==> 60480 + * + * With the "floor" formula we replace the two least significant digits with "00" + * Doing so will convert values like so: + * + * 31708938240/1024/1024 ==> 30200 + * 15854469120/1024/1024 ==> 15100 + * 63417876480/1024/1024 ==> 60400 + * + * @param {number} aNumber + * @param {number} (optional) magnitude, must be greater than 0. default 1024^2 + * @param {number} (optional) significantDigits to remove, must be a positive integer or 0. default 2 + * @returns {number} if the "aNumber" parameter is absent, the return value will be undefined. + * If any of the arguments are disallowed values, the value "NaN" will be returned. + * @overridable + */ +function deResNumber(aNumber, magnitude, significantDigits) { + var returnValue = undefined; + + if (isDefined(aNumber)) { + if (!isDefined(magnitude)) { + magnitude = 1024 * 1024; + } + if (!isDefined(significantDigits)) { + significantDigits = 2; + } + + if ( + isNumber(aNumber) && + isNumber(magnitude) && + magnitude > 0 && + isInteger(significantDigits) && + significantDigits >= 0 + ) { + var roundFactor = Math.pow(10, significantDigits); + var roundOperation = aNumber > 0 ? 'floor' : 'ceil'; + + returnValue = Math[roundOperation](aNumber / magnitude / roundFactor) * roundFactor; + } else { + returnValue = NaN; + } + } + + return returnValue; +} + +var number = /*#__PURE__*/Object.freeze({ + __proto__: null, + deResNumber: deResNumber +}); + +/* + * src/config.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + * @deprecated This class will be removed in the next Major release because the methods of this class have been moved to the mt-client-config/src/config/metrics_config.js + * Config utility methods + * IMPORTANT: These methods should be called within the context of a Config instance that implements a value() method, + * such as @amp/mt-client-config (the singleton or any instance created by it). + * They can be attached via reflect.attachMethods, passing the config instance as the methodContext + * @example: + * var Config = require('@amp/mt-client-config'); + * var configUtils = ( ... ); // this file + * reflect.attachMethods(Config, configUtils, Config); + * Failing to use the correct context will result in thrown errors ("this.value is not a function"). + */ + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * @deprecated + * Boolean config value which, when "true", tells clients to avoid all metrics code paths (different than simply not sending metrics). + * Useful for avoiding discovered client bugs. + * NOTE1: This will cause unrecoverable event loss, as the clients will not be recording events at all. + * NOTE2: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded. + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {boolean} + */ +function disabled(topic) { + return this.value('disabled', topic) ? true : false; +} + +/** + * @deprecated - Deprecated Language, use denylist instead + * Array config value which instructs clients to avoid sending particular event types. + * Useful for reducing server processing in emergencies by abandoning less-critical events. + * Useful for dealing with urgent privacy concerns, etc., around specific events. + * NOTE1: This will cause unrecoverable event loss, as the clients will not be recording events at all. + * NOTE2: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Array} Guaranteed to always return a valid array, though it may be empty if the value was unset in config + */ +function blacklistedEvents(topic) { + return denylistedEvents.call(this, topic); +} + +/** + * @deprecated + * Array config value which instructs clients to avoid sending particular event types. + * Useful for reducing server processing in emergencies by abandoning less-critical events. + * Useful for dealing with urgent privacy concerns, etc., around specific events. + * NOTE1: This will cause unrecoverable event loss, as the clients will not be recording events at all. + * NOTE2: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * NOTE3: To honor both old blacklistedEvents configs and new denylistedEvents Configs, we'll merge the blacklistedEvents config with the denylistedEvents config. + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Array} Guaranteed to always return a valid array, though it may be empty if the value was unset in config + */ +function denylistedEvents(topic) { + var denylistedEventsArray = this.value('denylistedEvents', topic); + var blacklistedEventsArray = this.value('blacklistedEvents', topic); + return dedupedArray(blacklistedEventsArray, denylistedEventsArray); +} + +/** + * @deprecated + * Array config value which instructs clients to avoid sending particular event fields. + * Useful for dealing with urgent privacy concerns, etc., around specific event fields (e.g. dsid) + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Array} Guaranteed to always return a valid array, though it may be empty if the value was unset in config + */ +function blacklistedFields(topic) { + return denylistedFields.call(this, topic); +} + +/** + * @deprecated + * Array config value which instructs clients to avoid sending particular event fields. + * Useful for dealing with urgent privacy concerns, etc., around specific event fields (e.g. dsid) + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * NOTE2: To honor both old blacklistedFields configs and new denylistedFields configs, we'll merge the blacklistedEvents config with the denylistedEvents config. + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Array} Guaranteed to always return a valid array, though it may be empty if the value was unset in config + */ +function denylistedFields(topic) { + var denylistedFieldsArray = this.value('denylistedFields', topic); + var blacklistedFieldsArray = this.value('blacklistedFields', topic); + return dedupedArray(blacklistedFieldsArray, denylistedFieldsArray); +} + +/** + * @deprecated + * Remove all blacklisted fields from the passed-in object. + * IMPORTANT: This action is performed in-place for performance of not having to create new objects each time. + * NOTE: Typically all event_handlers will call this in addition to "recordEvent()" calling it because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {Object} eventFields a dictionary of event data + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Object} the passed-in object with any blacklisted fields removed + */ +function removeBlacklistedFields(eventFields, topic) { + return removeDenylistedFields.call(this, eventFields, topic); +} + +/** + * @deprecated + * Remove all denylisted fields from the passed-in object. + * IMPORTANT: This action is performed in-place for performance of not having to create new objects each time. + * NOTE: Typically all event_handlers will call this in addition to "recordEvent()" calling it because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {Object} eventFields a dictionary of event data + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Object} the passed-in object with any denylisted fields removed + */ +function removeDenylistedFields(eventFields, topic) { + if (eventFields) { + var denylistedFieldsArray = denylistedFields.call(this, topic); + + for (var ii = 0; ii < denylistedFieldsArray.length; ii++) { + var aDenylistedField = denylistedFieldsArray[ii]; + // Double check this is not null (or empty string), or "delete" will blow up... + if (aDenylistedField) { + if (aDenylistedField in eventFields) { + delete eventFields[aDenylistedField]; + } + } + } + } + return eventFields; +} + +/** + * @deprecated + * Convenience function used by event handlers to determine if they should build and return metricsData. + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} anEventType + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Boolean} returns "true" if <b>either</b> "disabled()" is true or "denylistedEvents()" contains this eventType + */ +function metricsDisabledOrBlacklistedEvent(anEventType, topic) { + return metricsDisabledOrDenylistedEvent.call(this, anEventType, topic); +} + +/** + * @deprecated + * Convenience function used by event handlers to determine if they should build and return metricsData. + * NOTE: Typically all event_handlers will check for this in addition to "recordEvent()" checking because that way + * if a client overrides "recordEvent", these checks will still take effect. + * We also test it in recordEvent() in case someone creates their own event_handler, we'd still want to exclude what needs to be excluded, in case they don't. + * @param {String} anEventType + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Boolean} returns "true" if <b>either</b> "disabled()" is true or "denylistedEvents()" contains this eventType + */ +function metricsDisabledOrDenylistedEvent(anEventType, topic) { + var returnValue = + disabled.call(this, topic) || + (anEventType ? denylistedEvents.call(this, topic).indexOf(anEventType) > -1 : false); + + return returnValue; +} + +/** + * @deprecated + * Config map which instructs clients to de-res (lower the resolution of) particular event fields. + * The Privacy team typically requires device capacity information to be de-resed. + * @param {String} (optional) topic the Figaro topic to use to look up config values + * @returns {Array} An array of config objects { fieldName, (optional) magnitude, (optional) significantDigits } + * Guaranteed to always return a valid array, though it may be empty if the value was unset in config + */ +function deResFields(topic) { + var returnArray = this.value('deResFields', topic); + + return returnArray || []; +} + +/** + * @deprecated + * De-res appropriate fields in the passed-in object by lowering the resolution of those field values. + * For example, a raw number of bytes "de-res"'d to MB, but without the "floor" filter, would look like these examples: + * 31708938240/1024/1024 ==> 30240 + * 15854469120/1024/1024 ==> 15120 + * 63417876480/1024/1024 ==> 60480 + * + * With the "floor" formula we replace the two least significant digits with "00" + * Doing so will convert values like so: + * + * 31708938240/1024/1024 ==> 30200 + * 15854469120/1024/1024 ==> 15100 + * 63417876480/1024/1024 ==> 60400 + * + * IMPORTANT: This action is performed in-place for performance of not having to create new objects each time. + * NOTE: Be careful not to call this method more than once for a given event, as de-resing a number more than + * once can lead to inaccurate reporting (numbers will likely be smaller than their real values) + * @param {Object} eventFields a dictionary of event data + * @param {String} (optional) topic the Figaro topic to use to look up de-res config values + * @returns {Object} the passed-in object with any fields de-resed + */ +function applyDeRes(eventFields, topic) { + if (eventFields) { + var deResFieldsConfigArray = deResFields.call(this, topic); + var fieldName; + + deResFieldsConfigArray.forEach(function (deResFieldConfig) { + fieldName = deResFieldConfig.fieldName; + if (fieldName in eventFields) { + eventFields[fieldName] = deResNumber( + eventFields[fieldName], + deResFieldConfig.magnitude, + deResFieldConfig.significantDigits + ); + } + }); + } + return eventFields; +} + +var config = /*#__PURE__*/Object.freeze({ + __proto__: null, + disabled: disabled, + blacklistedEvents: blacklistedEvents, + denylistedEvents: denylistedEvents, + blacklistedFields: blacklistedFields, + denylistedFields: denylistedFields, + removeBlacklistedFields: removeBlacklistedFields, + removeDenylistedFields: removeDenylistedFields, + metricsDisabledOrBlacklistedEvent: metricsDisabledOrBlacklistedEvent, + metricsDisabledOrDenylistedEvent: metricsDisabledOrDenylistedEvent, + deResFields: deResFields, + applyDeRes: applyDeRes +}); + +/* + * src/string.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + **************************** PUBLIC METHODS/IVARS **************************** + */ + +/** Canned alphabets for use with convertNumberToBaseAlphabet + * Users can create their own alphabets/bases, e.g. "base61Alphabet", + * by truncating characters from the below, pre-defined, alphabets) + */ +var base10Alphabet = '0123456789'; +var base16Alphabet = base10Alphabet + 'ABCDEF'; +var base36Alphabet = base10Alphabet + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; +var base61Alphabet = base36Alphabet + 'abcdefghijklmnopqrstuvwxy'; +var base62Alphabet = base61Alphabet + 'z'; + +/** + * Test if mainString starts with subString. + * Optionally specify boolean "ignoreCase". + * @param mainString + * @param subString + * @param ignoreCase + * @returns {boolean} "false" if "mainString" or "subString" are null. + */ +function startsWith(mainString, subString, ignoreCase) { + var returnValue = false; + + if (mainString && subString) { + mainString = mainString.substr(0, subString.length); + if (ignoreCase) { + mainString = mainString.toLowerCase(); + subString = subString.toLowerCase(); + } + returnValue = mainString.indexOf(subString) === 0; + } + return returnValue; +} + +/** + * Test if one string ends with another. + * @param mainString + * @param subString + * @param ignoreCase + * @returns {boolean} "false" if "mainString" or "subString" are null. + */ +function endsWith(mainString, subString, ignoreCase) { + var returnValue = false; + if (mainString && subString) { + if (ignoreCase) { + mainString = mainString.toLowerCase(); + subString = subString.toLowerCase(); + } + // These two lines of logic (the guts) are the implementation from Prototype.js, which is well-optimized and well-tested. + var endIndex = mainString.length - subString.length; + returnValue = endIndex >= 0 && mainString.lastIndexOf(subString) === endIndex; + } + return returnValue; +} + +/** + * Removes characters in the passed-in charString from the front and back of baseString + * + * If no "charsString" is passed in (or it's non-null but identical to stringWhitespace), it tries to use the browser-platform-native "trim()" if found, otherwise trims the stringWhitespace characters (which are the same set trimmed by the built-in function): + * If a non-null "charsString" string is passed in, it will try to remove all characters within that string, regardless of their order. + * + * (NOTE: These come from WebKit's built-in "trim()" method, who's testcase lives here: + * http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/third_party/string-trim.js?spec=svn3842&r=3052 + * '\u0009' (HORIZONTAL TAB) + * '\u000A' (LINE FEED OR NEW LINE) + * '\u000B' (VERTICAL TAB) + * '\u000C' (FORMFEED) + * '\u000D' (CARRIAGE RETURN) + * '\u0020' (SPACE) + * '\u00A0' (NO-BREAK SPACE) + * '\u2000' (EN QUAD) + * '\u2001' (EM QUAD) + * '\u2002' (EN SPACE) + * '\u2003' (EM SPACE) + * '\u2004' (THREE-PER-EM SPACE) + * '\u2005' (FOUR-PER-EM SPACE) + * '\u2006' (SIX-PER-EM SPACE) + * '\u2007' (FIGURE SPACE) + * '\u2008' (PUNCTUATION SPACE) + * '\u2009' (THIN SPACE) + * '\u200A' (HAIR SPACE) + * '\u3000' (IDEOGRAPHIC SPACE) + * '\u2028' (LINE SEPARATOR) + * '\u2029' (PARAGRAPH SEPARATOR) + * '\u200B' (ZERO WIDTH SPACE (category Cf)'} + * NOTE: If you pass a custom "charString" and want whitespace removed as well, be sure to include the whitespace string as well + * Examples: " hello world ".trim() -> "hello world" -- " e hello world f".trim(stringWhitespace+"ef") -> "hello world" + * @param basestring is the string to trim + * @param If no "charsString" is passed in (or it's non-null but identical to stringWhitespace), it tries to use the browser-platform-native "trim()" if found, otherwise trims the stringWhitespace characters (which are the same set trimmed by the built-in function): + * If a non-null "charsString" string is passed in, it will try to remove all characters within that string, regardless of their order. + * @param forceNonNativeTrim is mostly used for testing purposes, but does what it says. + */ +function trim(baseString, charString, forceNonNativeTrim) { + var returnValue = null; + var stringWhitespace = + '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u3000\u2028\u2029\u200B'; + var whitespaceTrimStartRegex = new RegExp('^[' + stringWhitespace + ']+'); // No need to create a new one of these objects on each call! + var whitespaceTrimEndRegex = new RegExp('[' + stringWhitespace + ']+$'); // No need to create a new one of these objects on each call! + + if (baseString) { + if (!forceNonNativeTrim && (!charString || charString == stringWhitespace) && baseString.trim) { + // Use browser-built-in trim, if it exists... + returnValue = baseString.trim(); + } else { + // NOTE: IF YOU MODIFY THIS METHOD, COPY AND TEST THE MODIFICATION TO itmsCheck.js WHICH HAS A COPY/PASTED VERSION (SANS COMMENTS) + var trimChars = null; + var startRegex = null; + var endRegex = null; + + if (charString && typeof charString !== 'undefined') { + // This is bits and pieces combined together from here: http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript + charString = charString.replace(/([.?*+^$[\]\\(){}-])/g, '\\$1'); // If we don't do this, then if "mainString" has .'s, or other regex chars in it, the regex interprets them as part of the regex! + trimChars = '[' + charString + ']'; + startRegex = new RegExp('^' + trimChars + '+'); + endRegex = new RegExp(trimChars + '+$'); + } else { + trimChars = stringWhitespace; + startRegex = whitespaceTrimStartRegex; + endRegex = whitespaceTrimEndRegex; + } + var str = baseString.replace(startRegex, ''); + returnValue = str.replace(endRegex, ''); + } + } + return returnValue; +} + +/** + * Changes snake_case "source" string to lowerCamelCase or UpperCamelCase + * @param {String} source underscore separated sentence/list of words + * @param {Boolean} upperCamelCase - optional parameter specifying whether to capitalize the first letter, defaults to false + * @return {String} result the source parameter in lower or upper camel case + */ +function snakeCaseToCamelCase(source, upperCamelCase) { + var result = ''; + if (source) { + var words = source.toLowerCase().split('_'); + var firstChar; + + for (var i = 0; i < words.length; i++) { + firstChar = words[i][0]; + if (i !== 0 || upperCamelCase) { + firstChar = firstChar.toUpperCase(); + } + result += firstChar + words[i].slice(1); + } + } + return result; +} + +/** + * Changes snake_case "source" string to UpperCamelCase + * @param {String} source Underscore separated sentence/list of words + * @return {String} result The source parameter in upper camel case + */ +function snakeCaseToUpperCamelCase(source) { + return snakeCaseToCamelCase(source, true); +} + +/** + * Turns an object into a query param string + * @param {Object} params is the set of key-value pairs to turn into a query param string. + * @returns {String} a query param string with URI-encoded values created using the key-value pairs in the passed in object. e.g. "app=com.apple.Safari&testValue=test&eventTime=14927450" + * NOTE: The first key of the returned string is never prefaced, not with an ampersand (&) or a question mark (?) + * @example + * var paramString = _utils.string.paramString({ + * app: 'com.apple.Safari', + * testValue: 'test', + * eventTime: 14927450 + * }); + */ +function paramString(params) { + var paramString = ''; + var delimiter = ''; + var firstKey = true; + + for (var key in params) { + var value = params[key]; + if (value || value === 0 || value === false) { + paramString += delimiter + key + '=' + encodeURIComponent(value); + if (firstKey) { + delimiter = '&'; + firstKey = false; + } + } + } + return paramString; +} + +function exceptionString(className, methodName) { + return ( + 'The function ' + + className + + '.' + + methodName + + '() must be overridden with a platform-specific delegate function.' + + 'If you have no data for this function, have your delegate return null ' + + "or undefined (no 'return')" + ); +} + +/** + * Parses a user agent string for a particular product name and returns its version + * @param {String} userAgent that conforms with RFC 7231 section 5.5.3 regarding User-Agents + * @param {String} (optional) productName the name of a product identifier to search for e.g. 'iTunes'; if omitted, defaults to the first identifier + * @return {String} the version of the product, or null if none found + * @example + * versionStringFromUserAgent('iTunes/12.6 (Macintosh; OS X 10.12.4) AppleWebKit/603.1.30.0.34') returns '12.6' + * versionStringFromUserAgent('iTunes/12.6 (Macintosh; OS X 10.12.4) AppleWebKit/603.1.30.0.34', 'AppleWebKit') returns '603.1.30.0.34' + * versionStringFromUserAgent('iTunes/12.6 (Macintosh; OS X 10.12.4) AppleWebKit/603.1.30.0.34', 'Macintosh') returns null + * (strings contained in parentheses are counted as comments, not product identifiers) + */ +function versionStringFromUserAgent(userAgent, productName) { + var returnValue = null; + + productName = productName || '\\S+'; // default to the first product name + + var re = new RegExp('\\b' + productName + '/(\\S+)\\b', 'i'); + var match = re.exec(userAgent); + + if (match && match[1]) { + returnValue = match[1]; + } + + return returnValue; +} + +/** + * Takes a client ID (universally unique per device) and generates another UUID that is unique per request + * TODO: have a fallback in case clientId is unavailable + * @param {string} clientId, a base-61 UUID that uses 'z' as a delimiter + * @return {string} A generated UUID, to be used in visit stitching + */ +function requestId(clientId) { + // NOTE: The reason we integrate "clientId" into this requestId (uuid) is because there is no itms.crypto functionality in ITMLKit for creating robust UUIDs, so we + // leverage off the fact that "clientId" was created cryptographically strong in Java. + var delimiter = 'z'; + var epochTime = Date.now(); + var randomNum = Math.floor(Math.random() * 100000); + + // convert to base 36, and use uppercase since 'z' is a delimiter in clientId + epochTime = epochTime.toString(36).toUpperCase(); + randomNum = randomNum.toString(36).toUpperCase(); + + return clientId + delimiter + epochTime + delimiter + randomNum; +} + +/** + * Generates a RFC4122-compliant UUID (v4) + * See https://tools.ietf.org/html/rfc4122 + * For a discussion on the probability of collisions of version 4 UUIDs, see: + * https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions + * @param {Function} (optional) pseudoRNG a function that returns a pseudo random number between 0 and 1 + * defaults to a cryptographically strong PRNG when available or Math.random() + * which is not cryptographically strong, but can be used where a small number of collisions are acceptable + * @return {String} + * TODO: consider optimizing to use fewer calls to randomHexCharacter (i.e. switch to a randomHexString method) + */ +function uuid(pseudoRNG) { + var template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; + var uuid = ''; + var character; + + for (var i = 0, len = template.length; i < len; i++) { + character = template.charAt(i); + + if (character === 'x') { + uuid += randomHexCharacter(pseudoRNG); + } else if (character === 'y') { + uuid += randomHexCharacter(pseudoRNG, '8', 'b'); + } else { + uuid += character; + } + } + + return uuid; +} + +/** + * Generates a random hexdecimal character between 0 and f + * @param {Function} (optional) a pseudo random number generator that returns a value between 0 and 1 + * defaults to a cryptographically strong PRNG when available or Math.random() + * which is not cryptographically strong, but can be used where a small number of collisions are acceptable + * @param {String} (optional) min the lowest character (0-f) to include, inclusive + * @param {String} (optional) max the highest character (0-f) to include, inclusive + * @return {String} + */ +function randomHexCharacter(pseudoRNG, min, max) { + var globalObject = globalScope(); + var cryptoObject = globalObject.crypto || globalObject.msCrypto; + var randomCharacter; + + if (pseudoRNG) { + randomCharacter = ((pseudoRNG() * 16) | 0).toString(16); + } else if (cryptoObject && cryptoObject.getRandomValues) { + randomCharacter = (cryptoObject.getRandomValues(new Uint8Array(1))[0] & 15).toString(16); + } else if (cryptoObject && cryptoObject.randomBytes) { + randomCharacter = cryptoObject.randomBytes(1).toString('hex')[0]; + } else { + randomCharacter = ((Math.random() * 16) | 0).toString(16); + } + + // rejection sampling: if character not in desired range, generate another one + if (min && max && (randomCharacter < min || randomCharacter > max)) { + randomCharacter = randomHexCharacter(pseudoRNG, min, max); + } + + return randomCharacter; +} + +/** + * Adapted from MTStringUtil.java, which copied from MZStringUtil.java. + * Base-2 to base-62 target alphabets are accepted. + * Alphabet order must be 0-9, then "A" stands for 10, "Z" for 35, "a" (lower-case) for 36 and "z" (lower-case) for 61. + * Not sure if there is any standard for displaying numbers higher than base-36. Lowercase letters are used to go up + * to base-62. + * + * @param {Number} number a POSITIVE base 10 number to convert + * @param {String} targetAlphabet indicates the base of the target value (by virtue of the length of the alphabet) + * as well as the characters to use during conversion. "Canned" alphabets are provided, + * but homegrown alphabets may be used by truncating values from canned alphabets. + * @return {String} a string that has been converted to targetAlphabet + */ +function convertNumberToBaseAlphabet(number, targetAlphabet) { + var returnValue = ''; + var targetRadix = targetAlphabet.length; + + if (targetRadix <= 36) { + returnValue = number.toString(targetRadix).toUpperCase(); + } else { + var remainder; + var charForRemainder; + var charArray = []; + + while (number > 0) { + remainder = number % targetRadix; + charForRemainder = targetAlphabet.charAt(remainder); + charArray.push(charForRemainder); + number = (number - remainder) / targetRadix; + } + + returnValue = charArray.reverse().join(''); + } + + if (returnValue === '') { + returnValue = '0'; + } + + return returnValue; +} + +/** + * Generates a random base62 string. If strong crypto is available, this will try to use it first. + * See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#Browser_compatibility + * for availability. + * @param {Boolean} hasPrefix - optional parameter specifying whether to add version/crypto indicator prefix to the string. + * @return {string} A random 24 character base62 random string if successful, or null if no strong crypto is available. + */ +function cryptoRandomBase62String(hasPrefix) { + var base62String; + // Test to make sure unsigned numbers with full 32 bits are supported + if (Math.floor(0xffffffff / 0x100) == 0xffffff) { + var globalObject = globalScope(); + var cryptoObject = globalObject.crypto || globalObject.msCrypto; + var arr; + var i; + var j; + var num; + var isCrypto; + + // UUID has 16 bytes + if (cryptoObject && cryptoObject.getRandomValues) { + arr = cryptoObject.getRandomValues(new Uint32Array(16 / Uint32Array.BYTES_PER_ELEMENT)); + isCrypto = true; + } else if (cryptoObject && cryptoObject.randomBytes) { + var b = cryptoObject.randomBytes(16); + arr = new Uint32Array(b.buffer, b.byteOffset, b.byteLength / Uint32Array.BYTES_PER_ELEMENT); + isCrypto = true; + } else { + arr = new Uint32Array(16 / Uint32Array.BYTES_PER_ELEMENT); + for (i = 0; i < arr.length; i++) { + arr[i] = Math.floor(Math.random() * Math.floor(0xffffffff)); + } + } + + if (arr) { + base62String = ''; + for (i = 0; i < arr.length; i++) { + num = arr[i]; + for (j = 0; j < 6; j++) { + // 4-byte block encoded to 6 byte base62 + base62String += base62Alphabet[num % 62]; + num = Math.floor(num / 62); + } + } + if (hasPrefix) { + // 1st char: version (currently 1) + // 2nd char: separator _ + // 3rd char: encryption type, 0 = unknown, 1 = yes, 2 = no + // 4th char: separator _ + base62String = '1_' + (isCrypto ? '1' : '2') + '_' + base62String; + } + } + } + return base62String; +} + +var string = /*#__PURE__*/Object.freeze({ + __proto__: null, + base10Alphabet: base10Alphabet, + base16Alphabet: base16Alphabet, + base36Alphabet: base36Alphabet, + base61Alphabet: base61Alphabet, + base62Alphabet: base62Alphabet, + startsWith: startsWith, + endsWith: endsWith, + trim: trim, + snakeCaseToCamelCase: snakeCaseToCamelCase, + snakeCaseToUpperCamelCase: snakeCaseToUpperCamelCase, + exceptionString: exceptionString, + paramString: paramString, + versionStringFromUserAgent: versionStringFromUserAgent, + requestId: requestId, + uuid: uuid, + randomHexCharacter: randomHexCharacter, + convertNumberToBaseAlphabet: convertNumberToBaseAlphabet, + cryptoRandomBase62String: cryptoRandomBase62String +}); + +/* + * src/cookies.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + * + * Cookie related util methods + * @constructor + * + * Packaging note: tree-shaking does not remove unused functions from this class object + * It might be more efficient to separate the cookie delegate from the utility functions + */ +var cookies = { + /** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + + /** + * Allows replacement of one or more of this class' functions + * Any method on the passed-in object which matches a method that this class has will be called instead of the built-in class method. + * To replace *all* methods of his class, simply have your delegate implement all the methods of this class + * Your delegate can be a true object instance, an anonymous object, or a class object. + * Your delegate is free to have as many additional non-matching methods as it likes. + * It can even act as a delegate for multiple MetricsKit objects, though that is not recommended. + * + * NOTE: when the delegate function is called, it will include an additional final parameter representing the original function that it replaced. + * This allows the delegate to, essentially, call "super" before or after it does some work. + * @example: + * To override one or more methods, in place: + * eventRecorder.setDelegate({recordEvent: itms.recordEvent}); + * To override one or more methods with a separate object: + * eventRecorder.setDelegate(eventRecorderDelegate); + * (where "eventRecorderDelegate" might be defined elsewhere as, e.g.: + * var eventRecorderDelegate = {recordEvent: itms.recordEvent, + * sendMethod: 'itms'}; + * To override one or more methods with an instantiated object from a class definition: + * eventRecorder.setDelegate(new EventRecorderDelegate()); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.prototype.recordEvent = itms.recordEvent; + * EventRecorderDelegate.prototype.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * To override one or more methods with a class object (with "static" methods): + * eventRecorder.setDelegate(EventRecorderDelegate); + * (where "EventRecorderDelegate" might be defined elsewhere as, e.g.: + * function EventRecorderDelegate() { + * } + * EventRecorderDelegate.recordEvent = itms.recordEvent; + * EventRecorderDelegate.sendMethod = function sendMethod() { + * return 'itms'; + * }; + * @param {Object} Object or Class with delegate method(s) to be called instead of default (built-in) methods. + * @returns {Boolean} true if one or more methods on the delegate object match one or more methods on the default object, + * otherwise returns false. + */ + setDelegate: function setDelegate(delegate) { + return attachDelegate(this, delegate); + }, + + /** + * The cookie string, e.g. "iTunes.cookie" (iTunes desktop), "iTunes.cookieForDefaultURL" (HTML iOS), "itms.cookie" (itml app), "document.cookie" (browser) + * NOTE: Callers should override this method if they want to supply a different cookie. + * @overridable + */ + cookie: function cookie() { + var cookieOwnerObject; + + if (typeof window !== 'undefined' && 'iTunes' in window && 'cookie' in iTunes) { + cookieOwnerObject = iTunes; + } else if (typeof itms !== 'undefined' && isDefined(itms.cookie)) { + cookieOwnerObject = itms; + } else if (typeof document !== 'undefined') { + cookieOwnerObject = document; + } else { + throw 'cookies.cookie: No cookie object available'; + } + + return cookieOwnerObject.cookie; + }, + + // NOTE: MetricsKit does not currently need to set cookies, so setting functions are commented out until they are needed + /** + * Normal/Primary cookie setter method. + * Invokes JavaScript's "escape()" function on "cookieValue" before passing to "cookies.setUnescaped()" + * + * Set cookie with the given name and value at the path "/" + * @param cookieName the name of the cookie; must not contain whitespace or semicolons. + * @param cookieValue must not contain semicolons or whitespace. Use escape() if necessary + * @param lifespanInSeconds may be one of: 1. the time-to-live in seconds, 2. null to expire at browser (session) termination, 3. negative to delete a cookie + * @param path the path to use (optional,if null, defaults to "/") + * @param domain the domain to use (if null, defaults to the current domain) + */ + // this.set = function set(cookieName, cookieValue, lifespanInSeconds, path, domain) { }; + + /** + * "Normal Use" cookie getter method. + * Invokes JavaScript's "unescape()" function on value returned from "cookies.getUnescaped()" and returns that unescaped value. + */ + get: function get(cookieName) { + // NOTE: IF YOU MODIFY THIS METHOD, COPY AND TEST THE MODIFICATION TO itmsCheck.js WHICH HAS A COPY/PASTED VERSION (SANS COMMENTS) + var returnValue = this.getUnescaped(cookieName); + if (returnValue) returnValue = unescape(returnValue); + return returnValue; + }, + + // NOTE, The jingle "setUnescaped" method has a lot of special-case code both for devices and ITML. + // That is important functionality for setting cookies on those platforms but we don't currently need to set cookies (we used to do it as a device workaround in iOS6) and + // to include it here, we would need to create platform-specific delegates, so let's save all that mess for the day we actually need this functionality at + // which point you SHOULD grab and adapt the code from Jingle: + setUnescaped: function setUnescaped(cookieName, cookieValue, lifespanInSeconds, path, domain) {}, + + /** + * Funnel-point cookie-getting method. + * Parsing document.cookie is simple, but there are a lot of quirks. + * + * The simple format is "a=b; c=d;", but according to RFC 2965 (from 2006, but only Opera supports it), + * any amount of whitespace (including none) is optional as a separator. + * NOTE:KBERN: We trim whitespace from the beginning and end of both keys and values based on my reading of: + * http://tools.ietf.org/html/rfc2965 + * on page 3 it says, + * "NOTE: The syntax above allows whitespace between the attribute and the = sign.", and so even though + * Meaning that a) there does have to be an "=" sign, and b) there can be whitespace on both sides of it. + * Safari seems to collapse whitespace around the "=", but there is no guarantee that all browsers will + * behave that way on all platforms, therefore I think that both the keys and values need to be trimmed of + * both leading and trailing spaces. + * + * According to the most widely supported spec (the 1995 Netscape cookie draft doc), the name-value pairs are + * "a sequence of characters excluding semi-colon, comma and white space". This means the value can contain + * the equals sign, and any other number of weird characters! + * + * In short, don't mess with this function. + * + * <rdar://problem/8123192> Javascript cookie parsing is chopping off trailing = signs + */ + getUnescaped: function getUnescaped(cookieName) { + var result = null; + + // NOTE: IF YOU MODIFY THIS METHOD, COPY AND TEST THE MODIFICATION TO itmsCheck.js WHICH HAS A COPY/PASTED VERSION (SANS COMMENTS) + var cookieString = this._getRaw(); + if (cookieString && cookieName) { + var splitCookies = cookieString.split(';'); + + // GO THROUGH THE COOKIES BACKWARDS BECAUSE... + // (This comment, and searching from back->front is from Dojo: http://www.bedework.org/trac/bedework/browser/trunk/deployment/resources/javascript/dojo-0.4.1-ajax/src/io/cookie.js?rev=1164 + // I haven't tried to reproduce this, but it's no skin off our backs to go backwards anyway, so...) + // Which cookie should we return? + // If there are cookies set for different sub domains in the current + // scope there could be more than one cookie with the same name. + // I think taking the last one in the list takes the one from the + // deepest subdomain, which is what we're doing here. + for (var i = splitCookies.length - 1; !result && i >= 0; i--) { + var aCookie = splitCookies[i]; + var separatorIndex = aCookie.indexOf('='); + + if (separatorIndex > 0) { + if (separatorIndex + 1 == aCookie.length) { + result = ''; // there *is* a cookie key, but there is nothing to the right of the "=" + } else { + // Trim all leading and trailing whitespace from key... + var cookieKey = trim(aCookie.substring(0, separatorIndex)); + + if (cookieKey == cookieName) { + // Trim all leading and trailing whitespace from the value as well, since there may be whitespace to the right of the "=" sign + result = trim(aCookie.substring(separatorIndex + 1)); + } + } + } + } + } + return result; + }, + + /** + * adding a cover accessor to document.cookie so that in iOS 4.2 and newer we can use iTunes.cookies instead (allows access to private storage) + */ + + /** + * Clear a cookie (expire/delete/remove it) + * @param {Object} cookieName + */ + remove: function remove(cookieName, domain) { + return this.setUnescaped(cookieName, '.', this.EXPIRE_NOW, null, domain); + }, + + // PRIVATE METHODS: + + // NOTE: MetricsKit does not currently need to set cookies, so setting functions are commented out until they are needed + /** + * @param val the raw Cookie string (Webkit), or a Cookie dict (ITML) to be set. + */ + // this._setRaw = function _setRaw(val) { }; + + /** + * @returns all Cookies as a string. + */ + _getRaw: function _getRaw() { + return this.cookie() || ''; + } +}; + +// CONSTANTS: +// Convenient lifespanInSeconds values: +cookies.EXPIRE_NOW = -1; +cookies.EXPIRE_SESSION = null; // or "0" +cookies.EXPIRE_ONE_SECOND = 1; +cookies.EXPIRE_ONE_MINUTE = cookies.EXPIRE_ONE_SECOND * 60; +cookies.EXPIRE_ONE_HOUR = cookies.EXPIRE_ONE_MINUTE * 60; +cookies.EXPIRE_ONE_DAY = cookies.EXPIRE_ONE_HOUR * 24; +cookies.EXPIRE_ONE_WEEK = cookies.EXPIRE_ONE_DAY * 7; +cookies.EXPIRE_ONE_MONTH = cookies.EXPIRE_ONE_DAY * 31; +cookies.EXPIRE_ONE_YEAR = cookies.EXPIRE_ONE_DAY * 365; +cookies.EXPIRE_ONE_SIDEREAL_YEAR = cookies.EXPIRE_ONE_DAY * 365.25; // (31556926279 or so)... For those who want decades long accuracy :-( ... of course we could also make special day times since a day is really 24 hours and 2 milliseconds long :-) +cookies.EXPIRE_SIX_MONTHS = cookies.EXPIRE_ONE_DAY * 180; // <rdar://problem/11067278> Cookies: reduce max age to 6 months + +/* + * src/utils/delegates_info.js + * mt-metricskit-utils-private + * + * Copyright © 2016 Apple Inc. All rights reserved. + * + */ + +/** + * Used to store information + * about attached delegates as a tree, + * in which delegates may contain their own + * list of "child" delegates as an array + * @type {Object} + * @example + * // If metricsKit.attachDelegate(delegatesITML) + * delegatesMap = { + '@amp/mt-metricskit5.1.0': { + name: '@amp/mt-metricskit', + version: '5.1.0' + // The accessor delegatesMap[...] would not run correctly if initialized + // at the same time as this object, but since it's added after delegatesMap + // is created, it works + delegates: [delegatesMap['@amp/mt-metricskit-delegates-itml3.1.2']] + // The above array contains a pointer to another object in delegatesMap + } + '@amp/mt-metricskit-delegates-itml3.1.2': { + name: '@amp/mt-metricskit-delegates-itml', + version: '3.1.2' + } + } + */ +var delegatesMap = {}; + +/* + * Used to keep a record of which delegates have been added to which targets + * for use with deduping the child 'delegates' field of a delegate object + * (The reason for using this object instead of adding it to the delegatesMap + * or changing the delegates subfield in delegatesMap to be an object + * is that delegatesMap represents the value of the mt-metricskit base field + * xpDelegatesInfo, which requires a certain format. + * This map is a theoretical extension of delegatesMap, to facilitate constant + * time lookup for use with deduping.) + * @type {Object} + * @example + * After mt-metricskit adds delegates-itml and delegates-html as delegates + * and delegates-itml has added base-events as a delegate + * { + * 'mt-metricskit2.1.1': ['mt-metricskit-delegates-itml3.1.5', 'mt-metricskit-delegates-html0.1.3'], + * 'mt-metrickit-delegates-itml3.1.5': ['mt-metricskit-base-events1.1.2'] + * } + */ +var dedupingMap = {}; + +/** + * @param {Object} delegate The object to retrieve name & version info off of + * @param {function} delegate.mtName Returns the name of the delegate as a string + * @param {function} delegate.mtVersion Returns the version of the delegate as a string + * @returns {Object} Contains the passed-in delegate's info, esp. name & version + */ +var createDelegateInfoObject = function createDelegateInfoObject(delegate) { + var delegateInfo = {}; + + if (typeof delegate.mtName === 'function' && typeof delegate.mtVersion === 'function') { + // Add delegate name, version, and any previously-attached "child" delegates to delegatesInfoList + delegateInfo.name = delegate.mtName(); + delegateInfo.version = delegate.mtVersion(); + } + + return delegateInfo; +}; + +/** + * Returns a concatted string of the passed-in delegate's + * name and version, to be used when storing the delegate in delegatesMap + * @param {Object} delegate Delegate to create key string from + * @param {function} delegate.mtName Returns the name of the delegate as a string + * @param {function} delegate.mtVersion Returns the version of the delegate as a string + * @returns {String} The concatted name and version of the passed-in delegate + * @example + * "mt-metricskit-delegates-itml3.1.2" + */ +var createDelegateKey = function createDelegateKey(delegate) { + var delegateKey; + if (typeof delegate.mtName === 'function' && typeof delegate.mtVersion === 'function') { + delegateKey = delegate.mtName() + delegate.mtVersion(); + } + return delegateKey; +}; + +/** + * Creates delegate info objects for passed-in target object and delegate object, + * and stores them in the delegatesMap. The info object of the delegate being attached + * to the target will be stored in the target's info object's 'delegates' field + * @param {Object} target The object being partially or wholly overwritten by the delegate + * @param {Object} delegate The object overwriting the target object's functionality + * @param {function} target.mtName Returns the name of the target as a string + * @param {function} target.mtVersion Returns the version of the target as a string + * @param {function} delegate.mtName Returns the name of the delegate as a string + * @param {function} delegate.mtVersion Returns the version of the delegate as a string + */ +function storeDelegateInfo(target, delegate) { + var targetKey = createDelegateKey(target); + var delegateKey = createDelegateKey(delegate); + if (targetKey && delegateKey) { + // Create delegate info objects (containing delegate's name & version) + // and add to delegatesMap + if (!delegatesMap[delegateKey]) { + delegatesMap[delegateKey] = createDelegateInfoObject(delegate); + } + if (!delegatesMap[targetKey]) { + delegatesMap[targetKey] = createDelegateInfoObject(target); + dedupingMap[targetKey] = {}; + } + // Add delegate's info object to target's delegates array in delegatesMap + if (delegatesMap[targetKey].delegates) { + if (!dedupingMap[targetKey][delegateKey]) { + delegatesMap[targetKey].delegates.push(delegatesMap[delegateKey]); + } + } else { + delegatesMap[targetKey].delegates = [delegatesMap[delegateKey]]; + } + dedupingMap[targetKey][delegateKey] = true; + } +} + +/** + * Return the delegate object stored in delegatesMap + * for the passed-in delegate + * @returns {Object} The stored delegate object for the passed-in delegate + */ +function getStoredDelegateObject(delegate) { + return delegatesMap[createDelegateKey(delegate)]; +} + +var delegates_info = /*#__PURE__*/Object.freeze({ + __proto__: null, + storeDelegateInfo: storeDelegateInfo, + getStoredDelegateObject: getStoredDelegateObject +}); + +/* + * src/key_value.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + * Takes a SINGLE searchSource and string representation of an object path (a namespace) and returns the object at that subpath (possibly after first creating it) + * If the objects in the path do not already exist off of searchSource and "createIfNeeded" is true, this method will create all the JavaScript objects required to make it a valid object path + * If any component already exists, it will be maintained. New components are created as "{}" + * e.g. after this call: + * keyValue.valueForKeyPath(rootObject, 'foo.bar.Tot') + * there will always be a valid JavaScript object of: + * rootObject.foo.bar.Tot + * @param searchSource a key/value object + * @param keyPath a simple property key name, e.g. "foo", or a nested property key name "path", e.g. foo.bar.tot + * @return the discovered and/or created object at the specified path extending off the specified searchSources + * If "createIfNeeded" is not specified, and keyPath or searchSources are not both valid, "undefined" will be returned (since "null" could be a valid return value stored at some keyPath) + * If the goal of the caller is simply to create the object path and "createIfNeeded" has been specified, the return value may be ignored. + * @example valueForKeyPath("foo", {"bar":10, "foo":12}); returns "12" + */ +function _valueForKeyPath(keyPath, searchSource, createIfNeeded) { + var tailObject = searchSource; + + if (keyPath && searchSource) { + var objectStrings = keyPath.split('.'); + + for (var ii = 0; tailObject && ii < objectStrings.length; ii++) { + var anObjectString = objectStrings[ii]; + if (!(anObjectString in tailObject) && createIfNeeded) { + tailObject[anObjectString] = {}; + } + if (anObjectString in tailObject) { + tailObject = tailObject[anObjectString]; + } else { + tailObject = null; + } + } + } + return tailObject; +} + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Takes one or more searchSources and string representation of an object path (a namespace) and returns the object at that subpath + * @param {String} keyPath a simple property key name, e.g. "foo", or a nested property key name "path", e.g. foo.bar.tot + * @param {varargs} searchSources at least one key/value object(s), or array(s) of key/value objects, or list(s) of key/value objects. + * Later sets of key/value pairs overwrite earlier sets. + * Callers are not asked to pass a unified (coalesced) set of key/value objects because that is a much more expensive operation + * to preform each time we are searching for a keyPath. + * @return the discovered object at the specified path extending off the specified searchSources. Later sets of key/value pairs overwrite earlier sets. + * @example valueForKeyPath("foo", {"bar":10, "foo":12}); returns "12" + * @example valueForKeyPath("foo.cat", [{"bar":10, "foo":12}, {"bar":10, "foo":{"cat":"meow", "dog":"ruff"}}]); returns "meow" + * @example valueForKeyPath("foo.cat", {"bar":10, "foo":12}, {"bar":10, "foo":{"cat":"meow", "dog":"ruff"}}); returns "meow" + */ +function valueForKeyPath(keyPath /*, searchSources<varargs>*/) { + var returnValue = null; + + if (keyPath && arguments.length > 1) { + // Pass in all sources after the first ("keyPath"). We do this even if there's only one param, in case that one param is an object instead of an array + var normalizedSearchSources = sourcesArray(Array.prototype.slice.call(arguments, 1)); + + // Now we just loop through our normalizedSearchSources looking for "keyPath" in any of them. + // We start at the end and look backwards, because the later sources take precedence over earlier ones. + for (var ii = normalizedSearchSources.length - 1; ii >= 0; ii--) { + var aSearchSource = normalizedSearchSources[ii]; + + returnValue = _valueForKeyPath(keyPath, aSearchSource); + if (isDefinedNonNull(returnValue)) { + break; + } + } + } + return returnValue; +} + +/** + * If the objects in the path do not already exist off of searchSource, this method will create all the JavaScript objects required to make it a valid object path + * If any component already exists, it will be maintained. New components are created as "{}" + * e.g. after this call: + * keyValue.createObjectAtKeyPath(rootObject, 'foo.bar.Tot') + * there will always be a valid JavaScript object of: + * rootObject.foo.bar.Tot + * @param searchSource a key/value object + * @param keyPath a simple property key name, e.g. "foo", or a nested property key name "path", e.g. foo.bar.tot + * @return the created object at the specified path extending off the specified searchSources + * If the goal of the caller is simply to create the object path and "createIfNeeded" has been specified, the return value may be ignored. + */ +function createObjectAtKeyPath(keyPath, searchSource) { + return _valueForKeyPath(keyPath, searchSource, true); +} + +/** + * Expands the sources param, and any varargs that might follow it and puts them all into an array. + * Items within "sources" or varargs can, themselves, be arrays, in which case they will be decomposed and their items added to the top level of the returned array. + * @example sourcesArray("foo", {"bar":10, "foo":12}); returns ["foo", {"bar":10, "foo":12}] + * @example sourcesArray("foo.cat", [{"bar":10, "foo":12}, {"bar":10, "foo":{"cat":"meow", "dog":"ruff"}}]); returns ["foo.cat", {"bar":10, "foo":12}, {"bar":10, "foo":{"cat":"meow", "dog":"ruff"}}] + * @example sourcesArray("foo.cat", {"bar":10, "foo":12}, {"bar":10, "foo":{"cat":"meow", "dog":"ruff"}}); returns ["foo.cat", {"bar":10, "foo":12}, {"bar":10, "foo":{"cat":"meow", "dog":"ruff"}}] + * @param sources an object, an array of objects, an array of objects where some objects are themselves arrays + * @param varargs additional objects to be added to the returned array. + * @returns {Array} + */ +function sourcesArray(sources /*, varargs*/) { + var returnValue = []; + var arrayifiedSources = []; + + // This will add in the individual searchSources whether they are in a single object or an array... + arrayifiedSources = arrayifiedSources.concat(sources); + // This will add in anything that "arguments" had as varargs... + if (arguments && arguments.length > 1) { + arrayifiedSources = arrayifiedSources.concat(Array.prototype.slice.call(arguments, 1)); + } + + // If any of the items in "sources" is already an array, this loop will expand it and add each element as an individual source. + // We only do this one level deep (i.e. we don't look to see if there are arrays within arrays in this list). + for (var ii = 0; ii < arrayifiedSources.length; ii++) { + var arrayItem = arrayifiedSources[ii]; + returnValue = returnValue.concat(arrayItem); + } + + return returnValue; +} + +var key_value = /*#__PURE__*/Object.freeze({ + __proto__: null, + valueForKeyPath: valueForKeyPath, + createObjectAtKeyPath: createObjectAtKeyPath, + sourcesArray: sourcesArray +}); + +/* + * src/metrics/utils/event_fields.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * Takes one or more eventFields objects, cleans them (removes keys that are typeof 'function', keys with 'null' values, keys with 'undefined' values), + * merges them (later objects take precedence), and returns a single object with the union of all remaining fields. + * Passed in objects are treated as immutable and so will never be modified. + * @param eventFields an object with keys and values OR an array of objects with keys and values. + * If only one parameter is provided, the return value will be the non-null values of that single object. + * @param varargs additional objects to be merged in, similar to eventFields (i.e. dictionaries, arrays of dictionaries, or some combination of the two). + * Later objects take precedence over earlier ones. + * @return a new object with the union of all non-function, non-null and non-undefined fields. + * Passed in objects are treated as immutable and so will never be modified. + * @example mergeAndCleanEventFields({}) ===> {} + * @example mergeAndCleanEventFields(null) ===> {} + * @example mergeAndCleanEventFields({"foo":10}) ===> {"foo":10} + * @example mergeAndCleanEventFields({"foo":10, "bar":null}) ===> {"foo":10} + * @example mergeAndCleanEventFields({"foo":10, "bar":null}, {"cat":null}) ===> {"foo":10} + * @example mergeAndCleanEventFields({"foo":10, "bar":null}, {"cat":null, "mouse":"gray"}) ===> {"foo":10, "mouse":"gray"} + * @example mergeAndCleanEventFields({"foo":10, "bar":null}, {"cat":null, "mouse":"gray", "dog":"bark"}) ===> {"foo":10, "mouse":"gray", "dog":"bark"} + * @example mergeAndCleanEventFields({"foo":10, "bar":null}, {"cat":null, "mouse":"gray", "dog":"bark", "foo":11}) ===> {"foo":11, "mouse":"gray", "dog":"bark"} + * @example mergeAndCleanEventFields({"foo":10, "bar":null}, {"cat":null, "mouse":"gray", "dog":"bark", "foo":11}, {"foo":12}) ===> {"foo":12, "mouse":"gray", "dog":"bark"} + */ +function mergeAndCleanEventFields(eventFields /*, varargs*/) { + var argumentsArray = [false, false, false].concat(Array.prototype.slice.call(arguments)); + // expand argumentsArray, in case it contains arrays) + var expandedArgumentsArray = []; + + for (var ii = 0; ii < argumentsArray.length; ii++) { + var itemToPush = argumentsArray[ii]; + // Either push each item in this item... + if (itemToPush && itemToPush.constructor === Array) { + for (var jj = 0; jj < itemToPush.length; jj++) { + expandedArgumentsArray.push(itemToPush[jj]); + } + // or push the item itself (if it is not an array) + } else { + expandedArgumentsArray.push(itemToPush); + } + } + return copyKeysAndValues.apply(null, expandedArgumentsArray); +} + +/** + * This method is the workhorse of all the various eventHandlers. + * It will take all of the parameters of the callers "metricsData()" method, merge them together, + * invoke accessors on their known fields, and return the resultant map. + * @param eventHandler the calling eventHandler + * @param knownFields the calling eventHandler's list (array) of strings that are that handler's known field values. + * If the caller has accessors to be invoked, they must be present in the "knownFields" array + * @param {Boolean} includeAllKnownFields if false, only known field accessors that match caller provided field names will be invoked. + * @returns {Arguments} all of the arguments that the calling eventHandler received. + * @example: + * Page.prototype.metricsData = function(pageId, pageType, pageContext, eventFieldsMapN(varargs)) { + * var pageFields = { pageId: pageId, pageType: pageType, pageContext: pageContext }; + * return utils.eventFields.processMetricsData(this, this.knownFields(), true, pageFields, eventFieldsMapN); }); + */ +function processMetricsData(eventHandler, knownFields, includeAllKnownFields, callerSuppliedEventFieldsMapsArray) { + var callerProvidedFields = mergeAndCleanEventFields(callerSuppliedEventFieldsMapsArray); + // Initialize returnValue with the passed-in fields in case there are fields we haven't contemplated or don't have accessor methods for, they will still be included. + var returnValue = callerProvidedFields; + + if (eventHandler && knownFields) { + var knownFieldValues = {}; + + if (!includeAllKnownFields) { + // only include known field names that were also included in caller provided maps + knownFields = knownFields.filter(function (fieldName) { + return fieldName in callerProvidedFields; + }); + } + if (knownFields.length) { + for (var ii = 0; ii < knownFields.length; ii++) { + var knownFieldName = knownFields[ii]; + var knownFieldAccessor = eventHandler[knownFieldName]; + + if (isFunction(knownFieldAccessor)) { + // NOTE: If the accessor method prefers to use a value from the passed-in callerProvidedFields, it must do that on its own. + knownFieldValues[knownFieldName] = knownFieldAccessor.call(eventHandler, callerProvidedFields); + } + } + } + + returnValue = mergeAndCleanEventFields(returnValue, knownFieldValues); + } + + return returnValue; +} + +/** + * Returns an object containing the intersection of properties in + * data and matching string values in the fieldMap property corresponding to 'sectionName' + * ( e.g. fieldMap.custom[sectionName] is an object containing arrays of strings which + * correspond to the keys desired in the mappedFields object ) + * @param {Object} data The model data corresponding to element we're mapping fields for + * @param {String} sectionName Specifies which section of the fieldMap to use (eg: 'impressions', 'location', or 'custom') + * @param {Object} fieldsMap contains one or more field mapping(s) + * @param {Function} (optional) onError callback to be invoked with an error message; e.g. console.error + * @return {Object} Contains intersection of data and fieldsMap values + * @example + * // where impressionFieldsMapSection = { + * // impressionType: ['type', 'impressionType'], + * // id: ['targetId', 'id'] + * //}; + * applyFieldsMap({type: 'button', id: '123', name: 'playbutton'}, 'impressions') + * // returns {impressionType: 'button', id: '123'} + */ +function applyFieldsMap(data, sectionName, fieldsMap, onError) { + var fieldsMapSection; + var mappedFields; + var errorMessage; + + if (data && sectionName && fieldsMap) { + mappedFields = {}; + fieldsMapSection = valueForKeyPath(sectionName, fieldsMap, fieldsMap.custom); + if (fieldsMapSection) { + var i; + var value; + if (isArray(fieldsMapSection)) { + for (i = 0; i < fieldsMapSection.length; ++i) { + value = data[fieldsMapSection[i]]; + if (isDefinedNonNull(value)) { + mappedFields[fieldsMapSection[i]] = value; + } + } + } else if (isObject(fieldsMapSection)) { + for (var key in fieldsMapSection) { + for (i = 0; i < fieldsMapSection[key].length; ++i) { + value = valueForKeyPath(fieldsMapSection[key][i], data); + if (isDefinedNonNull(value)) { + mappedFields[key] = value; + break; + } + } + } + } else { + errorMessage = + 'metrics: incorrect data type provided to applyFieldsMap (only accepts objects and Arrays)'; + } + } else { + errorMessage = 'metrics: unable to get ' + sectionName + ' section from fieldsMap'; + } + } else { + var missingArgs = []; + + if (!data) { + missingArgs.push('data'); + } + if (!sectionName) { + missingArgs.push('sectionName'); + } + if (!fieldsMap) { + missingArgs.push('fieldsMap'); + } + + errorMessage = 'metrics: missing argument(s): ' + missingArgs.join(',') + ' not provided to applyFieldsMap'; + } + + if (errorMessage && isFunction(onError)) { + onError(errorMessage); + } + + return mappedFields; +} + +var event_fields = /*#__PURE__*/Object.freeze({ + __proto__: null, + mergeAndCleanEventFields: mergeAndCleanEventFields, + processMetricsData: processMetricsData, + applyFieldsMap: applyFieldsMap +}); + +/* + * src/metrics/utils/network.js + * mt-metricskit + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + * Makes an XHR GET request + * @param {String} url of the request + * @param {Function} onSuccessHandler function to execute on request success (response returned) + * @param {Function} onFailureHandler function to execute on request failure (error returned) + */ +function makeAjaxGetRequest(url, onSuccessHandler, onFailureHandler) { + makeAjaxRequest(url, 'GET', null, onSuccessHandler, onFailureHandler); +} + +/** + * Creates, modifies, and sends an XMLHttpRequest object + * @param {String} url url of endpoint + * @param {String} method "GET", "POST", etc. + * @param {*} data data to send to endpoint if method is "POST", "PUT", etc. + * @param {Function} [onSuccess] optional function to execute on request success (takes response returned) + * @param {Function} [onFailure] optional function to execute on request failure (takes error returned and optional status code) + * @param {Object} [options] optional dictionary of options which define how to modify the XMLHttpRequest + * @param {boolean} [options.async] optional boolean which determines if the request should be asynchronous + * @param {Number} [options.timeout] optional which determines request timeout + * @param {Number} [options.withCredentials] optional which determines request withCredentials + */ +function makeAjaxRequest(url, method, data, onSuccess, onFailure, options) { + var request = new XMLHttpRequest(); + data = data || undefined; + options = options || {}; + onSuccess = isFunction(onSuccess) ? onSuccess : function () {}; + onFailure = isFunction(onFailure) ? onFailure : function () {}; + // Sets async to true by default + var async = options.async === false ? false : true; + + // synchronous requests should not use the timeout property: + // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout + if (options.timeout && async) { + request.timeout = options.timeout; + } + + request.onload = function onload() { + // Successful response status is defined as 2xx by default + if (request.status >= 200 && request.status < 300) { + onSuccess(request.response); + } else { + // Pass in optional status code so status logic can be performed on failure + onFailure( + new Error('XHR error: server responded with status ' + request.status + ' ' + request.statusText), + request.status + ); + } + }; + request.onerror = function onError() { + onFailure(new Error('XHR error')); + }; + + request.open(method, url, async); + + // Because of rdar://72864343, we allow callers to optionally specify "withCredentials". + // The default value is true, which allows CORS requests to have cookies set on response + // (e.g. we're itunes.apple.com, userxp is xp.apple.com). + request.withCredentials = typeof options.withCredentials === 'boolean' ? options.withCredentials : true; + request.setRequestHeader('Content-type', 'application/json'); + + request.send(data); +} + +var network = /*#__PURE__*/Object.freeze({ + __proto__: null, + makeAjaxGetRequest: makeAjaxGetRequest, + makeAjaxRequest: makeAjaxRequest +}); + +/* + * src/metrics/utils/sampling.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PRIVATE METHODS/IVARS ************************************ + */ +var _sessions = {}; + +/** + * Manually clears an active sampling session + * @param {String} sessionName + */ +var _clearSession = function _clearSession(sessionName) { + if (_sessions[sessionName]) { + clearTimeout(_sessions[sessionName]); + _sessions[sessionName] = null; + } +}; + +/** + ****************************** PSEUDO-PRIVATE METHODS/IVARS ********************************** + * These functions need to be accessible for ease of testing, but should not be used by clients + */ +function _utClearSessions() { + for (var sessionName in _sessions) { + _clearSession(sessionName); + } +} + +/** + ************************************ PUBLIC METHODS/IVARS ************************************ + */ + +/** + * A random lottery that is successful with samplingPercentage frequency + * @param {Number} samplingPercentage (between 0 and 1) + * @return {Boolean} whether or not the lottery was successful + */ +function lottery(samplingPercentage) { + return Math.random() < samplingPercentage; +} + +/** + * Determines whether a particular sampling session is active + * @param {String} sessionName the name associated with a particular session + * @param {Number} sessionSamplingPercentage (between 0 and 1) + * @param {Number} sessionDuration (in ms) + * @return {Boolean} whether or not the sampling session associated with sessionName is currently active + */ +function sessionSampled(sessionName, sessionSamplingPercentage, sessionDuration) { + var returnValue; + + // if a timer is currently running, we are sampled in to this session + if (_sessions[sessionName]) { + returnValue = true; + } else { + // roll the dice + var sampleNow = lottery(sessionSamplingPercentage); + + // check if we need to enable sampling for sessionDuration ms + if (sampleNow && sessionDuration > 0) { + _sessions[sessionName] = setTimeout(_clearSession.bind(null, sessionName), sessionDuration); + } + + returnValue = sampleNow; + } + + return returnValue; +} + +/** + * Determines whether an eventType should be sampled. + * Session sampling (sessionSamplingPercentage and sessionDuration) will be checked first, and samplingPercentage will be used as a fallback. + * @param {Boolean} (optional) samplingForced whether to always sample in. Default false. + * @param {Number} (optional) sessionSamplingPercentage (between 0 and 1) the frequency at which to initiate sampling sessions for eventType. Default 0. + * @param {Number} (optional) sessionDuration the duration, in milliseconds, of sampling sessions for this eventType. Default 0. + * @param {Number} (optional) samplingPercentage (between 0 and 1) the frequency at which to sample in individual events. Default 0. + * @example sampling.isSampledIn('pageRender', null, 0.05, 60000); + * @return {Boolean} whether or not eventType is currently sampled in + */ +function isSampledIn(eventType, samplingForced, sessionSamplingPercentage, sessionDuration, samplingPercentage) { + return ( + samplingForced || + sessionSampled(eventType, sessionSamplingPercentage, sessionDuration) || + lottery(samplingPercentage) + ); +} + +var sampling = /*#__PURE__*/Object.freeze({ + __proto__: null, + _utClearSessions: _utClearSessions, + lottery: lottery, + sessionSampled: sessionSampled, + isSampledIn: isSampledIn +}); + +/* + * src/storage.js + * mt-metricskit-utils-private + * + * Copyright © 2015-2017 Apple Inc. All rights reserved. + * + */ + +/** + ************************************ PRIVATE METHODS/IVARS ************************************ + */ + +var CONSTANTS = { + STORAGE_TYPE: { + LOCAL_STORAGE: 'localStorage', + SESSION_STORAGE: 'sessionStorage' + } +}; + +/** + * Cover function for sessionStorage/localStorage. + * Some clients do not have implementations of storage objects, and some are platform-specific (e.g. iTunes.sessionStorage) + * This helper method will shim storage object functionality as necessary + * @param {object} storageObject - this is either the platform-specific implementation of session/localStorage, or null if none exists + * @returns {object} a storage object, either the provided platform-specific one or a placeholder/shimmed version + */ +var _storageObject = function _storageObject(storageObject) { + var aStorageObject = null; // We'll capture this variable as a singleton in the closure below... + var errorShown = false; + + return function () { + if (!storageObject) { + if (!errorShown) { + console.error( + 'storageObject: storage object not found. Override this function if there is a platform-specific implementation' + ); + errorShown = true; // only show error the first time + } + // Let's not stop the whole app by "throw"ing here, and let's not make all callers be required to check for undefined return values. + // We'll just create a placeholder sessionStorage object which will not hold values across page JS contexts, but at least it will hold them for *some* time... + if (!aStorageObject) { + aStorageObject = { + storage: {}, + getItem: function (key) { + return this.storage[key]; + }, + + setItem: function (key, value) { + this.storage[key] = value; + }, + + removeItem: function (key) { + delete this.storage[key]; + } + }; + } + } else { + aStorageObject = storageObject; + } + return aStorageObject; + }; +}; + +/** + * Fetches the given storage object from the global object. This function wraps around a try-catch to avoid any exception from being thrown + * in cases where the local storage API is disabled (say for example by disabling cookies in Safari preferences) + * @param {Object} storageObjectType the storage object type to be evaluated on the window object. Possible values are localStorage or sessionStorage and are defined in CONSTANTS.STORAGE_TYPE object above. + * @return {Object} the storage object or null if key doesn't exist in storage or the storage api is disabled + */ +function _defaultStorageObject(storageObjectType) { + var storageObject = null; + var storageObjectClass = null; + var isLocalStorageObjectType = storageObjectType === CONSTANTS.STORAGE_TYPE.LOCAL_STORAGE; + try { + storageObjectClass = isLocalStorageObjectType ? typeof localStorage : typeof sessionStorage; + if (storageObjectClass !== 'undefined') { + storageObject = isLocalStorageObjectType ? localStorage : sessionStorage; + } else { + storageObject = null; + } + } catch (e) { + // We allow the current process to run without interruption instead of bringing the app down + storageObject = null; + console.error('_utils.storage._defaultStorageObject: Unable to retrieve storage object: ' + e); + } + return storageObject; +} + +/** + ************************************ PSEUDO-PRIVATE METHODS/IVARS ************************************ + * These functions need to be accessible for ease of testing, but should not be used by clients + */ +function _utDefaultStorageObject(storageObjectType) { + return _defaultStorageObject(storageObjectType); +} + +/** + **************************** PUBLIC METHODS/IVARS **************************** + */ +var localStorageObject = _storageObject(_defaultStorageObject(CONSTANTS.STORAGE_TYPE.LOCAL_STORAGE)); +var sessionStorageObject = _storageObject(_defaultStorageObject(CONSTANTS.STORAGE_TYPE.SESSION_STORAGE)); + +/** + * Stringifies an object and saves it to storage + * @param {Object} storageObject an object that adheres to the Web Storage API + * @param {String} key + * @param {Object} (optional) objectToSave the object to stringify and save; if null, key will be removed from storageObject + * @return {Object} the object that was saved to storage or null if nothing was saved (if removing an item, returns undefined) + */ +function saveObjectToStorage(storageObject, key, objectToSave) { + var result = null; + + if (objectToSave) { + // setItem may throw errors if storage is full, or stringify could error + try { + storageObject.setItem(key, JSON.stringify(objectToSave)); + result = objectToSave; + } catch (e) {} + } else { + result = storageObject.removeItem(key); + } + + return result; +} + +/** + * Fetches an object stored as a serialized JSON string and unpacks it + * @param {Object} storageObject an object that adheres to the Web Storage API + * @param {String} key + * @return {Object} the object from storage or null if key doesn't exist in storage (returns undefined if stored value failed to parse) + */ +function objectFromStorage(storageObject, key) { + var result = null; + var serializedObject = storageObject.getItem(key); + + if (serializedObject) { + try { + result = JSON.parse(serializedObject); + } catch (e) { + result = undefined; + } + } + + return result; +} + +var storage = /*#__PURE__*/Object.freeze({ + __proto__: null, + _utDefaultStorageObject: _utDefaultStorageObject, + localStorageObject: localStorageObject, + sessionStorageObject: sessionStorageObject, + saveObjectToStorage: saveObjectToStorage, + objectFromStorage: objectFromStorage +}); + +export { backoff, config, cookies, delegates_info as delegatesInfo, event_fields as eventFields, key_value as keyValue, network, number, reflect, sampling, storage, string }; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/actions/action-dispatcher.js b/shared/metrics-8/node_modules/@jet/engine/lib/actions/action-dispatcher.js new file mode 100644 index 0000000..1280b06 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/actions/action-dispatcher.js @@ -0,0 +1,64 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ActionDispatcher = void 0; +const optional_1 = require("@jet/environment/types/optional"); +class ActionDispatcher { + constructor(metricsPipeline) { + this.implementations = {}; + this.metricsPipeline = metricsPipeline; + } + register(type, implementation) { + if (type in this.implementations) { + console.error(`An implementation is already registered for ${type}`); + } + this.implementations[type] = implementation; + } + async perform(action, metricsBehavior) { + if (!(action.$kind in this.implementations)) { + // 1. If there is no implementation registered for the type of action + // we were passed, we check for a chained dispatcher to forward to. + // If one is found, we forward this call without doing any work. + // If none is found, we give up. + if (optional_1.isSome(this.next)) { + return await this.next.perform(action, metricsBehavior); + } + else { + return "unsupported"; + } + } + // 2. We have an implementation for the action we were given. + // We are responsible for processing metrics for that action. + this.processMetrics(action, metricsBehavior); + if (optional_1.isSome(this.next)) { + // 3a. If we have another dispatcher we are chained to, we forward to it + // if the implementation we have for the given action decides it cannot + // support performing it. + const outcome = await this.implementations[action.$kind](action); + if (outcome === "unsupported") { + return await this.next.perform(action, { behavior: "notProcessed" }); + } + else { + return outcome; + } + } + else { + // 3b. We hand off control to the implementation we have for the given action type. + // If the implementation cannot perform the action, we give up. + return await this.implementations[action.$kind](action); + } + } + processMetrics(action, metricsBehavior) { + if (metricsBehavior.behavior === "notProcessed") { + return; + } + const actionMetrics = action.actionMetrics; + const context = { + customMetrics: actionMetrics.custom, + pageFields: metricsBehavior.context.pageFields, + }; + action.actionMetrics.data.forEach((data) => { + this.metricsPipeline.process(data, context); + }); + } +} +exports.ActionDispatcher = ActionDispatcher; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/actions/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/actions/index.js new file mode 100644 index 0000000..303d3bb --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/actions/index.js @@ -0,0 +1,13 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./action-dispatcher"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/index.js new file mode 100644 index 0000000..c4bd837 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/index.js @@ -0,0 +1,17 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./jet-bag"), exports); +__exportStar(require("./jet-host"), exports); +__exportStar(require("./jet-network-fetch"), exports); +__exportStar(require("./localized-strings-bundle"), exports); +__exportStar(require("./localized-strings-json-object"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-bag.js b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-bag.js new file mode 100644 index 0000000..0ea378c --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-bag.js @@ -0,0 +1,40 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.JetBag = void 0; +class JetBag { + constructor(backing) { + this.backing = backing; + } + registerBagKeys() { + // do nothing. + } + string(key) { + const value = this.backing[key]; + return typeof value === "string" || value === null ? value : undefined; + } + double(key) { + const value = this.backing[key]; + return typeof value === "number" || value === null ? value : undefined; + } + integer(key) { + const value = this.backing[key]; + return typeof value === "number" || value === null ? value : undefined; + } + boolean(key) { + const value = this.backing[key]; + return typeof value === "boolean" || value === null ? value : undefined; + } + array(key) { + const value = this.backing[key]; + return Array.isArray(value) || value === null ? value : undefined; + } + dictionary(key) { + const value = this.backing[key]; + return typeof value === "object" ? value : undefined; + } + url(key) { + const value = this.backing[key]; + return typeof value === "string" ? value : undefined; + } +} +exports.JetBag = JetBag; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-host.js b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-host.js new file mode 100644 index 0000000..ed816b4 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-host.js @@ -0,0 +1,19 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.JetHost = void 0; +class JetHost { + constructor(options) { + this.osBuild = "unknown"; + this.deviceModel = "unknown"; + this.deviceModelFamily = "unknown"; + this.devicePhysicalModel = "unknown"; + this.deviceLocalizedModel = "unknown"; + this.clientIdentifier = "unknown"; + this.clientVersion = "unknown"; + this.platform = options.platform; + } + isOSAtLeast() { + return false; + } +} +exports.JetHost = JetHost; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-network-fetch.js b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-network-fetch.js new file mode 100644 index 0000000..8330737 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/jet-network-fetch.js @@ -0,0 +1,39 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.JetNetworkFetch = void 0; +const optional_1 = require("@jet/environment/types/optional"); +class JetNetworkFetch { + async fetch(request) { + var _a, _b, _c; + if (optional_1.isNothing(process === null || process === void 0 ? void 0 : process.env.MEDIA_API_TOKEN)) { + return await Promise.reject(new Error("process.env.MEDIA_API_TOKEN must be specified")); + } + const headers = { + ...((_a = request.headers) !== null && _a !== void 0 ? _a : {}), + authorization: `Bearer ${process === null || process === void 0 ? void 0 : process.env.MEDIA_API_TOKEN}`, + }; + const response = await fetch(request.url, { + body: request.body, + method: (_b = request.method) !== null && _b !== void 0 ? _b : undefined, + cache: (_c = request.cache) !== null && _c !== void 0 ? _c : undefined, + headers: headers, + }); + return { + ok: response.ok, + headers: Array.from(response.headers.keys()).reduce((previous, key) => { + const value = response.headers.get(key); + if (optional_1.isSome(value)) { + previous[key] = value; + } + return previous; + }, {}), + redirected: response.redirected, + status: response.status, + statusText: response.statusText, + url: response.url, + body: await response.text(), + metrics: [], + }; + } +} +exports.JetNetworkFetch = JetNetworkFetch; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/localized-strings-bundle.js b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/localized-strings-bundle.js new file mode 100644 index 0000000..0c4bf44 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/localized-strings-bundle.js @@ -0,0 +1,68 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.LocalizedStringsBundle = void 0; +const environment_1 = require("@jet/environment"); +const localized_strings_json_object_1 = require("./localized-strings-json-object"); +/** + * A localized string data source which loads strings from the application bundle. + * + * The bundle used by this data source can be a web app webpack bundle + * or a native app bundle bridged over to JS code. + */ +class LocalizedStringsBundle { + // MARK: - Initialization + /** + * Create localized strings bundle with all required attributes. + * + * @param bundle - The app bundle object. + */ + constructor(bundle) { + this.bundle = bundle; + } + // MARK: - LocalizedStringsDataSource + async fetchStrings(language) { + var _a; + // Load the strings from bundle and cache them. + const localizations = this.bundle.localizationsProperty; + if (environment_1.isNothing(localizations)) { + throw new Error("Localized strings bundle index file is missing 'localizations' property"); + } + let strings; + const format = (_a = localizations.format) !== null && _a !== void 0 ? _a : "json/inline" /* jsonInline */; + if (format === "json/inline" /* jsonInline */) { + const inlineLocalizations = localizations; + strings = inlineLocalizations[language]; + } + else { + const externalLocalizations = localizations; + switch (externalLocalizations.format) { + case "json/multi-file" /* jsonMultiFile */: + { + // The path points to directory where JSON files are located. + // We don't even have to list a directory, just construct a final path. + // The path is also not an OS path but a bundle (e.g. JetPack) path. + // Bundle APIs always use "/" in the path, same as the paths used in the + // index.json (manifest) files. + const jsonPath = `${externalLocalizations.path}/${language}.json`; + strings = (await this.bundle.loadResource(jsonPath)); + } + break; + case "json/single-file" /* jsonSingleFile */: + // The bundle contains single JSON file with all strings dictionary in it. + strings = (await this.bundle.loadResource(externalLocalizations.path))[language]; + break; + case "loctable" /* loctable */: + throw new Error("Loctable format not supported in JS implementation"); + case "js" /* js */: + throw new Error("Not yet implemented"); + default: + throw new Error(`Unknown localization format: ${JSON.stringify(format)}`); + } + } + if (environment_1.isNothing(strings)) { + throw new Error(`Missing strings for ${language}`); + } + return new localized_strings_json_object_1.LocalizedStringsJSONObject(strings); + } +} +exports.LocalizedStringsBundle = LocalizedStringsBundle; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/localized-strings-json-object.js b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/localized-strings-json-object.js new file mode 100644 index 0000000..eb7a9dd --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/dependencies/localized-strings-json-object.js @@ -0,0 +1,21 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.LocalizedStringsJSONObject = void 0; +/** + * A type providing access to underlying localized strings JSON object. + */ +class LocalizedStringsJSONObject { + /** + * Create localized strings JSON object. + * + * @param strings - A dictionary containing localized strings. + */ + constructor(strings) { + this.strings = strings; + } + // MARK: - Localized Strings + string(key) { + return this.strings[key]; + } +} +exports.LocalizedStringsJSONObject = LocalizedStringsJSONObject; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/index.js new file mode 100644 index 0000000..624ce25 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/index.js @@ -0,0 +1,15 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./actions"), exports); +__exportStar(require("./dependencies"), exports); +__exportStar(require("./metrics"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/index.js new file mode 100644 index 0000000..e8d9f32 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/index.js @@ -0,0 +1,16 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./metrics-fields-aggregator"), exports); +__exportStar(require("./metrics-fields-builder"), exports); +__exportStar(require("./metrics-fields-context"), exports); +__exportStar(require("./metrics-fields-provider"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-aggregator.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-aggregator.js new file mode 100644 index 0000000..b53d7a9 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-aggregator.js @@ -0,0 +1,45 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.MetricsFieldsAggregator = void 0; +const optional_1 = require("@jet/environment/types/optional"); +const page_metrics_fields_provider_1 = require("../field-providers/page-metrics-fields-provider"); +class MetricsFieldsAggregator { + constructor() { + this.optInProviders = new Map(); + this.optOutProviders = new Map(); + } + static makeDefaultAggregator() { + const aggregator = new MetricsFieldsAggregator(); + aggregator.addOptInProvider(new page_metrics_fields_provider_1.PageMetricsFieldsProvider(), "pageFields"); + return aggregator; + } + addOptInProvider(provider, request) { + this.optInProviders.set(request, provider); + } + addOptOutProvider(provider, request) { + this.optOutProviders.set(request, provider); + } + removeOptInProvider(request) { + this.optInProviders.delete(request); + } + removeOptOutProvider(request) { + this.optOutProviders.delete(request); + } + addMetricsFields(options) { + options.including.forEach((request) => { + const provider = this.optInProviders.get(request); + if (optional_1.isNothing(provider)) { + // No provider registered + return; + } + provider.addMetricsFields(options.builder, options.context); + }); + this.optOutProviders.forEach((provider, request) => { + if (optional_1.isNothing(provider) || options.excluding.includes(request)) { + return; + } + provider.addMetricsFields(options.builder, options.context); + }); + } +} +exports.MetricsFieldsAggregator = MetricsFieldsAggregator; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-builder.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-builder.js new file mode 100644 index 0000000..d00e47b --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-builder.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.SimpleMetricsFieldsBuilder = void 0; +class SimpleMetricsFieldsBuilder { + constructor(baseFields) { + this.fields = baseFields; + } + addValue(value, field) { + this.fields[field] = value; + } + get allMetricsFields() { + return this.fields; + } +} +exports.SimpleMetricsFieldsBuilder = SimpleMetricsFieldsBuilder; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-context.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-context.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-context.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-provider.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-provider.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/aggregating/metrics-fields-provider.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/field-providers/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/field-providers/index.js new file mode 100644 index 0000000..250f6dc --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/field-providers/index.js @@ -0,0 +1,13 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./page-metrics-fields-provider"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/field-providers/page-metrics-fields-provider.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/field-providers/page-metrics-fields-provider.js new file mode 100644 index 0000000..67e0f53 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/field-providers/page-metrics-fields-provider.js @@ -0,0 +1,19 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.PageMetricsFieldsProvider = void 0; +const optional_1 = require("@jet/environment/types/optional"); +class PageMetricsFieldsProvider { + addMetricsFields(builder, context) { + const pageFields = context.pageFields; + if (optional_1.isNothing(pageFields)) { + // No page fields + return; + } + for (const field in pageFields) { + if (Object.prototype.hasOwnProperty.call(pageFields, field)) { + builder.addValue(pageFields[field], field); + } + } + } +} +exports.PageMetricsFieldsProvider = PageMetricsFieldsProvider; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/index.js new file mode 100644 index 0000000..469a960 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/index.js @@ -0,0 +1,18 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./aggregating"), exports); +__exportStar(require("./field-providers"), exports); +__exportStar(require("./linting"), exports); +__exportStar(require("./presenters"), exports); +__exportStar(require("./metrics-pipeline"), exports); +__exportStar(require("./recording"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/linting/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/linting/index.js new file mode 100644 index 0000000..7fb1fcf --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/linting/index.js @@ -0,0 +1,13 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./metrics-event-linter"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/linting/metrics-event-linter.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/linting/metrics-event-linter.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/linting/metrics-event-linter.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/metrics-pipeline.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/metrics-pipeline.js new file mode 100644 index 0000000..ea32424 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/metrics-pipeline.js @@ -0,0 +1,35 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.MetricsPipeline = exports.FlushBehavior = void 0; +const metrics_fields_builder_1 = require("./aggregating/metrics-fields-builder"); +// eslint-disable-next-line @typescript-eslint/no-shadow +var FlushBehavior; +(function (FlushBehavior) { + FlushBehavior[FlushBehavior["automatic"] = 0] = "automatic"; + FlushBehavior[FlushBehavior["never"] = 1] = "never"; +})(FlushBehavior = exports.FlushBehavior || (exports.FlushBehavior = {})); +class MetricsPipeline { + constructor(options) { + var _a; + this.aggregator = options.aggregator; + this.linter = options.linter; + this.recorder = options.recorder; + this.flushBehavior = (_a = options.flushBehavior) !== null && _a !== void 0 ? _a : FlushBehavior.automatic; + } + async process(data, context) { + const builder = new metrics_fields_builder_1.SimpleMetricsFieldsBuilder(data.fields); + this.aggregator.addMetricsFields({ + including: data.includingFields, + excluding: data.excludingFields, + builder: builder, + context: context, + }); + const lintedEvent = await this.linter.processEvent(builder.allMetricsFields); + this.recorder.record(lintedEvent, data.topic); + if (data.shouldFlush && this.flushBehavior === FlushBehavior.automatic) { + this.recorder.flush(); + } + return lintedEvent; + } +} +exports.MetricsPipeline = MetricsPipeline; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/index.js new file mode 100644 index 0000000..56e55cf --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/index.js @@ -0,0 +1,13 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./page-metrics-presenter"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/page-metrics-presenter.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/page-metrics-presenter.js new file mode 100644 index 0000000..c04bbd6 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/page-metrics-presenter.js @@ -0,0 +1,51 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.PageMetricsPresenter = void 0; +const optional_1 = require("@jet/environment/types/optional"); +class PageMetricsPresenter { + constructor(metricsPipeline) { + this.metricsPipeline = metricsPipeline; + this.isViewAppeared = false; + } + set pageMetrics(pageMetrics) { + this.pageMetricsStore = pageMetrics; + if (optional_1.isSome(pageMetrics) && this.isViewAppeared) { + this.processInstructions("pageEnter"); + } + } + get pageMetrics() { + return this.pageMetricsStore; + } + async processInstructions(invocationPoint) { + var _a, _b, _c; + if (optional_1.isNothing(this.pageMetrics)) { + return; + } + // istanbul ignore next + const invocationContext = { + customMetrics: (_a = this.baseContext) === null || _a === void 0 ? void 0 : _a.customMetrics, + pageFields: { + ...(_b = this.baseContext) === null || _b === void 0 ? void 0 : _b.pageFields, + ...(_c = this.pageMetrics) === null || _c === void 0 ? void 0 : _c.pageFields, + }, + }; + await Promise.all(this.pageMetrics.instructions.map((instruction) => { + const { invocationPoints } = instruction; + if (invocationPoints.length === 0 || !invocationPoints.includes(invocationPoint)) { + return; + } + return this.metricsPipeline.process(instruction.data, invocationContext); + })); + } + async didEnterPage() { + this.isViewAppeared = true; + if (optional_1.isSome(this.pageMetrics)) { + await this.processInstructions("pageEnter"); + } + } + async didLeavePage() { + await this.processInstructions("pageExit"); + this.isViewAppeared = false; + } +} +exports.PageMetricsPresenter = PageMetricsPresenter; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/index.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/index.js new file mode 100644 index 0000000..dad3b1c --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/index.js @@ -0,0 +1,14 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./logging-event-recorder"), exports); +__exportStar(require("./metrics-event-recorder"), exports); diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/logging-event-recorder.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/logging-event-recorder.js new file mode 100644 index 0000000..0e5ba43 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/logging-event-recorder.js @@ -0,0 +1,13 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.LoggingEventRecorder = void 0; +class LoggingEventRecorder { + record(event) { + console.log(`Record Event [${String(event.fields.eventType)}]`, event); + } + async flush() { + console.log("Flushing"); + return 0; + } +} +exports.LoggingEventRecorder = LoggingEventRecorder; diff --git a/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/metrics-event-recorder.js b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/metrics-event-recorder.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/lib/metrics/recording/metrics-event-recorder.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/index.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/index.js new file mode 100644 index 0000000..3f77177 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/index.js @@ -0,0 +1,19 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./models"), exports); +__exportStar(require("./types/globals"), exports); +__exportStar(require("./types/javascriptcore"), exports); +__exportStar(require("./types/metrics"), exports); +__exportStar(require("./types/models"), exports); +__exportStar(require("./types/optional"), exports); +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/json/validation.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/json/validation.js new file mode 100644 index 0000000..1351ad7 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/json/validation.js @@ -0,0 +1,250 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.unexpectedNull = exports.catchingContext = exports.context = exports.recordValidationIncidents = exports.endContext = exports.getContextNames = exports.beginContext = exports.messageForRecoveryAction = exports.isValidatable = exports.unexpectedType = exports.extendedTypeof = void 0; +const optional_1 = require("../types/optional"); +/** + * Returns a string containing the type of a given value. + * This function augments the built in `typeof` operator + * to return sensible values for arrays and null values. + * + * @privateRemarks + * This function is exported for testing. + * + * @param value - The value to find the type of. + * @returns A string containing the type of `value`. + */ +function extendedTypeof(value) { + if (Array.isArray(value)) { + return "array"; + } + else if (value === null) { + return "null"; + } + else { + return typeof value; + } +} +exports.extendedTypeof = extendedTypeof; +/** + * Reports a non-fatal validation failure, logging a message to the console. + * @param recovery - The recovery action taken when the bad type was found. + * @param expected - The expected type of the value. + * @param actual - The actual value. + * @param pathString - A string containing the path to the value on the object which failed type validation. + */ +function unexpectedType(recovery, expected, actual, pathString) { + const actualType = extendedTypeof(actual); + const prettyPath = optional_1.isSome(pathString) && pathString.length > 0 ? pathString : "<this>"; + trackIncident({ + type: "badType", + expected: expected, + // Our test assertions are matching the string interpolation of ${actual} value. + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + actual: `${actualType} (${actual})`, + objectPath: prettyPath, + contextNames: getContextNames(), + recoveryAction: recovery, + stack: new Error().stack, + }); +} +exports.unexpectedType = unexpectedType; +// endregion +/** + * Determines if a given object conforms to the Validatable interface + * @param possibleValidatable - An object that might be considered validatable + * + * @returns `true` if it is an instance of Validatable, `false` if not + */ +function isValidatable(possibleValidatable) { + if (optional_1.isNothing(possibleValidatable)) { + return false; + } + // MAINTAINER'S NOTE: We must check for either the existence of a pre-existing incidents + // property *or* the ability to add one. Failure to do so will cause + // problems for clients that either a) use interfaces to define their + // view models; or b) return collections from their service routes. + return (Object.prototype.hasOwnProperty.call(possibleValidatable, "$incidents") || + Object.isExtensible(possibleValidatable)); +} +exports.isValidatable = isValidatable; +/** + * Returns a developer-readable diagnostic message for a given recovery action. + * @param action - The recovery action to get the message for. + * @returns The message for `action`. + */ +function messageForRecoveryAction(action) { + switch (action) { + case "coercedValue": + return "Coerced format"; + case "defaultValue": + return "Default value used"; + case "ignoredValue": + return "Ignored value"; + default: + return "Unknown"; + } +} +exports.messageForRecoveryAction = messageForRecoveryAction; +// region Contexts +/** + * Shared validation context "stack". + * + * Because validation incidents propagate up the context stack, + * the representation used here is optimized for memory usage. + * A more literal representation of this would be a singly linked + * list describing a basic stack, but that will produce a large + * amount of unnecessary garbage and require copying `incidents` + * arrays backwards. + */ +const contextState = { + /// The names of each validation context on the stack. + nameStack: Array(), + /// All incidents reported so far. Cleared when the + /// context stack is emptied. + incidents: Array(), + // TODO: Removal of this is being tracked here: + // <rdar://problem/35015460> Intro Pricing: Un-suppress missing parent 'offers' error when server address missing key + /// The paths for incidents we wish to forgo tracking. + suppressedIncidentPaths: Array(), +}; +/** + * Begin a new validation context with a given name, + * pushing it onto the validation context stack. + * @param name - The name for the validation context. + */ +function beginContext(name) { + contextState.nameStack.push(name); +} +exports.beginContext = beginContext; +/** + * Traverses the validation context stack and collects all of the context names. + * @returns The names of all validation contexts on the stack, from oldest to newest. + */ +function getContextNames() { + if (contextState.nameStack.length === 0) { + return ["<empty stack>"]; + } + return contextState.nameStack.slice(0); +} +exports.getContextNames = getContextNames; +/** + * Ends the current validation context + */ +function endContext() { + if (contextState.nameStack.length === 0) { + console.warn("endContext() called without active validation context, ignoring"); + } + contextState.nameStack.pop(); +} +exports.endContext = endContext; +/** + * Records validation incidents back into an object that implements Validatable. + * + * Note: This method has a side-effect that the incident queue and name stack are cleared + * to prepare for the next thread's invocation. + * + * @param possibleValidatable - An object that may conform to Validatable, onto which we + * want to stash our validation incidents + */ +function recordValidationIncidents(possibleValidatable) { + if (isValidatable(possibleValidatable)) { + possibleValidatable.$incidents = contextState.incidents; + } + contextState.incidents = []; + contextState.nameStack = []; + contextState.suppressedIncidentPaths = []; +} +exports.recordValidationIncidents = recordValidationIncidents; +/** + * Create a transient validation context, and call a function that will return a value. + * + * Prefer this function over manually calling begin/endContext, + * it is exception safe. + * + * @param name - The name of the context + * @param producer - A function that produces a result + * @returns <Result> The resulting type + */ +function context(name, producer, suppressingPath) { + let suppressingName = null; + if (optional_1.isSome(suppressingPath) && suppressingPath.length > 0) { + suppressingName = name; + contextState.suppressedIncidentPaths.push(suppressingPath); + } + let result = null; + try { + beginContext(name); + result = producer(); + } + catch (e) { + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + if (!e.hasThrown) { + unexpectedType("defaultValue", "no exception", e.message); + e.hasThrown = true; + } + throw e; + } + finally { + if (name === suppressingName) { + contextState.suppressedIncidentPaths.pop(); + } + endContext(); + } + return result; +} +exports.context = context; +/** + * Create a transient validation context, that catches errors and returns null + * + * @param name - The name of the context + * @param producer - A function that produces a result + * @param caught - An optional handler to provide a value when an error is caught + * @returns <Result> The resulting type + */ +function catchingContext(name, producer, caught) { + let result = null; + try { + result = context(name, producer); + } + catch (e) { + result = null; + if (optional_1.isSome(caught)) { + result = caught(e); + } + } + return result; +} +exports.catchingContext = catchingContext; +/** + * Track an incident within the current validation context. + * @param incident - An incident object describing the problem. + */ +function trackIncident(incident) { + if (contextState.suppressedIncidentPaths.includes(incident.objectPath)) { + return; + } + contextState.incidents.push(incident); +} +// endregion +// region Nullability +/** + * Reports a non-fatal error indicating a value was unexpectedly null. + * @param recovery - The recovery action taken when the null value was found. + * @param expected - The expected type of the value. + * @param pathString - A string containing the path to the value on the object which was null. + */ +function unexpectedNull(recovery, expected, pathString) { + const prettyPath = optional_1.isSome(pathString) && pathString.length > 0 ? pathString : "<this>"; + trackIncident({ + type: "nullValue", + expected: expected, + actual: "null", + objectPath: prettyPath, + contextNames: getContextNames(), + recoveryAction: recovery, + stack: new Error().stack, + }); +} +exports.unexpectedNull = unexpectedNull; +// endregion +//# sourceMappingURL=validation.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/alert-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/alert-action.js new file mode 100644 index 0000000..fcc6ea5 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/alert-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=alert-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/compound-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/compound-action.js new file mode 100644 index 0000000..b3546e5 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/compound-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=compound-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/empty-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/empty-action.js new file mode 100644 index 0000000..efb4d70 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/empty-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=empty-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/external-url-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/external-url-action.js new file mode 100644 index 0000000..479a640 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/external-url-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=external-url-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/flow-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/flow-action.js new file mode 100644 index 0000000..6f70d98 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/flow-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=flow-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/flow-back-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/flow-back-action.js new file mode 100644 index 0000000..75c66a2 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/flow-back-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=flow-back-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/http-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/http-action.js new file mode 100644 index 0000000..e1fb6c3 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/http-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=http-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/http-template-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/http-template-action.js new file mode 100644 index 0000000..6cb84d4 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/http-template-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=http-template-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/index.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/index.js new file mode 100644 index 0000000..5e729a0 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/index.js @@ -0,0 +1,22 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./alert-action"), exports); +__exportStar(require("./compound-action"), exports); +__exportStar(require("./empty-action"), exports); +__exportStar(require("./external-url-action"), exports); +__exportStar(require("./flow-action"), exports); +__exportStar(require("./flow-back-action"), exports); +__exportStar(require("./http-action"), exports); +__exportStar(require("./http-template-action"), exports); +__exportStar(require("./toast-action"), exports); +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/toast-action.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/toast-action.js new file mode 100644 index 0000000..5d6a299 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/actions/toast-action.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=toast-action.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/artwork.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/artwork.js new file mode 100644 index 0000000..f13a40c --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/artwork.js @@ -0,0 +1,39 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.makeArtworkURLTemplate = void 0; +const validation = require("../json/validation"); +const optional_1 = require("../types/optional"); +const urls_1 = require("../util/urls"); +/** + * Regex to parse artwork URL template string. + */ +const URL_TEMPLATE_PARSER = new RegExp("^({w}|[0-9]+(?:.[0-9]*)?)x({h}|[0-9]+(?:.[0-9]*)?)({c}|[a-z]{2}).({f}|[a-z]+)$"); +/** + * Create an instance of artwork URL template from string. + * @param fromString - String to create artwork URL template from. + * @returns A new artwork URL template or `null` if string + * does not represent a valid artwork URL template. + */ +function makeArtworkURLTemplate(fromString) { + // A valid URL that ends with '{w}x{h}{c}.{f}' + // with any of placeholders possibly resolved to an actual value. + const url = new urls_1.URL(fromString); + if (url.pathname === undefined) { + validation.context("makeArtworkURLTemplate", () => { + validation.unexpectedType("ignoredValue", "A valid URL string", fromString); + }); + return null; + } + // Expecting 5 matches: whole string + width, height, crop code and format. + const lastPathComponent = fromString.substring(fromString.lastIndexOf("/") + 1); + const matches = URL_TEMPLATE_PARSER.exec(lastPathComponent); + if (optional_1.isNothing(matches) || matches.length !== 5) { + validation.context("makeArtworkURLTemplate", () => { + validation.unexpectedType("ignoredValue", "A valid artwork URL template ending with {w}x{h}{c}.{f} format", lastPathComponent); + }); + return null; + } + return fromString; +} +exports.makeArtworkURLTemplate = makeArtworkURLTemplate; +//# sourceMappingURL=artwork.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/button.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/button.js new file mode 100644 index 0000000..036c19a --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/button.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=button.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/color.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/color.js new file mode 100644 index 0000000..c68a1df --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/color.js @@ -0,0 +1,131 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.areEqual = exports.luminanceFrom = exports.dynamicWith = exports.named = exports.rgbWith = exports.htmlWith = void 0; +const optional_1 = require("../types/optional"); +// endregion +// region Constructors +/** + * Create new `HTMLColor` from hexadecimal string representation. + * + * @param hexString - Hexadecimal string representation. + */ +function htmlWith(hexString) { + if (optional_1.isNothing(hexString)) { + return null; + } + return { + $kind: "html", + value: hexString, + }; +} +exports.htmlWith = htmlWith; +/** + * Create new `RBGColor` with RGB components and opacity value. + * + * @param red - Red color value. + * @param green - Green color value. + * @param blue - Blue color value. + * @param alpha - Opacity value. + */ +function rgbWith(red, green, blue, alpha = 1.0) { + const newColor = { + $kind: "rgb", + red: red, + green: green, + blue: blue, + alpha: alpha, + }; + return newColor; +} +exports.rgbWith = rgbWith; +/** + * Create new named color using the color name. + * + * @param name - The name of the color. + */ +function named(name) { + const newColor = { + $kind: "named", + name: name, + }; + return newColor; +} +exports.named = named; +/** + * Create new dynamic color with light and dark color variants. + * + * @param lightColor - The light color variant. + * @param lightHighContrastColor - The light hight-contrast color variant. + * @param darkColor - The dark color variant. + * @param darkHighContrastColor - The dark hight-contrast color variant. + */ +function dynamicWith(lightColor, lightHighContrastColor, darkColor, darkHighContrastColor) { + const newColor = { + $kind: "dynamic", + lightColor: lightColor, + lightHighContrastColor: lightHighContrastColor, + darkColor: darkColor, + darkHighContrastColor: darkHighContrastColor, + }; + return newColor; +} +exports.dynamicWith = dynamicWith; +// endregion +// region Properties +/** + * Get the luminance of the color. + * + * @param rgbColor - The RGB color to get luminance for. + */ +function luminanceFrom(rgbColor) { + // Note: This is lifted from UIColor_Private + // Using RGB color components, calculates and returns (0.2126 * r) + (0.7152 * g) + (0.0722 * b). + return rgbColor.red * 0.2126 + rgbColor.green * 0.7152 + rgbColor.blue * 0.0722; +} +exports.luminanceFrom = luminanceFrom; +// endregion +// region Identity +/** + * Compare two colors for equality. + * + * @param color1 - Left hand side color to compare. + * @param color2 - Right hand side color to compare. + * @returns A Boolean indicating whether the colors are equal. + */ +function areEqual(color1, color2) { + if (optional_1.isNothing(color1)) { + return optional_1.isNothing(color2); + } + else if (optional_1.isNothing(color2)) { + return optional_1.isNothing(color1); + } + const kind1 = color1.$kind; + const kind2 = color2.$kind; + if (kind1 === "named" && kind2 === "named") { + const namedColor1 = color1; + const namedColor2 = color2; + return namedColor1.name === namedColor2.name; + } + else if (kind1 === "rgb" && kind2 === "rgb") { + const rgbColor1 = color1; + const rgbColor2 = color2; + return (rgbColor1.red === rgbColor2.red && + rgbColor1.green === rgbColor2.green && + rgbColor1.blue === rgbColor2.blue && + rgbColor1.alpha === rgbColor2.alpha); + } + else if (kind1 === "dynamic" && kind2 === "dynamic") { + const dynamicColor1 = color1; + const dynamicColor2 = color2; + return (areEqual(dynamicColor1.lightColor, dynamicColor2.lightColor) && + areEqual(dynamicColor1.lightHighContrastColor, dynamicColor2.lightHighContrastColor) && + areEqual(dynamicColor1.darkColor, dynamicColor2.darkColor) && + areEqual(dynamicColor1.darkHighContrastColor, dynamicColor2.darkHighContrastColor)); + } + else { + return false; + } +} +exports.areEqual = areEqual; +// endregion +//# sourceMappingURL=color.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/index.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/index.js new file mode 100644 index 0000000..19009ad --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/index.js @@ -0,0 +1,21 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./actions"), exports); +__exportStar(require("./artwork"), exports); +__exportStar(require("./button"), exports); +__exportStar(require("./color"), exports); +__exportStar(require("./menu"), exports); +__exportStar(require("./paragraph"), exports); +__exportStar(require("./programmed-text"), exports); +__exportStar(require("./video"), exports); +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/menu.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/menu.js new file mode 100644 index 0000000..200dc6b --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/menu.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.menuSeparatorID = void 0; +/** + * A standard identifier for including a separator in a menu. + */ +exports.menuSeparatorID = "com.apple.JetEngine.separator"; +//# sourceMappingURL=menu.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/paragraph.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/paragraph.js new file mode 100644 index 0000000..3518ea7 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/paragraph.js @@ -0,0 +1,4 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +// endregion +//# sourceMappingURL=paragraph.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/programmed-text.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/programmed-text.js new file mode 100644 index 0000000..18a8337 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/programmed-text.js @@ -0,0 +1,5 @@ +"use strict"; +// region ProgrammedText +Object.defineProperty(exports, "__esModule", { value: true }); +// endregion +//# sourceMappingURL=programmed-text.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/video.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/video.js new file mode 100644 index 0000000..0f0031f --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/models/video.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=video.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/bag.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/bag.js new file mode 100644 index 0000000..ffe6106 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/bag.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=bag.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/bundle.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/bundle.js new file mode 100644 index 0000000..9a818e7 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/bundle.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=bundle.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/cookie-provider.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/cookie-provider.js new file mode 100644 index 0000000..e681941 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/cookie-provider.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=cookie-provider.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/cryptography.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/cryptography.js new file mode 100644 index 0000000..de648d8 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/cryptography.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=cryptography.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/host.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/host.js new file mode 100644 index 0000000..9dbd12d --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/host.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=host.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/index.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/index.js new file mode 100644 index 0000000..028f856 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/index.js @@ -0,0 +1,51 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +/* `preprocessor` and `testContent` are normally replaced by inline literals while bundling an app's JS. + * + * If these values have not been set we want to provide defaults however + * attempting to access them can trigger a ReferenceError as the + * variables are undefined (distinct from a defined variable being set to + * `undefined`). + * + * `typeof` checks can safely test undefined variables, note that these checks will become: + * `typeof { DEBUG_BUILD: true, ... }` when webpack's DefinePlugin is used in @jet/build's webpack task. + * When these variables have not been replaced we need to use `globalThis` to set them on the global scope + * in order to avoid ReferenceErrors attempting to access them. + */ +if (typeof preprocessor === "undefined") { + globalThis.preprocessor = { + PRODUCTION_BUILD: false, + CARRY_BUILD: false, + DEBUG_BUILD: false, + INTERNAL_BUILD: false, + }; +} +if (typeof testContent === "undefined") { + globalThis.testContent = { + INCLUDE_TEST_CONTENT: false, + }; +} +__exportStar(require("./bag"), exports); +__exportStar(require("./bundle"), exports); +__exportStar(require("./cookie-provider"), exports); +__exportStar(require("./cryptography"), exports); +__exportStar(require("./host"), exports); +__exportStar(require("./jscookie"), exports); +__exportStar(require("./net"), exports); +__exportStar(require("./platform"), exports); +__exportStar(require("./plist"), exports); +__exportStar(require("./preprocessor"), exports); +__exportStar(require("./random"), exports); +__exportStar(require("./service"), exports); +__exportStar(require("./types"), exports); +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/jscookie.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/jscookie.js new file mode 100644 index 0000000..bfd5e29 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/jscookie.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=jscookie.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/net.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/net.js new file mode 100644 index 0000000..6b810d4 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/net.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=net.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/platform.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/platform.js new file mode 100644 index 0000000..eafaa33 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/platform.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=platform.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/plist.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/plist.js new file mode 100644 index 0000000..29503e6 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/plist.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=plist.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/preprocessor.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/preprocessor.js new file mode 100644 index 0000000..a04398d --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/preprocessor.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=preprocessor.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/random.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/random.js new file mode 100644 index 0000000..3484776 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/random.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=random.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/service.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/service.js new file mode 100644 index 0000000..a4b3c49 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/service.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=service.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/types.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/types.js new file mode 100644 index 0000000..62c2e70 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/globals/types.js @@ -0,0 +1,16 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.services = exports.random = exports.plist = exports.platform = exports.net = exports.localizer = exports.host = exports.cryptography = exports.cookieProvider = exports.bundle = exports.bag = void 0; +const metatype_1 = require("../../util/metatype"); +exports.bag = metatype_1.makeMetatype("jet-engine:bag"); +exports.bundle = metatype_1.makeMetatype("jet-engine:bundle"); +exports.cookieProvider = metatype_1.makeMetatype("jet-engine:cookieProvider"); +exports.cryptography = metatype_1.makeMetatype("jet-engine:cryptography"); +exports.host = metatype_1.makeMetatype("jet-engine:host"); +exports.localizer = metatype_1.makeMetatype("jet-engine:localizer"); +exports.net = metatype_1.makeMetatype("jet-engine:net"); +exports.platform = metatype_1.makeMetatype("jet-engine:platform"); +exports.plist = metatype_1.makeMetatype("jet-engine:plist"); +exports.random = metatype_1.makeMetatype("jet-engine:random"); +exports.services = metatype_1.makeMetatype("jet-engine:services"); +//# sourceMappingURL=types.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/javascriptcore/console.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/javascriptcore/console.js new file mode 100644 index 0000000..c4fb39a --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/javascriptcore/console.js @@ -0,0 +1,14 @@ +"use strict"; +/* + * Describes standard functionality available in JSContexts + * + * Types are defined here to allow us to match the behavior available in JSContext in the target OS + * which may not exactly match the definitions in standard TypeScript lib files, particularly on a + * pre-release OS. + * + * The living standard for the Console API is available at https://console.spec.whatwg.org + * The WebKit team has documented their interfaces at https://webkit.org/web-inspector/console-object-api/ + * The equivalent interface in Node is https://nodejs.org/api/console.html + */ +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=console.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/javascriptcore/index.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/javascriptcore/index.js new file mode 100644 index 0000000..ee0a3fc --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/javascriptcore/index.js @@ -0,0 +1,14 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __exportStar = (this && this.__exportStar) || function(m, exports) { + for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +__exportStar(require("./console"), exports); +//# sourceMappingURL=index.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/metrics.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/metrics.js new file mode 100644 index 0000000..565e4b9 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/metrics.js @@ -0,0 +1,57 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.notInstrumented = exports.PageInvocationPoint = exports.EMPTY_LINTED_METRICS_EVENT = void 0; +/** + * An empty linted metrics event. + * + * The empty events should be skipped from recording + * by metrics event recorders. + */ +exports.EMPTY_LINTED_METRICS_EVENT = { + fields: {}, + issues: [], +}; +var PageInvocationPoint; +(function (PageInvocationPoint) { + PageInvocationPoint["pageEnter"] = "pageEnter"; + PageInvocationPoint["pageExit"] = "pageExit"; + PageInvocationPoint["appExit"] = "appExit"; + PageInvocationPoint["appEnter"] = "appEnter"; + PageInvocationPoint["backButton"] = "backButton"; +})(PageInvocationPoint = exports.PageInvocationPoint || (exports.PageInvocationPoint = {})); +/** + * Returns an empty metrics instance of the specified metrics type. + * @param metricsType - Type of the metrics data to return. + * + * @deprecated Do not use, all metrics events should be instrumented. + */ +function notInstrumented(metricsType) { + switch (metricsType) { + case 0 /* ActionMetrics */: + return { + data: [], + custom: {}, + }; + case 1 /* FetchTimingMetrics */: + return {}; + case 2 /* PageMetrics */: + return { + instructions: [], + custom: {}, + }; + case 3 /* ImpressionMetrics */: + return { + id: { + id: "", + impressionIndex: NaN, + }, + fields: {}, + custom: {}, + }; + default: + return {}; + } +} +exports.notInstrumented = notInstrumented; +// endregion +//# sourceMappingURL=metrics.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/models.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/models.js new file mode 100644 index 0000000..b2dccd6 --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/models.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=models.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/optional.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/optional.js new file mode 100644 index 0000000..ea3aaeb --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/types/optional.js @@ -0,0 +1,71 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.flatMapOptional = exports.mapOptional = exports.unsafeUnwrapOptional = exports.unwrapOptional = exports.isSome = exports.isNothing = exports.unsafeUninitialized = void 0; +/** + * Bypass the protection provided by the `Optional` type + * and pretend to produce a value of `Some<T>` while + * actually returning `Nothing`. + */ +function unsafeUninitialized() { + return undefined; +} +exports.unsafeUninitialized = unsafeUninitialized; +/** + * Test whether an optional does not contain a value. + * + * @param value - An optional value to test. + */ +function isNothing(value) { + return value === undefined || value === null; +} +exports.isNothing = isNothing; +/** + * Test whether an optional contains a value. + * @param value - An optional value to test. + */ +function isSome(value) { + return value !== undefined && value !== null; +} +exports.isSome = isSome; +/** + * Unwrap the value contained in a given optional, + * throwing an error if there is no value. + * + * @param value - A value to unwrap. + */ +function unwrapOptional(value) { + if (isNothing(value)) { + throw new ReferenceError(); + } + return value; +} +exports.unwrapOptional = unwrapOptional; +/** + * Unwrap the value contained in a given optional + * without checking if the value exists. + * + * @param value - A value to unwrap. + */ +function unsafeUnwrapOptional(value) { + return value; +} +exports.unsafeUnwrapOptional = unsafeUnwrapOptional; +function mapOptional(value, body) { + if (isSome(value)) { + return body(value); + } + else { + return value; + } +} +exports.mapOptional = mapOptional; +function flatMapOptional(value, body) { + if (isSome(value)) { + return body(value); + } + else { + return value; + } +} +exports.flatMapOptional = flatMapOptional; +//# sourceMappingURL=optional.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/util/metatype.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/util/metatype.js new file mode 100644 index 0000000..372f58f --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/util/metatype.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.makeMetatype = void 0; +function makeMetatype(name) { + return { + name: name, + }; +} +exports.makeMetatype = makeMetatype; +//# sourceMappingURL=metatype.js.map
\ No newline at end of file diff --git a/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/util/urls.js b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/util/urls.js new file mode 100644 index 0000000..fd7ec3e --- /dev/null +++ b/shared/metrics-8/node_modules/@jet/engine/node_modules/@jet/environment/util/urls.js @@ -0,0 +1,370 @@ +"use strict"; +// MARK: - Parsing Regular Expressions +Object.defineProperty(exports, "__esModule", { value: true }); +exports.URL = void 0; +const optional_1 = require("../types/optional"); +const protocolRegex = /^([a-z][a-z0-9.+-]*:)(\/\/)?([\S\s]*)/i; +const queryParamRegex = /([^=?&]+)=?([^&]*)/g; +const componentOrder = ["hash", "query", "pathname", "host"]; +class URL { + constructor(url) { + var _a; + this.query = {}; + if (optional_1.isNothing(url)) { + return; + } + // Split the protocol from the rest of the urls + let remainder = url; + const match = protocolRegex.exec(url); + if (optional_1.isSome(match)) { + // Pull out the protocol + let protocol = match[1]; + if (protocol !== null && protocol !== undefined) { + protocol = protocol.split(":")[0]; + } + this.protocol = protocol !== null && protocol !== void 0 ? protocol : undefined; + // Save the remainder + remainder = (_a = match[3]) !== null && _a !== void 0 ? _a : undefined; + } + // Then match each component in a specific order + let parse = { remainder: remainder, result: undefined }; + for (const component of componentOrder) { + if (parse === undefined || parse.remainder === undefined) { + break; + } + switch (component) { + case "hash": { + parse = splitUrlComponent(parse.remainder, "#", "suffix"); + this.hash = parse === null || parse === void 0 ? void 0 : parse.result; + break; + } + case "query": { + parse = splitUrlComponent(parse.remainder, "?", "suffix"); + if ((parse === null || parse === void 0 ? void 0 : parse.result) !== undefined) { + this.query = URL.queryFromString(parse.result); + } + break; + } + case "pathname": { + parse = splitUrlComponent(parse.remainder, "/", "suffix"); + if ((parse === null || parse === void 0 ? void 0 : parse.result) !== undefined) { + // Replace the initial /, since paths require it + this.pathname = "/" + parse.result; + } + break; + } + case "host": { + const authorityParse = splitUrlComponent(parse.remainder, "@", "prefix"); + const userInfo = authorityParse === null || authorityParse === void 0 ? void 0 : authorityParse.result; + const hostPort = authorityParse === null || authorityParse === void 0 ? void 0 : authorityParse.remainder; + if (userInfo !== undefined) { + const userInfoSplit = userInfo.split(":"); + this.username = decodeURIComponent(userInfoSplit[0]); + this.password = decodeURIComponent(userInfoSplit[1]); + } + if (hostPort !== undefined) { + const hostPortSplit = hostPort.split(":"); + this.host = hostPortSplit[0]; + this.port = hostPortSplit[1]; + } + break; + } + default: { + throw new Error("Unhandled case!"); + } + } + } + } + get(component) { + switch (component) { + // Exhaustive match to make sure TS property minifiers and other + // transformer plugins do not break this code. + case "protocol": + return this.protocol; + case "username": + return this.username; + case "password": + return this.password; + case "port": + return this.port; + case "pathname": + return this.pathname; + case "query": + return this.query; + case "hash": + return this.hash; + default: + // The fallback for component which is not a property of URL object. + return this[component]; + } + } + set(component, value) { + if (value === undefined) { + return this; + } + if (component === "query") { + if (typeof value === "string") { + value = URL.queryFromString(value); + } + } + switch (component) { + // Exhaustive match to make sure TS property minifiers and other + // transformer plugins do not break this code. + case "protocol": + this.protocol = value; + break; + case "username": + this.username = value; + break; + case "password": + this.password = value; + break; + case "port": + this.port = value; + break; + case "pathname": + this.pathname = value; + break; + case "query": + this.query = value; + break; + case "hash": + this.hash = value; + break; + default: + // The fallback for component which is not a property of URL object. + this[component] = value; + break; + } + return this; + } + append(component, value) { + let existingValue = this.get(component); + let newValue; + if (component === "query") { + if (existingValue === undefined) { + existingValue = {}; + } + if (typeof value === "string") { + value = URL.queryFromString(value); + } + if (typeof existingValue === "string") { + newValue = { existingValue, ...value }; + } + else { + newValue = { ...existingValue, ...value }; + } + } + else { + if (existingValue === undefined) { + existingValue = ""; + } + let existingValueString = existingValue; + if (existingValueString === undefined) { + existingValueString = ""; + } + let newValueString = existingValueString; + if (component === "pathname") { + const pathLength = existingValueString.length; + if (pathLength === 0 || existingValue[pathLength - 1] !== "/") { + newValueString += "/"; + } + } + // The component is not "query" so we treat value as string. + // eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-plus-operands + newValueString += value; + newValue = newValueString; + } + return this.set(component, newValue); + } + param(key, value) { + if (key === null) { + return this; + } + if (this.query === undefined) { + this.query = {}; + } + if (value === undefined) { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete this.query[key]; + } + else { + this.query[key] = value; + } + return this; + } + removeParam(key) { + if (key === undefined || this.query === undefined) { + return this; + } + if (key in this.query) { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete this.query[key]; + } + return this; + } + path(value) { + return this.append("pathname", value); + } + pathExtension() { + var _a, _b; + // Extract path extension if one exists + if (this.pathname === undefined) { + return undefined; + } + const lastFilenameComponents = (_b = (_a = this.pathname + .split("/") + .filter((item) => item.length > 0) // Remove any double or trailing slashes + .pop()) === null || _a === void 0 ? void 0 : _a.split(".")) !== null && _b !== void 0 ? _b : []; + if (lastFilenameComponents.filter(function (part) { + return part !== ""; + }).length < 2 // Remove any empty parts (e.g. .ssh_config -> ["ssh_config"]) + ) { + return undefined; + } + return lastFilenameComponents.pop(); + } + /** + * Returns the path components of the URL + * @returns An array of non-empty path components from `urls`. + */ + pathComponents() { + if (this.pathname === undefined) { + return []; + } + return this.pathname.split("/").filter((component) => component.length > 0); + } + /** + * Same as toString + * + * @returns A string representation of the URL + */ + build() { + return this.toString(); + } + /** + * Converts the URL to a string + * + * @returns A string representation of the URL + */ + toString() { + let url = ""; + if (this.protocol !== undefined) { + url += this.protocol + "://"; + } + if (this.username !== undefined) { + url += encodeURIComponent(this.username); + if (this.password !== undefined) { + url += ":" + encodeURIComponent(this.password); + } + url += "@"; + } + if (this.host !== undefined) { + url += this.host; + if (this.port !== undefined) { + url += ":" + this.port; + } + } + if (this.pathname !== undefined) { + url += this.pathname; + } + if (this.query !== undefined && Object.keys(this.query).length !== 0) { + url += "?" + URL.toQueryString(this.query); + } + if (this.hash !== undefined) { + url += "#" + this.hash; + } + return url; + } + // ---------------- + // Static API + // ---------------- + /** + * Converts a string into a query dictionary + * @param query - The string to parse + * @returns The query dictionary containing the key-value pairs in the query string + */ + static queryFromString(query) { + const result = {}; + let parseResult = queryParamRegex.exec(query); + while (parseResult !== null) { + const key = decodeURIComponent(parseResult[1]); + const value = decodeURIComponent(parseResult[2]); + result[key] = value; + parseResult = queryParamRegex.exec(query); + } + return result; + } + /** + * Converts a query dictionary into a query string + * + * @param query - The query dictionary + * @returns The string representation of the query dictionary + */ + static toQueryString(query) { + let queryString = ""; + let first = true; + for (const key of Object.keys(query)) { + if (!first) { + queryString += "&"; + } + first = false; + queryString += encodeURIComponent(key); + const value = query[key]; + if (value !== null && value.length > 0) { + queryString += "=" + encodeURIComponent(value); + } + } + return queryString; + } + /** + * Convenience method to instantiate a URL from a string + * @param url - The URL string to parse + * @returns The new URL object representing the URL + */ + static from(url) { + return new URL(url); + } + /** + * Convenience method to instantiate a URL from numerous (optional) components + * @param protocol - The protocol type + * @param host - The host name + * @param path - The path + * @param query - The query + * @param hash - The hash + * @returns The new URL object representing the URL + */ + static fromComponents(protocol, host, path, query, hash) { + const url = new URL(); + url.protocol = protocol; + url.host = host; + url.pathname = path; + url.query = query !== null && query !== void 0 ? query : {}; + url.hash = hash; + return url; + } +} +exports.URL = URL; +// MARK: - Helpers +function splitUrlComponent(input, marker, style) { + const index = input.indexOf(marker); + let result; + let remainder = input; + if (index !== -1) { + const prefix = input.slice(0, index); + const suffix = input.slice(index + marker.length, input.length); + if (style === "prefix") { + result = prefix; + remainder = suffix; + } + else { + result = suffix; + remainder = prefix; + } + } + return { + result: result, + remainder: remainder, + }; +} +//# sourceMappingURL=urls.js.map
\ No newline at end of file diff --git a/shared/metrics-8/src/constants.ts b/shared/metrics-8/src/constants.ts new file mode 100644 index 0000000..6eff451 --- /dev/null +++ b/shared/metrics-8/src/constants.ts @@ -0,0 +1,19 @@ +/** + * A list of event types we use across all onyx apps for metrics. + */ +export const METRICS_EVENT_TYPES = [ + // The following types come from the jet enum `MetricsEventType` + // https://github.pie.apple.com/app-store/jet-js/blob/505144151e875c1bcbacd898216127fbc14c1562/packages/environment/src/types/metrics.ts#L198-L205 + // and the events could be handled by MetricsKit + // https://github.pie.apple.com/amp-ae/mt-metricskit/tree/dev/packages/processors/mt-metricskit-processor-clickstream/src/metrics/event_handlers + 'account', // For GDPR + 'click', + 'dialog', + 'enter', + 'exit', + 'impressions', + 'media', + 'page', + 'pageRender', + 'search', +] as const; diff --git a/shared/metrics-8/src/impression-provider.ts b/shared/metrics-8/src/impression-provider.ts new file mode 100644 index 0000000..852d418 --- /dev/null +++ b/shared/metrics-8/src/impression-provider.ts @@ -0,0 +1,27 @@ +import type { + MetricsFieldsBuilder, + MetricsFieldsContext, + MetricsFieldsProvider, +} from '@jet/engine'; +import { IMPRESSION_CONTEXT_NAME } from './impressions/constants'; +import type { Impressions } from './impressions'; + +export class ImpressionFieldProvider implements MetricsFieldsProvider { + constructor(private readonly appContext: Map<string, unknown>) { + this.appContext = appContext; + } + + addMetricsFields( + builder: MetricsFieldsBuilder, + _metricsContext: MetricsFieldsContext, + ) { + const impressionInstance = this.appContext.get( + IMPRESSION_CONTEXT_NAME, + ) as Impressions; + + if (impressionInstance?.settings?.captureType === 'jet') { + let impressions = impressionInstance.consumeImpressions(); + builder.addValue(impressions, 'impressions'); + } + } +} diff --git a/shared/metrics-8/src/impression-snapshot-provider.ts b/shared/metrics-8/src/impression-snapshot-provider.ts new file mode 100644 index 0000000..c7261f9 --- /dev/null +++ b/shared/metrics-8/src/impression-snapshot-provider.ts @@ -0,0 +1,27 @@ +import type { + MetricsFieldsBuilder, + MetricsFieldsContext, + MetricsFieldsProvider, +} from '@jet/engine'; +import { IMPRESSION_CONTEXT_NAME } from './impressions/constants'; +import type { Impressions } from './impressions'; + +export class ImpressionSnapshotFieldProvider implements MetricsFieldsProvider { + constructor(private readonly appContext: Map<string, unknown>) { + this.appContext = appContext; + } + + addMetricsFields( + builder: MetricsFieldsBuilder, + _metricsContext: MetricsFieldsContext, + ) { + const impressionInstance = this.appContext.get( + IMPRESSION_CONTEXT_NAME, + ) as Impressions; + + if (impressionInstance?.settings?.captureType === 'jet') { + let impressions = impressionInstance.captureSnapshotImpression(); + builder.addValue(impressions, 'impressions'); + } + } +} diff --git a/shared/metrics-8/src/impressions/constants.ts b/shared/metrics-8/src/impressions/constants.ts new file mode 100644 index 0000000..0638f9e --- /dev/null +++ b/shared/metrics-8/src/impressions/constants.ts @@ -0,0 +1 @@ +export const IMPRESSION_CONTEXT_NAME = 'metrics:impression' as const; diff --git a/shared/metrics-8/src/impressions/index.ts b/shared/metrics-8/src/impressions/index.ts new file mode 100644 index 0000000..b904feb --- /dev/null +++ b/shared/metrics-8/src/impressions/index.ts @@ -0,0 +1,252 @@ +import type { Logger, LoggerFactory } from '@amp/web-apps-logger/src/types'; +import { IMPRESSION_CONTEXT_NAME } from './constants'; +import { createSvelteImpressionAction } from './utils/svelte/impressions-svelte-action'; +import type { + AppImpressionModel, + ImpressionSettings, + ImpressionsInstance, +} from './types'; +import type { + ImpressionObserver, + newInstanceWithMetricsConfig, +} from '@amp-metrics/mt-impressions-observer'; +import type { ClickstreamProcessor } from '@amp-metrics/mt-metricskit-processor-clickstream'; + +/** + * Adapter class to handle interactions with + * metricsKit impression observer. + */ +export class Impressions implements ImpressionsInstance { + private readonly logger: Logger; + private impressionObserverInstance: ImpressionObserver | undefined; + private hasInitialized: boolean = false; + private impressionDataMap: Map<HTMLElement, any> = new Map(); + private currentSnapshot: Record<string, unknown>[] = []; + private readonly impressionSettings: ImpressionSettings | undefined; + + constructor( + loggerFactory: LoggerFactory, + context: Map<string, unknown>, + settings?: ImpressionSettings, + ) { + this.logger = loggerFactory.loggerFor(IMPRESSION_CONTEXT_NAME); + this.impressionSettings = settings; + + context.set(IMPRESSION_CONTEXT_NAME, this); + } + + async init( + makeImpressionObserver: typeof newInstanceWithMetricsConfig, + clickStreamInstance: ClickstreamProcessor, + ) { + if (this.hasInitialized) { + this.logger.warn( + 'Ignoring, Impressions.init() can only be called once', + ); + return; + } + + const options = { root: document, rootMargin: '0px' }; + const config = clickStreamInstance.config; + const impressionObserver: ImpressionObserver = + await makeImpressionObserver(config, options); + + impressionObserver.setDelegate({ + extractImpressionInfo: (domNode: HTMLElement) => { + const dataMap = this.impressionDataMap; + const nodeMetricData = dataMap.get(domNode); + if (nodeMetricData) { + const impressionData = nodeMetricData.impressionMetrics; + impressionData.location = + clickStreamInstance.utils.eventFields.buildLocationStructure( + domNode, + (node: HTMLElement) => { + const metrics = dataMap.get(node); + if (metrics?.location) { + return metrics.location; + } + return; + }, + ); + return impressionData; + } else { + this.logger.warn('no impression data found for', domNode); + } + }, + }); + + this.impressionObserverInstance = impressionObserver; + this.impressionDataMap.forEach((_value, node) => { + this.logger.debug('observing deffered node', node); + this.impressionObserverInstance?.observe(node); + }); + this.hasInitialized = true; + + this.logger.debug('impressions initialized'); + } + + get settings() { + return this.impressionSettings; + } + + isEnabled(event: 'click' | 'exit' | 'impressions'): boolean { + if (this.impressionSettings?.captureType === 'jet') { + return ( + this.impressionSettings?.metricsKitEvents?.includes(event) ?? + false + ); + } + return true; + } + + consumeImpressions(): Record<string, unknown>[] | undefined { + if (this.hasInitialized) { + this.logger.debug('consuming impression metrics'); + return this.impressionObserverInstance?.consumeImpressions(); + } + this.logger.warn('impressions not avaiable yet'); + return; + } + + captureSnapshotImpression(): Record<string, unknown>[] | undefined { + const snapshot = + this.impressionObserverInstance?.snapshotImpressions() ?? []; + + // if the current page already transitioned. fallback to the snapshot we captured before transition + if (snapshot.length === 0) { + return this.getSnapshotImpression(); + } + + return snapshot; + } + + getSnapshotImpression(): Record<string, unknown>[] | undefined { + if (this.hasInitialized) { + return this.currentSnapshot; + } + this.logger.warn('impressions not avaiable yet'); + return; + } + + setCurrentSnapshot(): void { + if (this.hasInitialized) { + this.logger.debug('capturing impression snapshot'); + this.currentSnapshot = + this.impressionObserverInstance?.snapshotImpressions() ?? []; + } else { + this.logger.warn('impressions not avaiable yet'); + } + } + + get nodeList() { + const impressionClass = this; + + return new Proxy(impressionClass.impressionDataMap, { + get(target, prop, receiver) { + const orginalFn = Reflect.get(target, prop, receiver); + + // overriding 'set' to also be able to observe + if (prop === 'set') { + return ( + node: HTMLElement, + value: Record<string, unknown>, + ) => { + if (impressionClass.hasInitialized) { + impressionClass.logger.debug( + 'observing', + node, + value, + ); + + impressionClass.impressionObserverInstance?.observe( + node, + ); + } + + return orginalFn.bind(target)(node, value); + }; + } + + // overriding 'delete' to also be able to unobserve + if (prop === 'delete') { + return (node: HTMLElement) => { + if (impressionClass.hasInitialized) { + impressionClass.logger.debug('unobserve', node); + impressionClass.impressionObserverInstance?.unobserve( + node, + ); + } + + return orginalFn.bind(target)(node); + }; + } + + return orginalFn.bind(target); + }, + set(target, prop, value) { + return Reflect.set(target, prop, value); + }, + }); + } +} + +/** + * Server Noop for above + */ +class ServerNoopImpressions implements ImpressionsInstance { + readonly nodeList: WeakMap<any, any>; + constructor() { + this.nodeList = new WeakMap(); + } + setCurrentSnapshot(): void {} +} + +/** + * Gets the current Impression instance from the Svelte context. + * + * @return The current instance of Impression + */ +export function generateBrowserImpressionsContextGetter( + getContext: (context: string) => unknown, +): () => AppImpressionModel { + return function getImpressions(): AppImpressionModel { + const impressions = getContext(IMPRESSION_CONTEXT_NAME) as + | Impressions + | undefined; + + if (!impressions) { + const noopImpressions = new ServerNoopImpressions(); + return { + captureImpressions: (_node: any, _impressionsData: any) => { + return { + destroy() {}, + }; + }, + impressions: noopImpressions, + }; + } + + return { + captureImpressions: createSvelteImpressionAction(impressions), + impressions, + }; + }; +} + +/** + * Server No-op for generateImpressionsContextGetter + * + */ +export function generateServerImpressionsContextGetter( + _getContext: (context: string) => unknown, +): () => AppImpressionModel { + const impressions = new ServerNoopImpressions(); + return () => ({ + captureImpressions: (_node: any, _impressionsData: any) => { + return { + destroy() {}, + }; + }, + impressions, + }); +} diff --git a/shared/metrics-8/src/index.ts b/shared/metrics-8/src/index.ts new file mode 100644 index 0000000..59510b0 --- /dev/null +++ b/shared/metrics-8/src/index.ts @@ -0,0 +1,578 @@ +import type { Logger, LoggerFactory } from '@amp/web-apps-logger'; +import { getPWADisplayMode, PWADisplayMode } from '@amp/web-apps-utils/src'; +import type { + LintedMetricsEvent, + MetricsData, + MetricsFields, +} from '@jet/environment/types/metrics'; +import type { PageMetrics } from '@jet/environment/types/metrics'; + +import type { Opt } from '@jet/environment'; + +import { + MetricsFieldsAggregator, + type MetricsFieldsContext, + type MetricsFieldsProvider, + MetricsPipeline, + PageMetricsPresenter, + type MetricsEventRecorder, +} from '@jet/engine'; + +import { + CompositeEventRecorder, + type FunnelKitConfig, + FunnelKitRecorder, + LoggingEventRecorder, + type MetricKitConfig, + MetricsKitRecorder, + VoidEventRecorder, +} from './recorder'; + +import type { + MetricsEnterEventType, + MetricsExitEventType, + SystemLoggerLevel, +} from './types'; + +import type { + EnvironmentDelegates, + WebDelegates as WebDelegatesInstance, +} from '@amp-metrics/mt-metricskit-delegates-web'; +import type { ClickstreamProcessor as ClickstreamProcessorInstance } from '@amp-metrics/mt-metricskit-processor-clickstream'; +import { Impressions } from './impressions'; +import { buildMakeAjaxRequest } from './utils/metrics-dev-console/metrics-dev-network'; +import { ImpressionFieldProvider } from './impression-provider'; +import { ImpressionSnapshotFieldProvider } from './impression-snapshot-provider'; +import type { ImpressionSettings } from './impressions/types'; + +const CONTEXT_NAME = 'metrics'; + +export type MetricsProvider = { + provider: MetricsFieldsProvider; + request: string; +}; + +export interface MetricSettings { + shouldEnableImpressions?: () => boolean; + shouldEnableFunnelKit: () => boolean; + getConsumerId: () => Promise<string>; + suppressMetricsKit?: boolean; + impressions?: ImpressionSettings; +} + +interface InitializedMetrics { + clickstream: ClickstreamProcessorInstance; + webDelegate: WebDelegatesInstance; +} + +interface Config { + baseFields: { + appName: string; + delegateApp: string; + appVersion: string; + resourceRevNum: string; + storageObject?: 'sessionStorage' | 'localStorage'; + }; + clickstream: MetricKitConfig; + + /** + * `FunnelKit` configuration + * + * Can be `undefined` to disable the `FunnelKit` recorder entirely + */ + funnel?: FunnelKitConfig; + + initialURL?: string | null; +} + +type ClickstreamProcessorClass = typeof ClickstreamProcessorInstance; +type WebDelegatesClass = typeof WebDelegatesInstance; + +export class Metrics { + private readonly log: Logger; + private impressions: InstanceType<typeof Impressions> | undefined; + + // Properties asynchronously set in the `init` function + private ClickstreamProcessor!: ClickstreamProcessorClass; + private WebDelegates!: WebDelegatesClass; + + private readonly metricsKitRecorder?: MetricsKitRecorder; + private readonly funnelKitRecorder?: FunnelKitRecorder; + private firstEnterRecorded: boolean = false; + private funnelKit: ClickstreamProcessorInstance | undefined; + private config: Config; + + public readonly metricsPipeline: MetricsPipeline; + public currentPageMetrics: Opt<PageMetricsPresenter>; + + static load( + loggerFactory: LoggerFactory, + context: Map<string, unknown>, + processEvent: (fields: MetricsFields) => Promise<LintedMetricsEvent>, + config: Config, + listofMetricProviders: MetricsProvider[], + settings: MetricSettings, + ): Metrics { + const { + getConsumerId, + shouldEnableFunnelKit, + suppressMetricsKit = false, + } = settings; + + const log = loggerFactory.loggerFor('Metrics'); + + // server + if (typeof window === 'undefined' || suppressMetricsKit) { + const recorder = new VoidEventRecorder(); + const metricsPipeline = new MetricsPipeline({ + aggregator: new MetricsFieldsAggregator(), + linter: { + async processEvent( + fields: MetricsFields, + ): Promise<LintedMetricsEvent> { + return { fields }; + }, + }, + recorder, + }); + + return new Metrics(log, metricsPipeline, config); + } + + config.initialURL = window.location.href; + + const aggregator = setupAggregators(listofMetricProviders, context); + + let impressions: InstanceType<typeof Impressions> | undefined = + undefined; + if (settings.shouldEnableImpressions?.() ?? false) { + impressions = new Impressions( + loggerFactory, + context, + settings?.impressions, + ); + } + + const metricsKitRecorder = new MetricsKitRecorder( + loggerFactory, + config.clickstream, + impressions, + ); + + const recorders: MetricsEventRecorder[] = [ + new LoggingEventRecorder(loggerFactory), + metricsKitRecorder, + ]; + + const funnelKitRecorder = config.funnel + ? new FunnelKitRecorder(loggerFactory, config.funnel, impressions) + : undefined; + if (funnelKitRecorder) { + recorders.push(funnelKitRecorder); + } + + let recorder = new CompositeEventRecorder(recorders); + + const metricsPipeline = new MetricsPipeline({ + aggregator, + linter: { + processEvent: async (fields: MetricsFields) => { + const lintedEvent = await processEvent(fields); + + // `dsId` is added by the LintMetricsEventIntentController in music-ui-js, but is not needed and erroneous for web + // https://github.pie.apple.com/music/music-ui-js/blob/50cbae83deccffad37e5b617394ea30b7e082660/src/metrics/LintMetricsEventIntentController.ts#L19-L22 + if (lintedEvent.fields?.dsId) { + delete lintedEvent.fields.dsId; + } + + // Consumer ID needs to be added at the time of processEvent because the ConsumerID is available after Sign In and not before sign In + // Using it through the delegates does not have ability to fetch it dynamically + const consumerId = await getConsumerId(); + if (consumerId) { + lintedEvent.fields.consumerId = consumerId; + } + + return lintedEvent; + }, + }, + recorder, + }); + + const metricsInstance = new Metrics( + log, + metricsPipeline, + config, + metricsKitRecorder, + funnelKitRecorder, + impressions, + ); + metricsInstance.watchEnterAndExit(); + + (async () => { + try { + const metricsDependencies = [ + import('@amp-metrics/mt-metricskit-processor-clickstream'), + import('@amp-metrics/mt-metricskit-delegates-web'), + impressions + ? import('@amp-metrics/mt-impressions-observer') + : undefined, + ] as const; + + const [ + { ClickstreamProcessor }, + { WebDelegates }, + impressionsDependency, + ] = await Promise.all(metricsDependencies); + + metricsInstance.onDependenciesLoaded( + ClickstreamProcessor, + WebDelegates, + ); + + const { clickstream, webDelegate } = setupMtkit( + ClickstreamProcessor, + WebDelegates, + config, + ); + + if (impressions && impressionsDependency) { + const { newInstanceWithMetricsConfig } = + impressionsDependency; + impressions.init(newInstanceWithMetricsConfig, clickstream); + } + + const eventRecorder = webDelegate.eventRecorder; + metricsKitRecorder.setupEventRecorder( + eventRecorder, + clickstream, + ); + + if (shouldEnableFunnelKit()) { + metricsInstance.enableFunnelKit(); + } + log.info('Metricskit loaded'); + } catch (e) { + log.warn('Metricskit failed to load', e); + } + })(); + + // Save Metrics Instance on Context before Returning + context.set(CONTEXT_NAME, metricsInstance); + + return metricsInstance; + } + + private constructor( + log: Logger, + metricsPipeline: MetricsPipeline, + config: Config, + metricsKitRecorder?: MetricsKitRecorder, + funnelKitRecorder?: FunnelKitRecorder, + impressions?: InstanceType<typeof Impressions>, + ) { + this.log = log; + this.metricsPipeline = metricsPipeline; + this.metricsKitRecorder = metricsKitRecorder; + this.funnelKitRecorder = funnelKitRecorder; + this.config = config; + this.impressions = impressions; + } + + /** + * Metrics code that should get called before a page changes. + */ + willPageTransition(): void { + this.impressions?.setCurrentSnapshot(); + } + + async didEnterPage< + T extends { pageMetrics: PageMetrics; canonicalURL: string }, + >(page: T | null): Promise<void> { + if (this.currentPageMetrics) { + await this.currentPageMetrics.didLeavePage(); + this.currentPageMetrics = null; + } + + if (page?.pageMetrics) { + this.currentPageMetrics = new PageMetricsPresenter( + this.metricsPipeline, + ); + this.currentPageMetrics.pageMetrics = page.pageMetrics; + await this.currentPageMetrics.didEnterPage(); + } else { + this.log.warn('No pageMetrics', page); + } + + if (!this.firstEnterRecorded) { + const event = document.referrer?.length > 0 ? 'link' : 'launch'; + this.enter(event, { openUrl: page?.canonicalURL }); + this.firstEnterRecorded = true; + } + } + + async enter(type: MetricsEnterEventType, fields?: Opt<MetricsFields>) { + let openUrl: string = window.location.href; + let pwaDisplayMode: PWADisplayMode | null = null; + + if (fields?.openUrl) { + openUrl = fields?.openUrl as string; + } + + if (type === 'launch' && this.config.initialURL) { + openUrl = this.config.initialURL; + // Clearing the initial URL as we don't need this post launch event + this.config.initialURL = null; + pwaDisplayMode = getPWADisplayMode(); + } + + this.recordCustomEvent({ + eventType: 'enter', + extRefUrl: document.referrer ?? '', + refUrl: document.referrer ?? '', + openUrl, + type, + // only add buildFlavor property if coming from the PWA (represented by 'standalone' in the manifest.json) or android app + ...(pwaDisplayMode === PWADisplayMode.STANDALONE || + pwaDisplayMode === PWADisplayMode.TWA + ? { buildFlavor: pwaDisplayMode } + : {}), + }); + } + + async exit(type: MetricsExitEventType, _fields?: Opt<MetricsFields>) { + this.recordCustomEvent({ + eventType: 'exit', + type, + }); + } + + async pageTransition() { + this.log.info('triggered metrics for page transition'); + if (this.impressions) { + this.impressions.setCurrentSnapshot(); + } + } + + private watchEnterAndExit() { + document.addEventListener( + 'visibilitychange', + this.onVisibilityChange.bind(this), + ); + } + + async onVisibilityChange() { + if (document.visibilityState === 'visible') { + this.enter('taskSwitch'); + } else { + this.exit('taskSwitch'); + } + } + + async processEvent(metricsFields: MetricsFields) { + const metricsData: MetricsData = { + excludingFields: [], + includingFields: [], + shouldFlush: false, + fields: metricsFields, + }; + const context: MetricsFieldsContext = {}; + await this.metricsPipeline.process(metricsData, context); + } + + async recordCustomEvent(fields?: Opt<MetricsFields>) { + await this.processEvent({ + ...this.currentPageMetrics?.pageMetrics?.pageFields, + ...fields, + }); + } + + /** + * Sets up FunnelKit for clickstream events + */ + private setupFunnelKit(): void { + if (!this.config.funnel) { + this.log.warn( + 'Tried to set up `FunnelKit` but no config was provided', + ); + return; + } + + const { topic } = this.config.funnel; + const { clickstream, webDelegate } = setupStarkit( + this.ClickstreamProcessor, + this.WebDelegates, + this.config.funnel, + this.config.baseFields, + ); + clickstream.config.setDebugSource(null); + + // Disable PII fields and cookies for the funnel topic + webDelegate.eventRecorder.setProperties?.(topic, { + anonymous: true, + }); + + this.funnelKitRecorder?.setupEventRecorder(clickstream); + this.funnelKit = clickstream; + } + + private onDependenciesLoaded( + ClickstreamProcessor: ClickstreamProcessorClass, + webDelegate: WebDelegatesClass, + ): void { + this.ClickstreamProcessor = ClickstreamProcessor; + this.WebDelegates = webDelegate; + } + + disableMetrics(): void { + this.metricsKitRecorder?.disable(); + } + + enableMetrics(): void { + this.metricsKitRecorder?.enable(); + } + + enableFunnelKit(): void { + if (!this.funnelKit) { + this.setupFunnelKit(); + } + this.funnelKitRecorder?.enableFunnelKit(); + } + + disableFunnelKit(): void { + this.funnelKitRecorder?.disableFunnelKit(); + } +} + +/** + * Shared setup for *kit, namely MetricsKit and FunnelKit + */ +function setupStarkit( + ClickstreamProcessor: ClickstreamProcessorClass, + WebDelegates: WebDelegatesClass, + setupConfig: FunnelKitConfig | MetricKitConfig, + config: Config['baseFields'], +): InitializedMetrics { + const { topic } = setupConfig; + const webDelegate = new WebDelegates(topic); + + if (import.meta.env.APP_SCOPE === 'internal') { + try { + // Temporary setup to get Network Dependency + const networkCopy = { + ...Object.getPrototypeOf(webDelegate.config.network), + }; + + const makeAjaxRequest = buildMakeAjaxRequest(networkCopy, topic); + + webDelegate.setNetwork({ + makeAjaxRequest, + }); + } catch (e) { + console.warn('failed to setup flush logger'); + } + } + + const clickstream = new ClickstreamProcessor(webDelegate); + + const systemLoggerLevel: SystemLoggerLevel = 'none'; + clickstream.system.logger.setLevel(systemLoggerLevel); + clickstream.init(); + + setupMtkitDelegates(clickstream, setupConfig, config); + return { clickstream, webDelegate }; +} + +/** + * MetricsKit setup for main clickstream events + */ +function setupMtkit( + ClickstreamProcessor: ClickstreamProcessorClass, + webDelegates: WebDelegatesClass, + config: Config, +): InitializedMetrics { + const mtkit = setupStarkit( + ClickstreamProcessor, + webDelegates, + config.clickstream, + config.baseFields, + ); + return mtkit; +} + +function setupMtkitDelegates( + mtkit: ClickstreamProcessorInstance, + setupConfig: FunnelKitConfig | MetricKitConfig, + config: Config['baseFields'], +): void { + const { appName, delegateApp, appVersion, resourceRevNum, storageObject } = + config; + const additionalDelegates: EnvironmentDelegates = { + app: () => appName, + appVersion: () => appVersion, + delegateApp: () => delegateApp, + resourceRevNum: () => resourceRevNum, + }; + + if (storageObject === 'sessionStorage') { + additionalDelegates['localStorageObject'] = () => { + return sessionStorage; + }; + } + + mtkit.system.environment.setDelegate(additionalDelegates); + + if (Array.isArray(setupConfig.constraintProfiles)) { + mtkit.config.setDelegate({ + constraintProfiles: () => setupConfig.constraintProfiles, + }); + } +} + +function setupAggregators( + metricsFieldsProviders: MetricsProvider[], + context: Map<string, unknown>, +): MetricsFieldsAggregator { + const aggregator = MetricsFieldsAggregator.makeDefaultAggregator(); + + aggregator.addOptInProvider( + new ImpressionFieldProvider(context), + 'impressions', + ); + + aggregator.addOptInProvider( + new ImpressionSnapshotFieldProvider(context), + 'impressionsSnapshot', + ); + + metricsFieldsProviders.forEach((metricsFields) => { + aggregator.addOptOutProvider( + metricsFields.provider, + metricsFields.request, + ); + }); + + return aggregator; +} + +/** + * Gets the current Metrics instance from the Svelte context. + * + * @return metrics The current instance of Metrics + */ + +export function generateMetricsContextGetter( + getContext: (context: string) => unknown, +): () => Metrics { + return function getMetrics(): Metrics { + const metrics = getContext(CONTEXT_NAME) as Metrics | undefined; + + if (!metrics) { + throw new Error('getMetrics called before Metrics.load'); + } + + return metrics; + }; +} + +export * from './impressions/index'; +export * from './impressions/utils/svelte/impressions-svelte-action'; diff --git a/shared/metrics-8/src/recorder/composite.ts b/shared/metrics-8/src/recorder/composite.ts new file mode 100644 index 0000000..6302921 --- /dev/null +++ b/shared/metrics-8/src/recorder/composite.ts @@ -0,0 +1,20 @@ +import type { MetricsEventRecorder } from '@jet/engine'; +import type { LintedMetricsEvent } from '@jet/environment/types/metrics'; +import type { Opt } from '@jet/environment/types/optional'; + +export class CompositeEventRecorder implements MetricsEventRecorder { + constructor(private readonly eventRecorders: MetricsEventRecorder[]) {} + + record(event: LintedMetricsEvent, topic: Opt<string>): void { + for (const eventRecorder of this.eventRecorders) { + eventRecorder.record(event, topic); + } + } + + async flush(): Promise<number> { + const flushed: number[] = await Promise.all( + this.eventRecorders.map((recorder) => recorder.flush()), + ); + return Math.max(...flushed); + } +} diff --git a/shared/metrics-8/src/recorder/funnelkit.ts b/shared/metrics-8/src/recorder/funnelkit.ts new file mode 100644 index 0000000..7f3fa84 --- /dev/null +++ b/shared/metrics-8/src/recorder/funnelkit.ts @@ -0,0 +1,237 @@ +import type { MetricsEventRecorder } from '@jet/engine'; +import type { LintedMetricsEvent } from '@jet/environment/types/metrics'; +import type { Opt } from '@jet/environment/types/optional'; +import type { Logger, LoggerFactory } from '@amp/web-apps-logger'; +import type { ClickstreamProcessor as ClickstreamProcessorInstance } from '@amp-metrics/mt-metricskit-processor-clickstream'; +import type { Impressions } from '../impressions'; +import { sendToMetricsDevConsole } from '../utils/metrics-dev-console/setup-metrics-dev'; +import { getEventFieldsWithTopic } from '../utils/get-event-field-topic'; +import { eventType } from '../utils/metrics-dev-console/constants'; + +interface DeferredEvent { + event: LintedMetricsEvent; + topic: Opt<string>; +} + +export interface FunnelKitConfig { + constraintProfiles: string[]; + topic: string; +} + +/** + * These fields are considered PII and should be ignored by FunnelKit. + * `consumerId` is added via the `processEvent` based on when it is available (see jet/metrics/index.ts) + * However it should be ignored when sent to the FunnelKit topic. + */ +const IGNORED_FIELDS = ['consumerId']; + +export class FunnelKitRecorder implements MetricsEventRecorder { + private readonly log: Logger; + private funnelKit: ClickstreamProcessorInstance | undefined; + private funnelKitEnabled: boolean = false; + private recordedEventsCount: number; + private config: FunnelKitConfig; + private readonly impressions: InstanceType<typeof Impressions> | undefined; + + /** + * Queues events prior to the mt-event-queue recorder being available + */ + private readonly deferredEvents: DeferredEvent[]; + + constructor( + loggerFactory: LoggerFactory, + config: FunnelKitConfig, + impressions: InstanceType<typeof Impressions> | undefined, + ) { + this.log = loggerFactory.loggerFor('FunnelKitRecorder'); + this.deferredEvents = []; + this.recordedEventsCount = 0; + this.config = config; + this.impressions = impressions; + } + + async record( + event: LintedMetricsEvent, + eventTopic: Opt<string>, + ): Promise<void> { + let topic = eventTopic ?? this.config.topic; + + // TV always uses the config topic + // TODO: rdar://151772731 (Align funnel metrics between Music + TV) + if (this.config.topic === 'xp_amp_tv_unidentified') { + topic = this.config.topic; + } + + if (!this.funnelKitEnabled) { + this.log.info('FunnelKit not enabled', event, topic); + return; + } + + if (this.funnelKit) { + const eventHandler = event.fields.eventType as string; + const { pageId, pageType, pageContext } = event.fields; + if (!eventHandler) { + this.log.warn('No `eventType` found on event', event, topic); + } else if (!this.impressions && eventHandler === 'impressions') { + this.log.info( + 'Supressing impression event. Impressions not enabled', + ); + return; + } + + // when the user leaves a page to report the accumulated impressions for that page + if ( + (this.impressions?.isEnabled('exit') && + eventHandler === 'exit') || + (this.impressions?.isEnabled('click') && + event.fields.actionType === 'navigate') + ) { + // create + capture impressions + const accumulatedImpressions = + this.impressions.consumeImpressions(); + const metricsData = this.funnelKit?.eventHandlers[ + 'impressions' + ]?.metricsData(pageId, pageType, pageContext, { + impressions: accumulatedImpressions, + }); + + metricsData + ?.recordEvent(topic) + .then((data) => { + this.log.info( + 'impressions event captured', + data, + topic, + ); + sendToMetricsDevConsole( + data as { [key: string]: unknown }, + topic, + ); + }) + .catch((e) => { + this.log.warn( + 'failed to capture impression metrics', + e, + topic, + ); + }); + } + + let impressionsData: Record<string, unknown> = {}; + // snapshot impressions to include in click events + if ( + (this.impressions?.isEnabled('click') && + eventHandler === 'click') || + (this.impressions?.isEnabled('impressions') && + eventHandler === 'impressions') + ) { + const snapshotImpressions = + this.impressions.captureSnapshotImpression(); + impressionsData = snapshotImpressions + ? { + impressions: snapshotImpressions, + } + : {}; + } + + const eventFields = getEventFieldsWithTopic(event, topic); + // Handle transaction events differently per Ember implementation + // https://github.pie.apple.com/amp-ui/ember-metrics/blob/7eb762601db5e37cb428d7a4e6f24e22d0529515/addon/services/metrics.js#L347-L349 + const metricsDataArgs = + eventHandler === 'transaction' + ? [eventFields] + : [pageId, pageType, pageContext, eventFields]; + + try { + const baseFields = await this.funnelKit.eventHandlers[ + eventHandler + ] + ?.metricsData( + // @ts-expect-error TypeScript doesn't handle spreading the argument array well + ...metricsDataArgs, + ) + .toJSON(); + + const metricsData = { + ...baseFields, + ...eventFields, + ...impressionsData, + }; + IGNORED_FIELDS.forEach( + (ignoredField) => delete metricsData[ignoredField], + ); + this.log.info('FunnelKit event data', metricsData, topic); + + try { + const data = + await this.funnelKit.system.eventRecorder.recordEvent( + topic, + metricsData, + ); + sendToMetricsDevConsole(data, topic); + } catch (e) { + this.log.info( + 'FunnelKit failed to capture', + metricsData, + topic, + ); + } + + // on exit events we should flush all metrics + if (eventHandler === 'exit') { + this.funnelKit?.system.eventRecorder.flushUnreportedEvents?.( + true, + ); + + sendToMetricsDevConsole( + { metricsDevType: eventType.FLUSH, status: 'SUCCESS' }, + topic, + ); + } + + this.recordedEventsCount++; + } catch (e) { + this.log.error('FunnelKit failed to capture metric', e, topic); + } + } else { + this.deferredEvents.push({ event, topic }); + } + } + + async flush(): Promise<number> { + if (!this.funnelKitEnabled) { + return 0; + } + + await this.funnelKit?.system.eventRecorder.flushUnreportedEvents(false); + const count = this.recordedEventsCount; + this.recordedEventsCount = 0; + return count; + } + + setupEventRecorder(funnelKit: ClickstreamProcessorInstance): void { + this.funnelKit = funnelKit; + this.deferredEvents.forEach(({ event, topic }) => + this.record(event, topic), + ); + this.deferredEvents.length = 0; + } + + enableFunnelKit(): void { + if (this.funnelKitEnabled) { + return; + } + + this.log.info('Enabling FunnelKit'); + this.funnelKitEnabled = true; + } + + disableFunnelKit(): void { + if (!this.funnelKitEnabled) { + return; + } + + this.log.info('Disabling FunnelKit'); + this.funnelKitEnabled = false; + } +} diff --git a/shared/metrics-8/src/recorder/logging.ts b/shared/metrics-8/src/recorder/logging.ts new file mode 100644 index 0000000..baa0373 --- /dev/null +++ b/shared/metrics-8/src/recorder/logging.ts @@ -0,0 +1,21 @@ +import type { MetricsEventRecorder } from '@jet/engine'; +import type { LintedMetricsEvent } from '@jet/environment/types/metrics'; +import type { Opt } from '@jet/environment/types/optional'; +import type { Logger, LoggerFactory } from '@amp/web-apps-logger'; + +export class LoggingEventRecorder implements MetricsEventRecorder { + private readonly log: Logger; + + constructor(loggerFactory: LoggerFactory) { + this.log = loggerFactory.loggerFor('LoggingEventRecorder'); + } + + record(event: LintedMetricsEvent, topic: Opt<string>): void { + this.log.info('logged metrics event:', event, topic); + } + + async flush(): Promise<number> { + this.log.info('flushed metrics'); + return 0; + } +} diff --git a/shared/metrics-8/src/recorder/metricskit.ts b/shared/metrics-8/src/recorder/metricskit.ts new file mode 100644 index 0000000..9d724c9 --- /dev/null +++ b/shared/metrics-8/src/recorder/metricskit.ts @@ -0,0 +1,239 @@ +import type { MetricsEventRecorder } from '@jet/engine'; +import type { LintedMetricsEvent } from '@jet/environment/types/metrics'; +import type { Opt } from '@jet/environment/types/optional'; +import type { Logger, LoggerFactory } from '@amp/web-apps-logger'; + +import { METRICS_EVENT_TYPES } from '../constants'; + +import type { WebDelegates as WebDelegatesInstance } from '@amp-metrics/mt-metricskit-delegates-web'; +import type { ClickstreamProcessor as ClickstreamProcessorInstance } from '@amp-metrics/mt-metricskit-processor-clickstream'; +import type { Impressions } from '../impressions'; +import { sendToMetricsDevConsole } from '../utils/metrics-dev-console/setup-metrics-dev'; +import { getEventFieldsWithTopic } from '../utils/get-event-field-topic'; +import { eventType } from '../utils/metrics-dev-console/constants'; + +interface DeferredEvent { + event: LintedMetricsEvent; + topic: Opt<string>; +} + +type EventRecorder = WebDelegatesInstance['eventRecorder']; + +type MetricEventType = (typeof METRICS_EVENT_TYPES)[number]; + +export interface MetricKitConfig { + constraintProfiles: string[]; + topic: string; +} + +export class MetricsKitRecorder implements MetricsEventRecorder { + private readonly log: Logger; + private eventRecorder: EventRecorder | undefined; + private mtkit: ClickstreamProcessorInstance | undefined; + private recordedEventsCount: number; + private config: MetricKitConfig; + private readonly impressions: InstanceType<typeof Impressions> | undefined; + private enabled: boolean = true; + + /** + * Queues events prior to the mt-event-queue recorder being available + */ + private readonly deferredEvents: DeferredEvent[]; + + constructor( + loggerFactory: LoggerFactory, + config: MetricKitConfig, + impressions: InstanceType<typeof Impressions> | undefined, + ) { + this.log = loggerFactory.loggerFor('MetricsKitRecorder'); + this.deferredEvents = []; + this.recordedEventsCount = 0; + this.config = config; + this.impressions = impressions; + } + + record(event: LintedMetricsEvent, topic: Opt<string>): void { + topic = topic ?? this.config.topic; + if (this.isDisabled()) { + this.log.info( + `topic ${this.config.topic} is disabled following event not captured:`, + event, + ); + return; + } + + if (this.eventRecorder) { + const eventHandler = event.fields.eventType as MetricEventType; + const { pageId, pageType, pageContext } = event.fields; + if (!eventHandler) { + this.log.warn('No `eventType` found on event', event, topic); + return; + } else if (!METRICS_EVENT_TYPES.includes(eventHandler)) { + this.log.warn( + 'Invalid `eventType` found on event', + event, + topic, + ); + return; + } else if (!this.impressions && eventHandler === 'impressions') { + this.log.info( + 'Supressing impression event. Impressions not enabled', + ); + return; + } + + // when the user leaves a page to report the accumulated impressions for that page + if ( + (this.impressions?.isEnabled('exit') && + eventHandler === 'exit') || + (this.impressions?.isEnabled('click') && + event.fields.actionType === 'navigate') + ) { + // create + capture impressions + const accumulatedImpressions = + this.impressions.consumeImpressions(); + + const metricsData = this.mtkit?.eventHandlers[ + 'impressions' + ]?.metricsData(pageId, pageType, pageContext, { + impressions: accumulatedImpressions, + }); + + metricsData + ?.recordEvent(topic) + .then((data) => { + this.log.info( + 'impressions event captured', + data, + topic, + ); + sendToMetricsDevConsole( + data as { [key: string]: unknown }, + topic ?? '', + ); + }) + .catch((e) => { + this.log.warn( + 'failed to capture impression metrics', + e, + topic, + ); + }); + } + + let impressionsData = {}; + // snapshot impressions to include in click events + if ( + (this.impressions?.isEnabled('click') && + eventHandler === 'click') || + (this.impressions?.isEnabled('impressions') && + eventHandler === 'impressions') + ) { + const snapshotImpressions = + this.impressions.captureSnapshotImpression(); + impressionsData = { + impressions: snapshotImpressions, + }; + } + + const eventFields = getEventFieldsWithTopic(event, topic); + // click events are the only ones with different method signature + // https://github.pie.apple.com/amp-metrics/mt-metricskit/blob/7.3.5/src/metrics/event_handlers/click.js#L133 + const metricsDataArgs = + eventHandler === 'click' // TODO rdar://102438307 (JMOTW Clickstream – Pass targetElement to click events) + ? [ + pageId, + pageType, + pageContext, + null, + { ...eventFields, ...impressionsData }, + ] + : [pageId, pageType, pageContext, eventFields]; + + if (eventHandler === 'impressions') { + metricsDataArgs.push(impressionsData); + } + + let metricsData = this.mtkit?.eventHandlers[ + eventHandler + ]?.metricsData( + // @ts-expect-error TypeScript doesn't handle spreading the argument array well + ...metricsDataArgs, + ); + + metricsData + ?.recordEvent(topic) + .then((data) => { + this.log.info('MetricsKit event data', data, topic); + sendToMetricsDevConsole( + data as { [key: string]: unknown }, + topic ?? '', + ); + }) + .catch((e) => { + this.log.error( + 'MetricsKit failed to capture metric', + e, + topic, + ); + }); + + this.recordedEventsCount++; + + // on exit events we should flush all metrics + if (eventHandler === 'exit') { + this.eventRecorder?.flushUnreportedEvents?.(true); + sendToMetricsDevConsole( + { metricsDevType: eventType.FLUSH, status: 'SUCCESS' }, + topic, + ); + } + } else { + this.deferredEvents.push({ event, topic }); + } + } + + async flush(): Promise<number> { + await this.eventRecorder?.flushUnreportedEvents?.(false); + const count = this.recordedEventsCount; + this.recordedEventsCount = 0; + return count; + } + + setupEventRecorder( + eventRecorder: EventRecorder, + mtkit: ClickstreamProcessorInstance, + ): void { + this.eventRecorder = eventRecorder; + this.mtkit = mtkit; + this.deferredEvents.forEach(({ event, topic }) => + this.record(event, topic), + ); + this.deferredEvents.length = 0; + } + + isDisabled(): boolean { + return !this.enabled; + } + + enable(): void { + if (this.enabled) { + this.log.info( + `Clickstream topic ${this.config.topic} already enabled`, + ); + return; + } + + this.log.info(`Enabling clickstream topic ${this.config.topic}`); + this.enabled = true; + } + + disable(): void { + if (this.isDisabled()) { + return; + } + + this.log.info(`Disabling clickstream topic ${this.config.topic}`); + this.enabled = false; + } +} diff --git a/shared/metrics-8/src/recorder/void.ts b/shared/metrics-8/src/recorder/void.ts new file mode 100644 index 0000000..475c759 --- /dev/null +++ b/shared/metrics-8/src/recorder/void.ts @@ -0,0 +1,17 @@ +import type { MetricsEventRecorder } from '@jet/engine'; +import type { LintedMetricsEvent } from '@jet/environment/types/metrics'; +import type { Opt } from '@jet/environment/types/optional'; + +export class VoidEventRecorder implements MetricsEventRecorder { + private recorded: number = 0; + + record(_event: LintedMetricsEvent, _topic: Opt<string>): void { + this.recorded++; + } + + async flush(): Promise<number> { + const { recorded } = this; + this.recorded = 0; + return recorded; + } +} diff --git a/shared/metrics-8/src/utils/get-event-field-topic.ts b/shared/metrics-8/src/utils/get-event-field-topic.ts new file mode 100644 index 0000000..96bb125 --- /dev/null +++ b/shared/metrics-8/src/utils/get-event-field-topic.ts @@ -0,0 +1,11 @@ +import type { LintedMetricsEvent } from '@jet/environment'; +import type { MetricsFields } from '~/types'; + +export function getEventFieldsWithTopic( + event: LintedMetricsEvent, + topic: string, +) { + return 'topic' in event.fields + ? event.fields + : ({ ...event.fields, topic } as MetricsFields); +} diff --git a/shared/metrics-8/src/utils/metrics-dev-console/constants.ts b/shared/metrics-8/src/utils/metrics-dev-console/constants.ts new file mode 100644 index 0000000..7193da8 --- /dev/null +++ b/shared/metrics-8/src/utils/metrics-dev-console/constants.ts @@ -0,0 +1,7 @@ +/** + * Event type constants for metrics development console + */ +export const eventType = { + RECORD: 'record', + FLUSH: 'flush', +} as const; diff --git a/shared/metrics-8/src/utils/metrics-dev-console/setup-metrics-dev.ts b/shared/metrics-8/src/utils/metrics-dev-console/setup-metrics-dev.ts new file mode 100644 index 0000000..fb7def6 --- /dev/null +++ b/shared/metrics-8/src/utils/metrics-dev-console/setup-metrics-dev.ts @@ -0,0 +1,55 @@ +import { isFlushEvent, makeFlushEvent } from './events/flush-event'; +import { makeRecordEvent } from './events/record-event'; +import type { MetricsOptions, FlushEvent, MetricsObject } from './type'; + +/** + * Updates the metrics console by dispatching appropriate events + */ +const updateMetricsConsole = ( + topic: string, + metricsData: MetricsOptions | FlushEvent, +): void => { + let event = null; + const { metricsDevType, ...data } = metricsData ?? ({} as MetricsObject); + + if (isFlushEvent(metricsData)) { + event = makeFlushEvent(metricsData, topic); + } else if (metricsData) { + event = makeRecordEvent(data, topic); + } + + if (event) { + try { + window.dispatchEvent(event); + } catch (e) { + console.error('metric console failed', e); + } + } +}; + +const isMetricsDevConsoleEnabled = () => { + return ( + typeof window !== 'undefined' && + window.localStorage?.getItem('metrics-dev') === 'true' + ); +}; + +/** + * Sends metrics data to the development console if enabled + * @param metricsData - The metrics data to send + * @param topic - The topic/category for the metrics + */ +export const sendToMetricsDevConsole = ( + metricsData: MetricsOptions, + topic: string, +): void => { + if (import.meta.env.APP_SCOPE === 'internal') { + if (isMetricsDevConsoleEnabled()) { + try { + updateMetricsConsole(topic, metricsData); + } catch (error) { + console.warn('Failed to send metrics to dev console:', error); + } + } + } +}; diff --git a/shared/storefronts/src/index.js b/shared/storefronts/src/index.js new file mode 100644 index 0000000..74f4843 --- /dev/null +++ b/shared/storefronts/src/index.js @@ -0,0 +1,19 @@ +// helper functions available for use at runtime +/** + * @param {Region[]} regions - array of region objects that include region name and locales + * @returns {StorefrontNameTranslations} - storefront ID (ie: us) mapped to that storefront name translated in all supported languages + */ +export function getFormattedStorefrontNameTranslations(regions) { + return Object.fromEntries( + regions.flatMap(({ locales }) => { + const storefronts = {}; + for (const locale of locales) { + if (!(locale.id in storefronts)) { + storefronts[locale.id] = { default: locale.name }; + } + storefronts[locale.id][locale.language] = locale.name; + } + return Object.entries(storefronts); + }), + ); +} diff --git a/shared/utils/node_modules/@amp/runtime-detect/dist/extensions/compare.js b/shared/utils/node_modules/@amp/runtime-detect/dist/extensions/compare.js new file mode 100644 index 0000000..200cdfb --- /dev/null +++ b/shared/utils/node_modules/@amp/runtime-detect/dist/extensions/compare.js @@ -0,0 +1,83 @@ +import { eq, gt, gte, lt, lte } from '../version.js'; + +function _define_property(obj, key, value) { + if (key in obj) { + Object.defineProperty(obj, key, { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } else { + obj[key] = value; + } + return obj; +} +function _object_spread(target) { + for(var i = 1; i < arguments.length; i++){ + var source = arguments[i] != null ? arguments[i] : {}; + var ownKeys = Object.keys(source); + if (typeof Object.getOwnPropertySymbols === "function") { + ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { + return Object.getOwnPropertyDescriptor(source, sym).enumerable; + })); + } + ownKeys.forEach(function(key) { + _define_property(target, key, source[key]); + }); + } + return target; +} +function ownKeys(object, enumerableOnly) { + var keys = Object.keys(object); + if (Object.getOwnPropertySymbols) { + var symbols = Object.getOwnPropertySymbols(object); + if (enumerableOnly) { + symbols = symbols.filter(function(sym) { + return Object.getOwnPropertyDescriptor(object, sym).enumerable; + }); + } + keys.push.apply(keys, symbols); + } + return keys; +} +function _object_spread_props(target, source) { + source = source != null ? source : {}; + if (Object.getOwnPropertyDescriptors) { + Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + } else { + ownKeys(Object(source)).forEach(function(key) { + Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); + }); + } + return target; +} +function compareExtension(descriptor) { + function makeComparable(data) { + var _data_major, _data_minor, _data_patch; + const version = { + major: (_data_major = data.major) !== null && _data_major !== void 0 ? _data_major : 0, + minor: (_data_minor = data.minor) !== null && _data_minor !== void 0 ? _data_minor : 0, + patch: (_data_patch = data.patch) !== null && _data_patch !== void 0 ? _data_patch : 0 + }; + return _object_spread_props(_object_spread({}, data), { + eq: (value)=>eq(version, value), + gt: (value)=>gt(version, value), + gte: (value)=>gte(version, value), + lt: (value)=>lt(version, value), + lte: (value)=>lte(version, value), + is: (value)=>data.name === value || data.variant === value + }); + } + return _object_spread_props(_object_spread({}, descriptor), { + extensions: [ + ...descriptor.extensions, + 'compare' + ], + browser: makeComparable(descriptor.browser), + engine: makeComparable(descriptor.engine), + os: makeComparable(descriptor.os) + }); +} + +export { compareExtension }; diff --git a/shared/utils/node_modules/@amp/runtime-detect/dist/extensions/flags.js b/shared/utils/node_modules/@amp/runtime-detect/dist/extensions/flags.js new file mode 100644 index 0000000..04980ee --- /dev/null +++ b/shared/utils/node_modules/@amp/runtime-detect/dist/extensions/flags.js @@ -0,0 +1,105 @@ +function _define_property(obj, key, value) { + if (key in obj) { + Object.defineProperty(obj, key, { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } else { + obj[key] = value; + } + return obj; +} +function _object_spread(target) { + for(var i = 1; i < arguments.length; i++){ + var source = arguments[i] != null ? arguments[i] : {}; + var ownKeys = Object.keys(source); + if (typeof Object.getOwnPropertySymbols === "function") { + ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { + return Object.getOwnPropertyDescriptor(source, sym).enumerable; + })); + } + ownKeys.forEach(function(key) { + _define_property(target, key, source[key]); + }); + } + return target; +} +function ownKeys(object, enumerableOnly) { + var keys = Object.keys(object); + if (Object.getOwnPropertySymbols) { + var symbols = Object.getOwnPropertySymbols(object); + if (enumerableOnly) { + symbols = symbols.filter(function(sym) { + return Object.getOwnPropertyDescriptor(object, sym).enumerable; + }); + } + keys.push.apply(keys, symbols); + } + return keys; +} +function _object_spread_props(target, source) { + source = source != null ? source : {}; + if (Object.getOwnPropertyDescriptors) { + Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + } else { + ownKeys(Object(source)).forEach(function(key) { + Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); + }); + } + return target; +} +function flagsExtension(descriptor) { + let osName = descriptor.os.name; + const isAndroid = descriptor.os.name === 'android'; + let isMacOS = descriptor.os.name === 'macos'; + let isIOS = descriptor.os.name === 'ios'; + var _descriptor_navigator_maxTouchPoints; + /** + * Newer iPads will identify as macOS in the UserAgent string but can still be differentiated by + * inspecting `maxTouchPoints`. The macOS and iOS values need to be reset when detected. + */ const isIPadOS = osName === 'ipados' || isIOS && /ipad/i.test(descriptor.ua) || isMacOS && ((_descriptor_navigator_maxTouchPoints = descriptor.navigator.maxTouchPoints) !== null && _descriptor_navigator_maxTouchPoints !== void 0 ? _descriptor_navigator_maxTouchPoints : 0) >= 2; + if (isIPadOS) { + osName = 'ipados'; + isIOS = false; + isMacOS = false; + } + const browser = _object_spread_props(_object_spread({}, descriptor.browser), { + isUnknown: descriptor.browser.name === 'unknown', + isSafari: descriptor.browser.name === 'safari', + isChrome: descriptor.browser.name === 'chrome', + isFirefox: descriptor.browser.name === 'firefox', + isEdge: descriptor.browser.name === 'edge', + isWebView: descriptor.browser.name === 'webview', + isOther: descriptor.browser.name === 'other', + isMobile: descriptor.browser.mobile || isIOS || isIPadOS || isAndroid || false + }); + const engine = _object_spread_props(_object_spread({}, descriptor.engine), { + isUnknown: descriptor.engine.name === 'unknown', + isWebKit: descriptor.engine.name === 'webkit', + isBlink: descriptor.engine.name === 'blink', + isGecko: descriptor.engine.name === 'gecko' + }); + const os = _object_spread_props(_object_spread({}, descriptor.os), { + name: osName, + isUnknown: descriptor.os.name === 'unknown', + isLinux: descriptor.os.name === 'linux', + isWindows: descriptor.os.name === 'windows', + isMacOS, + isAndroid, + isIOS, + isIPadOS + }); + return _object_spread_props(_object_spread({}, descriptor), { + extensions: [ + ...descriptor.extensions, + 'flags' + ], + browser, + os, + engine + }); +} + +export { flagsExtension }; diff --git a/shared/utils/node_modules/@amp/runtime-detect/dist/rules.js b/shared/utils/node_modules/@amp/runtime-detect/dist/rules.js new file mode 100644 index 0000000..5ffb91f --- /dev/null +++ b/shared/utils/node_modules/@amp/runtime-detect/dist/rules.js @@ -0,0 +1,22 @@ +function applyRules(rules, navigator, data) { + const { userAgent } = navigator !== null && navigator !== void 0 ? navigator : {}; + if (typeof userAgent !== 'string' || userAgent.trim() === '') { + return data; + } + for (const rule of rules){ + const patterns = rule.slice(0, -1); + const parser = rule[rule.length - 1]; + let match = null; + for (const pattern of patterns){ + match = userAgent.match(pattern); + if (match !== null) { + Object.assign(data, parser(match, navigator, data)); + break; + } + } + if (match !== null) break; + } + return data; +} + +export { applyRules }; diff --git a/shared/utils/node_modules/@amp/runtime-detect/dist/user-agent.js b/shared/utils/node_modules/@amp/runtime-detect/dist/user-agent.js new file mode 100644 index 0000000..f936336 --- /dev/null +++ b/shared/utils/node_modules/@amp/runtime-detect/dist/user-agent.js @@ -0,0 +1,392 @@ +import { parseVersion } from './version.js'; +import { applyRules } from './rules.js'; + +function _define_property(obj, key, value) { + if (key in obj) { + Object.defineProperty(obj, key, { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } else { + obj[key] = value; + } + return obj; +} +function _object_spread(target) { + for(var i = 1; i < arguments.length; i++){ + var source = arguments[i] != null ? arguments[i] : {}; + var ownKeys = Object.keys(source); + if (typeof Object.getOwnPropertySymbols === "function") { + ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { + return Object.getOwnPropertyDescriptor(source, sym).enumerable; + })); + } + ownKeys.forEach(function(key) { + _define_property(target, key, source[key]); + }); + } + return target; +} +function ownKeys(object, enumerableOnly) { + var keys = Object.keys(object); + if (Object.getOwnPropertySymbols) { + var symbols = Object.getOwnPropertySymbols(object); + if (enumerableOnly) { + symbols = symbols.filter(function(sym) { + return Object.getOwnPropertyDescriptor(object, sym).enumerable; + }); + } + keys.push.apply(keys, symbols); + } + return keys; +} +function _object_spread_props(target, source) { + source = source != null ? source : {}; + if (Object.getOwnPropertyDescriptors) { + Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + } else { + ownKeys(Object(source)).forEach(function(key) { + Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); + }); + } + return target; +} +var _match_, _match_1; +const RULES = { + // BROWSER ========================================================================== + browser: [ + // WEBVIEW ------------------------------------------------------------------------ + // iTunes/Music.app/TV.app + [ + /^(itunes|music|tv)\/([\w.]+)\s/i, + (match)=>_object_spread_props(_object_spread({ + name: 'webview', + variant: match[1].trim().toLowerCase().replace(/(music|tv)/i, '$1.app') + }, parseVersion(match[2])), { + mobile: false + }) + ], + // Facebook + [ + /(?:(?:fban\/fbios|fb_iab\/fb4a)(?!.+fbav)|;fbav\/([\w.]+);)/i, + (match)=>_object_spread({ + name: 'webview', + variant: 'facebook', + mobile: true + }, parseVersion(match[1])) + ], + // Instagram / SnapChat + [ + /(instagram|snapchat)[/ ]([-\w.]+)/i, + (match)=>_object_spread({ + name: 'webview', + variant: match[1].trim().toLowerCase(), + mobile: true + }, parseVersion(match[2])) + ], + // TikTok + [ + /musical_ly(?:.+app_?version\/|_)([\w.]+)/i, + (match)=>_object_spread({ + name: 'webview', + variant: 'tiktok', + mobile: true + }, parseVersion(match[1])) + ], + // Twitter + [ + /twitter/i, + ()=>({ + name: 'webview', + variant: 'twitter', + mobile: true + }) + ], + // Chrome WebView + [ + / wv\).+?(?:version|chrome)\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'webview', + mobile: true + }, parseVersion(match[1])) + ], + // ELECTRON ----------------------------------------------------------------------- + [ + /electron\/([\w.]+) safari/i, + (match)=>_object_spread({ + name: 'electron', + mobile: false + }, parseVersion(match[1])) + ], + // OTHER -------------------------------------------------------------------------- + [ + /tesla\/(.*?(20\d\d\.([-\w.])+))/i, + (match)=>_object_spread_props(_object_spread({ + name: 'other', + variant: 'tesla', + mobile: false + }, parseVersion(match[2])), { + version: match[1] + }) + ], + [ + /(samsung|huawei)browser\/([-\w.]+)/i, + (match)=>_object_spread({ + name: 'other', + variant: match[1].trim().toLowerCase().replace(/browser/i, ''), + mobile: true + }, parseVersion(match[2])) + ], + [ + /yabrowser\/([-\w.]+)/i, + (match)=>_object_spread({ + name: 'other', + variant: 'yandex', + mobile: false + }, parseVersion(match[1])) + ], + [ + /(brave|flock|rockmelt|midori|epiphany|silk|skyfire|ovibrowser|bolt|iron|vivaldi|iridium|phantomjs|bowser|quark|qupzilla|falkon|rekonq|puffin|whale(?!.+naver)|qqbrowserlite|qq|duckduckgo)\/([-\w.]+)/i, + (match, { userAgent })=>_object_spread({ + name: 'other', + variant: match[1].trim().toLowerCase(), + mobile: /mobile/i.test(userAgent) + }, parseVersion(match[2].replace(/-/g, '.'))) + ], + // EDGE / IE ---------------------------------------------------------------------- + [ + /edg(e|ios|a)?\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'edge', + mobile: /(edgios|edga)/i.test((_match_ = match[1]) !== null && _match_ !== void 0 ? _match_ : '') + }, parseVersion(match[2])) + ], + [ + /trident.+rv[: ]([\w.]{1,9})\b.+like gecko/i, + (match)=>_object_spread({ + name: 'ie', + mobile: false + }, parseVersion(match[1])) + ], + // OPERA -------------------------------------------------------------------------- + [ + /opr\/([\w.]+)/i, + /opera mini\/([-\w.]+)/i, + /opera [mobiletab]{3,6}\b.+version\/([-\w.]+)/i, + /opera(?:.+version\/|[/ ]+)([\w.]+)/i, + (match)=>_object_spread({ + name: 'opera', + mobile: /mobile/i.test(match[0]) + }, parseVersion(match[1])) + ], + // CHROME ------------------------------------------------------------------------- + // Headless + [ + /headlesschrome(?:\/([\w.]+)| )/i, + (match)=>_object_spread({ + name: 'chrome', + variant: 'headless', + mobile: false + }, parseVersion(match[1])) + ], + // Chrome for iOS + [ + /\b(?:crmo|crios)\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'chrome', + mobile: true + }, parseVersion(match[1])) + ], + // Chrome + [ + /chrome(?: browser)?\/v?([\w.]+)( mobile)?/i, + (match)=>_object_spread({ + name: 'chrome', + mobile: /mobile/i.test((_match_1 = match[2]) !== null && _match_1 !== void 0 ? _match_1 : '') + }, parseVersion(match[1])) + ], + // FIREFOX ------------------------------------------------------------------------ + // Focus + [ + /\bfocus\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'firefox', + variant: 'focus', + mobile: true + }, parseVersion(match[1])) + ], + // Firefox for iOS + [ + /fxios\/([\w.-]+)/i, + /(?:mobile|tablet);.*(?:firefox)\/([\w.-]+)/i, + (match)=>_object_spread({ + name: 'firefox', + mobile: true + }, parseVersion(match[1])) + ], + // Firefox OSS versions + [ + /(icedragon|iceweasel|camino|chimera|fennec|maemo browser|minimo|conkeror|klar)[/ ]?([\w.+]+)/i, + (match)=>_object_spread({ + name: 'firefox', + variant: match[1].trim().toLowerCase(), + mobile: false + }, parseVersion(match[2])) + ], + // Firefox + [ + /(?:firefox)\/([\w.]+)/i, + /(?:mozilla)\/([\w.]+) .+rv:.+gecko\/\d+/i, + (match)=>_object_spread({ + name: 'firefox', + mobile: false + }, parseVersion(match[1])) + ], + // SAFARI ------------------------------------------------------------------------- + [ + /version\/([\w.,]+) .*mobile(?:\/\w+ | ?)safari/i, + /version\/([\w.,]+) .*(safari)/i, + /webkit.+?(?:mobile ?safari|safari)(?:\/([\w.]+))/i, + (match)=>_object_spread({ + name: 'safari', + mobile: /mobile/i.test(match[0]) + }, parseVersion(match[1])) + ] + ], + // ENGINE --------------------------------------------------------------------------- + engine: [ + [ + /webkit\/(?:537\.36).+chrome\/(?!27)([\w.]+)/i, + (match)=>_object_spread({ + name: 'blink' + }, parseVersion(match[1])) + ], + [ + /windows.+ edge\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'blink' + }, parseVersion(match[1])) + ], + [ + /presto\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'presto' + }, parseVersion(match[2])) + ], + [ + /trident\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'trident' + }, parseVersion(match[1])) + ], + [ + /gecko\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'gecko' + }, parseVersion(match[1])) + ], + [ + /(khtml|netfront|netsurf|amaya|lynx|w3m|goanna)\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'other' + }, parseVersion(match[2])) + ], + [ + /webkit\/([\w.]+)/i, + (match)=>_object_spread({ + name: 'webkit' + }, parseVersion(match[1])) + ] + ], + // OS ------------------------------------------------------------------------------- + os: [ + // Windows + [ + /microsoft windows (vista|xp)/i, + /windows nt 6\.2; (arm)/i, + /windows (?:phone(?: os)?|mobile)[/ ]?([\d.\w ]*)/i, + /windows[/ ]?([ntce\d. ]+\w)(?!.+xbox)/i, + /(?:win(?=3|9|n)|win 9x )([nt\d.]+)/i, + (match)=>_object_spread({ + name: 'windows' + }, parseVersion(match[1])) + ], + // iOS (iPhone/iPad) + [ + /ip[honead]{2,4}\b(?:.*os ([\w]+) like mac|; opera)/i, + /(?:ios;fbsv\/|iphone.+ios[/ ])([\d.]+)/i, + (match)=>_object_spread({ + name: 'ios' + }, parseVersion(match[1].replace(/_/g, '.'))) + ], + // macOS + [ + /mac(?:intosh;?)? os x ?([\d._]+)/i, + (match)=>_object_spread({ + name: 'macos' + }, parseVersion(match[1].replace(/_/g, '.'))) + ], + // ChromeOS + [ + /cros [\w]+(?:\)| ([\w.]+)\b)/i, + (match)=>_object_spread({ + name: 'chromeos' + }, parseVersion(match[1])) + ], + // Android + [ + /(?:android|webos|qnx|bada|rim tablet os|maemo|meego|sailfish)[-/ ]?([\w.]*)/i, + /droid ([\w.]+|[\d+])\b.+(android[- ]x86|harmonyos)/i, + (match)=>_object_spread({ + name: 'android' + }, parseVersion(match[1])) + ], + // Linux + [ + /linux/i, + ()=>({ + name: 'linux' + }) + ] + ] +}; +/** + * Extend a data structure by running a list of functions over it. + */ function applyExtensions(data, extensions) { + let result = data; + for (const extension of extensions){ + result = extension(result); + } + return result; +} +/** + * Parse the user agent string from the navigator object into a descriptor. + * + * @param navigator The Navigator object to parse + * @param options Parse options + * @returns The descriptor with optional extensions applied + */ function parseUserAgent(navigator, options) { + var _navigator, _options; + var _navigator_userAgent; + const descriptor = { + navigator: navigator, + ua: (_navigator_userAgent = (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.userAgent) !== null && _navigator_userAgent !== void 0 ? _navigator_userAgent : '', + extensions: [], + browser: applyRules(RULES.browser, navigator, { + name: 'unknown', + mobile: false + }), + engine: applyRules(RULES.engine, navigator, { + name: 'unknown' + }), + os: applyRules(RULES.os, navigator, { + name: 'unknown' + }) + }; + var _options_extensions; + return applyExtensions(descriptor, (_options_extensions = (_options = options) === null || _options === void 0 ? void 0 : _options.extensions) !== null && _options_extensions !== void 0 ? _options_extensions : []); +} + +export { parseUserAgent }; diff --git a/shared/utils/node_modules/@amp/runtime-detect/dist/version.js b/shared/utils/node_modules/@amp/runtime-detect/dist/version.js new file mode 100644 index 0000000..eba858c --- /dev/null +++ b/shared/utils/node_modules/@amp/runtime-detect/dist/version.js @@ -0,0 +1,99 @@ +function parseVersion(input) { + const data = { + version: input.toLowerCase() + }; + const parts = input.toLowerCase().split('.').filter((p)=>!!p); + // Only parse single parts that are actual numbers. + // This check will prevent `parseInt` from pasing the leading chars if they are + // valid numbers. + if (parts.length <= 1 && !/^\d+$/.test(parts[0])) { + return data; + } + const major = parseInt(parts[0], 10); + const minor = parseInt(parts[1], 10); + const patch = parseInt(parts[2], 10); + // Only add converted versions if `major` part was valid + if (!Number.isNaN(major)) { + data.major = major; + if (!Number.isNaN(minor)) data.minor = minor; + if (!Number.isNaN(patch)) data.patch = patch; + } + return data; +} +/** + * Check if the given value is a complete Version struct. + */ // eslint-disable-next-line @typescript-eslint/no-explicit-any +function isVersion(value) { + var _value, _value1; + return typeof ((_value = value) === null || _value === void 0 ? void 0 : _value.major) === 'number' && typeof ((_value1 = value) === null || _value1 === void 0 ? void 0 : _value1.minor) === 'number'; +} +/** + * Compare two version numbers together. + * + * NOTE: This only supports the first 3 segments (major, minor, patch) and does not + * do a full SemVer compare. + * + * @example + * ```javascript + * compareVersion('1.2.3', '1.2.4'); + * // => -1 + * ``` + */ function compareVersion(base, comp) { + let baseList = toNumbers(base); + let compList = toNumbers(comp); + // Right pad versions with zeros to make them equal length + const versionLength = Math.max(baseList.length, compList.length); + baseList = baseList.concat(Array(versionLength).fill(0)).slice(0, versionLength); + compList = compList.concat(Array(versionLength).fill(0)).slice(0, versionLength); + /** Constrain the given value to the output range of this function. */ const constrain = (value)=>{ + if (value <= -1) return -1; + else if (value >= 1) return 1; + else return 0; + }; + for(let index = 0; index < versionLength; index++){ + const aValue = baseList[index]; + const bValue = compList[index]; + if (aValue !== bValue) { + return constrain(aValue - bValue); + } + } + return 0; +} +function eq(base, comp) { + return compareVersion(base, comp) === 0; +} +function gt(base, comp) { + return compareVersion(base, comp) > 0; +} +function gte(base, comp) { + const result = compareVersion(base, comp); + return result > 0 || result === 0; +} +function lt(base, comp) { + return compareVersion(base, comp) < 0; +} +function lte(base, comp) { + const result = compareVersion(base, comp); + return result < 0 || result === 0; +} +function toNumbers(value) { + if (Array.isArray(value)) { + return value; + } else if (typeof value === 'number') { + return [ + value + ]; + } else if (typeof value === 'string') { + return toNumbers(parseVersion(value)); + } else { + const values = [ + value.major, + value.minor, + value.patch + ]; + const uidx = values.indexOf(undefined); + return uidx === -1 ? values : values.slice(0, uidx); + } +} + +export { compareVersion, eq, gt, gte, isVersion, lt, lte, parseVersion }; diff --git a/shared/utils/src/get-pwa-display-mode.ts b/shared/utils/src/get-pwa-display-mode.ts new file mode 100644 index 0000000..506c80d --- /dev/null +++ b/shared/utils/src/get-pwa-display-mode.ts @@ -0,0 +1,39 @@ +export enum PWADisplayMode { + TWA = 'twa', + BROWSER = 'browser', + STANDALONE = 'standalone', + MINIMAL = 'minimal-ui', + FULLSCREEN = 'fullscreen', + OVERLAY = 'window-controls-overlay', + UNKNOWN = 'unknown', +} + +/** + * For PWA, reads the "display" value from the manifest.json and returns the proper value if it matches. + * Inspired by the sample snippet here: https://web.dev/learn/pwa/detection + */ +export const getPWADisplayMode = (): PWADisplayMode => { + switch (true) { + case document.referrer.startsWith('android-app://'): + return PWADisplayMode.TWA; + + case window.matchMedia('(display-mode: browser)').matches: + return PWADisplayMode.BROWSER; + + case window.matchMedia('(display-mode: standalone)').matches: + return PWADisplayMode.STANDALONE; + + case window.matchMedia('(display-mode: minimal-ui)').matches: + return PWADisplayMode.MINIMAL; + + case window.matchMedia('(display-mode: fullscreen)').matches: + return PWADisplayMode.FULLSCREEN; + + case window.matchMedia('(display-mode: window-controls-overlay)') + .matches: + return PWADisplayMode.OVERLAY; + + default: + return PWADisplayMode.UNKNOWN; + } +}; diff --git a/shared/utils/src/history.ts b/shared/utils/src/history.ts new file mode 100644 index 0000000..498f7d1 --- /dev/null +++ b/shared/utils/src/history.ts @@ -0,0 +1,168 @@ +import type { Logger, LoggerFactory } from '@amp/web-apps-logger'; +import { LruMap } from './lru-map'; +import type { ScrollableElement } from './try-scroll'; +import { tryScroll } from './try-scroll'; +import { removeHost } from './url'; +import { generateUuid } from './uuid'; + +export interface Options { + getScrollablePageElement(): ScrollableElement | null; +} + +type Id = string; +const HISTORY_SIZE_LIMIT = 10; + +interface WithScrollPosition<State> { + scrollY: number; + state: State; +} +/** + * We are using a currentStateId on this class to always store the state id instead of saving + * it on the window.history.state because there seems to be a bug in Safari where it is mutating + * the window.history.state to null after our Sign In flow which includes multiple iframes + * and multiple internal state changes inside the iframes. We can move back to window.history.state storing the id + * if the Safari Issue is fixed in future. + */ +export class History<State> { + private readonly log: Logger; + private readonly states: LruMap<Id, WithScrollPosition<State>>; + private readonly getScrollablePageElement: () => ScrollableElement | null; + private currentStateId: string | undefined; + + constructor( + loggerFactory: LoggerFactory, + options: Options, + sizeLimit: number = HISTORY_SIZE_LIMIT, + ) { + this.log = loggerFactory.loggerFor('History'); + this.states = new LruMap(sizeLimit); + this.getScrollablePageElement = options.getScrollablePageElement; + } + + // Update page data but keep scroll position + updateState(update: (state?: State) => State): void { + if (!this.currentStateId) { + this.log.warn( + 'failed: encountered a null currentStateId inside updateState', + ); + return; + } + + const currentState = this.states.get(this.currentStateId); + const newState = update(currentState?.state); + this.log.info('updateState', newState, this.currentStateId); + this.states.set(this.currentStateId, { + ...(currentState as WithScrollPosition<State>), + state: newState, + }); + } + + replaceState(state: State, url: string | null): void { + const id = generateId(); + this.log.info('replaceState', state, url, id); + window.history.replaceState({ id }, '', this.removeHost(url)); + this.currentStateId = id; + this.states.set(id, { state, scrollY: 0 }); + this.scrollTop = 0; + } + + pushState(state: State, url: string | null): void { + const id = generateId(); + this.log.info('pushState', state, url, id); + window.history.pushState({ id }, '', this.removeHost(url)); + this.currentStateId = id; + this.states.set(id, { state, scrollY: 0 }); + this.scrollTop = 0; + } + + beforeTransition(): void { + const { state } = window.history; + + if (!state) { + return; + } + + const oldState = this.states.get(state.id); + if (!oldState) { + this.log.info( + 'current history state evicted from LRU, not saving scroll position', + ); + return; + } + + const { scrollTop } = this; + + this.states.set(state.id, { + ...oldState, + scrollY: scrollTop, + }); + + this.log.info('saving scroll position', scrollTop); + } + + private removeHost(url: string | null): string | undefined { + if (!url) { + this.log.warn('received null URL'); + return; + } + + // TODO: rdar://77982655 (Investigate router improvements): host mismatch? + return removeHost(url); + } + + onPopState( + listener: (url: string, state: State | undefined) => void, + ): void { + window.addEventListener('popstate', (event: PopStateEvent): void => { + this.currentStateId = event.state?.id; + + if (!this.currentStateId) { + this.log.warn( + 'encountered a null event.state.id in onPopState event: ', + window.location.href, + ); + } + + this.log.info('popstate', this.states, this.currentStateId); + const state = this.currentStateId + ? this.states.get(this.currentStateId) + : undefined; + listener(window.location.href, state?.state); + + if (!state) { + return; + } + + const { scrollY } = state; + + this.log.info('restoring scroll to', scrollY); + + tryScroll(this.log, () => this.getScrollablePageElement(), scrollY); + }); + } + + private get scrollTop(): number { + return this.getScrollablePageElement()?.scrollTop || 0; + } + + private set scrollTop(scrollTop: number) { + const element = this.getScrollablePageElement(); + if (element) { + element.scrollTop = scrollTop; + } + } + + // TODO: rdar://77982655 (Investigate router improvements): offPopState? +} + +/** + * Generate a (unique) id for storing in window.history.state. + * + * @return the generated ID + */ +function generateId(): Id { + // The use of something random (and not say, an incrementing counter) is important + // here. These states can survive refreshes so the IDs used must be globally unique + // (and not just unique to the current page load). + return generateUuid(); +} diff --git a/shared/utils/src/is-pojo.ts b/shared/utils/src/is-pojo.ts new file mode 100644 index 0000000..4363454 --- /dev/null +++ b/shared/utils/src/is-pojo.ts @@ -0,0 +1,20 @@ +/** + * Determine if {@linkcode arg} is a Plain Old JavaScript Object. + * + * @see https://masteringjs.io/tutorials/fundamentals/pojo + * + * @param arg to test + * @returns true if {@linkcode arg} is a POJO + */ +export function isPOJO(arg: unknown): arg is Record<string, unknown> { + if (!arg || typeof arg !== 'object') { + return false; + } + + const proto = Object.getPrototypeOf(arg); + if (!proto) { + return true; // `Object.create(null)` + } + + return proto === Object.prototype; +} diff --git a/shared/utils/src/launch/launch-client.ts b/shared/utils/src/launch/launch-client.ts new file mode 100644 index 0000000..24378d6 --- /dev/null +++ b/shared/utils/src/launch/launch-client.ts @@ -0,0 +1,109 @@ +import { createClientLink } from './scheme'; +import type { Platform } from '../platform'; + +/** + * Navigator for older Microsoft (MS) browsers like Internet Explorer. + */ +type MSNavigator = Navigator & { + msLaunchUri: ( + href: string | URL, + successCallback: () => void, + failureCallback: () => void, + ) => void; +}; + +/** + * Check if the given value is an MSNavigator. + */ +function isMSNavigator(value: Partial<MSNavigator>): value is MSNavigator { + return typeof value?.msLaunchUri === 'function'; +} + +/** + * Callback for client launches. + */ +export type LaunchCallback = (result: { + link: URL; + success: boolean; +}) => void | Promise<void>; + +/** + * Attempt to launch the native client for the given Web URL. + */ +export function launchClient( + url: string | URL, + platform: Platform, + callback: LaunchCallback = () => {}, +): void { + const { window, browser, os } = platform; + + /** URL for opening the native application */ + const link = createClientLink(url, { platform }); + + // macOS Safari + if (os.isMacOS && browser.isSafari) { + launchOnMacOS(link, platform, callback); + } + // Proprietary msLaunchUri method (IE 10+ on Windows 8+) + else if (isMSNavigator(platform.navigator)) { + platform.navigator.msLaunchUri( + String(link), + () => callback({ link, success: true }), + () => callback({ link, success: false }), + ); + } + // Other platforms + else { + try { + // on iOS, Windows and Android simply opening the href works + window!.top!.window.location.href = String(link); + callback({ link, success: true }); + } catch (e) { + // we know this is NOT installed + callback({ link, success: false }); + } + } +} + +function launchOnMacOS( + link: URL, + platform: Platform, + callback: LaunchCallback, +): void { + const { window } = platform; + + if (typeof window === 'undefined') { + callback({ link, success: false }); + return; + } + + /** Timer for blur fallback */ + let timer: number; + + /** IFrame reference for opening the client link */ + let iframe: HTMLIFrameElement | undefined; + + /** Cleanup function run after the client launch has been initiated */ + function finalize() { + clearTimeout(timer); + window!.removeEventListener('blur', finalize); + if (iframe !== undefined) { + window!.document.body.removeChild(iframe); + } + + callback({ link, success: true }); + } + + // Add an iFrame window to the current document to open the URL + iframe = window.document.createElement('iframe'); + iframe.id = 'launch-client-opener'; + iframe.style.display = 'none'; + window.document.body.appendChild(iframe); + + // Redirect the iFrame to the client link to trigger it to open + iframe.contentWindow!.location.href = String(link); + + // Wait a tiny amount of time for the client launch to appear + window.addEventListener('blur', finalize); + timer = setTimeout(finalize, 50) as unknown as number; +} diff --git a/shared/utils/src/launch/scheme.ts b/shared/utils/src/launch/scheme.ts new file mode 100644 index 0000000..1b548c4 --- /dev/null +++ b/shared/utils/src/launch/scheme.ts @@ -0,0 +1,339 @@ +import { removeScheme } from '..'; +import { Platform } from '../platform'; + +/** + * Check if the URL hostname matches the given value. + */ +const matchesHostName = (url: URL, hostName: string) => + url.hostname === hostName; + +/** + * Check if the URL `?app=xyz` search param matches the given value. + */ +const matchesAppName = (url: URL, appName: string) => + url.searchParams.get('app') === appName; + +/** + * Check if the URL `?mt=n` search param matches any of the given values. + */ +const matchesMediaType = (url: URL, mediaTypes: string[]) => { + const mt = url.searchParams.get('mt'); + return mt ? mediaTypes.includes(mt) : false; +}; + +/** + * Check if the URL pathname matches the given pattern. + */ +const matchesPathName = (url: URL, pattern: RegExp | string) => + new RegExp(pattern).test(url.pathname); + +/** + * Check if the URL is for Audiobooks + */ +const isAudiobookURL = (url: URL): boolean => + matchesAppName(url, 'audiobook') || + matchesMediaType(url, ['3']) || + matchesPathName(url, /\/(audiobook\/|viewAudiobook)/i); + +/** + * Check if the URL is for Books. + */ +const isBooksURL = (url: URL): boolean => + !isAudiobookURL(url) && + (matchesHostName(url, 'books.apple.com') || + matchesAppName(url, 'books') || + matchesMediaType(url, ['11', '13']) || + matchesPathName(url, '/book/')); + +/** + * Check if the URL is for Commerce. + */ +const isCommerceURL = (url: URL): boolean => + matchesHostName(url, 'finance-app.itunes.apple.com') || + matchesPathName(url, '/account/'); + +/** + * Check if the URL is for a macOS App. + */ +const isMacAppURL = (url: URL): boolean => + matchesAppName(url, 'mac-app') || + matchesMediaType(url, ['12']) || + matchesPathName(url, '/mac-app/'); + +/** + * Check if the URL is an AppStore Story. + */ +const isStoryURL = (url: URL): boolean => + matchesAppName(url, 'story') || matchesPathName(url, '/story/'); + +/** + * Check if the URL is for Messages. + */ +const isMessagesURL = (url: URL): boolean => matchesAppName(url, 'messages'); + +/** + * Check if the URL is for Music. + */ +const isMusicURL = (url: URL): boolean => + matchesHostName(url, 'music.apple.com') || + matchesAppName(url, 'music') || + matchesPathName( + url, + /\/(album|artist|playlist|station|curator|music-video)\//i, + ); + +/** + * Check if the URL is for Podcasts. + */ +const isPodcastsURL = (url: URL): boolean => + matchesHostName(url, 'podcasts.apple.com') || + matchesAppName(url, 'podcasts') || + matchesMediaType(url, ['2']) || + matchesPathName(url, '/podcast/'); + +/** + * Check if the URL is for TV. + */ +const isTVURL = (url: URL): boolean => + matchesHostName(url, 'tv.apple.com') || + matchesPathName( + url, + /\/(episode|movie|movie-collection|show|season|sporting-event|person)\//i, + ); + +/** + * Check if the URL is for the Watch. + */ +const isWatchURL = (url: URL): boolean => matchesAppName(url, 'watch'); + +/** + * Check if the URL is developer.apple.com related. + */ +const isDeveloperURL = (url: URL): boolean => + matchesAppName(url, 'developer') || matchesPathName(url, '/developer/'); + +/** + * Check if the URL is for an app. + */ +const isAppsURL = (url: URL): boolean => + matchesMediaType(url, ['8']) && !isMessagesURL(url) && !isWatchURL(url); + +/** + * Function for identifying application schemes from web URLs. + */ +type SchemeIdentifier = (url: URL, platform: Platform) => boolean; + +/** + * List of schemes and functions to identify them based on a URL and Platform details. + * + * These schemes are derived from [Jingle Properties](https://github.pie.apple.com/amp-dev/Jingle/blob/6392929afb8540ac488315647992c3f46a9cc82f/MZConfig/Properties/apps/MZInit2/common.properties#L993). + * + * ```java + * // <rdar://problem/66551318> iOS Bag: Move mobile-url-handlers to a property defined list + * MZInit.iOS.acceptedUrlHandlers=("applenews", "applenewss", "applestore", "applestore-sec", "bridge", "com.apple.tv", "disneymoviesanywhere",\ + * "http", "https", "itms", "itmss", "itms-apps", "itms-appss", "itms-books", "itms-bookss", "itms-gc", "itms-gcs", "itms-itunesu",\ + * "itms-itunesus", "itms-podcast", "itms-podcasts", "itms-ui", "its-music", "its-musics", "its-news", "its-newss", "its-videos",\ + * "its-videoss", "itsradio", "livenation", "mailto", "message", "moviesanywhere", "music", "musics", "prefs", "shoebox") + * ``` + */ +const identifiers: [string, SchemeIdentifier, ...SchemeIdentifier[]][] = [ + [ + 'itms-apps', + (url, platform) => + platform.os.isIOS && + (isCommerceURL(url) || + isAppsURL(url) || + isStoryURL(url) || + isDeveloperURL(url)), + ], + + // Watch app on mobile + [ + 'itms-watch', + (url, platform) => platform.browser.isMobile && isWatchURL(url), + ], + + // Messages app on mobile + [ + 'itms-messages', + function (url: URL, platform: Platform) { + return platform.browser.isMobile && isMessagesURL(url); + }, + ], + + [ + 'itms-books', + (url, platform) => + platform.os.isMacOS && + platform.os.gte('10.15') && + isAudiobookURL(url), + (url, _platform) => isBooksURL(url), + ], + + // Music on Android + [ + 'apple-music', + (url, platform) => platform.os.isAndroid && isMusicURL(url), + ], + + // Music on iOS/macOS + [ + 'music', + (url, platform) => platform.os.isIOS && isMusicURL(url), + (url, platform) => { + return ( + platform.os.isMacOS && + platform.os.gte('10.15') && + isMusicURL(url) + ); + }, + ], + + // Podcasts on iOS + [ + 'itms-podcasts', + (url, platform) => platform.os.isIOS && isPodcastsURL(url), + ], + + // Podcasts on macOS + [ + 'podcasts', + (url, platform) => + platform.os.isMacOS && + platform.os.gte('10.15') && + isPodcastsURL(url), + ], + + // TV on iOS + [ + 'com.apple.tv', + (url, platform) => + platform.os.isIOS && platform.os.gte('10.2') && isTVURL(url), + ], + + // TV on macOS + [ + 'videos', + (url: URL, platform: Platform) => + platform.os.isMacOS && platform.os.gte('10.15') && isTVURL(url), + ], + + [ + 'macappstore', + (url, _platform) => isMacAppURL(url), + (url, platform) => + platform.os.isMacOS && + platform.os.gte('10.15') && + isCommerceURL(url), + + // Story and developer pages should launch Mac App Store on Mojave(10.14)+ + // <rdar://problem/46461633> Story page with ls=1 QP should attempt to open Mac App Store on Mojave + + // rdar://81291713 (Star: https://apps.apple.com/developer/id463855590?ls=1 launches Music App) + (url, platform) => + platform.os.isMacOS && + platform.os.gte('10.14') && + (isStoryURL(url) || isDeveloperURL(url)), + ], + + // Catch All + ['itms', (_url, _platform) => true], +]; + +/** + * Get the Scheme for attempting to open a platform native application. + * + * @see {@link https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Syntax} + */ +export function detectClientScheme( + url: string | URL, + options?: { platform?: Platform }, +): string { + url = new URL(url); + + // Assume that any URLs that don't have the http(s) scheme already have the + // correct scheme assigned. + if (/https?/i.test(url.protocol)) { + const platform = options?.platform ?? Platform.detect(); + + for (const [scheme, ...fns] of identifiers) { + for (const fn of fns) { + if (fn(url, platform)) { + return scheme; + } + } + } + } + + // At this point something should have matched. If not just return the original + // scheme and have the browser or system handle it. + return normalizeScheme(url.protocol); +} + +/** + * Check if the given URL has an Apple specific Scheme. + * + * @example + * ```javascript + * hasAppleClientScheme('music://music.apple.com/browse') // => true + * hasAppleClientScheme('https://music.apple.com/browse') // => false + * ``` + */ +export function hasAppleClientScheme( + url: URL | string, + _options?: { platform?: Platform }, +) { + const pattern = + /^(?:itms(?:-.*)?|macappstore|podcast|video|(?:apple-)?music)s?(:|$)/im; + return pattern.test(new URL(url).protocol); +} + +/** + * Create a link for attempting to open a platform native application based on a web URL. + * + * @example + * ```javascript + * createClientLink('https://music.apple.com/browse'); + * // => 'music://music.apple.com/browse' + * ``` + */ +export function createClientLink( + url: string | URL, + options?: { platform?: Platform }, +): URL { + const link = new URL(url); + + // Removes any development prefixes in order to correctly identify the scheme + link.host = link.host.replace( + /^(?:[^-]+[-.])?([^.]+)\.apple\.com/, + '$1.apple.com', + ); + + // Remove any port designation, this should not be present in application links + link.port = ''; + + const scheme = detectClientScheme(link, { + platform: options?.platform, + }); + + // If the identified scheme is already assigned we want to leave the URL unmodified + if (scheme === normalizeScheme(link.protocol)) { + return new URL(url); + } + + return new URL(scheme + '://' + removeScheme(link)); +} + +/** + * Normalize a scheme value by removing any separators from it. + * + * @example + * ```javascript + * normalizeScheme('music') // => 'music' + * normalizeScheme('TV') // => 'tv' + * normalizeScheme('https:') // => 'https' + * normalizeScheme('https://') // => 'https' + * ``` + */ +function normalizeScheme(value: string): string { + return value.replace(/[:]+$/, '').toLowerCase(); +} diff --git a/shared/utils/src/lru-map.ts b/shared/utils/src/lru-map.ts new file mode 100644 index 0000000..79eb41c --- /dev/null +++ b/shared/utils/src/lru-map.ts @@ -0,0 +1,60 @@ +/** + * LRU Map implementation storing key/values up to a provided size limit. Beyond that + * size limit, the least recently used entry is evicted. + * + * @see https://github.pie.apple.com/isao/lru-map + */ +export class LruMap<K, V> extends Map<K, V> { + private sizeLimit: number; + + constructor(sizeLimit: number) { + super(); + this.setSizeLimit(sizeLimit); + // Needed to convince TS that this is set (it's actually handled by setSizeLimit) + this.sizeLimit = sizeLimit; + } + + /** + * Retrieve a value from the map with a given key. + * @param key The key for the entry + * @return value The entry's value (or undefined if non existent) + */ + get(key: K): V | undefined { + let value: V | undefined; + + if (this.has(key)) { + value = super.get(key); + + // Map entries are always in insertion order. So + // readding, pushes this entry to the top of the LRU. + this.delete(key); + super.set(key, value!); + } + + return value; + } + + set(key: K, value: V): this { + super.set(key, value); + this.prune(); + return this; + } + + setSizeLimit(newSizeLimit: number): void { + if (newSizeLimit < 0 || !isFinite(newSizeLimit)) { + throw new Error( + `setSizeLimit expects finite positive number, got: ${newSizeLimit}`, + ); + } + + this.sizeLimit = newSizeLimit; + this.prune(); + } + + private prune(): void { + while (this.size > this.sizeLimit) { + const leastRecentlyUsedKey = this.keys().next().value; + this.delete(leastRecentlyUsedKey); + } + } +} diff --git a/shared/utils/src/object-from-entries.ts b/shared/utils/src/object-from-entries.ts new file mode 100644 index 0000000..80d3cdb --- /dev/null +++ b/shared/utils/src/object-from-entries.ts @@ -0,0 +1,18 @@ +// TODO: rdar://78109780 (Update to Node 16) +/** + * Create an object from an iterable of key/value pairs. + * + * @param entries The key value pairs (ex. [['a', 1], ['b', 2]]) + * @return The created object + */ +export function fromEntries<V>(entries: Iterable<readonly [PropertyKey, V]>): { + [k: string]: V; +} { + const result: Record<PropertyKey, V> = {}; + + for (const [key, value] of entries) { + result[key] = value; + } + + return result; +} diff --git a/shared/utils/src/optional.ts b/shared/utils/src/optional.ts new file mode 100644 index 0000000..7058803 --- /dev/null +++ b/shared/utils/src/optional.ts @@ -0,0 +1,22 @@ +export type Optional<T> = T | None; +export type None = null | undefined; + +/** + * Determine if an optional value is present. + * + * @param optional value + * @return true if present, false otherwise + */ +export function isSome<T>(optional: Optional<T>): optional is T { + return optional !== null && optional !== undefined; +} + +/** + * Determine if an optional value is not present. + * + * @param optional value + * @return true if not present, false otherwise + */ +export function isNone<T>(optional: Optional<T>): optional is None { + return optional === null || optional === undefined; +} diff --git a/shared/utils/src/platform.ts b/shared/utils/src/platform.ts new file mode 100644 index 0000000..15644e5 --- /dev/null +++ b/shared/utils/src/platform.ts @@ -0,0 +1,249 @@ +import { + parseUserAgent, + flagsExtension, + compareExtension, +} from '@amp/runtime-detect'; +import { launchClient, type LaunchCallback } from './launch/launch-client'; + +type NavigatorLike = { + userAgent: string; + maxTouchPoints?: number; +}; + +/** + * Detect a Platform descriptor from the browsers user agent. + */ +function detectDescriptor(options?: { + window?: Window; + navigator?: NavigatorLike; +}) { + const defaultNavigator: NavigatorLike = + typeof options?.window?.navigator !== 'undefined' + ? options.window.navigator + : { + userAgent: '', + maxTouchPoints: 0, + }; + + return parseUserAgent(options?.navigator ?? defaultNavigator, { + extensions: [flagsExtension, compareExtension], + }); +} + +export type PlatformDescriptor = ReturnType<typeof detectDescriptor>; + +export class Platform { + static detect( + this: typeof Platform, + options?: { window?: Window; navigator?: NavigatorLike }, + ) { + const window = options?.window ?? globalThis?.window; + return new this({ + window: window, + descriptor: detectDescriptor({ + window: window, + navigator: options?.navigator, + }), + }); + } + + /** + * Descriptor from detecting platform data. + */ + readonly descriptor: PlatformDescriptor; + + /** + * Navigator value used to create the platform descriptor. + */ + readonly navigator: NavigatorLike; + + /** + * Reference to the platform Window object. This might be `undefined` in some + * environments. + */ + readonly window: Window | undefined; + + /** + * User Agent string the platform descriptor was parsed from. + */ + readonly ua: string; + + /** + * Browser descriptor for the Platform. + */ + readonly browser: PlatformDescriptor['browser']; + + /** + * Browser Engine descriptor for the Platform. + */ + readonly engine: PlatformDescriptor['engine']; + + /** + * Operating System descriptor for the Platform. + */ + readonly os: PlatformDescriptor['os']; + + constructor(config: { + descriptor: PlatformDescriptor; + window?: Window; + navigator?: NavigatorLike; + }) { + const { descriptor } = config; + this.descriptor = descriptor; + this.navigator = config.navigator ?? descriptor.navigator; + this.window = config.window; + + this.ua = descriptor.ua; + this.browser = descriptor.browser; + this.engine = descriptor.engine; + this.os = descriptor.os; + } + + /** + * Check if Apple native applications can be opened on the Platform. + */ + canOpenNative(): boolean { + return this.ismacOS() || this.isiOS(); + } + + /** + * Check if the Platform is running a mobile browser. + */ + isMobile(): boolean { + return this.browser.isMobile; + } + + /** + * Check if the Platform registers as running the Android operating system. + */ + isAndroid(): boolean { + return this.os.isAndroid; + } + + /** + * Check if the Platform registers as running the iOS operating system. + */ + isiOS(): boolean { + return this.os.isIOS; + } + + /** + * Check if the Platform registers as running the iPadOS operating system. + */ + isiPadOS(): boolean { + return this.os.isIPadOS; + } + + /** + * Check if the Platform registers as running the macOS operating system. + */ + ismacOS(): boolean { + return this.os.isMacOS; + } + + /** + * Check if the Platform registers as running the Windows operating system. + */ + isWindows(): boolean { + return this.os.isWindows; + } + + /** + * Check if the Platform registers as running a Linux operating system. + */ + isLinux(): boolean { + return this.os.isLinux; + } + + /** + * Check if the Platform is running the Apple Safari browser. + */ + isSafari(): boolean { + return this.browser.isSafari; + } + + /** + * Check if the Platform is running the Google Chrome browser. + */ + isChrome(): boolean { + return this.browser.isChrome; + } + + /** + * Check if the Platform is running the Mozilla Firefox browser. + */ + isFirefox(): boolean { + return this.browser.isFirefox; + } + + /** + * Check if the Platform is running the Microsoft Edge browser. + */ + isEdge(): boolean { + return this.browser.isEdge; + } + + /** + * Get name for the Platform browser. + * @deprecated Use `platform.browser.name` directly + */ + clientName(): string { + return this.browser.name[0].toUpperCase() + this.browser.name.slice(1); + } + + /** + * Get the Platform browser major version number. + * @deprecated Use `platform.browser.major` directly + */ + majorVersion(): number { + return this.browser.major ?? 0; + } + + /** + * Get the Platform browser minor version number. + * @deprecated Use `platform.browser.minor` directly + */ + minorVersion(): number { + return this.browser.minor ?? 0; + } + + /** + * Get the name for the Platform operating system. + * @deprecated Use `platform.os.name` directly + */ + osName(): string { + return this.os.name; + } + + /** + * Attempt to launch a native client for the given web URL. + * + * The callback is called with a report if the attempt was successful. + * + * @example + * ```javascript + * platform.launchClient( + * 'https://music.apple.com/browse', + * function ({ link, success }) { + * if (success) { + * console.log(`Opened client with ${link}`); + * } else { + * console.log(`Failed to open client with ${link}`); + * } + * } + * ); + * ``` + */ + launchClient(url: string, callback?: LaunchCallback): void { + launchClient(url, this, callback); + } + + /** + * Check if the platform has full support for playing encrypted HLS content. + */ + hasEncryptedPlaybackSupport(): boolean { + return !this.os.isIOS || this.os.gte('17.5'); + } +} + +export const platform = Platform.detect(); diff --git a/shared/utils/src/try-scroll.ts b/shared/utils/src/try-scroll.ts new file mode 100644 index 0000000..1e6b0d2 --- /dev/null +++ b/shared/utils/src/try-scroll.ts @@ -0,0 +1,65 @@ +import type { Logger } from '@amp/web-apps-logger'; +export interface ScrollableElement { + scrollTop: number; + scrollHeight: number; + offsetHeight: number; +} + +// Global is okay here as this only runs in the browser +let nextTry: number | null = null; + +export function tryScroll( + log: Logger, + getScrollablePageElement: Function, + scrollY: number, +): void { + let tries = 0; + + if (nextTry !== null) { + window.cancelAnimationFrame(nextTry); + } + + nextTry = window.requestAnimationFrame(function doNextTry() { + // At 16ms per frame, this is 1600ms + // See: https://github.com/DockYard/ember-router-scroll/blob/2f17728f/addon/services/router-scroll.js#L56 + if (++tries >= 100) { + log.warn("wasn't able to restore scroll within 100 frames"); + nextTry = null; + return; + } + + let element = getScrollablePageElement(); + if (!element) { + log.warn( + 'could not restore scroll: the scrollable element is missing', + ); + return; + } + const { scrollHeight, offsetHeight } = element; + + // Only scroll once we're able to get a full screen of content when + // scrollTop is set to scrollY + // + // +16 is a bit of a fudge factor to count for imperfections in + // features like lazy loading. If the scroll position to restore is + // the very bottom of the page, then scrollY + offsetHeight must be + // exactly scrollHeight. But if lazy loading components (for example) + // cause the page to grow by a few pixels, then this will never hold. + // Thus, we fudge by a few pixels to be more forgiving in this scenario. + const canScroll = scrollY + offsetHeight <= scrollHeight + 16; + + if (!canScroll) { + log.info('page is not tall enough for scroll yet', { + scrollHeight, + offsetHeight, + }); + + nextTry = window.requestAnimationFrame(doNextTry); + return; + } + + element.scrollTop = scrollY; + log.info('scroll restored to', scrollY); + nextTry = null; + }); +} diff --git a/shared/utils/src/url.ts b/shared/utils/src/url.ts new file mode 100644 index 0000000..f15792d --- /dev/null +++ b/shared/utils/src/url.ts @@ -0,0 +1,90 @@ +/** + * Remove the scheme and separators from the given URL. + * + * @example + * ```javascript + * removeScheme('https://music.apple.com/browse') // => 'music.apple.com/browse' + * removeScheme('apple-music://music.apple.com/browse') // => 'music.apple.com/browse' + * removeScheme('music.apple.com/browse') // => 'music.apple.com/browse' + * ``` + */ +export function removeScheme( + url: string | URL | null | undefined, +): string | undefined { + if (url === null || url === undefined) { + return undefined; + } + + return String(url).replace(/^((?:[^:]*:[/]{0,2})|(?::?\/\/))/i, ''); +} + +/** + * Strip scheme and host (hostname + port) from a URL, leaving just the path, query + * params, and hash. + * + * @param {string} url The URL possibly containing a host + * @returns {string} hostlessUrl The url without its host + */ +export function removeHost( + url: string | URL | null | undefined, +): string | undefined { + return removeScheme(url)?.replace(/^([^/]*)/i, ''); +} + +/** + * Strip query params and fragment from a URL. + */ +export function removeQueryParams( + url: string | URL | undefined, +): string | undefined { + if (url === undefined) { + return undefined; + } + + const value = String(url); + const splitIndex = value.indexOf('?'); + return splitIndex >= 0 ? value.slice(0, splitIndex) : value; +} + +export function getBaseUrl(): string { + const currentUrl = new URL(window.location.href); + return `${currentUrl.protocol}//${currentUrl.host}`; +} + +export function buildUrl(props: { + protocol?: string; + hostname: string; + pathname?: string | string[]; + queryParams?: string | Record<string, string>; + hash?: string; +}): URL { + const { + hostname, + pathname = '/', + queryParams = {}, + protocol = 'https', + hash = '', + } = props; + + // Base URL with domain + const url = new URL(protocol + '://' + removeScheme(hostname)); + + // URL path + url.pathname = Array.isArray(pathname) + ? '/' + pathname.map(encodeURIComponent).join('/').replace(/[/]+/, '/') + : pathname; + + // URL search (a.k.a. queryParams) + if (typeof queryParams === 'string') { + url.search = queryParams; + } else { + for (const [key, value] of Object.entries(queryParams)) { + url.searchParams.set(key, value); + } + } + + // URL hash + url.hash = hash; + + return url; +} diff --git a/shared/utils/src/uuid.ts b/shared/utils/src/uuid.ts new file mode 100644 index 0000000..0afa5ee --- /dev/null +++ b/shared/utils/src/uuid.ts @@ -0,0 +1,22 @@ +/** + * Generate a variant 1 UUIDv4. + * + * @return the UUID + */ +export function generateUuid(): string { + return 'xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx'.replace( + /[xV]/g, + (placeholder) => { + let nibble = (Math.random() * 16) | 0; + + if (placeholder === 'V') { + // Per RFC, the two MSB of byte 8 must be 0b10 (0x8). + // 0x3 (0b11) masks out the bottom two bits. + // See: https://tools.ietf.org/html/rfc4122.html#section-4.1.1 + nibble = (nibble & 0x3) | 0x8; + } + + return nibble.toString(16); + }, + ); +} |
