Skip to content

Commit

Permalink
various module type import functions for loading external scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Komorski committed Sep 10, 2024
1 parent 59a9fae commit 5a613d9
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion modules/adlooxAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import adapterManager from '../src/adapterManager.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import {loadExternalScript} from '../src/adloader.js';
import {loadExternalScriptByAnalyticsModule as loadExternalScript} from '../src/adloader.js';
import {auctionManager} from '../src/auctionManager.js';
import {AUCTION_COMPLETED} from '../src/auction.js';
import {EVENTS} from '../src/constants.js';
Expand Down
4 changes: 2 additions & 2 deletions modules/ftrackIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as utils from '../src/utils.js';
import {submodule} from '../src/hook.js';
import {getStorageManager} from '../src/storageManager.js';
import {uspDataHandler} from '../src/adapterManager.js';
import {loadExternalScript} from '../src/adloader.js';
import {loadExternalScriptByUserIdModule} from '../src/adloader.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';

/**
Expand Down Expand Up @@ -142,7 +142,7 @@ export const ftrackIdSubmodule = {
}

// Creates an async script element and appends it to the document
loadExternalScript(config.params.url, MODULE_NAME);
loadExternalScriptByUserIdModule(config.params.url, MODULE_NAME);
}
};
},
Expand Down
2 changes: 1 addition & 1 deletion modules/id5IdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {getStorageManager} from '../src/storageManager.js';
import {gppDataHandler, uspDataHandler} from '../src/adapterManager.js';
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
import {GreedyPromise} from '../src/utils/promise.js';
import {loadExternalScript} from '../src/adloader.js';
import {loadExternalScriptByUserIdModule as loadExternalScript} from '../src/adloader.js';

/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
Expand Down
2 changes: 1 addition & 1 deletion modules/improvedigitalBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {ortbConverter} from '../libraries/ortbConverter/converter.js';
* Also see https://github.com/prebid/Prebid.js/issues/11656
*/
// eslint-disable-next-line no-restricted-imports
import {loadExternalScript} from '../src/adloader.js';
import {loadExternalScriptByBidderModule as loadExternalScript} from '../src/adloader.js';

/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
Expand Down
2 changes: 1 addition & 1 deletion modules/justIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import * as utils from '../src/utils.js'
import { submodule } from '../src/hook.js'
import { loadExternalScript } from '../src/adloader.js'
import { loadExternalScriptByUserIdModule as loadExternalScript } from '../src/adloader.js'
import {includes} from '../src/polyfill.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/showheroes-bsBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { VIDEO, BANNER } from '../src/mediaTypes.js';
* Also see https://github.com/prebid/Prebid.js/issues/11656
*/
// eslint-disable-next-line no-restricted-imports
import { loadExternalScript } from '../src/adloader.js';
import { loadExternalScriptByBidderModule as loadExternalScript } from '../src/adloader.js';

const PROD_ENDPOINT = 'https://bs.showheroes.com/api/v1/bid';
const STAGE_ENDPOINT = 'https://bid-service.stage.showheroes.com/api/v1/bid';
Expand Down
2 changes: 1 addition & 1 deletion modules/tncIdSystem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { submodule } from '../src/hook.js';
import { logInfo } from '../src/utils.js';
import { loadExternalScript } from '../src/adloader.js';
import { loadExternalScriptByUserIdModule as loadExternalScript } from '../src/adloader.js';

const MODULE_NAME = 'tncId';
let url = null;
Expand Down
36 changes: 28 additions & 8 deletions src/adloader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { LOAD_EXTERNAL_SCRIPT } from './activities/activities.js';
import { activityParams } from './activities/activityParams.js';
import { MODULE_TYPE_PREBID } from './activities/modules.js';
import { MODULE_TYPE_ANALYTICS, MODULE_TYPE_BIDDER, MODULE_TYPE_RTD, MODULE_TYPE_UID } from './activities/modules.js';
import { isActivityAllowed } from './activities/rules.js';
import {includes} from './polyfill.js';
import { logError, logWarn, insertElement, setScriptAttributes } from './utils.js';
import { includes } from './polyfill.js';
import { insertElement, logError, logWarn, setScriptAttributes } from './utils.js';

const _requestCache = new WeakMap();
// The below list contains modules or vendors whom Prebid allows to load external JS.
Expand Down Expand Up @@ -44,6 +44,30 @@ const _approvedLoadExternalJSList = [
'id5',
];

function isLoadingExternalScriptIsAllowed(moduleType, moduleCode) {
return isActivityAllowed(LOAD_EXTERNAL_SCRIPT, activityParams(moduleType, moduleCode));
}

export function loadExternalScriptByBidderModule(url, moduleCode, callback, doc, attributes) {
if (isLoadingExternalScriptIsAllowed(MODULE_TYPE_BIDDER, moduleCode)) { return loadScript(url, moduleCode, callback, doc, attributes); }
}

export function loadExternalScriptByRtdModule(url, moduleCode, callback, doc, attributes) {
if (isLoadingExternalScriptIsAllowed(MODULE_TYPE_RTD, moduleCode)) { return loadScript(url, moduleCode, callback, doc, attributes); }
}

export function loadExternalScriptByAnalyticsModule(url, moduleCode, callback, doc, attributes) {
if (isLoadingExternalScriptIsAllowed(MODULE_TYPE_ANALYTICS, moduleCode)) { return loadScript(url, moduleCode, callback, doc, attributes); }
}

export function loadExternalScriptByUserIdModule(url, moduleCode, callback, doc, attributes) {
if (isLoadingExternalScriptIsAllowed(MODULE_TYPE_UID, moduleCode)) { return loadScript(url, moduleCode, callback, doc, attributes); }
}

// defaults to loadExternalScriptByRtdModule as its the most common use
export function loadExternalScript(url, moduleCode, callback, doc, attributes) {
return loadExternalScriptByRtdModule(url, moduleCode, callback, doc, attributes);
}
/**
* Loads external javascript. Can only be used if external JS is approved by Prebid. See https://github.com/prebid/prebid-js-external-js-template#policy
* Each unique URL will be loaded at most 1 time.
Expand All @@ -53,11 +77,7 @@ const _approvedLoadExternalJSList = [
* @param {Document} [doc] the context document, in which the script will be loaded, defaults to loaded document
* @param {object} attributes an object of attributes to be added to the script with setAttribute by [key] and [value]; Only the attributes passed in the first request of a url will be added.
*/
export function loadExternalScript(url, moduleCode, callback, doc, attributes) {
if (!isActivityAllowed(LOAD_EXTERNAL_SCRIPT, activityParams(MODULE_TYPE_PREBID, moduleCode))) {
return;
}

function loadScript(url, moduleCode, callback, doc, attributes) {
if (!moduleCode || !url) {
logError('cannot load external script without url and moduleCode');
return;
Expand Down
12 changes: 10 additions & 2 deletions test/mocks/adloaderStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import * as adloader from 'src/adloader.js';

// this export is for adloader's tests against actual implementation
export let loadExternalScript = adloader.loadExternalScript;
export let loadExternalScriptByAnalyticsModule = adloader.loadExternalScriptByAnalyticsModule;
export let loadExternalScriptByUserIdModule = adloader.loadExternalScriptByUserIdModule;

export let loadExternalScriptStub = createStub();
export let loadExternalScriptByAnalyticsModuleStub = createStub('loadExternalScriptByAnalyticsModule');
export let loadExternalScriptByUserIdModuleStub = createStub('loadExternalScriptByUserIdModule');

function createStub() {
return sinon.stub(adloader, 'loadExternalScript').callsFake((...args) => {
function createStub(fnName = 'loadExternalScript') {
return sinon.stub(adloader, fnName).callsFake((...args) => {
if (typeof args[2] === 'function') {
args[2]();
} else if (typeof args[3] === 'function') {
Expand All @@ -19,5 +23,9 @@ function createStub() {

beforeEach(function() {
loadExternalScriptStub.restore();
loadExternalScriptByAnalyticsModuleStub.restore();
loadExternalScriptByUserIdModuleStub.restore();
loadExternalScriptStub = createStub();
loadExternalScriptByAnalyticsModuleStub = createStub('loadExternalScriptByAnalyticsModule');
loadExternalScriptByUserIdModuleStub = createStub('loadExternalScriptByUserIdModule');
});
1 change: 0 additions & 1 deletion test/spec/adloader_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,5 @@ describe('adLoader', function () {
} finally {
unregisterRule?.();
}

})
});
4 changes: 2 additions & 2 deletions test/spec/modules/adlooxAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from 'chai';
import * as events from 'src/events.js';
import { EVENTS } from 'src/constants.js';
import * as utils from 'src/utils.js';
import { loadExternalScriptStub } from 'test/mocks/adloaderStub.js';
import { loadExternalScriptByAnalyticsModuleStub } from 'test/mocks/adloaderStub.js';

const analyticsAdapterName = 'adloox';

Expand Down Expand Up @@ -169,7 +169,7 @@ describe('Adloox Analytics Adapter', function () {

events.emit(EVENTS.BID_WON, bid);

const [urlInserted, moduleCode] = loadExternalScriptStub.getCall(0).args;
const [urlInserted, moduleCode] = loadExternalScriptByAnalyticsModuleStub.getCall(0).args;

expect(urlInserted.substr(0, url.length)).to.equal(url);
expect(moduleCode).to.equal(analyticsAdapterName);
Expand Down
2 changes: 1 addition & 1 deletion test/spec/modules/ftrackIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ftrackIdSubmodule } from 'modules/ftrackIdSystem.js';
import * as utils from 'src/utils.js';
import { uspDataHandler } from 'src/adapterManager.js';
import { loadExternalScript } from 'src/adloader.js';
import { loadExternalScriptByUserIdModule as loadExternalScript } from 'src/adloader.js';
import { getGlobal } from 'src/prebidGlobal.js';
import {attachIdSystem, init, setSubmoduleRegistry} from 'modules/userId/index.js';
import {createEidsArray} from 'modules/userId/eids.js';
Expand Down
2 changes: 1 addition & 1 deletion test/spec/modules/justIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { justIdSubmodule, ConfigWrapper, jtUtils, EX_URL_REQUIRED, EX_INVALID_MODE } from 'modules/justIdSystem.js';
import { loadExternalScriptStub } from 'test/mocks/adloaderStub.js';
import { loadExternalScriptByUserIdModuleStub as loadExternalScriptStub } from 'test/mocks/adloaderStub.js';
import * as utils from 'src/utils.js';

const DEFAULT_PARTNER = 'pbjs-just-id-module';
Expand Down

0 comments on commit 5a613d9

Please sign in to comment.