header-generator
Advanced tools
Comparing version 1.1.3 to 1.2.0-beta.0
{ | ||
"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.<(BrowserSpecification\|string)></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.<string></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.<string></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, | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5396844
11
1465
146
3
1
+ Addedbrowserslist@^4.19.1
+ Addedbrowserslist@4.24.2(transitive)
+ Addedcaniuse-lite@1.0.30001680(transitive)
+ Addedelectron-to-chromium@1.5.62(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addednode-releases@2.0.18(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedupdate-browserslist-db@1.1.1(transitive)