@omnixal/openclaw-nats-plugin 0.2.13 → 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.13",
3
+ "version": "0.2.15",
4
4
  "description": "NATS JetStream event-driven plugin for OpenClaw",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -2,12 +2,21 @@ import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import { existsSync } from 'node:fs';
4
4
  import { homedir } from 'node:os';
5
+ import http from 'node:http';
5
6
  import type { IncomingMessage, ServerResponse } from 'node:http';
6
7
 
7
8
  const ROUTE_PREFIX = '/nats-dashboard';
8
9
  const SIDECAR_URL = process.env.NATS_SIDECAR_URL || 'http://127.0.0.1:3104';
9
10
  const API_KEY = process.env.NATS_PLUGIN_API_KEY || 'dev-nats-plugin-key';
10
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
+ }
19
+
11
20
  // Stable location (copied during setup) takes priority over in-package dist
12
21
  const STABLE_DIST = path.join(homedir(), '.openclaw', 'nats-plugin', 'dashboard');
13
22
  const PACKAGE_DIST = path.resolve(__dirname, '../../dashboard/dist');
@@ -26,10 +35,40 @@ const MIME_TYPES: Record<string, string> = {
26
35
 
27
36
  export function createDashboardHandler() {
28
37
  return async (req: IncomingMessage, res: ServerResponse): Promise<boolean> => {
29
- const url = new URL(req.url!, `http://${req.headers.host}`);
30
- 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
+ }
31
48
 
32
- // API proxy: /nats-dashboard/api/* sidecar
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
+ }
70
+
71
+ // API proxy: /api/* → sidecar
33
72
  if (subPath.startsWith('/api/')) {
34
73
  return proxyToSidecar(subPath, url.search, req, res);
35
74
  }
@@ -52,7 +91,6 @@ async function proxyToSidecar(
52
91
  res: ServerResponse,
53
92
  ): Promise<boolean> {
54
93
  try {
55
- const targetUrl = `${SIDECAR_URL}${subPath}${search}`;
56
94
  const headers: Record<string, string> = {
57
95
  'Authorization': `Bearer ${API_KEY}`,
58
96
  };
@@ -65,27 +103,69 @@ async function proxyToSidecar(
65
103
  body = await readBody(req);
66
104
  }
67
105
 
68
- const upstream = await fetch(targetUrl, {
106
+ // Use node:http directly — global fetch() may be intercepted by gateway SSRF guards
107
+ const upstream = await httpRequest({
108
+ hostname: sidecarParsed.hostname,
109
+ port: Number(sidecarParsed.port),
110
+ path: `${subPath}${search}`,
69
111
  method: req.method || 'GET',
70
112
  headers,
71
- body,
72
- signal: AbortSignal.timeout(10000),
73
- });
74
-
75
- res.statusCode = upstream.status;
76
- res.setHeader('content-type', upstream.headers.get('content-type') || 'application/json');
77
- const responseBody = await upstream.text();
78
- res.end(responseBody);
79
- } catch {
113
+ timeout: 10_000,
114
+ }, body);
115
+
116
+ res.statusCode = upstream.statusCode;
117
+ res.setHeader('content-type', upstream.headers['content-type'] || 'application/json');
118
+ res.end(upstream.body);
119
+ } catch (err) {
120
+ const message = err instanceof Error ? err.message : String(err);
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);
80
124
  res.statusCode = 502;
81
125
  res.setHeader('content-type', 'application/json');
82
- res.end(JSON.stringify({ error: 'Sidecar unreachable' }));
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
+ }));
83
133
  }
84
134
  return true;
85
135
  }
86
136
 
87
137
  const MAX_BODY_BYTES = 1_048_576; // 1MB
88
138
 
139
+ interface HttpResponse {
140
+ statusCode: number;
141
+ headers: Record<string, string | string[] | undefined>;
142
+ body: string;
143
+ }
144
+
145
+ function httpRequest(
146
+ opts: http.RequestOptions,
147
+ body?: string,
148
+ ): Promise<HttpResponse> {
149
+ return new Promise((resolve, reject) => {
150
+ const req = http.request(opts, (res) => {
151
+ const chunks: Buffer[] = [];
152
+ res.on('data', (chunk: Buffer) => chunks.push(chunk));
153
+ res.on('end', () => {
154
+ resolve({
155
+ statusCode: res.statusCode || 500,
156
+ headers: res.headers,
157
+ body: Buffer.concat(chunks).toString(),
158
+ });
159
+ });
160
+ res.on('error', reject);
161
+ });
162
+ req.on('error', reject);
163
+ req.on('timeout', () => { req.destroy(); reject(new Error('Request timeout')); });
164
+ if (body) req.write(body);
165
+ req.end();
166
+ });
167
+ }
168
+
89
169
  function readBody(req: IncomingMessage): Promise<string> {
90
170
  return new Promise((resolve, reject) => {
91
171
  const chunks: Buffer[] = [];