@unavatar/core 3.32.6 → 3.32.7

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/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.32.6",
5
+ "version": "3.32.7",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -1,24 +1,62 @@
1
1
  'use strict'
2
2
 
3
- const NPM_URL = 'https://www.npmjs.com'
3
+ const { getAvatarUrl: getGitHubAvatarUrl } = require('./github')
4
+ const stringify = require('../util/stringify')
5
+
6
+ const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
7
+ const GITLAB_API_URL = 'https://gitlab.com/api/v4'
4
8
 
5
9
  const stripAtPrefix = input => input.replace(/^@/, '')
6
10
 
7
- const getAvatarUrl = input => `${NPM_URL}/~${stripAtPrefix(input)}`
11
+ const GITHUB_RE = /github\.com[/:]([^/]+)/
12
+ const GITLAB_RE = /gitlab\.com[/:]([^/]+)/
13
+
14
+ const getRepoUsername = objects => {
15
+ let gitlabFallback
16
+
17
+ for (const { package: pkg } of objects) {
18
+ const repo = pkg?.links?.repository
19
+ if (!repo) continue
8
20
 
9
- const getAvatar = $ => {
10
- const src = $('a[aria-label="Your profile picture"] img').attr('src')
11
- if (!src) return
21
+ const githubMatch = GITHUB_RE.exec(repo)
22
+ if (githubMatch) return { platform: 'github', username: githubMatch[1] }
12
23
 
13
- return src.startsWith('/') ? `${NPM_URL}${src}` : src
24
+ if (!gitlabFallback) {
25
+ const gitlabMatch = GITLAB_RE.exec(repo)
26
+ if (gitlabMatch) { gitlabFallback = { platform: 'gitlab', username: gitlabMatch[1] } }
27
+ }
28
+ }
29
+
30
+ return gitlabFallback
14
31
  }
15
32
 
16
- module.exports = ({ createHtmlProvider }) =>
17
- createHtmlProvider({
18
- name: 'npm',
19
- url: getAvatarUrl,
20
- getter: getAvatar
21
- })
33
+ const getGitLabAvatarUrl = async ({ got, username }) => {
34
+ const { body } = await got(
35
+ `${GITLAB_API_URL}/users?username=${encodeURIComponent(username)}`,
36
+ { responseType: 'json' }
37
+ )
38
+ return body?.[0]?.avatar_url
39
+ }
40
+
41
+ module.exports = ({ constants, got }) =>
42
+ async function npm (input) {
43
+ const username = stripAtPrefix(input)
44
+ const url = `${NPM_REGISTRY_URL}/-/v1/search?${stringify({
45
+ text: `maintainer:${username}`,
46
+ size: 5
47
+ })}`
48
+
49
+ const { body } = await got(url, { responseType: 'json' })
50
+ const objects = (body?.objects ?? []).filter(
51
+ o => o.package?.publisher?.username === username
52
+ )
53
+
54
+ const match = getRepoUsername(objects)
55
+ if (!match) return
56
+
57
+ if (match.platform === 'github') {
58
+ return getGitHubAvatarUrl({ constants, input: match.username })
59
+ }
22
60
 
23
- module.exports.getAvatarUrl = getAvatarUrl
24
- module.exports.getAvatar = getAvatar
61
+ return getGitLabAvatarUrl({ got, username: match.username })
62
+ }