e6b6107e02
* Lex SDK improvements * changeset * tidy * tidy * Fix `include` option in `tsconfig.test.json` files * tidy * ignore "require" in cjs files * tidy * tidy * Improve error management * rename xrpc-error file * tests * fix lint * lint * tidy * puppeteer cache busting * fix oauth tests * tidy * wip * tidy * tidy * tidy * Forbid use of unsafe integers
53 lines
1.3 KiB
JavaScript
Executable File
53 lines
1.3 KiB
JavaScript
Executable File
/* eslint-env node, commonjs */
|
|
|
|
'use strict'
|
|
|
|
const { once } = require('node:events')
|
|
const { createServer } = require('node:http')
|
|
const files = require('./dist/files.json')
|
|
|
|
exports.middleware = middleware
|
|
function middleware(
|
|
req,
|
|
res,
|
|
next = (err) => {
|
|
if (err) console.error(err)
|
|
|
|
const { statusCode, statusMessage } = err
|
|
? { statusCode: 404, statusMessage: 'Not Found' }
|
|
: { statusCode: 500, statusMessage: 'Internal Server Error' }
|
|
|
|
res
|
|
.writeHead(statusCode, statusMessage, { 'content-type': 'text/plain' })
|
|
.end(statusMessage)
|
|
},
|
|
) {
|
|
const path = req.url?.split('?')[0].slice(1) || 'index.html'
|
|
const file = Object.hasOwn(files, path) ? files[path] : null
|
|
|
|
if (file) {
|
|
res
|
|
.writeHead(200, 'OK', { 'content-type': file.mime })
|
|
.end(Buffer.from(file.data, 'base64'))
|
|
} else {
|
|
next()
|
|
}
|
|
}
|
|
|
|
exports.start = start
|
|
async function start(port = 0) {
|
|
const server = createServer(middleware)
|
|
server.listen(port)
|
|
await once(server, 'listening')
|
|
return server
|
|
}
|
|
|
|
if (require.main === module) {
|
|
const port = Number(process.argv[2] || process.env.PORT || 0)
|
|
start(port).then((server) => {
|
|
const address = server.address()
|
|
const port = typeof address === 'string' ? address : address && address.port
|
|
console.log(`Listening on http://127.0.0.1:${port}/`)
|
|
})
|
|
}
|