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