Skip to content

Commit f989570

Browse files
Refactor identity handling: move uuidNameKey, uuidIdKey, and buildIdentityPayload to utils; clean up profileRoute and uuidRoute
1 parent c6d206b commit f989570

4 files changed

Lines changed: 18 additions & 33 deletions

File tree

pages/defaultPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function escapeHtml(value) {
6868
.replace(/&/g, "&")
6969
.replace(/</g, "&lt;")
7070
.replace(/>/g, "&gt;")
71-
.replace(/\"/g, "&quot;")
71+
.replace(/"/g, "&quot;")
7272
.replace(/'/g, "&#39;");
7373
}
7474

routes/profileRoute.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CACHE_TTL_SECONDS, STATUS } from "../config/constants";
22
import { fetchSessionProfile, lookupByUsername } from "../services/minecraftApi";
33
import { getCachedEntry, setCachedEntry } from "../utils/cache";
4+
import { buildIdentityPayload, uuidIdKey, uuidNameKey } from "../utils/identity";
45
import { badRequest, internalError, jsonResponse, notFound } from "../utils/responses";
56
import {
67
buildCacheMeta,
@@ -9,14 +10,6 @@ import {
910
parseUsernameOrUuid
1011
} from "../utils/utils";
1112

12-
function uuidNameKey(username) {
13-
return `uuid:username:${username.toLowerCase()}`;
14-
}
15-
16-
function uuidIdKey(uuid) {
17-
return `uuid:id:${uuid.toLowerCase()}`;
18-
}
19-
2013
function decodeTextures(rawProfile) {
2114
const texturesProperty = rawProfile.properties?.find((property) => property.name === "textures");
2215
return texturesProperty?.value ? decodeBase64Json(texturesProperty.value) : null;
@@ -44,14 +37,6 @@ function isValidCachedProfile(cached) {
4437
return Boolean(cached?.payload?.profile?.rawProfile) && typeof cached.cachedAt === "number";
4538
}
4639

47-
function buildIdentityPayload(id, name, profile) {
48-
return {
49-
id,
50-
name,
51-
profile
52-
};
53-
}
54-
5540
async function cacheIdentityPayload(env, payload) {
5641
await Promise.all([
5742
setCachedEntry(env, uuidNameKey(payload.name), payload, CACHE_TTL_SECONDS),

routes/uuidRoute.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { CACHE_TTL_SECONDS, STATUS } from "../config/constants";
22
import { getCachedEntry, setCachedEntry } from "../utils/cache";
3+
import { buildIdentityPayload, uuidIdKey, uuidNameKey } from "../utils/identity";
34
import { badRequest, internalError, jsonResponse, notFound } from "../utils/responses";
45
import { buildCacheMeta, nowSeconds, parseUsernameOrUuid } from "../utils/utils";
56
import { lookupByUsername, lookupByUuid } from "../services/minecraftApi";
67

7-
function uuidNameKey(username) {
8-
return `uuid:username:${username.toLowerCase()}`;
9-
}
10-
11-
function uuidIdKey(uuid) {
12-
return `uuid:id:${uuid.toLowerCase()}`;
13-
}
14-
158
function buildResponse(profile, cache) {
169
return {
1710
cache,
@@ -25,14 +18,6 @@ function isValidCachedIdentity(cached) {
2518
return Boolean(cached?.payload?.id) && Boolean(cached?.payload?.name) && typeof cached.cachedAt === "number";
2619
}
2720

28-
function buildIdentityPayload(id, name, profile) {
29-
return {
30-
id,
31-
name,
32-
profile: profile ?? null
33-
};
34-
}
35-
3621
async function cacheIdentityPayload(env, payload) {
3722
await Promise.all([
3823
setCachedEntry(env, uuidNameKey(payload.name), payload, CACHE_TTL_SECONDS),

utils/identity.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function uuidNameKey(username) {
2+
return `uuid:username:${username.toLowerCase()}`;
3+
}
4+
5+
export function uuidIdKey(uuid) {
6+
return `uuid:id:${uuid.toLowerCase()}`;
7+
}
8+
9+
export function buildIdentityPayload(id, name, profile = null) {
10+
return {
11+
id,
12+
name,
13+
profile
14+
};
15+
}

0 commit comments

Comments
 (0)