warframe-nexus-query 2.2.0 → 2.2.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/index.js +7 -7
- package/lib/market/v2/MarketFetcherV2.js +31 -10
- package/lib/market/v2/models/Item.js +1 -1
- package/lib/market/v2/utils/cache.js +103 -1
- package/package.json +3 -6
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { readdir, unlink, rmdir } from 'node:fs/promises';
|
|
2
|
+
import { existsSync as exists } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
4
|
|
|
5
5
|
import md from 'node-md-config';
|
|
6
6
|
|
|
@@ -138,13 +138,13 @@ export default class PriceCheckQuerier {
|
|
|
138
138
|
this.logger.log('no market fetcher, or skipMarket was true');
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
if (
|
|
142
|
-
const files = await
|
|
141
|
+
if (exists(`${global.__basedir}/tmp`)) {
|
|
142
|
+
const files = await readdir(`${global.__basedir}/tmp`);
|
|
143
143
|
let allSuccess = true;
|
|
144
144
|
await Promise.all(
|
|
145
145
|
files.map(async (file) => {
|
|
146
146
|
try {
|
|
147
|
-
await
|
|
147
|
+
await unlink(join(global.__basedir, 'tmp', file));
|
|
148
148
|
} catch (e) {
|
|
149
149
|
allSuccess = false;
|
|
150
150
|
this.logger.debug(`Couldn't delete ${file}`);
|
|
@@ -153,7 +153,7 @@ export default class PriceCheckQuerier {
|
|
|
153
153
|
);
|
|
154
154
|
|
|
155
155
|
if (allSuccess) {
|
|
156
|
-
await
|
|
156
|
+
await rmdir(`${global.__basedir}/tmp`);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
}
|
|
@@ -58,10 +58,19 @@ export default class MarketFetcherV2 {
|
|
|
58
58
|
* @returns {Promise<Item[]>}
|
|
59
59
|
*/
|
|
60
60
|
async getItems() {
|
|
61
|
-
|
|
61
|
+
const cachedItems = await this.#cache.get('items', 'items', async () => {
|
|
62
62
|
const data = await this.#http.get('/items', { locale: this.locale });
|
|
63
|
-
|
|
63
|
+
// Store raw data, not Item instances, to support persistence
|
|
64
|
+
return data;
|
|
64
65
|
});
|
|
66
|
+
|
|
67
|
+
// If cached items are already Item instances, return them
|
|
68
|
+
if (cachedItems.length > 0 && cachedItems[0] instanceof Item) {
|
|
69
|
+
return cachedItems;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Otherwise, reconstruct Item instances from raw data
|
|
73
|
+
return cachedItems.map((item) => new Item(item, this.locale));
|
|
65
74
|
}
|
|
66
75
|
|
|
67
76
|
/**
|
|
@@ -71,10 +80,19 @@ export default class MarketFetcherV2 {
|
|
|
71
80
|
*/
|
|
72
81
|
async getItemBySlug(slug) {
|
|
73
82
|
const cacheKey = `item_${slug}`;
|
|
74
|
-
|
|
83
|
+
const cachedItem = await this.#cache.get(cacheKey, 'items', async () => {
|
|
75
84
|
const data = await this.#http.get(`/item/${slug}`, { locale: this.locale });
|
|
76
|
-
|
|
85
|
+
// Store raw data, not Item instance, to support persistence
|
|
86
|
+
return data;
|
|
77
87
|
});
|
|
88
|
+
|
|
89
|
+
// If cached item is already an Item instance, return it
|
|
90
|
+
if (cachedItem instanceof Item) {
|
|
91
|
+
return cachedItem;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Otherwise, reconstruct Item instance from raw data
|
|
95
|
+
return new Item(cachedItem, this.locale);
|
|
78
96
|
}
|
|
79
97
|
|
|
80
98
|
/**
|
|
@@ -235,12 +253,12 @@ export default class MarketFetcherV2 {
|
|
|
235
253
|
// ==========================================================================
|
|
236
254
|
|
|
237
255
|
/**
|
|
238
|
-
* Query market for item (
|
|
256
|
+
* Query market for item (v1 compatibility method)
|
|
239
257
|
* @param {string} query - Search query
|
|
240
|
-
* @param {Object} options -
|
|
258
|
+
* @param {Object} options - Query options
|
|
241
259
|
* @param {string} options.platform - Platform
|
|
242
|
-
* @param {function} [options.successfulQuery] - Callback
|
|
243
|
-
* @returns {Promise<
|
|
260
|
+
* @param {function} [options.successfulQuery] - Callback on success
|
|
261
|
+
* @returns {Promise<Summary[]>}
|
|
244
262
|
*/
|
|
245
263
|
async queryMarket(query, options = {}) {
|
|
246
264
|
const { platform, successfulQuery } = options;
|
|
@@ -264,8 +282,11 @@ export default class MarketFetcherV2 {
|
|
|
264
282
|
throw new Error(`No items found for query: ${query}`);
|
|
265
283
|
}
|
|
266
284
|
|
|
267
|
-
// Use first match
|
|
268
|
-
const
|
|
285
|
+
// Use first match - but only use slug for now
|
|
286
|
+
const matchedSlug = matches[0].slug;
|
|
287
|
+
|
|
288
|
+
// Fetch full item data with all properties (tradingTax, ducats, etc.)
|
|
289
|
+
const item = await this.getItemBySlug(matchedSlug);
|
|
269
290
|
|
|
270
291
|
// Get top orders for the item
|
|
271
292
|
const topOrders = await this.getTopOrders(item.slug, { platform });
|
|
@@ -44,7 +44,7 @@ export default class Item {
|
|
|
44
44
|
this.locale = locale;
|
|
45
45
|
|
|
46
46
|
// Extract localized fields for convenience
|
|
47
|
-
const localizedData = this.i18n[locale]
|
|
47
|
+
const localizedData = this.i18n[locale] ?? this.i18n.en ?? {};
|
|
48
48
|
this.name = localizedData.name || this.slug;
|
|
49
49
|
this.description = localizedData.description;
|
|
50
50
|
this.wikiLink = localizedData.wikiLink;
|
|
@@ -1,20 +1,98 @@
|
|
|
1
|
+
import flatCache from 'flat-cache';
|
|
2
|
+
|
|
1
3
|
import { API_BASE_URL, VERSION_CHECK_INTERVAL, CACHE_TTL } from '../constants.js';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Version-based cache system for Warframe Market API v2
|
|
5
7
|
* Uses version hashes from /v2/versions endpoint to invalidate cache
|
|
8
|
+
* Backs up to persistent storage using flat-cache
|
|
6
9
|
*/
|
|
7
10
|
export default class VersionedCache {
|
|
8
11
|
#cache;
|
|
9
12
|
#versions;
|
|
10
13
|
#lastVersionCheck;
|
|
14
|
+
#persistentCache;
|
|
15
|
+
#cacheId;
|
|
11
16
|
|
|
12
|
-
constructor({ maxSize = 100, ttl = CACHE_TTL } = {}) {
|
|
17
|
+
constructor({ maxSize = 100, ttl = CACHE_TTL, cacheId = 'warframe-market-v2', persistent = true } = {}) {
|
|
13
18
|
this.#cache = new Map();
|
|
14
19
|
this.maxSize = maxSize;
|
|
15
20
|
this.ttl = ttl;
|
|
16
21
|
this.#versions = undefined;
|
|
17
22
|
this.#lastVersionCheck = undefined;
|
|
23
|
+
this.#cacheId = cacheId;
|
|
24
|
+
|
|
25
|
+
// Initialize persistent cache
|
|
26
|
+
if (persistent) {
|
|
27
|
+
this.#persistentCache = flatCache.create(cacheId);
|
|
28
|
+
this.#loadFromDisk();
|
|
29
|
+
} else {
|
|
30
|
+
this.#persistentCache = undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Load cache from disk into memory
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
#loadFromDisk() {
|
|
39
|
+
if (!this.#persistentCache) return;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const keys = this.#persistentCache.keys();
|
|
43
|
+
|
|
44
|
+
// Load all cached items
|
|
45
|
+
keys.forEach((key) => {
|
|
46
|
+
const item = this.#persistentCache.getKey(key);
|
|
47
|
+
if (item) {
|
|
48
|
+
// Check if item is still valid (TTL not expired)
|
|
49
|
+
if (item.timestamp && Date.now() - item.timestamp <= this.ttl) {
|
|
50
|
+
this.#cache.set(key, item);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Load versions data
|
|
56
|
+
const versionsData = this.#persistentCache.getKey('_versions_metadata');
|
|
57
|
+
if (versionsData) {
|
|
58
|
+
this.#versions = versionsData.versions;
|
|
59
|
+
this.#lastVersionCheck = versionsData.lastVersionCheck;
|
|
60
|
+
}
|
|
61
|
+
} catch (error) {
|
|
62
|
+
// If loading fails, start with empty cache
|
|
63
|
+
// eslint-disable-next-line no-console
|
|
64
|
+
console.warn('Failed to load cache from disk:', error.message);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Save cache to disk
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
#saveToDisk() {
|
|
73
|
+
if (!this.#persistentCache) return;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
// Save all cache entries
|
|
77
|
+
Array.from(this.#cache.entries()).forEach(([key, value]) => {
|
|
78
|
+
this.#persistentCache.setKey(key, value);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Save versions metadata
|
|
82
|
+
if (this.#versions || this.#lastVersionCheck) {
|
|
83
|
+
this.#persistentCache.setKey('_versions_metadata', {
|
|
84
|
+
versions: this.#versions,
|
|
85
|
+
lastVersionCheck: this.#lastVersionCheck,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Persist to disk
|
|
90
|
+
this.#persistentCache.save(true);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
// If saving fails, log but don't crash
|
|
93
|
+
// eslint-disable-next-line no-console
|
|
94
|
+
console.warn('Failed to save cache to disk:', error.message);
|
|
95
|
+
}
|
|
18
96
|
}
|
|
19
97
|
|
|
20
98
|
/**
|
|
@@ -53,6 +131,11 @@ export default class VersionedCache {
|
|
|
53
131
|
if (this.#cache.size >= this.maxSize) {
|
|
54
132
|
const firstKey = this.#cache.keys().next().value;
|
|
55
133
|
this.#cache.delete(firstKey);
|
|
134
|
+
|
|
135
|
+
// Remove from persistent cache too
|
|
136
|
+
if (this.#persistentCache) {
|
|
137
|
+
this.#persistentCache.removeKey(firstKey);
|
|
138
|
+
}
|
|
56
139
|
}
|
|
57
140
|
|
|
58
141
|
this.#cache.set(key, {
|
|
@@ -72,6 +155,9 @@ export default class VersionedCache {
|
|
|
72
155
|
});
|
|
73
156
|
}
|
|
74
157
|
}
|
|
158
|
+
|
|
159
|
+
// Persist to disk
|
|
160
|
+
this.#saveToDisk();
|
|
75
161
|
}
|
|
76
162
|
|
|
77
163
|
/**
|
|
@@ -143,6 +229,9 @@ export default class VersionedCache {
|
|
|
143
229
|
this.#versions = await response.json();
|
|
144
230
|
this.#lastVersionCheck = Date.now();
|
|
145
231
|
|
|
232
|
+
// Persist versions to disk
|
|
233
|
+
this.#saveToDisk();
|
|
234
|
+
|
|
146
235
|
return this.#versions;
|
|
147
236
|
} catch (error) {
|
|
148
237
|
// If fetch fails and we have cached versions, return them
|
|
@@ -160,6 +249,13 @@ export default class VersionedCache {
|
|
|
160
249
|
this.#cache.clear();
|
|
161
250
|
this.#versions = undefined;
|
|
162
251
|
this.#lastVersionCheck = undefined;
|
|
252
|
+
|
|
253
|
+
// Clear persistent cache
|
|
254
|
+
if (this.#persistentCache) {
|
|
255
|
+
this.#persistentCache.destroy();
|
|
256
|
+
// Recreate the cache file (creates new empty cache)
|
|
257
|
+
this.#persistentCache = flatCache.create(this.#cacheId);
|
|
258
|
+
}
|
|
163
259
|
}
|
|
164
260
|
|
|
165
261
|
/**
|
|
@@ -168,6 +264,12 @@ export default class VersionedCache {
|
|
|
168
264
|
*/
|
|
169
265
|
delete(key) {
|
|
170
266
|
this.#cache.delete(key);
|
|
267
|
+
|
|
268
|
+
// Remove from persistent cache
|
|
269
|
+
if (this.#persistentCache) {
|
|
270
|
+
this.#persistentCache.removeKey(key);
|
|
271
|
+
this.#persistentCache.save(true);
|
|
272
|
+
}
|
|
171
273
|
}
|
|
172
274
|
|
|
173
275
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "warframe-nexus-query",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "Query prices form nexus-stats.com and warframe.market.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"warframe",
|
|
@@ -30,16 +30,14 @@
|
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build:docs": "jsdoc -c jsdoc-config.json -d docs",
|
|
33
|
-
"coverage": "npm test && c8 report --reporter=text-lcov | coveralls",
|
|
33
|
+
"coverage": "npm test && c8 report --reporter=text-lcov | npx coveralls",
|
|
34
34
|
"lint": "eslint index.js lib/ test/",
|
|
35
35
|
"lint:fix": "eslint index.js lib/ test/ --fix",
|
|
36
36
|
"prepare": "husky && npx install-peerdeps @wfcd/eslint-config@latest -S",
|
|
37
37
|
"test": "c8 mocha"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"
|
|
41
|
-
"image-downloader": "^4.0.2",
|
|
42
|
-
"json-fetch-cache": "^1.2.6",
|
|
40
|
+
"flat-cache": "^6.1.19",
|
|
43
41
|
"json-query": "^2.2.2",
|
|
44
42
|
"node-md-config": "^2.0.1",
|
|
45
43
|
"numeral": "^2.0.6"
|
|
@@ -51,7 +49,6 @@
|
|
|
51
49
|
"@wfcd/eslint-config": "^1.6.1",
|
|
52
50
|
"c8": "^10.1.2",
|
|
53
51
|
"chai": "^6.0.1",
|
|
54
|
-
"coveralls": "^3.1.1",
|
|
55
52
|
"husky": "^9.0.11",
|
|
56
53
|
"install-peerdeps": "^3.0.3",
|
|
57
54
|
"lint-staged": "^16.0.0",
|