@snowcone-app/mockup-resolver 1.0.5 → 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 +14 -0
- package/package.json +2 -2
- package/src/index.ts +76 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
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
|
+
|
|
10
|
+
## 1.0.6
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [[`66caf3e`](https://github.com/snowcone-app/snowcone-monorepo/commit/66caf3e5a0b9dbd5d34e40fa585f10deadcef6a7)]:
|
|
15
|
+
- @snowcone-app/mockup-url@0.3.2
|
|
16
|
+
|
|
3
17
|
## 1.0.5
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/mockup-resolver",
|
|
3
|
-
"version": "1.0.
|
|
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.
|
|
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" | "
|
|
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 "
|
|
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 "
|
|
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
|
-
//
|
|
793
|
-
//
|
|
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);
|
|
@@ -1054,6 +1075,12 @@ async function handleMockupUrl(request: Request, env: Env): Promise<Response> {
|
|
|
1054
1075
|
const urlObj = new URL(unsigned);
|
|
1055
1076
|
const sigParams = new URLSearchParams(urlObj.search);
|
|
1056
1077
|
sigParams.delete("seal");
|
|
1078
|
+
// Mirror the BOX's sealed canonical exactly (rendercenter url-verifier
|
|
1079
|
+
// deletes BOTH seal and debug before verifying — debug is observability
|
|
1080
|
+
// transport appended to already-sealed URLs since long before ADR-0096).
|
|
1081
|
+
// Signing WITH debug included made every debug render 401 "Invalid URL
|
|
1082
|
+
// signature" at the box.
|
|
1083
|
+
sigParams.delete("debug");
|
|
1057
1084
|
const relative = `${urlObj.pathname}?${sigParams.toString()}`;
|
|
1058
1085
|
const sig = await signUrl(relative, signingSecret);
|
|
1059
1086
|
urlObj.searchParams.append("seal", sig);
|
|
@@ -1415,10 +1442,29 @@ async function handlePublicResolve(
|
|
|
1415
1442
|
};
|
|
1416
1443
|
const globalCheck: Promise<LimitVerdict> = (async () => {
|
|
1417
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 };
|
|
1418
1455
|
const globalRl = env.API_RATE_LIMITER.get(
|
|
1419
1456
|
env.API_RATE_LIMITER.idFromName(`global:public:${clientIp}`)
|
|
1420
1457
|
);
|
|
1421
|
-
|
|
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;
|
|
1422
1468
|
} catch (e) {
|
|
1423
1469
|
console.error(`Public resolve: global rate-limit error (fail open): ${e}`);
|
|
1424
1470
|
return { allowed: true };
|
|
@@ -1427,10 +1473,27 @@ async function handlePublicResolve(
|
|
|
1427
1473
|
const acctCheck: Promise<LimitVerdict> = (async () => {
|
|
1428
1474
|
if (!account.public?.rateLimit) return { allowed: true };
|
|
1429
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 };
|
|
1430
1484
|
const acctRl = env.API_RATE_LIMITER.get(
|
|
1431
1485
|
env.API_RATE_LIMITER.idFromName(`${accountId}:public:${clientIp}`)
|
|
1432
1486
|
);
|
|
1433
|
-
|
|
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;
|
|
1434
1497
|
} catch (e) {
|
|
1435
1498
|
console.error(`Public resolve: account rate-limit error (fail open): ${e}`);
|
|
1436
1499
|
return { allowed: true };
|
|
@@ -1661,6 +1724,12 @@ async function handlePublicResolve(
|
|
|
1661
1724
|
}
|
|
1662
1725
|
const sigParams = new URLSearchParams(urlObj.search);
|
|
1663
1726
|
sigParams.delete("seal");
|
|
1727
|
+
// Mirror the BOX's sealed canonical exactly (rendercenter url-verifier
|
|
1728
|
+
// deletes BOTH seal and debug before verifying — debug is observability
|
|
1729
|
+
// transport appended to already-sealed URLs since long before ADR-0096).
|
|
1730
|
+
// Signing WITH debug included made every debug render 401 "Invalid URL
|
|
1731
|
+
// signature" at the box.
|
|
1732
|
+
sigParams.delete("debug");
|
|
1664
1733
|
const relative = `${urlObj.pathname}?${sigParams.toString()}`;
|
|
1665
1734
|
const sig = await signUrl(relative, signingSecret);
|
|
1666
1735
|
urlObj.searchParams.append("seal", sig);
|