-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Prebid core: automatic dynamic loading of debugging module #8106
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f8393b2
WIP: move all debugging logic to the debugging module
dgirardi b616d0b
Dynamic loading of modules
dgirardi 8760a30
Merge branch 'debugging-in-debugging' into debugging
dgirardi d042bd3
Move all debugging logic to the debugging module
dgirardi 1db2ec3
Automatically load debugging module when needed
dgirardi 8d1e1ca
Build and dynamically load "standalone" version of debugging module
dgirardi 1479b88
Merge branch 'master' into debugging
dgirardi 88ed886
Merge branch 'master' into debugging
dgirardi 8d243d8
add note to PR_REVIEW
dgirardi 55018a7
use loadExternalScript; only add debugging hooks when needed
dgirardi 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## Warning | ||
|
||
This module is also packaged as a "standalone" .js file and loaded dynamically by prebid-core when debugging configuration is passed to `setConfig` or loaded from session storage. | ||
|
||
"Standalone" means that it does not have a compile-time dependency on `prebid-core.js` and can therefore work even if it was not built together with it (as would be the case when Prebid is pulled from npm). | ||
|
||
Because of this, **this module cannot freely import symbols from core**: anything that depends on Prebid global state (which includes, but is not limited to, `config`, `auctionManager`, `adapterManager`, etc) would *not* work as expected. | ||
|
||
Imports must be limited to logic that is stateless and free of side effects; symbols from `utils.js` are mostly OK, with the notable exception of logging functions (which have a dependency on `config`). | ||
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import {deepClone, delayExecution} from '../../src/utils.js'; | ||
import {BidInterceptor} from './bidInterceptor.js'; | ||
import {makePbsInterceptor} from './pbsInterceptor.js'; | ||
import {addHooks, removeHooks} from './legacy.js'; | ||
|
||
const interceptorHooks = []; | ||
let bidInterceptor; | ||
let enabled = false; | ||
|
||
function enableDebugging(debugConfig, {fromSession = false, config, hook, logger}) { | ||
config.setConfig({debug: true}); | ||
bidInterceptor.updateConfig(debugConfig); | ||
resetHooks(true); | ||
// also enable "legacy" overrides | ||
removeHooks({hook}); | ||
addHooks(debugConfig, {hook, logger}); | ||
if (!enabled) { | ||
enabled = true; | ||
logger.logMessage(`Debug overrides enabled${fromSession ? ' from session' : ''}`); | ||
} | ||
} | ||
|
||
export function disableDebugging({hook, logger}) { | ||
bidInterceptor.updateConfig(({})); | ||
resetHooks(false); | ||
// also disable "legacy" overrides | ||
removeHooks({hook}); | ||
if (enabled) { | ||
enabled = false; | ||
logger.logMessage('Debug overrides disabled'); | ||
} | ||
} | ||
|
||
function saveDebuggingConfig(debugConfig, {sessionStorage = window.sessionStorage, DEBUG_KEY} = {}) { | ||
if (!debugConfig.enabled) { | ||
try { | ||
sessionStorage.removeItem(DEBUG_KEY); | ||
} catch (e) { | ||
} | ||
} else { | ||
if (debugConfig.intercept) { | ||
debugConfig = deepClone(debugConfig); | ||
debugConfig.intercept = bidInterceptor.serializeConfig(debugConfig.intercept); | ||
} | ||
try { | ||
sessionStorage.setItem(DEBUG_KEY, JSON.stringify(debugConfig)); | ||
} catch (e) { | ||
} | ||
} | ||
} | ||
|
||
export function getConfig(debugging, {sessionStorage = window.sessionStorage, DEBUG_KEY, config, hook, logger} = {}) { | ||
if (debugging == null) return; | ||
saveDebuggingConfig(debugging, {sessionStorage, DEBUG_KEY}); | ||
if (!debugging.enabled) { | ||
disableDebugging({hook, logger}); | ||
} else { | ||
enableDebugging(debugging, {config, hook, logger}); | ||
} | ||
} | ||
|
||
export function sessionLoader({DEBUG_KEY, storage, config, hook, logger}) { | ||
let overrides; | ||
try { | ||
storage = storage || window.sessionStorage; | ||
overrides = JSON.parse(storage.getItem(DEBUG_KEY)); | ||
} catch (e) { | ||
} | ||
if (overrides) { | ||
enableDebugging(overrides, {fromSession: true, config, hook, logger}); | ||
} | ||
} | ||
|
||
function resetHooks(enable) { | ||
interceptorHooks.forEach(([getHookFn, interceptor]) => { | ||
getHookFn().getHooks({hook: interceptor}).remove(); | ||
}); | ||
if (enable) { | ||
interceptorHooks.forEach(([getHookFn, interceptor]) => { | ||
getHookFn().before(interceptor); | ||
}); | ||
} | ||
} | ||
|
||
function registerBidInterceptor(getHookFn, interceptor) { | ||
const interceptBids = (...args) => bidInterceptor.intercept(...args); | ||
interceptorHooks.push([getHookFn, function (next, ...args) { | ||
interceptor(next, interceptBids, ...args); | ||
}]); | ||
} | ||
|
||
export function bidderBidInterceptor(next, interceptBids, spec, bids, bidRequest, ajax, wrapCallback, cbs) { | ||
const done = delayExecution(cbs.onCompletion, 2); | ||
({bids, bidRequest} = interceptBids({bids, bidRequest, addBid: cbs.onBid, done})); | ||
if (bids.length === 0) { | ||
done(); | ||
} else { | ||
next(spec, bids, bidRequest, ajax, wrapCallback, {...cbs, onCompletion: done}); | ||
} | ||
} | ||
|
||
export function install({DEBUG_KEY, config, hook, createBid, logger}) { | ||
bidInterceptor = new BidInterceptor({logger}); | ||
const pbsBidInterceptor = makePbsInterceptor({createBid}); | ||
registerBidInterceptor(() => hook.get('processBidderRequests'), bidderBidInterceptor); | ||
registerBidInterceptor(() => hook.get('processPBSRequest'), pbsBidInterceptor); | ||
sessionLoader({DEBUG_KEY, config, hook, logger}); | ||
config.getConfig('debugging', ({debugging}) => getConfig(debugging, {DEBUG_KEY, config, hook, logger}), {init: true}); | ||
} |
This file contains 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,62 +1,7 @@ | ||
import {deepClone, delayExecution} from '../../src/utils.js'; | ||
import {processBidderRequests} from '../../src/adapters/bidderFactory.js'; | ||
import {BidInterceptor} from './bidInterceptor.js'; | ||
import {config} from '../../src/config.js'; | ||
import {hook} from '../../src/hook.js'; | ||
import {pbsBidInterceptor} from './pbsInterceptor.js'; | ||
import { | ||
onDisableOverrides, | ||
onEnableOverrides, | ||
saveDebuggingConfig | ||
} from '../../src/debugging.js'; | ||
import {install} from './debugging.js'; | ||
import {prefixLog} from '../../src/utils.js'; | ||
import {createBid} from '../../src/bidfactory.js'; | ||
|
||
const interceptorHooks = []; | ||
const bidInterceptor = new BidInterceptor(); | ||
|
||
saveDebuggingConfig.before(function (next, debugConfig, ...args) { | ||
if (debugConfig.intercept) { | ||
debugConfig = deepClone(debugConfig); | ||
debugConfig.intercept = bidInterceptor.serializeConfig(debugConfig.intercept); | ||
} | ||
next(debugConfig, ...args); | ||
}); | ||
|
||
function resetHooks(enable) { | ||
interceptorHooks.forEach(([getHookFn, interceptor]) => { | ||
getHookFn().getHooks({hook: interceptor}).remove(); | ||
}); | ||
if (enable) { | ||
interceptorHooks.forEach(([getHookFn, interceptor]) => { | ||
getHookFn().before(interceptor); | ||
}) | ||
} | ||
} | ||
|
||
onEnableOverrides.push((overrides) => { | ||
bidInterceptor.updateConfig(overrides); | ||
resetHooks(true); | ||
}); | ||
|
||
onDisableOverrides.push(() => { | ||
bidInterceptor.updateConfig({}); | ||
resetHooks(false); | ||
}) | ||
|
||
function registerBidInterceptor(getHookFn, interceptor) { | ||
const interceptBids = (...args) => bidInterceptor.intercept(...args); | ||
interceptorHooks.push([getHookFn, function (next, ...args) { | ||
interceptor(next, interceptBids, ...args) | ||
}]); | ||
} | ||
|
||
export function bidderBidInterceptor(next, interceptBids, spec, bids, bidRequest, ajax, wrapCallback, cbs) { | ||
const done = delayExecution(cbs.onCompletion, 2); | ||
({bids, bidRequest} = interceptBids({bids, bidRequest, addBid: cbs.onBid, done})); | ||
if (bids.length === 0) { | ||
done(); | ||
} else { | ||
next(spec, bids, bidRequest, ajax, wrapCallback, {...cbs, onCompletion: done}); | ||
} | ||
} | ||
|
||
registerBidInterceptor(() => processBidderRequests, bidderBidInterceptor); | ||
registerBidInterceptor(() => hook.get('processPBSRequest'), pbsBidInterceptor); | ||
install({config, hook, createBid, logger: prefixLog('DEBUG:')}); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are no import guards that can be setup at build time, then this may be something to mention in the PR_REVIEW doc for others to be on the look out for.