UNPKG

3.42 kBJavaScriptView Raw
1import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
2import { arrayify, logger } from '@sentry/utils';
3
4var installedIntegrations = [];
5
6/** Map of integrations assigned to a client */
7
8/**
9 * Remove duplicates from the given array, preferring the last instance of any duplicate. Not guaranteed to
10 * preseve the order of integrations in the array.
11 *
12 * @private
13 */
14function filterDuplicates(integrations) {
15 var integrationsByName = {};
16
17 integrations.forEach(currentInstance => {
18 const { name } = currentInstance;
19
20 var existingInstance = integrationsByName[name];
21
22 // We want integrations later in the array to overwrite earlier ones of the same type, except that we never want a
23 // default instance to overwrite an existing user instance
24 if (existingInstance && !existingInstance.isDefaultInstance && currentInstance.isDefaultInstance) {
25 return;
26 }
27
28 integrationsByName[name] = currentInstance;
29 });
30
31 return Object.values(integrationsByName);
32}
33
34/** Gets integrations to install */
35function getIntegrationsToSetup(options) {
36 var defaultIntegrations = options.defaultIntegrations || [];
37 var userIntegrations = options.integrations;
38
39 // We flag default instances, so that later we can tell them apart from any user-created instances of the same class
40 defaultIntegrations.forEach(integration => {
41 integration.isDefaultInstance = true;
42 });
43
44 let integrations;
45
46 if (Array.isArray(userIntegrations)) {
47 integrations = [...defaultIntegrations, ...userIntegrations];
48 } else if (typeof userIntegrations === 'function') {
49 integrations = arrayify(userIntegrations(defaultIntegrations));
50 } else {
51 integrations = defaultIntegrations;
52 }
53
54 var finalIntegrations = filterDuplicates(integrations);
55
56 // The `Debug` integration prints copies of the `event` and `hint` which will be passed to `beforeSend`. It therefore
57 // has to run after all other integrations, so that the changes of all event processors will be reflected in the
58 // printed values. For lack of a more elegant way to guarantee that, we therefore locate it and, assuming it exists,
59 // pop it out of its current spot and shove it onto the end of the array.
60 var debugIndex = finalIntegrations.findIndex(integration => integration.name === 'Debug');
61 if (debugIndex !== -1) {
62 const [debugInstance] = finalIntegrations.splice(debugIndex, 1);
63 finalIntegrations.push(debugInstance);
64 }
65
66 return finalIntegrations;
67}
68
69/**
70 * Given a list of integration instances this installs them all. When `withDefaults` is set to `true` then all default
71 * integrations are added unless they were already provided before.
72 * @param integrations array of integration instances
73 * @param withDefault should enable default integrations
74 */
75function setupIntegrations(integrations) {
76 var integrationIndex = {};
77
78 integrations.forEach(integration => {
79 integrationIndex[integration.name] = integration;
80
81 if (installedIntegrations.indexOf(integration.name) === -1) {
82 integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
83 installedIntegrations.push(integration.name);
84 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`Integration installed: ${integration.name}`);
85 }
86 });
87
88 return integrationIndex;
89}
90
91export { getIntegrationsToSetup, installedIntegrations, setupIntegrations };
92//# sourceMappingURL=integration.js.map