@alitu/user-agents-v2-api 0.1.0

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # user-agents-v2-api
2
+
3
+ API for identifying User-Agent strings using OPAWG's user-agents-v2 lists.
package/dist/index.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.default = parseUserAgent;
15
+ var user_agents_v2_1 = require("./user-agents-v2");
16
+ // Files must be searched in this order to accurately identify User-Agent
17
+ // https://github.com/the-podcast-host/user-agents-v2?tab=readme-ov-file#quick-start
18
+ var files = [
19
+ { filename: 'bots', entries: user_agents_v2_1.default.bots },
20
+ { filename: 'apps', entries: user_agents_v2_1.default.apps },
21
+ { filename: 'libraries', entries: user_agents_v2_1.default.libraries },
22
+ { filename: 'browsers', entries: user_agents_v2_1.default.browsers },
23
+ ];
24
+ /**
25
+ * Check if a string is URL encoded.
26
+ * @param {string} str string to check
27
+ * @returns {boolean} boolean
28
+ */
29
+ function isUrlEncoded(str) {
30
+ try {
31
+ return str !== decodeURIComponent(str);
32
+ }
33
+ catch (error) {
34
+ return false;
35
+ }
36
+ }
37
+ /**
38
+ * Search OPAWG user-agent-v2 lists for User-Agent string.
39
+ * @param {string} userAgent User-Agent string
40
+ * @returns {OPAWGPatternEntry | undefined} OPAWG pattern entry, undefined if not found
41
+ */
42
+ function parseUserAgent(userAgent) {
43
+ // Skip empty strings
44
+ if (!userAgent)
45
+ return undefined;
46
+ // Remove URL encoding
47
+ var sanitizedUserAgent = isUrlEncoded(userAgent)
48
+ ? decodeURIComponent(userAgent)
49
+ : userAgent;
50
+ // Remove newlines
51
+ sanitizedUserAgent = userAgent.replace(/(\r\n|\n|\r)/g, '');
52
+ // Iterate lists in order
53
+ for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
54
+ var _a = files_1[_i], filename = _a.filename, entries = _a.entries;
55
+ for (var _b = 0, entries_1 = entries; _b < entries_1.length; _b++) {
56
+ var entry = entries_1[_b];
57
+ // Return entry if match is found
58
+ if (sanitizedUserAgent.match(entry.regexp)) {
59
+ return __assign({ filename: filename }, entry);
60
+ }
61
+ }
62
+ }
63
+ // If no match is found, return undefined
64
+ return undefined;
65
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var _1 = require(".");
4
+ test('identifies bots', function () {
5
+ var entry = (0, _1.default)('Mozilla/5.0 (compatible; HubSpot Crawler; +https://www.hubspot.com)');
6
+ expect(entry.name === 'HubSpot Crawler');
7
+ });
8
+ test('identifies apps', function () {
9
+ var entry = (0, _1.default)('Overcast/3.0 (+http://overcast.fm/; iOS podcast app)');
10
+ expect(entry.name).toBe('Overcast');
11
+ });
12
+ test('identifies libraries', function () {
13
+ var entry = (0, _1.default)('AppleCoreMedia/1.0.0.16G114 (iPod touch; U; CPU OS 12_4_2 like Mac OS X; en_us)');
14
+ expect(entry.name).toBe('AppleCoreMedia');
15
+ });
16
+ test('identifies bot libraries', function () {
17
+ var entry = (0, _1.default)('Apache-HttpClient/4.5.3-SNAPSHOT (Java/1.8.0_73)');
18
+ expect(entry.name).toBe('Apache HttpClient');
19
+ expect(entry.category).toBe('bot');
20
+ });
21
+ test('identifies browsers', function () {
22
+ var entry = (0, _1.default)('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36');
23
+ expect(entry.name).toBe('Chrome');
24
+ });