mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-11-16 15:11:50 +03:00
[ua-helpers] Fix type error
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"title": "User-Agent Helpers",
|
||||
"name": "@ua-parser-js/user-agent-helpers",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"author": "Faisal Salman <f@faisalman.com>",
|
||||
"description": "A collection of utility methods for working with user-agent",
|
||||
"main": "user-agent-helpers.js",
|
||||
"module": "user-agent-helpers.mjs",
|
||||
"scripts": {
|
||||
"test": "mocha ../../test/mocha-test-helpers"
|
||||
"test": "mocha ./test/*"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -64,4 +64,14 @@ import { unfreezeUA } from '@ua-parser-js/user-agent-helpers';
|
||||
unfreezeUA()
|
||||
.then(newUA => console.log(newUA));
|
||||
// 'Mozilla/5.0 (Windows NT 11.0; ARM) AppleWebKit/537.36 (KHTML, like Gecko) New Browser/110.1.2.3 Chromium/110.1.2.3 Safari/537.36'
|
||||
|
||||
/*
|
||||
// Alternatively:
|
||||
const ua = navigator.userAgent;
|
||||
const ch = await navigator.userAgentData.getHighEntropyValues();
|
||||
const newUA = await unfreezeUA(ua, ch);
|
||||
|
||||
// Server environment:
|
||||
const newUA = await unfreezeUA(req.headers);
|
||||
*/
|
||||
```
|
||||
56
src/user-agent-helpers/test/index.js
Normal file
56
src/user-agent-helpers/test/index.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const { isFrozenUA, unfreezeUA } = require('../user-agent-helpers');
|
||||
const { UAClientHints } = require('@ua-parser-js/ua-client-hints');
|
||||
const assert = require('assert');
|
||||
|
||||
describe('isFrozenUA()', () => {
|
||||
it('Returns whether a user agent is frozen', () => {
|
||||
|
||||
const regularWindowsUA = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.1234.56 Safari/537.36";
|
||||
const freezedWindowsUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.0.0 Safari/537.36";
|
||||
|
||||
const regularMacUA = "";
|
||||
const freezedMacUA = "";
|
||||
|
||||
const regularLinuxUA = "";
|
||||
const freezedLinuxUA = "";
|
||||
|
||||
const regularCrOSUA = "";
|
||||
const freezedCrOSUA = "";
|
||||
|
||||
const regularFuchsiaUA = "";
|
||||
const freezedFuchsiaUA = "";
|
||||
|
||||
const regularMobileUA = "Mozilla/5.0 (Linux; Android 9; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.1234.56 Mobile Safari/537.36";
|
||||
const freezedMobileUA = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.0.0 Mobile Safari/537.36";
|
||||
|
||||
const regularTabletUA = "Mozilla/5.0 (Linux; Android 9; SM-T810) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.1234.56 Safari/537.36";
|
||||
const freezedTabletUA = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.0.0 Safari/537.36";
|
||||
|
||||
assert.strictEqual(isFrozenUA(regularWindowsUA), false);
|
||||
assert.strictEqual(isFrozenUA(freezedWindowsUA), true);
|
||||
assert.strictEqual(isFrozenUA(regularMobileUA), false);
|
||||
assert.strictEqual(isFrozenUA(freezedMobileUA), true);
|
||||
assert.strictEqual(isFrozenUA(regularTabletUA), false);
|
||||
assert.strictEqual(isFrozenUA(freezedTabletUA), true);
|
||||
});
|
||||
});
|
||||
|
||||
const req = {
|
||||
headers : {
|
||||
'sec-ch-ua' : '"Chromium";v="93", "Google Chrome";v="93", " Not;A Brand";v="99"',
|
||||
'sec-ch-ua-full-version-list' : '"Chromium";v="93.0.1.2", "Google Chrome";v="93.0.1.2", " Not;A Brand";v="99.0.1.2"',
|
||||
'sec-ch-ua-arch' : '"arm"',
|
||||
'sec-ch-ua-bitness' : '"64"',
|
||||
'sec-ch-ua-mobile' : '?1',
|
||||
'sec-ch-ua-model' : '"Pixel 99"',
|
||||
'sec-ch-ua-platform' : '"Linux"',
|
||||
'sec-ch-ua-platform-version' : '"13"',
|
||||
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
|
||||
}};
|
||||
|
||||
describe('unfreezeUA()', () => {
|
||||
it('returns an unfreezed user-agent using real data from client hints HTTP headers (sec-ch-ua)', async () => {
|
||||
const unfreezed = await unfreezeUA(req.headers);
|
||||
assert.strictEqual(unfreezed, 'Mozilla/5.0 (X11; Linux arm64) AppleWebKit/537.36 (KHTML, like Gecko) Chromium/93.0.1.2 Chrome/93.0.1.2 Safari/537.36');
|
||||
});
|
||||
});
|
||||
35
src/user-agent-helpers/user-agent-helpers.d.ts
vendored
35
src/user-agent-helpers/user-agent-helpers.d.ts
vendored
@@ -1,4 +1,35 @@
|
||||
export type UABrowser = {
|
||||
brand: string | null,
|
||||
version: string | null
|
||||
};
|
||||
|
||||
export type UADataType = boolean | string | Array<UABrowser> | null;
|
||||
export type UADataField =
|
||||
'brands' |
|
||||
'mobile' |
|
||||
'platform' |
|
||||
'architecture' |
|
||||
'bitness' |
|
||||
'formFactor' |
|
||||
'fullVersionList' |
|
||||
'model' |
|
||||
'platformVersion' |
|
||||
'wow64';
|
||||
|
||||
export type HeaderType = 'sf-boolean' | 'sf-string' | 'sf-list';
|
||||
export type HeaderField =
|
||||
'sec-ch-ua-arch' |
|
||||
'sec-ch-ua-bitness' |
|
||||
'sec-ch-ua' |
|
||||
'sec-ch-ua-form-factor' |
|
||||
'sec-ch-ua-full-version-list' |
|
||||
'sec-ch-ua-mobile' |
|
||||
'sec-ch-ua-model' |
|
||||
'sec-ch-ua-platform' |
|
||||
'sec-ch-ua-platform-version' |
|
||||
'sec-ch-ua-wow64';
|
||||
|
||||
export function isFrozenUA(ua: string): boolean;
|
||||
export function unfreezeUA(): Promise<string>;
|
||||
export function unfreezeUA(ua: string, ch: ClientHintsJSHighEntropy): Promise<string>;
|
||||
export function unfreezeUA(headers: ClientHintsHTTPHeaders): Promise<string>;
|
||||
export function unfreezeUA(ua: string, ch: Record<UADataField, UADataType>): Promise<string>;
|
||||
export function unfreezeUA(headers: Record<HeaderField, HeaderType>): Promise<string>;
|
||||
Reference in New Issue
Block a user