-
Notifications
You must be signed in to change notification settings - Fork 168
Make the Dev Ext capable of Injecting CDN SDK #4026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BeltranBulbarellaDD
wants to merge
10
commits into
main
Choose a base branch
from
beltran.bulbarella/inject_cdn_dev_extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9dc67dc
add injection
BeltranBulbarellaDD c2c6c85
format
BeltranBulbarellaDD 30fe9f7
change cdn urls
BeltranBulbarellaDD e78903a
rollback injection
BeltranBulbarellaDD bf29a41
Merge branch 'main' into beltran.bulbarella/inject_cdn_dev_extension
BeltranBulbarellaDD e1b1380
Fix player loading
BeltranBulbarellaDD 7faade4
linter
BeltranBulbarellaDD 3008361
fix badge logic
BeltranBulbarellaDD 2296601
fix injection and public path
BeltranBulbarellaDD b08c7a9
format
BeltranBulbarellaDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| 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 { | ||
| CDN_LOGS_URL, | ||
| CDN_RUM_SLIM_URL, | ||
| CDN_RUM_URL, | ||
| DEV_LOGS_URL, | ||
| DEV_RUM_SLIM_URL, | ||
| DEV_RUM_URL, | ||
| } from '../common/packagesUrlConstants' | ||
| import { SESSION_STORAGE_SETTINGS_KEY } from '../common/sessionKeyConstant' | ||
| import { createLogger } from '../common/logger' | ||
|
|
||
| const logger = createLogger('main') | ||
|
|
||
| declare global { | ||
| interface Window extends EventTarget { | ||
|
|
@@ -33,6 +43,8 @@ export function main() { | |
| ) { | ||
| const ddRumGlobal = instrumentGlobal('DD_RUM') | ||
| const ddLogsGlobal = instrumentGlobal('DD_LOGS') | ||
| const shouldInjectCdnBundles = settings.injectCdnProd === 'on' | ||
| const shouldUseRedirect = settings.useDevBundles === 'npm' | ||
|
|
||
| if (settings.debugMode) { | ||
| setDebug(ddRumGlobal) | ||
|
|
@@ -47,9 +59,16 @@ export function main() { | |
| overrideInitConfiguration(ddLogsGlobal, settings.logsConfigurationOverride) | ||
| } | ||
|
|
||
| if (settings.useDevBundles === 'npm') { | ||
| injectDevBundle(settings.useRumSlim ? DEV_RUM_SLIM_URL : DEV_RUM_URL, ddRumGlobal) | ||
| injectDevBundle(DEV_LOGS_URL, ddLogsGlobal) | ||
| if (shouldInjectCdnBundles && shouldUseRedirect) { | ||
| void injectCdnBundles({ useRumSlim: settings.useRumSlim }).then(() => { | ||
| injectDevBundle(settings.useRumSlim ? DEV_RUM_SLIM_URL : DEV_RUM_URL, ddRumGlobal) | ||
| injectDevBundle(DEV_LOGS_URL, ddLogsGlobal) | ||
| }) | ||
| } else if (shouldInjectCdnBundles) { | ||
| void injectCdnBundles({ useRumSlim: settings.useRumSlim }) | ||
| } else if (shouldUseRedirect) { | ||
| injectDevBundle(settings.useRumSlim ? DEV_RUM_SLIM_URL : DEV_RUM_URL, ddRumGlobal, getDefaultRumConfig()) | ||
| injectDevBundle(DEV_LOGS_URL, ddLogsGlobal, getDefaultLogsConfig()) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -83,11 +102,34 @@ function noBrowserSdkLoaded() { | |
| return !window.DD_RUM && !window.DD_LOGS | ||
| } | ||
|
|
||
| function injectDevBundle(url: string, global: GlobalInstrumentation) { | ||
| function injectDevBundle(url: string, global: GlobalInstrumentation, config?: object | null) { | ||
| const existingInstance = global.get() | ||
|
|
||
| let initConfig = config | ||
| if ( | ||
| existingInstance && | ||
| 'getInitConfiguration' in existingInstance && | ||
| typeof existingInstance.getInitConfiguration === 'function' | ||
| ) { | ||
| try { | ||
| initConfig = existingInstance.getInitConfiguration() || config | ||
| } catch { | ||
| initConfig = config | ||
| } | ||
| } | ||
|
|
||
| loadSdkScriptFromURL(url) | ||
| const devInstance = global.get() as SdkPublicApi | ||
|
|
||
| if (devInstance) { | ||
| if (initConfig && 'init' in devInstance && typeof devInstance.init === 'function') { | ||
| try { | ||
| ;(devInstance as { init(config: object): void }).init(initConfig) | ||
| } catch (error) { | ||
| logger.error('[DD Browser SDK extension] Error initializing dev bundle:', error) | ||
| } | ||
| } | ||
|
|
||
| global.onSet((sdkInstance) => proxySdk(sdkInstance, devInstance)) | ||
| global.returnValue(devInstance) | ||
| } | ||
|
|
@@ -140,11 +182,29 @@ function loadSdkScriptFromURL(url: string) { | |
| // | ||
| // We'll probably have to revisit when using actual `import()` expressions instead of relying on | ||
| // Webpack runtime to load the chunks. | ||
| // Extract the base directory URL from the full file URL. | ||
| const baseUrl = url.substring(0, url.lastIndexOf('/') + 1) | ||
|
|
||
| // Override webpack's scriptUrl detection in multiple places to ensure chunks load from dev server | ||
| // 1. Replace the error throw with our base URL | ||
| sdkCode = sdkCode.replace( | ||
| 'if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");', | ||
| `if (!scriptUrl) scriptUrl = ${JSON.stringify(url)};` | ||
| `if (!scriptUrl) scriptUrl = ${JSON.stringify(baseUrl)};` | ||
| ) | ||
|
|
||
| // 2. Set scriptUrl early if it's determined from document.currentScript or similar | ||
| sdkCode = sdkCode.replace(/var scriptUrl\s*=\s*[^;]+;/g, `var scriptUrl = ${JSON.stringify(baseUrl)};`) | ||
|
|
||
| // 3. Override __webpack_require__.p (publicPath) if it exists | ||
| sdkCode = sdkCode.replace( | ||
| /__webpack_require__\.p\s*=\s*[^;]+;/g, | ||
| `__webpack_require__.p = ${JSON.stringify(baseUrl)};` | ||
| ) | ||
|
||
|
|
||
| // 4. Inject publicPath override at the start of the webpack runtime | ||
| const publicPathOverride = `(function(){try{if(typeof __webpack_require__!=='undefined'){__webpack_require__.p=${JSON.stringify(baseUrl)};}}catch(e){}})();` | ||
| sdkCode = publicPathOverride + sdkCode | ||
|
|
||
| const script = document.createElement('script') | ||
| script.type = 'text/javascript' | ||
| script.text = sdkCode | ||
|
|
@@ -182,3 +242,89 @@ function instrumentGlobal(global: 'DD_RUM' | 'DD_LOGS') { | |
| function proxySdk(target: SdkPublicApi, root: SdkPublicApi) { | ||
| Object.assign(target, root) | ||
| } | ||
|
|
||
| function injectCdnBundles({ useRumSlim }: { useRumSlim: boolean }) { | ||
| const rumUrl = useRumSlim ? CDN_RUM_SLIM_URL : CDN_RUM_URL | ||
| const logsUrl = CDN_LOGS_URL | ||
|
|
||
| return injectWhenDocumentReady(() => | ||
| Promise.all([ | ||
| injectAndInitializeSDK(rumUrl, 'DD_RUM', getDefaultRumConfig()), | ||
| injectAndInitializeSDK(logsUrl, 'DD_LOGS', getDefaultLogsConfig()), | ||
| ]).then(() => undefined) | ||
| ) | ||
| } | ||
|
|
||
| function injectWhenDocumentReady<T>(callback: () => Promise<T> | T) { | ||
| if (document.readyState === 'loading') { | ||
| return new Promise<T>((resolve) => { | ||
| document.addEventListener( | ||
| 'DOMContentLoaded', | ||
| () => { | ||
| resolve(callback()) | ||
| }, | ||
| { once: true } | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| return Promise.resolve(callback()) | ||
| } | ||
|
|
||
| function injectAndInitializeSDK(url: string, globalName: 'DD_RUM' | 'DD_LOGS', config: object | null) { | ||
| if (window[globalName]) { | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| return new Promise<void>((resolve) => { | ||
| const script = document.createElement('script') | ||
| script.src = url | ||
| script.async = true | ||
| script.onload = () => { | ||
| const sdkGlobal = window[globalName] | ||
| if (config && sdkGlobal && 'init' in sdkGlobal) { | ||
| try { | ||
| ;(sdkGlobal as { init(config: object): void }).init(config) | ||
| } catch (error) { | ||
| // Ignore "already initialized" errors - this can happen when dev bundles override CDN bundles | ||
| const errorMessage = error instanceof Error ? error.message : String(error) | ||
| if (!errorMessage.includes('already initialized')) { | ||
| // Only log non-initialization errors | ||
| // eslint-disable-next-line no-console | ||
| console.error(`[DD Browser SDK extension] Error initializing ${globalName}:`, error) | ||
| } | ||
| } | ||
| } | ||
| resolve() | ||
| } | ||
| script.onerror = () => { | ||
| resolve() | ||
| } | ||
|
|
||
| try { | ||
| document.head.appendChild(script) | ||
| } catch { | ||
| document.documentElement.appendChild(script) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function getDefaultRumConfig() { | ||
| return { | ||
| applicationId: 'xxx', | ||
| clientToken: 'xxx', | ||
| site: 'datadoghq.com', | ||
| service: 'browser-sdk-extension', | ||
| allowedTrackingOrigins: [location.origin], | ||
| sessionReplaySampleRate: 100, | ||
BeltranBulbarellaDD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| function getDefaultLogsConfig() { | ||
| return { | ||
| clientToken: 'xxx', | ||
| site: 'datadoghq.com', | ||
| service: 'browser-sdk-extension', | ||
| allowedTrackingOrigins: [location.origin], | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.