file-chunk-worker
v1.1.6
Published
A lightweight file chunk processor with MD5 calculation using Web Workers
Maintainers
Readme
file-chunk-worker
🚀 高性能文件分片处理器,基于 Web Workers 实现多线程并发处理,支持进度反馈,适用于大文件上传、MD5 计算、分片校验等场景。
利用多线程并发处理文件块,大幅提升处理速度,告别主线程阻塞!
✨ 特性
- ✅ 多线程并发处理:使用 Web Workers,避免主线程阻塞
- ✅ 自动分片:按指定大小切分文件
- ✅ 进度反馈:实时返回处理进度(0 ~ 1)
- ✅ 高性能:结合
spark-md5快速计算哈希 - ✅ 轻量无依赖(除 spark-md5):仅 5KB 左右(gzip)
- ✅ 支持 ESM / UMD:现代与传统项目皆可用
📦 安装
npm install file-chunk-worker
JavaScript 使用
import FileProcessor from 'file-chunk-worker';
// 假设你有一个 File 对象(如 input[type=file] 选择的文件)
const file = document.querySelector('input[type=file]').files[0];
const processor = new FileProcessor(file, {
chunkSize: 1024 * 1024, // 每片 1MB(可选,默认 1MB)
threadCount: 4 // 使用 4 个线程(可选,默认 4)
});
// 开始处理
const chunks = await processor.calculateMd5((progress) => {
console.log(`处理进度: ${(progress * 100).toFixed(2)}%`);
});
console.log('所有分片:', chunks);
// 输出示例:
// [
// { chunkIndex: 0, hash: 'a1b2c3d4...', size: 1048576, file: Blob },
// { chunkIndex: 1, hash: 'e5f6g7h8...', size: 800000, file: Blob }
// ]
📚 API 参考
# new FaceGuard(options)
| 参数名 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| file | File | null | 要处理的文件对象 |
| options.chunkSize | number | 1M | 每个分片的大小(字节)|
| options.threadCount | number | 4 |使用的线程数 |返回值
Promise<Chunk[]>:分片数组,每个分片包含:
| 属性名 | 类型 | 描述 |
| --- | --- | --- |
| chunkIndex | number | 分片索引 |
| start | number | 起始字节 |
| end | number | 结束字节 |
| hash | string | 分片的 MD5 哈希值 |
| size | number | 分片大小(字节) |
| file | Blob | 分片文件对象 |
