@candypoets/nipworker 0.91.3 → 0.92.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.
@@ -1,49 +1,49 @@
1
- import * as E from "flatbuffers";
2
- import { WebSocketServer as h, WebSocket as S } from "ws";
3
- import { SocksProxyAgent as v } from "socks-proxy-agent";
4
- import { C as T, W as k, M, G as A, R as F, N, S as C } from "./worker-message.js";
5
- function q(t = {}) {
6
- const n = t.host ?? "127.0.0.1", r = t.port ?? 7777, e = t.path ?? "/", s = t.logger ?? console, o = t.torSocksProxy;
7
- let f;
1
+ import * as A from "flatbuffers";
2
+ import { WebSocketServer as b, WebSocket as m } from "ws";
3
+ import { SocksProxyAgent as R } from "socks-proxy-agent";
4
+ import { C as T, W as M, M as N, G as I, R as W, N as B, S as v } from "./worker-message.js";
5
+ function X(t = {}) {
6
+ const n = t.host ?? "127.0.0.1", r = t.port ?? 7777, e = t.path ?? "/", o = t.logger ?? console, a = t.torSocksProxy;
7
+ let s;
8
8
  try {
9
- f = new h({
9
+ s = new b({
10
10
  host: n,
11
11
  port: r,
12
12
  path: e
13
13
  });
14
14
  } catch (i) {
15
- throw s.error(`[relay-proxy] failed to create WebSocketServer: ${String(i)}`), i;
15
+ throw o.error(`[relay-proxy] failed to create WebSocketServer: ${String(i)}`), i;
16
16
  }
17
- let a = r;
18
- const c = f.address();
19
- return c && typeof c == "object" && (a = c.port), f.on("error", (i) => {
20
- s.error(`[relay-proxy] WebSocketServer error: ${String(i)}`);
21
- }), f.on("connection", (i) => {
17
+ let f = r;
18
+ const c = s.address();
19
+ return c && typeof c == "object" && (f = c.port), s.on("error", (i) => {
20
+ o.error(`[relay-proxy] WebSocketServer error: ${String(i)}`);
21
+ }), s.on("connection", (i) => {
22
22
  const u = {
23
23
  relaySockets: /* @__PURE__ */ new Map(),
24
24
  pendingFrames: /* @__PURE__ */ new Map(),
25
25
  dedupBySubId: /* @__PURE__ */ new Map(),
26
26
  lastSubIdByRelay: /* @__PURE__ */ new Map(),
27
- torSocksProxy: o
27
+ torSocksProxy: a
28
28
  };
29
29
  i.on("message", (y, d) => {
30
30
  if (d) {
31
- const p = $(y);
31
+ const p = k(y);
32
32
  if (!p) return;
33
- x(u, i, p, s);
33
+ E(u, i, p, o);
34
34
  return;
35
35
  }
36
- const l = w(y);
37
- l && b(u, l, s);
36
+ const g = $(y);
37
+ g && O(u, g, o);
38
38
  }), i.on("close", () => {
39
- g(u, s);
39
+ l(u, o);
40
40
  }), i.on("error", () => {
41
- g(u, s);
41
+ l(u, o);
42
42
  });
43
- }), s.info(`[relay-proxy] listening on ws://${n}:${a}${e}`), {
44
- port: a,
43
+ }), o.info(`[relay-proxy] listening on ws://${n}:${f}${e}`), {
44
+ port: f,
45
45
  close: () => new Promise((i, u) => {
46
- f.close((y) => {
46
+ s.close((y) => {
47
47
  if (y) {
48
48
  u(y);
49
49
  return;
@@ -53,11 +53,11 @@ function q(t = {}) {
53
53
  })
54
54
  };
55
55
  }
56
- function X(t) {
57
- const n = t.path ?? "/", r = t.logger ?? console, e = t.torSocksProxy, s = new h({
56
+ function Y(t) {
57
+ const n = t.path ?? "/", r = t.logger ?? console, e = t.torSocksProxy, o = new b({
58
58
  noServer: true
59
- }), o = /* @__PURE__ */ new Map();
60
- return s.on("connection", (a) => {
59
+ }), a = /* @__PURE__ */ new Map();
60
+ return o.on("connection", (f) => {
61
61
  const c = {
62
62
  relaySockets: /* @__PURE__ */ new Map(),
63
63
  pendingFrames: /* @__PURE__ */ new Map(),
@@ -65,124 +65,124 @@ function X(t) {
65
65
  lastSubIdByRelay: /* @__PURE__ */ new Map(),
66
66
  torSocksProxy: e
67
67
  };
68
- o.set(a, c), a.on("message", (i, u) => {
69
- const y = o.get(a);
68
+ a.set(f, c), f.on("message", (i, u) => {
69
+ const y = a.get(f);
70
70
  if (!y) return;
71
71
  if (u) {
72
- const l = $(i);
73
- if (!l) return;
74
- x(y, a, l, r);
72
+ const g = k(i);
73
+ if (!g) return;
74
+ E(y, f, g, r);
75
75
  return;
76
76
  }
77
- const d = w(i);
78
- d && b(y, d, r);
79
- }), a.on("close", () => {
80
- const i = o.get(a);
81
- i && (g(i, r), o.delete(a));
82
- }), a.on("error", () => {
83
- const i = o.get(a);
84
- i && (g(i, r), o.delete(a));
77
+ const d = $(i);
78
+ d && O(y, d, r);
79
+ }), f.on("close", () => {
80
+ const i = a.get(f);
81
+ i && (l(i, r), a.delete(f));
82
+ }), f.on("error", () => {
83
+ const i = a.get(f);
84
+ i && (l(i, r), a.delete(f));
85
85
  });
86
86
  }), {
87
- wss: s,
88
- close: () => new Promise((a, c) => {
89
- o.forEach((i) => g(i, r)), o.clear(), s.close((i) => {
87
+ wss: o,
88
+ close: () => new Promise((f, c) => {
89
+ a.forEach((i) => l(i, r)), a.clear(), o.close((i) => {
90
90
  if (i) {
91
91
  c(i);
92
92
  return;
93
93
  }
94
- a();
94
+ f();
95
95
  });
96
96
  }),
97
- handleUpgrade: (a, c, i) => {
98
- (a.url ?? "").startsWith(n) && s.handleUpgrade(a, c, i, (y) => {
99
- s.emit("connection", y, a);
97
+ handleUpgrade: (f, c, i) => {
98
+ (f.url ?? "").startsWith(n) && o.handleUpgrade(f, c, i, (y) => {
99
+ o.emit("connection", y, f);
100
100
  });
101
101
  }
102
102
  };
103
103
  }
104
- function Y(t) {
105
- const { server: n, path: r = "/", logger: e = console, torSocksProxy: s } = t, o = new h({
104
+ function Z(t) {
105
+ const { server: n, path: r = "/", logger: e = console, torSocksProxy: o } = t, a = new b({
106
106
  server: n,
107
107
  path: r
108
108
  });
109
- return o.on("connection", (f) => {
110
- const a = {
109
+ return a.on("connection", (s) => {
110
+ const f = {
111
111
  relaySockets: /* @__PURE__ */ new Map(),
112
112
  pendingFrames: /* @__PURE__ */ new Map(),
113
113
  dedupBySubId: /* @__PURE__ */ new Map(),
114
114
  lastSubIdByRelay: /* @__PURE__ */ new Map(),
115
- torSocksProxy: s
115
+ torSocksProxy: o
116
116
  };
117
- f.on("message", (c, i) => {
117
+ s.on("message", (c, i) => {
118
118
  if (i) {
119
- const y = $(c);
119
+ const y = k(c);
120
120
  if (!y) return;
121
- x(a, f, y, e);
121
+ E(f, s, y, e);
122
122
  return;
123
123
  }
124
- const u = w(c);
125
- u && b(a, u, e);
126
- }), f.on("close", () => {
127
- g(a, e);
128
- }), f.on("error", () => {
129
- g(a, e);
124
+ const u = $(c);
125
+ u && O(f, u, e);
126
+ }), s.on("close", () => {
127
+ l(f, e);
128
+ }), s.on("error", () => {
129
+ l(f, e);
130
130
  });
131
131
  }), e.info(`[relay-proxy] attached to server at path: ${r}`), {
132
- close: () => new Promise((f, a) => {
133
- o.close((c) => {
132
+ close: () => new Promise((s, f) => {
133
+ a.close((c) => {
134
134
  if (c) {
135
- a(c);
135
+ f(c);
136
136
  return;
137
137
  }
138
- f();
138
+ s();
139
139
  });
140
140
  })
141
141
  };
142
142
  }
143
- function Z(t, n) {
144
- const r = n.path ?? "/ws-proxy", e = n.logger ?? console, s = n.torSocksProxy;
145
- let o = null;
146
- const f = /* @__PURE__ */ new Map(), a = t.listen.bind(t);
143
+ function U(t, n) {
144
+ const r = n.path ?? "/ws-proxy", e = n.logger ?? console, o = n.torSocksProxy;
145
+ let a = null;
146
+ const s = /* @__PURE__ */ new Map(), f = t.listen.bind(t);
147
147
  return t.listen = (...c) => {
148
- const i = a(...c);
149
- return o = new h({
148
+ const i = f(...c);
149
+ return a = new b({
150
150
  server: i,
151
151
  path: r
152
- }), o.on("connection", (u) => {
152
+ }), a.on("connection", (u) => {
153
153
  const y = {
154
154
  relaySockets: /* @__PURE__ */ new Map(),
155
155
  pendingFrames: /* @__PURE__ */ new Map(),
156
156
  dedupBySubId: /* @__PURE__ */ new Map(),
157
157
  lastSubIdByRelay: /* @__PURE__ */ new Map(),
158
- torSocksProxy: s
158
+ torSocksProxy: o
159
159
  };
160
- f.set(u, y), u.on("message", (d, l) => {
161
- const p = f.get(u);
160
+ s.set(u, y), u.on("message", (d, g) => {
161
+ const p = s.get(u);
162
162
  if (!p) return;
163
- if (l) {
164
- const B = $(d);
165
- if (!B) return;
166
- x(p, u, B, e);
163
+ if (g) {
164
+ const x = k(d);
165
+ if (!x) return;
166
+ E(p, u, x, e);
167
167
  return;
168
168
  }
169
- const I = w(d);
170
- I && b(p, I, e);
169
+ const h = $(d);
170
+ h && O(p, h, e);
171
171
  }), u.on("close", () => {
172
- const d = f.get(u);
173
- d && (g(d, e), f.delete(u));
172
+ const d = s.get(u);
173
+ d && (l(d, e), s.delete(u));
174
174
  }), u.on("error", () => {
175
- const d = f.get(u);
176
- d && (g(d, e), f.delete(u));
175
+ const d = s.get(u);
176
+ d && (l(d, e), s.delete(u));
177
177
  });
178
178
  }), e.info(`[relay-proxy] attached to Express server at path: ${r}`), i;
179
179
  }, {
180
180
  close: () => new Promise((c, i) => {
181
- if (!o) {
181
+ if (!a) {
182
182
  c();
183
183
  return;
184
184
  }
185
- f.forEach((u) => g(u, e)), f.clear(), o.close((u) => {
185
+ s.forEach((u) => l(u, e)), s.clear(), a.close((u) => {
186
186
  if (u) {
187
187
  i(u);
188
188
  return;
@@ -192,58 +192,58 @@ function Z(t, n) {
192
192
  })
193
193
  };
194
194
  }
195
- function b(t, n, r) {
195
+ function O(t, n, r) {
196
196
  r.info(`[relay-proxy] handleClientCommand: ${n.slice(0, 200)}`);
197
197
  let e;
198
198
  try {
199
199
  e = JSON.parse(n);
200
- } catch (s) {
201
- r.warn(`[relay-proxy] failed to parse command JSON: ${s}`);
200
+ } catch (o) {
201
+ r.warn(`[relay-proxy] failed to parse command JSON: ${o}`);
202
202
  return;
203
203
  }
204
- if (H(e)) {
204
+ if (z(e)) {
205
205
  r.info(`[relay-proxy] received auth_response for relay: ${e.relay}`);
206
- const s = JSON.stringify(["AUTH", e.event]);
207
- r.info(`[relay-proxy] sending AUTH frame to relay: ${e.relay}`), P(t, e.relay, s, r);
206
+ const o = JSON.stringify(["AUTH", e.event]);
207
+ r.info(`[relay-proxy] sending AUTH frame to relay: ${e.relay}`), C(t, e.relay, o, r);
208
208
  return;
209
209
  }
210
- if (G(e)) {
210
+ if (K(e)) {
211
211
  t.dedupBySubId.delete(e.subscription_id);
212
- for (const [s, o] of t.relaySockets.entries())
213
- o.readyState === S.OPEN && o.send(JSON.stringify(["CLOSE", e.subscription_id])), t.lastSubIdByRelay.set(s, e.subscription_id);
212
+ for (const [o, a] of t.relaySockets.entries())
213
+ a.readyState === m.OPEN && a.send(JSON.stringify(["CLOSE", e.subscription_id])), t.lastSubIdByRelay.set(o, e.subscription_id);
214
214
  }
215
215
  }
216
- function x(t, n, r, e) {
216
+ function E(t, n, r, e) {
217
217
  e.info(`[relay-proxy] handleEnvelope: ${r.relays.length} relays, ${r.frames.length} frames`), e.info(`[relay-proxy] relays: ${r.relays.join(", ")}`);
218
- const s = r.frames.map((o) => ({
219
- frame: o,
220
- state: L(o)
218
+ const o = r.frames.map((a) => ({
219
+ frame: a,
220
+ state: J(a)
221
221
  }));
222
- e.info(`[relay-proxy] tracked ${s.length} frames`);
223
- for (const o of r.relays) {
224
- e.info(`[relay-proxy] processing relay: ${o}`), W(t, n, o, e);
225
- for (const f of s) {
226
- _(t, o, f.state);
227
- const a = f.frame;
228
- e.info(`[relay-proxy] sending frame to ${o}: ${a.slice(0, 100)}`), P(t, o, a, e);
222
+ e.info(`[relay-proxy] tracked ${o.length} frames`);
223
+ for (const a of r.relays) {
224
+ e.info(`[relay-proxy] processing relay: ${a}`), F(t, n, a, e);
225
+ for (const s of o) {
226
+ _(t, a, s.state);
227
+ const f = s.frame;
228
+ e.info(`[relay-proxy] sending frame to ${a}: ${f.slice(0, 100)}`), C(t, a, f, e);
229
229
  }
230
230
  }
231
231
  }
232
- function W(t, n, r, e) {
233
- const s = t.relaySockets.get(r);
234
- if (s && s.readyState !== S.CLOSED) {
232
+ function F(t, n, r, e) {
233
+ const o = t.relaySockets.get(r);
234
+ if (o && o.readyState !== m.CLOSED) {
235
235
  e.info(`[relay-proxy] reusing existing connection to ${r}`);
236
236
  return;
237
237
  }
238
238
  e.info(`[relay-proxy] creating new WebSocket connection to ${r}`);
239
- const o = new URL(r), f = `${o.protocol}//${o.host}`, a = o.hostname.endsWith(".onion"), c = {
239
+ const a = new URL(r), s = `${a.protocol}//${a.host}`, f = a.hostname.endsWith(".onion"), c = {
240
240
  headers: {
241
- Origin: f,
241
+ Origin: s,
242
242
  "User-Agent": "nipworker/0.91.0"
243
243
  }
244
244
  };
245
- a && t.torSocksProxy && (e.info(`[relay-proxy] using Tor SOCKS proxy ${t.torSocksProxy} for ${r}`), c.agent = new v(t.torSocksProxy));
246
- const i = new S(r, c);
245
+ f && t.torSocksProxy && (e.info(`[relay-proxy] using Tor SOCKS proxy ${t.torSocksProxy} for ${r}`), c.agent = new R(t.torSocksProxy));
246
+ const i = new m(r, c);
247
247
  t.relaySockets.set(r, i), t.pendingFrames.set(r, []);
248
248
  const u = Date.now();
249
249
  i.on("open", () => {
@@ -256,23 +256,21 @@ function W(t, n, r, e) {
256
256
  t.pendingFrames.set(r, []);
257
257
  }
258
258
  }), i.on("message", (y) => {
259
- const d = w(y);
260
- if (e.info(`[relay-proxy] received from ${r}: ${d == null ? void 0 : d.slice(0, 100)}`), !d || n.readyState !== S.OPEN) {
261
- e.warn(`[relay-proxy] cannot forward message: raw=${!!d}, clientOpen=${n.readyState === S.OPEN}`);
259
+ const d = $(y);
260
+ if (e.info(`[relay-proxy] received from ${r}: ${d == null ? void 0 : d.slice(0, 100)}`), !d || n.readyState !== m.OPEN) {
261
+ e.warn(`[relay-proxy] cannot forward message: raw=${!!d}, clientOpen=${n.readyState === m.OPEN}`);
262
262
  return;
263
263
  }
264
- const l = t.lastSubIdByRelay.get(r), p = J(t, r, d, l, e);
265
- if (!p) {
266
- e.warn("[relay-proxy] failed to convert relay frame to worker message");
267
- return;
268
- }
269
- e.info(`[relay-proxy] forwarding ${p.length} bytes to client`), n.send(p, { binary: true });
264
+ const g = t.lastSubIdByRelay.get(r), p = V(t, r, d, g, e, (h, x) => {
265
+ e[h](`[relay-proxy] ${r}: ${x}`);
266
+ });
267
+ p && (e.info(`[relay-proxy] forwarding ${p.length} bytes to client`), n.send(p, { binary: true }));
270
268
  }), i.on("close", (y, d) => {
271
269
  var _a, _b;
272
270
  if (e.info(`[relay-proxy] connection closed to ${r} (code: ${y}, reason: ${d})`), t.pendingFrames.has(r) && ((_a = t.pendingFrames.get(r)) == null ? void 0 : _a.length) > 0) {
273
- const l = ((_b = t.pendingFrames.get(r)) == null ? void 0 : _b.length) ?? 0;
274
- if (e.warn(`[relay-proxy] connection to ${r} closed with ${l} pending frames - subscription may fail`), n.readyState === S.OPEN) {
275
- const p = m(
271
+ const g = ((_b = t.pendingFrames.get(r)) == null ? void 0 : _b.length) ?? 0;
272
+ if (e.warn(`[relay-proxy] connection to ${r} closed with ${g} pending frames - subscription may fail`), n.readyState === m.OPEN) {
273
+ const p = S(
276
274
  t.lastSubIdByRelay.get(r) ?? "",
277
275
  r,
278
276
  "failed",
@@ -284,8 +282,8 @@ function W(t, n, r, e) {
284
282
  t.pendingFrames.delete(r);
285
283
  }), i.on("error", (y) => {
286
284
  if (!t.pendingFrames.has(r)) return;
287
- const d = String(y), l = Date.now() - u;
288
- e.warn(`[relay-proxy] relay socket error for ${r} after ${l}ms: ${d}`), d.includes("403") && e.warn(`[relay-proxy] 403 Forbidden from ${r} - relay may require NIP-42 auth or block this connection`), t.pendingFrames.delete(r);
285
+ const d = String(y), g = Date.now() - u;
286
+ e.warn(`[relay-proxy] relay socket error for ${r} after ${g}ms: ${d}`), d.includes("403") && e.warn(`[relay-proxy] 403 Forbidden from ${r} - relay may require NIP-42 auth or block this connection`), t.pendingFrames.delete(r);
289
287
  });
290
288
  }
291
289
  function _(t, n, r) {
@@ -298,147 +296,153 @@ function _(t, n, r) {
298
296
  t.dedupBySubId.delete(r.subId);
299
297
  }
300
298
  }
301
- function L(t) {
302
- const n = R(t);
299
+ function J(t) {
300
+ const n = P(t);
303
301
  if (!n) return null;
304
302
  const r = n[0];
305
303
  if (r !== "REQ" && r !== "CLOSE") return null;
306
304
  const e = typeof n[1] == "string" ? n[1] : null;
307
305
  return e ? { type: r, subId: e } : null;
308
306
  }
309
- function P(t, n, r, e) {
310
- const s = t.relaySockets.get(n);
311
- if (!s) {
307
+ function C(t, n, r, e) {
308
+ const o = t.relaySockets.get(n);
309
+ if (!o) {
312
310
  e.warn(`[relay-proxy] no relay socket for ${n}`);
313
311
  return;
314
312
  }
315
- if (s.readyState === S.OPEN) {
316
- e.info(`[relay-proxy] sending frame to ${n} (OPEN): ${r.slice(0, 100)}`), s.send(r);
313
+ if (o.readyState === m.OPEN) {
314
+ e.info(`[relay-proxy] sending frame to ${n} (OPEN): ${r.slice(0, 100)}`), o.send(r);
317
315
  return;
318
316
  }
319
- if (s.readyState === S.CONNECTING) {
317
+ if (o.readyState === m.CONNECTING) {
320
318
  e.info(`[relay-proxy] queueing frame for ${n} (CONNECTING)`);
321
- const o = t.pendingFrames.get(n) ?? [];
322
- o.push(r), t.pendingFrames.set(n, o);
319
+ const a = t.pendingFrames.get(n) ?? [];
320
+ a.push(r), t.pendingFrames.set(n, a);
323
321
  return;
324
322
  }
325
- e.warn(`[relay-proxy] dropping frame for closed relay socket ${n} (state: ${s.readyState})`);
323
+ e.warn(`[relay-proxy] dropping frame for closed relay socket ${n} (state: ${o.readyState})`);
326
324
  }
327
- function J(t, n, r, e, s) {
328
- const o = R(r);
329
- if (!o || o.length < 1 || typeof o[0] != "string")
330
- return O(e ?? "", n, r);
331
- const f = o[0];
325
+ function V(t, n, r, e, o, a) {
326
+ const s = P(r);
327
+ if (!s)
328
+ return a == null ? void 0 : a("warn", `parse failed: invalid JSON or not an array, raw=${r.slice(0, 200)}`), w(e ?? "", n, r);
329
+ if (s.length < 1 || typeof s[0] != "string")
330
+ return a == null ? void 0 : a("warn", `parse failed: empty frame or missing kind, frame=${JSON.stringify(s).slice(0, 200)}`), w(e ?? "", n, r);
331
+ const f = s[0];
332
332
  if (f === "EVENT") {
333
- const a = typeof o[1] == "string" ? o[1] : "", c = j(o[2]);
334
- if (!a || !c)
335
- return O(a, n, r);
336
- const i = t.dedupBySubId.get(a);
337
- return i && i.has(c.id) ? null : (i && i.add(c.id), V(a, n, c));
333
+ const c = typeof s[1] == "string" ? s[1] : "", i = D(s[2]);
334
+ if (!c)
335
+ return a == null ? void 0 : a("warn", `EVENT missing subscription ID, frame=${JSON.stringify(s).slice(0, 200)}`), w(c, n, r);
336
+ if (!i) {
337
+ const y = H(s[2]);
338
+ return a == null ? void 0 : a("warn", `EVENT has invalid event structure: ${y}, frame=${JSON.stringify(s).slice(0, 300)}`), w(c, n, r);
339
+ }
340
+ const u = t.dedupBySubId.get(c);
341
+ return u && u.has(i.id) ? (a == null ? void 0 : a("info", `EVENT deduplicated: subId=${c}, eventId=${i.id.slice(0, 16)}...`), null) : (u && u.add(i.id), j(c, n, i));
338
342
  }
339
343
  if (f === "NOTICE") {
340
- const a = o[1] === void 0 ? null : String(o[1]);
341
- return m("", n, "NOTICE", a);
344
+ const c = s[1] === void 0 ? null : String(s[1]);
345
+ return S("", n, "NOTICE", c);
342
346
  }
343
347
  if (f === "AUTH") {
344
- const a = o[1] === void 0 ? null : String(o[1]);
345
- s == null ? void 0 : s.info(`[relay-proxy] received AUTH challenge from ${n}: ${a}`);
346
- const c = m(e ?? "", n, "AUTH", a);
347
- return s == null ? void 0 : s.info(`[relay-proxy] built AUTH worker message (${c.length} bytes)`), c;
348
+ const c = s[1] === void 0 ? null : String(s[1]);
349
+ o == null ? void 0 : o.info(`[relay-proxy] received AUTH challenge from ${n}: ${c}`);
350
+ const i = S(e ?? "", n, "AUTH", c);
351
+ return o == null ? void 0 : o.info(`[relay-proxy] built AUTH worker message (${i.length} bytes)`), i;
348
352
  }
349
353
  if (f === "CLOSED") {
350
- const a = typeof o[1] == "string" ? o[1] : "", c = o[2] === void 0 ? null : String(o[2]);
351
- return m(a, n, "CLOSED", c);
354
+ const c = typeof s[1] == "string" ? s[1] : "", i = s[2] === void 0 ? null : String(s[2]);
355
+ return S(c, n, "CLOSED", i);
352
356
  }
353
357
  if (f === "OK") {
354
- const a = typeof o[1] == "string" ? o[1] : "", c = o[2] === void 0 ? "false" : String(o[2]), i = o[3] === void 0 ? null : String(o[3]);
355
- return m(a, n, c, i);
358
+ const c = typeof s[1] == "string" ? s[1] : "", i = s[2] === void 0 ? "false" : String(s[2]), u = s[3] === void 0 ? null : String(s[3]);
359
+ return S(c, n, i, u);
356
360
  }
357
361
  if (f === "EOSE") {
358
- const a = typeof o[1] == "string" ? o[1] : "";
359
- return m(a, n, "EOSE", null);
362
+ const c = typeof s[1] == "string" ? s[1] : "";
363
+ return S(c, n, "EOSE", null);
360
364
  }
361
- return O(e ?? "", n, r);
365
+ return a == null ? void 0 : a("info", `unknown frame kind='${f}', forwarding as raw message`), w(e ?? "", n, r);
362
366
  }
363
- function V(t, n, r) {
364
- const e = new E.Builder(1024), s = t ? e.createString(t) : 0, o = e.createString(n), f = e.createString(r.id), a = e.createString(r.pubkey), c = e.createString(r.content), i = e.createString(r.sig), u = new Array(r.tags.length);
367
+ function j(t, n, r) {
368
+ const e = new A.Builder(1024), o = t ? e.createString(t) : 0, a = e.createString(n), s = e.createString(r.id), f = e.createString(r.pubkey), c = e.createString(r.content), i = e.createString(r.sig), u = new Array(r.tags.length);
365
369
  for (let p = 0; p < r.tags.length; p++)
366
- u[p] = D(e, r.tags[p]);
367
- const y = N.createTagsVector(e, u), d = N.createNostrEvent(
370
+ u[p] = G(e, r.tags[p]);
371
+ const y = B.createTagsVector(e, u), d = B.createNostrEvent(
368
372
  e,
373
+ s,
369
374
  f,
370
- a,
371
375
  r.kind,
372
376
  c,
373
377
  y,
374
378
  r.created_at,
375
379
  i
376
- ), l = k.createWorkerMessage(
380
+ ), g = M.createWorkerMessage(
377
381
  e,
378
- s,
379
382
  o,
380
- M.NostrEvent,
381
- A.NostrEvent,
383
+ a,
384
+ N.NostrEvent,
385
+ I.NostrEvent,
382
386
  d
383
387
  );
384
- return e.finish(l), e.asUint8Array();
388
+ return e.finish(g), e.asUint8Array();
385
389
  }
386
- function m(t, n, r, e) {
387
- const s = new E.Builder(256), o = t ? s.createString(t) : 0, f = s.createString(n), a = s.createString(r), c = e === null ? 0 : s.createString(e), i = T.createConnectionStatus(
390
+ function S(t, n, r, e) {
391
+ const o = new A.Builder(256), a = t ? o.createString(t) : 0, s = o.createString(n), f = o.createString(r), c = e === null ? 0 : o.createString(e), i = T.createConnectionStatus(
392
+ o,
388
393
  s,
389
394
  f,
390
- a,
391
395
  c
392
- ), u = k.createWorkerMessage(
393
- s,
396
+ ), u = M.createWorkerMessage(
394
397
  o,
395
- f,
396
- M.ConnectionStatus,
397
- A.ConnectionStatus,
398
+ a,
399
+ s,
400
+ N.ConnectionStatus,
401
+ I.ConnectionStatus,
398
402
  i
399
403
  );
400
- return s.finish(u), s.asUint8Array();
404
+ return o.finish(u), o.asUint8Array();
401
405
  }
402
- function O(t, n, r) {
403
- const e = new E.Builder(256), s = t ? e.createString(t) : 0, o = e.createString(n), f = e.createString(r), a = F.createRaw(e, f), c = k.createWorkerMessage(
406
+ function w(t, n, r) {
407
+ const e = new A.Builder(256), o = t ? e.createString(t) : 0, a = e.createString(n), s = e.createString(r), f = W.createRaw(e, s), c = M.createWorkerMessage(
404
408
  e,
405
- s,
406
409
  o,
407
- M.Raw,
408
- A.Raw,
409
- a
410
+ a,
411
+ N.Raw,
412
+ I.Raw,
413
+ f
410
414
  );
411
415
  return e.finish(c), e.asUint8Array();
412
416
  }
413
- function j(t) {
417
+ function D(t) {
414
418
  if (!t || typeof t != "object") return null;
415
419
  const n = t;
416
420
  if (typeof n.id != "string" || typeof n.pubkey != "string" || typeof n.kind != "number" || typeof n.content != "string" || typeof n.created_at != "number" || typeof n.sig != "string" || !Array.isArray(n.tags))
417
421
  return null;
418
422
  const r = n.tags;
419
423
  let e = false;
420
- for (const o of r) {
421
- if (!Array.isArray(o) || o.length === 0) {
424
+ for (const a of r) {
425
+ if (!Array.isArray(a) || a.length === 0) {
422
426
  e = true;
423
427
  continue;
424
428
  }
425
- for (const f of o)
426
- if (typeof f != "string") {
429
+ for (const s of a)
430
+ if (typeof s != "string") {
427
431
  e = true;
428
432
  break;
429
433
  }
430
434
  }
431
- let s;
435
+ let o;
432
436
  if (!e)
433
- s = r;
437
+ o = r;
434
438
  else {
435
- s = [];
436
- for (const o of r) {
437
- if (!Array.isArray(o)) continue;
438
- const f = [];
439
- for (const a of o)
440
- typeof a == "string" && f.push(a);
441
- f.length > 0 && s.push(f);
439
+ o = [];
440
+ for (const a of r) {
441
+ if (!Array.isArray(a)) continue;
442
+ const s = [];
443
+ for (const f of a)
444
+ typeof f == "string" && s.push(f);
445
+ s.length > 0 && o.push(s);
442
446
  }
443
447
  }
444
448
  return {
@@ -448,17 +452,43 @@ function j(t) {
448
452
  content: n.content,
449
453
  created_at: n.created_at,
450
454
  sig: n.sig,
451
- tags: s
455
+ tags: o
452
456
  };
453
457
  }
454
- function D(t, n) {
458
+ function H(t) {
459
+ if (!t) return "value is null/undefined";
460
+ if (typeof t != "object") return `expected object, got ${typeof t}`;
461
+ const n = t, r = [
462
+ ["id", "string", typeof n.id],
463
+ ["pubkey", "string", typeof n.pubkey],
464
+ ["kind", "number", typeof n.kind],
465
+ ["content", "string", typeof n.content],
466
+ ["created_at", "number", typeof n.created_at],
467
+ ["sig", "string", typeof n.sig]
468
+ ];
469
+ for (const [e, o, a] of r)
470
+ if (a !== o)
471
+ return `${e} is ${a === "undefined" ? "missing" : `type ${a} (expected ${o})`}`;
472
+ if (!Array.isArray(n.tags))
473
+ return `tags is ${n.tags === void 0 ? "missing" : `type ${typeof n.tags} (expected array)`}`;
474
+ for (let e = 0; e < n.tags.length; e++) {
475
+ const o = n.tags[e];
476
+ if (!Array.isArray(o))
477
+ return `tags[${e}] is not an array`;
478
+ for (let a = 0; a < o.length; a++)
479
+ if (typeof o[a] != "string")
480
+ return `tags[${e}][${a}] is type ${typeof o[a]} (expected string)`;
481
+ }
482
+ return "unknown validation error";
483
+ }
484
+ function G(t, n) {
455
485
  const r = new Array(n.length);
456
- for (let s = 0; s < n.length; s++)
457
- r[s] = t.createString(n[s]);
458
- const e = C.createItemsVector(t, r);
459
- return C.createStringVec(t, e);
486
+ for (let o = 0; o < n.length; o++)
487
+ r[o] = t.createString(n[o]);
488
+ const e = v.createItemsVector(t, r);
489
+ return v.createStringVec(t, e);
460
490
  }
461
- function R(t) {
491
+ function P(t) {
462
492
  try {
463
493
  const n = JSON.parse(t);
464
494
  return Array.isArray(n) ? n : null;
@@ -466,34 +496,34 @@ function R(t) {
466
496
  return null;
467
497
  }
468
498
  }
469
- function $(t, n) {
470
- const r = w(t);
499
+ function k(t, n) {
500
+ const r = $(t);
471
501
  if (!r)
472
502
  return null;
473
503
  try {
474
504
  const e = JSON.parse(r);
475
505
  if (!Array.isArray(e.relays) || !Array.isArray(e.frames))
476
506
  return n == null ? void 0 : n.warn(`[relay-proxy] parseEnvelope: invalid structure (relays: ${Array.isArray(e.relays)}, frames: ${Array.isArray(e.frames)})`), null;
477
- const s = e.relays.filter((f) => typeof f == "string"), o = e.frames.filter((f) => typeof f == "string");
478
- return n == null ? void 0 : n.info(`[relay-proxy] parseEnvelope: ${s.length} relays, ${o.length} frames`), s.length === 0 || o.length === 0 ? (n == null ? void 0 : n.warn("[relay-proxy] parseEnvelope: empty relays or frames after filter"), null) : { relays: s, frames: o };
507
+ const o = e.relays.filter((s) => typeof s == "string"), a = e.frames.filter((s) => typeof s == "string");
508
+ return n == null ? void 0 : n.info(`[relay-proxy] parseEnvelope: ${o.length} relays, ${a.length} frames`), o.length === 0 || a.length === 0 ? (n == null ? void 0 : n.warn("[relay-proxy] parseEnvelope: empty relays or frames after filter"), null) : { relays: o, frames: a };
479
509
  } catch {
480
510
  return null;
481
511
  }
482
512
  }
483
- function w(t) {
513
+ function $(t) {
484
514
  return typeof t == "string" ? t : t instanceof ArrayBuffer ? Buffer.from(t).toString("utf8") : Buffer.isBuffer(t) ? t.toString("utf8") : t instanceof Uint8Array ? Buffer.from(t).toString("utf8") : Array.isArray(t) ? Buffer.concat(t.filter((n) => Buffer.isBuffer(n))).toString("utf8") : "";
485
515
  }
486
- function H(t) {
516
+ function z(t) {
487
517
  if (!t || typeof t != "object") return false;
488
518
  const n = t;
489
519
  return n.type === "auth_response" && typeof n.relay == "string" && n.event !== void 0;
490
520
  }
491
- function G(t) {
521
+ function K(t) {
492
522
  if (!t || typeof t != "object") return false;
493
523
  const n = t;
494
524
  return n.type === "close_sub" && typeof n.subscription_id == "string";
495
525
  }
496
- function g(t, n) {
526
+ function l(t, n) {
497
527
  n == null ? void 0 : n.info("[relay-proxy] closing session"), t.dedupBySubId.clear(), t.lastSubIdByRelay.clear();
498
528
  for (const [r, e] of t.relaySockets.entries())
499
529
  try {
@@ -503,8 +533,8 @@ function g(t, n) {
503
533
  t.relaySockets.clear(), t.pendingFrames.clear();
504
534
  }
505
535
  export {
506
- Y as attachRelayProxyToServer,
507
- Z as createExpressRelayProxyMiddleware,
508
- q as createRelayProxyServer,
509
- X as createRelayProxyWebSocketServer
536
+ Z as attachRelayProxyToServer,
537
+ U as createExpressRelayProxyMiddleware,
538
+ X as createRelayProxyServer,
539
+ Y as createRelayProxyWebSocketServer
510
540
  };