Personal Telegram developer console for GitHub

fix/repository-id-navigation-fallback
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 128 lines 4.49 KB
Raw
1 const MAX_CHARS = 3000;
2 const MAX_BYTES = 12000;
3 const MAX_COMMIT_MESSAGE = 120;
4 const SAFE_HANDLE = /^[A-Za-z0-9_-]{10}$/;
5
6 const encoder = new TextEncoder();
7
8 const sensitivePath = value => {
9 const path = String(value || '').replaceAll('\\', '/').toLowerCase();
10 const name = path.split('/').pop() || '';
11 if (name === '.env' || name.startsWith('.env.')) return true;
12 if (name === 'id_rsa' || name === 'id_ed25519') return true;
13 if (/\.(pem|key|p12|pfx)$/i.test(name)) return true;
14 if (name === 'credentials.json') return true;
15 if (/^service-account.*\.json$/i.test(name)) return true;
16 return false;
17 };
18
19 const codePointLength = value => Array.from(value).length;
20
21 export function validateEditableFile({ path, text, byteLength } = {}) {
22 const source = String(text ?? '');
23 const charLength = codePointLength(source);
24 const computedBytes = encoder.encode(source).byteLength;
25 const providerBytes = Number(byteLength);
26 const effectiveBytes = Number.isFinite(providerBytes) && providerBytes >= 0
27 ? Math.max(computedBytes, providerBytes)
28 : computedBytes;
29
30 let reason = '';
31 if (sensitivePath(path)) reason = 'Sensitive file path is not editable.';
32 else if (source.includes('\0')) reason = 'Binary-like content is not editable.';
33 else if (/^version https:\/\/git-lfs\.github\.com\/spec\/v1(?:\r?\n|$)/.test(source)) reason = 'Git LFS pointer files are not editable.';
34 else if (charLength > MAX_CHARS) reason = 'Editable text is limited to 3,000 Unicode characters.';
35 else if (effectiveBytes > MAX_BYTES) reason = 'Editable text is limited to 12,000 UTF-8 bytes.';
36
37 return {
38 editable: reason === '',
39 reason,
40 text: source,
41 charLength,
42 byteLength: effectiveBytes
43 };
44 }
45
46 export function normalizeCommitMessage(value) {
47 const normalized = String(value ?? '').trim().replace(/\s+/g, ' ');
48 if (!normalized) throw new Error('Commit message is required');
49 if (codePointLength(normalized) > MAX_COMMIT_MESSAGE) {
50 throw new Error('Commit message must be 120 characters or fewer');
51 }
52 return normalized;
53 }
54
55 export function makeTargetBranch(workspaceId) {
56 const id = String(workspaceId ?? '');
57 if (!SAFE_HANDLE.test(id)) throw new Error('Workspace ID must be a 10-character URL-safe handle');
58 return `nexa/edit-${id}`;
59 }
60
61 export async function digestText(value) {
62 const digest = await crypto.subtle.digest('SHA-256', encoder.encode(String(value ?? '')));
63 return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
64 }
65
66 function linesOf(value) {
67 return String(value ?? '').split('\n');
68 }
69
70 function diffOperations(beforeLines, afterLines) {
71 const rows = beforeLines.length + 1;
72 const cols = afterLines.length + 1;
73 const table = new Uint16Array(rows * cols);
74 const at = (i, j) => i * cols + j;
75
76 for (let i = beforeLines.length - 1; i >= 0; i -= 1) {
77 for (let j = afterLines.length - 1; j >= 0; j -= 1) {
78 table[at(i, j)] = beforeLines[i] === afterLines[j]
79 ? table[at(i + 1, j + 1)] + 1
80 : Math.max(table[at(i + 1, j)], table[at(i, j + 1)]);
81 }
82 }
83
84 const operations = [];
85 let i = 0;
86 let j = 0;
87 while (i < beforeLines.length || j < afterLines.length) {
88 if (i < beforeLines.length && j < afterLines.length && beforeLines[i] === afterLines[j]) {
89 operations.push({ kind: ' ', line: beforeLines[i] });
90 i += 1;
91 j += 1;
92 continue;
93 }
94 if (j < afterLines.length && (i >= beforeLines.length || table[at(i, j + 1)] >= table[at(i + 1, j)])) {
95 operations.push({ kind: '+', line: afterLines[j] });
96 j += 1;
97 continue;
98 }
99 operations.push({ kind: '-', line: beforeLines[i] });
100 i += 1;
101 }
102 return operations;
103 }
104
105 export function buildUnifiedDiff({ path, before, after, maxChars = 2600 } = {}) {
106 const safePath = String(path || 'file').replace(/[\r\n]/g, '');
107 const operations = diffOperations(linesOf(before), linesOf(after));
108 const additions = operations.reduce((count, op) => count + (op.kind === '+' ? 1 : 0), 0);
109 const deletions = operations.reduce((count, op) => count + (op.kind === '-' ? 1 : 0), 0);
110 const raw = [
111 `--- a/${safePath}`,
112 `+++ b/${safePath}`,
113 '@@',
114 ...operations.map(op => `${op.kind}${op.line}`)
115 ].join('\n');
116
117 const limit = Math.max(32, Math.min(10000, Number(maxChars) || 2600));
118 if (raw.length <= limit) {
119 return { text: raw, additions, deletions, truncated: false };
120 }
121 const marker = '\n…';
122 return {
123 text: `${raw.slice(0, Math.max(0, limit - marker.length))}${marker}`,
124 additions,
125 deletions,
126 truncated: true
127 };
128 }