Skip to content

Commit

Permalink
[helpers] split helpers into 2 new packages: user-agent-helpers &…
Browse files Browse the repository at this point in the history
… `client-hints-helpers`
  • Loading branch information
faisalman committed Aug 23, 2023
1 parent 1296576 commit 3f105fe
Show file tree
Hide file tree
Showing 16 changed files with 543 additions and 359 deletions.
24 changes: 20 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 48 additions & 41 deletions script/build-module.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,64 @@
#!/usr/bin/env node
/* jshint esversion: 6 */
const fs = require('fs');
const PATH = {
main : {
src : 'src/main/ua-parser.js',
dest : 'src/main/ua-parser.mjs',
title : ''
},
enums : {
src : 'src/enums/ua-parser-enums.js',
dest :'src/enums/ua-parser-enums.mjs',
title : 'enums'
},
extensions : {
src : 'src/extensions/ua-parser-extensions.js',
dest : 'src/extensions/ua-parser-extensions.mjs',
title : 'extensions'
},
helpers : {
src : 'src/helpers/ua-parser-helpers.js',
dest : 'src/helpers/ua-parser-helpers.mjs',
title : 'helpers'
}
};
const generateMJS = (module, replacers) => {
const { src, dest, title } = PATH[module];

const generateMJS = (module) => {
let { src, dest, title, replacements } = module;
let text = fs.readFileSync(src, 'utf-8');
replacers.forEach(replacer => {
text = text.replace(replacer[0], replacer[1]);

replacements.push(
[/const (.+?)\s*=\s*require\((.+)\)/ig, 'import $1 from $2'],
[/module\.exports =/ig, 'export']
);
replacements.forEach(rep => {
text = text.replace(rep[0], rep[1]);
});

console.log(`Generate ${dest}`);

fs.writeFileSync(dest,
`// Generated ESM version of UAParser.js ${title}
`// Generated ESM version of ${title}
// DO NOT EDIT THIS FILE!
// Source: /${src}
${text}`, 'utf-8');
};

// ua-parser.mjs
generateMJS('main', [
[/\(func[\s\S]+strict\';/ig, ''],
[/esversion\: 3/ig, 'esversion: 6'],
[/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};']
]);

// ua-parser-enum.mjs
generateMJS('enums', [[/module\.exports =/ig, 'export']]);
};

// ua-parser-extension.mjs
generateMJS('extensions', [[/module\.exports =/ig, 'export']]);
const modules = [
{
src : 'src/main/ua-parser.js',
dest : 'src/main/ua-parser.mjs',
title : 'ua-parser-js',
replacements : [
[/\(func[\s\S]+strict\';/ig, ''],
[/esversion\: 3/ig, 'esversion: 6'],
[/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};']
]
},{
src : 'src/enums/ua-parser-enums.js',
dest :'src/enums/ua-parser-enums.mjs',
title : 'ua-parser-js/enums',
replacements : []
},
{
src : 'src/extensions/ua-parser-extensions.js',
dest : 'src/extensions/ua-parser-extensions.mjs',
title : 'ua-parser-js/extensions',
replacements : []
},
{
src : 'src/user-agent-helpers/user-agent-helpers.js',
dest : 'src/user-agent-helpers/user-agent-helpers.mjs',
title : '@ua-parser-js/user-agent-helpers',
replacements : []
},
{
src : 'src/client-hints-helpers/client-hints-helpers.js',
dest : 'src/client-hints-helpers/client-hints-helpers.mjs',
title : '@ua-parser-js/client-hints-helpers',
replacements : []
}
];

// ua-parser-helpers.mjs
generateMJS('helpers', [[/module\.exports =/ig, 'export']]);
modules.forEach(module => generateMJS(module));
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ export interface ClientHintsHTTPHeaders {
'sec-ch-ua-wow64'?: string;
}

export function isFrozenUA(ua: string): boolean;
export function unfreezeUA(): Promise<string>;
export function UACHParser(headers: ClientHintsHTTPHeaders): ClientHintsJSHighEntropy;
88 changes: 88 additions & 0 deletions src/client-hints-helpers/client-hints-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
////////////////////////////////////////////////////
/* A collection of utility methods for client-hints
https://github.com/faisalman/ua-parser-js
Author: Faisal Salman <f@faisalman.com>
MIT License */
///////////////////////////////////////////////////

/*jshint esversion: 11 */

const UACHMap = {
'sec-ch-ua-arch' : {
prop : 'architecture',
type : 'sf-string'
},
'sec-ch-ua-bitness' : {
prop : 'bitness',
type : 'sf-string'
},
'sec-ch-ua' : {
prop : 'brands',
type : 'sf-list'
},
'sec-ch-ua-form-factor' : {
prop : 'formFactor',
type : 'sf-string'
},
'sec-ch-ua-full-version-list' : {
prop : 'fullVersionList',
type : 'sf-list'
},
'sec-ch-ua-mobile' : {
prop : 'mobile',
type : 'sf-boolean',
},
'sec-ch-ua-model' : {
prop : 'model',
type : 'sf-string',
},
'sec-ch-ua-platform' : {
prop : 'platform',
type : 'sf-string'
},
'sec-ch-ua-platform-version' : {
prop : 'platformVersion',
type : 'sf-string'
},
'sec-ch-ua-wow64' : {
prop : 'wow64',
type : 'sf-boolean'
}
};

const UACHParser = (headers) => {
const parse = (str, type) => {
if (!str) {
return '';
}
switch (type) {
case 'sf-boolean':
return /\?1/.test(str);
case 'sf-list':
return str.replace(/\\?\"/g, '')
.split(', ')
.map(brands => {
const [brand, version] = brands.split(';v=');
return {
brand : brand,
version : version
};
});
case 'sf-string':
default:
return str.replace(/\\?\"/g, '');
}
};
let ch = {};
Object.keys(UACHMap).forEach(field => {
if (headers.hasOwnProperty(field)) {
const { prop, type } = UACHMap[field];
ch[prop] = parse(headers[field], type);
}
});
return ch;
};

module.exports = {
UACHParser
};
92 changes: 92 additions & 0 deletions src/client-hints-helpers/client-hints-helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Generated ESM version of @ua-parser-js/client-hints-helpers
// DO NOT EDIT THIS FILE!
// Source: /src/client-hints-helpers/client-hints-helpers.js

////////////////////////////////////////////////////
/* A collection of utility methods for client-hints
https://github.com/faisalman/ua-parser-js
Author: Faisal Salman <f@faisalman.com>
MIT License */
///////////////////////////////////////////////////

/*jshint esversion: 11 */

const UACHMap = {
'sec-ch-ua-arch' : {
prop : 'architecture',
type : 'sf-string'
},
'sec-ch-ua-bitness' : {
prop : 'bitness',
type : 'sf-string'
},
'sec-ch-ua' : {
prop : 'brands',
type : 'sf-list'
},
'sec-ch-ua-form-factor' : {
prop : 'formFactor',
type : 'sf-string'
},
'sec-ch-ua-full-version-list' : {
prop : 'fullVersionList',
type : 'sf-list'
},
'sec-ch-ua-mobile' : {
prop : 'mobile',
type : 'sf-boolean',
},
'sec-ch-ua-model' : {
prop : 'model',
type : 'sf-string',
},
'sec-ch-ua-platform' : {
prop : 'platform',
type : 'sf-string'
},
'sec-ch-ua-platform-version' : {
prop : 'platformVersion',
type : 'sf-string'
},
'sec-ch-ua-wow64' : {
prop : 'wow64',
type : 'sf-boolean'
}
};

const UACHParser = (headers) => {
const parse = (str, type) => {
if (!str) {
return '';
}
switch (type) {
case 'sf-boolean':
return /\?1/.test(str);
case 'sf-list':
return str.replace(/\\?\"/g, '')
.split(', ')
.map(brands => {
const [brand, version] = brands.split(';v=');
return {
brand : brand,
version : version
};
});
case 'sf-string':
default:
return str.replace(/\\?\"/g, '');
}
};
let ch = {};
Object.keys(UACHMap).forEach(field => {
if (headers.hasOwnProperty(field)) {
const { prop, type } = UACHMap[field];
ch[prop] = parse(headers[field], type);
}
});
return ch;
};

export {
UACHParser
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"title": "UAParser.js Helpers",
"name": "@ua-parser-js/helpers",
"version": "0.0.3",
"title": "Client-Hints Helpers",
"name": "@ua-parser-js/client-hints-helpers",
"version": "0.0.1",
"author": "Faisal Salman <f@faisalman.com>",
"description": "A collection of utility methods for UAParser.js",
"main": "ua-parser-helpers.js",
"module": "ua-parser-helpers.mjs",
"description": "A collection of utility methods for working with client-hints",
"main": "client-hints-helpers.js",
"module": "client-hints-helpers.mjs",
"scripts": {
"test": "echo 1"
"test": "mocha ../../test/mocha-test-helpers"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 3f105fe

Please sign in to comment.