@contextvm/sdk 0.1.30 → 0.1.31
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/package.json
CHANGED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { NostrEvent, Filter } from 'nostr-tools';
|
|
2
|
-
import { RelayHandler } from '../core/interfaces.js';
|
|
3
|
-
/**
|
|
4
|
-
* A RelayHandler implementation that uses the nostrify library's NPool to manage connections and subscriptions.
|
|
5
|
-
*/
|
|
6
|
-
export declare class NostrifyRelayPool implements RelayHandler {
|
|
7
|
-
private readonly pool;
|
|
8
|
-
private readonly relayUrls;
|
|
9
|
-
private subscriptions;
|
|
10
|
-
/**
|
|
11
|
-
* Creates a new NostrifyRelayPool instance.
|
|
12
|
-
* @param relayUrls - An array of relay URLs to connect to.
|
|
13
|
-
*/
|
|
14
|
-
constructor(relayUrls: string[]);
|
|
15
|
-
connect(): Promise<void>;
|
|
16
|
-
disconnect(): Promise<void>;
|
|
17
|
-
publish(event: NostrEvent): Promise<void>;
|
|
18
|
-
/**
|
|
19
|
-
* Creates a simplified subscription wrapper around the NPool's req method.
|
|
20
|
-
* This provides a cleaner interface similar to SimplePool's subscribeMany.
|
|
21
|
-
*/
|
|
22
|
-
private createSubscription;
|
|
23
|
-
subscribe(filters: Filter[], onEvent: (event: NostrEvent) => void, onEose?: () => void): Promise<void>;
|
|
24
|
-
unsubscribe(): void;
|
|
25
|
-
}
|
|
26
|
-
//# sourceMappingURL=nostrify-relay-pool.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nostrify-relay-pool.d.ts","sourceRoot":"","sources":["../../../src/relay/nostrify-relay-pool.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAKrD;;GAEG;AACH,qBAAa,iBAAkB,YAAW,YAAY;IACpD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IAGrC,OAAO,CAAC,aAAa,CAKb;IAER;;;OAGG;gBACS,SAAS,EAAE,MAAM,EAAE;IAkBzB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAkBxB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAY/C;;;OAGG;IACH,OAAO,CAAC,kBAAkB;IA4CpB,SAAS,CACb,OAAO,EAAE,MAAM,EAAE,EACjB,OAAO,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,EACpC,MAAM,CAAC,EAAE,MAAM,IAAI,GAClB,OAAO,CAAC,IAAI,CAAC;IAehB,WAAW,IAAI,IAAI;CAUpB"}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { NPool, NRelay1 } from '@nostrify/nostrify';
|
|
2
|
-
import { createLogger } from '../core/utils/logger.js';
|
|
3
|
-
const logger = createLogger('nostrify-relay');
|
|
4
|
-
/**
|
|
5
|
-
* A RelayHandler implementation that uses the nostrify library's NPool to manage connections and subscriptions.
|
|
6
|
-
*/
|
|
7
|
-
export class NostrifyRelayPool {
|
|
8
|
-
/**
|
|
9
|
-
* Creates a new NostrifyRelayPool instance.
|
|
10
|
-
* @param relayUrls - An array of relay URLs to connect to.
|
|
11
|
-
*/
|
|
12
|
-
constructor(relayUrls) {
|
|
13
|
-
// Subscription management similar to SimpleRelayPool
|
|
14
|
-
this.subscriptions = [];
|
|
15
|
-
this.relayUrls = relayUrls;
|
|
16
|
-
this.pool = new NPool({
|
|
17
|
-
open: (url) => new NRelay1(url),
|
|
18
|
-
reqRouter: (filters) => {
|
|
19
|
-
const relayMap = new Map();
|
|
20
|
-
relayUrls.forEach((url) => {
|
|
21
|
-
relayMap.set(url, filters);
|
|
22
|
-
});
|
|
23
|
-
return relayMap;
|
|
24
|
-
},
|
|
25
|
-
eventRouter: () => {
|
|
26
|
-
return relayUrls;
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
async connect() {
|
|
31
|
-
logger.info('Connecting to relays', { relayUrls: this.relayUrls });
|
|
32
|
-
// The NPool automatically connects to relays when needed
|
|
33
|
-
// We don't need to explicitly connect here, but we could validate the relay URLs
|
|
34
|
-
for (const url of this.relayUrls) {
|
|
35
|
-
try {
|
|
36
|
-
// Validate URL format
|
|
37
|
-
new URL(url);
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
logger.error('Invalid relay URL', { url, error });
|
|
41
|
-
throw new Error(`Invalid relay URL: ${url}`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
logger.info('Relay pool initialized', { relayUrls: this.relayUrls });
|
|
45
|
-
}
|
|
46
|
-
async disconnect() {
|
|
47
|
-
this.pool.close();
|
|
48
|
-
}
|
|
49
|
-
async publish(event) {
|
|
50
|
-
logger.debug('Publishing event', { eventId: event.id, kind: event.kind });
|
|
51
|
-
try {
|
|
52
|
-
await this.pool.event(event);
|
|
53
|
-
logger.debug('Event published successfully', { eventId: event.id });
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
logger.error('Failed to publish event', { eventId: event.id, error });
|
|
57
|
-
throw error;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Creates a simplified subscription wrapper around the NPool's req method.
|
|
62
|
-
* This provides a cleaner interface similar to SimplePool's subscribeMany.
|
|
63
|
-
*/
|
|
64
|
-
createSubscription(filters, onEvent, onEose) {
|
|
65
|
-
const abortController = new AbortController();
|
|
66
|
-
// Start the subscription in the background
|
|
67
|
-
(async () => {
|
|
68
|
-
try {
|
|
69
|
-
const messageStream = this.pool.req(filters, {
|
|
70
|
-
signal: abortController.signal,
|
|
71
|
-
});
|
|
72
|
-
let eoseReceived = false;
|
|
73
|
-
for await (const message of messageStream) {
|
|
74
|
-
if (abortController.signal.aborted)
|
|
75
|
-
break;
|
|
76
|
-
if (message[0] === 'EVENT') {
|
|
77
|
-
const event = message[2];
|
|
78
|
-
onEvent(event);
|
|
79
|
-
}
|
|
80
|
-
else if (message[0] === 'EOSE' && !eoseReceived && onEose) {
|
|
81
|
-
eoseReceived = true;
|
|
82
|
-
onEose();
|
|
83
|
-
}
|
|
84
|
-
else if (message[0] === 'CLOSED') {
|
|
85
|
-
// Subscription was closed by the relay
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
|
92
|
-
logger.debug('Subscription aborted');
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
logger.error('Subscription error', { error });
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
})();
|
|
99
|
-
return {
|
|
100
|
-
close: () => abortController.abort(),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
async subscribe(filters, onEvent, onEose) {
|
|
104
|
-
logger.debug('Creating subscription', { filters });
|
|
105
|
-
// Create a subscription using our simplified wrapper
|
|
106
|
-
const closer = this.createSubscription(filters, onEvent, onEose);
|
|
107
|
-
// Store the subscription for cleanup
|
|
108
|
-
this.subscriptions.push({
|
|
109
|
-
filters,
|
|
110
|
-
onEvent,
|
|
111
|
-
onEose,
|
|
112
|
-
closer,
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
unsubscribe() {
|
|
116
|
-
logger.debug('Unsubscribing from all subscriptions');
|
|
117
|
-
// Close all active subscriptions
|
|
118
|
-
for (const subscription of this.subscriptions) {
|
|
119
|
-
subscription.closer.close();
|
|
120
|
-
}
|
|
121
|
-
this.subscriptions = [];
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
//# sourceMappingURL=nostrify-relay-pool.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
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