Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Delete distributorId before sending error (#87)
Browse files Browse the repository at this point in the history
* Delete distributorId before sending error

* Move config check to the initialization

* Use valid config in minimal live connect

* Expose event bus in minimal LC

* Add LC instance to liQ_instances in mininal LC

* include event-bus to esm and cjs

* adjust the initializer to use the global bus and add mode param

* small refactoring

Co-authored-by: wiem <welabidine@liveintent.com>
  • Loading branch information
leonelcuevas and wi101 authored Nov 16, 2022
1 parent 175c1e4 commit 318aa85
Show file tree
Hide file tree
Showing 10 changed files with 18,333 additions and 51 deletions.
18,205 changes: 18,187 additions & 18 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/config-validators/remove-invalid-pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {LiveConnectConfiguration} config
* @return {LiveConnectConfiguration}
*/
export function removeInvalidPairs (config, eventBus) {
if (config && config.appId && config.distributorId) {
const distributorId = config.distributorId
delete config.distributorId
eventBus.emitError('AppIdAndDistributorIdPresent', new Error(`Event contains both appId: ${config.appId} and distributorId: ${distributorId}. Ignoring distributorId`))
}
return config
}
13 changes: 9 additions & 4 deletions src/initializer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { StandardLiveConnect } from './standard-live-connect'
import { MinimalLiveConnect } from './minimal-live-connect'
import { isObject } from './utils/types'
const _minimalMode = process.env.LiveConnectMode === 'minimal'
const _initializationFunction = _minimalMode ? MinimalLiveConnect : StandardLiveConnect
import { EVENT_BUS_NAMESPACE } from './utils/consts'
import { GlobalEventBus } from './events/event-bus'

/**
* @param {LiveConnectConfiguration} liveConnectConfig
* @param {StorageHandler} externalStorageHandler
* @param {CallHandler} externalCallHandler
* @param {string} mode
* @param {EventBus} externalEventBus
* @returns {LiveConnect}
* @constructor
*/
export function LiveConnect (liveConnectConfig, externalStorageHandler, externalCallHandler, externalEventBus) {
export function LiveConnect (liveConnectConfig, externalStorageHandler, externalCallHandler, mode, externalEventBus) {
console.log('Initializing LiveConnect', liveConnectConfig)
const minimalMode = mode === 'minimal' || process.env.LiveConnectMode === 'minimal'
const bus = externalEventBus || GlobalEventBus(EVENT_BUS_NAMESPACE, 5, err => console.error(err))
const configuration = (isObject(liveConnectConfig) && liveConnectConfig) || {}
return _initializationFunction(configuration, externalStorageHandler, externalCallHandler, externalEventBus)
const initializationFunction = minimalMode ? MinimalLiveConnect : StandardLiveConnect
return initializationFunction(configuration, externalStorageHandler, externalCallHandler, bus)
}
13 changes: 8 additions & 5 deletions src/minimal-live-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IdentityResolver } from './idex/identity-resolver-nocache'
import { enrich as peopleVerified } from './enrichers/people-verified'
import { enrich as additionalIdentifiers } from './enrichers/identifiers-nohash'
import { enrich as privacyConfig } from './enrichers/privacy-config'
import { removeInvalidPairs } from './config-validators/remove-invalid-pairs'
import { StorageHandler } from './handlers/read-storage-handler'
import { CallHandler } from './handlers/call-handler'
import { StorageStrategy } from './model/storage-strategy'
Expand All @@ -30,20 +31,22 @@ import { LocalEventBus } from './events/event-bus'
function _minimalInitialization (liveConnectConfig, externalStorageHandler, externalCallHandler, eventBus) {
try {
const callHandler = CallHandler(externalCallHandler, eventBus)
const configWithPrivacy = merge(liveConnectConfig, privacyConfig(liveConnectConfig))
const validLiveConnectConfig = removeInvalidPairs(liveConnectConfig, eventBus)
const configWithPrivacy = merge(validLiveConnectConfig, privacyConfig(validLiveConnectConfig))
const storageStrategy = configWithPrivacy.privacyMode ? StorageStrategy.disabled : configWithPrivacy.storageStrategy
const storageHandler = StorageHandler(storageStrategy, externalStorageHandler, eventBus)
const peopleVerifiedData = merge(configWithPrivacy, peopleVerified(configWithPrivacy, storageHandler))
const peopleVerifiedDataWithAdditionalIds = merge(peopleVerifiedData, additionalIdentifiers(peopleVerifiedData, storageHandler))
const resolver = IdentityResolver(peopleVerifiedDataWithAdditionalIds, callHandler, eventBus)
return {
push: (arg) => window[liveConnectConfig.globalVarName].push(arg),
fire: () => window[liveConnectConfig.globalVarName].push({}),
push: (arg) => window[validLiveConnectConfig.globalVarName].push(arg),
fire: () => window[validLiveConnectConfig.globalVarName].push({}),
peopleVerifiedId: peopleVerifiedDataWithAdditionalIds.peopleVerifiedId,
ready: true,
resolve: resolver.resolve,
resolutionCallUrl: resolver.getUrl,
config: liveConnectConfig
config: validLiveConnectConfig,
eventBus: eventBus
}
} catch (x) {
console.error(x)
Expand All @@ -70,7 +73,7 @@ export function MinimalLiveConnect (liveConnectConfig, externalStorageHandler, e
const lc = _minimalInitialization(configuration, externalStorageHandler, externalCallHandler, eventBus)
window.liQ_instances = window.liQ_instances || []
if (window.liQ_instances.filter(i => i.config.globalVarName === configuration.globalVarName).length === 0) {
window.liQ_instances.push(window[configuration.globalVarName])
window.liQ_instances.push(lc)
}
return lc
} catch (x) {
Expand Down
8 changes: 0 additions & 8 deletions src/pixel/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,7 @@ export function StateWrapper (state, eventBus) {
return array
}

function _removeInvalidPair () {
if (_state.appId && _state.distributorId) {
eventBus.emitError('AppIdAndDistributorIdPresent', new Error(`Event contains both appId: ${_state.appId} and distributorId ${_state.distributorId}. Ignoring distributorId`))
delete _state.distributorId
}
}

function _asQuery () {
_removeInvalidPair()
return new Query(_asTuples())
}

Expand Down
6 changes: 4 additions & 2 deletions src/standard-live-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { resolve as decisionsResolve } from './manager/decisions'
import { enrich as pageEnrich } from './enrichers/page'
import { enrich as identifiersEnrich } from './enrichers/identifiers'
import { enrich as privacyConfig } from './enrichers/privacy-config'
import { removeInvalidPairs } from './config-validators/remove-invalid-pairs'
import { isArray, isObject, merge } from './utils/types'
import { IdentityResolver } from './idex/identity-resolver'
import { StorageHandler } from './handlers/storage-handler'
Expand Down Expand Up @@ -132,7 +133,8 @@ function _getInitializedLiveConnect (liveConnectConfig) {
function _standardInitialization (liveConnectConfig, externalStorageHandler, externalCallHandler, eventBus) {
try {
const callHandler = CallHandler(externalCallHandler, eventBus)
const configWithPrivacy = merge(liveConnectConfig, privacyConfig(liveConnectConfig))
const validLiveConnectConfig = removeInvalidPairs(liveConnectConfig, eventBus)
const configWithPrivacy = merge(validLiveConnectConfig, privacyConfig(validLiveConnectConfig))
errorHandler.register(configWithPrivacy, callHandler, eventBus)
const storageStrategy = configWithPrivacy.privacyMode ? StorageStrategy.disabled : configWithPrivacy.storageStrategy
const storageHandler = StorageHandler(storageStrategy, externalStorageHandler, eventBus)
Expand All @@ -159,7 +161,7 @@ function _standardInitialization (liveConnectConfig, externalStorageHandler, ext
ready: true,
resolve: resolver.resolve,
resolutionCallUrl: resolver.getUrl,
config: liveConnectConfig,
config: validLiveConnectConfig,
eventBus: eventBus
}
} catch (x) {
Expand Down
49 changes: 49 additions & 0 deletions test/unit/config-validators/remove-invalid-pairs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, use } from 'chai'
import dirtyChai from 'dirty-chai'
import { removeInvalidPairs } from '../../../src/config-validators/remove-invalid-pairs'
import { LocalEventBus } from '../../../src/events/event-bus'

use(dirtyChai)

describe('RemoveInvalidPairsTransformer', () => {
const eventBus = LocalEventBus()
let errors = []

beforeEach(() => {
eventBus.on('li_errors', (error) => { errors.push(error) })
errors = []
})

it('should send event and remove distributorId when both appId and distributorId are present', function () {
const config = {
appId: 'a-0100',
distributorId: 'did-9898',
liveConnectId: '213245'
}
const expectedResult = {
appId: 'a-0100',
liveConnectId: '213245'
}
const result = removeInvalidPairs(config, eventBus)

expect(result).to.eql(expectedResult)
expect(errors).to.not.be.empty()
})

it('should not modify config if appId and distributorId are not present', function () {
const config = {
appId: 'a-0100',
liveConnectId: '213245',
globalVarName: 'liQTest'
}
const expectedResult = {
appId: 'a-0100',
liveConnectId: '213245',
globalVarName: 'liQTest'
}
const result = removeInvalidPairs(config, eventBus)

expect(result).to.eql(expectedResult)
expect(errors).to.be.empty()
})
})
42 changes: 41 additions & 1 deletion test/unit/minimal-live-connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import jsdom from 'mocha-jsdom'
import sinon from 'sinon'
import { expect, use } from 'chai'
import { MinimalLiveConnect } from '../../src/minimal-live-connect'
import { EVENT_BUS_NAMESPACE } from '../../src/utils/consts'
import { LiveConnect } from '../../src/initializer'
import * as storage from '../shared/utils/storage'
import * as calls from '../shared/utils/calls'
import dirtyChai from 'dirty-chai'
Expand Down Expand Up @@ -46,20 +48,43 @@ describe('MinimalLiveConnect', () => {
expect(window.liQ).to.not.be.undefined()
})

it('should expose liQ via the initializer', function () {
expect(window.liQ).to.be.undefined()
LiveConnect({}, storage, calls, 'minimal')
expect(window.liQ).to.not.be.undefined()
expect(window[EVENT_BUS_NAMESPACE]).to.not.be.undefined()
})

it('should accept a single event and put it in the queue', function () {
const lc = MinimalLiveConnect({}, storage, calls)
lc.push({ event: 'some' })
console.log(window.liQ)
expect(window.liQ.length).to.eql(1)
})

it('should accept a single event and put it in the queue via the initializer', function () {
const lc = LiveConnect({}, storage, calls, 'minimal')
lc.push({ event: 'some' })
console.log(window.liQ)
expect(window.liQ.length).to.eql(1)
expect(window[EVENT_BUS_NAMESPACE]).to.not.be.undefined()
})

it('should accept firing an event and put it in the queue', function () {
const lc = MinimalLiveConnect({}, storage, calls)
lc.fire()
console.log(window.liQ)
expect(window.liQ.length).to.eql(1)
})

it('should accept firing an event and put it in the queue via the initializer', function () {
const lc = LiveConnect({}, storage, calls, 'minimal')
lc.fire()
console.log(window.liQ)
expect(window.liQ.length).to.eql(1)
expect(window[EVENT_BUS_NAMESPACE]).to.not.be.undefined()
})

it('should return the resolution Url', function () {
const lc = MinimalLiveConnect({}, storage, calls)
expect(lc.resolutionCallUrl()).to.eql('https://idx.liadm.com/idex/unknown/any')
Expand All @@ -74,8 +99,23 @@ describe('MinimalLiveConnect', () => {
it('should expose the LC instance as globalVarName instead of liQ when provided', function () {
expect(window.liQTest).to.be.undefined()
expect(window.liQ).to.be.undefined()
MinimalLiveConnect({ globalVarName: 'liQTest' }, storage, calls)
const lc = MinimalLiveConnect({ globalVarName: 'liQTest' }, storage, calls)
expect(window.liQ).to.be.undefined()
expect(window.liQTest).to.not.be.undefined()
expect(window.liQTest).to.be.an('array')
expect(lc.ready).to.be.true()
expect(window.liQ_instances).to.have.members([lc])
})

it('should remove distributorId when both appId and distributorId are present', function () {
const config = { appId: 'a-00xx', distributorId: 'did-00xx', globalVarName: 'liQTest' }
const lc = MinimalLiveConnect(config, storage, calls)
const expectedConfig = { appId: 'a-00xx', globalVarName: 'liQTest' }
expect(lc.config).to.eql(expectedConfig)
})

it('should expose the eventBus through the LC instance', function () {
const lc = MinimalLiveConnect({}, storage, calls)
expect(lc.eventBus).to.not.be.undefined()
})
})
13 changes: 0 additions & 13 deletions test/unit/pixel/state.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,4 @@ describe('EventComposition', () => {
expect(event.asQuery().toQueryString()).to.eql('?did=did-9898&duid=213245')
assert.includeDeepMembers(event.asTuples(), [['did', 'did-9898'], ['duid', '213245']])
})

it('should ignore distributorId when both appId and distributorId are present', function () {
const eventBus = LocalEventBus()
const pixelData = {
appId: 'a-0100',
distributorId: 'did-9898'
}
const event = new StateWrapper(pixelData, eventBus)

expect(event.data).to.eql(pixelData)
expect(event.asQuery().toQueryString()).to.eql('?aid=a-0100')
assert.includeDeepMembers(event.asTuples(), [['aid', 'a-0100']])
})
})
23 changes: 23 additions & 0 deletions test/unit/standard-live-connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { hashEmail } from '../../src/utils/hash'
import dirtyChai from 'dirty-chai'
import { LocalEventBus } from '../../src/events/event-bus'
import { StorageHandler } from '../../src/handlers/storage-handler'
import { EVENT_BUS_NAMESPACE } from '../../src/utils/consts'
import { LiveConnect } from '../../src/initializer'

use(dirtyChai)
const eventBus = LocalEventBus()
Expand Down Expand Up @@ -57,6 +59,15 @@ describe('StandardLiveConnect', () => {
expect(window.liQ_instances).to.have.members([window.liQ])
})

it('should initialise the event bus, and hook the error handler via the initializer', function () {
LiveConnect({})
const eventBus = window.liQ.eventBus
const errorHandler = eventBus.h
expect(errorHandler).to.have.key(C.ERRORS_PREFIX)
expect(errorHandler[C.ERRORS_PREFIX].length).to.be.eql(1)
expect(window.liQ_instances).to.have.members([window.liQ])
expect(window[EVENT_BUS_NAMESPACE]).to.be.eq(eventBus)
})
it('should expose liQ', function () {
expect(window.liQ).to.be.undefined()
StandardLiveConnect({}, storage, calls)
Expand Down Expand Up @@ -238,4 +249,16 @@ describe('StandardLiveConnect', () => {
expect(window.liQTest.ready).to.be.true()
expect(window.liQ_instances).to.have.members([window.liQTest])
})

it('should remove distributorId when both appId and distributorId are present', function () {
const config = { appId: 'a-00xx', distributorId: 'did-00xx', globalVarName: 'liQTest' }
const lc = StandardLiveConnect(config, storage, calls)
const expectedConfig = { appId: 'a-00xx', globalVarName: 'liQTest' }
expect(lc.config).to.eql(expectedConfig)
})

it('should expose the eventBus through the LC instance', function () {
const lc = StandardLiveConnect({}, storage, calls)
expect(lc.eventBus).to.not.be.undefined()
})
})

0 comments on commit 318aa85

Please sign in to comment.