File size: 5,097 Bytes
54a51e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SampleViewer access proxy. Viewer pages for these sections live in gated HF datasets
// (under viewer/<section>/); this worker serves them on the Space origin using the
// visitor's own HF OAuth token (scope gated-repos). Nothing here is secret.
const SECTIONS = {
  egoSample: 'faradayfuture/egoSample',
  teleopSample: 'faradayfuture/teleopSample',
  motionSample: 'faradayfuture/motionSample',
};
const VERSION = 'sv-1';
const TYPES = {
  html: 'text/html; charset=utf-8', htm: 'text/html; charset=utf-8', js: 'text/javascript; charset=utf-8',
  mjs: 'text/javascript; charset=utf-8', css: 'text/css; charset=utf-8', json: 'application/json',
  mp4: 'video/mp4', webm: 'video/webm', jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png',
  webp: 'image/webp', gif: 'image/gif', svg: 'image/svg+xml', glb: 'model/gltf-binary', bin: 'application/octet-stream',
  txt: 'text/plain; charset=utf-8', csv: 'text/csv; charset=utf-8', wasm: 'application/wasm',
};

self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', e => e.waitUntil(self.clients.claim()));

let TOKEN = null;
async function token() {
  if (TOKEN) return TOKEN;
  try { const r = await (await caches.open('sv-auth')).match('/__sv_token'); TOKEN = r ? await r.text() : null; } catch (e) {}
  return TOKEN;
}
self.addEventListener('message', e => {
  const d = e.data || {};
  if (d.type !== 'token') return;
  TOKEN = d.token || null;
  cache.clear();
  e.waitUntil(caches.open('sv-auth').then(c => TOKEN ? c.put('/__sv_token', new Response(TOKEN)) : c.delete('/__sv_token'))
    .then(() => e.source && e.source.postMessage({ type: 'token-ok' })));
});

// small in-memory LRU of whole files (so video Range requests are served locally)
const cache = new Map(); let cacheBytes = 0; const CACHE_MAX = 300e6;
function remember(key, buf) {
  if (buf.byteLength > 80e6) return;
  cache.set(key, buf); cacheBytes += buf.byteLength;
  for (const [k, v] of cache) { if (cacheBytes <= CACHE_MAX) break; cache.delete(k); cacheBytes -= v.byteLength; }
}
const inflight = new Map();

self.addEventListener('fetch', e => {
  const u = new URL(e.request.url);
  if (u.origin !== self.location.origin || e.request.method !== 'GET') return;
  const m = u.pathname.match(/^\/(egoSample|teleopSample|motionSample)(\/.*)?$/);
  if (!m) return;
  let path = decodeURIComponent((m[2] || '/').slice(1));
  if (path === '' || path.endsWith('/')) path += 'index.html';
  e.respondWith(serve(e.request, m[1], path, u));
});

function toGate(u, why) {
  return Response.redirect(new URL('/gate.html?to=' + encodeURIComponent(u.pathname + u.search) + (why ? '&why=' + why : ''), u).href, 302);
}

async function getFile(sec, path, tok) {
  const key = sec + '/' + path;
  if (cache.has(key)) { const b = cache.get(key); cache.delete(key); cache.set(key, b); return { status: 200, buf: b }; }
  if (inflight.has(key)) return inflight.get(key);
  const p = (async () => {
    const url = 'https://huggingface.co/datasets/' + SECTIONS[sec] + '/resolve/main/viewer/' + sec + '/' + path.split('/').map(encodeURIComponent).join('/');
    const r = await fetch(url, { headers: { Authorization: 'Bearer ' + tok }, cache: 'no-store' });
    if (!r.ok) return { status: r.status };
    const buf = await r.arrayBuffer(); remember(key, buf);
    return { status: 200, buf };
  })().finally(() => inflight.delete(key));
  inflight.set(key, p);
  return p;
}

async function serve(req, sec, path, u) {
  const nav = req.mode === 'navigate';
  const tok = await token();
  if (!tok) return nav ? toGate(u, 'login') : new Response('', { status: 401 });
  let f;
  try { f = await getFile(sec, path, tok); } catch (err) { return new Response('Network error', { status: 502 }); }
  if (f.status === 401) return nav ? toGate(u, 'login') : new Response('', { status: 401 });
  if (f.status === 403) return nav ? toGate(u, 'denied') : new Response('', { status: 403 });
  if (f.status !== 200) return new Response('Not found', { status: f.status === 404 ? 404 : 502 });
  const ext = (path.split('.').pop() || '').toLowerCase();
  const type = TYPES[ext] || 'application/octet-stream';
  const total = f.buf.byteLength;
  const base = { 'Content-Type': type, 'Accept-Ranges': 'bytes', 'Cache-Control': 'private, no-store', 'X-Sample-Viewer': VERSION };
  const range = req.headers.get('range');
  const mm = range && /bytes=(\d*)-(\d*)/.exec(range);
  if (mm && (mm[1] !== '' || mm[2] !== '')) {
    let start, end;
    if (mm[1] === '') { start = Math.max(0, total - Number(mm[2])); end = total - 1; }
    else { start = Number(mm[1]); end = mm[2] === '' ? total - 1 : Math.min(Number(mm[2]), total - 1); }
    if (start >= total || start > end) return new Response('', { status: 416, headers: { 'Content-Range': 'bytes */' + total } });
    return new Response(f.buf.slice(start, end + 1), { status: 206, headers: { ...base, 'Content-Range': `bytes ${start}-${end}/${total}`, 'Content-Length': String(end - start + 1) } });
  }
  return new Response(f.buf, { status: 200, headers: { ...base, 'Content-Length': String(total) } });
}