Skip to content

Commit 2299e9a

Browse files
authored
Rename LegacySettings to CDNSettings (#1088)
1 parent ef959f6 commit 2299e9a

File tree

20 files changed

+80
-72
lines changed

20 files changed

+80
-72
lines changed

.changeset/ninety-gifts-march.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
Refactor to change interface name from `legacySettings` -> `cdnSettings`, in order to clarify code.

packages/browser/src/browser/__tests__/csp-detection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import jsdom, { JSDOM } from 'jsdom'
22
import unfetch from 'unfetch'
3-
import { LegacySettings } from '..'
3+
import { CDNSettings } from '..'
44
import { pWhile } from '../../lib/p-while'
55
import { snippet } from '../../tester/__fixtures__/segment-snippet'
66
import * as Factory from '../../test-helpers/factories'
77

8-
const cdnResponse: LegacySettings = {
8+
const cdnResponse: CDNSettings = {
99
integrations: {
1010
Zapier: {
1111
type: 'server',

packages/browser/src/browser/__tests__/standalone-errors.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import jsdom, { JSDOM } from 'jsdom'
2-
import { AnalyticsBrowser, LegacySettings } from '..'
2+
import { AnalyticsBrowser, CDNSettings } from '..'
33
import { snippet } from '../../tester/__fixtures__/segment-snippet'
44
import { pWhile } from '../../lib/p-while'
55
import unfetch from 'unfetch'
66
import { RemoteMetrics } from '../../core/stats/remote-metrics'
77
import * as Factory from '../../test-helpers/factories'
88

9-
const cdnResponse: LegacySettings = {
9+
const cdnResponse: CDNSettings = {
1010
integrations: {
1111
Zapier: {
1212
type: 'server',

packages/browser/src/browser/__tests__/standalone.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import jsdom, { JSDOM } from 'jsdom'
22
import unfetch from 'unfetch'
3-
import { LegacySettings } from '..'
3+
import { CDNSettings } from '..'
44
import { pWhile } from '../../lib/p-while'
55
import { snippet } from '../../tester/__fixtures__/segment-snippet'
66
import * as Factory from '../../test-helpers/factories'
77
import { getGlobalAnalytics } from '../..'
88

9-
const cdnResponse: LegacySettings = {
9+
const cdnResponse: CDNSettings = {
1010
integrations: {
1111
Zapier: {
1212
type: 'server',

packages/browser/src/browser/index.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,29 @@ import { attachInspector } from '../core/inspector'
3636
import { Stats } from '../core/stats'
3737
import { setGlobalAnalyticsKey } from '../lib/global-analytics-helper'
3838

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

4343
versionSettings?: {
4444
version?: string
4545
override?: string
46-
componentTypes?: Array<'browser' | 'android' | 'ios' | 'server'>
46+
componentTypes?: ('browser' | 'android' | 'ios' | 'server')[]
4747
}
4848

49-
bundlingStatus?: string
49+
/**
50+
* We know if an integration is device mode if it has `bundlingStatus: 'bundled'` and the `browser` componentType in `versionSettings`.
51+
* 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.
52+
*/
53+
bundlingStatus?: 'bundled' | 'unbundled'
5054

5155
/**
5256
* Consent settings for the integration
5357
*/
5458
consentSettings?: {
5559
/**
5660
* Consent categories for the integration
57-
* @example ["Analytics", "Advertising", "CAT001"]
61+
* @example ["CAT001", "CAT002"]
5862
*/
5963
categories: string[]
6064
}
@@ -67,9 +71,13 @@ export interface LegacyIntegrationConfiguration {
6771
[key: string]: any
6872
}
6973

70-
export interface LegacySettings {
74+
/**
75+
* The remote settings object for a source, typically fetched from the Segment CDN.
76+
* Warning: this is an *unstable* object.
77+
*/
78+
export interface CDNSettings {
7179
integrations: {
72-
[name: string]: LegacyIntegrationConfiguration
80+
[creationName: string]: RemoteIntegrationSettings
7381
}
7482

7583
middlewareSettings?: {
@@ -109,7 +117,7 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {
109117
* If provided, `AnalyticsBrowser` will not fetch remote settings
110118
* for the source.
111119
*/
112-
cdnSettings?: LegacySettings & Record<string, unknown>
120+
cdnSettings?: CDNSettings & Record<string, unknown>
113121
/**
114122
* If provided, will override the default Segment CDN (https://cdn.segment.com) for this application.
115123
*/
@@ -119,7 +127,7 @@ export interface AnalyticsBrowserSettings extends AnalyticsSettings {
119127
export function loadLegacySettings(
120128
writeKey: string,
121129
cdnURL?: string
122-
): Promise<LegacySettings> {
130+
): Promise<CDNSettings> {
123131
const baseUrl = cdnURL ?? getCDN()
124132

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

140-
function hasLegacyDestinations(settings: LegacySettings): boolean {
148+
function hasLegacyDestinations(settings: CDNSettings): boolean {
141149
return (
142150
getProcessEnv().NODE_ENV !== 'test' &&
143151
// just one integration means segmentio
144152
Object.keys(settings.integrations).length > 1
145153
)
146154
}
147155

148-
function hasTsubMiddleware(settings: LegacySettings): boolean {
156+
function hasTsubMiddleware(settings: CDNSettings): boolean {
149157
return (
150158
getProcessEnv().NODE_ENV !== 'test' &&
151159
(settings.middlewareSettings?.routingRules?.length ?? 0) > 0
@@ -185,7 +193,7 @@ async function flushFinalBuffer(
185193

186194
async function registerPlugins(
187195
writeKey: string,
188-
legacySettings: LegacySettings,
196+
cdnSettings: CDNSettings,
189197
analytics: Analytics,
190198
options: InitOptions,
191199
pluginLikes: (Plugin | PluginFactory)[] = [],
@@ -201,24 +209,22 @@ async function registerPlugins(
201209
typeof pluginLike.pluginName === 'string'
202210
) as PluginFactory[]
203211

204-
const tsubMiddleware = hasTsubMiddleware(legacySettings)
212+
const tsubMiddleware = hasTsubMiddleware(cdnSettings)
205213
? await import(
206214
/* webpackChunkName: "tsub-middleware" */ '../plugins/routing-middleware'
207215
).then((mod) => {
208-
return mod.tsubMiddleware(
209-
legacySettings.middlewareSettings!.routingRules
210-
)
216+
return mod.tsubMiddleware(cdnSettings.middlewareSettings!.routingRules)
211217
})
212218
: undefined
213219

214220
const legacyDestinations =
215-
hasLegacyDestinations(legacySettings) || legacyIntegrationSources.length > 0
221+
hasLegacyDestinations(cdnSettings) || legacyIntegrationSources.length > 0
216222
? await import(
217223
/* webpackChunkName: "ajs-destination" */ '../plugins/ajs-destination'
218224
).then((mod) => {
219225
return mod.ajsDestinations(
220226
writeKey,
221-
legacySettings,
227+
cdnSettings,
222228
analytics.integrations,
223229
options,
224230
tsubMiddleware,
@@ -227,7 +233,7 @@ async function registerPlugins(
227233
})
228234
: []
229235

230-
if (legacySettings.legacyVideoPluginsEnabled) {
236+
if (cdnSettings.legacyVideoPluginsEnabled) {
231237
await import(
232238
/* webpackChunkName: "legacyVideos" */ '../plugins/legacy-video-plugins'
233239
).then((mod) => {
@@ -239,13 +245,13 @@ async function registerPlugins(
239245
? await import(
240246
/* webpackChunkName: "schemaFilter" */ '../plugins/schema-filter'
241247
).then((mod) => {
242-
return mod.schemaFilter(options.plan?.track, legacySettings)
248+
return mod.schemaFilter(options.plan?.track, cdnSettings)
243249
})
244250
: undefined
245251

246-
const mergedSettings = mergedOptions(legacySettings, options)
252+
const mergedSettings = mergedOptions(cdnSettings, options)
247253
const remotePlugins = await remoteLoader(
248-
legacySettings,
254+
cdnSettings,
249255
analytics.integrations,
250256
mergedSettings,
251257
options,
@@ -274,15 +280,15 @@ async function registerPlugins(
274280
await segmentio(
275281
analytics,
276282
mergedSettings['Segment.io'] as SegmentioSettings,
277-
legacySettings.integrations
283+
cdnSettings.integrations
278284
)
279285
)
280286
}
281287

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

284290
if (
285-
Object.entries(legacySettings.enabledMiddleware ?? {}).some(
291+
Object.entries(cdnSettings.enabledMiddleware ?? {}).some(
286292
([, enabled]) => enabled
287293
)
288294
) {
@@ -291,7 +297,7 @@ async function registerPlugins(
291297
).then(async ({ remoteMiddlewares }) => {
292298
const middleware = await remoteMiddlewares(
293299
ctx,
294-
legacySettings,
300+
cdnSettings,
295301
options.obfuscate
296302
)
297303
const promises = middleware.map((mdw) =>

packages/browser/src/core/analytics/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { PriorityQueue } from '../../lib/priority-queue'
4141
import { getGlobal } from '../../lib/get-global'
4242
import { AnalyticsClassic, AnalyticsCore } from './interfaces'
4343
import { HighEntropyHint } from '../../lib/client-hints/interfaces'
44-
import type { LegacySettings } from '../../browser'
44+
import type { CDNSettings } from '../../browser'
4545
import {
4646
CookieOptions,
4747
MemoryStorage,
@@ -109,7 +109,7 @@ export interface InitOptions {
109109
* This callback allows you to update/mutate CDN Settings.
110110
* This is called directly after settings are fetched from the CDN.
111111
*/
112-
updateCDNSettings?: (settings: LegacySettings) => LegacySettings
112+
updateCDNSettings?: (settings: CDNSettings) => CDNSettings
113113
/**
114114
* Disables or sets constraints on processing of query string parameters
115115
*/
@@ -143,9 +143,7 @@ export interface InitOptions {
143143
* disable: (cdnSettings) => cdnSettings.foo === 'bar'
144144
* ```
145145
*/
146-
disable?:
147-
| boolean
148-
| ((cdnSettings: LegacySettings) => boolean | Promise<boolean>)
146+
disable?: boolean | ((cdnSettings: CDNSettings) => boolean | Promise<boolean>)
149147
}
150148

151149
/* analytics-classic stubs */

packages/browser/src/lib/merged-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSONObject, Options } from '../core/events/interfaces'
2-
import { LegacySettings } from '../browser'
2+
import { CDNSettings } from '../browser'
33

44
/**
55
* Merge legacy settings and initialized Integration option overrides.
@@ -11,7 +11,7 @@ import { LegacySettings } from '../browser'
1111
* the Analytics constructor.
1212
*/
1313
export function mergedOptions(
14-
settings: LegacySettings,
14+
cdnSettings: CDNSettings,
1515
options: Options
1616
): Record<string, JSONObject> {
1717
const optionOverrides = Object.entries(options.integrations ?? {}).reduce(
@@ -31,7 +31,7 @@ export function mergedOptions(
3131
{} as Record<string, JSONObject>
3232
)
3333

34-
return Object.entries(settings.integrations).reduce(
34+
return Object.entries(cdnSettings.integrations).reduce(
3535
(integrationSettings, [integration, settings]) => {
3636
return {
3737
...integrationSettings,

packages/browser/src/plugins/ajs-destination/__tests__/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import jsdom from 'jsdom'
33
import unfetch from 'unfetch'
44
import { ajsDestinations, LegacyDestination } from '..'
55
import { Analytics } from '../../../core/analytics'
6-
import { LegacySettings } from '../../../browser'
6+
import { CDNSettings } from '../../../browser'
77
import { Context } from '../../../core/context'
88
import { Plan } from '../../../core/events'
99
import { tsubMiddleware } from '../../routing-middleware'
1010
import { AMPLITUDE_WRITEKEY } from '../../../test-helpers/test-writekeys'
1111
import { PersistedPriorityQueue } from '../../../lib/priority-queue/persisted'
1212
import * as Factory from '../../../test-helpers/factories'
1313

14-
const cdnResponse: LegacySettings = {
14+
const cdnResponse: CDNSettings = {
1515
integrations: {
1616
Zapier: {
1717
type: 'server',

packages/browser/src/plugins/ajs-destination/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Integrations, JSONObject } from '../../core/events'
22
import { Alias, Facade, Group, Identify, Page, Track } from '@segment/facade'
33
import { Analytics, InitOptions } from '../../core/analytics'
4-
import { LegacySettings } from '../../browser'
4+
import { CDNSettings } from '../../browser'
55
import { isOffline, isOnline } from '../../core/connection'
66
import { Context, ContextCancelation } from '../../core/context'
77
import { isServer } from '../../core/environment'
@@ -331,7 +331,7 @@ export class LegacyDestination implements InternalPluginWithAddMiddleware {
331331

332332
export function ajsDestinations(
333333
writeKey: string,
334-
settings: LegacySettings,
334+
settings: CDNSettings,
335335
globalIntegrations: Integrations = {},
336336
options: InitOptions = {},
337337
routingMiddleware?: DestinationMiddlewareFunction,

packages/browser/src/plugins/ajs-destination/loader.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Analytics } from '../../core/analytics'
2-
import { LegacyIntegrationConfiguration } from '../../browser'
2+
import { RemoteIntegrationSettings } from '../../browser'
33
import { getNextIntegrationsURL } from '../../lib/parse-cdn'
44
import { Context } from '../../core/context'
55
import { User } from '../../core/user'
@@ -119,11 +119,11 @@ export async function unloadIntegration(
119119
}
120120

121121
export function resolveVersion(
122-
settings?: LegacyIntegrationConfiguration
122+
integrationConfig?: RemoteIntegrationSettings
123123
): string {
124124
return (
125-
settings?.versionSettings?.override ??
126-
settings?.versionSettings?.version ??
125+
integrationConfig?.versionSettings?.override ??
126+
integrationConfig?.versionSettings?.version ??
127127
'latest'
128128
)
129129
}

0 commit comments

Comments
 (0)