applesauce-relay 0.11.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/LICENSE +21 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/operators/index.d.ts +2 -0
- package/dist/operators/index.js +2 -0
- package/dist/operators/mark-from-relay.d.ts +4 -0
- package/dist/operators/mark-from-relay.js +9 -0
- package/dist/operators/only-events.d.ts +5 -0
- package/dist/operators/only-events.js +5 -0
- package/dist/pool.d.ts +12 -0
- package/dist/pool.js +41 -0
- package/dist/relay.d.ts +35 -0
- package/dist/relay.js +120 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 hzrd149
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { tap } from "rxjs";
|
|
2
|
+
import { addSeenRelay } from "applesauce-core/helpers";
|
|
3
|
+
/** Marks all events as from the relay */
|
|
4
|
+
export function markFromRelay(relay) {
|
|
5
|
+
return (source) => source.pipe(tap((message) => {
|
|
6
|
+
if (typeof message !== "string")
|
|
7
|
+
addSeenRelay(message, relay);
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { OperatorFunction } from "rxjs";
|
|
2
|
+
import { NostrEvent } from "nostr-tools";
|
|
3
|
+
import { SubscriptionResponse } from "../relay.js";
|
|
4
|
+
/** Filter subscription responses and only return the events */
|
|
5
|
+
export declare function onlyEvents(): OperatorFunction<SubscriptionResponse, NostrEvent>;
|
package/dist/pool.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NostrEvent, type Filter } from "nostr-tools";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import { Relay, PublishResponse, SubscriptionResponse } from "./relay.js";
|
|
4
|
+
export declare class RelayPool {
|
|
5
|
+
relays: Map<string, Relay>;
|
|
6
|
+
/** Get or create a new relay connection */
|
|
7
|
+
relay(url: string): Relay;
|
|
8
|
+
/** Make a REQ to multiple relays but does not deduplicate events */
|
|
9
|
+
req(relays: string[], filters: Filter[], id?: string): Observable<SubscriptionResponse>;
|
|
10
|
+
/** Send an EVENT message to multiple relays */
|
|
11
|
+
event(relays: string[], event: NostrEvent): Observable<PublishResponse[]>;
|
|
12
|
+
}
|
package/dist/pool.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { combineLatest, endWith, ignoreElements, merge, takeWhile } from "rxjs";
|
|
2
|
+
import { nanoid } from "nanoid";
|
|
3
|
+
import { Relay } from "./relay.js";
|
|
4
|
+
import { onlyEvents } from "./operators/only-events.js";
|
|
5
|
+
export class RelayPool {
|
|
6
|
+
relays = new Map();
|
|
7
|
+
/** Get or create a new relay connection */
|
|
8
|
+
relay(url) {
|
|
9
|
+
let relay = this.relays.get(url);
|
|
10
|
+
if (relay)
|
|
11
|
+
return relay;
|
|
12
|
+
else {
|
|
13
|
+
relay = new Relay(url);
|
|
14
|
+
this.relays.set(url, relay);
|
|
15
|
+
return relay;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Make a REQ to multiple relays but does not deduplicate events */
|
|
19
|
+
req(relays, filters, id = nanoid()) {
|
|
20
|
+
// create a REQ observable for each relay
|
|
21
|
+
const requests = relays.map((url) => this.relay(url).req(filters, id));
|
|
22
|
+
// create an observable that completes when all relays send EOSE
|
|
23
|
+
const eose = merge(
|
|
24
|
+
// create an array of observables for each relay that completes when EOSE
|
|
25
|
+
...requests.map((o) => o.pipe(
|
|
26
|
+
// complete on EOSE message
|
|
27
|
+
takeWhile((m) => m !== "EOSE")))).pipe(
|
|
28
|
+
// ignore all messages
|
|
29
|
+
ignoreElements(),
|
|
30
|
+
// emit EOSE on complete
|
|
31
|
+
endWith("EOSE"));
|
|
32
|
+
// create a stream that only emits events
|
|
33
|
+
const events = merge(...requests).pipe(onlyEvents());
|
|
34
|
+
// merge events and single EOSE streams
|
|
35
|
+
return merge(events, eose);
|
|
36
|
+
}
|
|
37
|
+
/** Send an EVENT message to multiple relays */
|
|
38
|
+
event(relays, event) {
|
|
39
|
+
return combineLatest(relays.map((url) => this.relay(url).event(event)));
|
|
40
|
+
}
|
|
41
|
+
}
|
package/dist/relay.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { BehaviorSubject, Observable } from "rxjs";
|
|
2
|
+
import { WebSocketSubject, WebSocketSubjectConfig } from "rxjs/webSocket";
|
|
3
|
+
import { type Filter, type NostrEvent } from "nostr-tools";
|
|
4
|
+
import { logger } from "applesauce-core";
|
|
5
|
+
export type SubscriptionResponse = "EOSE" | NostrEvent;
|
|
6
|
+
export type PublishResponse = {
|
|
7
|
+
ok: boolean;
|
|
8
|
+
message?: string;
|
|
9
|
+
from: string;
|
|
10
|
+
};
|
|
11
|
+
export type RelayOptions = {
|
|
12
|
+
WebSocket?: WebSocketSubjectConfig<any>["WebSocketCtor"];
|
|
13
|
+
};
|
|
14
|
+
export declare class Relay {
|
|
15
|
+
url: string;
|
|
16
|
+
log: typeof logger;
|
|
17
|
+
socket$: WebSocketSubject<any>;
|
|
18
|
+
connected$: BehaviorSubject<boolean>;
|
|
19
|
+
challenge$: Observable<string>;
|
|
20
|
+
authenticated$: BehaviorSubject<boolean>;
|
|
21
|
+
notices$: Observable<string>;
|
|
22
|
+
protected authRequiredForReq: BehaviorSubject<boolean>;
|
|
23
|
+
protected authRequiredForPublish: BehaviorSubject<boolean>;
|
|
24
|
+
protected reset(): void;
|
|
25
|
+
constructor(url: string, opts?: RelayOptions);
|
|
26
|
+
protected waitForAuth<T extends unknown = unknown>(requireAuth: Observable<boolean>, observable: Observable<T>): Observable<T>;
|
|
27
|
+
req(filters: Filter[], id?: string): Observable<SubscriptionResponse>;
|
|
28
|
+
/** send an Event message */
|
|
29
|
+
event(event: NostrEvent, verb?: "EVENT" | "AUTH"): Observable<PublishResponse>;
|
|
30
|
+
/** send and AUTH message */
|
|
31
|
+
auth(event: NostrEvent): Observable<{
|
|
32
|
+
ok: boolean;
|
|
33
|
+
message?: string;
|
|
34
|
+
}>;
|
|
35
|
+
}
|
package/dist/relay.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { BehaviorSubject, combineLatest, EMPTY, filter, map, merge, NEVER, of, shareReplay, switchMap, take, takeWhile, tap, timeout, } from "rxjs";
|
|
2
|
+
import { webSocket } from "rxjs/webSocket";
|
|
3
|
+
import { nanoid } from "nanoid";
|
|
4
|
+
import { logger } from "applesauce-core";
|
|
5
|
+
import { markFromRelay } from "./operators/mark-from-relay.js";
|
|
6
|
+
export class Relay {
|
|
7
|
+
url;
|
|
8
|
+
log = logger.extend("Bakery");
|
|
9
|
+
socket$;
|
|
10
|
+
connected$ = new BehaviorSubject(false);
|
|
11
|
+
challenge$;
|
|
12
|
+
authenticated$ = new BehaviorSubject(false);
|
|
13
|
+
notices$;
|
|
14
|
+
authRequiredForReq = new BehaviorSubject(false);
|
|
15
|
+
authRequiredForPublish = new BehaviorSubject(false);
|
|
16
|
+
reset() {
|
|
17
|
+
this.authenticated$.next(false);
|
|
18
|
+
this.authRequiredForReq.next(false);
|
|
19
|
+
this.authRequiredForPublish.next(false);
|
|
20
|
+
}
|
|
21
|
+
constructor(url, opts) {
|
|
22
|
+
this.url = url;
|
|
23
|
+
this.log = this.log.extend(url);
|
|
24
|
+
this.socket$ = webSocket({
|
|
25
|
+
url,
|
|
26
|
+
openObserver: {
|
|
27
|
+
next: () => {
|
|
28
|
+
this.log("Connected");
|
|
29
|
+
this.connected$.next(true);
|
|
30
|
+
this.reset();
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
closeObserver: {
|
|
34
|
+
next: () => {
|
|
35
|
+
this.log("Disconnected");
|
|
36
|
+
this.connected$.next(false);
|
|
37
|
+
this.reset();
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
WebSocketCtor: opts?.WebSocket,
|
|
41
|
+
});
|
|
42
|
+
// create an observable for listening for AUTH
|
|
43
|
+
this.challenge$ = this.socket$.pipe(
|
|
44
|
+
// listen for AUTH messages
|
|
45
|
+
filter((message) => message[0] === "AUTH"),
|
|
46
|
+
// pick the challenge string out
|
|
47
|
+
map((m) => m[1]),
|
|
48
|
+
// cache and share the challenge
|
|
49
|
+
shareReplay(1));
|
|
50
|
+
this.notices$ = this.socket$.pipe(
|
|
51
|
+
// listen for NOTICE messages
|
|
52
|
+
filter((m) => m[0] === "NOTICE"),
|
|
53
|
+
// pick the string out of the message
|
|
54
|
+
map((m) => m[1]));
|
|
55
|
+
}
|
|
56
|
+
waitForAuth(requireAuth, observable) {
|
|
57
|
+
return combineLatest([requireAuth, this.authenticated$]).pipe(
|
|
58
|
+
// return EMPTY if auth is required and not authenticated
|
|
59
|
+
switchMap(([required, authenticated]) => {
|
|
60
|
+
if (required && !authenticated)
|
|
61
|
+
return EMPTY;
|
|
62
|
+
else
|
|
63
|
+
return observable;
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
req(filters, id = nanoid()) {
|
|
67
|
+
return this.waitForAuth(this.authRequiredForReq, this.socket$
|
|
68
|
+
.multiplex(() => ["REQ", id, ...filters], () => ["CLOSE", id], (message) => (message[0] === "EVENT" || message[0] === "CLOSE" || message[0] === "EOSE") && message[1] === id)
|
|
69
|
+
.pipe(
|
|
70
|
+
// listen for CLOSE auth-required
|
|
71
|
+
tap((m) => {
|
|
72
|
+
if (m[0] === "CLOSE" && m[1].startsWith("auth-required") && !this.authRequiredForReq.value) {
|
|
73
|
+
this.authRequiredForReq.next(true);
|
|
74
|
+
}
|
|
75
|
+
}),
|
|
76
|
+
// complete when CLOSE is sent
|
|
77
|
+
takeWhile((m) => m[0] !== "CLOSE"),
|
|
78
|
+
// pick event out of EVENT messages
|
|
79
|
+
map((message) => {
|
|
80
|
+
if (message[0] === "EOSE")
|
|
81
|
+
return "EOSE";
|
|
82
|
+
else
|
|
83
|
+
return message[2];
|
|
84
|
+
}),
|
|
85
|
+
// mark events as from relays
|
|
86
|
+
markFromRelay(this.url),
|
|
87
|
+
// if no events are seen in 10s, emit EOSE
|
|
88
|
+
timeout({
|
|
89
|
+
first: 10_000,
|
|
90
|
+
with: () => merge(of("EOSE"), NEVER),
|
|
91
|
+
})));
|
|
92
|
+
}
|
|
93
|
+
/** send an Event message */
|
|
94
|
+
event(event, verb = "EVENT") {
|
|
95
|
+
const observable = this.socket$
|
|
96
|
+
.multiplex(() => [verb, event], () => void 0, (m) => m[0] === "OK" && m[1] === event.id)
|
|
97
|
+
.pipe(
|
|
98
|
+
// format OK message
|
|
99
|
+
map((m) => ({ ok: m[2], message: m[3], from: this.url })),
|
|
100
|
+
// complete on first value
|
|
101
|
+
take(1),
|
|
102
|
+
// listen for OK auth-required
|
|
103
|
+
tap(({ ok, message }) => {
|
|
104
|
+
if (ok === false && message.startsWith("auth-required") && !this.authRequiredForPublish.value) {
|
|
105
|
+
this.authRequiredForPublish.next(true);
|
|
106
|
+
}
|
|
107
|
+
}));
|
|
108
|
+
// skip wait for auth if verb is AUTH
|
|
109
|
+
if (verb === "AUTH")
|
|
110
|
+
return observable;
|
|
111
|
+
else
|
|
112
|
+
return this.waitForAuth(this.authRequiredForPublish, observable);
|
|
113
|
+
}
|
|
114
|
+
/** send and AUTH message */
|
|
115
|
+
auth(event) {
|
|
116
|
+
return this.event(event, "AUTH").pipe(
|
|
117
|
+
// update authenticated
|
|
118
|
+
tap((result) => this.authenticated$.next(result.ok)));
|
|
119
|
+
}
|
|
120
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "applesauce-relay",
|
|
3
|
+
"version": "0.11.0",
|
|
4
|
+
"description": "A collection of observable based loaders built on rx-nostr",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"nostr",
|
|
10
|
+
"applesauce"
|
|
11
|
+
],
|
|
12
|
+
"author": "hzrd149",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"applesauce"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./operators": {
|
|
25
|
+
"import": "./dist/operators/index.js",
|
|
26
|
+
"require": "./dist/operators/index.js",
|
|
27
|
+
"types": "./dist/operators/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./operators/*": {
|
|
30
|
+
"import": "./dist/operators/*.js",
|
|
31
|
+
"require": "./dist/operators/*.js",
|
|
32
|
+
"types": "./dist/operators/*.d.ts"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"nanoid": "^5.0.9",
|
|
37
|
+
"nostr-tools": "^2.10.4",
|
|
38
|
+
"rxjs": "^7.8.1",
|
|
39
|
+
"applesauce-core": "^0.11.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"typescript": "^5.7.3",
|
|
43
|
+
"vitest": "^3.0.5",
|
|
44
|
+
"vitest-nostr": "^0.4.1",
|
|
45
|
+
"vitest-websocket-mock": "^0.4.0"
|
|
46
|
+
},
|
|
47
|
+
"funding": {
|
|
48
|
+
"type": "lightning",
|
|
49
|
+
"url": "lightning:nostrudel@geyser.fund"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsc",
|
|
53
|
+
"watch:build": "tsc --watch > /dev/null",
|
|
54
|
+
"test": "vitest run --passWithNoTests",
|
|
55
|
+
"watch:test": "vitest"
|
|
56
|
+
}
|
|
57
|
+
}
|