applesauce-relay 0.0.0-next-20250916134023 → 0.0.0-next-20250918142212
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/group.d.ts +2 -2
- package/dist/pool.d.ts +5 -1
- package/dist/pool.js +20 -1
- package/dist/relay.d.ts +6 -0
- package/dist/relay.js +18 -0
- package/dist/types.d.ts +9 -1
- package/package.json +3 -3
package/dist/group.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { type NostrEvent } from "nostr-tools";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
|
-
import { IEventStoreActions } from "applesauce-core";
|
|
3
|
+
import { IAsyncEventStoreActions, IEventStoreActions } from "applesauce-core";
|
|
4
4
|
import { FilterInput, IGroup, IRelay, PublishOptions, PublishResponse, RequestOptions, SubscriptionOptions, SubscriptionResponse } from "./types.js";
|
|
5
5
|
export declare class RelayGroup implements IGroup {
|
|
6
6
|
relays: IRelay[];
|
|
7
7
|
constructor(relays: IRelay[]);
|
|
8
8
|
/** Takes an array of observables and only emits EOSE when all observables have emitted EOSE */
|
|
9
|
-
protected mergeEOSE(requests: Observable<SubscriptionResponse>[], eventStore?: IEventStoreActions): Observable<import("nostr-tools").Event | "EOSE">;
|
|
9
|
+
protected mergeEOSE(requests: Observable<SubscriptionResponse>[], eventStore?: IEventStoreActions | IAsyncEventStoreActions): Observable<import("nostr-tools").Event | "EOSE">;
|
|
10
10
|
/**
|
|
11
11
|
* Make a request to all relays
|
|
12
12
|
* @note This does not deduplicate events
|
package/dist/pool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type NostrEvent } from "nostr-tools";
|
|
2
|
-
import { BehaviorSubject, Observable } from "rxjs";
|
|
2
|
+
import { BehaviorSubject, Observable, Subject } from "rxjs";
|
|
3
3
|
import { RelayGroup } from "./group.js";
|
|
4
4
|
import { Relay, RelayOptions } from "./relay.js";
|
|
5
5
|
import { FilterInput, IPool, IRelay, PublishResponse, SubscriptionResponse } from "./types.js";
|
|
@@ -9,6 +9,10 @@ export declare class RelayPool implements IPool {
|
|
|
9
9
|
get groups(): Map<string, RelayGroup>;
|
|
10
10
|
relays$: BehaviorSubject<Map<string, Relay>>;
|
|
11
11
|
get relays(): Map<string, Relay>;
|
|
12
|
+
/** A signal when a relay is added */
|
|
13
|
+
add$: Subject<IRelay>;
|
|
14
|
+
/** A signal when a relay is removed */
|
|
15
|
+
remove$: Subject<IRelay>;
|
|
12
16
|
/** An array of relays to never connect to */
|
|
13
17
|
blacklist: Set<string>;
|
|
14
18
|
constructor(options?: RelayOptions | undefined);
|
package/dist/pool.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BehaviorSubject } from "rxjs";
|
|
1
|
+
import { BehaviorSubject, Subject } from "rxjs";
|
|
2
2
|
import { normalizeURL } from "applesauce-core/helpers";
|
|
3
3
|
import { RelayGroup } from "./group.js";
|
|
4
4
|
import { Relay } from "./relay.js";
|
|
@@ -12,10 +12,27 @@ export class RelayPool {
|
|
|
12
12
|
get relays() {
|
|
13
13
|
return this.relays$.value;
|
|
14
14
|
}
|
|
15
|
+
/** A signal when a relay is added */
|
|
16
|
+
add$ = new Subject();
|
|
17
|
+
/** A signal when a relay is removed */
|
|
18
|
+
remove$ = new Subject();
|
|
15
19
|
/** An array of relays to never connect to */
|
|
16
20
|
blacklist = new Set();
|
|
17
21
|
constructor(options) {
|
|
18
22
|
this.options = options;
|
|
23
|
+
// Listen for relays being added and removed to emit connect / disconnect signals
|
|
24
|
+
// const listeners = new Map<IRelay, Subscription>();
|
|
25
|
+
// this.add$.subscribe((relay) =>
|
|
26
|
+
// listeners.set(
|
|
27
|
+
// relay,
|
|
28
|
+
// relay.connected$.subscribe((conn) => (conn ? this.connect$.next(relay) : this.disconnect$.next(relay))),
|
|
29
|
+
// ),
|
|
30
|
+
// );
|
|
31
|
+
// this.remove$.subscribe((relay) => {
|
|
32
|
+
// const listener = listeners.get(relay);
|
|
33
|
+
// if (listener) listener.unsubscribe();
|
|
34
|
+
// listeners.delete(relay);
|
|
35
|
+
// });
|
|
19
36
|
}
|
|
20
37
|
filterBlacklist(urls) {
|
|
21
38
|
return urls.filter((url) => !this.blacklist.has(url));
|
|
@@ -35,6 +52,7 @@ export class RelayPool {
|
|
|
35
52
|
relay = new Relay(url, this.options);
|
|
36
53
|
this.relays.set(url, relay);
|
|
37
54
|
this.relays$.next(this.relays);
|
|
55
|
+
this.add$.next(relay);
|
|
38
56
|
return relay;
|
|
39
57
|
}
|
|
40
58
|
/** Create a group of relays */
|
|
@@ -68,6 +86,7 @@ export class RelayPool {
|
|
|
68
86
|
instance?.close();
|
|
69
87
|
this.relays.delete(instance.url);
|
|
70
88
|
this.relays$.next(this.relays);
|
|
89
|
+
this.remove$.next(instance);
|
|
71
90
|
}
|
|
72
91
|
/** Make a REQ to multiple relays that does not deduplicate events */
|
|
73
92
|
req(relays, filters, id) {
|
package/dist/relay.d.ts
CHANGED
|
@@ -14,6 +14,8 @@ export type RelayOptions = {
|
|
|
14
14
|
eoseTimeout?: number;
|
|
15
15
|
/** How long to wait for an OK message from the relay (default 10s) */
|
|
16
16
|
eventTimeout?: number;
|
|
17
|
+
/** How long to wait for a publish to complete (default 30s) */
|
|
18
|
+
publishTimeout?: number;
|
|
17
19
|
/** How long to keep the connection alive after nothing is subscribed (default 30s) */
|
|
18
20
|
keepAlive?: number;
|
|
19
21
|
};
|
|
@@ -70,6 +72,8 @@ export declare class Relay implements IRelay {
|
|
|
70
72
|
eoseTimeout: number;
|
|
71
73
|
/** How long to wait for an OK message from the relay (default 10s) */
|
|
72
74
|
eventTimeout: number;
|
|
75
|
+
/** How long to wait for a publish to complete (default 30s) */
|
|
76
|
+
publishTimeout: number;
|
|
73
77
|
/** How long to keep the connection alive after nothing is subscribed (default 30s) */
|
|
74
78
|
keepAlive: number;
|
|
75
79
|
protected receivedAuthRequiredForReq: BehaviorSubject<boolean>;
|
|
@@ -101,6 +105,8 @@ export declare class Relay implements IRelay {
|
|
|
101
105
|
protected customRetryOperator<T extends unknown = unknown>(times: undefined | boolean | number | RetryConfig, base?: RetryConfig): MonoTypeOperatorFunction<T>;
|
|
102
106
|
/** Internal operator for creating the repeat() operator */
|
|
103
107
|
protected customRepeatOperator<T extends unknown = unknown>(times: undefined | boolean | number | RepeatConfig | undefined): MonoTypeOperatorFunction<T>;
|
|
108
|
+
/** Internal operator for creating the timeout() operator */
|
|
109
|
+
protected customTimeoutOperator<T extends unknown = unknown>(timeout: undefined | boolean | number, defaultTimeout: number): MonoTypeOperatorFunction<T>;
|
|
104
110
|
/** Creates a REQ that retries when relay errors ( default 3 retries ) */
|
|
105
111
|
subscription(filters: Filter | Filter[], opts?: SubscriptionOptions): Observable<SubscriptionResponse>;
|
|
106
112
|
/** Makes a single request that retires on errors and completes on EOSE */
|
package/dist/relay.js
CHANGED
|
@@ -77,6 +77,8 @@ export class Relay {
|
|
|
77
77
|
eoseTimeout = 10_000;
|
|
78
78
|
/** How long to wait for an OK message from the relay (default 10s) */
|
|
79
79
|
eventTimeout = 10_000;
|
|
80
|
+
/** How long to wait for a publish to complete (default 30s) */
|
|
81
|
+
publishTimeout = 30_000;
|
|
80
82
|
/** How long to keep the connection alive after nothing is subscribed (default 30s) */
|
|
81
83
|
keepAlive = 30_000;
|
|
82
84
|
// Subjects that track if an "auth-required" message has been received for REQ or EVENT
|
|
@@ -108,6 +110,8 @@ export class Relay {
|
|
|
108
110
|
this.eoseTimeout = opts.eoseTimeout;
|
|
109
111
|
if (opts?.eventTimeout !== undefined)
|
|
110
112
|
this.eventTimeout = opts.eventTimeout;
|
|
113
|
+
if (opts?.publishTimeout !== undefined)
|
|
114
|
+
this.publishTimeout = opts.publishTimeout;
|
|
111
115
|
if (opts?.keepAlive !== undefined)
|
|
112
116
|
this.keepAlive = opts.keepAlive;
|
|
113
117
|
// Create an observable that tracks boolean authentication state
|
|
@@ -378,6 +382,18 @@ export class Relay {
|
|
|
378
382
|
else
|
|
379
383
|
return repeat(times);
|
|
380
384
|
}
|
|
385
|
+
/** Internal operator for creating the timeout() operator */
|
|
386
|
+
customTimeoutOperator(timeout, defaultTimeout) {
|
|
387
|
+
// Do nothing if disabled
|
|
388
|
+
if (timeout === false)
|
|
389
|
+
return identity;
|
|
390
|
+
// If true default to 30 seconds
|
|
391
|
+
else if (timeout === true)
|
|
392
|
+
return simpleTimeout(defaultTimeout);
|
|
393
|
+
// Otherwise use the timeout value or default to 30 seconds
|
|
394
|
+
else
|
|
395
|
+
return simpleTimeout(timeout ?? defaultTimeout);
|
|
396
|
+
}
|
|
381
397
|
/** Creates a REQ that retries when relay errors ( default 3 retries ) */
|
|
382
398
|
subscription(filters, opts) {
|
|
383
399
|
return this.req(filters, opts?.id).pipe(
|
|
@@ -410,6 +426,8 @@ export class Relay {
|
|
|
410
426
|
}),
|
|
411
427
|
// Retry the publish until it succeeds or the number of retries is reached
|
|
412
428
|
this.customRetryOperator(opts?.retries ?? opts?.reconnect ?? true, DEFAULT_RETRY_CONFIG),
|
|
429
|
+
// Add timeout for publishing
|
|
430
|
+
this.customTimeoutOperator(opts?.timeout, this.publishTimeout),
|
|
413
431
|
// Single subscription
|
|
414
432
|
share()));
|
|
415
433
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -21,6 +21,8 @@ export type PublishOptions = {
|
|
|
21
21
|
* @see https://rxjs.dev/api/index/function/retry
|
|
22
22
|
*/
|
|
23
23
|
reconnect?: boolean | number | Parameters<typeof retry>[0];
|
|
24
|
+
/** Timeout for publish in milliseconds (default 30 seconds) */
|
|
25
|
+
timeout?: number | boolean;
|
|
24
26
|
};
|
|
25
27
|
/** Options for the request method on the pool and relay */
|
|
26
28
|
export type RequestOptions = SubscriptionOptions;
|
|
@@ -58,6 +60,7 @@ export interface IRelay extends MultiplexWebSocket {
|
|
|
58
60
|
challenge$: Observable<string | null>;
|
|
59
61
|
authenticated$: Observable<boolean>;
|
|
60
62
|
notices$: Observable<string[]>;
|
|
63
|
+
error$: Observable<Error | null>;
|
|
61
64
|
readonly connected: boolean;
|
|
62
65
|
readonly authenticated: boolean;
|
|
63
66
|
readonly challenge: string | null;
|
|
@@ -91,7 +94,12 @@ export interface IGroup {
|
|
|
91
94
|
/** Open a subscription with retries */
|
|
92
95
|
subscription(filters: FilterInput, opts?: SubscriptionOptions): Observable<SubscriptionResponse>;
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
/** Signals emitted by the pool */
|
|
98
|
+
export interface IPoolSignals {
|
|
99
|
+
add$: Observable<IRelay>;
|
|
100
|
+
remove$: Observable<IRelay>;
|
|
101
|
+
}
|
|
102
|
+
export interface IPool extends IPoolSignals {
|
|
95
103
|
/** Get or create a relay */
|
|
96
104
|
relay(url: string): IRelay;
|
|
97
105
|
/** Create a relay group */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-relay",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20250918142212",
|
|
4
4
|
"description": "nostr relay communication framework built on rxjs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -52,14 +52,14 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@noble/hashes": "^1.7.1",
|
|
55
|
-
"applesauce-core": "0.0.0-next-
|
|
55
|
+
"applesauce-core": "0.0.0-next-20250918142212",
|
|
56
56
|
"nanoid": "^5.0.9",
|
|
57
57
|
"nostr-tools": "~2.15",
|
|
58
58
|
"rxjs": "^7.8.1"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@hirez_io/observer-spy": "^2.2.0",
|
|
62
|
-
"applesauce-signers": "0.0.0-next-
|
|
62
|
+
"applesauce-signers": "0.0.0-next-20250918142212",
|
|
63
63
|
"typescript": "^5.7.3",
|
|
64
64
|
"vitest": "^3.2.4",
|
|
65
65
|
"vitest-websocket-mock": "^0.5.0"
|