@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.
- package/dist/cache/index.js +58 -58
- package/dist/cache/pkg/cache.d.ts +8 -8
- package/dist/cache/pkg/cache_bg.wasm.d.ts +8 -8
- package/dist/connections/index.js +115 -103
- package/dist/connections/pkg/connections.d.ts +15 -9
- package/dist/connections/pkg/connections_bg.wasm.d.ts +10 -9
- package/dist/connections/proxy.js +1 -1
- package/dist/connections/proxy.js.map +3 -3
- package/dist/crypto/index.js +46 -46
- package/dist/crypto/pkg/crypto.d.ts +7 -7
- package/dist/crypto/pkg/crypto_bg.wasm.d.ts +7 -7
- package/dist/generated/nostr/fb/publish.d.ts +8 -1
- package/dist/generated/nostr/fb/publish.d.ts.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.js +231 -182
- package/dist/parser/index.js +308 -308
- package/dist/relayProxyServer.js +251 -221
- package/package.json +1 -1
package/dist/relayProxyServer.js
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import { WebSocketServer as
|
|
3
|
-
import { SocksProxyAgent as
|
|
4
|
-
import { C as T, W as
|
|
5
|
-
function
|
|
6
|
-
const n = t.host ?? "127.0.0.1", r = t.port ?? 7777, e = t.path ?? "/",
|
|
7
|
-
let
|
|
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
|
-
|
|
9
|
+
s = new b({
|
|
10
10
|
host: n,
|
|
11
11
|
port: r,
|
|
12
12
|
path: e
|
|
13
13
|
});
|
|
14
14
|
} catch (i) {
|
|
15
|
-
throw
|
|
15
|
+
throw o.error(`[relay-proxy] failed to create WebSocketServer: ${String(i)}`), i;
|
|
16
16
|
}
|
|
17
|
-
let
|
|
18
|
-
const c =
|
|
19
|
-
return c && typeof c == "object" && (
|
|
20
|
-
|
|
21
|
-
}),
|
|
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:
|
|
27
|
+
torSocksProxy: a
|
|
28
28
|
};
|
|
29
29
|
i.on("message", (y, d) => {
|
|
30
30
|
if (d) {
|
|
31
|
-
const p =
|
|
31
|
+
const p = k(y);
|
|
32
32
|
if (!p) return;
|
|
33
|
-
|
|
33
|
+
E(u, i, p, o);
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
|
-
const
|
|
37
|
-
|
|
36
|
+
const g = $(y);
|
|
37
|
+
g && O(u, g, o);
|
|
38
38
|
}), i.on("close", () => {
|
|
39
|
-
|
|
39
|
+
l(u, o);
|
|
40
40
|
}), i.on("error", () => {
|
|
41
|
-
|
|
41
|
+
l(u, o);
|
|
42
42
|
});
|
|
43
|
-
}),
|
|
44
|
-
port:
|
|
43
|
+
}), o.info(`[relay-proxy] listening on ws://${n}:${f}${e}`), {
|
|
44
|
+
port: f,
|
|
45
45
|
close: () => new Promise((i, u) => {
|
|
46
|
-
|
|
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
|
|
57
|
-
const n = t.path ?? "/", r = t.logger ?? console, e = t.torSocksProxy,
|
|
56
|
+
function Y(t) {
|
|
57
|
+
const n = t.path ?? "/", r = t.logger ?? console, e = t.torSocksProxy, o = new b({
|
|
58
58
|
noServer: true
|
|
59
|
-
}),
|
|
60
|
-
return
|
|
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
|
-
|
|
69
|
-
const y =
|
|
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
|
|
73
|
-
if (!
|
|
74
|
-
|
|
72
|
+
const g = k(i);
|
|
73
|
+
if (!g) return;
|
|
74
|
+
E(y, f, g, r);
|
|
75
75
|
return;
|
|
76
76
|
}
|
|
77
|
-
const d =
|
|
78
|
-
d &&
|
|
79
|
-
}),
|
|
80
|
-
const i =
|
|
81
|
-
i && (
|
|
82
|
-
}),
|
|
83
|
-
const i =
|
|
84
|
-
i && (
|
|
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:
|
|
88
|
-
close: () => new Promise((
|
|
89
|
-
|
|
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
|
-
|
|
94
|
+
f();
|
|
95
95
|
});
|
|
96
96
|
}),
|
|
97
|
-
handleUpgrade: (
|
|
98
|
-
(
|
|
99
|
-
|
|
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
|
|
105
|
-
const { server: n, path: r = "/", logger: e = console, torSocksProxy:
|
|
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
|
|
110
|
-
const
|
|
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:
|
|
115
|
+
torSocksProxy: o
|
|
116
116
|
};
|
|
117
|
-
|
|
117
|
+
s.on("message", (c, i) => {
|
|
118
118
|
if (i) {
|
|
119
|
-
const y =
|
|
119
|
+
const y = k(c);
|
|
120
120
|
if (!y) return;
|
|
121
|
-
|
|
121
|
+
E(f, s, y, e);
|
|
122
122
|
return;
|
|
123
123
|
}
|
|
124
|
-
const u =
|
|
125
|
-
u &&
|
|
126
|
-
}),
|
|
127
|
-
|
|
128
|
-
}),
|
|
129
|
-
|
|
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((
|
|
133
|
-
|
|
132
|
+
close: () => new Promise((s, f) => {
|
|
133
|
+
a.close((c) => {
|
|
134
134
|
if (c) {
|
|
135
|
-
|
|
135
|
+
f(c);
|
|
136
136
|
return;
|
|
137
137
|
}
|
|
138
|
-
|
|
138
|
+
s();
|
|
139
139
|
});
|
|
140
140
|
})
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
|
-
function
|
|
144
|
-
const r = n.path ?? "/ws-proxy", e = n.logger ?? console,
|
|
145
|
-
let
|
|
146
|
-
const
|
|
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 =
|
|
149
|
-
return
|
|
148
|
+
const i = f(...c);
|
|
149
|
+
return a = new b({
|
|
150
150
|
server: i,
|
|
151
151
|
path: r
|
|
152
|
-
}),
|
|
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:
|
|
158
|
+
torSocksProxy: o
|
|
159
159
|
};
|
|
160
|
-
|
|
161
|
-
const p =
|
|
160
|
+
s.set(u, y), u.on("message", (d, g) => {
|
|
161
|
+
const p = s.get(u);
|
|
162
162
|
if (!p) return;
|
|
163
|
-
if (
|
|
164
|
-
const
|
|
165
|
-
if (!
|
|
166
|
-
|
|
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
|
|
170
|
-
|
|
169
|
+
const h = $(d);
|
|
170
|
+
h && O(p, h, e);
|
|
171
171
|
}), u.on("close", () => {
|
|
172
|
-
const d =
|
|
173
|
-
d && (
|
|
172
|
+
const d = s.get(u);
|
|
173
|
+
d && (l(d, e), s.delete(u));
|
|
174
174
|
}), u.on("error", () => {
|
|
175
|
-
const d =
|
|
176
|
-
d && (
|
|
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 (!
|
|
181
|
+
if (!a) {
|
|
182
182
|
c();
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
-
|
|
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
|
|
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 (
|
|
201
|
-
r.warn(`[relay-proxy] failed to parse command JSON: ${
|
|
200
|
+
} catch (o) {
|
|
201
|
+
r.warn(`[relay-proxy] failed to parse command JSON: ${o}`);
|
|
202
202
|
return;
|
|
203
203
|
}
|
|
204
|
-
if (
|
|
204
|
+
if (z(e)) {
|
|
205
205
|
r.info(`[relay-proxy] received auth_response for relay: ${e.relay}`);
|
|
206
|
-
const
|
|
207
|
-
r.info(`[relay-proxy] sending AUTH frame to 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);
|
|
208
208
|
return;
|
|
209
209
|
}
|
|
210
|
-
if (
|
|
210
|
+
if (K(e)) {
|
|
211
211
|
t.dedupBySubId.delete(e.subscription_id);
|
|
212
|
-
for (const [
|
|
213
|
-
|
|
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
|
|
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
|
|
219
|
-
frame:
|
|
220
|
-
state:
|
|
218
|
+
const o = r.frames.map((a) => ({
|
|
219
|
+
frame: a,
|
|
220
|
+
state: J(a)
|
|
221
221
|
}));
|
|
222
|
-
e.info(`[relay-proxy] tracked ${
|
|
223
|
-
for (const
|
|
224
|
-
e.info(`[relay-proxy] processing relay: ${
|
|
225
|
-
for (const
|
|
226
|
-
_(t,
|
|
227
|
-
const
|
|
228
|
-
e.info(`[relay-proxy] sending frame to ${
|
|
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
|
|
233
|
-
const
|
|
234
|
-
if (
|
|
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
|
|
239
|
+
const a = new URL(r), s = `${a.protocol}//${a.host}`, f = a.hostname.endsWith(".onion"), c = {
|
|
240
240
|
headers: {
|
|
241
|
-
Origin:
|
|
241
|
+
Origin: s,
|
|
242
242
|
"User-Agent": "nipworker/0.91.0"
|
|
243
243
|
}
|
|
244
244
|
};
|
|
245
|
-
|
|
246
|
-
const i = new
|
|
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 =
|
|
260
|
-
if (e.info(`[relay-proxy] received from ${r}: ${d == null ? void 0 : d.slice(0, 100)}`), !d || n.readyState !==
|
|
261
|
-
e.warn(`[relay-proxy] cannot forward message: raw=${!!d}, clientOpen=${n.readyState ===
|
|
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
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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
|
|
274
|
-
if (e.warn(`[relay-proxy] connection to ${r} closed with ${
|
|
275
|
-
const p =
|
|
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),
|
|
288
|
-
e.warn(`[relay-proxy] relay socket error for ${r} after ${
|
|
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
|
|
302
|
-
const n =
|
|
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
|
|
310
|
-
const
|
|
311
|
-
if (!
|
|
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 (
|
|
316
|
-
e.info(`[relay-proxy] sending frame to ${n} (OPEN): ${r.slice(0, 100)}`),
|
|
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 (
|
|
317
|
+
if (o.readyState === m.CONNECTING) {
|
|
320
318
|
e.info(`[relay-proxy] queueing frame for ${n} (CONNECTING)`);
|
|
321
|
-
const
|
|
322
|
-
|
|
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: ${
|
|
323
|
+
e.warn(`[relay-proxy] dropping frame for closed relay socket ${n} (state: ${o.readyState})`);
|
|
326
324
|
}
|
|
327
|
-
function
|
|
328
|
-
const
|
|
329
|
-
if (!
|
|
330
|
-
return
|
|
331
|
-
|
|
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
|
|
334
|
-
if (!
|
|
335
|
-
return
|
|
336
|
-
|
|
337
|
-
|
|
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
|
|
341
|
-
return
|
|
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
|
|
345
|
-
|
|
346
|
-
const
|
|
347
|
-
return
|
|
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
|
|
351
|
-
return
|
|
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
|
|
355
|
-
return
|
|
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
|
|
359
|
-
return
|
|
362
|
+
const c = typeof s[1] == "string" ? s[1] : "";
|
|
363
|
+
return S(c, n, "EOSE", null);
|
|
360
364
|
}
|
|
361
|
-
return
|
|
365
|
+
return a == null ? void 0 : a("info", `unknown frame kind='${f}', forwarding as raw message`), w(e ?? "", n, r);
|
|
362
366
|
}
|
|
363
|
-
function
|
|
364
|
-
const e = new
|
|
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] =
|
|
367
|
-
const y =
|
|
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
|
-
),
|
|
380
|
+
), g = M.createWorkerMessage(
|
|
377
381
|
e,
|
|
378
|
-
s,
|
|
379
382
|
o,
|
|
380
|
-
|
|
381
|
-
|
|
383
|
+
a,
|
|
384
|
+
N.NostrEvent,
|
|
385
|
+
I.NostrEvent,
|
|
382
386
|
d
|
|
383
387
|
);
|
|
384
|
-
return e.finish(
|
|
388
|
+
return e.finish(g), e.asUint8Array();
|
|
385
389
|
}
|
|
386
|
-
function
|
|
387
|
-
const
|
|
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 =
|
|
393
|
-
s,
|
|
396
|
+
), u = M.createWorkerMessage(
|
|
394
397
|
o,
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
+
a,
|
|
399
|
+
s,
|
|
400
|
+
N.ConnectionStatus,
|
|
401
|
+
I.ConnectionStatus,
|
|
398
402
|
i
|
|
399
403
|
);
|
|
400
|
-
return
|
|
404
|
+
return o.finish(u), o.asUint8Array();
|
|
401
405
|
}
|
|
402
|
-
function
|
|
403
|
-
const e = new
|
|
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
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
+
a,
|
|
411
|
+
N.Raw,
|
|
412
|
+
I.Raw,
|
|
413
|
+
f
|
|
410
414
|
);
|
|
411
415
|
return e.finish(c), e.asUint8Array();
|
|
412
416
|
}
|
|
413
|
-
function
|
|
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
|
|
421
|
-
if (!Array.isArray(
|
|
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
|
|
426
|
-
if (typeof
|
|
429
|
+
for (const s of a)
|
|
430
|
+
if (typeof s != "string") {
|
|
427
431
|
e = true;
|
|
428
432
|
break;
|
|
429
433
|
}
|
|
430
434
|
}
|
|
431
|
-
let
|
|
435
|
+
let o;
|
|
432
436
|
if (!e)
|
|
433
|
-
|
|
437
|
+
o = r;
|
|
434
438
|
else {
|
|
435
|
-
|
|
436
|
-
for (const
|
|
437
|
-
if (!Array.isArray(
|
|
438
|
-
const
|
|
439
|
-
for (const
|
|
440
|
-
typeof
|
|
441
|
-
|
|
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:
|
|
455
|
+
tags: o
|
|
452
456
|
};
|
|
453
457
|
}
|
|
454
|
-
function
|
|
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
|
|
457
|
-
r[
|
|
458
|
-
const e =
|
|
459
|
-
return
|
|
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
|
|
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
|
|
470
|
-
const r =
|
|
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
|
|
478
|
-
return n == null ? void 0 : n.info(`[relay-proxy] parseEnvelope: ${
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
536
|
+
Z as attachRelayProxyToServer,
|
|
537
|
+
U as createExpressRelayProxyMiddleware,
|
|
538
|
+
X as createRelayProxyServer,
|
|
539
|
+
Y as createRelayProxyWebSocketServer
|
|
510
540
|
};
|