-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
110 lines (94 loc) · 3.06 KB
/
Copy pathindex.js
File metadata and controls
110 lines (94 loc) · 3.06 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
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env node
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { readFile } from 'node:fs/promises'
import Fastify from 'fastify'
import fastifyStatic from '@fastify/static'
import { generateHomepage } from './homepage.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const fastify = Fastify()
const hostname = 'localhost'
let port = 8080
const enableCORS = true
const enableWasmMultithreading = true
// Support for command-line argument to specify the target directory
const targetDir = process.argv[2] ? join(process.cwd(), process.argv[2]) : process.cwd()
// Add response headers based on request
fastify.addHook('onSend', (request, reply, payload, done) => {
const path = request.url
if (enableWasmMultithreading &&
(
path == '/' ||
path.includes('.js') ||
path.includes('.html') ||
path.includes('.htm')
)
) {
reply.header('Cross-Origin-Opener-Policy', 'same-origin')
reply.header('Cross-Origin-Embedder-Policy', 'require-corp')
reply.header('Cross-Origin-Resource-Policy', 'cross-origin')
}
if (enableCORS) {
reply.header('Access-Control-Allow-Origin', '*')
}
if (path.endsWith('.br')) {
reply.header('Content-Encoding', 'br')
} else if (path.endsWith('.gz')) {
reply.header('Content-Encoding', 'gzip')
}
if (path.includes('.wasm')) {
reply.header('Content-Type', 'application/wasm')
} else if (path.includes('.js')) {
reply.header('Content-Type', 'application/javascript')
} else if (path.includes('.json')) {
reply.header('Content-Type', 'application/json')
} else if (
path.includes('.data') ||
path.includes('.bundle') ||
path.endsWith('.unityweb')
) {
reply.header('Content-Type', 'application/octet-stream')
}
if (request.headers['cache-control'] == 'no-cache' &&
(
request.headers['if-modified-since'] ||
request.headers['if-none-match']
)
) {
delete request.headers['cache-control']
}
done()
})
// Homepage route
fastify.get('/', async (request, reply) => {
reply.header('Content-Type', 'text/html')
reply.send(await generateHomepage(targetDir))
})
// Register static file serving
fastify.register(fastifyStatic, {
root: targetDir,
prefix: '/',
immutable: true
})
const start = async (currentPort) => {
try {
await fastify.listen({ port: currentPort, host: hostname })
console.log(`Web server serving at http://${hostname}:${currentPort}`)
} catch (err) {
if (err.code === 'EADDRINUSE') {
console.log(`Port ${currentPort} is in use, trying ${currentPort + 1}...`)
await start(currentPort + 1)
} else {
console.error(err)
process.exit(1)
}
}
}
start(port)
// Handle process termination
process.on('SIGINT', async () => {
await fastify.close()
console.log('Server stopped.')
process.exit(0)
})