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 fs = require('fs');
const http = require('http');
const pug = require('pug');
const compiledFunction =
pug.compileFile(path.join(__dirname, '/index.pug'));
const core = require51('fiftyone.pipeline.core');
const optionsExtension =
require51('fiftyone.devicedetection.shared').optionsExtension;
const dataExtension =
require51('fiftyone.devicedetection.shared').dataExtension;
require(path.join(__dirname, '/../exampleUtils'));
let pipeline;
const setPipeline = (options) => {
const dataFilePath = optionsExtension.getDataFilePath(options);
if (!dataFilePath) {
throw 'A data file must be specified in the 51d.json file.';
}
if (!fs.existsSync(dataFilePath)) {
const newDataFilePath =
ExampleUtils.
findFile(path.basename(dataFilePath));
if (newDataFilePath !== undefined) {
optionsExtension.setDataFilePath(options, newDataFilePath);
console.warn('Failed to find a device detection data file at ' +
`at the specified path '${dataFilePath}'. Use '${newDataFilePath}' ` +
'instead.');
} else {
throw 'Failed to find a device detection data file at ' +
`'${dataFilePath}'. If using the lite file, then make sure the ` +
'device-detection-data submodule has been updated by running ' +
'\'git submodule update --recursive\'. Otherwise, ensure that the ' +
'filename is correct in 51d.json.';
}
}
pipeline = new core.PipelineBuilder({
addJavaScriptBuilder: true,
javascriptBuilderSettings: {
endPoint: '/json'
}
}).buildFromConfiguration(options);
pipeline.on('error', console.error);
};
const server = http.createServer((req, res) => {
const flowData = pipeline.createFlowData();
flowData.evidence.addFromRequest(req);
if (req.url.startsWith('/json')) {
flowData.process().then(function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(flowData.jsonbundler.json));
});
} else {
flowData.process().then(function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
core.Helpers.setResponseHeaders(res, flowData);
const allEvidence = flowData.evidence.getAll();
const evidences =
pipeline.getElement('device').evidenceKeyFilter.filterEvidence(
allEvidence);
res.end(compiledFunction(
{
dataFilePublishedTime: new Date().getTime(),
dataFileAgeWarning: DATA_FILE_AGE_WARNING,
responseHeaders: res.getHeaders(),
evidenceUsed: evidences,
allEvidence,
fiftyOneJs: flowData.javascriptbuilder.javascript,
hardwareVendor: dataExtension.getValueHelper(flowData.device, 'hardwarevendor'),
hardwareName: dataExtension.getValueHelper(flowData.device, 'hardwarename'),
deviceType: dataExtension.getValueHelper(flowData.device, 'devicetype'),
platformVendor: dataExtension.getValueHelper(flowData.device, 'platformvendor'),
platformName: dataExtension.getValueHelper(flowData.device, 'platformname'),
platformVersion: dataExtension.getValueHelper(flowData.device, 'platformversion'),
browserVendor: dataExtension.getValueHelper(flowData.device, 'browservendor'),
browserName: dataExtension.getValueHelper(flowData.device, 'browsername'),
browserVersion: dataExtension.getValueHelper(flowData.device, 'browserversion'),
screenWidth: dataExtension.getValueHelper(flowData.device, 'screenpixelswidth'),
screenHeight: dataExtension.getValueHelper(flowData.device, 'screenpixelsheight')
})
);
});
}
});
if (process.env.JEST_WORKER_ID === undefined) {
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')));
setPipeline(options);
const port = 3001;
const hostname = 'localhost';
server.listen(port, hostname);
console.log(`Server listening on: http:
};
module.exports = {
server,
setPipeline
};
Definition fiftyone.devicedetection.onpremise/examples/onpremise/exampleUtils.js:36