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(() => {
if (pub) {
System.HandleAuth = pub.nip42Auth;
System.HandleAuth = pub.nip42Auth.bind(pub);
}
}, [pub]);

View File

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

View File

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