/ shared / utils / node_modules / @amp / runtime-detect / dist / user-agent.js
user-agent.js
  1  import { parseVersion } from './version.js';
  2  import { applyRules } from './rules.js';
  3  
  4  function _define_property(obj, key, value) {
  5      if (key in obj) {
  6          Object.defineProperty(obj, key, {
  7              value: value,
  8              enumerable: true,
  9              configurable: true,
 10              writable: true
 11          });
 12      } else {
 13          obj[key] = value;
 14      }
 15      return obj;
 16  }
 17  function _object_spread(target) {
 18      for(var i = 1; i < arguments.length; i++){
 19          var source = arguments[i] != null ? arguments[i] : {};
 20          var ownKeys = Object.keys(source);
 21          if (typeof Object.getOwnPropertySymbols === "function") {
 22              ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
 23                  return Object.getOwnPropertyDescriptor(source, sym).enumerable;
 24              }));
 25          }
 26          ownKeys.forEach(function(key) {
 27              _define_property(target, key, source[key]);
 28          });
 29      }
 30      return target;
 31  }
 32  function ownKeys(object, enumerableOnly) {
 33      var keys = Object.keys(object);
 34      if (Object.getOwnPropertySymbols) {
 35          var symbols = Object.getOwnPropertySymbols(object);
 36          if (enumerableOnly) {
 37              symbols = symbols.filter(function(sym) {
 38                  return Object.getOwnPropertyDescriptor(object, sym).enumerable;
 39              });
 40          }
 41          keys.push.apply(keys, symbols);
 42      }
 43      return keys;
 44  }
 45  function _object_spread_props(target, source) {
 46      source = source != null ? source : {};
 47      if (Object.getOwnPropertyDescriptors) {
 48          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
 49      } else {
 50          ownKeys(Object(source)).forEach(function(key) {
 51              Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
 52          });
 53      }
 54      return target;
 55  }
 56  var _match_, _match_1;
 57  const RULES = {
 58      // BROWSER ==========================================================================
 59      browser: [
 60          // WEBVIEW ------------------------------------------------------------------------
 61          // iTunes/Music.app/TV.app
 62          [
 63              /^(itunes|music|tv)\/([\w.]+)\s/i,
 64              (match)=>_object_spread_props(_object_spread({
 65                      name: 'webview',
 66                      variant: match[1].trim().toLowerCase().replace(/(music|tv)/i, '$1.app')
 67                  }, parseVersion(match[2])), {
 68                      mobile: false
 69                  })
 70          ],
 71          // Facebook
 72          [
 73              /(?:(?:fban\/fbios|fb_iab\/fb4a)(?!.+fbav)|;fbav\/([\w.]+);)/i,
 74              (match)=>_object_spread({
 75                      name: 'webview',
 76                      variant: 'facebook',
 77                      mobile: true
 78                  }, parseVersion(match[1]))
 79          ],
 80          // Instagram / SnapChat
 81          [
 82              /(instagram|snapchat)[/ ]([-\w.]+)/i,
 83              (match)=>_object_spread({
 84                      name: 'webview',
 85                      variant: match[1].trim().toLowerCase(),
 86                      mobile: true
 87                  }, parseVersion(match[2]))
 88          ],
 89          // TikTok
 90          [
 91              /musical_ly(?:.+app_?version\/|_)([\w.]+)/i,
 92              (match)=>_object_spread({
 93                      name: 'webview',
 94                      variant: 'tiktok',
 95                      mobile: true
 96                  }, parseVersion(match[1]))
 97          ],
 98          // Twitter
 99          [
100              /twitter/i,
101              ()=>({
102                      name: 'webview',
103                      variant: 'twitter',
104                      mobile: true
105                  })
106          ],
107          // Chrome WebView
108          [
109              / wv\).+?(?:version|chrome)\/([\w.]+)/i,
110              (match)=>_object_spread({
111                      name: 'webview',
112                      mobile: true
113                  }, parseVersion(match[1]))
114          ],
115          // ELECTRON -----------------------------------------------------------------------
116          [
117              /electron\/([\w.]+) safari/i,
118              (match)=>_object_spread({
119                      name: 'electron',
120                      mobile: false
121                  }, parseVersion(match[1]))
122          ],
123          // OTHER --------------------------------------------------------------------------
124          [
125              /tesla\/(.*?(20\d\d\.([-\w.])+))/i,
126              (match)=>_object_spread_props(_object_spread({
127                      name: 'other',
128                      variant: 'tesla',
129                      mobile: false
130                  }, parseVersion(match[2])), {
131                      version: match[1]
132                  })
133          ],
134          [
135              /(samsung|huawei)browser\/([-\w.]+)/i,
136              (match)=>_object_spread({
137                      name: 'other',
138                      variant: match[1].trim().toLowerCase().replace(/browser/i, ''),
139                      mobile: true
140                  }, parseVersion(match[2]))
141          ],
142          [
143              /yabrowser\/([-\w.]+)/i,
144              (match)=>_object_spread({
145                      name: 'other',
146                      variant: 'yandex',
147                      mobile: false
148                  }, parseVersion(match[1]))
149          ],
150          [
151              /(brave|flock|rockmelt|midori|epiphany|silk|skyfire|ovibrowser|bolt|iron|vivaldi|iridium|phantomjs|bowser|quark|qupzilla|falkon|rekonq|puffin|whale(?!.+naver)|qqbrowserlite|qq|duckduckgo)\/([-\w.]+)/i,
152              (match, { userAgent })=>_object_spread({
153                      name: 'other',
154                      variant: match[1].trim().toLowerCase(),
155                      mobile: /mobile/i.test(userAgent)
156                  }, parseVersion(match[2].replace(/-/g, '.')))
157          ],
158          // EDGE / IE ----------------------------------------------------------------------
159          [
160              /edg(e|ios|a)?\/([\w.]+)/i,
161              (match)=>_object_spread({
162                      name: 'edge',
163                      mobile: /(edgios|edga)/i.test((_match_ = match[1]) !== null && _match_ !== void 0 ? _match_ : '')
164                  }, parseVersion(match[2]))
165          ],
166          [
167              /trident.+rv[: ]([\w.]{1,9})\b.+like gecko/i,
168              (match)=>_object_spread({
169                      name: 'ie',
170                      mobile: false
171                  }, parseVersion(match[1]))
172          ],
173          // OPERA --------------------------------------------------------------------------
174          [
175              /opr\/([\w.]+)/i,
176              /opera mini\/([-\w.]+)/i,
177              /opera [mobiletab]{3,6}\b.+version\/([-\w.]+)/i,
178              /opera(?:.+version\/|[/ ]+)([\w.]+)/i,
179              (match)=>_object_spread({
180                      name: 'opera',
181                      mobile: /mobile/i.test(match[0])
182                  }, parseVersion(match[1]))
183          ],
184          // CHROME -------------------------------------------------------------------------
185          // Headless
186          [
187              /headlesschrome(?:\/([\w.]+)| )/i,
188              (match)=>_object_spread({
189                      name: 'chrome',
190                      variant: 'headless',
191                      mobile: false
192                  }, parseVersion(match[1]))
193          ],
194          // Chrome for iOS
195          [
196              /\b(?:crmo|crios)\/([\w.]+)/i,
197              (match)=>_object_spread({
198                      name: 'chrome',
199                      mobile: true
200                  }, parseVersion(match[1]))
201          ],
202          // Chrome
203          [
204              /chrome(?: browser)?\/v?([\w.]+)( mobile)?/i,
205              (match)=>_object_spread({
206                      name: 'chrome',
207                      mobile: /mobile/i.test((_match_1 = match[2]) !== null && _match_1 !== void 0 ? _match_1 : '')
208                  }, parseVersion(match[1]))
209          ],
210          // FIREFOX ------------------------------------------------------------------------
211          // Focus
212          [
213              /\bfocus\/([\w.]+)/i,
214              (match)=>_object_spread({
215                      name: 'firefox',
216                      variant: 'focus',
217                      mobile: true
218                  }, parseVersion(match[1]))
219          ],
220          // Firefox for iOS
221          [
222              /fxios\/([\w.-]+)/i,
223              /(?:mobile|tablet);.*(?:firefox)\/([\w.-]+)/i,
224              (match)=>_object_spread({
225                      name: 'firefox',
226                      mobile: true
227                  }, parseVersion(match[1]))
228          ],
229          // Firefox OSS versions
230          [
231              /(icedragon|iceweasel|camino|chimera|fennec|maemo browser|minimo|conkeror|klar)[/ ]?([\w.+]+)/i,
232              (match)=>_object_spread({
233                      name: 'firefox',
234                      variant: match[1].trim().toLowerCase(),
235                      mobile: false
236                  }, parseVersion(match[2]))
237          ],
238          // Firefox
239          [
240              /(?:firefox)\/([\w.]+)/i,
241              /(?:mozilla)\/([\w.]+) .+rv:.+gecko\/\d+/i,
242              (match)=>_object_spread({
243                      name: 'firefox',
244                      mobile: false
245                  }, parseVersion(match[1]))
246          ],
247          // SAFARI -------------------------------------------------------------------------
248          [
249              /version\/([\w.,]+) .*mobile(?:\/\w+ | ?)safari/i,
250              /version\/([\w.,]+) .*(safari)/i,
251              /webkit.+?(?:mobile ?safari|safari)(?:\/([\w.]+))/i,
252              (match)=>_object_spread({
253                      name: 'safari',
254                      mobile: /mobile/i.test(match[0])
255                  }, parseVersion(match[1]))
256          ]
257      ],
258      // ENGINE ---------------------------------------------------------------------------
259      engine: [
260          [
261              /webkit\/(?:537\.36).+chrome\/(?!27)([\w.]+)/i,
262              (match)=>_object_spread({
263                      name: 'blink'
264                  }, parseVersion(match[1]))
265          ],
266          [
267              /windows.+ edge\/([\w.]+)/i,
268              (match)=>_object_spread({
269                      name: 'blink'
270                  }, parseVersion(match[1]))
271          ],
272          [
273              /presto\/([\w.]+)/i,
274              (match)=>_object_spread({
275                      name: 'presto'
276                  }, parseVersion(match[2]))
277          ],
278          [
279              /trident\/([\w.]+)/i,
280              (match)=>_object_spread({
281                      name: 'trident'
282                  }, parseVersion(match[1]))
283          ],
284          [
285              /gecko\/([\w.]+)/i,
286              (match)=>_object_spread({
287                      name: 'gecko'
288                  }, parseVersion(match[1]))
289          ],
290          [
291              /(khtml|netfront|netsurf|amaya|lynx|w3m|goanna)\/([\w.]+)/i,
292              (match)=>_object_spread({
293                      name: 'other'
294                  }, parseVersion(match[2]))
295          ],
296          [
297              /webkit\/([\w.]+)/i,
298              (match)=>_object_spread({
299                      name: 'webkit'
300                  }, parseVersion(match[1]))
301          ]
302      ],
303      // OS -------------------------------------------------------------------------------
304      os: [
305          // Windows
306          [
307              /microsoft windows (vista|xp)/i,
308              /windows nt 6\.2; (arm)/i,
309              /windows (?:phone(?: os)?|mobile)[/ ]?([\d.\w ]*)/i,
310              /windows[/ ]?([ntce\d. ]+\w)(?!.+xbox)/i,
311              /(?:win(?=3|9|n)|win 9x )([nt\d.]+)/i,
312              (match)=>_object_spread({
313                      name: 'windows'
314                  }, parseVersion(match[1]))
315          ],
316          // iOS (iPhone/iPad)
317          [
318              /ip[honead]{2,4}\b(?:.*os ([\w]+) like mac|; opera)/i,
319              /(?:ios;fbsv\/|iphone.+ios[/ ])([\d.]+)/i,
320              (match)=>_object_spread({
321                      name: 'ios'
322                  }, parseVersion(match[1].replace(/_/g, '.')))
323          ],
324          // macOS
325          [
326              /mac(?:intosh;?)? os x ?([\d._]+)/i,
327              (match)=>_object_spread({
328                      name: 'macos'
329                  }, parseVersion(match[1].replace(/_/g, '.')))
330          ],
331          // ChromeOS
332          [
333              /cros [\w]+(?:\)| ([\w.]+)\b)/i,
334              (match)=>_object_spread({
335                      name: 'chromeos'
336                  }, parseVersion(match[1]))
337          ],
338          // Android
339          [
340              /(?:android|webos|qnx|bada|rim tablet os|maemo|meego|sailfish)[-/ ]?([\w.]*)/i,
341              /droid ([\w.]+|[\d+])\b.+(android[- ]x86|harmonyos)/i,
342              (match)=>_object_spread({
343                      name: 'android'
344                  }, parseVersion(match[1]))
345          ],
346          // Linux
347          [
348              /linux/i,
349              ()=>({
350                      name: 'linux'
351                  })
352          ]
353      ]
354  };
355  /**
356   * Extend a data structure by running a list of functions over it.
357   */ function applyExtensions(data, extensions) {
358      let result = data;
359      for (const extension of extensions){
360          result = extension(result);
361      }
362      return result;
363  }
364  /**
365   * Parse the user agent string from the navigator object into a descriptor.
366   *
367   * @param navigator The Navigator object to parse
368   * @param options Parse options
369   * @returns The descriptor with optional extensions applied
370   */ function parseUserAgent(navigator, options) {
371      var _navigator, _options;
372      var _navigator_userAgent;
373      const descriptor = {
374          navigator: navigator,
375          ua: (_navigator_userAgent = (_navigator = navigator) === null || _navigator === void 0 ? void 0 : _navigator.userAgent) !== null && _navigator_userAgent !== void 0 ? _navigator_userAgent : '',
376          extensions: [],
377          browser: applyRules(RULES.browser, navigator, {
378              name: 'unknown',
379              mobile: false
380          }),
381          engine: applyRules(RULES.engine, navigator, {
382              name: 'unknown'
383          }),
384          os: applyRules(RULES.os, navigator, {
385              name: 'unknown'
386          })
387      };
388      var _options_extensions;
389      return applyExtensions(descriptor, (_options_extensions = (_options = options) === null || _options === void 0 ? void 0 : _options.extensions) !== null && _options_extensions !== void 0 ? _options_extensions : []);
390  }
391  
392  export { parseUserAgent };