-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiovation.js
More file actions
77 lines (67 loc) · 2.03 KB
/
iovation.js
File metadata and controls
77 lines (67 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const loadDynamicScript = (scriptSrc, scriptId, callback, errorCallback) => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = scriptSrc;
script.id = scriptId;
document.body.appendChild(script);
script.onload = () => {
if (callback) callback();
};
script.onerror = () => {
if (errorCallback) errorCallback();
};
};
const statuses = {
EXECUTED: 'executed',
NOTEXECUTED: 'not_executed',
FAILED: 'failed',
};
const iovationName = 'iovation';
class Alloy {
constructor(options) {
this.serviceStatus = {};
this.data = {};
this.readyCallback = options.readyCallback;
if (options.services[iovationName]) {
this.loadIovation();
}
}
loadIovation() {
this.serviceStatus[iovationName] = statuses.NOTEXECUTED;
// iovation looks for this fn when blackbox is complete
window.io_bb_callback = (bb, complete) => {
if (complete) {
this.data.iovation_blackbox = bb;
this.markServiceExecuted(iovationName);
}
};
const iovationProdUrl = 'https://mpsnare.iesnare.com/snare.js';
loadDynamicScript(
iovationProdUrl,
iovationName,
null,
() => { this.markServiceFailure(iovationName); },
);
}
markServiceExecuted(serviceName) {
this.serviceStatus[serviceName] = statuses.EXECUTED;
if (this.areAllServicesDone()) {
if (this.readyCallback) {
this.readyCallback(this.data, this.serviceStatus);
}
}
}
markServiceFailure(serviceName) {
this.serviceStatus[serviceName] = statuses.FAILED;
if (this.areAllServicesDone()) {
if (this.readyCallback) {
this.readyCallback(this.data, this.serviceStatus);
}
}
}
areAllServicesDone() {
return Object.keys(this.serviceStatus).every(serviceKey =>
this.serviceStatus[serviceKey] !== statuses.NOTEXECUTED);
}
}
export default Alloy