1 | import { arrayify, logger } from '@sentry/utils';
|
2 | import { getCurrentHub } from './hub.js';
|
3 | import { addGlobalEventProcessor } from './scope.js';
|
4 |
|
5 | const installedIntegrations = [];
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function filterDuplicates(integrations) {
|
16 | const integrationsByName = {};
|
17 |
|
18 | integrations.forEach(currentInstance => {
|
19 | const { name } = currentInstance;
|
20 |
|
21 | const existingInstance = integrationsByName[name];
|
22 |
|
23 |
|
24 |
|
25 | if (existingInstance && !existingInstance.isDefaultInstance && currentInstance.isDefaultInstance) {
|
26 | return;
|
27 | }
|
28 |
|
29 | integrationsByName[name] = currentInstance;
|
30 | });
|
31 |
|
32 | return Object.keys(integrationsByName).map(k => integrationsByName[k]);
|
33 | }
|
34 |
|
35 |
|
36 | function getIntegrationsToSetup(options) {
|
37 | const defaultIntegrations = options.defaultIntegrations || [];
|
38 | const userIntegrations = options.integrations;
|
39 |
|
40 |
|
41 | defaultIntegrations.forEach(integration => {
|
42 | integration.isDefaultInstance = true;
|
43 | });
|
44 |
|
45 | let integrations;
|
46 |
|
47 | if (Array.isArray(userIntegrations)) {
|
48 | integrations = [...defaultIntegrations, ...userIntegrations];
|
49 | } else if (typeof userIntegrations === 'function') {
|
50 | integrations = arrayify(userIntegrations(defaultIntegrations));
|
51 | } else {
|
52 | integrations = defaultIntegrations;
|
53 | }
|
54 |
|
55 | const finalIntegrations = filterDuplicates(integrations);
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const debugIndex = findIndex(finalIntegrations, integration => integration.name === 'Debug');
|
62 | if (debugIndex !== -1) {
|
63 | const [debugInstance] = finalIntegrations.splice(debugIndex, 1);
|
64 | finalIntegrations.push(debugInstance);
|
65 | }
|
66 |
|
67 | return finalIntegrations;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | function setupIntegrations(integrations) {
|
77 | const integrationIndex = {};
|
78 |
|
79 | integrations.forEach(integration => {
|
80 |
|
81 | if (integration) {
|
82 | setupIntegration(integration, integrationIndex);
|
83 | }
|
84 | });
|
85 |
|
86 | return integrationIndex;
|
87 | }
|
88 |
|
89 |
|
90 | function setupIntegration(integration, integrationIndex) {
|
91 | integrationIndex[integration.name] = integration;
|
92 |
|
93 | if (installedIntegrations.indexOf(integration.name) === -1) {
|
94 | integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
|
95 | installedIntegrations.push(integration.name);
|
96 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log(`Integration installed: ${integration.name}`);
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 | function findIndex(arr, callback) {
|
102 | for (let i = 0; i < arr.length; i++) {
|
103 | if (callback(arr[i]) === true) {
|
104 | return i;
|
105 | }
|
106 | }
|
107 |
|
108 | return -1;
|
109 | }
|
110 |
|
111 | export { getIntegrationsToSetup, installedIntegrations, setupIntegration, setupIntegrations };
|
112 |
|