@candypoets/nipworker 0.94.0 → 0.95.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/NarrowTypes.js +91 -87
- package/dist/cache/index.js +53 -53
- package/dist/cache/pkg/cache.d.ts +2 -2
- package/dist/cache/pkg/cache_bg.wasm.d.ts +2 -2
- package/dist/connections/index.js +13 -13
- 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 +4 -4
- package/dist/crypto/index.js +40 -40
- package/dist/generated/nostr/fb/content-block.d.ts +3 -2
- package/dist/generated/nostr/fb/content-block.d.ts.map +1 -1
- package/dist/generated/nostr/fb/content-data.d.ts +5 -3
- package/dist/generated/nostr/fb/content-data.d.ts.map +1 -1
- package/dist/generated/nostr/fb/emoji-data.d.ts +30 -0
- package/dist/generated/nostr/fb/emoji-data.d.ts.map +1 -0
- package/dist/generated/nostr/fb/kind1018-parsed.d.ts +37 -0
- package/dist/generated/nostr/fb/kind1018-parsed.d.ts.map +1 -0
- package/dist/generated/nostr/fb/kind1068-parsed.d.ts +58 -0
- package/dist/generated/nostr/fb/kind1068-parsed.d.ts.map +1 -0
- package/dist/generated/nostr/fb/parsed-data-union.d.ts +2 -0
- package/dist/generated/nostr/fb/parsed-data-union.d.ts.map +1 -1
- package/dist/generated/nostr/fb/parsed-data.d.ts +17 -13
- package/dist/generated/nostr/fb/parsed-data.d.ts.map +1 -1
- package/dist/generated/nostr/fb/parsed-event.d.ts +4 -2
- package/dist/generated/nostr/fb/parsed-event.d.ts.map +1 -1
- package/dist/generated/nostr/fb/poll-option.d.ts +26 -0
- package/dist/generated/nostr/fb/poll-option.d.ts.map +1 -0
- package/dist/generated/nostr/fb/poll-type.d.ts +5 -0
- package/dist/generated/nostr/fb/poll-type.d.ts.map +1 -0
- package/dist/generated/nostr/fb.d.ts +5 -0
- package/dist/generated/nostr/fb.d.ts.map +1 -1
- package/dist/index.js +159 -150
- package/dist/index2.js +31 -31
- package/dist/lib/ConnectionTracker.d.ts +2 -0
- package/dist/lib/ConnectionTracker.d.ts.map +1 -1
- package/dist/lib/NarrowTypes.d.ts +2 -1
- package/dist/lib/NarrowTypes.d.ts.map +1 -1
- package/dist/parser/index.js +305 -305
- package/dist/relayProxyServer.js +298 -352
- package/dist/utils.js +91 -86
- package/dist/worker-message.js +1148 -759
- package/package.json +1 -1
package/dist/relayProxyServer.js
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import { WebSocketServer as
|
|
3
|
-
import { SocksProxyAgent as
|
|
4
|
-
import { C as
|
|
5
|
-
function X(
|
|
6
|
-
const
|
|
7
|
-
let
|
|
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
|
-
|
|
10
|
-
host:
|
|
9
|
+
f = new w({
|
|
10
|
+
host: t,
|
|
11
11
|
port: r,
|
|
12
|
-
path:
|
|
12
|
+
path: n
|
|
13
13
|
});
|
|
14
14
|
} catch (i) {
|
|
15
|
-
throw
|
|
15
|
+
throw s.error(`[relay-proxy] failed to create WebSocketServer: ${String(i)}`), i;
|
|
16
16
|
}
|
|
17
|
-
let
|
|
18
|
-
const
|
|
19
|
-
return
|
|
20
|
-
|
|
21
|
-
}),
|
|
22
|
-
const
|
|
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:
|
|
27
|
+
torSocksProxy: o
|
|
28
28
|
};
|
|
29
|
-
i.on("message", (
|
|
29
|
+
i.on("message", (g, d) => {
|
|
30
30
|
if (d) {
|
|
31
|
-
const
|
|
32
|
-
if (!
|
|
33
|
-
|
|
31
|
+
const y = x(g);
|
|
32
|
+
if (!y) return;
|
|
33
|
+
M(c, i, y, s);
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
|
-
const
|
|
37
|
-
|
|
36
|
+
const p = b(g);
|
|
37
|
+
p && O(c, p, s);
|
|
38
38
|
}), i.on("close", () => {
|
|
39
|
-
l(
|
|
39
|
+
l(c);
|
|
40
40
|
}), i.on("error", () => {
|
|
41
|
-
l(
|
|
41
|
+
l(c);
|
|
42
42
|
});
|
|
43
|
-
}),
|
|
44
|
-
port:
|
|
45
|
-
close: () => new Promise((i,
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
|
|
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(
|
|
57
|
-
const
|
|
56
|
+
function Y(e) {
|
|
57
|
+
const t = e.path ?? "/", r = e.logger ?? console, n = e.torSocksProxy, s = new w({
|
|
58
58
|
noServer: true
|
|
59
|
-
}),
|
|
60
|
-
return
|
|
61
|
-
const
|
|
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:
|
|
66
|
+
torSocksProxy: n
|
|
67
67
|
};
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
if (!
|
|
71
|
-
if (
|
|
72
|
-
const
|
|
73
|
-
if (!
|
|
74
|
-
|
|
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 =
|
|
78
|
-
d && O(
|
|
79
|
-
}),
|
|
80
|
-
const i =
|
|
81
|
-
i && (l(i
|
|
82
|
-
}),
|
|
83
|
-
const i =
|
|
84
|
-
i && (l(i
|
|
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:
|
|
88
|
-
close: () => new Promise((
|
|
89
|
-
|
|
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
|
-
|
|
91
|
+
u(i);
|
|
92
92
|
return;
|
|
93
93
|
}
|
|
94
|
-
|
|
94
|
+
a();
|
|
95
95
|
});
|
|
96
96
|
}),
|
|
97
|
-
handleUpgrade: (
|
|
98
|
-
(
|
|
99
|
-
|
|
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(
|
|
105
|
-
const { server:
|
|
106
|
-
server:
|
|
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
|
|
110
|
-
const
|
|
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:
|
|
115
|
+
torSocksProxy: s
|
|
116
116
|
};
|
|
117
|
-
|
|
117
|
+
f.on("message", (u, i) => {
|
|
118
118
|
if (i) {
|
|
119
|
-
const
|
|
120
|
-
if (!
|
|
121
|
-
|
|
119
|
+
const g = x(u);
|
|
120
|
+
if (!g) return;
|
|
121
|
+
M(a, f, g, n);
|
|
122
122
|
return;
|
|
123
123
|
}
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
}),
|
|
127
|
-
l(
|
|
128
|
-
}),
|
|
129
|
-
l(
|
|
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
|
-
}),
|
|
132
|
-
close: () => new Promise((
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
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
|
-
|
|
138
|
+
f();
|
|
139
139
|
});
|
|
140
140
|
})
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
|
-
function
|
|
144
|
-
const r =
|
|
145
|
-
let
|
|
146
|
-
const
|
|
147
|
-
return
|
|
148
|
-
const i =
|
|
149
|
-
return
|
|
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
|
-
}),
|
|
153
|
-
const
|
|
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:
|
|
158
|
+
torSocksProxy: s
|
|
159
159
|
};
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
if (!
|
|
163
|
-
if (
|
|
164
|
-
const
|
|
165
|
-
if (!
|
|
166
|
-
|
|
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
|
|
170
|
-
|
|
171
|
-
}),
|
|
172
|
-
const d =
|
|
173
|
-
d && (l(d
|
|
174
|
-
}),
|
|
175
|
-
const d =
|
|
176
|
-
d && (l(d
|
|
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
|
-
}),
|
|
178
|
+
}), n.info(`[relay-proxy] attached to Express server at path: ${r}`), i;
|
|
179
179
|
}, {
|
|
180
|
-
close: () => new Promise((
|
|
181
|
-
if (!
|
|
182
|
-
|
|
180
|
+
close: () => new Promise((u, i) => {
|
|
181
|
+
if (!o) {
|
|
182
|
+
u();
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
-
|
|
186
|
-
if (
|
|
187
|
-
i(
|
|
185
|
+
f.forEach((c) => l(c)), f.clear(), o.close((c) => {
|
|
186
|
+
if (c) {
|
|
187
|
+
i(c);
|
|
188
188
|
return;
|
|
189
189
|
}
|
|
190
|
-
|
|
190
|
+
u();
|
|
191
191
|
});
|
|
192
192
|
})
|
|
193
193
|
};
|
|
194
194
|
}
|
|
195
|
-
function O(
|
|
196
|
-
|
|
197
|
-
let e;
|
|
195
|
+
function O(e, t, r) {
|
|
196
|
+
let n;
|
|
198
197
|
try {
|
|
199
|
-
|
|
200
|
-
} catch (
|
|
201
|
-
r.warn(`[relay-proxy] failed to parse command JSON: ${
|
|
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(
|
|
205
|
-
|
|
206
|
-
|
|
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(
|
|
211
|
-
|
|
212
|
-
for (const [
|
|
213
|
-
|
|
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
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
|
233
|
-
const
|
|
234
|
-
if (
|
|
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:
|
|
231
|
+
Origin: f,
|
|
242
232
|
"User-Agent": "nipworker/0.91.0"
|
|
243
233
|
}
|
|
244
234
|
};
|
|
245
|
-
|
|
246
|
-
const i = new
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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", (
|
|
259
|
-
const
|
|
260
|
-
if (
|
|
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
|
-
|
|
265
|
-
|
|
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.
|
|
271
|
-
const
|
|
272
|
-
if (
|
|
273
|
-
const p =
|
|
274
|
-
|
|
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: ${
|
|
259
|
+
`Connection failed: ${c} ${g}`
|
|
278
260
|
);
|
|
279
|
-
|
|
261
|
+
t.send(p, { binary: true });
|
|
280
262
|
}
|
|
281
263
|
}
|
|
282
|
-
|
|
283
|
-
}), i.on("error", (
|
|
284
|
-
if (!
|
|
285
|
-
|
|
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 _(
|
|
270
|
+
function _(e, t, r) {
|
|
290
271
|
if (r) {
|
|
291
|
-
if (
|
|
292
|
-
const
|
|
293
|
-
|
|
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
|
-
|
|
277
|
+
e.dedupBySubId.delete(r.subId);
|
|
297
278
|
}
|
|
298
279
|
}
|
|
299
|
-
function J(
|
|
300
|
-
const
|
|
301
|
-
if (!
|
|
302
|
-
const r =
|
|
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
|
|
305
|
-
return
|
|
285
|
+
const n = typeof t[1] == "string" ? t[1] : null;
|
|
286
|
+
return n ? { type: r, subId: n } : null;
|
|
306
287
|
}
|
|
307
|
-
function C(
|
|
308
|
-
const
|
|
309
|
-
if (!
|
|
310
|
-
|
|
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 (
|
|
314
|
-
|
|
294
|
+
if (s.readyState === S.OPEN) {
|
|
295
|
+
s.send(r);
|
|
315
296
|
return;
|
|
316
297
|
}
|
|
317
|
-
if (
|
|
318
|
-
e.
|
|
319
|
-
|
|
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
|
-
|
|
303
|
+
n.warn(`[relay-proxy] dropping frame for closed relay socket ${t} (state: ${s.readyState})`);
|
|
324
304
|
}
|
|
325
|
-
function
|
|
326
|
-
const
|
|
327
|
-
if (!
|
|
328
|
-
return
|
|
329
|
-
|
|
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
|
|
334
|
-
if (!
|
|
335
|
-
return a
|
|
336
|
-
|
|
337
|
-
|
|
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
|
|
345
|
-
return
|
|
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
|
|
349
|
-
|
|
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
|
|
355
|
-
return
|
|
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
|
|
359
|
-
return
|
|
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
|
|
363
|
-
return
|
|
334
|
+
const a = typeof o[1] == "string" ? o[1] : "";
|
|
335
|
+
return m(a, t, "EOSE", null);
|
|
364
336
|
}
|
|
365
|
-
return
|
|
337
|
+
return h(n ?? "", t, r);
|
|
366
338
|
}
|
|
367
|
-
function
|
|
368
|
-
const
|
|
369
|
-
for (let
|
|
370
|
-
|
|
371
|
-
const
|
|
372
|
-
|
|
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
|
-
|
|
377
|
-
|
|
348
|
+
u,
|
|
349
|
+
g,
|
|
378
350
|
r.created_at,
|
|
379
351
|
i
|
|
380
|
-
),
|
|
381
|
-
|
|
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
|
|
360
|
+
return n.finish(p), n.asUint8Array();
|
|
389
361
|
}
|
|
390
|
-
function
|
|
391
|
-
const
|
|
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
|
-
|
|
370
|
+
o,
|
|
371
|
+
f,
|
|
401
372
|
I.ConnectionStatus,
|
|
373
|
+
B.ConnectionStatus,
|
|
402
374
|
i
|
|
403
375
|
);
|
|
404
|
-
return
|
|
376
|
+
return s.finish(c), s.asUint8Array();
|
|
405
377
|
}
|
|
406
|
-
function
|
|
407
|
-
const
|
|
408
|
-
|
|
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
|
-
|
|
384
|
+
B.Raw,
|
|
385
|
+
a
|
|
414
386
|
);
|
|
415
|
-
return
|
|
387
|
+
return n.finish(u), n.asUint8Array();
|
|
416
388
|
}
|
|
417
|
-
function
|
|
418
|
-
if (!
|
|
419
|
-
const
|
|
420
|
-
if (typeof
|
|
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 =
|
|
423
|
-
let
|
|
424
|
-
for (const
|
|
425
|
-
if (!Array.isArray(
|
|
426
|
-
|
|
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
|
|
430
|
-
if (typeof
|
|
431
|
-
|
|
401
|
+
for (const f of o)
|
|
402
|
+
if (typeof f != "string") {
|
|
403
|
+
n = true;
|
|
432
404
|
break;
|
|
433
405
|
}
|
|
434
406
|
}
|
|
435
|
-
let
|
|
436
|
-
if (!
|
|
437
|
-
|
|
407
|
+
let s;
|
|
408
|
+
if (!n)
|
|
409
|
+
s = r;
|
|
438
410
|
else {
|
|
439
|
-
|
|
440
|
-
for (const
|
|
441
|
-
if (!Array.isArray(
|
|
442
|
-
const
|
|
443
|
-
for (const
|
|
444
|
-
typeof
|
|
445
|
-
|
|
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:
|
|
450
|
-
pubkey:
|
|
451
|
-
kind:
|
|
452
|
-
content:
|
|
453
|
-
created_at:
|
|
454
|
-
sig:
|
|
455
|
-
tags:
|
|
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
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
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
|
|
437
|
+
function $(e) {
|
|
492
438
|
try {
|
|
493
|
-
const
|
|
494
|
-
return Array.isArray(
|
|
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
|
|
500
|
-
const r =
|
|
445
|
+
function x(e, t) {
|
|
446
|
+
const r = b(e);
|
|
501
447
|
if (!r)
|
|
502
448
|
return null;
|
|
503
449
|
try {
|
|
504
|
-
const
|
|
505
|
-
if (!Array.isArray(
|
|
506
|
-
return
|
|
507
|
-
const
|
|
508
|
-
return
|
|
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
|
|
514
|
-
return typeof
|
|
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(
|
|
517
|
-
if (!
|
|
518
|
-
const
|
|
519
|
-
return
|
|
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(
|
|
522
|
-
if (!
|
|
523
|
-
const
|
|
524
|
-
return
|
|
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(
|
|
527
|
-
|
|
528
|
-
for (const [r,
|
|
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
|
|
476
|
+
n.close();
|
|
531
477
|
} catch {
|
|
532
478
|
}
|
|
533
|
-
|
|
479
|
+
e.relaySockets.clear(), e.pendingFrames.clear();
|
|
534
480
|
}
|
|
535
481
|
export {
|
|
536
482
|
Z as attachRelayProxyToServer,
|
|
537
|
-
|
|
483
|
+
q as createExpressRelayProxyMiddleware,
|
|
538
484
|
X as createRelayProxyServer,
|
|
539
485
|
Y as createRelayProxyWebSocketServer
|
|
540
486
|
};
|