Update index.js
Browse files
index.js
CHANGED
|
@@ -1,30 +1,64 @@
|
|
| 1 |
-
const node = {}
|
| 2 |
-
|
| 3 |
for (let m of modules.split`|`) node[m] = require(`node:${m}`)
|
| 4 |
|
| 5 |
-
const
|
| 6 |
express = require('express'),
|
| 7 |
morgan = require('morgan'),
|
|
|
|
| 8 |
serveIndex = require('serve-index')
|
| 9 |
|
| 10 |
const limitSize = '69mb',
|
| 11 |
tmpDir = node.os.tmpdir(),
|
| 12 |
{ env } = node.process,
|
| 13 |
-
PORT = env.SERVER_PORT || env.PORT || 7860
|
| 14 |
-
|
| 15 |
-
const logger = (...args) => console.log(
|
| 16 |
-
new Date().toLocaleTimeString(
|
| 17 |
-
'fr', { timeZone: 'Asia/Jakarta' }
|
| 18 |
-
),
|
| 19 |
-
...args
|
| 20 |
-
)
|
| 21 |
|
| 22 |
const execPromise = node.util.promisify(
|
| 23 |
node.child_process.exec
|
| 24 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
express()
|
| 27 |
.enable('trust proxy')
|
|
|
|
| 28 |
.set('json spaces', 2)
|
| 29 |
.use(express.json({ limit: limitSize }))
|
| 30 |
.use(express.urlencoded({ extended: true, limit: limitSize }))
|
|
@@ -34,7 +68,12 @@ express()
|
|
| 34 |
express.static(tmpDir),
|
| 35 |
serveIndex(tmpDir, { hidden: true, icons: true })
|
| 36 |
)
|
| 37 |
-
.all('/', (_, res) =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
.post('/eval', async (req, res) => {
|
| 39 |
let code = req.body.code, o
|
| 40 |
code = `(async function MengEval() { ${code} })()`
|
|
@@ -42,4 +81,4 @@ express()
|
|
| 42 |
catch (e) { o = e }
|
| 43 |
finally { res.send(node.util.format(o)) }
|
| 44 |
})
|
| 45 |
-
.listen(PORT, logger)
|
|
|
|
| 1 |
+
const node = {},
|
| 2 |
+
modules = 'child_process|fs|os|process|timers|util'
|
| 3 |
for (let m of modules.split`|`) node[m] = require(`node:${m}`)
|
| 4 |
|
| 5 |
+
const prettyBytes = require('bytes'),
|
| 6 |
express = require('express'),
|
| 7 |
morgan = require('morgan'),
|
| 8 |
+
{ default: prettyMs } = require('pretty-ms'),
|
| 9 |
serveIndex = require('serve-index')
|
| 10 |
|
| 11 |
const limitSize = '69mb',
|
| 12 |
tmpDir = node.os.tmpdir(),
|
| 13 |
{ env } = node.process,
|
| 14 |
+
PORT = env.SERVER_PORT || env.PORT || 7860,
|
| 15 |
+
timeZone = env.TZ || 'Asia/Jakarta'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
const execPromise = node.util.promisify(
|
| 18 |
node.child_process.exec
|
| 19 |
+
),
|
| 20 |
+
formatBytes = (val) => prettyBytes(val, { unitSeparator: ' ' }),
|
| 21 |
+
isNumber = (val) => typeof val === 'number',
|
| 22 |
+
logger = (...args) => console.log(
|
| 23 |
+
new Date().toLocaleTimeString('fr', { timeZone }),
|
| 24 |
+
...args
|
| 25 |
+
),
|
| 26 |
+
transformObj = (obj, cb) => JSON.parse(
|
| 27 |
+
JSON.stringify(
|
| 28 |
+
obj,
|
| 29 |
+
(key, val) => cb(key, val) ? formatBytes(val) : val
|
| 30 |
+
).replace(/_(\w)/g, (_, g) => g.toUpperCase())
|
| 31 |
+
),
|
| 32 |
+
getServerStats = () => {
|
| 33 |
+
const proc = node.process
|
| 34 |
+
const stats = {}
|
| 35 |
+
stats.uptime = prettyMs(proc.uptime() * 1e3)
|
| 36 |
+
stats.osUptime = prettyMs(node.os.uptime() * 1e3)
|
| 37 |
+
const report = proc.report?.getReport?.()
|
| 38 |
+
if (report) Object.assign(stats, {
|
| 39 |
+
getReport: report.header,
|
| 40 |
+
javascriptHeap: transformObj(
|
| 41 |
+
report.javascriptHeap,
|
| 42 |
+
(k, v) => !/(ContextCount|Garbage)/i.test(k) && isNumber(v)
|
| 43 |
+
),
|
| 44 |
+
resourceUsage: transformObj(
|
| 45 |
+
report.resourceUsage,
|
| 46 |
+
(k, v) => !/(Percent|IO|reads|write)/i.test(k) && isNumber(v)
|
| 47 |
+
),
|
| 48 |
+
uvthreadResourceUsage: report.uvthreadResourceUsage
|
| 49 |
+
})
|
| 50 |
+
|
| 51 |
+
const memUsage = proc.memoryUsage()
|
| 52 |
+
stats.memoryUsage = {}
|
| 53 |
+
for (let [k, v] of Object.entries(memUsage))
|
| 54 |
+
stats.memoryUsage[k] = formatBytes(v)
|
| 55 |
+
|
| 56 |
+
return stats
|
| 57 |
+
}
|
| 58 |
|
| 59 |
express()
|
| 60 |
.enable('trust proxy')
|
| 61 |
+
.set('etag', false)
|
| 62 |
.set('json spaces', 2)
|
| 63 |
.use(express.json({ limit: limitSize }))
|
| 64 |
.use(express.urlencoded({ extended: true, limit: limitSize }))
|
|
|
|
| 68 |
express.static(tmpDir),
|
| 69 |
serveIndex(tmpDir, { hidden: true, icons: true })
|
| 70 |
)
|
| 71 |
+
.all('/', (_, res) => {
|
| 72 |
+
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
|
| 73 |
+
res.set('Pragma', 'no-cache')
|
| 74 |
+
res.set('Expires', '0')
|
| 75 |
+
res.json(getServerStats())
|
| 76 |
+
})
|
| 77 |
.post('/eval', async (req, res) => {
|
| 78 |
let code = req.body.code, o
|
| 79 |
code = `(async function MengEval() { ${code} })()`
|
|
|
|
| 81 |
catch (e) { o = e }
|
| 82 |
finally { res.send(node.util.format(o)) }
|
| 83 |
})
|
| 84 |
+
.listen(PORT, () => logger('Running at port', PORT))
|