Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Feb 16, 2024
1 parent 0dd090d commit f550494
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
sourceType: "module",
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
Expand All @@ -16,5 +17,6 @@ module.exports = {
"@typescript-eslint/prefer-nullish-coalescing": "error",
"spaced-comment": ["warn", "always", { markers: ["/"] }],
curly: ["warn", "multi-line", "consistent"],
"object-shorthand": ["error", "always"],
},
};
2 changes: 1 addition & 1 deletion lib/fast-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class FastTracker implements Tracker {
}
}

peer.sendMessage({ action: "scrape", files: files }, peer);
peer.sendMessage({ action: "scrape", files }, peer);
}
}

Expand Down
31 changes: 12 additions & 19 deletions lib/run-uws-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { HttpResponse, HttpRequest } from "uWebSockets.js";
import Debug from "debug";
import { UWebSocketsTracker } from "./uws-tracker.js";
import { FastTracker } from "./fast-tracker.js";
import { PeerContext, Tracker } from "./tracker.js";
import { Tracker } from "./tracker.js";

const debugRequests = Debug("wt-tracker:uws-tracker-requests");
const debugRequestsEnabled = debugRequests.enabled;
Expand Down Expand Up @@ -167,7 +167,7 @@ function validateSettings(jsonSettings: UnknownObject): Settings | undefined {
}

return {
servers: servers,
servers,
tracker: jsonSettings.tracker,
websocketsAccess: jsonSettings.websocketsAccess,
};
Expand All @@ -188,11 +188,11 @@ async function runServers(tracker: Tracker, settings: Settings): Promise<void> {

const serverPromises = settings.servers.map(async (serverSettings) => {
const server = buildServer({
tracker: tracker,
serverSettings: serverSettings,
tracker,
serverSettings,
websocketsAccess: settings.websocketsAccess,
indexHtml: indexHtml,
servers: servers,
indexHtml,
servers,
});
servers.push(server);
await server.run();
Expand All @@ -204,11 +204,6 @@ async function runServers(tracker: Tracker, settings: Settings): Promise<void> {
await Promise.all(serverPromises);
}

interface ExtendedSwarm {
peers: readonly PeerContext[];
infoHash: string;
}

function buildServer({
tracker,
serverSettings,
Expand Down Expand Up @@ -242,16 +237,14 @@ function buildServer({
debugRequest(server, request);

const swarms = tracker.swarms;
const peerCountPerInfoHash: Record<string, number> = {};
const peersCountPerInfoHash: Record<string, number> = {};

let peersCount = 0;
for (const swarm of swarms.values() as unknown as ExtendedSwarm[]) {
for (const [infoHash, swarm] of swarms) {
peersCount += swarm.peers.length;

const infoHashHex = Buffer.from(swarm.infoHash, "binary").toString(
"hex",
);
peerCountPerInfoHash[infoHashHex] = peersCount;
const infoHashHex = Buffer.from(infoHash, "binary").toString("hex");
peersCountPerInfoHash[infoHashHex] = peersCount;
}

const serversStats = new Array<{
Expand All @@ -269,10 +262,10 @@ function buildServer({
response.writeHeader("Content-Type", "application/json").end(
JSON.stringify({
torrentsCount: swarms.size,
peersCount: peersCount,
peersCount,
servers: serversStats,
memory: process.memoryUsage(),
peerCountPerInfoHash,
peersCountPerInfoHash,
}),
);
})
Expand Down

0 comments on commit f550494

Please sign in to comment.