Personal Telegram developer console for GitHub
feat(cp6): export incident durable object
davoodmehraban89 committed
Oct 5, 2026 at 06:19 UTC
9dc164ce7308504855772452404fc482e37f6640
1 file changed
+13
-115
src/index.js
+13
-115
@@ -3,118 +3,16 @@ import { DurableWorkspaceStoreClient } from './core/workspace-store.js';
3
import { handleWorkspaceEditorRequest, workspaceEditorPageResponse } from './workspace-editor.js';
4
export { ApprovalReplayGuard } from './durable/approval-replay-guard.js';
5
export { WorkspaceStore } from './durable/workspace-store.js';
6
-
7
-const jsonResponse = (payload, status = 200) => new Response(JSON.stringify(payload), {
8
- status,
9
- headers: { 'Content-Type': 'application/json; charset=utf-8' }
10
-});
11
-
12
-const publicHealthPayload = () => ({
13
- status: 'online',
14
- bot: 'Nexa Dev Bot',
15
- version: '1.1.1',
16
- mode: 'Cloudflare Worker Webhook'
17
-});
18
-
19
-function senderIdFromUpdate(update) {
20
- return update?.message?.from?.id ?? update?.callback_query?.from?.id ?? null;
21
-}
22
-
23
-function isOwnerUpdate(update, env) {
24
- if (!env.OWNER_TELEGRAM_ID) return false;
25
- const senderId = senderIdFromUpdate(update);
26
- return senderId !== null && String(senderId) === String(env.OWNER_TELEGRAM_ID);
27
-}
28
-
29
-function safeErrorMessage(error, env = {}) {
30
- let message = String(error?.message || 'Unknown error');
31
- for (const secret of [
32
- env.TELEGRAM_BOT_TOKEN,
33
- env.TELEGRAM_WEBHOOK_SECRET,
34
- env.GITHUB_TOKEN,
35
- env.CLOUDFLARE_API_TOKEN,
36
- env.APPROVAL_SIGNING_SECRET
37
- ]) {
38
- const value = String(secret || '');
39
- if (value) message = message.split(value).join('[REDACTED]');
40
- }
41
- return message;
42
-}
43
-
44
-/**
45
- * Cloudflare Worker entrypoint.
46
- * Security invariants:
47
- * - Public health endpoints expose only minimal non-sensitive service identity.
48
- * - Telegram webhook requests must carry the configured secret header.
49
- * - Telegram updates are owner-only and unauthorized senders are ignored silently.
50
- * - Workspace Mini App API validates Telegram initData, freshness and owner identity.
51
- * - Mini App saves only the durable Draft; GitHub writes remain behind Safe Approval.
52
- * - Missing security or transport configuration fails closed.
53
- */
54
-export default {
55
- async fetch(request, env = {}, ctx = {}) {
56
- const url = new URL(request.url);
57
-
58
- if (request.method === 'GET' && url.pathname === '/workspace/editor') {
59
- return workspaceEditorPageResponse();
60
- }
61
-
62
- if (url.pathname === '/workspace/api/editor' && (request.method === 'GET' || request.method === 'PUT')) {
63
- const workspaces = new DurableWorkspaceStoreClient({ namespace: env.WORKSPACE_STORE });
64
- return handleWorkspaceEditorRequest(request, {
65
- workspaces,
66
- botToken: env.TELEGRAM_BOT_TOKEN || '',
67
- ownerId: env.OWNER_TELEGRAM_ID || ''
68
- });
69
- }
70
-
71
- if (request.method === 'GET') {
72
- if (url.pathname === '/health' || url.pathname === '/' || url.pathname === '/status') {
73
- return jsonResponse(publicHealthPayload());
74
- }
75
-
76
- return new Response('Not Found', { status: 404 });
77
- }
78
-
79
- if (request.method === 'POST' && url.pathname === '/telegram/webhook') {
80
- if (!env.TELEGRAM_WEBHOOK_SECRET) {
81
- return new Response('Webhook secret not configured', { status: 503 });
82
- }
83
-
84
- const suppliedSecret = request.headers.get('X-Telegram-Bot-Api-Secret-Token');
85
- if (suppliedSecret !== env.TELEGRAM_WEBHOOK_SECRET) {
86
- return new Response('Forbidden', { status: 403 });
87
- }
88
-
89
- if (!env.OWNER_TELEGRAM_ID) {
90
- return new Response('Owner not configured', { status: 503 });
91
- }
92
-
93
- if (!env.TELEGRAM_BOT_TOKEN) {
94
- return new Response('Telegram bot token not configured', { status: 503 });
95
- }
96
-
97
- try {
98
- const update = await request.json();
99
-
100
- if (!isOwnerUpdate(update, env)) {
101
- return jsonResponse({ ok: true, ignored: true });
102
- }
103
-
104
- const bot = new GuidedNexaDevBot(env);
105
- const result = await bot.handleUpdate(update);
106
- return jsonResponse(result);
107
- } catch (err) {
108
- const safeError = safeErrorMessage(err, env);
109
- console.error('telegram_webhook_error', safeError);
110
- return jsonResponse({ ok: false, error: 'Telegram webhook processing failed' }, 400);
111
- }
112
- }
113
-
114
- if (request.method === 'POST') {
115
- return new Response('Not Found', { status: 404 });
116
- }
117
-
118
- return new Response('Method Not Allowed', { status: 405 });
119
- }
120
-};
6
+export { IncidentStore } from './durable/incident-store.js';
7
+
8
+const jsonResponse = (payload, status = 200) => new Response(JSON.stringify(payload), { status, headers: { 'Content-Type': 'application/json; charset=utf-8' } });
9
+const publicHealthPayload = () => ({ status: 'online', bot: 'Nexa Dev Bot', version: '1.1.1', mode: 'Cloudflare Worker Webhook' });
10
+function senderIdFromUpdate(update) { return update?.message?.from?.id ?? update?.callback_query?.from?.id ?? null; }
11
+function isOwnerUpdate(update, env) { if (!env.OWNER_TELEGRAM_ID) return false; const senderId = senderIdFromUpdate(update); return senderId !== null && String(senderId) === String(env.OWNER_TELEGRAM_ID); }
12
+function safeErrorMessage(error, env = {}) { let message=String(error?.message||'Unknown error'); for(const secret of [env.TELEGRAM_BOT_TOKEN,env.TELEGRAM_WEBHOOK_SECRET,env.GITHUB_TOKEN,env.CLOUDFLARE_API_TOKEN,env.APPROVAL_SIGNING_SECRET]){const value=String(secret||'');if(value)message=message.split(value).join('[REDACTED]');} return message; }
13
+export default { async fetch(request,env={},ctx={}) { const url=new URL(request.url);
14
+ if(request.method==='GET'&&url.pathname==='/workspace/editor')return workspaceEditorPageResponse();
15
+ if(url.pathname==='/workspace/api/editor'&&(request.method==='GET'||request.method==='PUT')){const workspaces=new DurableWorkspaceStoreClient({namespace:env.WORKSPACE_STORE});return handleWorkspaceEditorRequest(request,{workspaces,botToken:env.TELEGRAM_BOT_TOKEN||'',ownerId:env.OWNER_TELEGRAM_ID||''});}
16
+ if(request.method==='GET'){if(url.pathname==='/health'||url.pathname==='/'||url.pathname==='/status')return jsonResponse(publicHealthPayload());return new Response('Not Found',{status:404});}
17
+ if(request.method==='POST'&&url.pathname==='/telegram/webhook'){if(!env.TELEGRAM_WEBHOOK_SECRET)return new Response('Webhook secret not configured',{status:503});if(request.headers.get('X-Telegram-Bot-Api-Secret-Token')!==env.TELEGRAM_WEBHOOK_SECRET)return new Response('Forbidden',{status:403});if(!env.OWNER_TELEGRAM_ID)return new Response('Owner not configured',{status:503});if(!env.TELEGRAM_BOT_TOKEN)return new Response('Telegram bot token not configured',{status:503});try{const update=await request.json();if(!isOwnerUpdate(update,env))return jsonResponse({ok:true,ignored:true});const bot=new GuidedNexaDevBot(env);return jsonResponse(await bot.handleUpdate(update));}catch(err){console.error('telegram_webhook_error',safeErrorMessage(err,env));return jsonResponse({ok:false,error:'Telegram webhook processing failed'},400);}}
18
+ if(request.method==='POST')return new Response('Not Found',{status:404}); return new Response('Method Not Allowed',{status:405}); } };