@unavatar/core 3.67.0 → 3.69.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 CHANGED
@@ -44,6 +44,7 @@
44
44
  - [DuckDuckGo](#duckduckgo)
45
45
  - [Duolingo](#duolingo)
46
46
  - [Email](#email)
47
+ - [ENS](#ens)
47
48
  - [Facebook](#facebook)
48
49
  - [Farcaster](#farcaster)
49
50
  - [Flickr](#flickr)
@@ -62,6 +63,7 @@
62
63
  - [Keybase](#keybase)
63
64
  - [Kick](#kick)
64
65
  - [Ko-fi](#ko-fi)
66
+ - [Lemmy](#lemmy)
65
67
  - [Libravatar](#libravatar)
66
68
  - [LinkedIn](#linkedin)
67
69
  - [Mastodon](#mastodon)
@@ -772,6 +774,14 @@ Target a specific provider when you know where the avatar comes from.
772
774
 
773
775
  e.g., [unavatar.io/email/hello@microlink.io](https://unavatar.io/email/hello@microlink.io)
774
776
 
777
+ ### ENS
778
+
779
+ Get the avatar set on any Ethereum Name Service (ENS) name, including subnames like `jesse.base.eth`. A bare label is treated as a `.eth` name.
780
+
781
+ e.g., [unavatar.io/ens/vitalik.eth](https://unavatar.io/ens/vitalik.eth)
782
+
783
+ Names ending in `.eth` are detected automatically, so [unavatar.io/vitalik.eth](https://unavatar.io/vitalik.eth) resolves through ENS too.
784
+
775
785
  ### Facebook
776
786
 
777
787
  Get any Facebook user, page, or group profile picture by their username or ID.
@@ -918,6 +928,12 @@ Get any Ko-fi page's profile picture by the creator username.
918
928
 
919
929
  e.g., [unavatar.io/ko-fi/geekshock](https://unavatar.io/ko-fi/geekshock)
920
930
 
931
+ ### Lemmy
932
+
933
+ Get any Lemmy user's profile picture from any instance using the public user API. Pass the handle as `user@instance` so the account resolves on the correct home instance.
934
+
935
+ e.g., [unavatar.io/lemmy/ruud@lemmy.world](https://unavatar.io/lemmy/ruud@lemmy.world)
936
+
921
937
  ### Libravatar
922
938
 
923
939
  Resolve avatars via [Libravatar](https://www.libravatar.org), the federated, open source alternative to Gravatar, using a plain email or a precomputed hash. Emails are trimmed, lowercased, and hashed with SHA-256; 64-hex SHA-256 and 32-hex MD5 values are passed through (normalized to lowercase).
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.67.0",
5
+ "version": "3.69.0",
6
6
  "main": "src/index.js",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
@@ -119,6 +119,7 @@
119
119
  "youtube"
120
120
  ],
121
121
  "dependencies": {
122
+ "@adraffy/ens-normalize": "~1.11.1",
122
123
  "@keyvhq/compress": "~2.2.0",
123
124
  "@keyvhq/core": "~2.2.7",
124
125
  "@keyvhq/memoize": "~2.2.1",
@@ -15,11 +15,13 @@ const ExtendableError = require('../util/error')
15
15
 
16
16
  const DATA_URI_REGEX = dataUriRegex()
17
17
  const DOMAIN_REGEX = urlRegex({ strict: false })
18
+ const ENS_REGEX = /^[^\s/.][^\s/]*\.eth$/i
18
19
 
19
20
  const isHash = require('../util/is-hash')
20
21
 
21
22
  const getInputType = input => {
22
23
  if (isEmail(input) || isHash(input)) return 'email'
24
+ if (ENS_REGEX.test(input.trim())) return 'ens'
23
25
  if (stableRegex(DOMAIN_REGEX, input)) return 'domain'
24
26
  return 'username'
25
27
  }
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+
3
+ const { ens_normalize: normalizeEnsName } = require('@adraffy/ens-normalize')
4
+
5
+ const ENS_AVATAR_URL = 'https://metadata.ens.domains/mainnet/avatar'
6
+ const ENS_TLD = '.eth'
7
+
8
+ const parseInput = input => {
9
+ const name = input.trim()
10
+ try {
11
+ return normalizeEnsName(name.includes('.') ? name : `${name}${ENS_TLD}`)
12
+ } catch {}
13
+ }
14
+
15
+ const getAvatarUrl = input => {
16
+ const name = parseInput(input)
17
+ return name && `${ENS_AVATAR_URL}/${encodeURIComponent(name)}`
18
+ }
19
+
20
+ module.exports = () =>
21
+ function ens (input) {
22
+ return getAvatarUrl(input)
23
+ }
24
+
25
+ module.exports.parseInput = parseInput
26
+ module.exports.getAvatarUrl = getAvatarUrl
@@ -42,6 +42,7 @@ const providerTiers = {
42
42
  'keybase',
43
43
  'kick',
44
44
  'ko-fi',
45
+ 'lemmy',
45
46
  'linkedin',
46
47
  'mastodon',
47
48
  'medium',
@@ -95,7 +96,8 @@ const providerTiers = {
95
96
  'zhihu'
96
97
  ]
97
98
  ],
98
- domain: [['bimi'], ['duckduckgo', 'google', 'microlink']]
99
+ domain: [['bimi'], ['duckduckgo', 'google', 'microlink']],
100
+ ens: [['ens']]
99
101
  }
100
102
 
101
103
  const { emailHash, ...publicTiers } = providerTiers
@@ -132,6 +134,7 @@ module.exports = ctx => {
132
134
  dribbble: require('./dribbble')(ctx),
133
135
  duckduckgo: require('./duckduckgo')(ctx),
134
136
  duolingo: require('./duolingo')(ctx),
137
+ ens: require('./ens')(ctx),
135
138
  facebook: require('./facebook')(ctx),
136
139
  farcaster: require('./farcaster')(ctx),
137
140
  flickr: require('./flickr')(ctx),
@@ -150,6 +153,7 @@ module.exports = ctx => {
150
153
  keybase: require('./keybase')(ctx),
151
154
  kick: require('./kick')(ctx),
152
155
  'ko-fi': require('./ko-fi')(ctx),
156
+ lemmy: require('./lemmy')(ctx),
153
157
  libravatar: require('./libravatar')(ctx),
154
158
  linkedin: require('./linkedin')(ctx),
155
159
  mastodon: require('./mastodon')(ctx),
@@ -0,0 +1,32 @@
1
+ 'use strict'
2
+
3
+ const { parseInput } = require('./mastodon')
4
+
5
+ const getAvatarUrl = ({ username, server }) =>
6
+ `https://${server}/api/v3/user?username=${encodeURIComponent(username)}`
7
+
8
+ const getAvatar = body => {
9
+ const pic = body?.person_view?.person?.avatar
10
+ return typeof pic === 'string' && pic ? pic : undefined
11
+ }
12
+
13
+ module.exports = ({ got, isReservedIp }) =>
14
+ async function lemmy (input) {
15
+ const parsed = parseInput(input)
16
+ if (!parsed) return
17
+
18
+ if (await isReservedIp(parsed.server)) return
19
+
20
+ const { statusCode, body } = await got(getAvatarUrl(parsed), {
21
+ responseType: 'json',
22
+ throwHttpErrors: false
23
+ })
24
+
25
+ if (statusCode >= 400) return
26
+
27
+ return getAvatar(body)
28
+ }
29
+
30
+ module.exports.parseInput = parseInput
31
+ module.exports.getAvatarUrl = getAvatarUrl
32
+ module.exports.getAvatar = getAvatar