4 * Build in Native modules.
6 var path = require('path')
9 , tmp = require('tmp');
12 * Third party modules.
14 var request = require('request')
15 , yaml = require('yamlparser');
18 * Update the regexp.js file
20 * @param {Function} callback Completion callback.
23 exports.update = function update(callback) {
24 // Prepend local additions that are missing from the source
25 fs.readFile(exports.before, 'utf8', function reading(err, before) {
26 if (err) return callback(err);
28 // Fetch the remote resource as that is frequently updated
29 request(exports.remote, function downloading(err, res, remote) {
30 if (err) return callback(err);
31 if (res.statusCode !== 200) return callback(new Error('Invalid statusCode returned'));
33 // Append get some local additions that are missing from the source
34 fs.readFile(exports.after, 'utf8', function reading(err, after) {
35 if (err) return callback(err);
38 exports.parse([ before, remote, after ], function parsing(err, results, source) {
39 callback(err, results);
41 if (!source || err) return;
44 // Save to a tmp file to avoid potential concurrency issues.
46 tmp.file(function (err, tempFilePath) {
49 fs.writeFile(tempFilePath, source, function idk(err) {
52 fs.rename(tempFilePath, exports.output, function(err) {
64 * Parse the given sources.
66 * @param {Array} sources String versions of the source
67 * @param {Function} callback completion callback
70 exports.parse = function parse(sources, callback) {
73 var data = sources.reduce(function parser(memo, data) {
74 // Try to repair some of the odd structures that are in the yaml files
75 // before parsing it so we generate a uniform structure:
77 // Normalize the Operating system versions:
78 data = data.replace(/os_v([1-3])_replacement/gim, function replace(match, version) {
79 return 'v'+ version +'_replacement';
82 // Make sure that we are able to parse the yaml string
83 try { data = yaml.eval(data); }
90 // merge the data with the memo;
91 Object.keys(data).forEach(function (key) {
92 var results = data[key];
93 memo[key] = memo[key] || [];
95 for (var i = 0, l = results.length; i < l; i++) {
96 memo[key].push(results[i]);
105 resource: 'user_agent_parsers'
106 , replacement: 'family_replacement'
110 resource: 'device_parsers'
111 , replacement: 'device_replacement'
115 resource: 'os_parsers'
116 , replacement: 'os_replacement'
119 ].forEach(function parsing(details) {
120 results[details.resource] = results[details.resource] || [];
122 var resources = data[details.resource]
123 , name = details.resource.replace('_parsers', '')
127 for (var i = 0, l = resources.length; i < l; i++) {
128 resource = resources[i];
130 // We need to JSON stringify the data to properly add slashes escape other
131 // kinds of crap in the RegularExpression. If we don't do thing we get
132 // some illegal token warnings.
133 parser = 'parser = Object.create(null);\n';
134 parser += 'parser[0] = new RegExp('+ JSON.stringify(resource.regex) + ');\n';
136 // Check if we have replacement for the parsed family name
137 if (resource[details.replacement]) {
138 parser += 'parser[1] = "'+ resource[details.replacement].replace('"', '\\"') +'";';
140 parser += 'parser[1] = 0;';
145 if (resource.v1_replacement) {
146 parser += 'parser[2] = "'+ resource.v1_replacement.replace('"', '\\"') +'";';
148 parser += 'parser[2] = 0;';
153 if (resource.v2_replacement) {
154 parser += 'parser[3] = "'+ resource.v2_replacement.replace('"', '\\"') +'";';
156 parser += 'parser[3] = 0;';
161 if (resource.v3_replacement) {
162 parser += 'parser[4] = "'+ resource.v3_replacement.replace('"', '\\"') +'";';
164 parser += 'parser[4] = 0;';
168 parser += 'exports.'+ details.name +'['+ i +'] = parser;';
169 results[details.resource].push(parser);
173 // Generate a correct format
174 exports.generate(results, callback);
178 * Generate the regular expressions file source code.
180 * @param {Object} results The parsed result of the regexp.yaml.
181 * @param {Function} callback Completion callback
184 exports.generate = function generate(results, callback) {
189 , 'exports.browser = Object.create(null);'
190 , results.user_agent_parsers.join('\n')
191 , 'exports.browser.length = '+ results.user_agent_parsers.length +';'
193 , 'exports.device = Object.create(null);'
194 , results.device_parsers.join('\n')
195 , 'exports.device.length = '+ results.device_parsers.length +';'
197 , 'exports.os = Object.create(null);'
198 , results.os_parsers.join('\n')
199 , 'exports.os.length = '+ results.os_parsers.length +';'
202 // Now that we have generated the structure for the RegExps export file we
203 // need to validate that we created a JavaScript compatible file, if we would
204 // write the file without checking it's content we could be breaking the
207 exports: {} // Emulate a module context, so everything is attached here
210 // Crossing our fingers that it worked
211 try { vm.runInNewContext(regexps, sandbox, 'validating.vm'); }
212 catch (e) { return callback(e, null, regexps); }
214 callback(undefined, sandbox.exports, regexps);
218 * The location of the ua-parser regexes yaml file.
223 exports.remote = 'https://raw.githubusercontent.com/ua-parser/uap-core/master/regexes.yaml';
226 * The locations of our local regexes yaml files.
231 exports.before = path.resolve(__dirname, '..', 'static', 'user_agent.before.yaml');
232 exports.after = path.resolve(__dirname, '..', 'static', 'user_agent.after.yaml');
235 * The the output location for the generated regexps file
240 exports.output = path.resolve(__dirname, '..', 'lib', 'regexps.js');
243 * The leader that needs to be added so people know they shouldn't touch all the
249 exports.LEADER = fs.readFileSync(path.join(__dirname, 'donotedit'), 'UTF-8');