@siteboon/claude-code-ui 1.24.0 → 1.25.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.
@@ -0,0 +1,303 @@
1
+ import express from 'express';
2
+ import path from 'path';
3
+ import http from 'http';
4
+ import mime from 'mime-types';
5
+ import fs from 'fs';
6
+ import {
7
+ scanPlugins,
8
+ getPluginsConfig,
9
+ getPluginsDir,
10
+ savePluginsConfig,
11
+ getPluginDir,
12
+ resolvePluginAssetPath,
13
+ installPluginFromGit,
14
+ updatePluginFromGit,
15
+ uninstallPlugin,
16
+ } from '../utils/plugin-loader.js';
17
+ import {
18
+ startPluginServer,
19
+ stopPluginServer,
20
+ getPluginPort,
21
+ isPluginRunning,
22
+ } from '../utils/plugin-process-manager.js';
23
+
24
+ const router = express.Router();
25
+
26
+ // GET / — List all installed plugins (includes server running status)
27
+ router.get('/', (req, res) => {
28
+ try {
29
+ const plugins = scanPlugins().map(p => ({
30
+ ...p,
31
+ serverRunning: p.server ? isPluginRunning(p.name) : false,
32
+ }));
33
+ res.json({ plugins });
34
+ } catch (err) {
35
+ res.status(500).json({ error: 'Failed to scan plugins', details: err.message });
36
+ }
37
+ });
38
+
39
+ // GET /:name/manifest — Get single plugin manifest
40
+ router.get('/:name/manifest', (req, res) => {
41
+ try {
42
+ if (!/^[a-zA-Z0-9_-]+$/.test(req.params.name)) {
43
+ return res.status(400).json({ error: 'Invalid plugin name' });
44
+ }
45
+ const plugins = scanPlugins();
46
+ const plugin = plugins.find(p => p.name === req.params.name);
47
+ if (!plugin) {
48
+ return res.status(404).json({ error: 'Plugin not found' });
49
+ }
50
+ res.json(plugin);
51
+ } catch (err) {
52
+ res.status(500).json({ error: 'Failed to read plugin manifest', details: err.message });
53
+ }
54
+ });
55
+
56
+ // GET /:name/assets/* — Serve plugin static files
57
+ router.get('/:name/assets/*', (req, res) => {
58
+ const pluginName = req.params.name;
59
+ if (!/^[a-zA-Z0-9_-]+$/.test(pluginName)) {
60
+ return res.status(400).json({ error: 'Invalid plugin name' });
61
+ }
62
+ const assetPath = req.params[0];
63
+
64
+ if (!assetPath) {
65
+ return res.status(400).json({ error: 'No asset path specified' });
66
+ }
67
+
68
+ const resolvedPath = resolvePluginAssetPath(pluginName, assetPath);
69
+ if (!resolvedPath) {
70
+ return res.status(404).json({ error: 'Asset not found' });
71
+ }
72
+
73
+ try {
74
+ const stat = fs.statSync(resolvedPath);
75
+ if (!stat.isFile()) {
76
+ return res.status(404).json({ error: 'Asset not found' });
77
+ }
78
+ } catch {
79
+ return res.status(404).json({ error: 'Asset not found' });
80
+ }
81
+
82
+ const contentType = mime.lookup(resolvedPath) || 'application/octet-stream';
83
+ res.setHeader('Content-Type', contentType);
84
+ const stream = fs.createReadStream(resolvedPath);
85
+ stream.on('error', () => {
86
+ if (!res.headersSent) {
87
+ res.status(500).json({ error: 'Failed to read asset' });
88
+ } else {
89
+ res.end();
90
+ }
91
+ });
92
+ stream.pipe(res);
93
+ });
94
+
95
+ // PUT /:name/enable — Toggle plugin enabled/disabled (starts/stops server if applicable)
96
+ router.put('/:name/enable', async (req, res) => {
97
+ try {
98
+ const { enabled } = req.body;
99
+ if (typeof enabled !== 'boolean') {
100
+ return res.status(400).json({ error: '"enabled" must be a boolean' });
101
+ }
102
+
103
+ const plugins = scanPlugins();
104
+ const plugin = plugins.find(p => p.name === req.params.name);
105
+ if (!plugin) {
106
+ return res.status(404).json({ error: 'Plugin not found' });
107
+ }
108
+
109
+ const config = getPluginsConfig();
110
+ config[req.params.name] = { ...config[req.params.name], enabled };
111
+ savePluginsConfig(config);
112
+
113
+ // Start or stop the plugin server as needed
114
+ if (plugin.server) {
115
+ if (enabled && !isPluginRunning(plugin.name)) {
116
+ const pluginDir = getPluginDir(plugin.name);
117
+ if (pluginDir) {
118
+ try {
119
+ await startPluginServer(plugin.name, pluginDir, plugin.server);
120
+ } catch (err) {
121
+ console.error(`[Plugins] Failed to start server for "${plugin.name}":`, err.message);
122
+ }
123
+ }
124
+ } else if (!enabled && isPluginRunning(plugin.name)) {
125
+ await stopPluginServer(plugin.name);
126
+ }
127
+ }
128
+
129
+ res.json({ success: true, name: req.params.name, enabled });
130
+ } catch (err) {
131
+ res.status(500).json({ error: 'Failed to update plugin', details: err.message });
132
+ }
133
+ });
134
+
135
+ // POST /install — Install plugin from git URL
136
+ router.post('/install', async (req, res) => {
137
+ try {
138
+ const { url } = req.body;
139
+ if (!url || typeof url !== 'string') {
140
+ return res.status(400).json({ error: '"url" is required and must be a string' });
141
+ }
142
+
143
+ // Basic URL validation
144
+ if (!url.startsWith('https://') && !url.startsWith('git@')) {
145
+ return res.status(400).json({ error: 'URL must start with https:// or git@' });
146
+ }
147
+
148
+ const manifest = await installPluginFromGit(url);
149
+
150
+ // Auto-start the server if the plugin has one (enabled by default)
151
+ if (manifest.server) {
152
+ const pluginDir = getPluginDir(manifest.name);
153
+ if (pluginDir) {
154
+ try {
155
+ await startPluginServer(manifest.name, pluginDir, manifest.server);
156
+ } catch (err) {
157
+ console.error(`[Plugins] Failed to start server for "${manifest.name}":`, err.message);
158
+ }
159
+ }
160
+ }
161
+
162
+ res.json({ success: true, plugin: manifest });
163
+ } catch (err) {
164
+ res.status(400).json({ error: 'Failed to install plugin', details: err.message });
165
+ }
166
+ });
167
+
168
+ // POST /:name/update — Pull latest from git (restarts server if running)
169
+ router.post('/:name/update', async (req, res) => {
170
+ try {
171
+ const pluginName = req.params.name;
172
+
173
+ if (!/^[a-zA-Z0-9_-]+$/.test(pluginName)) {
174
+ return res.status(400).json({ error: 'Invalid plugin name' });
175
+ }
176
+
177
+ const wasRunning = isPluginRunning(pluginName);
178
+ if (wasRunning) {
179
+ await stopPluginServer(pluginName);
180
+ }
181
+
182
+ const manifest = await updatePluginFromGit(pluginName);
183
+
184
+ // Restart server if it was running before the update
185
+ if (wasRunning && manifest.server) {
186
+ const pluginDir = getPluginDir(pluginName);
187
+ if (pluginDir) {
188
+ try {
189
+ await startPluginServer(pluginName, pluginDir, manifest.server);
190
+ } catch (err) {
191
+ console.error(`[Plugins] Failed to restart server for "${pluginName}":`, err.message);
192
+ }
193
+ }
194
+ }
195
+
196
+ res.json({ success: true, plugin: manifest });
197
+ } catch (err) {
198
+ res.status(400).json({ error: 'Failed to update plugin', details: err.message });
199
+ }
200
+ });
201
+
202
+ // ALL /:name/rpc/* — Proxy requests to plugin's server subprocess
203
+ router.all('/:name/rpc/*', async (req, res) => {
204
+ const pluginName = req.params.name;
205
+ const rpcPath = req.params[0] || '';
206
+
207
+ if (!/^[a-zA-Z0-9_-]+$/.test(pluginName)) {
208
+ return res.status(400).json({ error: 'Invalid plugin name' });
209
+ }
210
+
211
+ let port = getPluginPort(pluginName);
212
+ if (!port) {
213
+ // Lazily start the plugin server if it exists and is enabled
214
+ const plugins = scanPlugins();
215
+ const plugin = plugins.find(p => p.name === pluginName);
216
+ if (!plugin || !plugin.server) {
217
+ return res.status(503).json({ error: 'Plugin server is not running' });
218
+ }
219
+ if (!plugin.enabled) {
220
+ return res.status(503).json({ error: 'Plugin is disabled' });
221
+ }
222
+ const pluginDir = path.join(getPluginsDir(), plugin.dirName);
223
+ try {
224
+ port = await startPluginServer(pluginName, pluginDir, plugin.server);
225
+ } catch (err) {
226
+ return res.status(503).json({ error: 'Plugin server failed to start', details: err.message });
227
+ }
228
+ }
229
+
230
+ // Inject configured secrets as headers
231
+ const config = getPluginsConfig();
232
+ const pluginConfig = config[pluginName] || {};
233
+ const secrets = pluginConfig.secrets || {};
234
+
235
+ const headers = {
236
+ 'content-type': req.headers['content-type'] || 'application/json',
237
+ };
238
+
239
+ // Add per-plugin secrets as X-Plugin-Secret-* headers
240
+ for (const [key, value] of Object.entries(secrets)) {
241
+ headers[`x-plugin-secret-${key.toLowerCase()}`] = String(value);
242
+ }
243
+
244
+ // Reconstruct query string
245
+ const qs = req.url.includes('?') ? '?' + req.url.split('?').slice(1).join('?') : '';
246
+
247
+ const options = {
248
+ hostname: '127.0.0.1',
249
+ port,
250
+ path: `/${rpcPath}${qs}`,
251
+ method: req.method,
252
+ headers,
253
+ };
254
+
255
+ const proxyReq = http.request(options, (proxyRes) => {
256
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
257
+ proxyRes.pipe(res);
258
+ });
259
+
260
+ proxyReq.on('error', (err) => {
261
+ if (!res.headersSent) {
262
+ res.status(502).json({ error: 'Plugin server error', details: err.message });
263
+ } else {
264
+ res.end();
265
+ }
266
+ });
267
+
268
+ // Forward body (already parsed by express JSON middleware, so re-stringify).
269
+ // Check content-length to detect whether a body was actually sent, since
270
+ // req.body can be falsy for valid payloads like 0, false, null, or {}.
271
+ const hasBody = req.headers['content-length'] && parseInt(req.headers['content-length'], 10) > 0;
272
+ if (hasBody && req.body !== undefined) {
273
+ const bodyStr = JSON.stringify(req.body);
274
+ proxyReq.setHeader('content-length', Buffer.byteLength(bodyStr));
275
+ proxyReq.write(bodyStr);
276
+ }
277
+
278
+ proxyReq.end();
279
+ });
280
+
281
+ // DELETE /:name — Uninstall plugin (stops server first)
282
+ router.delete('/:name', async (req, res) => {
283
+ try {
284
+ const pluginName = req.params.name;
285
+
286
+ // Validate name format to prevent path traversal
287
+ if (!/^[a-zA-Z0-9_-]+$/.test(pluginName)) {
288
+ return res.status(400).json({ error: 'Invalid plugin name' });
289
+ }
290
+
291
+ // Stop server and wait for the process to fully exit before deleting files
292
+ if (isPluginRunning(pluginName)) {
293
+ await stopPluginServer(pluginName);
294
+ }
295
+
296
+ await uninstallPlugin(pluginName);
297
+ res.json({ success: true, name: pluginName });
298
+ } catch (err) {
299
+ res.status(400).json({ error: 'Failed to uninstall plugin', details: err.message });
300
+ }
301
+ });
302
+
303
+ export default router;
@@ -1,9 +1,9 @@
1
- import matter from 'gray-matter';
2
1
  import { promises as fs } from 'fs';
3
2
  import path from 'path';
4
3
  import { execFile } from 'child_process';
5
4
  import { promisify } from 'util';
6
5
  import { parse as parseShellCommand } from 'shell-quote';
6
+ import { parseFrontmatter } from './frontmatter.js';
7
7
 
8
8
  const execFileAsync = promisify(execFile);
9
9
 
@@ -32,7 +32,7 @@ const BASH_COMMAND_ALLOWLIST = [
32
32
  */
33
33
  export function parseCommand(content) {
34
34
  try {
35
- const parsed = matter(content);
35
+ const parsed = parseFrontmatter(content);
36
36
  return {
37
37
  data: parsed.data || {},
38
38
  content: parsed.content || '',
@@ -0,0 +1,18 @@
1
+ import matter from 'gray-matter';
2
+
3
+ const disabledFrontmatterEngine = () => ({});
4
+
5
+ const frontmatterOptions = {
6
+ language: 'yaml',
7
+ // Disable JS/JSON frontmatter parsing to avoid executable project content.
8
+ // Mirrors Gatsby's mitigation for gray-matter.
9
+ engines: {
10
+ js: disabledFrontmatterEngine,
11
+ javascript: disabledFrontmatterEngine,
12
+ json: disabledFrontmatterEngine
13
+ }
14
+ };
15
+
16
+ export function parseFrontmatter(content) {
17
+ return matter(content, frontmatterOptions);
18
+ }