@omnixal/openclaw-nats-plugin 0.2.14 → 0.2.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnixal/openclaw-nats-plugin",
3
- "version": "0.2.14",
3
+ "version": "0.2.15",
4
4
  "description": "NATS JetStream event-driven plugin for OpenClaw",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -8,7 +8,14 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
8
8
  const ROUTE_PREFIX = '/nats-dashboard';
9
9
  const SIDECAR_URL = process.env.NATS_SIDECAR_URL || 'http://127.0.0.1:3104';
10
10
  const API_KEY = process.env.NATS_PLUGIN_API_KEY || 'dev-nats-plugin-key';
11
- const sidecarParsed = new URL(SIDECAR_URL);
11
+
12
+ let sidecarParsed: URL;
13
+ try {
14
+ sidecarParsed = new URL(SIDECAR_URL);
15
+ } catch (e) {
16
+ console.error(`[nats-dashboard] Invalid NATS_SIDECAR_URL: ${SIDECAR_URL}`, e);
17
+ sidecarParsed = new URL('http://127.0.0.1:3104');
18
+ }
12
19
 
13
20
  // Stable location (copied during setup) takes priority over in-package dist
14
21
  const STABLE_DIST = path.join(homedir(), '.openclaw', 'nats-plugin', 'dashboard');
@@ -28,10 +35,40 @@ const MIME_TYPES: Record<string, string> = {
28
35
 
29
36
  export function createDashboardHandler() {
30
37
  return async (req: IncomingMessage, res: ServerResponse): Promise<boolean> => {
31
- const url = new URL(req.url!, `http://${req.headers.host}`);
32
- const subPath = url.pathname.slice(ROUTE_PREFIX.length);
38
+ const rawUrl = req.url || '/';
39
+ const url = new URL(rawUrl, `http://${req.headers.host || 'localhost'}`);
40
+
41
+ // Support both prefixed and stripped paths (OpenClaw may strip the prefix)
42
+ let subPath: string;
43
+ if (url.pathname.startsWith(ROUTE_PREFIX)) {
44
+ subPath = url.pathname.slice(ROUTE_PREFIX.length);
45
+ } else {
46
+ subPath = url.pathname;
47
+ }
48
+
49
+ // Debug endpoint: /nats-dashboard/api/_debug (or /api/_debug if prefix stripped)
50
+ if (subPath === '/api/_debug') {
51
+ res.statusCode = 200;
52
+ res.setHeader('content-type', 'application/json');
53
+ res.end(JSON.stringify({
54
+ rawUrl,
55
+ pathname: url.pathname,
56
+ subPath,
57
+ sidecarUrl: SIDECAR_URL,
58
+ sidecarHost: sidecarParsed.hostname,
59
+ sidecarPort: sidecarParsed.port,
60
+ apiKey: API_KEY ? `${API_KEY.slice(0, 4)}...` : '(not set)',
61
+ distDir: DIST_DIR,
62
+ distExists: existsSync(path.join(DIST_DIR, 'index.html')),
63
+ env: {
64
+ NATS_SIDECAR_URL: process.env.NATS_SIDECAR_URL || '(default)',
65
+ NATS_PLUGIN_API_KEY: process.env.NATS_PLUGIN_API_KEY ? 'set' : '(default)',
66
+ },
67
+ }, null, 2));
68
+ return true;
69
+ }
33
70
 
34
- // API proxy: /nats-dashboard/api/* → sidecar
71
+ // API proxy: /api/* → sidecar
35
72
  if (subPath.startsWith('/api/')) {
36
73
  return proxyToSidecar(subPath, url.search, req, res);
37
74
  }
@@ -81,10 +118,18 @@ async function proxyToSidecar(
81
118
  res.end(upstream.body);
82
119
  } catch (err) {
83
120
  const message = err instanceof Error ? err.message : String(err);
84
- console.error(`[nats-dashboard] Sidecar proxy error: ${message} (url=${SIDECAR_URL}${subPath})`);
121
+ const stack = err instanceof Error ? err.stack : undefined;
122
+ console.error(`[nats-dashboard] Sidecar proxy error: ${message} (target=${sidecarParsed.hostname}:${sidecarParsed.port}${subPath})`);
123
+ if (stack) console.error(stack);
85
124
  res.statusCode = 502;
86
125
  res.setHeader('content-type', 'application/json');
87
- res.end(JSON.stringify({ error: 'Sidecar unreachable', detail: message }));
126
+ res.end(JSON.stringify({
127
+ error: 'Sidecar unreachable',
128
+ detail: message,
129
+ target: `${sidecarParsed.hostname}:${sidecarParsed.port}${subPath}`,
130
+ sidecarUrl: SIDECAR_URL,
131
+ hint: 'Open /nats-dashboard/api/_debug for full diagnostics',
132
+ }));
88
133
  }
89
134
  return true;
90
135
  }