Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

header-generator

Package Overview
Dependencies
Maintainers
2
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

header-generator - npm Package Compare versions

Comparing version 1.1.3 to 1.2.0-beta.0

src/constants.js

3

package.json
{
"name": "header-generator",
"version": "1.1.3",
"version": "1.2.0-beta.0",
"description": "NodeJs package for generating browser-like headers.",

@@ -17,2 +17,3 @@ "author": {

"dependencies": {
"browserslist": "^4.19.1",
"generative-bayesian-network": "0.1.0-beta.1",

@@ -19,0 +20,0 @@ "ow": "^0.23.0"

@@ -137,2 +137,3 @@ # Header generator

| browsers | <code>Array.&lt;(BrowserSpecification\|string)&gt;</code> | List of BrowserSpecifications to generate the headers for, or one of `chrome`, `firefox` and `safari`. |
| browserListQuery | <code>string</code> | Browser generation query based on the real world data. For more info see the [query docs](https://github.com/browserslist/browserslist#full-list). If `browserListQuery` is passed the `browsers` array is ignored. |
| operatingSystems | <code>Array.&lt;string&gt;</code> | List of operating systems to generate the headers for. The options are `windows`, `macos`, `linux`, `android` and `ios`. |

@@ -139,0 +140,0 @@ | devices | <code>Array.&lt;string&gt;</code> | List of devices to generate the headers for. Options are `desktop` and `mobile`. |

@@ -14,3 +14,4 @@ const { BayesianNetwork } = require('generative-bayesian-network');

const uniqueBrowserStrings = require('./data_files/browser-helper-file.json');
const { getBrowser, getUserAgent } = require('./utils');
const { getBrowser, getUserAgent, getBrowsersFromQuery } = require('./utils');
const { SUPPORTED_BROWSERS } = require('./constants');

@@ -108,2 +109,3 @@ const uniqueBrowsers = [];

httpVersion: ow.optional.string,
browserListQuery: ow.optional.string,
};

@@ -135,2 +137,3 @@

*/
/**

@@ -140,2 +143,5 @@ * @typedef HeaderGeneratorOptions

* or one of `chrome`, `firefox` and `safari`.
* @param {string} browserListQuery - Browser generation query based on the real world data.
* For more info see the [query docs](https://github.com/browserslist/browserslist#full-list).
* If `browserListQuery` is passed the `browsers` array is ignored.
* @param {Array<string>} operatingSystems - List of operating systems to generate the headers for.

@@ -160,26 +166,19 @@ * The options are `windows`, `macos`, `linux`, `android` and `ios`.

ow(options, 'HeaderGeneratorOptions', ow.object.exactShape(headerGeneratorOptionsShape));
this.defaultOptions = JSON.parse(JSON.stringify(options));
// Use a default setup when the necessary values are not provided
if (!this.defaultOptions.locales) {
this.defaultOptions.locales = ['en-US'];
}
if (!this.defaultOptions.httpVersion) {
this.defaultOptions.httpVersion = '2';
}
if (!this.defaultOptions.browsers) {
this.defaultOptions.browsers = [
{ name: 'chrome' },
{ name: 'firefox' },
{ name: 'safari' },
];
}
if (!this.defaultOptions.operatingSystems) {
this.defaultOptions.operatingSystems = [
'windows',
'macos',
'linux',
'android',
'ios',
];
}
const {
browsers = SUPPORTED_BROWSERS,
operatingSystems = ['windows', 'macos', 'linux', 'android', 'ios'],
devices = ['desktop'],
locales = ['en-US'],
httpVersion = '2',
browserListQuery,
} = options;
this.browserListQuery = browserListQuery;
this.globalOptions = {
browsers: this._prepareBrowsersConfig(browsers, browserListQuery, httpVersion),
operatingSystems,
devices,
locales,
httpVersion,
};

@@ -198,14 +197,5 @@ this.inputGeneratorNetwork = new BayesianNetwork(inputNetworkDefinition);

ow(options, 'HeaderGeneratorOptions', ow.object.exactShape(headerGeneratorOptionsShape));
const headerOptions = JSON.parse(JSON.stringify({ ...this.defaultOptions, ...options }));
headerOptions.browsers = headerOptions.browsers.map((browserObject) => {
if (typeof browserObject === 'string') {
browserObject = { name: browserObject };
}
const headerOptions = JSON.parse(JSON.stringify({ ...this.globalOptions, ...options }));
headerOptions.browsers = this._prepareBrowsersConfig(headerOptions.browsers, headerOptions.browserListQuery, headerOptions.httpVersion);
if (!browserObject.httpVersion) {
browserObject.httpVersion = headerOptions.httpVersion;
}
return browserObject;
});
const possibleAttributeValues = {};

@@ -338,4 +328,21 @@

}
_prepareBrowsersConfig(browsers, browserListQuery, httpVersion) {
let finalBrowsers = browsers;
if (browserListQuery) {
finalBrowsers = getBrowsersFromQuery(browserListQuery);
}
return finalBrowsers.map((browser) => {
if (typeof browser === 'string') {
return { name: browser, httpVersion };
}
browser.httpVersion = httpVersion;
return browser;
});
}
}
module.exports = HeaderGenerator;

@@ -0,1 +1,4 @@

const browsersList = require('browserslist');
const { SUPPORTED_BROWSERS } = require('./constants');
const getUserAgent = (headers) => {

@@ -30,5 +33,55 @@ let userAgent;

const getBrowsersWithVersions = (browserList) => {
const browsersWithVersions = {};
for (const browserDefinition of browserList) {
const [browser, version] = browserDefinition.split(' ');
if (!SUPPORTED_BROWSERS.includes(browser)) {
// eslint-disable-next-line no-continue
continue;
}
if (browsersWithVersions[browser]) {
browsersWithVersions[browser].push(version);
} else {
browsersWithVersions[browser] = [version];
}
}
return browsersWithVersions;
};
const getOptimizedVersionDistribution = (browsersWithVersions) => {
const finalOptimizedBrowsers = [];
Object.entries(browsersWithVersions).forEach(([browser, versions]) => {
const sortedVersions = versions.sort((a, b) => a - b);
let lowestVersionSoFar = sortedVersions[0];
sortedVersions.forEach((version, index) => {
const nextVersion = sortedVersions[index + 1];
const isLast = index === sortedVersions.length - 1;
const isNextVersionGap = nextVersion - version > 1;
if (isNextVersionGap || isLast) {
finalOptimizedBrowsers.push({
name: browser,
minVersion: lowestVersionSoFar,
maxVersion: version,
});
lowestVersionSoFar = nextVersion;
}
});
});
return finalOptimizedBrowsers;
};
const getBrowsersFromQuery = (browserListQuery) => {
const browserList = browsersList(browserListQuery);
const browsersWithVersions = getBrowsersWithVersions(browserList);
return getOptimizedVersionDistribution(browsersWithVersions);
};
module.exports = {
getUserAgent,
getBrowser,
getBrowsersFromQuery,
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc