@askjo/camofox-browser 1.5.2 → 1.7.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.
Files changed (43) hide show
  1. package/Dockerfile +17 -2
  2. package/README.md +138 -8
  3. package/camofox.config.json +18 -0
  4. package/lib/auth.js +71 -0
  5. package/lib/config.js +27 -1
  6. package/lib/cookies.js +38 -1
  7. package/lib/downloads.js +10 -2
  8. package/lib/extract.js +74 -0
  9. package/lib/inflight.js +16 -0
  10. package/lib/metrics.js +29 -0
  11. package/lib/openapi.js +100 -0
  12. package/lib/persistence.js +89 -0
  13. package/lib/plugins.js +175 -0
  14. package/lib/reporter.js +751 -0
  15. package/lib/tmp-cleanup.js +40 -0
  16. package/lib/tracing.js +137 -0
  17. package/openclaw.plugin.json +1 -1
  18. package/package.json +8 -2
  19. package/plugins/persistence/AGENTS.md +37 -0
  20. package/plugins/persistence/README.md +48 -0
  21. package/plugins/persistence/index.js +124 -0
  22. package/plugins/persistence/persistence.test.js +117 -0
  23. package/plugins/persistence/plugin.test.js +98 -0
  24. package/plugins/vnc/AGENTS.md +42 -0
  25. package/plugins/vnc/README.md +165 -0
  26. package/plugins/vnc/apt.txt +7 -0
  27. package/plugins/vnc/index.js +142 -0
  28. package/plugins/vnc/spawn.js +8 -0
  29. package/plugins/vnc/vnc-launcher.js +64 -0
  30. package/plugins/vnc/vnc-watcher.sh +82 -0
  31. package/plugins/vnc/vnc.test.js +204 -0
  32. package/plugins/youtube/AGENTS.md +25 -0
  33. package/plugins/youtube/apt.txt +1 -0
  34. package/plugins/youtube/index.js +206 -0
  35. package/plugins/youtube/post-install.sh +5 -0
  36. package/plugins/youtube/youtube.test.js +41 -0
  37. package/scripts/exec.js +8 -0
  38. package/scripts/generate-openapi.js +24 -0
  39. package/scripts/install-plugin-deps.sh +63 -0
  40. package/scripts/plugin.js +342 -0
  41. package/scripts/plugin.test.js +117 -0
  42. package/server.js +2124 -355
  43. /package/{lib → plugins/youtube}/youtube.js +0 -0
@@ -0,0 +1,342 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * camofox plugin manager — install, remove, and list plugins.
5
+ *
6
+ * Usage:
7
+ * node scripts/plugin.js install <source> Install a plugin from git URL or local path
8
+ * node scripts/plugin.js remove <name> Remove a plugin and its config entry
9
+ * node scripts/plugin.js list List installed plugins and their source
10
+ *
11
+ * Sources:
12
+ * git:github.com/user/repo Git shorthand
13
+ * https://github.com/user/repo Git URL
14
+ * /absolute/path/to/plugin-dir Local directory (copied)
15
+ * ./relative/path/to/plugin-dir Local directory (copied)
16
+ *
17
+ * Plugin name is inferred from the repo/directory name. If the repo root has
18
+ * an index.js with register(), it's used directly. If it has a plugins/ subdir,
19
+ * each subdirectory is installed as a separate plugin.
20
+ *
21
+ * After install, the plugin is added to camofox.config.json plugins[] and
22
+ * npm dependencies are installed if the plugin has a package.json.
23
+ */
24
+
25
+ import fs from 'fs';
26
+ import path from 'path';
27
+ import { execSync } from './exec.js';
28
+ import { fileURLToPath } from 'url';
29
+
30
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
31
+ const ROOT = path.join(__dirname, '..');
32
+ const PLUGINS_DIR = path.join(ROOT, 'plugins');
33
+ const CONFIG_PATH = path.join(ROOT, 'camofox.config.json');
34
+
35
+ // ── Config helpers ──────────────────────────────────────────────────────────
36
+
37
+ function readConfig() {
38
+ try {
39
+ return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
40
+ } catch {
41
+ return { id: 'camofox-browser', name: 'Camofox Browser', version: '0.0.0', plugins: [] };
42
+ }
43
+ }
44
+
45
+ function writeConfig(config) {
46
+ fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n');
47
+ }
48
+
49
+ /**
50
+ * Get the set of enabled plugin names from config.
51
+ * Handles both array format ["youtube"] and object format { "youtube": { "enabled": true } }.
52
+ */
53
+ function getEnabledPlugins(config) {
54
+ if (!config.plugins) return new Set();
55
+ if (Array.isArray(config.plugins)) return new Set(config.plugins);
56
+ if (typeof config.plugins === 'object') {
57
+ const enabled = new Set();
58
+ for (const [name, conf] of Object.entries(config.plugins)) {
59
+ if (conf === false || (typeof conf === 'object' && conf.enabled === false)) continue;
60
+ enabled.add(name);
61
+ }
62
+ return enabled;
63
+ }
64
+ return new Set();
65
+ }
66
+
67
+ function addToConfig(name) {
68
+ const config = readConfig();
69
+ if (Array.isArray(config.plugins)) {
70
+ if (!config.plugins.includes(name)) {
71
+ config.plugins.push(name);
72
+ writeConfig(config);
73
+ }
74
+ } else if (typeof config.plugins === 'object') {
75
+ if (!config.plugins[name] || config.plugins[name].enabled === false) {
76
+ config.plugins[name] = config.plugins[name] || {};
77
+ config.plugins[name].enabled = true;
78
+ writeConfig(config);
79
+ }
80
+ } else {
81
+ config.plugins = [name];
82
+ writeConfig(config);
83
+ }
84
+ }
85
+
86
+ function removeFromConfig(name) {
87
+ const config = readConfig();
88
+ if (Array.isArray(config.plugins)) {
89
+ config.plugins = config.plugins.filter(p => p !== name);
90
+ writeConfig(config);
91
+ } else if (typeof config.plugins === 'object' && config.plugins[name] !== undefined) {
92
+ delete config.plugins[name];
93
+ writeConfig(config);
94
+ }
95
+ }
96
+
97
+ // ── Source parsing ──────────────────────────────────────────────────────────
98
+
99
+ function parseSource(source) {
100
+ // Local path
101
+ if (source.startsWith('/') || source.startsWith('./') || source.startsWith('../')) {
102
+ const resolved = path.resolve(source);
103
+ if (!fs.existsSync(resolved)) {
104
+ fatal(`Local path not found: ${resolved}`);
105
+ }
106
+ if (!fs.statSync(resolved).isDirectory()) {
107
+ fatal(`Source must be a directory: ${resolved}`);
108
+ }
109
+ return { type: 'local', path: resolved, name: path.basename(resolved) };
110
+ }
111
+
112
+ // Git URL — https://, ssh://, git@, git:
113
+ let gitUrl = source;
114
+ if (gitUrl.startsWith('git:')) {
115
+ gitUrl = gitUrl.slice(4);
116
+ // git:github.com/user/repo → https://github.com/user/repo
117
+ if (!gitUrl.startsWith('http') && !gitUrl.startsWith('ssh://') && !gitUrl.startsWith('git@')) {
118
+ gitUrl = `https://${gitUrl}`;
119
+ }
120
+ }
121
+
122
+ // Strip trailing .git
123
+ gitUrl = gitUrl.replace(/\.git$/, '');
124
+
125
+ // Extract name from URL
126
+ const name = gitUrl.split('/').pop().replace(/[^a-zA-Z0-9_-]/g, '');
127
+ if (!name) fatal(`Cannot infer plugin name from: ${source}`);
128
+
129
+ // Re-add .git for clone
130
+ const cloneUrl = gitUrl.endsWith('.git') ? gitUrl : `${gitUrl}.git`;
131
+
132
+ return { type: 'git', url: cloneUrl, name };
133
+ }
134
+
135
+ // ── Install ─────────────────────────────────────────────────────────────────
136
+
137
+ function isPluginDir(dir) {
138
+ const indexPath = path.join(dir, 'index.js');
139
+ if (!fs.existsSync(indexPath)) return false;
140
+ const content = fs.readFileSync(indexPath, 'utf-8');
141
+ return /\bregister\b/.test(content);
142
+ }
143
+
144
+ function installFromLocal(srcDir, name) {
145
+ const destDir = path.join(PLUGINS_DIR, name);
146
+ if (fs.existsSync(destDir)) {
147
+ fatal(`Plugin "${name}" already exists. Remove it first: node scripts/plugin.js remove ${name}`);
148
+ }
149
+ copyDirSync(srcDir, destDir);
150
+ return [name];
151
+ }
152
+
153
+ function installFromGit(url, name) {
154
+ const tmpDir = path.join(ROOT, '.tmp-plugin-clone');
155
+ try {
156
+ if (fs.existsSync(tmpDir)) fs.rmSync(tmpDir, { recursive: true });
157
+
158
+ console.log(`Cloning ${url}...`);
159
+ execSync(`git clone --depth 1 ${url} ${tmpDir}`, { stdio: 'pipe' });
160
+
161
+ // Case 1: Root is a plugin (has index.js with register)
162
+ if (isPluginDir(tmpDir)) {
163
+ return installFromLocal(tmpDir, name);
164
+ }
165
+
166
+ // Case 2: Has plugins/ subdir with plugin directories
167
+ const pluginsSubdir = path.join(tmpDir, 'plugins');
168
+ if (fs.existsSync(pluginsSubdir) && fs.statSync(pluginsSubdir).isDirectory()) {
169
+ const installed = [];
170
+ for (const entry of fs.readdirSync(pluginsSubdir, { withFileTypes: true })) {
171
+ if (!entry.isDirectory()) continue;
172
+ if (entry.name.startsWith('_') || entry.name.startsWith('.')) continue;
173
+ const subDir = path.join(pluginsSubdir, entry.name);
174
+ if (isPluginDir(subDir)) {
175
+ installFromLocal(subDir, entry.name);
176
+ installed.push(entry.name);
177
+ }
178
+ }
179
+ if (installed.length === 0) {
180
+ fatal(`No plugins found in ${url} — expected index.js with register() at root or in plugins/*/`);
181
+ }
182
+ return installed;
183
+ }
184
+
185
+ fatal(`No plugins found in ${url} — expected index.js with register() at root or plugins/*/ subdirs`);
186
+ } finally {
187
+ if (fs.existsSync(tmpDir)) fs.rmSync(tmpDir, { recursive: true });
188
+ }
189
+ }
190
+
191
+ function installPluginDeps(name) {
192
+ const pluginDir = path.join(PLUGINS_DIR, name);
193
+
194
+ // npm install if package.json exists
195
+ const pkgJson = path.join(pluginDir, 'package.json');
196
+ if (fs.existsSync(pkgJson)) {
197
+ console.log(`Installing npm dependencies for ${name}...`);
198
+ execSync('npm install --omit=dev', { cwd: pluginDir, stdio: 'inherit' });
199
+ }
200
+
201
+ // Check for apt.txt / post-install.sh (just warn — can't run apt locally)
202
+ if (fs.existsSync(path.join(pluginDir, 'apt.txt'))) {
203
+ console.log(`⚠ ${name} has apt.txt — system packages need Docker build or manual install`);
204
+ }
205
+ if (fs.existsSync(path.join(pluginDir, 'post-install.sh'))) {
206
+ console.log(`⚠ ${name} has post-install.sh — run it manually or rebuild Docker image`);
207
+ }
208
+ }
209
+
210
+ // ── Remove ──────────────────────────────────────────────────────────────────
211
+
212
+ function removePlugin(name) {
213
+ const pluginDir = path.join(PLUGINS_DIR, name);
214
+ if (!fs.existsSync(pluginDir)) {
215
+ fatal(`Plugin "${name}" not found in plugins/`);
216
+ }
217
+
218
+ fs.rmSync(pluginDir, { recursive: true });
219
+ removeFromConfig(name);
220
+ console.log(`✓ Removed plugin "${name}"`);
221
+ }
222
+
223
+ // ── List ────────────────────────────────────────────────────────────────────
224
+
225
+ function listPlugins() {
226
+ const config = readConfig();
227
+ const configPlugins = getEnabledPlugins(config);
228
+
229
+ if (!fs.existsSync(PLUGINS_DIR)) {
230
+ console.log('No plugins directory.');
231
+ return;
232
+ }
233
+
234
+ const entries = fs.readdirSync(PLUGINS_DIR, { withFileTypes: true });
235
+ const plugins = entries
236
+ .filter(e => e.isDirectory() && !e.name.startsWith('_') && !e.name.startsWith('.'))
237
+ .map(e => e.name);
238
+
239
+ if (plugins.length === 0) {
240
+ console.log('No plugins installed.');
241
+ return;
242
+ }
243
+
244
+ console.log('Installed plugins:\n');
245
+ for (const name of plugins.sort()) {
246
+ const enabled = configPlugins.size === 0 || configPlugins.has(name);
247
+ const status = enabled ? '✓' : '○';
248
+ const hasTest = fs.existsSync(path.join(PLUGINS_DIR, name, `${name}.test.js`))
249
+ || fs.readdirSync(path.join(PLUGINS_DIR, name)).some(f => f.endsWith('.test.js'));
250
+ const hasDeps = fs.existsSync(path.join(PLUGINS_DIR, name, 'apt.txt'))
251
+ || fs.existsSync(path.join(PLUGINS_DIR, name, 'post-install.sh'));
252
+ const hasPkg = fs.existsSync(path.join(PLUGINS_DIR, name, 'package.json'));
253
+
254
+ const flags = [
255
+ hasTest ? 'tests' : null,
256
+ hasDeps ? 'sys-deps' : null,
257
+ hasPkg ? 'npm-deps' : null,
258
+ ].filter(Boolean).join(', ');
259
+
260
+ console.log(` ${status} ${name}${flags ? ` (${flags})` : ''}`);
261
+ }
262
+
263
+ if (configPlugins.size > 0) {
264
+ console.log(`\n${configPlugins.size} plugin(s) enabled in camofox.config.json`);
265
+ } else {
266
+ console.log('\nNo plugins[] in config — all plugins are loaded');
267
+ }
268
+ }
269
+
270
+ // ── Helpers ─────────────────────────────────────────────────────────────────
271
+
272
+ function copyDirSync(src, dest) {
273
+ fs.mkdirSync(dest, { recursive: true });
274
+ for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
275
+ const srcPath = path.join(src, entry.name);
276
+ const destPath = path.join(dest, entry.name);
277
+ // Skip .git, node_modules
278
+ if (entry.name === '.git' || entry.name === 'node_modules') continue;
279
+ if (entry.isDirectory()) {
280
+ copyDirSync(srcPath, destPath);
281
+ } else {
282
+ fs.copyFileSync(srcPath, destPath);
283
+ }
284
+ }
285
+ }
286
+
287
+ function fatal(msg) {
288
+ console.error(`Error: ${msg}`);
289
+ process.exit(1);
290
+ }
291
+
292
+ // ── CLI ─────────────────────────────────────────────────────────────────────
293
+
294
+ const [,, action, ...args] = process.argv;
295
+
296
+ switch (action) {
297
+ case 'install': {
298
+ const source = args[0];
299
+ if (!source) fatal('Usage: plugin install <git-url|local-path>');
300
+
301
+ const parsed = parseSource(source);
302
+ const installed = parsed.type === 'git'
303
+ ? installFromGit(parsed.url, parsed.name)
304
+ : installFromLocal(parsed.path, parsed.name);
305
+
306
+ for (const name of installed) {
307
+ addToConfig(name);
308
+ installPluginDeps(name);
309
+ }
310
+
311
+ console.log(`\n✓ Installed: ${installed.join(', ')}`);
312
+ console.log(' Restart the server to load new plugin(s).');
313
+ break;
314
+ }
315
+
316
+ case 'remove': {
317
+ const name = args[0];
318
+ if (!name) fatal('Usage: plugin remove <name>');
319
+ removePlugin(name);
320
+ break;
321
+ }
322
+
323
+ case 'list':
324
+ case 'ls': {
325
+ listPlugins();
326
+ break;
327
+ }
328
+
329
+ default:
330
+ console.log(`camofox plugin manager
331
+
332
+ Usage:
333
+ node scripts/plugin.js install <source> Install from git URL or local path
334
+ node scripts/plugin.js remove <name> Remove a plugin
335
+ node scripts/plugin.js list List installed plugins
336
+
337
+ Sources:
338
+ git:github.com/user/repo
339
+ https://github.com/user/repo
340
+ ./path/to/local/plugin`);
341
+ if (action) process.exit(1);
342
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Tests for scripts/plugin.js — plugin install, remove, list.
3
+ */
4
+
5
+ import fs from 'fs';
6
+ import path from 'path';
7
+ import { execSync } from './exec.js';
8
+ import { fileURLToPath } from 'url';
9
+
10
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
11
+ const ROOT = path.join(__dirname, '..');
12
+ const SCRIPT = path.join(ROOT, 'scripts', 'plugin.js');
13
+ const PLUGINS_DIR = path.join(ROOT, 'plugins');
14
+ const CONFIG_PATH = path.join(ROOT, 'camofox.config.json');
15
+
16
+ const run = (args) => execSync(`node ${SCRIPT} ${args}`, { cwd: ROOT, encoding: 'utf-8' });
17
+
18
+ // Save/restore config around tests
19
+ let originalConfig;
20
+ beforeAll(() => { originalConfig = fs.readFileSync(CONFIG_PATH, 'utf-8'); });
21
+ afterAll(() => { fs.writeFileSync(CONFIG_PATH, originalConfig); });
22
+
23
+ // Clean up test plugins after each test
24
+ afterEach(() => {
25
+ const testDir = path.join(PLUGINS_DIR, 'test-plugin');
26
+ if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true });
27
+ // Restore config
28
+ fs.writeFileSync(CONFIG_PATH, originalConfig);
29
+ });
30
+
31
+ describe('plugin list', () => {
32
+ test('lists youtube as enabled', () => {
33
+ const out = run('list');
34
+ expect(out).toContain('youtube');
35
+ expect(out).toContain('✓');
36
+ });
37
+ });
38
+
39
+ describe('plugin install (local)', () => {
40
+ const tmpDir = path.join(ROOT, '.tmp-test-plugin');
41
+
42
+ beforeEach(() => {
43
+ fs.mkdirSync(tmpDir, { recursive: true });
44
+ fs.writeFileSync(path.join(tmpDir, 'index.js'),
45
+ 'export function register(app, ctx) { app.get("/test", (req, res) => res.json({})); }');
46
+ });
47
+
48
+ afterEach(() => {
49
+ if (fs.existsSync(tmpDir)) fs.rmSync(tmpDir, { recursive: true });
50
+ const installed = path.join(PLUGINS_DIR, '.tmp-test-plugin');
51
+ if (fs.existsSync(installed)) fs.rmSync(installed, { recursive: true });
52
+ });
53
+
54
+ test('copies plugin dir and updates config', () => {
55
+ const out = run(`install ${tmpDir}`);
56
+ expect(out).toContain('Installed');
57
+
58
+ // Plugin dir exists
59
+ const installed = path.join(PLUGINS_DIR, '.tmp-test-plugin');
60
+ expect(fs.existsSync(installed)).toBe(true);
61
+ expect(fs.existsSync(path.join(installed, 'index.js'))).toBe(true);
62
+
63
+ // Config updated
64
+ const config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
65
+ if (Array.isArray(config.plugins)) {
66
+ expect(config.plugins).toContain('.tmp-test-plugin');
67
+ } else {
68
+ expect(config.plugins['.tmp-test-plugin']).toBeDefined();
69
+ }
70
+ });
71
+
72
+ test('rejects duplicate install', () => {
73
+ run(`install ${tmpDir}`);
74
+ expect(() => run(`install ${tmpDir}`)).toThrow();
75
+ });
76
+ });
77
+
78
+ describe('plugin remove', () => {
79
+ const tmpDir = path.join(ROOT, '.tmp-test-plugin-rm');
80
+
81
+ beforeEach(() => {
82
+ fs.mkdirSync(tmpDir, { recursive: true });
83
+ fs.writeFileSync(path.join(tmpDir, 'index.js'),
84
+ 'export function register(app, ctx) {}');
85
+ run(`install ${tmpDir}`);
86
+ });
87
+
88
+ afterEach(() => {
89
+ if (fs.existsSync(tmpDir)) fs.rmSync(tmpDir, { recursive: true });
90
+ const installed = path.join(PLUGINS_DIR, '.tmp-test-plugin-rm');
91
+ if (fs.existsSync(installed)) fs.rmSync(installed, { recursive: true });
92
+ });
93
+
94
+ test('removes plugin dir and config entry', () => {
95
+ const out = run('remove .tmp-test-plugin-rm');
96
+ expect(out).toContain('Removed');
97
+
98
+ const installed = path.join(PLUGINS_DIR, '.tmp-test-plugin-rm');
99
+ expect(fs.existsSync(installed)).toBe(false);
100
+
101
+ const config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
102
+ expect(config.plugins).not.toContain('.tmp-test-plugin-rm');
103
+ });
104
+
105
+ test('errors on unknown plugin', () => {
106
+ expect(() => run('remove nonexistent-plugin-xyz')).toThrow();
107
+ });
108
+ });
109
+
110
+ describe('plugin help', () => {
111
+ test('shows usage with no args', () => {
112
+ const out = run('');
113
+ expect(out).toContain('Usage');
114
+ expect(out).toContain('install');
115
+ expect(out).toContain('remove');
116
+ });
117
+ });