blob: ce5b0bbd6c6c9f5ccb7e4e283dafa2d99273b658 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
/**
* Fetch the Mojang username API through api.ashcon.app
*/
import fetch from 'node-fetch'
import { Agent } from 'https'
import { isUuid, undashUuid } from './util'
// We need to create an agent to prevent memory leaks
const httpsAgent = new Agent({
keepAlive: true
})
interface MojangApiResponse {
/** These uuids are already undashed */
uuid: string
username: string
}
/**
* Get mojang api data from the session server
*/
export async function profileFromUuid(uuid: string): Promise<MojangApiResponse> {
const fetchResponse = await fetch(
// using mojang directly is faster than ashcon lol, also mojang removed the ratelimits from here
`https://sessionserver.mojang.com/session/minecraft/profile/${undashUuid(uuid)}`,
{ agent: () => httpsAgent }
)
let data
try {
data = await fetchResponse.json()
} catch {
// if it errors, just return null
return { uuid: null, username: null }
}
return {
uuid: data.id,
username: data.name
}
}
export async function profileFromUsername(username: string): Promise<MojangApiResponse> {
// since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/
const fetchResponse = await fetch(
`https://api.mojang.com/users/profiles/minecraft/${username}`,
{ agent: () => httpsAgent }
)
let data
try {
data = await fetchResponse.json()
} catch {
return { uuid: null, username: null }
}
return {
uuid: data.id,
username: data.name
}
}
export async function profileFromUser(user: string): Promise<MojangApiResponse> {
if (isUuid(user)) {
return await profileFromUuid(user)
} else
return await profileFromUsername(user)
}
|