-
Notifications
You must be signed in to change notification settings - Fork 4
/
configurator.js
116 lines (104 loc) · 4.57 KB
/
configurator.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
/**
* This example is displayed at the end of the [Configurator](https://configure.51degrees.com/)
* process, which is used to create resource keys for use with the 51Degrees cloud service.
*
* It shows how to call the cloud with the newly created key and how to access the values
* of the selected properties.
*
* See [Getting Started](https://51degrees.com/documentation/_examples__device_detection__getting_started__console__cloud.html)
* for a fuller example.
*
* Required npm Dependencies:
* - fiftyone.devicedetection
*/
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../node_modules/', requestedPackage));
} catch (e) {
return require(path.join(__dirname, '/../../../../', requestedPackage));
}
};
const DeviceDetectionCloudPipelineBuilder = require51('fiftyone.devicedetection.cloud').DeviceDetectionCloudPipelineBuilder;
const ExampleUtils = require(path.join(__dirname, '/../exampleUtils'));
const run = async function (resourceKey, output) {
// The pipeline should be managed as a singleton. Creating a pipeline instance for every request
// will cause extreme resource problems.
const pipeline = new DeviceDetectionCloudPipelineBuilder({
resourceKey,
// inhibit sharing usage for this test, usually this should be set 'true'.
shareUsage: false
}).build();
// To monitor the pipeline we can put in listeners for various log events.
// Valid types are info, debug, warn, error
pipeline.on('error', console.error);
// Get a flow data from the singleton pipeline for each detection
const data = pipeline.createFlowData();
// Add the evidence values to the flow data
data.evidence.add(
'header.user-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/98.0.4758.102 Safari/537.36');
data.evidence.add(
'header.sec-ch-ua-mobile',
'?0');
data.evidence.add(
'header.sec-ch-ua',
'" Not A; Brand";v="99", "Chromium";v="98", ' +
'"Google Chrome";v="98"');
data.evidence.add(
'header.sec-ch-ua-platform',
'"Windows"');
data.evidence.add(
'header.sec-ch-ua-platform-version',
'"14.0.0"');
// Process the flow data.
await data.process();
// Get the results.
const device = data.device;
output.write(`device.ismobile: ${device.ismobile.value}\n`);
};
// Don't run the server if under TEST
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
// Use the supplied resource key or try to obtain one from the environment variable.
const resourceKey = args.length > 0 ? args[0] : process.env[ExampleUtils.RESOURCE_KEY_ENV_VAR];
// If we don't have a resource key then log an error
if (!resourceKey) {
console.log(
'No resource key specified on the command line or in the environment variable ' +
`'${ExampleUtils.RESOURCE_KEY_ENV_VAR}'. The 51Degrees cloud service is accessed ` +
'using a \'ResourceKey\'. For more information see ' +
'https://51degrees.com/documentation/_info__resource_keys.html. A resource key with the ' +
'properties required by this example can be created for free at ' +
'https://configure.51degrees.com/g3gMZdPY. Once complete, populate the config file or ' +
'environment variable mentioned at the start of this message with the key.');
} else {
run(resourceKey, process.stdout);
}
}
module.exports = {
run
};