Update app.mjs
Browse files
app.mjs
CHANGED
|
@@ -14,113 +14,127 @@ import pluginStealth from 'puppeteer-extra-plugin-stealth'
|
|
| 14 |
const TIMEOUT_MS = 6e4
|
| 15 |
const MAX_CODE_LENGTH = 6e4
|
| 16 |
|
| 17 |
-
!isMainThread
|
| 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 |
-
app.
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
const TIMEOUT_MS = 6e4
|
| 15 |
const MAX_CODE_LENGTH = 6e4
|
| 16 |
|
| 17 |
+
if (!isMainThread) {
|
| 18 |
+
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
| 19 |
+
parentPort.on('message', async (code) => {
|
| 20 |
+
try {
|
| 21 |
+
const fn = new AsyncFunction(
|
| 22 |
+
'playwright',
|
| 23 |
+
'pluginStealth',
|
| 24 |
+
'console',
|
| 25 |
+
code
|
| 26 |
+
)
|
| 27 |
+
parentPort.postMessage({
|
| 28 |
+
result: await fn(playwright, pluginStealth, console)
|
| 29 |
+
})
|
| 30 |
+
} catch (e) {
|
| 31 |
+
parentPort.postMessage({ error: format(e) })
|
| 32 |
+
}
|
| 33 |
+
})
|
| 34 |
+
} else {
|
| 35 |
+
const isNumber = (v) => typeof v === 'number' && !isNaN(v)
|
| 36 |
+
|
| 37 |
+
const transformObj = (obj, cb) =>
|
| 38 |
+
JSON.parse(
|
| 39 |
+
JSON.stringify(obj, (key, val) =>
|
| 40 |
+
cb(key, val) ? prettyBytes(val) : val
|
| 41 |
+
).replace(/_(\w)/g, (_, g) => g.toUpperCase())
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
const getServerStats = () => {
|
| 45 |
+
const stats = {}
|
| 46 |
+
stats.uptime = prettyMs(process.uptime() * 1e3)
|
| 47 |
+
stats.osUptime = prettyMs(os.uptime() * 1e3)
|
| 48 |
+
|
| 49 |
+
const report = process.report?.getReport?.()
|
| 50 |
+
if (report)
|
| 51 |
+
Object.assign(stats, {
|
| 52 |
+
header: report.header,
|
| 53 |
+
javascriptHeap: transformObj(
|
| 54 |
+
report.javascriptHeap,
|
| 55 |
+
(k, v) => !/(ContextCount|Garbage)/i.test(k) && isNumber(v)
|
| 56 |
+
),
|
| 57 |
+
resourceUsage: transformObj(
|
| 58 |
+
report.resourceUsage,
|
| 59 |
+
(k, v) =>
|
| 60 |
+
!/(Percent|IO|reads|write)/i.test(k) && isNumber(v)
|
| 61 |
+
),
|
| 62 |
+
uvthreadResourceUsage: transformObj(
|
| 63 |
+
report.uvthreadResourceUsage,
|
| 64 |
+
(k, v) => isNumber(v)
|
| 65 |
+
)
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
stats.memoryUsage = transformObj(process.memoryUsage(), (k, v) =>
|
| 69 |
+
isNumber(v)
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return stats
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const runBrowserScript = (code) =>
|
| 76 |
+
new Promise((resolve, reject) => {
|
| 77 |
+
const worker = new Worker(new URL(import.meta.url), {
|
| 78 |
+
resourceLimits: { maxOldGenerationSizeMb: 512 }
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
const timer = setTimeout(() => {
|
| 82 |
+
worker.terminate()
|
| 83 |
+
reject(new Error('Execution timeout'))
|
| 84 |
+
}, TIMEOUT_MS)
|
| 85 |
+
|
| 86 |
+
const cleanup = () => {
|
| 87 |
+
clearTimeout(timer)
|
| 88 |
+
worker.terminate()
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
worker.postMessage(code)
|
| 92 |
+
worker.on('message', ({ result, error }) => {
|
| 93 |
+
cleanup()
|
| 94 |
+
error ? reject(new Error(error)) : resolve(format(result))
|
| 95 |
+
})
|
| 96 |
+
worker.on('error', (e) => {
|
| 97 |
+
cleanup()
|
| 98 |
+
reject(e)
|
| 99 |
+
})
|
| 100 |
+
worker.on('exit', (exitCode) => {
|
| 101 |
+
clearTimeout(timer)
|
| 102 |
+
exitCode !== 0 &&
|
| 103 |
+
reject(new Error(`Worker exited with code ${exitCode}`))
|
| 104 |
+
})
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
+
const app = new Hono()
|
| 108 |
+
|
| 109 |
+
app.use(logger())
|
| 110 |
+
app.use(prettyJSON({ force: true }))
|
| 111 |
+
app.use(
|
| 112 |
+
'/file/*',
|
| 113 |
+
serveStatic({
|
| 114 |
+
root: os.tmpdir(),
|
| 115 |
+
rewriteRequestPath: (path) => path.replace(/^\/file/, '')
|
| 116 |
+
})
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
app.get('/', (c) => c.json(getServerStats()))
|
| 120 |
+
|
| 121 |
+
app.post('/run', async (c) => {
|
| 122 |
+
const body = await c.req.json().catch(() => ({}))
|
| 123 |
+
const { code } = body
|
| 124 |
+
|
| 125 |
+
if (!code) return c.json({ error: 'Code is required' }, 400)
|
| 126 |
+
if (typeof code !== 'string')
|
| 127 |
+
return c.json({ error: 'Code must be a string' }, 400)
|
| 128 |
+
if (code.length > MAX_CODE_LENGTH)
|
| 129 |
+
return c.json({ error: 'Code too long' }, 400)
|
| 130 |
+
|
| 131 |
+
try {
|
| 132 |
+
return c.json({ result: await runBrowserScript(code) })
|
| 133 |
+
} catch (e) {
|
| 134 |
+
return c.json({ error: format(e) }, 500)
|
| 135 |
+
}
|
| 136 |
+
})
|
| 137 |
+
|
| 138 |
+
const port = process.env.SPACE_ID ? 7860 : +(process.env.PORT || 3000)
|
| 139 |
+
serve({ fetch: app.fetch, port }, console.log)
|
| 140 |
+
}
|