diff --git a/.gitignore b/.gitignore index 551b248..9cab6c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /node_modules -/dist +/lib /test_dist /coverage /.nyc_output diff --git a/README.md b/README.md index 0f1d97e..000f6f7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/wt-tracker b/bin/wt-tracker index eabc576..965a001 100755 --- a/bin/wt-tracker +++ b/bin/wt-tracker @@ -1,3 +1,3 @@ #!/usr/bin/env node -import "../dist/run-uws-tracker.js"; +import "../lib/run-uws-tracker.js"; diff --git a/package.json b/package.json index 683793b..72c5330 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/lib/fast-tracker.ts b/src/fast-tracker.ts similarity index 98% rename from lib/fast-tracker.ts rename to src/fast-tracker.ts index 6bfeedc..6c8f5c8 100644 --- a/lib/fast-tracker.ts +++ b/src/fast-tracker.ts @@ -158,7 +158,7 @@ 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; @@ -166,21 +166,21 @@ export class FastTracker implements Tracker { 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"); } diff --git a/lib/index.ts b/src/index.ts similarity index 84% rename from lib/index.ts rename to src/index.ts index c339637..b6911ca 100644 --- a/lib/index.ts +++ b/src/index.ts @@ -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, diff --git a/lib/run-uws-tracker.ts b/src/run-uws-tracker.ts similarity index 100% rename from lib/run-uws-tracker.ts rename to src/run-uws-tracker.ts diff --git a/lib/tracker.ts b/src/tracker.ts similarity index 87% rename from lib/tracker.ts rename to src/tracker.ts index 75e417c..11bf0b3 100644 --- a/lib/tracker.ts +++ b/src/tracker.ts @@ -18,11 +18,11 @@ export interface SocketContext { sendMessage: (json: object, peer: SocketContext) => void; } -export type Swarm = { +export interface Swarm { infoHash: string; completedPeers?: Set; peers: PeerContext[]; -}; +} export interface PeerContext { peerId: string; @@ -35,8 +35,8 @@ export interface PeerContext { export interface Tracker { readonly swarms: ReadonlyMap; 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 {} diff --git a/lib/uws-tracker.ts b/src/uws-tracker.ts similarity index 100% rename from lib/uws-tracker.ts rename to src/uws-tracker.ts diff --git a/test/announce.test.ts b/test/announce.test.ts index aa8c46c..8ce7dfe 100644 --- a/test/announce.test.ts +++ b/test/announce.test.ts @@ -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(); diff --git a/test/memory/heap-usage.ts b/test/memory/heap-usage.ts index 9886be1..d8fea45 100644 --- a/test/memory/heap-usage.ts +++ b/test/memory/heap-usage.ts @@ -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; @@ -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()) @@ -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; @@ -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(); diff --git a/test/performance/announce.ts b/test/performance/announce.ts index ea5f1f1..2825c6f 100644 --- a/test/performance/announce.ts +++ b/test/performance/announce.ts @@ -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; diff --git a/test/simulation.test.ts b/test/simulation.test.ts index a511cdd..f3b2ed6 100644 --- a/test/simulation.test.ts +++ b/test/simulation.test.ts @@ -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", () => { @@ -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(), @@ -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) { @@ -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 diff --git a/tsconfig.json b/tsconfig.json index b995b24..91e1b66 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "lib": ["ES2020"], "moduleResolution": "NodeNext", "declaration": true, - "outDir": "./dist", + "outDir": "./lib", "strict": true, "noUnusedLocals": true, "noUnusedParameters": false, @@ -15,5 +15,5 @@ "types": ["node"] }, "compileOnSave": true, - "include": ["lib/**/*"] + "include": ["src/**/*"] } diff --git a/tsconfig.lint.json b/tsconfig.lint.json index 8fb8f07..98263ff 100644 --- a/tsconfig.lint.json +++ b/tsconfig.lint.json @@ -1,4 +1,4 @@ { "extends": "./tsconfig.json", - "include": ["lib/**/*", "test/**/*", ".eslintrc.cjs", "vitest.config.ts"] + "include": ["src/**/*", "test/**/*", ".eslintrc.cjs", "vitest.config.ts"] } diff --git a/tsconfig.test.json b/tsconfig.test.json index 2880783..f97c424 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -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/**/*"] } diff --git a/vitest.config.ts b/vitest.config.ts index 903e4f0..321e77e 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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"], }, }, });