Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezzzhak committed Sep 17, 2024
1 parent e903753 commit 8b205b6
Show file tree
Hide file tree
Showing 33 changed files with 290 additions and 265 deletions.
2 changes: 1 addition & 1 deletion src/client-hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface ResultClientHints {

/* tslint:disable */

export class ClientHints {
export default class ClientHints {

/**
* @returns {{'accept-ch': ''}}
Expand Down
198 changes: 39 additions & 159 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
import { JSONObject, ResultClientHints } from './client-hints';

// os parsers
import { OsParser } from './parser/os-parser';
import OsParser from './parser/os-parser';

// client parsers
import { FeedReaderParser } from './parser/client/feed-reader';
import { MobileAppParser } from './parser/client/mobile-app';
import { MediaPlayerParser } from './parser/client/media-player';
import { PimParser } from './parser/client/pim';
import { BrowserParser } from './parser/client/browser';
import { LibraryParser } from './parser/client/library';
import FeedReaderParser from './parser/client/feed-reader';
import MobileAppParser from './parser/client/mobile-app';
import MediaPlayerParser from './parser/client/media-player';
import PimParser from './parser/client/pim';
import BrowserParser from './parser/client/browser';
import LibraryParser from './parser/client/library';

// device parsers
import { HbbTvParser } from './parser/device/hbb-tv';
import { ShellTvParser } from './parser/device/shell-tv';
import { NotebookParser } from './parser/device/notebook';
import { ConsoleParser } from './parser/device/console';
import { CarParser } from './parser/device/car';
import { CameraParser } from './parser/device/camera';
import { PortableMediaPlayerParser } from './parser/device/portable-media-player';
import { MobileParser } from './parser/device/mobile';
import HbbTvParser from './parser/device/hbb-tv';
import ShellTvParser from './parser/device/shell-tv';
import NotebookParser from './parser/device/notebook';
import ConsoleParser from './parser/device/console';
import CarParser from './parser/device/car';
import CameraParser from './parser/device/camera';
import PortableMediaPlayerParser from './parser/device/portable-media-player';
import MobileParser from './parser/device/mobile';

// other parsers
import { AliasDevice } from './parser/device/alias-device';
import { IndexerClient } from './parser/client/indexer-client';
import { IndexerDevice } from './parser/device/indexer-device';
import { InfoDevice } from './parser/device/info-device';
import { VendorFragmentParser } from './parser/vendor-fragment-parser';
import { BotParser } from './parser/bot-parser';
import AliasDevice from './parser/device/alias-device';
import IndexerClient from './parser/client/indexer-client';
import IndexerDevice from './parser/device/indexer-device';
import InfoDevice from './parser/device/info-device';
import VendorFragmentParser from './parser/vendor-fragment-parser';
import BotParser from './parser/bot-parser';

// check parsers
import { DeviceTrusted } from './parser/device/device-trusted';
import DeviceTrusted from './parser/device/device-trusted';

// constants, lists, parser names
const VENDOR_FRAGMENT_PARSER = 'VendorFragment';
Expand All @@ -48,7 +48,15 @@ import CLIENT_PARSER_LIST from './parser/const/client-parser';
import MOBILE_BROWSER_LIST from './parser/client/browser-short-mobile';
// helpers
import * as helper from './parser/helper';
import * as module from 'module';

import {
DetectResult,
DeviceDetectorOptions, DeviceType, ResultBot,
ResultClient,
ResultDevice,
ResultDeviceCode, ResultOs,
ResultVendor
} from './types';

const { hasUserAgentClientHintsFragment, hasDeviceModelByClientHints, attr } = helper;

Expand All @@ -64,135 +72,7 @@ IndexerDevice.init();
IndexerClient.init();


export interface DeviceDetectorOptions {
skipBotDetection?: boolean;
osVersionTruncate?: number | null;
clientVersionTruncate?: number | null;
maxUserAgentSize?: number | null;
clientIndexes?: boolean;
deviceIndexes?: boolean;
deviceAliasCode?: boolean;
deviceInfo?: boolean;
deviceTrusted?: boolean;
}

export interface ResultDeviceCode {
name: string;
}

export interface ResultVendor {
id: string;
name: string;
}

export interface DetectResult {
os: ResultOs;
client: ResultClient;
device: ResultDevice;
}

export interface ResultOs {
name: string;
short_name: string;
version: string;
platform: string;
family: string;
}

export interface ResultClient {
type: string;
name: string;
short_name?: string;
version: string;
engine?: string;
engine_version?: string;
family?: string;
}

export interface ResultDevice {
id: string;
type: string;
brand: string;
model: string;
code?: string;
trusted?: boolean | null;
info?: ResultDeviceInfo | null;
regex?: any
}

export interface ResultAliasDevice {
name: string;
}

export interface DeviceType {
id?: string;
type: string;
}

export interface ResultBot {
name: string;
producer: any;
category: string;
url: string;
}

export interface ResultDeviceInfoDisplay {
size: string;
resolution?: string | ResultDeviceInfoResolution;
ratio?: string;
ppi?: string;
}

export interface ResultDeviceInfoResolution {
width: string;
height: string;
}

export interface ResultDeviceInfoPerformance {
antutu?: number;
geekbench?: number;
}

export interface ResultDeviceInfoHardwareGPU {
name: string;
clock_rate?: number;
}

export interface ResultDeviceInfoHardware {
ram: number;
cpu_id?: number;
cpu?: ResultDeviceInfoHardwareCPU;
gpu?: ResultDeviceInfoHardwareGPU;
}

export interface ResultDeviceInfoSize {
width: string;
height: string;
thickness: string;
}

export interface ResultDeviceInfoHardwareCPU {
name: string;
type: string;
cores?: number;
clock_rate?: number;
process?: string;
gpu_id?: number;
}

export interface ResultDeviceInfo {
display?: ResultDeviceInfoDisplay;
sim?: number | null;
size?: string | ResultDeviceInfoSize | null;
weight?: string | null;
release?: string | null;
os?: string | null;
hardware?: ResultDeviceInfoHardware | null;
performance?: ResultDeviceInfoPerformance | null;
}


export class DeviceDetector {
export default class DeviceDetector {

#vendorParserList = {};
#osParserList = {};
Expand Down Expand Up @@ -649,15 +529,15 @@ export class DeviceDetector {

userAgent = this.prepareUserAgent(userAgent);

let osName = attr(osData, 'name', '');
let osFamily = attr(osData, 'family', '');
let osVersion = attr(osData, 'version', '');
const osName = attr(osData, 'name', '');
const osFamily = attr(osData, 'family', '');
const osVersion = attr(osData, 'version', '');

let clientType = attr(clientData, 'type', '');
let clientShortName = attr(clientData, 'short_name', '');
const clientType = attr(clientData, 'type', '');
const clientShortName = attr(clientData, 'short_name', '');

let clientName = attr(clientData, 'name', '');
let clientFamily = attr(clientData, 'family', '');
const clientName = attr(clientData, 'name', '');
const clientFamily = attr(clientData, 'family', '');
let deviceType = attr(deviceData, 'type', '');

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/abstract-parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as helper from './helper';

const BASE_REGEXES_DIR = __dirname + '/../regexes/';
const BASE_REGEXES_DIR = helper.getBaseRegexDir()

/**
* @param {string} result
Expand Down
4 changes: 2 additions & 2 deletions src/parser/bot-parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AbstractParser from './abstract-parser';
import { ResultBot } from '../index';
import { ResultBot } from '../types';

export class BotParser extends AbstractParser {
export default class BotParser extends AbstractParser {
constructor() {
super();
this.fixtureFile = 'bots.yml';
Expand Down
14 changes: 6 additions & 8 deletions src/parser/client-abstract-parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { IndexerClient } from './client/indexer-client';

import IndexerClient from './client/indexer-client';
import AbstractParser from './abstract-parser';
import { JSONObject, ResultClientHints } from '../client-hints';
import { ResultClient } from '../index';
import { ResultClient } from '../types';

export class ClientAbstractParser extends AbstractParser {
export default class ClientAbstractParser extends AbstractParser {

#clientIndexes = false;

Expand All @@ -29,7 +28,7 @@ export class ClientAbstractParser extends AbstractParser {
* @returns {ResultClient|null}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
parse(userAgent: string, clientHintData: ResultClientHints|JSONObject): ResultClient | null {
parse(userAgent: string, clientHintData: ResultClientHints| JSONObject): ResultClient | JSONObject | null {
if (!userAgent) {
return null;
}
Expand All @@ -56,7 +55,7 @@ export class ClientAbstractParser extends AbstractParser {
/**
* @param {string} userAgent
*/
parseUserAgentByPositions(userAgent: string): ResultClient | null {
parseUserAgentByPositions(userAgent: string): ResultClient | JSONObject | null {
const positions = IndexerClient.findClientRegexPositionsForUserAgent(userAgent, String(this.type));
if (positions !== null && positions.length) {
for (let i = 0, l = positions.length; i < l; i++) {
Expand All @@ -76,7 +75,7 @@ export class ClientAbstractParser extends AbstractParser {
* @param {Number} position
* @returns {{ResultClient|null}
*/
parseUserAgentByPosition(userAgent: string, position = 0): ResultClient | null {
parseUserAgentByPosition(userAgent: string, position = 0): ResultClient | JSONObject | null {
const item = this.collection[position];

if (item === void 0) {
Expand All @@ -98,4 +97,3 @@ export class ClientAbstractParser extends AbstractParser {
}
}

module.exports = ClientAbstractParser;
24 changes: 12 additions & 12 deletions src/parser/client/browser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ClientAbstractParser } from '../client-abstract-parser';
import ClientAbstractParser from '../client-abstract-parser';
import CLIENT_TYPE from './../const/client-type';
import BROWSER_FAMILIES from './browser-families';
import { ArrayPath } from '../../array-path';
import * as helper from './../helper';
import { BrowserHints } from './hints/browser-hints';
import __BROWSER_SHORT from './browser-short';
import { ResultClient } from '../../index';
import { ResultClient } from '../../types';

const BROWSER_SHORT = helper.revertObject(__BROWSER_SHORT);

Expand Down Expand Up @@ -35,7 +35,7 @@ const compareBrandForClientHints = (brand) => {
return brand;
};

export class BrowserParser extends ClientAbstractParser {
export default class BrowserParser extends ClientAbstractParser {

public engine_collection: any[];

Expand Down Expand Up @@ -331,12 +331,12 @@ export class BrowserParser extends ClientAbstractParser {
*/
buildName(name) {
let result = name;
let normalName = this.getCollectionBrowsers()[name];
const normalName = this.getCollectionBrowsers()[name];
if (normalName === void 0) {
let lname = name.toLowerCase();
let browsers = this.getAvailableBrowsers();
const lowerName = name.toLowerCase();
const browsers = this.getAvailableBrowsers();
for (let i = 0, l = browsers.length; i < l; i++) {
if (lname === browsers[i].toLowerCase()) {
if (lowerName === browsers[i].toLowerCase()) {
result = browsers[i];
break;
}
Expand All @@ -353,7 +353,7 @@ export class BrowserParser extends ClientAbstractParser {
*/
buildShortName(name) {
const UNKNOWN = 'UNK';
let result = this.getCollectionBrowsers()[name];
const result = this.getCollectionBrowsers()[name];
if (result !== void 0) {
return result;
}
Expand Down Expand Up @@ -392,7 +392,7 @@ export class BrowserParser extends ClientAbstractParser {
result = engine.default;
}
if (engine.hasOwnProperty('versions')) {
let versions = Object.keys(engine.versions).sort(helper.versionCompare);
const versions = Object.keys(engine.versions).sort(helper.versionCompare);
for (let i = 0, l = versions.length; i < l; i++) {
if (
browserVersion !== '' &&
Expand All @@ -414,9 +414,9 @@ export class BrowserParser extends ClientAbstractParser {
parseEngine(userAgent) {
let result = '';
for (let i = 0, l = this.engine_collection.length; i < l; i++) {
let item = this.engine_collection[i];
let regex = this.getBaseRegExp(item.regex);
let match = regex.exec(userAgent);
const item = this.engine_collection[i];
const regex = this.getBaseRegExp(item.regex);
const match = regex.exec(userAgent);
if (match !== null) {
result = item.name;
break;
Expand Down
Loading

0 comments on commit 8b205b6

Please sign in to comment.