@ysgroup/core 0.1.28 → 0.1.30

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ysgroup/core",
3
- "version": "0.1.28",
3
+ "version": "0.1.30",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -23,6 +23,6 @@
23
23
  "access": "public"
24
24
  },
25
25
  "devDependencies": {
26
- "@ysgroup/theme": "^0.3.1"
26
+ "@ysgroup/theme": "^0.3.3"
27
27
  }
28
28
  }
@@ -8,6 +8,12 @@ export const FILE_CHUNK_SIZE = 1024 * 1024;
8
8
  /** 单块上传、读取、finish 的默认超时:一块 1 MiB 的 base64,慢网下也够。 */
9
9
  export const FILE_CHUNK_TIMEOUT_MS = 60_000;
10
10
 
11
+ /**
12
+ * 同一文件同时在途的块数上限(默认 3)。实际取它和服务端 begin 回的 max_inflight 的较小值;
13
+ * 旧服务端不回 max_inflight 时按 1(严格逐块)发送。高延迟网络下比逐块等待快一到两倍。
14
+ */
15
+ export const FILE_CHUNK_INFLIGHT = 3;
16
+
11
17
  /** 计算 sha256 时每次从文件读出的字节数(不把整个文件读进内存)。 */
12
18
  export const FILE_HASH_SLICE_SIZE = 1024 * 1024;
13
19
 
@@ -30,6 +36,12 @@ export type YsFileCall = (
30
36
  /** 上传的阶段:`hash` 计算整份 sha256,`upload` 发块(秒传时没有 upload 阶段)。 */
31
37
  export type YsFilePhase = "hash" | "upload";
32
38
 
39
+ /**
40
+ * onPhase 报告的阶段:`hash` 计算校验、`upload` 发块、`finish` 块已发完,等服务端核对整份内容、按文件规范检查和转换
41
+ * (大图转 WebP 要一两秒,页面据此显示「处理中」)、`prove` 秒传的持有验证。
42
+ */
43
+ export type YsUploadStage = "hash" | "upload" | "finish" | "prove";
44
+
33
45
  /**
34
46
  * 进度回调。ratio 为总进度 0~1,只增不减。uploadChunked 另传 phase 与本阶段进度 phaseRatio(0~1);
35
47
  * readChunked 只传 ratio。只写 `(ratio) => …` 的旧回调不受影响。
@@ -185,6 +197,10 @@ export interface YsUploadChunkedOptions {
185
197
  idKey?: string;
186
198
  /** 进度:`(ratio, phase, phaseRatio)`,见 YsFileProgress 与 FILE_HASH_PROGRESS_WEIGHT。 */
187
199
  onProgress?: YsFileProgress;
200
+ /** 进入新阶段时调用一次,见 YsUploadStage(用来显示「校验中 / 上传中 / 处理中」)。 */
201
+ onPhase?: (stage: YsUploadStage) => void;
202
+ /** 同时在途的块数,默认 FILE_CHUNK_INFLIGHT;不超过服务端 begin 回的 max_inflight(没回时为 1)。 */
203
+ inflight?: number;
188
204
  /**
189
205
  * begin 返回同内容的已有文件时调用(列表由服务端按权限过滤)。返回 false 或抛错则中止上传
190
206
  * (已建的上传会话发 cancel;返回 false 时抛 name 为 AbortError 的错误),可返回 Promise(如等用户确认)。
@@ -252,6 +268,12 @@ export async function uploadChunked<T = unknown>(
252
268
  reported = ratio;
253
269
  options.onProgress?.(ratio, phase, phaseRatio);
254
270
  };
271
+ let stage: YsUploadStage | "" = "";
272
+ const enter = (next: YsUploadStage) => {
273
+ if (stage === next) return;
274
+ stage = next;
275
+ options.onPhase?.(next);
276
+ };
255
277
  const cancel = () => {
256
278
  if (resumeKey) removeRecord(resumeKey);
257
279
  if (cancelled || !id) return;
@@ -263,6 +285,7 @@ export async function uploadChunked<T = unknown>(
263
285
  };
264
286
 
265
287
  try {
288
+ enter("hash");
266
289
  const checksum = await sha256Hex(file, {
267
290
  signal,
268
291
  onProgress: (ratio) =>
@@ -318,6 +341,7 @@ export async function uploadChunked<T = unknown>(
318
341
  }
319
342
 
320
343
  if (session.status === "challenge") {
344
+ enter("prove");
321
345
  const ranges = Array.isArray(session.ranges)
322
346
  ? (session.ranges as { start: number; length: number }[])
323
347
  : [];
@@ -355,8 +379,16 @@ export async function uploadChunked<T = unknown>(
355
379
  phaseRatio,
356
380
  );
357
381
  };
382
+ enter("upload");
358
383
  uploadReport(start);
359
- for (let index = start; index < count; index += 1) {
384
+ const windowSize = Math.max(
385
+ 1,
386
+ Math.min(
387
+ options.inflight ?? FILE_CHUNK_INFLIGHT,
388
+ positive(session.max_inflight) ?? 1,
389
+ ),
390
+ );
391
+ const sendChunk = async (index: number) => {
360
392
  const piece = file.slice(index * chunkSize, (index + 1) * chunkSize);
361
393
  const data = await abortable(blobToBase64(piece), signal);
362
394
  await withRetry(
@@ -373,8 +405,27 @@ export async function uploadChunked<T = unknown>(
373
405
  retryDelayMs,
374
406
  signal,
375
407
  );
376
- uploadReport(index + 1);
408
+ };
409
+ // 滑动窗口:最多 windowSize 块同时在途,按序确认;服务端让先到的后面块等前面块写完。
410
+ const pending: Promise<void>[] = [];
411
+ let confirmed = start;
412
+ for (let index = start; index < count; index += 1) {
413
+ const sending = sendChunk(index);
414
+ // 前面的块失败时后面的结果不再等待,避免未处理的拒绝。
415
+ sending.catch(() => undefined);
416
+ pending.push(sending);
417
+ if (pending.length >= windowSize) {
418
+ await pending.shift();
419
+ confirmed += 1;
420
+ uploadReport(confirmed);
421
+ }
422
+ }
423
+ while (pending.length) {
424
+ await pending.shift();
425
+ confirmed += 1;
426
+ uploadReport(confirmed);
377
427
  }
428
+ enter("finish");
378
429
  const result = await abortable(
379
430
  call(options.finishEvent, { [idKey]: id, checksum }, { timeoutMs }),
380
431
  signal,
package/src/index.ts CHANGED
@@ -49,6 +49,7 @@ export {
49
49
  type PasswordWrap,
50
50
  } from "./password";
51
51
  export {
52
+ FILE_CHUNK_INFLIGHT,
52
53
  FILE_CHUNK_RETRIES,
53
54
  FILE_CHUNK_SIZE,
54
55
  FILE_CHUNK_TIMEOUT_MS,
@@ -72,6 +73,7 @@ export {
72
73
  type YsUploadChunkedOptions,
73
74
  type YsUploadResult,
74
75
  type YsUploadResumeOptions,
76
+ type YsUploadStage,
75
77
  } from "./fileTransfer";
76
78
  export {
77
79
  normalizeSearchText,
@@ -95,3 +97,17 @@ export {
95
97
  type YsCatalogStore,
96
98
  type YsCatalogStoreDefinition,
97
99
  } from "./catalog";
100
+ export {
101
+ YS_STATUS_LABELS,
102
+ YS_STATUS_NAMES,
103
+ YS_STATUS_OPTIONS,
104
+ ysRecordStatus,
105
+ ysStatusCounts,
106
+ ysStatusEmptyText,
107
+ ysStatusRevealHint,
108
+ ysStatusToggle,
109
+ ysStatusVisible,
110
+ type YsRecordStatus,
111
+ type YsStatusCounts,
112
+ type YsStatusOption,
113
+ } from "./status";
package/src/status.ts ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * 列表记录的四种状态和表格上方的状态筛选(规范:启用与归档、表格检索区)。
3
+ *
4
+ * - 每条记录只属于一种状态,按优先级判断:已删除 > 已归档 > 已停用 > 启用。
5
+ * 已归档的记录必然停用,但只算「已归档」;没有 enabled 字段的记录算启用。
6
+ * - 列表默认只显示启用的记录;表格上方三个勾选框「显示已停用」「显示已归档」「显示软删除」各看一类,
7
+ * 三者互斥(勾一个就取消另外两个),都不勾回到启用。所以筛选状态只需一个值:当前看哪一类。
8
+ * - 服务端分页的表把这个值原样作为查询条件(如 status: "enabled"),默认 "enabled"。
9
+ */
10
+
11
+ export type YsRecordStatus = "enabled" | "disabled" | "archived" | "deleted";
12
+ /** 勾选框对应的三类(启用是都不勾时的默认)。 */
13
+ export type YsStatusOption = Exclude<YsRecordStatus, "enabled">;
14
+ export type YsStatusCounts = Record<YsRecordStatus, number>;
15
+
16
+ /** 勾选框固定顺序。 */
17
+ export const YS_STATUS_OPTIONS: readonly YsStatusOption[] = [
18
+ "disabled",
19
+ "archived",
20
+ "deleted",
21
+ ];
22
+
23
+ /** 勾选框文字。 */
24
+ export const YS_STATUS_LABELS: Record<YsStatusOption, string> = {
25
+ disabled: "显示已停用",
26
+ archived: "显示已归档",
27
+ deleted: "显示软删除",
28
+ };
29
+
30
+ /** 状态名称(提示文字里用)。 */
31
+ export const YS_STATUS_NAMES: Record<YsRecordStatus, string> = {
32
+ enabled: "启用",
33
+ disabled: "已停用",
34
+ archived: "已归档",
35
+ deleted: "已删除",
36
+ };
37
+
38
+ type StatusRow = { enabled?: unknown; archived?: unknown; deleted?: unknown };
39
+
40
+ /** 一条记录属于哪种状态。`deleted`、`archived` 只认 true;`enabled` 只有 false 才算停用。 */
41
+ export function ysRecordStatus(row: StatusRow): YsRecordStatus {
42
+ if (row.deleted === true) return "deleted";
43
+ if (row.archived === true) return "archived";
44
+ if (row.enabled === false) return "disabled";
45
+ return "enabled";
46
+ }
47
+
48
+ /** 记录是否属于当前查看的那一类。 */
49
+ export function ysStatusVisible(row: StatusRow, view: YsRecordStatus): boolean {
50
+ return ysRecordStatus(row) === view;
51
+ }
52
+
53
+ /** 四种状态各有几条(勾选框后面的数量、空表提示用)。 */
54
+ export function ysStatusCounts(rows: readonly StatusRow[]): YsStatusCounts {
55
+ const counts: YsStatusCounts = {
56
+ enabled: 0,
57
+ disabled: 0,
58
+ archived: 0,
59
+ deleted: 0,
60
+ };
61
+ for (const row of rows) counts[ysRecordStatus(row)] += 1;
62
+ return counts;
63
+ }
64
+
65
+ /** 勾选框变化后的查看状态:勾上就看这一类,取消勾选回到启用。 */
66
+ export function ysStatusToggle(
67
+ option: YsStatusOption,
68
+ checked: boolean,
69
+ ): YsRecordStatus {
70
+ return checked ? option : "enabled";
71
+ }
72
+
73
+ /**
74
+ * 操作后记录离开当前列表时,告诉用户去哪里找它;还在当前列表里返回空串。
75
+ * 例:默认列表里把记录改成停用 →「勾选「显示已停用」可查看」;在「显示已归档」里取消归档 →「取消勾选「显示已归档」可查看」或「勾选「显示已停用」可查看」。
76
+ */
77
+ export function ysStatusRevealHint(
78
+ view: YsRecordStatus,
79
+ status: YsRecordStatus,
80
+ ): string {
81
+ if (view === status) return "";
82
+ if (status === "enabled")
83
+ return `取消勾选「${YS_STATUS_LABELS[view as YsStatusOption]}」可查看`;
84
+ return `勾选「${YS_STATUS_LABELS[status]}」可查看`;
85
+ }
86
+
87
+ /**
88
+ * 当前查看的一类没有记录时,空表里说明另有几条在哪里;默认列表且别处也没有记录时返回空串(照常写「暂无数据」)。
89
+ * counts 按当前搜索和筛选范围统计,有搜索词时也能用。
90
+ * 例:「没有启用的记录。已停用 3 条,勾选上方「显示已停用」查看」;多类时「…已停用 3 条、已删除 2 条,勾选上方对应选项查看」。
91
+ */
92
+ export function ysStatusEmptyText(
93
+ view: YsRecordStatus,
94
+ counts: YsStatusCounts,
95
+ options: readonly YsStatusOption[] = YS_STATUS_OPTIONS,
96
+ ): string {
97
+ if (counts[view] > 0) return "";
98
+ const others = options.filter(
99
+ (option) => option !== view && counts[option] > 0,
100
+ );
101
+ // 表里没有启用字段(options 不含 disabled)时不说「启用的」,免得误解成有启停
102
+ const hasEnabled = options.includes("disabled");
103
+ if (view !== "enabled")
104
+ return counts.enabled > 0
105
+ ? `没有${YS_STATUS_NAMES[view]}的记录,取消勾选可查看${hasEnabled ? "启用的" : "其余"} ${counts.enabled} 条`
106
+ : `没有${YS_STATUS_NAMES[view]}的记录`;
107
+ if (!others.length) return "";
108
+ const parts = others
109
+ .map((option) => `${YS_STATUS_NAMES[option]} ${counts[option]} 条`)
110
+ .join("、");
111
+ // 只有一类时点名勾哪个;多类时不逐个列勾选框名,免得空表里折成两行
112
+ const where =
113
+ others.length === 1 ? `「${YS_STATUS_LABELS[others[0]!]}」` : "对应选项";
114
+ return `${hasEnabled ? "没有启用的记录" : "当前没有记录"}。${parts},勾选上方${where}查看`;
115
+ }