mowojang 0.7.0 → 0.7.2

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 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 getProfileCacheKey;
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.0"];
5
+ var define_VERSION_default = ["0.7.2"];
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
- getProfileCacheKey(player) {
282
- const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
283
- return `profile:${normalizedPlayer}`;
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.getProfileCacheKey(username);
310
- const uuidID = this.getProfileCacheKey(UUID);
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.getProfileCacheKey(player),
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.getSessionCacheKey(player),
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,25 @@ 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
- const fetchResponse = await this.axios.get(session.data.skin.url, {
601
- id: this.getSkinBufferCacheKey(player),
602
- responseType: "arraybuffer",
603
- cache: config?.cache ?? { ttl: 24 * 60 * 60 * 1e3 }
604
- });
605
- return Buffer.from(fetchResponse.data);
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
+ // @ts-expect-error
603
+ err.config.internalId
604
+ );
605
+ this.logger.info("Mowojang", `Check Status of Mojang Textures API on: https://mowojang-status.pixelic.dev`);
606
+ }
607
+ return null;
608
+ }
606
609
  }
607
610
  /**
608
611
  * A simple Wrapper to retrieve only the Cape Data from a Player's Session
@@ -632,12 +635,25 @@ var Client = class {
632
635
  async getCapeBuffer(player, config) {
633
636
  const session = await this.getSession(player, config);
634
637
  if (session.error || !session.data.cape) return null;
635
- const fetchResponse = await this.axios.get(session.data.cape.url, {
636
- id: this.getCapeBufferCacheKey(session.data.cape.hash),
637
- responseType: "arraybuffer",
638
- cache: config?.cache ?? { ttl: 30 * 24 * 60 * 60 * 1e3 }
639
- });
640
- return Buffer.from(fetchResponse.data);
638
+ try {
639
+ const fetchResponse = await this.axios.get(session.data.cape.url, {
640
+ id: this.getCacheKey("cape", session.data.cape.hash, true),
641
+ responseType: "arraybuffer",
642
+ cache: config?.cache ?? { ttl: 30 * 24 * 60 * 60 * 1e3 }
643
+ });
644
+ return Buffer.from(fetchResponse.data);
645
+ } catch (err) {
646
+ if (isAxiosError(err)) {
647
+ this.logger.error(
648
+ "Mowojang",
649
+ `An Error occured whilst trying to fetch Cape Texture for ${player}`,
650
+ // @ts-expect-error
651
+ err.config.internalId
652
+ );
653
+ this.logger.info("Mowojang", `Check Status of Mojang Textures API on: https://mowojang-status.pixelic.dev`);
654
+ }
655
+ return null;
656
+ }
641
657
  }
642
658
  };
643
659
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mowojang",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "A simple TypeScript compatible wrapper for the Mowojang-API.",
5
5
  "exports": {
6
6
  ".": {