@unavatar/core 3.7.51 → 3.7.53

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/util/got.js +22 -4
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@unavatar/core",
3
3
  "description": "Get unified user avatar from social networks, including Instagram, SoundCloud, Telegram, Twitter, YouTube & more.",
4
4
  "homepage": "https://unavatar.io",
5
- "version": "3.7.51",
5
+ "version": "3.7.53",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
package/src/util/got.js CHANGED
@@ -5,15 +5,33 @@ const tlsHook = require('https-tls/hook')
5
5
  const uaHints = require('ua-hints')
6
6
  const got = require('got')
7
7
 
8
- const randomUserAgent = uniqueRandomArray(require('top-user-agents'))
8
+ const topUserAgents = require('top-user-agents')
9
+ const randomUserAgent = uniqueRandomArray(topUserAgents)
10
+
11
+ const precomputedUaHints = new Map(
12
+ topUserAgents.map(userAgent => [userAgent, uaHints(userAgent)])
13
+ )
14
+
15
+ const precomputedUaHintEntries = new Map(
16
+ topUserAgents.map(userAgent => [
17
+ userAgent,
18
+ Object.entries(precomputedUaHints.get(userAgent))
19
+ ])
20
+ )
21
+
22
+ const getUaHintEntries = userAgent =>
23
+ precomputedUaHintEntries.get(userAgent) || Object.entries(uaHints(userAgent))
9
24
 
10
25
  const userAgentHook = options => {
11
- if (options.headers['user-agent'] === 'got (https://github.com/sindresorhus/got)') {
12
- const userAgent = randomUserAgent()
26
+ let userAgent = options.headers['user-agent']
27
+ if (userAgent === 'got (https://github.com/sindresorhus/got)') {
28
+ userAgent = randomUserAgent()
13
29
  options.headers['user-agent'] = userAgent
14
30
  }
15
31
 
16
- for (const [key, value] of Object.entries(uaHints(options.headers['user-agent']))) {
32
+ const uaHintEntries = getUaHintEntries(userAgent)
33
+ for (let index = 0; index < uaHintEntries.length; index++) {
34
+ const [key, value] = uaHintEntries[index]
17
35
  options.headers[key] = value
18
36
  }
19
37
  }