-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
128 lines (124 loc) · 3.75 KB
/
server.js
File metadata and controls
128 lines (124 loc) · 3.75 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import express from 'express'
import https from 'node:https'
import http from 'node:http'
import * as path from 'node:path'
import * as fs from 'node:fs/promises'
import * as fs2 from 'node:fs'
// import { JSDOM } from "jsdom"
import { parse as parseHTML } from 'node-html-parser'
const
isProduction = process.env.NODE_ENV === "production",
normalizePath = pn => (pn = `${pn ?? ''}`) && (
(pn.startsWith('./') ? './' : '')
+ path.normalize(pn)
),
__dirname = normalizePath(import.meta.dirname + "/src"),
host = process.env.HOST ?? '*.localhost',
port = process.env.PORT ?? 8234,
gscToken = process.env.GSC_TOKEN,
ssh = await (async () => {
try {
// const dir = path.join(process.env.HOME ?? import.meta.resolve('../'))
// return await (await import(path.join(dir, '.ssh/index.js'))).default()
return await (await import("../.ssh/index.js")).default()
} catch (e) {
console.error(e)
}
})(),
app = express(),
server = (ssh ? https : http).createServer(...(
ssh ? [ssh, app] : [app]
)),
urlRegex = /^([^?]*)(\?[^#]+)?(#.*)?$/s,
getUrlParts = url => {
url = `${url ?? ''}`
const arr = url.match(urlRegex)
arr[0] = normalizePath(arr[1])
const srch = arr[2]
arr[1] = srch && new URLSearchParams(srch)
arr[2] = arr[3] ?? ''
arr.length = 3; return arr
},
mergeUrlParts = (pn, srch, hash) => {
pn = normalizePath(pn)
srch = `${srch ?? ''}`, hash = `${hash ?? ''}`
if (!/^\.{0,2}\//.test(pn)) pn = `/${pn}`
if (srch && !srch.startsWith('?')) srch = `?${srch}`
if (hash && !hash.startsWith('#')) hash = `#${srch}`
return pn + srch + hash
},
resolvePath = (pn, ext) => {
pn = normalizePath(pn)
if (!ext) return pn
let pn1 = pn + (pn.endsWith('.') ? '' : '.') + ext
if (fstF.has(pn1)) return pn1
let pn2 = pn + (pn.endsWith('/') ? '' : '/') + `index.${ext}`
if (fstF.has(pn2)) return pn2
return pn
}
app.use((req, res, next) => {
if (!/^(?:GET|HEAD|OPTIONS)$/i.test(req.method)) {
res
.set('Allow', 'GET, HEAD, OPTIONS')
.status(405).end()
return
}
res.set({
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'credentialless',
'Origin-Agent-Cluster': '?1'
})
next()
})
app.use(async (req, res, next) => {
let status, content, contentType = "text/plain"
try {
const srcUrl = req.originalUrl
let [pn, search, hash] = getUrlParts(srcUrl)
switch (pn) {
case "/index.html":
res.redirect(301, "/")
return
case "/github.svg":
case "/": break
default:
res.status(404).end()
return
}
let accept = search?.get("accept")
if (accept != null) contentType = accept, content = ""
else {
if (pn === "/") pn = "/index.html"
const url = normalizePath(__dirname + pn)
try {
content = await fs.readFile(url)
if (pn === "/index.html") {
const root = parseHTML(content)
const metaEl = parseHTML(
"<meta name='google-site-verification' " +
`content="${gscToken ?? ""}">`
)
root.querySelector("head").prepend(metaEl)
content = root.outerHTML
}
switch (pn) {
case "/index.html": contentType = "text/html"; break
case "/github.svg": contentType = "image/svg+xml"
}
} catch (err) {
status = 404
throw err
}
}
} catch (err) {
console.error(err)
content = err.stack
status ??= 500
}
res.set({ 'Content-Type': contentType })
res.status(status ?? 200).send(content ?? "")
})
if (!isProduction) server.listen(port, () => console.log(
`Server is listening on: \x1b[32mhttps//${host}/${port}\x1b[0m`
))
export default isProduction ? app : undefined