Personal Telegram developer console for GitHub

feat(approval): persist destructive approval context

davoodmehraban89 committed Oct 5, 2026 at 05:07 UTC 73c6879a93a53a41f2d93a45ff1efc2d1003f0f0
1 file changed +21 -39
src/core/durable-replay-store.js
+21 -39
@@ -1,47 +1,29 @@
1 export class DurableApprovalReplayStore {
2 - constructor({ namespace } = {}) {
3 - this.namespace = namespace || null;
2 + constructor({ namespace } = {}) { this.namespace = namespace || null; }
3 + isConfigured() { return Boolean(this.namespace && typeof this.namespace.idFromName === 'function' && typeof this.namespace.get === 'function'); }
4 + #stub(ownerId, nonce) {
5 + if (!this.isConfigured()) throw new Error('Durable approval replay protection is not configured');
6 + const owner = String(ownerId ?? ''); const nonceValue = String(nonce ?? '');
7 + if (!owner || !nonceValue) throw new Error('Invalid durable approval replay input');
8 + return this.namespace.get(this.namespace.idFromName(`${owner}:${nonceValue}`));
9 }
5 -
6 - isConfigured() {
7 - return Boolean(
8 - this.namespace &&
9 - typeof this.namespace.idFromName === 'function' &&
10 - typeof this.namespace.get === 'function'
11 - );
10 + async put({ ownerId, nonce, expiresAt, payload } = {}) {
11 + const expiry = Number(expiresAt); if (!Number.isFinite(expiry) || !payload) throw new Error('Invalid durable approval replay input');
12 + const response = await this.#stub(ownerId, nonce).fetch(new Request('https://approval-replay/prepare', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ expiresAt: expiry, payload }) }));
13 + if (!response.ok) throw new Error(`Durable approval preparation failed (${response.status})`);
14 + return true;
15 + }
16 + async peek({ ownerId, nonce } = {}) {
17 + const response = await this.#stub(ownerId, nonce).fetch(new Request('https://approval-replay/peek'));
18 + if (!response.ok) return null;
19 + const body = await response.json().catch(() => null); return body?.payload || null;
20 }
13 -
21 async consume({ ownerId, nonce, expiresAt } = {}) {
15 - if (!this.isConfigured()) {
16 - throw new Error('Durable approval replay protection is not configured');
17 - }
18 -
19 - const owner = String(ownerId ?? '');
20 - const nonceValue = String(nonce ?? '');
21 - const expiry = Number(expiresAt);
22 - if (!owner || !nonceValue || !Number.isFinite(expiry)) {
23 - throw new Error('Invalid durable approval replay input');
24 - }
25 -
26 - const id = this.namespace.idFromName(`${owner}:${nonceValue}`);
27 - const stub = this.namespace.get(id);
28 - const response = await stub.fetch(new Request('https://approval-replay/consume', {
29 - method: 'POST',
30 - headers: { 'content-type': 'application/json' },
31 - body: JSON.stringify({ expiresAt: expiry })
32 - }));
33 -
34 - let body = null;
35 - try {
36 - body = await response.json();
37 - } catch {
38 - body = null;
39 - }
40 -
22 + const expiry = Number(expiresAt); if (!Number.isFinite(expiry)) throw new Error('Invalid durable approval replay input');
23 + const response = await this.#stub(ownerId, nonce).fetch(new Request('https://approval-replay/consume', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ expiresAt: expiry }) }));
24 + const body = await response.json().catch(() => null);
25 if (response.status === 409 || response.status === 410) return false;
42 - if (!response.ok || body?.accepted !== true) {
43 - throw new Error(`Durable approval replay protection failed (${response.status})`);
44 - }
26 + if (!response.ok || body?.accepted !== true) throw new Error(`Durable approval replay protection failed (${response.status})`);
27 return true;
28 }
29 }