workspaceFileEntriesCodec.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { WorkspaceFileEntry } from "@zcode/shared";
  2. /**
  3. * 工作区文件条目的列式编解码(worker/跨进程传输专用,零运行时依赖)。
  4. * 每行 `type\trelativePath`——name 是 relativePath 的最后段、path 由
  5. * rootPath 拼回,不重复传输(37 万条实测 65.7MB → ~25MB)。
  6. * Unix 文件名可含 \t、\n、\\,打包时转义、解包时还原。
  7. *
  8. * 传输形态选择(性能约束):
  9. * - RPC 顶层返回裸 string:走框架的 String 快速路径(长度前缀+原始字节),
  10. * 避免 Object 的 JSON.stringify/parse 对含 \t\n 大字符串的转义开销(实测 6-9s);
  11. * - 单条消息不超过 WORKSPACE_FILE_ENTRIES_CHUNK_SIZE(~4MB):大消息在 renderer
  12. * 接收端的分帧重组是秒级主线程长任务(实测 4.6-6.3s),分块拉取 + 块间让出
  13. * 事件循环后,主线程任何时刻只处理一小块(~50ms),输入永不冻结。
  14. */
  15. const FIELD_SEPARATOR = "\t";
  16. const LINE_SEPARATOR = "\n";
  17. /** 单块目标大小(字符数);调用方按 totalLength 分块拉取。 */
  18. export const WORKSPACE_FILE_ENTRIES_CHUNK_SIZE = 4_000_000;
  19. function escapeField(value: string): string {
  20. return value.replace(/\\/g, "\\\\").replace(/\t/g, "\\t").replace(/\n/g, "\\n");
  21. }
  22. // 正则一次性还原转义(比逐字符循环快数倍,37 万行下节省秒级 worker 时间)。
  23. const UNESCAPE_PATTERN = /\\(.)/g;
  24. function unescapeField(value: string): string {
  25. return value.replace(UNESCAPE_PATTERN, (match, char: string) => {
  26. if (char === "t") {
  27. return "\t";
  28. }
  29. if (char === "n") {
  30. return "\n";
  31. }
  32. return char;
  33. });
  34. }
  35. export function packWorkspaceFileEntries(entries: WorkspaceFileEntry[]): string {
  36. const lines: string[] = [];
  37. for (const entry of entries) {
  38. lines.push(`${entry.type}${FIELD_SEPARATOR}${escapeField(entry.relativePath)}`);
  39. }
  40. return lines.join(LINE_SEPARATOR);
  41. }
  42. /**
  43. * 解包并拼回完整 entries:name 取 relativePath 最后段,path 由 rootPath 拼出
  44. * (rootPath 以分隔符结尾与否都兼容;Windows 下用 \、其余用 /)。
  45. */
  46. export function unpackWorkspaceFileEntries(packed: string, rootPath: string): WorkspaceFileEntry[] {
  47. if (packed.length === 0) {
  48. return [];
  49. }
  50. const separator = rootPath.includes("\\") ? "\\" : "/";
  51. const prefix =
  52. rootPath.endsWith("/") || rootPath.endsWith("\\") ? rootPath : `${rootPath}${separator}`;
  53. const lines = packed.split(LINE_SEPARATOR);
  54. const entries: WorkspaceFileEntry[] = [];
  55. for (const line of lines) {
  56. if (!line) {
  57. continue;
  58. }
  59. const tabAt = line.indexOf(FIELD_SEPARATOR);
  60. if (tabAt === -1) {
  61. continue;
  62. }
  63. const relativePath = unescapeField(line.slice(tabAt + 1));
  64. const lastSlash = relativePath.lastIndexOf("/");
  65. // Windows root 下把 posix 相对路径的分隔符也换回 \,保持与 node:path.join 一致。
  66. const pathPart = separator === "/" ? relativePath : relativePath.split("/").join("\\");
  67. entries.push({
  68. name: lastSlash === -1 ? relativePath : relativePath.slice(lastSlash + 1),
  69. path: `${prefix}${pathPart}`,
  70. relativePath,
  71. type: line.slice(0, tabAt) === "directory" ? "directory" : "file",
  72. });
  73. }
  74. return entries;
  75. }