Personal Telegram developer console for GitHub
feat(approval): store exact destructive intent durably
davoodmehraban89 committed
Oct 5, 2026 at 05:07 UTC
93ed328a6fba3ac0266556110c08c9edfe74ef59
1 file changed
+24
-38
src/durable/approval-replay-guard.js
+24
-38
@@ -1,47 +1,33 @@
1
const json = (payload, status = 200) => Response.json(payload, { status });
2
3
export class ApprovalReplayGuard {
4
- constructor(state) {
5
- this.state = state;
6
- }
7
-
4
+ constructor(state) { this.state = state; }
5
async fetch(request) {
6
const url = new URL(request.url);
10
- if (request.method !== 'POST' || url.pathname !== '/consume') {
11
- return new Response('Not Found', { status: 404 });
12
- }
13
-
14
- let payload;
15
- try {
16
- payload = await request.json();
17
- } catch {
18
- return json({ accepted: false, reason: 'invalid_payload' }, 400);
19
- }
20
-
21
- const expiresAt = Number(payload?.expiresAt);
22
- if (!Number.isFinite(expiresAt)) {
23
- return json({ accepted: false, reason: 'invalid_expiry' }, 400);
7
+ if (request.method === 'POST' && url.pathname === '/prepare') {
8
+ const payload = await request.json().catch(() => null); const expiresAt = Number(payload?.expiresAt);
9
+ if (!Number.isFinite(expiresAt) || expiresAt < Date.now() || !payload?.payload) return json({ prepared: false }, 400);
10
+ const prepared = await this.state.storage.transaction(async txn => {
11
+ if (await txn.get('used')) return false;
12
+ const existing = await txn.get('payload');
13
+ if (existing) return JSON.stringify(existing) === JSON.stringify(payload.payload);
14
+ await txn.put('payload', payload.payload); await txn.put('expiresAt', expiresAt); return true;
15
+ });
16
+ if (!prepared) return json({ prepared: false }, 409);
17
+ await this.state.storage.setAlarm(expiresAt); return json({ prepared: true });
18
}
25
- if (expiresAt < Date.now()) {
26
- return json({ accepted: false, reason: 'expired' }, 410);
19
+ if (request.method === 'GET' && url.pathname === '/peek') {
20
+ const expiresAt = Number(await this.state.storage.get('expiresAt'));
21
+ if (!Number.isFinite(expiresAt) || expiresAt < Date.now() || await this.state.storage.get('used')) return json({ payload: null }, 404);
22
+ return json({ payload: await this.state.storage.get('payload') || null });
23
}
28
-
29
- const accepted = await this.state.storage.transaction(async txn => {
30
- const usedUntil = await txn.get('used');
31
- if (usedUntil) return false;
32
- await txn.put('used', expiresAt);
33
- return true;
34
- });
35
-
36
- if (!accepted) {
37
- return json({ accepted: false, reason: 'already_used' }, 409);
38
- }
39
-
40
- await this.state.storage.setAlarm(expiresAt);
41
- return json({ accepted: true });
42
- }
43
-
44
- async alarm() {
45
- await this.state.storage.deleteAll();
24
+ if (request.method !== 'POST' || url.pathname !== '/consume') return new Response('Not Found', { status: 404 });
25
+ const payload = await request.json().catch(() => null); const expiresAt = Number(payload?.expiresAt);
26
+ if (!Number.isFinite(expiresAt)) return json({ accepted: false, reason: 'invalid_expiry' }, 400);
27
+ if (expiresAt < Date.now()) return json({ accepted: false, reason: 'expired' }, 410);
28
+ const accepted = await this.state.storage.transaction(async txn => { const usedUntil = await txn.get('used'); if (usedUntil) return false; await txn.put('used', expiresAt); return true; });
29
+ if (!accepted) return json({ accepted: false, reason: 'already_used' }, 409);
30
+ await this.state.storage.setAlarm(expiresAt); return json({ accepted: true });
31
}
32
+ async alarm() { await this.state.storage.deleteAll(); }
33
}