leejunyoung5819mc-paper-plugin-test-mcp-26.3

공개

마인크래프트 26.3 Paper 서버의 플러그인 테스트를 하려는 AI 에이전트를 위한 MCP 도구

64줄2.1 KB
1// Small download helper: hash-verified, cached, with bounded concurrency.
2import fs from 'node:fs';
3import path from 'node:path';
4import crypto from 'node:crypto';
5
6export async function fetchJson<T = any>(url: string): Promise<T> {
7 const res = await fetch(url, { headers: { 'User-Agent': 'mc-tester/0.1' } });
8 if (!res.ok) throw new Error(`GET ${url} -> ${res.status}`);
9 return (await res.json()) as T;
10}
11
12function hashFile(file: string, algo: 'sha1' | 'sha256'): string {
13 return crypto.createHash(algo).update(fs.readFileSync(file)).digest('hex');
14}
15
16export interface DownloadSpec {
17 url: string;
18 dest: string;
19 sha1?: string;
20 sha256?: string;
21}
22
23export async function download(spec: DownloadSpec): Promise<string> {
24 const { url, dest } = spec;
25 const algo = spec.sha256 ? 'sha256' : spec.sha1 ? 'sha1' : null;
26 const want = spec.sha256 ?? spec.sha1;
27 if (fs.existsSync(dest)) {
28 if (!algo || hashFile(dest, algo) === want) return dest;
29 }
30 fs.mkdirSync(path.dirname(dest), { recursive: true });
31 let lastErr: unknown;
32 for (let attempt = 1; attempt <= 3; attempt++) {
33 try {
34 const res = await fetch(url, { headers: { 'User-Agent': 'mc-tester/0.1' } });
35 if (!res.ok) throw new Error(`GET ${url} -> ${res.status}`);
36 const buf = Buffer.from(await res.arrayBuffer());
37 if (algo) {
38 const got = crypto.createHash(algo).update(buf).digest('hex');
39 if (got !== want) throw new Error(`hash mismatch for ${url}: ${got} != ${want}`);
40 }
41 const tmp = dest + '.part';
42 fs.writeFileSync(tmp, buf);
43 fs.renameSync(tmp, dest);
44 return dest;
45 } catch (e) {
46 lastErr = e;
47 }
48 }
49 throw lastErr;
50}
51
52export async function downloadAll(specs: DownloadSpec[], concurrency = 16, onProgress?: (done: number, total: number) => void): Promise<void> {
53 let i = 0;
54 let done = 0;
55 const worker = async () => {
56 while (i < specs.length) {
57 const s = specs[i++];
58 await download(s);
59 done++;
60 if (onProgress && (done % 200 === 0 || done === specs.length)) onProgress(done, specs.length);
61 }
62 };
63 await Promise.all(Array.from({ length: Math.min(concurrency, specs.length) }, worker));
64}