11import { Integrations , JSONObject } from '@/core/events'
22import { Alias , Facade , Group , Identify , Page , Track } from '@segment/facade'
33import { Analytics , InitOptions } from '../../core/analytics'
4- import { LegacySettings } from '../../browser'
4+ import { LegacyIntegrationConfiguration , LegacySettings } from '../../browser'
55import { isOffline , isOnline } from '../../core/connection'
66import { Context , ContextCancelation } from '../../core/context'
77import { isServer } from '../../core/environment'
@@ -20,10 +20,16 @@ import {
2020import {
2121 buildIntegration ,
2222 loadIntegration ,
23+ resolveIntegrationNameFromSource ,
2324 resolveVersion ,
2425 unloadIntegration ,
2526} from './loader'
2627import { LegacyIntegration , LegacyIntegrationSource } from './types'
28+ import { isPlainObject } from '@segment/analytics-core'
29+ import {
30+ isDisabledIntegration as shouldSkipIntegration ,
31+ isInstallableIntegration ,
32+ } from './utils'
2733
2834export type ClassType < T > = new ( ...args : unknown [ ] ) => T
2935
@@ -331,56 +337,49 @@ export function ajsDestinations(
331337 }
332338
333339 const routingRules = settings . middlewareSettings ?. routingRules ?? [ ]
334-
340+ const remoteIntegrationsConfig = settings . integrations
341+ const localIntegrationsConfig = options . integrations
335342 // merged remote CDN settings with user provided options
336343 const integrationOptions = mergedOptions ( settings , options ?? { } ) as Record <
337344 string ,
338345 JSONObject
339346 >
340347
341- return Object . entries ( settings . integrations )
342- . map ( ( [ name , integrationSettings ] ) => {
343- if ( name . startsWith ( 'Segment' ) ) {
344- return
345- }
346-
347- const allDisableAndNotDefined =
348- globalIntegrations . All === false &&
349- globalIntegrations [ name ] === undefined
350-
351- if ( globalIntegrations [ name ] === false || allDisableAndNotDefined ) {
352- return
353- }
354-
355- const { type, bundlingStatus, versionSettings } = integrationSettings
356- // We use `!== 'unbundled'` (versus `=== 'bundled'`) to be inclusive of
357- // destinations without a defined value for `bundlingStatus`
358- const deviceMode =
359- bundlingStatus !== 'unbundled' &&
360- ( type === 'browser' ||
361- versionSettings ?. componentTypes ?. includes ( 'browser' ) )
362-
363- // checking for iterable is a quick fix we need in place to prevent
364- // errors showing Iterable as a failed destiantion. Ideally, we should
365- // fix the Iterable metadata instead, but that's a longer process.
366- if ( ( ! deviceMode && name !== 'Segment.io' ) || name === 'Iterable' ) {
367- return
368- }
348+ const adhocIntegrationSources = legacyIntegrationSources ?. reduce (
349+ ( acc , integrationSource ) => ( {
350+ ...acc ,
351+ [ resolveIntegrationNameFromSource ( integrationSource ) ] : integrationSource ,
352+ } ) ,
353+ { } as Record < string , LegacyIntegrationSource >
354+ )
369355
370- const integrationSource = legacyIntegrationSources ?. find (
371- ( integrationSource ) =>
372- ( 'Integration' in integrationSource
373- ? integrationSource . Integration
374- : integrationSource
375- ) . prototype . name === name
356+ const installableIntegrations = new Set ( [
357+ // Remotely configured installable integrations
358+ ...Object . entries ( remoteIntegrationsConfig )
359+ . filter ( ( [ name , integrationSettings ] ) =>
360+ isInstallableIntegration ( name , integrationSettings )
376361 )
362+ . map ( ( [ name ] ) => name ) ,
363+
364+ // Directly provided integration sources are only installable if settings for them are available
365+ ...Object . keys ( adhocIntegrationSources || { } ) . filter (
366+ ( name ) =>
367+ isPlainObject ( remoteIntegrationsConfig [ name ] ) ||
368+ isPlainObject ( localIntegrationsConfig ?. [ name ] )
369+ ) ,
370+ ] )
371+
372+ return Array . from ( installableIntegrations )
373+ . filter ( ( name ) => ! shouldSkipIntegration ( name , globalIntegrations ) )
374+ . map ( ( name ) => {
375+ const integrationSettings = remoteIntegrationsConfig [ name ]
377376 const version = resolveVersion ( integrationSettings )
378377 const destination = new LegacyDestination (
379378 name ,
380379 version ,
381380 integrationOptions [ name ] ,
382381 options ,
383- integrationSource
382+ adhocIntegrationSources ?. [ name ]
384383 )
385384
386385 const routing = routingRules . filter (
@@ -392,5 +391,4 @@ export function ajsDestinations(
392391
393392 return destination
394393 } )
395- . filter ( ( xt ) => xt !== undefined ) as LegacyDestination [ ]
396394}
0 commit comments