@oxyhq/core 18.0.0 → 19.0.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.
Files changed (43) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/i18n/locales/en-US.json +48 -5
  3. package/dist/cjs/i18n/locales/es-ES.json +48 -5
  4. package/dist/cjs/i18n/locales/locales/en-US.json +48 -5
  5. package/dist/cjs/i18n/locales/locales/es-ES.json +48 -5
  6. package/dist/cjs/index.js +10 -6
  7. package/dist/cjs/mixins/OxyServices.accounts.js +27 -2
  8. package/dist/cjs/mixins/OxyServices.user.js +14 -20
  9. package/dist/cjs/server/index.js +8 -2
  10. package/dist/cjs/server/userInvalidation.js +6 -28
  11. package/dist/cjs/utils/identityCacheSweep.js +97 -0
  12. package/dist/esm/.tsbuildinfo +1 -1
  13. package/dist/esm/i18n/locales/en-US.json +48 -5
  14. package/dist/esm/i18n/locales/es-ES.json +48 -5
  15. package/dist/esm/i18n/locales/locales/en-US.json +48 -5
  16. package/dist/esm/i18n/locales/locales/es-ES.json +48 -5
  17. package/dist/esm/index.js +1 -1
  18. package/dist/esm/mixins/OxyServices.accounts.js +22 -1
  19. package/dist/esm/mixins/OxyServices.user.js +14 -20
  20. package/dist/esm/server/index.js +5 -1
  21. package/dist/esm/server/userInvalidation.js +5 -26
  22. package/dist/esm/utils/identityCacheSweep.js +92 -0
  23. package/dist/types/.tsbuildinfo +1 -1
  24. package/dist/types/index.d.ts +2 -2
  25. package/dist/types/mixins/OxyServices.accounts.d.ts +39 -7
  26. package/dist/types/mixins/OxyServices.user.d.ts +9 -7
  27. package/dist/types/models/interfaces.d.ts +11 -3
  28. package/dist/types/server/index.d.ts +4 -2
  29. package/dist/types/server/userInvalidation.d.ts +5 -24
  30. package/dist/types/utils/identityCacheSweep.d.ts +80 -0
  31. package/package.json +2 -2
  32. package/src/i18n/locales/en-US.json +48 -5
  33. package/src/i18n/locales/es-ES.json +48 -5
  34. package/src/index.ts +8 -2
  35. package/src/mixins/OxyServices.accounts.ts +58 -7
  36. package/src/mixins/OxyServices.user.ts +14 -20
  37. package/src/mixins/__tests__/identityWriteCacheInvalidation.test.ts +370 -0
  38. package/src/models/interfaces.ts +11 -3
  39. package/src/server/__tests__/userInvalidation.test.ts +3 -20
  40. package/src/server/index.ts +5 -2
  41. package/src/server/userInvalidation.ts +8 -36
  42. package/src/utils/__tests__/identityCacheSweep.test.ts +151 -0
  43. package/src/utils/identityCacheSweep.ts +104 -0
@@ -0,0 +1,104 @@
1
+ /**
2
+ * THE enumeration of `OxyServices` GET-cache keys that can carry a single
3
+ * account's identity, and the one sweep that clears them.
4
+ *
5
+ * WHY THIS IS ONE LIST
6
+ * --------------------
7
+ * An Oxy account is readable under SEVERAL cache keys, and a write that only
8
+ * busts the key it happens to know about leaves every other one serving the
9
+ * pre-write snapshot for up to its TTL — from the caller's OWN in-memory cache,
10
+ * with a perfectly healthy server. That failure has already shipped twice with
11
+ * two different sets of keys:
12
+ *
13
+ * - `updateAccount` busted `GET:/accounts/<id>` and the account lists, but a
14
+ * profile screen reads `GET:/profiles/username/<name>` and
15
+ * `GET:/users/<id>`, so a channel's new picture stayed invisible for the
16
+ * full 5-minute profile TTL.
17
+ * - `updateProfile` busted four of the six keys below, missing
18
+ * `GET:/auth/lookup/` (the login-flow avatar/display-name lookup) and
19
+ * `GET:/profiles/resolve` (handle resolution) — two independently-drifted
20
+ * copies of a list that has to agree.
21
+ *
22
+ * So the list lives here, once, and every writer calls
23
+ * {@link evictOxyIdentityCache}. Adding a new identity read means adding its key
24
+ * HERE and every writer inherits it.
25
+ *
26
+ * WHERE THE LINE IS DRAWN
27
+ * -----------------------
28
+ * These are the SINGLE-PROFILE reads — the account is the subject of the
29
+ * response and is addressable by id, handle, or session. Reads that merely
30
+ * CONTAIN an account among many (`GET:/profiles/search`,
31
+ * `GET:/users/<other>/followers`, `GET:/profiles/<other>/similar`) are
32
+ * deliberately NOT swept: an account cannot be located in them without the very
33
+ * lookup being invalidated, so sweeping them means sweeping the whole namespace
34
+ * on every identity change — a real cost on a backend consuming the
35
+ * cross-service invalidation signal, for a surface where a stale thumbnail
36
+ * expires on its own in ~2 minutes.
37
+ *
38
+ * WHY PREFIXES RATHER THAN EXACT KEYS
39
+ * -----------------------------------
40
+ * Only the by-id key can be built from a user id. The handle-keyed and
41
+ * session-keyed entries cannot — deriving a handle from an id needs the lookup
42
+ * we are invalidating, and the SDK never tracks active session ids centrally.
43
+ * Prefix sweeping is also what makes a USERNAME CHANGE correct: the entry under
44
+ * the OLD handle is unreachable by construction (nothing in the write response
45
+ * carries it), and a sweep targeted at the new handle alone would leave the old
46
+ * one serving the pre-rename profile until its TTL. Over-eviction costs a
47
+ * refetch; under-eviction serves wrong data.
48
+ *
49
+ * Platform-neutral by construction (no imports, no `OxyServices` reference) so
50
+ * the client mixins and the Node-only `@oxyhq/core/server` invalidation
51
+ * subscriber can share it without either pulling in the other.
52
+ */
53
+
54
+ /**
55
+ * The cache-eviction surface of an `OxyServices` instance. Declared
56
+ * structurally so this module stays free of any client import.
57
+ */
58
+ export interface OxyIdentityCacheEvictor {
59
+ clearCacheEntry(key: string): void;
60
+ clearCacheByPrefix(prefix: string): number;
61
+ }
62
+
63
+ /**
64
+ * Cache-key PREFIXES under which an account's identity can be served, for the
65
+ * reads whose key cannot be derived from a user id. Swept wholesale.
66
+ */
67
+ export const OXY_IDENTITY_CACHE_PREFIXES: readonly string[] = [
68
+ // `getUserBySession` — keyed by session id, which the SDK never enumerates.
69
+ 'GET:/session/user/',
70
+ // `getCurrentUser` (and `GET:/users/me/graph`, harmlessly included).
71
+ 'GET:/users/me',
72
+ // `lookupUsername` — the pre-session login lookup; carries avatar + display name.
73
+ 'GET:/auth/lookup/',
74
+ // `getProfileByUsername` — keyed by handle, including the pre-rename handle.
75
+ 'GET:/profiles/username/',
76
+ // `resolveProfile` — keyed by fediverse handle in the query payload.
77
+ 'GET:/profiles/resolve',
78
+ ];
79
+
80
+ /**
81
+ * Build the exact cache key `getUserById` reads under. The only identity key
82
+ * derivable from a user id, so the only one that does not need a prefix sweep.
83
+ */
84
+ export function oxyUserByIdCacheKey(userId: string): string {
85
+ return `GET:/users/${userId}`;
86
+ }
87
+
88
+ /**
89
+ * Sweep an `OxyServices` GET response cache of everything that could carry the
90
+ * given account's identity.
91
+ *
92
+ * @param oxy - Anything exposing the SDK's two eviction methods.
93
+ * @param userId - The account whose by-id entry to drop. Optional: a caller
94
+ * that does not know the id still clears every handle-, session-
95
+ * and self-keyed entry, which is the majority of the surface.
96
+ */
97
+ export function evictOxyIdentityCache(oxy: OxyIdentityCacheEvictor, userId?: string): void {
98
+ for (const prefix of OXY_IDENTITY_CACHE_PREFIXES) {
99
+ oxy.clearCacheByPrefix(prefix);
100
+ }
101
+ if (userId) {
102
+ oxy.clearCacheEntry(oxyUserByIdCacheKey(userId));
103
+ }
104
+ }