@celljs/ta-workspace 4.7.0 → 4.8.0

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.
@@ -0,0 +1,137 @@
1
+ /** A file of an archive: its path in it, with `/`, and its bytes. */
2
+ export interface ZipEntry {
3
+ path: string;
4
+ data: Uint8Array;
5
+ /** Of the last change; default now. */
6
+ modified?: Date;
7
+ }
8
+
9
+ /** Deflates bytes (raw deflate, without a header); undefined leaves them stored. */
10
+ export type Deflate = (data: Uint8Array) => Promise<Uint8Array>;
11
+
12
+ const CRC_TABLE = (() => {
13
+ const table = new Uint32Array(256);
14
+ for (let n = 0; n < 256; n++) {
15
+ let c = n;
16
+ for (let k = 0; k < 8; k++) {
17
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
18
+ }
19
+ table[n] = c >>> 0;
20
+ }
21
+ return table;
22
+ })();
23
+
24
+ /** At most 4 GB in all and 65535 entries: what a zip without its 64-bit extension holds. */
25
+ export const ZIP_MAX_BYTES = 0xffffffff;
26
+ export const ZIP_MAX_ENTRIES = 0xffff;
27
+
28
+ /** A zip archive in the page, as folders are downloaded: each file deflated when that makes it smaller. */
29
+ export namespace ZipUtil {
30
+ export function crc32(data: Uint8Array): number {
31
+ let crc = 0xffffffff;
32
+ for (let i = 0; i < data.length; i++) {
33
+ crc = CRC_TABLE[(crc ^ data[i]) & 0xff] ^ (crc >>> 8);
34
+ }
35
+ return (crc ^ 0xffffffff) >>> 0;
36
+ }
37
+
38
+ /** DOS time and date, as zip keeps them (local time, to two seconds). */
39
+ function dosTime(date: Date): [number, number] {
40
+ const year = Math.max(1980, date.getFullYear());
41
+ return [
42
+ (date.getHours() << 11) | (date.getMinutes() << 5) | Math.floor(date.getSeconds() / 2),
43
+ ((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(),
44
+ ];
45
+ }
46
+
47
+ export async function create(entries: readonly ZipEntry[], deflate?: Deflate): Promise<Uint8Array<ArrayBuffer>> {
48
+ if (entries.length > ZIP_MAX_ENTRIES) {
49
+ throw new Error(`An archive holds at most ${ZIP_MAX_ENTRIES} files.`);
50
+ }
51
+ const encoder = new TextEncoder();
52
+ const locals: Uint8Array[] = [];
53
+ const centrals: Uint8Array[] = [];
54
+ let offset = 0;
55
+ for (const entry of entries) {
56
+ const name = encoder.encode(entry.path.replace(/^\/+/, ''));
57
+ const crc = crc32(entry.data);
58
+ const deflated = deflate && entry.data.length > 0 ? await deflate(entry.data) : undefined;
59
+ const compressed = deflated && deflated.length < entry.data.length ? deflated : entry.data;
60
+ const method = compressed === entry.data ? 0 : 8;
61
+ const [time, date] = dosTime(entry.modified ?? new Date());
62
+ if (offset + 30 + name.length + compressed.length > ZIP_MAX_BYTES) {
63
+ throw new Error('An archive holds at most 4 GB.');
64
+ }
65
+ // Bit 11: the names are UTF-8.
66
+ const header = (signature: number, central: boolean) => {
67
+ const view = new DataView(new ArrayBuffer(central ? 46 : 30));
68
+ let at = 0;
69
+ const u16 = (value: number) => {
70
+ view.setUint16(at, value, true);
71
+ at += 2;
72
+ };
73
+ const u32 = (value: number) => {
74
+ view.setUint32(at, value >>> 0, true);
75
+ at += 4;
76
+ };
77
+ u32(signature);
78
+ if (central) {
79
+ u16(20);
80
+ }
81
+ u16(20);
82
+ u16(0x0800);
83
+ u16(method);
84
+ u16(time);
85
+ u16(date);
86
+ u32(crc);
87
+ u32(compressed.length);
88
+ u32(entry.data.length);
89
+ u16(name.length);
90
+ u16(0);
91
+ if (central) {
92
+ u16(0);
93
+ u16(0);
94
+ u16(0);
95
+ u32(0);
96
+ u32(offset);
97
+ }
98
+ return new Uint8Array(view.buffer);
99
+ };
100
+ const local = header(0x04034b50, false);
101
+ locals.push(local, name, compressed);
102
+ centrals.push(header(0x02014b50, true), name);
103
+ offset += local.length + name.length + compressed.length;
104
+ }
105
+ const centralSize = centrals.reduce((sum, part) => sum + part.length, 0);
106
+ const end = new DataView(new ArrayBuffer(22));
107
+ end.setUint32(0, 0x06054b50, true);
108
+ end.setUint16(8, entries.length, true);
109
+ end.setUint16(10, entries.length, true);
110
+ end.setUint32(12, centralSize, true);
111
+ end.setUint32(16, offset, true);
112
+ const parts = [...locals, ...centrals, new Uint8Array(end.buffer)];
113
+ const out = new Uint8Array(parts.reduce((sum, part) => sum + part.length, 0));
114
+ let at = 0;
115
+ for (const part of parts) {
116
+ out.set(part, at);
117
+ at += part.length;
118
+ }
119
+ return out;
120
+ }
121
+
122
+ /** Raw deflate of the browser (CompressionStream), where it has one. */
123
+ export function browserDeflate(): Deflate | undefined {
124
+ const Stream = (
125
+ globalThis as { CompressionStream?: new (format: string) => { readable: ReadableStream<Uint8Array>; writable: WritableStream<Uint8Array> } }
126
+ ).CompressionStream;
127
+ if (!Stream) {
128
+ return undefined;
129
+ }
130
+ return async data => {
131
+ const stream = new Stream('deflate-raw');
132
+ const writer = stream.writable.getWriter();
133
+ void writer.write(data).then(() => writer.close());
134
+ return new Uint8Array(await new Response(stream.readable).arrayBuffer());
135
+ };
136
+ }
137
+ }
@@ -1,14 +1,23 @@
1
- import type { TaCommandOutput, TaRunCommandOptions } from '@celljs/ta-protocol';
1
+ import { TA_UPLOAD_PART_BYTES, type TaCommandOutput, type TaDirEntry, type TaRunCommandOptions } from '@celljs/ta-protocol';
2
2
  import { pi } from './host';
3
3
  import { type FileDiff, type GitStatus, GitUtil } from './util/git';
4
4
  import { type Searcher, SearchUtil, type TextQuery, type TextResult } from './util/search';
5
+ import { type UploadBatch, UploadUtil } from './util/upload';
5
6
  import { WorkspaceUtil } from './util/workspace';
7
+ import { type ZipEntry, ZipUtil } from './util/zip';
6
8
 
7
9
  const SEARCH_LIMIT = 2000;
8
10
  const SEARCH_BYTES = 8 * 1024 * 1024;
9
11
  const SEARCHERS: Searcher[] = ['rg', 'git', 'grep'];
10
12
  /** Pulling and pushing wait for the network. */
11
13
  const REMOTE_TIMEOUT_MS = 120000;
14
+ /** A folder is downloaded as a zip made in the page: up to this much. */
15
+ const MAX_ARCHIVE_BYTES = 1024 * 1024 * 1024;
16
+ /** Files of a folder read at the same time while it is packed. */
17
+ const DOWNLOAD_CONCURRENCY = 4;
18
+
19
+ /** Bytes done of all, while files go up or a folder is packed; `name` is the file at hand. */
20
+ export type TransferProgress = (done: number, total: number, name: string) => void;
12
21
 
13
22
  /** Search, git and changes to files for the views: the programs of the server (runCommand), read here. */
14
23
  export namespace WorkspaceClient {
@@ -116,4 +125,97 @@ export namespace WorkspaceClient {
116
125
  export function remove(cwd: string, path: string): Promise<void> {
117
126
  return run(cwd, 'rm', ['-rf', '--', path]);
118
127
  }
128
+
129
+ /** Into `folder` ('' for `cwd`), never over what is there: `mv -n` keeps it. */
130
+ export function move(cwd: string, paths: string[], folder: string): Promise<void> {
131
+ return run(cwd, 'mv', ['-n', '--', ...paths, folder || '.']);
132
+ }
133
+
134
+ /** Files of the user's machine into `folder` of `cwd`, a part at a time; answers how many. */
135
+ export async function upload(cwd: string, folder: string, batch: UploadBatch, onProgress?: TransferProgress): Promise<number> {
136
+ for (const empty of UploadUtil.emptyFolders(batch)) {
137
+ await createFolder(cwd, WorkspaceUtil.join(folder, empty));
138
+ }
139
+ const total = UploadUtil.totalBytes(batch);
140
+ let sent = 0;
141
+ for (const { path, file } of batch.files) {
142
+ const target = WorkspaceUtil.join(folder, UploadUtil.segments(path).join('/'));
143
+ for (const [start, end] of UploadUtil.parts(file.size, TA_UPLOAD_PART_BYTES)) {
144
+ onProgress?.(sent, total, file.name);
145
+ const bytes = new Uint8Array(await file.slice(start, end).arrayBuffer());
146
+ await pi.workspace.uploadFile(target, UploadUtil.toBase64(bytes), { cwd, append: start > 0 });
147
+ sent += end - start;
148
+ }
149
+ }
150
+ onProgress?.(sent, total, '');
151
+ return batch.files.length;
152
+ }
153
+
154
+ /** The files of a folder at any depth, `.git` left out as the explorer does, with their sizes. */
155
+ export async function filesOf(cwd: string, folder: string): Promise<TaDirEntry[]> {
156
+ const files: TaDirEntry[] = [];
157
+ let level = [folder];
158
+ while (level.length) {
159
+ const listed = await Promise.all(level.map(dir => pi.workspace.listDirectory(dir, cwd)));
160
+ level = [];
161
+ for (const entry of listed.flat()) {
162
+ if (entry.kind === 'directory') {
163
+ level.push(entry.path);
164
+ } else {
165
+ files.push(entry);
166
+ }
167
+ }
168
+ }
169
+ return files;
170
+ }
171
+
172
+ /** A file saved by the browser; a folder as a zip of its files, made here (`''` for `cwd` itself). */
173
+ export async function download(cwd: string, entry: Pick<TaDirEntry, 'path' | 'name' | 'kind'>, onProgress?: TransferProgress): Promise<void> {
174
+ if (entry.kind === 'file') {
175
+ save(await pi.workspace.fileUrl(entry.path, cwd), entry.name);
176
+ return;
177
+ }
178
+ const files = await filesOf(cwd, entry.path);
179
+ const total = files.reduce((sum, file) => sum + (file.size ?? 0), 0);
180
+ if (total > MAX_ARCHIVE_BYTES) {
181
+ throw new Error(`${WorkspaceUtil.size(total)} is more than a folder downloaded here may hold (${WorkspaceUtil.size(MAX_ARCHIVE_BYTES)}).`);
182
+ }
183
+ // The URL of the folder: those of its files are below it, as a page there loads its own.
184
+ const base = (await pi.workspace.fileUrl(entry.path, cwd)).replace(/\/?$/, '/');
185
+ const offset = entry.path ? entry.path.length + 1 : 0;
186
+ const entries: ZipEntry[] = new Array(files.length);
187
+ let done = 0;
188
+ let next = 0;
189
+ const worker = async () => {
190
+ while (next < files.length) {
191
+ const index = next++;
192
+ const file = files[index];
193
+ const relative = file.path.slice(offset);
194
+ onProgress?.(done, total, file.name);
195
+ const response = await fetch(base + relative.split('/').map(encodeURIComponent).join('/'));
196
+ if (!response.ok) {
197
+ throw new Error(`${file.path}: HTTP ${response.status}`);
198
+ }
199
+ entries[index] = { path: `${entry.name}/${relative}`, data: new Uint8Array(await response.arrayBuffer()) };
200
+ done += file.size ?? 0;
201
+ }
202
+ };
203
+ await Promise.all(Array.from({ length: Math.min(DOWNLOAD_CONCURRENCY, files.length) }, worker));
204
+ onProgress?.(total, total, '');
205
+ const zip = await ZipUtil.create(entries, ZipUtil.browserDeflate());
206
+ const url = URL.createObjectURL(new Blob([zip], { type: 'application/zip' }));
207
+ save(url, `${entry.name}.zip`);
208
+ setTimeout(() => URL.revokeObjectURL(url), 60000);
209
+ }
210
+
211
+ function save(url: string, name: string): void {
212
+ const link = document.createElement('a');
213
+ link.href = url;
214
+ link.download = name;
215
+ link.rel = 'noopener';
216
+ link.style.display = 'none';
217
+ document.body.append(link);
218
+ link.click();
219
+ link.remove();
220
+ }
119
221
  }
@@ -1,66 +0,0 @@
1
- import { describe, expect, it } from 'bun:test';
2
- import { AnsiScreen, AnsiUtil } from './ansi';
3
-
4
- describe('AnsiScreen', () => {
5
- it('writes lines, returns and backspaces', () => {
6
- const screen = new AnsiScreen(20, 5);
7
- screen.feed('hello\r\nworld');
8
- screen.feed('\rW');
9
- screen.feed('orl\b\bX');
10
- expect(screen.text()).toBe('hello\nWoXld');
11
- expect(screen.cursor()).toEqual({ row: 1, col: 3 });
12
- });
13
-
14
- it('keeps colors, and a sequence cut in two', () => {
15
- const screen = new AnsiScreen();
16
- screen.feed('a\x1b[1;3');
17
- screen.feed('1mred\x1b[0m b');
18
- const [line] = screen.spans();
19
- expect(line).toEqual([
20
- { text: 'a', style: {} },
21
- { text: 'red', style: { bold: true, fg: 1 } },
22
- { text: ' b', style: {} },
23
- ]);
24
- screen.feed('\x1b[38;5;208mx\x1b[48;2;255;0;16my');
25
- const spans = screen.spans()[0];
26
- expect(spans[spans.length - 2].style).toEqual({ fg: 208 });
27
- expect(spans[spans.length - 1].style).toEqual({ fg: 208, bg: '#ff0010' });
28
- });
29
-
30
- it('drops titles and modes, and erases', () => {
31
- const screen = new AnsiScreen(40, 5);
32
- screen.feed('\x1b]2;title\x07\x1b]7;file://x\x1b\\\x1b[?2004h$ ls -la');
33
- screen.feed('\x1b[3D\x1b[K');
34
- expect(screen.text()).toBe('$ ls');
35
- screen.feed('\r\n1\r\n2\x1b[2J\x1b[Hok');
36
- expect(screen.text().split('\n').filter(Boolean).pop()).toBe('ok');
37
- screen.feed('\x1b[3J');
38
- expect(screen.text()).toBe('');
39
- });
40
-
41
- it('wraps long lines and keeps at most so many', () => {
42
- const screen = new AnsiScreen(4, 2, 3);
43
- screen.feed('abcdefgh\r\n1\r\n2');
44
- expect(screen.text()).toBe('efgh\n1\n2');
45
- });
46
-
47
- it('moves the cursor within the screen', () => {
48
- const screen = new AnsiScreen(10, 3);
49
- screen.feed('one\r\ntwo\r\nthree\x1b[2;1HTWO\x1b[1G\x1b[2CX');
50
- expect(screen.text()).toBe('one\nTWX\nthree');
51
- screen.feed('\x1b7\x1b[3;1Hz\x1b8!');
52
- expect(screen.text()).toBe('one\nTWX!\nzhree');
53
- });
54
- });
55
-
56
- describe('AnsiUtil', () => {
57
- it('maps the palette to the theme and to rgb', () => {
58
- expect(AnsiUtil.color(1)).toBe('var(--ta-red)');
59
- expect(AnsiUtil.color(8)).toBe('var(--ta-muted)');
60
- expect(AnsiUtil.color(196)).toBe('rgb(255, 0, 0)');
61
- expect(AnsiUtil.color(232)).toBe('rgb(8, 8, 8)');
62
- expect(AnsiUtil.css({})).toBeUndefined();
63
- expect(AnsiUtil.css({ inverse: true })).toEqual({ color: 'var(--ta-main)', background: 'var(--ta-text)' });
64
- expect(AnsiUtil.css({ bold: true, fg: 2 })).toEqual({ color: 'var(--ta-green)', fontWeight: '600' });
65
- });
66
- });