Skip to content

Instantly share code, notes, and snippets.

@hirbod
Created May 24, 2025 18:41
Show Gist options
  • Save hirbod/65803303891ddd55dd8273000363e628 to your computer and use it in GitHub Desktop.
Save hirbod/65803303891ddd55dd8273000363e628 to your computer and use it in GitHub Desktop.
TUS FileUpload Class using next generation expo-file-system/next for fast chunked uploads with contant RAM by fair CPU usage
import { File, type FileHandle } from 'expo-file-system/next'
interface FileInput {
uri: string
}
export default class TusFileReader {
async openFile({ uri }: FileInput) {
const handle = new File(uri)
if (!handle.exists || !handle.size) throw new Error(`File ${uri} not found`)
return new FileSource(handle.open(), handle.size)
}
}
class FileSource {
constructor(
private h: FileHandle,
private _size: number
) {}
get size() {
return this._size
}
async slice(start: number, end: number) {
if (start >= this._size) return { value: new Uint8Array(0), done: true }
this.h.offset = start
const chunk = this.h.readBytes(Math.min(end, this._size) - start)
return { value: chunk, done: false }
}
close() {
this.h.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment