aboutsummaryrefslogtreecommitdiff
path: root/build/mojang.js
blob: 934b14feed06405a9ab179e902f04bf3f3ff7123 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";
/**
 * Fetch the Mojang username API through api.ashcon.app
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.profileFromUser = exports.profileFromUsernameAlternative = exports.profileFromUsername = exports.profileFromUuid = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const https_1 = require("https");
const util_1 = require("./util");
// We need to create an agent to prevent memory leaks
const httpsAgent = new https_1.Agent({
    keepAlive: true
});
/**
 * Get mojang api data from the session server
 */
async function profileFromUuid(uuid) {
    let fetchResponse;
    try {
        fetchResponse = await node_fetch_1.default(
        // using mojang directly is faster than ashcon lol, also mojang removed the ratelimits from here
        `https://sessionserver.mojang.com/session/minecraft/profile/${util_1.undashUuid(uuid)}`, { agent: () => httpsAgent });
    }
    catch {
        // if there's an error, wait a second and try again
        await new Promise((resolve) => setTimeout(resolve, 1000));
        return await profileFromUuid(uuid);
    }
    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
    };
}
exports.profileFromUuid = profileFromUuid;
async function profileFromUsername(username) {
    // since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/
    let fetchResponse;
    try {
        fetchResponse = await node_fetch_1.default(`https://api.mojang.com/users/profiles/minecraft/${username}`, { agent: () => httpsAgent });
    }
    catch {
        // if there's an error, wait a second and try again
        await new Promise((resolve) => setTimeout(resolve, 1000));
        return await profileFromUsername(username);
    }
    let data;
    try {
        data = await fetchResponse.json();
    }
    catch { }
    if (!(data === null || data === void 0 ? void 0 : data.id)) {
        console.log('mojang api failed, trying ashcon as backup');
        return await profileFromUsernameAlternative(username);
    }
    return {
        uuid: data.id,
        username: data.name
    };
}
exports.profileFromUsername = profileFromUsername;
async function profileFromUsernameAlternative(username) {
    let fetchResponse;
    try {
        fetchResponse = await node_fetch_1.default(`https://api.ashcon.app/mojang/v2/user/${username}`, { agent: () => httpsAgent });
    }
    catch {
        // if there's an error, wait a second and try again
        await new Promise((resolve) => setTimeout(resolve, 1000));
        return await profileFromUsernameAlternative(username);
    }
    let data;
    try {
        data = await fetchResponse.json();
    }
    catch {
        return { uuid: null, username: null };
    }
    return {
        uuid: util_1.undashUuid(data.uuid),
        username: data.username
    };
}
exports.profileFromUsernameAlternative = profileFromUsernameAlternative;
async function profileFromUser(user) {
    if (util_1.isUuid(user)) {
        return await profileFromUuid(user);
    }
    else
        return await profileFromUsername(user);
}
exports.profileFromUser = profileFromUser;