@10x-media/analytics 0.1.0-beta.4 → 1.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +280 -0
- package/LICENSE +21 -21
- package/README.md +15 -4
- package/dist/adapters/ga4/disableKey.d.ts +9 -0
- package/dist/adapters/ga4/disableKey.js +21 -0
- package/dist/adapters/ga4/disableKey.js.map +1 -0
- package/dist/adapters/ga4/eventName.d.ts +15 -0
- package/dist/adapters/ga4/eventName.js +19 -0
- package/dist/adapters/ga4/eventName.js.map +1 -0
- package/dist/adapters/ga4/ga4.d.ts +5 -0
- package/dist/adapters/ga4/ga4.js +186 -58
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/ga4/measurementId.js +15 -0
- package/dist/adapters/ga4/measurementId.js.map +1 -0
- package/dist/adapters/goalRead.js +126 -0
- package/dist/adapters/goalRead.js.map +1 -0
- package/dist/adapters/http/fetchJson.js +1 -1
- package/dist/adapters/plausible/plausible.d.ts +25 -0
- package/dist/adapters/plausible/plausible.js +159 -38
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.d.ts +10 -1
- package/dist/adapters/posthog/posthog.js +130 -21
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/series.js +11 -1
- package/dist/adapters/series.js.map +1 -1
- package/dist/adapters/umami/umami.d.ts +13 -1
- package/dist/adapters/umami/umami.js +278 -26
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/binding/resolvePath.js +18 -2
- package/dist/binding/resolvePath.js.map +1 -1
- package/dist/capture/getTrackerConfig.d.ts +20 -0
- package/dist/capture/getTrackerConfig.js +31 -0
- package/dist/capture/getTrackerConfig.js.map +1 -0
- package/dist/capture/mountPath.js +14 -0
- package/dist/capture/mountPath.js.map +1 -0
- package/dist/capture/proxyEndpoint.js +127 -0
- package/dist/capture/proxyEndpoint.js.map +1 -0
- package/dist/capture/proxyRoutes.js +108 -0
- package/dist/capture/proxyRoutes.js.map +1 -0
- package/dist/capture/requestBody.js +68 -0
- package/dist/capture/requestBody.js.map +1 -0
- package/dist/capture/slots.d.ts +8 -0
- package/dist/capture/slots.js +54 -0
- package/dist/capture/slots.js.map +1 -0
- package/dist/capture/trackerConfig.d.ts +33 -0
- package/dist/capture/trackerConfig.js +95 -0
- package/dist/capture/trackerConfig.js.map +1 -0
- package/dist/capture/trackerEndpoint.js +35 -0
- package/dist/capture/trackerEndpoint.js.map +1 -0
- package/dist/charts/BarList.d.ts +16 -1
- package/dist/charts/BarList.js +41 -19
- package/dist/charts/BarList.js.map +1 -1
- package/dist/charts/TrendChart.d.ts +16 -3
- package/dist/charts/TrendChart.js +172 -106
- package/dist/charts/TrendChart.js.map +1 -1
- package/dist/charts/bars.d.ts +4 -0
- package/dist/charts/bars.js +3 -1
- package/dist/charts/bars.js.map +1 -1
- package/dist/charts/bucket.js +10 -1
- package/dist/charts/bucket.js.map +1 -1
- package/dist/charts/spline.js +2 -2
- package/dist/charts/spline.js.map +1 -1
- package/dist/charts/styles.js +18 -1
- package/dist/charts/styles.js.map +1 -1
- package/dist/core/cacheKey.js +26 -3
- package/dist/core/cacheKey.js.map +1 -1
- package/dist/core/capabilities.d.ts +17 -2
- package/dist/core/capabilities.js +20 -1
- package/dist/core/capabilities.js.map +1 -1
- package/dist/core/capture.d.ts +49 -0
- package/dist/core/contract.d.ts +74 -5
- package/dist/core/contract.js +30 -1
- package/dist/core/contract.js.map +1 -1
- package/dist/core/granularity.js +1 -1
- package/dist/core/options.d.ts +168 -8
- package/dist/core/options.js +132 -4
- package/dist/core/options.js.map +1 -1
- package/dist/core/registry.d.ts +1 -0
- package/dist/core/scopeList.js +32 -0
- package/dist/core/scopeList.js.map +1 -0
- package/dist/core/scopedRead.js +101 -29
- package/dist/core/scopedRead.js.map +1 -1
- package/dist/core/serverEvent.d.ts +60 -0
- package/dist/core/serverEvent.js +12 -0
- package/dist/core/serverEvent.js.map +1 -0
- package/dist/exports/adapters/ga4.d.ts +2 -1
- package/dist/exports/adapters/ga4.js +2 -1
- package/dist/exports/adapters/native.d.ts +3 -1
- package/dist/exports/adapters/native.js +2 -1
- package/dist/exports/client.d.ts +16 -2
- package/dist/exports/client.js +13 -1
- package/dist/exports/geo.d.ts +2 -2
- package/dist/exports/next.d.ts +2 -1
- package/dist/exports/next.js +2 -1
- package/dist/exports/react.d.ts +4 -0
- package/dist/exports/react.js +5 -0
- package/dist/exports/rsc.d.ts +12 -4
- package/dist/exports/rsc.js +7 -2
- package/dist/exports/tracker.d.ts +21 -0
- package/dist/exports/tracker.js +15 -0
- package/dist/exports/types.d.ts +17 -3
- package/dist/exports/types.js +4 -2
- package/dist/fields/AnalyticsPanelClient.js +2 -2
- package/dist/fields/AnalyticsPanelField.js +1 -1
- package/dist/fields/AnalyticsStatField.js +2 -2
- package/dist/fields/config/FilterDimensionSelectField.d.ts +14 -0
- package/dist/fields/config/FilterDimensionSelectField.js +77 -0
- package/dist/fields/config/FilterDimensionSelectField.js.map +1 -0
- package/dist/fields/config/FilterOperatorSelectField.d.ts +14 -0
- package/dist/fields/config/FilterOperatorSelectField.js +75 -0
- package/dist/fields/config/FilterOperatorSelectField.js.map +1 -0
- package/dist/fields/config/MetricSelectField.d.ts +23 -0
- package/dist/fields/config/MetricSelectField.js +51 -0
- package/dist/fields/config/MetricSelectField.js.map +1 -0
- package/dist/fields/config/SourceSelectField.d.ts +14 -0
- package/dist/fields/config/SourceSelectField.js +46 -0
- package/dist/fields/config/SourceSelectField.js.map +1 -0
- package/dist/fields/config/fetchSources.d.ts +16 -0
- package/dist/fields/config/fetchSources.js +33 -0
- package/dist/fields/config/fetchSources.js.map +1 -0
- package/dist/fields/config/narrow.js +32 -0
- package/dist/fields/config/narrow.js.map +1 -0
- package/dist/fields/config/sourcesState.js +27 -0
- package/dist/fields/config/sourcesState.js.map +1 -0
- package/dist/fields/config/toSelectOption.js +27 -0
- package/dist/fields/config/toSelectOption.js.map +1 -0
- package/dist/fields/config/useAnalyticsSources.d.ts +24 -0
- package/dist/fields/config/useAnalyticsSources.js +62 -0
- package/dist/fields/config/useAnalyticsSources.js.map +1 -0
- package/dist/fields/config/useFilterCapabilities.d.ts +28 -0
- package/dist/fields/config/useFilterCapabilities.js +52 -0
- package/dist/fields/config/useFilterCapabilities.js.map +1 -0
- package/dist/fields/readForDocument.d.ts +5 -0
- package/dist/fields/readForDocument.js +22 -10
- package/dist/fields/readForDocument.js.map +1 -1
- package/dist/goals/GoalSelectField.d.ts +7 -0
- package/dist/goals/GoalSelectField.js +77 -0
- package/dist/goals/GoalSelectField.js.map +1 -0
- package/dist/goals/collection.js +254 -0
- package/dist/goals/collection.js.map +1 -0
- package/dist/goals/currency.js +14 -0
- package/dist/goals/currency.js.map +1 -0
- package/dist/goals/fetchGoals.d.ts +18 -0
- package/dist/goals/fetchGoals.js +33 -0
- package/dist/goals/fetchGoals.js.map +1 -0
- package/dist/goals/goalField.d.ts +31 -0
- package/dist/goals/goalField.js +56 -0
- package/dist/goals/goalField.js.map +1 -0
- package/dist/goals/goalsEndpoint.d.ts +1 -0
- package/dist/goals/goalsEndpoint.js +58 -0
- package/dist/goals/goalsEndpoint.js.map +1 -0
- package/dist/goals/match.js +64 -0
- package/dist/goals/match.js.map +1 -0
- package/dist/goals/resolver.d.ts +8 -0
- package/dist/goals/resolver.js +147 -0
- package/dist/goals/resolver.js.map +1 -0
- package/dist/goals/trackGoalAction.d.ts +76 -0
- package/dist/goals/trackGoalAction.js +158 -0
- package/dist/goals/trackGoalAction.js.map +1 -0
- package/dist/goals/types.d.ts +33 -0
- package/dist/goals/types.js +7 -0
- package/dist/goals/types.js.map +1 -0
- package/dist/goals/useAnalyticsGoals.d.ts +20 -0
- package/dist/goals/useAnalyticsGoals.js +67 -0
- package/dist/goals/useAnalyticsGoals.js.map +1 -0
- package/dist/index.d.ts +14 -3
- package/dist/index.js +185 -19
- package/dist/index.js.map +1 -1
- package/dist/native/collections/events.js +65 -1
- package/dist/native/collections/events.js.map +1 -1
- package/dist/native/collections/rollups.js +39 -3
- package/dist/native/collections/rollups.js.map +1 -1
- package/dist/native/eventScan.js +25 -0
- package/dist/native/eventScan.js.map +1 -0
- package/dist/native/geo/composeGeoResolvers.js +2 -2
- package/dist/native/geo/composeGeoResolvers.js.map +1 -1
- package/dist/native/geo/geoResolver.d.ts +9 -2
- package/dist/native/geo/geoResolver.js.map +1 -1
- package/dist/native/geo/maxmindResolver.js +3 -5
- package/dist/native/geo/maxmindResolver.js.map +1 -1
- package/dist/native/ingest/bots.d.ts +11 -0
- package/dist/native/ingest/bots.js +71 -0
- package/dist/native/ingest/bots.js.map +1 -0
- package/dist/native/ingest/browser.js +47 -0
- package/dist/native/ingest/browser.js.map +1 -0
- package/dist/native/ingest/clientIp.js +20 -0
- package/dist/native/ingest/clientIp.js.map +1 -0
- package/dist/native/ingest/device.js +11 -3
- package/dist/native/ingest/device.js.map +1 -1
- package/dist/native/ingest/endpoint.d.ts +1 -0
- package/dist/native/ingest/endpoint.js +107 -21
- package/dist/native/ingest/endpoint.js.map +1 -1
- package/dist/native/ingest/flushBatch.js +29 -38
- package/dist/native/ingest/flushBatch.js.map +1 -1
- package/dist/native/ingest/language.js +14 -0
- package/dist/native/ingest/language.js.map +1 -0
- package/dist/native/ingest/normalizeEvent.d.ts +30 -0
- package/dist/native/ingest/normalizeEvent.js +115 -16
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/ingest/referrer.js +48 -0
- package/dist/native/ingest/referrer.js.map +1 -0
- package/dist/native/ingest/requestHost.js +57 -0
- package/dist/native/ingest/requestHost.js.map +1 -0
- package/dist/native/ingest/resolveHostname.d.ts +29 -0
- package/dist/native/ingest/resolveHostname.js +52 -0
- package/dist/native/ingest/resolveHostname.js.map +1 -0
- package/dist/native/ingest/salt.js +27 -2
- package/dist/native/ingest/salt.js.map +1 -1
- package/dist/native/ingest/serverTrack.d.ts +20 -0
- package/dist/native/ingest/serverTrack.js +109 -0
- package/dist/native/ingest/serverTrack.js.map +1 -0
- package/dist/native/ingest/source.d.ts +11 -0
- package/dist/native/ingest/source.js +264 -16
- package/dist/native/ingest/source.js.map +1 -1
- package/dist/native/ingest/utm.js +33 -0
- package/dist/native/ingest/utm.js.map +1 -0
- package/dist/native/ingest/validate.js +26 -0
- package/dist/native/ingest/validate.js.map +1 -0
- package/dist/native/nativeAdapter.d.ts +37 -1
- package/dist/native/nativeAdapter.js +175 -22
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/query/eventAgg.js +175 -0
- package/dist/native/query/eventAgg.js.map +1 -0
- package/dist/native/retention/pruneTask.js +53 -13
- package/dist/native/retention/pruneTask.js.map +1 -1
- package/dist/native/rollupAcc.js +12 -1
- package/dist/native/rollupAcc.js.map +1 -1
- package/dist/native/rollups/bucketKey.js +15 -0
- package/dist/native/rollups/bucketKey.js.map +1 -0
- package/dist/native/rollups/bumpRollups.js +116 -0
- package/dist/native/rollups/bumpRollups.js.map +1 -0
- package/dist/native/rollups/deltas.js +86 -11
- package/dist/native/rollups/deltas.js.map +1 -1
- package/dist/native/rollups/insertManyIfNew.js +93 -0
- package/dist/native/rollups/insertManyIfNew.js.map +1 -0
- package/dist/next/captureRewrites.d.ts +42 -0
- package/dist/next/captureRewrites.js +57 -0
- package/dist/next/captureRewrites.js.map +1 -0
- package/dist/next/posthogProxyRewrites.d.ts +5 -0
- package/dist/next/posthogProxyRewrites.js +16 -21
- package/dist/next/posthogProxyRewrites.js.map +1 -1
- package/dist/plugin/documentEndpoint.js +71 -24
- package/dist/plugin/documentEndpoint.js.map +1 -1
- package/dist/plugin/errors.d.ts +31 -0
- package/dist/plugin/errors.js +79 -0
- package/dist/plugin/errors.js.map +1 -0
- package/dist/plugin/goalHint.d.ts +1 -0
- package/dist/plugin/goalHint.js +24 -0
- package/dist/plugin/goalHint.js.map +1 -0
- package/dist/plugin/paths.js +19 -1
- package/dist/plugin/paths.js.map +1 -1
- package/dist/plugin/queryEndpoint.js +124 -0
- package/dist/plugin/queryEndpoint.js.map +1 -0
- package/dist/plugin/readContextForRequest.js +73 -0
- package/dist/plugin/readContextForRequest.js.map +1 -0
- package/dist/plugin/realtimeEndpoint.js +39 -18
- package/dist/plugin/realtimeEndpoint.js.map +1 -1
- package/dist/plugin/refreshEndpoint.js +113 -0
- package/dist/plugin/refreshEndpoint.js.map +1 -0
- package/dist/plugin/runtime.d.ts +1 -0
- package/dist/plugin/runtime.js +33 -1
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/scopeChange.js +22 -0
- package/dist/plugin/scopeChange.js.map +1 -0
- package/dist/plugin/scopeFieldBoot.js +22 -0
- package/dist/plugin/scopeFieldBoot.js.map +1 -0
- package/dist/plugin/sourcesEndpoint.js +28 -0
- package/dist/plugin/sourcesEndpoint.js.map +1 -0
- package/dist/plugin/warmTask.js +0 -0
- package/dist/plugin/warmTask.js.map +1 -1
- package/dist/providers/access.js +2 -1
- package/dist/providers/access.js.map +1 -1
- package/dist/providers/collection.js +90 -29
- package/dist/providers/collection.js.map +1 -1
- package/dist/providers/factory.js +11 -3
- package/dist/providers/factory.js.map +1 -1
- package/dist/providers/resolver.js.map +1 -1
- package/dist/query/dates.js +64 -0
- package/dist/query/dates.js.map +1 -0
- package/dist/query/errors.d.ts +14 -0
- package/dist/query/fetchQuery.d.ts +79 -0
- package/dist/query/fetchQuery.js +117 -0
- package/dist/query/fetchQuery.js.map +1 -0
- package/dist/query/parse.d.ts +22 -0
- package/dist/query/parse.js +168 -0
- package/dist/query/parse.js.map +1 -0
- package/dist/query/response.d.ts +39 -0
- package/dist/query/response.js +11 -0
- package/dist/query/response.js.map +1 -0
- package/dist/react/AnalyticsProvider.d.ts +50 -0
- package/dist/react/AnalyticsProvider.js +67 -0
- package/dist/react/AnalyticsProvider.js.map +1 -0
- package/dist/react/TrackerBoot.d.ts +22 -0
- package/dist/react/TrackerBoot.js +30 -0
- package/dist/react/TrackerBoot.js.map +1 -0
- package/dist/react/useAnalytics.d.ts +20 -0
- package/dist/react/useAnalytics.js +52 -0
- package/dist/react/useAnalytics.js.map +1 -0
- package/dist/rsc/AnalyticsScripts.d.ts +30 -0
- package/dist/rsc/AnalyticsScripts.js +41 -0
- package/dist/rsc/AnalyticsScripts.js.map +1 -0
- package/dist/surfacing/cacheStore.d.ts +1 -0
- package/dist/surfacing/cacheStore.js +18 -2
- package/dist/surfacing/cacheStore.js.map +1 -1
- package/dist/surfacing/engine.d.ts +1 -0
- package/dist/surfacing/engine.js +47 -6
- package/dist/surfacing/engine.js.map +1 -1
- package/dist/surfacing/epoch.d.ts +1 -0
- package/dist/surfacing/epoch.js +72 -0
- package/dist/surfacing/epoch.js.map +1 -0
- package/dist/surfacing/queue.js +22 -6
- package/dist/surfacing/queue.js.map +1 -1
- package/dist/surfacing/rateLimiter.js +130 -0
- package/dist/surfacing/rateLimiter.js.map +1 -0
- package/dist/surfacing/retryPolicy.js +15 -0
- package/dist/surfacing/retryPolicy.js.map +1 -0
- package/dist/sync/collection.js +23 -7
- package/dist/sync/collection.js.map +1 -1
- package/dist/sync/syncTask.js +68 -33
- package/dist/sync/syncTask.js.map +1 -1
- package/dist/testing/memoryAdapter.d.ts +2 -0
- package/dist/testing/memoryAdapter.js +17 -1
- package/dist/testing/memoryAdapter.js.map +1 -1
- package/dist/timeframe/tz.js +17 -6
- package/dist/timeframe/tz.js.map +1 -1
- package/dist/tracker/autoCapture.d.ts +48 -0
- package/dist/tracker/autoCapture.js +129 -0
- package/dist/tracker/autoCapture.js.map +1 -0
- package/dist/tracker/consent.d.ts +24 -0
- package/dist/tracker/consent.js +48 -0
- package/dist/tracker/consent.js.map +1 -0
- package/dist/tracker/createTracker.d.ts +18 -0
- package/dist/tracker/createTracker.js +215 -0
- package/dist/tracker/createTracker.js.map +1 -0
- package/dist/tracker/exclusion.d.ts +28 -0
- package/dist/tracker/exclusion.js +50 -0
- package/dist/tracker/exclusion.js.map +1 -0
- package/dist/tracker/initTracker.d.ts +22 -0
- package/dist/tracker/initTracker.js +33 -0
- package/dist/tracker/initTracker.js.map +1 -0
- package/dist/tracker/loadScript.d.ts +14 -0
- package/dist/tracker/loadScript.js +41 -0
- package/dist/tracker/loadScript.js.map +1 -0
- package/dist/tracker/pageTracking.d.ts +17 -0
- package/dist/tracker/pageTracking.js +38 -0
- package/dist/tracker/pageTracking.js.map +1 -0
- package/dist/tracker/registry.d.ts +27 -0
- package/dist/tracker/registry.js +73 -0
- package/dist/tracker/registry.js.map +1 -0
- package/dist/tracker/sinks/ga4.d.ts +29 -0
- package/dist/tracker/sinks/ga4.js +30 -0
- package/dist/tracker/sinks/ga4.js.map +1 -0
- package/dist/tracker/sinks/native.d.ts +22 -0
- package/dist/tracker/sinks/native.js +56 -0
- package/dist/tracker/sinks/native.js.map +1 -0
- package/dist/tracker/sinks/plausible.d.ts +20 -0
- package/dist/tracker/sinks/plausible.js +32 -0
- package/dist/tracker/sinks/plausible.js.map +1 -0
- package/dist/tracker/sinks/posthog.d.ts +18 -0
- package/dist/tracker/sinks/posthog.js +30 -0
- package/dist/tracker/sinks/posthog.js.map +1 -0
- package/dist/tracker/sinks/umami.d.ts +18 -0
- package/dist/tracker/sinks/umami.js +23 -0
- package/dist/tracker/sinks/umami.js.map +1 -0
- package/dist/tracker/sinks/vendor.d.ts +14 -0
- package/dist/tracker/sinks/vendor.js +81 -0
- package/dist/tracker/sinks/vendor.js.map +1 -0
- package/dist/tracker/transport.js +28 -0
- package/dist/tracker/transport.js.map +1 -0
- package/dist/tracker/types.d.ts +91 -0
- package/dist/translations/ar.js +221 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +145 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +145 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +221 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +221 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +221 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +144 -0
- package/dist/translations/keys.js +145 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +221 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/metricKeys.js +8 -2
- package/dist/translations/metricKeys.js.map +1 -1
- package/dist/translations/pt.js +221 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +221 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/server.js.map +1 -1
- package/dist/translations/uk.js +221 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +221 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/view/AnalyticsNavLink.d.ts +19 -0
- package/dist/view/AnalyticsNavLink.js +40 -0
- package/dist/view/AnalyticsNavLink.js.map +1 -0
- package/dist/view/AnalyticsView.d.ts +23 -0
- package/dist/view/AnalyticsView.js +45 -0
- package/dist/view/AnalyticsView.js.map +1 -0
- package/dist/view/AnalyticsViewClient.d.ts +15 -0
- package/dist/view/AnalyticsViewClient.js +244 -0
- package/dist/view/AnalyticsViewClient.js.map +1 -0
- package/dist/view/authRedirect.js +32 -0
- package/dist/view/authRedirect.js.map +1 -0
- package/dist/view/conversionRate.js +18 -0
- package/dist/view/conversionRate.js.map +1 -0
- package/dist/view/dayRange.js +68 -0
- package/dist/view/dayRange.js.map +1 -0
- package/dist/view/gating.d.ts +1 -0
- package/dist/view/gating.js +142 -0
- package/dist/view/gating.js.map +1 -0
- package/dist/view/labels.js +76 -0
- package/dist/view/labels.js.map +1 -0
- package/dist/view/registerView.js +48 -0
- package/dist/view/registerView.js.map +1 -0
- package/dist/view/sections/Breakdowns.js +198 -0
- package/dist/view/sections/Breakdowns.js.map +1 -0
- package/dist/view/sections/EmptyStates.js +92 -0
- package/dist/view/sections/EmptyStates.js.map +1 -0
- package/dist/view/sections/FilterChips.js +46 -0
- package/dist/view/sections/FilterChips.js.map +1 -0
- package/dist/view/sections/GoalsPanel.js +82 -0
- package/dist/view/sections/GoalsPanel.js.map +1 -0
- package/dist/view/sections/OverviewCards.js +65 -0
- package/dist/view/sections/OverviewCards.js.map +1 -0
- package/dist/view/sections/RealtimeStrip.js +80 -0
- package/dist/view/sections/RealtimeStrip.js.map +1 -0
- package/dist/view/sections/Toolbar.js +207 -0
- package/dist/view/sections/Toolbar.js.map +1 -0
- package/dist/view/sections/Trend.js +125 -0
- package/dist/view/sections/Trend.js.map +1 -0
- package/dist/view/sections/styles.js +76 -0
- package/dist/view/sections/styles.js.map +1 -0
- package/dist/view/state.js +211 -0
- package/dist/view/state.js.map +1 -0
- package/dist/view/useViewQueries.js +153 -0
- package/dist/view/useViewQueries.js.map +1 -0
- package/dist/view/viewProps.d.ts +40 -0
- package/dist/view/viewProps.js +90 -0
- package/dist/view/viewProps.js.map +1 -0
- package/dist/widgets/AnalyticsBreakdownWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsBreakdownWidget.js +78 -11
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
- package/dist/widgets/AnalyticsGoalsWidget.d.ts +14 -0
- package/dist/widgets/AnalyticsGoalsWidget.js +196 -0
- package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -0
- package/dist/widgets/AnalyticsMetricWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsMetricWidget.js +63 -13
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
- package/dist/widgets/AnalyticsRealtimeWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js +45 -23
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
- package/dist/widgets/AnalyticsTrendWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsTrendWidget.js +79 -16
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
- package/dist/widgets/RealtimeCounter.d.ts +4 -0
- package/dist/widgets/RealtimeCounter.js +18 -4
- package/dist/widgets/RealtimeCounter.js.map +1 -1
- package/dist/widgets/WidgetViewLink.js +17 -0
- package/dist/widgets/WidgetViewLink.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +38 -0
- package/dist/widgets/breakdownTypes.js.map +1 -1
- package/dist/widgets/cardChrome.js +31 -7
- package/dist/widgets/cardChrome.js.map +1 -1
- package/dist/widgets/comparison.js +15 -1
- package/dist/widgets/comparison.js.map +1 -1
- package/dist/widgets/defaults.d.ts +6 -2
- package/dist/widgets/defaults.js +78 -23
- package/dist/widgets/defaults.js.map +1 -1
- package/dist/widgets/filterCaption.js +27 -0
- package/dist/widgets/filterCaption.js.map +1 -0
- package/dist/widgets/filterField.d.ts +19 -0
- package/dist/widgets/filterField.js +75 -0
- package/dist/widgets/filterField.js.map +1 -0
- package/dist/widgets/prepareWidgetRead.d.ts +7 -0
- package/dist/widgets/prepareWidgetRead.js +74 -0
- package/dist/widgets/prepareWidgetRead.js.map +1 -0
- package/dist/widgets/range.js +43 -12
- package/dist/widgets/range.js.map +1 -1
- package/dist/widgets/readForWidget.d.ts +26 -3
- package/dist/widgets/readForWidget.js +48 -44
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +34 -2
- package/dist/widgets/readForWidgetBreakdown.js +53 -49
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetGoals.d.ts +67 -0
- package/dist/widgets/readForWidgetGoals.js +140 -0
- package/dist/widgets/readForWidgetGoals.js.map +1 -0
- package/dist/widgets/readForWidgetRealtime.d.ts +8 -0
- package/dist/widgets/readForWidgetRealtime.js +23 -21
- package/dist/widgets/readForWidgetRealtime.js.map +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +26 -2
- package/dist/widgets/readForWidgetSeries.js +78 -53
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/readMeta.js +18 -0
- package/dist/widgets/readMeta.js.map +1 -0
- package/dist/widgets/realtimePoll.js +16 -1
- package/dist/widgets/realtimePoll.js.map +1 -1
- package/dist/widgets/registerWidgets.js +76 -14
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/dist/widgets/types.d.ts +15 -0
- package/dist/widgets/types.js +16 -2
- package/dist/widgets/types.js.map +1 -1
- package/dist/widgets/viewLink.d.ts +22 -0
- package/dist/widgets/viewLink.js +86 -0
- package/dist/widgets/viewLink.js.map +1 -0
- package/package.json +27 -7
- package/dist/native/rollups/applyDistinctDeltas.js +0 -9
- package/dist/native/rollups/applyDistinctDeltas.js.map +0 -1
- package/dist/native/rollups/bumpRollup.js +0 -68
- package/dist/native/rollups/bumpRollup.js.map +0 -1
- package/dist/native/rollups/insertIfNew.js +0 -19
- package/dist/native/rollups/insertIfNew.js.map +0 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
//#region src/native/geo/composeGeoResolvers.ts
|
|
2
2
|
function composeGeoResolvers(...resolvers) {
|
|
3
|
-
return async (headers) => {
|
|
3
|
+
return async (headers, opts) => {
|
|
4
4
|
for (const resolve of resolvers) {
|
|
5
|
-
const geo = await resolve(headers);
|
|
5
|
+
const geo = await resolve(headers, opts);
|
|
6
6
|
if (geo.country) return geo;
|
|
7
7
|
}
|
|
8
8
|
return {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"composeGeoResolvers.js","names":[],"sources":["../../../src/native/geo/composeGeoResolvers.ts"],"sourcesContent":["import type { Geo, GeoResolver } from './geoResolver'\n\nexport function composeGeoResolvers(...resolvers: GeoResolver[]): GeoResolver {\n\treturn async (headers: Headers): Promise<Geo> => {\n\t\tfor (const resolve of resolvers) {\n\t\t\tconst geo = await resolve(headers)\n\t\t\tif (geo.country) {\n\t\t\t\treturn geo\n\t\t\t}\n\t\t}\n\t\treturn {}\n\t}\n}\n"],"mappings":";AAEA,SAAgB,oBAAoB,GAAG,WAAuC;CAC7E,OAAO,OAAO,
|
|
1
|
+
{"version":3,"file":"composeGeoResolvers.js","names":[],"sources":["../../../src/native/geo/composeGeoResolvers.ts"],"sourcesContent":["import type { Geo, GeoResolver, GeoResolverOptions } from './geoResolver'\n\nexport function composeGeoResolvers(...resolvers: GeoResolver[]): GeoResolver {\n\treturn async (headers: Headers, opts?: GeoResolverOptions): Promise<Geo> => {\n\t\tfor (const resolve of resolvers) {\n\t\t\tconst geo = await resolve(headers, opts)\n\t\t\tif (geo.country) {\n\t\t\t\treturn geo\n\t\t\t}\n\t\t}\n\t\treturn {}\n\t}\n}\n"],"mappings":";AAEA,SAAgB,oBAAoB,GAAG,WAAuC;CAC7E,OAAO,OAAO,SAAkB,SAA4C;EAC3E,KAAK,MAAM,WAAW,WAAW;GAChC,MAAM,MAAM,MAAM,QAAQ,SAAS,IAAI;GACvC,IAAI,IAAI,SACP,OAAO;EAET;EACA,OAAO,CAAC;CACT;AACD"}
|
|
@@ -4,9 +4,16 @@ interface Geo {
|
|
|
4
4
|
region?: string;
|
|
5
5
|
city?: string;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
interface GeoResolverOptions {
|
|
8
|
+
trustedProxyHops?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* `opts` carries the install's `trustedProxyHops` for a resolver that reads the client address
|
|
12
|
+
* from the forwarded chain. A resolver that only reads platform headers can ignore it.
|
|
13
|
+
*/
|
|
14
|
+
type GeoResolver = (headers: Headers, opts?: GeoResolverOptions) => Geo | Promise<Geo>;
|
|
8
15
|
declare const platformHeaderResolver: GeoResolver;
|
|
9
16
|
declare const noopResolver: GeoResolver;
|
|
10
17
|
//#endregion
|
|
11
|
-
export { Geo, GeoResolver, noopResolver, platformHeaderResolver };
|
|
18
|
+
export { Geo, GeoResolver, GeoResolverOptions, noopResolver, platformHeaderResolver };
|
|
12
19
|
//# sourceMappingURL=geoResolver.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"geoResolver.js","names":[],"sources":["../../../src/native/geo/geoResolver.ts"],"sourcesContent":["export interface Geo {\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n}\n\nexport type GeoResolver = (headers: Headers) => Geo | Promise<Geo>\n\nexport const platformHeaderResolver: GeoResolver = (headers) => ({\n\tcountry: headers.get('x-vercel-ip-country') ?? headers.get('cf-ipcountry') ?? undefined,\n\tregion: headers.get('x-vercel-ip-country-region') ?? undefined,\n\tcity: headers.get('x-vercel-ip-city') ?? undefined,\n})\n\nexport const noopResolver: GeoResolver = () => ({})\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"geoResolver.js","names":[],"sources":["../../../src/native/geo/geoResolver.ts"],"sourcesContent":["export interface Geo {\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n}\n\nexport interface GeoResolverOptions {\n\ttrustedProxyHops?: number\n}\n\n/**\n * `opts` carries the install's `trustedProxyHops` for a resolver that reads the client address\n * from the forwarded chain. A resolver that only reads platform headers can ignore it.\n */\nexport type GeoResolver = (headers: Headers, opts?: GeoResolverOptions) => Geo | Promise<Geo>\n\nexport const platformHeaderResolver: GeoResolver = (headers) => ({\n\tcountry: headers.get('x-vercel-ip-country') ?? headers.get('cf-ipcountry') ?? undefined,\n\tregion: headers.get('x-vercel-ip-country-region') ?? undefined,\n\tcity: headers.get('x-vercel-ip-city') ?? undefined,\n})\n\nexport const noopResolver: GeoResolver = () => ({})\n"],"mappings":";AAgBA,MAAa,0BAAuC,aAAa;CAChE,SAAS,QAAQ,IAAI,qBAAqB,KAAK,QAAQ,IAAI,cAAc,KAAK,KAAA;CAC9E,QAAQ,QAAQ,IAAI,4BAA4B,KAAK,KAAA;CACrD,MAAM,QAAQ,IAAI,kBAAkB,KAAK,KAAA;AAC1C;AAEA,MAAa,sBAAmC,CAAC"}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
+
import { clientIpFromHeaders } from "../ingest/clientIp.js";
|
|
1
2
|
//#region src/native/geo/maxmindResolver.ts
|
|
2
|
-
const clientIp = (headers) => {
|
|
3
|
-
return headers.get("x-forwarded-for")?.split(",")[0]?.trim() || headers.get("x-real-ip") || null;
|
|
4
|
-
};
|
|
5
3
|
/**
|
|
6
4
|
* Optional MaxMind GeoLite2 resolver. The mmdb and the maxmind lib are loaded
|
|
7
5
|
* lazily on first use. Any failure (lib not installed, file missing/unreadable,
|
|
@@ -25,8 +23,8 @@ function maxmindResolver(options) {
|
|
|
25
23
|
})();
|
|
26
24
|
return readerPromise;
|
|
27
25
|
};
|
|
28
|
-
return async (headers) => {
|
|
29
|
-
const ip =
|
|
26
|
+
return async (headers, opts) => {
|
|
27
|
+
const ip = clientIpFromHeaders(headers, { trustedProxyHops: opts?.trustedProxyHops });
|
|
30
28
|
if (!ip) return {};
|
|
31
29
|
let reader;
|
|
32
30
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maxmindResolver.js","names":[],"sources":["../../../src/native/geo/maxmindResolver.ts"],"sourcesContent":["import type { CityResponse, Reader } from 'maxmind'\nimport type { Geo, GeoResolver } from './geoResolver'\n\nexport interface MaxmindOptions {\n\tdbPath: string\n}\n\
|
|
1
|
+
{"version":3,"file":"maxmindResolver.js","names":[],"sources":["../../../src/native/geo/maxmindResolver.ts"],"sourcesContent":["import type { CityResponse, Reader } from 'maxmind'\nimport { clientIpFromHeaders } from '../ingest/clientIp'\nimport type { Geo, GeoResolver, GeoResolverOptions } from './geoResolver'\n\nexport interface MaxmindOptions {\n\tdbPath: string\n}\n\n/**\n * Optional MaxMind GeoLite2 resolver. The mmdb and the maxmind lib are loaded\n * lazily on first use. Any failure (lib not installed, file missing/unreadable,\n * lookup error) degrades to empty geo after a single warning, so a missing or\n * unconfigured database never breaks ingestion.\n */\nexport function maxmindResolver(options: MaxmindOptions): GeoResolver {\n\tlet readerPromise: Promise<Reader<CityResponse>> | undefined\n\tlet warned = false\n\n\tconst warn = (detail: unknown): null => {\n\t\tif (!warned) {\n\t\t\twarned = true\n\t\t\tconsole.warn(\n\t\t\t\t`[analytics] MaxMind geo unavailable for \"${options.dbPath}\"; geo will be empty.`,\n\t\t\t\tdetail instanceof Error ? detail.message : detail\n\t\t\t)\n\t\t}\n\t\treturn null\n\t}\n\n\tconst getReader = (): Promise<Reader<CityResponse>> => {\n\t\tif (!readerPromise) {\n\t\t\treaderPromise = (async () => {\n\t\t\t\tconst { open } = await import('maxmind')\n\t\t\t\treturn open<CityResponse>(options.dbPath)\n\t\t\t})()\n\t\t}\n\t\treturn readerPromise\n\t}\n\n\treturn async (headers: Headers, opts?: GeoResolverOptions): Promise<Geo> => {\n\t\tconst ip = clientIpFromHeaders(headers, { trustedProxyHops: opts?.trustedProxyHops })\n\t\tif (!ip) {\n\t\t\treturn {}\n\t\t}\n\t\tlet reader: Reader<CityResponse> | null\n\t\ttry {\n\t\t\treader = await getReader()\n\t\t} catch (err) {\n\t\t\twarn(err)\n\t\t\treturn {}\n\t\t}\n\t\ttry {\n\t\t\tconst r = reader.get(ip)\n\t\t\tif (!r) {\n\t\t\t\treturn {}\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tcountry: r.country?.iso_code ?? undefined,\n\t\t\t\tregion: r.subdivisions?.[0]?.iso_code ?? undefined,\n\t\t\t\tcity: r.city?.names.en ?? undefined,\n\t\t\t}\n\t\t} catch (err) {\n\t\t\twarn(err)\n\t\t\treturn {}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;AAcA,SAAgB,gBAAgB,SAAsC;CACrE,IAAI;CACJ,IAAI,SAAS;CAEb,MAAM,QAAQ,WAA0B;EACvC,IAAI,CAAC,QAAQ;GACZ,SAAS;GACT,QAAQ,KACP,4CAA4C,QAAQ,OAAO,wBAC3D,kBAAkB,QAAQ,OAAO,UAAU,MAC5C;EACD;EACA,OAAO;CACR;CAEA,MAAM,kBAAiD;EACtD,IAAI,CAAC,eACJ,iBAAiB,YAAY;GAC5B,MAAM,EAAE,SAAS,MAAM,OAAO;GAC9B,OAAO,KAAmB,QAAQ,MAAM;EACzC,GAAG;EAEJ,OAAO;CACR;CAEA,OAAO,OAAO,SAAkB,SAA4C;EAC3E,MAAM,KAAK,oBAAoB,SAAS,EAAE,kBAAkB,MAAM,iBAAiB,CAAC;EACpF,IAAI,CAAC,IACJ,OAAO,CAAC;EAET,IAAI;EACJ,IAAI;GACH,SAAS,MAAM,UAAU;EAC1B,SAAS,KAAK;GACb,KAAK,GAAG;GACR,OAAO,CAAC;EACT;EACA,IAAI;GACH,MAAM,IAAI,OAAO,IAAI,EAAE;GACvB,IAAI,CAAC,GACJ,OAAO,CAAC;GAET,OAAO;IACN,SAAS,EAAE,SAAS,YAAY,KAAA;IAChC,QAAQ,EAAE,eAAe,IAAI,YAAY,KAAA;IACzC,MAAM,EAAE,MAAM,MAAM,MAAM,KAAA;GAC3B;EACD,SAAS,KAAK;GACb,KAAK,GAAG;GACR,OAAO,CAAC;EACT;CACD;AACD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/native/ingest/bots.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Whether a user agent belongs to automated traffic. An agent that says nothing counts as
|
|
4
|
+
* automated (every real browser sends one); the synthetic agent of a server event never does,
|
|
5
|
+
* since that call is trusted host code rather than a request off the internet.
|
|
6
|
+
*/
|
|
7
|
+
declare const isBot: (ua: string) => boolean;
|
|
8
|
+
type BotFilter = (ua: string) => boolean;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { BotFilter, isBot };
|
|
11
|
+
//# sourceMappingURL=bots.d.ts.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import "./device.js";
|
|
2
|
+
//#region src/native/ingest/bots.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lowercase substrings of the user agents automated traffic announces itself with: crawlers
|
|
5
|
+
* and their preview fetchers, uptime and performance monitors, headless browsers and the
|
|
6
|
+
* default agents of HTTP clients. Deliberately small and hand-held rather than a vendor list,
|
|
7
|
+
* so every entry can be justified against real agents and none of them can match a browser.
|
|
8
|
+
*/
|
|
9
|
+
const BOT_MARKERS = new Set([
|
|
10
|
+
"bot",
|
|
11
|
+
"crawl",
|
|
12
|
+
"spider",
|
|
13
|
+
"slurp",
|
|
14
|
+
"headless",
|
|
15
|
+
"lighthouse",
|
|
16
|
+
"pagespeed",
|
|
17
|
+
"gtmetrix",
|
|
18
|
+
"pingdom",
|
|
19
|
+
"statuscake",
|
|
20
|
+
"datadog",
|
|
21
|
+
"newrelic",
|
|
22
|
+
"facebookexternalhit",
|
|
23
|
+
"whatsapp",
|
|
24
|
+
"embedly",
|
|
25
|
+
"phantomjs",
|
|
26
|
+
"puppeteer",
|
|
27
|
+
"playwright",
|
|
28
|
+
"selenium",
|
|
29
|
+
"python-requests",
|
|
30
|
+
"curl/",
|
|
31
|
+
"wget/",
|
|
32
|
+
"go-http-client",
|
|
33
|
+
"okhttp",
|
|
34
|
+
"axios/",
|
|
35
|
+
"node-fetch",
|
|
36
|
+
"java/",
|
|
37
|
+
"libwww",
|
|
38
|
+
"httpclient"
|
|
39
|
+
]);
|
|
40
|
+
/**
|
|
41
|
+
* Device tokens that contain a marker and belong to a real phone. The agent is scanned with
|
|
42
|
+
* them removed, because `bot` has to keep matching mid-word (Googlebot, AhrefsBot) and would
|
|
43
|
+
* otherwise count every Cubot handset as a crawler.
|
|
44
|
+
*/
|
|
45
|
+
const NOT_BOT_TOKENS = ["cubot"];
|
|
46
|
+
/**
|
|
47
|
+
* Whether a user agent belongs to automated traffic. An agent that says nothing counts as
|
|
48
|
+
* automated (every real browser sends one); the synthetic agent of a server event never does,
|
|
49
|
+
* since that call is trusted host code rather than a request off the internet.
|
|
50
|
+
*/
|
|
51
|
+
const isBot = (ua) => {
|
|
52
|
+
if (ua === "analytics-server") return false;
|
|
53
|
+
let agent = ua.trim().toLowerCase();
|
|
54
|
+
if (agent === "") return true;
|
|
55
|
+
for (const token of NOT_BOT_TOKENS) agent = agent.replaceAll(token, "");
|
|
56
|
+
for (const marker of BOT_MARKERS) if (agent.includes(marker)) return true;
|
|
57
|
+
return false;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The `filterBots` option as one predicate. A host filter that throws throws through to the
|
|
61
|
+
* handler, which keeps the event and says so once: swallowing it here would leave a broken
|
|
62
|
+
* predicate silent for the life of the process.
|
|
63
|
+
*/
|
|
64
|
+
const resolveBotFilter = (option) => {
|
|
65
|
+
if (option === false) return () => false;
|
|
66
|
+
return typeof option === "function" ? option : isBot;
|
|
67
|
+
};
|
|
68
|
+
//#endregion
|
|
69
|
+
export { isBot, resolveBotFilter };
|
|
70
|
+
|
|
71
|
+
//# sourceMappingURL=bots.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bots.js","names":[],"sources":["../../../src/native/ingest/bots.ts"],"sourcesContent":["import { SERVER_USER_AGENT } from './device'\n\n/**\n * Lowercase substrings of the user agents automated traffic announces itself with: crawlers\n * and their preview fetchers, uptime and performance monitors, headless browsers and the\n * default agents of HTTP clients. Deliberately small and hand-held rather than a vendor list,\n * so every entry can be justified against real agents and none of them can match a browser.\n */\nexport const BOT_MARKERS: ReadonlySet<string> = new Set([\n\t'bot',\n\t'crawl',\n\t'spider',\n\t'slurp',\n\t'headless',\n\t'lighthouse',\n\t'pagespeed',\n\t'gtmetrix',\n\t'pingdom',\n\t'statuscake',\n\t'datadog',\n\t'newrelic',\n\t'facebookexternalhit',\n\t'whatsapp',\n\t'embedly',\n\t'phantomjs',\n\t'puppeteer',\n\t'playwright',\n\t'selenium',\n\t'python-requests',\n\t'curl/',\n\t'wget/',\n\t'go-http-client',\n\t'okhttp',\n\t'axios/',\n\t'node-fetch',\n\t'java/',\n\t'libwww',\n\t'httpclient',\n])\n\n/**\n * Device tokens that contain a marker and belong to a real phone. The agent is scanned with\n * them removed, because `bot` has to keep matching mid-word (Googlebot, AhrefsBot) and would\n * otherwise count every Cubot handset as a crawler.\n */\nconst NOT_BOT_TOKENS: readonly string[] = ['cubot']\n\n/**\n * Whether a user agent belongs to automated traffic. An agent that says nothing counts as\n * automated (every real browser sends one); the synthetic agent of a server event never does,\n * since that call is trusted host code rather than a request off the internet.\n */\nexport const isBot = (ua: string): boolean => {\n\tif (ua === SERVER_USER_AGENT) {\n\t\treturn false\n\t}\n\tlet agent = ua.trim().toLowerCase()\n\tif (agent === '') {\n\t\treturn true\n\t}\n\tfor (const token of NOT_BOT_TOKENS) {\n\t\tagent = agent.replaceAll(token, '')\n\t}\n\tfor (const marker of BOT_MARKERS) {\n\t\tif (agent.includes(marker)) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nexport type BotFilter = (ua: string) => boolean\n\n/**\n * The `filterBots` option as one predicate. A host filter that throws throws through to the\n * handler, which keeps the event and says so once: swallowing it here would leave a broken\n * predicate silent for the life of the process.\n */\nexport const resolveBotFilter = (option: boolean | BotFilter | undefined): BotFilter => {\n\tif (option === false) {\n\t\treturn () => false\n\t}\n\treturn typeof option === 'function' ? option : isBot\n}\n"],"mappings":";;;;;;;;AAQA,MAAa,cAAmC,IAAI,IAAI;CACvD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,CAAC;;;;;;AAOD,MAAM,iBAAoC,CAAC,OAAO;;;;;;AAOlD,MAAa,SAAS,OAAwB;CAC7C,IAAI,OAAA,oBACH,OAAO;CAER,IAAI,QAAQ,GAAG,KAAK,EAAE,YAAY;CAClC,IAAI,UAAU,IACb,OAAO;CAER,KAAK,MAAM,SAAS,gBACnB,QAAQ,MAAM,WAAW,OAAO,EAAE;CAEnC,KAAK,MAAM,UAAU,aACpB,IAAI,MAAM,SAAS,MAAM,GACxB,OAAO;CAGT,OAAO;AACR;;;;;;AASA,MAAa,oBAAoB,WAAuD;CACvF,IAAI,WAAW,OACd,aAAa;CAEd,OAAO,OAAO,WAAW,aAAa,SAAS;AAChD"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "./device.js";
|
|
2
|
+
//#region src/native/ingest/browser.ts
|
|
3
|
+
/**
|
|
4
|
+
* Order is the whole algorithm: every Chromium fork still calls itself Chrome, and every iOS
|
|
5
|
+
* browser still calls itself Safari, so the fork has to be recognized before the engine it
|
|
6
|
+
* borrowed the token from.
|
|
7
|
+
*/
|
|
8
|
+
const BROWSERS = [
|
|
9
|
+
[/edg(?:a|ios|e)?\//, "edge"],
|
|
10
|
+
[/\bopr\/|\bopt\/|\bopera\b/, "opera"],
|
|
11
|
+
[/samsungbrowser/, "samsung"],
|
|
12
|
+
[/firefox\/|fxios\//, "firefox"],
|
|
13
|
+
[/chrome\/|crios\/|chromium\//, "chrome"],
|
|
14
|
+
[/safari\//, "safari"]
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* `CrOS` and `Android` both sit inside an `X11`/`Linux` UA, so they are matched ahead of it.
|
|
18
|
+
* `cros` needs its word boundaries: it is also a substring of `Microsoft`.
|
|
19
|
+
*/
|
|
20
|
+
const PLATFORMS = [
|
|
21
|
+
[/iphone|ipad|ipod/, "ios"],
|
|
22
|
+
[/android/, "android"],
|
|
23
|
+
[/\bcros\b/, "chromeos"],
|
|
24
|
+
[/windows|win64|win32/, "windows"],
|
|
25
|
+
[/macintosh|mac os x/, "macos"],
|
|
26
|
+
[/linux|x11/, "linux"]
|
|
27
|
+
];
|
|
28
|
+
const lookup = (table, ua, fallback) => {
|
|
29
|
+
if (ua === "analytics-server") return;
|
|
30
|
+
const s = ua.toLowerCase();
|
|
31
|
+
return table.find(([pattern]) => pattern.test(s))?.[1] ?? fallback;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Coarse browser family from a user-agent string, `other` for anything unrecognized (a bot,
|
|
35
|
+
* a crawler, an HTTP client), and undefined when the agent is the synthetic server one so
|
|
36
|
+
* server events abstain from the browser breakdown rather than skewing it.
|
|
37
|
+
*/
|
|
38
|
+
const classifyBrowser = (ua) => lookup(BROWSERS, ua, "other");
|
|
39
|
+
/**
|
|
40
|
+
* Operating system from a user-agent's platform tokens, on the same terms as
|
|
41
|
+
* `classifyBrowser`: `other` when nothing matches, undefined for the server agent.
|
|
42
|
+
*/
|
|
43
|
+
const classifyOs = (ua) => lookup(PLATFORMS, ua, "other");
|
|
44
|
+
//#endregion
|
|
45
|
+
export { classifyBrowser, classifyOs };
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","names":[],"sources":["../../../src/native/ingest/browser.ts"],"sourcesContent":["import { SERVER_USER_AGENT } from './device'\n\nexport type BrowserName = 'chrome' | 'safari' | 'firefox' | 'edge' | 'opera' | 'samsung' | 'other'\n\nexport type OsName = 'ios' | 'android' | 'windows' | 'macos' | 'linux' | 'chromeos' | 'other'\n\n/**\n * Order is the whole algorithm: every Chromium fork still calls itself Chrome, and every iOS\n * browser still calls itself Safari, so the fork has to be recognized before the engine it\n * borrowed the token from.\n */\nconst BROWSERS: ReadonlyArray<[RegExp, BrowserName]> = [\n\t[/edg(?:a|ios|e)?\\//, 'edge'],\n\t[/\\bopr\\/|\\bopt\\/|\\bopera\\b/, 'opera'],\n\t[/samsungbrowser/, 'samsung'],\n\t[/firefox\\/|fxios\\//, 'firefox'],\n\t[/chrome\\/|crios\\/|chromium\\//, 'chrome'],\n\t[/safari\\//, 'safari'],\n]\n\n/**\n * `CrOS` and `Android` both sit inside an `X11`/`Linux` UA, so they are matched ahead of it.\n * `cros` needs its word boundaries: it is also a substring of `Microsoft`.\n */\nconst PLATFORMS: ReadonlyArray<[RegExp, OsName]> = [\n\t[/iphone|ipad|ipod/, 'ios'],\n\t[/android/, 'android'],\n\t[/\\bcros\\b/, 'chromeos'],\n\t[/windows|win64|win32/, 'windows'],\n\t[/macintosh|mac os x/, 'macos'],\n\t[/linux|x11/, 'linux'],\n]\n\nconst lookup = <T>(table: ReadonlyArray<[RegExp, T]>, ua: string, fallback: T): T | undefined => {\n\tif (ua === SERVER_USER_AGENT) {\n\t\treturn undefined\n\t}\n\tconst s = ua.toLowerCase()\n\treturn table.find(([pattern]) => pattern.test(s))?.[1] ?? fallback\n}\n\n/**\n * Coarse browser family from a user-agent string, `other` for anything unrecognized (a bot,\n * a crawler, an HTTP client), and undefined when the agent is the synthetic server one so\n * server events abstain from the browser breakdown rather than skewing it.\n */\nexport const classifyBrowser = (ua: string): BrowserName | undefined =>\n\tlookup(BROWSERS, ua, 'other')\n\n/**\n * Operating system from a user-agent's platform tokens, on the same terms as\n * `classifyBrowser`: `other` when nothing matches, undefined for the server agent.\n */\nexport const classifyOs = (ua: string): OsName | undefined => lookup(PLATFORMS, ua, 'other')\n"],"mappings":";;;;;;;AAWA,MAAM,WAAiD;CACtD,CAAC,qBAAqB,MAAM;CAC5B,CAAC,6BAA6B,OAAO;CACrC,CAAC,kBAAkB,SAAS;CAC5B,CAAC,qBAAqB,SAAS;CAC/B,CAAC,+BAA+B,QAAQ;CACxC,CAAC,YAAY,QAAQ;AACtB;;;;;AAMA,MAAM,YAA6C;CAClD,CAAC,oBAAoB,KAAK;CAC1B,CAAC,WAAW,SAAS;CACrB,CAAC,YAAY,UAAU;CACvB,CAAC,uBAAuB,SAAS;CACjC,CAAC,sBAAsB,OAAO;CAC9B,CAAC,aAAa,OAAO;AACtB;AAEA,MAAM,UAAa,OAAmC,IAAY,aAA+B;CAChG,IAAI,OAAA,oBACH;CAED,MAAM,IAAI,GAAG,YAAY;CACzB,OAAO,MAAM,MAAM,CAAC,aAAa,QAAQ,KAAK,CAAC,CAAC,IAAI,MAAM;AAC3D;;;;;;AAOA,MAAa,mBAAmB,OAC/B,OAAO,UAAU,IAAI,OAAO;;;;;AAM7B,MAAa,cAAc,OAAmC,OAAO,WAAW,IAAI,OAAO"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/native/ingest/clientIp.ts
|
|
2
|
+
/**
|
|
3
|
+
* The originating client IP a proxy chain reported: the first hop of `x-forwarded-for`, else
|
|
4
|
+
* `x-real-ip`. Null when the request carries neither, so callers can omit the value instead of
|
|
5
|
+
* forwarding a fabricated one.
|
|
6
|
+
*
|
|
7
|
+
* With `trustedProxyHops >= 1` the forwarded chain is the only source, counted from the right,
|
|
8
|
+
* and a chain shorter than the count answers null rather than falling back to a header the
|
|
9
|
+
* client could have written.
|
|
10
|
+
*/
|
|
11
|
+
const clientIpFromHeaders = (headers, opts = {}) => {
|
|
12
|
+
const chain = (headers.get("x-forwarded-for") ?? "").split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
13
|
+
const hops = opts.trustedProxyHops ?? 0;
|
|
14
|
+
if (hops >= 1) return chain[chain.length - hops] ?? null;
|
|
15
|
+
return chain[0] ?? (headers.get("x-real-ip")?.trim() || null);
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { clientIpFromHeaders };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=clientIp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clientIp.js","names":[],"sources":["../../../src/native/ingest/clientIp.ts"],"sourcesContent":["export interface ClientIpOptions {\n\t/**\n\t * Trusted proxies in front of the app, counted from the right of `x-forwarded-for`: the\n\t * address the outermost trusted proxy saw. Unset reads the leftmost entry, which is correct\n\t * for honest traffic and is whatever the client wrote for the rest, so an install behind a\n\t * proxy it controls should set it. There is no default of 1: an install behind a CDN plus a\n\t * proxy would then read the CDN's address for every visitor, collapsing geo and the visitor\n\t * hash onto one value, which is worse than a spoofable one.\n\t */\n\ttrustedProxyHops?: number\n}\n\n/**\n * The originating client IP a proxy chain reported: the first hop of `x-forwarded-for`, else\n * `x-real-ip`. Null when the request carries neither, so callers can omit the value instead of\n * forwarding a fabricated one.\n *\n * With `trustedProxyHops >= 1` the forwarded chain is the only source, counted from the right,\n * and a chain shorter than the count answers null rather than falling back to a header the\n * client could have written.\n */\nexport const clientIpFromHeaders = (\n\theaders: Headers,\n\topts: ClientIpOptions = {}\n): string | null => {\n\tconst chain = (headers.get('x-forwarded-for') ?? '')\n\t\t.split(',')\n\t\t.map((entry) => entry.trim())\n\t\t.filter(Boolean)\n\tconst hops = opts.trustedProxyHops ?? 0\n\tif (hops >= 1) {\n\t\treturn chain[chain.length - hops] ?? null\n\t}\n\treturn chain[0] ?? (headers.get('x-real-ip')?.trim() || null)\n}\n"],"mappings":";;;;;;;;;;AAqBA,MAAa,uBACZ,SACA,OAAwB,CAAC,MACN;CACnB,MAAM,SAAS,QAAQ,IAAI,iBAAiB,KAAK,IAC/C,MAAM,GAAG,EACT,KAAK,UAAU,MAAM,KAAK,CAAC,EAC3B,OAAO,OAAO;CAChB,MAAM,OAAO,KAAK,oBAAoB;CACtC,IAAI,QAAQ,GACX,OAAO,MAAM,MAAM,SAAS,SAAS;CAEtC,OAAO,MAAM,OAAO,QAAQ,IAAI,WAAW,GAAG,KAAK,KAAK;AACzD"}
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
//#region src/native/ingest/device.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* The user agent server-side tracking stamps when the event has no browser request behind
|
|
4
|
+
* it. There is no device to report, so events carrying it abstain from the device breakdown
|
|
5
|
+
* rather than piling into `desktop` and skewing it.
|
|
6
|
+
*/
|
|
7
|
+
const SERVER_USER_AGENT = "analytics-server";
|
|
8
|
+
/**
|
|
9
|
+
* Coarse device class from a user-agent string, or undefined when the agent is not a device
|
|
10
|
+
* at all. Tablets are checked before phones because an Android tablet UA lacks the `Mobile`
|
|
11
|
+
* token an Android phone carries.
|
|
5
12
|
*/
|
|
6
13
|
const classifyDevice = (ua) => {
|
|
14
|
+
if (ua === "analytics-server") return;
|
|
7
15
|
const s = ua.toLowerCase();
|
|
8
16
|
if (/ipad|tablet|playbook|silk|kindle/.test(s) || /android/.test(s) && !/mobi/.test(s)) return "tablet";
|
|
9
17
|
if (/mobi|iphone|ipod|android|blackberry|iemobile|opera mini|windows phone/.test(s)) return "mobile";
|
|
10
18
|
return "desktop";
|
|
11
19
|
};
|
|
12
20
|
//#endregion
|
|
13
|
-
export { classifyDevice };
|
|
21
|
+
export { SERVER_USER_AGENT, classifyDevice };
|
|
14
22
|
|
|
15
23
|
//# sourceMappingURL=device.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"device.js","names":[],"sources":["../../../src/native/ingest/device.ts"],"sourcesContent":["export type DeviceType = 'desktop' | 'mobile' | 'tablet'\n\n/**\n * Coarse device class from a user-agent string. Tablets are checked before phones
|
|
1
|
+
{"version":3,"file":"device.js","names":[],"sources":["../../../src/native/ingest/device.ts"],"sourcesContent":["export type DeviceType = 'desktop' | 'mobile' | 'tablet'\n\n/**\n * The user agent server-side tracking stamps when the event has no browser request behind\n * it. There is no device to report, so events carrying it abstain from the device breakdown\n * rather than piling into `desktop` and skewing it.\n */\nexport const SERVER_USER_AGENT = 'analytics-server'\n\n/**\n * Coarse device class from a user-agent string, or undefined when the agent is not a device\n * at all. Tablets are checked before phones because an Android tablet UA lacks the `Mobile`\n * token an Android phone carries.\n */\nexport const classifyDevice = (ua: string): DeviceType | undefined => {\n\tif (ua === SERVER_USER_AGENT) {\n\t\treturn undefined\n\t}\n\tconst s = ua.toLowerCase()\n\tif (/ipad|tablet|playbook|silk|kindle/.test(s) || (/android/.test(s) && !/mobi/.test(s))) {\n\t\treturn 'tablet'\n\t}\n\tif (/mobi|iphone|ipod|android|blackberry|iemobile|opera mini|windows phone/.test(s)) {\n\t\treturn 'mobile'\n\t}\n\treturn 'desktop'\n}\n"],"mappings":";;;;;;AAOA,MAAa,oBAAoB;;;;;;AAOjC,MAAa,kBAAkB,OAAuC;CACrE,IAAI,OAAA,oBACH;CAED,MAAM,IAAI,GAAG,YAAY;CACzB,IAAI,mCAAmC,KAAK,CAAC,KAAM,UAAU,KAAK,CAAC,KAAK,CAAC,OAAO,KAAK,CAAC,GACrF,OAAO;CAER,IAAI,wEAAwE,KAAK,CAAC,GACjF,OAAO;CAER,OAAO;AACR"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { PayloadRequest } from "payload";
|
|
@@ -1,28 +1,114 @@
|
|
|
1
|
+
import { readCappedBody } from "../../capture/requestBody.js";
|
|
2
|
+
import { analyticsError, errorResponse } from "../../plugin/errors.js";
|
|
1
3
|
import { flushBatch } from "./flushBatch.js";
|
|
2
4
|
import { normalizeEvent } from "./normalizeEvent.js";
|
|
3
5
|
import { dailySalt } from "./salt.js";
|
|
6
|
+
import { rawEventError } from "./validate.js";
|
|
7
|
+
import { isBot } from "./bots.js";
|
|
8
|
+
import { resolveEventHostname } from "./resolveHostname.js";
|
|
4
9
|
//#region src/native/ingest/endpoint.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
/**
|
|
11
|
+
* What an accepted beacon answers, and what a dropped one answers too: same status, same body,
|
|
12
|
+
* no header of its own and nothing logged per event, so the endpoint never tells a prober which
|
|
13
|
+
* hosts or tenants exist.
|
|
14
|
+
*/
|
|
15
|
+
const accepted = () => Response.json({ ok: true }, { status: 202 });
|
|
16
|
+
const REQUEST_HOSTNAME = { kind: "request" };
|
|
17
|
+
/**
|
|
18
|
+
* What an operator has to hear about once, and the line that tells them where to look. A
|
|
19
|
+
* misconfigured resolver, a broken filter or a header-stripping proxy otherwise costs an
|
|
20
|
+
* install its traffic in silence, since the response cannot say any of it.
|
|
21
|
+
*/
|
|
22
|
+
const WARN_REASONS = {
|
|
23
|
+
"no scope resolved": "analytics: ingest is dropping events because scopeResolver answered no scope for them. Check that resolver, and list any platform domain in platformHostnames so it ingests under the null scope. Logged once per process.",
|
|
24
|
+
"hostname refused": "analytics: ingest is dropping events because the hostname option refused the request host. Check that option, and platformHostnames for a platform domain. Logged once per process.",
|
|
25
|
+
"bot filter threw": "analytics: the filterBots function threw, so the event was kept. Check that function; every beacon counts while it throws. Logged once per process.",
|
|
26
|
+
"no user agent": "analytics: a beacon arrived with no user agent and was not recorded: an intermediary may be stripping the header. Logged once per process."
|
|
27
|
+
};
|
|
28
|
+
/** One event, not a session replay: far above any legitimate payload, far below a DoS. */
|
|
29
|
+
const MAX_INGEST_BODY_BYTES = 64 * 1024;
|
|
30
|
+
/**
|
|
31
|
+
* Parses the buffered body, answering undefined for anything the validator could not judge:
|
|
32
|
+
* a garbage POST is a 400 like any other bad payload rather than a thrown parse error that
|
|
33
|
+
* escapes into Payload's routeError as a logged 500.
|
|
34
|
+
*/
|
|
35
|
+
const parseBody = (bytes) => {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(new TextDecoder().decode(bytes));
|
|
38
|
+
return typeof parsed === "object" && parsed !== null ? parsed : void 0;
|
|
39
|
+
} catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* The refusal for the first field an event failed on: naming it costs nothing and saves a
|
|
45
|
+
* tracker author guessing which one of four the endpoint refused. A body that is not an
|
|
46
|
+
* object at all fails the very check a typeless event does, so it names `type`.
|
|
47
|
+
*/
|
|
48
|
+
const invalidField = (param) => errorResponse(400, analyticsError("invalid_param", `analytics: ${param} is missing or invalid`, param));
|
|
49
|
+
const makeIngestHandler = ({ geoResolver, getBuffer = () => null, resolvers = {}, attribution = {}, filterBots = isBot }) => {
|
|
50
|
+
const warned = /* @__PURE__ */ new Set();
|
|
51
|
+
const warnOnce = (req, reason) => {
|
|
52
|
+
if (!warned.has(reason)) {
|
|
53
|
+
warned.add(reason);
|
|
54
|
+
req.payload.logger?.warn(WARN_REASONS[reason]);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const drop = (req, reason) => {
|
|
58
|
+
warnOnce(req, reason);
|
|
59
|
+
return accepted();
|
|
60
|
+
};
|
|
61
|
+
return async (req) => {
|
|
62
|
+
const userAgent = req.headers.get("user-agent") ?? "";
|
|
63
|
+
let automated = false;
|
|
64
|
+
try {
|
|
65
|
+
automated = filterBots(userAgent);
|
|
66
|
+
} catch {
|
|
67
|
+
warnOnce(req, "bot filter threw");
|
|
68
|
+
}
|
|
69
|
+
if (automated) {
|
|
70
|
+
if (userAgent.trim() === "") warnOnce(req, "no user agent");
|
|
71
|
+
return accepted();
|
|
72
|
+
}
|
|
73
|
+
const { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers;
|
|
74
|
+
const read = await readCappedBody(req, MAX_INGEST_BODY_BYTES);
|
|
75
|
+
if (!read.ok) return read.reason === "too-large" ? errorResponse(413, analyticsError("payload_too_large", "analytics: the event body is too large")) : errorResponse(400, analyticsError("invalid_param", "analytics: the event body could not be read"));
|
|
76
|
+
const raw = parseBody(read.body);
|
|
77
|
+
if (raw === void 0) return invalidField("type");
|
|
78
|
+
const param = rawEventError(raw);
|
|
79
|
+
if (param !== void 0) return invalidField(param);
|
|
80
|
+
const now = /* @__PURE__ */ new Date();
|
|
81
|
+
const resolvedScope = resolveScope ? await resolveScope(req) : null;
|
|
82
|
+
const scope = resolveScope ? resolvedScope ?? "" : void 0;
|
|
83
|
+
const hostname = await resolveEventHostname({
|
|
84
|
+
option: attribution.hostname ?? REQUEST_HOSTNAME,
|
|
85
|
+
claimed: raw.hostname,
|
|
86
|
+
req,
|
|
87
|
+
scope: resolvedScope,
|
|
88
|
+
trustedProxyHops: attribution.trustedProxyHops
|
|
89
|
+
});
|
|
90
|
+
if (hostname === null) return drop(req, "hostname refused");
|
|
91
|
+
if (resolveScope !== void 0 && resolvedScope === null && !(attribution.platformHostnames?.has(hostname) ?? false)) return drop(req, "no scope resolved");
|
|
92
|
+
const salt = await dailySalt(req.payload, now);
|
|
93
|
+
const timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : void 0;
|
|
94
|
+
const goals = resolveGoals ? await resolveGoals(req, scope ?? null) : void 0;
|
|
95
|
+
const event = await normalizeEvent({
|
|
96
|
+
raw,
|
|
97
|
+
hostname,
|
|
98
|
+
headers: req.headers,
|
|
99
|
+
geoResolver,
|
|
100
|
+
salt,
|
|
101
|
+
now,
|
|
102
|
+
scope,
|
|
103
|
+
timezone,
|
|
104
|
+
goals,
|
|
105
|
+
trustedProxyHops: attribution.trustedProxyHops
|
|
106
|
+
});
|
|
107
|
+
const buffer = getBuffer();
|
|
108
|
+
if (buffer) buffer.add(event);
|
|
109
|
+
else await flushBatch(req.payload, [event]);
|
|
110
|
+
return accepted();
|
|
111
|
+
};
|
|
26
112
|
};
|
|
27
113
|
//#endregion
|
|
28
114
|
export { makeIngestHandler };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type RawEventInput, type StoredEvent } from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n}\n\nexport const makeIngestHandler =\n\t(\n\t\tgeoResolver: GeoResolver,\n\t\tgetBuffer: () => WriteBuffer<StoredEvent> | null = () => null,\n\t\tresolvers: IngestResolvers = {}\n\t): PayloadHandler =>\n\tasync (req) => {\n\t\tconst { scope: resolveScope, timezone: resolveTimezone } = resolvers\n\t\tconst ct = req.headers.get('content-type') ?? ''\n\t\tconst raw = (\n\t\t\tct.startsWith('application/json')\n\t\t\t\t? await req.json?.()\n\t\t\t\t: JSON.parse((await req.text?.()) ?? '{}')\n\t\t) as RawEventInput\n\t\tif (!raw?.path || !raw?.hostname || (raw.type !== 'pageview' && raw.type !== 'event')) {\n\t\t\treturn Response.json({ error: 'invalid payload' }, { status: 400 })\n\t\t}\n\t\tconst now = new Date()\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst scope = resolveScope ? ((await resolveScope(req)) ?? '') : undefined\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn Response.json({ ok: true }, { status: 202 })\n\t}\n"],"mappings":";;;;AAYA,MAAa,qBAEX,aACA,kBAAyD,MACzD,YAA6B,CAAC,MAE/B,OAAO,QAAQ;CACd,MAAM,EAAE,OAAO,cAAc,UAAU,oBAAoB;CAE3D,MAAM,OADK,IAAI,QAAQ,IAAI,cAAc,KAAK,IAE1C,WAAW,kBAAkB,IAC7B,MAAM,IAAI,OAAO,IACjB,KAAK,MAAO,MAAM,IAAI,OAAO,KAAM,IAAI;CAE3C,IAAI,CAAC,KAAK,QAAQ,CAAC,KAAK,YAAa,IAAI,SAAS,cAAc,IAAI,SAAS,SAC5E,OAAO,SAAS,KAAK,EAAE,OAAO,kBAAkB,GAAG,EAAE,QAAQ,IAAI,CAAC;CAEnE,MAAM,sBAAM,IAAI,KAAK;CACrB,MAAM,OAAO,MAAM,UAAU,IAAI,SAAS,GAAG;CAC7C,MAAM,QAAQ,eAAiB,MAAM,aAAa,GAAG,KAAM,KAAM,KAAA;CACjE,MAAM,WAAW,kBAAkB,MAAM,gBAAgB,KAAK,SAAS,IAAI,IAAI,KAAA;CAC/E,MAAM,QAAQ,MAAM,eAAe;EAClC;EACA,SAAS,IAAI;EACb;EACA;EACA;EACA;EACA;CACD,CAAC;CACD,MAAM,SAAS,UAAU;CACzB,IAAI,QACH,OAAO,IAAI,KAAK;MAEhB,MAAM,WAAW,IAAI,SAAS,CAAC,KAAK,CAAC;CAEtC,OAAO,SAAS,KAAK,EAAE,IAAI,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AACnD"}
|
|
1
|
+
{"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport { readCappedBody } from '../../capture/requestBody'\nimport type { Goal } from '../../goals/types'\nimport { analyticsError, errorResponse } from '../../plugin/errors'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { type BotFilter, isBot } from './bots'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type RawEventInput, type StoredEvent } from './normalizeEvent'\nimport { type ResolvedHostnameOption, resolveEventHostname } from './resolveHostname'\nimport { dailySalt } from './salt'\nimport { type RawEventField, rawEventError } from './validate'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n\tgoals?: (req: PayloadRequest, scope?: string | null) => Promise<Goal[]>\n}\n\n/** How the handler reads the request behind an event, as opposed to what the body claims. */\nexport interface IngestAttribution {\n\ttrustedProxyHops?: number\n\t/** Resolved `hostname` option; absent attributes the event to the request's own host. */\n\thostname?: ResolvedHostnameOption\n\t/** Hostnames that ingest on a scoped install even when the request resolves no scope. */\n\tplatformHostnames?: ReadonlySet<string>\n}\n\n/**\n * What an accepted beacon answers, and what a dropped one answers too: same status, same body,\n * no header of its own and nothing logged per event, so the endpoint never tells a prober which\n * hosts or tenants exist.\n */\nconst accepted = (): Response => Response.json({ ok: true }, { status: 202 })\n\nconst REQUEST_HOSTNAME: ResolvedHostnameOption = { kind: 'request' }\n\n/**\n * What an operator has to hear about once, and the line that tells them where to look. A\n * misconfigured resolver, a broken filter or a header-stripping proxy otherwise costs an\n * install its traffic in silence, since the response cannot say any of it.\n */\nconst WARN_REASONS = {\n\t'no scope resolved':\n\t\t'analytics: ingest is dropping events because scopeResolver answered no scope for them. Check that resolver, and list any platform domain in platformHostnames so it ingests under the null scope. Logged once per process.',\n\t'hostname refused':\n\t\t'analytics: ingest is dropping events because the hostname option refused the request host. Check that option, and platformHostnames for a platform domain. Logged once per process.',\n\t'bot filter threw':\n\t\t'analytics: the filterBots function threw, so the event was kept. Check that function; every beacon counts while it throws. Logged once per process.',\n\t'no user agent':\n\t\t'analytics: a beacon arrived with no user agent and was not recorded: an intermediary may be stripping the header. Logged once per process.',\n} as const\n\ntype WarnReason = keyof typeof WARN_REASONS\n\n/** One event, not a session replay: far above any legitimate payload, far below a DoS. */\nexport const MAX_INGEST_BODY_BYTES = 64 * 1024\n\n/**\n * Parses the buffered body, answering undefined for anything the validator could not judge:\n * a garbage POST is a 400 like any other bad payload rather than a thrown parse error that\n * escapes into Payload's routeError as a logged 500.\n */\nconst parseBody = (bytes: ArrayBuffer): RawEventInput | undefined => {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(new TextDecoder().decode(bytes))\n\t\treturn typeof parsed === 'object' && parsed !== null ? (parsed as RawEventInput) : undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * The refusal for the first field an event failed on: naming it costs nothing and saves a\n * tracker author guessing which one of four the endpoint refused. A body that is not an\n * object at all fails the very check a typeless event does, so it names `type`.\n */\nconst invalidField = (param: RawEventField): Response =>\n\terrorResponse(\n\t\t400,\n\t\tanalyticsError('invalid_param', `analytics: ${param} is missing or invalid`, param)\n\t)\n\nexport interface IngestHandlerOptions {\n\tgeoResolver: GeoResolver\n\tgetBuffer?: () => WriteBuffer<StoredEvent> | null\n\tresolvers?: IngestResolvers\n\tattribution?: IngestAttribution\n\t/** Which agents never reach the pipeline. Defaults to the built-in `isBot`. */\n\tfilterBots?: BotFilter\n}\n\nexport const makeIngestHandler = ({\n\tgeoResolver,\n\tgetBuffer = () => null,\n\tresolvers = {},\n\tattribution = {},\n\tfilterBots = isBot,\n}: IngestHandlerOptions): PayloadHandler => {\n\t// One line per reason for the life of the handler, which is the life of the process: a\n\t// public endpoint must not be a log amplifier, and an attacker chooses how often it drops.\n\tconst warned = new Set<WarnReason>()\n\tconst warnOnce = (req: PayloadRequest, reason: WarnReason): void => {\n\t\tif (!warned.has(reason)) {\n\t\t\twarned.add(reason)\n\t\t\treq.payload.logger?.warn(WARN_REASONS[reason])\n\t\t}\n\t}\n\tconst drop = (req: PayloadRequest, reason: WarnReason): Response => {\n\t\twarnOnce(req, reason)\n\t\treturn accepted()\n\t}\n\n\treturn async (req) => {\n\t\t// First, before the body is even read, so a crawler costs one header lookup. Its beacon\n\t\t// is answered exactly like a kept one and nothing is logged: bots are expected traffic\n\t\t// rather than a misconfiguration an operator has to hear about once per process.\n\t\tconst userAgent = req.headers.get('user-agent') ?? ''\n\t\tlet automated = false\n\t\ttry {\n\t\t\tautomated = filterBots(userAgent)\n\t\t} catch {\n\t\t\twarnOnce(req, 'bot filter threw')\n\t\t}\n\t\tif (automated) {\n\t\t\t// The exception to the silence: an agent-less beacon is indistinguishable from an\n\t\t\t// intermediary stripping the header, which would zero an install's numbers.\n\t\t\tif (userAgent.trim() === '') {\n\t\t\t\twarnOnce(req, 'no user agent')\n\t\t\t}\n\t\t\treturn accepted()\n\t\t}\n\t\tconst { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers\n\t\t// Read like the capture proxy does, and for the same reasons: this is a public,\n\t\t// unauthenticated path, so the body is capped before it is buffered and a body that\n\t\t// dies in transit (a beacon from an unloading tab) answers 400 rather than throwing.\n\t\tconst read = await readCappedBody(req, MAX_INGEST_BODY_BYTES)\n\t\tif (!read.ok) {\n\t\t\treturn read.reason === 'too-large'\n\t\t\t\t? errorResponse(\n\t\t\t\t\t\t413,\n\t\t\t\t\t\tanalyticsError('payload_too_large', 'analytics: the event body is too large')\n\t\t\t\t\t)\n\t\t\t\t: errorResponse(\n\t\t\t\t\t\t400,\n\t\t\t\t\t\tanalyticsError('invalid_param', 'analytics: the event body could not be read')\n\t\t\t\t\t)\n\t\t}\n\t\tconst raw = parseBody(read.body)\n\t\tif (raw === undefined) {\n\t\t\treturn invalidField('type')\n\t\t}\n\t\tconst param = rawEventError(raw)\n\t\tif (param !== undefined) {\n\t\t\treturn invalidField(param)\n\t\t}\n\t\tconst now = new Date()\n\t\tconst resolvedScope = resolveScope ? await resolveScope(req) : null\n\t\tconst scope = resolveScope ? (resolvedScope ?? '') : undefined\n\t\tconst hostname = await resolveEventHostname({\n\t\t\toption: attribution.hostname ?? REQUEST_HOSTNAME,\n\t\t\tclaimed: raw.hostname,\n\t\t\treq,\n\t\t\tscope: resolvedScope,\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t})\n\t\tif (hostname === null) {\n\t\t\treturn drop(req, 'hostname refused')\n\t\t}\n\t\t// A scoped install keeps only what a scope answers for. Its platform hostnames are\n\t\t// infrastructure rather than tenants, so they ingest under the null scope.\n\t\tconst unscoped =\n\t\t\tresolveScope !== undefined &&\n\t\t\tresolvedScope === null &&\n\t\t\t!(attribution.platformHostnames?.has(hostname) ?? false)\n\t\tif (unscoped) {\n\t\t\treturn drop(req, 'no scope resolved')\n\t\t}\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\t// Goals resolve under the same scope the event is stamped with, so a tenant's event\n\t\t// can only ever complete that tenant's goals.\n\t\tconst goals = resolveGoals ? await resolveGoals(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\thostname,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t\tgoals,\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn accepted()\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AAiCA,MAAM,iBAA2B,SAAS,KAAK,EAAE,IAAI,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AAE5E,MAAM,mBAA2C,EAAE,MAAM,UAAU;;;;;;AAOnE,MAAM,eAAe;CACpB,qBACC;CACD,oBACC;CACD,oBACC;CACD,iBACC;AACF;;AAKA,MAAa,wBAAwB,KAAK;;;;;;AAO1C,MAAM,aAAa,UAAkD;CACpE,IAAI;EACH,MAAM,SAAkB,KAAK,MAAM,IAAI,YAAY,EAAE,OAAO,KAAK,CAAC;EAClE,OAAO,OAAO,WAAW,YAAY,WAAW,OAAQ,SAA2B,KAAA;CACpF,QAAQ;EACP;CACD;AACD;;;;;;AAOA,MAAM,gBAAgB,UACrB,cACC,KACA,eAAe,iBAAiB,cAAc,MAAM,yBAAyB,KAAK,CACnF;AAWD,MAAa,qBAAqB,EACjC,aACA,kBAAkB,MAClB,YAAY,CAAC,GACb,cAAc,CAAC,GACf,aAAa,YAC8B;CAG3C,MAAM,yBAAS,IAAI,IAAgB;CACnC,MAAM,YAAY,KAAqB,WAA6B;EACnE,IAAI,CAAC,OAAO,IAAI,MAAM,GAAG;GACxB,OAAO,IAAI,MAAM;GACjB,IAAI,QAAQ,QAAQ,KAAK,aAAa,OAAO;EAC9C;CACD;CACA,MAAM,QAAQ,KAAqB,WAAiC;EACnE,SAAS,KAAK,MAAM;EACpB,OAAO,SAAS;CACjB;CAEA,OAAO,OAAO,QAAQ;EAIrB,MAAM,YAAY,IAAI,QAAQ,IAAI,YAAY,KAAK;EACnD,IAAI,YAAY;EAChB,IAAI;GACH,YAAY,WAAW,SAAS;EACjC,QAAQ;GACP,SAAS,KAAK,kBAAkB;EACjC;EACA,IAAI,WAAW;GAGd,IAAI,UAAU,KAAK,MAAM,IACxB,SAAS,KAAK,eAAe;GAE9B,OAAO,SAAS;EACjB;EACA,MAAM,EAAE,OAAO,cAAc,UAAU,iBAAiB,OAAO,iBAAiB;EAIhF,MAAM,OAAO,MAAM,eAAe,KAAK,qBAAqB;EAC5D,IAAI,CAAC,KAAK,IACT,OAAO,KAAK,WAAW,cACpB,cACA,KACA,eAAe,qBAAqB,wCAAwC,CAC7E,IACC,cACA,KACA,eAAe,iBAAiB,6CAA6C,CAC9E;EAEH,MAAM,MAAM,UAAU,KAAK,IAAI;EAC/B,IAAI,QAAQ,KAAA,GACX,OAAO,aAAa,MAAM;EAE3B,MAAM,QAAQ,cAAc,GAAG;EAC/B,IAAI,UAAU,KAAA,GACb,OAAO,aAAa,KAAK;EAE1B,MAAM,sBAAM,IAAI,KAAK;EACrB,MAAM,gBAAgB,eAAe,MAAM,aAAa,GAAG,IAAI;EAC/D,MAAM,QAAQ,eAAgB,iBAAiB,KAAM,KAAA;EACrD,MAAM,WAAW,MAAM,qBAAqB;GAC3C,QAAQ,YAAY,YAAY;GAChC,SAAS,IAAI;GACb;GACA,OAAO;GACP,kBAAkB,YAAY;EAC/B,CAAC;EACD,IAAI,aAAa,MAChB,OAAO,KAAK,KAAK,kBAAkB;EAQpC,IAHC,iBAAiB,KAAA,KACjB,kBAAkB,QAClB,EAAE,YAAY,mBAAmB,IAAI,QAAQ,KAAK,QAElD,OAAO,KAAK,KAAK,mBAAmB;EAErC,MAAM,OAAO,MAAM,UAAU,IAAI,SAAS,GAAG;EAC7C,MAAM,WAAW,kBAAkB,MAAM,gBAAgB,KAAK,SAAS,IAAI,IAAI,KAAA;EAG/E,MAAM,QAAQ,eAAe,MAAM,aAAa,KAAK,SAAS,IAAI,IAAI,KAAA;EACtE,MAAM,QAAQ,MAAM,eAAe;GAClC;GACA;GACA,SAAS,IAAI;GACb;GACA;GACA;GACA;GACA;GACA;GACA,kBAAkB,YAAY;EAC/B,CAAC;EACD,MAAM,SAAS,UAAU;EACzB,IAAI,QACH,OAAO,IAAI,KAAK;OAEhB,MAAM,WAAW,IAAI,SAAS,CAAC,KAAK,CAAC;EAEtC,OAAO,SAAS;CACjB;AACD"}
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import "../collections/seen.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { bucketKey } from "../rollups/applyDistinctDeltas.js";
|
|
1
|
+
import { SEEN_SLUG } from "../collections/seen.js";
|
|
2
|
+
import { bucketKey } from "../rollups/bucketKey.js";
|
|
3
|
+
import { bumpRollups } from "../rollups/bumpRollups.js";
|
|
5
4
|
import { computeRollupDeltas } from "../rollups/deltas.js";
|
|
5
|
+
import { insertManyIfNew } from "../rollups/insertManyIfNew.js";
|
|
6
6
|
import { writeEvent } from "./writeEvent.js";
|
|
7
7
|
//#region src/native/ingest/flushBatch.ts
|
|
8
|
+
/**
|
|
9
|
+
* Coalesces a batch of events into the fewest writes: events insert concurrently, base rollup
|
|
10
|
+
* increments are summed per bucket, and distinct visitor / session candidates are de-duplicated
|
|
11
|
+
* within the batch before one insert-if-new decides which of them the ledger had not seen.
|
|
12
|
+
* Whatever the batch size, that is three round trips for the rollups and the ledger rather
|
|
13
|
+
* than one per bucket. Correctness matches the per-event path because the seen ledger still
|
|
14
|
+
* de-dupes across batches, and its unique index still arbitrates between concurrent writers.
|
|
15
|
+
*/
|
|
8
16
|
async function flushBatch(payload, events) {
|
|
9
17
|
if (events.length === 0) return;
|
|
10
18
|
await Promise.all(events.map((event) => writeEvent(payload, event)));
|
|
@@ -13,14 +21,10 @@ async function flushBatch(payload, events) {
|
|
|
13
21
|
for (const event of events) for (const delta of computeRollupDeltas(event)) {
|
|
14
22
|
const bk = bucketKey(delta.key);
|
|
15
23
|
const agg = base.get(bk);
|
|
16
|
-
if (agg)
|
|
17
|
-
|
|
18
|
-
agg.events += delta.inc.events;
|
|
19
|
-
agg.durationMs += delta.inc.durationMs;
|
|
20
|
-
agg.samples += delta.inc.samples;
|
|
21
|
-
} else base.set(bk, {
|
|
24
|
+
if (agg) for (const metric of Object.keys(agg.inc)) agg.inc[metric] += delta.inc[metric];
|
|
25
|
+
else base.set(bk, {
|
|
22
26
|
key: delta.key,
|
|
23
|
-
...delta.inc
|
|
27
|
+
inc: { ...delta.inc }
|
|
24
28
|
});
|
|
25
29
|
candidates.set(`${bk}|visitor|${event.visitorHash}`, {
|
|
26
30
|
key: delta.key,
|
|
@@ -33,35 +37,22 @@ async function flushBatch(payload, events) {
|
|
|
33
37
|
value: event.sessionId
|
|
34
38
|
});
|
|
35
39
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
value: candidate.value,
|
|
48
|
-
period: candidate.key.period
|
|
49
|
-
})) continue;
|
|
50
|
-
const metric = candidate.kind === "visitor" ? "visitors" : "sessions";
|
|
51
|
-
const dk = `${bucketKey(candidate.key)}|${metric}`;
|
|
52
|
-
const entry = distinct.get(dk);
|
|
53
|
-
if (entry) entry.count += 1;
|
|
54
|
-
else distinct.set(dk, {
|
|
40
|
+
await bumpRollups(payload, [...base.values()]);
|
|
41
|
+
const pending = [...candidates.values()];
|
|
42
|
+
const isNew = await insertManyIfNew(payload, SEEN_SLUG, pending.map((candidate) => ({
|
|
43
|
+
bucket: bucketKey(candidate.key),
|
|
44
|
+
kind: candidate.kind,
|
|
45
|
+
value: candidate.value,
|
|
46
|
+
period: candidate.key.period
|
|
47
|
+
})));
|
|
48
|
+
const distinct = [];
|
|
49
|
+
pending.forEach((candidate, index) => {
|
|
50
|
+
if (isNew[index]) distinct.push({
|
|
55
51
|
key: candidate.key,
|
|
56
|
-
|
|
57
|
-
count: 1
|
|
52
|
+
inc: candidate.kind === "visitor" ? { visitors: 1 } : { sessions: 1 }
|
|
58
53
|
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const inc = {};
|
|
62
|
-
inc[entry.metric] = entry.count;
|
|
63
|
-
await bumpRollup(payload, entry.key, inc);
|
|
64
|
-
}
|
|
54
|
+
});
|
|
55
|
+
await bumpRollups(payload, distinct);
|
|
65
56
|
}
|
|
66
57
|
//#endregion
|
|
67
58
|
export { flushBatch };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/
|
|
1
|
+
{"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/bucketKey'\nimport { bumpRollups, type RollupBump } from '../rollups/bumpRollups'\nimport { computeRollupDeltas, type RollupInc, type RollupKey } from '../rollups/deltas'\nimport { insertManyIfNew } from '../rollups/insertManyIfNew'\nimport type { StoredEvent } from './normalizeEvent'\nimport { writeEvent } from './writeEvent'\n\ninterface BaseAgg {\n\tkey: RollupKey\n\tinc: RollupInc\n}\n\ninterface DistinctCandidate {\n\tkey: RollupKey\n\tkind: 'visitor' | 'session'\n\tvalue: string\n}\n\n/**\n * Coalesces a batch of events into the fewest writes: events insert concurrently, base rollup\n * increments are summed per bucket, and distinct visitor / session candidates are de-duplicated\n * within the batch before one insert-if-new decides which of them the ledger had not seen.\n * Whatever the batch size, that is three round trips for the rollups and the ledger rather\n * than one per bucket. Correctness matches the per-event path because the seen ledger still\n * de-dupes across batches, and its unique index still arbitrates between concurrent writers.\n */\nexport async function flushBatch(payload: Payload, events: StoredEvent[]): Promise<void> {\n\tif (events.length === 0) {\n\t\treturn\n\t}\n\tawait Promise.all(events.map((event) => writeEvent(payload, event)))\n\n\tconst base = new Map<string, BaseAgg>()\n\tconst candidates = new Map<string, DistinctCandidate>()\n\tfor (const event of events) {\n\t\tfor (const delta of computeRollupDeltas(event)) {\n\t\t\tconst bk = bucketKey(delta.key)\n\t\t\tconst agg = base.get(bk)\n\t\t\tif (agg) {\n\t\t\t\tfor (const metric of Object.keys(agg.inc) as Array<keyof RollupInc>) {\n\t\t\t\t\tagg.inc[metric] += delta.inc[metric]\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tbase.set(bk, { key: delta.key, inc: { ...delta.inc } })\n\t\t\t}\n\t\t\tcandidates.set(`${bk}|visitor|${event.visitorHash}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'visitor',\n\t\t\t\tvalue: event.visitorHash,\n\t\t\t})\n\t\t\tcandidates.set(`${bk}|session|${event.sessionId}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'session',\n\t\t\t\tvalue: event.sessionId,\n\t\t\t})\n\t\t}\n\t}\n\n\tawait bumpRollups(payload, [...base.values()])\n\n\tconst pending = [...candidates.values()]\n\tconst isNew = await insertManyIfNew(\n\t\tpayload,\n\t\tSEEN_SLUG,\n\t\tpending.map((candidate) => ({\n\t\t\tbucket: bucketKey(candidate.key),\n\t\t\tkind: candidate.kind,\n\t\t\tvalue: candidate.value,\n\t\t\tperiod: candidate.key.period,\n\t\t}))\n\t)\n\t// One increment per ledger row this flush actually created; bumpRollups sums the ones that\n\t// land on the same bucket.\n\tconst distinct: RollupBump[] = []\n\tpending.forEach((candidate, index) => {\n\t\tif (isNew[index]) {\n\t\t\tdistinct.push({\n\t\t\t\tkey: candidate.key,\n\t\t\t\tinc: candidate.kind === 'visitor' ? { visitors: 1 } : { sessions: 1 },\n\t\t\t})\n\t\t}\n\t})\n\tawait bumpRollups(payload, distinct)\n}\n"],"mappings":";;;;;;;;;;;;;;;AA4BA,eAAsB,WAAW,SAAkB,QAAsC;CACxF,IAAI,OAAO,WAAW,GACrB;CAED,MAAM,QAAQ,IAAI,OAAO,KAAK,UAAU,WAAW,SAAS,KAAK,CAAC,CAAC;CAEnE,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,6BAAa,IAAI,IAA+B;CACtD,KAAK,MAAM,SAAS,QACnB,KAAK,MAAM,SAAS,oBAAoB,KAAK,GAAG;EAC/C,MAAM,KAAK,UAAU,MAAM,GAAG;EAC9B,MAAM,MAAM,KAAK,IAAI,EAAE;EACvB,IAAI,KACH,KAAK,MAAM,UAAU,OAAO,KAAK,IAAI,GAAG,GACvC,IAAI,IAAI,WAAW,MAAM,IAAI;OAG9B,KAAK,IAAI,IAAI;GAAE,KAAK,MAAM;GAAK,KAAK,EAAE,GAAG,MAAM,IAAI;EAAE,CAAC;EAEvD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,eAAe;GACpD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;EACD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,aAAa;GAClD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;CACF;CAGD,MAAM,YAAY,SAAS,CAAC,GAAG,KAAK,OAAO,CAAC,CAAC;CAE7C,MAAM,UAAU,CAAC,GAAG,WAAW,OAAO,CAAC;CACvC,MAAM,QAAQ,MAAM,gBACnB,SACA,WACA,QAAQ,KAAK,eAAe;EAC3B,QAAQ,UAAU,UAAU,GAAG;EAC/B,MAAM,UAAU;EAChB,OAAO,UAAU;EACjB,QAAQ,UAAU,IAAI;CACvB,EAAE,CACH;CAGA,MAAM,WAAyB,CAAC;CAChC,QAAQ,SAAS,WAAW,UAAU;EACrC,IAAI,MAAM,QACT,SAAS,KAAK;GACb,KAAK,UAAU;GACf,KAAK,UAAU,SAAS,YAAY,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE;EACrE,CAAC;CAEH,CAAC;CACD,MAAM,YAAY,SAAS,QAAQ;AACpC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The visitor's preferred language from an `Accept-Language` header: the first tag, without
|
|
3
|
+
* its quality factor, lowercased (`de-DE,de;q=0.9` becomes `de-de`). A wildcard or an absent
|
|
4
|
+
* header states no preference, so nothing is reported.
|
|
5
|
+
*/
|
|
6
|
+
const primaryLanguage = (acceptLanguage) => {
|
|
7
|
+
const tag = acceptLanguage?.split(",")[0]?.split(";")[0]?.trim().toLowerCase();
|
|
8
|
+
if (!tag || tag === "*") return;
|
|
9
|
+
return tag.slice(0, 16);
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
export { primaryLanguage };
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=language.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"language.js","names":[],"sources":["../../../src/native/ingest/language.ts"],"sourcesContent":["/** Long enough for any real BCP 47 tag; a longer one is junk, not a language. */\nexport const MAX_LANGUAGE_LENGTH = 16\n\n/**\n * The visitor's preferred language from an `Accept-Language` header: the first tag, without\n * its quality factor, lowercased (`de-DE,de;q=0.9` becomes `de-de`). A wildcard or an absent\n * header states no preference, so nothing is reported.\n */\nexport const primaryLanguage = (acceptLanguage: string | null): string | undefined => {\n\tconst tag = acceptLanguage?.split(',')[0]?.split(';')[0]?.trim().toLowerCase()\n\tif (!tag || tag === '*') {\n\t\treturn undefined\n\t}\n\treturn tag.slice(0, MAX_LANGUAGE_LENGTH)\n}\n"],"mappings":";;;;;AAQA,MAAa,mBAAmB,mBAAsD;CACrF,MAAM,MAAM,gBAAgB,MAAM,GAAG,EAAE,IAAI,MAAM,GAAG,EAAE,IAAI,KAAK,EAAE,YAAY;CAC7E,IAAI,CAAC,OAAO,QAAQ,KACnB;CAED,OAAO,IAAI,MAAM,GAAA,EAAsB;AACxC"}
|