@atproto/identity 0.5.18 → 0.5.19

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atproto/identity
2
2
 
3
+ ## 0.5.19
4
+
5
+ ### Patch Changes
6
+
7
+ - [#5707](https://github.com/bluesky-social/atproto/pull/5707) [`8eac4b0`](https://github.com/bluesky-social/atproto/commit/8eac4b0c0e11170171a1978123078f0a0b17ddd3) Thanks [@foysalit](https://github.com/foysalit)! - Use a bounded in-memory DID cache with configurable Ozone cache limits.
8
+
3
9
  ## 0.5.18
4
10
 
5
11
  ### Patch Changes
@@ -2,17 +2,37 @@ import type { CacheResult, DidCache, DidDocument } from '../types.js';
2
2
  type CacheVal = {
3
3
  doc: DidDocument;
4
4
  updatedAt: number;
5
+ size: number;
5
6
  };
7
+ /** Retention limits. Setting any limit to zero disables retention. */
8
+ export type MemoryCacheOptions = {
9
+ /** Maximum number of cached documents. Defaults to 10,000. */
10
+ maxEntries?: number;
11
+ /** Maximum UTF-8 bytes of serialized documents and DID keys. Defaults to 32 MiB. */
12
+ maxBytes?: number;
13
+ /** Maximum UTF-8 bytes per serialized document. Defaults to 64 KiB. */
14
+ maxDocBytes?: number;
15
+ };
16
+ /**
17
+ * A bounded LRU cache. Oversized documents can be resolved but are not retained.
18
+ * Treat cached documents as immutable so their measured sizes remain accurate.
19
+ */
6
20
  export declare class MemoryCache implements DidCache {
7
21
  staleTTL: number;
8
22
  maxTTL: number;
9
- constructor(staleTTL?: number, maxTTL?: number);
23
+ readonly maxEntries: number;
24
+ readonly maxBytes: number;
25
+ readonly maxDocBytes: number;
26
+ private bytes;
27
+ constructor(staleTTL?: number, maxTTL?: number, opts?: MemoryCacheOptions);
28
+ /** Use the DidCache methods to mutate entries and maintain size accounting. */
10
29
  cache: Map<string, CacheVal>;
11
30
  cacheDid(did: string, doc: DidDocument): Promise<void>;
12
31
  refreshCache(did: string, getDoc: () => Promise<DidDocument | null>): Promise<void>;
13
32
  checkCache(did: string): Promise<CacheResult | null>;
14
33
  clearEntry(did: string): Promise<void>;
15
34
  clear(): Promise<void>;
35
+ private remove;
16
36
  }
17
37
  export {};
18
38
  //# sourceMappingURL=memory-cache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"memory-cache.d.ts","sourceRoot":"","sources":["../../src/did/memory-cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAErE,KAAK,QAAQ,GAAG;IACd,GAAG,EAAE,WAAW,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,qBAAa,WAAY,YAAW,QAAQ;IACnC,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACrB,YAAY,QAAQ,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAG7C;IAEM,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAY;IAEzC,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3D;IAEK,YAAY,CAChB,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,GACxC,OAAO,CAAC,IAAI,CAAC,CAKf;IAEK,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAYzD;IAEK,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3C;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3B;CACF"}
1
+ {"version":3,"file":"memory-cache.d.ts","sourceRoot":"","sources":["../../src/did/memory-cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,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,IAAI,CAAC,CAG3B;IAED,OAAO,CAAC,MAAM;CAMf"}
@@ -1,12 +1,52 @@
1
1
  import { DAY, HOUR } from '@atproto/common-web';
2
+ const encoder = new TextEncoder();
3
+ /**
4
+ * A bounded LRU cache. Oversized documents can be resolved but are not retained.
5
+ * Treat cached documents as immutable so their measured sizes remain accurate.
6
+ */
2
7
  export class MemoryCache {
3
- constructor(staleTTL, maxTTL) {
8
+ constructor(staleTTL, maxTTL, opts = {}) {
9
+ this.bytes = 0;
10
+ /** Use the DidCache methods to mutate entries and maintain size accounting. */
4
11
  this.cache = new Map();
5
12
  this.staleTTL = staleTTL ?? HOUR;
6
13
  this.maxTTL = maxTTL ?? DAY;
14
+ this.maxEntries = opts.maxEntries ?? 10_000;
15
+ this.maxBytes = opts.maxBytes ?? 32 * 1024 * 1024;
16
+ this.maxDocBytes = opts.maxDocBytes ?? 64 * 1024;
17
+ for (const limit of [this.maxEntries, this.maxBytes, this.maxDocBytes]) {
18
+ if (!Number.isSafeInteger(limit) || limit < 0) {
19
+ throw new RangeError('Cache limits must be non-negative safe integers');
20
+ }
21
+ }
7
22
  }
8
23
  async cacheDid(did, doc) {
9
- this.cache.set(did, { doc, updatedAt: Date.now() });
24
+ // @NOTE Remove the previous version even if the replacement cannot be cached.
25
+ this.remove(did);
26
+ if (this.maxEntries === 0)
27
+ return;
28
+ let docBytes;
29
+ try {
30
+ const json = JSON.stringify(doc);
31
+ if (json === undefined)
32
+ return;
33
+ docBytes = encoder.encode(json).byteLength;
34
+ }
35
+ catch {
36
+ return;
37
+ }
38
+ const size = docBytes + encoder.encode(did).byteLength;
39
+ if (docBytes > this.maxDocBytes || size > this.maxBytes)
40
+ return;
41
+ while (this.cache.size >= this.maxEntries ||
42
+ this.bytes + size > this.maxBytes) {
43
+ const oldest = this.cache.keys().next();
44
+ if (oldest.done)
45
+ break;
46
+ this.remove(oldest.value);
47
+ }
48
+ this.cache.set(did, { doc, updatedAt: Date.now(), size });
49
+ this.bytes += size;
10
50
  }
11
51
  async refreshCache(did, getDoc) {
12
52
  const doc = await getDoc();
@@ -21,18 +61,34 @@ export class MemoryCache {
21
61
  const now = Date.now();
22
62
  const expired = now > val.updatedAt + this.maxTTL;
23
63
  const stale = now > val.updatedAt + this.staleTTL;
64
+ if (expired) {
65
+ this.remove(did);
66
+ }
67
+ else {
68
+ this.cache.delete(did);
69
+ this.cache.set(did, val);
70
+ }
24
71
  return {
25
- ...val,
72
+ doc: val.doc,
73
+ updatedAt: val.updatedAt,
26
74
  did,
27
75
  stale,
28
76
  expired,
29
77
  };
30
78
  }
31
79
  async clearEntry(did) {
32
- this.cache.delete(did);
80
+ this.remove(did);
33
81
  }
34
82
  async clear() {
35
83
  this.cache.clear();
84
+ this.bytes = 0;
85
+ }
86
+ remove(did) {
87
+ const val = this.cache.get(did);
88
+ if (!val)
89
+ return;
90
+ this.cache.delete(did);
91
+ this.bytes -= val.size;
36
92
  }
37
93
  }
38
94
  //# sourceMappingURL=memory-cache.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"memory-cache.js","sourceRoot":"","sources":["../../src/did/memory-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAQ/C,MAAM,OAAO,WAAW;IAGtB,YAAY,QAAiB,EAAE,MAAe;QAKvC,UAAK,GAA0B,IAAI,GAAG,EAAE,CAAA;QAJ7C,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,IAAI,CAAA;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,GAAG,CAAA;IAC7B,CAAC;IAID,KAAK,CAAC,QAAQ,CAAC,GAAW,EAAE,GAAgB;QAC1C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IACrD,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,GAAW,EACX,MAAyC;QAEzC,MAAM,GAAG,GAAG,MAAM,MAAM,EAAE,CAAA;QAC1B,IAAI,GAAG,EAAE,CAAC;YACR,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;QAC/B,CAAC;IACH,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,GAAW;QAC1B,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC/B,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAA;QACrB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QACtB,MAAM,OAAO,GAAG,GAAG,GAAG,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,CAAA;QACjD,MAAM,KAAK,GAAG,GAAG,GAAG,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAA;QACjD,OAAO;YACL,GAAG,GAAG;YACN,GAAG;YACH,KAAK;YACL,OAAO;SACR,CAAA;IACH,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,GAAW;QAC1B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACxB,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IACpB,CAAC;CACF","sourcesContent":["import { DAY, HOUR } from '@atproto/common-web'\nimport type { CacheResult, DidCache, DidDocument } from '../types.js'\n\ntype CacheVal = {\n doc: DidDocument\n updatedAt: number\n}\n\nexport class MemoryCache implements DidCache {\n public staleTTL: number\n public maxTTL: number\n constructor(staleTTL?: number, maxTTL?: number) {\n this.staleTTL = staleTTL ?? HOUR\n this.maxTTL = maxTTL ?? DAY\n }\n\n public cache: Map<string, CacheVal> = new Map()\n\n async cacheDid(did: string, doc: DidDocument): Promise<void> {\n this.cache.set(did, { doc, updatedAt: Date.now() })\n }\n\n async refreshCache(\n did: string,\n getDoc: () => Promise<DidDocument | null>,\n ): Promise<void> {\n const doc = await getDoc()\n if (doc) {\n await this.cacheDid(did, doc)\n }\n }\n\n async checkCache(did: string): Promise<CacheResult | null> {\n const val = this.cache.get(did)\n if (!val) return null\n const now = Date.now()\n const expired = now > val.updatedAt + this.maxTTL\n const stale = now > val.updatedAt + this.staleTTL\n return {\n ...val,\n did,\n stale,\n expired,\n }\n }\n\n async clearEntry(did: string): Promise<void> {\n this.cache.delete(did)\n }\n\n async clear(): Promise<void> {\n this.cache.clear()\n }\n}\n"]}
1
+ {"version":3,"file":"memory-cache.js","sourceRoot":"","sources":["../../src/did/memory-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAmB/C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC;;;GAGG;AACH,MAAM,OAAO,WAAW;IAQtB,YACE,QAAiB,EACjB,MAAe,EACf,IAAI,GAAuB,EAAE;QALvB,UAAK,GAAG,CAAC,CAAA;QAmBjB,+EAA+E;QACxE,UAAK,GAA0B,IAAI,GAAG,EAAE,CAAA;QAb7C,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,IAAI,CAAA;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,GAAG,CAAA;QAC3B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,MAAM,CAAA;QAC3C,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,EAAE,GAAG,IAAI,GAAG,IAAI,CAAA;QACjD,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,EAAE,GAAG,IAAI,CAAA;QAChD,KAAK,MAAM,KAAK,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;YACvE,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,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;IAChB,CAAC;IAEO,MAAM,CAAC,GAAW;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC/B,IAAI,CAAC,GAAG;YAAE,OAAM;QAChB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACtB,IAAI,CAAC,KAAK,IAAI,GAAG,CAAC,IAAI,CAAA;IACxB,CAAC;CACF","sourcesContent":["import { DAY, HOUR } from '@atproto/common-web'\nimport type { CacheResult, DidCache, DidDocument } from '../types.js'\n\ntype CacheVal = {\n doc: DidDocument\n updatedAt: number\n size: number\n}\n\n/** Retention limits. Setting any limit to zero disables retention. */\nexport type MemoryCacheOptions = {\n /** Maximum number of cached documents. Defaults to 10,000. */\n maxEntries?: number\n /** Maximum UTF-8 bytes of serialized documents and DID keys. Defaults to 32 MiB. */\n maxBytes?: number\n /** Maximum UTF-8 bytes per serialized document. Defaults to 64 KiB. */\n maxDocBytes?: number\n}\n\nconst encoder = new TextEncoder()\n\n/**\n * A bounded LRU cache. Oversized documents can be resolved but are not retained.\n * Treat cached documents as immutable so their measured sizes remain accurate.\n */\nexport class MemoryCache implements DidCache {\n public staleTTL: number\n public maxTTL: number\n public readonly maxEntries: number\n public readonly maxBytes: number\n public readonly maxDocBytes: number\n private bytes = 0\n\n constructor(\n staleTTL?: number,\n maxTTL?: number,\n opts: MemoryCacheOptions = {},\n ) {\n this.staleTTL = staleTTL ?? HOUR\n this.maxTTL = maxTTL ?? DAY\n this.maxEntries = opts.maxEntries ?? 10_000\n this.maxBytes = opts.maxBytes ?? 32 * 1024 * 1024\n this.maxDocBytes = opts.maxDocBytes ?? 64 * 1024\n for (const limit of [this.maxEntries, this.maxBytes, this.maxDocBytes]) {\n if (!Number.isSafeInteger(limit) || limit < 0) {\n throw new RangeError('Cache limits must be non-negative safe integers')\n }\n }\n }\n\n /** Use the DidCache methods to mutate entries and maintain size accounting. */\n public cache: Map<string, CacheVal> = new Map()\n\n async cacheDid(did: string, doc: DidDocument): Promise<void> {\n // @NOTE Remove the previous version even if the replacement cannot be cached.\n this.remove(did)\n if (this.maxEntries === 0) return\n\n let docBytes: number\n try {\n const json = JSON.stringify(doc)\n if (json === undefined) return\n docBytes = encoder.encode(json).byteLength\n } catch {\n return\n }\n const size = docBytes + encoder.encode(did).byteLength\n if (docBytes > this.maxDocBytes || size > this.maxBytes) return\n\n while (\n this.cache.size >= this.maxEntries ||\n this.bytes + size > this.maxBytes\n ) {\n const oldest = this.cache.keys().next()\n if (oldest.done) break\n this.remove(oldest.value)\n }\n this.cache.set(did, { doc, updatedAt: Date.now(), size })\n this.bytes += size\n }\n\n async refreshCache(\n did: string,\n getDoc: () => Promise<DidDocument | null>,\n ): Promise<void> {\n const doc = await getDoc()\n if (doc) {\n await this.cacheDid(did, doc)\n }\n }\n\n async checkCache(did: string): Promise<CacheResult | null> {\n const val = this.cache.get(did)\n if (!val) return null\n const now = Date.now()\n const expired = now > val.updatedAt + this.maxTTL\n const stale = now > val.updatedAt + this.staleTTL\n if (expired) {\n this.remove(did)\n } else {\n this.cache.delete(did)\n this.cache.set(did, val)\n }\n return {\n doc: val.doc,\n updatedAt: val.updatedAt,\n did,\n stale,\n expired,\n }\n }\n\n async clearEntry(did: string): Promise<void> {\n this.remove(did)\n }\n\n async clear(): Promise<void> {\n this.cache.clear()\n this.bytes = 0\n }\n\n private remove(did: string): void {\n const val = this.cache.get(did)\n if (!val) return\n this.cache.delete(did)\n this.bytes -= val.size\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/identity",
3
- "version": "0.5.18",
3
+ "version": "0.5.19",
4
4
  "license": "MIT",
5
5
  "description": "Library for decentralized identities in atproto using DIDs and handles",
6
6
  "keywords": [
@@ -30,9 +30,9 @@
30
30
  "node": ">=22"
31
31
  },
32
32
  "dependencies": {
33
- "@atproto/crypto": "^0.5.7",
33
+ "@atproto-labs/fetch-node": "^0.4.2",
34
34
  "@atproto/common-web": "^0.5.15",
35
- "@atproto-labs/fetch-node": "^0.4.2"
35
+ "@atproto/crypto": "^0.5.7"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@did-plc/lib": "^0.0.1",