Skip to content

Commit

Permalink
feat: create prototype detect Iphone 13 by gpu hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchezzzhak committed Jan 9, 2024
1 parent d1ed685 commit c52811e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 16 deletions.
7 changes: 6 additions & 1 deletion client-hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,17 @@ class ClientHints {
for (let key in meta) {
let value = meta[key];
let lowerCaseKey = key.toLowerCase();

if (value === void 0) {
continue;
}

switch (lowerCaseKey) {
case 'width':
case 'height':
case 'ratio':
case 'hashc':
result.meta[key] = parseFloat(value.toFixed(3));
result.meta[key] = parseFloat(value).toFixed(3);
break;
case 'gpu':
case 'hashg':
Expand Down
35 changes: 28 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const VendorFragmentParser = require(
'./parser/vendor-fragment-abstract-parser');
// other parsers
const AliasDevice = require('./parser/device/alias-device');
const DeviceHint = require('./parser/device/hints/device-hint');
const IndexerClient = require('./parser/client/indexer-client');
const IndexerDevice = require('./parser/device/indexer-device');

Expand All @@ -43,13 +44,19 @@ const VENDOR_FRAGMENT_PARSER = 'VendorFragment';
const OS_PARSER = 'Os';
const BOT_PARSER = 'Bot';

// ===========================
// static private parser init
// ===========================

const deviceHint = new DeviceHint();
const aliasDevice = new AliasDevice();
aliasDevice.setReplaceBrand(false);


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


class DeviceDetector {
/**
* @typedef DeviceDetectorOptions
Expand Down Expand Up @@ -594,7 +601,7 @@ class DeviceDetector {
/**
* parse device
* @param {string} userAgent
* @param clientHints
* @param {Object} clientHints
* @return {ResultDevice}
*/
parseDevice(userAgent, clientHints) {
Expand All @@ -618,16 +625,30 @@ class DeviceDetector {
model: '',
};

if (clientHints) {
let resultHint = deviceHint.parse(clientHints);
if (resultHint.type) {
result.type = resultHint.type;
result.brand = resultHint.brand;
result.model = resultHint.model;
if (deviceCode === '' && resultHint.code && this.deviceAliasCode) {
result.code = resultHint.code;
}
}
}

if (this.deviceAliasCode) {
result.code = deviceCode;
}

for (let name in this.deviceParserList) {
let parser = this.deviceParserList[name];
let resultMerge = parser.parse(ua, brandIndexes);
if (resultMerge) {
result = Object.assign({}, result, resultMerge);
break;
if (result && result.brand === '') {
for (let name in this.deviceParserList) {
let parser = this.deviceParserList[name];
let resultMerge = parser.parse(ua, brandIndexes);
if (resultMerge) {
result = Object.assign({}, result, resultMerge);
break;
}
}
}

Expand Down
31 changes: 28 additions & 3 deletions parser/device/hints/device-hint.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
const AbstractParser = require('../../abstract-parser');

class DeviceHint {
class DeviceHint extends AbstractParser {

constructor() {
super();
this.fixtureFile = 'device-hint.yml';
this.loadCollection();
}

parse(clientHints = {}){
if (!clientHints.meta || !clientHints.meta.hashG) {
return {};
}

let hash = clientHints.meta.hashG;
let item = this.collection[hash];

if (item === void 0) {
return {};
}

if (item && !item.devices) {
return {
code: String(item.code),
brand: String(item.brand),
type: String(item.device),
model: String(item.model),
};
}

parse(hints = {}){
return {};
}


}

module.exports = DeviceHint;
23 changes: 18 additions & 5 deletions tests/device.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const excludeFilesNames = [
'alias_devices.yml',
'clienthints-app.yml',
'clienthints.yml',
'device-hash.yml',
];
const ymlDeviceFiles = fs.readdirSync(fixtureFolder + 'devices/');
const TIMEOUT = 6000;
Expand Down Expand Up @@ -87,12 +88,18 @@ function testsFromFixtureDeviceMobile(fixture) {
}

const runTest = (fixture, result) => {

if (fixture.headers) {

let messageError = 'fixture data\n' + perryJSON(fixture);

// remove client hints for diff result
if (fixture.headers !== void 0) {
delete fixture.headers;
}

let messageError = 'fixture data\n' + perryJSON(fixture);
// remove client meta for diff result
if (fixture.meta !== void 0) {
delete fixture.meta;
}

if (result.client) {
delete result.client.short_name;
if (fixture.browser_family !== void 0) {
Expand Down Expand Up @@ -153,7 +160,9 @@ const createTestForFile = (file) => {
return this.skip();
}
let cloneFixture = Object.assign({}, fixture);
let clientHintData = clientHints.parse(cloneFixture.headers);
let headers = cloneFixture.headers;
let meta = cloneFixture.meta;
let clientHintData = clientHints.parse(headers, meta);
let result = null;

if (forAsync) {
Expand Down Expand Up @@ -241,6 +250,10 @@ describe('tests devices clienthints-app', function() {
createTestForFile('clienthints-app.yml');
});

describe('tests devices device-hash', function() {
createTestForFile('device-hash.yml');
});

describe('tests devices clienthints', function() {
createTestForFile('clienthints.yml');
});
File renamed without changes.

0 comments on commit c52811e

Please sign in to comment.