Skip to content

Commit

Permalink
slim down
Browse files Browse the repository at this point in the history
  • Loading branch information
schmod committed Sep 7, 2022
1 parent 328ded9 commit 196ba64
Show file tree
Hide file tree
Showing 8 changed files with 1,396 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Version 2.0

- **v2.4.3**

- Slimmed down the size/complexity of the built regexps.js file. The regexps are now stored in a normal array.

- **v2.4.0**

- Forked and renamed to useragent-ng
Expand Down
47 changes: 47 additions & 0 deletions benchmark/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"microtime": "",
"benchmark": "",
"useragent": "2.3.0",
"useragent-ng": "2.4.1",
"ua-parser": "0.3.1",
"useragent_parser": "1.0.0",
"useragent-parser": "0.1.1"
Expand Down
14 changes: 10 additions & 4 deletions benchmark/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var microtime = require('microtime')
/**
* Useragent parsers.
*/
var useragent2 = require('../')
var useragentNg2 = require('../')
, useragentNg = require('useragent-ng')
, useragent = require('useragent');

/**
Expand All @@ -37,12 +38,17 @@ var useragentlist = path.join(__dirname, '..', 'test', 'fixtures', file+'.yaml')
var froomfroom = new benchmark.Suite;

froomfroom
.add('useragent latest', function () {
.add('useragent-ng latest', function () {
for (var i = 0; i < length; i++ ) {
useragent2.parse(testcases[i]);
useragentNg2.parse(testcases[i]);
}
})
.add('useragent1', function () {
.add('useragent-ng npm', function () {
for (var i = 0; i < length; i++ ) {
useragentNg.parse(testcases[i]);
}
})
.add('useragent classic', function () {
for (var i = 0; i < length; i++ ) {
useragent.parse(testcases[i]);
}
Expand Down
94 changes: 49 additions & 45 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ function getKnown() {
return null;
}

var old = require('./regexps');
var known = new Set();
for (var type in old) {
if (!old.hasOwnProperty(type)) continue;
var group = old[type];
for (var i = 0; i < group.length; i++) {
var regex = group[i][0].source;
known.add(regex);
try {
var old = require('./regexps');
for (var type in old) {
if (!old.hasOwnProperty(type)) continue;
var group = old[type];
for (var i = 0; i < group.length; i++) {
var regex = group[i][0].source;
known.add(regex);
}
}
} catch (e) {
console.warn("Error parsing old regexps.js file. Ignoring and continuing...", e)
}
return known;
}
Expand Down Expand Up @@ -150,76 +154,77 @@ exports.parse = function parse(sources, callback) {
results[details.resource] = results[details.resource] || [];

var resources = data[details.resource]
, name = details.resource.replace('_parsers', '')
, resource
, regex
, source
, parser;
, parser
, compiled;

for (var i = 0, l = resources.length; i < l; i++) {
resource = resources[i];
regex = resource.regex;

source = new RegExp(regex).source;
if (known && !known.has(source)) {

compiled = new RegExp(regex, resource.regex_flag);
if (known && !known.has(compiled.source)) {
// A quick check, regexes not matching those are clearly safe
// This check excludes about 35% of all regexps we have
if (!isSafe(source)) {
unsafe.push(source);
if (!isSafe(compiled.source)) {
unsafe.push(compiled.source);
}
known.add(source);
known.add(compiled.source);
}

// We need to JSON stringify the data to properly add slashes escape other
// kinds of crap in the RegularExpression. If we don't do thing we get
// some illegal token warnings.
parser = 'parser = Object.create(null);\n';
parser += 'parser[0] = new RegExp('+ JSON.stringify(regex)
if (resource.regex_flag) {
parser += ',"' + resource.regex_flag + '"';
}
parser += ');\n';
// some illegal token warnings./(Linux)(?:[ /](\d+)\.(\d+)(?:\.(\d+)|)|)/

// parser = 'parser = Object.create(null);\n';
parser = ' [' + compiled.toString();

// Check if we have replacement for the parsed family name
if (resource[details.replacement]) {
parser += 'parser[1] = "'+ resource[details.replacement].replace('"', '\\"') +'";\n';
parser += ', "'+ resource[details.replacement].replace('"', '\\"') +'"';
} else {
parser += 'parser[1] = 0;\n';
parser += ', 0';
}

if (details.name === 'device') {
if (resource.brand_replacement) {
parser += 'parser[2] = "'+ resource.brand_replacement.replace('"', '\\"') +'";\n';
parser += ', "'+ resource.brand_replacement.replace('"', '\\"') +'"';
} else if (resource.model_replacement) {
parser += 'parser[2] = 0;\n';
parser += ', 0';
}

if (resource.model_replacement) {
parser += 'parser[3] = "'+ resource.model_replacement.replace('"', '\\"') +'";\n';
parser += ', "'+ resource.model_replacement.replace('"', '\\"') +'"';
}
} else {
if (resource.v1_replacement) {
parser += 'parser[2] = "'+ resource.v1_replacement.replace('"', '\\"') +'";\n';
parser += ', "'+ resource.v1_replacement.replace('"', '\\"') +'"';
} else if (resource.v2_replacement || resource.v3_replacement) {
parser += 'parser[2] = 0;\n';
parser += ', 0';
}

if (resource.v2_replacement) {
parser += 'parser[3] = "'+ resource.v2_replacement.replace('"', '\\"') +'";\n';
parser += ', "'+ resource.v2_replacement.replace('"', '\\"') +'"';
} else if (resource.v3_replacement) {
parser += 'parser[3] = 0;\n';
parser += ', 0';
}

if (resource.v3_replacement) {
parser += 'parser[4] = "'+ resource.v3_replacement.replace('"', '\\"') +'";\n';
parser += ', "'+ resource.v3_replacement.replace('"', '\\"') +'"';
}
}

parser += 'exports.'+ details.name +'['+ i +'] = parser;';
parser += ']';

// parser += 'exports.'+ details.name +'['+ i +'] = parser;';
results[details.resource].push(parser);
}
});

// TODO: consider getting rid of these checks - we either need to scan the REs for REDoS vulnerabilities
// ourselves OR consider uap-core to be trustworthy-enough. (The latter seems fine)
if (unsafe.length > 0) {
console.log('There are new regexps! Here they are, one per line:');
for (var i = 0; i < unsafe.length; i++) {
Expand All @@ -245,18 +250,17 @@ exports.generate = function generate(results, callback) {
var regexps = [
'"use strict";'
, exports.LEADER
, 'var parser;'
, 'exports.browser = Object.create(null);'
, results.user_agent_parsers.join('\n')
, 'exports.browser.length = '+ results.user_agent_parsers.length +';'

, 'exports.device = Object.create(null);'
, results.device_parsers.join('\n')
, 'exports.device.length = '+ results.device_parsers.length +';'

, 'exports.os = Object.create(null);'
, results.os_parsers.join('\n')
, 'exports.os.length = '+ results.os_parsers.length +';'
, 'exports.browser = ['
, results.user_agent_parsers.join(',\n')
, '];'

, 'exports.device = ['
, results.device_parsers.join(',\n')
, '];'

, 'exports.os = ['
, results.os_parsers.join(',\n')
, '];'
].join('\n\n');

// Now that we have generated the structure for the RegExps export file we
Expand Down
Loading

0 comments on commit 196ba64

Please sign in to comment.