Skip to content

Commit

Permalink
Rename LegacySettings to CDNSettings (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored May 14, 2024
1 parent ef959f6 commit 2299e9a
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 72 deletions.
4 changes: 4 additions & 0 deletions .changeset/ninety-gifts-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
'@segment/analytics-next': minor
---
Refactor to change interface name from `legacySettings` -> `cdnSettings`, in order to clarify code.
4 changes: 2 additions & 2 deletions packages/browser/src/browser/__tests__/csp-detection.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import jsdom, { JSDOM } from 'jsdom'
import unfetch from 'unfetch'
import { LegacySettings } from '..'
import { CDNSettings } from '..'
import { pWhile } from '../../lib/p-while'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import * as Factory from '../../test-helpers/factories'

const cdnResponse: LegacySettings = {
const cdnResponse: CDNSettings = {
integrations: {
Zapier: {
type: 'server',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jsdom, { JSDOM } from 'jsdom'
import { AnalyticsBrowser, LegacySettings } from '..'
import { AnalyticsBrowser, CDNSettings } from '..'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import { pWhile } from '../../lib/p-while'
import unfetch from 'unfetch'
import { RemoteMetrics } from '../../core/stats/remote-metrics'
import * as Factory from '../../test-helpers/factories'

const cdnResponse: LegacySettings = {
const cdnResponse: CDNSettings = {
integrations: {
Zapier: {
type: 'server',
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/browser/__tests__/standalone.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import jsdom, { JSDOM } from 'jsdom'
import unfetch from 'unfetch'
import { LegacySettings } from '..'
import { CDNSettings } from '..'
import { pWhile } from '../../lib/p-while'
import { snippet } from '../../tester/__fixtures__/segment-snippet'
import * as Factory from '../../test-helpers/factories'
import { getGlobalAnalytics } from '../..'

const cdnResponse: LegacySettings = {
const cdnResponse: CDNSettings = {
integrations: {
Zapier: {
type: 'server',
Expand Down
54 changes: 30 additions & 24 deletions packages/browser/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,29 @@ import { attachInspector } from '../core/inspector'
import { Stats } from '../core/stats'
import { setGlobalAnalyticsKey } from '../lib/global-analytics-helper'

export interface LegacyIntegrationConfiguration {
export interface RemoteIntegrationSettings {
/* @deprecated - This does not indicate browser types anymore */
type?: string

versionSettings?: {
version?: string
override?: string
componentTypes?: Array<'browser' | 'android' | 'ios' | 'server'>
componentTypes?: ('browser' | 'android' | 'ios' | 'server')[]
}

bundlingStatus?: string
/**
* We know if an integration is device mode if it has `bundlingStatus: 'bundled'` and the `browser` componentType in `versionSettings`.
* History: The term 'bundle' is left over from before action destinations, when a device mode destinations were 'bundled' in a custom bundle for every analytics.js source.
*/
bundlingStatus?: 'bundled' | 'unbundled'

/**
* Consent settings for the integration
*/
consentSettings?: {
/**
* Consent categories for the integration
* @example ["Analytics", "Advertising", "CAT001"]
* @example ["CAT001", "CAT002"]
*/
categories: string[]
}
Expand All @@ -67,9 +71,13 @@ export interface LegacyIntegrationConfiguration {
[key: string]: any
}

export interface LegacySettings {
/**
* The remote settings object for a source, typically fetched from the Segment CDN.
* Warning: this is an *unstable* object.
*/
export interface CDNSettings {
integrations: {
[name: string]: LegacyIntegrationConfiguration
[creationName: string]: RemoteIntegrationSettings
}

middlewareSettings?: {
Expand Down Expand Up @@ -109,7 +117,7 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {
* If provided, `AnalyticsBrowser` will not fetch remote settings
* for the source.
*/
cdnSettings?: LegacySettings & Record<string, unknown>
cdnSettings?: CDNSettings & Record<string, unknown>
/**
* If provided, will override the default Segment CDN (https://cdn.segment.com) for this application.
*/
Expand All @@ -119,7 +127,7 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {
export function loadLegacySettings(
writeKey: string,
cdnURL?: string
): Promise<LegacySettings> {
): Promise<CDNSettings> {
const baseUrl = cdnURL ?? getCDN()

return fetch(`${baseUrl}/v1/projects/${writeKey}/settings`)
Expand All @@ -137,15 +145,15 @@ export function loadLegacySettings(
})
}

function hasLegacyDestinations(settings: LegacySettings): boolean {
function hasLegacyDestinations(settings: CDNSettings): boolean {
return (
getProcessEnv().NODE_ENV !== 'test' &&
// just one integration means segmentio
Object.keys(settings.integrations).length > 1
)
}

function hasTsubMiddleware(settings: LegacySettings): boolean {
function hasTsubMiddleware(settings: CDNSettings): boolean {
return (
getProcessEnv().NODE_ENV !== 'test' &&
(settings.middlewareSettings?.routingRules?.length ?? 0) > 0
Expand Down Expand Up @@ -185,7 +193,7 @@ async function flushFinalBuffer(

async function registerPlugins(
writeKey: string,
legacySettings: LegacySettings,
cdnSettings: CDNSettings,
analytics: Analytics,
options: InitOptions,
pluginLikes: (Plugin | PluginFactory)[] = [],
Expand All @@ -201,24 +209,22 @@ async function registerPlugins(
typeof pluginLike.pluginName === 'string'
) as PluginFactory[]

const tsubMiddleware = hasTsubMiddleware(legacySettings)
const tsubMiddleware = hasTsubMiddleware(cdnSettings)
? await import(
/* webpackChunkName: "tsub-middleware" */ '../plugins/routing-middleware'
).then((mod) => {
return mod.tsubMiddleware(
legacySettings.middlewareSettings!.routingRules
)
return mod.tsubMiddleware(cdnSettings.middlewareSettings!.routingRules)
})
: undefined

const legacyDestinations =
hasLegacyDestinations(legacySettings) || legacyIntegrationSources.length > 0
hasLegacyDestinations(cdnSettings) || legacyIntegrationSources.length > 0
? await import(
/* webpackChunkName: "ajs-destination" */ '../plugins/ajs-destination'
).then((mod) => {
return mod.ajsDestinations(
writeKey,
legacySettings,
cdnSettings,
analytics.integrations,
options,
tsubMiddleware,
Expand All @@ -227,7 +233,7 @@ async function registerPlugins(
})
: []

if (legacySettings.legacyVideoPluginsEnabled) {
if (cdnSettings.legacyVideoPluginsEnabled) {
await import(
/* webpackChunkName: "legacyVideos" */ '../plugins/legacy-video-plugins'
).then((mod) => {
Expand All @@ -239,13 +245,13 @@ async function registerPlugins(
? await import(
/* webpackChunkName: "schemaFilter" */ '../plugins/schema-filter'
).then((mod) => {
return mod.schemaFilter(options.plan?.track, legacySettings)
return mod.schemaFilter(options.plan?.track, cdnSettings)
})
: undefined

const mergedSettings = mergedOptions(legacySettings, options)
const mergedSettings = mergedOptions(cdnSettings, options)
const remotePlugins = await remoteLoader(
legacySettings,
cdnSettings,
analytics.integrations,
mergedSettings,
options,
Expand Down Expand Up @@ -274,15 +280,15 @@ async function registerPlugins(
await segmentio(
analytics,
mergedSettings['Segment.io'] as SegmentioSettings,
legacySettings.integrations
cdnSettings.integrations
)
)
}

const ctx = await analytics.register(...toRegister)

if (
Object.entries(legacySettings.enabledMiddleware ?? {}).some(
Object.entries(cdnSettings.enabledMiddleware ?? {}).some(
([, enabled]) => enabled
)
) {
Expand All @@ -291,7 +297,7 @@ async function registerPlugins(
).then(async ({ remoteMiddlewares }) => {
const middleware = await remoteMiddlewares(
ctx,
legacySettings,
cdnSettings,
options.obfuscate
)
const promises = middleware.map((mdw) =>
Expand Down
8 changes: 3 additions & 5 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { PriorityQueue } from '../../lib/priority-queue'
import { getGlobal } from '../../lib/get-global'
import { AnalyticsClassic, AnalyticsCore } from './interfaces'
import { HighEntropyHint } from '../../lib/client-hints/interfaces'
import type { LegacySettings } from '../../browser'
import type { CDNSettings } from '../../browser'
import {
CookieOptions,
MemoryStorage,
Expand Down Expand Up @@ -109,7 +109,7 @@ export interface InitOptions {
* This callback allows you to update/mutate CDN Settings.
* This is called directly after settings are fetched from the CDN.
*/
updateCDNSettings?: (settings: LegacySettings) => LegacySettings
updateCDNSettings?: (settings: CDNSettings) => CDNSettings
/**
* Disables or sets constraints on processing of query string parameters
*/
Expand Down Expand Up @@ -143,9 +143,7 @@ export interface InitOptions {
* disable: (cdnSettings) => cdnSettings.foo === 'bar'
* ```
*/
disable?:
| boolean
| ((cdnSettings: LegacySettings) => boolean | Promise<boolean>)
disable?: boolean | ((cdnSettings: CDNSettings) => boolean | Promise<boolean>)
}

/* analytics-classic stubs */
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/src/lib/merged-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONObject, Options } from '../core/events/interfaces'
import { LegacySettings } from '../browser'
import { CDNSettings } from '../browser'

/**
* Merge legacy settings and initialized Integration option overrides.
Expand All @@ -11,7 +11,7 @@ import { LegacySettings } from '../browser'
* the Analytics constructor.
*/
export function mergedOptions(
settings: LegacySettings,
cdnSettings: CDNSettings,
options: Options
): Record<string, JSONObject> {
const optionOverrides = Object.entries(options.integrations ?? {}).reduce(
Expand All @@ -31,7 +31,7 @@ export function mergedOptions(
{} as Record<string, JSONObject>
)

return Object.entries(settings.integrations).reduce(
return Object.entries(cdnSettings.integrations).reduce(
(integrationSettings, [integration, settings]) => {
return {
...integrationSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import jsdom from 'jsdom'
import unfetch from 'unfetch'
import { ajsDestinations, LegacyDestination } from '..'
import { Analytics } from '../../../core/analytics'
import { LegacySettings } from '../../../browser'
import { CDNSettings } from '../../../browser'
import { Context } from '../../../core/context'
import { Plan } from '../../../core/events'
import { tsubMiddleware } from '../../routing-middleware'
import { AMPLITUDE_WRITEKEY } from '../../../test-helpers/test-writekeys'
import { PersistedPriorityQueue } from '../../../lib/priority-queue/persisted'
import * as Factory from '../../../test-helpers/factories'

const cdnResponse: LegacySettings = {
const cdnResponse: CDNSettings = {
integrations: {
Zapier: {
type: 'server',
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/plugins/ajs-destination/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Integrations, JSONObject } from '../../core/events'
import { Alias, Facade, Group, Identify, Page, Track } from '@segment/facade'
import { Analytics, InitOptions } from '../../core/analytics'
import { LegacySettings } from '../../browser'
import { CDNSettings } from '../../browser'
import { isOffline, isOnline } from '../../core/connection'
import { Context, ContextCancelation } from '../../core/context'
import { isServer } from '../../core/environment'
Expand Down Expand Up @@ -331,7 +331,7 @@ export class LegacyDestination implements InternalPluginWithAddMiddleware {

export function ajsDestinations(
writeKey: string,
settings: LegacySettings,
settings: CDNSettings,
globalIntegrations: Integrations = {},
options: InitOptions = {},
routingMiddleware?: DestinationMiddlewareFunction,
Expand Down
8 changes: 4 additions & 4 deletions packages/browser/src/plugins/ajs-destination/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Analytics } from '../../core/analytics'
import { LegacyIntegrationConfiguration } from '../../browser'
import { RemoteIntegrationSettings } from '../../browser'
import { getNextIntegrationsURL } from '../../lib/parse-cdn'
import { Context } from '../../core/context'
import { User } from '../../core/user'
Expand Down Expand Up @@ -119,11 +119,11 @@ export async function unloadIntegration(
}

export function resolveVersion(
settings?: LegacyIntegrationConfiguration
integrationConfig?: RemoteIntegrationSettings
): string {
return (
settings?.versionSettings?.override ??
settings?.versionSettings?.version ??
integrationConfig?.versionSettings?.override ??
integrationConfig?.versionSettings?.version ??
'latest'
)
}
4 changes: 2 additions & 2 deletions packages/browser/src/plugins/ajs-destination/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Integrations } from '@segment/analytics-core'
import { LegacyIntegrationConfiguration } from '../..'
import { RemoteIntegrationSettings } from '../..'

export const isInstallableIntegration = (
name: string,
integrationSettings: LegacyIntegrationConfiguration
integrationSettings: RemoteIntegrationSettings
) => {
const { type, bundlingStatus, versionSettings } = integrationSettings
// We use `!== 'unbundled'` (versus `=== 'bundled'`) to be inclusive of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import braze from '@segment/analytics-browser-actions-braze'

import * as loader from '../../../lib/load-script'
import { ActionDestination, PluginFactory, remoteLoader } from '..'
import { AnalyticsBrowser, LegacySettings } from '../../../browser'
import { AnalyticsBrowser, CDNSettings } from '../../../browser'
import { InitOptions } from '../../../core/analytics'
import { Context } from '../../../core/context'
import { tsubMiddleware } from '../../routing-middleware'
Expand Down Expand Up @@ -520,7 +520,7 @@ describe('Remote Loader', () => {
})

it('accepts settings overrides from merged integrations', async () => {
const cdnSettings: LegacySettings = {
const cdnSettings: CDNSettings = {
integrations: {
remotePlugin: {
name: 'Charlie Brown',
Expand Down Expand Up @@ -785,7 +785,7 @@ describe('Remote Loader', () => {
track: (ctx: Context) => ctx,
}

const cdnSettings: LegacySettings = {
const cdnSettings: CDNSettings = {
integrations: {},
middlewareSettings: {
routingRules: [
Expand Down Expand Up @@ -856,7 +856,7 @@ describe('Remote Loader', () => {
track: (ctx: Context) => ctx,
}

const cdnSettings: LegacySettings = {
const cdnSettings: CDNSettings = {
integrations: {},
middlewareSettings: {
routingRules: [
Expand Down Expand Up @@ -923,7 +923,7 @@ describe('Remote Loader', () => {
},
}

const cdnSettings: LegacySettings = {
const cdnSettings: CDNSettings = {
integrations: {},
remotePlugins: [
{
Expand Down
Loading

0 comments on commit 2299e9a

Please sign in to comment.