|
| 1 | +import type { Client, Envelope, EventProcessor, Integration } from '@sentry/types'; |
| 2 | +import { logger, serializeEnvelope } from '@sentry/utils'; |
| 3 | + |
| 4 | +import { makeUtf8TextEncoder } from '../transports/TextEncoder'; |
| 5 | +import { ReactNativeLibraries } from '../utils/rnlibraries'; |
| 6 | + |
| 7 | +type SpotlightReactNativeIntegrationOptions = { |
| 8 | + /** |
| 9 | + * The URL of the Sidecar instance to connect and forward events to. |
| 10 | + * If not set, Spotlight will try to connect to the Sidecar running on localhost:8969. |
| 11 | + * |
| 12 | + * @default "http://localhost:8969/stream" |
| 13 | + */ |
| 14 | + sidecarUrl?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * Use this integration to send errors and transactions to Spotlight. |
| 19 | + * |
| 20 | + * Learn more about spotlight at https://spotlightjs.com |
| 21 | + */ |
| 22 | +export function Spotlight({ |
| 23 | + sidecarUrl = getDefaultSidecarUrl(), |
| 24 | +}: SpotlightReactNativeIntegrationOptions = {}): Integration { |
| 25 | + logger.info('[Spotlight] Using Sidecar URL', sidecarUrl); |
| 26 | + |
| 27 | + return { |
| 28 | + name: 'Spotlight', |
| 29 | + |
| 30 | + setupOnce(_: (callback: EventProcessor) => void, getCurrentHub) { |
| 31 | + const client = getCurrentHub().getClient(); |
| 32 | + if (client) { |
| 33 | + setup(client, sidecarUrl); |
| 34 | + } else { |
| 35 | + logger.warn('[Spotlight] Could not initialize Sidecar integration due to missing Client'); |
| 36 | + } |
| 37 | + }, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function setup(client: Client, sidecarUrl: string): void { |
| 42 | + sendEnvelopesToSidecar(client, sidecarUrl); |
| 43 | +} |
| 44 | + |
| 45 | +function sendEnvelopesToSidecar(client: Client, sidecarUrl: string): void { |
| 46 | + if (!client.on) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + client.on('beforeEnvelope', (originalEnvelope: Envelope) => { |
| 51 | + // TODO: This is a workaround for spotlight/sidecar not supporting images |
| 52 | + const spotlightEnvelope: Envelope = [...originalEnvelope]; |
| 53 | + const envelopeItems = [...originalEnvelope[1]].filter( |
| 54 | + item => typeof item[0].content_type !== 'string' || !item[0].content_type.startsWith('image'), |
| 55 | + ); |
| 56 | + |
| 57 | + spotlightEnvelope[1] = envelopeItems as Envelope[1]; |
| 58 | + |
| 59 | + fetch(sidecarUrl, { |
| 60 | + method: 'POST', |
| 61 | + body: serializeEnvelope(spotlightEnvelope, makeUtf8TextEncoder()), |
| 62 | + headers: { |
| 63 | + 'Content-Type': 'application/x-sentry-envelope', |
| 64 | + }, |
| 65 | + mode: 'cors', |
| 66 | + }).catch(err => { |
| 67 | + logger.error( |
| 68 | + "[Spotlight] Sentry SDK can't connect to Spotlight is it running? See https://spotlightjs.com to download it.", |
| 69 | + err, |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +function getDefaultSidecarUrl(): string { |
| 76 | + try { |
| 77 | + const { url } = ReactNativeLibraries.Devtools?.getDevServer(); |
| 78 | + return `http://${getHostnameFromString(url)}:8969/stream`; |
| 79 | + } catch (_oO) { |
| 80 | + // We can't load devserver URL |
| 81 | + } |
| 82 | + return 'http://localhost:8969/stream'; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * React Native implementation of the URL class is missing the `hostname` property. |
| 87 | + */ |
| 88 | +function getHostnameFromString(urlString: string): string | null { |
| 89 | + const regex = /^(?:\w+:)?\/\/([^/:]+)(:\d+)?(.*)$/; |
| 90 | + const matches = urlString.match(regex); |
| 91 | + |
| 92 | + if (matches && matches[1]) { |
| 93 | + return matches[1]; |
| 94 | + } else { |
| 95 | + // Invalid URL format |
| 96 | + return null; |
| 97 | + } |
| 98 | +} |
0 commit comments