Skip to content

Commit

Permalink
chore: set types for clienthints
Browse files Browse the repository at this point in the history
feat: skip parse device is UserAgent ClientHints
  • Loading branch information
sanchezzzhak committed Sep 13, 2024
1 parent f397a09 commit b9e8973
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 61 deletions.
41 changes: 40 additions & 1 deletion client-hints.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,46 @@ export default class ClientHints {
static getHeaderClientHints(): JSONObject;
static isSupport(headers: JSONObject): boolean;

parse(hints: JSONObject, meta: JSONObject): JSONObject;
parse(hints: JSONObject, meta: JSONObject): ResultClientHints;
}

export interface ResultMetaClientHints {
width?: string;
height?: string;
gpu?: string;
gamut?: string;
ram?: string;
colorDepth?: string;
cpuCores?: number;
}

export interface ResultPrefersClientHints {
colorScheme: string;
}

export interface ResultClientPropClientHints {
brands: string[];
version: string;
}

export interface ResultOsPropClientHints {
name: string;
platform: string;
bitness: string;
version: string;
}

export interface ResultDevicePropClientHints {
model: string;
}

export interface ResultClientHints {
upgradeHeader: boolean;
meta?: ResultMetaClientHints;
prefers?: ResultPrefersClientHints;
os: ResultOsPropClientHints;
client: ResultClientPropClientHints;
device: ResultDevicePropClientHints;
}

export type JSONValue =
Expand Down
49 changes: 35 additions & 14 deletions client-hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const CH_UA_ARCH = 'sec-ch-ua-arch';
const CH_UA = 'sec-ch-ua';
const CH_BITNESS = 'sec-ch-ua-bitness';
const CH_UA_PREFERS_COLOR_SCHEME = 'sec-ch-prefers-color-scheme';
const CH_UA_FORM_FACTORS = 'sec-ch-ua-form-factors';

/*
All combinations
Expand Down Expand Up @@ -67,10 +68,10 @@ class ClientHints {
* @returns {{'accept-ch': ''}}
* @example
* ```js
const hintHeaders = ClientHints.getHeaderClientHints();
for (let name in hintHeaders) {
res.setHeader(name, hintHeaders[headerName]);
}
* const hintHeaders = ClientHints.getHeaderClientHints();
* for (let name in hintHeaders) {
* res.setHeader(name, hintHeaders[headerName]);
* }
* ```
*/
static getHeaderClientHints() {
Expand All @@ -83,7 +84,8 @@ class ClientHints {
CH_UA_MODEL,
CH_UA_ARCH,
CH_BITNESS,
CH_UA_PREFERS_COLOR_SCHEME
CH_UA_PREFERS_COLOR_SCHEME,
CH_UA_FORM_FACTORS
].join(', ')
};
}
Expand All @@ -101,8 +103,8 @@ class ClientHints {
}

/**
* @param {{}} hints
* @param {{}} result
* @param {JSONObject|{}} hints
* @param {ResultClientHints|JSONObject} result
* @private
*/
__parseHints(hints, result) {
Expand Down Expand Up @@ -188,13 +190,23 @@ class ClientHints {
result.app = '';
}
break;
case 'formfactors':
result.formFactors = value.map(val => val.toLowerCase());
break;
case 'http-sec-ch-ua-form-factors':
case 'sec-ch-ua-form-factors':
let matchFactors = /"([a-z]+)"/i.exec(value.toLowerCase());
if (matchFactors && matchFactors[1]) {
result.formFactors = matchFactors[1];
}
break;
}
}
}

/**
* @param {{}} meta
* @param {{}} result
* @param {JSONObject|{}} meta
* @param {ResultClientHints} result
* @private
*/
__parseMeta(meta, result) {
Expand Down Expand Up @@ -227,13 +239,14 @@ class ClientHints {
}

/**
* @param {{}} hints - headers or client-hints params
* @param {{}} meta - client custom js metric params
* @return {ResultClientHints|JSONObject|{}}
* @private
*/
parse(hints, meta = {}) {
let result = {
__blank() {
return {
upgradeHeader: false,
isMobile: false,
formFactors: [],
meta: {
width: '',
height: '',
Expand All @@ -248,9 +261,17 @@ class ClientHints {
client: { brands: [], version: '' },
device: { model: '' }
};
}

/**
* @param {JSONObject|{}} hints - headers or client-hints params
* @param {JSONObject|{}} meta - client custom js metric params
* @return {ResultClientHints}
*/
parse(hints, meta = {}) {
let result = this.__blank()
this.__parseHints(hints, result);
this.__parseMeta(meta, result);

return result;
}

Expand Down
26 changes: 13 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { JSONObject } from './client-hints';
import { JSONObject, ResultClientHints } from './client-hints';

export default class DeviceDetector {
/**
* @param {DeviceDetectorOptions} options
**/
constructor(options?: DeviceDetectorOptions);

detect: (userAgent: string, clientHints?: any) => DetectResult;
detectAsync: (userAgent: string, clientHints?: any) => Promise<DetectResult>;
parseBot: (userAgent: string, clientHints?: any) => ResultBot;
parseOs: (userAgent: string, clientHints?: any) => ResultOs;
parseClient: (userAgent: string, clientHints?: any) => ResultClient;
parseDevice: (userAgent: string, clientHints?: any) => ResultDevice;
detect: (userAgent: string, clientHints?: ResultClientHints|JSONObject) => DetectResult;
detectAsync: (userAgent: string, clientHints?: ResultClientHints|JSONObject) => Promise<DetectResult>;
parseBot: (userAgent: string, clientHints?: ResultClientHints|JSONObject) => ResultBot;
parseOs: (userAgent: string, clientHints?: ResultClientHints|JSONObject) => ResultOs;
parseClient: (userAgent: string, clientHints?: ResultClientHints|JSONObject) => ResultClient;
parseDevice: (userAgent: string, clientHints?: ResultClientHints|JSONObject) => ResultDevice;
parseDeviceType: (
userAgent: string,
os?: ResultOs,
client?: ResultClient,
deviceData?: ResultDevice,
clientHints?: any
clientHints?: ResultClientHints|JSONObject
) => DeviceType;
parseVendor: (userAgent: string) => ResultVendor;
parseVendor: (userAgent: string) => ResultVendor|null;
parseDeviceCode: (userAgent: string) => ResultDeviceCode;
set skipBotDetection(arg: boolean);
get skipBotDetection(): boolean;
Expand All @@ -29,7 +29,7 @@ export default class DeviceDetector {
get clientVersionTruncate(): any;
set clientIndexes(arg: boolean);
get clientIndexes(): boolean;
get maxUserAgentSize(): null|number;
get maxUserAgentSize(): number|null;
set maxUserAgentSize(size: number);
setOsVersionTruncate(value: any): void;
setClientVersionTruncate(value: any): void;
Expand Down Expand Up @@ -163,10 +163,10 @@ export default class DeviceDetector {
/**
* restore original userAgent from clientHints object
* @param {string} userAgent
* @param {JSONObject} clientHints
* @param {ResultClientHints|JSONObject} clientHints
* @return string
*/
restoreUserAgentFromClientHints(userAgent: string, clientHints?: JSONObject): string;
restoreUserAgentFromClientHints(userAgent: string, clientHints?: JSONObject|ResultClientHints): string;
}

export interface DeviceDetectorOptions {
Expand Down Expand Up @@ -225,7 +225,7 @@ export interface ResultDevice {
}

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

Expand Down
Loading

0 comments on commit b9e8973

Please sign in to comment.