Personal Telegram developer console for GitHub
feat: add workspace edit policy and diff
davoodmehraban89 committed
Oct 3, 2026 at 12:27 UTC
b3dbbaa07a0aaa642392c49ec7a203a065306258
1 file changed
+128
src/core/workspace-policy.js
new
+128
@@ -0,0 +1,128 @@
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
+}