@ysgroup/core 0.1.24 → 0.1.25
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/package.json +1 -1
- package/src/fileTransfer.ts +226 -100
- package/src/index.ts +7 -0
package/package.json
CHANGED
package/src/fileTransfer.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
/* @ysgroup/core —— 业务 WS
|
|
1
|
+
/* @ysgroup/core —— 业务 WS 分块文件传输(先算整份 sha256,再秒传 / 续传 / 分块上传;读取逐块拼成 Blob)。 */
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
4
|
+
|
|
5
|
+
/** 每块原始字节数:1 MiB,与 ys_base.files 的 CHUNK_SIZE 一致。服务端 begin 返回 chunk_size 时以服务端为准。 */
|
|
4
6
|
export const FILE_CHUNK_SIZE = 1024 * 1024;
|
|
5
7
|
|
|
6
8
|
/** 单块上传、读取、finish 的默认超时:一块 1 MiB 的 base64,慢网下也够。 */
|
|
7
9
|
export const FILE_CHUNK_TIMEOUT_MS = 60_000;
|
|
8
10
|
|
|
11
|
+
/** 计算 sha256 时每次从文件读出的字节数(不把整个文件读进内存)。 */
|
|
12
|
+
export const FILE_HASH_SLICE_SIZE = 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* uploadChunked 合并进度里「计算校验」所占的比例:算 sha256 时 ratio 从 0 走到 0.1,上传从 0.1 走到 1。
|
|
16
|
+
* 计算是本地的,一般远快于上传;需要分开显示时用 onProgress 的 phase 和第三个参数(本阶段 0~1)。
|
|
17
|
+
*/
|
|
18
|
+
export const FILE_HASH_PROGRESS_WEIGHT = 0.1;
|
|
19
|
+
|
|
9
20
|
/**
|
|
10
21
|
* 业务调用函数:`store.call` 或业务仓自己的包装(如 `callWs`)都能直接传。
|
|
11
22
|
* 第三个参数只用到 `{ timeoutMs }`。
|
|
@@ -16,8 +27,18 @@ export type YsFileCall = (
|
|
|
16
27
|
options?: { timeoutMs?: number },
|
|
17
28
|
) => Promise<unknown>;
|
|
18
29
|
|
|
19
|
-
/**
|
|
20
|
-
export type
|
|
30
|
+
/** 上传的阶段:`hash` 计算整份 sha256,`upload` 发块(秒传时没有 upload 阶段)。 */
|
|
31
|
+
export type YsFilePhase = "hash" | "upload";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 进度回调。ratio 为总进度 0~1,只增不减。uploadChunked 另传 phase 与本阶段进度 phaseRatio(0~1);
|
|
35
|
+
* readChunked 只传 ratio。只写 `(ratio) => …` 的旧回调不受影响。
|
|
36
|
+
*/
|
|
37
|
+
export type YsFileProgress = (
|
|
38
|
+
ratio: number,
|
|
39
|
+
phase?: YsFilePhase,
|
|
40
|
+
phaseRatio?: number,
|
|
41
|
+
) => void;
|
|
21
42
|
|
|
22
43
|
/** 浏览器本地读文件出的错(不是服务端错误),调用方可单独提示「请重新选择文件」。 */
|
|
23
44
|
export class YsLocalFileError extends Error {
|
|
@@ -51,19 +72,70 @@ export function base64ToBytes(value: string): Uint8Array<ArrayBuffer> {
|
|
|
51
72
|
return bytes;
|
|
52
73
|
}
|
|
53
74
|
|
|
75
|
+
function toHex(bytes: Uint8Array): string {
|
|
76
|
+
let hex = "";
|
|
77
|
+
for (const byte of bytes) hex += byte.toString(16).padStart(2, "0");
|
|
78
|
+
return hex;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function readBytes(blob: Blob): Promise<Uint8Array> {
|
|
82
|
+
try {
|
|
83
|
+
return new Uint8Array(await blob.arrayBuffer());
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new YsLocalFileError(undefined, { cause: error });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface YsSha256Options {
|
|
90
|
+
/** 本次计算的进度 0~1。 */
|
|
91
|
+
onProgress?: (ratio: number) => void;
|
|
92
|
+
signal?: AbortSignal;
|
|
93
|
+
/** 每次读出的字节数,默认 FILE_HASH_SLICE_SIZE(1 MiB)。 */
|
|
94
|
+
sliceSize?: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
54
97
|
/**
|
|
55
|
-
* 整个 Blob 的 sha256
|
|
56
|
-
*
|
|
98
|
+
* 整个 Blob 的 sha256(小写十六进制)。按 1 MiB 切片流式计算,不把整个文件读进内存;
|
|
99
|
+
* 用 `@noble/hashes`(纯 JS),不依赖 HTTPS 下才有的 Web Crypto,HTTP 访问也能算。读失败抛 YsLocalFileError。
|
|
57
100
|
*/
|
|
58
|
-
export async function sha256Hex(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
101
|
+
export async function sha256Hex(
|
|
102
|
+
blob: Blob,
|
|
103
|
+
options: YsSha256Options = {},
|
|
104
|
+
): Promise<string> {
|
|
105
|
+
const { signal, onProgress } = options;
|
|
106
|
+
const sliceSize = Math.max(1, options.sliceSize ?? FILE_HASH_SLICE_SIZE);
|
|
107
|
+
signal?.throwIfAborted();
|
|
108
|
+
const hasher = sha256.create();
|
|
109
|
+
onProgress?.(0);
|
|
110
|
+
for (let start = 0; start < blob.size; start += sliceSize) {
|
|
111
|
+
const bytes = await abortable(
|
|
112
|
+
readBytes(blob.slice(start, start + sliceSize)),
|
|
113
|
+
signal,
|
|
114
|
+
);
|
|
115
|
+
hasher.update(bytes);
|
|
116
|
+
onProgress?.(Math.min(start + sliceSize, blob.size) / blob.size);
|
|
117
|
+
}
|
|
118
|
+
if (!blob.size) onProgress?.(1);
|
|
119
|
+
return toHex(hasher.digest());
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 持有验证:每段 sha256(nonce + 文件里这一段原文)。 */
|
|
123
|
+
async function proveRanges(
|
|
124
|
+
file: Blob,
|
|
125
|
+
nonce: string,
|
|
126
|
+
ranges: { start: number; length: number }[],
|
|
127
|
+
signal?: AbortSignal,
|
|
128
|
+
): Promise<string[]> {
|
|
129
|
+
const prefix = new TextEncoder().encode(nonce);
|
|
130
|
+
const proofs: string[] = [];
|
|
131
|
+
for (const { start, length } of ranges) {
|
|
132
|
+
const segment = await abortable(
|
|
133
|
+
readBytes(file.slice(start, start + length)),
|
|
134
|
+
signal,
|
|
135
|
+
);
|
|
136
|
+
proofs.push(toHex(sha256.create().update(prefix).update(segment).digest()));
|
|
137
|
+
}
|
|
138
|
+
return proofs;
|
|
67
139
|
}
|
|
68
140
|
|
|
69
141
|
/** 续传记录在 localStorage 里的键前缀。 */
|
|
@@ -75,17 +147,28 @@ export const FILE_UPLOAD_RESUME_TTL_MS = 24 * 3600 * 1000;
|
|
|
75
147
|
/** 开启续传、重试时每块最多重试的次数(不含第一次)。 */
|
|
76
148
|
export const FILE_CHUNK_RETRIES = 3;
|
|
77
149
|
|
|
150
|
+
/** 秒传的持有验证事件默认名。 */
|
|
151
|
+
export const FILE_PROVE_EVENT = "prove_file_upload";
|
|
152
|
+
|
|
78
153
|
export interface YsUploadResumeOptions {
|
|
79
|
-
/**
|
|
80
|
-
|
|
81
|
-
|
|
154
|
+
/**
|
|
155
|
+
* 0.1.24 的按编号续传事件(如 `resume_file_upload`)。0.1.25 起续传由 begin 按内容决定,不再调用,保留只为兼容旧写法。
|
|
156
|
+
*/
|
|
157
|
+
event?: string;
|
|
158
|
+
/** 区分「同一文件传给不同业务对象」的附加键,如业务对象 id;同时作为 begin 的 `resume_key` 发给服务端。 */
|
|
82
159
|
key?: string;
|
|
83
160
|
/** 本地续传记录保留时长,默认 FILE_UPLOAD_RESUME_TTL_MS。 */
|
|
84
161
|
ttlMs?: number;
|
|
85
162
|
}
|
|
86
163
|
|
|
164
|
+
/** begin 返回的同内容文件(字段由服务端 visible_duplicates 决定,常见有 `_id`、`name`、上传时间)。 */
|
|
165
|
+
export type YsFileDuplicate = Record<string, unknown>;
|
|
166
|
+
|
|
87
167
|
export interface YsUploadChunkedOptions {
|
|
88
|
-
/**
|
|
168
|
+
/**
|
|
169
|
+
* 开始上传的事件与附加数据。实际发送 `{ name, size, checksum, resume_key?, resume_id?, ...data }`,
|
|
170
|
+
* data 里同名字段优先(如 content_type 自己传)。checksum 为整份 sha256。
|
|
171
|
+
*/
|
|
89
172
|
begin: { event: string; data?: Record<string, unknown> };
|
|
90
173
|
/** 发块事件,如 `upload_file_chunk`。数据 `{ [idKey]: id, index, size, data }`,size 为这一块原始字节数。 */
|
|
91
174
|
chunkEvent: string;
|
|
@@ -93,22 +176,32 @@ export interface YsUploadChunkedOptions {
|
|
|
93
176
|
finishEvent: string;
|
|
94
177
|
/** 取消事件,如 `cancel_file_upload`。出错或中止时发 `{ [idKey]: id }`,不等结果、不抛错。 */
|
|
95
178
|
cancelEvent: string;
|
|
179
|
+
/** 秒传的持有验证事件,默认 FILE_PROVE_EVENT(`prove_file_upload`)。数据 `{ challenge_id, proofs }`。 */
|
|
180
|
+
proveEvent?: string;
|
|
96
181
|
/**
|
|
97
182
|
* 上传编号字段名:既用来从 begin 结果里读编号,也用作后续请求里的字段名。
|
|
98
183
|
* 不传时先看 `upload_id`,没有再看 `_id`,找到哪个就用哪个。
|
|
99
184
|
*/
|
|
100
185
|
idKey?: string;
|
|
186
|
+
/** 进度:`(ratio, phase, phaseRatio)`,见 YsFileProgress 与 FILE_HASH_PROGRESS_WEIGHT。 */
|
|
101
187
|
onProgress?: YsFileProgress;
|
|
188
|
+
/**
|
|
189
|
+
* begin 返回同内容的已有文件时调用(列表由服务端按权限过滤)。返回 false 或抛错则中止上传
|
|
190
|
+
* (已建的上传会话发 cancel;返回 false 时抛 name 为 AbortError 的错误),可返回 Promise(如等用户确认)。
|
|
191
|
+
*/
|
|
192
|
+
onDuplicates?: (
|
|
193
|
+
list: YsFileDuplicate[],
|
|
194
|
+
) => boolean | void | Promise<boolean | void>;
|
|
102
195
|
/** 中止(用户主动取消):立即发 cancel 并抛出 signal.reason(默认 AbortError),不等正在进行的请求;清掉续传记录。 */
|
|
103
196
|
signal?: AbortSignal;
|
|
104
|
-
/** 每块、finish 的单次超时,默认 FILE_CHUNK_TIMEOUT_MS。begin、cancel
|
|
197
|
+
/** 每块、finish、prove 的单次超时,默认 FILE_CHUNK_TIMEOUT_MS。begin、cancel 用调用方默认超时。 */
|
|
105
198
|
timeoutMs?: number;
|
|
106
199
|
/**
|
|
107
|
-
* 断点续传(服务端需 ys_base 0.1.
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* 连接断开、超时、页面关闭导致的失败不发 cancel
|
|
111
|
-
*
|
|
200
|
+
* 断点续传(服务端需 ys_base 0.1.100 起的 FileStore)。服务端按「上传人 + 内容 sha256 + resume_key」找到未完成的
|
|
201
|
+
* 上传就从已收到的块接着发,改了文件名也能续。开启时另按「begin 事件 + sha256 + 大小 + key」把上传编号记在
|
|
202
|
+
* localStorage(`ys:upload:` 前缀),下次 begin 带 `resume_id` 精确指定会话。成功或取消后清掉记录。
|
|
203
|
+
* 连接断开、超时、页面关闭导致的失败不发 cancel、保留记录;服务端明确拒绝(带 code 的业务错误)与用户中止照旧
|
|
204
|
+
* 发 cancel 并清记录。不传或 false 时:失败一律发 cancel、不重试。
|
|
112
205
|
*/
|
|
113
206
|
resume?: YsUploadResumeOptions | false;
|
|
114
207
|
/** 每块遇到网络中断、超时(以及服务端 408/429/502/503/504)时的重试次数。开启 resume 时默认 FILE_CHUNK_RETRIES,否则 0。 */
|
|
@@ -123,17 +216,22 @@ interface ResumeRecord {
|
|
|
123
216
|
expiresAt: number;
|
|
124
217
|
}
|
|
125
218
|
|
|
219
|
+
/** uploadChunked 的结果:服务端 finish / prove 的返回值;是普通对象时一定带 `instant`(true 为秒传)。 */
|
|
220
|
+
export type YsUploadResult<T> = T extends object ? T & { instant: boolean } : T;
|
|
221
|
+
|
|
126
222
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
223
|
+
* 上传一个文件:先流式算整份 sha256(phase=hash),begin 带上 checksum,按服务端答复走三条路之一:
|
|
224
|
+
* - `challenge`(内容已存在):读出指定的几段算 sha256(nonce + 片段),调 proveEvent 证明持有,通过即秒传;
|
|
225
|
+
* - `resume`(同一上传人有该内容未完成的上传):从服务端已收到的块接着发;
|
|
226
|
+
* - `upload`(或旧服务端没有 status):从第 0 块按序发。块大小、块数以服务端返回为准。
|
|
227
|
+
* 发完调 finish(带 sha256)。返回 finish / prove 的结果,普通对象结果补上 `instant`。
|
|
228
|
+
* 出错:拿到上传编号后不等结果地发 cancel,再把原错误抛出;开启 resume 时网络类失败例外(保留可续传)。
|
|
131
229
|
*/
|
|
132
230
|
export async function uploadChunked<T = unknown>(
|
|
133
231
|
call: YsFileCall,
|
|
134
232
|
file: Blob & { name?: string; lastModified?: number },
|
|
135
233
|
options: YsUploadChunkedOptions,
|
|
136
|
-
): Promise<T
|
|
234
|
+
): Promise<YsUploadResult<T>> {
|
|
137
235
|
const { signal } = options;
|
|
138
236
|
const timeoutMs = options.timeoutMs ?? FILE_CHUNK_TIMEOUT_MS;
|
|
139
237
|
const resume = options.resume || undefined;
|
|
@@ -144,24 +242,15 @@ export async function uploadChunked<T = unknown>(
|
|
|
144
242
|
const retryDelayMs = options.retryDelayMs ?? 500;
|
|
145
243
|
signal?.throwIfAborted();
|
|
146
244
|
const name = file.name ?? "";
|
|
147
|
-
const resumeKey = resume
|
|
148
|
-
? FILE_UPLOAD_RESUME_PREFIX +
|
|
149
|
-
[
|
|
150
|
-
options.begin.event,
|
|
151
|
-
name,
|
|
152
|
-
file.size,
|
|
153
|
-
file.lastModified ?? "",
|
|
154
|
-
resume.key ?? "",
|
|
155
|
-
].join("|")
|
|
156
|
-
: "";
|
|
157
245
|
let idKey = options.idKey ?? "";
|
|
158
246
|
let id = "";
|
|
247
|
+
let resumeKey = "";
|
|
159
248
|
let cancelled = false;
|
|
160
249
|
let reported = -1;
|
|
161
|
-
const report = (ratio: number) => {
|
|
250
|
+
const report = (ratio: number, phase: YsFilePhase, phaseRatio: number) => {
|
|
162
251
|
if (ratio <= reported) return;
|
|
163
252
|
reported = ratio;
|
|
164
|
-
options.onProgress?.(ratio);
|
|
253
|
+
options.onProgress?.(ratio, phase, phaseRatio);
|
|
165
254
|
};
|
|
166
255
|
const cancel = () => {
|
|
167
256
|
if (resumeKey) removeRecord(resumeKey);
|
|
@@ -173,52 +262,30 @@ export async function uploadChunked<T = unknown>(
|
|
|
173
262
|
.catch(() => undefined);
|
|
174
263
|
};
|
|
175
264
|
|
|
176
|
-
let session: Record<string, unknown> | undefined;
|
|
177
|
-
let start = 0;
|
|
178
265
|
try {
|
|
266
|
+
const checksum = await sha256Hex(file, {
|
|
267
|
+
signal,
|
|
268
|
+
onProgress: (ratio) =>
|
|
269
|
+
report(ratio * FILE_HASH_PROGRESS_WEIGHT, "hash", ratio),
|
|
270
|
+
});
|
|
271
|
+
let record: ResumeRecord | undefined;
|
|
179
272
|
if (resume) {
|
|
180
273
|
sweepRecords();
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
call(resume.event, {
|
|
186
|
-
[record.idKey]: record.id,
|
|
187
|
-
name,
|
|
188
|
-
size: file.size,
|
|
189
|
-
}),
|
|
190
|
-
signal,
|
|
191
|
-
)) as Record<string, unknown> | undefined;
|
|
192
|
-
const received = nonNegative(result?.received);
|
|
193
|
-
const count = nonNegative(result?.chunk_count);
|
|
194
|
-
if (
|
|
195
|
-
received === undefined ||
|
|
196
|
-
(count !== undefined && received > count)
|
|
197
|
-
)
|
|
198
|
-
throw new Error("续传结果无效");
|
|
199
|
-
session = result;
|
|
200
|
-
start = received;
|
|
201
|
-
id = record.id;
|
|
202
|
-
idKey = record.idKey;
|
|
203
|
-
} catch (error) {
|
|
204
|
-
if (signal?.aborted) {
|
|
205
|
-
// 用户在续传询问时就取消了:把记录里的上传一并取消。
|
|
206
|
-
id = record.id;
|
|
207
|
-
idKey = record.idKey;
|
|
208
|
-
throw error;
|
|
209
|
-
}
|
|
210
|
-
// 过期、不存在、不一致或网络问题:丢掉记录,改走全新上传。
|
|
211
|
-
removeRecord(resumeKey);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
274
|
+
resumeKey =
|
|
275
|
+
FILE_UPLOAD_RESUME_PREFIX +
|
|
276
|
+
[options.begin.event, checksum, file.size, resume.key ?? ""].join("|");
|
|
277
|
+
record = readRecord(resumeKey);
|
|
214
278
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
279
|
+
const beginPromise = call(options.begin.event, {
|
|
280
|
+
name,
|
|
281
|
+
size: file.size,
|
|
282
|
+
checksum,
|
|
283
|
+
...(resume ? { resume_key: resume.key ?? "" } : {}),
|
|
284
|
+
...(record ? { resume_id: record.id } : {}),
|
|
285
|
+
...options.begin.data,
|
|
286
|
+
}).then((result) => {
|
|
287
|
+
const begin = (result ?? {}) as Record<string, unknown>;
|
|
288
|
+
if (begin.status !== "challenge") {
|
|
222
289
|
if (!idKey) idKey = "upload_id" in begin ? "upload_id" : "_id";
|
|
223
290
|
id = String(begin[idKey] ?? "");
|
|
224
291
|
if (id && resumeKey)
|
|
@@ -228,18 +295,67 @@ export async function uploadChunked<T = unknown>(
|
|
|
228
295
|
expiresAt:
|
|
229
296
|
Date.now() + (resume?.ttlMs ?? FILE_UPLOAD_RESUME_TTL_MS),
|
|
230
297
|
});
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
298
|
+
}
|
|
299
|
+
// begin 回来前已经中止:补发 cancel,丢掉服务端刚建的上传。
|
|
300
|
+
if (signal?.aborted) cancel();
|
|
301
|
+
return begin;
|
|
302
|
+
});
|
|
303
|
+
const session = await abortable(beginPromise, signal);
|
|
304
|
+
|
|
305
|
+
const duplicates = Array.isArray(session.duplicates)
|
|
306
|
+
? (session.duplicates as YsFileDuplicate[])
|
|
307
|
+
: [];
|
|
308
|
+
if (duplicates.length && options.onDuplicates) {
|
|
309
|
+
const decision = await abortable(
|
|
310
|
+
Promise.resolve(options.onDuplicates(duplicates)),
|
|
311
|
+
signal,
|
|
312
|
+
);
|
|
313
|
+
if (decision === false)
|
|
314
|
+
throw new DOMException(
|
|
315
|
+
"已存在相同内容的文件,已取消上传",
|
|
316
|
+
"AbortError",
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (session.status === "challenge") {
|
|
321
|
+
const ranges = Array.isArray(session.ranges)
|
|
322
|
+
? (session.ranges as { start: number; length: number }[])
|
|
323
|
+
: [];
|
|
324
|
+
const proofs = await proveRanges(
|
|
325
|
+
file,
|
|
326
|
+
String(session.nonce ?? ""),
|
|
327
|
+
ranges,
|
|
328
|
+
signal,
|
|
329
|
+
);
|
|
330
|
+
const result = await abortable(
|
|
331
|
+
call(
|
|
332
|
+
options.proveEvent ?? FILE_PROVE_EVENT,
|
|
333
|
+
{ challenge_id: session.challenge_id, proofs },
|
|
334
|
+
{ timeoutMs },
|
|
335
|
+
),
|
|
336
|
+
signal,
|
|
337
|
+
);
|
|
338
|
+
if (resumeKey) removeRecord(resumeKey);
|
|
339
|
+
report(1, "upload", 1);
|
|
340
|
+
return withInstant(result, true) as YsUploadResult<T>;
|
|
238
341
|
}
|
|
342
|
+
|
|
343
|
+
if (!id)
|
|
344
|
+
throw new Error(`${options.begin.event} 没有返回上传编号 ${idKey}`);
|
|
239
345
|
const chunkSize = positive(session.chunk_size) ?? FILE_CHUNK_SIZE;
|
|
240
346
|
const count =
|
|
241
347
|
nonNegative(session.chunk_count) ?? Math.ceil(file.size / chunkSize);
|
|
242
|
-
|
|
348
|
+
const start = Math.min(nonNegative(session.received) ?? 0, count);
|
|
349
|
+
const uploadReport = (done: number) => {
|
|
350
|
+
const phaseRatio = count ? done / count : 0;
|
|
351
|
+
report(
|
|
352
|
+
FILE_HASH_PROGRESS_WEIGHT +
|
|
353
|
+
(1 - FILE_HASH_PROGRESS_WEIGHT) * phaseRatio,
|
|
354
|
+
"upload",
|
|
355
|
+
phaseRatio,
|
|
356
|
+
);
|
|
357
|
+
};
|
|
358
|
+
uploadReport(start);
|
|
243
359
|
for (let index = start; index < count; index += 1) {
|
|
244
360
|
const piece = file.slice(index * chunkSize, (index + 1) * chunkSize);
|
|
245
361
|
const data = await abortable(blobToBase64(piece), signal);
|
|
@@ -257,24 +373,34 @@ export async function uploadChunked<T = unknown>(
|
|
|
257
373
|
retryDelayMs,
|
|
258
374
|
signal,
|
|
259
375
|
);
|
|
260
|
-
|
|
376
|
+
uploadReport(index + 1);
|
|
261
377
|
}
|
|
262
|
-
const
|
|
263
|
-
const result = (await abortable(
|
|
378
|
+
const result = await abortable(
|
|
264
379
|
call(options.finishEvent, { [idKey]: id, checksum }, { timeoutMs }),
|
|
265
380
|
signal,
|
|
266
|
-
)
|
|
381
|
+
);
|
|
267
382
|
if (resumeKey) removeRecord(resumeKey);
|
|
268
|
-
report(1);
|
|
269
|
-
return result
|
|
383
|
+
report(1, "upload", 1);
|
|
384
|
+
return withInstant(result, false) as YsUploadResult<T>;
|
|
270
385
|
} catch (error) {
|
|
271
|
-
// 开启续传时,连接断开、超时这类失败保留记录、不发 cancel
|
|
386
|
+
// 开启续传时,连接断开、超时这类失败保留记录、不发 cancel,下次同一内容从断点接着传。
|
|
272
387
|
if (resumeKey && !signal?.aborted && isTransient(error)) throw error;
|
|
273
388
|
cancel();
|
|
274
389
|
throw error;
|
|
275
390
|
}
|
|
276
391
|
}
|
|
277
392
|
|
|
393
|
+
function withInstant(result: unknown, instant: boolean): unknown {
|
|
394
|
+
if (
|
|
395
|
+
result &&
|
|
396
|
+
typeof result === "object" &&
|
|
397
|
+
!Array.isArray(result) &&
|
|
398
|
+
!("instant" in result)
|
|
399
|
+
)
|
|
400
|
+
return { ...(result as Record<string, unknown>), instant };
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
|
|
278
404
|
/** 网络中断、超时、服务暂不可用:可以重试或续传。带数字 code 的错误视为服务端已明确答复。 */
|
|
279
405
|
function isTransient(error: unknown): boolean {
|
|
280
406
|
if (error instanceof YsLocalFileError) return false;
|
|
@@ -339,7 +465,7 @@ function writeRecord(key: string, record: ResumeRecord): void {
|
|
|
339
465
|
try {
|
|
340
466
|
storage()?.setItem(key, JSON.stringify(record));
|
|
341
467
|
} catch {
|
|
342
|
-
//
|
|
468
|
+
// 存不下(隐私模式、配额满)就不记,服务端照样按内容续传。
|
|
343
469
|
}
|
|
344
470
|
}
|
|
345
471
|
|
package/src/index.ts
CHANGED
|
@@ -44,6 +44,9 @@ export {
|
|
|
44
44
|
FILE_CHUNK_RETRIES,
|
|
45
45
|
FILE_CHUNK_SIZE,
|
|
46
46
|
FILE_CHUNK_TIMEOUT_MS,
|
|
47
|
+
FILE_HASH_PROGRESS_WEIGHT,
|
|
48
|
+
FILE_HASH_SLICE_SIZE,
|
|
49
|
+
FILE_PROVE_EVENT,
|
|
47
50
|
FILE_UPLOAD_RESUME_PREFIX,
|
|
48
51
|
FILE_UPLOAD_RESUME_TTL_MS,
|
|
49
52
|
YsLocalFileError,
|
|
@@ -53,9 +56,13 @@ export {
|
|
|
53
56
|
sha256Hex,
|
|
54
57
|
uploadChunked,
|
|
55
58
|
type YsFileCall,
|
|
59
|
+
type YsFileDuplicate,
|
|
60
|
+
type YsFilePhase,
|
|
56
61
|
type YsFileProgress,
|
|
57
62
|
type YsReadChunkedOptions,
|
|
63
|
+
type YsSha256Options,
|
|
58
64
|
type YsUploadChunkedOptions,
|
|
65
|
+
type YsUploadResult,
|
|
59
66
|
type YsUploadResumeOptions,
|
|
60
67
|
} from "./fileTransfer";
|
|
61
68
|
export {
|