@omnixal/openclaw-nats-plugin 0.2.13 → 0.2.14

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.14",
4
4
  "description": "NATS JetStream event-driven plugin for OpenClaw",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -2,11 +2,13 @@ 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';
11
+ const sidecarParsed = new URL(SIDECAR_URL);
10
12
 
11
13
  // Stable location (copied during setup) takes priority over in-package dist
12
14
  const STABLE_DIST = path.join(homedir(), '.openclaw', 'nats-plugin', 'dashboard');
@@ -52,7 +54,6 @@ async function proxyToSidecar(
52
54
  res: ServerResponse,
53
55
  ): Promise<boolean> {
54
56
  try {
55
- const targetUrl = `${SIDECAR_URL}${subPath}${search}`;
56
57
  const headers: Record<string, string> = {
57
58
  'Authorization': `Bearer ${API_KEY}`,
58
59
  };
@@ -65,27 +66,61 @@ async function proxyToSidecar(
65
66
  body = await readBody(req);
66
67
  }
67
68
 
68
- const upstream = await fetch(targetUrl, {
69
+ // Use node:http directly — global fetch() may be intercepted by gateway SSRF guards
70
+ const upstream = await httpRequest({
71
+ hostname: sidecarParsed.hostname,
72
+ port: Number(sidecarParsed.port),
73
+ path: `${subPath}${search}`,
69
74
  method: req.method || 'GET',
70
75
  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 {
76
+ timeout: 10_000,
77
+ }, body);
78
+
79
+ res.statusCode = upstream.statusCode;
80
+ res.setHeader('content-type', upstream.headers['content-type'] || 'application/json');
81
+ res.end(upstream.body);
82
+ } catch (err) {
83
+ const message = err instanceof Error ? err.message : String(err);
84
+ console.error(`[nats-dashboard] Sidecar proxy error: ${message} (url=${SIDECAR_URL}${subPath})`);
80
85
  res.statusCode = 502;
81
86
  res.setHeader('content-type', 'application/json');
82
- res.end(JSON.stringify({ error: 'Sidecar unreachable' }));
87
+ res.end(JSON.stringify({ error: 'Sidecar unreachable', detail: message }));
83
88
  }
84
89
  return true;
85
90
  }
86
91
 
87
92
  const MAX_BODY_BYTES = 1_048_576; // 1MB
88
93
 
94
+ interface HttpResponse {
95
+ statusCode: number;
96
+ headers: Record<string, string | string[] | undefined>;
97
+ body: string;
98
+ }
99
+
100
+ function httpRequest(
101
+ opts: http.RequestOptions,
102
+ body?: string,
103
+ ): Promise<HttpResponse> {
104
+ return new Promise((resolve, reject) => {
105
+ const req = http.request(opts, (res) => {
106
+ const chunks: Buffer[] = [];
107
+ res.on('data', (chunk: Buffer) => chunks.push(chunk));
108
+ res.on('end', () => {
109
+ resolve({
110
+ statusCode: res.statusCode || 500,
111
+ headers: res.headers,
112
+ body: Buffer.concat(chunks).toString(),
113
+ });
114
+ });
115
+ res.on('error', reject);
116
+ });
117
+ req.on('error', reject);
118
+ req.on('timeout', () => { req.destroy(); reject(new Error('Request timeout')); });
119
+ if (body) req.write(body);
120
+ req.end();
121
+ });
122
+ }
123
+
89
124
  function readBody(req: IncomingMessage): Promise<string> {
90
125
  return new Promise((resolve, reject) => {
91
126
  const chunks: Buffer[] = [];