Skip to content

Migrate YAML-based Regex Rules to CommonJS Modules #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ const aliasDevice = new AliasDevice();
aliasDevice.setReplaceBrand(false);
const infoDevice = new InfoDevice();

IndexerDevice.init();
IndexerClient.init();

class DeviceDetector {

vendorParserList = {};
Expand Down
34 changes: 1 addition & 33 deletions parser/abstract-parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const helper = require('./helper');
const BASE_REGEXES_DIR = __dirname + '/../regexes/';

/**
* @param {string} result
Expand All @@ -20,45 +19,14 @@ function fixStringVersion(result) {
return result.trim();
}

const collectionMap = {};

class ParserAbstract {
constructor() {
this.fixtureFile = null;
this.collection = null;
this.type = null;
this.versionTruncation = null;
this.maxUserAgentSize = null;
}

get collection() {
if (!this.hasLoadCollection()) {
return null;
}
return collectionMap[this.fixtureFile];
}

hasLoadCollection() {
return collectionMap[this.fixtureFile] !== void 0
}

/**
* load collection
*/
loadCollection() {
if (!this.hasLoadCollection()) {
collectionMap[this.fixtureFile] = this.loadYMLFile(this.fixtureFile);
}
}

/**
* load yaml file
* @param {string} file
* @returns {*}
*/
loadYMLFile(file) {
return helper.loadYMLFile(BASE_REGEXES_DIR + file);
}

Comment on lines -33 to -61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer required as the collections are in memory.

/**
* A special method that overwrites placeholders in a string
* @param {string} item
Expand Down
3 changes: 1 addition & 2 deletions parser/bot-abstract-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const ParserAbstract = require('./abstract-parser');
class BotAbstractParser extends ParserAbstract {
constructor() {
super();
this.fixtureFile = 'bots.yml';
this.loadCollection();
this.collection = require('../regexes/bots');
}

/**
Expand Down
10 changes: 2 additions & 8 deletions parser/client/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ const compareBrandForClientHints = (brand) => {
class Browser extends ClientAbstractParser {
constructor() {
super();
this.engine_collection = [];
this.fixtureFile = 'client/browsers.yml';
this.loadCollection();
this.engine_collection = require('../../regexes/client/browser_engine');
this.collection = require('../../regexes/client/browsers');
this.type = CLIENT_TYPE.BROWSER;
this.collectionLength = this.collection.length;
}
Expand All @@ -50,11 +49,6 @@ class Browser extends ClientAbstractParser {
return Object.keys(this.getCollectionBrowsers());
}

loadCollection() {
super.loadCollection();
this.engine_collection = this.loadYMLFile('client/browser_engine.yml');
}

/**
* Generates the result for the parse method
*
Expand Down
3 changes: 1 addition & 2 deletions parser/client/feed-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const CLIENT_TYPE = require('./../const/client-type');
class FeedReader extends ClientAbstractParser {
constructor() {
super();
this.fixtureFile = 'client/feed_readers.yml';
this.loadCollection();
this.collection = require('../../regexes/client/feed_readers');
this.collectionLength = this.collection.length;
this.type = CLIENT_TYPE.FEED_READER;
}
Expand Down
3 changes: 1 addition & 2 deletions parser/client/hints/app-hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class AppHints extends AbstractParser
{
constructor() {
super();
this.fixtureFile = 'client/hints/apps.yml';
this.loadCollection();
this.collection = require('../../../regexes/client/hints/apps');
}

parse(clientHints) {
Expand Down
3 changes: 1 addition & 2 deletions parser/client/hints/browser-hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class BrowserHints extends AbstractParser
{
constructor() {
super();
this.fixtureFile = 'client/hints/browsers.yml';
this.loadCollection();
this.collection = require('../../../regexes/client/hints/browsers');
}

parse(clientHints) {
Expand Down
17 changes: 1 addition & 16 deletions parser/client/indexer-client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const helper = require('../helper');
const CLIENT_TYPES = require('../const/client-type');
const collection = require('../../regexes/client-index-hash');

const CLIENT_TYPES_MAP = {}
CLIENT_TYPES_MAP[CLIENT_TYPES.BROWSER] = 0;
Expand All @@ -9,8 +10,6 @@ CLIENT_TYPES_MAP[CLIENT_TYPES.MEDIA_PLAYER] = 3;
CLIENT_TYPES_MAP[CLIENT_TYPES.FEED_READER] = 4;
CLIENT_TYPES_MAP[CLIENT_TYPES.PIM] = 5;

let collection;
let path = __dirname + '/../../regexes/client-index-hash.yml';

class IndexerClient {

Expand All @@ -20,10 +19,6 @@ class IndexerClient {
* @returns {null|array}
*/
static findClientRegexPositionsForUserAgent(userAgent, type) {
if (!IndexerClient.ready()) {
return null;
}

let index = CLIENT_TYPES_MAP[type];
if (index === void 0) {
return null;
Expand All @@ -39,16 +34,6 @@ class IndexerClient {
return null;
}

static ready() {
return collection !== void 0;
}

static init() {
if (helper.hasFile(path)) {
collection = helper.loadYMLFile(path);
}
}

Comment on lines -42 to -51
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer required as the collection is ready once this module is resolved.

}

module.exports = IndexerClient;
3 changes: 1 addition & 2 deletions parser/client/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const CLIENT_TYPE = require('./../const/client-type');
class Library extends ClientAbstractParser {
constructor() {
super();
this.fixtureFile = 'client/libraries.yml';
this.loadCollection();
this.collection = require('../../regexes/client/libraries');
this.collectionLength = this.collection.length;
this.type = CLIENT_TYPE.LIBRARY;
}
Expand Down
3 changes: 1 addition & 2 deletions parser/client/media-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const CLIENT_TYPE = require('./../const/client-type');
class MediaPlayer extends ClientAbstractParser {
constructor() {
super();
this.fixtureFile = 'client/mediaplayers.yml';
this.loadCollection();
this.collection = require('../../regexes/client/mediaplayers');
this.collectionLength = this.collection.length;
this.type = CLIENT_TYPE.MEDIA_PLAYER;
}
Expand Down
3 changes: 1 addition & 2 deletions parser/client/mobile-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const appHints = new AppHints;
class MobileApp extends ClientAbstractParser {
constructor() {
super();
this.fixtureFile = 'client/mobile_apps.yml';
this.loadCollection();
this.collection = require('../../regexes/client/mobile_apps');
this.collectionLength = this.collection.length;
this.type = CLIENT_TYPE.MOBILE_APP;
}
Expand Down
3 changes: 1 addition & 2 deletions parser/client/pim.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const CLIENT_TYPE = require('./../const/client-type');
class PIM extends ClientAbstractParser {
constructor() {
super();
this.fixtureFile = 'client/pim.yml';
this.loadCollection();
this.collection = require('../../regexes/client/pim');
this.collectionLength = this.collection.length;
this.type = CLIENT_TYPE.PIM;
}
Expand Down
3 changes: 1 addition & 2 deletions parser/device/alias-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class AliasDevice extends AbstractParser {
#replaceBrand = true;
constructor() {
super();
this.fixtureFile = 'device/alias-device.yml';
this.loadCollection();
this.collection = require('../../regexes/device/alias-device');
}

/**
Expand Down
3 changes: 1 addition & 2 deletions parser/device/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const DeviceAbstractParser = require('./../device-abstract-parser');
class Camera extends DeviceAbstractParser {
constructor() {
super();
this.fixtureFile = 'device/cameras.yml';
this.loadCollection();
this.collection = require('../../regexes/device/cameras');
}
}

Expand Down
3 changes: 1 addition & 2 deletions parser/device/car-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const DeviceAbstractParser = require('./../device-abstract-parser');
class CarBrowser extends DeviceAbstractParser {
constructor() {
super();
this.fixtureFile = 'device/car_browsers.yml';
this.loadCollection();
this.collection = require('../../regexes/device/car_browsers');
}
}

Expand Down
3 changes: 1 addition & 2 deletions parser/device/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const DeviceAbstractParser = require('./../device-abstract-parser');
class Console extends DeviceAbstractParser {
constructor() {
super();
this.fixtureFile = 'device/consoles.yml';
this.loadCollection();
this.collection = require('../../regexes/device/consoles');
}
}

Expand Down
3 changes: 1 addition & 2 deletions parser/device/hbb-tv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class HbbTv extends DeviceAbstractParser {
*/
constructor() {
super();
this.fixtureFile = 'device/televisions.yml';
this.loadCollection();
this.collection = require('../../regexes/device/televisions');
}

/**
Expand Down
19 changes: 1 addition & 18 deletions parser/device/indexer-device.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const helper = require("../helper");

let collection;
let path = __dirname + '/../../regexes/device-index-hash.yml';
const collection = require('../../regexes/device-index-hash');

class IndexerDevice {
static findDeviceBrandsForDeviceCode(deviceCode) {
if (!IndexerDevice.ready()) {
return null;
}

let lDeviceCode = deviceCode.toLowerCase();
let brands = collection[lDeviceCode];
if (brands !== void 0) {
Expand All @@ -18,16 +11,6 @@ class IndexerDevice {
return [];
}

static ready() {
return collection !== void 0;
}

static init() {
if (helper.hasFile(path)) {
collection = helper.loadYMLFile(path);
}
}

}
Comment on lines -21 to -30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer required as the collection is ready once this module is resolved.


module.exports = IndexerDevice;
24 changes: 4 additions & 20 deletions parser/device/info-device.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const ParserAbstract = require('./../abstract-parser');
const DataPacker = require('./../../lib/data-packer');
const collectionHardwareCPU = require('../../regexes/device-info/hardware-cpu');
const collectionHardwareGPU = require('../../regexes/device-info/hardware-gpu');
const collectionSoftware = require('../../regexes/device-info/software');



Expand Down Expand Up @@ -177,9 +180,6 @@ const SHORT_KEYS = {
TG: 'performance.geekbench' // int: geekbench score
};

let collectionHardwareCPU = null;
let collectionHardwareGPU = null;
let collectionSoftware = null;

/**
* Class for obtaining information on a device
Expand All @@ -193,23 +193,7 @@ class InfoDevice extends ParserAbstract {
/** @type {boolean} convert display.resolution 1080x1920 to object {width, height} */
this.resolutionConvertObject = false;
/** @type {string} fixture path to file */
this.fixtureFile = 'device-info/device.yml';
this.loadCollection();
}

loadCollection() {
super.loadCollection();
// load hardware properties
if (collectionHardwareCPU === null) {
collectionHardwareCPU = this.loadYMLFile('device-info/hardware-cpu.yml');
}
if (collectionHardwareGPU === null) {
collectionHardwareGPU = this.loadYMLFile('device-info/hardware-gpu.yml');
}
// load software properties
if (collectionSoftware === null) {
collectionSoftware = this.loadYMLFile('device-info/software.yml');
}
this.collection = require('../../regexes/device-info/device');
}

/**
Expand Down
3 changes: 1 addition & 2 deletions parser/device/mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const DeviceAbstractParser = require('./../device-abstract-parser');
class Mobile extends DeviceAbstractParser {
constructor() {
super();
this.fixtureFile = 'device/mobiles.yml';
this.loadCollection();
this.collection = require('../../regexes/device/mobiles');
}
}

Expand Down
3 changes: 1 addition & 2 deletions parser/device/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const DEVICE_TYPE = require('./../const/device-type');
class Notebook extends DeviceAbstractParser {
constructor() {
super();
this.fixtureFile = 'device/notebooks.yml';
this.loadCollection();
this.collection = require('../../regexes/device/notebooks');
}

/**
Expand Down
3 changes: 1 addition & 2 deletions parser/device/portable-media-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const DeviceAbstractParser = require('./../device-abstract-parser');
class PortableMediaPlayer extends DeviceAbstractParser {
constructor() {
super();
this.fixtureFile = 'device/portable_media_player.yml';
this.loadCollection();
this.collection = require('../../regexes/device/portable_media_player');
}
}

Expand Down
3 changes: 1 addition & 2 deletions parser/device/shell-tv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class ShellTv extends DeviceAbstractParser {
*/
constructor() {
super();
this.fixtureFile = 'device/shell_tv.yml';
this.loadCollection();
this.collection = require('../../regexes/device/shell_tv');
}

/**
Expand Down
5 changes: 0 additions & 5 deletions parser/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,6 @@ function loadYMLFile(file) {
return YAML.load(fs.readFileSync(file));
}

function hasFile(file) {
return fs.existsSync(file);
}

/**
* Remove chars for string
* @param {string} str
Expand Down Expand Up @@ -442,7 +438,6 @@ module.exports = {
attr,
revertObject,
loadYMLFile,
hasFile,
trimChars,
splitUserAgent,
matchReplace,
Expand Down
Loading
Loading