-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssr.js
36 lines (31 loc) · 1.33 KB
/
ssr.js
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
#!/usr/bin/env node
/* global Location */
import { randomBytes } from 'node:crypto'
import { readFile } from 'node:fs/promises'
import { createServer } from 'node:http'
import { reset, toHTML } from './minidom.js'
// @ts-expect-error
const input = globalThis?.process?.argv?.[2] || globalThis?.Deno?.args?.[0]
const server = createServer()
server.on('request', async (req, res) => {
// @ts-expect-error
globalThis.location = new Location(new URL(req.url, `http://${req.headers.host}`))
// @ts-expect-error
globalThis.skruvSSRScript = await readFile(input, 'utf8')
// Force reexecution for each run by appending a query-string
const frontend = await import(process.cwd() + '/' + input + '?' + randomBytes(32).toString('hex'))
// TODO: Should not be needed when top-level-await is properly supported
if (frontend.default instanceof Function) { await frontend.default() }
/** @type {Record<string, string>} */
const headers = {}
// @ts-expect-error
const responseBody = toHTML(document.documentElement, '', headers)
reset()
res.statusCode = parseInt(headers.status) || 200
for (const key in headers) {
res.setHeader(key, headers[key])
}
res.end(responseBody)
})
server.listen(process.env.PORT || 8000)
console.log(`listening on http://127.0.0.1:${process.env.PORT || 8000}. Change port with the environment variable PORT`)