@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.
- package/dist/cache/index.js +12 -12
- package/dist/cache/pkg/cache.d.ts +4 -4
- package/dist/cache/pkg/cache_bg.wasm.d.ts +4 -4
- package/dist/connections/index.js +19 -19
- package/dist/connections/pkg/connections.d.ts +5 -5
- package/dist/connections/pkg/connections_bg.wasm.d.ts +5 -5
- package/dist/connections/proxy.js +1 -1
- package/dist/connections/proxy.js.map +3 -3
- package/dist/connections/types.d.ts +12 -0
- package/dist/connections/types.d.ts.map +1 -1
- package/dist/proxy/relayProxyServer.d.ts +4 -0
- package/dist/proxy/relayProxyServer.d.ts.map +1 -1
- package/dist/relayProxyServer.js +326 -319
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -1
package/dist/relayProxyServer.js
CHANGED
|
@@ -1,503 +1,510 @@
|
|
|
1
1
|
import * as E from "flatbuffers";
|
|
2
|
-
import { WebSocketServer as h, WebSocket as
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
host:
|
|
10
|
-
port:
|
|
9
|
+
f = new h({
|
|
10
|
+
host: n,
|
|
11
|
+
port: r,
|
|
11
12
|
path: e
|
|
12
13
|
});
|
|
13
|
-
} catch (
|
|
14
|
-
throw
|
|
14
|
+
} catch (i) {
|
|
15
|
+
throw s.error(`[relay-proxy] failed to create WebSocketServer: ${String(i)}`), i;
|
|
15
16
|
}
|
|
16
|
-
let a =
|
|
17
|
-
const
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
}),
|
|
21
|
-
const
|
|
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
|
-
|
|
28
|
-
if (
|
|
29
|
-
const
|
|
30
|
-
if (!
|
|
31
|
-
|
|
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
|
|
35
|
-
|
|
36
|
-
}),
|
|
37
|
-
|
|
38
|
-
}),
|
|
39
|
-
|
|
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
|
-
}),
|
|
43
|
+
}), s.info(`[relay-proxy] listening on ws://${n}:${a}${e}`), {
|
|
42
44
|
port: a,
|
|
43
|
-
close: () => new Promise((
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
|
|
45
|
+
close: () => new Promise((i, u) => {
|
|
46
|
+
f.close((y) => {
|
|
47
|
+
if (y) {
|
|
48
|
+
u(y);
|
|
47
49
|
return;
|
|
48
50
|
}
|
|
49
|
-
|
|
51
|
+
i();
|
|
50
52
|
});
|
|
51
53
|
})
|
|
52
54
|
};
|
|
53
55
|
}
|
|
54
|
-
function
|
|
55
|
-
const
|
|
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
|
|
59
|
-
const
|
|
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,
|
|
66
|
-
const
|
|
67
|
-
if (!
|
|
68
|
-
if (
|
|
69
|
-
const
|
|
70
|
-
if (!
|
|
71
|
-
|
|
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
|
|
75
|
-
|
|
77
|
+
const d = w(i);
|
|
78
|
+
d && b(y, d, r);
|
|
76
79
|
}), a.on("close", () => {
|
|
77
|
-
const
|
|
78
|
-
|
|
80
|
+
const i = o.get(a);
|
|
81
|
+
i && (g(i, r), o.delete(a));
|
|
79
82
|
}), a.on("error", () => {
|
|
80
|
-
const
|
|
81
|
-
|
|
83
|
+
const i = o.get(a);
|
|
84
|
+
i && (g(i, r), o.delete(a));
|
|
82
85
|
});
|
|
83
86
|
}), {
|
|
84
|
-
wss:
|
|
85
|
-
close: () => new Promise((a,
|
|
86
|
-
o.forEach((
|
|
87
|
-
if (
|
|
88
|
-
i
|
|
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,
|
|
95
|
-
(a.url ?? "").startsWith(
|
|
96
|
-
|
|
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
|
|
102
|
-
const { server:
|
|
103
|
-
server:
|
|
104
|
-
path:
|
|
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", (
|
|
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
|
-
|
|
114
|
-
if (
|
|
115
|
-
const
|
|
116
|
-
if (!
|
|
117
|
-
|
|
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
|
|
121
|
-
|
|
122
|
-
}),
|
|
123
|
-
|
|
124
|
-
}),
|
|
125
|
-
|
|
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: ${
|
|
128
|
-
close: () => new Promise((
|
|
129
|
-
o.close((
|
|
130
|
-
if (
|
|
131
|
-
a(
|
|
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
|
-
|
|
138
|
+
f();
|
|
135
139
|
});
|
|
136
140
|
})
|
|
137
141
|
};
|
|
138
142
|
}
|
|
139
|
-
function
|
|
140
|
-
const
|
|
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
|
|
143
|
-
return
|
|
144
|
-
const
|
|
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:
|
|
147
|
-
path:
|
|
148
|
-
}), o.on("connection", (
|
|
149
|
-
const
|
|
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
|
-
|
|
156
|
-
const
|
|
157
|
-
if (!
|
|
158
|
-
if (
|
|
159
|
-
const B =
|
|
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
|
-
|
|
166
|
+
x(p, u, B, e);
|
|
162
167
|
return;
|
|
163
168
|
}
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
}),
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
}),
|
|
170
|
-
const
|
|
171
|
-
|
|
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: ${
|
|
178
|
+
}), e.info(`[relay-proxy] attached to Express server at path: ${r}`), i;
|
|
174
179
|
}, {
|
|
175
|
-
close: () => new Promise((
|
|
180
|
+
close: () => new Promise((c, i) => {
|
|
176
181
|
if (!o) {
|
|
177
|
-
|
|
182
|
+
c();
|
|
178
183
|
return;
|
|
179
184
|
}
|
|
180
|
-
|
|
181
|
-
if (
|
|
182
|
-
|
|
185
|
+
f.forEach((u) => g(u, e)), f.clear(), o.close((u) => {
|
|
186
|
+
if (u) {
|
|
187
|
+
i(u);
|
|
183
188
|
return;
|
|
184
189
|
}
|
|
185
|
-
|
|
190
|
+
c();
|
|
186
191
|
});
|
|
187
192
|
})
|
|
188
193
|
};
|
|
189
194
|
}
|
|
190
|
-
function b(n, r
|
|
191
|
-
|
|
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(
|
|
195
|
-
} catch (
|
|
196
|
-
|
|
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 (
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
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 (
|
|
206
|
-
|
|
207
|
-
for (const [
|
|
208
|
-
|
|
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
|
|
212
|
-
e.info(`[relay-proxy] handleEnvelope: ${
|
|
213
|
-
const
|
|
214
|
-
frame:
|
|
215
|
-
state:
|
|
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 ${
|
|
218
|
-
for (const
|
|
219
|
-
e.info(`[relay-proxy] processing relay: ${
|
|
220
|
-
for (const
|
|
221
|
-
|
|
222
|
-
const
|
|
223
|
-
e.info(`[relay-proxy] sending frame to ${
|
|
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
|
|
228
|
-
const
|
|
229
|
-
if (
|
|
230
|
-
e.info(`[relay-proxy] reusing existing connection to ${
|
|
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 ${
|
|
234
|
-
const
|
|
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:
|
|
241
|
+
Origin: f,
|
|
237
242
|
"User-Agent": "nipworker/0.91.0"
|
|
238
243
|
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const
|
|
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 ${
|
|
244
|
-
const
|
|
245
|
-
if (
|
|
246
|
-
e.info(`[relay-proxy] sending ${
|
|
247
|
-
for (const
|
|
248
|
-
i.send(
|
|
249
|
-
|
|
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", (
|
|
252
|
-
const
|
|
253
|
-
if (e.info(`[relay-proxy] received from ${
|
|
254
|
-
e.warn(`[relay-proxy] cannot forward message: raw=${!!
|
|
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
|
|
258
|
-
if (!
|
|
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 ${
|
|
263
|
-
}), i.on("close", (
|
|
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 ${
|
|
266
|
-
const
|
|
267
|
-
if (e.warn(`[relay-proxy] connection to ${
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
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: ${
|
|
279
|
+
`Connection failed: ${y} ${d}`
|
|
273
280
|
);
|
|
274
|
-
|
|
281
|
+
n.send(p, { binary: true });
|
|
275
282
|
}
|
|
276
283
|
}
|
|
277
|
-
|
|
278
|
-
}), i.on("error", (
|
|
279
|
-
if (!
|
|
280
|
-
const
|
|
281
|
-
e.warn(`[relay-proxy] relay socket error for ${
|
|
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
|
|
285
|
-
if (
|
|
286
|
-
if (
|
|
287
|
-
const e =
|
|
288
|
-
|
|
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
|
-
|
|
298
|
+
t.dedupBySubId.delete(r.subId);
|
|
292
299
|
}
|
|
293
300
|
}
|
|
294
|
-
function
|
|
295
|
-
const
|
|
296
|
-
if (!
|
|
297
|
-
const
|
|
298
|
-
if (
|
|
299
|
-
const e = typeof
|
|
300
|
-
return e ? { type:
|
|
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
|
|
303
|
-
const
|
|
304
|
-
if (!
|
|
305
|
-
e.warn(`[relay-proxy] no relay socket for ${
|
|
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 (
|
|
309
|
-
e.info(`[relay-proxy] sending frame to ${
|
|
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 (
|
|
313
|
-
e.info(`[relay-proxy] queueing frame for ${
|
|
314
|
-
const
|
|
315
|
-
|
|
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 ${
|
|
325
|
+
e.warn(`[relay-proxy] dropping frame for closed relay socket ${n} (state: ${s.readyState})`);
|
|
319
326
|
}
|
|
320
|
-
function
|
|
321
|
-
const
|
|
322
|
-
if (!
|
|
323
|
-
return O(e ?? "",
|
|
324
|
-
const
|
|
325
|
-
if (
|
|
326
|
-
const
|
|
327
|
-
if (!
|
|
328
|
-
return O(
|
|
329
|
-
const
|
|
330
|
-
return
|
|
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 (
|
|
333
|
-
const
|
|
334
|
-
return
|
|
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 (
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
const
|
|
340
|
-
return
|
|
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 (
|
|
343
|
-
const
|
|
344
|
-
return
|
|
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 (
|
|
347
|
-
const
|
|
348
|
-
return
|
|
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 (
|
|
351
|
-
const
|
|
352
|
-
return
|
|
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 ?? "",
|
|
361
|
+
return O(e ?? "", n, r);
|
|
355
362
|
}
|
|
356
|
-
function
|
|
357
|
-
const e = new E.Builder(1024),
|
|
358
|
-
for (let
|
|
359
|
-
u[
|
|
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
|
-
|
|
368
|
-
|
|
369
|
-
), l =
|
|
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
|
|
380
|
-
const
|
|
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
|
|
400
|
+
return s.finish(u), s.asUint8Array();
|
|
394
401
|
}
|
|
395
|
-
function O(n, r
|
|
396
|
-
const e = new E.Builder(256),
|
|
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
|
-
|
|
402
|
-
i
|
|
409
|
+
a
|
|
403
410
|
);
|
|
404
|
-
return e.finish(
|
|
411
|
+
return e.finish(c), e.asUint8Array();
|
|
405
412
|
}
|
|
406
|
-
function
|
|
407
|
-
if (!
|
|
408
|
-
const
|
|
409
|
-
if (typeof
|
|
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
|
|
418
|
+
const r = n.tags;
|
|
412
419
|
let e = false;
|
|
413
|
-
for (const
|
|
414
|
-
if (!Array.isArray(
|
|
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
|
|
419
|
-
if (typeof
|
|
425
|
+
for (const f of o)
|
|
426
|
+
if (typeof f != "string") {
|
|
420
427
|
e = true;
|
|
421
428
|
break;
|
|
422
429
|
}
|
|
423
430
|
}
|
|
424
|
-
let
|
|
431
|
+
let s;
|
|
425
432
|
if (!e)
|
|
426
|
-
|
|
433
|
+
s = r;
|
|
427
434
|
else {
|
|
428
|
-
|
|
429
|
-
for (const
|
|
430
|
-
if (!Array.isArray(
|
|
431
|
-
const
|
|
432
|
-
for (const
|
|
433
|
-
typeof
|
|
434
|
-
|
|
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:
|
|
439
|
-
pubkey:
|
|
440
|
-
kind:
|
|
441
|
-
content:
|
|
442
|
-
created_at:
|
|
443
|
-
sig:
|
|
444
|
-
tags:
|
|
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
|
|
448
|
-
const
|
|
449
|
-
for (let
|
|
450
|
-
|
|
451
|
-
const e =
|
|
452
|
-
return
|
|
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(
|
|
461
|
+
function R(t) {
|
|
455
462
|
try {
|
|
456
|
-
const
|
|
457
|
-
return Array.isArray(
|
|
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
|
|
463
|
-
const
|
|
464
|
-
if (!
|
|
469
|
+
function $(t, n) {
|
|
470
|
+
const r = w(t);
|
|
471
|
+
if (!r)
|
|
465
472
|
return null;
|
|
466
473
|
try {
|
|
467
|
-
const e = JSON.parse(
|
|
474
|
+
const e = JSON.parse(r);
|
|
468
475
|
if (!Array.isArray(e.relays) || !Array.isArray(e.frames))
|
|
469
|
-
return
|
|
470
|
-
const
|
|
471
|
-
return
|
|
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(
|
|
477
|
-
return typeof
|
|
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
|
|
480
|
-
if (!
|
|
481
|
-
const
|
|
482
|
-
return
|
|
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
|
|
485
|
-
if (!
|
|
486
|
-
const
|
|
487
|
-
return
|
|
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
|
|
490
|
-
|
|
491
|
-
for (const [
|
|
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
|
-
|
|
500
|
+
n == null ? void 0 : n.info(`[relay-proxy] closing socket to ${r}`), e.close();
|
|
494
501
|
} catch {
|
|
495
502
|
}
|
|
496
|
-
|
|
503
|
+
t.relaySockets.clear(), t.pendingFrames.clear();
|
|
497
504
|
}
|
|
498
505
|
export {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
506
|
+
Y as attachRelayProxyToServer,
|
|
507
|
+
Z as createExpressRelayProxyMiddleware,
|
|
508
|
+
q as createRelayProxyServer,
|
|
509
|
+
X as createRelayProxyWebSocketServer
|
|
503
510
|
};
|