serve-lunasqu.ee/applications/highlight-termbin/index.js

126 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-02-05 16:27:36 +00:00
const express = require('express')
const path = require('path')
const fs = require('fs')
2022-01-03 19:53:56 +00:00
const crypto = require('crypto')
2019-02-05 16:27:36 +00:00
const fsp = fs.promises
const router = express.Router()
const cfgLoader = require(path.join('..', '..', 'config-loader'))(path.join(__dirname, 'config.json'), {
root: path.join(process.cwd(), '..', 'fiche', 'code'),
2019-02-26 09:52:23 +00:00
style: 'tomorrow-night',
2022-01-03 19:53:56 +00:00
index: 'index.html',
2019-02-05 16:27:36 +00:00
})
2022-01-03 19:53:56 +00:00
function calculateHash(file, type){
const testFile = fs.readFileSync(file);
const shasum = crypto.createHash('sha384')
.update(testFile)
.digest('base64');
return `sha384-${shasum}`
}
2020-09-13 17:24:36 +00:00
function encodeSpecial (string) {
2020-01-06 20:38:44 +00:00
let i = string.length
2020-09-13 17:24:36 +00:00
const a = []
2020-01-06 20:38:44 +00:00
2020-09-13 17:24:36 +00:00
while (i--) {
2020-01-06 20:38:44 +00:00
var iC = string[i].charCodeAt()
2020-09-13 17:24:36 +00:00
if (iC < 65 || iC > 127 || (iC > 90 && iC < 97)) {
2020-01-06 20:38:44 +00:00
a[i] = '&#' + iC + ';'
2020-09-13 17:24:36 +00:00
} else {
2020-01-06 20:38:44 +00:00
a[i] = string[i]
}
}
return a.join('')
}
2019-02-05 16:27:36 +00:00
async function init () {
2020-09-13 17:24:36 +00:00
const config = await cfgLoader
const root = path.resolve(config.root)
2022-01-03 19:53:56 +00:00
const themeFile = path.join(__dirname, `${config.style}.css`)
const scriptFile = path.join(__dirname, 'highlight.min.js')
const themeFileHash = calculateHash(themeFile)
const scriptFileHash = calculateHash(scriptFile)
2019-02-05 16:27:36 +00:00
2022-11-27 06:46:53 +00:00
await fsp.mkdir(root, { recursive: true })
2019-02-05 16:27:36 +00:00
await fsp.access(root, fs.constants.F_OK)
2022-01-03 19:53:56 +00:00
router.get('/theme.css', (req, res) => {
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
res.sendFile(themeFile)
});
router.get('/script.js', (req, res) => {
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
res.sendFile(scriptFile)
});
2019-02-05 16:27:36 +00:00
router.get('/:name', async (req, res, next) => {
2020-09-13 17:24:36 +00:00
const name = req.params.name
2019-02-05 16:47:08 +00:00
if (name.length > 5) {
2019-02-05 16:27:36 +00:00
try {
2020-09-13 17:24:36 +00:00
const text = await fsp.readFile(path.join(root, 'index.html'))
2019-02-05 16:27:36 +00:00
res.send(text)
} catch (e) {
res.status(403)
}
return res.end()
}
2020-09-13 17:24:36 +00:00
const fichePath = path.join(root, name, 'index.txt')
const ua = req.get('User-Agent')
2019-02-05 16:27:36 +00:00
let reqRaw = req.query.raw != null
2019-08-06 12:35:01 +00:00
if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true
2019-02-05 16:27:36 +00:00
2020-09-13 17:24:36 +00:00
let text
2019-02-05 16:27:36 +00:00
try {
2020-09-13 17:24:36 +00:00
text = await fsp.readFile(fichePath, { encoding: 'utf8' })
2019-02-05 16:27:36 +00:00
} catch (e) {
return res.status(404).end()
}
2019-02-05 17:15:00 +00:00
res.header('Cache-Control', 'max-age=' + 7 * 24 * 60 * 60 * 1000)
2019-02-05 16:27:36 +00:00
if (reqRaw) {
return res.set('Content-Type', 'text/plain').send(text)
}
2020-09-13 17:24:36 +00:00
const etext = encodeSpecial(text)
const payload = `
<!DOCTYPE html>
<html class="hljs">
<head>
2022-01-03 19:53:56 +00:00
<link rel="stylesheet" href="theme.css" integrity="${themeFileHash}" />
<script src="script.js" integrity="${scriptFileHash}"></script>
2020-09-13 17:24:36 +00:00
</head>
<body>
<pre><code>${etext}</code></pre>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>
`
res.set('Content-Type', 'text/html; charset=UTF-8').send(payload)
2019-02-05 16:27:36 +00:00
})
2019-02-26 09:52:23 +00:00
router.get('/', (req, res, next) => {
if (!config.index) {
return next()
}
2019-02-26 10:06:47 +00:00
let inp = config.index
if (inp.indexOf('/') !== 0) {
inp = path.join(__dirname, inp)
2019-02-26 09:52:23 +00:00
} else {
2019-02-26 10:06:47 +00:00
inp = path.resolve(inp)
2019-02-26 09:52:23 +00:00
}
2019-02-26 10:06:47 +00:00
res.sendFile(inp)
2019-02-26 09:52:23 +00:00
})
2019-02-05 16:27:36 +00:00
return router
}
module.exports = init