WIP v1.3.7: 기록 묶음(경기 · 경기 전후 대화) 관리 · zip 내보내기/받아오기

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-16 05:21:32 +09:00
parent 98b8342678
commit c397bacf8c
23 changed files with 1376 additions and 17 deletions

View File

@@ -243,11 +243,16 @@ export async function buildServer(
const origin = String(req.headers.origin || '');
if (origin && origin !== cfg.publicUrl) return reply.code(403).send({ error: 'bad_origin' });
}
// 기록 묶음 지우기 · 받아오기는 관리자만 (보기 · zip 내려받기는 서버 멤버 누구나)
const p = req.url.split('?')[0];
const bundles = p.startsWith('/api/bundles');
if (bundles && req.method !== 'GET' && !canViewLogs(s.user.id)) return reply.code(403).send({ error: 'forbidden', message: '기록 지우기 · 받아오기는 관리자만 할 수 있습니다' });
const headers: Record<string, string> = {};
for (const k of ['range', 'content-type', 'accept']) if (req.headers[k]) headers[k] = String(req.headers[k]);
const body = req.body === undefined || req.body === null ? null : typeof req.body === 'string' ? req.body : JSON.stringify(req.body);
const body = req.body === undefined || req.body === null ? null : Buffer.isBuffer(req.body) || typeof req.body === 'string' ? req.body : JSON.stringify(req.body);
try {
const r = await hub.request(req.method, req.url, headers, s.user, body);
// zip 만들기 · 받아오기는 오래 걸릴 수 있다
const r = await hub.request(req.method, req.url, headers, s.user, body, bundles ? 180000 : 20000);
for (const [k, v] of Object.entries(r.headers)) if (k !== 'content-length') reply.header(k, v);
return reply.code(r.status).send(r.body);
} catch (e: any) {
@@ -255,6 +260,8 @@ export async function buildServer(
return reply.code(504).send({ error: 'timeout' });
}
};
// zip 받아오기 본문 (300MB 까지)
app.addContentTypeParser('application/zip', { parseAs: 'buffer', bodyLimit: 300 * 1024 * 1024 }, (_req, body, done) => done(null, body));
app.route({ method: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'], url: '/api/*', handler: relay });
// ------------------------------------------------------------------ 게임 이미지 캐시 (보고서 7-7)

View File

@@ -188,7 +188,7 @@ export class RelayHub {
}
/** 브라우저 요청 하나를 게임 PC 로 넘긴다 */
request(method: string, path: string, headers: Record<string, string>, user: { id: string; name: string; avatar: string | null }, body: string | null, timeoutMs = 20000): Promise<RelayResponse> {
request(method: string, path: string, headers: Record<string, string>, user: { id: string; name: string; avatar: string | null }, body: string | Buffer | null, timeoutMs = 20000): Promise<RelayResponse> {
const dev = this.device;
if (!dev || !this.status().online) return Promise.reject(new Error('device_offline'));
const id = crypto.randomBytes(8).toString('hex');
@@ -200,6 +200,17 @@ export class RelayHub {
}
}, timeoutMs);
this.pending.set(id, { resolve, reject, timer, chunks: [] });
if (Buffer.isBuffer(body)) {
// 바이너리 본문(zip 받아오기): 1MB 조각으로 먼저 보내고 요청을 보낸다 (앱은 한 메시지 8MB 까지)
const idBuf = Buffer.from(id, 'utf8');
const head = Buffer.alloc(4);
head.writeUInt32BE(idBuf.length, 0);
const CH = 1024 * 1024;
for (let i = 0; i < body.length; i += CH) dev.ws.send(Buffer.concat([head, idBuf, body.subarray(i, i + CH)]), { binary: true });
const msg: RelayToDevice = { t: 'req', id, method, path, headers, user, body: null, bin: true };
dev.ws.send(JSON.stringify(msg));
return;
}
const msg: RelayToDevice = { t: 'req', id, method, path, headers, user, body };
dev.ws.send(JSON.stringify(msg));
});