Download src/security/host-shell-parser.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/security/host-shell-parser.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/security/host-shell-parser.js
-
curl -L -o host-shell-parser.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/security/host-shell-parser.js
1.67 kB
| // ์ค๋ช ๋ฌธ์: docs/specs/host-command-guard.md (v1.0.0) | |
| // ์ ๋ขฐํ์ง ์๋ ์ ๋ฌธ๋ฒ ๋ถ์์ ๋ณ๋ Worker์ ํ์ ํ๋ค. resolver/exec callback์ ์ ๊ณตํ์ง ์๋๋ค. | |
| const { Worker, isMainThread, parentPort, workerData } = require('node:worker_threads'); | |
| function failure(code = 'HOST_COMMAND_UNSUPPORTED') { | |
| return Object.assign(new Error('์ด ์คํ ๊ตฌ๋ฌธ์ ํธ์คํธ ๋ณด์ ์ ์ฑ ์ผ๋ก ๊ฒ์ฆํ ์ ์์ต๋๋ค.'), { code }); | |
| } | |
| async function parseShell(source) { | |
| if (typeof source !== 'string' || !source.trim() || source.length > 8192 || source.includes('\0')) throw failure(); | |
| let worker, timer; | |
| try { | |
| return await new Promise((resolve, reject) => { | |
| worker = new Worker(require('../utils/external-runtime-path').externalRuntimePath(__filename), { workerData: { kind: 'bada-shell-ast', source }, | |
| resourceLimits: { maxOldGenerationSizeMb: 64, stackSizeMb: 2 } }); | |
| timer = setTimeout(() => reject(failure()), 2000); | |
| worker.once('message', value => value?.ok === true ? resolve(value.ast) : reject(failure())); | |
| worker.once('error', () => reject(failure())); | |
| worker.once('exit', code => { if (code !== 0) reject(failure()); }); | |
| }); | |
| } catch { throw failure(); } | |
| finally { clearTimeout(timer); if (worker) await worker.terminate(); } | |
| } | |
| if (!isMainThread && workerData?.kind === 'bada-shell-ast') { | |
| (async () => { | |
| try { | |
| const { parse } = await import('@isdk/bash-parser'); | |
| const ast = await parse(workerData.source); | |
| parentPort.postMessage({ ok: true, ast }); | |
| } catch { parentPort.postMessage({ ok: false }); } | |
| })(); | |
| } | |
| module.exports = { parseShell, failure }; | |