30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
// 不可信字节的解析入口(docs/adr/ADR-005-sandbox.md):缺省在进程内解析;服务端启动时用 setTorrentParser 换成沙箱子进程实现。
|
|
// adapters 不依赖服务端,测试与单独使用时照常进程内解析。
|
|
import { BencodeError, parseTorrent, type TorrentMeta } from "./qbittorrent/torrent.ts";
|
|
|
|
export type TorrentParser = (bytes: Uint8Array) => Promise<TorrentMeta>;
|
|
|
|
let parser: TorrentParser = async (bytes) => parseTorrent(bytes);
|
|
|
|
export function setTorrentParser(p: TorrentParser): void {
|
|
parser = p;
|
|
}
|
|
|
|
/** 解析 .torrent;不是有效种子抛 BencodeError */
|
|
export function parseTorrentIsolated(bytes: Uint8Array): Promise<TorrentMeta> {
|
|
return parser(bytes);
|
|
}
|
|
|
|
/** 校验是 .torrent 并取 v1 infohash 与名称;不是种子返回 null(取代进程内的 inspectTorrent) */
|
|
export async function inspectTorrentIsolated(
|
|
bytes: Uint8Array,
|
|
): Promise<{ infoHash: string; name: string | null } | null> {
|
|
try {
|
|
const m = await parser(bytes);
|
|
return { infoHash: m.infohashV1 ?? m.hash, name: m.name || null };
|
|
} catch (e) {
|
|
if (e instanceof BencodeError) return null;
|
|
throw e;
|
|
}
|
|
}
|