Work on new net

This commit is contained in:
Jon Staab
2025-02-20 16:34:43 -08:00
parent d8bbb5dd6a
commit 7706034b99
7 changed files with 168 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
import type {WebSocketSubject} from "rxjs/websocket"
import {Subject} from "rxjs"
import type {SignedEvent} from "@welshman/util"
import {createEvent, CLIENT_AUTH} from "@welshman/util"
import type {SocketResponse} from "./socket.js"
export const createAuthEvent = (url: string, challenge: string) =>
createEvent(CLIENT_AUTH, {
tags: [
["relay", url],
["challenge", challenge],
],
})
export type AuthResult = {
ok: boolean
reason?: string
}
export const authenticate = (socket: WebSocketSubject<SocketResponse>, event: SignedEvent) => {
const subject = new Subject<AuthResult>()
socket.next(["AUTH", event])
socket.subscribe(message => {
if (message[0] === "OK") {
const [id, ok = false, reason = ""] = message.slice(1)
if (id === event.id) {
subject.next({ok, reason})
}
}
})
return subject
}
export const forceAuth = <T>(socket: WebSocketSubject<T>) => {}
+10
View File
@@ -0,0 +1,10 @@
import {webSocket} from "rxjs/websocket"
import type {SignedEvent} from "@welshman/util"
export type SocketResponse =
| ["AUTH", string]
| ["EVENT", string, SignedEvent]
| ["EOSE", string, SignedEvent]
| ["OK", string, boolean, string]
export const connect = (url: string) => webSocket<SocketResponse>(url)