-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (47 loc) · 1.88 KB
/
server.js
File metadata and controls
60 lines (47 loc) · 1.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
import { createServer, request } from 'http'
import { readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import { rootDir } from './utils.js'
import { generateExoFile } from './exo-parser.js'
// Start esbuild's server on a random local port
const { generate, serve } = await import('./build.js')
const { host: hostname, port } = await serve()
// Set domain before we run the tests
const PORT = process.env.PORT || port + 1
process.env.DOMAIN = process.env.DOMAIN || `http://localhost:${PORT}`
// run tests
await (await import('./test-runner.js')).run()
// load CF worker mock
const { API, sendResponse } = await import('./mocks.js')
// load KV data
const db = join(rootDir, '.nan.kv.json')
NAN.entries = await readFile(db, 'utf8')
.then(JSON.parse)
.catch(() => ({}))
// Then start a proxy server on port 3000
createServer(async (req, res) => {
const { url: path, method } = req
const url = new URL(`${process.env.DOMAIN}${path}`)
console.log(req.method, url.pathname, Object.fromEntries(url.searchParams))
if (path.startsWith('/js/')) {
// Forward each incoming request to esbuild
const options = { port, method, hostname, path, headers: req.headers }
const proxyReq = request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers)
proxyRes.pipe(res, { end: true })
})
// Forward the body of the request to esbuild
return req.pipe(proxyReq, { end: true })
}
// Handle the root index
if (!url.pathname.startsWith('/api/')) return res.end(await generate('index'))
// Forward to the mock of cloudflare worker
const { body, options } = await API(req)
// Save KV data
await writeFile(db, JSON.stringify(NAN.entries), 'utf8')
// Apply headers from the worker
sendResponse({ body, options, res })
}).listen(PORT, async () => {
await generateExoFile()
console.log(`Dev server ready on ${process.env.DOMAIN}`)
})