@denniskrol/device-profiles 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +145 -0
- package/dist/deviceprofiles.json +1 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +65 -0
- package/package.json +47 -0
- package/src/deviceprofiles.json +1 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface StorageQuotaInfo {
|
|
2
|
+
quota: number;
|
|
3
|
+
usage: number;
|
|
4
|
+
available: number;
|
|
5
|
+
}
|
|
6
|
+
export interface DeviceProfile {
|
|
7
|
+
userAgent: string;
|
|
8
|
+
platform?: string | null;
|
|
9
|
+
deviceMemory?: number | null;
|
|
10
|
+
hardwareConcurrency?: number | null;
|
|
11
|
+
vendor?: string | null;
|
|
12
|
+
screenHeight?: number | null;
|
|
13
|
+
screenWidth?: number | null;
|
|
14
|
+
viewportHeight?: number | null;
|
|
15
|
+
viewportWidth?: number | null;
|
|
16
|
+
devicePixelRatio?: number | null;
|
|
17
|
+
webglRenderer?: string | null;
|
|
18
|
+
webglVendor?: string | null;
|
|
19
|
+
webGpuArchitecture?: string | null;
|
|
20
|
+
webGpuVendor?: string | null;
|
|
21
|
+
storageQuota?: StorageQuotaInfo | null;
|
|
22
|
+
jsHeapSizeLimit?: number | null;
|
|
23
|
+
fonts?: string[];
|
|
24
|
+
userAgentData?: any;
|
|
25
|
+
weight?: number;
|
|
26
|
+
deviceType?: string;
|
|
27
|
+
browser?: string;
|
|
28
|
+
osName?: string;
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
}
|
|
31
|
+
export type DeviceProfileFilter = Partial<Record<keyof DeviceProfile, any>> & {};
|
|
32
|
+
export declare class DeviceProfiles {
|
|
33
|
+
[key: string]: any;
|
|
34
|
+
userAgent: string;
|
|
35
|
+
private _profile;
|
|
36
|
+
constructor(filter?: DeviceProfileFilter);
|
|
37
|
+
static all(): DeviceProfile[];
|
|
38
|
+
static random(filter?: DeviceProfileFilter): DeviceProfile;
|
|
39
|
+
get profile(): DeviceProfile;
|
|
40
|
+
}
|
|
41
|
+
export default DeviceProfiles;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DeviceProfiles = void 0;
|
|
7
|
+
/* Device Profiles library */
|
|
8
|
+
const deviceprofiles_json_1 = __importDefault(require("./deviceprofiles.json"));
|
|
9
|
+
const profiles = deviceprofiles_json_1.default;
|
|
10
|
+
function matchesFilter(profile, filter) {
|
|
11
|
+
for (const [key, expected] of Object.entries(filter)) {
|
|
12
|
+
const actual = profile[key];
|
|
13
|
+
if (Array.isArray(expected)) {
|
|
14
|
+
// inclusion list
|
|
15
|
+
if (!expected.includes(actual))
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
else if (expected instanceof RegExp) {
|
|
19
|
+
if (typeof actual !== 'string' || !expected.test(actual))
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
else if (expected !== undefined) {
|
|
23
|
+
if (actual !== expected)
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
function pickWeightedRandom(items) {
|
|
30
|
+
var _a;
|
|
31
|
+
if (!items.length) {
|
|
32
|
+
throw new Error('No device profiles available for the given filter.');
|
|
33
|
+
}
|
|
34
|
+
const totalWeight = items.reduce((sum, p) => { var _a; return sum + ((_a = p.weight) !== null && _a !== void 0 ? _a : 1); }, 0);
|
|
35
|
+
let threshold = Math.random() * totalWeight;
|
|
36
|
+
for (const p of items) {
|
|
37
|
+
threshold -= ((_a = p.weight) !== null && _a !== void 0 ? _a : 1);
|
|
38
|
+
if (threshold <= 0)
|
|
39
|
+
return p;
|
|
40
|
+
}
|
|
41
|
+
// Fallback (floating point) return last
|
|
42
|
+
return items[items.length - 1];
|
|
43
|
+
}
|
|
44
|
+
class DeviceProfiles {
|
|
45
|
+
constructor(filter = {}) {
|
|
46
|
+
const filtered = Object.keys(filter).length
|
|
47
|
+
? profiles.filter(p => matchesFilter(p, filter))
|
|
48
|
+
: profiles;
|
|
49
|
+
this._profile = pickWeightedRandom(filtered);
|
|
50
|
+
this.userAgent = this._profile.userAgent; // explicit assignment
|
|
51
|
+
Object.assign(this, this._profile);
|
|
52
|
+
}
|
|
53
|
+
static all() {
|
|
54
|
+
return [...profiles];
|
|
55
|
+
}
|
|
56
|
+
static random(filter = {}) {
|
|
57
|
+
const filtered = Object.keys(filter).length
|
|
58
|
+
? profiles.filter(p => matchesFilter(p, filter))
|
|
59
|
+
: profiles;
|
|
60
|
+
return pickWeightedRandom(filtered);
|
|
61
|
+
}
|
|
62
|
+
get profile() { return this._profile; }
|
|
63
|
+
}
|
|
64
|
+
exports.DeviceProfiles = DeviceProfiles;
|
|
65
|
+
exports.default = DeviceProfiles;
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@denniskrol/device-profiles",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Daily updated browser device profiles",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"device-profiles",
|
|
7
|
+
"browser",
|
|
8
|
+
"user-agent"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/denniskrol/device-profiles#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/denniskrol/device-profiles/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/denniskrol/device-profiles.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "Unlicense",
|
|
19
|
+
"author": "Dennis Krol",
|
|
20
|
+
"type": "commonjs",
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"require": "./dist/index.js",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./deviceprofiles.json": "./src/deviceprofiles.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src/deviceprofiles.json"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p tsconfig.json",
|
|
37
|
+
"clean": "rimraf dist",
|
|
38
|
+
"prepare": "npm run build",
|
|
39
|
+
"test": "vitest run"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^20.19.25",
|
|
43
|
+
"rimraf": "^5.0.5",
|
|
44
|
+
"typescript": "^5.5.0",
|
|
45
|
+
"vitest": "^2.1.9"
|
|
46
|
+
}
|
|
47
|
+
}
|