Personal Telegram developer console for GitHub

audit/pr-menu-consolidation
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 133 lines 4.59 KB
Raw
1 const MAX_CHARS = 20000;
2 const MAX_BYTES = 65536;
3 const MAX_COMMIT_MESSAGE = 120;
4 const SAFE_HANDLE = /^[A-Za-z0-9_-]{10}$/;
5
6 export const WORKSPACE_EDITOR_LIMITS = Object.freeze({
7 maxChars: MAX_CHARS,
8 maxBytes: MAX_BYTES
9 });
10
11 const encoder = new TextEncoder();
12
13 const sensitivePath = value => {
14 const path = String(value || '').replaceAll('\\', '/').toLowerCase();
15 const name = path.split('/').pop() || '';
16 if (name === '.env' || name.startsWith('.env.')) return true;
17 if (name === 'id_rsa' || name === 'id_ed25519') return true;
18 if (/\.(pem|key|p12|pfx)$/i.test(name)) return true;
19 if (name === 'credentials.json') return true;
20 if (/^service-account.*\.json$/i.test(name)) return true;
21 return false;
22 };
23
24 const codePointLength = value => Array.from(value).length;
25
26 export function validateEditableFile({ path, text, byteLength } = {}) {
27 const source = String(text ?? '');
28 const charLength = codePointLength(source);
29 const computedBytes = encoder.encode(source).byteLength;
30 const providerBytes = Number(byteLength);
31 const effectiveBytes = Number.isFinite(providerBytes) && providerBytes >= 0
32 ? Math.max(computedBytes, providerBytes)
33 : computedBytes;
34
35 let reason = '';
36 if (sensitivePath(path)) reason = 'Sensitive file path is not editable.';
37 else if (source.includes('\0')) reason = 'Binary-like content is not editable.';
38 else if (/^version https:\/\/git-lfs\.github\.com\/spec\/v1(?:\r?\n|$)/.test(source)) reason = 'Git LFS pointer files are not editable.';
39 else if (charLength > MAX_CHARS) reason = 'Editable text is limited to 20,000 Unicode characters.';
40 else if (effectiveBytes > MAX_BYTES) reason = 'Editable text is limited to 65,536 UTF-8 bytes.';
41
42 return {
43 editable: reason === '',
44 reason,
45 text: source,
46 charLength,
47 byteLength: effectiveBytes
48 };
49 }
50
51 export function normalizeCommitMessage(value) {
52 const normalized = String(value ?? '').trim().replace(/\s+/g, ' ');
53 if (!normalized) throw new Error('Commit message is required');
54 if (codePointLength(normalized) > MAX_COMMIT_MESSAGE) {
55 throw new Error('Commit message must be 120 characters or fewer');
56 }
57 return normalized;
58 }
59
60 export function makeTargetBranch(workspaceId) {
61 const id = String(workspaceId ?? '');
62 if (!SAFE_HANDLE.test(id)) throw new Error('Workspace ID must be a 10-character URL-safe handle');
63 return `nexa/edit-${id}`;
64 }
65
66 export async function digestText(value) {
67 const digest = await crypto.subtle.digest('SHA-256', encoder.encode(String(value ?? '')));
68 return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
69 }
70
71 function linesOf(value) {
72 return String(value ?? '').split('\n');
73 }
74
75 function diffOperations(beforeLines, afterLines) {
76 const rows = beforeLines.length + 1;
77 const cols = afterLines.length + 1;
78 const table = new Uint16Array(rows * cols);
79 const at = (i, j) => i * cols + j;
80
81 for (let i = beforeLines.length - 1; i >= 0; i -= 1) {
82 for (let j = afterLines.length - 1; j >= 0; j -= 1) {
83 table[at(i, j)] = beforeLines[i] === afterLines[j]
84 ? table[at(i + 1, j + 1)] + 1
85 : Math.max(table[at(i + 1, j)], table[at(i, j + 1)]);
86 }
87 }
88
89 const operations = [];
90 let i = 0;
91 let j = 0;
92 while (i < beforeLines.length || j < afterLines.length) {
93 if (i < beforeLines.length && j < afterLines.length && beforeLines[i] === afterLines[j]) {
94 operations.push({ kind: ' ', line: beforeLines[i] });
95 i += 1;
96 j += 1;
97 continue;
98 }
99 if (j < afterLines.length && (i >= beforeLines.length || table[at(i, j + 1)] >= table[at(i + 1, j)])) {
100 operations.push({ kind: '+', line: afterLines[j] });
101 j += 1;
102 continue;
103 }
104 operations.push({ kind: '-', line: beforeLines[i] });
105 i += 1;
106 }
107 return operations;
108 }
109
110 export function buildUnifiedDiff({ path, before, after, maxChars = 2600 } = {}) {
111 const safePath = String(path || 'file').replace(/[\r\n]/g, '');
112 const operations = diffOperations(linesOf(before), linesOf(after));
113 const additions = operations.reduce((count, op) => count + (op.kind === '+' ? 1 : 0), 0);
114 const deletions = operations.reduce((count, op) => count + (op.kind === '-' ? 1 : 0), 0);
115 const raw = [
116 `--- a/${safePath}`,
117 `+++ b/${safePath}`,
118 '@@',
119 ...operations.map(op => `${op.kind}${op.line}`)
120 ].join('\n');
121
122 const limit = Math.max(32, Math.min(10000, Number(maxChars) || 2600));
123 if (raw.length <= limit) {
124 return { text: raw, additions, deletions, truncated: false };
125 }
126 const marker = '\n…';
127 return {
128 text: `${raw.slice(0, Math.max(0, limit - marker.length))}${marker}`,
129 additions,
130 deletions,
131 truncated: true
132 };
133 }