forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prebid core: automatic dynamic loading of debugging module (prebid#8106)
* WIP: move all debugging logic to the debugging module * Dynamic loading of modules * Move all debugging logic to the debugging module * Automatically load debugging module when needed * Build and dynamically load "standalone" version of debugging module * add note to PR_REVIEW * use loadExternalScript; only add debugging hooks when needed
- Loading branch information
Showing
22 changed files
with
697 additions
and
448 deletions.
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.