feat(core,onebot): debug-console stream-invoke + upload endpoints
Phase 2 of the debug-console expansion. Two backend endpoints under the existing /api/* bearer auth: - POST /api/debug/invoke-stream — drive a Stream API action (#163) and relay every frame to the browser over an SSE body. The frontend reads it with fetch()+a stream reader (header auth), so it needs no SSE query-token allowlist entry. Frames are NEVER dropped: backpressure is handled by waiting for the consumer to drain, not by discarding (a lost chunk corrupts a transfer). Non-stream actions emit a single frame. - POST /api/debug/upload — stream a browser-selected file to a temp path on THIS server (the bot runs server-side, not on the operator's machine), so the path can feed a send action's file/image/record/video param. Raw bytes in the body (no multipart buffering), ?filename= for a readable suffix, streamed to disk under os.tmpdir()/snowluma-debug- upload with a 4 GiB cap and a 24h TTL sweep. instance.invokeStream() delegates to ApiHandler.processStreamRequest. Pure logic (filename sanitisation, fenced size-capped disk sink, the stream-invoke Response builder) lives in debug-tools.ts with 12 unit tests. Adversarially reviewed: path traversal is robust (19 hostile filenames all fenced), frames provably never dropped, auth runs before any disk write. Fixed a High found in review — the write stream had no 'error' listener (an async ENOSPC/EIO during flush would crash the whole process) and a late-open race could leak a partial file; both now raced into the awaits and cleaned up after 'close'.
This commit is contained in:
4 files changed
+395
No files matched your search
@@ -22,6 +22,7 @@ import { coerceSettingsPatch } from './system-settings';
|
||||
import { buildBackup, planRestore, validateBackup } from './backup';
|
||||
import { collectActionDocs, collectCategories } from '@snowluma/onebot/action-docs';
|
||||
import { createFramePusher } from './debug-stream';
|
||||
import { buildStreamInvokeResponse, streamUploadToDisk } from './debug-tools';
|
||||
import { bindStateStream } from './state-stream';
|
||||
import type { StateBus, StateResource } from './state-bus';
|
||||
import { startConnectionDiffLoop } from './connection-diff-loop';
|
||||
@@ -929,6 +930,42 @@ export async function initWebUI(
|
||||
return c.json(result);
|
||||
});
|
||||
|
||||
// Invoke a Stream API action (or any action) and relay every frame to the
|
||||
// browser over an SSE body. The frontend reads this with fetch()+a stream
|
||||
// reader (Bearer auth in the header), so — unlike /api/debug/stream — it does
|
||||
// NOT need a query-token allowlist entry. Frames are never dropped.
|
||||
app.post('/api/debug/invoke-stream', async (c) => {
|
||||
let body: unknown;
|
||||
try { body = await c.req.json(); } catch { return c.json({ status: 'failed', message: '请求格式错误' }, 400); }
|
||||
const { uin, action, params } = (body ?? {}) as { uin?: unknown; action?: unknown; params?: unknown };
|
||||
if (typeof uin !== 'string' || !UIN_REGEX.test(uin)) return c.json({ status: 'failed', message: '无效的账号' }, 400);
|
||||
if (typeof action !== 'string' || !action) return c.json({ status: 'failed', message: 'action 必填' }, 400);
|
||||
if (params !== undefined && (typeof params !== 'object' || params === null || Array.isArray(params))) {
|
||||
return c.json({ status: 'failed', message: 'params 必须是对象' }, 400);
|
||||
}
|
||||
const inst = oneBotManager.getInstance(uin);
|
||||
if (!inst) return c.json({ status: 'failed', message: '账号不在线' }, 404);
|
||||
const rawRequest = JSON.stringify({ action, params: params ?? {} });
|
||||
return buildStreamInvokeResponse(
|
||||
(rq, emit, alive) => inst.invokeStream(rq, emit, alive),
|
||||
rawRequest,
|
||||
c.req.raw.signal,
|
||||
);
|
||||
});
|
||||
|
||||
// Stream a browser-selected file to a temp path on THIS server (the bot runs
|
||||
// here, not on the operator's machine), so the returned path can be fed to a
|
||||
// send action's file/image/record/video param. Raw bytes in the body,
|
||||
// `?filename=` for a human-readable suffix; streamed to disk with a byte cap.
|
||||
app.post('/api/debug/upload', async (c) => {
|
||||
try {
|
||||
const result = await streamUploadToDisk(c.req.raw.body as ReadableStream<Uint8Array> | null, c.req.query('filename'));
|
||||
return c.json({ status: 'ok', path: result.path, size: result.size });
|
||||
} catch (err) {
|
||||
return c.json({ status: 'failed', message: err instanceof Error ? err.message : '上传失败' }, 400);
|
||||
}
|
||||
});
|
||||
|
||||
// Live merged SSE of OneBot events + action calls across all accounts. Taps
|
||||
// are attached only while a client is connected (on-demand). A slow client is
|
||||
// dropped (not back-pressured onto the bot) with a periodic drop marker.
|
||||
|
||||
Reference in new issue
Block a user