Skip to content

Commit cec7877

Browse files
refactor(internet): remove weigths in userAgent (#1761)
1 parent ac1a3de commit cec7877

File tree

2 files changed

+22
-56
lines changed

2 files changed

+22
-56
lines changed

src/modules/internet/user-agent.ts

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,6 @@ type Browser = 'chrome' | 'iexplorer' | 'firefox' | 'safari' | 'opera';
5454
* @param faker An existing faker instance.
5555
*/
5656
export function generate(faker: Faker): string {
57-
const weightedKeyFromObject = <T extends Record<string, number>>(
58-
obj: T
59-
): keyof T => {
60-
//returns a random key from the passed object; keys are weighted by the decimal probability in their value
61-
const rand = faker.number.int(100) / 100;
62-
let min = 0;
63-
let max = 0;
64-
let return_val: string;
65-
66-
for (const key in obj) {
67-
if (Object.prototype.hasOwnProperty.call(obj, key)) {
68-
max = obj[key] + min;
69-
return_val = key;
70-
if (rand >= min && rand <= max) {
71-
break;
72-
}
73-
74-
min = min + obj[key];
75-
}
76-
}
77-
78-
return return_val;
79-
};
80-
8157
const randomLang = (): string =>
8258
faker.helpers.arrayElement([
8359
'AB',
@@ -179,39 +155,29 @@ export function generate(faker: Faker): string {
179155
]);
180156

181157
const randomBrowserAndOS = (): [Browser, OS] => {
182-
const browser: Browser = weightedKeyFromObject({
183-
chrome: 0.45132810566,
184-
iexplorer: 0.27477061836,
185-
firefox: 0.19384170608,
186-
safari: 0.06186781118,
187-
opera: 0.01574236955,
188-
});
189-
const os: OS = weightedKeyFromObject(
190-
{
191-
chrome: { win: 0.89, mac: 0.09, lin: 0.02 },
192-
firefox: { win: 0.83, mac: 0.16, lin: 0.01 },
193-
opera: { win: 0.91, mac: 0.03, lin: 0.06 },
194-
safari: { win: 0.04, mac: 0.96 },
195-
iexplorer: { win: 1 },
196-
}[browser]
197-
);
158+
const browserToOsMap = {
159+
chrome: ['win', 'mac', 'lin'],
160+
firefox: ['win', 'mac', 'lin'],
161+
opera: ['win', 'mac', 'lin'],
162+
safari: ['win', 'mac'],
163+
iexplorer: ['win'],
164+
} satisfies Record<Browser, OS[]>;
165+
const browser: Browser = faker.helpers.objectKey(browserToOsMap);
166+
const os: OS = faker.helpers.arrayElement(browserToOsMap[browser]);
198167

199168
return [browser, os];
200169
};
201170

202-
const randomProc = (arch: OS): string => {
203-
const procs = {
204-
lin: ['i686', 'x86_64'],
205-
mac: { Intel: 0.48, PPC: 0.01, 'U; Intel': 0.48, 'U; PPC': 0.01 },
206-
win: ['', 'WOW64', 'Win64; x64'],
207-
};
208-
const archValue = procs[arch];
209-
const proc = Array.isArray(archValue)
210-
? faker.helpers.arrayElement(archValue)
211-
: weightedKeyFromObject(archValue);
212-
213-
return proc;
214-
};
171+
const randomProc = (arch: OS): string =>
172+
faker.helpers.arrayElement(
173+
(
174+
{
175+
lin: ['i686', 'x86_64'],
176+
mac: ['Intel', 'PPC', 'U; Intel', 'U; PPC'],
177+
win: ['', 'WOW64', 'Win64; x64'],
178+
} satisfies Record<OS, string[]>
179+
)[arch]
180+
);
215181

216182
const randomRevision = (dots: number): string => {
217183
let return_val = '';

test/__snapshots__/internet.spec.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ exports[`internet > 42 > url > with slash appended 1`] = `"https://hasty-sherbet
6868

6969
exports[`internet > 42 > url > without slash appended and with http protocol 1`] = `"http://hasty-sherbet.org"`;
7070

71-
exports[`internet > 42 > userAgent 1`] = `"Mozilla/5.0 (Windows; U; Windows NT 6.2) AppleWebKit/538.0.2 (KHTML, like Gecko) Chrome/29.0.815.0 Safari/538.0.2"`;
71+
exports[`internet > 42 > userAgent 1`] = `"Mozilla/5.0 (X11; Linux x86_64; rv:15.1) Gecko/20100101 Firefox/15.1.7"`;
7272

7373
exports[`internet > 42 > userName > noArgs 1`] = `"Garnet.Schinner73"`;
7474

@@ -148,7 +148,7 @@ exports[`internet > 1211 > url > with slash appended 1`] = `"https://vicious-inf
148148

149149
exports[`internet > 1211 > url > without slash appended and with http protocol 1`] = `"http://vicious-infrastructure.org"`;
150150

151-
exports[`internet > 1211 > userAgent 1`] = `"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6 rv:3.0; PT) AppleWebKit/538.2.0 (KHTML, like Gecko) Version/4.1.0 Safari/538.2.0"`;
151+
exports[`internet > 1211 > userAgent 1`] = `"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.3; Trident/4.0)"`;
152152

153153
exports[`internet > 1211 > userName > noArgs 1`] = `"Tito_Koelpin22"`;
154154

@@ -228,7 +228,7 @@ exports[`internet > 1337 > url > with slash appended 1`] = `"https://fair-mile.c
228228

229229
exports[`internet > 1337 > url > without slash appended and with http protocol 1`] = `"http://fair-mile.com"`;
230230

231-
exports[`internet > 1337 > userAgent 1`] = `"Mozilla/5.0 (Windows; U; Windows NT 6.1) AppleWebKit/532.0.0 (KHTML, like Gecko) Chrome/13.0.832.0 Safari/532.0.0"`;
231+
exports[`internet > 1337 > userAgent 1`] = `"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.7.0; rv:6.2) Gecko/20100101 Firefox/6.2.2"`;
232232

233233
exports[`internet > 1337 > userName > noArgs 1`] = `"Devyn21"`;
234234

0 commit comments

Comments
 (0)