-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.mjs
More file actions
98 lines (84 loc) · 2.88 KB
/
Copy pathserver.mjs
File metadata and controls
98 lines (84 loc) · 2.88 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
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
import http from 'node:http'
import { createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
const HOST = process.env.HOST || '0.0.0.0'
const PORT = Number(process.env.PORT || 3000)
const rootDir = path.resolve(process.cwd(), 'dist')
function contentTypeFor(filePath) {
switch (path.extname(filePath).toLowerCase()) {
case '.html':
return 'text/html; charset=utf-8'
case '.js':
return 'text/javascript; charset=utf-8'
case '.css':
return 'text/css; charset=utf-8'
case '.json':
return 'application/json; charset=utf-8'
case '.svg':
return 'image/svg+xml'
case '.png':
return 'image/png'
case '.jpg':
case '.jpeg':
return 'image/jpeg'
case '.webp':
return 'image/webp'
case '.ico':
return 'image/x-icon'
case '.txt':
return 'text/plain; charset=utf-8'
default:
return 'application/octet-stream'
}
}
function send(res, statusCode, headers) {
res.writeHead(statusCode, headers)
res.end()
}
async function tryServeFile(req, res, filePath, { cache } = { cache: 'none' }) {
try {
const fileStat = await stat(filePath)
if (!fileStat.isFile()) return false
const headers = {
'Content-Type': contentTypeFor(filePath),
'Content-Length': String(fileStat.size),
'Cache-Control':
cache === 'immutable' ? 'public, max-age=31536000, immutable' : 'no-cache',
}
if (req.method === 'HEAD') {
send(res, 200, headers)
return true
}
res.writeHead(200, headers)
createReadStream(filePath).pipe(res)
return true
} catch {
return false
}
}
const server = http.createServer(async (req, res) => {
if (!req.url) return send(res, 400, { 'Content-Type': 'text/plain; charset=utf-8' })
if (req.method !== 'GET' && req.method !== 'HEAD') {
return send(res, 405, { 'Content-Type': 'text/plain; charset=utf-8' })
}
const parsed = url.parse(req.url)
const pathname = decodeURIComponent(parsed.pathname || '/')
const requestedPath = pathname.replace(/^\/+/, '')
const candidate = path.resolve(rootDir, requestedPath || 'index.html')
if (!candidate.startsWith(rootDir + path.sep) && candidate !== rootDir) {
return send(res, 400, { 'Content-Type': 'text/plain; charset=utf-8' })
}
const cache = pathname.startsWith('/assets/') ? 'immutable' : 'none'
if (await tryServeFile(req, res, candidate, { cache })) return
// SPA fallback: serve index.html for unknown routes (but keep 404 for missing assets)
if (!pathname.startsWith('/assets/')) {
const indexPath = path.join(rootDir, 'index.html')
if (await tryServeFile(req, res, indexPath, { cache: 'none' })) return
}
send(res, 404, { 'Content-Type': 'text/plain; charset=utf-8' })
})
server.listen(PORT, HOST, () => {
console.log(`graph-viewer: serving ${rootDir} on http://${HOST}:${PORT}`)
})