@ultimat3/core 31.7.0 → 31.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/core",
3
- "version": "31.7.0",
3
+ "version": "31.9.0",
4
4
  "description": "Ultimate's foundation: errors, context, env, config, clock, ids, logging, telemetry, lifecycle",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -54,6 +54,6 @@
54
54
  "test": "bun test"
55
55
  },
56
56
  "dependencies": {
57
- "@ultimat3/schema": "31.7.0"
57
+ "@ultimat3/schema": "31.9.0"
58
58
  }
59
59
  }
@@ -196,7 +196,7 @@ export function isPublicAddress(address: string): boolean {
196
196
  * the full IPv6 string gave one host ~2^64 budgets. Anything that is not an address literal is
197
197
  * returned unchanged — the caller's key stays whatever it was, never a guessed network.
198
198
  */
199
- export function addressNetwork(address: string): string {
199
+ function parseNetwork(address: string): string {
200
200
  const text = literalOf(address);
201
201
  if (!text.includes(':')) {
202
202
  const v4 = parseV4(text);
@@ -218,3 +218,40 @@ export function addressNetwork(address: string): string {
218
218
  .map((group) => group.toString(16))
219
219
  .join(':')}::/64`;
220
220
  }
221
+
222
+ /** How many addresses `addressNetwork` remembers. Past it the least recently asked is forgotten. */
223
+ export const ADDRESS_NETWORK_MEMO_SIZE = 4096;
224
+
225
+ /**
226
+ * `parse`, memoised in a bounded LRU. Exported so a test can count the parses behind it; the one
227
+ * production instance is `addressNetwork`. A `Map`'s insertion order is its recency: a hit moves to
228
+ * the end, the first key is the coldest. Bounded because the caller set is attacker-chosen.
229
+ */
230
+ export function networkMemo(
231
+ parse: (address: string) => string,
232
+ size: number = ADDRESS_NETWORK_MEMO_SIZE,
233
+ ): (address: string) => string {
234
+ const known = new Map<string, string>();
235
+ return (address) => {
236
+ const hit = known.get(address);
237
+ if (hit !== undefined) {
238
+ known.delete(address);
239
+ known.set(address, hit);
240
+ return hit;
241
+ }
242
+ const network = parse(address);
243
+ if (known.size >= size) {
244
+ const coldest = known.keys().next();
245
+ if (coldest.done !== true) known.delete(coldest.value);
246
+ }
247
+ known.set(address, network);
248
+ return network;
249
+ };
250
+ }
251
+
252
+ /**
253
+ * The NETWORK a caller's address names (`parseNetwork` above), memoised: every rate-limited
254
+ * request, auth's ip key and realtime's subscription book ask it, and parsing an IPv6 literal is
255
+ * ~2 µs a time for an answer that never changes. One memo, so every caller gets it.
256
+ */
257
+ export const addressNetwork: (address: string) => string = networkMemo(parseNetwork);