mowojang 0.6.1 → 0.7.0
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 +52 -14
- package/dist/index.js +82 -7
- package/package.json +14 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CacheOptions, CacheRequestConfig, buildStorage, buildMemoryStorage } from 'axios-cache-interceptor';
|
|
1
|
+
import { CacheOptions, NotEmptyStorageValue, StorageValue, AxiosStorage, CacheRequestConfig, buildStorage, buildMemoryStorage } from 'axios-cache-interceptor';
|
|
2
2
|
import { LoggerConfig } from '@pixelic/logger';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -35,30 +35,57 @@ type UUID = string;
|
|
|
35
35
|
* };
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
|
|
38
|
+
interface ClientOptions {
|
|
39
39
|
logger?: LoggerConfig;
|
|
40
40
|
validation?: ValidationOptions;
|
|
41
41
|
timeout?: number;
|
|
42
42
|
cache?: CacheOptions;
|
|
43
43
|
baseURL?: string;
|
|
44
44
|
fallback?: boolean;
|
|
45
|
-
}
|
|
45
|
+
}
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
interface AxiosOptions {
|
|
48
48
|
timeout?: number;
|
|
49
49
|
cache?: CacheOptions;
|
|
50
50
|
fallback: boolean;
|
|
51
|
-
}
|
|
51
|
+
}
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
interface ValidationOptions {
|
|
54
54
|
enabled?: boolean;
|
|
55
55
|
minimumUsernameLength?: 1 | 2;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Cache helpers that proxy the internal storage from `axios-cache-interceptor`.
|
|
60
|
+
*
|
|
61
|
+
* WARNING: This is an API intended for very specific usecases.
|
|
62
|
+
* Incorrect writes/deletes or clearing storage at the wrong time can break cache behavior or cause unexpected errors.
|
|
63
|
+
*/
|
|
64
|
+
type MowojangCache = readonly {
|
|
65
|
+
/** Clears the entire internal cache storage. */
|
|
66
|
+
readonly clear: () => Promise<void>;
|
|
67
|
+
/** Sets a raw internal cache entry for a cache key. */
|
|
68
|
+
readonly set: (key: string, value: NotEmptyStorageValue) => Promise<void>;
|
|
69
|
+
/** Returns the raw internal cache entry for a cache key. */
|
|
70
|
+
readonly get: (key: string) => Promise<StorageValue>;
|
|
71
|
+
/** Deletes a raw internal cache entry by cache key. */
|
|
72
|
+
readonly del: (key: string) => Promise<void>;
|
|
73
|
+
/** Checks whether a cached value exists for a cache key. */
|
|
74
|
+
readonly has: (key: string) => Promise<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* Direct reference to the internal axios-cache-interceptor storage instance.
|
|
77
|
+
*
|
|
78
|
+
* **DO NOT USE THIS IF YOU ARE NOT 100% SURE WHAT YOU ARE DOING.**
|
|
79
|
+
*
|
|
80
|
+
* @link https://axios-cache-interceptor.js.org/guide/storages
|
|
81
|
+
*/
|
|
82
|
+
readonly _storage: AxiosStorage;
|
|
56
83
|
};
|
|
57
84
|
|
|
58
|
-
|
|
85
|
+
interface MowojangRequestConfig {
|
|
59
86
|
validation?: ValidationOptions;
|
|
60
87
|
cache?: CacheRequestConfig<any, any>["cache"];
|
|
61
|
-
}
|
|
88
|
+
}
|
|
62
89
|
|
|
63
90
|
type MowojangError = "INVALID_PLAYER";
|
|
64
91
|
|
|
@@ -73,18 +100,18 @@ type MowojangResponse<D, E extends MowojangError | undefined> = Promise<
|
|
|
73
100
|
}
|
|
74
101
|
>;
|
|
75
102
|
|
|
76
|
-
|
|
103
|
+
interface MowojangProfile {
|
|
77
104
|
UUID: UUID;
|
|
78
105
|
username: Username;
|
|
79
|
-
}
|
|
106
|
+
}
|
|
80
107
|
|
|
81
|
-
|
|
108
|
+
interface MowojangSession {
|
|
82
109
|
UUID: UUID;
|
|
83
110
|
username: Username;
|
|
84
111
|
skin: MowojangSkin;
|
|
85
112
|
cape: MowojangCape;
|
|
86
113
|
actions: ("FORCED_NAME_CHANGE" | "USING_BANNED_SKIN")[];
|
|
87
|
-
}
|
|
114
|
+
}
|
|
88
115
|
|
|
89
116
|
type MowojangSkin = null | {
|
|
90
117
|
url: string;
|
|
@@ -104,6 +131,9 @@ declare class Client {
|
|
|
104
131
|
private axios;
|
|
105
132
|
private validation;
|
|
106
133
|
private baseURL;
|
|
134
|
+
readonly cache: MowojangCache;
|
|
135
|
+
readonly version: string;
|
|
136
|
+
static readonly version: string;
|
|
107
137
|
/**
|
|
108
138
|
* Creates a new Mowojang Client Instance
|
|
109
139
|
*
|
|
@@ -125,8 +155,16 @@ declare class Client {
|
|
|
125
155
|
constructor(clientOptions?: ClientOptions);
|
|
126
156
|
private shouldValidate;
|
|
127
157
|
private getValidationMinLength;
|
|
128
|
-
private
|
|
158
|
+
private getProfileCacheKey;
|
|
159
|
+
private getSessionCacheKey;
|
|
160
|
+
private getSkinBufferCacheKey;
|
|
161
|
+
private getCapeBufferCacheKey;
|
|
129
162
|
private mirrorProfileCacheEntries;
|
|
163
|
+
private _clearCache;
|
|
164
|
+
private _setCacheKey;
|
|
165
|
+
private _getCacheKey;
|
|
166
|
+
private _delCacheKey;
|
|
167
|
+
private _hasCacheKey;
|
|
130
168
|
/**
|
|
131
169
|
* Returns an Array of Player Profiles consisting of their Usernames and UUIDs
|
|
132
170
|
*
|
|
@@ -259,4 +297,4 @@ declare const cache: {
|
|
|
259
297
|
buildMemoryStorage: typeof buildMemoryStorage;
|
|
260
298
|
};
|
|
261
299
|
|
|
262
|
-
export { type AxiosOptions, Client, type ClientOptions, type MowojangCape, type MowojangError, type MowojangProfile, type MowojangRequestConfig, type MowojangResponse, type MowojangSession, type MowojangSkin, type Player, type UUID, type Username, type ValidationOptions, cache, utils, validate };
|
|
300
|
+
export { type AxiosOptions, Client, type ClientOptions, type MowojangCache, type MowojangCape, type MowojangError, type MowojangProfile, type MowojangRequestConfig, type MowojangResponse, type MowojangSession, type MowojangSkin, type Player, type UUID, type Username, type ValidationOptions, cache, utils, validate };
|
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.
|
|
5
|
+
var define_VERSION_default = ["0.7.0"];
|
|
6
6
|
|
|
7
7
|
// src/utils.ts
|
|
8
8
|
var undashUUID = /* @__PURE__ */ __name((UUID) => UUID.replace(/-/g, "").toLowerCase(), "undashUUID");
|
|
@@ -97,7 +97,7 @@ var axiosInstance_default = /* @__PURE__ */ __name((options, logger) => {
|
|
|
97
97
|
return instance;
|
|
98
98
|
}, "default");
|
|
99
99
|
|
|
100
|
-
// node_modules/.pnpm/@jsr+pixelic__logger@0.
|
|
100
|
+
// node_modules/.pnpm/@jsr+pixelic__logger@0.3.0/node_modules/@jsr/pixelic__logger/src/index.js
|
|
101
101
|
var ANSI = {
|
|
102
102
|
reset: "\x1B[0m",
|
|
103
103
|
gray: "\x1B[90m",
|
|
@@ -123,11 +123,13 @@ var Logger = class {
|
|
|
123
123
|
__name(this, "Logger");
|
|
124
124
|
}
|
|
125
125
|
minLevel = "INFO";
|
|
126
|
+
services;
|
|
126
127
|
/**
|
|
127
128
|
* @constructor Creates a Logger Instance
|
|
128
129
|
*/
|
|
129
130
|
constructor(config) {
|
|
130
131
|
if (config?.minLevel) this.minLevel = config.minLevel;
|
|
132
|
+
this.services = config?.services ?? [];
|
|
131
133
|
}
|
|
132
134
|
/**
|
|
133
135
|
* @private Checks whether the given Log Level satisfies the set `minLevel` of the constructor
|
|
@@ -235,6 +237,9 @@ var Client = class {
|
|
|
235
237
|
axios;
|
|
236
238
|
validation;
|
|
237
239
|
baseURL;
|
|
240
|
+
cache;
|
|
241
|
+
version = define_VERSION_default[0];
|
|
242
|
+
static version = define_VERSION_default[0];
|
|
238
243
|
/**
|
|
239
244
|
* Creates a new Mowojang Client Instance
|
|
240
245
|
*
|
|
@@ -258,17 +263,36 @@ var Client = class {
|
|
|
258
263
|
this.validation = clientOptions?.validation ?? {};
|
|
259
264
|
this.baseURL = (clientOptions?.baseURL ?? "https://mowojang.matdoes.dev").toLowerCase();
|
|
260
265
|
this.axios = axiosInstance_default({ fallback: true, ...clientOptions }, this.logger);
|
|
266
|
+
this.cache = {
|
|
267
|
+
clear: /* @__PURE__ */ __name(async () => await this._clearCache(), "clear"),
|
|
268
|
+
set: /* @__PURE__ */ __name(async (key, value) => await this._setCacheKey(key, value), "set"),
|
|
269
|
+
get: /* @__PURE__ */ __name(async (key) => await this._getCacheKey(key), "get"),
|
|
270
|
+
del: /* @__PURE__ */ __name(async (key) => await this._delCacheKey(key), "del"),
|
|
271
|
+
has: /* @__PURE__ */ __name(async (key) => await this._hasCacheKey(key), "has"),
|
|
272
|
+
_storage: this.axios.storage
|
|
273
|
+
};
|
|
261
274
|
}
|
|
262
275
|
shouldValidate(config) {
|
|
263
|
-
return Boolean(this.validation?.enabled
|
|
276
|
+
return Boolean(this.validation?.enabled ?? config?.validation?.enabled);
|
|
264
277
|
}
|
|
265
278
|
getValidationMinLength(config) {
|
|
266
279
|
return this.validation?.minimumUsernameLength ?? config?.validation?.minimumUsernameLength;
|
|
267
280
|
}
|
|
268
|
-
|
|
281
|
+
getProfileCacheKey(player) {
|
|
269
282
|
const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
|
|
270
283
|
return `profile:${normalizedPlayer}`;
|
|
271
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}`;
|
|
295
|
+
}
|
|
272
296
|
async mirrorProfileCacheEntries(ID, username, UUID, internalId) {
|
|
273
297
|
try {
|
|
274
298
|
const storage = this.axios.storage;
|
|
@@ -282,8 +306,8 @@ var Client = class {
|
|
|
282
306
|
}
|
|
283
307
|
const value = await storage.get(ID);
|
|
284
308
|
if (!value) return;
|
|
285
|
-
const usernameID = this.
|
|
286
|
-
const uuidID = this.
|
|
309
|
+
const usernameID = this.getProfileCacheKey(username);
|
|
310
|
+
const uuidID = this.getProfileCacheKey(UUID);
|
|
287
311
|
if (ID !== usernameID) {
|
|
288
312
|
await storage.set(usernameID, value);
|
|
289
313
|
this.logger.debug("Mowojang", `PROFILE-CACHE-MIRROR-SET ${ID} -> ${usernameID}`, internalId);
|
|
@@ -296,6 +320,54 @@ var Client = class {
|
|
|
296
320
|
this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error", internalId);
|
|
297
321
|
}
|
|
298
322
|
}
|
|
323
|
+
async _clearCache() {
|
|
324
|
+
try {
|
|
325
|
+
if (this.axios.storage.clear) {
|
|
326
|
+
return await this.axios.storage.clear();
|
|
327
|
+
}
|
|
328
|
+
} catch (error) {
|
|
329
|
+
this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
async _setCacheKey(key, value) {
|
|
333
|
+
try {
|
|
334
|
+
if (this.axios.storage.set) {
|
|
335
|
+
return await this.axios.storage.set(key, value);
|
|
336
|
+
}
|
|
337
|
+
} catch (error) {
|
|
338
|
+
this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
async _getCacheKey(key) {
|
|
342
|
+
try {
|
|
343
|
+
if (this.axios.storage.get) {
|
|
344
|
+
return await this.axios.storage.get(key);
|
|
345
|
+
}
|
|
346
|
+
} catch (error) {
|
|
347
|
+
this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
|
|
348
|
+
}
|
|
349
|
+
return { state: "empty" };
|
|
350
|
+
}
|
|
351
|
+
async _delCacheKey(key) {
|
|
352
|
+
try {
|
|
353
|
+
if (this.axios.storage.remove) {
|
|
354
|
+
return await this.axios.storage.remove(key);
|
|
355
|
+
}
|
|
356
|
+
} catch (error) {
|
|
357
|
+
this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async _hasCacheKey(key) {
|
|
361
|
+
try {
|
|
362
|
+
if (this.axios.storage.get) {
|
|
363
|
+
const value = await this.axios.storage.get(key);
|
|
364
|
+
return value.state === "cached" && "data" in value;
|
|
365
|
+
}
|
|
366
|
+
} catch (error) {
|
|
367
|
+
this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
|
|
368
|
+
}
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
299
371
|
/**
|
|
300
372
|
* Returns an Array of Player Profiles consisting of their Usernames and UUIDs
|
|
301
373
|
*
|
|
@@ -357,7 +429,7 @@ var Client = class {
|
|
|
357
429
|
if (this.shouldValidate(config) && !validatePlayer(player, this.getValidationMinLength(config)))
|
|
358
430
|
return { data: null, error: "INVALID_INPUT" };
|
|
359
431
|
const fetchResponse = await this.axios.get(`${this.baseURL}/${player}`, {
|
|
360
|
-
id: this.
|
|
432
|
+
id: this.getProfileCacheKey(player),
|
|
361
433
|
cache: config?.cache ?? { ttl: 60 * 60 * 1e3 }
|
|
362
434
|
});
|
|
363
435
|
if (config?.cache !== false && (typeof config?.cache !== "object" || config.cache.enabled !== false)) {
|
|
@@ -456,6 +528,7 @@ var Client = class {
|
|
|
456
528
|
const UUID = validateUUID(player) ? undashUUID(player) : await this.getUUID(player);
|
|
457
529
|
if (!UUID) return { data: null, error: "INVALID_PLAYER" };
|
|
458
530
|
return await this.axios.get(`${this.baseURL}/session/minecraft/profile/${UUID}`, {
|
|
531
|
+
id: this.getSessionCacheKey(player),
|
|
459
532
|
cache: config?.cache ?? { ttl: 15 * 60 * 1e3 }
|
|
460
533
|
}).then((fetchResponse) => {
|
|
461
534
|
let textures = fetchResponse.data.properties.find((property) => property.name === "textures").value;
|
|
@@ -525,6 +598,7 @@ var Client = class {
|
|
|
525
598
|
const session = await this.getSession(player, config);
|
|
526
599
|
if (session.error || !session.data.skin) return null;
|
|
527
600
|
const fetchResponse = await this.axios.get(session.data.skin.url, {
|
|
601
|
+
id: this.getSkinBufferCacheKey(player),
|
|
528
602
|
responseType: "arraybuffer",
|
|
529
603
|
cache: config?.cache ?? { ttl: 24 * 60 * 60 * 1e3 }
|
|
530
604
|
});
|
|
@@ -559,6 +633,7 @@ var Client = class {
|
|
|
559
633
|
const session = await this.getSession(player, config);
|
|
560
634
|
if (session.error || !session.data.cape) return null;
|
|
561
635
|
const fetchResponse = await this.axios.get(session.data.cape.url, {
|
|
636
|
+
id: this.getCapeBufferCacheKey(session.data.cape.hash),
|
|
562
637
|
responseType: "arraybuffer",
|
|
563
638
|
cache: config?.cache ?? { ttl: 30 * 24 * 60 * 60 * 1e3 }
|
|
564
639
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mowojang",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A simple TypeScript compatible wrapper for the Mowojang-API.",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -32,15 +32,23 @@
|
|
|
32
32
|
"axios-cache-interceptor": "^1.12.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@
|
|
35
|
+
"@eslint/js": "^10.0.1",
|
|
36
|
+
"@pixelic/logger": "npm:@jsr/pixelic__logger@^0.3.0",
|
|
36
37
|
"@types/node": "^26.1.2",
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^8.66.0",
|
|
39
|
+
"@typescript-eslint/parser": "^8.66.0",
|
|
40
|
+
"eslint": "^10.8.0",
|
|
41
|
+
"eslint-config-prettier": "^10.1.8",
|
|
42
|
+
"eslint-plugin-prettier": "^5.5.6",
|
|
43
|
+
"prettier": "^3.9.6",
|
|
37
44
|
"tsup": "^8.5.1",
|
|
38
|
-
"typescript": "^6.0.3"
|
|
45
|
+
"typescript": "^6.0.3",
|
|
46
|
+
"typescript-eslint": "^8.66.0"
|
|
39
47
|
},
|
|
40
48
|
"scripts": {
|
|
41
|
-
"build": "tsup",
|
|
49
|
+
"build": "pnpm tsup",
|
|
42
50
|
"test": "node test test/index.js",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
51
|
+
"lint:check": "pnpm eslint .",
|
|
52
|
+
"lint:fix": "pnpm eslint . --fix"
|
|
45
53
|
}
|
|
46
54
|
}
|