Personal Telegram developer console for GitHub

feat: add durable approval replay store

davoodmehraban89 committed Oct 3, 2026 at 06:30 UTC f7575c75cdf872899c5c7f43c1cd0fff4879493a
1 file changed +47
src/core/durable-replay-store.js new
+47
@@ -0,0 +1,47 @@
1 +export class DurableApprovalReplayStore {
2 + constructor({ namespace } = {}) {
3 + this.namespace = namespace || null;
4 + }
5 +
6 + isConfigured() {
7 + return Boolean(
8 + this.namespace &&
9 + typeof this.namespace.idFromName === 'function' &&
10 + typeof this.namespace.get === 'function'
11 + );
12 + }
13 +
14 + 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 +
41 + 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 + }
45 + return true;
46 + }
47 +}