-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.ts
More file actions
148 lines (135 loc) · 5.54 KB
/
Copy pathindex.ts
File metadata and controls
148 lines (135 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable no-param-reassign */
import type { ApplicationState } from '@rudderstack/analytics-js-common/types/ApplicationState';
import type {
IPluginsManager,
PluginName,
} from '@rudderstack/analytics-js-common/types/PluginsManager';
import type { IExternalSrcLoader } from '@rudderstack/analytics-js-common/services/ExternalSrcLoader/types';
import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
import type { ExtensionPlugin } from '@rudderstack/analytics-js-common/types/PluginEngine';
import type { IErrorHandler } from '@rudderstack/analytics-js-common/types/ErrorHandler';
import type { Destination } from '@rudderstack/analytics-js-common/types/Destination';
import {
isDestinationSDKMounted,
initializeDestination,
applySourceConfigurationOverrides,
filterDisabledDestination,
} from './utils';
import { DEVICE_MODE_DESTINATIONS_PLUGIN, SCRIPT_LOAD_TIMEOUT_MS } from './constants';
import { DESTINATION_NOT_SUPPORTED_ERROR, DESTINATION_SDK_LOAD_ERROR } from './logMessages';
import {
destDisplayNamesToFileNamesMap,
filterDestinations,
} from '../shared-chunks/deviceModeDestinations';
const pluginName: PluginName = 'DeviceModeDestinations';
const DeviceModeDestinations = (): ExtensionPlugin => ({
name: pluginName,
initialize: (state: ApplicationState) => {
state.plugins.loadedPlugins.value = [...state.plugins.loadedPlugins.value, pluginName];
},
nativeDestinations: {
setActiveDestinations(
state: ApplicationState,
pluginsManager: IPluginsManager,
errorHandler?: IErrorHandler,
logger?: ILogger,
): void {
state.nativeDestinations.loadIntegration.value = state.loadOptions.value
.loadIntegration as boolean;
// Filter destination that doesn't have mapping config-->Integration names
const configSupportedDestinations =
state.nativeDestinations.configuredDestinations.value.filter((configDest: Destination) => {
if (destDisplayNamesToFileNamesMap[configDest.displayName]) {
return true;
}
errorHandler?.onError(
new Error(DESTINATION_NOT_SUPPORTED_ERROR(configDest.userFriendlyId)),
DEVICE_MODE_DESTINATIONS_PLUGIN,
);
return false;
});
// Apply source configuration overrides if provided
const destinationsWithOverrides = state.loadOptions.value.sourceConfigurationOverride
? applySourceConfigurationOverrides(
configSupportedDestinations,
state.loadOptions.value.sourceConfigurationOverride,
logger,
)
: filterDisabledDestination(configSupportedDestinations);
// Filter destinations that are disabled through load or consent API options
const destinationsToLoad = filterDestinations(
state.consents.postConsent.value?.integrations ??
state.nativeDestinations.loadOnlyIntegrations.value,
destinationsWithOverrides,
);
const consentedDestinations = destinationsToLoad.filter(
dest =>
// if consent manager is not configured, then default to load the destination
pluginsManager.invokeSingle(
`consentManager.isDestinationConsented`,
state,
dest.config,
errorHandler,
logger,
) ?? true,
);
state.nativeDestinations.activeDestinations.value = consentedDestinations;
},
load(
state: ApplicationState,
externalSrcLoader: IExternalSrcLoader,
errorHandler?: IErrorHandler,
logger?: ILogger,
externalScriptOnLoad?: (id?: string) => unknown,
) {
const integrationsCDNPath = state.lifecycle.integrationsCDNPath.value;
const activeDestinations = state.nativeDestinations.activeDestinations.value;
activeDestinations.forEach((dest: Destination) => {
const sdkName = destDisplayNamesToFileNamesMap[dest.displayName];
const destSDKIdentifier = `${sdkName}_RS`; // this is the name of the object loaded on the window
const sdkTypeName = sdkName;
if (sdkTypeName && !isDestinationSDKMounted(destSDKIdentifier, sdkTypeName, logger)) {
const destSdkURL = `${integrationsCDNPath}/${sdkName}.min.js`;
externalSrcLoader.loadJSFile({
url: destSdkURL,
id: dest.userFriendlyId,
callback:
externalScriptOnLoad ??
((id?: string) => {
if (!id) {
logger?.error(
DESTINATION_SDK_LOAD_ERROR(
DEVICE_MODE_DESTINATIONS_PLUGIN,
dest.userFriendlyId,
),
);
state.nativeDestinations.failedDestinations.value = [
...state.nativeDestinations.failedDestinations.value,
dest,
];
} else {
initializeDestination(
dest,
state,
destSDKIdentifier,
sdkTypeName,
errorHandler,
logger,
);
}
}),
timeout: SCRIPT_LOAD_TIMEOUT_MS,
});
} else if (sdkTypeName) {
initializeDestination(dest, state, destSDKIdentifier, sdkTypeName, errorHandler, logger);
} else {
logger?.error(
DESTINATION_SDK_LOAD_ERROR(DEVICE_MODE_DESTINATIONS_PLUGIN, dest.displayName),
);
}
});
},
},
});
export { DeviceModeDestinations };
export default DeviceModeDestinations;