@candypoets/nipworker 0.91.1 → 0.91.3

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