@celljs/ta-extensions 0.0.0-stage → 4.6.1
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/README.md +19 -2
- package/host/index.mjs +271 -0
- package/package.json +44 -4
- package/skills/ta-package/SKILL.md +90 -0
- package/web/index.js +941 -0
- package/web-src/actions.ts +184 -0
- package/web-src/archive.spec.ts +101 -0
- package/web-src/archive.ts +229 -0
- package/web-src/extensions-util.spec.ts +38 -0
- package/web-src/extensions-util.ts +40 -0
- package/web-src/host.spec.ts +72 -0
- package/web-src/host.ts +101 -0
- package/web-src/i18n.ts +157 -0
- package/web-src/import-dialog.tsx +89 -0
- package/web-src/index.tsx +46 -0
- package/web-src/page.tsx +246 -0
- package/web-src/pictures.tsx +87 -0
- package/web-src/react.ts +11 -0
- package/web-src/style.ts +90 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { TaPackageInfo } from '@celljs/ta-protocol';
|
|
2
|
+
import type { WebExtensionAPI } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
3
|
+
import { ExtensionsUtil } from './extensions-util';
|
|
4
|
+
import type { ExportedPackage, ImportedPackage } from './host';
|
|
5
|
+
import { ExtensionWords, type Words } from './i18n';
|
|
6
|
+
|
|
7
|
+
export interface PackagesState {
|
|
8
|
+
/** Undefined until the first list came. */
|
|
9
|
+
packages?: TaPackageInfo[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The packages of the server, listed again after each change and each reply of Ta, which may have made one. */
|
|
13
|
+
export class PackagesStore {
|
|
14
|
+
protected current: PackagesState = {};
|
|
15
|
+
protected readonly listeners = new Set<() => void>();
|
|
16
|
+
|
|
17
|
+
constructor(protected readonly pi: WebExtensionAPI) {}
|
|
18
|
+
|
|
19
|
+
get state(): PackagesState {
|
|
20
|
+
return this.current;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
subscribe(listener: () => void): () => void {
|
|
24
|
+
this.listeners.add(listener);
|
|
25
|
+
return () => this.listeners.delete(listener);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Shown at once, before the server confirms; the next load says what holds. */
|
|
29
|
+
patch(id: string, change: Partial<TaPackageInfo>): void {
|
|
30
|
+
this.current = { packages: this.current.packages?.map(pkg => (pkg.id === id ? { ...pkg, ...change } : pkg)) };
|
|
31
|
+
this.publish();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async load(): Promise<void> {
|
|
35
|
+
try {
|
|
36
|
+
this.current = { packages: await this.pi.packages.listPackages() };
|
|
37
|
+
} catch (error) {
|
|
38
|
+
this.current = { packages: this.current.packages ?? [] };
|
|
39
|
+
this.pi.ui.notify(error instanceof Error ? error.message : String(error), 'error');
|
|
40
|
+
}
|
|
41
|
+
this.publish();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected publish(): void {
|
|
45
|
+
for (const listener of this.listeners) {
|
|
46
|
+
listener();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What the page does, each in the words of the page, failures as notices. */
|
|
52
|
+
export class ExtensionActions {
|
|
53
|
+
constructor(
|
|
54
|
+
protected readonly pi: WebExtensionAPI,
|
|
55
|
+
protected readonly store: PackagesStore,
|
|
56
|
+
) {}
|
|
57
|
+
|
|
58
|
+
get words(): Words {
|
|
59
|
+
return ExtensionWords.of(this.pi.ui.language);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A new conversation, shown, in which Ta makes the extension. */
|
|
63
|
+
make(text: string): Promise<void> {
|
|
64
|
+
return this.converse(ExtensionWords.fill(this.words.makePrompt, { text: text.trim() }));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async change(pkg: TaPackageInfo): Promise<void> {
|
|
68
|
+
const words = this.words;
|
|
69
|
+
const text = await this.pi.ui.input(ExtensionWords.fill(words.changeTitle, { name: pkg.name }), words.changePlaceholder);
|
|
70
|
+
if (text?.trim()) {
|
|
71
|
+
await this.converse(ExtensionWords.fill(words.changePrompt, { name: pkg.name, root: pkg.root, text: text.trim() }));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async exportPackage(pkg: TaPackageInfo): Promise<void> {
|
|
76
|
+
await this.guard(async () => {
|
|
77
|
+
const { fileName, data } = await this.pi.callHost<ExportedPackage>('exportPackage', pkg.id);
|
|
78
|
+
const url = URL.createObjectURL(new Blob([ExtensionsUtil.fromBase64(data) as BlobPart], { type: 'application/gzip' }));
|
|
79
|
+
const link = document.createElement('a');
|
|
80
|
+
link.href = url;
|
|
81
|
+
link.download = fileName;
|
|
82
|
+
document.body.appendChild(link);
|
|
83
|
+
link.click();
|
|
84
|
+
link.remove();
|
|
85
|
+
setTimeout(() => URL.revokeObjectURL(url), 10_000);
|
|
86
|
+
this.pi.ui.notify(ExtensionWords.fill(this.words.exported, { file: fileName }));
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async remove(pkg: TaPackageInfo): Promise<void> {
|
|
91
|
+
const words = this.words;
|
|
92
|
+
const confirmed = await this.pi.ui.confirm(ExtensionWords.fill(words.removeTitle, { name: pkg.name }), words.removeDetail, {
|
|
93
|
+
confirmLabel: words.remove,
|
|
94
|
+
danger: true,
|
|
95
|
+
});
|
|
96
|
+
if (confirmed) {
|
|
97
|
+
await this.guard(async () => {
|
|
98
|
+
await this.pi.packages.removePackage(pkg.id);
|
|
99
|
+
await this.changed();
|
|
100
|
+
this.pi.ui.notify(ExtensionWords.fill(words.removed, { name: pkg.name }));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async setEnabled(pkg: TaPackageInfo, enabled: boolean): Promise<void> {
|
|
106
|
+
this.store.patch(pkg.id, { enabled });
|
|
107
|
+
await this.guard(async () => {
|
|
108
|
+
await this.pi.packages.setPackageEnabled(pkg.id, enabled);
|
|
109
|
+
await this.changed();
|
|
110
|
+
this.pi.ui.notify(ExtensionWords.fill(enabled ? this.words.turnedOn : this.words.turnedOff, { name: pkg.name }));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async copySource(pkg: TaPackageInfo): Promise<void> {
|
|
115
|
+
await this.guard(async () => {
|
|
116
|
+
await navigator.clipboard.writeText(pkg.source);
|
|
117
|
+
this.pi.ui.notify(ExtensionWords.fill(this.words.copied, { source: pkg.source }));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** True once it is in: imported, or the user kept the one there. */
|
|
122
|
+
async importFile(file: File): Promise<boolean> {
|
|
123
|
+
const words = this.words;
|
|
124
|
+
if (!ExtensionsUtil.isArchive(file.name)) {
|
|
125
|
+
this.pi.ui.notify(words.notArchive, 'warning');
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
return this.guard(async () => {
|
|
129
|
+
const data = ExtensionsUtil.toBase64(new Uint8Array(await file.arrayBuffer()));
|
|
130
|
+
let result = await this.pi.callHost<ImportedPackage>('importPackage', data, false);
|
|
131
|
+
if (result.status === 'exists') {
|
|
132
|
+
const replace = await this.pi.ui.confirm(ExtensionWords.fill(words.existsTitle, { name: result.name }), words.existsDetail, {
|
|
133
|
+
confirmLabel: words.replace,
|
|
134
|
+
danger: true,
|
|
135
|
+
});
|
|
136
|
+
if (!replace) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
result = await this.pi.callHost<ImportedPackage>('importPackage', data, true);
|
|
140
|
+
}
|
|
141
|
+
await this.changed();
|
|
142
|
+
this.pi.ui.notify(ExtensionWords.fill(words.imported, { name: result.name }));
|
|
143
|
+
return true;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async install(source: string): Promise<boolean> {
|
|
148
|
+
const trimmed = source.trim();
|
|
149
|
+
if (!trimmed) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return this.guard(async () => {
|
|
153
|
+
await this.pi.packages.installPackage(trimmed, 'user');
|
|
154
|
+
await this.changed();
|
|
155
|
+
this.pi.ui.notify(ExtensionWords.fill(this.words.installedOk, { name: trimmed }));
|
|
156
|
+
return true;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The page and the conversation shown load what changed, and the list follows. */
|
|
161
|
+
protected async changed(): Promise<void> {
|
|
162
|
+
await this.pi.reloadExtensions();
|
|
163
|
+
await this.store.load();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
protected async converse(text: string): Promise<void> {
|
|
167
|
+
await this.guard(async () => {
|
|
168
|
+
const { cwd } = await this.pi.server.getServerInfo();
|
|
169
|
+
const { sessionId } = await this.pi.server.openSession({ cwd });
|
|
170
|
+
await this.pi.switchSession(sessionId);
|
|
171
|
+
await this.pi.sendUserMessage(text);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
protected async guard<T>(action: () => Promise<T>): Promise<T | false> {
|
|
176
|
+
try {
|
|
177
|
+
return await action();
|
|
178
|
+
} catch (error) {
|
|
179
|
+
this.pi.ui.notify(error instanceof Error ? error.message : String(error), 'error');
|
|
180
|
+
await this.store.load();
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { gzipSync } from 'node:zlib';
|
|
7
|
+
import { ExtensionArchive } from './archive';
|
|
8
|
+
|
|
9
|
+
function write(file: string, content: string) {
|
|
10
|
+
mkdirSync(join(file, '..'), { recursive: true });
|
|
11
|
+
writeFileSync(file, content);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A tar of one entry, as another tar program could write it. */
|
|
15
|
+
function entry(name: string, body = '', type = '0'): Buffer {
|
|
16
|
+
const header = Buffer.alloc(512);
|
|
17
|
+
header.write(name, 0, 100);
|
|
18
|
+
header.write('0000644\0', 100);
|
|
19
|
+
header.write(`${body.length.toString(8).padStart(11, '0')}\0`, 124);
|
|
20
|
+
header.write('00000000000\0', 136);
|
|
21
|
+
header.write(type, 156);
|
|
22
|
+
header.write('ustar\u000000', 257, 'latin1');
|
|
23
|
+
header.fill(32, 148, 156);
|
|
24
|
+
const sum = header.reduce((total, byte) => total + byte, 0);
|
|
25
|
+
header.write(`${sum.toString(8).padStart(6, '0')}\0 `, 148, 'latin1');
|
|
26
|
+
const data = Buffer.alloc(Math.ceil(body.length / 512) * 512);
|
|
27
|
+
data.write(body);
|
|
28
|
+
return Buffer.concat([header, data]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const tgz = (...entries: Buffer[]) => gzipSync(Buffer.concat([...entries, Buffer.alloc(1024)]));
|
|
32
|
+
|
|
33
|
+
describe('ExtensionArchive', () => {
|
|
34
|
+
let home: string;
|
|
35
|
+
let root: string;
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
home = mkdtempSync(join(tmpdir(), 'ta-ext-archive-'));
|
|
38
|
+
root = join(home, 'pomodoro');
|
|
39
|
+
write(join(root, 'package.json'), JSON.stringify({ name: 'pomodoro', version: '0.1.0' }));
|
|
40
|
+
write(join(root, 'web.js'), 'export default function () {}\n');
|
|
41
|
+
write(join(root, 'bin/run.sh'), '#!/bin/sh\necho hi\n');
|
|
42
|
+
chmodSync(join(root, 'bin/run.sh'), 0o755);
|
|
43
|
+
write(join(root, 'node_modules/left/index.js'), '');
|
|
44
|
+
write(join(root, '.git/HEAD'), '');
|
|
45
|
+
});
|
|
46
|
+
afterEach(() => rmSync(home, { recursive: true, force: true }));
|
|
47
|
+
|
|
48
|
+
it('packs a package without node_modules and .git, and reads it back', () => {
|
|
49
|
+
const files = ExtensionArchive.collect(root);
|
|
50
|
+
expect(files.map(file => file.path)).toEqual(['bin/run.sh', 'package.json', 'web.js']);
|
|
51
|
+
const { top, files: back } = ExtensionArchive.unpack(ExtensionArchive.pack('pomodoro', files));
|
|
52
|
+
expect(top).toBe('pomodoro');
|
|
53
|
+
expect(back.map(file => [file.path, Buffer.from(file.data).toString(), file.executable])).toEqual(
|
|
54
|
+
files.map(file => [file.path, Buffer.from(file.data).toString(), file.executable]),
|
|
55
|
+
);
|
|
56
|
+
expect(back.find(file => file.path === 'bin/run.sh')?.executable).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('leaves links out', () => {
|
|
60
|
+
symlinkSync('/etc/hosts', join(root, 'hosts'));
|
|
61
|
+
expect(ExtensionArchive.collect(root).map(file => file.path)).not.toContain('hosts');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('makes what tar reads, and reads what tar makes', () => {
|
|
65
|
+
const deep = `${'a'.repeat(60)}/${'b'.repeat(60)}/file.js`;
|
|
66
|
+
write(join(root, deep), 'deep');
|
|
67
|
+
const archive = join(home, 'pomodoro.tgz');
|
|
68
|
+
writeFileSync(archive, ExtensionArchive.pack('pomodoro', ExtensionArchive.collect(root)));
|
|
69
|
+
const out = join(home, 'out');
|
|
70
|
+
mkdirSync(out);
|
|
71
|
+
execFileSync('tar', ['-xzf', archive, '-C', out]);
|
|
72
|
+
expect(readFileSync(join(out, 'pomodoro', deep), 'utf8')).toBe('deep');
|
|
73
|
+
expect(statSync(join(out, 'pomodoro/bin/run.sh')).mode & 0o111).not.toBe(0);
|
|
74
|
+
const made = join(home, 'made.tgz');
|
|
75
|
+
execFileSync('tar', ['-czf', made, '--exclude', 'node_modules', '--exclude', '.git', '-C', home, 'pomodoro']);
|
|
76
|
+
const { top, files } = ExtensionArchive.unpack(readFileSync(made));
|
|
77
|
+
expect(top).toBe('pomodoro');
|
|
78
|
+
expect(files.map(file => file.path).sort()).toContain(deep);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('reads the package/ of npm pack', () => {
|
|
82
|
+
const { top, files } = ExtensionArchive.unpack(tgz(entry('package/package.json', '{"name":"@you/tools"}'), entry('package/index.js', '1')));
|
|
83
|
+
expect(top).toBe('package');
|
|
84
|
+
expect(files.map(file => file.path)).toEqual(['package.json', 'index.js']);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('refuses paths out of the directory, more than one directory, and no package.json', () => {
|
|
88
|
+
expect(() => ExtensionArchive.unpack(tgz(entry('x/package.json', '{}'), entry('x/../../evil', 'x')))).toThrow('out of its directory');
|
|
89
|
+
expect(() => ExtensionArchive.unpack(tgz(entry('/etc/passwd', 'x')))).toThrow('out of its directory');
|
|
90
|
+
expect(() => ExtensionArchive.unpack(tgz(entry('a/package.json', '{}'), entry('b/index.js', '')))).toThrow('more than one directory');
|
|
91
|
+
expect(() => ExtensionArchive.unpack(tgz(entry('a/index.js', '')))).toThrow('no package.json');
|
|
92
|
+
expect(() => ExtensionArchive.unpack(new Uint8Array([1, 2, 3]))).toThrow('not a .tgz');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('skips links in an archive, and what the tar of macOS adds', () => {
|
|
96
|
+
const { files } = ExtensionArchive.unpack(
|
|
97
|
+
tgz(entry('._a', 'x'), entry('a/package.json', '{}'), entry('a/._package.json', 'x'), entry('a/link', '', '2')),
|
|
98
|
+
);
|
|
99
|
+
expect(files.map(file => file.path)).toEqual(['package.json']);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { lstatSync, readdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { gunzipSync, gzipSync } from 'node:zlib';
|
|
4
|
+
|
|
5
|
+
/** A file of a package, by its path inside the package, with `/` between the parts. */
|
|
6
|
+
export interface ArchiveFile {
|
|
7
|
+
path: string;
|
|
8
|
+
data: Uint8Array;
|
|
9
|
+
executable?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** An archive read back: the directory at its top and the files under it, by their paths inside it. */
|
|
13
|
+
export interface UnpackedArchive {
|
|
14
|
+
top: string;
|
|
15
|
+
files: ArchiveFile[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const BLOCK = 512;
|
|
19
|
+
/** What a package carries that the one who gets it installs or has already. */
|
|
20
|
+
const LEFT_OUT = new Set(['node_modules', '.git', '.DS_Store']);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A package in one file to hand on: a gzipped tar of its directory, as `tar -czf <name>.tgz <name>` makes
|
|
24
|
+
* it, so that `tar` reads it too. Only directories and plain files; links and the rest are left out.
|
|
25
|
+
*/
|
|
26
|
+
export namespace ExtensionArchive {
|
|
27
|
+
/** Unpacked, at most: a package is code and a few assets, not data. */
|
|
28
|
+
export const MAX_BYTES = 20 * 1024 * 1024;
|
|
29
|
+
export const MAX_FILES = 5000;
|
|
30
|
+
|
|
31
|
+
/** The files of the package in `root`, without node_modules and .git; links are left out. */
|
|
32
|
+
export function collect(root: string): ArchiveFile[] {
|
|
33
|
+
const files: ArchiveFile[] = [];
|
|
34
|
+
let bytes = 0;
|
|
35
|
+
const walk = (directory: string, prefix: string) => {
|
|
36
|
+
for (const name of readdirSync(directory).sort()) {
|
|
37
|
+
if (LEFT_OUT.has(name)) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const file = join(directory, name);
|
|
41
|
+
const stat = lstatSync(file);
|
|
42
|
+
const path = prefix ? `${prefix}/${name}` : name;
|
|
43
|
+
if (stat.isDirectory()) {
|
|
44
|
+
walk(file, path);
|
|
45
|
+
} else if (stat.isFile()) {
|
|
46
|
+
bytes += stat.size;
|
|
47
|
+
if (files.length >= MAX_FILES || bytes > MAX_BYTES) {
|
|
48
|
+
throw new Error(`The package in ${root} is larger than ${MAX_FILES} files or ${MAX_BYTES / 1024 / 1024} MB.`);
|
|
49
|
+
}
|
|
50
|
+
files.push({ path, data: readFileSync(file), executable: (stat.mode & 0o111) !== 0 });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
walk(root, '');
|
|
55
|
+
return files;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function pack(top: string, files: ArchiveFile[], mtime = Date.now()): Uint8Array {
|
|
59
|
+
const blocks: Uint8Array[] = [header(`${top}/`, 0, 0o755, '5', mtime)];
|
|
60
|
+
const directories = new Set<string>();
|
|
61
|
+
for (const file of files) {
|
|
62
|
+
const parts = file.path.split('/');
|
|
63
|
+
for (let i = 1; i < parts.length; i++) {
|
|
64
|
+
const directory = parts.slice(0, i).join('/');
|
|
65
|
+
if (!directories.has(directory)) {
|
|
66
|
+
directories.add(directory);
|
|
67
|
+
blocks.push(header(`${top}/${directory}/`, 0, 0o755, '5', mtime));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
blocks.push(header(`${top}/${file.path}`, file.data.length, file.executable ? 0o755 : 0o644, '0', mtime));
|
|
71
|
+
blocks.push(file.data, new Uint8Array(padding(file.data.length)));
|
|
72
|
+
}
|
|
73
|
+
blocks.push(new Uint8Array(BLOCK * 2));
|
|
74
|
+
return gzipSync(Buffer.concat(blocks));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The files of an archive of {@link pack}, or of any tar of one directory (`npm pack` makes one of
|
|
79
|
+
* `package/`). Throws for what is not one: paths out of the directory, more than one at the top, no
|
|
80
|
+
* package.json there, more than the limits.
|
|
81
|
+
*/
|
|
82
|
+
export function unpack(archive: Uint8Array): UnpackedArchive {
|
|
83
|
+
let tar: Buffer;
|
|
84
|
+
try {
|
|
85
|
+
tar = gunzipSync(archive, { maxOutputLength: MAX_BYTES + MAX_FILES * BLOCK * 2 });
|
|
86
|
+
} catch {
|
|
87
|
+
throw new Error('The file is not a .tgz of a package, or is larger than the limit.');
|
|
88
|
+
}
|
|
89
|
+
const files: ArchiveFile[] = [];
|
|
90
|
+
let top: string | undefined;
|
|
91
|
+
let longPath: string | undefined;
|
|
92
|
+
let bytes = 0;
|
|
93
|
+
for (let offset = 0; offset + BLOCK <= tar.length; ) {
|
|
94
|
+
const block = tar.subarray(offset, offset + BLOCK);
|
|
95
|
+
if (block.every(byte => byte === 0)) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (!checksumOk(block)) {
|
|
99
|
+
throw new Error('The file is not a .tgz of a package: a header of it is broken.');
|
|
100
|
+
}
|
|
101
|
+
const size = octal(block.subarray(124, 136));
|
|
102
|
+
const type = String.fromCharCode(block[156] || 48);
|
|
103
|
+
const body = tar.subarray(offset + BLOCK, offset + BLOCK + size);
|
|
104
|
+
offset += BLOCK + size + padding(size);
|
|
105
|
+
if (type === 'x') {
|
|
106
|
+
longPath = paxPath(body) ?? longPath;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (type === 'L') {
|
|
110
|
+
longPath = text(body);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const name = longPath ?? joinName(text(block.subarray(345, 500)), text(block.subarray(0, 100)));
|
|
114
|
+
longPath = undefined;
|
|
115
|
+
if (type !== '0' && type !== '7' && type !== '5') {
|
|
116
|
+
// Links, devices, global headers: a package has none that matter.
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const parts = safeParts(name);
|
|
120
|
+
if (parts.some(part => part.startsWith('._') || part === '__MACOSX')) {
|
|
121
|
+
// What the tar of macOS adds for the attributes of each file.
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (top === undefined) {
|
|
125
|
+
top = parts[0];
|
|
126
|
+
} else if (parts[0] !== top) {
|
|
127
|
+
throw new Error(`The file holds more than one directory: ${top} and ${parts[0]}.`);
|
|
128
|
+
}
|
|
129
|
+
if (type === '5' || parts.length === 1) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
bytes += size;
|
|
133
|
+
if (files.length >= MAX_FILES || bytes > MAX_BYTES) {
|
|
134
|
+
throw new Error(`The package is larger than ${MAX_FILES} files or ${MAX_BYTES / 1024 / 1024} MB.`);
|
|
135
|
+
}
|
|
136
|
+
files.push({ path: parts.slice(1).join('/'), data: new Uint8Array(body), executable: (octal(block.subarray(100, 108)) & 0o111) !== 0 });
|
|
137
|
+
}
|
|
138
|
+
if (top === undefined || !files.some(file => file.path === 'package.json')) {
|
|
139
|
+
throw new Error('The file is not a package: it has no package.json in its directory.');
|
|
140
|
+
}
|
|
141
|
+
return { top, files };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function header(path: string, size: number, mode: number, type: string, mtime: number): Uint8Array {
|
|
145
|
+
const block = Buffer.alloc(BLOCK);
|
|
146
|
+
const [prefix, name] = split(path);
|
|
147
|
+
block.write(name, 0, 100, 'utf8');
|
|
148
|
+
field(block, 100, 8, mode);
|
|
149
|
+
field(block, 108, 8, 0);
|
|
150
|
+
field(block, 116, 8, 0);
|
|
151
|
+
field(block, 124, 12, size);
|
|
152
|
+
field(block, 136, 12, Math.floor(mtime / 1000));
|
|
153
|
+
block.write(type, 156, 1, 'latin1');
|
|
154
|
+
block.write('ustar\u000000', 257, 8, 'latin1');
|
|
155
|
+
block.write(prefix, 345, 155, 'utf8');
|
|
156
|
+
block.fill(32, 148, 156);
|
|
157
|
+
const sum = block.reduce((total, byte) => total + byte, 0);
|
|
158
|
+
block.write(`${sum.toString(8).padStart(6, '0')}\u0000 `, 148, 8, 'latin1');
|
|
159
|
+
return block;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** ustar's two fields of a path: up to 155 bytes of directories, and up to 100 bytes of the rest. */
|
|
163
|
+
function split(path: string): [string, string] {
|
|
164
|
+
if (Buffer.byteLength(path) <= 100) {
|
|
165
|
+
return ['', path];
|
|
166
|
+
}
|
|
167
|
+
for (let at = path.indexOf('/'); at > 0; at = path.indexOf('/', at + 1)) {
|
|
168
|
+
const prefix = path.slice(0, at);
|
|
169
|
+
const name = path.slice(at + 1);
|
|
170
|
+
if (Buffer.byteLength(prefix) <= 155 && Buffer.byteLength(name) <= 100) {
|
|
171
|
+
return [prefix, name];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
throw new Error(`The path ${path} is too long for the archive.`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function field(block: Buffer, offset: number, length: number, value: number): void {
|
|
178
|
+
block.write(`${value.toString(8).padStart(length - 1, '0')}\u0000`, offset, length, 'latin1');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function octal(bytes: Uint8Array): number {
|
|
182
|
+
const value = Number.parseInt(text(bytes).trim() || '0', 8);
|
|
183
|
+
return Number.isFinite(value) ? value : 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function text(bytes: Uint8Array): string {
|
|
187
|
+
const end = bytes.indexOf(0);
|
|
188
|
+
return Buffer.from(end < 0 ? bytes : bytes.subarray(0, end)).toString('utf8');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function checksumOk(block: Uint8Array): boolean {
|
|
192
|
+
let sum = 0;
|
|
193
|
+
for (let i = 0; i < BLOCK; i++) {
|
|
194
|
+
sum += i >= 148 && i < 156 ? 32 : block[i];
|
|
195
|
+
}
|
|
196
|
+
return sum === octal(block.subarray(148, 156));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function joinName(prefix: string, name: string): string {
|
|
200
|
+
return prefix ? `${prefix}/${name}` : name;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The `path` record of a pax header: `<length> path=<path>\n`. */
|
|
204
|
+
function paxPath(body: Uint8Array): string | undefined {
|
|
205
|
+
for (const record of Buffer.from(body).toString('utf8').split('\n')) {
|
|
206
|
+
const match = /^\d+ path=(.*)$/.exec(record);
|
|
207
|
+
if (match) {
|
|
208
|
+
return match[1];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** The parts of a path that stays in the directory of the archive: nothing absolute, no `..`. */
|
|
215
|
+
function safeParts(path: string): string[] {
|
|
216
|
+
if (path.startsWith('/') || path.includes('\\') || path.includes('\u0000') || /^[a-zA-Z]:/.test(path)) {
|
|
217
|
+
throw new Error(`The file has a path out of its directory: ${path}`);
|
|
218
|
+
}
|
|
219
|
+
const parts = path.split('/').filter(part => part !== '' && part !== '.');
|
|
220
|
+
if (parts.length === 0 || parts.some(part => part === '..')) {
|
|
221
|
+
throw new Error(`The file has a path out of its directory: ${path}`);
|
|
222
|
+
}
|
|
223
|
+
return parts;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function padding(size: number): number {
|
|
227
|
+
return (BLOCK - (size % BLOCK)) % BLOCK;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import type { TaPackageInfo } from '@celljs/ta-protocol';
|
|
3
|
+
import { ExtensionsUtil } from './extensions-util';
|
|
4
|
+
|
|
5
|
+
const pkg = (name: string, origin: TaPackageInfo['origin']): TaPackageInfo => ({
|
|
6
|
+
id: name,
|
|
7
|
+
name,
|
|
8
|
+
scope: 'user',
|
|
9
|
+
origin,
|
|
10
|
+
enabled: true,
|
|
11
|
+
source: name,
|
|
12
|
+
root: `/home/${name}`,
|
|
13
|
+
resources: [],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('ExtensionsUtil', () => {
|
|
17
|
+
it('lists the packages of the user apart from those installed, by name, without the built-in ones', () => {
|
|
18
|
+
const groups = ExtensionsUtil.group([
|
|
19
|
+
pkg('zeta', 'directory'),
|
|
20
|
+
pkg('@celljs/ta-channels', 'builtin'),
|
|
21
|
+
pkg('tools', 'package'),
|
|
22
|
+
pkg('alpha', 'directory'),
|
|
23
|
+
]);
|
|
24
|
+
expect(groups.mine.map(info => info.name)).toEqual(['alpha', 'zeta']);
|
|
25
|
+
expect(groups.installed.map(info => info.name)).toEqual(['tools']);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('takes .tgz and .tar.gz files', () => {
|
|
29
|
+
expect(ExtensionsUtil.isArchive('pomodoro-0.1.0.tgz')).toBe(true);
|
|
30
|
+
expect(ExtensionsUtil.isArchive('Pomodoro.TAR.GZ')).toBe(true);
|
|
31
|
+
expect(ExtensionsUtil.isArchive('pomodoro.zip')).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('carries bytes through base64, also many of them', () => {
|
|
35
|
+
const bytes = new Uint8Array(200_000).map((_, i) => (i * 7) % 256);
|
|
36
|
+
expect(ExtensionsUtil.fromBase64(ExtensionsUtil.toBase64(bytes))).toEqual(bytes);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { TaPackageInfo } from '@celljs/ta-protocol';
|
|
2
|
+
|
|
3
|
+
/** What the page lists, apart: the packages of the user's own (made by Ta, or imported from a file) and those installed by a source. */
|
|
4
|
+
export interface PackageGroups {
|
|
5
|
+
mine: TaPackageInfo[];
|
|
6
|
+
installed: TaPackageInfo[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export namespace ExtensionsUtil {
|
|
10
|
+
export function group(packages: readonly TaPackageInfo[]): PackageGroups {
|
|
11
|
+
const byName = (a: TaPackageInfo, b: TaPackageInfo) => a.name.localeCompare(b.name);
|
|
12
|
+
return {
|
|
13
|
+
mine: packages.filter(pkg => pkg.origin === 'directory').sort(byName),
|
|
14
|
+
installed: packages.filter(pkg => pkg.origin === 'package').sort(byName),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A file the host takes: a gzipped tar, as Export makes it or `npm pack`. */
|
|
19
|
+
export function isArchive(fileName: string): boolean {
|
|
20
|
+
return /\.(tgz|tar\.gz)$/i.test(fileName);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function toBase64(bytes: Uint8Array): string {
|
|
24
|
+
let binary = '';
|
|
25
|
+
// In pieces: one call with every byte as an argument overflows the stack.
|
|
26
|
+
for (let at = 0; at < bytes.length; at += 0x8000) {
|
|
27
|
+
binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000));
|
|
28
|
+
}
|
|
29
|
+
return btoa(binary);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function fromBase64(data: string): Uint8Array {
|
|
33
|
+
const binary = atob(data);
|
|
34
|
+
const bytes = new Uint8Array(binary.length);
|
|
35
|
+
for (let i = 0; i < binary.length; i++) {
|
|
36
|
+
bytes[i] = binary.charCodeAt(i);
|
|
37
|
+
}
|
|
38
|
+
return bytes;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import type { TaExtensionHost, TaPackageInfo } from '@celljs/ta-protocol';
|
|
6
|
+
import { ExtensionsService, PackageFiles } from './host';
|
|
7
|
+
|
|
8
|
+
function write(file: string, content: string) {
|
|
9
|
+
mkdirSync(join(file, '..'), { recursive: true });
|
|
10
|
+
writeFileSync(file, content);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('ExtensionsService', () => {
|
|
14
|
+
let home: string;
|
|
15
|
+
let agentDir: string;
|
|
16
|
+
let packages: TaPackageInfo[];
|
|
17
|
+
let service: ExtensionsService;
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
home = mkdtempSync(join(tmpdir(), 'ta-ext-host-'));
|
|
20
|
+
agentDir = join(home, 'agent');
|
|
21
|
+
const root = join(agentDir, 'extensions/pomodoro');
|
|
22
|
+
write(join(root, 'package.json'), JSON.stringify({ name: 'pomodoro', version: '0.2.0' }));
|
|
23
|
+
write(join(root, 'web.js'), 'export default function () {}\n');
|
|
24
|
+
const info = (id: string, origin: TaPackageInfo['origin'], dir: string): TaPackageInfo => ({
|
|
25
|
+
id,
|
|
26
|
+
name: id,
|
|
27
|
+
version: '0.2.0',
|
|
28
|
+
scope: 'user',
|
|
29
|
+
origin,
|
|
30
|
+
enabled: true,
|
|
31
|
+
source: dir,
|
|
32
|
+
root: dir,
|
|
33
|
+
resources: ['web'],
|
|
34
|
+
});
|
|
35
|
+
packages = [info('pomodoro', 'directory', root), info('@celljs/ta-channels', 'builtin', join(home, 'builtin'))];
|
|
36
|
+
service = new ExtensionsService({ agentDir, listPackages: async () => packages } as unknown as TaExtensionHost);
|
|
37
|
+
});
|
|
38
|
+
afterEach(() => rmSync(home, { recursive: true, force: true }));
|
|
39
|
+
|
|
40
|
+
it('exports a package as one file, which imports elsewhere as it was', async () => {
|
|
41
|
+
const exported = await service.exportPackage('pomodoro');
|
|
42
|
+
expect(exported.fileName).toBe('pomodoro-0.2.0.tgz');
|
|
43
|
+
const other = new ExtensionsService({ agentDir: join(home, 'other'), listPackages: async () => [] } as unknown as TaExtensionHost);
|
|
44
|
+
expect(await other.importPackage(exported.data)).toEqual({ status: 'imported', name: 'pomodoro', directory: 'pomodoro' });
|
|
45
|
+
expect(readFileSync(join(home, 'other/extensions/pomodoro/web.js'), 'utf8')).toBe('export default function () {}\n');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('asks before replacing a package of the same directory, and leaves nothing half done', async () => {
|
|
49
|
+
const { data } = await service.exportPackage('pomodoro');
|
|
50
|
+
write(join(agentDir, 'extensions/pomodoro/web.js'), 'changed');
|
|
51
|
+
expect(await service.importPackage(data)).toMatchObject({ status: 'exists' });
|
|
52
|
+
expect(readFileSync(join(agentDir, 'extensions/pomodoro/web.js'), 'utf8')).toBe('changed');
|
|
53
|
+
expect(await service.importPackage(data, true)).toMatchObject({ status: 'imported' });
|
|
54
|
+
expect(readFileSync(join(agentDir, 'extensions/pomodoro/web.js'), 'utf8')).toBe('export default function () {}\n');
|
|
55
|
+
expect(readdirSync(join(agentDir, 'extensions'))).toEqual(['pomodoro']);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('does not export what comes with Ta, nor what is not there', async () => {
|
|
59
|
+
await expect(service.exportPackage('@celljs/ta-channels')).rejects.toThrow('comes with Ta');
|
|
60
|
+
await expect(service.exportPackage('nope')).rejects.toThrow('No package has the id nope');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('refuses what is not an archive of a package', async () => {
|
|
64
|
+
await expect(service.importPackage(Buffer.from('hello').toString('base64'))).rejects.toThrow('not a .tgz');
|
|
65
|
+
expect(existsSync(join(agentDir, 'extensions/hello'))).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('names the directory after the package, without its scope', () => {
|
|
69
|
+
expect(PackageFiles.directoryName('@you/Pomodoro Timer')).toBe('pomodoro-timer');
|
|
70
|
+
expect(PackageFiles.directoryName('..')).toBe('extension');
|
|
71
|
+
});
|
|
72
|
+
});
|