-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathmain.ts
More file actions
182 lines (158 loc) · 5.79 KB
/
main.ts
File metadata and controls
182 lines (158 loc) · 5.79 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import type { Settings } from '../common/extension.types'
import { EventListeners } from '../common/eventListeners'
import { DEV_LOGS_URL, DEV_RUM_SLIM_URL, DEV_RUM_URL } from '../common/packagesUrlConstants'
import { SESSION_STORAGE_SETTINGS_KEY } from '../common/sessionKeyConstant'
const windowWithSdkGlobals = window as Window & {
DD_RUM?: SdkPublicApi
DD_LOGS?: SdkPublicApi
__ddBrowserSdkExtensionCallback?: (message: unknown) => void
}
interface SdkPublicApi {
[key: string]: (...args: any[]) => unknown
}
export function main() {
// Prevent multiple executions when the devetools are reconnecting
if (windowWithSdkGlobals.__ddBrowserSdkExtensionCallback) {
return
}
sendEventsToExtension()
const settings = getSettings()
if (
settings &&
// Avoid instrumenting SDK global variables if the SDKs are already loaded.
// This happens when the page is loaded and then the devtools are opened.
noBrowserSdkLoaded()
) {
const ddRumGlobal = instrumentGlobal('DD_RUM')
const ddLogsGlobal = instrumentGlobal('DD_LOGS')
if (settings.debugMode) {
setDebug(ddRumGlobal)
setDebug(ddLogsGlobal)
}
if (settings.rumConfigurationOverride) {
overrideInitConfiguration(ddRumGlobal, settings.rumConfigurationOverride)
}
if (settings.logsConfigurationOverride) {
overrideInitConfiguration(ddLogsGlobal, settings.logsConfigurationOverride)
}
if (settings.useDevBundles === 'npm') {
injectDevBundle(settings.useRumSlim ? DEV_RUM_SLIM_URL : DEV_RUM_URL, ddRumGlobal)
injectDevBundle(DEV_LOGS_URL, ddLogsGlobal)
}
}
}
function sendEventsToExtension() {
// This script is executed in the "main" execution world, the same world as the webpage. Thus, it
// can define a global callback variable to listen to SDK events.
windowWithSdkGlobals.__ddBrowserSdkExtensionCallback = (message: unknown) => {
// Relays any message to the "isolated" content-script via a custom event.
window.dispatchEvent(
new CustomEvent('__ddBrowserSdkMessage', {
detail: message,
})
)
}
}
function getSettings() {
try {
// sessionStorage access throws in sandboxed iframes
const stringSettings = sessionStorage.getItem(SESSION_STORAGE_SETTINGS_KEY)
// JSON.parse throws if the stringSettings is not a valid JSON
return JSON.parse(stringSettings || 'null') as Settings | null
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error getting settings', error)
}
}
function noBrowserSdkLoaded() {
return !windowWithSdkGlobals.DD_RUM && !windowWithSdkGlobals.DD_LOGS
}
function injectDevBundle(url: string, global: GlobalInstrumentation) {
loadSdkScriptFromURL(url)
const devInstance = global.get() as SdkPublicApi
if (devInstance) {
global.onSet((sdkInstance) => proxySdk(sdkInstance, devInstance))
global.returnValue(devInstance)
}
}
function setDebug(global: GlobalInstrumentation) {
global.onSet((sdkInstance) => {
// Ensure the sdkInstance has a '_setDebug' method, excluding async stubs.
if ('_setDebug' in sdkInstance) {
sdkInstance._setDebug(true)
}
})
}
function overrideInitConfiguration(global: GlobalInstrumentation, configurationOverride: object) {
global.onSet((sdkInstance) => {
// Ensure the sdkInstance has an 'init' method, excluding async stubs.
if ('init' in sdkInstance) {
const originalInit = sdkInstance.init
sdkInstance.init = (config: any) => {
originalInit({
...config,
...configurationOverride,
allowedTrackingOrigins: [location.origin],
})
}
}
})
}
function loadSdkScriptFromURL(url: string) {
const xhr = new XMLHttpRequest()
try {
xhr.open('GET', url, false) // `false` makes the request synchronous
xhr.send()
} catch (error) {
// eslint-disable-next-line no-console
console.error(`[DD Browser SDK extension] Error while loading ${url}:`, error)
return
}
if (xhr.status === 200) {
let sdkCode = xhr.responseText
// Webpack expects the script to be loaded with a `<script src="...">` tag to get its URL to
// know where to load the relative chunks. By loading it with an XHR and evaluating it in an
// inline script tag, Webpack does not know where to load the chunks from.
//
// Let's replace Webpack logic that breaks with our own logic to define the URL. It's not
// pretty, but loading the script this way isn't either, so...
//
// We'll probably have to revisit when using actual `import()` expressions instead of relying on
// Webpack runtime to load the chunks.
sdkCode = sdkCode.replace(
'if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");',
`if (!scriptUrl) scriptUrl = ${JSON.stringify(url)};`
)
const script = document.createElement('script')
script.type = 'text/javascript'
script.text = sdkCode
document.documentElement.prepend(script)
}
}
type GlobalInstrumentation = ReturnType<typeof instrumentGlobal>
function instrumentGlobal(global: 'DD_RUM' | 'DD_LOGS') {
const eventListeners = new EventListeners<SdkPublicApi>()
let returnedInstance: SdkPublicApi | undefined
let lastInstance: SdkPublicApi | undefined
Object.defineProperty(window, global, {
set(sdkInstance: SdkPublicApi) {
eventListeners.notify(sdkInstance)
lastInstance = sdkInstance
},
get(): SdkPublicApi | undefined {
return returnedInstance ?? lastInstance
},
})
return {
get: () => windowWithSdkGlobals[global],
onSet: (callback: (sdkInstance: SdkPublicApi) => void) => {
eventListeners.subscribe(callback)
},
returnValue: (sdkInstance: SdkPublicApi) => {
returnedInstance = sdkInstance
},
}
}
function proxySdk(target: SdkPublicApi, root: SdkPublicApi) {
Object.assign(target, root)
}