fix: upgrade existing connection to non-ephemeral
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Kieran 2023-11-02 06:46:36 +09:00
parent 32c80ed1c5
commit d4bf929e60
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 20 additions and 9 deletions

View File

@ -1,7 +1,7 @@
import { v4 as uuid } from "uuid";
import debug from "debug";
import WebSocket from "isomorphic-ws";
import { unwrap, ExternalStore, unixNowMs, dedupe } from "@snort/shared";
import { ExternalStore, unixNowMs, dedupe } from "@snort/shared";
import { DefaultConnectTimeout } from "./const";
import { ConnectionStats } from "./connection-stats";
@ -51,6 +51,7 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
#ephemeralCheck?: ReturnType<typeof setInterval>;
#activity: number = unixNowMs();
#expectAuth = false;
#ephemeral: boolean;
Id: string;
Address: string;
@ -78,7 +79,6 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
Auth?: AuthHandler;
AwaitingAuth: Map<string, boolean>;
Authed = false;
Ephemeral: boolean;
Down = true;
constructor(addr: string, options: RelaySettings, auth?: AuthHandler, ephemeral: boolean = false) {
@ -90,10 +90,19 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
this.EventsCallback = new Map();
this.AwaitingAuth = new Map();
this.Auth = auth;
this.Ephemeral = ephemeral;
this.#ephemeral = ephemeral;
this.#log = debug("Connection").extend(addr);
}
get Ephemeral() {
return this.#ephemeral;
}
set Ephemeral(v: boolean) {
this.#ephemeral = v;
this.#setupEphemeral();
}
async Connect() {
try {
if (this.Info === undefined) {
@ -460,11 +469,11 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
}
#setupEphemeral() {
if (this.#ephemeralCheck) {
clearInterval(this.#ephemeralCheck);
this.#ephemeralCheck = undefined;
}
if (this.Ephemeral) {
if (this.#ephemeralCheck) {
clearInterval(this.#ephemeralCheck);
this.#ephemeralCheck = undefined;
}
this.#ephemeralCheck = setInterval(() => {
const lastActivity = unixNowMs() - this.#activity;
if (lastActivity > 30_000 && !this.IsClosed) {

View File

@ -147,7 +147,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
async ConnectToRelay(address: string, options: RelaySettings) {
try {
const addr = unwrap(sanitizeRelayUrl(address));
if (!this.#sockets.has(addr)) {
const existing = this.#sockets.get(addr);
if (!existing) {
const c = new Connection(addr, options, this.#handleAuth?.bind(this));
this.#sockets.set(addr, c);
c.OnEvent = (s, e) => this.#onEvent(s, e);
@ -157,7 +158,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
await c.Connect();
} else {
// update settings if already connected
unwrap(this.#sockets.get(addr)).Settings = options;
existing.Settings = options;
existing.Ephemeral = false;
}
} catch (e) {
console.error(e);