File size: 2,334 Bytes
e8efdb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const http = require('http'), fs = require('fs'), path = require('path');
const PORT = 8150;
const ROOT = __dirname;
const SPLITS = path.join(ROOT, 'model_splits');

const MIME = {
  '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript',
  '.json': 'application/json', '.gguf': 'application/octet-stream',
  '.wasm': 'application/wasm', '.ts': 'text/javascript',
};

http.createServer((req, res) => {
  if (req.method === 'OPTIONS') {
    res.writeHead(200, {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET,HEAD',
      'Access-Control-Allow-Headers': 'Content-Type,Range',
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin',
    });
    return res.end();
  }

  let p = decodeURIComponent(req.url.split('?')[0]);
  if (p === '/') p = '/index.html';

  let fp;
  if (p.startsWith('/model/')) {
    fp = path.join(SPLITS, p.slice('/model/'.length));
  } else {
    fp = path.join(ROOT, p);
  }

  fs.stat(fp, (e, st) => {
    if (e) { res.writeHead(404); return res.end('not found: ' + fp); }
    const total = st.size;
    const range = req.headers.range;
    const ct = MIME[path.extname(fp)] || 'application/octet-stream';

    const headers = {
      'Content-Type': ct,
      'Accept-Ranges': 'bytes',
      'Access-Control-Allow-Origin': '*',
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin',
    };

    if (range) {
      const m = range.match(/bytes=(\d+)-(\d*)/);
      if (m) {
        const start = parseInt(m[1]);
        const end = m[2] ? parseInt(m[2]) : total - 1;
        headers['Content-Range'] = 'bytes ' + start + '-' + end + '/' + total;
        headers['Content-Length'] = end - start + 1;
        res.writeHead(206, headers);
        fs.createReadStream(fp, { start, end }).pipe(res);
        return;
      }
    }

    headers['Content-Length'] = total;
    res.writeHead(200, headers);
    fs.createReadStream(fp).pipe(res);
  });
}).listen(PORT, () => {
  console.log('Gemma WebGPU on :' + PORT);
  console.log('Model splits: ' + SPLITS);
  const files = fs.readdirSync(SPLITS).filter(f => f.endsWith('.gguf'));
  console.log('Split files: ' + files.length);
  console.log('CORS + COEP/COOP headers enabled for wllama multi-threading');
});