summaryrefslogtreecommitdiff
path: root/web/static/app.mjs
blob: 429bef7bcd5d0647a2269846583952934c2bde3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import { Client } from './client.mjs';
const $ = id => document.getElementById(id);
const text = new TextDecoder('utf-8', { fatal: true });
let client = null, current = null, currentPath = '/', busy = false, dirty = false;
let configuration = null;

function status(message, error = false) {
  $('status').textContent = message;
  $('status').classList.toggle('error', error);
}
function controls() {
  for (const id of ['go', 'reload', 'newfile', 'newdir', 'upload']) $(id).disabled = busy;
  $('save').disabled = busy || !current || current.stat.directory || $('editor').hidden || !dirty;
  $('download').disabled = busy || !current || current.stat.directory;
  $('follow').disabled = busy || !current || current.stat.directory;
  $('rename').disabled = busy || currentPath === '/';
  $('delete').disabled = busy || currentPath === '/';
  $('editor').readOnly = busy;
  $('edit-status').textContent = dirty ? 'Unsaved changes.' : 'No unsaved changes.';
  document.querySelector('main').setAttribute('aria-busy', String(busy));
}
async function action(operation) {
  if (busy) return;
  busy = true;
  controls();
  try { await operation(); }
  catch (error) { status(error.message, true); }
  finally { busy = false; controls(); }
}
async function connectedClient() {
  if (client && !client.closed) return client;
  if (!configuration) {
    const response = await fetch('/_cloud9/config.json');
    if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.');
    configuration = await response.json();
  }
  try { client = await Client.connect('/_cloud9/9p', configuration.user, configuration.tree); }
  catch { throw new Error('Unable to reach the file server. Reload to try again.'); }
  return client;
}
async function readPath(path) {
  const active = await connectedClient();
  try { return await active.readPath(path); }
  catch (error) {
    // A timed-out/closed transport has lost its fids. Retry reads once on a
    // fresh session. Writes are never replayed after an uncertain result.
    if (!active.closed) throw error;
    return (await connectedClient()).readPath(path);
  }
}
function normalizePath(path) {
  const parts = [];
  for (const name of path.split('/')) {
    if (!name || name === '.') continue;
    if (name === '..') parts.pop(); else parts.push(name);
  }
  return '/' + parts.join('/');
}
function pathURL(path) {
  const parts = path.split('/').map(encodeURIComponent);
  // Preserve access to an upstream directory named _cloud9 without colliding
  // with the literal gateway prefix. Decode each component exactly once.
  if (parts[1] === '_cloud9') parts[1] = '%5Fcloud9';
  return parts.join('/');
}
function locationPath() {
  const parts = location.pathname.split('/').map(part => {
    let name;
    try { name = decodeURIComponent(part); }
    catch { throw new Error('The URL contains invalid path encoding.'); }
    if (name.includes('/') || name.includes('\0')) throw new Error('The URL contains an invalid path component.');
    return name;
  });
  return normalizePath(parts.join('/'));
}
function child(path, name) { return `${path.replace(/\/$/, '')}/${name}`; }
function mode(stat) {
  let result = stat.directory ? 'd' : '-';
  for (let bit = 8; bit >= 0; bit--) result += stat.mode & (1 << bit) ? 'rwx'[(8 - bit) % 3] : '-';
  return result;
}
function pathLink(label, path, className = '') {
  const link = document.createElement('a');
  link.textContent = label;
  link.href = pathURL(path);
  link.dataset.path = path;
  link.className = className;
  return link;
}
function breadcrumbs(path) {
  const node = $('breadcrumbs');
  node.replaceChildren(pathLink('root', '/'));
  let walked = '';
  for (const name of path.split('/').filter(Boolean)) {
    walked += '/' + name;
    const separator = document.createElement('span');
    separator.textContent = '/'; separator.className = 'separator';
    node.append(separator, pathLink(name, walked));
  }
  node.lastElementChild.setAttribute('aria-current', 'page');
  const parent = path.replace(/\/[^/]+$/, '') || '/';
  $('up').hidden = path === '/';
  $('up').href = pathURL(parent);
  $('up').dataset.path = parent;
}
async function navigate(path, historyMode = 'push') {
  path = normalizePath(path);
  stopFollow();
  status('Loading…');
  const result = await readPath(path);
  current = result; currentPath = path; dirty = false;
  $('path').value = path;
  const url = pathURL(path);
  if (historyMode === 'push' && location.pathname + location.search + location.hash !== url) history.pushState(null, '', url);
  document.title = `${path} — cloud9`;
  breadcrumbs(path);
  $('directory').hidden = !result.stat.directory;
  $('file').hidden = result.stat.directory;
  if (result.stat.directory) {
    const rows = document.createDocumentFragment();
    const sorted = result.entries.sort((a, b) => Number(b.directory) - Number(a.directory) || a.name.localeCompare(b.name));
    for (const entry of sorted) {
      const row = document.createElement('tr');
      const permissions = document.createElement('td'), name = document.createElement('td'), size = document.createElement('td');
      permissions.className = 'mode'; permissions.textContent = mode(entry);
      name.append(pathLink(entry.name + (entry.directory ? '/' : ''), child(path, entry.name), 'entry' + (entry.directory ? ' directory' : '')));
      size.className = 'size'; size.textContent = entry.directory ? '—' : entry.length.toLocaleString();
      row.append(permissions, name, size); rows.append(row);
    }
    $('entries').replaceChildren(rows);
    $('empty').hidden = sorted.length !== 0;
    status(`${sorted.length} ${sorted.length === 1 ? 'entry' : 'entries'}`);
  } else {
    $('filename').textContent = result.stat.name;
    $('file-mode').textContent = mode(result.stat);
    $('file-size').textContent = `${result.bytes.length.toLocaleString()} bytes`;
    let editable = true;
    try {
      if (result.bytes.includes(0)) throw new Error('Binary');
      $('editor').value = text.decode(result.bytes);
    } catch { editable = false; $('editor').value = ''; }
    $('editor').hidden = !editable;
    $('binary').hidden = editable;
    $('edit-actions').hidden = !editable;
    status('File loaded.');
  }
  updateStat(result);
  markTree(path);
}
function mayLeave() { return !dirty || window.confirm('Discard unsaved changes?'); }
document.addEventListener('click', event => {
  const link = event.target.closest('a[data-path]');
  if (!link || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
  event.preventDefault();
  if (!busy && mayLeave()) action(() => navigate(link.dataset.path));
});
$('path-form').onsubmit = event => {
  event.preventDefault();
  if (mayLeave()) action(() => navigate($('path').value));
};
$('reload').onclick = () => { if (mayLeave()) action(() => navigate(current ? currentPath : locationPath(), 'none')); };
$('editor').oninput = () => { dirty = true; controls(); };
$('save').onclick = () => action(async () => {
  const bytes = new TextEncoder().encode($('editor').value);
  const active = await connectedClient();
  let count;
  try { count = await active.writePath(currentPath, bytes); }
  catch (error) {
    if (active.closed) throw new Error('Save interrupted. The file may be partially written; your edits are still here.');
    throw error;
  }
  // Keep the editor intact if refreshing after a successful write fails.
  dirty = false;
  await navigate(currentPath, 'none');
  status(`Saved ${count.toLocaleString()} bytes.`);
});
$('download').onclick = () => {
  const url = URL.createObjectURL(new Blob([current.bytes]));
  const link = document.createElement('a'); link.href = url; link.download = current.stat.name; link.click();
  setTimeout(() => URL.revokeObjectURL(url), 1000);
};
window.addEventListener('popstate', () => {
  if (busy || !mayLeave()) { history.pushState(null, '', pathURL(currentPath)); return; }
  action(() => navigate(locationPath(), 'none'));
});
window.addEventListener('beforeunload', event => {
  if (dirty) { event.preventDefault(); event.returnValue = ''; }
});

// ---- HTTP /fs view: tree, stat panel, create/rename/delete/upload, follow ----
// These operate over the gateway's HTTP mapping of the same 9P tree, so they
// stay independent of the WebSocket editing session above.
function fsURL(path) { return '/fs' + path.split('/').map(encodeURIComponent).join('/'); }
function parentOf(path) { return path.replace(/\/[^/]+$/, '') || '/'; }
function baseName(path) { return path.split('/').filter(Boolean).pop() || ''; }
function dirContext() { return current && current.stat.directory ? currentPath : parentOf(currentPath); }
function sortEntries(a, b) { return Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name); }

async function fetchDir(path) {
  const response = await fetch(fsURL(path) + (path.endsWith('/') ? '' : '/'), { headers: { accept: 'application/json' } });
  if (!response.ok) throw new Error(`list failed: ${response.status}`);
  return response.json();
}

function renderStat(rows) {
  const dl = $('stat');
  dl.replaceChildren();
  for (const [key, value] of rows) {
    const dt = document.createElement('dt'); dt.textContent = key;
    const dd = document.createElement('dd'); dd.textContent = String(value);
    dl.append(dt, dd);
  }
}
function updateStat(result) {
  const s = result.stat;
  const rows = [
    ['name', s.name || baseName(currentPath) || '/'],
    ['type', s.directory ? 'directory' : 'file'],
    ['mode', mode(s)],
    ['length', s.directory ? '—' : (s.length ?? 0).toLocaleString()],
  ];
  renderStat(rows);
  $('stat-panel').hidden = false;
  // Augment with qid and mtime from a HEAD, best-effort.
  fetch(fsURL(currentPath), { method: 'HEAD' }).then(response => {
    if (!response.ok) return;
    const qid = response.headers.get('x-9p-qid');
    const mtime = response.headers.get('x-9p-mtime');
    const extra = [];
    if (qid) extra.push(['qid', qid]);
    if (mtime && mtime !== '0') extra.push(['mtime', new Date(Number(mtime) * 1000).toISOString()]);
    if (extra.length) renderStat(rows.concat(extra));
  }).catch(() => {});
}

function treeItem(entry, path) {
  const li = document.createElement('li');
  const node = document.createElement('span');
  node.className = 'node' + (entry.dir ? ' dir' : '');
  node.dataset.node = path;
  const twist = document.createElement('span');
  twist.className = 'twist';
  twist.textContent = entry.dir ? '▸' : ' ';
  node.append(twist, document.createTextNode(entry.name));
  li.append(node);
  node.onclick = () => {
    if (entry.dir) toggleTree(li, path, twist);
    else if (!busy && mayLeave()) action(() => navigate(path));
  };
  return li;
}
async function toggleTree(li, path, twist) {
  const open = li.querySelector(':scope > ul');
  if (open) { open.remove(); twist.textContent = '▸'; return; }
  twist.textContent = '▾';
  const ul = document.createElement('ul');
  li.append(ul);
  try {
    for (const entry of (await fetchDir(path)).sort(sortEntries)) ul.append(treeItem(entry, child(path, entry.name)));
  } catch (error) { ul.textContent = '(unavailable)'; }
}
async function loadTree() {
  const root = $('filetree');
  try {
    const entries = (await fetchDir('/')).sort(sortEntries);
    root.replaceChildren(...entries.map(entry => treeItem(entry, '/' + entry.name)));
  } catch { /* upstream not ready; leave the last tree in place */ }
}
function markTree(path) {
  for (const node of $('filetree').querySelectorAll('.node.current')) node.classList.remove('current');
  const match = $('filetree').querySelector(`.node[data-node="${CSS.escape(path)}"]`);
  if (match) match.classList.add('current');
}

let stream = null;
function stopFollow() {
  if (stream) { stream.close(); stream = null; }
  $('follow').classList.remove('active');
  $('stream').hidden = true;
  $('stream').textContent = '';
}
function startFollow() {
  if (!current || current.stat.directory) return;
  $('editor').hidden = true; $('binary').hidden = true; $('edit-actions').hidden = true;
  const view = $('stream');
  view.hidden = false; view.textContent = '';
  $('follow').classList.add('active');
  stream = new EventSource(fsURL(currentPath) + '?follow=1');
  stream.onmessage = event => { view.textContent += event.data + '\n'; view.scrollTop = view.scrollHeight; };
  stream.addEventListener('eof', stopFollow);
  stream.onerror = () => { status('Stream ended.'); stopFollow(); };
}
$('follow').onclick = () => {
  if ($('follow').classList.contains('active')) { stopFollow(); action(() => navigate(currentPath, 'none')); }
  else startFollow();
};

async function mutate(fn, done) {
  if (busy) return;
  busy = true; controls();
  try { await fn(); await loadTree(); status(done); }
  catch (error) { status(error.message, true); }
  finally { busy = false; controls(); }
}
function needOk(response, what) {
  if (!response.ok) throw new Error(`${what} failed: ${response.status}`);
}
$('newfile').onclick = () => {
  const name = window.prompt('New file name:');
  if (!name) return;
  const target = child(dirContext(), name);
  mutate(async () => {
    needOk(await fetch(fsURL(target), { method: 'PUT', body: '' }), 'Create');
    await navigate(target);
  }, `Created ${name}.`);
};
$('newdir').onclick = () => {
  const name = window.prompt('New folder name:');
  if (!name) return;
  const target = child(dirContext(), name);
  mutate(async () => {
    needOk(await fetch(fsURL(target) + '/', { method: 'PUT' }), 'Create folder');
    await navigate(dirContext(), 'none');
  }, `Created ${name}/.`);
};
$('delete').onclick = () => {
  if (currentPath === '/' || !window.confirm(`Delete ${currentPath}?`)) return;
  const parent = parentOf(currentPath);
  mutate(async () => {
    needOk(await fetch(fsURL(currentPath), { method: 'DELETE' }), 'Delete');
    await navigate(parent);
  }, 'Deleted.');
};
$('rename').onclick = () => {
  if (!current || current.stat.directory) { status('Rename is available for files.', true); return; }
  const name = window.prompt('Rename to:', baseName(currentPath));
  if (!name) return;
  const target = child(parentOf(currentPath), name);
  const from = currentPath;
  mutate(async () => {
    // A copy-then-remove rename over HTTP; not atomic.
    const bytes = new Uint8Array(await (await fetch(fsURL(from))).arrayBuffer());
    needOk(await fetch(fsURL(target), { method: 'PUT', body: bytes }), 'Rename (write)');
    needOk(await fetch(fsURL(from), { method: 'DELETE' }), 'Rename (remove)');
    await navigate(target);
  }, `Renamed to ${name}.`);
};
$('upload').onclick = () => { if (!busy) $('upload-input').click(); };
$('upload-input').onchange = () => {
  const file = $('upload-input').files[0];
  if (!file) return;
  const target = child(dirContext(), file.name);
  mutate(async () => {
    needOk(await fetch(fsURL(target), { method: 'PUT', body: file }), 'Upload');
    await navigate(target);
  }, `Uploaded ${file.name}.`);
  $('upload-input').value = '';
};

action(() => navigate(locationPath(), 'none')).then(loadTree);