Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

2.2 KB
Raw Blame
import type { SharedFile } from './models'

export const FILE_CHUNK_SIZE = 384 * 1024
export const FILE_LIMIT = 100 * 1024 * 1024

export function validateSharedFile(file: SharedFile) {
  if (!Number.isSafeInteger(file.size) || file.size < 0 || file.size > FILE_LIMIT || !/^[a-f0-9]{64}$/.test(file.sha256)) throw new Error('Invalid file metadata')
  if (!file.name || file.name.length > 200 || /[/\\\x00-\x1f\x7f]/.test(file.name) || file.name === '.' || file.name === '..') throw new Error('Invalid filename')
}

/** A display filename is a suggestion to the native dialog, never a storage path. */
export function saveName(name: string) {
  const portable = name.replace(/[<>:"|?*]/g, '_').replace(/[. ]+$/, '')
  return portable && !/^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\.|$)/i.test(portable) ? portable : `file-${portable || 'download'}`
}

/** A shared file's whole bytes, read chunk by chunk with `read(offset)` and checked against its SHA-256. */
export async function readSharedFile(file: SharedFile, read: (offset: number) => Promise<{ data: string; size: number }>): Promise<Uint8Array> {
  validateSharedFile(file)
  const bytes = new Uint8Array(file.size)
  let offset = 0
  do {
    const chunk = decodeFileChunk(await read(offset), file.size, offset)
    bytes.set(chunk, offset)
    offset += chunk.length
  } while (offset < file.size)
  if ((await sha256Hex(bytes)) !== file.sha256) throw new Error('The file failed its integrity check')
  return bytes
}

export async function sha256Hex(bytes: Uint8Array<ArrayBuffer>): Promise<string> {
  const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes))
  return Array.from(digest, (b) => b.toString(16).padStart(2, '0')).join('')
}

export function decodeFileChunk(chunk: { data: string; size: number }, size: number, offset: number): Uint8Array {
  if (chunk.size !== size || typeof chunk.data !== 'string' || chunk.data.length > Math.ceil(FILE_CHUNK_SIZE / 3) * 4) throw new Error('Invalid download response')
  const bytes = Uint8Array.from(atob(chunk.data), (v) => v.charCodeAt(0))
  if (bytes.length !== Math.min(FILE_CHUNK_SIZE, size - offset)) throw new Error('Download was truncated or changed')
  return bytes
}