mowojang 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -4
- package/dist/index.js +45 -31
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -60,6 +60,8 @@ interface ValidationOptions {
|
|
|
60
60
|
*
|
|
61
61
|
* WARNING: This is an API intended for very specific usecases.
|
|
62
62
|
* Incorrect writes/deletes or clearing storage at the wrong time can break cache behavior or cause unexpected errors.
|
|
63
|
+
*
|
|
64
|
+
* @link https://github.com/Pixelicc/mowojang/blob/main/docs/custom-caching.md#internal-cache-access
|
|
63
65
|
*/
|
|
64
66
|
type MowojangCache = readonly {
|
|
65
67
|
/** Clears the entire internal cache storage. */
|
|
@@ -155,10 +157,7 @@ declare class Client {
|
|
|
155
157
|
constructor(clientOptions?: ClientOptions);
|
|
156
158
|
private shouldValidate;
|
|
157
159
|
private getValidationMinLength;
|
|
158
|
-
private
|
|
159
|
-
private getSessionCacheKey;
|
|
160
|
-
private getSkinBufferCacheKey;
|
|
161
|
-
private getCapeBufferCacheKey;
|
|
160
|
+
private getCacheKey;
|
|
162
161
|
private mirrorProfileCacheEntries;
|
|
163
162
|
private _clearCache;
|
|
164
163
|
private _setCacheKey;
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// <define:VERSION>
|
|
5
|
-
var define_VERSION_default = ["0.7.
|
|
5
|
+
var define_VERSION_default = ["0.7.1"];
|
|
6
6
|
|
|
7
7
|
// src/utils.ts
|
|
8
8
|
var undashUUID = /* @__PURE__ */ __name((UUID) => UUID.replace(/-/g, "").toLowerCase(), "undashUUID");
|
|
@@ -278,20 +278,10 @@ var Client = class {
|
|
|
278
278
|
getValidationMinLength(config) {
|
|
279
279
|
return this.validation?.minimumUsernameLength ?? config?.validation?.minimumUsernameLength;
|
|
280
280
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
getSessionCacheKey(player) {
|
|
286
|
-
const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
|
|
287
|
-
return `session:${normalizedPlayer}`;
|
|
288
|
-
}
|
|
289
|
-
getSkinBufferCacheKey(player) {
|
|
290
|
-
const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
|
|
291
|
-
return `skin:${normalizedPlayer}`;
|
|
292
|
-
}
|
|
293
|
-
getCapeBufferCacheKey(hash) {
|
|
294
|
-
return `cape:${hash}`;
|
|
281
|
+
getCacheKey(prefix, value, hash = false) {
|
|
282
|
+
if (hash) return `${prefix}:${value}`;
|
|
283
|
+
const normalized = validateUUID(value) ? `uuid:${undashUUID(value).toLowerCase()}` : `username:${value.toLowerCase()}`;
|
|
284
|
+
return `${prefix}:${normalized}`;
|
|
295
285
|
}
|
|
296
286
|
async mirrorProfileCacheEntries(ID, username, UUID, internalId) {
|
|
297
287
|
try {
|
|
@@ -306,8 +296,8 @@ var Client = class {
|
|
|
306
296
|
}
|
|
307
297
|
const value = await storage.get(ID);
|
|
308
298
|
if (!value) return;
|
|
309
|
-
const usernameID = this.
|
|
310
|
-
const uuidID = this.
|
|
299
|
+
const usernameID = this.getCacheKey("profile", username);
|
|
300
|
+
const uuidID = this.getCacheKey("profile", UUID);
|
|
311
301
|
if (ID !== usernameID) {
|
|
312
302
|
await storage.set(usernameID, value);
|
|
313
303
|
this.logger.debug("Mowojang", `PROFILE-CACHE-MIRROR-SET ${ID} -> ${usernameID}`, internalId);
|
|
@@ -429,7 +419,7 @@ var Client = class {
|
|
|
429
419
|
if (this.shouldValidate(config) && !validatePlayer(player, this.getValidationMinLength(config)))
|
|
430
420
|
return { data: null, error: "INVALID_INPUT" };
|
|
431
421
|
const fetchResponse = await this.axios.get(`${this.baseURL}/${player}`, {
|
|
432
|
-
id: this.
|
|
422
|
+
id: this.getCacheKey("profile", player),
|
|
433
423
|
cache: config?.cache ?? { ttl: 60 * 60 * 1e3 }
|
|
434
424
|
});
|
|
435
425
|
if (config?.cache !== false && (typeof config?.cache !== "object" || config.cache.enabled !== false)) {
|
|
@@ -528,7 +518,7 @@ var Client = class {
|
|
|
528
518
|
const UUID = validateUUID(player) ? undashUUID(player) : await this.getUUID(player);
|
|
529
519
|
if (!UUID) return { data: null, error: "INVALID_PLAYER" };
|
|
530
520
|
return await this.axios.get(`${this.baseURL}/session/minecraft/profile/${UUID}`, {
|
|
531
|
-
id: this.
|
|
521
|
+
id: this.getCacheKey("session", player),
|
|
532
522
|
cache: config?.cache ?? { ttl: 15 * 60 * 1e3 }
|
|
533
523
|
}).then((fetchResponse) => {
|
|
534
524
|
let textures = fetchResponse.data.properties.find((property) => property.name === "textures").value;
|
|
@@ -597,12 +587,24 @@ var Client = class {
|
|
|
597
587
|
async getSkinBuffer(player, config) {
|
|
598
588
|
const session = await this.getSession(player, config);
|
|
599
589
|
if (session.error || !session.data.skin) return null;
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
590
|
+
try {
|
|
591
|
+
const fetchResponse = await this.axios.get(session.data.skin.url, {
|
|
592
|
+
id: this.getCacheKey("skin", player),
|
|
593
|
+
responseType: "arraybuffer",
|
|
594
|
+
cache: config?.cache ?? { ttl: 24 * 60 * 60 * 1e3 }
|
|
595
|
+
});
|
|
596
|
+
return Buffer.from(fetchResponse.data);
|
|
597
|
+
} catch (err) {
|
|
598
|
+
if (isAxiosError(err)) {
|
|
599
|
+
this.logger.error(
|
|
600
|
+
"Mowojang",
|
|
601
|
+
`An Error occured whilst trying to fetch Skin Texture for ${player}`,
|
|
602
|
+
err.request.internalId
|
|
603
|
+
);
|
|
604
|
+
this.logger.info("Mowojang", `Check Status of Mojang Textures API on: https://mowojang-status.pixelic.dev`);
|
|
605
|
+
}
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
606
608
|
}
|
|
607
609
|
/**
|
|
608
610
|
* A simple Wrapper to retrieve only the Cape Data from a Player's Session
|
|
@@ -632,12 +634,24 @@ var Client = class {
|
|
|
632
634
|
async getCapeBuffer(player, config) {
|
|
633
635
|
const session = await this.getSession(player, config);
|
|
634
636
|
if (session.error || !session.data.cape) return null;
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
637
|
+
try {
|
|
638
|
+
const fetchResponse = await this.axios.get(session.data.cape.url, {
|
|
639
|
+
id: this.getCacheKey("cape", session.data.cape.hash, true),
|
|
640
|
+
responseType: "arraybuffer",
|
|
641
|
+
cache: config?.cache ?? { ttl: 30 * 24 * 60 * 60 * 1e3 }
|
|
642
|
+
});
|
|
643
|
+
return Buffer.from(fetchResponse.data);
|
|
644
|
+
} catch (err) {
|
|
645
|
+
if (isAxiosError(err)) {
|
|
646
|
+
this.logger.error(
|
|
647
|
+
"Mowojang",
|
|
648
|
+
`An Error occured whilst trying to fetch Cape Texture for ${player}`,
|
|
649
|
+
err.request.internalId
|
|
650
|
+
);
|
|
651
|
+
this.logger.info("Mowojang", `Check Status of Mojang Textures API on: https://mowojang-status.pixelic.dev`);
|
|
652
|
+
}
|
|
653
|
+
return null;
|
|
654
|
+
}
|
|
641
655
|
}
|
|
642
656
|
};
|
|
643
657
|
|