Skip to content

Commit

Permalink
prevent writing to closed websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Jul 20, 2024
1 parent a64ad45 commit 74c5b08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
17 changes: 2 additions & 15 deletions ndk/src/relay/connectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export class NDKRelayConnectivity {

public disconnect(): void {
this._status = NDKRelayStatus.DISCONNECTING;
if (!this.relay.connected) return;

try {
this.relay.close();
} catch (e) {
Expand Down Expand Up @@ -174,21 +176,6 @@ export class NDKRelayConnectivity {
}

private async handleNotice(notice: string) {
// This is a prototype; if the relay seems to be complaining
// remove it from relay set selection for a minute.
if (notice.includes("oo many") || notice.includes("aximum")) {
this.disconnect();

// fixme
setTimeout(() => this.connect(), 2000);
this.debug(this.relay.url, "Relay complaining?", notice);
// this.complaining = true;
// setTimeout(() => {
// this.complaining = false;
// console.log(this.relay.url, 'Reactivate relay');
// }, 60000);
}

this.ndkRelay.emit("notice", notice);
}

Expand Down
2 changes: 1 addition & 1 deletion ndk/src/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class NDKRelay extends EventEmitter<{
* @param filters Filters to execute
*/
public subscribe(subscription: NDKSubscription, filters: NDKFilter[]): void {
this.debug("Subscribing to %o", filters);
// this.debug("Subscribing to %o", filters);
this.subs.subscribe(subscription, filters);
}

Expand Down
12 changes: 9 additions & 3 deletions ndk/src/relay/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class NDKRelaySubscriptions {
groupableId: NDKFilterGroupingId | null,
groupedSubscriptions: NDKGroupedSubscriptions,
mergedFilters: NDKFilter[]
): Subscription {
): Subscription | undefined {
const subscriptions: NDKSubscription[] = [];

for (const { subscription } of groupedSubscriptions) {
Expand Down Expand Up @@ -407,7 +407,13 @@ export class NDKRelaySubscriptions {
// subOptions.skipVerification = true;
// }

const sub = this.conn.relay.subscribe(mergedFilters, subOptions);
let sub: Subscription;
const connected = this.conn.relay.connected;
if (!connected) {
this.debug('was about to send to a disconnected relay', this.conn.relay.url, mergedFilters);
return;
}
sub = this.conn.relay.subscribe(mergedFilters, subOptions);

this.activeSubscriptions.set(sub, groupedSubscriptions);
if (groupableId) {
Expand All @@ -423,7 +429,7 @@ export class NDKRelaySubscriptions {
}
});

this.executeSubscriptionsWhenConnected(groupableId, groupedSubscriptions, mergedFilters);
// this.executeSubscriptionsWhenConnected(groupableId, groupedSubscriptions, mergedFilters);

return sub;
}
Expand Down

0 comments on commit 74c5b08

Please sign in to comment.