Open History to see the last commit
1export interface ZipEntry {
2 readonly path: string;
3 readonly name: string;
4 readonly isDirectory: boolean;
5 readonly sizeBytes: number;
6 readonly mimeType: string;
7 readonly bytes: Uint8Array<ArrayBuffer>;
8}
9
10export interface ZipTreeNode {
11 readonly path: string;
12 readonly label: string;
13 readonly entry: ZipEntry | null;
14 readonly children: ReadonlyArray<ZipTreeNode>;
15 readonly fileCount: number;
16}
17
18export interface ZipTreeRow {
19 readonly node: ZipTreeNode;
20 readonly depth: number;
21}
22
23interface MutableNode {
24 path: string;
25 name: string;
26 entry: ZipEntry | null;
27 children: Map<string, MutableNode>;
28}
29
30const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
31
32function freeze(node: MutableNode): ZipTreeNode {
33 let current = node;
34 let label = node.name;
35 while (current.path !== "" && current.entry === null && current.children.size === 1) {
36 const [only] = current.children.values();
37 if (!only || only.entry !== null) break;
38 current = only;
39 label = `${label}/${only.name}`;
40 }
41 const children = [...current.children.values()]
42 .sort((left, right) =>
43 (left.entry === null) !== (right.entry === null)
44 ? left.entry === null
45 ? -1
46 : 1
47 : collator.compare(left.name, right.name),
48 )
49 .map(freeze);
50 return {
51 path: current.path,
52 label,
53 entry: current.entry,
54 children,
55 fileCount:
56 current.entry === null ? children.reduce((sum, child) => sum + child.fileCount, 0) : 1,
57 };
58}
59
60export function buildZipTree(entries: ReadonlyArray<ZipEntry>): ReadonlyArray<ZipTreeNode> {
61 const root: MutableNode = { path: "", name: "", entry: null, children: new Map() };
62 for (const entry of entries) {
63 const parts = entry.path.split("/").filter(Boolean);
64 let current = root;
65 parts.forEach((part, index) => {
66 const leaf = index === parts.length - 1 && !entry.isDirectory;
67 const key = leaf ? part : `${part}/`;
68 let child = current.children.get(key);
69 if (!child) {
70 child = {
71 path: parts.slice(0, index + 1).join("/"),
72 name: part,
73 entry: leaf ? entry : null,
74 children: new Map(),
75 };
76 current.children.set(key, child);
77 }
78 current = child;
79 });
80 }
81 return freeze(root).children;
82}
83
84function flatten(
85 output: ZipTreeRow[],
86 node: ZipTreeNode,
87 depth: number,
88 collapsed: ReadonlySet<string>,
89 query: string,
90): boolean {
91 if (node.entry) {
92 if (query && !node.entry.path.toLowerCase().includes(query)) return false;
93 output.push({ node, depth });
94 return true;
95 }
96 const children: ZipTreeRow[] = [];
97 let visible = !query;
98 if (query || !collapsed.has(node.path)) {
99 for (const child of node.children) {
100 if (flatten(children, child, depth + 1, collapsed, query)) visible = true;
101 }
102 }
103 if (!visible) return false;
104 output.push({ node, depth }, ...children);
105 return true;
106}
107
108export function flattenZipTree(
109 nodes: ReadonlyArray<ZipTreeNode>,
110 collapsed: ReadonlySet<string>,
111 query: string,
112): ReadonlyArray<ZipTreeRow> {
113 const output: ZipTreeRow[] = [];
114 const normalized = query.trim().toLowerCase();
115 for (const node of nodes) flatten(output, node, 0, collapsed, normalized);
116 return output;
117}
118
119export function zipFolderLabel(label: string) {
120 const parts = label.split("/");
121 const name = parts.at(-1) ?? label;
122 if (parts.length > 3) return { prefix: `${parts[0]}/…/${parts.at(-2)}/`, name };
123 return { prefix: parts.length > 1 ? `${parts.slice(0, -1).join("/")}/` : "", name };
124}
125
126export function zipDownloadTimeLeft(input: {
127 readonly receivedBytes: number;
128 readonly totalBytes: number;
129 readonly elapsedMs: number;
130}) {
131 if (input.receivedBytes <= 0 || input.totalBytes <= input.receivedBytes || input.elapsedMs < 1000)
132 return null;
133 const seconds = Math.ceil(
134 ((input.totalBytes - input.receivedBytes) * input.elapsedMs) / input.receivedBytes / 1000,
135 );
136 return seconds < 60
137 ? `About ${seconds} ${seconds === 1 ? "second" : "seconds"} left`
138 : `About ${Math.ceil(seconds / 60)} minutes left`;
139}