breakpoint-transfer-plugin 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,257 @@
1
+ # Breakpoint Transfer Plugin
2
+
3
+ [![npm version](https://img.shields.io/npm/v/breakpoint-transfer-plugin.svg)](https://www.npmjs.com/package/breakpoint-transfer-plugin)
4
+ [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC)
5
+ [![GitHub stars](https://img.shields.io/github/stars/lvdaxianer/breakpoint-transfer-plugin)](https://github.com/lvdaxianer/breakpoint-transfer-plugin)
6
+
7
+ ---
8
+
9
+ **GitHub**: https://github.com/lvdaxianer/breakpoint-transfer-plugin
10
+
11
+ ## Introduction
12
+
13
+ `breakpoint-transfer-plugin` is a JavaScript/TypeScript SDK plugin for implementing file breakpoint continuation. It is primarily designed for large file upload scenarios, supporting breakpoint continuation, quick upload, concurrent control, progress tracking, and more.
14
+
15
+ ### Project Overview
16
+
17
+ This project consists of **frontend** and **backend** parts:
18
+
19
+ - **Frontend**: This documentation describes `breakpoint-transfer-plugin`, handling file chunking, progress tracking, upload control, and other frontend logic
20
+ - **Backend**: Handles file receiving, chunk merging, persistent storage, and other server-side logic
21
+
22
+ **Backend Documentation**:
23
+ - International: https://github.com/lvdaxianer/spring-boot-launcher/tree/main/breakpoint-transfer-launcher
24
+ - China: https://gitee.com/breakpoint-transfer-launcher/spring-boot-launcher/tree/main/breakpoint-transfer-launcher
25
+
26
+ ## Key Features
27
+
28
+ - **Breakpoint Continuation**: Supports resuming uploads from the last break point after network interruption
29
+ - **Quick Upload (Instant Transmission)**: Quickly checks if a file has been uploaded via file hash to avoid duplicate uploads
30
+ - **Concurrent Control**: Supports configuring the number of simultaneous file uploads
31
+ - **Progress Tracking**: Real-time upload progress display with multiple status indicators
32
+ - **Multi-language Support**: Supports Chinese, English, and Japanese
33
+ - **Persistent Storage**: Uses IndexedDB to persist upload progress, supporting recovery after page refresh
34
+ - **Web Worker**: Supports using Web Workers to calculate file hash without blocking the main thread
35
+
36
+ ## Highlights
37
+
38
+ ### Architecture
39
+
40
+ - **Event-Driven Design**: Uses publish-subscribe pattern for loose coupling between modules
41
+ - **TypeScript-First**: Complete TypeScript type definitions for better developer experience
42
+ - **Modular Architecture**: Clear separation of concerns with dedicated modules for storage, events, tools, and state management
43
+
44
+ ### Core Capabilities
45
+
46
+ - **Smart Chunking**: Adaptive chunk size based on network speed detection
47
+ - **Auto-Retry**: Configurable retry mechanism with exponential backoff support
48
+ - **State Management**: Comprehensive state tracking for each upload operation
49
+ - **Pause/Resume/Cancel**: Full control over upload lifecycle
50
+
51
+ ### Performance Optimizations
52
+
53
+ - **Web Worker Offloading**: File hash calculation runs in background thread
54
+ - **IndexedDB Caching**: Persistent storage with optimized key-value indexing
55
+ - **Concurrent Limiter**: PLimit implementation controls parallel uploads
56
+ - **Network Speed Detection**: Dynamic chunk sizing based on real network conditions
57
+
58
+ ### Developer Experience
59
+
60
+ - **Promise-Based API**: Modern async/await compatible interface
61
+ - **Callback Support**: Optional callbacks for progress tracking
62
+ - **I18n Ready**: Built-in multi-language support (ZH, EN, JA_JP)
63
+ - **Zero-Dependency Core**: Minimal core dependencies for lightweight integration
64
+
65
+ ## Feature Support Matrix
66
+
67
+ | Feature | HTTP | HTTPS | localhost |
68
+ |---------|:----:|:-----:|:---------:|
69
+ | Large File Chunk Upload | ✅ | ✅ | ✅ |
70
+ | Quick Upload (Instant) | ❌ | ✅ | ✅ |
71
+ | Breakpoint Continuation | ❌ | ✅ | ✅ |
72
+ | Concurrent Control (Leaky Bucket) | ✅ | ✅ | ✅ |
73
+ | Pause | ✅ | ✅ | ✅ |
74
+ | Same File Mutex Upload | ✅ | ✅ | ✅ |
75
+ | Delete | ✅ | ✅ | ✅ |
76
+ | Web Worker Hash Algorithm | ❌ | ✅ | ✅ |
77
+ | Failure Retry | ✅ | ✅ | ✅ |
78
+ | Hash Algorithm Queue | ✅ | ✅ | ✅ |
79
+ | Persistence (IndexedDB) | ✅ | ✅ | ✅ |
80
+ | Refresh Retention | ✅ | ✅ | ✅ |
81
+ | Error Feedback | ✅ | ✅ | ✅ |
82
+ | I18n (ZH/EN/JA) | ✅ | ✅ | ✅ |
83
+ | Network Speed Adaptation | ✅ | ✅ | ✅ |
84
+
85
+ **Notes**:
86
+ - ❌ Not supported: Quick upload, breakpoint continuation, and Web Worker require Secure Context (HTTPS or localhost)
87
+ - ✅ Supported: All other features work in all environments
88
+
89
+ ## Installation
90
+
91
+ ```bash
92
+ npm install breakpoint-transfer-plugin
93
+ # or
94
+ pnpm add breakpoint-transfer-plugin
95
+ ```
96
+
97
+ ## Quick Start
98
+
99
+ ```typescript
100
+ import { uploadHandler } from "breakpoint-transfer-plugin";
101
+
102
+ // 1. Configure upload parameters
103
+ uploadHandler.config({
104
+ // Request interface configuration (required)
105
+ req: {
106
+ // Chunk upload
107
+ sectionUploadReq: async (calculationHashCode, chunkFileName, formData) => {
108
+ const res = await fetch(
109
+ `/breakpoint/transfer/upload/section/${calculationHashCode}/${chunkFileName}`,
110
+ {
111
+ method: "POST",
112
+ body: formData,
113
+ },
114
+ );
115
+ return res.json();
116
+ },
117
+ // Merge chunks
118
+ mergeUploadReq: async (calculationHashCode, fileName) => {
119
+ const res = await fetch(
120
+ `/breakpoint/transfer/upload/merge/${calculationHashCode}/${fileName}`,
121
+ { method: "GET" },
122
+ );
123
+ return res.json();
124
+ },
125
+ // Check if file exists (instant upload verification)
126
+ verifyFileExistReq: async (calculationHashName) => {
127
+ const res = await fetch(
128
+ `/breakpoint/transfer/upload/verify/${calculationHashName}`,
129
+ { method: "GET" },
130
+ );
131
+ return res.json();
132
+ },
133
+ // List uploaded chunks
134
+ listFilesReq: async (calculationHashCode) => {
135
+ const res = await fetch(
136
+ `/breakpoint/transfer/upload/list/${calculationHashCode}`,
137
+ { method: "GET" },
138
+ );
139
+ return res.json();
140
+ },
141
+ },
142
+ // Maximum retry attempts (default: 3)
143
+ maxRetryTimes: 3,
144
+ // Concurrent limit (default: 2)
145
+ concurrentLimit: 2,
146
+ // Base network speed Bytes/s (default: 1024)
147
+ baseNetworkSpeed: 1024,
148
+ // Enable persistence (default: false)
149
+ persist: true,
150
+ // Language setting (default: ZH)
151
+ language: "zh",
152
+ });
153
+
154
+ // 2. Start uploading file
155
+ const fileInput = document.getElementById("fileInput") as HTMLInputElement;
156
+ fileInput.addEventListener("change", async (e) => {
157
+ const file = e.target.files?.[0];
158
+ if (!file) return;
159
+
160
+ try {
161
+ const [baseDir, fileName] = await uploadHandler(file);
162
+ console.log("Upload successful:", baseDir, fileName);
163
+ } catch (error) {
164
+ console.error("Upload failed:", error);
165
+ }
166
+ });
167
+ ```
168
+
169
+ ## API Reference
170
+
171
+ ### Upload Configuration `uploadHandler.config(config)`
172
+
173
+ | Parameter | Type | Required | Default | Description |
174
+ |-----------|------|----------|---------|-------------|
175
+ | req | Object | Yes | - | Request interface configuration |
176
+ | req.verifyFileExistReq | Function | Yes | - | Instant upload verification API |
177
+ | req.sectionUploadReq | Function | Yes | - | Chunk upload API |
178
+ | req.mergeUploadReq | Function | Yes | - | Chunk merge API |
179
+ | req.listFilesReq | Function | Yes | - | List uploaded chunks API |
180
+ | maxRetryTimes | Number | No | 3 | Maximum retry attempts |
181
+ | concurrentLimit | Number | No | 2 | Concurrent upload limit |
182
+ | baseNetworkSpeed | Number | No | 1024 | Base network speed Bytes/s |
183
+ | persist | Boolean | No | false | Enable persistence |
184
+ | language | String | No | "zh" | Language setting |
185
+
186
+ ### Upload Status `UploadProgressState`
187
+
188
+ | Status | Description |
189
+ |--------|-------------|
190
+ | Prepare | In preparation |
191
+ | HashCalculationWaiting | Calculating hash |
192
+ | Waiting | Waiting in queue |
193
+ | Uploading | Uploading |
194
+ | Merge | Merging files |
195
+ | Done | Upload complete |
196
+ | QuickUpload | Instant upload successful |
197
+ | BreakPointUpload | Breakpoint continuation |
198
+ | Pause | Paused |
199
+ | PauseRetry | Pause retry |
200
+ | Retry | Retrying |
201
+ | RetryFailed | Retry failed |
202
+ | Canceled | Canceled |
203
+ | RequestError | Request error |
204
+ | NetworkDisconnected | Network disconnected |
205
+ | RefreshRetry | Refresh retry |
206
+
207
+ ### Language Setting `uploadHandler.lng(language)`
208
+
209
+ Supported languages:
210
+ - `zh` - Chinese
211
+ - `en` - English
212
+ - `ja_JP` - Japanese
213
+
214
+ ## Project Structure
215
+
216
+ ```
217
+ src/
218
+ ├── core/
219
+ │ ├── constant.ts # Constant definitions
220
+ │ ├── types/ # Type definitions
221
+ │ │ └── index.ts
222
+ │ ├── tools.ts # Utility functions
223
+ │ ├── subscriber.ts # Event subscription
224
+ │ ├── store.ts # Storage management
225
+ │ ├── variable.ts # Global variables
226
+ │ ├── Logger.ts # Logging utility
227
+ │ ├── PLimit.ts # Concurrency control
228
+ │ ├── language.ts # Multi-language
229
+ │ └── index.ts # Core exports
230
+ ├── index.ts # Main entry
231
+ └──
232
+ ```
233
+
234
+ ## Dependencies
235
+
236
+ - `i18next` - Multi-language support
237
+ - `jsmethod-extra` - JavaScript utility library
238
+ - `localforage` - IndexedDB wrapper
239
+
240
+ ## Build
241
+
242
+ ```bash
243
+ # Install dependencies
244
+ pnpm install
245
+
246
+ # Build
247
+ pnpm build
248
+
249
+ # Lint
250
+ pnpm lint
251
+ ```
252
+
253
+ ---
254
+
255
+ ## License
256
+
257
+ ISC
@@ -0,0 +1,257 @@
1
+ # Breakpoint Transfer Plugin
2
+
3
+ [![npm version](https://img.shields.io/npm/v/breakpoint-transfer-plugin.svg)](https://www.npmjs.com/package/breakpoint-transfer-plugin)
4
+ [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC)
5
+ [![GitHub stars](https://img.shields.io/github/stars/lvdaxianer/breakpoint-transfer-plugin)](https://github.com/lvdaxianer/breakpoint-transfer-plugin)
6
+
7
+ ---
8
+
9
+ **GitHub**: https://github.com/lvdaxianer/breakpoint-transfer-plugin
10
+
11
+ ## 简介
12
+
13
+ `breakpoint-transfer-plugin` 是一个用于实现文件断点续传功能的 JavaScript/TypeScript SDK 插件。主要用于大文件上传场景,支持断点续传、秒传、并发控制、进度追踪等功能。
14
+
15
+ ### 项目说明
16
+
17
+ 本项目分为 **前端** 和 **后端** 两部分:
18
+
19
+ - **前端**:即本文档描述的 `breakpoint-transfer-plugin`,负责文件分片、进度追踪、上传控制等前端逻辑
20
+ - **后端**:负责文件接收、分片合并、持久化存储等服务端逻辑
21
+
22
+ **后端文档参照**:
23
+ - 国外:https://github.com/lvdaxianer/spring-boot-launcher/tree/main/breakpoint-transfer-launcher
24
+ - 国内:https://gitee.com/breakpoint-transfer-launcher/spring-boot-launcher/tree/main/breakpoint-transfer-launcher
25
+
26
+ ## 核心特性
27
+
28
+ - **断点续传**: 支持网络中断后从上次断开的位置继续上传
29
+ - **秒传功能**: 通过文件 hash 快速判断是否已上传,避免重复上传
30
+ - **并发控制**: 支持配置同时上传的文件数量限制
31
+ - **进度追踪**: 实时显示上传进度,支持多种状态展示
32
+ - **多语言支持**: 支持中文、英文、日文三种语言
33
+ - **持久化存储**: 使用 IndexedDB 持久化上传进度,支持页面刷新后恢复
34
+ - **Web Worker**: 支持使用 Web Worker 计算文件 hash,不阻塞主线程
35
+
36
+ ## 项目亮点
37
+
38
+ ### 架构设计
39
+
40
+ - **事件驱动架构**: 采用发布订阅模式,实现模块间松耦合
41
+ - **TypeScript 优先**: 完整的类型定义,提供良好的开发体验
42
+ - **模块化设计**: 清晰的功能划分,存储、事件、工具、状态管理各司其职
43
+
44
+ ### 核心能力
45
+
46
+ - **智能分片**: 根据网络速度动态调整分片大小
47
+ - **自动重试**: 可配置的重试机制,支持指数退避
48
+ - **状态管理**: 全面的上传状态跟踪
49
+ - **暂停/恢复/取消**: 完全控制上传生命周期
50
+
51
+ ### 性能优化
52
+
53
+ - **Web Worker 卸载**: 文件 hash 计算在后台线程执行
54
+ - **IndexedDB 缓存**: 优化的键值索引持久化存储
55
+ - **并发限制器**: PLimit 实现控制并行上传数量
56
+ - **网速检测**: 基于实时网络条件动态调整分片大小
57
+
58
+ ### 开发体验
59
+
60
+ - **Promise API**: 现代异步/await 兼容接口
61
+ - **回调支持**: 可选的进度回调函数
62
+ - **国际化支持**: 内置多语言 (中文、英文、日文)
63
+ - **零依赖核心**: 轻量级集成,最小化核心依赖
64
+
65
+ ## 功能支持矩阵
66
+
67
+ | 功能 | HTTP | HTTPS | localhost |
68
+ |------|:----:|:-----:|:---------:|
69
+ | 大文件切割上传 | ✅ | ✅ | ✅ |
70
+ | 秒传 | ❌ | ✅ | ✅ |
71
+ | 断点续传 | ❌ | ✅ | ✅ |
72
+ | 基于漏斗算法的并发控制 | ✅ | ✅ | ✅ |
73
+ | 暂停 | ✅ | ✅ | ✅ |
74
+ | 相同文件互斥上传 | ✅ | ✅ | ✅ |
75
+ | 删除 | ✅ | ✅ | ✅ |
76
+ | 基于 Web Worker 的 Hash 摘要算法 | ❌ | ✅ | ✅ |
77
+ | 失败重试 | ✅ | ✅ | ✅ |
78
+ | Hash 摘要算法队列 | ✅ | ✅ | ✅ |
79
+ | 持久化方案 (IndexedDB) | ✅ | ✅ | ✅ |
80
+ | 刷新留存 | ✅ | ✅ | ✅ |
81
+ | 异常反馈提示 | ✅ | ✅ | ✅ |
82
+ | 国际化 (汉/英/日) | ✅ | ✅ | ✅ |
83
+ | 监听网络,动态调整上传速度 | ✅ | ✅ | ✅ |
84
+
85
+ **说明**:
86
+ - ❌ 表示不支持:秒传、断点续传、Web Worker 需要 Secure Context (HTTPS 或 localhost)
87
+ - ✅ 表示支持:其他功能在所有环境下均可正常使用
88
+
89
+ ## 安装
90
+
91
+ ```bash
92
+ npm install breakpoint-transfer-plugin
93
+ # 或
94
+ pnpm add breakpoint-transfer-plugin
95
+ ```
96
+
97
+ ## 快速开始
98
+
99
+ ```typescript
100
+ import { uploadHandler } from "breakpoint-transfer-plugin";
101
+
102
+ // 1. 配置上传参数
103
+ uploadHandler.config({
104
+ // 请求接口配置(必填)
105
+ req: {
106
+ // 分片上传
107
+ sectionUploadReq: async (calculationHashCode, chunkFileName, formData) => {
108
+ const res = await fetch(
109
+ `/breakpoint/transfer/upload/section/${calculationHashCode}/${chunkFileName}`,
110
+ {
111
+ method: "POST",
112
+ body: formData,
113
+ },
114
+ );
115
+ return res.json();
116
+ },
117
+ // 合并分片
118
+ mergeUploadReq: async (calculationHashCode, fileName) => {
119
+ const res = await fetch(
120
+ `/breakpoint/transfer/upload/merge/${calculationHashCode}/${fileName}`,
121
+ { method: "GET" },
122
+ );
123
+ return res.json();
124
+ },
125
+ // 检查文件是否已存在(秒传验证)
126
+ verifyFileExistReq: async (calculationHashName) => {
127
+ const res = await fetch(
128
+ `/breakpoint/transfer/upload/verify/${calculationHashName}`,
129
+ { method: "GET" },
130
+ );
131
+ return res.json();
132
+ },
133
+ // 查询已上传的分片
134
+ listFilesReq: async (calculationHashCode) => {
135
+ const res = await fetch(
136
+ `/breakpoint/transfer/upload/list/${calculationHashCode}`,
137
+ { method: "GET" },
138
+ );
139
+ return res.json();
140
+ },
141
+ },
142
+ // 最大重试次数(默认: 3)
143
+ maxRetryTimes: 3,
144
+ // 并发限制(默认: 2)
145
+ concurrentLimit: 2,
146
+ // 基础网速 Bytes/s(默认: 1024)
147
+ baseNetworkSpeed: 1024,
148
+ // 是否持久化(默认: false)
149
+ persist: true,
150
+ // 语言设置(默认: ZH)
151
+ language: "zh",
152
+ });
153
+
154
+ // 2. 开始上传文件
155
+ const fileInput = document.getElementById("fileInput") as HTMLInputElement;
156
+ fileInput.addEventListener("change", async (e) => {
157
+ const file = e.target.files?.[0];
158
+ if (!file) return;
159
+
160
+ try {
161
+ const [baseDir, fileName] = await uploadHandler(file);
162
+ console.log("上传成功:", baseDir, fileName);
163
+ } catch (error) {
164
+ console.error("上传失败:", error);
165
+ }
166
+ });
167
+ ```
168
+
169
+ ## API 参考
170
+
171
+ ### 上传配置 `uploadHandler.config(config)`
172
+
173
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
174
+ |------|------|------|--------|------|
175
+ | req | Object | 是 | - | 请求接口配置 |
176
+ | req.verifyFileExistReq | Function | 是 | - | 秒传验证接口 |
177
+ | req.sectionUploadReq | Function | 是 | - | 分片上传接口 |
178
+ | req.mergeUploadReq | Function | 是 | - | 合并分片接口 |
179
+ | req.listFilesReq | Function | 是 | - | 查询已上传分片接口 |
180
+ | maxRetryTimes | Number | 否 | 3 | 最大重试次数 |
181
+ | concurrentLimit | Number | 否 | 2 | 并发上传限制 |
182
+ | baseNetworkSpeed | Number | 否 | 1024 | 基础网速 Bytes/s |
183
+ | persist | Boolean | 否 | false | 是否启用持久化 |
184
+ | language | String | 否 | "zh" | 语言设置 |
185
+
186
+ ### 上传状态 `UploadProgressState`
187
+
188
+ | 状态 | 说明 |
189
+ |------|------|
190
+ | Prepare | 准备中 |
191
+ | HashCalculationWaiting | 计算 hash 中 |
192
+ | Waiting | 排队等待中 |
193
+ | Uploading | 上传中 |
194
+ | Merge | 合并文件中 |
195
+ | Done | 上传完成 |
196
+ | QuickUpload | 秒传成功 |
197
+ | BreakPointUpload | 断点续传中 |
198
+ | Pause | 已暂停 |
199
+ | PauseRetry | 暂停重试 |
200
+ | Retry | 重试中 |
201
+ | RetryFailed | 重试失败 |
202
+ | Canceled | 已取消 |
203
+ | RequestError | 请求错误 |
204
+ | NetworkDisconnected | 网络断开 |
205
+ | RefreshRetry | 刷新重试 |
206
+
207
+ ### 语言设置 `uploadHandler.lng(language)`
208
+
209
+ 支持的语言:
210
+ - `zh` - 中文
211
+ - `en` - 英文
212
+ - `ja_JP` - 日文
213
+
214
+ ## 项目结构
215
+
216
+ ```
217
+ src/
218
+ ├── core/
219
+ │ ├── constant.ts # 常量定义
220
+ │ ├── types/ # 类型定义
221
+ │ │ └── index.ts
222
+ │ ├── tools.ts # 工具函数
223
+ │ ├── subscriber.ts # 事件订阅
224
+ │ ├── store.ts # 存储管理
225
+ │ ├── variable.ts # 全局变量
226
+ │ ├── Logger.ts # 日志工具
227
+ │ ├── PLimit.ts # 并发控制
228
+ │ ├── language.ts # 多语言
229
+ │ └── index.ts # 核心导出
230
+ ├── index.ts # 主入口
231
+ └──
232
+ ```
233
+
234
+ ## 依赖
235
+
236
+ - `i18next` - 多语言支持
237
+ - `jsmethod-extra` - JavaScript 工具库
238
+ - `localforage` - IndexedDB 封装
239
+
240
+ ## 构建
241
+
242
+ ```bash
243
+ # 安装依赖
244
+ pnpm install
245
+
246
+ # 构建
247
+ pnpm build
248
+
249
+ # 代码检查
250
+ pnpm lint
251
+ ```
252
+
253
+ ---
254
+
255
+ ## License
256
+
257
+ ISC