-
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.
* add stub registration * update docs
- Loading branch information
Showing
17 changed files
with
255 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
console.log('nothing here yet') | ||
import {registerStub} from './service/registerStub' | ||
|
||
export default registerStub |
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,34 @@ | ||
export class PostMessageHandler { | ||
handle(event = {}) { | ||
const payload = this._toPayload(event.data) | ||
payload && | ||
window.__tcfapi( | ||
payload.command, | ||
payload.version, | ||
(retValue, success) => { | ||
const returnMsg = { | ||
__tcfapiReturn: { | ||
returnValue: retValue, | ||
success: success, | ||
callId: payload.callId | ||
} | ||
} | ||
if (typeof event.data === 'string') { | ||
event.source.postMessage(JSON.stringify(returnMsg), '*') | ||
} else { | ||
event.source.postMessage(returnMsg, '*') | ||
} | ||
}, | ||
payload.parameter | ||
) | ||
} | ||
|
||
_toPayload(message = {}) { | ||
try { | ||
const json = typeof message === 'string' ? JSON.parse(message) : message | ||
return json?.__tcfapiCall | ||
} catch (error) { | ||
return undefined | ||
} | ||
} | ||
} |
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,33 @@ | ||
/* eslint-disable standard/no-callback-literal */ | ||
export class TcfApiHandler { | ||
constructor() { | ||
this._queue = [] | ||
} | ||
|
||
handle({command, version, callback, parameter}) { | ||
switch (command) { | ||
case PING_COMMAND: { | ||
if (typeof callback === 'function') { | ||
callback({ | ||
gdprApplies: true, | ||
cmpLoaded: false, | ||
cmpStatus: 'stub' | ||
}) | ||
} | ||
break | ||
} | ||
case PENDING_COMMAND: { | ||
return this._queue | ||
} | ||
default: { | ||
this._queue.push(() => | ||
window.__tcfapi(command, version, callback, parameter) | ||
) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
const PING_COMMAND = 'ping' | ||
const PENDING_COMMAND = 'pending' |
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,10 @@ | ||
import {PostMessageHandler} from './handler/PostMessageHandler' | ||
|
||
const postMessageHandler = new PostMessageHandler() | ||
export const registerIframeMessageHandler = () => { | ||
window.addEventListener( | ||
'message', | ||
event => postMessageHandler.handle(event), | ||
false | ||
) | ||
} |
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,14 @@ | ||
import {registerTcfApiLocator} from './registerTcfApiLocator' | ||
import {registerIframeMessageHandler} from './registerIframeMessageHandler' | ||
import {registerTcfApiHandler} from './registerTcfApiHandler' | ||
|
||
export const registerStub = () => { | ||
if (typeof window === 'undefined') { | ||
return | ||
} | ||
if (!registerTcfApiLocator()) { | ||
return | ||
} | ||
registerIframeMessageHandler() | ||
registerTcfApiHandler() | ||
} |
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,7 @@ | ||
import {TcfApiHandler} from './handler/TcfApiHandler' | ||
|
||
const tcfApiHandler = new TcfApiHandler() | ||
export const registerTcfApiHandler = () => { | ||
window.__tcfapi = (command, version, callback, parameter) => | ||
tcfApiHandler.handle({command, version, callback, parameter}) | ||
} |
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,16 @@ | ||
import {waitUntil} from './waitUntil' | ||
|
||
export const registerTcfApiLocator = () => { | ||
if (window.frames[TCF_API_LOCATOR]) { | ||
return false | ||
} | ||
waitUntil({condition: () => !!window.document.body}).then(() => { | ||
const iframe = window.document.createElement('iframe') | ||
iframe.style.cssText = 'display:none' | ||
iframe.name = TCF_API_LOCATOR | ||
window.document.body.appendChild(iframe) | ||
}) | ||
return true | ||
} | ||
|
||
const TCF_API_LOCATOR = '__tcfapiLocator' |
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,20 @@ | ||
export const waitUntil = ({ | ||
condition, | ||
timeout = 20000, | ||
interval = 5, | ||
timeoutMessage | ||
}) => | ||
new Promise((resolve, reject) => { | ||
const iid = setInterval(() => { | ||
if (condition()) { | ||
clearTimeout(tid) | ||
clearInterval(iid) | ||
resolve() | ||
} | ||
}, interval) | ||
const tid = setTimeout(() => { | ||
clearTimeout(tid) | ||
clearInterval(iid) | ||
reject(new Error(timeoutMessage)) | ||
}, timeout) | ||
}) |
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,6 +1,69 @@ | ||
import jsdom from 'jsdom-global' | ||
import {expect} from 'chai' | ||
import registerStub from '../main' | ||
import {waitUntil} from '../main/service/waitUntil' | ||
|
||
describe('boros tcf stub', () => { | ||
it('should be tested', () => { | ||
expect(true).to.be.true | ||
beforeEach(() => { | ||
jsdom(null, {runScripts: 'dangerously'}) | ||
window.postMessage = message => { | ||
const event = new window.MessageEvent('message', { | ||
data: message, | ||
source: window | ||
}) | ||
window.dispatchEvent(event) | ||
} | ||
}) | ||
|
||
it('should register the __tcfapiLocator iframe', () => { | ||
registerStub() | ||
return waitUntil({condition: () => !!window.frames.__tcfapiLocator}) | ||
}) | ||
|
||
it('should register the __tcfapi facade with pending command', () => { | ||
registerStub() | ||
let pending = window.__tcfapi('pending') | ||
expect(pending.length).to.equal(0) | ||
|
||
let pinged = false | ||
window.__tcfapi('ping', 2, () => (pinged = true)) | ||
expect(pinged).to.be.true | ||
pending = window.__tcfapi('pending') | ||
expect(pending.length).to.equal(0) | ||
|
||
window.__tcfapi('anyOtherCommand', 2, () => null) | ||
pending = window.__tcfapi('pending') | ||
expect(pending.length).to.equal(1) | ||
}) | ||
|
||
it('should accept iframe communications', () => { | ||
registerStub() | ||
|
||
const callId = Math.random() + '' | ||
const msg = { | ||
__tcfapiCall: { | ||
command: 'ping', | ||
version: '2', | ||
callId: callId | ||
} | ||
} | ||
|
||
const responsePromise = new Promise((resolve, reject) => | ||
window.addEventListener('message', event => { | ||
try { | ||
const response = event.data.__tcfapiReturn | ||
if (response) { | ||
expect(response.callId).to.equal(callId) | ||
expect(response.returnValue.cmpStatus).to.equal('stub') | ||
resolve() | ||
} | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
) | ||
|
||
window.postMessage(msg, '*') | ||
return responsePromise | ||
}) | ||
}) |
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 was deleted.
Oops, something went wrong.
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,3 @@ | ||
import registerStub from '../main' | ||
|
||
registerStub() |
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