File size: 3,479 Bytes
e6c51f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const http = require('http');
const fs = require('fs').promises;
const path = require('path');

// This file is used for testing wasm build from emscripten
// Example build command:
// emcmake cmake -B build-wasm -DGGML_WEBGPU=ON -DLLAMA_OPENSSL=OFF
// cmake --build build-wasm --target test-backend-ops -j

const PORT = 8080;
const STATIC_DIR = path.join(__dirname, '../build-wasm/bin');
console.log(`Serving static files from: ${STATIC_DIR}`);

const mimeTypes = {
  '.html': 'text/html',
  '.js': 'text/javascript',
  '.css': 'text/css',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.gif': 'image/gif',
  '.svg': 'image/svg+xml',
  '.json': 'application/json',
  '.woff': 'font/woff',
  '.woff2': 'font/woff2',
};

async function generateDirListing(dirPath, reqUrl) {
  const files = await fs.readdir(dirPath);
  let html = `

    <!DOCTYPE html>

    <html>

    <head>

      <title>Directory Listing</title>

      <style>

        body { font-family: Arial, sans-serif; padding: 20px; }

        ul { list-style: none; padding: 0; }

        li { margin: 5px 0; }

        a { text-decoration: none; color: #0066cc; }

        a:hover { text-decoration: underline; }

      </style>

    </head>

    <body>

      <h1>Directory: ${reqUrl}</h1>

      <ul>

  `;

  if (reqUrl !== '/') {
    html += `<li><a href="../">../ (Parent Directory)</a></li>`;
  }

  for (const file of files) {
    const filePath = path.join(dirPath, file);
    const stats = await fs.stat(filePath);
    const link = encodeURIComponent(file) + (stats.isDirectory() ? '/' : '');
    html += `<li><a href="${link}">${file}${stats.isDirectory() ? '/' : ''}</a></li>`;
  }

  html += `

      </ul>

    </body>

    </html>

  `;
  return html;
}

const server = http.createServer(async (req, res) => {
  try {
    // Set COOP and COEP headers
    res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
    res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
    res.setHeader('Pragma', 'no-cache');
    res.setHeader('Expires', '0');

    const filePath = path.join(STATIC_DIR, decodeURIComponent(req.url));
    const stats = await fs.stat(filePath);

    if (stats.isDirectory()) {
      const indexPath = path.join(filePath, 'index.html');
      try {
        const indexData = await fs.readFile(indexPath);
        res.writeHeader(200, { 'Content-Type': 'text/html' });
        res.end(indexData);
      } catch {
        // No index.html, generate directory listing
        const dirListing = await generateDirListing(filePath, req.url);
        res.writeHeader(200, { 'Content-Type': 'text/html' });
        res.end(dirListing);
      }
    } else {
      const ext = path.extname(filePath).toLowerCase();
      const contentType = mimeTypes[ext] || 'application/octet-stream';
      const data = await fs.readFile(filePath);
      res.writeHeader(200, { 'Content-Type': contentType });
      res.end(data);
    }
  } catch (err) {
    if (err.code === 'ENOENT') {
      res.writeHeader(404, { 'Content-Type': 'text/plain' });
      res.end('404 Not Found');
    } else {
      res.writeHeader(500, { 'Content-Type': 'text/plain' });
      res.end('500 Internal Server Error');
    }
  }
});

server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});