@hadoku/prefs-client 1.1.31 → 1.1.32
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/people.d.ts +9 -3
- package/dist/people.js +8 -2
- package/package.json +2 -2
package/dist/people.d.ts
CHANGED
|
@@ -13,8 +13,11 @@ import './cosmetics-contract-CKe1lNNV.js';
|
|
|
13
13
|
* shows within the minute. `fresh` skips the cache (the person has just changed theirs).
|
|
14
14
|
* - **An id asked twice at once is asked once.** A burst of joins after a restart waits on the
|
|
15
15
|
* lookup already in flight for an id rather than starting another.
|
|
16
|
-
* - **A failure answers
|
|
17
|
-
*
|
|
16
|
+
* - **A failure answers with the last answer it had**, however old (within `staleMs`, a day by
|
|
17
|
+
* default), and nobody it never had one for; the failure is not cached, so an outage recovers
|
|
18
|
+
* on the next ask. It is reported (`onError`), never thrown: a missing look costs a face its
|
|
19
|
+
* dressing, not a join. Without the old answer, one slow lookup a minute after the last drew a
|
|
20
|
+
* whole sign-up list as initials until the next one succeeded.
|
|
18
21
|
* - **No key, no lookups** (unless `keyless`, for an e2e stand-in that answers without one).
|
|
19
22
|
*
|
|
20
23
|
* The User-Agent is the caller's: hadoku.me's WAF challenges a bare client one. Each lookup gives
|
|
@@ -32,6 +35,8 @@ interface PeopleLookupOptions {
|
|
|
32
35
|
keyless?: boolean;
|
|
33
36
|
/** How long an answer is kept. Default a minute. */
|
|
34
37
|
ttlMs?: number;
|
|
38
|
+
/** How long past its ttl an answer still stands in for a lookup that failed. Default a day. */
|
|
39
|
+
staleMs?: number;
|
|
35
40
|
/** How long one lookup may take. Default 5 s. */
|
|
36
41
|
timeoutMs?: number;
|
|
37
42
|
/** Told of a failed lookup, with the ids it was for. */
|
|
@@ -42,7 +47,8 @@ interface PeopleLookupOptions {
|
|
|
42
47
|
interface PeopleLookup {
|
|
43
48
|
/**
|
|
44
49
|
* Each id prefs-api answered for, now or within the cache's minute. An id it gave no entry for
|
|
45
|
-
* (an unknown or closed account) is answered `null`; an id the lookup could not reach is
|
|
50
|
+
* (an unknown or closed account) is answered `null`; an id the lookup could not reach is answered
|
|
51
|
+
* as it last was (within `staleMs`), and is absent when it never was.
|
|
46
52
|
*/
|
|
47
53
|
resolve(userIds: readonly string[], opts?: {
|
|
48
54
|
fresh?: boolean;
|
package/dist/people.js
CHANGED
|
@@ -38,12 +38,14 @@ async function resolveAvatars(opts, userIds) {
|
|
|
38
38
|
|
|
39
39
|
// src/people.ts
|
|
40
40
|
var DEFAULT_TTL_MS = 6e4;
|
|
41
|
+
var DEFAULT_STALE_MS = 24 * 60 * 6e4;
|
|
41
42
|
var DEFAULT_TIMEOUT_MS = 5e3;
|
|
42
43
|
var PRUNE_AT = 1024;
|
|
43
44
|
function createPeopleLookup(opts) {
|
|
44
45
|
const now = opts.now ?? Date.now;
|
|
45
46
|
const ttl = opts.ttlMs ?? DEFAULT_TTL_MS;
|
|
46
47
|
const timeout = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
48
|
+
const staleMs = opts.staleMs ?? DEFAULT_STALE_MS;
|
|
47
49
|
const baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
48
50
|
const cache = /* @__PURE__ */ new Map();
|
|
49
51
|
const inflight = /* @__PURE__ */ new Map();
|
|
@@ -59,6 +61,10 @@ function createPeopleLookup(opts) {
|
|
|
59
61
|
const hit = cache.get(userId);
|
|
60
62
|
return hit !== void 0 && hit.expiresAt > now() ? hit.person : void 0;
|
|
61
63
|
};
|
|
64
|
+
const last = (userId) => {
|
|
65
|
+
const hit = cache.get(userId);
|
|
66
|
+
return hit !== void 0 && hit.expiresAt + staleMs > now() ? hit.person : void 0;
|
|
67
|
+
};
|
|
62
68
|
const lookup = (ids) => {
|
|
63
69
|
const run = (async () => {
|
|
64
70
|
try {
|
|
@@ -69,7 +75,7 @@ function createPeopleLookup(opts) {
|
|
|
69
75
|
const at = now();
|
|
70
76
|
for (const id of ids) cache.set(id, { person: answer[id] ?? null, expiresAt: at + ttl });
|
|
71
77
|
if (cache.size > PRUNE_AT) {
|
|
72
|
-
for (const [k, e] of cache) if (e.expiresAt <= at) cache.delete(k);
|
|
78
|
+
for (const [k, e] of cache) if (e.expiresAt + staleMs <= at) cache.delete(k);
|
|
73
79
|
}
|
|
74
80
|
} catch (err) {
|
|
75
81
|
opts.onError?.(err, ids);
|
|
@@ -111,7 +117,7 @@ function createPeopleLookup(opts) {
|
|
|
111
117
|
if (missing.length > 0) waits.push(lookup(missing));
|
|
112
118
|
await Promise.all(waits);
|
|
113
119
|
for (const id of wanted) {
|
|
114
|
-
const hit =
|
|
120
|
+
const hit = last(id);
|
|
115
121
|
if (hit !== void 0) out.set(id, hit);
|
|
116
122
|
}
|
|
117
123
|
return out;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/prefs-client",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "1.1.32",
|
|
4
|
+
"gitHead": "cc5be66d5c8a25721efb205a9a208b544dd55aa7",
|
|
5
5
|
"description": "Client SDK for the hadoku unified preferences store. Wraps /prefs/api/v1/* with localStorage caching, debounced writes, Zod-validated reads, and a usePrefs() React hook, plus match history, ratings and leaderboards for every game (./games), the one resolved avatar per user (./avatar), and the avatar cosmetics renderer (./cosmetics).",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|