chirli 0.1.0 → 0.2.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/ARCHITECTURE.md +18 -20
- package/CONTRIBUTING.md +7 -3
- package/README.md +63 -55
- package/SECURITY.md +11 -11
- package/dist/allowlist.d.ts +3 -0
- package/dist/allowlist.js +9 -0
- package/dist/announcements.d.ts +4 -4
- package/dist/announcements.js +66 -34
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.js +340 -0
- package/dist/blossom.d.ts +2 -2
- package/dist/blossom.js +120 -66
- package/dist/bot-handlers.d.ts +9 -18
- package/dist/bot-handlers.js +125 -66
- package/dist/bot.d.ts +17 -20
- package/dist/bot.js +150 -55
- package/dist/client.d.ts +43 -39
- package/dist/client.js +476 -290
- package/dist/command-discovery.d.ts +2 -2
- package/dist/command-discovery.js +16 -7
- package/dist/command-schema.d.ts +2 -2
- package/dist/command-schema.js +62 -31
- package/dist/commands.d.ts +7 -6
- package/dist/commands.js +116 -51
- package/dist/defaults.js +13 -6
- package/dist/errors.d.ts +12 -12
- package/dist/errors.js +55 -18
- package/dist/inbox.d.ts +9 -7
- package/dist/inbox.js +107 -62
- package/dist/index.d.ts +25 -24
- package/dist/index.js +11 -11
- package/dist/key-sync.d.ts +4 -4
- package/dist/key-sync.js +88 -50
- package/dist/keys.d.ts +7 -6
- package/dist/keys.js +187 -93
- package/dist/message-storage-sqlite.d.ts +1 -1
- package/dist/message-storage-sqlite.js +32 -27
- package/dist/message-storage.d.ts +2 -2
- package/dist/messaging-metadata.d.ts +4 -4
- package/dist/messaging-metadata.js +73 -46
- package/dist/metadata.d.ts +3 -3
- package/dist/metadata.js +29 -15
- package/dist/modes.d.ts +7 -7
- package/dist/modes.js +53 -24
- package/dist/nip4e-compat.d.ts +2 -2
- package/dist/nip4e-compat.js +2 -2
- package/dist/nipcd.d.ts +14 -14
- package/dist/nipcd.js +225 -126
- package/dist/protocol.d.ts +7 -6
- package/dist/protocol.js +101 -54
- package/dist/public-inbox.d.ts +17 -15
- package/dist/public-inbox.js +129 -138
- package/dist/public-thread.d.ts +7 -4
- package/dist/public-thread.js +205 -88
- package/dist/relay-pool.d.ts +5 -5
- package/dist/relay-pool.js +172 -67
- package/dist/relay-roles.js +22 -13
- package/dist/relay.d.ts +11 -11
- package/dist/relay.js +194 -100
- package/dist/request-timing.d.ts +2 -2
- package/dist/request-timing.js +19 -7
- package/dist/session.js +7 -5
- package/dist/signer.d.ts +1 -1
- package/dist/signers/bunker-relays.d.ts +2 -2
- package/dist/signers/bunker-relays.js +16 -13
- package/dist/signers/bunker.d.ts +2 -2
- package/dist/signers/bunker.js +57 -37
- package/dist/signers/nsec.d.ts +2 -2
- package/dist/signers/nsec.js +8 -8
- package/dist/signers.d.ts +8 -8
- package/dist/signers.js +25 -18
- package/dist/storage-sqlite.d.ts +3 -1
- package/dist/storage-sqlite.js +110 -58
- package/dist/storage.d.ts +10 -6
- package/dist/store.d.ts +12 -8
- package/dist/store.js +156 -78
- package/docs/ACCESS.md +14 -25
- package/docs/API.md +125 -79
- package/docs/KEYS.md +3 -3
- package/docs/NIP-CD.md +29 -19
- package/docs/PUBLIC.md +33 -23
- package/docs/RECOVERY.md +7 -5
- package/docs/RELAYS.md +6 -6
- package/docs/SESSIONS.md +25 -21
- package/docs/SIGNERS.md +5 -3
- package/docs/STORAGE.md +15 -15
- package/docs/TUTORIAL.md +42 -39
- package/package.json +1 -37
- package/dist/inbound-policy.d.ts +0 -4
- package/dist/inbound-policy.js +0 -9
package/dist/relay-pool.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import WebSocket from
|
|
2
|
-
import { AbstractRelay } from
|
|
3
|
-
import { verifyEvent } from
|
|
4
|
-
import { RelayError } from
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
import { AbstractRelay, } from "nostr-tools/abstract-relay";
|
|
3
|
+
import { verifyEvent, } from "nostr-tools";
|
|
4
|
+
import { RelayError } from "./errors.js";
|
|
5
5
|
export const DEFAULT_RELAY_TIMEOUTS = Object.freeze({
|
|
6
6
|
connectionTimeoutMs: 5000,
|
|
7
7
|
eoseTimeoutMs: 25000,
|
|
@@ -16,14 +16,19 @@ class LibraryRelay extends AbstractRelay {
|
|
|
16
16
|
currentSocket;
|
|
17
17
|
authWrite = false;
|
|
18
18
|
constructor(url, socket, currentSocket) {
|
|
19
|
-
super(new URL(url).origin, {
|
|
19
|
+
super(new URL(url).origin, {
|
|
20
|
+
websocketImplementation: socket,
|
|
21
|
+
verifyEvent: (event) => {
|
|
20
22
|
try {
|
|
21
23
|
return verifyEvent(event);
|
|
22
24
|
}
|
|
23
25
|
catch {
|
|
24
26
|
return false;
|
|
25
27
|
}
|
|
26
|
-
},
|
|
28
|
+
},
|
|
29
|
+
enableReconnect: false,
|
|
30
|
+
enablePing: true,
|
|
31
|
+
});
|
|
27
32
|
this.currentSocket = currentSocket;
|
|
28
33
|
this.onnotice = () => { }; // Relay notices can contain private input or credential-bearing URLs.
|
|
29
34
|
}
|
|
@@ -34,7 +39,9 @@ class LibraryRelay extends AbstractRelay {
|
|
|
34
39
|
queueMicrotask(() => this.close());
|
|
35
40
|
// Subscription.fire/close do not await send. Retire failed demand through
|
|
36
41
|
// onclose instead of producing an orphan rejected promise.
|
|
37
|
-
return message.startsWith('["REQ",') || message.startsWith('["CLOSE",')
|
|
42
|
+
return message.startsWith('["REQ",') || message.startsWith('["CLOSE",')
|
|
43
|
+
? Promise.resolve()
|
|
44
|
+
: Promise.reject(new Error("Relay write failed"));
|
|
38
45
|
};
|
|
39
46
|
if (!this.connected || socket?.readyState !== WebSocket.OPEN)
|
|
40
47
|
return failed();
|
|
@@ -42,7 +49,9 @@ class LibraryRelay extends AbstractRelay {
|
|
|
42
49
|
// AbstractRelay.auth caches one promise for the entire connection, even after a
|
|
43
50
|
// different challenge. Use its publish ACK machinery for challenge-scoped AUTH.
|
|
44
51
|
// The signed template is constructed by this adapter; ordinary events stay EVENT.
|
|
45
|
-
socket.send(this.authWrite && message.startsWith('["EVENT",')
|
|
52
|
+
socket.send(this.authWrite && message.startsWith('["EVENT",')
|
|
53
|
+
? `["AUTH",${message.slice(9)}`
|
|
54
|
+
: message);
|
|
46
55
|
return Promise.resolve();
|
|
47
56
|
}
|
|
48
57
|
catch {
|
|
@@ -73,7 +82,8 @@ export class RelayPool {
|
|
|
73
82
|
}
|
|
74
83
|
isConnected(url) {
|
|
75
84
|
try {
|
|
76
|
-
return !this.closed &&
|
|
85
|
+
return (!this.closed &&
|
|
86
|
+
this.connections.get(new URL(url).href)?.relay.connected === true);
|
|
77
87
|
}
|
|
78
88
|
catch {
|
|
79
89
|
return false;
|
|
@@ -81,7 +91,7 @@ export class RelayPool {
|
|
|
81
91
|
}
|
|
82
92
|
acquire(url, consumer) {
|
|
83
93
|
if (this.closed)
|
|
84
|
-
throw this.error(url,
|
|
94
|
+
throw this.error(url, "RELAY_CLOSED");
|
|
85
95
|
const key = new URL(url).href;
|
|
86
96
|
let connection = this.connections.get(key);
|
|
87
97
|
if (!connection) {
|
|
@@ -93,42 +103,55 @@ export class RelayPool {
|
|
|
93
103
|
current.users.add(consumer);
|
|
94
104
|
let released = false;
|
|
95
105
|
return {
|
|
96
|
-
get authenticating() {
|
|
106
|
+
get authenticating() {
|
|
107
|
+
return current.authTimer !== undefined;
|
|
108
|
+
},
|
|
97
109
|
ready: current.ready,
|
|
98
110
|
subscribe: (filter, params) => current.relay.subscribe([filter], params),
|
|
99
111
|
publish: (event, afterAuth = false) => {
|
|
100
112
|
const body = JSON.stringify(event), prior = current.publications.get(event.id);
|
|
101
113
|
if (prior) {
|
|
102
114
|
if (prior.body !== body)
|
|
103
|
-
return Promise.reject(this.error(key,
|
|
115
|
+
return Promise.reject(this.error(key, "RELAY_EVENT_REJECTED"));
|
|
104
116
|
if (afterAuth && prior.replayGeneration !== current.authGeneration) {
|
|
105
117
|
if (prior.replays >= 1)
|
|
106
|
-
return Promise.reject(new Error(
|
|
118
|
+
return Promise.reject(new Error("auth-required: retry exhausted"));
|
|
107
119
|
prior.replays++;
|
|
108
120
|
prior.replayGeneration = current.authGeneration;
|
|
109
121
|
// Keep the existing library ACK slot when a challenge arrived without
|
|
110
122
|
// rejecting the first EVENT. Do not call publish() over the same slot.
|
|
111
|
-
void current.relay
|
|
123
|
+
void current.relay
|
|
124
|
+
.send(`["EVENT",${body}]`)
|
|
125
|
+
.catch(() => current.fail(this.error(key, "RELAY_CLOSED")));
|
|
112
126
|
}
|
|
113
127
|
return prior.pending;
|
|
114
128
|
}
|
|
115
129
|
if (current.ackIds.has(event.id))
|
|
116
|
-
return Promise.reject(this.error(key,
|
|
130
|
+
return Promise.reject(this.error(key, "RELAY_EVENT_REJECTED"));
|
|
117
131
|
current.relay.publishTimeout = this.timeouts.publishTimeoutMs;
|
|
118
132
|
current.acks.delete(event.id);
|
|
119
133
|
current.ackIds.add(event.id);
|
|
120
|
-
const pending = current.relay.publish(event).then(reason => {
|
|
134
|
+
const pending = current.relay.publish(event).then((reason) => {
|
|
121
135
|
if (current.acks.get(event.id) !== true)
|
|
122
|
-
throw new Error(
|
|
136
|
+
throw new Error("Invalid relay ACK");
|
|
123
137
|
return reason;
|
|
124
138
|
});
|
|
125
|
-
const entry = {
|
|
139
|
+
const entry = {
|
|
140
|
+
body,
|
|
141
|
+
pending,
|
|
142
|
+
replayGeneration: afterAuth ? current.authGeneration : -1,
|
|
143
|
+
replays: afterAuth ? 1 : 0,
|
|
144
|
+
};
|
|
126
145
|
current.publications.set(event.id, entry);
|
|
127
|
-
void pending
|
|
128
|
-
|
|
129
|
-
current.
|
|
130
|
-
|
|
131
|
-
|
|
146
|
+
void pending
|
|
147
|
+
.finally(() => {
|
|
148
|
+
if (current.publications.get(event.id) === entry) {
|
|
149
|
+
current.publications.delete(event.id);
|
|
150
|
+
current.acks.delete(event.id);
|
|
151
|
+
current.ackIds.delete(event.id);
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
.catch(() => { });
|
|
132
155
|
return pending;
|
|
133
156
|
},
|
|
134
157
|
release: () => {
|
|
@@ -139,16 +162,21 @@ export class RelayPool {
|
|
|
139
162
|
if (current.users.size || current.failed)
|
|
140
163
|
return;
|
|
141
164
|
if (!current.relay.connected) {
|
|
142
|
-
current.fail(this.error(key,
|
|
165
|
+
current.fail(this.error(key, "RELAY_CLOSED"));
|
|
143
166
|
return;
|
|
144
167
|
}
|
|
145
|
-
current.idle = setTimeout(() => current.fail(this.error(key,
|
|
168
|
+
current.idle = setTimeout(() => current.fail(this.error(key, "RELAY_CLOSED")), this.timeouts.idleTimeoutMs);
|
|
146
169
|
current.idle.unref();
|
|
147
|
-
}
|
|
170
|
+
},
|
|
148
171
|
};
|
|
149
172
|
}
|
|
150
173
|
error(url, code, extras = {}) {
|
|
151
|
-
return new RelayError(
|
|
174
|
+
return new RelayError("Relay operation failed", {
|
|
175
|
+
relay: url,
|
|
176
|
+
operation: code.startsWith("RELAY_AUTH") ? "auth" : "connect",
|
|
177
|
+
code,
|
|
178
|
+
...extras,
|
|
179
|
+
});
|
|
152
180
|
}
|
|
153
181
|
connect(url) {
|
|
154
182
|
let socket;
|
|
@@ -163,19 +191,44 @@ export class RelayPool {
|
|
|
163
191
|
static CLOSED = WebSocket.CLOSED;
|
|
164
192
|
constructor() {
|
|
165
193
|
socket = factory(url);
|
|
166
|
-
socket.on(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
socket.on(
|
|
194
|
+
socket.on("open", () => {
|
|
195
|
+
opened = true;
|
|
196
|
+
});
|
|
197
|
+
socket.on("error", (error) => {
|
|
198
|
+
networkError = new RelayError("Relay connection failed", {
|
|
199
|
+
relay: url,
|
|
200
|
+
operation: "connect",
|
|
201
|
+
code: "RELAY_CONNECTION_FAILED",
|
|
202
|
+
networkCode: error.code,
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
socket.on("unexpected-response", (_request, response) => {
|
|
206
|
+
response.resume();
|
|
207
|
+
networkError = new RelayError("Relay HTTP handshake rejected", {
|
|
208
|
+
relay: url,
|
|
209
|
+
operation: "connect",
|
|
210
|
+
code: "RELAY_HTTP_REJECTED",
|
|
211
|
+
statusCode: response.statusCode,
|
|
212
|
+
});
|
|
213
|
+
connection.fail(networkError);
|
|
214
|
+
});
|
|
215
|
+
socket.on("close", (closeCode) => {
|
|
216
|
+
networkError ??= new RelayError("Relay disconnected", {
|
|
217
|
+
relay: url,
|
|
218
|
+
operation: "subscribe",
|
|
219
|
+
code: "RELAY_CLOSED",
|
|
220
|
+
closeCode,
|
|
221
|
+
});
|
|
222
|
+
});
|
|
170
223
|
// Observe only small control frames. EVENT parsing, filtering and signature
|
|
171
224
|
// verification are owned by nostr-tools, not a second frame dispatcher.
|
|
172
|
-
socket.on(
|
|
225
|
+
socket.on("message", (raw) => {
|
|
173
226
|
const text = raw.toString();
|
|
174
227
|
const eventHeader = /^\s*\[\s*"EVENT"\s*,\s*("(?:\\.|[^"\\])*")\s*,/.exec(text);
|
|
175
228
|
if (eventHeader) {
|
|
176
229
|
try {
|
|
177
230
|
const id = JSON.parse(eventHeader[1]);
|
|
178
|
-
if (typeof id ===
|
|
231
|
+
if (typeof id === "string")
|
|
179
232
|
for (const user of [...connection.users])
|
|
180
233
|
user.rawEvent(id);
|
|
181
234
|
}
|
|
@@ -186,19 +239,24 @@ export class RelayPool {
|
|
|
186
239
|
return;
|
|
187
240
|
try {
|
|
188
241
|
const frame = JSON.parse(text);
|
|
189
|
-
if (!Array.isArray(frame) ||
|
|
242
|
+
if (!Array.isArray(frame) ||
|
|
243
|
+
typeof frame[1] !== "string" ||
|
|
244
|
+
connection.failed)
|
|
190
245
|
return;
|
|
191
|
-
if (frame[0] ===
|
|
246
|
+
if (frame[0] === "EOSE" && !connection.authTimer) {
|
|
192
247
|
for (const user of [...connection.users])
|
|
193
248
|
user.wireEose(frame[1]);
|
|
194
249
|
}
|
|
195
|
-
if (frame[0] ===
|
|
250
|
+
if (frame[0] === "AUTH")
|
|
196
251
|
thisAuth(frame[1]);
|
|
197
|
-
if (frame[0] ===
|
|
252
|
+
if (frame[0] === "OK" && connection.ackIds.has(frame[1]))
|
|
198
253
|
connection.acks.set(frame[1], frame[2] === true);
|
|
199
254
|
}
|
|
200
|
-
catch {
|
|
255
|
+
catch {
|
|
256
|
+
/* Untrusted control frames are ignored without diagnostics. */
|
|
257
|
+
}
|
|
201
258
|
});
|
|
259
|
+
// biome-ignore lint/correctness/noConstructorReturn: nostr-tools constructs the supplied WebSocket and needs the factory socket.
|
|
202
260
|
return socket;
|
|
203
261
|
}
|
|
204
262
|
};
|
|
@@ -208,8 +266,19 @@ export class RelayPool {
|
|
|
208
266
|
relay.publishTimeout = this.timeouts.publishTimeoutMs;
|
|
209
267
|
relay.baseEoseTimeout = this.timeouts.eoseTimeoutMs;
|
|
210
268
|
const abort = new AbortController();
|
|
211
|
-
connection = {
|
|
212
|
-
|
|
269
|
+
connection = {
|
|
270
|
+
url,
|
|
271
|
+
relay,
|
|
272
|
+
ready: Promise.resolve(),
|
|
273
|
+
users: new Set(),
|
|
274
|
+
publications: new Map(),
|
|
275
|
+
acks: new Map(),
|
|
276
|
+
ackIds: new Set(),
|
|
277
|
+
authGeneration: 0,
|
|
278
|
+
failed: false,
|
|
279
|
+
fail: () => { },
|
|
280
|
+
};
|
|
281
|
+
connection.fail = (error) => {
|
|
213
282
|
if (connection.failed)
|
|
214
283
|
return;
|
|
215
284
|
connection.failed = true;
|
|
@@ -228,35 +297,54 @@ export class RelayPool {
|
|
|
228
297
|
connection.users.clear();
|
|
229
298
|
relay.close();
|
|
230
299
|
};
|
|
231
|
-
relay.onclose = () => {
|
|
232
|
-
|
|
300
|
+
relay.onclose = () => {
|
|
301
|
+
if (opened || connection.failed)
|
|
302
|
+
connection.fail(networkError ?? this.error(url, "RELAY_CLOSED"));
|
|
303
|
+
};
|
|
233
304
|
const thisAuth = (challenge) => {
|
|
234
|
-
|
|
305
|
+
const sign = this.sign;
|
|
306
|
+
if (!sign || !connection.users.size || connection.challenge === challenge)
|
|
235
307
|
return;
|
|
236
308
|
connection.challenge = challenge;
|
|
237
309
|
const generation = ++connection.authGeneration;
|
|
238
|
-
connection.authTimer ??= setTimeout(() => connection.fail(this.error(url,
|
|
310
|
+
connection.authTimer ??= setTimeout(() => connection.fail(this.error(url, "RELAY_AUTH_TIMEOUT")), this.timeouts.authTimeoutMs);
|
|
239
311
|
for (const user of [...connection.users])
|
|
240
312
|
user.authenticating();
|
|
241
|
-
void Promise.resolve()
|
|
313
|
+
void Promise.resolve()
|
|
314
|
+
.then(() => sign.call(this, {
|
|
315
|
+
kind: 22242,
|
|
316
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
317
|
+
tags: [
|
|
318
|
+
["relay", url],
|
|
319
|
+
["challenge", challenge],
|
|
320
|
+
],
|
|
321
|
+
content: "",
|
|
322
|
+
}))
|
|
323
|
+
.then(async (event) => {
|
|
242
324
|
if (connection.failed || generation !== connection.authGeneration)
|
|
243
325
|
return;
|
|
244
326
|
// Enforce the signer boundary before inserting anything in the library ACK map.
|
|
245
|
-
if (!verifyEvent(event) ||
|
|
246
|
-
|
|
327
|
+
if (!verifyEvent(event) ||
|
|
328
|
+
event.kind !== 22242 ||
|
|
329
|
+
event.content !== "" ||
|
|
330
|
+
!event.tags.some((tag) => tag[0] === "relay" && tag[1] === url) ||
|
|
331
|
+
!event.tags.some((tag) => tag[0] === "challenge" && tag[1] === challenge))
|
|
332
|
+
throw new Error("Invalid auth signature");
|
|
247
333
|
relay.publishTimeout = this.timeouts.authTimeoutMs;
|
|
248
334
|
if (connection.ackIds.has(event.id))
|
|
249
|
-
throw new Error(
|
|
335
|
+
throw new Error("Conflicting auth operation");
|
|
250
336
|
connection.acks.delete(event.id);
|
|
251
337
|
connection.ackIds.add(event.id);
|
|
252
338
|
try {
|
|
253
339
|
await relay.publishAuth(event);
|
|
254
340
|
if (connection.acks.get(event.id) !== true)
|
|
255
|
-
throw new Error(
|
|
341
|
+
throw new Error("Invalid auth ACK");
|
|
256
342
|
}
|
|
257
343
|
catch (error) {
|
|
258
344
|
if (!connection.failed && generation === connection.authGeneration)
|
|
259
|
-
connection.fail(this.error(url,
|
|
345
|
+
connection.fail(this.error(url, "RELAY_AUTH_REJECTED", {
|
|
346
|
+
reason: error instanceof Error ? error.message : undefined,
|
|
347
|
+
}));
|
|
260
348
|
return;
|
|
261
349
|
}
|
|
262
350
|
finally {
|
|
@@ -269,15 +357,20 @@ export class RelayPool {
|
|
|
269
357
|
connection.authTimer = undefined;
|
|
270
358
|
for (const user of [...connection.users])
|
|
271
359
|
user.authenticated();
|
|
272
|
-
})
|
|
360
|
+
})
|
|
361
|
+
.catch(() => {
|
|
273
362
|
if (!connection.failed && generation === connection.authGeneration)
|
|
274
|
-
connection.fail(this.error(url,
|
|
363
|
+
connection.fail(this.error(url, "RELAY_AUTH_SIGNING_FAILED"));
|
|
275
364
|
});
|
|
276
365
|
};
|
|
277
|
-
const connecting = relay.connect({
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
366
|
+
const connecting = relay.connect({
|
|
367
|
+
timeout: this.timeouts.connectionTimeoutMs,
|
|
368
|
+
abort: abort.signal,
|
|
369
|
+
});
|
|
370
|
+
const connectedSocket = socket;
|
|
371
|
+
const receive = connectedSocket?.onmessage;
|
|
372
|
+
if (connectedSocket && receive)
|
|
373
|
+
connectedSocket.onmessage = (message) => {
|
|
281
374
|
if (connection.failed)
|
|
282
375
|
return;
|
|
283
376
|
const text = String(message.data);
|
|
@@ -290,26 +383,38 @@ export class RelayPool {
|
|
|
290
383
|
catch {
|
|
291
384
|
return;
|
|
292
385
|
}
|
|
293
|
-
if (!Array.isArray(frame) || typeof frame[1] !==
|
|
386
|
+
if (!Array.isArray(frame) || typeof frame[1] !== "string")
|
|
294
387
|
return;
|
|
295
|
-
if (frame[0] ===
|
|
388
|
+
if (frame[0] === "EVENT") {
|
|
296
389
|
// All SDK physical subscription IDs are UUIDs. Canonical framing keeps
|
|
297
390
|
// the library's fast header scanner safe with unusual whitespace/escapes.
|
|
298
|
-
if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(frame[1]) ||
|
|
391
|
+
if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(frame[1]) ||
|
|
392
|
+
frame.length !== 3)
|
|
299
393
|
return;
|
|
300
|
-
receive.call(
|
|
394
|
+
receive.call(connectedSocket, {
|
|
395
|
+
data: JSON.stringify(frame),
|
|
396
|
+
});
|
|
301
397
|
return;
|
|
302
398
|
}
|
|
303
|
-
if (![
|
|
399
|
+
if (!["EOSE", "AUTH", "OK", "CLOSED", "NOTICE"].includes(frame[0]))
|
|
304
400
|
return;
|
|
305
|
-
if (frame[0] ===
|
|
306
|
-
receive.call(
|
|
401
|
+
if (frame[0] === "CLOSED" && typeof frame[2] !== "string") {
|
|
402
|
+
receive.call(connectedSocket, {
|
|
403
|
+
data: JSON.stringify([
|
|
404
|
+
"CLOSED",
|
|
405
|
+
frame[1],
|
|
406
|
+
"error: malformed relay response",
|
|
407
|
+
]),
|
|
408
|
+
});
|
|
307
409
|
return;
|
|
308
410
|
}
|
|
309
|
-
receive.call(
|
|
411
|
+
receive.call(connectedSocket, message);
|
|
310
412
|
};
|
|
311
|
-
connection.ready = connecting.catch(error => {
|
|
312
|
-
const reason = error ===
|
|
413
|
+
connection.ready = connecting.catch((error) => {
|
|
414
|
+
const reason = error === "connection timed out"
|
|
415
|
+
? this.error(url, "RELAY_CONNECT_TIMEOUT")
|
|
416
|
+
: (networkError ??
|
|
417
|
+
this.error(url, connection.failed ? "RELAY_CLOSED" : "RELAY_CONNECTION_FAILED"));
|
|
313
418
|
connection.fail(reason);
|
|
314
419
|
throw reason;
|
|
315
420
|
});
|
|
@@ -319,6 +424,6 @@ export class RelayPool {
|
|
|
319
424
|
close() {
|
|
320
425
|
this.closed = true;
|
|
321
426
|
for (const connection of [...this.connections.values()])
|
|
322
|
-
connection.fail(this.error(connection.url,
|
|
427
|
+
connection.fail(this.error(connection.url, "RELAY_CLOSED"));
|
|
323
428
|
}
|
|
324
429
|
}
|
package/dist/relay-roles.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DEFAULT_DISCOVERY_RELAYS, DEFAULT_DM_RELAYS, DEFAULT_READ_RELAYS, DEFAULT_WRITE_RELAYS } from
|
|
2
|
-
import { websocketURL } from
|
|
1
|
+
import { DEFAULT_DISCOVERY_RELAYS, DEFAULT_DM_RELAYS, DEFAULT_READ_RELAYS, DEFAULT_WRITE_RELAYS, } from "./defaults.js";
|
|
2
|
+
import { websocketURL } from "./metadata.js";
|
|
3
3
|
export function relayUnion(...groups) {
|
|
4
4
|
const values = new Map();
|
|
5
5
|
for (const value of groups.flat()) {
|
|
@@ -12,31 +12,40 @@ export function relayUnion(...groups) {
|
|
|
12
12
|
}
|
|
13
13
|
export function relayRoles(input) {
|
|
14
14
|
const list = (value, fallback, name, empty = false) => {
|
|
15
|
-
if (value !== undefined &&
|
|
16
|
-
|
|
15
|
+
if (value !== undefined &&
|
|
16
|
+
(!Array.isArray(value) ||
|
|
17
|
+
(!empty && !value.length) ||
|
|
18
|
+
!Array.from(value).every(websocketURL))) {
|
|
19
|
+
throw new TypeError(`${name} must be ${empty ? "an" : "a nonempty"} array of WebSocket URLs`);
|
|
17
20
|
}
|
|
18
21
|
return relayUnion([...(value ?? fallback)]);
|
|
19
22
|
};
|
|
20
23
|
if (input.relays !== undefined) {
|
|
21
|
-
list(input.relays, [],
|
|
24
|
+
list(input.relays, [], "relays");
|
|
22
25
|
}
|
|
23
26
|
return {
|
|
24
|
-
dmRelays: list(input.dmRelays ?? input.relays, DEFAULT_DM_RELAYS,
|
|
25
|
-
readRelays: list(input.readRelays ?? input.relays, DEFAULT_READ_RELAYS,
|
|
26
|
-
writeRelays: list(input.writeRelays ?? input.relays, DEFAULT_WRITE_RELAYS,
|
|
27
|
-
discoveryRelays: list(input.discoveryRelays ?? input.relays, DEFAULT_DISCOVERY_RELAYS,
|
|
27
|
+
dmRelays: list(input.dmRelays ?? input.relays, DEFAULT_DM_RELAYS, "dmRelays"),
|
|
28
|
+
readRelays: list(input.readRelays ?? input.relays, DEFAULT_READ_RELAYS, "readRelays", true),
|
|
29
|
+
writeRelays: list(input.writeRelays ?? input.relays, DEFAULT_WRITE_RELAYS, "writeRelays"),
|
|
30
|
+
discoveryRelays: list(input.discoveryRelays ?? input.relays, DEFAULT_DISCOVERY_RELAYS, "discoveryRelays", true),
|
|
28
31
|
};
|
|
29
32
|
}
|
|
30
33
|
/** NIP-65: no marker means both roles, otherwise read/write. Canonical URLs deduplicate aliases. */
|
|
31
34
|
export function readWriteTags(read, write) {
|
|
32
|
-
const r = new Set(read.map(url => new URL(url).toString())), w = new Set(write.map(url => new URL(url).toString()));
|
|
33
|
-
return [...new Set([...r, ...w])].map(
|
|
35
|
+
const r = new Set(read.map((url) => new URL(url).toString())), w = new Set(write.map((url) => new URL(url).toString()));
|
|
36
|
+
return [...new Set([...r, ...w])].map((url) => [
|
|
37
|
+
"r",
|
|
38
|
+
url,
|
|
39
|
+
...(r.has(url) && w.has(url) ? [] : [r.has(url) ? "read" : "write"]),
|
|
40
|
+
]);
|
|
34
41
|
}
|
|
35
42
|
/** At least one ACK in each role set, with duplicate/overlapping sets coalesced. */
|
|
36
43
|
export function publicationGroups(groups) {
|
|
37
44
|
const seen = new Set();
|
|
38
|
-
return groups
|
|
39
|
-
|
|
45
|
+
return groups
|
|
46
|
+
.map((group) => relayUnion(group))
|
|
47
|
+
.filter((group) => {
|
|
48
|
+
const key = JSON.stringify(group.map((url) => new URL(url).toString()).sort());
|
|
40
49
|
if (!group.length || seen.has(key)) {
|
|
41
50
|
return false;
|
|
42
51
|
}
|
package/dist/relay.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { type RelayTimeouts } from
|
|
2
|
-
export { DEFAULT_RELAY_TIMEOUTS, type RelayTimeouts } from
|
|
3
|
-
import { RelayError } from
|
|
4
|
-
import { type Event, type Filter, type EventTemplate } from
|
|
1
|
+
import { type RelayTimeouts } from "./relay-pool.js";
|
|
2
|
+
export { DEFAULT_RELAY_TIMEOUTS, type RelayTimeouts } from "./relay-pool.js";
|
|
3
|
+
import { RelayError } from "./errors.js";
|
|
4
|
+
import { type Event, type Filter, type EventTemplate } from "nostr-tools";
|
|
5
5
|
export interface Subscription {
|
|
6
6
|
close(): void;
|
|
7
7
|
ready: Promise<void>;
|
|
8
8
|
}
|
|
9
9
|
export interface Transport {
|
|
10
10
|
query(relays: string[], filter: Filter): Promise<Event[]>;
|
|
11
|
-
subscribe(relays: string[], filter: Filter, onEvent: (event: Event, source:
|
|
11
|
+
subscribe(relays: string[], filter: Filter, onEvent: (event: Event, source: "live" | "history") => void, onDisconnect?: (error: Error) => void): Subscription;
|
|
12
12
|
publish(relays: string[], event: Event): Promise<void>;
|
|
13
13
|
close(): void;
|
|
14
14
|
}
|
|
@@ -16,17 +16,17 @@ export declare function physicalConnectionState(transport: Transport, relay: str
|
|
|
16
16
|
/** Verified layers may use this local-window boundary; it is never supplied by event tags. */
|
|
17
17
|
export declare function localReadBoundary(event: Event): number | undefined;
|
|
18
18
|
/** Only SDK long-lived inbox demand opts into a local initial-read window. */
|
|
19
|
-
export declare function subscribeInbox(transport: Transport, ...args: Parameters<Transport[
|
|
19
|
+
export declare function subscribeInbox(transport: Transport, ...args: Parameters<Transport["subscribe"]>): Subscription;
|
|
20
20
|
/** RPC response subscriptions are ready once REQ has been issued on the socket. */
|
|
21
|
-
export declare function subscribeRequests(transport: WebSocketTransport, ...args: Parameters<Transport[
|
|
21
|
+
export declare function subscribeRequests(transport: WebSocketTransport, ...args: Parameters<Transport["subscribe"]>): Subscription;
|
|
22
22
|
/** Opt-in performance metadata only: no event IDs, authors, content or raw URL paths/queries. */
|
|
23
23
|
export interface RelayTiming {
|
|
24
|
-
operation:
|
|
24
|
+
operation: "query" | "publish";
|
|
25
25
|
relay: string;
|
|
26
26
|
kinds: number[];
|
|
27
27
|
durationMs: number;
|
|
28
|
-
outcome:
|
|
29
|
-
code?: RelayError[
|
|
28
|
+
outcome: "ok" | "error";
|
|
29
|
+
code?: RelayError["code"];
|
|
30
30
|
}
|
|
31
31
|
export type RelayTimingObserver = (timing: RelayTiming) => void | Promise<void>;
|
|
32
32
|
export interface WebSocketTransportOptions extends Partial<RelayTimeouts> {
|
|
@@ -43,7 +43,7 @@ export declare class WebSocketTransport implements Transport {
|
|
|
43
43
|
readonly timeouts: Readonly<RelayTimeouts>;
|
|
44
44
|
constructor(options?: number | WebSocketTransportOptions, sign?: ((template: EventTemplate) => Promise<Event>) | undefined, onTiming?: RelayTimingObserver | undefined);
|
|
45
45
|
private timing;
|
|
46
|
-
subscribe(relays: string[], filter: Filter, onEvent: (event: Event, source:
|
|
46
|
+
subscribe(relays: string[], filter: Filter, onEvent: (event: Event, source: "live" | "history") => void, onDisconnect?: (error: Error) => void): Subscription;
|
|
47
47
|
private subscribeMode;
|
|
48
48
|
private queuedQuery;
|
|
49
49
|
query(relays: string[], filter: Filter): Promise<Event[]>;
|