import * as fsp from 'node:fs/promises' export async function fileExists(path: string): Promise { try { const stat = await fsp.stat(path) return stat.isFile() } catch { return false } } export async function directoryExists(path: string): Promise { try { const stat = await fsp.stat(path) return stat.isDirectory() } catch { return false } } export function sanitizeFilename(filename: string) { return filename.replace(/[/\\?%*:|"<>]/g, '_') } export async function writeWebStreamToFile(stream: ReadableStream, path: string) { const fd = await fsp.open(path, 'w+') const writer = fd.createWriteStream() for await (const chunk of stream as any) { writer.write(chunk) } writer.end() await new Promise((resolve, reject) => { writer.on('error', reject) writer.on('finish', resolve) }) }