@nostrify/nostrify 0.46.8 → 0.46.9
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/.turbo/turbo-build.log +124 -28
- package/.turbo/turbo-test.log +91 -123
- package/CHANGELOG.md +8 -0
- package/NIP05.ts +6 -6
- package/NSchema.ts +71 -30
- package/dist/NSchema.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/tsconfig.json +1 -2
- package/dist/BunkerURI.js +0 -48
- package/dist/NBrowserSigner.js +0 -82
- package/dist/NCache.js +0 -39
- package/dist/NConnectSigner.js +0 -132
- package/dist/NIP05.js +0 -35
- package/dist/NIP50.js +0 -21
- package/dist/NIP98.js +0 -74
- package/dist/NKinds.js +0 -25
- package/dist/NPool.js +0 -154
- package/dist/NRelay1.js +0 -318
- package/dist/NSchema.js +0 -187
- package/dist/NSecSigner.js +0 -38
- package/dist/NSet.js +0 -143
- package/dist/RelayError.js +0 -20
- package/dist/ln/LNURL.js +0 -101
- package/dist/ln/mod.js +0 -4
- package/dist/ln/mod.js.map +0 -7
- package/dist/ln/types/LNURLCallback.js +0 -0
- package/dist/ln/types/LNURLDetails.js +0 -0
- package/dist/mod.js +0 -30
- package/dist/mod.js.map +0 -7
- package/dist/test/ErrorRelay.js +0 -23
- package/dist/test/MockRelay.js +0 -61
- package/dist/test/TestRelayServer.js +0 -149
- package/dist/test/mod.js +0 -24
- package/dist/test/mod.js.map +0 -7
- package/dist/uploaders/BlossomUploader.js +0 -70
- package/dist/uploaders/NostrBuildUploader.js +0 -65
- package/dist/uploaders/mod.js +0 -6
- package/dist/uploaders/mod.js.map +0 -7
- package/dist/utils/CircularSet.js +0 -29
- package/dist/utils/Machina.js +0 -38
- package/dist/utils/N64.js +0 -17
- package/dist/utils/mod.js +0 -6
- package/dist/utils/mod.js.map +0 -7
package/dist/NCache.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { LRUCache } from "lru-cache";
|
|
2
|
-
import { matchFilters } from "nostr-tools";
|
|
3
|
-
import { NSet } from "./NSet.js";
|
|
4
|
-
class NCache extends NSet {
|
|
5
|
-
constructor(...args) {
|
|
6
|
-
super(new LRUCache(...args));
|
|
7
|
-
}
|
|
8
|
-
async event(event) {
|
|
9
|
-
this.add(event);
|
|
10
|
-
}
|
|
11
|
-
async query(filters) {
|
|
12
|
-
const events = [];
|
|
13
|
-
for (const event of this) {
|
|
14
|
-
if (matchFilters(filters, event)) {
|
|
15
|
-
this.cache.get(event.id);
|
|
16
|
-
events.push(event);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return events;
|
|
20
|
-
}
|
|
21
|
-
async remove(filters) {
|
|
22
|
-
for (const event of this) {
|
|
23
|
-
if (matchFilters(filters, event)) {
|
|
24
|
-
this.delete(event);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async count(filters) {
|
|
29
|
-
const events = await this.query(filters);
|
|
30
|
-
return {
|
|
31
|
-
count: events.length,
|
|
32
|
-
approximate: false
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
[Symbol.toStringTag] = "NCache";
|
|
36
|
-
}
|
|
37
|
-
export {
|
|
38
|
-
NCache
|
|
39
|
-
};
|
package/dist/NConnectSigner.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { NSchema as n } from "./NSchema.js";
|
|
3
|
-
class NConnectSigner {
|
|
4
|
-
relay;
|
|
5
|
-
pubkey;
|
|
6
|
-
signer;
|
|
7
|
-
timeout;
|
|
8
|
-
encryption;
|
|
9
|
-
constructor({ relay, pubkey, signer, timeout, encryption = "nip44" }) {
|
|
10
|
-
this.relay = relay;
|
|
11
|
-
this.pubkey = pubkey;
|
|
12
|
-
this.signer = signer;
|
|
13
|
-
this.timeout = timeout;
|
|
14
|
-
this.encryption = encryption;
|
|
15
|
-
}
|
|
16
|
-
async getPublicKey() {
|
|
17
|
-
return this.cmd("get_public_key", []);
|
|
18
|
-
}
|
|
19
|
-
async signEvent(event) {
|
|
20
|
-
const result = await this.cmd("sign_event", [JSON.stringify(event)]);
|
|
21
|
-
return n.json().pipe(n.event()).parse(result);
|
|
22
|
-
}
|
|
23
|
-
async getRelays() {
|
|
24
|
-
const result = await this.cmd("get_relays", []);
|
|
25
|
-
return n.json().pipe(
|
|
26
|
-
z.record(
|
|
27
|
-
z.string(),
|
|
28
|
-
z.object({ read: z.boolean(), write: z.boolean() })
|
|
29
|
-
)
|
|
30
|
-
).parse(result);
|
|
31
|
-
}
|
|
32
|
-
nip04 = {
|
|
33
|
-
encrypt: async (pubkey, plaintext) => {
|
|
34
|
-
return this.cmd("nip04_encrypt", [pubkey, plaintext]);
|
|
35
|
-
},
|
|
36
|
-
decrypt: async (pubkey, ciphertext) => {
|
|
37
|
-
return this.cmd("nip04_decrypt", [pubkey, ciphertext]);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
nip44 = {
|
|
41
|
-
encrypt: async (pubkey, plaintext) => {
|
|
42
|
-
return this.cmd("nip44_encrypt", [pubkey, plaintext]);
|
|
43
|
-
},
|
|
44
|
-
decrypt: async (pubkey, ciphertext) => {
|
|
45
|
-
return this.cmd("nip44_decrypt", [pubkey, ciphertext]);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
/** Send a `connect` command to the relay. It should respond with `ack`. */
|
|
49
|
-
async connect(secret) {
|
|
50
|
-
const params = [this.pubkey];
|
|
51
|
-
if (secret) {
|
|
52
|
-
params.push(secret);
|
|
53
|
-
}
|
|
54
|
-
return this.cmd("connect", params);
|
|
55
|
-
}
|
|
56
|
-
/** Send a `ping` command to the signer. It should respond with `pong`. */
|
|
57
|
-
async ping() {
|
|
58
|
-
return this.cmd("ping", []);
|
|
59
|
-
}
|
|
60
|
-
/** High-level RPC method. Returns the string result, or throws on error. */
|
|
61
|
-
async cmd(method, params) {
|
|
62
|
-
const signal = typeof this.timeout === "number" ? AbortSignal.timeout(this.timeout) : void 0;
|
|
63
|
-
const { result, error } = await this.send(
|
|
64
|
-
{ id: crypto.randomUUID(), method, params },
|
|
65
|
-
{ signal }
|
|
66
|
-
);
|
|
67
|
-
if (error) {
|
|
68
|
-
throw new Error(error);
|
|
69
|
-
}
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
/** Low-level send method. Deals directly with connect request/response. */
|
|
73
|
-
async send(request, opts = {}) {
|
|
74
|
-
const { signal } = opts;
|
|
75
|
-
const event = await this.signer.signEvent({
|
|
76
|
-
kind: 24133,
|
|
77
|
-
content: await this.encrypt(this.pubkey, JSON.stringify(request)),
|
|
78
|
-
created_at: Math.floor(Date.now() / 1e3),
|
|
79
|
-
tags: [["p", this.pubkey]]
|
|
80
|
-
});
|
|
81
|
-
const local = await this.signer.getPublicKey();
|
|
82
|
-
const req = this.relay.req(
|
|
83
|
-
[{ kinds: [24133], authors: [this.pubkey], "#p": [local] }],
|
|
84
|
-
{ signal }
|
|
85
|
-
);
|
|
86
|
-
const promise = new Promise((resolve, reject) => {
|
|
87
|
-
(async () => {
|
|
88
|
-
try {
|
|
89
|
-
for await (const msg of req) {
|
|
90
|
-
if (msg[0] === "CLOSED") throw new Error("Subscription closed");
|
|
91
|
-
if (msg[0] === "EVENT") {
|
|
92
|
-
const event2 = msg[2];
|
|
93
|
-
const decrypted = await this.decrypt(this.pubkey, event2.content);
|
|
94
|
-
const response = n.json().pipe(n.connectResponse()).parse(
|
|
95
|
-
decrypted
|
|
96
|
-
);
|
|
97
|
-
if (response.id === request.id) {
|
|
98
|
-
resolve(response);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
} catch (error) {
|
|
104
|
-
reject(error);
|
|
105
|
-
}
|
|
106
|
-
})();
|
|
107
|
-
});
|
|
108
|
-
await this.relay.event(event, { signal });
|
|
109
|
-
return promise;
|
|
110
|
-
}
|
|
111
|
-
/** Local encrypt depending on settings. */
|
|
112
|
-
async encrypt(pubkey, plaintext) {
|
|
113
|
-
switch (this.encryption) {
|
|
114
|
-
case "nip04":
|
|
115
|
-
return this.signer.nip04.encrypt(pubkey, plaintext);
|
|
116
|
-
case "nip44":
|
|
117
|
-
return this.signer.nip44.encrypt(pubkey, plaintext);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/** Local decrypt depending on settings. */
|
|
121
|
-
async decrypt(pubkey, ciphertext) {
|
|
122
|
-
switch (this.encryption) {
|
|
123
|
-
case "nip04":
|
|
124
|
-
return this.signer.nip04.decrypt(pubkey, ciphertext);
|
|
125
|
-
case "nip44":
|
|
126
|
-
return this.signer.nip44.decrypt(pubkey, ciphertext);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
export {
|
|
131
|
-
NConnectSigner
|
|
132
|
-
};
|
package/dist/NIP05.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { NSchema as n, z } from "./NSchema.js";
|
|
2
|
-
class NIP05 {
|
|
3
|
-
/** NIP-05 value regex. */
|
|
4
|
-
static regex() {
|
|
5
|
-
return /^(?:([\w.+-]+)@)?([\w.-]+)$/;
|
|
6
|
-
}
|
|
7
|
-
/** Nostr pubkey with relays object. */
|
|
8
|
-
static profilePointerSchema() {
|
|
9
|
-
return z.object({
|
|
10
|
-
pubkey: n.id(),
|
|
11
|
-
relays: n.relayUrl().array().optional()
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
/** Resolve NIP-05 name to a profile pointer. */
|
|
15
|
-
static async lookup(nip05, opts) {
|
|
16
|
-
const { fetch = globalThis.fetch.bind(globalThis), signal } = opts ?? {};
|
|
17
|
-
const match = nip05.match(NIP05.regex());
|
|
18
|
-
if (!match) throw new Error(`NIP-05: invalid name ${nip05}`);
|
|
19
|
-
const [_, name = "_", domain] = match;
|
|
20
|
-
const url = new URL("/.well-known/nostr.json", `https://${domain}/`);
|
|
21
|
-
url.searchParams.set("name", name);
|
|
22
|
-
const response = await fetch(url, { signal });
|
|
23
|
-
const json = await response.json();
|
|
24
|
-
try {
|
|
25
|
-
const pubkey = json.names[name];
|
|
26
|
-
const relays = json.relays?.[pubkey];
|
|
27
|
-
return NIP05.profilePointerSchema().parse({ pubkey, relays });
|
|
28
|
-
} catch {
|
|
29
|
-
throw new Error(`NIP-05: no match for ${nip05}`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export {
|
|
34
|
-
NIP05
|
|
35
|
-
};
|
package/dist/NIP50.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
class NIP50 {
|
|
2
|
-
static parseInput(input) {
|
|
3
|
-
const regex = /(\B-\w+:[^\s"]+)|(\b\w+:[^\s"]+)|(".*?")|(\S+)/g;
|
|
4
|
-
const tokens = [];
|
|
5
|
-
let match;
|
|
6
|
-
while ((match = regex.exec(input)) !== null) {
|
|
7
|
-
if (match[1] || match[2]) {
|
|
8
|
-
const [key, ...values] = (match[1] || match[2]).split(":");
|
|
9
|
-
tokens.push({ key, value: values.join(":") });
|
|
10
|
-
} else if (match[3]) {
|
|
11
|
-
tokens.push(match[3].replace(/"/g, ""));
|
|
12
|
-
} else if (match[4]) {
|
|
13
|
-
tokens.push(match[4]);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return tokens;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export {
|
|
20
|
-
NIP50
|
|
21
|
-
};
|
package/dist/NIP98.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { encodeHex } from "@std/encoding/hex";
|
|
2
|
-
import { verifyEvent as _verifyEvent } from "nostr-tools";
|
|
3
|
-
import { N64 } from "./utils/N64.js";
|
|
4
|
-
class NIP98 {
|
|
5
|
-
/** Generate an auth event template from a Request. */
|
|
6
|
-
static async template(request, opts) {
|
|
7
|
-
const {
|
|
8
|
-
validatePayload = ["POST", "PUT", "PATCH"].includes(request.method)
|
|
9
|
-
} = opts ?? {};
|
|
10
|
-
const { method, url } = request;
|
|
11
|
-
const tags = [
|
|
12
|
-
["method", method],
|
|
13
|
-
["u", url]
|
|
14
|
-
];
|
|
15
|
-
if (validatePayload) {
|
|
16
|
-
const buffer = await request.clone().arrayBuffer();
|
|
17
|
-
const digest = await crypto.subtle.digest("SHA-256", buffer);
|
|
18
|
-
tags.push(["payload", encodeHex(digest)]);
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
kind: 27235,
|
|
22
|
-
content: "",
|
|
23
|
-
tags,
|
|
24
|
-
created_at: Math.floor(Date.now() / 1e3)
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
/** Compare the auth event with the request, throwing a human-readable error if validation fails. */
|
|
28
|
-
static async verify(request, opts) {
|
|
29
|
-
const {
|
|
30
|
-
maxAge = 6e4,
|
|
31
|
-
validatePayload = ["POST", "PUT", "PATCH"].includes(request.method),
|
|
32
|
-
verifyEvent = _verifyEvent
|
|
33
|
-
} = opts ?? {};
|
|
34
|
-
const header = request.headers.get("authorization");
|
|
35
|
-
if (!header) {
|
|
36
|
-
throw new Error("Missing Nostr authorization header");
|
|
37
|
-
}
|
|
38
|
-
const token = header.match(/^Nostr (.+)$/)?.[1];
|
|
39
|
-
if (!token) {
|
|
40
|
-
throw new Error("Missing Nostr authorization token");
|
|
41
|
-
}
|
|
42
|
-
const event = N64.decodeEvent(token);
|
|
43
|
-
if (!verifyEvent(event)) {
|
|
44
|
-
throw new Error("Event signature is invalid");
|
|
45
|
-
}
|
|
46
|
-
const age = Date.now() - event.created_at * 1e3;
|
|
47
|
-
const u = event.tags.find(([name]) => name === "u")?.[1];
|
|
48
|
-
const method = event.tags.find(([name]) => name === "method")?.[1];
|
|
49
|
-
const payload = event.tags.find(([name]) => name === "payload")?.[1];
|
|
50
|
-
if (event.kind !== 27235) {
|
|
51
|
-
throw new Error("Event must be kind 27235");
|
|
52
|
-
}
|
|
53
|
-
if (u !== request.url) {
|
|
54
|
-
throw new Error("Event URL does not match request URL");
|
|
55
|
-
}
|
|
56
|
-
if (method !== request.method) {
|
|
57
|
-
throw new Error("Event method does not match HTTP request method");
|
|
58
|
-
}
|
|
59
|
-
if (age >= maxAge) {
|
|
60
|
-
throw new Error("Event expired");
|
|
61
|
-
}
|
|
62
|
-
if (validatePayload && payload !== void 0) {
|
|
63
|
-
const buffer = await request.clone().arrayBuffer();
|
|
64
|
-
const digest = await crypto.subtle.digest("SHA-256", buffer);
|
|
65
|
-
if (encodeHex(digest) !== payload) {
|
|
66
|
-
throw new Error("Event payload does not match request body");
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return event;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export {
|
|
73
|
-
NIP98
|
|
74
|
-
};
|
package/dist/NKinds.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
class NKinds {
|
|
2
|
-
/** Events are **regular**, which means they're all expected to be stored by relays. */
|
|
3
|
-
static regular(kind) {
|
|
4
|
-
return 1e3 <= kind && kind < 1e4 || [1, 2, 4, 5, 6, 7, 8, 16, 40, 41, 42, 43, 44].includes(kind);
|
|
5
|
-
}
|
|
6
|
-
/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */
|
|
7
|
-
static replaceable(kind) {
|
|
8
|
-
return 1e4 <= kind && kind < 2e4 || [0, 3].includes(kind);
|
|
9
|
-
}
|
|
10
|
-
/** Events are **ephemeral**, which means they are not expected to be stored by relays. */
|
|
11
|
-
static ephemeral(kind) {
|
|
12
|
-
return 2e4 <= kind && kind < 3e4;
|
|
13
|
-
}
|
|
14
|
-
/** Events are **addressable**, which means that, for each combination of `pubkey`, `kind` and the `d` tag, only the latest event is expected to be stored by relays, older versions are expected to be discarded. */
|
|
15
|
-
static addressable(kind) {
|
|
16
|
-
return 3e4 <= kind && kind < 4e4;
|
|
17
|
-
}
|
|
18
|
-
/** @deprecated Use `NKinds.addressable()` instead. */
|
|
19
|
-
static parameterizedReplaceable(kind) {
|
|
20
|
-
return NKinds.addressable(kind);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
export {
|
|
24
|
-
NKinds
|
|
25
|
-
};
|
package/dist/NPool.js
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import { getFilterLimit } from "nostr-tools";
|
|
2
|
-
import { CircularSet } from "./utils/CircularSet.js";
|
|
3
|
-
import { Machina } from "./utils/Machina.js";
|
|
4
|
-
import { NSet } from "./NSet.js";
|
|
5
|
-
class NPool {
|
|
6
|
-
_relays = /* @__PURE__ */ new Map();
|
|
7
|
-
opts;
|
|
8
|
-
constructor(opts) {
|
|
9
|
-
this.opts = opts;
|
|
10
|
-
}
|
|
11
|
-
/** Get or create a relay instance for the given URL. */
|
|
12
|
-
relay(url) {
|
|
13
|
-
const relay = this._relays.get(url);
|
|
14
|
-
if (relay) {
|
|
15
|
-
return relay;
|
|
16
|
-
} else {
|
|
17
|
-
const relay2 = this.opts.open(url);
|
|
18
|
-
this._relays.set(url, relay2);
|
|
19
|
-
return relay2;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/** Returns a new pool instance that uses the given relays. Connections are shared with the original pool. */
|
|
23
|
-
group(urls) {
|
|
24
|
-
return new NPool({
|
|
25
|
-
open: (url) => this.relay(url),
|
|
26
|
-
reqRouter: (filters) => new Map(urls.map((url) => [url, filters])),
|
|
27
|
-
eventRouter: () => urls
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
get relays() {
|
|
31
|
-
return this._relays;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Sends a `REQ` to relays based on the configured `reqRouter`.
|
|
35
|
-
*
|
|
36
|
-
* `EVENT` messages from the selected relays are yielded.
|
|
37
|
-
* `EOSE` and `CLOSE` messages are only yielded when all relays have emitted them.
|
|
38
|
-
*
|
|
39
|
-
* Deduplication of `EVENT` messages is attempted, so that each event is only yielded once.
|
|
40
|
-
* A circular set of 1000 is used to track seen event IDs, so it's possible that very
|
|
41
|
-
* long-running subscriptions (with over 1000 results) may yield duplicate events.
|
|
42
|
-
*/
|
|
43
|
-
async *req(filters, opts) {
|
|
44
|
-
const controller = new AbortController();
|
|
45
|
-
const signal = opts?.signal ? AbortSignal.any([opts.signal, controller.signal]) : controller.signal;
|
|
46
|
-
const routes = opts?.relays ? new Map(opts.relays.map((url) => [url, filters])) : await this.opts.reqRouter(filters);
|
|
47
|
-
if (routes.size < 1) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const machina = new Machina(signal);
|
|
51
|
-
const eoses = /* @__PURE__ */ new Set();
|
|
52
|
-
const closes = /* @__PURE__ */ new Set();
|
|
53
|
-
const events = new CircularSet(1e3);
|
|
54
|
-
const relayPromises = [];
|
|
55
|
-
for (const [url, filters2] of routes.entries()) {
|
|
56
|
-
const relay = this.relay(url);
|
|
57
|
-
const relayPromise = (async () => {
|
|
58
|
-
try {
|
|
59
|
-
for await (const msg of relay.req(filters2, { signal })) {
|
|
60
|
-
if (msg[0] === "EOSE") {
|
|
61
|
-
eoses.add(url);
|
|
62
|
-
if (eoses.size === routes.size) {
|
|
63
|
-
machina.push(msg);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (msg[0] === "CLOSED") {
|
|
67
|
-
closes.add(url);
|
|
68
|
-
if (closes.size === routes.size) {
|
|
69
|
-
machina.push(msg);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (msg[0] === "EVENT") {
|
|
73
|
-
const [, , event] = msg;
|
|
74
|
-
if (!events.has(event.id)) {
|
|
75
|
-
events.add(event.id);
|
|
76
|
-
machina.push(msg);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
} catch {
|
|
81
|
-
}
|
|
82
|
-
})();
|
|
83
|
-
relayPromises.push(relayPromise);
|
|
84
|
-
}
|
|
85
|
-
try {
|
|
86
|
-
for await (const msg of machina) {
|
|
87
|
-
yield msg;
|
|
88
|
-
}
|
|
89
|
-
} finally {
|
|
90
|
-
controller.abort();
|
|
91
|
-
await Promise.allSettled(relayPromises);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Events are sent to relays according to the `eventRouter`.
|
|
96
|
-
* Returns a fulfilled promise if ANY relay accepted the event,
|
|
97
|
-
* or a rejected promise if ALL relays rejected or failed to publish the event.
|
|
98
|
-
*/
|
|
99
|
-
async event(event, opts) {
|
|
100
|
-
const relayUrls = opts?.relays ?? await this.opts.eventRouter(event);
|
|
101
|
-
if (!relayUrls.length) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
await Promise.any(
|
|
105
|
-
relayUrls.map((url) => this.relay(url).event(event, opts))
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* This method calls `.req` internally and then post-processes the results.
|
|
110
|
-
* Please read the definition of `.req`.
|
|
111
|
-
*
|
|
112
|
-
* - The strategy is to seek regular events quickly, and to wait to find the latest versions of replaceable events.
|
|
113
|
-
* - Filters for replaceable events will wait for all relays to `EOSE` (or `CLOSE`, or for the signal to be aborted) to ensure the latest event versions are retrieved.
|
|
114
|
-
* - Filters for regular events will stop as soon as the filters are fulfilled.
|
|
115
|
-
* - Events are deduplicated, sorted, and only the latest version of replaceable events is kept.
|
|
116
|
-
* - If the signal is aborted, this method will return partial results instead of throwing.
|
|
117
|
-
*
|
|
118
|
-
* To implement a custom strategy, call `.req` directly.
|
|
119
|
-
*/
|
|
120
|
-
async query(filters, opts) {
|
|
121
|
-
const map = /* @__PURE__ */ new Map();
|
|
122
|
-
const events = new NSet(map);
|
|
123
|
-
const limit = filters.reduce(
|
|
124
|
-
(result, filter) => result + getFilterLimit(filter),
|
|
125
|
-
0
|
|
126
|
-
);
|
|
127
|
-
if (limit === 0) return [];
|
|
128
|
-
try {
|
|
129
|
-
for await (const msg of this.req(filters, opts)) {
|
|
130
|
-
if (msg[0] === "EOSE") break;
|
|
131
|
-
if (msg[0] === "EVENT") events.add(msg[2]);
|
|
132
|
-
if (msg[0] === "CLOSED") break;
|
|
133
|
-
}
|
|
134
|
-
} catch {
|
|
135
|
-
}
|
|
136
|
-
if (filters.some((filter) => typeof filter.search === "string")) {
|
|
137
|
-
return [...map.values()];
|
|
138
|
-
} else {
|
|
139
|
-
return [...events];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/** Close all the relays in the pool. */
|
|
143
|
-
async close() {
|
|
144
|
-
await Promise.all(
|
|
145
|
-
[...this._relays.values()].map((relay) => relay.close())
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
async [Symbol.asyncDispose]() {
|
|
149
|
-
await this.close();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
export {
|
|
153
|
-
NPool
|
|
154
|
-
};
|