fix: nip42

This commit is contained in:
Kieran 2023-04-18 21:29:38 +01:00
parent 28383b8464
commit f4244f881e
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 27 additions and 10 deletions

View File

@ -64,7 +64,7 @@ export default function Layout() {
useEffect(() => { useEffect(() => {
if (pub) { if (pub) {
System.HandleAuth = pub.nip42Auth; System.HandleAuth = pub.nip42Auth.bind(pub);
} }
}, [pub]); }, [pub]);

View File

@ -62,7 +62,7 @@ export class Connection {
OnEose?: (sub: string) => void; OnEose?: (sub: string) => void;
Auth?: AuthHandler; Auth?: AuthHandler;
AwaitingAuth: Map<string, boolean>; AwaitingAuth: Map<string, boolean>;
Authed: boolean; Authed = false;
Ephemeral: boolean; Ephemeral: boolean;
EphemeralTimeout: ReturnType<typeof setTimeout> | undefined; EphemeralTimeout: ReturnType<typeof setTimeout> | undefined;
Down = true; Down = true;
@ -90,7 +90,6 @@ export class Connection {
this.ReconnectTimer = null; this.ReconnectTimer = null;
this.EventsCallback = new Map(); this.EventsCallback = new Map();
this.AwaitingAuth = new Map(); this.AwaitingAuth = new Map();
this.Authed = false;
this.Auth = auth; this.Auth = auth;
this.Ephemeral = ephemeral; this.Ephemeral = ephemeral;
@ -189,7 +188,9 @@ export class Connection {
const tag = msg[0]; const tag = msg[0];
switch (tag) { switch (tag) {
case "AUTH": { case "AUTH": {
this._OnAuthAsync(msg[1]); this._OnAuthAsync(msg[1])
.then(() => this.#sendPendingRaw())
.catch(console.error);
this.Stats.EventsReceived++; this.Stats.EventsReceived++;
this.#UpdateState(); this.#UpdateState();
break; break;
@ -354,7 +355,7 @@ export class Connection {
this.CurrentState.avgLatency = this.CurrentState.avgLatency =
this.Stats.Latency.length > 0 this.Stats.Latency.length > 0
? this.Stats.Latency.reduce((acc, v) => acc + v, 0) / ? this.Stats.Latency.reduce((acc, v) => acc + v, 0) /
this.Stats.Latency.length this.Stats.Latency.length
: 0; : 0;
this.CurrentState.disconnects = this.Stats.Disconnects; this.CurrentState.disconnects = this.Stats.Disconnects;
this.CurrentState.info = this.Info; this.CurrentState.info = this.Info;
@ -376,11 +377,29 @@ export class Connection {
} }
#SendJson(obj: object) { #SendJson(obj: object) {
const authPending = !this.Authed && this.AwaitingAuth.size > 0; const authPending = !this.Authed && (this.AwaitingAuth.size > 0 || this.Info?.limitation?.auth_required === true);
if (this.Socket?.readyState !== WebSocket.OPEN || authPending) { if (this.Socket?.readyState !== WebSocket.OPEN || authPending) {
this.PendingRaw.push(obj); this.PendingRaw.push(obj);
return; return;
} }
this.#sendPendingRaw();
this.#sendOnWire(obj);
}
#sendPendingRaw() {
while (this.PendingRaw.length > 0) {
const next = this.PendingRaw.shift();
if (next) {
this.#sendOnWire(next);
}
}
}
#sendOnWire(obj: unknown) {
if (this.Socket?.readyState !== WebSocket.OPEN) {
throw new Error("Socket is not open");
}
const json = JSON.stringify(obj); const json = JSON.stringify(obj);
this.Socket.send(json); this.Socket.send(json);
} }
@ -414,10 +433,7 @@ export class Connection {
resolve(); resolve();
}); });
const req = ["AUTH", authEvent]; this.#sendOnWire(["AUTH", authEvent]);
this.#SendJson(req);
this.Stats.EventsSent++;
this.#UpdateState();
}); });
} }

View File

@ -11,5 +11,6 @@ export interface RelayInfo {
max_subscriptions: number; max_subscriptions: number;
max_filters: number; max_filters: number;
max_event_tags: number; max_event_tags: number;
auth_required: boolean
}; };
} }