fiftyone.pipeline.core 4.4.90 → 4.4.91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/constants.js +26 -0
- package/index.js +1 -0
- package/javascript-templates/JavaScriptResource.mustache +57 -41
- package/javascriptbuilder.js +16 -1
- package/package.json +1 -1
- package/tests/enableCookies.test.js +77 -0
- package/JavaScriptResource.mustache +0 -442
package/constants.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* *********************************************************************
|
|
2
|
+
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
|
|
3
|
+
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
|
|
4
|
+
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
|
|
5
|
+
*
|
|
6
|
+
* This Original Work is licensed under the European Union Public Licence
|
|
7
|
+
* (EUPL) v.1.2 and is subject to its terms as set out below.
|
|
8
|
+
*
|
|
9
|
+
* If a copy of the EUPL was not distributed with this file, You can obtain
|
|
10
|
+
* one at https://opensource.org/licenses/EUPL-1.2.
|
|
11
|
+
*
|
|
12
|
+
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
|
|
13
|
+
* amended by the European Commission) shall be deemed incompatible for
|
|
14
|
+
* the purposes of the Work and the provisions of the compatibility
|
|
15
|
+
* clause in Article 5 of the EUPL shall not apply.
|
|
16
|
+
*
|
|
17
|
+
* If using the Work as, or as part of, a network application, by
|
|
18
|
+
* including the attribution notice(s) required under Article 5 of the EUPL
|
|
19
|
+
* in the end user terms of the application under an appropriate heading,
|
|
20
|
+
* such notice(s) shall fulfill the requirements of that article.
|
|
21
|
+
* ********************************************************************* */
|
|
22
|
+
|
|
23
|
+
module.exports = {
|
|
24
|
+
evidenceEnableCookies: 'query.fod-js-enable-cookies',
|
|
25
|
+
evidenceObjectName: 'query.fod-js-object-name'
|
|
26
|
+
};
|
package/index.js
CHANGED
|
@@ -25,6 +25,7 @@ module.exports = {
|
|
|
25
25
|
ElementData: require('./elementData'),
|
|
26
26
|
ElementDataDictionary: require('./elementDataDictionary'),
|
|
27
27
|
ErrorMessages: require('./errorMessages'),
|
|
28
|
+
Constants: require('./constants.js'),
|
|
28
29
|
Evidence: require('./evidence'),
|
|
29
30
|
EvidenceKeyFilter: require('./evidenceKeyFilter'),
|
|
30
31
|
FlowData: require('./flowData'),
|
|
@@ -75,46 +75,59 @@ fiftyoneDegreesManager = function() {
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
// Get
|
|
78
|
+
// Get stored values with the '51D_' prefix that have been added to the request
|
|
79
79
|
// and return the data as key value pairs. This method is needed to extract
|
|
80
|
-
//
|
|
81
|
-
// where CORS will prevent
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
// stored values for inclusion in the GET or POST request for situations
|
|
81
|
+
// where CORS will prevent them from being sent to third parties.
|
|
82
|
+
var getFodSavedValues = function(){
|
|
83
|
+
let fodValues = {};
|
|
84
|
+
{{#_enableCookies}}
|
|
85
|
+
{
|
|
86
|
+
let keyValuePairs = document.cookie.split(/; */);
|
|
87
|
+
for(let nextPair of keyValuePairs) {
|
|
88
|
+
let firstEqualsLocation = nextPair.indexOf('=');
|
|
89
|
+
let name = nextPair.substring(0, firstEqualsLocation);
|
|
90
|
+
if(startsWith(name, "51D_")){
|
|
91
|
+
let value = nextPair.substring(firstEqualsLocation+1);
|
|
92
|
+
fodValues[name] = value;
|
|
93
|
+
}
|
|
90
94
|
}
|
|
91
|
-
}
|
|
92
|
-
|
|
95
|
+
};
|
|
96
|
+
{{/_enableCookies}}
|
|
97
|
+
{{^_enableCookies}}
|
|
98
|
+
{
|
|
99
|
+
// Collect values from session storage
|
|
100
|
+
let session51DataPrefix = sessionKey + "_data_";
|
|
101
|
+
for(let i = 0, n = window.sessionStorage.length; i < n; ++i) {
|
|
102
|
+
let nextKey = window.sessionStorage.key(i);
|
|
103
|
+
if(startsWith(nextKey, session51DataPrefix)){
|
|
104
|
+
let value = window.sessionStorage[nextKey];
|
|
105
|
+
fodValues[nextKey.substring(session51DataPrefix.length)] = value;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
{{/_enableCookies}}
|
|
110
|
+
return fodValues;
|
|
93
111
|
};
|
|
94
112
|
|
|
95
|
-
// Extract key value pairs from the '51D_' prefixed
|
|
113
|
+
// Extract key value pairs from the '51D_' prefixed values and concatenates
|
|
96
114
|
// them to form a query string for the subsequent json refresh.
|
|
97
|
-
var
|
|
98
|
-
var
|
|
115
|
+
var getParametersFromStorage = function(){
|
|
116
|
+
var fodValues = getFodSavedValues();
|
|
99
117
|
var keyValuePairs = [];
|
|
100
|
-
for (var key in
|
|
101
|
-
if (
|
|
102
|
-
// Url encode the
|
|
118
|
+
for (var key in fodValues) {
|
|
119
|
+
if (fodValues.hasOwnProperty(key)) {
|
|
120
|
+
// Url encode the value.
|
|
103
121
|
// This is done to ensure that invalid characters (e.g. = chars at the end of
|
|
104
122
|
// base 64 encoded strings) reach the server intact.
|
|
105
123
|
// The server will automatically decode the value before passing it into the
|
|
106
124
|
// Pipeline API.
|
|
107
|
-
keyValuePairs.push(key+"="+encodeURIComponent(
|
|
125
|
+
keyValuePairs.push(key+"="+encodeURIComponent(fodValues[key]));
|
|
108
126
|
}
|
|
109
127
|
}
|
|
110
128
|
return keyValuePairs;
|
|
111
129
|
};
|
|
112
130
|
|
|
113
|
-
// Delete a cookie.
|
|
114
|
-
function deleteCookie(name) {
|
|
115
|
-
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
116
|
-
}
|
|
117
|
-
|
|
118
131
|
// Fetch a value safely from the json object. If a key somewhere down the
|
|
119
132
|
// '.' separated hierarchy of keys is not present then 'undefined' is
|
|
120
133
|
// returned rather than letting an exception occur.
|
|
@@ -177,6 +190,12 @@ fiftyoneDegreesManager = function() {
|
|
|
177
190
|
// then process them and perform any call-backs required.
|
|
178
191
|
if (jsProperties !== undefined && jsProperties.length > 0) {
|
|
179
192
|
|
|
193
|
+
{{^_enableCookies}}
|
|
194
|
+
let valueSetPrefix = new RegExp('document\\.cookie\\s*=\\s*(("([A-Za-z0-9_"\\s\\+]+)\\s*=\\s*"\\s*\\+\\s*([^\\s};]+))|(`([A-Za-z0-9_]+)\\s*=\\s*\\$\\{([^}]+)\\}`))', 'g');
|
|
195
|
+
let session51DataPrefix = sessionKey + "_data_";
|
|
196
|
+
let sessionSetPatch = 'window.sessionStorage["' + session51DataPrefix + '$3$6"]=$4$7';
|
|
197
|
+
{{/_enableCookies}}
|
|
198
|
+
|
|
180
199
|
// Execute each of the JavaScript property code snippets using the
|
|
181
200
|
// index of the value to access the value to avoid problems with
|
|
182
201
|
// JavaScript returning erroneous values.
|
|
@@ -189,7 +208,7 @@ fiftyoneDegreesManager = function() {
|
|
|
189
208
|
if (body) {
|
|
190
209
|
toProcess++;
|
|
191
210
|
}
|
|
192
|
-
var isCached = sessionStorage && sessionStorage.getItem(sessionKey + "
|
|
211
|
+
var isCached = sessionStorage && sessionStorage.getItem(sessionKey + "_property_" + name);
|
|
193
212
|
|
|
194
213
|
if (!isCached) {
|
|
195
214
|
// Create new function bound to this instance and execute it.
|
|
@@ -207,6 +226,10 @@ fiftyoneDegreesManager = function() {
|
|
|
207
226
|
jsPropertiesStarted.push(name);
|
|
208
227
|
started++;
|
|
209
228
|
|
|
229
|
+
{{^_enableCookies}}
|
|
230
|
+
body = body.replaceAll(valueSetPrefix, sessionSetPatch);
|
|
231
|
+
{{/_enableCookies}}
|
|
232
|
+
|
|
210
233
|
if (body.indexOf(searchString) !== -1){
|
|
211
234
|
callbackCounter++;
|
|
212
235
|
body = body.replace(/\/\/ 51D replace this comment with callback function./g, 'callbackFunc(resolveFunc, rejectFunc);');
|
|
@@ -230,7 +253,7 @@ fiftyoneDegreesManager = function() {
|
|
|
230
253
|
func();
|
|
231
254
|
}
|
|
232
255
|
if (sessionStorage) {
|
|
233
|
-
sessionStorage.setItem(sessionKey + "
|
|
256
|
+
sessionStorage.setItem(sessionKey + "_property_" + name, true)
|
|
234
257
|
}
|
|
235
258
|
}
|
|
236
259
|
} else {
|
|
@@ -335,14 +358,6 @@ fiftyoneDegreesManager = function() {
|
|
|
335
358
|
// for 'complete' functions to fire.
|
|
336
359
|
fireChangeFuncs(json);
|
|
337
360
|
resolve(json);
|
|
338
|
-
{{^_enableCookies}}
|
|
339
|
-
var fodCookies = getFodCookies();
|
|
340
|
-
for (var key in fodCookies) {
|
|
341
|
-
if (fodCookies.hasOwnProperty(key)) {
|
|
342
|
-
deleteCookie(key);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
{{/_enableCookies}}
|
|
346
361
|
}
|
|
347
362
|
}
|
|
348
363
|
|
|
@@ -353,7 +368,7 @@ fiftyoneDegreesManager = function() {
|
|
|
353
368
|
// cached by browsers, the result of the POST request is stored in session
|
|
354
369
|
// storage on a successful response. This can then be checked before making
|
|
355
370
|
// repeat requests to the call-back URL.
|
|
356
|
-
// Any
|
|
371
|
+
// Any saved value parameters that have been set by the executed JavaScript
|
|
357
372
|
// properties are added to the list of parameters, this list is then
|
|
358
373
|
// serialized as Form Data and sent in the POST body to the call-back URL,
|
|
359
374
|
// refreshing the JSON data. The new JSON is then loaded if the request is
|
|
@@ -362,11 +377,11 @@ fiftyoneDegreesManager = function() {
|
|
|
362
377
|
var processRequest = function(resolve, reject){
|
|
363
378
|
loadParameters();
|
|
364
379
|
|
|
365
|
-
// Get additional parameters
|
|
380
|
+
// Get additional parameters in case they are not sent
|
|
366
381
|
// by the browser.
|
|
367
|
-
var
|
|
368
|
-
for(var
|
|
369
|
-
var parts =
|
|
382
|
+
var savedValueParams = getParametersFromStorage();
|
|
383
|
+
for(var savedValueIndex in savedValueParams) {
|
|
384
|
+
var parts = savedValueParams[savedValueIndex].split('=');
|
|
370
385
|
parameters[parts[0]] = parts[1];
|
|
371
386
|
}
|
|
372
387
|
|
|
@@ -419,6 +434,7 @@ fiftyoneDegreesManager = function() {
|
|
|
419
434
|
|
|
420
435
|
// Add the HTTP header for POST form data.
|
|
421
436
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
437
|
+
xhr.setRequestHeader('Accept', 'application/json');
|
|
422
438
|
|
|
423
439
|
xhr.onload = function () {
|
|
424
440
|
|
|
@@ -598,4 +614,4 @@ fiftyoneDegreesManager = function() {
|
|
|
598
614
|
{{/_supportsPromises}}
|
|
599
615
|
}
|
|
600
616
|
|
|
601
|
-
var {{_objName}} = new fiftyoneDegreesManager();
|
|
617
|
+
var {{_objName}} = new fiftyoneDegreesManager();
|
package/javascriptbuilder.js
CHANGED
|
@@ -33,6 +33,7 @@ const template = fs.readFileSync(
|
|
|
33
33
|
const FlowElement = require('./flowElement.js');
|
|
34
34
|
const EvidenceKeyFilter = require('./evidenceKeyFilter.js');
|
|
35
35
|
const ElementDataDictionary = require('./elementDataDictionary.js');
|
|
36
|
+
const Constants = require('./constants.js');
|
|
36
37
|
const uglifyJS = require('uglify-js');
|
|
37
38
|
|
|
38
39
|
/**
|
|
@@ -73,7 +74,10 @@ class JavaScriptBuilderElement extends FlowElement {
|
|
|
73
74
|
* callback url. This can be overriden with header.host evidence.
|
|
74
75
|
* @param {string} options.endPoint The endpoint of the client side
|
|
75
76
|
* callback url
|
|
76
|
-
* @param {boolean} options.enableCookies
|
|
77
|
+
* @param {boolean} options.enableCookies Whether the client JavaScript
|
|
78
|
+
* stored results of client side processing in cookies. This can also
|
|
79
|
+
* be set per request, using the "query.fod-js-enable-cookies" evidence key.
|
|
80
|
+
* For more details on personal data policy, see http://51degrees.com/terms/client-services-privacy-policy/
|
|
77
81
|
* @param {boolean} options.minify Whether to minify the JavaScript
|
|
78
82
|
*/
|
|
79
83
|
constructor ({
|
|
@@ -199,6 +203,17 @@ class JavaScriptBuilderElement extends FlowElement {
|
|
|
199
203
|
settings._sessionId = flowData.evidence.get('query.session-id');
|
|
200
204
|
settings._sequence = flowData.evidence.get('query.sequence');
|
|
201
205
|
|
|
206
|
+
// Try and get the requested enable cookies from evidence.
|
|
207
|
+
const enableCookies = flowData.evidence.get(Constants.evidenceEnableCookies);
|
|
208
|
+
if (enableCookies !== undefined) {
|
|
209
|
+
settings._enableCookies = (enableCookies?.toLowerCase?.() === 'true');
|
|
210
|
+
}
|
|
211
|
+
// Try and get the requested object name from evidence.
|
|
212
|
+
const objName = flowData.evidence.get(Constants.evidenceObjectName);
|
|
213
|
+
if (objName !== undefined) {
|
|
214
|
+
settings._objName = (objName);
|
|
215
|
+
}
|
|
216
|
+
|
|
202
217
|
let output = mustache.render(template, settings);
|
|
203
218
|
|
|
204
219
|
if (settings._minify) {
|
package/package.json
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/* *********************************************************************
|
|
2
|
+
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
|
|
3
|
+
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
|
|
4
|
+
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
|
|
5
|
+
*
|
|
6
|
+
* This Original Work is licensed under the European Union Public Licence
|
|
7
|
+
* (EUPL) v.1.2 and is subject to its terms as set out below.
|
|
8
|
+
*
|
|
9
|
+
* If a copy of the EUPL was not distributed with this file, You can obtain
|
|
10
|
+
* one at https://opensource.org/licenses/EUPL-1.2.
|
|
11
|
+
*
|
|
12
|
+
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
|
|
13
|
+
* amended by the European Commission) shall be deemed incompatible for
|
|
14
|
+
* the purposes of the Work and the provisions of the compatibility
|
|
15
|
+
* clause in Article 5 of the EUPL shall not apply.
|
|
16
|
+
*
|
|
17
|
+
* If using the Work as, or as part of, a network application, by
|
|
18
|
+
* including the attribution notice(s) required under Article 5 of the EUPL
|
|
19
|
+
* in the end user terms of the application under an appropriate heading,
|
|
20
|
+
* such notice(s) shall fulfill the requirements of that article.
|
|
21
|
+
* ********************************************************************* */
|
|
22
|
+
|
|
23
|
+
const core = require('fiftyone.pipeline.core');
|
|
24
|
+
const each = require('jest-each').default;
|
|
25
|
+
|
|
26
|
+
const cookieElement = new core.FlowElement({
|
|
27
|
+
dataKey: 'cookie',
|
|
28
|
+
properties: {
|
|
29
|
+
javascript: {
|
|
30
|
+
type: 'javascript'
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
processInternal: function (flowData) {
|
|
35
|
+
const contents = { javascript: 'document.cookie = "some cookie value"' };
|
|
36
|
+
|
|
37
|
+
const data = new core.ElementDataDictionary({
|
|
38
|
+
flowElement: this,
|
|
39
|
+
contents
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
flowData.setElementData(data);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const sequenceElement = new core.SequenceElement();
|
|
48
|
+
const jsonElement = new core.JsonBundler();
|
|
49
|
+
each([
|
|
50
|
+
[false, false, false],
|
|
51
|
+
[true, false, false],
|
|
52
|
+
[false, true, true],
|
|
53
|
+
[true, true, true]
|
|
54
|
+
])
|
|
55
|
+
.test('JavaScript cookies', (enableInConfig, enableInEvidence, expectCookie, done) => {
|
|
56
|
+
const jsElement = new core.JavascriptBuilder({ enableCookies: enableInConfig });
|
|
57
|
+
|
|
58
|
+
const pipeline = new core.PipelineBuilder()
|
|
59
|
+
.add(cookieElement)
|
|
60
|
+
.add(sequenceElement)
|
|
61
|
+
.add(jsonElement)
|
|
62
|
+
.add(jsElement)
|
|
63
|
+
.build();
|
|
64
|
+
|
|
65
|
+
const flowData = pipeline.createFlowData();
|
|
66
|
+
flowData.evidence.add(core.Constants.evidenceEnableCookies, enableInEvidence.toString());
|
|
67
|
+
flowData.process().then(function () {
|
|
68
|
+
const js = flowData.javascriptbuilder.javascript;
|
|
69
|
+
const matches = [...js.matchAll(/document\.cookie/g)];
|
|
70
|
+
if (expectCookie) {
|
|
71
|
+
expect(matches.length).toBe(2);
|
|
72
|
+
} else {
|
|
73
|
+
expect(matches.length).toBe(1);
|
|
74
|
+
}
|
|
75
|
+
done();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -1,442 +0,0 @@
|
|
|
1
|
-
fiftyoneDegreesManager = function() {
|
|
2
|
-
'use-strict';
|
|
3
|
-
var json = {{&_jsonObject}};
|
|
4
|
-
|
|
5
|
-
// Log any errors returned in the JSON object.
|
|
6
|
-
if(json.error !== undefined){
|
|
7
|
-
console.log(json.error);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// Set to true when the JSON object is complete.
|
|
11
|
-
var completed = false;
|
|
12
|
-
|
|
13
|
-
// changeFuncs is an array of functions. When onChange is called and passed
|
|
14
|
-
// a function, the function is registered and is called when processing is
|
|
15
|
-
// complete.
|
|
16
|
-
var changeFuncs = [];
|
|
17
|
-
|
|
18
|
-
// Counter is used to count how many pieces of callbacks are expected. Every
|
|
19
|
-
// time the completedCallback method is called, the counter is decremented
|
|
20
|
-
// by 1.
|
|
21
|
-
var callbackCounter = 0;
|
|
22
|
-
|
|
23
|
-
// Array of JavaScript properties that have started evaluation.
|
|
24
|
-
var jsPropertiesStarted = [];
|
|
25
|
-
|
|
26
|
-
// startsWith polyfill.
|
|
27
|
-
var startsWith = function(source, searchValue){
|
|
28
|
-
return source.lastIndexOf(searchValue, 0) === 0;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Get cookies with the '51D_' prefix that have been added to the request
|
|
32
|
-
// and return the data as key value pairs. This method is needed to extract
|
|
33
|
-
// cookie values for inclusion in the GET or POST request for situations
|
|
34
|
-
// where CORS will prevent cookies being sent to third parties.
|
|
35
|
-
var getFodCookies = function(){
|
|
36
|
-
var keyValuePairs = document.cookie.split(/; */);
|
|
37
|
-
var fodCookies = [];
|
|
38
|
-
for(var i = 0; i < keyValuePairs.length; i++) {
|
|
39
|
-
var name = keyValuePairs[i].substring(0, keyValuePairs[i].indexOf('='));
|
|
40
|
-
if(startsWith(name, "51D_")){
|
|
41
|
-
var value = keyValuePairs[i].substring(keyValuePairs[i].indexOf('=')+1);
|
|
42
|
-
fodCookies[name] = value;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return fodCookies;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
// Extract key value pairs from the '51D_' prefixed cookies and concatenates
|
|
49
|
-
// them to form a query string for the subsequent json refresh.
|
|
50
|
-
var getParametersFromCookies = function(){
|
|
51
|
-
var fodCookies = getFodCookies();
|
|
52
|
-
var keyValuePairs = [];
|
|
53
|
-
for (var key in fodCookies) {
|
|
54
|
-
if (fodCookies.hasOwnProperty(key)) {
|
|
55
|
-
keyValuePairs.push(key+"="+fodCookies[key]);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return keyValuePairs;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// Delete a cookie.
|
|
62
|
-
function deleteCookie(name) {
|
|
63
|
-
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// Fetch a value safely from the json object. If a key somewhere down the
|
|
67
|
-
// '.' separated hierarchy of keys is not present then 'undefined' is
|
|
68
|
-
// returned rather than letting an exception occur.
|
|
69
|
-
var getFromJson = function(key, allowObjects, allowBooleans) {
|
|
70
|
-
var result = undefined;
|
|
71
|
-
if(typeof allowObjects === 'undefined') { allowObjects = false; }
|
|
72
|
-
if(typeof allowBooleans === 'undefined') { allowBooleans = false; }
|
|
73
|
-
|
|
74
|
-
if (typeof(key) === 'string') {
|
|
75
|
-
var functions = json;
|
|
76
|
-
var segments = key.split('.');
|
|
77
|
-
var i = 0;
|
|
78
|
-
while (functions !== undefined && i < segments.length) {
|
|
79
|
-
functions = functions[segments[i++]];
|
|
80
|
-
}
|
|
81
|
-
if (typeof(functions) === "string") {
|
|
82
|
-
result = functions;
|
|
83
|
-
} else if (allowBooleans && typeof(functions) === "boolean") {
|
|
84
|
-
result = functions;
|
|
85
|
-
} else if (allowObjects && typeof functions === 'object' && functions !== null) {
|
|
86
|
-
result = functions;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Executed at the end of the processJSproperties method or for each piece
|
|
93
|
-
// of JavaScript which has 51D code injected. When there are 0 pieces of
|
|
94
|
-
// JavaScript left to process then reload the JSON object.
|
|
95
|
-
var completedCallback = function(resolve, reject){
|
|
96
|
-
callbackCounter--;
|
|
97
|
-
if (callbackCounter === 0){
|
|
98
|
-
{{#_updateEnabled}}
|
|
99
|
-
processRequest(resolve, reject);
|
|
100
|
-
{{/_updateEnabled}}
|
|
101
|
-
} else if (callbackCounter < 0){
|
|
102
|
-
reject('Too many callbacks.');
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Executes any Javascript contained in the json data. Sets the processedJs
|
|
107
|
-
// flag to true when there is no further Javascript to be processed.
|
|
108
|
-
var processJsProperties = function(resolve, reject, jsProperties, ignoreDelayFlag) {
|
|
109
|
-
var executeCallback = true;
|
|
110
|
-
var started = 0;
|
|
111
|
-
|
|
112
|
-
if (jsProperties !== undefined &&
|
|
113
|
-
jsProperties.length > 0) {
|
|
114
|
-
|
|
115
|
-
// Execute each of the Javascript property code snippets using the
|
|
116
|
-
// index of the value to access the value to avoid problems with
|
|
117
|
-
// JavaScript returning erroneous values.
|
|
118
|
-
for(var index = 0;
|
|
119
|
-
index < jsProperties.length;
|
|
120
|
-
index++) {
|
|
121
|
-
|
|
122
|
-
var name = jsProperties[index];
|
|
123
|
-
|
|
124
|
-
if(jsPropertiesStarted.includes(name) === false) {
|
|
125
|
-
// Create new function bound to this instance and execute it.
|
|
126
|
-
// This is needed to ensure the scope of the function is
|
|
127
|
-
// associated with this instance if any members are altered or
|
|
128
|
-
// added. Avoids global scoped variables.
|
|
129
|
-
var body = getFromJson(name);
|
|
130
|
-
var delay = getFromJson(name + 'delayexecution', false, true);
|
|
131
|
-
|
|
132
|
-
if ((ignoreDelayFlag || (delay === undefined || delay === false)) &&
|
|
133
|
-
body !== undefined) {
|
|
134
|
-
var func = undefined;
|
|
135
|
-
var searchString = '// 51D replace this comment with callback function.';
|
|
136
|
-
completed = false;
|
|
137
|
-
jsPropertiesStarted.push(name);
|
|
138
|
-
started++;
|
|
139
|
-
|
|
140
|
-
if(body.indexOf(searchString) !== -1){
|
|
141
|
-
callbackCounter++;
|
|
142
|
-
body = body.replace(/\/\/ 51D replace this comment with callback function./g, 'callbackFunc(resolveFunc, rejectFunc);');
|
|
143
|
-
func = new Function('callbackFunc', 'resolveFunc', 'rejectFunc',
|
|
144
|
-
"try {\n" +
|
|
145
|
-
body + "\n" +
|
|
146
|
-
"} catch (err) {\n" +
|
|
147
|
-
"console.log(err);" +
|
|
148
|
-
"}"
|
|
149
|
-
);
|
|
150
|
-
func(completedCallback, resolve, reject);
|
|
151
|
-
executeCallback = false;
|
|
152
|
-
} else {
|
|
153
|
-
func = new Function(
|
|
154
|
-
"try {\n" +
|
|
155
|
-
body + "\n" +
|
|
156
|
-
"} catch (err) {\n" +
|
|
157
|
-
"console.log(err);" +
|
|
158
|
-
"}"
|
|
159
|
-
);
|
|
160
|
-
func();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if(started === 0) {
|
|
168
|
-
executeCallback = false;
|
|
169
|
-
completed = true;
|
|
170
|
-
}
|
|
171
|
-
if(executeCallback) {
|
|
172
|
-
callbackCounter = 1;
|
|
173
|
-
completedCallback(resolve, reject);
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
{{#_updateEnabled}}
|
|
178
|
-
// Standard method to create a CORS HTTP request ready to send data.
|
|
179
|
-
var createCORSRequest = function(method, url) {
|
|
180
|
-
var xhr;
|
|
181
|
-
try {
|
|
182
|
-
xhr = new XMLHttpRequest();
|
|
183
|
-
} catch(err){
|
|
184
|
-
xhr = null;
|
|
185
|
-
}
|
|
186
|
-
if (xhr !== null && "withCredentials" in xhr) {
|
|
187
|
-
|
|
188
|
-
// Check if the XMLHttpRequest object has a "withCredentials"
|
|
189
|
-
// property.
|
|
190
|
-
// "withCredentials" only exists on XMLHTTPRequest2 objects.
|
|
191
|
-
xhr.open(method, url, true);
|
|
192
|
-
} else if (typeof XDomainRequest != "undefined") {
|
|
193
|
-
|
|
194
|
-
// Otherwise, check if XDomainRequest.
|
|
195
|
-
// XDomainRequest only exists in IE, and is IE's way of making CORS
|
|
196
|
-
// requests.
|
|
197
|
-
xhr = new XDomainRequest();
|
|
198
|
-
xhr.open(method, url);
|
|
199
|
-
} else {
|
|
200
|
-
|
|
201
|
-
// Otherwise, CORS is not supported by the browser.
|
|
202
|
-
xhr = null;
|
|
203
|
-
}
|
|
204
|
-
return xhr;
|
|
205
|
-
};
|
|
206
|
-
{{/_updateEnabled}}
|
|
207
|
-
|
|
208
|
-
// Check if the JSON object still has any JavaScript snippets to run.
|
|
209
|
-
var hasJSFunctions = function() {
|
|
210
|
-
for (var i = i; i < json.javascriptProperties; i++) {
|
|
211
|
-
var body = getFromJson(json.javascriptProperties[i]);
|
|
212
|
-
if (body !== undefined && body.length > 0) {
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return false;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// Process the JavaScript properties.
|
|
220
|
-
var process = function(resolve, reject){
|
|
221
|
-
processJsProperties(resolve, reject, json.javascriptProperties, false);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
var fireChangeFuncs = function(json) {
|
|
225
|
-
for (var i = 0; i < changeFuncs.length; i++) {
|
|
226
|
-
if (typeof changeFuncs[i] === 'function' &&
|
|
227
|
-
changeFuncs[i].length === 1) {
|
|
228
|
-
changeFuncs[i](json);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
{{#_updateEnabled}}
|
|
234
|
-
// Sends the cookie parameters that have been set by the executed Javascript
|
|
235
|
-
// in the POST body of a new request to refresh the json data. Resolve is
|
|
236
|
-
// called with the new json if the request is processed as expected. Reject
|
|
237
|
-
// is called if there was a problem. The parameters are send as a POST
|
|
238
|
-
// request so that encoding and data length issues are minimised.
|
|
239
|
-
var processRequest = function(resolve, reject){
|
|
240
|
-
// Request URL with a license key and User-Agent if provided.
|
|
241
|
-
var xhr = createCORSRequest('POST', '{{{_url}}}');
|
|
242
|
-
|
|
243
|
-
// If there is no support for HTTP requests then call reject and throw
|
|
244
|
-
// a no CORS support error.
|
|
245
|
-
if (!xhr) {
|
|
246
|
-
reject(new Error('CORS not supported'));
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Add the HTTP header for POST form data.
|
|
251
|
-
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
252
|
-
|
|
253
|
-
xhr.onload = function () {
|
|
254
|
-
|
|
255
|
-
// Get the response body from the request.
|
|
256
|
-
var responseText = xhr.responseText;
|
|
257
|
-
|
|
258
|
-
// Process the response as json and call the resolve method.
|
|
259
|
-
json = JSON.parse(responseText);
|
|
260
|
-
|
|
261
|
-
if (hasJSFunctions()) {
|
|
262
|
-
// json updated so fire 'on change' functions
|
|
263
|
-
// before executing any new JS properties that
|
|
264
|
-
// have come back.
|
|
265
|
-
fireChangeFuncs(json);
|
|
266
|
-
process(resolve, reject);
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
completed = true;
|
|
270
|
-
// json updated so fire 'on change' functions
|
|
271
|
-
// This must happen after completed = true in order
|
|
272
|
-
// for 'complete' functions to fire.
|
|
273
|
-
fireChangeFuncs(json);
|
|
274
|
-
resolve(json);
|
|
275
|
-
{{^_enableCookies}}
|
|
276
|
-
var fodCookies = getFodCookies();
|
|
277
|
-
for (var key in fodCookies) {
|
|
278
|
-
if (fodCookies.hasOwnProperty(key)) {
|
|
279
|
-
deleteCookie(key);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
{{/_enableCookies}}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
xhr.onerror = function () {
|
|
288
|
-
// An error occurred with the request. Return the details in the
|
|
289
|
-
// call to reject method.
|
|
290
|
-
reject(Error(xhr.statusText));
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
// Get additional parameters from cookies in case they are not sent
|
|
294
|
-
// by the browser.
|
|
295
|
-
var params = getParametersFromCookies();
|
|
296
|
-
|
|
297
|
-
// Send the cookie parameters as the POST body.
|
|
298
|
-
xhr.send(params.join('&').replace(/%20/g, '+'));
|
|
299
|
-
}
|
|
300
|
-
{{/_updateEnabled}}
|
|
301
|
-
|
|
302
|
-
// Function logs errors, used to 'reject' a promise or for error callbacks.
|
|
303
|
-
var catchError = function(value) {
|
|
304
|
-
console.log(value.message || value);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// Populate this instance of the FOD object with getters to access the
|
|
308
|
-
// properties. If the value is null then get the noValueMessage from the
|
|
309
|
-
// JSON object corresponding to the property.
|
|
310
|
-
var update = function(data){
|
|
311
|
-
var self = this;
|
|
312
|
-
Object.getOwnPropertyNames(data).forEach(function(key) {
|
|
313
|
-
self[key] = {};
|
|
314
|
-
for(var i in data[key]){
|
|
315
|
-
var obj = self[key];
|
|
316
|
-
(function(i) {
|
|
317
|
-
Object.defineProperty(obj, i, {
|
|
318
|
-
get: function (){
|
|
319
|
-
if(data[key][i] === null && (i !== "javascriptProperties")){
|
|
320
|
-
return data[key][i + "nullreason"];
|
|
321
|
-
} else {
|
|
322
|
-
return data[key][i];
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
})
|
|
326
|
-
})(i);
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
{{#_hasDelayedProperties}}
|
|
332
|
-
// Get the JS property(s) that, when evaluated, will populate
|
|
333
|
-
// evidence that can be used to determine the value of the
|
|
334
|
-
// supplied property.
|
|
335
|
-
// The supplied name can either be a complete property name or a top level
|
|
336
|
-
// aspect name.
|
|
337
|
-
// Where the aspect name is given, ALL evidence properties under that
|
|
338
|
-
// key will be returned.
|
|
339
|
-
// Example property names are 'location.country' or 'devices.profiles.hardwarename'
|
|
340
|
-
// Example aspect names are 'location' or 'devices'
|
|
341
|
-
var getEvidenceProperties = function (name) {
|
|
342
|
-
var evidenceProperties = getFromJson(name + 'evidenceproperties');
|
|
343
|
-
if(typeof evidenceProperties === "undefined") {
|
|
344
|
-
var item = getFromJson(name, true);
|
|
345
|
-
evidenceProperties = getEvidencePropertiesFromObject(item);
|
|
346
|
-
}
|
|
347
|
-
return evidenceProperties;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// Get all values in any 'evidenceproperty' fields on this object
|
|
351
|
-
// or sub-objects.
|
|
352
|
-
var getEvidencePropertiesFromObject = function (dataObject) {
|
|
353
|
-
evidenceProperties = [];
|
|
354
|
-
|
|
355
|
-
for (var prop in dataObject) {
|
|
356
|
-
if (dataObject.hasOwnProperty(prop)) {
|
|
357
|
-
var value = dataObject[prop];
|
|
358
|
-
// Property name ends with 'evidenceproperties' so is
|
|
359
|
-
// what we're looking for.
|
|
360
|
-
// Add the values to the array if we don't already have it.
|
|
361
|
-
if (value !== null && Array.isArray(value) && prop.endsWith('evidenceproperties')) {
|
|
362
|
-
value.forEach(function(item, index) {
|
|
363
|
-
if(evidenceProperties.includes(item) === false) {
|
|
364
|
-
evidenceProperties.push(item);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
// Item is an object so recursively call this method
|
|
369
|
-
// and add any resulting evidence properties to the list.
|
|
370
|
-
else if(typeof value === 'object' && value !== null) {
|
|
371
|
-
getEvidencePropertiesFromObject(value).forEach(function(item, index) {
|
|
372
|
-
if(evidenceProperties.includes(item) === false) {
|
|
373
|
-
evidenceProperties.push(item);
|
|
374
|
-
}
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
return evidenceProperties;
|
|
381
|
-
}
|
|
382
|
-
{{/_hasDelayedProperties}}
|
|
383
|
-
|
|
384
|
-
{{#_supportsPromises}}
|
|
385
|
-
this.promise = new Promise(function(resolve, reject) {
|
|
386
|
-
process(resolve,reject);
|
|
387
|
-
});
|
|
388
|
-
{{/_supportsPromises}}
|
|
389
|
-
|
|
390
|
-
this.onChange = function(resolve) {
|
|
391
|
-
changeFuncs.push(resolve);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
this.complete = function(resolve, properties) {
|
|
395
|
-
{{#_hasDelayedProperties}}
|
|
396
|
-
// If properties is set then check if we need to kick off
|
|
397
|
-
// processing of anything.
|
|
398
|
-
if(typeof properties !== "undefined") {
|
|
399
|
-
// If properties is a string then split on comma to produce
|
|
400
|
-
// an array of one or more key names.
|
|
401
|
-
if(typeof properties === "string") {
|
|
402
|
-
properties = properties.split(',');
|
|
403
|
-
}
|
|
404
|
-
if(Array.isArray(properties)) {
|
|
405
|
-
properties.forEach(function(key, i) {
|
|
406
|
-
// We pass an empty function rather than 'resolve' because we
|
|
407
|
-
// don't want to call resolve when a single evidence function
|
|
408
|
-
// evaluates but after all of them have completed.
|
|
409
|
-
// This is handled by the 'if(complete)' code below.
|
|
410
|
-
processJsProperties(function(json) {}, catchError, getEvidenceProperties(key), true);
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
{{/_hasDelayedProperties}}
|
|
416
|
-
if(completed){
|
|
417
|
-
resolve(json);
|
|
418
|
-
}else{
|
|
419
|
-
this.onChange(function(data) {
|
|
420
|
-
if(completed){
|
|
421
|
-
resolve(data);
|
|
422
|
-
}
|
|
423
|
-
})
|
|
424
|
-
}
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
// Update this instance with the initial JSON payload.
|
|
428
|
-
update.call(this, json);
|
|
429
|
-
{{#_supportsPromises}}
|
|
430
|
-
var parent = this;
|
|
431
|
-
this.promise.then(function(value) {
|
|
432
|
-
// JSON has been updated so replace the current instance.
|
|
433
|
-
update.call(parent, value);
|
|
434
|
-
completed = true;
|
|
435
|
-
}).catch(catchError);
|
|
436
|
-
{{/_supportsPromises}}
|
|
437
|
-
{{^_supportsPromises}}
|
|
438
|
-
process(function(json) {}, catchError);
|
|
439
|
-
{{/_supportsPromises}}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
var {{_objName}} = new fiftyoneDegreesManager();
|