Skip to content

Commit

Permalink
refactor: processStop
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Jun 19, 2024
1 parent 6463fc6 commit 12097e7
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions lib/fast-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export class FastTracker implements Tracker {
const peerId = json.peer_id as string;
let swarm: unknown = undefined;

if (peer.peerIdsOnSocket === undefined) {
peer.peerIdsOnSocket = new Set([peerId]);
}
peer.peerIdsOnSocket.add(peerId);

Check failure on line 142 in lib/fast-tracker.ts

View workflow job for this annotation

GitHub Actions / build-pr

test/announce.test.ts > announce > should send offers to peers in a swarm

TypeError: Cannot read properties of null (reading 'add') ❯ FastTracker.processAnnounce lib/fast-tracker.ts:142:26 ❯ FastTracker.processMessage lib/fast-tracker.ts:66:14 ❯ test/announce.test.ts:181:13

Check failure on line 142 in lib/fast-tracker.ts

View workflow job for this annotation

GitHub Actions / build-pr

test/announce.test.ts > announce > should process answer messages

TypeError: Cannot read properties of null (reading 'add') ❯ FastTracker.processAnnounce lib/fast-tracker.ts:142:26 ❯ FastTracker.processMessage lib/fast-tracker.ts:66:14 ❯ test/announce.test.ts:464:13

const peerContext: PeerContext = {
id: peerId,
sendMessage: peer.sendMessage,
Expand All @@ -144,11 +149,6 @@ export class FastTracker implements Tracker {

this.#peersContext.set(peerId, peerContext);

if (peer.peerIdsOnSocket === undefined) {
peer.peerIdsOnSocket = new Set([peerId]);
}
peer.peerIdsOnSocket.add(peerId);

swarm = (peer as unknown as UnknownObject)[infoHash as string];

const isPeerCompleted = completed || json.left === 0;
Expand Down Expand Up @@ -306,7 +306,6 @@ export class FastTracker implements Tracker {

private processAnswer(json: UnknownObject): void {
const toPeerId = json.to_peer_id as string;
// const toPeer = this.#peers.get(toPeerId);
const toPeer = this.#peersContext.get(toPeerId);
if (toPeer === undefined) {
throw new TrackerError("answer: to_peer_id is not in the swarm");
Expand All @@ -324,38 +323,43 @@ export class FastTracker implements Tracker {
);
}
}
// TODO: Implement the processStop method
private processStop(json: UnknownObject, peer: SocketContext): void {
// needs debug
// const peerIdToClose = json.peer_id as string;
const infoHash = json.info_hash;
const swarm = (peer as unknown as UnknownObject)[infoHash as string];

private processStop(json: UnknownObject, peerSocket: SocketContext): void {
const peerId = json.peer_id as string;
const infoHash = json.info_hash as string;

const peer = this.#peersContext.get(peerId);
if (peer === undefined) return;

const swarm = (peerSocket as unknown as UnknownObject)[infoHash];

if (!(swarm instanceof Swarm)) {
debug("stop event: peer not in the swarm");
return;
throw new TrackerError("stop: peer is not in the swarm");
}

swarm.removePeer(peer);
delete (peerSocket as unknown as UnknownObject)[infoHash];

if (debugEnabled) {
debug(
"stop event: peer",
Buffer.from("peer.id").toString("hex"),
"stop: peer",
Buffer.from(peerId).toString("hex"),
"removed from swarm",
Buffer.from(infoHash as string).toString("hex"),
Buffer.from(infoHash).toString("hex"),
);
}
// swarm.removePeer(peer);
delete (peer as unknown as UnknownObject)[infoHash as string];

if (swarm.peers.length === 0) {
if (debugEnabled) {
debug(
"stop event: swarm removed (empty)",
Buffer.from(infoHash as string).toString("hex"),
"stop: swarm removed (empty)",
Buffer.from(swarm.infoHash).toString("hex"),
);
}
this.#swarms.delete(infoHash as string);
this.#swarms.delete(swarm.infoHash);
}

this.#peersContext.delete(peerId);
}

private processScrape(json: UnknownObject, peer: SocketContext): void {
Expand Down

0 comments on commit 12097e7

Please sign in to comment.