fix: upgrade existing connection to non-ephemeral
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
32c80ed1c5
commit
d4bf929e60
@ -1,7 +1,7 @@
|
|||||||
import { v4 as uuid } from "uuid";
|
import { v4 as uuid } from "uuid";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
import WebSocket from "isomorphic-ws";
|
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 { DefaultConnectTimeout } from "./const";
|
||||||
import { ConnectionStats } from "./connection-stats";
|
import { ConnectionStats } from "./connection-stats";
|
||||||
@ -51,6 +51,7 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
|
|||||||
#ephemeralCheck?: ReturnType<typeof setInterval>;
|
#ephemeralCheck?: ReturnType<typeof setInterval>;
|
||||||
#activity: number = unixNowMs();
|
#activity: number = unixNowMs();
|
||||||
#expectAuth = false;
|
#expectAuth = false;
|
||||||
|
#ephemeral: boolean;
|
||||||
|
|
||||||
Id: string;
|
Id: string;
|
||||||
Address: string;
|
Address: string;
|
||||||
@ -78,7 +79,6 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
|
|||||||
Auth?: AuthHandler;
|
Auth?: AuthHandler;
|
||||||
AwaitingAuth: Map<string, boolean>;
|
AwaitingAuth: Map<string, boolean>;
|
||||||
Authed = false;
|
Authed = false;
|
||||||
Ephemeral: boolean;
|
|
||||||
Down = true;
|
Down = true;
|
||||||
|
|
||||||
constructor(addr: string, options: RelaySettings, auth?: AuthHandler, ephemeral: boolean = false) {
|
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.EventsCallback = new Map();
|
||||||
this.AwaitingAuth = new Map();
|
this.AwaitingAuth = new Map();
|
||||||
this.Auth = auth;
|
this.Auth = auth;
|
||||||
this.Ephemeral = ephemeral;
|
this.#ephemeral = ephemeral;
|
||||||
this.#log = debug("Connection").extend(addr);
|
this.#log = debug("Connection").extend(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get Ephemeral() {
|
||||||
|
return this.#ephemeral;
|
||||||
|
}
|
||||||
|
|
||||||
|
set Ephemeral(v: boolean) {
|
||||||
|
this.#ephemeral = v;
|
||||||
|
this.#setupEphemeral();
|
||||||
|
}
|
||||||
|
|
||||||
async Connect() {
|
async Connect() {
|
||||||
try {
|
try {
|
||||||
if (this.Info === undefined) {
|
if (this.Info === undefined) {
|
||||||
@ -460,11 +469,11 @@ export class Connection extends ExternalStore<ConnectionStateSnapshot> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#setupEphemeral() {
|
#setupEphemeral() {
|
||||||
if (this.Ephemeral) {
|
|
||||||
if (this.#ephemeralCheck) {
|
if (this.#ephemeralCheck) {
|
||||||
clearInterval(this.#ephemeralCheck);
|
clearInterval(this.#ephemeralCheck);
|
||||||
this.#ephemeralCheck = undefined;
|
this.#ephemeralCheck = undefined;
|
||||||
}
|
}
|
||||||
|
if (this.Ephemeral) {
|
||||||
this.#ephemeralCheck = setInterval(() => {
|
this.#ephemeralCheck = setInterval(() => {
|
||||||
const lastActivity = unixNowMs() - this.#activity;
|
const lastActivity = unixNowMs() - this.#activity;
|
||||||
if (lastActivity > 30_000 && !this.IsClosed) {
|
if (lastActivity > 30_000 && !this.IsClosed) {
|
||||||
|
@ -147,7 +147,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
|
|||||||
async ConnectToRelay(address: string, options: RelaySettings) {
|
async ConnectToRelay(address: string, options: RelaySettings) {
|
||||||
try {
|
try {
|
||||||
const addr = unwrap(sanitizeRelayUrl(address));
|
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));
|
const c = new Connection(addr, options, this.#handleAuth?.bind(this));
|
||||||
this.#sockets.set(addr, c);
|
this.#sockets.set(addr, c);
|
||||||
c.OnEvent = (s, e) => this.#onEvent(s, e);
|
c.OnEvent = (s, e) => this.#onEvent(s, e);
|
||||||
@ -157,7 +158,8 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
|
|||||||
await c.Connect();
|
await c.Connect();
|
||||||
} else {
|
} else {
|
||||||
// update settings if already connected
|
// update settings if already connected
|
||||||
unwrap(this.#sockets.get(addr)).Settings = options;
|
existing.Settings = options;
|
||||||
|
existing.Ephemeral = false;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
Loading…
Reference in New Issue
Block a user