@snowcone-app/mockup-resolver 1.0.7 → 1.0.8

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,12 @@
1
1
  # @snowcone-app/mockup-resolver
2
2
 
3
+ ## 1.0.8
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`a1a9095`](https://github.com/snowcone-app/snowcone-monorepo/commit/a1a909566506c63bcdc322fbd8de10b51043b9e0)]:
8
+ - @snowcone-app/mockup-url@0.3.4
9
+
3
10
  ## 1.0.7
4
11
 
5
12
  ### Patch Changes
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@snowcone-app/mockup-resolver",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "URL signing and verification Cloudflare Worker",
5
5
  "main": "dist/index.js",
6
6
  "author": "",
7
7
  "license": "ISC",
8
8
  "dependencies": {
9
- "@snowcone-app/mockup-url": "0.3.3"
9
+ "@snowcone-app/mockup-url": "0.3.4"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@cloudflare/workers-types": "^4.20250303.0",
package/src/index.ts CHANGED
@@ -408,6 +408,29 @@ async function hashApiKey(apiKey: string): Promise<string> {
408
408
  * invisible at a 20rps limit — while burst latency drops to ~0. A DENIED
409
409
  * verdict is cached for its window too, so hammering stays cheap.
410
410
  */
411
+ /**
412
+ * In-isolate TTL caches for the resolver's two remaining lookups (ADR-0096
413
+ * "rest of rsv to ~0"): account config (KV, changes rarely — revocation and
414
+ * limit edits lag <=60s on public renders) and product data (catalog fetch —
415
+ * edits lag <=30s; the render boxes' own product cache has explicit
416
+ * invalidation, this only delays the resolver's view). Both flatten the
417
+ * per-isolate warm path to ~0ms; a cold isolate still pays first-fetch.
418
+ */
419
+ const ACCT_CACHE_TTL_MS = 60_000;
420
+ const PRODUCT_CACHE_TTL_MS = 30_000;
421
+ const ttlCache = new Map<string, { until: number; value: unknown }>();
422
+
423
+ function ttlGet<T>(key: string): T | undefined {
424
+ const e = ttlCache.get(key);
425
+ if (!e || Date.now() >= e.until) return undefined;
426
+ return e.value as T;
427
+ }
428
+
429
+ function ttlSet(key: string, value: unknown, ttlMs: number): void {
430
+ if (ttlCache.size > 5_000) ttlCache.clear();
431
+ ttlCache.set(key, { until: Date.now() + ttlMs, value });
432
+ }
433
+
411
434
  const publicLimitBurstCache = new Map<
412
435
  string,
413
436
  { windowEnd: number; used: number; allowed: boolean }
@@ -1342,7 +1365,11 @@ async function handlePublicResolve(
1342
1365
 
1343
1366
  // 1. Shop ID lookup → AccountData (404 unknown, 403 revoked).
1344
1367
  const tAcct = Date.now();
1345
- const account = await loadAccountByKey(key, env);
1368
+ let account = ttlGet<AccountData | null>(`acct:${key}`);
1369
+ if (account === undefined) {
1370
+ account = await loadAccountByKey(key, env);
1371
+ ttlSet(`acct:${key}`, account, ACCT_CACHE_TTL_MS);
1372
+ }
1346
1373
  const acctMs = Date.now() - tAcct;
1347
1374
  if (!account) return errResp(404, "Unknown shop");
1348
1375
  if (account.revoked) return errResp(403, "Shop revoked");
@@ -1425,11 +1452,17 @@ async function handlePublicResolve(
1425
1452
  }
1426
1453
 
1427
1454
  const tLimits = Date.now();
1428
- const productPromise = fetchProduct(
1429
- productCode,
1430
- { host: meiliHost, apiKey: meiliKey, index: meiliIndex },
1431
- env.ACCOUNTS
1432
- );
1455
+ const cachedProduct = ttlGet<Awaited<ReturnType<typeof fetchProduct>>>(`prod:${productCode}`);
1456
+ const productPromise = cachedProduct !== undefined
1457
+ ? Promise.resolve(cachedProduct)
1458
+ : fetchProduct(
1459
+ productCode,
1460
+ { host: meiliHost, apiKey: meiliKey, index: meiliIndex },
1461
+ env.ACCOUNTS
1462
+ ).then((p) => {
1463
+ ttlSet(`prod:${productCode}`, p, PRODUCT_CACHE_TTL_MS);
1464
+ return p;
1465
+ });
1433
1466
  // Detached guard: when a limit blocks below, this promise is never awaited —
1434
1467
  // without a no-op catch, a fetch failure there would surface as an unhandled
1435
1468
  // rejection. The real `await productPromise` further down still rethrows.