This repository has been archived by the owner on Jan 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (56 loc) · 1.7 KB
/
server.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
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
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import nodeHTMLParser from 'node-html-parser'
import express from 'express'
import { config } from 'dotenv'
import { getEmbedTitle } from './embed.js'
import { TZ_NAMES } from './util.js'
config()
const app = express()
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
let indexContent
try {
indexContent = fs.readFileSync(
path.resolve(__dirname, '../build', 'index.html')
)
} catch (e) {
console.error(
'Failed to load "build/index.html" - make sure you run "npm run build" first'
)
process.exit(1)
}
app.get('/', (req, res) => {
const query = req.query
if (query.z || query.l) {
console.log(
`${new Date().toLocaleString()} ${req.get('user-agent')} - l=${
query.l || ''
} z=${query.z || ''} s=${query.s || ''} f=${query.f || ''}`
)
}
const parsedIndex = nodeHTMLParser.parse(indexContent)
const headElement = parsedIndex.querySelector('head')
headElement.insertAdjacentHTML(
'afterbegin',
'<script>window.__SNOWSTAMP_DYNAMIC__ = true</script>'
)
const embedTitle = getEmbedTitle(query)
if (embedTitle) {
console.log(
`Embed: ${TZ_NAMES[parseInt(query.z, 36)] || ''} - ${embedTitle}`
)
const metaTags = headElement.querySelectorAll('meta')
const metaTitleOG = metaTags.find(
(node) => node.attributes?.property === 'og:title'
)
metaTitleOG.setAttribute('content', embedTitle)
} else if (query.z || query.l) {
console.log('Invalid embed!')
}
res.send(parsedIndex.toString())
})
app.use('/', express.static(path.resolve(__dirname, `../build`)))
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Server started on port ${port}`))