Skip to content

Commit

Permalink
Refactor package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlika committed Jun 24, 2024
1 parent 551b04e commit 4c9f68d
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/node_modules
/dist
/lib
/test_dist
/coverage
/.nyc_output
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ npm run build
or

```sh
node dist/run-uws-tracker.js [config.json]
node lib/run-uws-tracker.js [config.json]
```

or
Expand Down
2 changes: 1 addition & 1 deletion bin/wt-tracker
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

import "../dist/run-uws-tracker.js";
import "../lib/run-uws-tracker.js";
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
"license": "Apache-2.0",
"author": "Novage",
"homepage": "https://github.com/Novage/wt-tracker",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": "./src/index.ts",
"types": "./src/index.ts",
"publishConfig": {
"exports": "lib/index.js",
"types": "lib/index.d.ts"
},
"type": "module",
"sideEffects": false,
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -26,11 +31,11 @@
"websockets"
],
"scripts": {
"start": "node ./dist/run-uws-tracker.js",
"start": "node ./lib/run-uws-tracker.js",
"build": "npm run lint && npm run clean && npm run compile",
"compile": "tsc",
"lint": "eslint --ext .ts lib",
"clean": "rimraf dist",
"lint": "eslint --ext .ts src",
"clean": "rimraf lib",
"watch": "tsc --watch",
"test": "vitest",
"test:coverage": "vitest run --coverage",
Expand Down
10 changes: 5 additions & 5 deletions lib/fast-tracker.ts → src/fast-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,29 +158,29 @@ export class FastTracker implements Tracker {
return this.#swarms;
}

public processMessage(jsonObject: object, peer: SocketContext): void {
public processMessage(jsonObject: object, socket: SocketContext): void {
const json = jsonObject as UnknownObject;
const action = json.action;

if (action === "announce") {
const event = json.event;
if (event === undefined) {
if (json.answer === undefined) {
this.processAnnounce(json, peer);
this.processAnnounce(json, socket);
} else {
this.processAnswer(json);
}
} else if (event === "started") {
this.processAnnounce(json, peer);
this.processAnnounce(json, socket);
} else if (event === "stopped") {
this.processStop(json);
} else if (event === "completed") {
this.processAnnounce(json, peer, true);
this.processAnnounce(json, socket, true);
} else {
throw new TrackerError("unknown announce event");
}
} else if (action === "scrape") {
this.processScrape(json, peer);
this.processScrape(json, socket);
} else {
throw new TrackerError("unknown action");
}
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

export type { UWebSocketsTracker } from "./uws-tracker.js";
export type { FastTracker } from "./fast-tracker.js";
export type {
export { UWebSocketsTracker } from "./uws-tracker.js";
export { FastTracker } from "./fast-tracker.js";
export {
Tracker,
SocketContext as PeerContext,
TrackerError,
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions lib/tracker.ts → src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export interface SocketContext {
sendMessage: (json: object, peer: SocketContext) => void;
}

export type Swarm = {
export interface Swarm {
infoHash: string;
completedPeers?: Set<string>;
peers: PeerContext[];
};
}

export interface PeerContext {
peerId: string;
Expand All @@ -35,8 +35,8 @@ export interface PeerContext {
export interface Tracker {
readonly swarms: ReadonlyMap<string, { peers: readonly PeerContext[] }>;
readonly settings: object;
processMessage: (json: object, peer: SocketContext) => void;
disconnectPeersFromSocket: (peer: SocketContext) => void;
processMessage: (json: object, socket: SocketContext) => void;
disconnectPeersFromSocket: (socket: SocketContext) => void;
}

export class TrackerError extends Error {}
File renamed without changes.
12 changes: 1 addition & 11 deletions test/announce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,9 @@
* limitations under the License.
*/

import { FastTracker } from "../lib/fast-tracker.js";
import { PeerContext } from "../lib/tracker.js";
import { FastTracker } from "../src/fast-tracker.js";
import { describe, it, expect } from "vitest";

import {
mock,
instance,
anything,
verify,
capture,
resetCalls,
} from "ts-mockito";

describe("announce", () => {
it("should add peers to swarms on announce", () => {
const tracker = new FastTracker();
Expand Down
12 changes: 6 additions & 6 deletions test/memory/heap-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { FastTracker } from "../../lib/fast-tracker.js";
import { SocketContext } from "../../lib/tracker.js";
import { FastTracker } from "../../src/fast-tracker.js";
import { SocketContext } from "../../src/tracker.js";

const peersCount = 100000;
const swarmsCount = 1000000000;
Expand Down Expand Up @@ -47,7 +47,7 @@ console.log(
);
console.log("\nadding peers to swarms");

const peers: SocketContext[] = [];
const sockets: SocketContext[] = [];
for (let p = 0; p < peersCount; p++) {
message.peer_id = p.toPrecision(19).toString();
message.info_hash = Math.floor(swarmsCount * Math.random())
Expand All @@ -57,7 +57,7 @@ for (let p = 0; p < peersCount; p++) {
sendMessage: () => p,
};
tracker.processMessage(message, peer);
peers.push(peer);
sockets.push(peer);
}

let peersCountAfter = 0;
Expand All @@ -73,11 +73,11 @@ console.log(

console.log("\nremoving peers");

for (const peer of peers) {
for (const peer of sockets) {
tracker.disconnectPeersFromSocket(peer);
}

peers.length = 0;
sockets.length = 0;

if (global.gc) {
global.gc();
Expand Down
4 changes: 2 additions & 2 deletions test/performance/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { FastTracker } from "../../lib/fast-tracker.js";
import { Tracker } from "../../lib/tracker.js";
import { FastTracker } from "../../src/fast-tracker.js";
import { Tracker } from "../../src/tracker.js";

function sendMessage() {}
const peersCount = 100000;
Expand Down
16 changes: 8 additions & 8 deletions test/simulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { FastTracker } from "../lib/fast-tracker.js";
import { SocketContext } from "../lib/tracker.js";
import { FastTracker } from "../src/fast-tracker.js";
import { SocketContext } from "../src/tracker.js";
import { describe, it, expect } from "vitest";

describe("simulation", () => {
Expand All @@ -28,13 +28,13 @@ describe("simulation", () => {

const tracker = new FastTracker();

const peers: SocketContext[] = [];
const sockets: SocketContext[] = [];
const peersData: Array<{ infoHash?: string; peerId: string }> = [];

for (let i = 0; i < peersCount; i++) {
peers.push({
sockets.push({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
sendMessage: (_json: object, _peer: SocketContext) => {},
sendMessage: (_json: object, _socket: SocketContext) => {},
});
peersData.push({
peerId: (i % Math.floor(peersCount * sameIdPeersRatio)).toString(),
Expand All @@ -57,8 +57,8 @@ describe("simulation", () => {
}

function doIteration() {
const peerIndex = Math.floor(Math.random() * peers.length);
const peer = peers[peerIndex];
const peerIndex = Math.floor(Math.random() * sockets.length);
const peer = sockets[peerIndex];
const peerData = peersData[peerIndex];

if (peerData.infoHash) {
Expand All @@ -82,7 +82,7 @@ describe("simulation", () => {
// disconnect
tracker.disconnectPeersFromSocket(peer);
peerData.infoHash = undefined;
peers[peerIndex] = { sendMessage: peer.sendMessage };
sockets[peerIndex] = { sendMessage: peer.sendMessage };
return;
} else {
// announce on the same torrent
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"lib": ["ES2020"],
"moduleResolution": "NodeNext",
"declaration": true,
"outDir": "./dist",
"outDir": "./lib",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
Expand All @@ -15,5 +15,5 @@
"types": ["node"]
},
"compileOnSave": true,
"include": ["lib/**/*"]
"include": ["src/**/*"]
}
2 changes: 1 addition & 1 deletion tsconfig.lint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["lib/**/*", "test/**/*", ".eslintrc.cjs", "vitest.config.ts"]
"include": ["src/**/*", "test/**/*", ".eslintrc.cjs", "vitest.config.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"outDir": "./test_dist",
"types": ["node", "mocha"]
},
"include": ["lib/tracker.ts", "lib/fast-tracker.ts", "test/**/*"]
"include": ["src/tracker.ts", "src/fast-tracker.ts", "test/**/*"]
}
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineConfig({
test: {
include: ["test/*.{test,spec}.{js,ts,jsx,tsx}"],
coverage: {
include: ["lib/fast-tracker.ts", "lib/tracker.ts"],
include: ["src/fast-tracker.ts", "src/tracker.ts"],
},
},
});

0 comments on commit 4c9f68d

Please sign in to comment.