-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
57 lines (53 loc) · 2.08 KB
/
Copy pathdev.js
File metadata and controls
57 lines (53 loc) · 2.08 KB
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
'use strict';
const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const port = Number(process.env.PORT || 4173);
const host = process.env.HM_PREVIEW_HOST || '127.0.0.1';
const root = path.join(__dirname, 'dist');
const types = {'.html':'text/html; charset=utf-8','.js':'text/javascript','.css':'text/css','.svg':'image/svg+xml','.webp':'image/webp','.png':'image/png','.jpg':'image/jpeg','.woff2':'font/woff2','.ico':'image/x-icon','.txt':'text/plain','.xml':'application/xml'};
const server = http.createServer((req, res) => {
let file;
try {
const url = new URL(req.url, 'http://localhost');
if (url.pathname === '/api/intake-config') {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({enabled:false, turnstileSiteKey:null, action:'brand_inquiry'}));
return;
}
if (url.pathname.startsWith('/api/')) {
res.writeHead(503, {'Content-Type':'application/json'});
res.end('{"ok":false,"error":"Preview delivery disabled"}');
return;
}
file = path.resolve(root, '.' + decodeURIComponent(url.pathname));
} catch {
res.writeHead(400, {'Content-Type':'text/plain; charset=utf-8'});
res.end('Bad request');
return;
}
if (!file.startsWith(root + path.sep) && file !== root) {
res.writeHead(403);
res.end();
return;
}
try {
if (fs.statSync(file).isDirectory()) file = path.join(file, 'index.html');
res.setHeader('Content-Type', types[path.extname(file)] || 'application/octet-stream');
res.end(fs.readFileSync(file));
} catch {
let body = 'Not found';
try {
body = fs.readFileSync(path.join(root, '404.html'));
} catch {
// A missing preview build must still produce a response.
}
res.writeHead(404, {'Content-Type':'text/html; charset=utf-8'});
res.end(body);
}
});
server.listen(port, host, () => {
const address = server.address();
const hostname = address.family === 'IPv6' ? `[${address.address}]` : address.address;
console.log(`Hammad Media preview at http://${hostname}:${address.port}`);
});