@snowcone-app/mockup-resolver 1.0.6 → 1.0.7

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.7
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`e82940a`](https://github.com/snowcone-app/snowcone-monorepo/commit/e82940a934cb00d870160d46b75a8b17b95a1c55)]:
8
+ - @snowcone-app/mockup-url@0.3.3
9
+
3
10
  ## 1.0.6
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.6",
3
+ "version": "1.0.7",
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.2"
9
+ "@snowcone-app/mockup-url": "0.3.3"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@cloudflare/workers-types": "^4.20250303.0",
package/src/index.ts CHANGED
@@ -415,16 +415,16 @@ const publicLimitBurstCache = new Map<
415
415
 
416
416
  /** grant = allowed locally (skip the DO await); deny = cached denial;
417
417
  * miss = no fresh window — do the authoritative DO round-trip. */
418
- function burstAllow(key: string, rps: number): "grant" | "deny" | "miss" {
418
+ function burstAllow(key: string, rps: number): "grant" | "deny" | "cold" | "exhausted" {
419
419
  const entry = publicLimitBurstCache.get(key);
420
420
  const nowMs = Date.now();
421
- if (!entry || nowMs >= entry.windowEnd) return "miss";
421
+ if (!entry || nowMs >= entry.windowEnd) return "cold";
422
422
  if (!entry.allowed) return "deny";
423
423
  if (entry.used < rps) {
424
424
  entry.used++;
425
425
  return "grant";
426
426
  }
427
- return "miss";
427
+ return "exhausted";
428
428
  }
429
429
 
430
430
  function burstRecord(key: string, allowed: boolean): void {
@@ -788,9 +788,23 @@ async function authenticate(
788
788
  globalVerdict = { allowed: false, scope: "second", retryAfter: 1000 } as Awaited<
789
789
  ReturnType<RateLimiterStub["checkPublicLimits"]>
790
790
  >;
791
+ } else if (globalGrant === "cold") {
792
+ // Kevin-approved optimistic first-sight (2026-08-15): a COLD per-IP DO
793
+ // wake measured 565ms synchronously on a visitor's FIRST render — an
794
+ // absurd tax for an abuse-guard. Allow immediately, settle the
795
+ // authoritative verdict in the background; a denial lands in the
796
+ // window cache and bites from the next request. Slack = one request
797
+ // per IP per window per isolate; daily caps still accrue in the DO.
798
+ globalVerdict = { allowed: true } as Awaited<
799
+ ReturnType<RateLimiterStub["checkPublicLimits"]>
800
+ >;
801
+ void globalRateLimiter
802
+ .checkPublicLimits(GLOBAL_IP_RATE_LIMIT_RPS, GLOBAL_DAILY_IP_RATE_LIMIT)
803
+ .then((v) => burstRecord(globalBurstKey, v.allowed))
804
+ .catch(() => {});
791
805
  } else {
792
- // Per-second + daily in ONE DO round-trip (was two sequential RPCs to
793
- // the same object — pure added latency on every public request).
806
+ // Local window EXHAUSTED — the rate limit may genuinely be engaging;
807
+ // this is the one case that must await the authoritative verdict.
794
808
  globalVerdict = await globalRateLimiter.checkPublicLimits(
795
809
  GLOBAL_IP_RATE_LIMIT_RPS,
796
810
  GLOBAL_DAILY_IP_RATE_LIMIT
@@ -863,6 +877,13 @@ async function authenticate(
863
877
  result = { allowed: false, retryAfter: 1000 } as Awaited<
864
878
  ReturnType<RateLimiterStub["checkRateLimit"]>
865
879
  >;
880
+ } else if (acctGrant === "cold") {
881
+ // Same optimistic first-sight as the global limiter (cold DO wake).
882
+ result = { allowed: true } as Awaited<ReturnType<RateLimiterStub["checkRateLimit"]>>;
883
+ void rateLimiter
884
+ .checkRateLimit(accountData.public.rateLimit)
885
+ .then((v) => burstRecord(acctBurstKey, v.allowed))
886
+ .catch(() => {});
866
887
  } else {
867
888
  // Call the method directly on the Durable Object via RPC
868
889
  result = await rateLimiter.checkRateLimit(accountData.public.rateLimit);
@@ -1421,10 +1442,29 @@ async function handlePublicResolve(
1421
1442
  };
1422
1443
  const globalCheck: Promise<LimitVerdict> = (async () => {
1423
1444
  try {
1445
+ // Kevin-approved optimistic limiting (2026-08-15): a cold per-IP DO
1446
+ // wake measured 565ms synchronously on a visitor's first render, and
1447
+ // even warm the RPC costs ~60ms per request. Same-window repeats and
1448
+ // first-sights allow immediately with the authoritative verdict
1449
+ // settling in the background (denials cached, bite from the next
1450
+ // request); only an EXHAUSTED local window — the limit genuinely
1451
+ // engaging — awaits the DO.
1452
+ const burstKey = `g:${clientIp}`;
1453
+ const grant = burstAllow(burstKey, 20);
1454
+ if (grant === "deny") return { allowed: false, scope: "second", retryAfter: 1000 };
1424
1455
  const globalRl = env.API_RATE_LIMITER.get(
1425
1456
  env.API_RATE_LIMITER.idFromName(`global:public:${clientIp}`)
1426
1457
  );
1427
- return await globalRl.checkPublicLimits(20, 50000);
1458
+ if (grant === "grant" || grant === "cold") {
1459
+ void globalRl
1460
+ .checkPublicLimits(20, 50000)
1461
+ .then((v) => burstRecord(burstKey, v.allowed))
1462
+ .catch(() => {});
1463
+ return { allowed: true };
1464
+ }
1465
+ const v = await globalRl.checkPublicLimits(20, 50000);
1466
+ burstRecord(burstKey, v.allowed);
1467
+ return v;
1428
1468
  } catch (e) {
1429
1469
  console.error(`Public resolve: global rate-limit error (fail open): ${e}`);
1430
1470
  return { allowed: true };
@@ -1433,10 +1473,27 @@ async function handlePublicResolve(
1433
1473
  const acctCheck: Promise<LimitVerdict> = (async () => {
1434
1474
  if (!account.public?.rateLimit) return { allowed: true };
1435
1475
  try {
1476
+ // Same optimistic pattern as the global check above.
1477
+ const burstKey = `${accountId}:${clientIp}`;
1478
+ const acctRps = Math.max(
1479
+ 1,
1480
+ Number((account.public.rateLimit as { requestsPerSecond?: number }).requestsPerSecond) || 5
1481
+ );
1482
+ const grant = burstAllow(burstKey, acctRps);
1483
+ if (grant === "deny") return { allowed: false, retryAfter: 1000 };
1436
1484
  const acctRl = env.API_RATE_LIMITER.get(
1437
1485
  env.API_RATE_LIMITER.idFromName(`${accountId}:public:${clientIp}`)
1438
1486
  );
1439
- return await acctRl.checkRateLimit(account.public.rateLimit);
1487
+ if (grant === "grant" || grant === "cold") {
1488
+ void acctRl
1489
+ .checkRateLimit(account.public.rateLimit)
1490
+ .then((v) => burstRecord(burstKey, v.allowed))
1491
+ .catch(() => {});
1492
+ return { allowed: true };
1493
+ }
1494
+ const v = await acctRl.checkRateLimit(account.public.rateLimit);
1495
+ burstRecord(burstKey, v.allowed);
1496
+ return v;
1440
1497
  } catch (e) {
1441
1498
  console.error(`Public resolve: account rate-limit error (fail open): ${e}`);
1442
1499
  return { allowed: true };