Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
use alternative mojang api
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Jan 13, 2024
1 parent fc08668 commit de342b6
Showing 1 changed file with 25 additions and 51 deletions.
76 changes: 25 additions & 51 deletions src/mojang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { Response as UndiciResponse } from 'undici/types/fetch'

// We need to create an agent to prevent memory leaks
const httpsAgent = new Agent({
keepAlive: true
keepAlive: true,
})

interface MojangApiResponse {
Expand All @@ -21,13 +21,15 @@ interface MojangApiResponse {
/**
* Get mojang api data from the session server
*/
export let profileFromUuid = async function profileFromUuid(uuid: string): Promise<MojangApiResponse> {
export let profileFromUuid = async function profileFromUuid(
uuid: string
): Promise<MojangApiResponse> {
let fetchResponse: UndiciResponse

try {
fetchResponse = await fetch(
// using mojang directly is faster than ashcon, also there seem to be no ratelimits here?
`https://sessionserver.mojang.com/session/minecraft/profile/${undashUuid(uuid)}`,
`https://mowojang.matdoes.dev/${undashUuid(uuid)}`
)
} catch {
// if there's an error, wait a second and try again
Expand All @@ -50,20 +52,19 @@ export let profileFromUuid = async function profileFromUuid(uuid: string): Promi
}
return {
uuid: data.id,
username: data.name
username: data.name,
}
}


export let profileFromUsername = async function profileFromUsername(username: string): Promise<MojangApiResponse> {
export let profileFromUsername = 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/

let fetchResponse: UndiciResponse

try {
fetchResponse = await fetch(
`https://api.mojang.com/users/profiles/minecraft/${username}`,
)
fetchResponse = await fetch(`https://mowojang.matdoes.dev/${username}`)
} catch {
// if there's an error, wait a second and try again
await sleep(1000)
Expand All @@ -74,56 +75,29 @@ export let profileFromUsername = async function profileFromUsername(username: st
const rawData = await fetchResponse.text()
try {
data = JSON.parse(rawData)
} catch { }


if (!data?.id) {
// return { uuid: null, username: null }
return await profileFromUsernameAlternative(username)
}
} catch {}

return {
uuid: data.id,
username: data.name
}
}

export async function profileFromUsernameAlternative(username: string): Promise<MojangApiResponse> {
let fetchResponse: UndiciResponse

try {
fetchResponse = await fetch(
`https://api.ashcon.app/mojang/v2/user/${username}`,
)
} catch {
// if there's an error, wait a second and try again
await sleep(1000)
return await profileFromUsernameAlternative(username)
}

let data
try {
data = await fetchResponse.json()
} catch {
return { uuid: null, username: null }
}
if (!data.uuid)
return { uuid: null, username: null }
return {
uuid: undashUuid(data.uuid),
username: data.username
username: data.name,
}
}

export let profileFromUser = async function profileFromUser(user: string): Promise<MojangApiResponse> {
export let profileFromUser = async function profileFromUser(
user: string
): Promise<MojangApiResponse> {
if (isUuid(user)) {
return await profileFromUuid(user)
} else
return await profileFromUsername(user)
} else return await profileFromUsername(user)
}


// this is necessary for mocking in the tests because es6
export function mockProfileFromUuid($value) { profileFromUuid = $value }
export function mockProfileFromUsername($value) { profileFromUsername = $value }
export function mockProfileFromUser($value) { profileFromUser = $value }
export function mockProfileFromUuid($value) {
profileFromUuid = $value
}
export function mockProfileFromUsername($value) {
profileFromUsername = $value
}
export function mockProfileFromUser($value) {
profileFromUser = $value
}

0 comments on commit de342b6

Please sign in to comment.