@alitu/user-agents-v2-api 0.1.6 → 0.2.2

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/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ import type { OPAWGPatternEntry } from './user-agents-v2';
2
2
  /**
3
3
  * Search OPAWG user-agent-v2 lists for User-Agent string.
4
4
  * @param {string} userAgent User-Agent string
5
+ * @param {string} [referrer] Referer string
5
6
  * @returns {OPAWGPatternEntry | undefined} OPAWG pattern entry, undefined if not found
6
7
  */
7
- export default function parseUserAgent(userAgent: string): OPAWGPatternEntry | undefined;
8
+ export default function parseUserAgent(userAgent: string, referrer?: string): OPAWGPatternEntry | undefined;
8
9
  export { OPAWGFileType } from './user-agents-v2';
package/dist/index.js CHANGED
@@ -27,9 +27,10 @@ function isUrlEncoded(str) {
27
27
  /**
28
28
  * Search OPAWG user-agent-v2 lists for User-Agent string.
29
29
  * @param {string} userAgent User-Agent string
30
+ * @param {string} [referrer] Referer string
30
31
  * @returns {OPAWGPatternEntry | undefined} OPAWG pattern entry, undefined if not found
31
32
  */
32
- function parseUserAgent(userAgent) {
33
+ function parseUserAgent(userAgent, referrer) {
33
34
  // Skip empty strings
34
35
  if (!userAgent)
35
36
  return undefined;
@@ -39,19 +40,29 @@ function parseUserAgent(userAgent) {
39
40
  : userAgent;
40
41
  // Remove newlines
41
42
  sanitizedUserAgent = sanitizedUserAgent.replace(/(\r\n|\n|\r)/g, '');
43
+ var userAgentMatch;
42
44
  // Iterate lists in order
43
- for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
45
+ filesLoop: for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
44
46
  var entries = files_1[_i];
45
47
  for (var _a = 0, entries_1 = entries; _a < entries_1.length; _a++) {
46
48
  var entry = entries_1[_a];
47
49
  // Return entry if match is found
48
50
  if (sanitizedUserAgent.match(entry.regexp)) {
51
+ userAgentMatch = entry;
52
+ break filesLoop;
53
+ }
54
+ }
55
+ }
56
+ // Attempt to lookup referrers if possible
57
+ if (userAgentMatch.type === 'browsers' && referrer) {
58
+ for (var _b = 0, _c = user_agents_v2_1.default.referrers; _b < _c.length; _b++) {
59
+ var entry = _c[_b];
60
+ if (referrer.match(entry.regexp)) {
49
61
  return entry;
50
62
  }
51
63
  }
52
64
  }
53
- // If no match is found, return undefined
54
- return undefined;
65
+ return userAgentMatch;
55
66
  }
56
67
  var user_agents_v2_2 = require("./user-agents-v2");
57
68
  Object.defineProperty(exports, "OPAWGFileType", { enumerable: true, get: function () { return user_agents_v2_2.OPAWGFileType; } });
@@ -22,3 +22,7 @@ test('identifies browsers', function () {
22
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
23
  expect(entry.name).toBe('Chrome');
24
24
  });
25
+ test('identifies web apps by referrers', function () {
26
+ 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', 'https://podcasts.apple.com/us/podcast/xya/id12341');
27
+ expect(entry.name).toBe('Apple Podcasts');
28
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alitu/user-agents-v2-api",
3
- "version": "0.1.6",
3
+ "version": "0.2.2",
4
4
  "description": "API for identifying User-Agent strings using OPAWG's user-agents-v2 lists.",
5
5
  "main": "dist/index.js",
6
6
  "repository": {