aboutsummaryrefslogtreecommitdiff
path: root/build/mojang.js
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-30 16:18:57 -0600
committermat <github@matdoes.dev>2021-12-30 16:18:57 -0600
commit342d31e99d1fbd7bcc941e34bb2a175382f3e26e (patch)
treec735fa67652d49386d3b55b42567ea3ebd57c5c4 /build/mojang.js
parentf7d1263ea669968a4b69d80cede9b77f240de4b6 (diff)
downloadskyblock-api-342d31e99d1fbd7bcc941e34bb2a175382f3e26e.tar.gz
skyblock-api-342d31e99d1fbd7bcc941e34bb2a175382f3e26e.tar.bz2
skyblock-api-342d31e99d1fbd7bcc941e34bb2a175382f3e26e.zip
remove build folder
Diffstat (limited to 'build/mojang.js')
-rw-r--r--build/mojang.js106
1 files changed, 0 insertions, 106 deletions
diff --git a/build/mojang.js b/build/mojang.js
deleted file mode 100644
index 7682839..0000000
--- a/build/mojang.js
+++ /dev/null
@@ -1,106 +0,0 @@
-/**
- * Fetch the Mojang username API through api.ashcon.app
- */
-import { isUuid, undashUuid } from './util.js';
-import fetch from 'node-fetch';
-import { Agent } from 'https';
-// We need to create an agent to prevent memory leaks
-const httpsAgent = new Agent({
- keepAlive: true
-});
-/**
- * Get mojang api data from the session server
- */
-export let profileFromUuid = async function profileFromUuid(uuid) {
- let fetchResponse;
- try {
- 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 });
- }
- catch {
- // if there's an error, wait a second and try again
- await new Promise((resolve) => setTimeout(resolve, 1000));
- return await profileFromUuid(uuid);
- }
- let dataString;
- try {
- dataString = await fetchResponse.text();
- }
- catch (err) {
- return { uuid: null, username: null };
- }
- let data;
- try {
- data = JSON.parse(dataString);
- }
- catch {
- // if it errors, just return null
- return { uuid: null, username: null };
- }
- return {
- uuid: data.id,
- username: data.name
- };
-};
-export let profileFromUsername = 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 fetch(`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 = null;
- const rawData = await fetchResponse.text();
- try {
- data = JSON.parse(rawData);
- }
- catch { }
- if (!data?.id) {
- // return { uuid: null, username: null }
- return await profileFromUsernameAlternative(username);
- }
- return {
- uuid: data.id,
- username: data.name
- };
-};
-export async function profileFromUsernameAlternative(username) {
- let fetchResponse;
- try {
- fetchResponse = await fetch(`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 };
- }
- if (!data.uuid)
- return { uuid: null, username: null };
- return {
- uuid: undashUuid(data.uuid),
- username: data.username
- };
-}
-export let profileFromUser = async function profileFromUser(user) {
- if (isUuid(user)) {
- return await profileFromUuid(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; }